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"
33 #include "AliRawEventHeaderBase.h"
35 ClassImp(AliAltroRawStreamV3)
38 //_____________________________________________________________________________
39 AliAltroRawStreamV3::AliAltroRawStreamV3(AliRawReader* rawReader) :
40 fIsShortDataHeader(kFALSE),
44 fRawReader(rawReader),
52 fChannelPayloadSize(-1),
53 fBunchDataPointer(NULL),
55 fRCUTrailerData(NULL),
66 fCheckAltroPayload(kTRUE)
69 // Create an object to read Altro raw digits in
70 // RCU version 3 and beyond format
71 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
74 //_____________________________________________________________________________
75 AliAltroRawStreamV3::~AliAltroRawStreamV3()
78 // delete old stream object if one exists
79 if (fOldStream) delete fOldStream;
82 //_____________________________________________________________________________
83 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
85 fIsShortDataHeader(stream.fIsShortDataHeader),
86 fDDLNumber(stream.fDDLNumber),
87 fRCUId(stream.fRCUId),
88 fHWAddress(stream.fHWAddress),
89 fRawReader(stream.fRawReader),
91 fPosition(stream.fPosition),
92 fCount(stream.fCount),
93 fStartTimeBin(stream.fStartTimeBin),
94 fBunchLength(stream.fBunchLength),
95 fBadChannel(stream.fBadChannel),
96 fPayloadSize(stream.fPayloadSize),
97 fChannelPayloadSize(stream.fChannelPayloadSize),
98 fBunchDataPointer(stream.fBunchDataPointer),
99 fBunchDataIndex(stream.fBunchDataIndex),
100 fRCUTrailerData(stream.fRCUTrailerData),
101 fRCUTrailerSize(stream.fRCUTrailerSize),
102 fFECERRA(stream.fFECERRA),
103 fFECERRB(stream.fFECERRB),
104 fERRREG2(stream.fERRREG2),
105 fERRREG3(stream.fERRREG3),
106 fActiveFECsA(stream.fActiveFECsA),
107 fActiveFECsB(stream.fActiveFECsB),
108 fAltroCFG1(stream.fAltroCFG1),
109 fAltroCFG2(stream.fAltroCFG2),
111 fCheckAltroPayload(stream.fCheckAltroPayload)
114 // Copy the bunch data array
115 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
117 if (stream.fOldStream)
118 fOldStream = new AliAltroRawStream(*stream.fOldStream);
121 //_____________________________________________________________________________
122 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
124 // assignment operator
126 if(&stream == this) return *this;
128 fIsShortDataHeader = stream.fIsShortDataHeader;
129 fDDLNumber = stream.fDDLNumber;
130 fRCUId = stream.fRCUId;
131 fHWAddress = stream.fHWAddress;
132 fRawReader = stream.fRawReader;
133 fData = stream.fData;
134 fPosition = stream.fPosition;
135 fCount = stream.fCount;
136 fStartTimeBin = stream.fStartTimeBin;
137 fBunchLength = stream.fBunchLength;
138 fBadChannel = stream.fBadChannel;
139 fPayloadSize = stream.fPayloadSize;
140 fChannelPayloadSize= stream.fChannelPayloadSize;
141 fBunchDataPointer = stream.fBunchDataPointer;
142 fBunchDataIndex = stream.fBunchDataIndex;
143 fRCUTrailerData = stream.fRCUTrailerData;
144 fRCUTrailerSize = stream.fRCUTrailerSize;
145 fFECERRA = stream.fFECERRA;
146 fFECERRB = stream.fFECERRB;
147 fERRREG2 = stream.fERRREG2;
148 fERRREG3 = stream.fERRREG3;
149 fActiveFECsA = stream.fActiveFECsA;
150 fActiveFECsB = stream.fActiveFECsB;
151 fAltroCFG1 = stream.fAltroCFG1;
152 fAltroCFG2 = stream.fAltroCFG2;
154 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
156 if (stream.fOldStream) {
157 if (fOldStream) delete fOldStream;
158 fOldStream = new AliAltroRawStream(stream.fRawReader);
159 *fOldStream = *stream.fOldStream;
162 fCheckAltroPayload = stream.fCheckAltroPayload;
167 //_____________________________________________________________________________
168 void AliAltroRawStreamV3::Reset()
170 // Complete reset of raw stream params
171 // Reset of the raw-reader as well
173 fDDLNumber = fRCUId = fHWAddress = -1;
174 fPosition = fCount = -1;
175 fBunchLength = fStartTimeBin = -1;
176 fBadChannel = kFALSE;
178 fChannelPayloadSize = -1;
179 fBunchDataPointer = NULL;
180 fBunchDataIndex = -1;
182 fRCUTrailerData = NULL;
185 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
187 if (fRawReader) fRawReader->Reset();
189 if (fOldStream) fOldStream->Reset();
192 //_____________________________________________________________________________
193 Bool_t AliAltroRawStreamV3::NextDDL()
195 // Read the next DDL payload (CDH + RCU trailer)
196 // Updates the information which is coming from these
200 // Get next DDL payload
201 // return wtih false in case no more data payloads
204 if (!fRawReader->ReadNextData(fData)) return kFALSE;
205 } while (fRawReader->GetDataSize() == 0);
207 fDDLNumber = fRawReader->GetDDLID();
208 fChannelPayloadSize = -1;
210 UChar_t rcuVer = fRawReader->GetBlockAttributes();
213 // old altro format data
215 fOldStream = new AliAltroRawStream(fRawReader);
216 AliInfo(Form("RCU firmware verion %d detected. Using AliAltroRawStream to decode the data.",
219 Bool_t status = fOldStream->NextDDL(fData);
221 fRCUId = fOldStream->GetRCUId();
222 fRCUTrailerSize = fOldStream->GetRCUTrailerSize();
223 fOldStream->GetRCUTrailerData(fRCUTrailerData);
224 fFECERRA = fOldStream->GetFECERRA();
225 fFECERRB = fOldStream->GetFECERRB();
226 fERRREG2 = fOldStream->GetERRREG2();
227 fERRREG3 = ((UInt_t)fOldStream->GetNChAddrMismatch()) |
228 (((UInt_t)fOldStream->GetNChLengthMismatch()) << 12);
229 fActiveFECsA = fOldStream->GetActiveFECsA();
230 fActiveFECsB = fOldStream->GetActiveFECsB();
231 fAltroCFG1 = fOldStream->GetAltroCFG1();
232 fAltroCFG2 = fOldStream->GetAltroCFG2();
233 if (fRawReader->GetType() == AliRawEventHeaderBase::kStartOfData)
234 fPayloadSize = fOldStream->GetRCUPayloadSizeInSOD();
239 return ReadRCUTrailer(rcuVer);
242 //_____________________________________________________________________________
243 Bool_t AliAltroRawStreamV3::NextChannel()
245 // Read the next Altro channel from the
247 // Updates the channel hardware address member and
248 // channel data size. Sets the error flag in case
249 // RCU signals readout error in this channel
251 Bool_t status = fOldStream->NextChannel();
253 fHWAddress = fOldStream->GetHWAddress();
254 fChannelPayloadSize = fOldStream->GetChannelPayloadSize();
260 fBadChannel = kFALSE;
266 word = Get32bitWord(fPosition++);
267 if (fPosition > fPayloadSize) return kFALSE;
269 while ((word >> 30) != 1);
271 // check for readout errors
272 fBadChannel = (word >> 29) & 0x1;
274 // extract channel payload and hw address
275 fCount = (word >> 16) & 0x3FF;
276 fChannelPayloadSize = fCount;
277 fHWAddress = word & 0xFFF;
279 // Now unpack the altro data
280 // Revert the order of the samples
281 // inside the bunch so that the
282 // first time is first in the samples
285 Int_t nwords = (fCount+2)/3;
286 for (Int_t iword = 0; iword < nwords; iword++) {
287 word = Get32bitWord(fPosition++);
288 if ((word >> 30) != 0) {
289 // Unexpected end of altro channel payload
290 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
292 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
297 fBunchData[isample++] = (word >> 20) & 0x3FF;
298 fBunchData[isample++] = (word >> 10) & 0x3FF;
299 fBunchData[isample++] = word & 0x3FF;
305 //_____________________________________________________________________________
306 Bool_t AliAltroRawStreamV3::NextBunch()
308 // Read next altro bunch from the
310 // Updates the start/end time-bins
311 // and the array with altro samples
313 Bool_t status = fOldStream->NextBunch(fBunchData,fBunchLength,fStartTimeBin);
314 if (status) fBunchDataPointer = &fBunchData[0];
315 else fBunchDataPointer = NULL;
319 Int_t prevTimeBin = (fBunchLength > 0) ? fStartTimeBin-fBunchLength+1 : 1024;
320 fBunchLength = fStartTimeBin = -1;
321 fBunchDataPointer = NULL;
323 if ((fBunchDataIndex >= fCount) || fBadChannel) return kFALSE;
325 fBunchLength = fBunchData[fBunchDataIndex];
326 if (fBunchLength <= 2) {
327 // Invalid bunch size
328 AliWarning(Form("Too short bunch length (%d) in Address=0x%x !",
329 fBunchLength,fHWAddress));
330 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
331 fCount = fBunchLength = -1;
334 if ((fBunchDataIndex + fBunchLength) > fCount) {
335 // Too long bunch detected
336 AliWarning(Form("Too long bunch detected in Address=0x%x ! Expected <= %d 10-bit words, found %d !",
337 fHWAddress,fCount-fBunchDataIndex,fBunchLength));
338 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
339 fCount = fBunchLength = -1;
345 fStartTimeBin = fBunchData[fBunchDataIndex++];
346 if (fCheckAltroPayload) {
347 if ((fStartTimeBin-fBunchLength+1) < 0) {
348 AliWarning(Form("Invalid start time-bin in Address=0x%x ! (%d-%d+1) < 0",
349 fHWAddress,fStartTimeBin,fBunchLength));
350 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
351 fCount = fBunchLength = -1;
354 if (fStartTimeBin >= prevTimeBin) {
355 AliWarning(Form("Invalid start time-bin in Address=0x%x ! (%d>=%d)",
356 fHWAddress,fStartTimeBin,prevTimeBin));
357 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
358 fCount = fBunchLength = -1;
363 fBunchDataPointer = &fBunchData[fBunchDataIndex];
365 fBunchDataIndex += fBunchLength;
370 //_____________________________________________________________________________
371 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
373 // This method returns the 32 bit word at a given
374 // position inside the raw data payload.
375 // The 'index' points to the beginning of the word.
376 // The method is supposed to be endian (platform)
379 index = (index << 2);
381 word |= fData[index++];
382 word |= fData[index++] << 8;
383 word |= fData[index++] << 16;
384 word |= fData[index++] << 24;
389 ///_____________________________________________________________________________
390 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
392 // Read the RCU trailer according
393 // to the RCU formware version
395 // Cross-check with version found in the
398 fRCUTrailerData = NULL;
402 Int_t index = fRawReader->GetDataSize()/4;
404 // First read 32-bit word with the
405 // trailer size (7 bits), RCU ID (9 bits) and
406 // RCU firmware version (8 bits?)
407 // The two major bit should be 11 (identifies
408 // the end of the trailer)
409 UInt_t word = Get32bitWord(--index);
411 if ((word >> 30) != 3) {
412 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
413 AliError("Last RCU trailer word not found!");
417 UChar_t ver = (word >> 16) & 0xFF;
419 // Wrong RCU firmware version detected
420 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
422 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
426 fRCUId = (Int_t)((word >> 7) & 0x1FF);
427 Int_t trailerSize = (word & 0x7F);
429 // Now read the beginning of the trailer
430 // where the payload size is written
431 if (trailerSize < 2) {
432 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
434 AliWarning(Form("Invalid trailer size found (%d bytes) !",
440 fRCUTrailerSize = trailerSize*4;
442 for (; trailerSize > 0; trailerSize--) {
443 word = Get32bitWord(--index);
444 if ((word >> 30) != 2) {
445 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
446 AliWarning("Missing RCU trailer identifier pattern!");
449 Int_t parCode = (word >> 26) & 0xF;
450 Int_t parData = word & 0x3FFFFFF;
454 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
455 fFECERRB = ((parData & 0x1FFF)) << 7;
459 fERRREG2 = parData & 0x1FF;
463 fERRREG3 = parData & 0x1FFFFFF;
467 fActiveFECsA = parData & 0xFFFF;
471 fActiveFECsB = parData & 0xFFFF;
475 fAltroCFG1 = parData & 0xFFFFF;
479 fAltroCFG2 = parData & 0x1FFFFFF;
482 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
483 AliWarning(Form("Undefined parameter code %d, ignore it !",
490 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
492 fRawReader->GetDataSize()));
493 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
495 fRawReader->GetDataSize()));
498 fRCUTrailerData = fData + index*4;
500 // Now read the payload size
501 // (First word in the RCU trailer)
502 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
504 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
505 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
506 fRawReader->GetDataSize(),
509 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 !",
510 fRawReader->GetDataSize(),
518 //_____________________________________________________________________________
519 Int_t AliAltroRawStreamV3::GetBranch() const
521 // The method provides the RCU branch index (0 or 1)
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 >> 11) & 0x1);
530 //_____________________________________________________________________________
531 Int_t AliAltroRawStreamV3::GetFEC() const
533 // The method provides the front-end card 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 >> 7) & 0xF);
542 //_____________________________________________________________________________
543 Int_t AliAltroRawStreamV3::GetAltro() const
545 // The method provides the altro chip 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 >> 4) & 0x7);
554 //_____________________________________________________________________________
555 Int_t AliAltroRawStreamV3::GetChannel() const
557 // The method provides the channel index
558 // for the current hardware address.
559 // In case the hardware address has not been yet
560 // initialized, the method returns -1
561 if (fHWAddress == -1) return -1;
563 return (fHWAddress & 0xF);
566 //_____________________________________________________________________________
567 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
569 // Return a pointer to the RCU trailer
570 // data. Should be called always after
571 // the RCU trailer was already processed
572 // in the GetPosition() method
573 if (!fRCUTrailerSize || !fRCUTrailerData) {
574 AliError("No valid RCU trailer data is found !");
579 data = fRCUTrailerData;
584 //_____________________________________________________________________________
585 void AliAltroRawStreamV3::PrintRCUTrailer() const
587 // Prints the contents of
588 // the RCU trailer data
589 printf("RCU trailer:\n===========\n");
590 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
591 printf("ERRREG2: 0x%x\n",fERRREG2);
592 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
593 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
594 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
595 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
596 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
597 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
598 printf("GlitchFilter: %d\n",GetGlitchFilter());
599 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
600 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
601 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
602 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
603 printf("Sparse readout: %d\n",GetSparseRO());
604 printf("Sampling time: %e s\n",GetTSample());
605 printf("L1 Phase: %e s\n",GetL1Phase());
606 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
607 printf("===========\n");
610 //_____________________________________________________________________________
611 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
613 // Select the raw data for specific
615 AliDebug(1,Form("Selecting raw data for detector %d",detId));
616 fRawReader->Select(detId);
619 //_____________________________________________________________________________
620 void AliAltroRawStreamV3::SelectRawData(const char *detName)
622 // Select the raw data for specific
624 AliDebug(1,Form("Selecting raw data for detector %s",detName));
625 fRawReader->Select(detName);
628 //_____________________________________________________________________________
629 Double_t AliAltroRawStreamV3::GetTSample() const
631 // Returns the sampling time
632 // in seconds. In case the rcu trailer
633 // was note read, return an invalid number (0)
635 if (!fRCUTrailerData) return 0.;
637 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
638 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
643 tSample = 2.0*kLHCTimeSample;
647 tSample = 4.0*kLHCTimeSample;
651 tSample = 8.0*kLHCTimeSample;
654 AliWarning(Form("Invalid sampling frequency value %d !",
663 //_____________________________________________________________________________
664 Double_t AliAltroRawStreamV3::GetL1Phase() const
666 // Returns the L1 phase w.r.t to the
668 if (!fRCUTrailerData) return 0.;
670 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
671 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
673 Double_t tSample = GetTSample();
674 if (phase >= tSample) {
675 AliWarning(Form("Invalid L1 trigger phase (%f >= %f) !",
683 //_____________________________________________________________________________
684 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
686 // Signal a mapping error
687 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
688 // classes in order to log an error related to bad altro mapping
690 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);
693 //_____________________________________________________________________________
694 UChar_t *AliAltroRawStreamV3::GetRCUPayloadInSOD() const
696 // Get a pointer to the data in case
699 if (fRawReader->GetType() == AliRawEventHeaderBase::kStartOfData) {
706 //_____________________________________________________________________________
707 Int_t AliAltroRawStreamV3::GetRCUPayloadSizeInSOD() const
709 // Get the size of the RCU data in case
712 if (fRawReader->GetType() == AliRawEventHeaderBase::kStartOfData) {