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