Belle II Software  release-08-01-10
KLMPackerModule.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 /* Own header. */
10 #include <klm/modules/KLMPacker/KLMPackerModule.h>
11 
12 /* Basf2 headers. */
13 #include <framework/logging/Logger.h>
14 
15 using namespace Belle2;
16 
17 REG_MODULE(KLMPacker);
18 
20  Module(),
21  m_ElementNumbers(&(KLMElementNumbers::Instance()))
22 {
23  setDescription("KLM raw data packer (creates RawKLM from KLMDigits).");
25 }
26 
28 {
29 }
30 
32 {
33  m_Digits.isRequired();
34  m_EventMetaData.isRequired();
35  m_RawKLMs.registerInDataStore();
36 }
37 
39 {
40  if (!m_ElectronicsMap.isValid())
41  B2FATAL("KLM electronics map is not available.");
42 }
43 
45 {
46  /* Indices: copper, data concentrator.
47  * Coppers from 0 to 3 are BKLM;
48  * coppers from 4 to 7 are EKLM.
49  */
50  std::vector<uint32_t> dataWords[8][4];
51 
52  /* Pack KLM digits. */
53  for (const KLMDigit& digit : m_Digits) {
54  uint32_t buf[2] = {0};
55  uint16_t bword1 = 0;
56  uint16_t bword2 = 0;
57  uint16_t bword3 = 0;
58  uint16_t bword4 = 0;
59  int channel =
61  digit.getSubdetector(), digit.getSection(), digit.getSector(),
62  digit.getLayer(), digit.getPlane(), digit.getStrip());
63  const KLMElectronicsChannel* electronicsChannel =
64  m_ElectronicsMap->getElectronicsChannel(channel);
65  if (electronicsChannel == nullptr)
66  B2FATAL("Incomplete KLM electronics map.");
67  int flag;
68  if (digit.inRPC())
69  flag = 2; // RPC
70  else
71  flag = 4; // Scintillator
72  int lane = electronicsChannel->getLane();
73  int plane = electronicsChannel->getAxis();
74  int strip = electronicsChannel->getChannel();
75  formatData(flag, lane, plane, strip,
76  digit.getCharge(), digit.getCTime(), digit.getTDC(),
77  bword1, bword2, bword3, bword4);
78  buf[0] |= bword2;
79  buf[0] |= ((bword1 << 16));
80  buf[1] |= bword4;
81  buf[1] |= ((bword3 << 16));
82  if (digit.getSubdetector() == KLMElementNumbers::c_BKLM) {
83  int copper = electronicsChannel->getCopper() - BKLM_ID - 1;
84  int dataConcentrator = electronicsChannel->getSlot() - 1;
85  dataWords[copper][dataConcentrator].push_back(buf[0]);
86  dataWords[copper][dataConcentrator].push_back(buf[1]);
87  } else {
88  int copper = electronicsChannel->getCopper() - EKLM_ID - 1;
89  int dataConcentrator = electronicsChannel->getSlot() - 1;
90  dataWords[copper + 4][dataConcentrator].push_back(buf[0]);
91  dataWords[copper + 4][dataConcentrator].push_back(buf[1]);
92  }
93  }
94 
95  /* Create RawKLM objects. */
96  RawCOPPERPackerInfo packerInfo;
97  packerInfo.exp_num = m_EventMetaData->getExperiment();
98  packerInfo.run_subrun_num = (m_EventMetaData->getRun() << 8) +
99  (m_EventMetaData->getSubrun() & 0xFF);
100  packerInfo.eve_num = m_EventMetaData->getEvent();
101  packerInfo.tt_ctime = 0;
102  packerInfo.tt_utime = 0;
103  packerInfo.b2l_ctime = 0;
104  int* detectorBuf[4];
105  int nWords[4];
106  for (int i = 0; i < 8; i++) {
107  if (i < 4) // BKLM
108  packerInfo.node_id = BKLM_ID + 1 + i;
109  else // EKLM
110  packerInfo.node_id = EKLM_ID + 1 + i - 4;
111  RawKLM* rawKlm = m_RawKLMs.appendNew();
112  for (int j = 0; j < 4; j++) {
113  nWords[j] = dataWords[i][j].size();
114  detectorBuf[j] = new int[nWords[j] + 1];
115  for (int k = 0; k < nWords[j]; k++)
116  detectorBuf[j][k] = dataWords[i][j][k];
117  detectorBuf[j][nWords[j]] = 0;
118  }
119  rawKlm->PackDetectorBuf(detectorBuf[0], nWords[0] + 1,
120  detectorBuf[1], nWords[1] + 1,
121  detectorBuf[2], nWords[2] + 1,
122  detectorBuf[3], nWords[3] + 1,
123  packerInfo);
124  for (int j = 0; j < 4; j++)
125  delete[] detectorBuf[j];
126  }
127 }
128 
129 /*
130  * Data format:
131  * Word 1: Bit 0-6 -> strip number.
132  * Bit 7 -> plane number.
133  * Bit 8-12 -> lane in the data concentrator.
134  * Bit 13-15 -> data type: RPC=0x010; scintillator=0x100.
135  * Word 2: 15 bits of ctime.
136  * Word 3: 10 bits of TDC.
137  * Word 4: 12 bits of charge.
138  */
139 void KLMPackerModule::formatData(int flag, int lane, int plane, int strip, int charge, uint16_t ctime, uint16_t tdc,
140  uint16_t& bword1, uint16_t& bword2, uint16_t& bword3, uint16_t& bword4)
141 {
142  bword1 = 0;
143  bword2 = 0;
144  bword3 = 0;
145  bword4 = 0;
146  bword1 |= (strip & 0x7F);
147  bword1 |= ((plane & 1) << 7);
148  bword1 |= ((lane & 0x1F) << 8);
149  bword1 |= (flag << 13);
150  bword2 |= (ctime & 0xFFFF);
151  bword3 |= (tdc & 0x3FF);
152  bword4 |= (charge & 0xFFF);
153 }
154 
156 {
157 }
158 
160 {
161 }
162 
KLM digit (class representing a digitized hit in RPCs or scintillators).
Definition: KLMDigit.h:29
BKLM electronics channel.
int getCopper() const
Get copper.
int getChannel() const
Get channel.
KLM element numbers.
KLMChannelNumber channelNumber(int subdetector, int section, int sector, int layer, int plane, int strip) const
Get channel number.
StoreArray< KLMDigit > m_Digits
KLM digits.
void initialize() override
Initializer.
void event() override
This method is called for each event.
const KLMElementNumbers * m_ElementNumbers
Element numbers.
void endRun() override
This method is called if the current run ends.
void terminate() override
This method is called at the end of the event processing.
void formatData(int flag, int lane, int plane, int strip, int charge, uint16_t ctime, uint16_t tdc, uint16_t &bword1, uint16_t &bword2, uint16_t &bword3, uint16_t &bword4)
Creation of raw data.
void beginRun() override
Called when entering a new run.
DBObjPtr< KLMElectronicsMap > m_ElectronicsMap
Electronics map.
StoreArray< RawKLM > m_RawKLMs
Raw data.
KLMPackerModule()
Constructor.
StoreObjPtr< EventMetaData > m_EventMetaData
Event meta data.
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
@ 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
struct to contain header information used by RawCOPPERFormat::Packer()
unsigned int b2l_ctime
32bit unitx time at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user ...
unsigned int eve_num
Run # and subrun # ( 22bit )
unsigned int tt_ctime
Node ID (32bit)
unsigned int tt_utime
27bit clock ticks at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user...
unsigned int node_id
Event Number (32bit)
unsigned int run_subrun_num
Experiment number (10bit)
unsigned int exp_num
Experiment number (10bit)
void PackDetectorBuf(int *detector_buf_1st, int nwords_1st, int *detector_buf_2nd, int nwords_2nd, int *detector_buf_3rd, int nwords_3rd, int *detector_buf_4th, int nwords_4th, RawCOPPERPackerInfo rawcprpacker_info)
Packer for RawCOPPER class Pack data (format ver.
Definition: RawCOPPER.cc:183
The Raw KLM class Class for RawCOPPER class data taken by KLM.
Definition: RawKLM.h:27
REG_MODULE(arichBtest)
Register the Module.
Abstract base class for different kinds of events.