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 **************************************************************************/
16 ///////////////////////////////////////////////////////////////////////////////
18 /// This is a base class for reading raw data digits in Altro format.
19 /// The class is able to read the RCU v3 and above formats.
20 /// The main difference between the format V3 and older ones is in
21 /// the coding of the 10-bit Altro payload words. In V3 3 10-bit words
22 /// are coded in one 32-bit word. The bits 30 and 31 are used to identify
23 /// the payload, altro header and RCU trailer contents.
26 /// cvetan.cheshkov@cern.ch 1/04/2009
27 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliAltroRawStreamV3.h"
30 #include "AliRawReader.h"
32 #include "AliAltroRawStream.h"
34 ClassImp(AliAltroRawStreamV3)
37 //_____________________________________________________________________________
38 AliAltroRawStreamV3::AliAltroRawStreamV3(AliRawReader* rawReader) :
39 fIsShortDataHeader(kFALSE),
43 fRawReader(rawReader),
51 fBunchDataPointer(NULL),
53 fRCUTrailerData(NULL),
66 // Create an object to read Altro raw digits in
67 // RCU version 3 and beyond format
68 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
71 //_____________________________________________________________________________
72 AliAltroRawStreamV3::~AliAltroRawStreamV3()
75 // delete old stream object if one exists
76 if (fOldStream) delete fOldStream;
79 //_____________________________________________________________________________
80 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
82 fIsShortDataHeader(stream.fIsShortDataHeader),
83 fDDLNumber(stream.fDDLNumber),
84 fRCUId(stream.fRCUId),
85 fHWAddress(stream.fHWAddress),
86 fRawReader(stream.fRawReader),
88 fPosition(stream.fPosition),
89 fCount(stream.fCount),
90 fStartTimeBin(stream.fStartTimeBin),
91 fBunchLength(stream.fBunchLength),
92 fBadChannel(stream.fBadChannel),
93 fPayloadSize(stream.fPayloadSize),
94 fBunchDataPointer(stream.fBunchDataPointer),
95 fBunchDataIndex(stream.fBunchDataIndex),
96 fRCUTrailerData(stream.fRCUTrailerData),
97 fRCUTrailerSize(stream.fRCUTrailerSize),
98 fFECERRA(stream.fFECERRA),
99 fFECERRB(stream.fFECERRB),
100 fERRREG2(stream.fERRREG2),
101 fERRREG3(stream.fERRREG3),
102 fActiveFECsA(stream.fActiveFECsA),
103 fActiveFECsB(stream.fActiveFECsB),
104 fAltroCFG1(stream.fAltroCFG1),
105 fAltroCFG2(stream.fAltroCFG2),
109 // Copy the bunch data array
110 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
112 if (stream.fOldStream)
113 fOldStream = new AliAltroRawStream(*stream.fOldStream);
116 //_____________________________________________________________________________
117 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
119 // assignment operator
121 if(&stream == this) return *this;
123 fIsShortDataHeader = stream.fIsShortDataHeader;
124 fDDLNumber = stream.fDDLNumber;
125 fRCUId = stream.fRCUId;
126 fHWAddress = stream.fHWAddress;
127 fRawReader = stream.fRawReader;
128 fData = stream.fData;
129 fPosition = stream.fPosition;
130 fCount = stream.fCount;
131 fStartTimeBin = stream.fStartTimeBin;
132 fBunchLength = stream.fBunchLength;
133 fBadChannel = stream.fBadChannel;
134 fPayloadSize = stream.fPayloadSize;
135 fBunchDataPointer = stream.fBunchDataPointer;
136 fBunchDataIndex = stream.fBunchDataIndex;
137 fRCUTrailerData = stream.fRCUTrailerData;
138 fRCUTrailerSize = stream.fRCUTrailerSize;
139 fFECERRA = stream.fFECERRA;
140 fFECERRB = stream.fFECERRB;
141 fERRREG2 = stream.fERRREG2;
142 fERRREG3 = stream.fERRREG3;
143 fActiveFECsA = stream.fActiveFECsA;
144 fActiveFECsB = stream.fActiveFECsB;
145 fAltroCFG1 = stream.fAltroCFG1;
146 fAltroCFG2 = stream.fAltroCFG2;
148 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
150 if (stream.fOldStream) {
151 if (fOldStream) delete fOldStream;
152 fOldStream = new AliAltroRawStream(stream.fRawReader);
153 *fOldStream = *stream.fOldStream;
159 //_____________________________________________________________________________
160 void AliAltroRawStreamV3::Reset()
162 // Complete reset of raw stream params
163 // Reset of the raw-reader as well
165 fDDLNumber = fRCUId = fHWAddress = -1;
166 fPosition = fCount = -1;
167 fBunchLength = fStartTimeBin = -1;
168 fBadChannel = kFALSE;
170 fBunchDataPointer = NULL;
171 fBunchDataIndex = -1;
173 fRCUTrailerData = NULL;
176 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
178 if (fRawReader) fRawReader->Reset();
180 if (fOldStream) fOldStream->Reset();
183 //_____________________________________________________________________________
184 Bool_t AliAltroRawStreamV3::NextDDL()
186 // Read the next DDL payload (CDH + RCU trailer)
187 // Updates the information which is coming from these
191 // Get next DDL payload
192 // return wtih false in case no more data payloads
195 if (!fRawReader->ReadNextData(fData)) return kFALSE;
196 } while (fRawReader->GetDataSize() == 0);
198 fDDLNumber = fRawReader->GetDDLID();
200 UChar_t rcuVer = fRawReader->GetBlockAttributes();
203 // old altro format data
205 fOldStream = new AliAltroRawStream(fRawReader);
206 AliInfo(Form("RCU firmware verion %d detected. Using AliAltroRawStream to decode the data.",
209 Bool_t status = fOldStream->NextDDL(fData);
211 fRCUId = fOldStream->GetRCUId();
212 fRCUTrailerSize = fOldStream->GetRCUTrailerSize();
213 fOldStream->GetRCUTrailerData(fRCUTrailerData);
214 fFECERRA = fOldStream->GetFECERRA();
215 fFECERRB = fOldStream->GetFECERRB();
216 fERRREG2 = fOldStream->GetERRREG2();
217 fERRREG3 = ((UInt_t)fOldStream->GetNChAddrMismatch()) |
218 (((UInt_t)fOldStream->GetNChLengthMismatch()) << 12);
219 fActiveFECsA = fOldStream->GetActiveFECsA();
220 fActiveFECsB = fOldStream->GetActiveFECsB();
221 fAltroCFG1 = fOldStream->GetAltroCFG1();
222 fAltroCFG2 = fOldStream->GetAltroCFG2();
227 return ReadRCUTrailer(rcuVer);
230 //_____________________________________________________________________________
231 Bool_t AliAltroRawStreamV3::NextChannel()
233 // Read the next Altro channel from the
235 // Updates the channel hardware address member and
236 // channel data size. Sets the error flag in case
237 // RCU signals readout error in this channel
239 Bool_t status = fOldStream->NextChannel();
240 if (status) fHWAddress = fOldStream->GetHWAddress();
245 fBadChannel = kFALSE;
250 word = Get32bitWord(fPosition++);
251 if (fPosition > fPayloadSize) return kFALSE;
253 while ((word >> 30) != 1);
255 // check for readout errors
256 fBadChannel = (word >> 29) & 0x1;
258 // extract channel payload and hw address
259 fCount = (word >> 16) & 0x3FF;
260 fHWAddress = word & 0xFFF;
262 // Now unpack the altro data
263 // Revert the order of the samples
264 // inside the bunch so that the
265 // first time is first in the samples
268 Int_t nwords = (fCount+2)/3;
269 for (Int_t iword = 0; iword < nwords; iword++) {
270 word = Get32bitWord(fPosition++);
271 if ((word >> 30) != 0) {
272 // Unexpected end of altro channel payload
273 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
275 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
280 fBunchData[isample++] = (word >> 20) & 0x3FF;
281 fBunchData[isample++] = (word >> 10) & 0x3FF;
282 fBunchData[isample++] = word & 0x3FF;
288 //_____________________________________________________________________________
289 Bool_t AliAltroRawStreamV3::NextBunch()
291 // Read next altro bunch from the
293 // Updates the start/end time-bins
294 // and the array with altro samples
296 Bool_t status = fOldStream->NextBunch(fBunchData,fBunchLength,fStartTimeBin);
297 if (status) fBunchDataPointer = &fBunchData[0];
298 else fBunchDataPointer = NULL;
302 fBunchLength = fStartTimeBin = -1;
303 fBunchDataPointer = NULL;
305 if ((fBunchDataIndex >= fCount) || fBadChannel) return kFALSE;
307 fBunchLength = fBunchData[fBunchDataIndex];
308 if ((fBunchDataIndex + fBunchLength) > fCount) {
309 // Too long bunch detected
310 AliWarning(Form("Too long bunch detected in Address=0x%x ! Expected <= %d 10-bit words, found %d !",
311 fHWAddress,fCount-fBunchDataIndex,fBunchLength));
312 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
313 fCount = fBunchLength = -1;
319 fStartTimeBin = fBunchData[fBunchDataIndex++];
321 fBunchDataPointer = &fBunchData[fBunchDataIndex];
323 fBunchDataIndex += fBunchLength;
328 //_____________________________________________________________________________
329 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
331 // This method returns the 32 bit word at a given
332 // position inside the raw data payload.
333 // The 'index' points to the beginning of the word.
334 // The method is supposed to be endian (platform)
337 index = (index << 2);
339 word |= fData[index++];
340 word |= fData[index++] << 8;
341 word |= fData[index++] << 16;
342 word |= fData[index++] << 24;
347 ///_____________________________________________________________________________
348 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
350 // Read the RCU trailer according
351 // to the RCU formware version
353 // Cross-check with version found in the
356 fRCUTrailerData = NULL;
360 Int_t index = fRawReader->GetDataSize()/4;
362 // First read 32-bit word with the
363 // trailer size (7 bits), RCU ID (9 bits) and
364 // RCU firmware version (8 bits?)
365 // The two major bit should be 11 (identifies
366 // the end of the trailer)
367 UInt_t word = Get32bitWord(--index);
369 if ((word >> 30) != 3) {
370 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
371 AliError("Last RCU trailer word not found!");
375 UChar_t ver = (word >> 16) & 0xFF;
377 // Wrong RCU firmware version detected
378 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
380 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
384 fRCUId = (Int_t)((word >> 7) & 0x1FF);
385 Int_t trailerSize = (word & 0x7F);
387 // Now read the beginning of the trailer
388 // where the payload size is written
389 if (trailerSize < 2) {
390 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
392 AliWarning(Form("Invalid trailer size found (%d bytes) !",
398 fRCUTrailerSize = trailerSize*4;
400 for (; trailerSize > 0; trailerSize--) {
401 word = Get32bitWord(--index);
402 if ((word >> 30) != 2) {
403 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
404 AliWarning("Missing RCU trailer identifier pattern!");
407 Int_t parCode = (word >> 26) & 0xF;
408 Int_t parData = word & 0x3FFFFFF;
412 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
413 fFECERRB = ((parData & 0x1FFF)) << 7;
417 fERRREG2 = parData & 0x1FF;
421 fERRREG3 = parData & 0x1FFFFFF;
425 fActiveFECsA = parData & 0xFFFF;
429 fActiveFECsB = parData & 0xFFFF;
433 fAltroCFG1 = parData & 0xFFFFF;
437 fAltroCFG2 = parData & 0x1FFFFFF;
440 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
441 AliWarning(Form("Undefined parameter code %d, ignore it !",
448 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
450 fRawReader->GetDataSize()));
451 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
453 fRawReader->GetDataSize()));
456 fRCUTrailerData = fData + index*4;
458 // Now read the payload size
459 // (First word in the RCU trailer)
460 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
462 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
463 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
464 fRawReader->GetDataSize(),
467 AliWarning(Form("Inconsistent raw data size ! Raw data size - %d bytes (from CDH), RCU trailer - %d bytes, raw data size (from RCU trailer) - %d bytes !",
468 fRawReader->GetDataSize(),
476 //_____________________________________________________________________________
477 Int_t AliAltroRawStreamV3::GetBranch() const
479 // The method provides the RCU branch index (0 or 1)
480 // for the current hardware address.
481 // In case the hardware address has not been yet
482 // initialized, the method returns -1
483 if (fHWAddress == -1) return -1;
485 return ((fHWAddress >> 11) & 0x1);
488 //_____________________________________________________________________________
489 Int_t AliAltroRawStreamV3::GetFEC() const
491 // The method provides the front-end card index
492 // for the current hardware address.
493 // In case the hardware address has not been yet
494 // initialized, the method returns -1
495 if (fHWAddress == -1) return -1;
497 return ((fHWAddress >> 7) & 0xF);
500 //_____________________________________________________________________________
501 Int_t AliAltroRawStreamV3::GetAltro() const
503 // The method provides the altro chip index
504 // for the current hardware address.
505 // In case the hardware address has not been yet
506 // initialized, the method returns -1
507 if (fHWAddress == -1) return -1;
509 return ((fHWAddress >> 4) & 0x7);
512 //_____________________________________________________________________________
513 Int_t AliAltroRawStreamV3::GetChannel() const
515 // The method provides the channel index
516 // for the current hardware address.
517 // In case the hardware address has not been yet
518 // initialized, the method returns -1
519 if (fHWAddress == -1) return -1;
521 return (fHWAddress & 0xF);
524 //_____________________________________________________________________________
525 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
527 // Return a pointer to the RCU trailer
528 // data. Should be called always after
529 // the RCU trailer was already processed
530 // in the GetPosition() method
531 if (!fRCUTrailerSize || !fRCUTrailerData) {
532 AliError("No valid RCU trailer data is found !");
537 data = fRCUTrailerData;
542 //_____________________________________________________________________________
543 void AliAltroRawStreamV3::PrintRCUTrailer() const
545 // Prints the contents of
546 // the RCU trailer data
547 printf("RCU trailer:\n===========\n");
548 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
549 printf("ERRREG2: 0x%x\n",fERRREG2);
550 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
551 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
552 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
553 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
554 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
555 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
556 printf("GlitchFilter: %d\n",GetGlitchFilter());
557 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
558 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
559 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
560 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
561 printf("Sparse readout: %d\n",GetSparseRO());
562 printf("Sampling time: %e s\n",GetTSample());
563 printf("L1 Phase: %e s\n",GetL1Phase());
564 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
565 printf("===========\n");
568 //_____________________________________________________________________________
569 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
571 // Select the raw data for specific
573 AliDebug(1,Form("Selecting raw data for detector %d",detId));
574 fRawReader->Select(detId);
577 //_____________________________________________________________________________
578 void AliAltroRawStreamV3::SelectRawData(const char *detName)
580 // Select the raw data for specific
582 AliDebug(1,Form("Selecting raw data for detector %s",detName));
583 fRawReader->Select(detName);
586 //_____________________________________________________________________________
587 Double_t AliAltroRawStreamV3::GetTSample() const
589 // Returns the sampling time
590 // in seconds. In case the rcu trailer
591 // was note read, return an invalid number (0)
593 if (!fRCUTrailerData) return 0.;
595 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
596 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
601 tSample = 2.0*kLHCTimeSample;
605 tSample = 4.0*kLHCTimeSample;
609 tSample = 8.0*kLHCTimeSample;
612 AliWarning(Form("Invalid sampling frequency value %d !",
621 //_____________________________________________________________________________
622 Double_t AliAltroRawStreamV3::GetL1Phase() const
624 // Returns the L1 phase w.r.t to the
626 if (!fRCUTrailerData) return 0.;
628 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
629 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
631 Double_t tSample = GetTSample();
632 if (phase >= tSample) {
633 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
641 //_____________________________________________________________________________
642 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
644 // Signal a mapping error
645 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
646 // classes in order to log an error related to bad altro mapping
648 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);