Belle II Software  release-08-01-10
3_bunchFinderPlots.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 <header>
14  <input>EvtGenSimRec.root, EvtGenSimRec_B2Kpi.root</input>
15  <output>bunchFinder.root</output>
16  <contact>marko.staric@ijs.si</contact>
17  <description>makes validation plots for TOPBunchFinder</description>
18 </header>
19 """
20 
21 from basf2 import create_path, process, statistics, Module
22 from ROOT import Belle2
23 from ROOT import TH1F, TFile, TNamed
24 import math
25 
26 
27 class MakePlots(Module):
28  '''
29  Makes validation histograms for bunch finder
30  '''
31 
32  def set_descr(self, h, D):
33  '''
34  Sets description, check and contact to validation histogram.
35  :param h validation histogram
36  :param D description text
37  '''
38  descr = TNamed('Description', D)
39  h.GetListOfFunctions().Add(descr)
40  check = TNamed('Check', 'Must agree with reference')
41  h.GetListOfFunctions().Add(check)
42  contact = TNamed('Contact', 'marko.staric@ijs.si')
43  h.GetListOfFunctions().Add(contact)
44 
45  def initialize(self):
46  ''' Initializes the Module: book histograms and set descriptions and checks'''
47 
48 
49  self.tfiletfile = TFile.Open("bunchFinder.root", "recreate")
50 
51 
52  self.recBunchNorecBunchNo = TH1F("recBunchNo",
53  "Reconstructed relative bunch number; relative bunch number; events per bunch",
54  100, -50.0 - 0.5, 50.0 - 0.5)
55  self.set_descrset_descr(self.recBunchNorecBunchNo, 'Reconstructed bunch number relative to L1 trigger.')
56 
57 
58  self.offsetoffset = TH1F("offset",
59  "Offset to reconstructed bunch; offset [ns]; events per bin",
60  100, -1.0, 1.0)
61  self.set_descrset_descr(self.offsetoffset, 'Offset to reconstructed bunch.')
62 
63 
64  self.numTracksnumTracks = TH1F("numTracks",
65  "Number of tracks in TOP acceptance; number of tracks; entries per bin",
66  10, 0.5, 10.5)
67  self.set_descrset_descr(self.numTracksnumTracks, 'Number of tracks per event in the acceptance of TOP.')
68 
69 
70  self.usedTracksusedTracks = TH1F("usedTracks",
71  "Number of tracks used for bunch finding; number of used track; entries per bins",
72  10, 0.5, 10.5)
73  self.set_descrset_descr(self.usedTracksusedTracks, 'Number of tracks in the event actually used for finding the bunch crossing.')
74 
75 
76  self.effi_numTrackseffi_numTracks = TH1F("effi_numTracks",
77  "Efficiency vs. number of tracks; number of tracks; efficiency",
78  10, 0.5, 10.5)
79  self.set_descrset_descr(self.effi_numTrackseffi_numTracks, 'Efficieny of finding the correct bunch crossing versus '
80  'the number of tracks per event in the acceptance of TOP.')
81 
82 
83  self.effi_usedTrackseffi_usedTracks = TH1F("effi_usedTracks",
84  "Efficiency vs. number of used tracks; number of used tracks; efficiency",
85  10, 0.5, 10.5)
86  self.set_descrset_descr(self.effi_usedTrackseffi_usedTracks, 'Efficieny of finding the correct bunch crossing versus '
87  'the number of tracks per event actually used.')
88 
89 
90  self.effieffi = TH1F("effi", "Acceptance and efficiency", 2, 0.5, 2.5)
91  self.effieffi.GetXaxis().SetBinLabel(1, "acceptance")
92  self.effieffi.GetXaxis().SetBinLabel(2, "efficiency")
93  self.effieffi.GetXaxis().SetLabelSize(0.08)
94  self.effieffi.GetXaxis().SetAlphanumeric()
95  self.set_descrset_descr(self.effieffi, 'Acceptance is defined as fraction of events with at least one track crossing TOP. '
96  'Efficiency is defined as fraction of correctly reconstructed bunch crossings in accepted events.')
97 
98 
99  self.nevnev = 0
100 
101  def event(self):
102 
103  ''' Event processor: fill histograms '''
104 
105  recBunch = Belle2.PyStoreObj('TOPRecBunch')
106  if not recBunch.isValid():
107  return
108  self.nevnev += 1
109  if recBunch.isReconstructed():
110  self.recBunchNorecBunchNo.Fill(recBunch.getBunchNo())
111  self.offsetoffset.Fill(recBunch.getCurrentOffset())
112  self.numTracksnumTracks.Fill(recBunch.getNumTracks())
113  self.usedTracksusedTracks.Fill(recBunch.getUsedTracks())
114  if recBunch.getBunchNo() == recBunch.getMCBunchNo():
115  self.effi_numTrackseffi_numTracks.Fill(recBunch.getNumTracks())
116  self.effi_usedTrackseffi_usedTracks.Fill(recBunch.getUsedTracks())
117 
118  def terminate(self):
119  ''' Processes and writes histograms to file'''
120 
121  eff = self.numTracksnumTracks.GetEntries() / self.nevnev
122  err = math.sqrt(eff * (1 - eff) / self.nevnev)
123  self.effieffi.SetBinContent(1, eff)
124  self.effieffi.SetBinError(1, err)
125 
126  eff = self.effi_numTrackseffi_numTracks.GetEntries() / self.numTracksnumTracks.GetEntries()
127  err = math.sqrt(eff * (1 - eff) / self.numTracksnumTracks.GetEntries())
128  self.effieffi.SetBinContent(2, eff)
129  self.effieffi.SetBinError(2, err)
130 
131  self.numTracksnumTracks.Sumw2()
132  self.effi_numTrackseffi_numTracks.Sumw2()
133  self.effi_numTrackseffi_numTracks.Divide(self.effi_numTrackseffi_numTracks, self.numTracksnumTracks, 1, 1, "B")
134 
135  self.usedTracksusedTracks.Sumw2()
136  self.effi_usedTrackseffi_usedTracks.Sumw2()
137  self.effi_usedTrackseffi_usedTracks.Divide(self.effi_usedTrackseffi_usedTracks, self.usedTracksusedTracks, 1, 1, "B")
138 
139  self.tfiletfile.Write()
140  self.tfiletfile.Close()
141 
142 
143 # Create path
144 main = create_path()
145 
146 # Input
147 main.add_module('RootInput', inputFileNames=['../EvtGenSimRec_B2Kpi.root', '../EvtGenSimRec.root'])
148 
149 # Make plots
150 main.add_module(MakePlots())
151 
152 main.add_module('Progress')
153 # Process events
154 process(main)
155 
156 # Print call statistics
157 print(statistics)
effi_usedTracks
validation histogram
recBunchNo
validation histogram
usedTracks
validation histogram
nev
number of events with valid TOPRecBunch object pointer
effi_numTracks
validation histogram
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67