Belle II Software  release-08-01-10
eventLevelClusteringInfo_countOutOfTime.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 Test EventLevelClusteringInfo counting of out of time ECLCalDigits.
13 
14 Creates 1 ECLCalDigit in each ECL detector region (FWD, Barrel, BWD).
15 
16 The created digits cover all possible combinations of:
17 time above threshold for counting
18 time below threshold for counting
19 energy above threshold for counting
20 energy below threshold for counting
21 
22 In order to be counted, both energy and time have to be above threshold
23 """
24 
25 import basf2 as b2
26 from ROOT import Belle2
27 from unittest import TestCase
28 import itertools
29 
30 from b2test_utils import skip_test_if_light
31 skip_test_if_light() # light builds don't contain ECLCalDigits
32 
33 b2.set_random_seed(42)
34 
35 # global variable that holds expected number of out of time digits
36 expectOutOfTime = {"FWD": 0, "BRL": 0, "BWD": 0}
37 
38 
39 class addECLCalDigitsModule(b2.Module):
40  """
41  Add combinations of ECLCalDigits above/below threshold to be counted as out of time
42  """
43 
44  def __init__(self):
45  """
46  Prepare ECLCalDigits parameters
47  """
48  super().__init__()
49 
50 
51  self.eventCountereventCounter = 0
52 
53  aboveEnergyThresh = [True, False]
54  aboveTimeThresh = [True, False]
55  thresholdNames = ["aboveEnergythresh", "aboveTimethresh"]
56 
57  thresholdsPerRegion = [dict(zip(thresholdNames, thresholds))
58  for thresholds in itertools.product(aboveEnergyThresh, aboveTimeThresh)]
59 
60  fwdThresholds, brlThresholds, bwdThresholds = itertools.tee(thresholdsPerRegion, 3)
61 
62  regions = ["FWD", "BRL", "BWD"]
63 
64 
65  self.digitParamsdigitParams = [dict(zip(regions, thresholds))
66  for thresholds in itertools.product(fwdThresholds, brlThresholds, bwdThresholds)]
67 
68 
69  self.energyThreshenergyThresh = -1
70 
71  self.timeThreshtimeThresh = -1
72 
73  def initialize(self):
74  """ module initialize - register ECLCalDigit in datastore """
75 
76 
77  self.eclCalDigitseclCalDigits = Belle2.PyStoreArray(Belle2.ECLCalDigit.Class(), "ECLCalDigits")
78  self.eclCalDigitseclCalDigits.registerInDataStore()
79 
80  def event(self):
81  """
82  Add ECLCalDigits according to self.digitParams
83  """
84  eclCalDigits = Belle2.PyStoreArray('ECLCalDigits')
85 
86  # cellIds for corresponding to different regions
87  cellId = {"FWD": 1, "BRL": 1153, "BWD": 7777}
88 
89  b2.B2DEBUG(37, "Event " + str(self.eventCountereventCounter))
90 
91  # ECLCalDigit parameter for this event
92  digitParam = self.digitParamsdigitParams[self.eventCountereventCounter]
93 
94  # Loop on detector regions
95  for region in cellId:
96 
97  # Create new ECLCalDigit
98  eclCalDigit = Belle2.ECLCalDigit()
99 
100  # Fill ECLCalDigit
101  eclCalDigit.setCellId(cellId[region])
102 
103  # Increment cellId.
104  # Important if we ever expand this test to have more than 1 digit per region.
105  cellId[region] += 1
106 
107  energy = digitParam[region]["aboveEnergythresh"] * (self.energyThreshenergyThresh + 1)
108  time = digitParam[region]["aboveTimethresh"] * (self.timeThreshtimeThresh + 1)
109  eclCalDigit.setEnergy(energy)
110  eclCalDigit.setTime(time)
111 
112  # Add ECLDigit to datastore
113  newDigit = eclCalDigits.appendNew()
114  newDigit.__assign__(eclCalDigit)
115 
116  # Set expected number of out of time calDigits per region
117  expectOutOfTime[region] = int(digitParam[region]["aboveEnergythresh"] and digitParam[region]["aboveTimethresh"])
118  b2.B2DEBUG(35, region + ": expecting " + str(expectOutOfTime[region]))
119  b2.B2DEBUG(39, "region = " + region + ", time = " + str(time) + ", energy = " + str(energy))
120 
121  # Increment event counter
122  self.eventCountereventCounter += 1
123 
124 
126  """
127  module which checks the number of out of time digits in EventLevelClusteringInfo is as expected
128  """
129 
130  def event(self):
131  """
132  event function
133  """
134  eventLevelClusteringInfo = Belle2.PyStoreObj('EventLevelClusteringInfo').obj()
135 
136  tc = TestCase('__init__')
137 
138  tc.assertEqual(eventLevelClusteringInfo.getNECLCalDigitsOutOfTimeFWD(), expectOutOfTime["FWD"])
139  tc.assertEqual(eventLevelClusteringInfo.getNECLCalDigitsOutOfTimeBarrel(), expectOutOfTime["BRL"])
140  tc.assertEqual(eventLevelClusteringInfo.getNECLCalDigitsOutOfTimeBWD(), expectOutOfTime["BWD"])
141  tc.assertEqual(eventLevelClusteringInfo.getNECLCalDigitsOutOfTime(),
142  expectOutOfTime["FWD"] + expectOutOfTime["BRL"] + expectOutOfTime["BWD"])
143 
144 
145 main = b2.create_path()
146 
147 # Create Event information
148 eventinfosetter = b2.register_module('EventInfoSetter')
149 main.add_module(eventinfosetter)
150 
151 gearbox = b2.register_module('Gearbox')
152 main.add_module(gearbox)
153 
154 geometry = b2.register_module('Geometry', useDB=False)
155 geometry.param('components', ['ECL'])
156 main.add_module(geometry)
157 
158 # Add ECLCalDigits
159 addECLCalDigits = main.add_module(addECLCalDigitsModule())
160 addECLCalDigits.logging.log_level = b2.LogLevel.DEBUG
161 addECLCalDigits.logging.debug_level = 10
162 
163 # Set number of events according to number of different combination in addECLCalDigits
164 eventinfosetter.param({'evtNumList': [len(addECLCalDigits.digitParams)], 'runList': [0]})
165 
166 ECLDigitCalibrator = b2.register_module('ECLDigitCalibrator')
167 main.add_module(ECLDigitCalibrator)
168 
169 # Extract energy and time threshold from ECLDigitCalibrator module
170 for param in ECLDigitCalibrator.available_params():
171  if param.name == "backgroundEnergyCut":
172  addECLCalDigits.energyThresh = param.values
173  elif param.name == "backgroundTimingCut":
174  addECLCalDigits.timeThresh = param.values
175 
176 main.add_module(checkNumOutOfTimeDigitsModule())
177 
178 # Process events
179 b2.process(main)
Class to store calibrated ECLDigits: ECLCalDigits.
Definition: ECLCalDigit.h:23
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
eventCounter
count number of times event method is called (each time use different combinations of ECLCalDigits