-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
executable file
·158 lines (124 loc) · 5.12 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
try:
import ROOT
print('imported ROOT from system install')
except ImportError, e:
import sys
import os
cadbrew = None
for p in os.environ['PATH'].split(':'):
pp = p.split('/')
if len(pp[-1]) == 0:
pp.pop()
if pp[-2] == 'CadfaelBrew' and pp[-1] == 'bin':
cadbrew = '/'.join(pp[:-1])
break
if cadbrew is None:
raise ImportError, e
sys.path.append(os.path.join(cadbrew, 'lib','root'))
import ROOT
print('imported ROOT from Cadfaelbrew')
import array
import math
import argparse
c = 299.792458 # speed of light in mm / ns;
m_e = 0.510998910 # electron mass in MeV
#def calc_tof(ene, dEne, tlen):
# gaminv = m_e / (m_e + ene)
# beta = math.sqrt(1. - gaminv*gaminv)
# dBeta = dEne * gaminv * gaminv * gaminv / m_e / beta
# tof = tlen / beta / c
# dTof = dEne * tof * gaminv * gaminv * gaminv / beta / beta / m_e
# return (tof, dTof, beta, dBeta)
def convert(inf):#, outf):
f = ROOT.TFile(inf)
t = f.Get("TSD")
# Create storage
anodic_t0 = ROOT.vector('double')()
anodic_t1 = ROOT.vector('double')()
anodic_t2 = ROOT.vector('double')()
anodic_t3 = ROOT.vector('double')()
anodic_t4 = ROOT.vector('double')()
cathodic_t5 = ROOT.vector('double')()
cathodic_t6 = ROOT.vector('double')()
cell_x = ROOT.vector('double')()
cell_y = ROOT.vector('double')()
caffe_category = array.array('l',[0])
n_gamma = array.array('i',[0])
n_positron = array.array('i',[0])
n_electron = array.array('i',[0])
n_alpha = array.array('i',[0])
# Set storage address
t.SetBranchAddress('timestamp.anodic_t0', anodic_t0)
t.SetBranchAddress('timestamp.anodic_t1', anodic_t1)
t.SetBranchAddress('timestamp.anodic_t2', anodic_t2)
t.SetBranchAddress('timestamp.anodic_t3', anodic_t3)
t.SetBranchAddress('timestamp.anodic_t4', anodic_t4)
t.SetBranchAddress('timestamp.cathodic_t5', cathodic_t5)
t.SetBranchAddress('timestamp.cathodic_t6', cathodic_t6)
t.SetBranchAddress('timestamp.cell_x', cell_x)
t.SetBranchAddress('timestamp.cell_y', cell_y)
t.SetBranchAddress('truth.caffe_category', caffe_category)
t.SetBranchAddress('truth.n_gamma', n_gamma)
t.SetBranchAddress('truth.n_positron', n_positron)
t.SetBranchAddress('truth.n_electron', n_electron)
t.SetBranchAddress('truth.n_alpha', n_alpha)
#np = array.array('i',[0])
#ng = array.array('i',[0])
#t.SetBranchAddress('particle.nofparticles',np)
#t.SetBranchAddress('particle.nofgammaonly',ng)
#tvx = array.array('d',[0.]) # true vertex x
#tvy = array.array('d',[0.]) # true vertex y
#tvz = array.array('d',[0.]) # true vertex z
#t.SetBranchAddress('truth.vertex_x',tvx)
#t.SetBranchAddress('truth.vertex_y',tvy)
#t.SetBranchAddress('truth.vertex_z',tvz)
entries = []
for e in xrange(t.GetEntries()):
t.GetEntry(e)
# check number of tracks is 2 and no other gammas (unassociated calorimeter hits)
#if np[0] != 2 or ng[0] != 0:
# continue
# do this regardless of event topology
entries.append(e)
#t.SetBranchAddress('particle.charge',Q)
# Create output histograms
#c_anodic_t0 = TCanvas('c_anodic_t0', 'c_anodic_t0', 600, 400)
h_anodic_t0 = ROOT.TH1F('h_anodic_t0', 'h_anodic_t0', 100, 4.76, 4.86) # Re-create timestamp histograms
# TODO: re-create fit!
# TODO: change root histogram variable names so my older C++ fit code will work with the new data
#c_anodic_t1 = ROOT.TCanvas('c_anodic_t1', 'c_anodic_t1', 600, 400)
h_anodic_t1 = ROOT.TH1F('h_anodic_t1', 'h_anodic_t1', 100, -10000 + 0.0, 100000 + 50.0)
for e in entries:
t.GetEntry(e)
#print v1[0], v2[0], v1[1], v2[1], ene[0], ene[1]
#each track starts on foil and ends on calo
for t0 in anodic_t0:
print(t0)
h_anodic_t0.Fill(t0)
for t1 in anodic_t1:
h_anodic_t1.Fill(t1)
c_anodic_t0 = ROOT.TCanvas('c_anodic_t0', 'c_anodic_t0', 200, 10, 700, 500)
h_anodic_t0.Draw();
c_anodic_t0.SaveAs('c_feast_t0.C')
c_anodic_t0.SaveAs('c_feast_t0.py')
c_anodic_t0.SaveAs('c_feast_t0.png')
c_anodic_t0.SaveAs('c_feast_t0.pdf')
del c_anodic_t0
c_anodic_t1 = ROOT.TCanvas('c_anodic_t1', 'c_anodic_t1', 200, 10, 700, 500)
h_anodic_t1.Draw();
c_anodic_t1.SaveAs('c_feast_t1.C')
c_anodic_t1.SaveAs('c_feast_t1.py') # TODO
c_anodic_t1.SaveAs('c_feast_t1.png')
c_anodic_t1.SaveAs('c_feast_t1.pdf')
del c_anodic_t1
# TODO: For some reason this code makes the file browser crash when run
# TODO: Not finished this code, moved to C++ version
def main():
parser = argparse.ArgumentParser(description='conversion')
parser.add_argument('-i','--input',required=True,help='Input directory name')
#parser.add_argument('-o','--output',required=True,help='Check if this run can begin')
args = parser.parse_args()
convert(args.input)#, args.output)
if __name__ == "__main__":
main()