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