Belle II Software development
SVDUnpackerModule.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#include <svd/modules/svdUnpacker/SVDUnpackerModule.h>
10
11
12#include <framework/datastore/DataStore.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/logging/Logger.h>
15
16#include <boost/crc.hpp> // for boost::crc_basic, boost::augmented_crc
17#define CRC16POLYREV 0x8005 // CRC-16 polynomial, normal representation
18
19#include <arpa/inet.h>
20
21#include <sstream>
22#include <iomanip>
23#include <cstring>
24#include <vector>
25#include <set>
26#include <map>
27#include <utility>
28#include <algorithm>
29
30using namespace std;
31using namespace Belle2;
32using namespace Belle2::SVD;
33
34//-----------------------------------------------------------------
35// Register the Module
36//-----------------------------------------------------------------
37REG_MODULE(SVDUnpacker);
38
39//-----------------------------------------------------------------
40// Implementation
41//-----------------------------------------------------------------
42
43std::string Belle2::SVD::SVDUnpackerModule::m_xmlFileName = std::string("SVDChannelMapping.xml");
44
45// seenHeadersAndTrailers is a bit-field set explicitly at the start of each event
46// cppcheck-suppress uninitMemberVar
51{
52 //Set module properties
53 setDescription("Produce SVDShaperDigits from RawSVD. NOTE: only zero-suppressed mode is currently supported!");
55
56 addParam("SVDEventInfo", m_svdEventInfoName, "Name of the SVDEventInfo object", string(""));
57 addParam("rawSVDListName", m_rawSVDListName, "Name of the raw SVD List", string(""));
58 addParam("svdShaperDigitListName", m_svdShaperDigitListName, "Name of the SVDShaperDigits list", string(""));
59 addParam("shutUpFTBError", m_shutUpFTBError,
60 "if >0 is the number of reported FTB header ERRORs before quiet operations. If <0 full log produced.", -1);
61 addParam("FADCTriggerNumberOffset", m_FADCTriggerNumberOffset,
62 "number to be added to the FADC trigger number to match the main trigger number", 0);
63 addParam("svdDAQDiagnosticsListName", m_svdDAQDiagnosticsListName, "Name of the DAQDiagnostics list", string(""));
64 addParam("softwarePipelineAddressEmulation", m_emulatePipelineAddress, "Estimate emulated pipeline address", bool(true));
65 addParam("killDigitsFromUpsetAPVs", m_killUpsetDigits, "Delete digits from upset APVs", bool(false));
66 addParam("silentlyAppend", m_silentAppend, "Append digits to a pre-existing non-empty storeArray", bool(false));
67 addParam("badMappingFatal", m_badMappingFatal, "Throw B2FATAL if there's a wrong payload in the database", bool(false));
68 addParam("UnpackerErrorRate", m_errorRate, "Unpacker will print one error every UnpackerErrorRate", int(1000));
69 addParam("PrintRawData", m_printRaw, "Printing Raw data words for debugging", bool(false));
70}
71
75
77{
78 m_eventMetaDataPtr.isRequired();
79 // Don't panic if no SVD data.
80 m_rawSVD.isOptional(m_rawSVDListName);
81
82 // Register default SVDEventInfo for unpacking Raw Data
84
87
90
91}
92
94{
95 if (!m_mapping.isValid())
96 B2FATAL("no valid SVD Channel Mapping. We stop here.");
97
98 m_wrongFTBcrc = 0;
99 if (m_mapping.hasChanged()) { m_map = std::make_unique<SVDOnlineToOfflineMap>(m_mapping->getFileName()); }
100
101 if (! m_map) { //give up
102 B2ERROR("SVD xml map not loaded." << std::endl <<
103 "No SVDShaperDigit will be produced for this run!");
104 return;
105 }
106
107 //number of FADC boards
108 nFADCboards = m_map->getFADCboardsNumber();
109
110 //passing APV<->FADC mapping from SVDOnlineToOfflineMap object
111 APVmap = &(m_map->APVforFADCmap);
112
113 //setting UnpackerErrorRate factor to use it for BadMapping error suppression
114 m_map->setErrorRate(m_errorRate);
115
118 nUpsetAPVsErrors = -1;
119 nSEURecoveryCase = -1;
122 nFADCMatchErrors = -1;
123 nAPVErrors = -1;
124 nFTBFlagsErrors = -1;
126
128
129 //get the relative time shift
130 if (!m_svdGlobalConfig.isValid())
131 B2FATAL("SVDGlobalConfigParameters not valid!!");
132
133 m_relativeTimeShift = m_svdGlobalConfig->getRelativeTimeShift();
134
135}
136
137#ifndef __clang__
138#pragma GCC diagnostic push
139#pragma GCC diagnostic ignored "-Wstack-usage="
140#endif
142{
143 if (!m_rawSVD || !m_rawSVD.getEntries())
144 return;
145
147 B2WARNING("Unpacking SVDShaperDigits to a non-empty pre-existing \n"
148 << "StoreArray. This can lead to undesired behaviour. At least\n"
149 << "remember to use SVDShaperDigitSorter in your path and \n"
150 << "set the silentlyAppend parameter of SVDUnpacker to true.");
151
152 SVDDAQDiagnostic* currentDAQDiagnostic;
153 vector<SVDDAQDiagnostic*> vDiagnostic_ptr;
154
155 map<SVDShaperDigit, SVDDAQDiagnostic*> diagnosticMap;
156 // Store encountered pipeline addresses with APVs in which they were observed
157 map<unsigned short, set<pair<unsigned short, unsigned short> > > apvsByPipeline;
158
159 if (!m_eventMetaDataPtr.isValid()) { // give up...
160 B2ERROR("Missing valid EventMetaData." << std::endl << "No SVDShaperDigit produced for this event!");
161 return;
162 }
163
164 bool nAPVmatch = true;
165 bool badMapping = false;
166 bool badHeader = false;
167 bool badTrailer = false;
168 bool missedHeader = false;
169 bool missedTrailer = false;
170
171 // flag to set SVDEventInfo once per event
172 bool isSetEventInfo = false;
173
174 //flag to set nAPVsamples in SVDEventInfo once per event
175 bool isSetNAPVsamples = false;
176
177 unsigned short nAPVheaders = 999;
178 set<short> seenAPVHeaders = {};
179
180 unsigned short nEntries_rawSVD = m_rawSVD.getEntries();
181 auto eventNo = m_eventMetaDataPtr->getEvent();
182
183 short fadc = 255, apv = 63;
184
185 unsigned short cntFADCboards = 0;
186 for (unsigned int i = 0; i < nEntries_rawSVD; i++) {
187
188 unsigned int numEntries_rawSVD = m_rawSVD[ i ]->GetNumEntries();
189 for (unsigned int j = 0; j < numEntries_rawSVD; j++) {
190
191 const unsigned short maxNumOfCh = m_rawSVD[i]->GetMaxNumOfCh(j);
192
193 std::vector<unsigned short> nWords;
194 nWords.reserve(maxNumOfCh);
195 std::vector<uint32_t*> data32tab(maxNumOfCh); //vector of pointers
196 for (unsigned int k = 0; k < maxNumOfCh; k++) {
197 nWords.push_back(m_rawSVD[i]->GetDetectorNwords(j, k));
198 data32tab[k] = reinterpret_cast<uint32_t*>(m_rawSVD[i]->GetDetectorBuffer(j, k)); // points at the beginning of the 1st buffer
199 }
200
201 unsigned short ftbError = 0;
202 unsigned short trgType = 0;
203 unsigned short trgNumber = 0;
204 unsigned short daqMode = -1;
205 unsigned short daqType = 0;
206 unsigned short cmc1;
207 unsigned short cmc2;
208 unsigned short apvErrors;
209 unsigned short pipAddr;
210 unsigned short ftbFlags = 0;
211 unsigned short apvErrorsOR = 0;
212
213 bool is3sampleData = false;
214 bool is6sampleData = false;
215
216 for (unsigned int buf = 0; buf < maxNumOfCh; buf++) { // loop over 4(COPPER) or 48(PCIe40) buffers
217
218 if (data32tab[buf] == nullptr || nWords.at(buf) == 0) {
219 if (data32tab[buf] != nullptr || nWords.at(buf) != 0) {
220 B2WARNING("Invalid combination of buffer pointer and nWords:" <<
221 LogVar("COPPER/PCIe40 ID", i) <<
222 LogVar("COPPER/PCIe40 Entry", j) <<
223 LogVar("COPPER/PCIe40 Channel", buf) <<
224 LogVar("data32tab[buf]", data32tab[buf]) <<
225 LogVar("nWords[buf]", nWords.at(buf)));
226 }
227 continue;
228 }
229 if (m_printRaw) printB2Debug(data32tab[buf], data32tab[buf], &data32tab[buf][nWords.at(buf) - 1], nWords.at(buf));
230
231 cntFADCboards++;
232
233 missedHeader = false;
234 missedTrailer = false;
235
236 uint32_t* data32_it = data32tab[buf];
237 short strip, sample[6];
238 vector<uint32_t> crc16vec;
239
240 //reset value for headers and trailers check
242
243 for (; data32_it != &data32tab[buf][nWords.at(buf)]; data32_it++) {
244 m_data32 = *data32_it; //put current 32-bit frame to union
245
246 if (m_data32 == 0xffaa0000) { // first part of FTB header
247 crc16vec.clear(); // clear the input container for crc16 calculation
248 crc16vec.push_back(m_data32);
249
250 seenHeadersAndTrailers |= 0x1; // we found FTB header
251
252 data32_it++; // go to 2nd part of FTB header
253 crc16vec.push_back(*data32_it);
254
255 m_data32 = *data32_it; //put the second 32-bit frame to union
256
257 ftbError = m_FTBHeader.errorsField;
258
259 if (ftbError != 240) {
261
263 switch (ftbError - 240) {
264 case 3:
265 B2ERROR("FADC Event Number is different from (FTB & TTD) Event Numbers");
266 break;
267 case 5:
268 B2ERROR("TTD Event Number is different from (FTB & FADC) Event Numbers");
269 break;
270 case 6:
271 B2ERROR("FTB Event Number is different from (TTD & FADC) Event Numbers");
272 break;
273 case 7:
274 B2ERROR("(FTB, TTD & FADC) Event Numbers are different from each other");
275 break;
276 default:
277 B2ERROR("Problem with errorsField variable in FTB Header" << LogVar("abnormal value", ftbError));
278 }
279 }
280 }
281
282 if (m_FTBHeader.eventNumber !=
283 (eventNo & 0xFFFFFF)) {
286 m_shutUpFTBError -= 1;
287 B2ERROR("Event number mismatch detected! The event number given by EventMetaData object is different from the one in the FTB Header."
288 << LogVar("Expected event number & 0xFFFFFF",
289 (eventNo & 0xFFFFFF)) << LogVar("Event number in the FTB", m_FTBHeader.eventNumber));
290 }
291 }
292
293 continue;
294 } // is FTB Header
295
296 crc16vec.push_back(m_data32);
297
298 if (m_MainHeader.check == 6) { // FADC header
299
300 seenHeadersAndTrailers |= 0x2; //we found FADC Header
301
302 fadc = m_MainHeader.FADCnum;
303 trgType = m_MainHeader.trgType;
304 trgNumber = m_MainHeader.trgNumber;
305 daqMode = m_MainHeader.DAQMode;
306 daqType = m_MainHeader.DAQType;
307
308 //Let's add run-dependent info: daqMode="11" in case of 3-mixed-6 sample acquisition mode.
309 if (daqType) daqMode = 3;
310
311 nAPVheaders = 0; // start counting APV headers for this FADC
312 nAPVmatch = true; //assume correct # of APV headers
313 badMapping = false; //assume correct mapping
314 badHeader = false;
315 badTrailer = false;
316
317 is3sampleData = false;
318 is6sampleData = false;
319
320 if (daqMode == 0) B2ERROR("SVDDataFormatCheck: the event " << eventNo <<
321 " is apparently taken with 1-sample mode, this is not expected.");
322 if (daqMode == 1) is3sampleData = true;
323 if (daqMode == 2) is6sampleData = true;
324
325 if (
326 m_MainHeader.trgNumber !=
327 ((eventNo - m_FADCTriggerNumberOffset) & 0xFF)) {
328
331 B2ERROR("Event number mismatch detected! The event number given by EventMetaData object is different from the one in the FADC Header. "
332 << LogVar("Event number", eventNo) << LogVar("FADC", fadc) << LogVar("Trigger number LSByte reported by the FADC",
333 m_MainHeader.trgNumber) << LogVar("+ offset", m_FADCTriggerNumberOffset) << LogVar("expected", (eventNo & 0xFF)));
334 badHeader = true;
335 }
336
337 // create SVDModeByte object from MainHeader vars
338 m_SVDModeByte = SVDModeByte(m_MainHeader.runType, 0, daqMode, m_MainHeader.trgTiming);
339
340 // create SVDEventInfo and fill it with SVDModeByte & SVDTriggerType objects
341 if (!isSetEventInfo) {
343 m_svdEventInfoPtr.create();
344 m_svdEventInfoPtr->setModeByte(m_SVDModeByte);
345 m_svdEventInfoPtr->setTriggerType(m_SVDTriggerType);
346 m_svdEventInfoPtr->setAPVClock(m_hwClock);
347
348 //set relative time shift
349 m_svdEventInfoPtr->setRelativeShift(m_relativeTimeShift);
350 // set X-talk info online from Raw Data
351 m_svdEventInfoPtr->setCrossTalk(m_MainHeader.xTalk);
352
353 isSetEventInfo = true;
354 } else { // let's check if the current SVDModeByte and SVDTriggerType are consistent with the one stored in SVDEventInfo
355 if (m_SVDModeByte != m_svdEventInfoPtr->getModeByte()) {m_svdEventInfoPtr->setMatchModeByte(false); badHeader = true; nEventInfoMatchErrors++;}
356 if (trgType != (m_svdEventInfoPtr->getTriggerType()).getType()) { m_svdEventInfoPtr->setMatchTriggerType(false); badHeader = true; nEventInfoMatchErrors++;}
357 }
358 } // is FADC header
359
360 if (m_APVHeader.check == 2) { // APV header
361
362 nAPVheaders++;
363 apv = m_APVHeader.APVnum;
364 seenAPVHeaders.insert(apv);
365
366 cmc1 = m_APVHeader.CMC1;
367 cmc2 = m_APVHeader.CMC2;
368 apvErrors = m_APVHeader.apvErr;
369 pipAddr = m_APVHeader.pipelineAddr;
370
371 if (apvErrors != 0) {
372 nAPVErrors++;
373 if (!(nAPVErrors % m_errorRate)
374 or nAPVErrors < 100) B2ERROR("APV error has been detected." << LogVar("FADC", fadc) << LogVar("APV", apv) << LogVar("Error value",
375 apvErrors));
376 }
377 // temporary SVDDAQDiagnostic object (no info from trailers and APVmatch code)
378 currentDAQDiagnostic = m_storeDAQDiagnostics.appendNew(trgNumber, trgType, pipAddr, cmc1, cmc2, apvErrors, ftbError, true,
379 nAPVmatch,
380 badHeader, missedHeader, missedTrailer,
381 fadc, apv);
382 vDiagnostic_ptr.push_back(currentDAQDiagnostic);
383
384 apvsByPipeline[pipAddr].insert(make_pair(fadc, apv));
385
386 // Let's check if the data frame does not come after APV header with piplAddr = 255 (special SEU recovery pseudo-data)
387 if (pipAddr == 255) {
388 data32_it++;
389 m_data32 = *data32_it;
390 if (m_data_A.check == 0) B2ERROR("The strip data frame is detected for the special SEU recovery mode. " << LogVar("pipline address",
391 pipAddr) << "This is unexpected!");
392 data32_it--;
393 m_data32 = *data32_it;
394 }
395
396 } //is APV Header
397
398 if (m_data_A.check == 0) { // data
399 strip = m_data_A.stripNum;
400
401 sample[0] = m_data_A.sample1;
402 sample[1] = m_data_A.sample2;
403 sample[2] = m_data_A.sample3;
404
405 sample[3] = 0;
406 sample[4] = 0;
407 sample[5] = 0;
408
409 // Let's check the next rawdata word to determine if we acquired 3 or 6 sample
410 data32_it++;
411 m_data32 = *data32_it;
412
413 if (m_data_B.check == 0 && strip == m_data_B.stripNum) { // 2nd data frame with the same strip number -> six samples
414
415 if (!isSetNAPVsamples) {
416 m_svdEventInfoPtr->setNSamples(6);
417 isSetNAPVsamples = true;
418 } else {
419 if (is3sampleData)
420 B2ERROR("DAQMode value (indicating 3-sample acquisition mode) doesn't correspond to the actual number of samples (6) in the data! The data might be corrupted!");
421 }
422
423 crc16vec.push_back(m_data32);
424
425 sample[3] = m_data_B.sample4;
426 sample[4] = m_data_B.sample5;
427 sample[5] = m_data_B.sample6;
428 }
429
430 else { // three samples
431 data32_it--;
432 m_data32 = *data32_it;
433
434 if (!isSetNAPVsamples) {
435 m_svdEventInfoPtr->setNSamples(3);
436 isSetNAPVsamples = true;
437 } else {
438 if (is6sampleData)
439 B2ERROR("DAQMode value (indicating 6-sample acquisition mode) doesn't correspond to the actual number of samples (3) in the data! The data might be corrupted!");
440 }
441 }
442
443 // Generating SVDShaperDigit object
444 SVDShaperDigit* newShaperDigit = m_map->NewShaperDigit(fadc, apv, strip, sample, 0.0);
445 if (newShaperDigit) {
446 diagnosticMap.insert(make_pair(*newShaperDigit, currentDAQDiagnostic));
447 delete newShaperDigit;
448 } else if (m_badMappingFatal) {
449 B2FATAL("Respective FADC/APV combination not found -->> incorrect payload in the database! ");
450 } else {
451 badMapping = true;
452 }
453
454 } //is data frame
455
456
457 if (m_FADCTrailer.check == 14) { // FADC trailer
458
459 seenHeadersAndTrailers |= 0x4; // we found FADC trailer
460
461 //additional check if we have a faulty/fake FADC that is not in the map
462 if (APVmap->find(fadc) == APVmap->end()) badMapping = true;
463
464 //comparing number of APV chips and the number of APV headers, for the current FADC
465 unsigned short nAPVs = APVmap->count(fadc);
466
467 if (nAPVheaders == 0) {
468 currentDAQDiagnostic = m_storeDAQDiagnostics.appendNew(0, 0, 0, 0, 0, 0, ftbError, true, nAPVmatch, badHeader, 0, 0, fadc, 0);
469 vDiagnostic_ptr.push_back(currentDAQDiagnostic);
470 }
471
472 if (nAPVs != nAPVheaders) {
473 // There is an APV missing, detect which it is.
474 for (const auto& fadcApv : *APVmap) {
475 if (fadcApv.first != fadc) continue;
476 if (seenAPVHeaders.find(fadcApv.second) == seenAPVHeaders.end()) {
477 // We have a missing APV. Look if it is a known one.
478 auto missingRec = m_missingAPVs.find(make_pair(fadcApv.first, fadcApv.second));
479 if (missingRec != m_missingAPVs.end()) {
480 // This is known to be missing, so keep quiet and just update event counters
481 if (missingRec->second.first > eventNo)
482 missingRec->second.first = eventNo;
483 if (missingRec->second.second < eventNo)
484 missingRec->second.second = eventNo;
485 } else {
486 // We haven't seen this previously.
488 m_missingAPVs.insert(make_pair(
489 make_pair(fadcApv.first, fadcApv.second),
490 make_pair(eventNo, eventNo)
491 ));
492 if (!(nMissingAPVsErrors % m_errorRate)) B2ERROR("missing APV header! " << LogVar("Event number", eventNo) << LogVar("APV",
493 int(fadcApv.second)) << LogVar("FADC",
494 int(fadcApv.first)));
495 }
496 }
497 }
498 nAPVmatch = false;
499 } // is nAPVs != nAPVheaders
500
501 seenAPVHeaders.clear();
502
503 ftbFlags = m_FADCTrailer.FTBFlags;
504 if ((ftbFlags >> 5) != 0) badTrailer = true;
505 if (ftbFlags != 0) {
507 if (!(nFTBFlagsErrors % m_errorRate) or nFTBFlagsErrors < 100) {
508 B2ERROR(" FTB Flags variable has an active error bit(s)" << LogVar("on FADC number", fadc));
509
510 if (ftbFlags & 16) B2ERROR("----> CRC error has been detected. Data might be corrupted!");
511 if (ftbFlags & 8) B2ERROR("----> Bad Event indication has been detected. Data might be corrupted!");
512 if (ftbFlags & 4) B2ERROR("----> Double Header has been detected. Data might be corrupted!");
513 if (ftbFlags & 2) B2ERROR("----> Time Out has been detected. Data might be corrupted!");
514 if (ftbFlags & 1) B2ERROR("----> Event Too Long! Data might be corrupted!");
515 }
516 }
517
518 apvErrorsOR = m_FADCTrailer.apvErrOR;
519
520
521 }// is FADC trailer
522
523 if (m_FTBTrailer.controlWord == 0xff55) {// FTB trailer
524
525 seenHeadersAndTrailers |= 0x8; // we found FTB trailer
526
527 //check CRC16
528 crc16vec.pop_back();
529 unsigned short iCRC = crc16vec.size();
530 std::vector<uint32_t> crc16input;
531 crc16input.reserve(iCRC);
532
533 for (unsigned short icrc = 0; icrc < iCRC; icrc++)
534 crc16input.push_back(htonl(crc16vec.at(icrc)));
535
536 //verify CRC16
537 boost::crc_basic<16> bcrc(0x8005, 0xffff, 0, false, false);
538 bcrc.process_bytes(crc16input.data(), crc16input.size() * sizeof(uint32_t));
539 unsigned int checkCRC = bcrc.checksum();
540
541 if (checkCRC != m_FTBTrailer.crc16) {
542 B2WARNING("FTB CRC16 checksum DOES NOT MATCH" << LogVar("for FADC no.", fadc));
544 }
545
546 } // is FTB trailer
547
548 } // end loop over 32-bit frames in each buffer
549
550 //Let's check if all the headers and trailers were in place in the last frame
551 if (seenHeadersAndTrailers != 0xf) {
552 if (!(seenHeadersAndTrailers & 1)) {B2ERROR("Missing FTB Header is detected. SVD data might be corrupted!" << LogVar("Event number", eventNo) << LogVar("FADC", fadc)); missedHeader = true;}
553 if (!(seenHeadersAndTrailers & 2)) {B2ERROR("Missing FADC Header is detected -> related FADC number couldn't be retrieved. SVD data might be corrupted! " << LogVar("Event number", eventNo) << LogVar("previous FADC", fadc)); missedHeader = true;}
554 if (!(seenHeadersAndTrailers & 4)) {B2ERROR("Missing FADC Trailer is detected. SVD data might be corrupted!" << LogVar("Event number", eventNo) << LogVar("FADC", fadc)); missedTrailer = true;}
555 if (!(seenHeadersAndTrailers & 8)) {B2ERROR("Missing FTB Trailer is detected. SVD data might be corrupted!" << LogVar("Event number", eventNo) << LogVar("FADC", fadc)); missedTrailer = true;}
556 }
557
558 for (auto p : vDiagnostic_ptr) {
559 // adding remaining info to Diagnostic object
560 p->setFTBFlags(ftbFlags);
561 p->setApvErrorOR(apvErrorsOR);
562 p->setAPVMatch(nAPVmatch);
563 p->setBadMapping(badMapping);
564 p->setBadTrailer(badTrailer);
565 p->setMissedHeader(missedHeader);
566 p->setMissedTrailer(missedTrailer);
567 }
568
569 vDiagnostic_ptr.clear();
570
571 } // end iteration on 4(COPPER)/48(PCIe40) data buffers
572
573 } // end event loop
574
575 }// end loop over RawSVD objects
576
577 // Check the number of FADC boards
578 if (cntFADCboards != nFADCboards) { // nFADCboards=52
580 if (!(nFADCMatchErrors % m_errorRate)) B2ERROR("Number of data objects in rawSVD do not match the number of FADC boards" <<
581 LogVar("# of data objects in rawSVD",
582 cntFADCboards) << LogVar("# of FADCs", nFADCboards) << LogVar("Event number", eventNo));
583
584 // We override all FADCMatch fields in diagnostics and set it to false.
585 for (auto& p : m_storeDAQDiagnostics) {
586 p.setFADCMatch(false);
587 }
588 }
589
590
591 // Check if we have special data for SEU recovery mode (pipAddr = 0xFF)
592 // If so, let's monitor affected FADC/APV's and the event range with seuRecMap
593 auto itPtr = apvsByPipeline.find(255);
594 if (itPtr != apvsByPipeline.end()) {
595 for (const auto& fadcApv : itPtr->second) {
596
597 auto seuRec = m_seuRecMap.find(make_pair(fadcApv.first, fadcApv.second));
598 if (seuRec != m_seuRecMap.end()) {
599 if (seuRec->second.first > eventNo)
600 seuRec->second.first = eventNo;
601 if (seuRec->second.second < eventNo)
602 seuRec->second.second = eventNo;
603 } else {
605 m_seuRecMap.insert(make_pair(
606 make_pair(fadcApv.first, fadcApv.second),
607 make_pair(eventNo, eventNo)
608 ));
610 B2ERROR("Special Recovery Data (Dummy APV Header) found due to the detection of Single Event Upset (SEU)!!!" << LogVar("APV",
611 int(fadcApv.second)) << LogVar("FADC", int(fadcApv.first)) << LogVar("Event number", eventNo));
612
613 }
614 for (auto& pp : m_storeDAQDiagnostics) {
615 if (pp.getFADCNumber() == fadcApv.first and pp.getAPVNumber() == fadcApv.second)
616 pp.setSEURecoData(true);
617 }
618 }
619 }
620
621
622 // Detect upset APVs and report/treat
623 auto majorAPV = max_element(apvsByPipeline.begin(), apvsByPipeline.end(),
624 [](const decltype(apvsByPipeline)::value_type & p1,
625 const decltype(apvsByPipeline)::value_type & p2) -> bool
626 { return p1.second.size() < p2.second.size(); }
627 );
628 // We set emuPipelineAddress fields in diagnostics to this.
630 for (auto& p : m_storeDAQDiagnostics)
631 p.setEmuPipelineAddress(majorAPV->first);
632
633 unsigned short apvsByPipelineSize = apvsByPipeline.size();
634 if (majorAPV->first == 255) apvsByPipelineSize = 1;
635
636 // And report any upset apvs or update records
637 if (apvsByPipelineSize > 1)
638 for (const auto& p : apvsByPipeline) {
639 if (p.first == majorAPV->first or p.first == 255) continue;
640 for (const auto& fadcApv : p.second) {
641 // We have an upset APV. Look if it is a known one.
642 auto upsetRec = m_upsetAPVs.find(make_pair(fadcApv.first, fadcApv.second));
643 if (upsetRec != m_upsetAPVs.end()) {
644 // This is known to be upset, so keep quiet and update event counters
645 if (upsetRec->second.first > eventNo)
646 upsetRec->second.first = eventNo;
647 if (upsetRec->second.second < eventNo)
648 upsetRec->second.second = eventNo;
649 } else {
650 // We haven't seen this one previously.
652 m_upsetAPVs.insert(make_pair(
653 make_pair(fadcApv.first, fadcApv.second),
654 make_pair(eventNo, eventNo)
655 ));
656
657 if (!(nUpsetAPVsErrors % m_errorRate)) B2ERROR("Upset APV detected!!!" << LogVar("APV", int(fadcApv.second)) << LogVar("FADC",
658 int(fadcApv.first)) << LogVar("Event number", eventNo));
659 }
660
661 for (auto& pp : m_storeDAQDiagnostics) {
662
663 if (pp.getFADCNumber() == fadcApv.first and pp.getAPVNumber() == fadcApv.second)
664 pp.setUpsetAPV(true);
665 }
666
667 }
668 }
669
670 // Here we can delete digits coming from upset APVs. We detect them by comparing
671 // actual and emulated pipeline address fields in DAQDiagnostics.
672 for (auto& p : diagnosticMap) {
673
674 if ((m_killUpsetDigits && p.second->getPipelineAddress() != p.second->getEmuPipelineAddress()) || p.second->getFTBError() != 240
675 || p.second->getFTBFlags() || p.second->getAPVError() || !(p.second->getAPVMatch()) || !(p.second->getFADCMatch())
676 || p.second->getBadHeader()
677 || p.second->getBadMapping() || p.second->getUpsetAPV() || p.second->getMissedHeader() || p.second->getMissedTrailer()) continue;
678 m_storeShaperDigits.appendNew(p.first);
679 }
680
681 if (!m_svdEventInfoPtr->getMatchTriggerType()) {if (!(nEventInfoMatchErrors % m_errorRate) or nEventInfoMatchErrors < 200) B2WARNING("Inconsistent SVD Trigger Type value for: " << LogVar("Event number", eventNo));}
682 if (!m_svdEventInfoPtr->getMatchModeByte()) {if (!(nEventInfoMatchErrors % m_errorRate) or nEventInfoMatchErrors < 200) B2WARNING("Inconsistent SVD ModeByte object for: " << LogVar("Event number", eventNo));}
683
684
685} //end event function
686#ifndef __clang__
687#pragma GCC diagnostic pop
688#endif
689
691{
692
693 // Summary report on missing APVs
694 if (m_missingAPVs.size() > 0) {
695 B2WARNING("SVDUnpacker summary 1: Missing APVs");
696 for (const auto& miss : m_missingAPVs)
697 B2WARNING(LogVar("Missing APV", miss.first.second) << LogVar("FADC", miss.first.first) << LogVar("since event",
698 miss.second.first) << LogVar("to event", miss.second.second));
699 }
700 if (m_upsetAPVs.size() > 0) {
701 B2WARNING("SVDUnpacker summary 2: Upset APVs");
702 for (const auto& upst : m_upsetAPVs)
703 B2WARNING(LogVar("Upset APV", upst.first.second) << LogVar("FADC", upst.first.first) <<
704 LogVar("since event", upst.second.first) << LogVar("to event", upst.second.second));
705 }
706
707 if (m_seuRecMap.size() > 0) {
708 B2WARNING("SVDUnpacker summary 2: Special SEU Recovery Data");
709 for (const auto& seu : m_seuRecMap)
710 B2WARNING(LogVar("SEU recovery data: APV", seu.first.second) << LogVar("FADC", seu.first.first) <<
711 LogVar("since event", seu.second.first) << LogVar("to event", seu.second.second));
712 }
713
714}
715
716
717// additional printing function
718void SVDUnpackerModule::printB2Debug(const uint32_t* data32, const uint32_t* data32_min, const uint32_t* data32_max, int nWords)
719{
720
721 const uint32_t* min = std::max((data32 - nWords), data32_min);
722 const uint32_t* max = std::min((data32 + nWords), data32_max);
723
724 size_t counter{0};
725 std::stringstream os;
726 os << std::hex << std::setfill('0');
727 for (const uint32_t* ptr = min; ptr <= max; ++ptr) {
728 os << std::setw(8) << *ptr;
729 if (++counter % 10 == 0) os << std::endl;
730 else os << " ";
731 }
732
733 os << std::endl;
734 B2INFO(os.str());
735 return;
736
737}
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition DataStore.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
Module()
Constructor.
Definition Module.cc:30
@ 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
Class to store SVD DAQ diagnostic information.
Class to store SVD mode information.
Definition SVDModeByte.h:69
The SVD ShaperDigit class.
Class to store Trigger Type information.
int m_wrongFTBcrc
FTB CRC no-Match counter.
StoreArray< SVDDAQDiagnostic > m_storeDAQDiagnostics
SVDDAQDiagnostic array.
unsigned short seenHeadersAndTrailers
this 4-bits value should be 1111 if no headers/trailers are missing
int m_shutUpFTBError
regulates the number of "Event number mismatch" errors reported
MainHeader m_MainHeader
Implementation of FADC Header.
StoreArray< RawSVD > m_rawSVD
output for RawSVD
FADCTrailer m_FADCTrailer
Implementation of FADC Trailer.
StoreObjPtr< SVDEventInfo > m_svdEventInfoPtr
SVDEventInfo output per event.
virtual void initialize() override
Initializes the Module.
StoreArray< SVDShaperDigit > m_storeShaperDigits
SVDShaperDigit array.
virtual void event() override
event
SVDUnpackerModule()
Constructor of the module.
int nAPVErrors
counter of APV errors
FTBHeader m_FTBHeader
Implementation of FTB Header.
virtual void endRun() override
end run
std::string m_rawSVDListName
RawSVD StoreArray name.
DBObjPtr< PayloadFile > m_mapping
pointer to the payload with the mapping
int nFADCMatchErrors
counter of FADC boards =/= n of RawData objects errors
bool m_killUpsetDigits
Optionally, we can kill digits coming from upset APVs right in the unpacker.
data_B m_data_B
Implementation of 2nd data word.
int m_FADCTriggerNumberOffset
FADC Trigger Offset.
std::map< std::pair< unsigned short, unsigned short >, std::pair< std::size_t, std::size_t > > m_missingAPVs
Map to store a list of missing APVs.
int nMissingAPVsErrors
counter of missing APVs errors
static std::string m_xmlFileName
XML filename.
int nEventMatchErrors
counter of Event match errors
FTBTrailer m_FTBTrailer
Implementation of FTB Trailer.
int m_errorRate
The parameter that indicates what fraction of B2ERRORs messages should be suppressed to not overload ...
APVHeader m_APVHeader
Implementation of APV Header.
int nUpsetAPVsErrors
counter of upset APV errors
virtual void beginRun() override
begin run
std::string m_svdShaperDigitListName
SVDShaperDigit StoreArray name.
DBObjPtr< HardwareClockSettings > m_hwClock
system clock
std::string m_svdEventInfoName
SVDEventInfo name.
DBObjPtr< SVDGlobalConfigParameters > m_svdGlobalConfig
SVDGlobal Configuration payload.
bool m_printRaw
Optionally we can get printout of Raw Data words.
int nEventInfoMatchErrors
counter of inconsistencies in SVDEventInfo within an event
bool m_badMappingFatal
Optionally we can stop the unpacking if there is a missing APV/FADC combination in the mapping -> wro...
int nErrorFieldErrors
counter of event mismatch errors in FTB's ErrorField
std::map< std::pair< unsigned short, unsigned short >, std::pair< std::size_t, std::size_t > > m_seuRecMap
Map to store a list of APVs for special data for SEU recovery.
data_A m_data_A
Implementation of 1st data word.
uint32_t m_data32
Input 32-bit data word.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Required input for EventMetaData.
bool m_silentAppend
Silently append new SVDShaperDigits to a pre-existing non-empty SVDShaperDigits storeArray.
std::unordered_multimap< unsigned char, unsigned char > * APVmap
pointer to APVforFADCmap filled by mapping procedure
SVDTriggerType m_SVDTriggerType
SVDTriggerType object.
SVDModeByte m_SVDModeByte
instance of SVDModeByte for the event
virtual ~SVDUnpackerModule() override
Destructor of the module.
std::unique_ptr< SVDOnlineToOfflineMap > m_map
Pointer to online-to-offline map.
int m_relativeTimeShift
latency difference between the 3- and 6-sample acquired events in usint of APV clock / 4,...
std::map< std::pair< unsigned short, unsigned short >, std::pair< std::size_t, std::size_t > > m_upsetAPVs
Map to store a list of upset APVs.
int nFTBFlagsErrors
counter of errors in FTBFlags variable
int nSEURecoveryCase
counter of SEU Special Recovery data cases
bool m_emulatePipelineAddress
Software emulation of pipeline address This is a replacement of hardware pipeline address emulation.
std::string m_svdDAQDiagnosticsListName
SVDDAQDiagnostic StoreArray name.
int nTriggerMatchErrors
counters for specific ERRORS produced by the Unpacker
static void printB2Debug(const uint32_t *data32, const uint32_t *data32_min, const uint32_t *data32_max, int nWords)
additional function that prints raw data words
unsigned short nFADCboards
how many FADCs we have
Class to store variables with their name which were sent to the logging service.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
Abstract base class for different kinds of events.
STL namespace.