Belle II Software  release-08-01-10
t0CalTime.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # -------------------------------------------------------------------------------------------
13 # example of comparing time vs channel before and after t0 calibration
14 # input: "t0CalTime_slot<#>.root" from t0LaserCalData.py
15 # Usage: basf2 t0LaserCalData.py -i <TOPDigits.root> <slot#>
16 # TOPDigits.root is the output file of unpackToTOPDigitsWithTBC.py
17 # -------------------------------------------------------------------------------------------
18 
19 import basf2 as b2
20 from ROOT import Belle2
21 import ROOT
22 from ROOT import gStyle, gROOT, addressof, TH2F
23 import sys
24 
25 
26 gStyle.SetOptStat(0)
27 gROOT.ProcessLine('struct TreeStruct {\
28  int slot;\
29  int channel;\
30  int pixel;\
31  int pmt;\
32  float time;\
33  float caltime;\
34  float height;\
35  float width;\
36  int quality;\
37  float rawtime;\
38  int firstwindow;\
39  int pmtpixel;\
40 };')
41 
42 from ROOT import TreeStruct # noqa
43 
44 args = sys.argv
45 
46 
47 class Ntuple(b2.Module):
48  ''' t0const ntpule infomation '''
49 
50  global t0const
51 
52  t0const = {0: 0.0}
53 
54 
55  f = ROOT.TFile.Open('t0const_slot' + str(args[1]) + '.root')
56  for event in f.chT0:
57  t0const[event.channel] = event.t0Const
58 
59 
60  histTimeCh = TH2F('before T0Cal slot#' + str(args[1]), 'before T0Cal slot#' + str(args[1]), 512, 0, 511, 500, 50, 100)
61 
62  histCalTimeCh = TH2F('after T0Cal slot#' + str(args[1]), 'after T0Cal slot#' + str(args[1]), 512, 0, 511, 500, 50, 100)
63 
64  histTimeCh.GetXaxis().SetTitle('channel')
65  histTimeCh.GetYaxis().SetTitle('time [ns]')
66  histCalTimeCh.GetXaxis().SetTitle('channel')
67  histCalTimeCh.GetYaxis().SetTitle('time [ns]')
68 
69  def initialize(self):
70  ''' Initialize the Module: output root file '''
71 
72 
73  self.filefile = ROOT.TFile('t0CalTime_slot' + str(args[1]) + '.root', 'recreate')
74 
75  self.treetree = ROOT.TTree('laser', '')
76 
77  self.datadata = TreeStruct()
78 
79  for key in TreeStruct.__dict__.keys():
80  if '__' not in key:
81  formstring = '/F'
82  if isinstance(self.datadata.__getattribute__(key), int):
83  formstring = '/I'
84  self.treetree.Branch(key, addressof(self.datadata, key), key + formstring)
85 
86  def event(self):
87  ''' Event processor: fill the tree and scatter plots '''
88 
89  digits = Belle2.PyStoreArray('TOPDigits')
90  for digit in digits:
91  if digit.getModuleID() == int(args[1]):
92  self.datadata.slot = digit.getModuleID()
93  self.datadata.channel = digit.getChannel()
94  self.datadata.pixel = digit.getPixelID()
95  self.datadata.time = digit.getTime()
96  self.datadata.caltime = digit.getTime() + t0const.get(digit.getChannel())
97  self.datadata.height = digit.getPulseHeight()
98  self.datadata.width = digit.getPulseWidth()
99  self.datadata.quality = digit.getHitQuality()
100  self.datadata.rawtime = digit.getRawTime()
101  self.datadata.firstwindow = digit.getFirstWindow()
102  self.datadata.pmtpixel = digit.getPMTPixel()
103  self.datadata.pmt = digit.getPMTNumber()
104 
105  self.filefile.cd()
106  self.treetree.Fill()
107 
108  self.histTimeChhistTimeCh.Fill(digit.getChannel(), digit.getTime())
109  self.histCalTimeChhistCalTimeCh.Fill(digit.getChannel(), digit.getTime() - t0const.get(digit.getChannel()))
110 
111  def terminate(self):
112  ''' Write the file '''
113 
114  self.filefile.cd()
115  self.filefile.Write()
116  self.histTimeChhistTimeCh.Write()
117  self.histCalTimeChhistCalTimeCh.Write()
118  self.filefile.Close()
119 
120 
121 # Create path
122 main = b2.create_path()
123 
124 # input
125 main.add_module('RootInput')
126 
127 # write to ntuple
128 main.add_module(Ntuple())
129 
130 # Show progress of processing
131 main.add_module('Progress')
132 
133 # Process events
134 b2.process(main)
135 
136 # Print statistics
137 print(b2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def terminate(self)
Definition: t0CalTime.py:111
file
output file name
Definition: t0CalTime.py:73
data
tree strruct
Definition: t0CalTime.py:77
histCalTimeCh
scatter plot after t0 calibartion
Definition: t0CalTime.py:62
tree
output tree name
Definition: t0CalTime.py:75
histTimeCh
scatter plot before t0 calibartion
Definition: t0CalTime.py:60
def initialize(self)
Definition: t0CalTime.py:69
def event(self)
Definition: t0CalTime.py:86