forked from artlbv/HexaBoardAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readHexConvData_multi.py
executable file
·292 lines (236 loc) · 9.54 KB
/
readHexConvData_multi.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python2
import sys,csv
import numpy as np
from array import array
print("Loading ROOT")
import ROOT as rt
# global variables
nchips = 4*4
nchans = 64
nsca = 13
def getTimePos(roll):
roll_np = np.array(roll)
timepos = np.array(range(13))
# roll/shift positions
if (roll_np[0] == 1 and roll_np[12] == 1):
# 100000000001
timepos[0] = 12
for i in range(1,13): timepos[i] = i-1
else:
# 000011000000
# 1. find first sca in track
pos_trk1 = -1
for i in range(13):
if roll[i] == 1:
pos_trk1 = i
break
# 2. shift all positions
for i in range(13):
if i <= pos_trk1 + 1: timepos[i] = i + 12 - (pos_trk1 + 1)
else: timepos[i] = i - 1 - (pos_trk1 + 1)
#print roll_np, timepos
return timepos
def createTree(fname):
#foutname = fname.replace('.txt','_new.root')
foutname = fname.replace('.txt','_multi.root')
print("Storing data in " + foutname)
fout = rt.TFile( foutname, 'recreate' )
tree = rt.TTree( 'sk2cms', 'sk2cms tree' )
# variables for tree
event_b = np.array( [ 0 ] , dtype=int)
chip_b = np.array( [ 0 ] , dtype=int)
#nsca_b = np.array( [ nsca ] , dtype=int)
#nchans_b = np.array( [ nchans ] , dtype=int)
# roll position: roll[13] of 1 or 0
#roll_b = np.array(nsca * [ 0 ], dtype=int)
roll_b = array('i', nchips * nsca * [ 0 ])
ts_b = array('i', nchips * nsca * [ 0 ])
# charges
hgain_b = array('i', nchips * nsca * nchans * [ -99 ])
lgain_b = array('i', nchips * nsca * nchans * [ -99 ])
# tot
tot_fast_b = array('i', nchips * nchans * [ -99 ])
tot_slow_b = array('i', nchips * nchans * [ -99 ])
# toa
toa_rise_b = array('i', nchips * nchans * [ -99 ])
toa_fall_b = array('i', nchips * nchans * [ -99 ])
## sums
sum_lg_b = array('i', nchips * [0])
sum_hg_b = array('i', nchips * [0])
nhits_toa_b = array('i', nchips * [0])
nhits_tot_b = array('i', nchips * [0])
nhits_toa_all_b = array('i', [0])
nhits_tot_all_b = array('i', [0])
# tree branches
## sk2cms data
tree.Branch( 'event', event_b, 'event/I' )
tree.Branch( 'chip', chip_b, 'chip/I' )
tree.Branch( 'roll', roll_b, 'roll[' + str(nchips) + '][13]/I' )
tree.Branch( 'timesamp', ts_b, 'timesamp[' + str(nchips) + '][13]/I' )
tree.Branch( 'hg', hgain_b, 'hg[' + str(nchips) + '][13][64]/I' )
tree.Branch( 'lg', lgain_b, 'lg[' + str(nchips) + '][13][64]/I' )
tree.Branch( 'tot_fast', tot_fast_b, 'tot_fast[' + str(nchips) + '][64]/I' )
tree.Branch( 'tot_slow', tot_slow_b, 'tot_slow[' + str(nchips) + '][64]/I' )
tree.Branch( 'toa_rise', toa_rise_b, 'toa_rise[' + str(nchips) + '][64]/I' )
tree.Branch( 'toa_fall', toa_fall_b, 'toa_fall[' + str(nchips) + '][64]/I' )
## sums
tree.Branch( 'sum_lg', sum_lg_b, 'sum_lg[' + str(nchips) + ']/I' )
tree.Branch( 'sum_hg', sum_hg_b, 'sum_hg[' + str(nchips) + ']/I' )
tree.Branch( 'nhits_toa', nhits_toa_b, 'nhits_toa[' + str(nchips) + ']/I' )
tree.Branch( 'nhits_tot', nhits_tot_b, 'nhits_tot[' + str(nchips) + ']/I' )
tree.Branch( 'nhits_toa_all', nhits_toa_all_b, 'nhits_toa_all/I' )
tree.Branch( 'nhits_tot_all', nhits_tot_all_b, 'nhits_tot_all/I' )
event = -99
gain_type = "lg"
chan = 0
chip = -99
#skip_file = False
fin = open(fname,"read")
print("Reading file %s"%fname)
for line in fin.readlines():#[:1000]:
if "Event" in line:
# check counter
if chan != 0:
print("Channel number incorrect!!!!!")
chan = 0
event = -99
gain_type = "lg"
chan = 0
chip = -99
break
#exit(0)
# fill previous event/chip ## Reset counters
if event > 0 and chip == nchips-1:
#calc toa/tot tot hits
nhits_toa_all_b[0] = int(sum(nhits_toa_b))
nhits_tot_all_b[0] = int(sum(nhits_tot_b))
#print nhits_toa_b, nhits_tot_b
#print nhits_toa_all_b, nhits_tot_all_b
tree.Fill()
#nhits_toa_all_b = 0
#nhits_tot_all_b = 0
#print nhits_toa_b, nhits_tot_b
#if event > 100: break
header_items = line.split()
event = int(header_items[1])
chip = int(header_items[3])
# read and convert roll position
roll_bin = format(int(header_items[5],16), '#015b')
roll = array('i', [ int(roll_bin[i+2]) for i in range(nsca) ] )
# check rollmask has only two 1
if sum(roll) != 2 and chip == 0:
print("Warning! In event %i the rollmask sum is %i" %(event, sum(roll)))
print("Stopping!")
break
'''
if chip > 0:
if roll_b != roll:
print "Rollmask mistmach between chips!:", event, roll_b, roll
'''
# fill branches
event_b[0] = event
chip_b[0] = chip
for i in range(nsca): roll_b[chip*nsca + i] = roll[i]
#print roll, roll_b
# time pos
timepos = getTimePos(roll)
sca_in_ts30 = -1
sca_in_ts3 = -1
#print timepos
for i in range(nsca):
ts_b[chip*nsca + i] = timepos[i]
if timepos[i] == 3: sca_in_ts3 = i
if timepos[i] == 0: sca_in_ts0 = i
#if (chip == 0) and (event % 100 == 0): print("Event %i, chip %i" % (event, chip))
if (chip == 0) and (event % 100 == 0): print("Event %i" % (event))
#print sum_lg_b
#print nhits_toa_b, nhits_tot_b
# reset arrays
for k in [chip]:
sum_lg_b[k] = 0
sum_hg_b[k] = 0
nhits_toa_b[k] = 0
nhits_tot_b[k] = 0
for j in range(nchans):
tot_fast_b[k*nchans + j] = -99
tot_slow_b[k*nchans + j] = -99
toa_rise_b[k*nchans + j] = -99
toa_fall_b[k*nchans + j] = -99
for i in range(nsca):
hgain_b[k*nchans*nsca + i*nchans + j] = -99
lgain_b[k*nchans*nsca + i*nchans + j] = -99
else:
# check line contains data (x nsca)
items = line.split()
#if (len(items) != nsca) or (len(items) != (nsca + 2)): continue
if (len(items) != 15):
if (len(items) != 13): continue
# check there was an event header before
if event == -99:
print("No event header before data line!");
continue
# read data
#print("Reading chan %i in %s" %(chan,gain_type) )
items = [int(item) for item in line.split()]
# convert over/undershoot
for i,item in enumerate(items):
if item == 0: items[i] = 4096
elif item == 4: items[i] = 0
# have to invert channel and sca number
if gain_type == "hg":
# fill charge
for i in range(nsca): hgain_b[chip * nchans*nsca + i*nchans + nchans-1-chan] = int(items[nsca-1-i])
# fill toa/tot
if (len(items) == 15):
toa_fall_b[chip * nchans + nchans-1-chan] = items[13]
tot_slow_b[chip * nchans + nchans-1-chan] = items[14]
#sum_hg_b[chip] += sum(items)
if chan %2 == 1:
#sum_hg_b[chip] += items[nsca-1-sca_in_ts3] - items[nsca-1-sca_in_ts0]
sum_hg_b[chip] += max(items)
elif gain_type == "lg":
# fill charge
for i in range(nsca): lgain_b[chip * nchans*nsca + i*nchans + nchans-1-chan] = int(items[nsca-1-i])
# fill tot/toa
if (len(items) == 15):
toa_rise_b[chip * nchans + nchans-1-chan] = items[13]
tot_fast_b[chip * nchans + nchans-1-chan] = items[14]
#sum_lg_b[chip] += sum(items)
if chan %2 == 1:
#sum_lg_b[chip] += items[nsca-1-sca_in_ts3] - items[nsca-1-sca_in_ts0]
sum_lg_b[chip] += max(items)
if items[13] > 5: nhits_toa_b[chip] += 1
if items[14] > 5: nhits_tot_b[chip] += 1
#nhits_toa_b[chip] += 1
# switch counters
if chan == 63:
chan = 0
if gain_type == "lg": gain_type = "hg"
elif gain_type == "hg": gain_type = "lg"
else:
chan += 1
# fill last event
tree.Fill()
print("Tree filled")
tree.Write()
tree.Print()
fout.Close()
if __name__ == "__main__":
if len(sys.argv) == 2:
fnames = sys.argv[1:]
#fname = fnames[0]
print '# Input file is', fnames[0]
elif len(sys.argv) > 2:
fnames = sys.argv[1:]
#fname = fnames[0] + "all.txt"
print("#input file %i"%len(fnames))
else:
print("No input files given!")
#exit(0)
#fname = "/Users/artur/Dropbox/Work/LLR/HGCAL/SK2cms/hexaboard/fromDocDB/RUN_170317_0912.txt"
fnames = ["/Users/artur/Downloads/Hexaboard_data/RUN_290317_1109.txt"]
print("Using " + fname[0])
for fname in fnames:
if "raw.txt" in fname: continue
print fname
createTree(fname)