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