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