Belle II Software development
CDCHistMaker.py
1#!/usr/bin/env python3
2
3
10'''
11Example script storing ADC, TDC and Hit distribution histograms.
12Usage :
13basf2 CDCHistMaker exp run
14'''
15import basf2 as b2
16from ROOT import Belle2
17from ROOT import TFile, TH1D, TH2D
18import argparse
19import glob
20import os
21
22
23b2.reset_database()
24b2.use_database_chain()
25b2.use_central_database("Calibration_Offline_Development", b2.LogLevel.INFO)
26
27
28nWires = [160, 160, 160, 160, 160, 160, 160, 160, # SL0
29 160, 160, 160, 160, 160, 160, # SL1
30 192, 192, 192, 192, 192, 192, # SL2
31 224, 224, 224, 224, 224, 224, # SL3
32 256, 256, 256, 256, 256, 256, # SL4
33 288, 288, 288, 288, 288, 288, # SL5
34 320, 320, 320, 320, 320, 320, # SL6
35 352, 352, 352, 352, 352, 352, # SL7
36 384, 384, 384, 384, 384, 384 # SL8
37 ]
38
39nWiresSum = []
40sum = 0
41for w in nWires:
42 sum = sum + w
43 nWiresSum.append(sum)
44
45
46def getHistID(lay, wire):
47 '''
48 Get histogram ID from (layer ID, wire ID).
49 '''
50 return nWiresSum[lay - 1] + wire if lay > 0 else wire
51
52
53histADC = []
54histTDC = []
55histADCinLayer = []
56
57for l_adc in range(56):
58 histADCinLayer.append(TH1D('h' + str(500000 + l_adc),
59 'ADC Layer' + str(l_adc),
60 400, 0.0, 400.))
61 for w in range(nWires[l_adc]):
62 hid = getHistID(l_adc, w)
63 hADC = TH1D('h' + str(100000 + hid),
64 'ADC Layer' + str(l_adc) + 'Wire' + str(w),
65 400, 0.0, 400.)
66 hTDC = TH1D('h' + str(200000 + hid),
67 'TDC Layer' + str(l_adc) + 'Wire' + str(w),
68 2000, 4000., 6000.)
69 histADC.append(hADC)
70 histTDC.append(hTDC)
71
72
73histADCTDC = [TH2D('h' + str(300000 + l_adc),
74 'ADC2TDC Layer' + str(l_adc),
75 200, 0.0, 400., 200, 4000, 6000)
76 for l_adc in range(56)]
77
78histHit = [TH1D('h' + str(400000 + l_adc),
79 'HitDist Layer' + str(l_adc),
80 400, 0.0, 400.) for l_adc in range(56)]
81
82
83class CDCHistMakerModule(b2.Module):
84
85 """
86 Class description.
87 """
88
89 def __init__(self, exp=0, run=0, dest='.'):
90 """
91 call constructor of base class, required.
92 """
93
94 super().__init__()
95
96 self.m_exp = exp
97
98 self.m_run = run
99
100 self.m_dest = dest
101 if os.path.exists(self.m_dest) is False:
102 os.mkdir(self.m_dest)
103
104 self.m_outputFile = self.m_dest + f'/dqm.{self.m_exp:0>4}.{self.m_run:0>5}.root'
105
106 def event(self):
107 """
108 reimplement b2.Module::event()
109 """
110
111 hits = Belle2.PyStoreArray('CDCHits')
112
113 for hit in hits:
114 # rawhit = hit.getRelatedTo('CDCRawHits')
115 sl = hit.getISuperLayer()
116 l_hit = hit.getILayer()
117 cl = l_hit if sl == 0 else 8 + (sl - 1) * 6 + l_hit
118 w = hit.getIWire()
119 adc = hit.getADCCount()
120 tdc = hit.getTDCCount()
121
122 # b = rawhit.getBoardId()
123 # c = rawhit.getFEChannel()
124 # B2DEBUG(99, 'sl ' + str(sl) + ' l_hit ' + str(l_hit) +
125 # ' cl ' + str(cl) + ' w ' + str(w) +
126 # ' b ' + str(b) + ' c ' + str(c))
127 hid = getHistID(cl, w)
128 histADCinLayer[cl].Fill(adc)
129 histADC[hid].Fill(adc)
130 histTDC[hid].Fill(tdc)
131 histADCTDC[cl].Fill(adc, tdc)
132 histHit[cl].Fill(w)
133
134 def terminate(self):
135 """
136 Draw histograms on canvas and save image.
137 """
138
139 TFile(self.m_outputFile, "RECREATE")
140 for h in histADC:
141 h.Write()
142 for h in histTDC:
143 h.Write()
144 for h in histADCinLayer:
145 h.Write()
146 for h in histADCTDC:
147 h.Write()
148 for h in histHit:
149 h.Write()
150
151
152def main(exp=1, run=3118, prefix='', dest=''):
153
154 # Seach dst files.
155 files = glob.glob(prefix + f'/dst.cosmic.{exp:0>4}.{run:0>5}' + '*.root')
156 # create path
157 main = b2.create_path()
158 # Input (ROOT file).
159 main.add_module('RootInput',
160 inputFileNames=files)
161
162 main.add_module(CDCHistMakerModule(exp, run, dest))
163 main.add_module('Progress')
164 # process events and print call statistics
165 b2.process(main)
166 print(b2.statistics)
167
168
169if __name__ == "__main__":
170 parser = argparse.ArgumentParser()
171
172 parser.add_argument('exp', help='Experimental number')
173 parser.add_argument('run', help='Run number')
174 args = parser.parse_args()
175
176 main(exp=args.exp, run=args.run,
177 prefix='/ghi/fs01/belle2/bdata/users/karim/data/GCR1/build-2017-08-21',
178 # dest='/ghi/fs01/belle2/bdata/group/detector/CDC/qam/GCR1/build-2017-08-21'
179 # dest='/ghi/fs01/belle2/bdata/group/detector/CDC/qam/GCR1/test'
180 dest='.' # Store current directory.
181 )
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def __init__(self, exp=0, run=0, dest='.')
Definition: CDCHistMaker.py:89
m_exp
Experimental number.
Definition: CDCHistMaker.py:96
Definition: main.py:1