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 fIsShortDataHeader(kFALSE),
49 fRawReader(rawReader),
53 fChannelPayloadSize(-1),
55 fRCUTrailerData(NULL),
67 // create an object to read Altro raw digits
70 //_____________________________________________________________________________
71 AliAltroRawStream::AliAltroRawStream(const AliAltroRawStream& stream) :
73 fIsShortDataHeader(stream.fIsShortDataHeader),
74 fDDLNumber(stream.fDDLNumber),
75 fPrevDDLNumber(stream.fPrevDDLNumber),
76 fRCUId(stream.fRCUId),
77 fPrevRCUId(stream.fPrevRCUId),
78 fHWAddress(stream.fHWAddress),
79 fPrevHWAddress(stream.fPrevHWAddress),
81 fPrevTime(stream.fPrevTime),
82 fSignal(stream.fSignal),
83 fTimeBunch(stream.fTimeBunch),
84 fRawReader(stream.fRawReader),
86 fPosition(stream.fPosition),
87 fCount(stream.fCount),
88 fChannelPayloadSize(stream.fChannelPayloadSize),
89 fBunchLength(stream.fBunchLength),
90 fRCUTrailerData(stream.fRCUTrailerData),
91 fRCUTrailerSize(stream.fRCUTrailerSize),
92 fFECERRA(stream.fFECERRA),
93 fFECERRB(stream.fFECERRB),
94 fERRREG2(stream.fERRREG2),
95 fERRREG3(stream.fERRREG3),
96 fERRREG4(stream.fERRREG4),
97 fActiveFECsA(stream.fActiveFECsA),
98 fActiveFECsB(stream.fActiveFECsB),
99 fAltroCFG1(stream.fAltroCFG1),
100 fAltroCFG2(stream.fAltroCFG2)
104 //_____________________________________________________________________________
105 AliAltroRawStream& AliAltroRawStream::operator = (const AliAltroRawStream& stream)
107 if(&stream == this) return *this;
109 fIsShortDataHeader = stream.fIsShortDataHeader;
110 fDDLNumber = stream.fDDLNumber;
111 fPrevDDLNumber = stream.fPrevDDLNumber;
112 fRCUId = stream.fRCUId;
113 fPrevRCUId = stream.fPrevRCUId;
114 fHWAddress = stream.fHWAddress;
115 fPrevHWAddress = stream.fPrevHWAddress;
116 fTime = stream.fTime;
117 fPrevTime = stream.fPrevTime;
118 fSignal = stream.fSignal;
119 fTimeBunch = stream.fTimeBunch;
120 fRawReader = stream.fRawReader;
121 fData = stream.fData;
122 fPosition = stream.fPosition;
123 fCount = stream.fCount;
124 fChannelPayloadSize= stream.fChannelPayloadSize;
125 fBunchLength = stream.fBunchLength;
126 fRCUTrailerData = stream.fRCUTrailerData;
127 fRCUTrailerSize = stream.fRCUTrailerSize;
128 fFECERRA = stream.fFECERRA;
129 fFECERRB = stream.fFECERRB;
130 fERRREG2 = stream.fERRREG2;
131 fERRREG3 = stream.fERRREG3;
132 fERRREG4 = stream.fERRREG4;
133 fActiveFECsA = stream.fActiveFECsA;
134 fActiveFECsB = stream.fActiveFECsB;
135 fAltroCFG1 = stream.fAltroCFG1;
136 fAltroCFG2 = stream.fAltroCFG2;
141 //_____________________________________________________________________________
142 AliAltroRawStream::~AliAltroRawStream()
148 //_____________________________________________________________________________
149 void AliAltroRawStream::Reset()
151 // reset altro raw stream params
153 fPosition = fCount = fBunchLength = 0;
154 fChannelPayloadSize = -1;
156 fRCUTrailerData = NULL;
159 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fERRREG4 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
161 fDDLNumber = fPrevDDLNumber = fRCUId = fPrevRCUId = fHWAddress = fPrevHWAddress = fTime = fPrevTime = fSignal = fTimeBunch = -1;
163 if (fRawReader) fRawReader->Reset();
166 //_____________________________________________________________________________
167 Bool_t AliAltroRawStream::Next()
169 // read the next raw digit
170 // returns kFALSE if there is no digit left
172 fPrevDDLNumber = fDDLNumber;
174 fPrevHWAddress = fHWAddress;
177 while (fCount == 0) { // next trailer
178 while (fPosition <= 0) { // next payload
180 if (!fRawReader->ReadNextData(fData)) return kFALSE;
181 } while (fRawReader->GetDataSize() == 0);
183 fDDLNumber = fRawReader->GetDDLID();
185 fPosition = GetPosition();
193 if (fBunchLength == 0) ReadBunch();
201 //_____________________________________________________________________________
202 Bool_t AliAltroRawStream::NextDDL(UChar_t *data)
206 if (!fRawReader->ReadNextData(fData)) return kFALSE;
207 } while (fRawReader->GetDataSize() == 0);
213 fDDLNumber = fRawReader->GetDDLID();
214 fChannelPayloadSize = -1;
215 fPosition = GetPosition();
220 //_____________________________________________________________________________
221 Bool_t AliAltroRawStream::NextChannel()
223 if (fPosition <= 0) return kFALSE;
230 //_____________________________________________________________________________
231 Bool_t AliAltroRawStream::NextBunch(UShort_t *bunchData,
235 if (fCount == 0) return kFALSE;
238 bunchLength = fTimeBunch;
239 startTimeBin = fTime;
241 while (fBunchLength > 0) {
243 bunchData[bunchLength-fBunchLength-1] = fSignal;
249 //_____________________________________________________________________________
250 void AliAltroRawStream::SelectRawData(Int_t detId)
252 // Select the raw data for specific
254 AliDebug(1,Form("Selecting raw data for detector %d",detId));
255 fRawReader->Select(detId);
258 //_____________________________________________________________________________
259 void AliAltroRawStream::SelectRawData(const char *detName)
261 // Select the raw data for specific
263 AliDebug(1,Form("Selecting raw data for detector %s",detName));
264 fRawReader->Select(detName);
267 //_____________________________________________________________________________
268 UShort_t AliAltroRawStream::GetNextWord()
270 // Read the next 10 bit word in backward direction
271 // The input stream access is given by fData and fPosition
275 Int_t iBit = fPosition * 10;
276 Int_t iByte = iBit / 8;
277 Int_t shift = iBit % 8;
279 // the raw data is written as integers where the low bits are filled first
280 // -> little endian is assumed here !
281 Int_t iByteLow = iByte;
283 Int_t iByteHigh = iByte;
284 return ((fData[iByteHigh] * 256 + fData[iByteLow]) >> shift) & 0x03FF;
287 //_____________________________________________________________________________
288 Bool_t AliAltroRawStream::ReadTrailer()
290 //Read a trailer of 40 bits in the backward reading mode
291 //First reading filling words
293 Int_t nFillWords = 0;
294 while ((temp = GetNextWord()) == 0x2AA) nFillWords++;
295 if (nFillWords == 0) {
296 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no 0x2AA");
298 AliWarning(Form("Incorrect trailer found ! Expected 0x2AA not found (0x%x != 0x2AA) ! Current position %d, DDL=%d",
299 temp,fPosition,fDDLNumber));
300 // trying to recover and find the next bunch
301 while ((fPosition > 2) && (temp != 0x2AA)) temp = GetNextWord();
303 fCount = fPosition = 0;
306 temp = GetNextWord();
309 //Then read the trailer
311 fRawReader->AddMajorErrorLog(kAltroTrailerErr,Form("size %d < 5",
314 AliWarning(Form("Incorrect raw data size ! Expected at least 5 words but found %d !",fPosition));
315 fCount = fPosition = 0;
319 fCount = (temp << 4) & 0x3FF;
320 if ((temp >> 6) != 0xA) {
321 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no 0xA");
323 AliWarning(Form("Incorrect trailer found ! Expecting 0xA but found 0x%x !",temp >> 6));
328 temp = GetNextWord();
329 fHWAddress = (temp & 0x3) << 10;
330 if (((temp >> 2) & 0xF) != 0xA) {
331 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"no second 0xA");
333 AliWarning(Form("Incorrect trailer found ! Expecting second 0xA but found 0x%x !",(temp >> 2) & 0xF));
337 fCount |= ((temp & 0x3FF) >> 6);
338 fChannelPayloadSize = fCount;
340 if (fCount >= fPosition) {
341 fRawReader->AddMajorErrorLog(kAltroTrailerErr,"invalid size");
343 AliWarning(Form("Incorrect trailer found ! The altro payload size is invalid (%d >= %d) !",fCount,fPosition));
347 temp = GetNextWord();
350 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
355 //_____________________________________________________________________________
356 void AliAltroRawStream::ReadBunch()
358 // Read altro payload in
359 // backward direction
361 fRawReader->AddMinorErrorLog(kBunchLengthReadErr,"");
363 AliWarning(Form("Could not read bunch length and time bin ! Only %d 10-bit words are left !",fCount));
364 fBunchLength = fTimeBunch = fCount = 0;
368 fBunchLength = GetNextWord() - 2;
369 if (fBunchLength > fCount) {
370 fRawReader->AddMinorErrorLog(kBunchLengthReadErr,Form("bl=%d",fBunchLength));
372 AliWarning(Form("Could not read bunch length ! Bunch length = %d (>%d)",fBunchLength,fCount));
373 fBunchLength = fTimeBunch = fCount = 0;
376 fTimeBunch = fBunchLength;
379 fTime = GetNextWord();
385 //_____________________________________________________________________________
386 void AliAltroRawStream::ReadAmplitude()
388 // Read next time bin amplitude
390 fRawReader->AddMinorErrorLog(kAmplitudeReadErr,"");
392 AliWarning("Could not read sample amplitude !");
393 fCount = fSignal = fBunchLength = 0;
397 fSignal = GetNextWord();
405 //_____________________________________________________________________________
406 Int_t AliAltroRawStream::GetPosition()
408 // Sets the position in the
410 // Read the RCU trailer
411 // This includes the trailer size,
412 // RCU identifier and raw data payload.
413 // The RCU trailer format is described
414 // in details in the RCU manual.
416 // We use the last word of the payload
417 // in order to decide which RCU firmware
418 // was used during the data taking.
419 // The firmware v2 adds 0xAAAA as 16
420 // most significant bits and since the
421 // payload size (firmware v1) can not be
422 // that big, we use this as a unique
423 // label of the firmware version.
425 Int_t index = fRawReader->GetDataSize();
426 UInt_t word = Get32bitWord(index);
427 if (((word >> 16) == 0xaaaa) || (word == 2)) {
428 // This is RCU formware v2
429 // The statement word==2 is needed only temporary
430 // in order to be able to read previously generated
433 Int_t trailerSize = 0;
435 AliInfo("Old simulated raw data is assumed!");
440 // First read 32-bit word with the
441 // trailer size (7 bits), RCU ID (9 bits) and
442 // 0xAAA (the rest - 16 bits)
443 fRCUId = (Int_t)((word >> 7) & 0x1ff);
444 trailerSize = (word & 0x7F);
447 // Now read the beginning of the trailer
448 // where the payload size is written
449 if (trailerSize < 2) {
450 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
452 AliWarning(Form("Invalid trailer size found (%d bytes) !",
456 Int_t position = ReadRCUTrailer(index,trailerSize);
457 // The size is specified in a number of 40bits
458 // Therefore we need to transform it to number of bytes
461 // Check the consistency of the header and trailer
462 if (((fRawReader->GetDataSize() - trailerSize*4) < position) ||
463 ((fRawReader->GetDataSize() - trailerSize*4) >= (position + 4))) {
464 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
465 fRawReader->GetDataSize(),
468 AliWarning(Form("Inconsistent raw data size ! Raw data size - %d bytes (from the header), RCU trailer - %d bytes, raw data paylod - %d bytes !",
469 fRawReader->GetDataSize(),
472 position = fRawReader->GetDataSize() - trailerSize*4;
475 return position * 8 / 10;
478 // In case of the Old RCU trailer format
479 // we have to read just the size of altro payload
480 // in units of 40-bit words
481 Int_t position = (Int_t)word;
485 fRCUTrailerData = NULL;
487 // The size is specified in a number of 40bits
488 // Therefore we need to transform it to number of bytes
491 if (!fIsShortDataHeader) {
493 // Check the consistency of the header and trailer
494 if (((fRawReader->GetDataSize() - 4) < position) ||
495 ((fRawReader->GetDataSize() - 4) >= (position + 4))) {
496 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d rcu=%d bytes",
497 fRawReader->GetDataSize()-4,
500 AliWarning(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
501 fRawReader->GetDataSize()-4,
503 position = fRawReader->GetDataSize()-4;
507 // Check the consistency of the header and trailer
508 // In this case the header is shorter by 4 bytes
509 if ((fRawReader->GetDataSize() < position) ||
510 (fRawReader->GetDataSize() >= (position + 4))) {
511 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d rcu=%d bytes",
512 fRawReader->GetDataSize(),
515 AliWarning(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
516 fRawReader->GetDataSize(),
518 position = fRawReader->GetDataSize();
521 // 7 32-bit words Common Data Header
522 // therefore we have to shift back by 4 bytes
523 // the pointer to the raw data payload
527 // Return the position in units of 10-bit words
528 return position*8/10;
532 //_____________________________________________________________________________
533 UInt_t AliAltroRawStream::Get32bitWord(Int_t &index)
535 // This method returns the 32 bit word at a given
536 // position inside the raw data payload.
537 // The 'index' points to the beginning of the next word.
538 // The method is supposed to be endian (platform)
542 AliFatal("Raw data paylod buffer is not yet initialized !");
546 fRawReader->AddFatalErrorLog(k32bitWordReadErr,Form("pos = %d",index));
548 AliWarning(Form("Invalid raw data payload position (%d) !",index));
552 word = fData[--index] << 24;
553 word |= fData[--index] << 16;
554 word |= fData[--index] << 8;
555 word |= fData[--index];
560 //_____________________________________________________________________________
561 Int_t AliAltroRawStream::ReadRCUTrailer(Int_t &index, Int_t trailerSize)
563 // The method decodes the RCU trailer data
564 // according to the RCU fw ver.2 specs
566 fRCUTrailerSize = trailerSize*4;
568 for (trailerSize -= 2; trailerSize > 0; trailerSize--) {
569 Int_t word = Get32bitWord(index);
570 Int_t parCode = word >> 26;
571 Int_t parData = word & 0x3FFFFFF;
575 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
576 fFECERRB = ((parData & 0x1FFF)) << 7;
580 fERRREG2 = parData & 0x1FF;
584 fERRREG3 = parData & 0xFFF;
588 fERRREG4 = parData & 0xFFF;
592 fActiveFECsA = parData & 0xFFFF;
596 fActiveFECsB = parData & 0xFFFF;
600 fAltroCFG1 = parData & 0xFFFFF;
604 fAltroCFG2 = parData & 0x1FFFFFF;
607 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
608 AliWarning(Form("Undefined parameter code %d, ignore it !",
615 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
617 fRawReader->GetDataSize()));
618 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
620 fRawReader->GetDataSize()));
622 fRCUTrailerData = fData + index;
624 Int_t position = Get32bitWord(index);
630 //_____________________________________________________________________________
631 Double_t AliAltroRawStream::GetTSample() const
633 // Returns the sampling time
634 // in seconds. In case the rcu trailer
635 // was note read, return an invalid number (0)
637 if (!fRCUTrailerData) return 0.;
639 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
640 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
645 tSample = 2.0*kLHCTimeSample;
649 tSample = 4.0*kLHCTimeSample;
653 tSample = 8.0*kLHCTimeSample;
656 AliWarning(Form("Invalid sampling frequency value %d !",
665 //_____________________________________________________________________________
666 Double_t AliAltroRawStream::GetL1Phase() const
668 // Returns the L1 phase w.r.t to the
670 if (!fRCUTrailerData) return 0.;
672 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
673 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
675 Double_t tSample = GetTSample();
676 if (phase >= tSample) {
677 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
685 //_____________________________________________________________________________
686 void AliAltroRawStream::PrintRCUTrailer() const
688 // Prints the contents of
689 // the RCU trailer data
690 printf("RCU trailer:\n===========\n");
691 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
692 printf("ERRREG2: 0x%x\n",fERRREG2);
693 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
694 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
695 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
696 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
697 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
698 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
699 printf("GlitchFilter: %d\n",GetGlitchFilter());
700 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
701 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
702 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
703 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
704 printf("Sparse readout: %d\n",GetSparseRO());
705 printf("Sampling time: %e s\n",GetTSample());
706 printf("L1 Phase: %e s\n",GetL1Phase());
707 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
708 printf("===========\n");
711 //_____________________________________________________________________________
712 Bool_t AliAltroRawStream::GetRCUTrailerData(UChar_t*& data) const
714 // Return a pointer to the RCU trailer
715 // data. Should be called always after
716 // the RCU trailer was already processed
717 // in the GetPosition() method
718 if (!fRCUTrailerSize || !fRCUTrailerData) {
719 AliError("No valid RCU trailer data is found !");
724 data = fRCUTrailerData;
729 //_____________________________________________________________________________
730 void AliAltroRawStream::PrintDebug() const
732 // The method prints all the available
733 // debug information.
734 // Its is used in case of decoding errors.
736 AliError("Start of debug printout\n--------------------");
739 if (fRawReader) fRawReader->Dump();
741 AliError("End of debug printout\n--------------------");
744 //_____________________________________________________________________________
745 Int_t AliAltroRawStream::GetBranch() const
747 // The method provides the RCU branch index (0 or 1)
748 // for the current hardware address.
749 // In case the hardware address has not been yet
750 // initialized, the method returns -1
751 if (fHWAddress == -1) return -1;
753 return ((fHWAddress >> 11) & 0x1);
756 //_____________________________________________________________________________
757 Int_t AliAltroRawStream::GetFEC() const
759 // The method provides the front-end card index
760 // for the current hardware address.
761 // In case the hardware address has not been yet
762 // initialized, the method returns -1
763 if (fHWAddress == -1) return -1;
765 return ((fHWAddress >> 7) & 0xF);
768 //_____________________________________________________________________________
769 Int_t AliAltroRawStream::GetAltro() const
771 // The method provides the altro chip index
772 // for the current hardware address.
773 // In case the hardware address has not been yet
774 // initialized, the method returns -1
775 if (fHWAddress == -1) return -1;
777 return ((fHWAddress >> 4) & 0x7);
780 //_____________________________________________________________________________
781 Int_t AliAltroRawStream::GetChannel() const
783 // The method provides the channel index
784 // for the current hardware address.
785 // In case the hardware address has not been yet
786 // initialized, the method returns -1
787 if (fHWAddress == -1) return -1;
789 return (fHWAddress & 0xF);
792 //_____________________________________________________________________________
793 void AliAltroRawStream::AddMappingErrorLog(const char *message)
795 // Signal a mapping error
796 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
797 // classes in order to log an error related to bad altro mapping
799 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);