Belle II Software  release-06-02-00
gainAnalysisPixelToPMT.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # convert an output root file of pixel-by-pixel gain analysis data to pmt-by-pmt format
14 #
15 # Usage: basf2 gainAnalysisPixelToPMT.py inputRoot(pixel-by-pixel result) outputRoot [maxHitTiming=-8.] [nMinEntries=100]
16 # ---------------------------------------------------------------------------------------
17 
18 import sys
19 import numpy
20 from array import array
21 from ROOT import gROOT
22 gROOT.SetBatch(True) # noqa
23 from ROOT import TFile, TTree
24 
25 
26 def gainana_pixelToPmt(inputRoot, outputRoot, maxHitTiming=-8., nMinEntries=100):
27 
28  f = TFile(inputRoot)
29  tr = f.Get("tree")
30 
31  f_out = TFile(outputRoot, "RECREATE")
32  tr_out = TTree("tree_pmt", "gain/eff. analysis output in PMT-by-PMT")
33 
34  nPmtCh = 16
35  nTopModules = 16
36  nPmtPerModule = 32
37  slotId = array('i', [0])
38  pmtId = array('i', [0])
39  nValidCh = array('i', [0])
40  meanGain = array('f', [0.])
41  stdDevGain = array('f', [0.])
42  maxMinRatio = array('f', [0.])
43  gain = array('f', nPmtCh * [0.])
44  efficiency = array('f', nPmtCh * [0.])
45 
46  tr_out.Branch('slotId', slotId, 'slotId/I')
47  tr_out.Branch('pmtId', pmtId, 'pmtId/I')
48  tr_out.Branch('nValidCh', nValidCh, 'nValidCh/I')
49  tr_out.Branch('meanGain', meanGain, 'meanGain/F')
50  tr_out.Branch('stdDevGain', stdDevGain, 'stdDevGain/F')
51  tr_out.Branch('maxMinRatio', maxMinRatio, 'maxMinRatio/F')
52  tr_out.Branch('gain', gain, 'gain[' + str(nPmtCh) + ']/F')
53  tr_out.Branch('efficiency', efficiency, 'efficiency[' + str(nPmtCh) + ']/F')
54 
55  for iSlot in range(nTopModules):
56  for iPMT in range(nPmtPerModule):
57 
58  slotId[0] = iSlot + 1
59  pmtId[0] = iPMT + 1
60 
61  cut = "slotId==" + str(slotId[0]) + " && pmtId==" + str(pmtId[0]) + \
62  " && hitTiming<(" + str(maxHitTiming) + ") && nEntries>" + str(nMinEntries)
63  tr.Draw("gain:efficiency:pmtChId", cut)
64  nValidCh[0] = tr.GetEntries(cut)
65  if nValidCh[0] < 2:
66  continue
67  elif nValidCh[0] >= nPmtCh:
68  print("ERROR : too many channels for slot" + str(slotId[0]).zfill(2) +
69  " PMT" + str(pmtId[0]).zfill(2) + "(" + str(nValidCh[0]) + ")")
70 
71  gainArray = []
72  effArray = []
73  chArray = []
74  for iCh in range(nValidCh[0]):
75  gainArray.append(tr.GetV1()[iCh])
76  effArray.append(tr.GetV2()[iCh])
77  chArray.append(int(tr.GetV3()[iCh]))
78 
79  meanGain[0] = numpy.average(gainArray)
80  stdDevGain[0] = numpy.std(gainArray)
81  maxMinRatio[0] = max(gainArray) / min(gainArray)
82  print(cut + " : nValidCh = " + str(nValidCh[0]))
83  print(" --> mean = " + str('%05.2f' % meanGain[0]) + ", StdDev = " + str('%05.2f' % stdDevGain[0]) +
84  ", max/min = " + str('%03.2f' % maxMinRatio[0]))
85 
86  for iCh in range(nPmtCh):
87  for jCh in range(nValidCh[0]):
88  if chArray[jCh] - 1 is iCh:
89  gain[iCh] = gainArray[jCh]
90  efficiency[iCh] = effArray[jCh]
91  break
92  else:
93  gain[iCh] = -1
94  efficiency[iCh] = -1
95 
96  tr_out.Fill()
97 
98  f_out.cd()
99  tr_out.Write()
100  f_out.Close()
101 
102 
103 if __name__ == '__main__':
104 
105  args = sys.argv
106  if len(args) > 4:
107  gainana_pixelToPmt(args[1], args[2], float(args[3]), int(args[4]))
108  elif len(args) > 3:
109  gainana_pixelToPmt(args[1], args[2], float(args[3]))
110  elif len(args) > 2:
111  gainana_pixelToPmt(args[1], args[2])
112  else:
113  print("usage:")
114  print(args[0] + " (root file for pixel-by-pixel gain result) (output file)" +
115  " [max. hit timing = -8 ns] [min. number of entries = 100]")