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