Belle II Software  release-06-00-14
ECLCompressBGOverlayModule.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 //This module
10 #include <ecl/modules/eclCompressBGOverlay/ECLCompressBGOverlayModule.h>
11 
12 //ECL
13 #include <ecl/dataobjects/ECLDsp.h>
14 #include <ecl/dataobjects/ECLTrig.h>
15 #include <ecl/dataobjects/ECLWaveforms.h>
16 #include <ecl/digitization/EclConfiguration.h>
17 #include <ecl/dbobjects/ECLChannelMap.h>
18 
19 using namespace Belle2;
20 using namespace ECL;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 REG_MODULE(ECLCompressBGOverlay)
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
31 {
32  //Set module properties
33  setDescription("Compress recorded waveforms for beam backround overlay in simulation.");
34  setPropertyFlags(c_ParallelProcessingCertified);
35  addParam("CompressionAlgorithm", m_compAlgo, "Waveform compression algorithm (0..127)", 0u);
36  addParam("WithTriggerTime", m_trgTime, "Trigger time from each create", 1u);
37  addParam("eclWaveformsName", m_eclWaveformsName, "Name of the collection with compressed waveforms", std::string(""));
38 }
39 
41 {
42 }
43 
45 {
46  m_eclDsps.registerInDataStore();
47  m_eclWaveforms.registerInDataStore(m_eclWaveformsName);
48  m_comp = selectAlgo(m_compAlgo & 0x7f);
49  if (m_comp == nullptr)
50  B2FATAL("Unknown compression algorithm: " << m_compAlgo);
51 }
52 
54 {
55  setReturnValue(0);
57 
58  // check the number of waveforms
59  if (m_eclDsps.getEntries() != ec.m_nch) return;
60 
61  // sort by cell id
62  unsigned short indx[ec.m_nch], count = 0;
63  memset(indx, 0xff, sizeof(indx));
64  for (const auto& t : m_eclDsps) indx[t.getCellId() - 1] = count++;
65 
66  // check that all crystals are here
67  for (auto t : indx) if (t >= ec.m_nch) return;
68 
69  int FitA[ec.m_nsmp]; // buffer for a waveform
70 
71  // compress waveforms
72  BitStream out(ec.m_nch * ec.m_nsmp);
73  out.putNBits((m_compAlgo & 0x7f) | (m_trgTime << 7), 8);
74  if (m_trgTime) {
75  if (!m_eclTrigs.isValid()) B2WARNING("Crate trigger times are asked to be saved but they are not provided.");
76  // check the number of trigger times
77  if (m_eclTrigs.getEntries() != ECL::ECL_CRATES) return;
78  unsigned char ttime[ECL::ECL_CRATES];
79  for (const auto& t : m_eclTrigs) {
80  int id = t.getTrigId(), time = t.getTimeTrig();
81  // unpack trigger time
82  ttime[id - 1] = (time - 2 * (time / 8)) / 2; // 0<=ttime<72
83  }
84  for (int i = 0; i < ECL::ECL_CRATES; i++) out.putNBits(ttime[i], 7);
85  }
86  for (int j = 0; j < ec.m_nch; j++) {
87  m_eclDsps[indx[j]]->getDspA(FitA);
88  m_comp->compress(out, FitA);
89  }
90  out.resize();
91 
92  ECLWaveforms* wf = new ECLWaveforms;
93  m_eclWaveforms.assign(wf);
94 
95  std::swap(out.getStore(), wf->getStore());
96  setReturnValue(1);
97 }
98 
100 {
101  if (m_comp) delete m_comp;
102 }
The ECLCompressBGOverlay module compresses recorded waveforms triggered by the random trigger and to ...
void initialize() override
Initialize variables
void event() override
Compression happens here.
void terminate() override
Cleanup variables
Class to store ECL waveforms for entire calorimeter.
Definition: ECLWaveforms.h:21
std::vector< unsigned int > & getStore()
get data
Definition: ECLWaveforms.h:27
Bit stream struct.
Definition: ECLCompress.h:28
singleton class to hold the ECL configuration
static EclConfiguration & get()
return this instance
static constexpr int m_nch
total number of electronic channels (crystals) in calorimeter
static constexpr int m_nsmp
number of ADC measurements for signal fitting
Base class for Modules.
Definition: Module.h:72
#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.