Belle II Software  release-08-00-04
v0ValidationCreatePlots.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 <header>
13  <contact>software-tracking@belle2.org</contact>
14  <input>V0ValidationHarvested.root</input>
15  <description>This module creates efficiency plots for the V0 validation.</description>
16 </header>
17 """
18 
19 
20 import numpy
21 import ROOT
22 
23 
25 
26  """Reads the output created by the V0Harvester and creates plots from it."""
27 
28  def __init__(self, input_file='../V0ValidationHarvested.root', output_file='V0Validation.root'):
29  """Reads the output created by the V0Harvester defines histograms which will be filled later.
30 
31  :param input_file: Output of V0ValidationHarvester.
32  :param output_file: Plots displayed in the V0Validation.
33  """
34 
35  self.input_fileinput_file = input_file
36 
37  self.output_fileoutput_file = output_file
38 
39 
40  self.hist_rhist_r = ROOT.TH1F("", "True R", 20, 0, 20)
41 
42  self.hist_thetahist_theta = ROOT.TH1F("", "True Theta", 26, 20, 150)
43 
44  self.hist_phihist_phi = ROOT.TH1F("", "True Phi", 36, -180, 180)
45 
46  self.hist_phist_p = ROOT.TH1F("", "True P", 25, 0.0, 1.0)
47 
48 
49  self.hist_r_foundhist_r_found = ROOT.TH1F("", "Found R", 20, 0, 20)
50 
51  self.hist_theta_foundhist_theta_found = ROOT.TH1F("", "Found Theta", 26, 20, 150)
52 
53  self.hist_phi_foundhist_phi_found = ROOT.TH1F("", "Found Phi", 36, -180, 180)
54 
55  self.hist_p_foundhist_p_found = ROOT.TH1F("", "Found P", 25, 0.0, 1.0)
56 
57 
58  self.hist_invariant_masshist_invariant_mass = ROOT.TH1F("", "", 60, 0.470, 0.530)
59 
60  self.hist_invariant_mass_reshist_invariant_mass_res = ROOT.TH1F("", "", 40, -0.02, 0.02)
61 
62 
63  self.hist_chi2hist_chi2 = ROOT.TH1F("", "", 50, 0, 50)
64 
65  self.hist_chi2_insidehist_chi2_inside = ROOT.TH1F("", "", 50, 0, 50)
66 
67  self.hist_chi2_outsidehist_chi2_outside = ROOT.TH1F("", "", 50, 0, 50)
68 
69 
70  self.hist_mass_vs_mc_masshist_mass_vs_mc_mass = ROOT.TH2F("", "", 80, 0, 0.8, 80, 0, 0.8)
71 
72 
73  self.hist_invariant_mass_residuumhist_invariant_mass_residuum = ROOT.TH1F("", "", 60, -0.05, 0.05)
74 
75  self.hist_r_residuumhist_r_residuum = ROOT.TH1F("", "", 60, -0.1, 0.1)
76 
77  self.hist_theta_residuumhist_theta_residuum = ROOT.TH1F("", "", 60, -0.1, 0.1)
78 
79  self.hist_phi_residuumhist_phi_residuum = ROOT.TH1F("", "", 60, -0.1, 0.1)
80 
81  self.hist_p_residuumhist_p_residuum = ROOT.TH1F("", "", 60, -0.05, 0.05)
82 
83  def collect_histograms(self):
84  """Fills the defined histograms with the V0Harvester data.
85 
86  :return: self
87  """
88  input_root_file = ROOT.TFile.Open(self.input_fileinput_file, "READ")
89 
90  for event in input_root_file.V0Harvester_tree:
91  self.hist_rhist_r.Fill(event.R_MC)
92  self.hist_thetahist_theta.Fill(numpy.rad2deg(event.THETA_MC))
93  self.hist_phihist_phi.Fill(numpy.rad2deg(event.PHI_MC))
94  self.hist_phist_p.Fill(event.P_MC)
95 
96  if event.FOUND:
97  self.hist_r_foundhist_r_found.Fill(event.R_MC)
98  self.hist_theta_foundhist_theta_found.Fill(numpy.rad2deg(event.THETA_MC))
99  self.hist_phi_foundhist_phi_found.Fill(numpy.rad2deg(event.PHI_MC))
100  self.hist_p_foundhist_p_found.Fill(event.P_MC)
101 
102  self.hist_invariant_masshist_invariant_mass.Fill(event.M)
103 
104  # Residuum histograms
105  self.hist_invariant_mass_residuumhist_invariant_mass_residuum.Fill(event.M - event.M_MC)
106  self.hist_r_residuumhist_r_residuum.Fill(event.R - event.R_MC)
107  self.hist_theta_residuumhist_theta_residuum.Fill(event.THETA - event.THETA_MC)
108  self.hist_phi_residuumhist_phi_residuum.Fill(event.PHI - event.PHI_MC)
109  self.hist_p_residuumhist_p_residuum.Fill(event.P - event.P_MC)
110 
111  self.hist_chi2hist_chi2.Fill(event.CHI2)
112  if event.R_MC > 1.0:
113  self.hist_chi2_outsidehist_chi2_outside.Fill(event.CHI2)
114  else:
115  assert event.R_MC <= 1.0
116  self.hist_chi2_insidehist_chi2_inside.Fill(event.CHI2)
117 
118  self.hist_mass_vs_mc_masshist_mass_vs_mc_mass.Fill(event.M, event.M_MC)
119 
120  input_root_file.Close()
121  return self
122 
123  @staticmethod
124  def efficiency_plot(found, all, title, x_variable, x_unit, description='', check='', contact='', meta_options=''):
125  """Create an efficiency plot.
126 
127  :param found: Histogram with all found entries (i.e. reconstructed).
128  :param all: Histogram with all entries (i.e. MCTruth).
129  :param title: Title of the histogram.
130  :param x_variable: x variable.
131  :param x_unit: x unit.
132  :param description: Description text shown on the validation page.
133  :param check: Check text shown on the validation page.
134  :param contact: Contact text shown on the validation page.
135  :param meta_options: Meta options for the validation page.
136  :return: ROOT.TEfficiency
137  """
138  efficiency = ROOT.TEfficiency(found, all)
139  efficiency.SetName("".join(title.split()))
140  ylabel = 'Efficiency / ({} {})'.format((found.GetXaxis().GetXmax() -
141  found.GetXaxis().GetXmin()) / found.GetNbinsX(), x_unit)
142  efficiency.SetTitle("{};{} / ({});{}".format(title, x_variable, x_unit, ylabel))
143  efficiency.GetListOfFunctions().Add(ROOT.TNamed('Description', description))
144  efficiency.GetListOfFunctions().Add(ROOT.TNamed('Check', check))
145  efficiency.GetListOfFunctions().Add(ROOT.TNamed('Contact', contact))
146  efficiency.GetListOfFunctions().Add(ROOT.TNamed('MetaOptions', meta_options))
147  return efficiency
148 
149  @staticmethod
150  def histogram_plot(hist, title, x_variable, x_unit=None, description='', check='', contact='', meta_options=''):
151  """Create (annotate) an histogram plot.
152 
153  :param hist: TH1F
154  :param title: Title of the histogram.
155  :param x_variable: x variable.
156  :param x_unit: x unit.
157  :param description: Description text shown on the validation page.
158  :param check: Check text shown on the validation page.
159  :param contact: Contact text shown on the validation page.
160  :param meta_options: Meta options for the validation page.
161  :return: modified hist
162  """
163  hist.SetName("".join(title.split()))
164  xlabel = '{} / ({})'.format(x_variable, x_unit) if x_unit is not None else '{}'.format(x_variable)
165  ylabel = 'Entries / ({} {})'.format((hist.GetXaxis().GetXmax() -
166  hist.GetXaxis().GetXmin()) /
167  hist.GetNbinsX(), x_unit) if x_unit is not None \
168  else 'Entries / ({})'.format((hist.GetXaxis().GetXmax() - hist.GetXaxis().GetXmin()) / hist.GetNbinsX())
169  hist.SetTitle("{};{};{}".format(title, xlabel, ylabel))
170  hist.GetListOfFunctions().Add(ROOT.TNamed('Description', description))
171  hist.GetListOfFunctions().Add(ROOT.TNamed('Check', check))
172  hist.GetListOfFunctions().Add(ROOT.TNamed('Contact', contact))
173  hist.GetListOfFunctions().Add(ROOT.TNamed('MetaOptions', meta_options))
174  return hist
175 
176  @staticmethod
177  def histogram_2d_plot(hist, title, x_variable, y_variable, x_unit=None, y_unit=None,
178  description='', check='', contact='', meta_options=''):
179  """Create a 2d hisogram plot.
180 
181  :param hist: TH2F
182  :param title: Title of the histogram.
183  :param x_variable: x variable
184  :param y_variable: y variable
185  :param x_unit: x unit
186  :param y_unit: y unit
187  :param description: Description text shown on the validation page.
188  :param check: Check text shown on the validation page.
189  :param contact: Contact text shown on the validation page.
190  :param meta_options: Meta options for the validation page.
191  :return: ROOT.TEfficiency
192  :return:
193  """
194  hist.SetName("".join(title.split()))
195  xlabel = '{} / ({})'.format(x_variable, x_unit) if x_unit is not None else '{}'.format(x_variable)
196  ylabel = '{} / ({})'.format(y_variable, y_unit) if y_unit is not None else '{}'.format(y_variable)
197  hist.SetTitle("{};{};{}".format(title, xlabel, ylabel))
198  hist.GetListOfFunctions().Add(ROOT.TNamed('Description', description))
199  hist.GetListOfFunctions().Add(ROOT.TNamed('Check', check))
200  hist.GetListOfFunctions().Add(ROOT.TNamed('Contact', contact))
201  hist.GetListOfFunctions().Add(ROOT.TNamed('MetaOptions', meta_options))
202  return hist
203 
204  def plot(self):
205  """Create plots with the data filled with 'collect_histograms'.
206 
207  :return: self
208  """
209  output_root_file = ROOT.TFile.Open(self.output_fileoutput_file, "RECREATE")
210 
211  V0ValidationPlots.efficiency_plot(
212  self.hist_r_foundhist_r_found,
213  self.hist_rhist_r,
214  'Efficiency vs R',
215  'r',
216  'cm',
217  description='Reconstruction Efficiency vs. r (perpendicular)',
218  check='Check that efficiencies are close to the reference. Report significant deviations.',
219  contact='software-tracking@belle2.org',
220  meta_options='shifter').Write()
221 
222  V0ValidationPlots.efficiency_plot(self.hist_theta_foundhist_theta_found, self.hist_thetahist_theta, 'Efficiency vs Theta', 'Theta', 'deg',
223  description='Reconstruction Efficiency vs. theta',
224  check='',
225  contact='software-tracking@belle2.org',
226  meta_options='').Write()
227 
228  V0ValidationPlots.efficiency_plot(self.hist_phi_foundhist_phi_found, self.hist_phihist_phi, 'Efficiency vs Phi', 'Phi', 'deg',
229  description='Reconstruction Efficiency vs phi',
230  check='',
231  contact='software-tracking@belle2.org',
232  meta_options='').Write()
233 
234  V0ValidationPlots.efficiency_plot(self.hist_p_foundhist_p_found, self.hist_phist_p, 'Efficiency vs P', 'P', 'GeV',
235  description='Reconstruction Efficiency vs momentum',
236  check='',
237  contact='software-tracking@belle2.org',
238  meta_options='').Write()
239 
240  V0ValidationPlots.histogram_plot(self.hist_invariant_masshist_invariant_mass, "KShort Invariant Mass", "m", "GeV",
241  description='Reconstructed invariant mass of KShorts with standard reconstruction',
242  check='Invariant mass peak around KShort nominal mass 497.61 MeV.',
243  contact='software-tracking@belle2.org',
244  meta_options='shifter').Write()
245 
246  V0ValidationPlots.histogram_plot(self.hist_invariant_mass_residuumhist_invariant_mass_residuum, "KShort Invariant Mass Residuum", "Rec - MC", "GeV",
247  description='Invariant mass residuum of KShorts with standard reconstruction',
248  check='',
249  contact='software-tracking@belle2.org',
250  meta_options='').Write()
251 
252  V0ValidationPlots.histogram_plot(self.hist_chi2hist_chi2, "Chi2 of Vertex Fits.", "Chi2", None,
253  description='Chi2 distributions of the vertex fits.',
254  check='Check if distribution looks like a Chi2 distribution with 1 dof',
255  contact='software-tracking@belle2.org',
256  meta_options='').Write()
257 
258  V0ValidationPlots.histogram_plot(self.hist_chi2_insidehist_chi2_inside, "Chi2 of Vertex Fits Inside Beampipe.", "Chi2", None,
259  description='Chi2 distributions of the vertex fits inside the beampipe.',
260  check='Check if distribution looks like a Chi2 distribution with 1 dof',
261  contact='software-tracking@belle2.org',
262  meta_options='').Write()
263 
264  V0ValidationPlots.histogram_plot(self.hist_chi2_outsidehist_chi2_outside, "Chi2 of Vertex Fits Outside Beampipe.", "Chi2", None,
265  description='Chi2 distributions of the vertex fits outside the beampipe.',
266  check='Check if distribution looks like a Chi2 distribution with 1 dof',
267  contact='software-tracking@belle2.org',
268  meta_options='').Write()
269 
270  V0ValidationPlots.histogram_2d_plot(self.hist_mass_vs_mc_masshist_mass_vs_mc_mass, "Reconstructed vs MC Mass.",
271  "Reconstructed Mass", "GeV", "MC Mass", "GeV",
272  description="Reconstructed mass vs invariant Mass.",
273  check="",
274  contact="software-tracking@belle2.org",
275  meta_options='').Write()
276 
277  V0ValidationPlots.histogram_plot(self.hist_r_residuumhist_r_residuum, "KShort R Residuum", "Rec - MC", "cm",
278  description='R residuum of KShorts with standard reconstruction',
279  check='',
280  contact='software-tracking@belle2.org',
281  meta_options='').Write()
282  V0ValidationPlots.histogram_plot(self.hist_theta_residuumhist_theta_residuum, "KShort Theta Residuum", "Rec - MC", "rad",
283  description='Theta residuum of KShorts with standard reconstruction',
284  check='',
285  contact='software-tracking@belle2.org',
286  meta_options='').Write()
287  V0ValidationPlots.histogram_plot(self.hist_phi_residuumhist_phi_residuum, "KShort Phi Residuum", "Rec - MC", "rad",
288  description='Phi residuum of KShorts with standard reconstruction',
289  check='',
290  contact='software-tracking@belle2.org',
291  meta_options='').Write()
292  V0ValidationPlots.histogram_plot(self.hist_p_residuumhist_p_residuum, "KShort Momentum Residuum", "Rec - MC", "GeV",
293  description='Momentum residuum of KShorts with standard reconstruction',
294  check='',
295  contact='software-tracking@belle2.org',
296  meta_options='').Write()
297 
298  output_root_file.Write()
299  output_root_file.Close()
300  return self
301 
302 
303 if __name__ == '__main__':
hist_chi2_outside
Chi2 of vertex fit outside beampipe.
hist_invariant_mass_res
Invariant mass residual histogram.
def efficiency_plot(found, all, title, x_variable, x_unit, description='', check='', contact='', meta_options='')
hist_mass_vs_mc_mass
2D histogram; invariant mass vs reconstructed mass
def histogram_plot(hist, title, x_variable, x_unit=None, description='', check='', contact='', meta_options='')
hist_chi2_inside
Chi2 of vertex fit inside beampipe.
def histogram_2d_plot(hist, title, x_variable, y_variable, x_unit=None, y_unit=None, description='', check='', contact='', meta_options='')
hist_invariant_mass_residuum
Invariant mass residuum histogram.
def __init__(self, input_file='../V0ValidationHarvested.root', output_file='V0Validation.root')
Definition: plot.py:1