Belle II Software development
cdc_tracking_validation.py
1
8
9# """
10# CDC tracking validation
11# """
12
13from prompt import ValidationSettings
14import sys
15import os
16import json
17
18
19settings = ValidationSettings(name='CDC Tracking',
20 description=__doc__,
21 download_files=[],
22 expert_config={'file_extension': 'png',
23 'algo_tz': 'tz2',
24 'algo_tw': 'tw0',
25 'algo_sr': 'sr0',
26 'algo_xt': 'xt0'})
27
28
29def findLastIteration(job_path, algorithm):
30 root = f"{job_path}/{algorithm}/"
31 list_dirs = [item for item in os.listdir(root) if item != 'outputdb']
32 list_dirs.sort(reverse=True)
33 print(f"The following iterations are available for {algorithm} algorithm: {list_dirs}")
34 return list_dirs[0]
35
36
37def run_validation(job_path, input_data_path, requested_iov, expert_config):
38 # job_path will be replaced with path/to/calibration_results
39 # input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
40
41 # will be included in the expert_config in the future
42 expert_config = json.loads(expert_config)
43 file_extension = expert_config['file_extension']
44 algo_tz = expert_config['algo_tz']
45 algo_tw = expert_config['algo_tw']
46 algo_sr = expert_config['algo_sr']
47 algo_xt = expert_config['algo_xt']
48
49 import os
50 import ROOT
51
52 ROOT.gROOT.SetBatch(True)
53 ROOT.gStyle.SetOptStat(0)
54
55 plot_directory = "plots"
56 if not os.path.exists(plot_directory):
57 os.makedirs(plot_directory)
58
59
60 print("**** T0 validation plots ****")
61
62 lastIt = findLastIteration(job_path, algo_tz)
63 histT0_tz = f'{job_path}/{algo_tz}/{lastIt}/algorithm_output/histT0_{algo_tz}.root'
64 f_histT0_tz = ROOT.TFile(histT0_tz)
65 print(f"Plots from {algo_tz}/{lastIt}/algorithm_output/histT0_{algo_tz}.root")
66
67 # plot total histograms of the fitted means and sigmas for each wire
68 hm_All = f_histT0_tz.Get("hm_All")
69 can1 = ROOT.TCanvas()
70 hm_All.GetXaxis().SetRangeUser(-1, 0)
71 hm_All.GetXaxis().SetTitle(r"<\Delta t> [ns]")
72 hm_All.Draw("pe")
73 can1.Draw()
74 can1.SaveAs(f"{plot_directory}/{algo_tz}_hm_All.{file_extension}")
75
76 hs_All = f_histT0_tz.Get("hs_All")
77 can2 = ROOT.TCanvas()
78 hs_All.GetXaxis().SetRangeUser(2, 8)
79 hs_All.GetXaxis().SetTitle(r"\sigma(\Delta t) [ns]")
80 hs_All.Draw("pe")
81 can2.Draw()
82 can2.SaveAs(f"{plot_directory}/{algo_tz}_hs_All.{file_extension}")
83
84 # plot total histogram of the fitted means for each wire + fit
85 hTotal = f_histT0_tz.Get("hTotal")
86 can3 = ROOT.TCanvas()
87 hTotal.GetXaxis().SetTitle(r"<\Delta t> [ns]")
88 hTotal.Draw("pe")
89 can3.Draw()
90 can3.SaveAs(f"{plot_directory}/{algo_tz}_hTotal.{file_extension}")
91
92 # plot histograms of the fitted means per layer
93 gr1 = [f_histT0_tz.Get(f'DeltaT0/lay{i}') for i in range(56)]
94 cs = [ROOT.TCanvas(f'cs{i}', '', 1200, 750) for i in range(7)]
95 for i in range(7):
96 cs[i].Divide(4, 2)
97 for j in range(7):
98 pad = [cs[j].GetPrimitive(f'cs{j}_{i + 1}') for i in range(8)]
99 cs[0].cd(j + 1)
100 for i in range(8):
101 pad[i].cd()
102 pad[i].SetGrid()
103 gid = 8 * j + i
104 if gr1[gid]:
105 gr1[gid].Draw("AP")
106 for i in range(7):
107 cs[i].Draw()
108 cs[i].SaveAs(f"{plot_directory}/{algo_tz}_layers-{i}.{file_extension}")
109
110
111 print("**** TW validation plots ****")
112
113 lastIt = findLastIteration(job_path, algo_tw)
114 histTW_tw = f'{job_path}/{algo_tw}/{lastIt}/algorithm_output/histTW_{algo_tw}.root'
115 f_histTW_tw = ROOT.TFile(histTW_tw)
116 print(f"Plots from {algo_tw}/{lastIt}/algorithm_output/histTW_{algo_tw}.root")
117
118 # plot histograms for each board
119 rangeBorad = range(1, 301)
120 board_1D = [f_histTW_tw.Get(f'h1D/board_{boardID}_1') for boardID in rangeBorad]
121 can = [ROOT.TCanvas(f'c{c}', f'c{c}', 2000, 1500) for c in range(12)]
122 for c in range(12):
123 ni, nj = 5, 5
124 can[c].Divide(ni, nj)
125 for j in range(ni * nj):
126 can[c].cd(j + 1)
127 boardNumber = j + c * (ni * nj)
128 if board_1D[boardNumber]:
129 board_1D[boardNumber].SetMarkerStyle(2)
130 board_1D[boardNumber].SetTitle(f'Board number {boardNumber}')
131 board_1D[boardNumber].GetXaxis().SetTitle("ADC count")
132 board_1D[boardNumber].GetYaxis().SetTitle(r"$\Delta t$ [ns]")
133 board_1D[boardNumber].Draw()
134 can[c].Draw()
135 can[c].SaveAs(f"{plot_directory}/{algo_tw}_boards-{c}.{file_extension}")
136
137
138 print("**** sigma res validation plots ****")
139
140 lastIt = findLastIteration(job_path, algo_sr)
141 histsr_sr = f'{job_path}/{algo_sr}/{lastIt}/algorithm_output/histSigma_{algo_sr}.root'
142 f_histsr_sr = ROOT.TFile(histsr_sr)
143 print(f"Plots from {algo_sr}/{lastIt}/algorithm_output/histSigma_{algo_sr}.root")
144
145 alpha = 4
146 theta = 3
147 for LR in [0, 1]:
148 histograms = [f_histsr_sr.Get(f'lay_{ilay}/sigma2_lay{ilay}_lr{LR}_al{alpha}_th{theta}') for ilay in range(56)]
149 ncols = 5
150 count_h = 0
151 for h in histograms:
152 if h:
153 count_h += 1
154 div, mod = count_h // ncols, count_h % ncols
155 nrows = div
156 if mod != 0:
157 nrows += 1
158
159 print(f"number of valid histograms = {count_h} => canvas layout = ({ncols}, {nrows})")
160
161 c2 = ROOT.TCanvas('c2', '', ncols * 700, nrows * 400) # ncols * 700, nrows * 400)
162 c2.Divide(ncols, nrows)
163 j = 0
164 for h in histograms:
165 if h:
166 j = j + 1
167 c2.cd(j)
168 h.GetXaxis().SetTitle("drift length [cm]")
169 h.GetYaxis().SetTitle("#sigma_{r}^{2} = #sigma_{u}.#sigma_{d}")
170 h.Draw("AP")
171 h.Draw()
172 c2.Draw()
173 c2.SaveAs(f"{plot_directory}/{algo_sr}_lr{LR}_al{alpha}_th{theta}.{file_extension}")
174
175
176 print("**** XT validation plots ****")
177
178 lastIt = findLastIteration(job_path, algo_xt)
179 histXT_xt = f'{job_path}/{algo_xt}/{lastIt}/algorithm_output/histXT_{algo_xt}.root'
180 f_histXT_xt = ROOT.TFile(histXT_xt)
181 print(f"Plots from {algo_xt}/{lastIt}/algorithm_output/histXT_{algo_xt}.root")
182
183 # get histograms for all layers for a specific incident angles
184 thetaID = 1
185 alphaID = 3
186 for LR in [0, 1]:
187 histograms = [f_histXT_xt.Get(f'lay_{layer}/m_hProf{layer}_{LR}_{alphaID}_{thetaID}') for layer in range(56)]
188 # get the number of plots in the canvas
189 ncols = 5
190 count_h = 0
191 for h in histograms:
192 if h:
193 count_h += 1
194 div, mod = count_h // ncols, count_h % ncols
195 nrows = div
196 if mod != 0:
197 nrows += 1
198
199 c2 = ROOT.TCanvas('c2', '', ncols * 900, nrows * 600)
200 c2.Divide(ncols, nrows)
201 j = 0
202 for histo in histograms:
203 if histo:
204 j = j + 1
205 c2.cd(j)
206 histo.GetYaxis().SetRangeUser(-1.2, 1.2)
207 histo.SetMarkerStyle(2)
208 histo.Draw()
209 c2.Draw()
210 c2.SaveAs(f"{plot_directory}/{algo_xt}_lr{LR}_al{alphaID}_th{thetaID}.{file_extension}")
211
212
213if __name__ == "__main__":
214 run_validation(*sys.argv[1:])