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