-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_analyzer.py
executable file
·379 lines (327 loc) · 16.5 KB
/
serial_analyzer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/python
import os
import os.path
import math
import time
import subprocess
import array
import numpy as np
import ROOT
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
from scipy.optimize import curve_fit
# from skspatial.objects import Line, Sphere
# from skspatial.plotting import plot_3d
import argparse
parser = argparse.ArgumentParser(description='serial_analyzer.py...')
parser.add_argument('-conf', metavar='config file', required=True, help='full path to config file')
argus = parser.parse_args()
configfile = argus.conf
import config
from config import *
### must be called here (first) and only once!
init_config(configfile,True)
import utils
from utils import *
import svd_fit
from svd_fit import *
import chi2_fit
from chi2_fit import *
import hists
from hists import *
import objects
from objects import *
import pixels
from pixels import *
import clusters
from clusters import *
import truth
from truth import *
import noise
from noise import *
import candidate
from candidate import *
import hough_seeder
from hough_seeder import *
import errors
from errors import *
ROOT.gErrorIgnoreLevel = ROOT.kError
ROOT.gROOT.SetBatch(1)
ROOT.gStyle.SetOptFit(0)
# ROOT.gStyle.SetOptStat(0)
#####################################################################################
#####################################################################################
#####################################################################################
def GetTree(tfilename):
tfile = ROOT.TFile(tfilename,"READ")
ttree = None
if(not cfg["isMC"]): ttree = tfile.Get("MyTree")
else: ttree = tfile.Get("tt")
print("Events in tree:",ttree.GetEntries())
if(cfg["nmax2process"]>0): print("Will process only",cfg["nmax2process"],"events")
return tfile,ttree
#####################################################################################
#####################################################################################
#####################################################################################
def Run(tfilename,tfnoisename,tfo,histos):
### the metadata:
tfmeta = ROOT.TFile(tfilename,"READ")
tmeta = tfmeta.Get("MyTreeMeta")
runnumber = -1
starttime = -1
duration = -1
if(tmeta is not None):
try:
nmeta = tmeta.GetEntries()
tmeta.GetEntry(0)
runnumber = tmeta.run_meta_data.run_number
ts_start = tmeta.run_meta_data.run_start
starttime = get_human_timestamp(ts_start)
if(nmeta>1): tmeta.GetEntry(nmeta-1)
ts_end = tmeta.run_meta_data.run_end
endtime = get_human_timestamp(ts_end)
duration = get_run_length(ts_start,ts_end)
except:
print("Problem with Meta tree.")
runnumber = get_run_from_file(tfilename) #TODO: can also be taken from the event tree itself later
# tfmeta.Close()
### get the tree
tfile,ttree = GetTree(tfilename)
truth_tree = None
# if(cfg["isCVRroot"]):
# truth_tree = tfile.Get("MCParticle")
### get the noise masking
masked = GetNoiseMask(tfnoisename)
if(cfg["isMC"]):
for det in cfg["detectors"]:
masked.update( {det:{}} )
### get the bare pixel matrix
hPixMatix = GetPixMatrix()
nprocevents = 0
nevents = ttree.GetEntries()
for ientry,evt in enumerate(ttree):
### before anything else
if(cfg["nmax2process"]>0 and nprocevents>cfg["nmax2process"]): break
### event counter
if(nprocevents%5000==0 and nprocevents>0): print(f"processed event:{nprocevents} out of {nevents} events")
nprocevents += 1
histos["h_events"].Fill(0.5)
histos["h_cutflow"].Fill( cfg["cuts"].index("All") )
### get the trigger number
trigger_number = evt.event.trg_n
### check event errors
nerrors,errors = check_errors(evt)
if(nerrors>0):
# print(f"Skipping event {ientry} due to errors: {errors}")
wgt = 1./float(len(cfg["detectors"]))
for det in cfg["detectors"]:
for err in errors[det]:
b = ERRORS.index(err)+1
histos["h_errors"].AddBinContent(b,wgt)
histos["h_errors_"+det].AddBinContent(b)
continue
histos["h_cutflow"].Fill( cfg["cuts"].index("0Err") )
# ### truth particles
# mcparticles = {}
# if(cfg["isCVRroot"] and truth_tree is not None):
# mcparticles = get_truth_cvr(truth_tree,ientry)
# for det in cfg["detectors"]:
# xtru,ytru,ztru = getTruPos(det,mcparticles,cfg["pdgIdMatch"])
# histos["h_tru_3D"].Fill( xtru,ytru,ztru )
# histos["h_tru_occ_2D_"+det].Fill( xtru,ytru )
### get the pixels
n_active_staves, n_active_chips, pixels = get_all_pixles(evt,hPixMatix,cfg["isCVRroot"])
for det in cfg["detectors"]:
fillPixOcc(det,pixels[det],masked[det],histos) ### fill pixel occupancy
if(n_active_chips!=len(cfg["detectors"])): continue
histos["h_cutflow"].Fill( cfg["cuts"].index("N_{hits/det}>0") )
### check if there's no noise
pixels_save = {} ### to hold a copy of all pixels
for det in cfg["detectors"]:
goodpixels = getGoodPixels(det,pixels[det],masked[det],hPixMatix[det])
pixels[det] = goodpixels
pixels_save.update({det:goodpixels.copy()})
### run clustering
clusters = {}
nclusters = 0
for det in cfg["detectors"]:
det_clusters = BFS_GetAllClusters(pixels[det],det)
clusters.update( {det:det_clusters} )
fillClsHists(det,clusters[det],masked[det],histos)
if(len(det_clusters)>0): nclusters += 1
### at least one cluster per layer
if(nclusters<len(cfg["detectors"])): continue
histos["h_cutflow"].Fill( cfg["cuts"].index("N_{cls/det}>0") )
### run seeding
seeder = HoughSeeder(clusters,ientry)
seed_cuslters = seeder.seed_clusters
histos["h_nSeeds"].Fill(seeder.summary["nseeds"])
if(seeder.summary["nplanes"]<len(cfg["detectors"]) or seeder.summary["nseeds"]<1): continue ### CUT!!!
histos["h_cutflow"].Fill( cfg["cuts"].index("N_{seeds}>0") )
### run tracking
vtx = [cfg["xVtx"],cfg["yVtx"],cfg["zVtx"]] if(cfg["doVtx"]) else []
evtx = [cfg["exVtx"],cfg["eyVtx"],cfg["ezVtx"]] if(cfg["doVtx"]) else []
best_Chi2 = {}
best_value_Chi2 = +1e10
tracks = []
### loop on all cluster combinations
for i0 in range(len(seed_cuslters["ALPIDE_0"])):
for i1 in range(len(seed_cuslters["ALPIDE_1"])):
for i2 in range(len(seed_cuslters["ALPIDE_2"])):
for i3 in range(len(seed_cuslters["ALPIDE_3"])):
clsx = {"ALPIDE_3":seed_cuslters["ALPIDE_3"][i3].xmm, "ALPIDE_2":seed_cuslters["ALPIDE_2"][i2].xmm, "ALPIDE_1":seed_cuslters["ALPIDE_1"][i1].xmm, "ALPIDE_0":seed_cuslters["ALPIDE_0"][i0].xmm}
clsy = {"ALPIDE_3":seed_cuslters["ALPIDE_3"][i3].ymm, "ALPIDE_2":seed_cuslters["ALPIDE_2"][i2].ymm, "ALPIDE_1":seed_cuslters["ALPIDE_1"][i1].ymm, "ALPIDE_0":seed_cuslters["ALPIDE_0"][i0].ymm}
clsz = {"ALPIDE_3":seed_cuslters["ALPIDE_3"][i3].zmm, "ALPIDE_2":seed_cuslters["ALPIDE_2"][i2].zmm, "ALPIDE_1":seed_cuslters["ALPIDE_1"][i1].zmm, "ALPIDE_0":seed_cuslters["ALPIDE_0"][i0].zmm}
clsdx = {"ALPIDE_3":seed_cuslters["ALPIDE_3"][i3].dxmm, "ALPIDE_2":seed_cuslters["ALPIDE_2"][i2].dxmm, "ALPIDE_1":seed_cuslters["ALPIDE_1"][i1].dxmm, "ALPIDE_0":seed_cuslters["ALPIDE_0"][i0].dxmm}
clsdy = {"ALPIDE_3":seed_cuslters["ALPIDE_3"][i3].dymm, "ALPIDE_2":seed_cuslters["ALPIDE_2"][i2].dymm, "ALPIDE_1":seed_cuslters["ALPIDE_1"][i1].dymm, "ALPIDE_0":seed_cuslters["ALPIDE_0"][i0].dymm}
#############################
### to check timing #TODO ###
#############################
points_SVD,errors_SVD = SVD_candidate(clsx,clsy,clsz,clsdx,clsdy,vtx,evtx)
points_Chi2,errors_Chi2 = Chi2_candidate(clsx,clsy,clsz,clsdx,clsdy,vtx,evtx)
chisq,ndof,direction_Chi2,centroid_Chi2,params_Chi2,success_Chi2 = fit_3d_chi2err(points_Chi2,errors_Chi2)
track = Track(clusters,points_Chi2,errors_Chi2,chisq,ndof,direction_Chi2,centroid_Chi2,params_Chi2,success_Chi2)
tracks.append(track)
chi2ndof_Chi2 = chisq/ndof if(ndof>0) else 99999
if(success_Chi2 and chi2ndof_Chi2<best_value_Chi2): ### happens only when success_Chi2==True
best_value_Chi2 = chi2ndof_Chi2
best_Chi2.update( {"svd_points":points_SVD} )
best_Chi2.update( {"points":points_Chi2} )
best_Chi2.update( {"errors":errors_Chi2} )
best_Chi2.update( {"direction":direction_Chi2} )
best_Chi2.update( {"centroid":centroid_Chi2} )
best_Chi2.update( {"chi2ndof":chi2ndof_Chi2} )
best_Chi2.update( {"params":params_Chi2} )
### plot everything which is fitted but the function will only put the track line if it passes the chi2 cut
fevtdisplayname = tfilenamein.replace("tree_","event_displays/").replace(".root",f"_{ientry}.pdf")
seeder.plot_seeder(fevtdisplayname)
plot_event(runnumber,starttime,duration,ientry,fevtdisplayname,clusters,tracks,chi2threshold=cfg["cut_chi2dof"])
### fit successful
passFit = (len(best_Chi2)>0)
if(passFit):
### get the best Chi2 fit
points_SVD = best_Chi2["svd_points"]
points_Chi2 = best_Chi2["points"]
errors_Chi2 = best_Chi2["errors"]
direction_Chi2 = best_Chi2["direction"]
centroid_Chi2 = best_Chi2["centroid"]
chi2ndof_Chi2 = best_Chi2["chi2ndof"]
params_Chi2 = best_Chi2["params"]
### fill some histos
histos["h_3Dchi2err"].Fill(chi2ndof_Chi2)
histos["h_3Dchi2err_full"].Fill(chi2ndof_Chi2)
histos["h_3Dchi2err_zoom"].Fill(chi2ndof_Chi2)
histos["h_cutflow"].Fill( cfg["cuts"].index("Fitted") )
dx = direction_Chi2[0]
dy = direction_Chi2[1]
dz = direction_Chi2[2]
theta = np.arctan(np.sqrt(dx*dx+dy*dy)/dz)
phi = np.arctan(dy/dx)
histos["h_Chi2_phi"].Fill(phi)
histos["h_Chi2_theta"].Fill(theta)
if(abs(np.sin(theta))>1e-10): histos["h_Chi2_theta_weighted"].Fill( theta,abs(1/(2*np.pi*np.sin(theta))) )
# print(f"event: {ientry}, chi2ndof_Chi2={chi2ndof_Chi2}")
if(chi2ndof_Chi2<=cfg["cut_chi2dof"]): histos["h_cutflow"].Fill( cfg["cuts"].index("#chi^{2}/N_{DoF}#leqX") )
### Chi2 track to cluster residuals
fill_trk2cls_residuals(points_SVD,direction_Chi2,centroid_Chi2,"h_Chi2fit_res_trk2cls",histos)
### Chi2 track to truth residuals
if(cfg["isMC"]): fill_trk2tru_residuals(mcparticles,cfg["pdgIdMatch"],points_SVD,direction_Chi2,centroid_Chi2,"h_Chi2fit_res_trk2tru",histos)
### Chi2 fit points on laters
fillFitOcc(params_Chi2,"h_fit_occ_2D", "h_fit_3D",histos)
### Chi2 track to vertex residuals
if(cfg["doVtx"]): fill_trk2vtx_residuals(vtx,direction_Chi2,centroid_Chi2,"h_Chi2fit_res_trk2vtx",histos)
# ### fill cluster size vs true position
# if(cfg["isCVRroot"] and truth_tree is not None):
# for det in cfg["detectors"]:
# xtru,ytru,ztru = getTruPos(det,mcparticles,cfg["pdgIdMatch"])
# wgt = clusters[det][0].n
# posx = ((xtru-cfg["pix_x"]/2.)%(2*cfg["pix_x"]))
# posy = ((ytru-cfg["pix_y"]/2.)%(2*cfg["pix_y"]))
# histos["h_csize_vs_trupos"].Fill(posx,posy,wgt)
# histos["h_ntrks_vs_trupos"].Fill(posx,posy)
# histos["h_csize_vs_trupos_"+det].Fill(posx,posy,wgt)
# histos["h_ntrks_vs_trupos_"+det].Fill(posx,posy)
# ### divide into smaller sizes
# strcsize = str(wgt) if(wgt<5) else "n"
# histos["h_csize_"+strcsize+"_vs_trupos"].Fill(posx,posy,wgt)
# histos["h_ntrks_"+strcsize+"_vs_trupos"].Fill(posx,posy)
# histos["h_csize_"+strcsize+"_vs_trupos_"+det].Fill(posx,posy,wgt)
# histos["h_ntrks_"+strcsize+"_vs_trupos_"+det].Fill(posx,posy)
# if(det=="ALPIDE_0"): print("Size:",wgt,"Tru:",xtru,ytru,"Residuals:",(xtru%pix_x),(ytru%pix_y))
#######################
### post processing ###
#######################
### cluster mean size vs position
tfo.cd()
hname = "h_csize_vs_trupos"
hnewname = hname.replace("csize","mean")
hdenname = hname.replace("csize","ntrks")
histos.update( {hnewname:histos[hname].Clone(hnewname)} )
histos[hnewname].Divide(histos[hdenname])
for det in cfg["detectors"]:
tfo.cd(det)
hname = "h_csize_vs_trupos_"+det
hnewname = hname.replace("csize","mean")
hdenname = hname.replace("csize","ntrks")
histos.update( {hnewname:histos[hname].Clone(hnewname)} )
histos[hnewname].Divide(histos[hdenname])
for j in range(1,6):
tfo.cd()
strcsize = str(j) if(j<5) else "n"
hname = "h_csize_"+strcsize+"_vs_trupos"
hnewname = hname.replace("csize","mean")
hdenname = hname.replace("csize","ntrks")
histos.update( {hnewname:histos[hname].Clone(hnewname)} )
histos[hnewname].Divide(histos[hdenname])
for det in cfg["detectors"]:
tfo.cd(det)
hname = "h_csize_"+strcsize+"_vs_trupos_"+det
hnewname = hname.replace("csize","mean")
hdenname = hname.replace("csize","ntrks")
histos.update( {hnewname:histos[hname].Clone(hnewname)} )
histos[hnewname].Divide(histos[hdenname])
#############################################################################
#############################################################################
#############################################################################
if __name__ == "__main__":
### get the start time
st = time.time()
### see https://root.cern/manual/python
print("---- start loading libs")
if(os.uname()[1]=="wisett"):
print("On DAQ PC (linux): must first add DetectorEvent lib:")
print("export LD_LIBRARY_PATH=$HOME/work/eudaq/lib:$LD_LIBRARY_PATH")
ROOT.gInterpreter.AddIncludePath('../eudaq/user/stave/module/inc/')
ROOT.gInterpreter.AddIncludePath('../eudaq/user/stave/hardware/inc/')
ROOT.gSystem.Load('libeudaq_det_event_dict.so')
else:
print("On mac: must first add DetectorEvent lib:")
print("export LD_LIBRARY_PATH=$PWD/DetectorEvent/20240911:$LD_LIBRARY_PATH")
ROOT.gInterpreter.AddIncludePath('DetectorEvent/20240911/')
ROOT.gSystem.Load('libtrk_event_dict.dylib')
print("---- finish loading libs")
### make directories, copy the input file to the new basedir and return the path to it
tfilenamein = make_run_dirs(cfg["inputfile"])
# tfilenamein = cfg["inputfile"]
### noise...
tfnoisename = tfilenamein.replace(".root","_noise.root")
isnoisefile = os.path.isfile(os.path.expanduser(tfnoisename))
print("Running on:",tfilenamein)
if(not isnoisefile):
print("Noise file",tfnoisename,"not found")
print("Generate it first by running noise_analyzer.py")
quit()
tfilenameout = tfilenamein.replace(".root","_histograms.root")
tfo = ROOT.TFile(tfilenameout,"RECREATE")
tfo.cd()
histos = book_histos(tfo)
Run(tfilenamein,tfnoisename,tfo,histos)
tfo.cd()
tfo.Write()
tfo.Close()
### get the end time and the execution time
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')