Belle II Software development
pmtPulseHeights.py
1#!/usr/bin/env python
2
3
10
11import basf2 as b2
12import sys
13from ROOT import Belle2, TFile, TCanvas, gDirectory, TH1F, gROOT
14import time
15
16# ---------------------------------------------------------------------------------------------------------
17# Extract pulse height distributions from the payload TOPCalPhotonYields and save canvases into a root file
18#
19# Usage: basf2 pmtPulseHeights.py expNo runNo [globalTag/localDB]
20# (default globalTag = data_reprocessing_prompt)
21# ---------------------------------------------------------------------------------------------------------
22
23if len(sys.argv) < 3:
24 print("usage: basf2", sys.argv[0], "expNo runNo [globalTag/localDB]")
25 sys.exit()
26
27expNo = int(sys.argv[1])
28runNo = int(sys.argv[2])
29globalTag = "data_reprocessing_prompt"
30if len(sys.argv) > 3:
31 globalTag = sys.argv[3]
32
33
34class SaveCanvases(b2.Module):
35 ''' Extract pulse height distributions from TOPCalPhotonYields and save canvases into a root file '''
36
37 def makePlot(self, h, slot, pmt):
38 '''
39 Makes a plot of pulse-height distributions of pixels of a single PMT and saves the canvas
40 :param h: a 2D histogram of pulse-height vs. pixel
41 :param slot: slot number
42 :param pmt: PMT number
43 '''
44
45 name = 's' + f'{slot:02d}' + '_pmt' + f'{pmt:02d}'
46 canvas = TCanvas(name, "slot " + str(slot) + " pmt " + str(pmt), 1000, 1000)
47 canvas.Divide(4, 4)
48 meanPH = 0
49 N = 0
50 for pmtPix in range(16):
51 col = ((pmt - 1) % 16) * 4 + pmtPix % 4
52 row = int((pmt - 1) / 16) * 4 + int(pmtPix / 4)
53 pix = row * 64 + col + 1
54 py = h.ProjectionY("px_" + str(slot) + "_" + str(pix), pix, pix)
55 py.SetTitle("PMT pixel " + str(pmtPix + 1))
56 irow = int(pmtPix / 4)
57 icol = pmtPix % 4
58 canvas.cd(icol + (3 - irow) * 4 + 1)
59 py.Draw()
60 if py.GetEntries() > 0:
61 n = py.Integral()
62 meanPH += py.GetMean() * n
63 N += n
64 canvas.Write()
65 if N > 0:
66 meanPH /= N
67 return meanPH
68
69 def initialize(self):
70 ''' initialize: implementation '''
71
72 gROOT.SetBatch(True)
73
74 db = Belle2.PyDBObj("TOPCalPhotonYields")
75 if not db:
76 return
77
78 fileName = "pmtPulseHeights-" + time.strftime("%Y-%m-%d", time.localtime(db.getTimeStamp())) + ".root"
79 f = TFile.Open(fileName, "recreate")
80
81 print("- global tag:", globalTag)
82 print("- time of measurement:",
83 time.strftime("%d %b %Y %H:%M:%S", time.localtime(db.getTimeStamp())), "(mean), ",
84 round(db.getTimeStampStd() / 3600 / 24, 2), "days (rms)")
85 oldDir = gDirectory
86 histos = []
87 for slot in range(1, 17):
88 oldDir.mkdir("slot" + f'{slot:02d}').cd()
89 h = db.getPulseHeights(slot)
90 if not h:
91 continue
92 hmean = TH1F('meanPH', 'slot ' + str(slot) + '; PMT number; mean pulse-height', 32, 0.5, 32.5)
93 for pmt in range(1, 33):
94 meanPH = self.makePlot(h, slot, pmt)
95 hmean.SetBinContent(pmt, meanPH)
96 histos.append(hmean)
97 oldDir.cd("..")
98
99 canvas = TCanvas("meanPH", "Mean pulse-heights", 1000, 1000)
100 canvas.Divide(4, 4)
101 for i, h in enumerate(histos):
102 canvas.cd(i + 1)
103 h.Draw()
104 canvas.Write()
105 f.Close()
106 print("--> canvases saved to:", fileName)
107
108
109b2.set_log_level(b2.LogLevel.ERROR)
110
111if '.txt' in globalTag:
112 b2.conditions.append_testing_payloads(globalTag)
113else:
114 b2.conditions.append_globaltag(globalTag)
115
116main = b2.create_path()
117main.add_module('EventInfoSetter', evtNumList=[1], runList=[runNo], expList=[expNo])
118main.add_module(SaveCanvases())
119b2.process(main)
Class to access a DBObjPtr from Python.
Definition: PyDBObj.h:50
def makePlot(self, h, slot, pmt)