25 mEnableAdcClockWarning(true),
26 mEnableSyncPatternWarning(true),
27 mEnableStoreGBTFrames(true),
28 mEnableCompileAdcValues(true),
32 mPositionForHalfSampa({-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}),
34 mGBTFramesAnalyzed(0),
37 mSampaVersion(sampaVersion),
40 mGBTFrames.reserve(
size);
42 for (
auto& aAdcValues : mAdcValues) {
43 aAdcValues =
new std::queue<short>;
49 for (
auto& aAdcValues : mAdcValues) {
56 std::cout <<
"Reading from file " << fileName << std::endl;
57 std::ifstream
file(fileName);
59 if (!
file.is_open()) {
60 LOG(error) <<
"Can't read file " << fileName;
79 std::string frameString;
80 unsigned word0, word1, word2, word3;
83 sscanf(frameString.substr(2, 8).c_str(),
"%x", &word3);
84 sscanf(frameString.substr(10, 8).c_str(),
"%x", &word2);
85 sscanf(frameString.substr(18, 8).c_str(),
"%x", &word1);
86 sscanf(frameString.substr(26, 8).c_str(),
"%x", &word0);
94 std::cout <<
"Reading from file " << fileName << std::endl;
95 std::ifstream
file(fileName);
97 if (!
file.is_open()) {
98 LOG(error) <<
"Can't read file " << fileName;
105 if (
type ==
"grorc") {
106 while (!
file.eof() && ((frames == -1) || (mGBTFramesAnalyzed < frames))) {
107 file.read((
char*)&rawData,
sizeof(rawData));
108 rawMarker = rawData & 0xFFFF0000;
109 if ((rawMarker == 0xDEF10000) || (rawMarker == 0xDEF40000)) {
110 file.read((
char*)&words, 3 *
sizeof(words[0]));
111 addGBTFrame(rawData, words[0], words[1], words[2]);
114 }
else if (
type ==
"trorc") {
115 while (!
file.eof() && ((frames == -1) || (mGBTFramesAnalyzed < frames))) {
116 file.read((
char*)&words, 4 *
sizeof(words[0]));
117 addGBTFrame(words[0], words[1], words[2], words[3]);
119 }
else if (
type ==
"trorc2") {
123 file.read((
char*)&words, 8 *
sizeof(words[0]));
126 uint32_t headerVersion = (words[0] >> 24) & 0xF;
127 if (headerVersion == 0) {
128 uint32_t readoutMode = words[0] & 0xFFFF;
129 uint32_t reserved_0 = (words[0] >> 16) & 0xF;
130 uint32_t
channelID = (words[0] >> 20) & 0xF;
131 uint32_t n_words = words[1];
132 uint64_t timestamp = words[2];
133 timestamp = (timestamp << 32) | words[3];
134 uint64_t event_count = words[4];
135 event_count = (event_count << 32) | words[5];
136 uint64_t reserved_1 = words[6];
137 reserved_1 = (reserved_1 << 32) | words[7];
138 LOG(
debug) <<
"Header version: " << headerVersion;
140 LOG(
debug) <<
"reserved_0: 0x" << std::hex << std::setfill(
'0') << std::right << std::setw(1) << reserved_0 << std::dec;
141 LOG(
debug) <<
"Readout mode: " << readoutMode;
142 LOG(
debug) <<
"n_words: " << n_words;
143 LOG(
debug) <<
"Timestamp: 0x" << std::hex << std::setfill(
'0') << std::right << std::setw(16) << timestamp << std::dec;
144 LOG(
debug) <<
"Event counter: " << event_count;
145 LOG(
debug) <<
"reserved_1: 0x" << std::hex << std::setfill(
'0') << std::right << std::setw(16) << reserved_1 << std::dec;
147 switch (readoutMode) {
149 for (
int i = 0;
i < (n_words - 8);
i =
i + 4) {
150 file.read((
char*)&words, 4 *
sizeof(words[0]));
151 addGBTFrame(words[0], words[1], words[2], words[3]);
159 std::array<bool, 5> writeValue;
160 writeValue.fill(
false);
161 std::array<std::array<uint32_t, 16>, 5> adcValues;
163 for (
int i = 0;
i < (n_words - 8);
i =
i + 4) {
164 file.read((
char*)&words, 4 *
sizeof(words[0]));
166 ids[4] = (words[0] >> 4) & 0xF;
167 ids[3] = (words[0] >> 8) & 0xF;
168 ids[2] = (words[0] >> 12) & 0xF;
169 ids[1] = (words[0] >> 16) & 0xF;
170 ids[0] = (words[0] >> 20) & 0xF;
172 adcValues[4][((
ids[4] & 0x7) * 2) + 1] = (((
ids[4] >> 3) & 0x1) == 0) ? 0 : words[3] & 0x3FF;
173 adcValues[4][((
ids[4] & 0x7) * 2)] = (((
ids[4] >> 3) & 0x1) == 0) ? 0 : (words[3] >> 10) & 0x3FF;
174 adcValues[3][((
ids[3] & 0x7) * 2) + 1] = (((
ids[3] >> 3) & 0x1) == 0) ? 0 : (words[3] >> 20) & 0x3FF;
175 adcValues[3][((
ids[3] & 0x7) * 2)] = (((
ids[3] >> 3) & 0x1) == 0) ? 0 : ((words[2] & 0xFF) << 2) | ((words[3] >> 30) & 0x3);
176 adcValues[2][((
ids[2] & 0x7) * 2) + 1] = (((
ids[2] >> 3) & 0x1) == 0) ? 0 : (words[2] >> 8) & 0x3FF;
177 adcValues[2][((
ids[2] & 0x7) * 2)] = (((
ids[2] >> 3) & 0x1) == 0) ? 0 : (words[2] >> 18) & 0x3FF;
178 adcValues[1][((
ids[1] & 0x7) * 2) + 1] = (((
ids[1] >> 3) & 0x1) == 0) ? 0 : ((words[1] & 0x3F) << 4) | ((words[2] >> 28) & 0xF);
179 adcValues[1][((
ids[1] & 0x7) * 2)] = (((
ids[1] >> 3) & 0x1) == 0) ? 0 : (words[1] >> 6) & 0x3FF;
180 adcValues[0][((
ids[0] & 0x7) * 2) + 1] = (((
ids[0] >> 3) & 0x1) == 0) ? 0 : (words[1] >> 16) & 0x3FF;
181 adcValues[0][((
ids[0] & 0x7) * 2)] = (((
ids[0] >> 3) & 0x1) == 0) ? 0 : ((words[0] & 0xF) << 6) | ((words[1] >> 26) & 0x3F);
183 for (
int j = 0;
j < 5; ++
j) {
184 std::cout << std::bitset<4>(
ids[
j]) <<
" " << adcValues[0][((
ids[0] & 0x7) * 2)] <<
" " << adcValues[0][((
ids[0] & 0x7) * 2) + 1] << std::endl;
186 std::cout << std::endl;
188 for (
int j = 0;
j < 5; ++
j) {
190 writeValue[
j] =
true;
193 for (
int j = 0;
j < 5; ++
j) {
194 if ((writeValue[
j] &
ids[
j]) == 0xF) {
195 for (
int k = 0; k < 16; ++k) {
196 mAdcValues[
j]->push(adcValues[
j][k]);
197 std::cout << adcValues[
j][k] <<
" ";
199 std::cout << std::endl;
202 std::cout << std::endl;
212 std::array<bool, 5> writeValue;
213 writeValue.fill(
false);
214 std::array<std::array<uint32_t, 16>, 5> adcValues;
216 for (
int i = 0;
i < (n_words - 8);
i =
i + 4) {
217 file.read((
char*)&words, 8 *
sizeof(words[0]));
219 ids[4] = (words[4] >> 4) & 0xF;
220 ids[3] = (words[4] >> 8) & 0xF;
221 ids[2] = (words[4] >> 12) & 0xF;
222 ids[1] = (words[4] >> 16) & 0xF;
223 ids[0] = (words[4] >> 20) & 0xF;
225 adcValues[4][((
ids[4] & 0x7) * 2) + 1] = (((
ids[4] >> 3) & 0x1) == 0) ? 0 : words[7] & 0x3FF;
226 adcValues[4][((
ids[4] & 0x7) * 2)] = (((
ids[4] >> 3) & 0x1) == 0) ? 0 : (words[7] >> 10) & 0x3FF;
227 adcValues[3][((
ids[3] & 0x7) * 2) + 1] = (((
ids[3] >> 3) & 0x1) == 0) ? 0 : (words[7] >> 20) & 0x3FF;
228 adcValues[3][((
ids[3] & 0x7) * 2)] = (((
ids[3] >> 3) & 0x1) == 0) ? 0 : ((words[6] & 0xFF) << 2) | ((words[7] >> 30) & 0x3);
229 adcValues[2][((
ids[2] & 0x7) * 2) + 1] = (((
ids[2] >> 3) & 0x1) == 0) ? 0 : (words[6] >> 8) & 0x3FF;
230 adcValues[2][((
ids[2] & 0x7) * 2)] = (((
ids[2] >> 3) & 0x1) == 0) ? 0 : (words[6] >> 18) & 0x3FF;
231 adcValues[1][((
ids[1] & 0x7) * 2) + 1] = (((
ids[1] >> 3) & 0x1) == 0) ? 0 : ((words[5] & 0x3F) << 4) | ((words[6] >> 28) & 0xF);
232 adcValues[1][((
ids[1] & 0x7) * 2)] = (((
ids[1] >> 3) & 0x1) == 0) ? 0 : (words[5] >> 6) & 0x3FF;
233 adcValues[0][((
ids[0] & 0x7) * 2) + 1] = (((
ids[0] >> 3) & 0x1) == 0) ? 0 : (words[5] >> 16) & 0x3FF;
234 adcValues[0][((
ids[0] & 0x7) * 2)] = (((
ids[0] >> 3) & 0x1) == 0) ? 0 : ((words[4] & 0xF) << 6) | ((words[5] >> 26) & 0x3F);
236 for (
int j = 0;
j < 5; ++
j) {
238 writeValue[
j] =
true;
241 for (
int j = 0;
j < 5; ++
j) {
242 if ((writeValue[
j] &
ids[
j]) == 0xF) {
243 for (
int k = 0; k < 16; ++k) {
244 mAdcValues[
j]->push(adcValues[
j][k]);
284 mGBTFramesAnalyzed = 0;
289void GBTFrameContainer::processAllFrames()
293 for (std::array<std::queue<short>*, 5>::iterator it = mAdcValues.begin(); it != mAdcValues.end(); ++it) {
294 if ((*it)->size() > 0) {
295 LOG(warning) <<
"There are already some ADC values for half SAMPA " << std::distance(mAdcValues.begin(), it)
296 <<
" , maybe the frames were already processed.";
301 for (
auto it = mGBTFrames.begin(); it != mGBTFrames.end(); ++it) {
306void GBTFrameContainer::processFrame(std::vector<GBTFrame>::iterator iFrame)
308 ++mGBTFramesAnalyzed;
310 if (mEnableAdcClockWarning) {
311 checkAdcClock(iFrame);
314 searchSyncPattern(iFrame);
316 if (mEnableCompileAdcValues) {
317 compileAdcValues(iFrame);
321void GBTFrameContainer::compileAdcValues(std::vector<GBTFrame>::iterator iFrame)
326 for (
short iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
327 if (mPositionForHalfSampa[iHalfSampa] == -1) {
330 if (mPositionForHalfSampa[iHalfSampa + 5] == -1) {
334 switch (mPositionForHalfSampa[iHalfSampa]) {
336 value1 = (iFrame->getHalfWord(iHalfSampa / 2, 1, iHalfSampa % 2) << 5) |
337 iFrame->getHalfWord(iHalfSampa / 2, 0, iHalfSampa % 2);
338 value2 = (iFrame->getHalfWord(iHalfSampa / 2, 3, iHalfSampa % 2) << 5) |
339 iFrame->getHalfWord(iHalfSampa / 2, 2, iHalfSampa % 2);
343 value1 = ((iFrame - 1)->getHalfWord(iHalfSampa / 2, 2, iHalfSampa % 2) << 5) |
344 (iFrame - 1)->getHalfWord(iHalfSampa / 2, 1, iHalfSampa % 2);
345 value2 = (iFrame->getHalfWord(iHalfSampa / 2, 0, iHalfSampa % 2) << 5) |
346 (iFrame - 1)->getHalfWord(iHalfSampa / 2, 3, iHalfSampa % 2);
350 value1 = ((iFrame - 1)->getHalfWord(iHalfSampa / 2, 3, iHalfSampa % 2) << 5) |
351 (iFrame - 1)->getHalfWord(iHalfSampa / 2, 2, iHalfSampa % 2);
352 value2 = (iFrame->getHalfWord(iHalfSampa / 2, 1, iHalfSampa % 2) << 5) |
353 iFrame->getHalfWord(iHalfSampa / 2, 0, iHalfSampa % 2);
357 value1 = (iFrame->getHalfWord(iHalfSampa / 2, 0, iHalfSampa % 2) << 5) |
358 (iFrame - 1)->getHalfWord(iHalfSampa / 2, 3, iHalfSampa % 2);
359 value2 = (iFrame->getHalfWord(iHalfSampa / 2, 2, iHalfSampa % 2) << 5) |
360 iFrame->getHalfWord(iHalfSampa / 2, 1, iHalfSampa % 2);
364 LOG(error) <<
"Position " << mPositionForHalfSampa[iHalfSampa] <<
" not known.";
368 if (mSampaVersion == 1 || mSampaVersion == 2) {
369 mAdcValues[iHalfSampa]->emplace(value1 ^ (1 << 9));
370 mAdcValues[iHalfSampa]->emplace(value2 ^ (1 << 9));
372 mAdcValues[iHalfSampa]->emplace(value1);
373 mAdcValues[iHalfSampa]->emplace(value2);
382void GBTFrameContainer::checkAdcClock(std::vector<GBTFrame>::iterator iFrame)
384 if (mAdcClock[0].addSequence(iFrame->getAdcClock(0))) {
385 LOG(warning) <<
"ADC clock error of SAMPA 0 in GBT Frame " << std::distance(mGBTFrames.begin(), iFrame);
387 if (mAdcClock[1].addSequence(iFrame->getAdcClock(1))) {
388 LOG(warning) <<
"ADC clock error of SAMPA 1 in GBT Frame " << std::distance(mGBTFrames.begin(), iFrame);
390 if (mAdcClock[2].addSequence(iFrame->getAdcClock(2))) {
391 LOG(warning) <<
"ADC clock error of SAMPA 2 in GBT Frame " << std::distance(mGBTFrames.begin(), iFrame);
395void GBTFrameContainer::searchSyncPattern(std::vector<GBTFrame>::iterator iFrame)
397 for (
short iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
398 mPositionForHalfSampa[iHalfSampa + 5] = mPositionForHalfSampa[iHalfSampa];
401 if (mSyncPattern[0].addSequence(
402 iFrame->getHalfWord(0, 0, 0),
403 iFrame->getHalfWord(0, 1, 0),
404 iFrame->getHalfWord(0, 2, 0),
405 iFrame->getHalfWord(0, 3, 0))) {
406 mPositionForHalfSampa[0] = mSyncPattern[0].getPosition();
409 if (mSyncPattern[1].addSequence(
410 iFrame->getHalfWord(0, 0, 1),
411 iFrame->getHalfWord(0, 1, 1),
412 iFrame->getHalfWord(0, 2, 1),
413 iFrame->getHalfWord(0, 3, 1))) {
414 mPositionForHalfSampa[1] = mSyncPattern[1].getPosition();
417 if (mSyncPattern[2].addSequence(
418 iFrame->getHalfWord(1, 0, 0),
419 iFrame->getHalfWord(1, 1, 0),
420 iFrame->getHalfWord(1, 2, 0),
421 iFrame->getHalfWord(1, 3, 0))) {
422 mPositionForHalfSampa[2] = mSyncPattern[2].getPosition();
425 if (mSyncPattern[3].addSequence(
426 iFrame->getHalfWord(1, 0, 1),
427 iFrame->getHalfWord(1, 1, 1),
428 iFrame->getHalfWord(1, 2, 1),
429 iFrame->getHalfWord(1, 3, 1))) {
430 mPositionForHalfSampa[3] = mSyncPattern[3].getPosition();
433 if (mSyncPattern[4].addSequence(
434 iFrame->getHalfWord(2, 0),
435 iFrame->getHalfWord(2, 1),
436 iFrame->getHalfWord(2, 2),
437 iFrame->getHalfWord(2, 3))) {
438 mPositionForHalfSampa[4] = mSyncPattern[4].getPosition();
441 if (mEnableSyncPatternWarning) {
442 if (mPositionForHalfSampa[0] != mPositionForHalfSampa[1]) {
443 LOG(warning) <<
"The two half words from SAMPA 0 don't start at the same position, lower bits start at "
444 << mPositionForHalfSampa[0] <<
", higher bits at " << mPositionForHalfSampa[1];
446 if (mPositionForHalfSampa[2] != mPositionForHalfSampa[3]) {
447 LOG(warning) <<
"The two half words from SAMPA 1 don't start at the same position, lower bits start at "
448 << mPositionForHalfSampa[2] <<
", higher bits at " << mPositionForHalfSampa[3];
450 if (mPositionForHalfSampa[0] != mPositionForHalfSampa[2] || mPositionForHalfSampa[0] != mPositionForHalfSampa[4]) {
451 LOG(warning) <<
"The three SAMPAs don't have the same position, SAMPA0 = " << mPositionForHalfSampa[0]
452 <<
", SAMPA1 = " << mPositionForHalfSampa[2] <<
", SAMPA2 = " << mPositionForHalfSampa[4];
471 bool dataAvailable =
false;
474 for (
int iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
475 if (mAdcValues[iHalfSampa]->
size() < 16) {
476 mTmpData[iHalfSampa].fill(0);
479 dataAvailable =
true;
480 for (
int iChannel = 0; iChannel < 16; ++iChannel) {
481 mTmpData[iHalfSampa][iChannel] = mAdcValues[iHalfSampa]->front();
482 mAdcValues[iHalfSampa]->pop();
487 if (!dataAvailable) {
488 return dataAvailable;
492 int iTimeBin = mTimebin;
498 for (
int iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
501 ? ((mCRU % 2) ? 16 : 0)
502 : ((iHalfSampa % 2) ? 16 : 0);
503 iSampa = (iHalfSampa == 4) ? 2 : (mCRU % 2) ? iHalfSampa / 2 + 3 : iHalfSampa / 2;
504 for (std::array<short, 16>::iterator it = mTmpData[iHalfSampa].
begin(); it != mTmpData[iHalfSampa].end(); ++it) {
510 iRow = padPos.getRow();
511 iPad = padPos.getPad();
514 container.emplace_back(mCRU, iCharge, iRow, iPad, iTimeBin);
522 return dataAvailable;
527 bool dataAvailable =
false;
530 for (
int iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
531 if (mAdcValues[iHalfSampa]->
size() < 16) {
532 mTmpData[iHalfSampa].fill(0);
535 dataAvailable =
true;
536 for (
int iChannel = 0; iChannel < 16; ++iChannel) {
537 mTmpData[iHalfSampa][iChannel] = mAdcValues[iHalfSampa]->front();
538 mAdcValues[iHalfSampa]->pop();
543 if (!dataAvailable) {
544 return dataAvailable;
560 for (
int iHalfSampa = 0; iHalfSampa < 5; ++iHalfSampa) {
561 iSampa = (iHalfSampa == 4) ? 2 : (mCRU % 2) ? iHalfSampa / 2 + 3 : iHalfSampa / 2;
563 container.emplace_back(iSampa, !((
bool)mCRU % 2), mTmpData[iHalfSampa]);
565 return dataAvailable;
570 LOG(info) <<
"Resetting GBT-Frame container";
576 mGBTFramesAnalyzed = 0;
579void GBTFrameContainer::resetAdcClock()
581 for (
auto& aAdcClock : mAdcClock) {
586void GBTFrameContainer::resetSyncPattern()
588 for (
auto& aSyncPattern : mSyncPattern) {
589 aSyncPattern.reset();
591 for (
auto& aPositionForHalfSampa : mPositionForHalfSampa) {
592 aPositionForHalfSampa = -1;
597void GBTFrameContainer::resetAdcValues()
600 for (std::array<std::queue<short>*, 5>::iterator it = mAdcValues.begin(); it != mAdcValues.end(); ++it) {
602 (*it) =
new std::queue<short>;
611 for (
auto& aAdcValues : mAdcValues) {
621 unsigned clock = (0xFFFF0000 >>
phase);
623 for (std::vector<GBTFrame>::iterator it = mGBTFrames.begin(); it != mGBTFrames.end(); ++it) {
624 it->setAdcClock(sampa, (clock >> 28) & 0xF);
625 clock = (clock << 4) | (clock >> 28);
Container class for the GBT Frames.
Class to monitor the ADC smapling clock contained in the GBT frame.
GBT Frame container class.
bool getData(std::vector< Digit > &container)
void reset()
Reset function to clear container.
void addGBTFramesFromBinaryFile(std::string fileName, std::string type="grorc", int frames=-1)
void overwriteAdcClock(int sampa, unsigned short phase)
~GBTFrameContainer()
Destructor.
std::vector< GBTFrame >::iterator begin()
void addGBTFrame(GBTFrame &frame)
void reProcessAllFrames()
Re-Processes all frames after resetting ADC clock and sync Pattern.
void addGBTFramesFromFile(std::string fileName)
GBTFrameContainer()
Default constructor.
const PadPos padPosRegion(const int cruNumber, const int fecInPartition, const int sampaOnFEC, const int channelOnSAMPA) const
static Mapper & instance(const std::string mappingDir="")
Class to monitor the data stream and detect synchronization patterns.
GLint GLint GLsizei GLint GLenum GLenum type
Global TPC definitions and constants.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"