Belle II Software  release-05-02-19
ecl_timing.py
1 # -*- coding: utf-8 -*-
2 
3 
10 
11 """
12 ECL Timing Validation
13 """
14 
15 from prompt import ValidationSettings
16 import sys
17 import ROOT as r
18 from pathlib import Path
19 import os
20 
21 
25 
26 
27 settings = ValidationSettings(name='ECL_timing',
28  description=__doc__,
29  download_files=['stdout'],
30  expert_config=None)
31 
32 
33 def run_validation(job_path, input_data_path, requested_iov, expert_config, **kwargs):
34  # job_path will be replaced with path/to/calibration_results
35  # input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
36 
37  # Verify that output from airflow is OK
38  from ROOT import TH1F, TCanvas, TFile, TGraph, TLine, kRed, gStyle
39  import numpy as np
40  import os
41  from array import array
42 
43  # Set root style so that there are ticks on all four sides of the plots
44  r.gStyle.SetPadTickX(1)
45  r.gStyle.SetPadTickY(1)
46 
47  # Output information about the parameters passed to the function
48  print("job_path = ", job_path)
49  print("input_data_path = ", input_data_path)
50  print("requested_iov = ", requested_iov)
51  print("expert_config = ", expert_config)
52 
53  if not os.path.exists('plots'):
54  os.makedirs('plots')
55 
56  # Add underflow, overflow and fit result
57  gStyle.SetOptStat(1111111)
58  gStyle.SetOptFit(1111)
59 
60  # ------------------------------------------------------------------------
61  # Validation histograms
62  clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal = TH1F(
63  "clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal",
64  "Bhabha: Cluster t means;Block of runs with one crystal calibration;Mean elec ECL cluster time / run block. Err = RMS",
65  30, 1, 30)
66  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal = TH1F(
67  "peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal",
68  "Bhabha: Mean(cluster t fit means);Runs of const crys calib;Mean(elec time fit mean / crys)/run block. Err=fit sigma",
69  30, 1, 30)
70 
71  clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal = TH1F(
72  "clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal",
73  "Hadron: Cluster t means;Block of runs with one crystal calibration;Mean photon ECL cluster time / run block. Err = RMS",
74  30, 1, 30)
75  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal = TH1F(
76  "peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal",
77  "Hadron: Mean(cluster t fit means);Runs of const crys calib;Mean(photon time fit mean / crys)/run block. Err=fit sigma",
78  30, 1, 30)
79 
80  myC = TCanvas("myC")
81 
82  # ------------------------------------------------------------------------
83  # bhabha self-consistency
84  print("\n---------------------------------------- \nBhabha self-consistency check: \n\n")
85  bhabhaVal_alg_output_dir = Path(job_path) / 'ECLcrystalTimeCalValidation_bhabhaPhysics/0/algorithm_output/'
86  bhabhaVal_files = sorted(bhabhaVal_alg_output_dir.glob('**/eclBhabhaTValidationAlgorithm_*.root'))
87 
88  print("List of bhabha validation files:\n")
89  print(bhabhaVal_files)
90 
91  num_files = len(bhabhaVal_files)
92  print(f'Looping over {num_files} files')
93  for count, in_file_name in enumerate(bhabhaVal_files, start=1):
94  in_file = r.TFile(str(in_file_name))
95  print("--------------------\nReading file ", in_file, ", crystal calib block # = ", count, "\n")
96 
97  inFileBaseName = str(in_file_name)
98  inFileBaseName = os.path.basename(inFileBaseName)
99  inFileBaseName = inFileBaseName[:-5]
100  print("inFileBaseName = ", inFileBaseName)
101 
102  # Read in the plots
103  peakClusterTimesGoodFit = in_file.Get("peakClusterTimesGoodFit")
104  clusterTime = in_file.Get("clusterTime")
105 
106  # Analyse the straight up cluster time histogram
107  title = "From: " + inFileBaseName
108  title = title + " : cluster time" + ", block " + str(count)
109  clusterTime.SetTitle(title)
110  clusterTime.Draw("")
111  clusterTime.Fit("gaus")
112 
113  clusterTimeFilename = str("plots/clusterTime__" + inFileBaseName + "__block" + str(count) + ".pdf")
114  print("clusterTimeFilename = ", clusterTimeFilename)
115  myC.Print(clusterTimeFilename)
116 
117  clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal.SetBinContent(count, clusterTime.GetMean())
118  clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal.SetBinError(count, clusterTime.GetStdDev())
119 
120  # Analyse the histogram of the mean of the fit to the cluster times
121  title = "From: " + inFileBaseName
122  title = title + " : cluster time fits" + ", block " + str(count)
123  peakClusterTimesGoodFit.SetTitle(title)
124  peakClusterTimesGoodFit.Draw("")
125  peakClusterTimesGoodFit.Fit("gaus")
126 
127  peakClusterTimesGoodFitFilename = str("plots/peakClusterTimesGoodFit__" + inFileBaseName + "__block" + str(count) + ".pdf")
128  print("peakClusterTimesGoodFitFilename = ", peakClusterTimesGoodFitFilename)
129  myC.Print(peakClusterTimesGoodFitFilename)
130 
131  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal.SetBinContent(count, peakClusterTimesGoodFit.GetMean())
132  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal.SetBinError(count, peakClusterTimesGoodFit.GetStdDev())
133 
134  clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal.Draw("")
135  myC.Print("plots/clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal.pdf")
136 
137  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal.Draw("")
138  myC.Print("plots/peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal.pdf")
139 
140  # ------------------------------------------------------------------------
141  # Hadron self-consistency
142  print("\n---------------------------------------- \nHadron self-consistency check: \n\n")
143  hadronVal_alg_output_dir = Path(job_path) / 'ECLcrystalTimeCalValidation_hadronPhysics/0/algorithm_output/'
144  hadronVal_files = sorted(hadronVal_alg_output_dir.glob('**/eclHadronTValidationAlgorithm_*.root'))
145 
146  print("List of hadron validation files:\n")
147  print(hadronVal_files)
148 
149  num_files = len(hadronVal_files)
150  print(f'Looping over {num_files} files')
151  for count, in_file_name in enumerate(hadronVal_files, start=1):
152  in_file = r.TFile(str(in_file_name))
153  print("--------------------\nReading file ", in_file, ", crystal calib block # = ", count, "\n")
154 
155  inFileBaseName = str(in_file_name)
156  inFileBaseName = os.path.basename(inFileBaseName)
157  inFileBaseName = inFileBaseName[:-5]
158  print("inFileBaseName = ", inFileBaseName)
159 
160  # Read in the plots
161  peakClusterTimesGoodFit = in_file.Get("peakClusterTimesGoodFit")
162  clusterTime = in_file.Get("clusterTime")
163 
164  # Analyse the straight up cluster time histogram
165  title = "From: " + inFileBaseName
166  title = title + " : cluster time" + ", block " + str(count)
167  clusterTime.SetTitle(title)
168  clusterTime.Draw("")
169  clusterTime.Fit("gaus")
170 
171  clusterTimeFilename = str("plots/clusterTime__" + inFileBaseName + "__block" + str(count) + ".pdf")
172  print("clusterTimeFilename = ", clusterTimeFilename)
173  myC.Print(clusterTimeFilename)
174 
175  clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal.SetBinContent(count, clusterTime.GetMean())
176  clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal.SetBinError(count, clusterTime.GetStdDev())
177 
178  # Analyse the histogram of the mean of the fit to the cluster times
179  title = "From: " + inFileBaseName
180  title = title + " : cluster time fits" + ", block " + str(count)
181  peakClusterTimesGoodFit.SetTitle(title)
182  peakClusterTimesGoodFit.Draw("")
183  peakClusterTimesGoodFit.Fit("gaus")
184 
185  peakClusterTimesGoodFitFilename = str("plots/peakClusterTimesGoodFit__" + inFileBaseName + "__block" + str(count) + ".pdf")
186  print("peakClusterTimesGoodFitFilename = ", peakClusterTimesGoodFitFilename)
187  myC.Print(peakClusterTimesGoodFitFilename)
188 
189  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal.SetBinContent(count, peakClusterTimesGoodFit.GetMean())
190  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal.SetBinError(count, peakClusterTimesGoodFit.GetStdDev())
191 
192  clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal.Draw("")
193  myC.Print("plots/clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal.pdf")
194 
195  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal.Draw("")
196  myC.Print("plots/peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal.pdf")
197 
198  # ------------------------------------------------------------------------
199  # Write out histograms
200  fout = TFile("ecl_timing_validateAirflow.root", "recreate")
201 
202  clusterTime_histMeanStdDev_CrystalCalibBlocksBhabhaVal.Write()
203  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksBhabhaVal.Write()
204  clusterTime_histMeanStdDev_CrystalCalibBlocksHadronVal.Write()
205  peakClusterTimesGoodFit_histMeanStdDev_CrystalCalibBlocksHadronVal.Write()
206 
207  fout.Close()
208 
209 
210 if __name__ == "__main__":
211  run_validation(*sys.argv[1:])