Skip to content

Commit 62e9d2b

Browse files
mfasDashahor02
authored andcommitted
[EMCAL-610] Fix undefined behaviour in bunch encoding
Bunch word was not set to 0 for the first bunch word in the channel, therefore the header markers (bit 30 and / or 31) could be set randomly, excluding the channel
1 parent cb48f16 commit 62e9d2b

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

Detectors/EMCAL/reconstruction/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ o2_add_executable(rawreader-file
6060
o2_add_test_root_macro(macros/RawFitterTESTs.C
6161
PUBLIC_LINK_LIBRARIES O2::EMCALReconstruction O2::Headers
6262
LABELS emcal COMPILE_ONLY)
63+
64+
o2_add_test_root_macro(macros/RawFitterTESTMulti.C
65+
PUBLIC_LINK_LIBRARIES O2::EMCALReconstruction O2::Headers
66+
LABELS emcal COMPILE_ONLY)
67+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#if !defined(__CLING__) || defined(__ROOTCLING__)
12+
#include <array>
13+
#include <iostream>
14+
#include <fstream>
15+
#include <vector>
16+
#include "RStringView.h"
17+
#include <Rtypes.h>
18+
#include "DetectorsRaw/RawFileReader.h"
19+
#include "DetectorsRaw/RDHUtils.h"
20+
#include "EMCALReconstruction/CaloFitResults.h"
21+
#include "EMCALReconstruction/Bunch.h"
22+
#include "EMCALReconstruction/CaloRawFitterStandard.h"
23+
#include "EMCALReconstruction/AltroDecoder.h"
24+
#include "EMCALReconstruction/CaloRawFitterGamma2.h"
25+
//#include "EMCALReconstruction/RawHeaderStream.h"
26+
#endif
27+
28+
using namespace o2::emcal;
29+
30+
/// \brief Testing the standard raw fitter on run2 to run3 converted data
31+
void RawFitterTESTMulti(const char* configfile = "")
32+
{
33+
34+
const Int_t NoiseThreshold = 3;
35+
36+
o2::raw::RawFileReader reader(configfile);
37+
reader.init();
38+
39+
// define the standard raw fitter
40+
//o2::emcal::CaloRawFitterStandard RawFitter;
41+
o2::emcal::CaloRawFitterGamma2 RawFitter;
42+
RawFitter.setAmpCut(NoiseThreshold);
43+
RawFitter.setL1Phase(0.);
44+
45+
while (1) {
46+
int tfID = reader.getNextTFToRead();
47+
if (tfID >= reader.getNTimeFrames()) {
48+
std::cerr << "nothing left to read after " << tfID << " TFs read" << std::endl;
49+
break;
50+
}
51+
std::vector<char> dataBuffer; // where to put extracted data
52+
std::cout << "Next iteration: Number of links: " << reader.getNLinks() << std::endl;
53+
for (int il = 0; il < reader.getNLinks(); il++) {
54+
auto& link = reader.getLink(il);
55+
std::cout << "Decoding link " << il << std::endl;
56+
57+
auto sz = link.getNextTFSize(); // size in bytes needed for the next TF of this link
58+
dataBuffer.resize(sz);
59+
link.readNextTF(dataBuffer.data());
60+
61+
// Parse
62+
o2::emcal::RawReaderMemory parser(dataBuffer);
63+
while (parser.hasNext()) {
64+
parser.next();
65+
//std::cout << "next page \n";
66+
if (o2::raw::RDHUtils::getFEEID(parser.getRawHeader()) >= 40)
67+
continue;
68+
69+
//std::cout<<rawreader.getRawHeader()<<std::endl;
70+
71+
// use the altro decoder to decode the raw data, and extract the RCU trailer
72+
o2::emcal::AltroDecoder decoder(parser);
73+
std::cout << "Decoding" << std::endl;
74+
decoder.decode();
75+
76+
//std::cout << decoder.getRCUTrailer() << std::endl;
77+
std::cout << "Found number of channels: " << decoder.getChannels().size() << std::endl;
78+
79+
// Loop over all the channels
80+
for (auto& chan : decoder.getChannels()) {
81+
std::cout << "processing next channel idx " << chan.getChannelIndex() << ", " << chan.getHardwareAddress() << std::endl;
82+
// define the conatiner for the fit results, and perform the raw fitting using the stadnard raw fitter
83+
//continue;
84+
std::cout << "Channel has " << chan.getBunches().size() << " bunches " << std::endl;
85+
try {
86+
o2::emcal::CaloFitResults fitResults = RawFitter.evaluate(chan.getBunches(), 0, 0);
87+
88+
// print the fit output
89+
//std::cout << "The Time is : " << fitResults.getTime() << " And the Amplitude is : " << fitResults.getAmp() << std::endl;
90+
std::cout << "Fit done" << std::endl;
91+
} catch (o2::emcal::CaloRawFitter::RawFitterError_t& fiterror) {
92+
std::cerr << "Error processing raw fit: " << o2::emcal::CaloRawFitter::createErrorMessage(fiterror) << std::endl;
93+
for (auto bunch : chan.getBunches()) {
94+
std::cout << "Next bunch: " << bunch.getADC().size() << " entries" << std::endl;
95+
bool first = true;
96+
for (auto en : bunch.getADC()) {
97+
if (!first) {
98+
std::cout << ", ";
99+
}
100+
std::cout << en;
101+
first = false;
102+
}
103+
std::cout << std::endl;
104+
}
105+
std::cout << "Channel end" << std::endl;
106+
}
107+
}
108+
}
109+
}
110+
reader.setNextTFToRead(++tfID);
111+
}
112+
}

Detectors/EMCAL/simulation/src/RawWriter.cxx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ bool RawWriter::processTrigger(const o2::emcal::TriggerRecord& trg)
131131
auto hwaddress = mMappingHandler->getMappingForDDL(srucont.mSRUid).getHardwareAddress(channel.mRow, channel.mCol, ChannelType_t::HIGH_GAIN); // @TODO distinguish between high- and low-gain cells
132132

133133
std::vector<int> rawbunches;
134+
int nbunches = 0;
134135
for (auto& bunch : findBunches(channel.mDigits)) {
135136
if (!bunch.mADCs.size()) {
136137
LOG(ERROR) << "Found bunch with without ADC entries - skipping ...";
@@ -141,10 +142,14 @@ bool RawWriter::processTrigger(const o2::emcal::TriggerRecord& trg)
141142
for (auto adc : bunch.mADCs) {
142143
rawbunches.push_back(adc);
143144
}
145+
nbunches++;
144146
}
145147
if (!rawbunches.size()) {
148+
LOG(DEBUG) << "No bunch selected";
146149
continue;
147150
}
151+
LOG(DEBUG) << "Selected " << nbunches << " bunches";
152+
148153
auto encodedbunches = encodeBunchData(rawbunches);
149154
auto chanhead = createChannelHeader(hwaddress, rawbunches.size(), false); /// bad channel status eventually to be added later
150155
char* chanheadwords = reinterpret_cast<char*>(&chanhead);
@@ -156,6 +161,8 @@ bool RawWriter::processTrigger(const o2::emcal::TriggerRecord& trg)
156161
if (encodedbunches.size() != nwordsRead) {
157162
LOG(ERROR) << "Mismatch in number of 32-bit words, encoded " << encodedbunches.size() << ", recalculated " << nwordsRead << std::endl;
158163
LOG(ERROR) << "Payload size: " << payloadsizeRead << ", number of words: " << rawbunches.size() << ", encodeed words " << encodedbunches.size() << ", calculated words " << nwordsRead << std::endl;
164+
} else {
165+
LOG(DEBUG) << "Matching number of payload 32-bit words, encoded " << encodedbunches.size() << ", decoded " << nwordsRead;
159166
}
160167
} else {
161168
LOG(ERROR) << "Header without header bit detected ..." << std::endl;
@@ -170,8 +177,10 @@ bool RawWriter::processTrigger(const o2::emcal::TriggerRecord& trg)
170177
}
171178

172179
if (!payload.size()) {
180+
LOG(DEBUG) << "Payload buffer has size 0" << std::endl;
173181
continue;
174182
}
183+
LOG(DEBUG) << "Payload buffer has size " << payload.size();
175184

176185
// Create RCU trailer
177186
auto trailerwords = createRCUTrailer(payload.size() / 4, 100., trg.getBCData().toLong(), srucont.mSRUid);
@@ -248,8 +257,12 @@ std::vector<int> RawWriter::encodeBunchData(const std::vector<int>& data)
248257
{
249258
std::vector<int> encoded;
250259
CaloBunchWord currentword;
260+
currentword.mDataWord = 0;
251261
int wordnumber = 0;
252262
for (auto adc : data) {
263+
if (adc > 0x3FF) {
264+
LOG(ERROR) << "Exceeding max ADC count for 10 bit ALTRO word: " << adc << " (max: 1023)" << std::endl;
265+
}
253266
switch (wordnumber) {
254267
case 0:
255268
currentword.mWord0 = adc;

0 commit comments

Comments
 (0)