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