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