Belle II Software  release-06-02-00
TOPTimeRecalibratorModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 // Own include
10 #include <top/modules/TOPTimeRecalibrator/TOPTimeRecalibratorModule.h>
11 #include <top/geometry/TOPGeometryPar.h>
12 
13 // framework - DataStore
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/datastore/StoreObjPtr.h>
16 
17 // framework aux
18 #include <framework/logging/Logger.h>
19 
20 
21 using namespace std;
22 
23 namespace Belle2 {
29  using namespace TOP;
30 
31  //-----------------------------------------------------------------
32  // Register module
33  //-----------------------------------------------------------------
34 
35  REG_MODULE(TOPTimeRecalibrator)
36 
37  //-----------------------------------------------------------------
38  // Implementation
39  //-----------------------------------------------------------------
40 
42 
43  {
44  // set module description
45  setDescription("Utility module for re-calibrating time of TOPDigits. "
46  "Useful for checking new calibrations on existing cDST files. "
47  "Note that pulseWidth and timeError are not changed "
48  "although they also depend on time calibration.");
49  setPropertyFlags(c_ParallelProcessingCertified);
50 
51  // Add parameters
52  addParam("useSampleTimeCalibration", m_useSampleTimeCalibration,
53  "if true, use sample time calibration", true);
54  addParam("useAsicShiftCalibration", m_useAsicShiftCalibration,
55  "if true, use ASIC shifts calibration", true);
56  addParam("useChannelT0Calibration", m_useChannelT0Calibration,
57  "if true, use channel T0 calibration", true);
58  addParam("useModuleT0Calibration", m_useModuleT0Calibration,
59  "if true, use module T0 calibration", true);
60  addParam("useCommonT0Calibration", m_useCommonT0Calibration,
61  "if true, use common T0 calibration", true);
62  addParam("useTimeWalkCalibration", m_useTimeWalkCalibration,
63  "if true, use time-walk calibration", true);
64  addParam("subtractBunchTime", m_subtractBunchTime,
65  "if true, subtract reconstructed bunch time", true);
66 
67  }
68 
69  void TOPTimeRecalibratorModule::initialize()
70  {
71 
72  // registration of objects in datastore
73  m_digits.isRequired();
74  m_recBunch.isRequired();
75 
76  // equidistant sample times in case calibration is not required
77  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
78  m_syncTimeBase = geo->getNominalTDC().getSyncTimeBase();
79  m_sampleTimes.setTimeAxis(m_syncTimeBase);
80 
81  }
82 
83  void TOPTimeRecalibratorModule::beginRun()
84  {
85  StoreObjPtr<EventMetaData> evtMetaData;
86 
87  // check if calibrations are available when needed - if not, terminate
88 
89  if (m_useSampleTimeCalibration) {
90  if (not m_timebase.isValid()) {
91  B2FATAL("Sample time calibration requested but not available for run "
92  << evtMetaData->getRun()
93  << " of experiment " << evtMetaData->getExperiment());
94  }
95  }
96  if (m_useChannelT0Calibration) {
97  if (not m_channelT0.isValid()) {
98  B2FATAL("Channel T0 calibration requested but not available for run "
99  << evtMetaData->getRun()
100  << " of experiment " << evtMetaData->getExperiment());
101  }
102  }
103  if (m_useAsicShiftCalibration) {
104  if (not m_asicShift.isValid()) {
105  B2FATAL("ASIC shifts calibration requested but not available for run "
106  << evtMetaData->getRun()
107  << " of experiment " << evtMetaData->getExperiment());
108  }
109  }
110  if (m_useModuleT0Calibration) {
111  if (not m_moduleT0.isValid()) {
112  B2FATAL("Module T0 calibration requested but not available for run "
113  << evtMetaData->getRun()
114  << " of experiment " << evtMetaData->getExperiment());
115  }
116  }
117  if (m_useCommonT0Calibration) {
118  if (not m_commonT0.isValid()) {
119  B2FATAL("Common T0 calibration requested but not available for run "
120  << evtMetaData->getRun()
121  << " of experiment " << evtMetaData->getExperiment());
122  }
123  }
124  if (m_useTimeWalkCalibration) {
125  if (not m_timeWalk.isValid()) {
126  // B2FATAL("Time-walk calibration requested but not available for run "
127  B2WARNING("Time-walk calibration is not available for run "
128  << evtMetaData->getRun()
129  << " of experiment " << evtMetaData->getExperiment());
130  }
131  }
132 
133  if (not m_feSetting.isValid()) {
134  B2FATAL("Front-end settings are not available for run "
135  << evtMetaData->getRun()
136  << " of experiment " << evtMetaData->getExperiment());
137  }
138 
139  }
140 
141  void TOPTimeRecalibratorModule::event()
142  {
143  int revo9cnt = m_recBunch->getRevo9Counter();
144  double SSTfrac = (revo9cnt % 6) / 6.0;
145  double offset = m_feSetting->getOffset() / 24.0;
146  double timeOffset = (SSTfrac + offset) * m_syncTimeBase; // in [ns], to be subtracted
147  const auto& feMapper = TOPGeometryPar::Instance()->getFrontEndMapper();
148  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
149 
150  for (auto& digit : m_digits) {
151 
152  // save MC offset status
153  bool offsetStatus = digit.hasStatus(TOPDigit::c_OffsetSubtracted);
154 
155  // reset status bits
156  unsigned short statusBits = 0;
157  digit.setStatus(statusBits);
158 
159  // get what's needed from a digit
160  double rawTimeLeading = digit.getRawTime();
161  auto window = digit.getFirstWindow();
162  auto moduleID = digit.getModuleID();
163  auto channel = digit.getChannel();
164 
165  // convert raw time to time using equidistant or calibrated time base
166  const auto* sampleTimes = &m_sampleTimes; // equidistant sample times
167  if (m_useSampleTimeCalibration) {
168  auto bs = channel / 128;
169  const auto* feemap = feMapper.getMap(moduleID, bs);
170  if (not feemap) {
171  B2ERROR("No front-end map available."
172  << LogVar("slot", moduleID)
173  << LogVar("boardstack", bs));
174  continue;
175  }
176  auto scrodID = feemap->getScrodID();
177  sampleTimes = m_timebase->getSampleTimes(scrodID, channel % 128);
178  if (sampleTimes->isCalibrated()) {
179  statusBits |= TOPDigit::c_TimeBaseCalibrated;
180  }
181  }
182  double time = sampleTimes->getTime(window, rawTimeLeading) - timeOffset;
183 
184  // apply other calibrations
185  if (m_useTimeWalkCalibration and m_timeWalk.isValid()) {
186  if (m_timeWalk->isCalibrated()) {
187  time -= m_timeWalk->getTimeWalk(digit.getPulseHeight());
188  }
189  }
190  if (m_useChannelT0Calibration) {
191  const auto& cal = m_channelT0;
192  if (cal->isCalibrated(moduleID, channel)) {
193  time -= cal->getT0(moduleID, channel);
194  statusBits |= TOPDigit::c_ChannelT0Calibrated;
195  }
196  }
197  if (m_useAsicShiftCalibration) {
198  auto asic = channel / 8;
199  if (m_asicShift->isCalibrated(moduleID, asic)) {
200  time -= m_asicShift->getT0(moduleID, asic);
201  }
202  }
203  if (m_useModuleT0Calibration) {
204  const auto& cal = m_moduleT0;
205  if (cal->isCalibrated(moduleID)) {
206  time -= cal->getT0(moduleID);
207  statusBits |= TOPDigit::c_ModuleT0Calibrated;
208  }
209  }
210  if (m_useCommonT0Calibration) {
211  const auto& cal = m_commonT0;
212  if (cal->isCalibrated()) {
213  time -= cal->getT0();
214  statusBits |= TOPDigit::c_CommonT0Calibrated;
215  }
216  }
217 
218  // subtract bunch time
219  if (m_subtractBunchTime and m_recBunch->isReconstructed()) {
220  time -= m_recBunch->getTime();
221  statusBits |= TOPDigit::c_EventT0Subtracted;
222  }
223 
224  // subtract offset used in MC if status bit was set in this digit
225  if (offsetStatus) {
226  time -= geo->getNominalTDC().getOffset();
227  statusBits |= TOPDigit::c_OffsetSubtracted;
228  }
229 
230  // set re-calibrated time and status bits
231  digit.setTime(time);
232  digit.setStatus(statusBits);
233  }
234 
235  }
236 
237 
239 } // end Belle2 namespace
240 
Base class for Modules.
Definition: Module.h:72
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Utility module for re-calibrating time of TOPDigits pulseWidth and timeError are not changed although...
Class to store variables with their name which were sent to the logging service.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.