1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This class provides access to Altro digits in raw data.
22 /// It loops over all Altro digits in the raw data given by the AliRawReader.
23 /// The Next method goes to the next digit. If there are no digits left
24 /// it returns kFALSE.
25 /// Several getters provide information about the current digit.
27 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliAltroRawStream.h"
30 #include "AliRawReader.h"
33 ClassImp(AliAltroRawStream)
36 //_____________________________________________________________________________
37 AliAltroRawStream::AliAltroRawStream(AliRawReader* rawReader) :
38 fNoAltroMapping(kTRUE),
39 fIsShortDataHeader(kFALSE),
50 fRawReader(rawReader),
55 fRCUTrailerData(NULL),
67 // create an object to read Altro raw digits
68 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
71 //_____________________________________________________________________________
72 AliAltroRawStream::AliAltroRawStream(const AliAltroRawStream& stream) :
74 fNoAltroMapping(stream.fNoAltroMapping),
75 fIsShortDataHeader(stream.fIsShortDataHeader),
76 fDDLNumber(stream.fDDLNumber),
77 fPrevDDLNumber(stream.fPrevDDLNumber),
78 fRCUId(stream.fRCUId),
79 fPrevRCUId(stream.fPrevRCUId),
80 fHWAddress(stream.fHWAddress),
81 fPrevHWAddress(stream.fPrevHWAddress),
83 fPrevTime(stream.fPrevTime),
84 fSignal(stream.fSignal),
85 fTimeBunch(stream.fTimeBunch),
86 fRawReader(stream.fRawReader),
88 fPosition(stream.fPosition),
89 fCount(stream.fCount),
90 fBunchLength(stream.fBunchLength),
91 fRCUTrailerData(stream.fRCUTrailerData),
92 fRCUTrailerSize(stream.fRCUTrailerSize),
93 fFECERRA(stream.fFECERRA),
94 fFECERRB(stream.fFECERRB),
95 fERRREG2(stream.fERRREG2),
96 fERRREG3(stream.fERRREG3),
97 fERRREG4(stream.fERRREG4),
98 fActiveFECsA(stream.fActiveFECsA),
99 fActiveFECsB(stream.fActiveFECsB),
100 fAltroCFG1(stream.fAltroCFG1),
101 fAltroCFG2(stream.fAltroCFG2)
103 fSegmentation[0] = stream.fSegmentation[0];
104 fSegmentation[1] = stream.fSegmentation[1];
105 fSegmentation[2] = stream.fSegmentation[2];
108 //_____________________________________________________________________________
109 AliAltroRawStream& AliAltroRawStream::operator = (const AliAltroRawStream& stream)
111 if(&stream == this) return *this;
113 fNoAltroMapping = stream.fNoAltroMapping;
114 fIsShortDataHeader = stream.fIsShortDataHeader;
115 fDDLNumber = stream.fDDLNumber;
116 fPrevDDLNumber = stream.fPrevDDLNumber;
117 fRCUId = stream.fRCUId;
118 fPrevRCUId = stream.fPrevRCUId;
119 fHWAddress = stream.fHWAddress;
120 fPrevHWAddress = stream.fPrevHWAddress;
121 fTime = stream.fTime;
122 fPrevTime = stream.fPrevTime;
123 fSignal = stream.fSignal;
124 fTimeBunch = stream.fTimeBunch;
125 fRawReader = stream.fRawReader;
126 fData = stream.fData;
127 fPosition = stream.fPosition;
128 fCount = stream.fCount;
129 fBunchLength = stream.fBunchLength;
130 fRCUTrailerData = stream.fRCUTrailerData;
131 fRCUTrailerSize = stream.fRCUTrailerSize;
132 fFECERRA = stream.fFECERRA;
133 fFECERRB = stream.fFECERRB;
134 fERRREG2 = stream.fERRREG2;
135 fERRREG3 = stream.fERRREG3;
136 fERRREG4 = stream.fERRREG4;
137 fActiveFECsA = stream.fActiveFECsA;
138 fActiveFECsB = stream.fActiveFECsB;
139 fAltroCFG1 = stream.fAltroCFG1;
140 fAltroCFG2 = stream.fAltroCFG2;
142 fSegmentation[0] = stream.fSegmentation[0];
143 fSegmentation[1] = stream.fSegmentation[1];
144 fSegmentation[2] = stream.fSegmentation[2];
149 //_____________________________________________________________________________
150 AliAltroRawStream::~AliAltroRawStream()
156 //_____________________________________________________________________________
157 void AliAltroRawStream::Reset()
159 // reset altro raw stream params
161 fPosition = fCount = fBunchLength = 0;
163 fRCUTrailerData = NULL;
166 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fERRREG4 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
168 fDDLNumber = fPrevDDLNumber = fRCUId = fPrevRCUId = fHWAddress = fPrevHWAddress = fTime = fPrevTime = fSignal = fTimeBunch = -1;
170 if (fRawReader) fRawReader->Reset();
172 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
175 //_____________________________________________________________________________
176 Bool_t AliAltroRawStream::Next()
178 // read the next raw digit
179 // returns kFALSE if there is no digit left
181 fPrevDDLNumber = fDDLNumber;
183 fPrevHWAddress = fHWAddress;
186 while (fCount == 0) { // next trailer
187 while (fPosition <= 0) { // next payload
189 if (!fRawReader->ReadNextData(fData)) return kFALSE;
190 } while (fRawReader->GetDataSize() == 0);
192 fDDLNumber = fRawReader->GetDDLID();
194 fPosition = GetPosition();
202 if (fBunchLength == 0) ReadBunch();
210 //_____________________________________________________________________________
211 void AliAltroRawStream::SelectRawData(Int_t detId)
213 // Select the raw data for specific
215 AliDebug(1,Form("Selecting raw data for detector %d",detId));
216 fRawReader->Select(detId);
219 //_____________________________________________________________________________
220 void AliAltroRawStream::SelectRawData(const char *detName)
222 // Select the raw data for specific
224 AliDebug(1,Form("Selecting raw data for detector %s",detName));
225 fRawReader->Select(detName);
228 //_____________________________________________________________________________
229 UShort_t AliAltroRawStream::GetNextWord()
231 // Read the next 10 bit word in backward direction
232 // The input stream access is given by fData and fPosition
236 Int_t iBit = fPosition * 10;
237 Int_t iByte = iBit / 8;
238 Int_t shift = iBit % 8;
240 // the raw data is written as integers where the low bits are filled first
241 // -> little endian is assumed here !
242 Int_t iByteLow = iByte;
244 Int_t iByteHigh = iByte;
245 return ((fData[iByteHigh] * 256 + fData[iByteLow]) >> shift) & 0x03FF;
248 //_____________________________________________________________________________
249 Bool_t AliAltroRawStream::ReadTrailer()
251 //Read a trailer of 40 bits in the backward reading mode
252 //In case of no mapping is provided, read a dummy trailer
253 if (fNoAltroMapping) {
254 AliError("No ALTRO mapping information is loaded! Reading a dummy trailer!");
255 return ReadDummyTrailer();
258 //First reading filling words
260 Int_t nFillWords = 0;
261 while ((temp = GetNextWord()) == 0x2AA) nFillWords++;
262 if (nFillWords == 0) {
263 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no 0x2AA");
265 AliWarning(Form("Incorrect trailer found ! Expected 0x2AA not found (0x%x != 0x2AA) ! Current position %d, DDL=%d",
266 temp,fPosition,fDDLNumber));
267 // trying to recover and find the next bunch
268 while ((fPosition > 2) && (temp != 0x2AA)) temp = GetNextWord();
270 fCount = fPosition = 0;
273 temp = GetNextWord();
276 //Then read the trailer
278 fRawReader->AddMajorErrorLog(kAltroTrailerErr,Form("size %d < 5",
281 AliWarning(Form("Incorrect raw data size ! Expected at least 5 words but found %d !",fPosition));
282 fCount = fPosition = 0;
286 fCount = (temp << 4) & 0x3FF;
287 if ((temp >> 6) != 0xA) {
288 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no 0xA");
290 AliWarning(Form("Incorrect trailer found ! Expecting 0xA but found 0x%x !",temp >> 6));
295 temp = GetNextWord();
296 fHWAddress = (temp & 0x3) << 10;
297 if (((temp >> 2) & 0xF) != 0xA) {
298 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no second 0xA");
300 AliWarning(Form("Incorrect trailer found ! Expecting second 0xA but found 0x%x !",(temp >> 2) & 0xF));
304 fCount |= ((temp & 0x3FF) >> 6);
306 if (fCount >= fPosition) {
307 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"invalid size");
309 AliWarning(Form("Incorrect trailer found ! The altro payload size is invalid (%d >= %d) !",fCount,fPosition));
313 temp = GetNextWord();
316 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
321 //_____________________________________________________________________________
322 Bool_t AliAltroRawStream::ReadDummyTrailer()
324 //Read a trailer of 40 bits in the backward reading mode
325 //In case of no mapping is provided, read a dummy trailer
327 while ((temp = GetNextWord()) == 0x2AA);
329 fSegmentation[0] = temp;
330 fSegmentation[1] = GetNextWord();
331 fSegmentation[2] = GetNextWord();
332 fCount = GetNextWord();
333 if (fCount == 0) return kFALSE;
336 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
341 //_____________________________________________________________________________
342 void AliAltroRawStream::ReadBunch()
344 // Read altro payload in
345 // backward direction
347 fRawReader->AddMinorErrorLog(kBunchLengthReadErr,"");
349 AliWarning(Form("Could not read bunch length and time bin ! Only %d 10-bit words are left !",fCount));
350 fBunchLength = fTimeBunch = fCount = 0;
354 fBunchLength = GetNextWord() - 2;
355 if (fBunchLength > fCount) {
356 fRawReader->AddMinorErrorLog(kBunchLengthReadErr,Form("bl=%d",fBunchLength));
358 AliWarning(Form("Could not read bunch length ! Bunch length = %d (>%d)",fBunchLength,fCount));
359 fBunchLength = fTimeBunch = fCount = 0;
362 fTimeBunch = fBunchLength;
365 fTime = GetNextWord();
371 //_____________________________________________________________________________
372 void AliAltroRawStream::ReadAmplitude()
374 // Read next time bin amplitude
376 fRawReader->AddMinorErrorLog(kAmplitudeReadErr,"");
378 AliWarning("Could not read sample amplitude !");
379 fCount = fSignal = fBunchLength = 0;
383 fSignal = GetNextWord();
391 //_____________________________________________________________________________
392 Int_t AliAltroRawStream::GetPosition()
394 // Sets the position in the
396 // Read the RCU trailer
397 // This includes the trailer size,
398 // RCU identifier and raw data payload.
399 // The RCU trailer format is described
400 // in details in the RCU manual.
402 // We use the last word of the payload
403 // in order to decide which RCU firmware
404 // was used during the data taking.
405 // The firmware v2 adds 0xAAAA as 16
406 // most significant bits and since the
407 // payload size (firmware v1) can not be
408 // that big, we use this as a unique
409 // label of the firmware version.
411 Int_t index = fRawReader->GetDataSize();
412 UInt_t word = Get32bitWord(index);
413 if (((word >> 16) == 0xaaaa) || (word == 2)) {
414 // This is RCU formware v2
415 // The statement word==2 is needed only temporary
416 // in order to be able to read previously generated
419 Int_t trailerSize = 0;
421 AliInfo("Old simulated raw data is assumed!");
426 // First read 32-bit word with the
427 // trailer size (7 bits), RCU ID (9 bits) and
428 // 0xAAA (the rest - 16 bits)
429 fRCUId = (Int_t)((word >> 7) & 0x1ff);
430 trailerSize = (word & 0x7F);
433 // Now read the beginning of the trailer
434 // where the payload size is written
435 if (trailerSize < 2) {
436 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
438 AliWarning(Form("Invalid trailer size found (%d bytes) !",
442 Int_t position = ReadRCUTrailer(index,trailerSize);
443 // The size is specified in a number of 40bits
444 // Therefore we need to transform it to number of bytes
447 // Check the consistency of the header and trailer
448 if (((fRawReader->GetDataSize() - trailerSize*4) < position) ||
449 ((fRawReader->GetDataSize() - trailerSize*4) >= (position + 4))) {
450 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
451 fRawReader->GetDataSize(),
454 AliWarning(Form("Inconsistent raw data size ! Raw data size - %d bytes (from the header), RCU trailer - %d bytes, raw data paylod - %d bytes !",
455 fRawReader->GetDataSize(),
458 position = fRawReader->GetDataSize() - trailerSize*4;
461 return position * 8 / 10;
464 // In case of the Old RCU trailer format
465 // we have to read just the size of altro payload
466 // in units of 40-bit words
467 Int_t position = (Int_t)word;
471 fRCUTrailerData = NULL;
473 // The size is specified in a number of 40bits
474 // Therefore we need to transform it to number of bytes
477 if (!fIsShortDataHeader) {
479 // Check the consistency of the header and trailer
480 if (((fRawReader->GetDataSize() - 4) < position) ||
481 ((fRawReader->GetDataSize() - 4) >= (position + 4))) {
482 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d rcu=%d bytes",
483 fRawReader->GetDataSize()-4,
486 AliWarning(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
487 fRawReader->GetDataSize()-4,
489 position = fRawReader->GetDataSize()-4;
493 // Check the consistency of the header and trailer
494 // In this case the header is shorter by 4 bytes
495 if ((fRawReader->GetDataSize() < position) ||
496 (fRawReader->GetDataSize() >= (position + 4))) {
497 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d rcu=%d bytes",
498 fRawReader->GetDataSize(),
501 AliWarning(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
502 fRawReader->GetDataSize(),
504 position = fRawReader->GetDataSize();
507 // 7 32-bit words Common Data Header
508 // therefore we have to shift back by 4 bytes
509 // the pointer to the raw data payload
513 // Return the position in units of 10-bit words
514 return position*8/10;
518 //_____________________________________________________________________________
519 UInt_t AliAltroRawStream::Get32bitWord(Int_t &index)
521 // This method returns the 32 bit word at a given
522 // position inside the raw data payload.
523 // The 'index' points to the beginning of the next word.
524 // The method is supposed to be endian (platform)
528 AliFatal("Raw data paylod buffer is not yet initialized !");
532 fRawReader->AddFatalErrorLog(k32bitWordReadErr,Form("pos = %d",index));
534 AliWarning(Form("Invalid raw data payload position (%d) !",index));
538 word = fData[--index] << 24;
539 word |= fData[--index] << 16;
540 word |= fData[--index] << 8;
541 word |= fData[--index];
546 //_____________________________________________________________________________
547 Int_t AliAltroRawStream::ReadRCUTrailer(Int_t &index, Int_t trailerSize)
549 // The method decodes the RCU trailer data
550 // according to the RCU fw ver.2 specs
552 fRCUTrailerSize = trailerSize*4;
554 for (trailerSize -= 2; trailerSize > 0; trailerSize--) {
555 Int_t word = Get32bitWord(index);
556 Int_t parCode = word >> 26;
557 Int_t parData = word & 0x3FFFFFF;
561 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
562 fFECERRB = ((parData & 0x1FFF)) << 7;
566 fERRREG2 = parData & 0x1FF;
570 fERRREG3 = parData & 0xFFF;
574 fERRREG4 = parData & 0xFFF;
578 fActiveFECsA = parData & 0xFFFF;
582 fActiveFECsB = parData & 0xFFFF;
586 fAltroCFG1 = parData & 0x7FFFF;
590 fAltroCFG2 = parData & 0x1FFFFFF;
593 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
594 AliWarning(Form("Undefined parameter code %d, ignore it !",
601 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
603 fRawReader->GetDataSize()));
604 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
606 fRawReader->GetDataSize()));
608 fRCUTrailerSize = trailerSize*4;
609 fRCUTrailerData = fData + index;
611 Int_t position = Get32bitWord(index);
617 //_____________________________________________________________________________
618 Double_t AliAltroRawStream::GetTSample() const
620 // Returns the sampling time
621 // in seconds. In case the rcu trailer
622 // was note read, return an invalid number (0)
624 if (!fRCUTrailerData) return 0.;
626 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
627 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
632 tSample = 2.0*kLHCTimeSample;
636 tSample = 4.0*kLHCTimeSample;
640 tSample = 8.0*kLHCTimeSample;
643 AliWarning(Form("Invalid sampling frequency value %d !",
652 //_____________________________________________________________________________
653 Double_t AliAltroRawStream::GetL1Phase() const
655 // Returns the L1 phase w.r.t to the
657 if (!fRCUTrailerData) return 0.;
659 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
660 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
662 Double_t tSample = GetTSample();
663 if (phase >= tSample) {
664 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
672 //_____________________________________________________________________________
673 void AliAltroRawStream::PrintRCUTrailer() const
675 // Prints the contents of
676 // the RCU trailer data
677 printf("RCU trailer:\n===========\n");
678 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
679 printf("ERRREG2: 0x%x\n",fERRREG2);
680 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
681 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
682 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
683 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
684 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
685 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
686 printf("GlitchFilter: %d\n",GetGlitchFilter());
687 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
688 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
689 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
690 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
691 printf("Sparse readout: %d\n",GetSparseRO());
692 printf("Sampling time: %e s\n",GetTSample());
693 printf("L1 Phase: %e s\n",GetL1Phase());
694 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
695 printf("===========\n");
698 //_____________________________________________________________________________
699 Bool_t AliAltroRawStream::GetRCUTrailerData(UChar_t*& data) const
701 // Return a pointer to the RCU trailer
702 // data. Should be called always after
703 // the RCU trailer was already processed
704 // in the GetPosition() method
705 if (!fRCUTrailerSize || !fRCUTrailerData) {
706 AliError("No valid RCU trailer data is found !");
711 data = fRCUTrailerData;
716 //_____________________________________________________________________________
717 void AliAltroRawStream::PrintDebug() const
719 // The method prints all the available
720 // debug information.
721 // Its is used in case of decoding errors.
723 AliError("Start of debug printout\n--------------------");
726 if (fRawReader) fRawReader->Dump();
728 AliError("End of debug printout\n--------------------");
731 //_____________________________________________________________________________
732 Int_t AliAltroRawStream::GetBranch() const
734 // The method provides the RCU branch index (0 or 1)
735 // for the current hardware address.
736 // In case the hardware address has not been yet
737 // initialized, the method returns -1
738 if (fHWAddress == -1) return -1;
740 return ((fHWAddress >> 11) & 0x1);
743 //_____________________________________________________________________________
744 Int_t AliAltroRawStream::GetFEC() const
746 // The method provides the front-end card index
747 // for the current hardware address.
748 // In case the hardware address has not been yet
749 // initialized, the method returns -1
750 if (fHWAddress == -1) return -1;
752 return ((fHWAddress >> 7) & 0xF);
755 //_____________________________________________________________________________
756 Int_t AliAltroRawStream::GetAltro() const
758 // The method provides the altro chip index
759 // for the current hardware address.
760 // In case the hardware address has not been yet
761 // initialized, the method returns -1
762 if (fHWAddress == -1) return -1;
764 return ((fHWAddress >> 4) & 0x7);
767 //_____________________________________________________________________________
768 Int_t AliAltroRawStream::GetChannel() const
770 // The method provides the channel index
771 // for the current hardware address.
772 // In case the hardware address has not been yet
773 // initialized, the method returns -1
774 if (fHWAddress == -1) return -1;
776 return (fHWAddress & 0xF);
779 //_____________________________________________________________________________
780 void AliAltroRawStream::AddMappingErrorLog(const char *message)
782 // Signal a mapping error
783 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
784 // classes in order to log an error related to bad altro mapping
786 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);