2 #include <ecl/modules/eclPacker/eclPackerModule.h>
9 #include <framework/logging/Logger.h>
12 #include <ecl/dataobjects/ECLDigit.h>
13 #include <ecl/dataobjects/ECLDsp.h>
16 #include <rawdata/dataobjects/RawECL.h>
24 ECLPackerModule::ECLPackerModule() :
28 m_EclWaveformSamples(),
40 setDescription(
"The module reads ECLDigits from the DataStore and writes ECLRaw data.");
50 ECLPackerModule::~ECLPackerModule()
71 B2FATAL(
"ECL Packer: Can't initialize eclChannelMapper!");
80 B2DEBUG(50,
"EclPacker:: event called ");
84 B2DEBUG(50,
"EclPacker:: output data arrays created");
86 int nActiveChannelsWithADCData, nActiveDSPChannels;
87 int triggerPhase = 0, dspMask = 0;
91 int nEclWaveform =
m_eclDsps.getEntries();
93 for (
int i = 0; i < ECL_CRATES; i++) {
95 for (
int j = 0; j < ECL_BARREL_SHAPERS_IN_CRATE; j++) {
103 for (
int j = 0; j < ECL_TOTAL_CHANNELS; j++) {
108 B2DEBUG(100,
"EclPacker:: N_Digits = " << nEclDigits);
109 B2DEBUG(100,
"EclPacker:: N_Waveforms = " << nEclWaveform);
111 int iCOPPER, iFINESSE, iCrate, iShaper, iChannel, nShapers;
113 B2DEBUG(100,
"EclPacker:: Hits ======>> ");
114 int tot_dsp_hits = 0;
116 for (
int i_digit = 0; i_digit < nEclDigits; i_digit++) {
127 if (iCrate < 1 && iShaper < 1 && iChannel < 1) {
128 B2ERROR(
"Wrong crate/shaper/channel ids: " << iCrate <<
" " << iShaper <<
" " << iChannel <<
" for CID " << cid);
129 throw eclPacker_internal_error();
140 B2DEBUG(100,
"ECL Packer:: N Hits above threshold = " << tot_dsp_hits <<
" nWaveforms = " << nEclWaveform);
144 B2DEBUG(100,
"ECL Packer:: Pack waveform data for this event: " <<
m_EvtNum);
145 for (
int i_wf = 0; i_wf < nEclWaveform; i_wf++) {
153 for (
int i_digit = 0; i_digit < nEclDigits; i_digit++) {
173 rawcprpacker_info.
tt_ctime = 0x7123456;
174 rawcprpacker_info.
tt_utime = 0xF1234567;
178 B2DEBUG(100,
"EclPacker:: proceed COPPERs... ");
179 B2DEBUG(100,
"EclPacker:: ECL_COPPERS = " << ECL_COPPERS);
182 for (iCOPPER = 1; iCOPPER <= ECL_COPPERS; iCOPPER++) {
184 std::vector <unsigned int> buff[ECL_FINESSES_IN_COPPER];
185 for (
int i = 0; i < ECL_FINESSES_IN_COPPER; i++) buff[i].clear();
187 int iCOPPERNode = (iCOPPER <= ECL_BARREL_COPPERS) ? BECL_ID + iCOPPER : EECL_ID + iCOPPER - ECL_BARREL_COPPERS;
192 B2DEBUG(200,
"iCOPPERNode = 0x" << std::hex << iCOPPERNode << std::dec <<
" nCrate1 = " << icr1 <<
" nCrate2 = " << icr2);
195 rawcprpacker_info.
node_id = iCOPPERNode;
198 int nwords[2] = {0, 0};
199 const int finesseHeaderNWords = 3;
202 for (iFINESSE = 0; iFINESSE < ECL_FINESSES_IN_COPPER; iFINESSE++) {
206 if (!nShapers) B2ERROR(
"Ecl packer:: Wrong shapers number " << nShapers);
209 B2DEBUG(200,
"Pack data for iCrate = " << iCrate <<
" nShapers = " << nShapers);
212 unsigned int eclCollectorHeader = (1 << nShapers) - 1;
214 buff[iFINESSE].push_back(eclCollectorHeader);
216 for (iShaper = 1; iShaper <= nShapers; iShaper++) {
218 nActiveDSPChannels =
shaperNHits[iCrate - 1][iShaper - 1];
219 B2DEBUG(200,
"iCrate = " << iCrate <<
" iShaper = " << iShaper <<
" nActiveDSPChannels = " << nActiveDSPChannels);
221 B2DEBUG(200,
"nActiveChannelsWithADCData = " << nActiveChannelsWithADCData);
225 unsigned int shaperDataLength = 4 + nActiveDSPChannels + nActiveChannelsWithADCData * ECL_ADC_SAMPLES_PER_CHANNEL;
227 unsigned int shaper_header_w0 = (0x10 << 16) + (shaperDataLength & 0xFFFF);
228 buff[iFINESSE].push_back(shaper_header_w0);
231 unsigned int shaper_header_w1 = (nActiveChannelsWithADCData & 0x1F) << 24;
232 shaper_header_w1 |= (ECL_ADC_SAMPLES_PER_CHANNEL & 0x7F) << 16;
233 shaper_header_w1 |= (nActiveDSPChannels & 0x1F) << 8;
234 shaper_header_w1 |= triggerPhase;
235 buff[iFINESSE].push_back(shaper_header_w1);
238 B2DEBUG(200,
"dspMask = " << std::hex << dspMask);
239 unsigned int shaper_header_w2 = (dspMask & 0xFFFF) << 16;
240 shaper_header_w2 |= (
m_EvtNum & 0xFFFF);
241 buff[iFINESSE].push_back(shaper_header_w2);
244 B2DEBUG(100,
"adcMask = " << std::hex << adcMask);
245 unsigned int shaper_header_w3 = (adcMask & 0xFFFF);
246 buff[iFINESSE].push_back(shaper_header_w3);
249 for (iChannel = 1; iChannel <= ECL_CHANNELS_IN_SHAPER; iChannel++) {
253 if (cid < 1)
continue;
256 if (i_digit < 0)
continue;
257 const int qua =
m_eclDigits[i_digit]->getQuality();
264 int chi_mantissa = 0, chi_exponent = 0;
265 int n_bits = ceil(log2(
double(chi)));
267 chi_exponent = ceil(
float(n_bits - 9) / 2.0);
268 chi_mantissa = chi >> chi_exponent * 2;
273 tim = (chi_exponent << 9) | chi_mantissa;
278 unsigned int hit_data = ((qua & 3) << 30) & 0xC0000000;
279 hit_data |= (tim & 0xFFF) << 18;
280 hit_data |= ((amp + 128) & 0x3FFFF);
281 buff[iFINESSE].push_back(hit_data);
283 B2DEBUG(100,
"cid = " << cid <<
" amp = " << amp <<
" tim = " << tim);
286 for (
int i = 0; i < ECL_CHANNELS_IN_SHAPER; i++)
adcBuffer_temp[i] = 0;
288 setBuffLength(ECL_ADC_SAMPLES_PER_CHANNEL * ECL_CHANNELS_IN_SHAPER);
289 for (iChannel = 1; iChannel <= ECL_CHANNELS_IN_SHAPER; iChannel++) {
291 if (cid < 1)
continue;
293 if (i_wf < 0)
continue;
294 B2DEBUG(200,
"i_wf = " << i_wf);
298 unsigned int adc_data_base = 0;
299 unsigned int adc_data_diff_width = 0;
305 for (
unsigned int iSample = 0; iSample < ECL_ADC_SAMPLES_PER_CHANNEL; iSample++) {
310 B2DEBUG(250,
"ampMin = " << ampMin <<
" ampMax = " << ampMax);
312 adc_data_base = ampMin & 0x3FFFF;
314 adc_data_diff_width = (
unsigned int)(log2((
float)ampMax - (
float)ampMin)) + 1;
315 adc_data_diff_width &= 0x1F;
318 B2DEBUG(250,
"Width = " << adc_data_diff_width <<
" Base = " << adc_data_base);
320 for (
unsigned int iSample = 0; iSample < ECL_ADC_SAMPLES_PER_CHANNEL; iSample++) {
322 B2DEBUG(250,
"offset = " << adc_data_offset);
326 for (
unsigned int iSample = 0; iSample < ECL_ADC_SAMPLES_PER_CHANNEL; iSample++) {
335 for (
int i = 0; i <
m_bufPos; i++) {
346 nwords[0] = buff[0].size();
347 nwords[1] = buff[1].size();
349 buff[0][0] |= (nwords[0] - finesseHeaderNWords) * 4;
350 buff[1][0] |= (nwords[1] - finesseHeaderNWords) * 4;
352 B2DEBUG(100,
"**** iEvt = " <<
m_EvtNum <<
" node= " << iCOPPERNode);
353 for (
unsigned int i = 0; i < 2; i++)
354 for (
unsigned int j = 0; j < buff[i].size(); j++) {
355 B2DEBUG(210,
">> " << std::hex << setfill(
'0') << setw(8) << buff[i][j]);
358 B2DEBUG(100,
"Call PackDetectorBuf");
359 newRawECL->
PackDetectorBuf((
int*)buff[0].data(), nwords[0], (
int*)buff[1].data(), nwords[1],
360 NULL, 0, NULL, 0, rawcprpacker_info);
387 unsigned int tmpval = 0;
389 if (!bitsToWrite)
return;
391 if (value > (
unsigned int)(1 << bitsToWrite) - 1) {
392 B2ERROR(
"Error compressing ADC samples: tying to write too long word");
393 throw Write_adc_samples_error();
398 B2ERROR(
"Error compressing ADC samples: unexpectedly reach end of buffer");
399 throw Write_adc_samples_error();