Belle II Software  release-05-02-19
TOPWaveformFeatureExtractorModule.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 
11 // Own include
12 #include <top/modules/TOPWaveformFeatureExtractor/TOPWaveformFeatureExtractorModule.h>
13 
14 // framework - DataStore
15 #include <framework/datastore/StoreArray.h>
16 
17 // framework aux
18 #include <framework/logging/Logger.h>
19 
20 // Dataobject classes
21 #include <top/dataobjects/TOPRawDigit.h>
22 #include <top/dataobjects/TOPRawWaveform.h>
23 
24 #include <top/geometry/TOPGeometryPar.h>
25 
26 using namespace std;
27 
28 namespace Belle2 {
34  using namespace TOP;
35 
36  //-----------------------------------------------------------------
37  // Register module
38  //-----------------------------------------------------------------
39 
40  REG_MODULE(TOPWaveformFeatureExtractor)
41 
42  //-----------------------------------------------------------------
43  // Implementation
44  //-----------------------------------------------------------------
45 
47 
48  {
49  // set module description (e.g. insert text)
50  setDescription("Module adds raw digits that are found in waveforms "
51  "but not already present in TOPRawDigits. "
52  "Only waveforms related to TOPRawDigits are used.");
53  setPropertyFlags(c_ParallelProcessingCertified);
54 
55  addParam("inputRawDigitsName", m_inputRawDigitsName,
56  "name of TOPRawDigit store array", string(""));
57  addParam("threshold", m_threshold,
58  "pulse height threshold [ADC counts]", 40);
59  addParam("hysteresis", m_hysteresis,
60  "threshold hysteresis [ADC counts]", 10);
61  addParam("thresholdCount", m_thresholdCount,
62  "minimal number of samples above threshold", 3);
63  addParam("setIntegral", m_setIntegral,
64  "calculate and set integral for online-extracted hits", true);
65 
66  }
67 
68  TOPWaveformFeatureExtractorModule::~TOPWaveformFeatureExtractorModule()
69  {
70  }
71 
72  void TOPWaveformFeatureExtractorModule::initialize()
73  {
74 
75  StoreArray<TOPRawDigit> rawDigits(m_inputRawDigitsName);
76  rawDigits.isRequired();
77 
78  }
79 
80  void TOPWaveformFeatureExtractorModule::beginRun()
81  {
82  }
83 
84  void TOPWaveformFeatureExtractorModule::event()
85  {
86 
87  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
88  const auto& tdc = geo->getNominalTDC();
89  int sampleDivisions = 0x1 << tdc.getSubBits();
90 
91  StoreArray<TOPRawDigit> rawDigits(m_inputRawDigitsName);
92  int initSize = rawDigits.getEntries();
93 
94  for (int i = 0; i < initSize; i++) {
95  auto& rawDigit = *rawDigits[i];
96  const auto* waveform = rawDigit.getRelated<TOPRawWaveform>();
97  if (!waveform) continue;
98  if (m_setIntegral) {
99  auto integral = waveform->getIntegral(rawDigit.getSampleRise(),
100  rawDigit.getSamplePeak(),
101  rawDigit.getSampleFall());
102  rawDigit.setIntegral(integral);
103  }
104  waveform->featureExtraction(m_threshold, m_hysteresis, m_thresholdCount);
105  const auto& features = waveform->getFeatureExtractionData();
106  int sampleRise = rawDigit.getSampleRise();
107  int sampleFall = rawDigit.getSampleFall() + 1;
108  for (const auto& feature : features) {
109 
110  // skip it, if hit already in rawDigits
111  int sRise = feature.sampleRise;
112  if (sRise >= sampleRise and sRise <= sampleFall) continue;
113  int sFall = feature.sampleFall + 1;
114  if (sFall >= sampleRise and sFall <= sampleFall) continue;
115 
116  // if not, append it
117  auto* newDigit = rawDigits.appendNew(rawDigit);
118  newDigit->setOfflineFlag();
119  newDigit->setSampleRise(feature.sampleRise);
120  newDigit->setDeltaSamplePeak(feature.samplePeak - feature.sampleRise);
121  newDigit->setDeltaSampleFall(feature.sampleFall - feature.sampleRise);
122  newDigit->setValueRise0(feature.vRise0);
123  newDigit->setValueRise1(feature.vRise1);
124  newDigit->setValueFall0(feature.vFall0);
125  newDigit->setValueFall1(feature.vFall1);
126  newDigit->setValuePeak(feature.vPeak);
127  newDigit->setIntegral(feature.integral);
128  double rawTime = newDigit->getCFDLeadingTime();
129  unsigned tfine = int(rawTime * sampleDivisions) % sampleDivisions; // TODO: <0 ?
130  newDigit->setTFine(tfine);
131  newDigit->addRelationTo(waveform);
132  }
133  }
134 
135  int finalSize = rawDigits.getEntries();
136  B2DEBUG(100, "TOPWaveformFeatureExtractor: appended " << finalSize - initSize
137  << " raw digits to initial " << initSize);
138 
139  }
140 
141 
142  void TOPWaveformFeatureExtractorModule::endRun()
143  {
144  }
145 
146  void TOPWaveformFeatureExtractorModule::terminate()
147  {
148  }
149 
150 
152 } // end Belle2 namespace
153 
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TOPRawWaveform
Class to store raw data waveforms.
Definition: TOPRawWaveform.h:34
Belle2::TOPWaveformFeatureExtractorModule
Waveform feature extractor: module adds rawDigits that are found in waveforms by feature extraction b...
Definition: TOPWaveformFeatureExtractorModule.h:38
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOPRawWaveform::getIntegral
int getIntegral(int sampleRise, int samplePeak, int sampleFall) const
Returns integral of a peak.
Definition: TOPRawWaveform.cc:21
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