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