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