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),
64 fCheckAltroPayload(kTRUE)
67 // Create an object to read Altro raw digits in
68 // RCU version 3 and beyond format
69 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
72 //_____________________________________________________________________________
73 AliAltroRawStreamV3::~AliAltroRawStreamV3()
76 // delete old stream object if one exists
77 if (fOldStream) delete fOldStream;
80 //_____________________________________________________________________________
81 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
83 fIsShortDataHeader(stream.fIsShortDataHeader),
84 fDDLNumber(stream.fDDLNumber),
85 fRCUId(stream.fRCUId),
86 fHWAddress(stream.fHWAddress),
87 fRawReader(stream.fRawReader),
89 fPosition(stream.fPosition),
90 fCount(stream.fCount),
91 fStartTimeBin(stream.fStartTimeBin),
92 fBunchLength(stream.fBunchLength),
93 fBadChannel(stream.fBadChannel),
94 fPayloadSize(stream.fPayloadSize),
95 fBunchDataPointer(stream.fBunchDataPointer),
96 fBunchDataIndex(stream.fBunchDataIndex),
97 fRCUTrailerData(stream.fRCUTrailerData),
98 fRCUTrailerSize(stream.fRCUTrailerSize),
99 fFECERRA(stream.fFECERRA),
100 fFECERRB(stream.fFECERRB),
101 fERRREG2(stream.fERRREG2),
102 fERRREG3(stream.fERRREG3),
103 fActiveFECsA(stream.fActiveFECsA),
104 fActiveFECsB(stream.fActiveFECsB),
105 fAltroCFG1(stream.fAltroCFG1),
106 fAltroCFG2(stream.fAltroCFG2),
108 fCheckAltroPayload(stream.fCheckAltroPayload)
111 // Copy the bunch data array
112 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
114 if (stream.fOldStream)
115 fOldStream = new AliAltroRawStream(*stream.fOldStream);
118 //_____________________________________________________________________________
119 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
121 // assignment operator
123 if(&stream == this) return *this;
125 fIsShortDataHeader = stream.fIsShortDataHeader;
126 fDDLNumber = stream.fDDLNumber;
127 fRCUId = stream.fRCUId;
128 fHWAddress = stream.fHWAddress;
129 fRawReader = stream.fRawReader;
130 fData = stream.fData;
131 fPosition = stream.fPosition;
132 fCount = stream.fCount;
133 fStartTimeBin = stream.fStartTimeBin;
134 fBunchLength = stream.fBunchLength;
135 fBadChannel = stream.fBadChannel;
136 fPayloadSize = stream.fPayloadSize;
137 fBunchDataPointer = stream.fBunchDataPointer;
138 fBunchDataIndex = stream.fBunchDataIndex;
139 fRCUTrailerData = stream.fRCUTrailerData;
140 fRCUTrailerSize = stream.fRCUTrailerSize;
141 fFECERRA = stream.fFECERRA;
142 fFECERRB = stream.fFECERRB;
143 fERRREG2 = stream.fERRREG2;
144 fERRREG3 = stream.fERRREG3;
145 fActiveFECsA = stream.fActiveFECsA;
146 fActiveFECsB = stream.fActiveFECsB;
147 fAltroCFG1 = stream.fAltroCFG1;
148 fAltroCFG2 = stream.fAltroCFG2;
150 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
152 if (stream.fOldStream) {
153 if (fOldStream) delete fOldStream;
154 fOldStream = new AliAltroRawStream(stream.fRawReader);
155 *fOldStream = *stream.fOldStream;
158 fCheckAltroPayload = stream.fCheckAltroPayload;
163 //_____________________________________________________________________________
164 void AliAltroRawStreamV3::Reset()
166 // Complete reset of raw stream params
167 // Reset of the raw-reader as well
169 fDDLNumber = fRCUId = fHWAddress = -1;
170 fPosition = fCount = -1;
171 fBunchLength = fStartTimeBin = -1;
172 fBadChannel = kFALSE;
174 fBunchDataPointer = NULL;
175 fBunchDataIndex = -1;
177 fRCUTrailerData = NULL;
180 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
182 if (fRawReader) fRawReader->Reset();
184 if (fOldStream) fOldStream->Reset();
187 //_____________________________________________________________________________
188 Bool_t AliAltroRawStreamV3::NextDDL()
190 // Read the next DDL payload (CDH + RCU trailer)
191 // Updates the information which is coming from these
195 // Get next DDL payload
196 // return wtih false in case no more data payloads
199 if (!fRawReader->ReadNextData(fData)) return kFALSE;
200 } while (fRawReader->GetDataSize() == 0);
202 fDDLNumber = fRawReader->GetDDLID();
204 UChar_t rcuVer = fRawReader->GetBlockAttributes();
207 // old altro format data
209 fOldStream = new AliAltroRawStream(fRawReader);
210 AliInfo(Form("RCU firmware verion %d detected. Using AliAltroRawStream to decode the data.",
213 Bool_t status = fOldStream->NextDDL(fData);
215 fRCUId = fOldStream->GetRCUId();
216 fRCUTrailerSize = fOldStream->GetRCUTrailerSize();
217 fOldStream->GetRCUTrailerData(fRCUTrailerData);
218 fFECERRA = fOldStream->GetFECERRA();
219 fFECERRB = fOldStream->GetFECERRB();
220 fERRREG2 = fOldStream->GetERRREG2();
221 fERRREG3 = ((UInt_t)fOldStream->GetNChAddrMismatch()) |
222 (((UInt_t)fOldStream->GetNChLengthMismatch()) << 12);
223 fActiveFECsA = fOldStream->GetActiveFECsA();
224 fActiveFECsB = fOldStream->GetActiveFECsB();
225 fAltroCFG1 = fOldStream->GetAltroCFG1();
226 fAltroCFG2 = fOldStream->GetAltroCFG2();
231 return ReadRCUTrailer(rcuVer);
234 //_____________________________________________________________________________
235 Bool_t AliAltroRawStreamV3::NextChannel()
237 // Read the next Altro channel from the
239 // Updates the channel hardware address member and
240 // channel data size. Sets the error flag in case
241 // RCU signals readout error in this channel
243 Bool_t status = fOldStream->NextChannel();
244 if (status) fHWAddress = fOldStream->GetHWAddress();
249 fBadChannel = kFALSE;
255 word = Get32bitWord(fPosition++);
256 if (fPosition > fPayloadSize) return kFALSE;
258 while ((word >> 30) != 1);
260 // check for readout errors
261 fBadChannel = (word >> 29) & 0x1;
263 // extract channel payload and hw address
264 fCount = (word >> 16) & 0x3FF;
265 fHWAddress = word & 0xFFF;
267 // Now unpack the altro data
268 // Revert the order of the samples
269 // inside the bunch so that the
270 // first time is first in the samples
273 Int_t nwords = (fCount+2)/3;
274 for (Int_t iword = 0; iword < nwords; iword++) {
275 word = Get32bitWord(fPosition++);
276 if ((word >> 30) != 0) {
277 // Unexpected end of altro channel payload
278 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
280 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
285 fBunchData[isample++] = (word >> 20) & 0x3FF;
286 fBunchData[isample++] = (word >> 10) & 0x3FF;
287 fBunchData[isample++] = word & 0x3FF;
293 //_____________________________________________________________________________
294 Bool_t AliAltroRawStreamV3::NextBunch()
296 // Read next altro bunch from the
298 // Updates the start/end time-bins
299 // and the array with altro samples
301 Bool_t status = fOldStream->NextBunch(fBunchData,fBunchLength,fStartTimeBin);
302 if (status) fBunchDataPointer = &fBunchData[0];
303 else fBunchDataPointer = NULL;
307 Int_t prevTimeBin = (fBunchLength > 0) ? fStartTimeBin-fBunchLength+1 : 1024;
308 fBunchLength = fStartTimeBin = -1;
309 fBunchDataPointer = NULL;
311 if ((fBunchDataIndex >= fCount) || fBadChannel) return kFALSE;
313 fBunchLength = fBunchData[fBunchDataIndex];
314 if (fBunchLength <= 2) {
315 // Invalid bunch size
316 AliWarning(Form("Too short bunch length (%d) in Address=0x%x !",
317 fBunchLength,fHWAddress));
318 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
319 fCount = fBunchLength = -1;
322 if ((fBunchDataIndex + fBunchLength) > fCount) {
323 // Too long bunch detected
324 AliWarning(Form("Too long bunch detected in Address=0x%x ! Expected <= %d 10-bit words, found %d !",
325 fHWAddress,fCount-fBunchDataIndex,fBunchLength));
326 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
327 fCount = fBunchLength = -1;
333 fStartTimeBin = fBunchData[fBunchDataIndex++];
334 if (fCheckAltroPayload) {
335 if ((fStartTimeBin-fBunchLength+1) < 0) {
336 AliWarning(Form("Invalid start time-bin in Address=0x%x ! (%d-%d+1) < 0",
337 fHWAddress,fStartTimeBin,fBunchLength));
338 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
339 fCount = fBunchLength = -1;
342 if (fStartTimeBin >= prevTimeBin) {
343 AliWarning(Form("Invalid start time-bin in Address=0x%x ! (%d>=%d)",
344 fHWAddress,fStartTimeBin,prevTimeBin));
345 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
346 fCount = fBunchLength = -1;
351 fBunchDataPointer = &fBunchData[fBunchDataIndex];
353 fBunchDataIndex += fBunchLength;
358 //_____________________________________________________________________________
359 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
361 // This method returns the 32 bit word at a given
362 // position inside the raw data payload.
363 // The 'index' points to the beginning of the word.
364 // The method is supposed to be endian (platform)
367 index = (index << 2);
369 word |= fData[index++];
370 word |= fData[index++] << 8;
371 word |= fData[index++] << 16;
372 word |= fData[index++] << 24;
377 ///_____________________________________________________________________________
378 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
380 // Read the RCU trailer according
381 // to the RCU formware version
383 // Cross-check with version found in the
386 fRCUTrailerData = NULL;
390 Int_t index = fRawReader->GetDataSize()/4;
392 // First read 32-bit word with the
393 // trailer size (7 bits), RCU ID (9 bits) and
394 // RCU firmware version (8 bits?)
395 // The two major bit should be 11 (identifies
396 // the end of the trailer)
397 UInt_t word = Get32bitWord(--index);
399 if ((word >> 30) != 3) {
400 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
401 AliError("Last RCU trailer word not found!");
405 UChar_t ver = (word >> 16) & 0xFF;
407 // Wrong RCU firmware version detected
408 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
410 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
414 fRCUId = (Int_t)((word >> 7) & 0x1FF);
415 Int_t trailerSize = (word & 0x7F);
417 // Now read the beginning of the trailer
418 // where the payload size is written
419 if (trailerSize < 2) {
420 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
422 AliWarning(Form("Invalid trailer size found (%d bytes) !",
428 fRCUTrailerSize = trailerSize*4;
430 for (; trailerSize > 0; trailerSize--) {
431 word = Get32bitWord(--index);
432 if ((word >> 30) != 2) {
433 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
434 AliWarning("Missing RCU trailer identifier pattern!");
437 Int_t parCode = (word >> 26) & 0xF;
438 Int_t parData = word & 0x3FFFFFF;
442 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
443 fFECERRB = ((parData & 0x1FFF)) << 7;
447 fERRREG2 = parData & 0x1FF;
451 fERRREG3 = parData & 0x1FFFFFF;
455 fActiveFECsA = parData & 0xFFFF;
459 fActiveFECsB = parData & 0xFFFF;
463 fAltroCFG1 = parData & 0xFFFFF;
467 fAltroCFG2 = parData & 0x1FFFFFF;
470 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
471 AliWarning(Form("Undefined parameter code %d, ignore it !",
478 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
480 fRawReader->GetDataSize()));
481 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
483 fRawReader->GetDataSize()));
486 fRCUTrailerData = fData + index*4;
488 // Now read the payload size
489 // (First word in the RCU trailer)
490 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
492 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
493 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
494 fRawReader->GetDataSize(),
497 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 !",
498 fRawReader->GetDataSize(),
506 //_____________________________________________________________________________
507 Int_t AliAltroRawStreamV3::GetBranch() const
509 // The method provides the RCU branch index (0 or 1)
510 // for the current hardware address.
511 // In case the hardware address has not been yet
512 // initialized, the method returns -1
513 if (fHWAddress == -1) return -1;
515 return ((fHWAddress >> 11) & 0x1);
518 //_____________________________________________________________________________
519 Int_t AliAltroRawStreamV3::GetFEC() const
521 // The method provides the front-end card index
522 // for the current hardware address.
523 // In case the hardware address has not been yet
524 // initialized, the method returns -1
525 if (fHWAddress == -1) return -1;
527 return ((fHWAddress >> 7) & 0xF);
530 //_____________________________________________________________________________
531 Int_t AliAltroRawStreamV3::GetAltro() const
533 // The method provides the altro chip index
534 // for the current hardware address.
535 // In case the hardware address has not been yet
536 // initialized, the method returns -1
537 if (fHWAddress == -1) return -1;
539 return ((fHWAddress >> 4) & 0x7);
542 //_____________________________________________________________________________
543 Int_t AliAltroRawStreamV3::GetChannel() const
545 // The method provides the channel index
546 // for the current hardware address.
547 // In case the hardware address has not been yet
548 // initialized, the method returns -1
549 if (fHWAddress == -1) return -1;
551 return (fHWAddress & 0xF);
554 //_____________________________________________________________________________
555 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
557 // Return a pointer to the RCU trailer
558 // data. Should be called always after
559 // the RCU trailer was already processed
560 // in the GetPosition() method
561 if (!fRCUTrailerSize || !fRCUTrailerData) {
562 AliError("No valid RCU trailer data is found !");
567 data = fRCUTrailerData;
572 //_____________________________________________________________________________
573 void AliAltroRawStreamV3::PrintRCUTrailer() const
575 // Prints the contents of
576 // the RCU trailer data
577 printf("RCU trailer:\n===========\n");
578 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
579 printf("ERRREG2: 0x%x\n",fERRREG2);
580 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
581 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
582 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
583 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
584 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
585 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
586 printf("GlitchFilter: %d\n",GetGlitchFilter());
587 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
588 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
589 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
590 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
591 printf("Sparse readout: %d\n",GetSparseRO());
592 printf("Sampling time: %e s\n",GetTSample());
593 printf("L1 Phase: %e s\n",GetL1Phase());
594 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
595 printf("===========\n");
598 //_____________________________________________________________________________
599 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
601 // Select the raw data for specific
603 AliDebug(1,Form("Selecting raw data for detector %d",detId));
604 fRawReader->Select(detId);
607 //_____________________________________________________________________________
608 void AliAltroRawStreamV3::SelectRawData(const char *detName)
610 // Select the raw data for specific
612 AliDebug(1,Form("Selecting raw data for detector %s",detName));
613 fRawReader->Select(detName);
616 //_____________________________________________________________________________
617 Double_t AliAltroRawStreamV3::GetTSample() const
619 // Returns the sampling time
620 // in seconds. In case the rcu trailer
621 // was note read, return an invalid number (0)
623 if (!fRCUTrailerData) return 0.;
625 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
626 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
631 tSample = 2.0*kLHCTimeSample;
635 tSample = 4.0*kLHCTimeSample;
639 tSample = 8.0*kLHCTimeSample;
642 AliWarning(Form("Invalid sampling frequency value %d !",
651 //_____________________________________________________________________________
652 Double_t AliAltroRawStreamV3::GetL1Phase() const
654 // Returns the L1 phase w.r.t to the
656 if (!fRCUTrailerData) return 0.;
658 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
659 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
661 Double_t tSample = GetTSample();
662 if (phase >= tSample) {
663 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
671 //_____________________________________________________________________________
672 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
674 // Signal a mapping error
675 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
676 // classes in order to log an error related to bad altro mapping
678 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);