Belle II Software  release-08-01-10
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/BitStream.h>
17 #include <ecl/digitization/EclConfiguration.h>
18 #include <ecl/dbobjects/ECLChannelMap.h>
19 
20 using namespace Belle2;
21 using namespace ECL;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(ECLCompressBGOverlay);
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
32 {
33  //Set module properties
34  setDescription("Compress recorded waveforms for beam background overlay in simulation.");
36  addParam("CompressionAlgorithm", m_compAlgo, "Waveform compression algorithm (0..127)", 0u);
37  addParam("WithTriggerTime", m_trgTime, "Trigger time from each create", 1u);
38  addParam("eclWaveformsName", m_eclWaveformsName, "Name of the collection with compressed waveforms", std::string(""));
39 }
40 
42 {
43 }
44 
46 {
47  m_eclDsps.registerInDataStore();
48  m_eclWaveforms.registerInDataStore(m_eclWaveformsName);
49  m_comp = selectAlgo(m_compAlgo & 0x7f);
50  if (m_comp == nullptr)
51  B2FATAL("Unknown compression algorithm: " << m_compAlgo);
52 }
53 
55 {
56  setReturnValue(0);
58 
59  // check the number of waveforms
60  if (m_eclDsps.getEntries() != ec.m_nch) return;
61 
62  // sort by cell id
63  unsigned short indx[ec.m_nch], count = 0;
64  memset(indx, 0xff, sizeof(indx));
65  for (const auto& t : m_eclDsps) indx[t.getCellId() - 1] = count++;
66 
67  // check that all crystals are here
68  for (auto t : indx) if (t >= ec.m_nch) return;
69 
70  int FitA[ec.m_nsmp]; // buffer for a waveform
71 
72  // compress waveforms
73  BitStream out(ec.m_nch * ec.m_nsmp);
74  out.putNBits((m_compAlgo & 0x7f) | (m_trgTime << 7), 8);
75  if (m_trgTime) {
76  if (!m_eclTrigs.isValid()) B2WARNING("Crate trigger times are asked to be saved but they are not provided.");
77  // check the number of trigger times
78  if (m_eclTrigs.getEntries() != ECL::ECL_CRATES) return;
79  unsigned char ttime[ECL::ECL_CRATES];
80  for (const auto& t : m_eclTrigs) {
81  int id = t.getTrigId(), time = t.getTimeTrig();
82  // unpack trigger time
83  ttime[id - 1] = (time - 2 * (time / 8)) / 2; // 0<=ttime<72
84  }
85  for (int i = 0; i < ECL::ECL_CRATES; i++) out.putNBits(ttime[i], 7);
86  }
87  for (int j = 0; j < ec.m_nch; j++) {
88  m_eclDsps[indx[j]]->getDspA(FitA);
89  m_comp->compress(out, FitA);
90  }
91  out.resize();
92 
93  ECLWaveforms* wf = new ECLWaveforms;
94  m_eclWaveforms.assign(wf);
95 
96  std::swap(out.getStore(), wf->getStore());
97  setReturnValue(1);
98 }
99 
101 {
102  if (m_comp) delete m_comp;
103 }
StoreArray< ECLDsp > m_eclDsps
recorded waveforms
StoreObjPtr< ECLWaveforms > m_eclWaveforms
compressed waveforms
void initialize() override
Initialize variables
void event() override
Compression happens here.
StoreArray< ECLTrig > m_eclTrigs
recorded trigger time and tags
void terminate() override
Cleanup variables
unsigned int m_compAlgo
Module parameters.
unsigned int m_trgTime
store or not trigger time from each crate
ECL::ECLCompress * m_comp
pointer to a compression object which do all work
std::string m_eclWaveformsName
name of background waveforms storage
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: BitStream.h:19
virtual void compress(BitStream &out, const int *adc)=0
Compress the ECL waveform.
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
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:220
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
REG_MODULE(arichBtest)
Register the Module.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
Abstract base class for different kinds of events.