Belle II Software  release-08-01-10
averageQE.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import basf2 as b2
13 import sys
14 from ROOT import Belle2
15 from ROOT import TH1F, TH2F, TFile, TProfile
16 
17 # --------------------------------------------------------------------
18 # A tool to calculate average quantum and collection efficiency from
19 # Nagoya QA data stored in central database, payload TOPPmtQEs
20 #
21 # usage: basf2 averageQE.py [globalTag]
22 # --------------------------------------------------------------------
23 
24 
25 class AverageQE(b2.Module):
26  ''' determination of average quantum and collection efficiency from TOPPmtQEs '''
27 
28  def initialize(self):
29  ''' run the procedure, store the results in a root file and print in xml format '''
30 
31  dbarray = Belle2.PyDBArray('TOPPmtQEs')
32  if dbarray.getEntries() == 0:
33  b2.B2ERROR("No entries in TOPPmtQEs")
34  return
35 
36  geo = Belle2.PyDBObj('TOPGeometry')
37  if not geo:
38  b2.B2ERROR("No TOPGeometry")
39  return
40 
41  Bfield_on = True # use collection efficiencies for 1.5 T
42 
43  lambdaStep = dbarray[0].getLambdaStep()
44  lambdaFirst = dbarray[0].getLambdaFirst()
45  lambdaLast = dbarray[0].getLambdaLast()
46  n = int((lambdaLast - lambdaFirst) / lambdaStep) + 1
47  print(lambdaFirst, lambdaLast, lambdaStep, n)
48  if Bfield_on:
49  Bfield = '(1.5 T)'
50  else:
51  Bfield = '(0 T)'
52 
53  pde_2d = TH2F("pde_2d", "quantum times collection efficiencies " + Bfield,
54  n, lambdaFirst - lambdaStep / 2, lambdaLast + lambdaStep / 2,
55  300, 0.0, 1.0)
56  pde_prof = TProfile("pde_prof", "quantum times collection efficiencies " + Bfield,
57  n, lambdaFirst - lambdaStep / 2, lambdaLast + lambdaStep / 2,
58  0.0, 1.0)
59  pde_nom = TH1F("pde_nom", "nominal quantum times collection efficiency (as found in DB)",
60  n, lambdaFirst - lambdaStep / 2, lambdaLast + lambdaStep / 2)
61  ce_1d = TH1F("ce_1d", "collection efficiencies " + Bfield, 100, 0.0, 1.0)
62 
63  ce = 0
64  for pmtQE in dbarray:
65  ce_1d.Fill(pmtQE.getCE(Bfield_on))
66  ce += pmtQE.getCE(Bfield_on)
67  for pmtPixel in range(1, 17):
68  for i in range(n):
69  lam = lambdaFirst + lambdaStep * i
70  effi = pmtQE.getEfficiency(pmtPixel, lam, Bfield_on)
71  pde_2d.Fill(lam, effi)
72  pde_prof.Fill(lam, effi)
73 
74  nominalQE = geo.getNominalQE()
75  for i in range(n):
76  lam = lambdaFirst + lambdaStep * i
77  effi = nominalQE.getEfficiency(lam)
78  pde_nom.SetBinContent(i+1, effi)
79 
80  ce /= dbarray.getEntries()
81  print('average CE =', ce)
82  file = TFile('averageQE.root', 'recreate')
83  pde_2d.Write()
84  pde_prof.Write()
85  pde_nom.Write()
86  ce_1d.Write()
87  file.Close()
88 
89  print()
90  if Bfield_on:
91  print('<ColEffi descr="average over all PMTs for 1.5 T">' + str(round(ce, 4)) + '</ColEffi>')
92  else:
93  print('<ColEffi descr="average over all PMTs for 0 T">' + str(round(ce, 4)) + '</ColEffi>')
94  print('<LambdaFirst unit="nm">' + str(lambdaFirst) + '</LambdaFirst>')
95  print('<LambdaStep unit="nm">' + str(lambdaStep) + '</LambdaStep>')
96  for i in range(n):
97  qe = pde_prof.GetBinContent(i+1) / ce
98  print('<Qeffi>' + str(round(qe, 3)) + '</Qeffi>')
99  print()
100 
101 
102 # Central database
103 argvs = sys.argv
104 if len(argvs) == 2:
105  b2.conditions.append_globaltag(argvs[1])
106 
107 # Create path
108 main = b2.create_path()
109 
110 # Set number of events to generate
111 eventinfosetter = b2.register_module('EventInfoSetter')
112 eventinfosetter.param({'evtNumList': [1]})
113 main.add_module(eventinfosetter)
114 
115 # Run the procedure
116 main.add_module(AverageQE())
117 
118 # Process events
119 b2.process(main)
Class to access a DB Array from Python.
Definition: PyDBArray.h:43
Class to access a DBObjPtr from Python.
Definition: PyDBObj.h:50
def initialize(self)
Definition: averageQE.py:28