Belle II Software  release-06-00-14
TOPTriggerDigitizerModule.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 // Own include
9 #include <top/modules/TOPDigitizer/TOPTriggerDigitizerModule.h>
10 
11 // Hit classes
12 #include <top/dataobjects/TOPRawWaveform.h>
13 #include <top/dataobjects/TOPTriggerDigit.h>
14 #include <top/dataobjects/TOPTriggerMCInfo.h>
15 
16 // framework - DataStore
17 #include <framework/datastore/StoreArray.h>
18 #include <framework/datastore/StoreObjPtr.h>
19 
20 // framework aux
21 #include <framework/logging/Logger.h>
22 
23 // ROOT
24 #include <TRandom.h>
25 
26 
27 using namespace std;
28 
29 namespace Belle2 {
35  //-----------------------------------------------------------------
36  // Register the Module
37  //-----------------------------------------------------------------
38 
39  REG_MODULE(TOPTriggerDigitizer)
40 
41 
42  //-----------------------------------------------------------------
43  // Implementation
44  //-----------------------------------------------------------------
45 
47  {
48  // Set description()
49  setDescription("Digitizer that provides time stamps for TOP trigger");
50  setPropertyFlags(c_ParallelProcessingCertified);
51 
52  // Add parameters
53  addParam("threshold", m_threshold,
54  "pulse height threshold [ADC counts]", 27);
55  addParam("hysteresis", m_hysteresis,
56  "pulse height threshold hysteresis [ADC counts]", 10);
57  addParam("gateWidth", m_gateWidth,
58  "width of discriminator gate [samples]", 8);
59  addParam("samplingPhase", m_samplingPhase,
60  "sampling phase [samples]", 7);
61  }
62 
63  TOPTriggerDigitizerModule::~TOPTriggerDigitizerModule()
64  {
65  }
66 
67  void TOPTriggerDigitizerModule::initialize()
68  {
69  // input
71  waveforms.isRequired();
72 
73  // output
75  digits.registerInDataStore();
76  digits.registerRelationTo(waveforms);
78  mcInfo.registerInDataStore();
79 
80  if (m_samplingPhase < 0 or m_samplingPhase >= c_SamplingCycle)
81  B2ERROR("samplingPhase must be positive and less than " << c_SamplingCycle);
82 
83  }
84 
85  void TOPTriggerDigitizerModule::beginRun()
86  {
87  }
88 
89  void TOPTriggerDigitizerModule::event()
90  {
91 
92  // input: simulated waveforms
94 
95  // output: time stamps for trigger input
98  mcInfo.create();
99 
100  if (waveforms.getEntries() == 0) {
101  B2ERROR("No waveforms available for digitization");
102  return;
103  }
104  unsigned revo9count = waveforms[0]->getRevo9Counter();
105  int offsetSamples = waveforms[0]->getOffsetWindows() * TOPRawWaveform::c_WindowSize +
106  (revo9count % 6) * TOPRawWaveform::c_WindowSize / 3;
107 
108  int bunchTimeStamp = int((revo9count + gRandom->Rndm()) * c_SamplingCycle / 3.0);
109  mcInfo->setBunchTimeStamp(bunchTimeStamp);
110 
111  int offset = bunchTimeStamp - offsetSamples / c_SamplingCycle;
112 
113  for (const auto& waveform : waveforms) {
114  const auto& data = waveform.getWaveform();
115  int currentThr = m_threshold;
116  bool lastState = false;
117  int gate = 0;
118  TOPTriggerDigit* digit = 0;
119  for (int i = 0; i < (int) data.size(); i++) {
120  gate--;
121  if (data[i] > currentThr) {
122  currentThr = m_threshold - m_hysteresis;
123  if (!lastState) gate = m_gateWidth;
124  lastState = true;
125  } else {
126  currentThr = m_threshold;
127  lastState = false;
128  }
129  if (i % c_SamplingCycle == m_samplingPhase and gate > 0) {
130  if (i / c_SamplingCycle == 0) continue; // first time stamp is not correct
131  if (!digit) {
132  digit = digits.appendNew(waveform.getModuleID(),
133  waveform.getChannel(),
134  waveform.getScrodID());
135  digit->addRelationTo(&waveform);
136  }
137  int timeStamp = i / c_SamplingCycle + offset;
138  while (timeStamp < 0) {timeStamp += c_Frame9Period;};
139  timeStamp = timeStamp % c_Frame9Period;
140  digit->appendTimeStamp(timeStamp);
141  }
142  }
143  }
144 
145  }
146 
147 
148  void TOPTriggerDigitizerModule::endRun()
149  {
150 
151  }
152 
153  void TOPTriggerDigitizerModule::terminate()
154  {
155 
156  }
157 
158 
160 } // end Belle2 namespace
161 
Base class for Modules.
Definition: Module.h:72
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Class to store trigger time stamps.
void appendTimeStamp(short timeStamp)
Append time stamp.
Digitizer that provides time stamps for TOP trigger.
#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.