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