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"
33 ClassImp(AliAltroRawStreamV3)
36 //_____________________________________________________________________________
37 AliAltroRawStreamV3::AliAltroRawStreamV3(AliRawReader* rawReader) :
38 fIsShortDataHeader(kFALSE),
42 fRawReader(rawReader),
50 fBunchDataPointer(NULL),
52 fRCUTrailerData(NULL),
64 // Create an object to read Altro raw digits in
65 // RCU version 3 and beyond format
66 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
69 //_____________________________________________________________________________
70 AliAltroRawStreamV3::~AliAltroRawStreamV3()
76 //_____________________________________________________________________________
77 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
79 fIsShortDataHeader(stream.fIsShortDataHeader),
80 fDDLNumber(stream.fDDLNumber),
81 fRCUId(stream.fRCUId),
82 fHWAddress(stream.fHWAddress),
83 fRawReader(stream.fRawReader),
85 fPosition(stream.fPosition),
86 fCount(stream.fCount),
87 fStartTimeBin(stream.fStartTimeBin),
88 fBunchLength(stream.fBunchLength),
89 fBadChannel(stream.fBadChannel),
90 fPayloadSize(stream.fPayloadSize),
91 fBunchDataPointer(stream.fBunchDataPointer),
92 fBunchDataIndex(stream.fBunchDataIndex),
93 fRCUTrailerData(stream.fRCUTrailerData),
94 fRCUTrailerSize(stream.fRCUTrailerSize),
95 fFECERRA(stream.fFECERRA),
96 fFECERRB(stream.fFECERRB),
97 fERRREG2(stream.fERRREG2),
98 fERRREG3(stream.fERRREG3),
99 fActiveFECsA(stream.fActiveFECsA),
100 fActiveFECsB(stream.fActiveFECsB),
101 fAltroCFG1(stream.fAltroCFG1),
102 fAltroCFG2(stream.fAltroCFG2)
105 // Copy the bunch data array
106 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
109 //_____________________________________________________________________________
110 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
112 // assignment operator
114 if(&stream == this) return *this;
116 fIsShortDataHeader = stream.fIsShortDataHeader;
117 fDDLNumber = stream.fDDLNumber;
118 fRCUId = stream.fRCUId;
119 fHWAddress = stream.fHWAddress;
120 fRawReader = stream.fRawReader;
121 fData = stream.fData;
122 fPosition = stream.fPosition;
123 fCount = stream.fCount;
124 fStartTimeBin = stream.fStartTimeBin;
125 fBunchLength = stream.fBunchLength;
126 fBadChannel = stream.fBadChannel;
127 fPayloadSize = stream.fPayloadSize;
128 fBunchDataPointer = stream.fBunchDataPointer;
129 fBunchDataIndex = stream.fBunchDataIndex;
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 fActiveFECsA = stream.fActiveFECsA;
137 fActiveFECsB = stream.fActiveFECsB;
138 fAltroCFG1 = stream.fAltroCFG1;
139 fAltroCFG2 = stream.fAltroCFG2;
141 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
146 //_____________________________________________________________________________
147 void AliAltroRawStreamV3::Reset()
149 // Complete reset of raw stream params
150 // Reset of the raw-reader as well
152 fDDLNumber = fRCUId = fHWAddress = -1;
153 fPosition = fCount = -1;
154 fBunchLength = fStartTimeBin = -1;
155 fBadChannel = kFALSE;
157 fBunchDataPointer = NULL;
158 fBunchDataIndex = -1;
160 fRCUTrailerData = NULL;
163 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
165 if (fRawReader) fRawReader->Reset();
169 //_____________________________________________________________________________
170 Bool_t AliAltroRawStreamV3::NextDDL()
172 // Read the next DDL payload (CDH + RCU trailer)
173 // Updates the information which is coming from these
177 // Get next DDL payload
178 // return wtih false in case no more data payloads
181 if (!fRawReader->ReadNextData(fData)) return kFALSE;
182 } while (fRawReader->GetDataSize() == 0);
184 fDDLNumber = fRawReader->GetDDLID();
186 UChar_t rcuVer = fRawReader->GetBlockAttributes();
188 if (!ReadRCUTrailer(rcuVer)) return kFALSE;
193 //_____________________________________________________________________________
194 Bool_t AliAltroRawStreamV3::NextChannel()
196 // Read the next Altro channel from the
198 // Updates the channel hardware address member and
199 // channel data size. Sets the error flag in case
200 // RCU signals readout error in this channel
202 fBadChannel = kFALSE;
207 word = Get32bitWord(fPosition++);
208 if (fPosition > fPayloadSize) return kFALSE;
210 while ((word >> 30) != 1);
212 // check for readout errors
213 fBadChannel = (word >> 29) & 0x1;
215 // extract channel payload and hw address
216 fCount = (word >> 16) & 0x3FF;
217 fHWAddress = word & 0xFFF;
219 // Now unpack the altro data
220 // Revert the order of the samples
221 // inside the bunch so that the
222 // first time is first in the samples
225 Int_t nwords = (fCount+2)/3;
226 for (Int_t iword = 0; iword < nwords; iword++) {
227 word = Get32bitWord(fPosition++);
228 if ((word >> 30) != 0) {
229 // Unexpected end of altro channel payload
230 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
232 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
237 fBunchData[isample++] = (word >> 20) & 0x3FF;
238 fBunchData[isample++] = (word >> 10) & 0x3FF;
239 fBunchData[isample++] = word & 0x3FF;
245 //_____________________________________________________________________________
246 Bool_t AliAltroRawStreamV3::NextBunch()
248 // Read next altro bunch from the
250 // Updates the start/end time-bins
251 // and the array with altro samples
252 fBunchLength = fStartTimeBin = -1;
253 fBunchDataPointer = NULL;
255 if ((fBunchDataIndex >= fCount) || fBadChannel) return kFALSE;
257 fBunchLength = fBunchData[fBunchDataIndex];
258 if ((fBunchDataIndex + fBunchLength) > fCount) {
259 // Too long bunch detected
260 AliWarning(Form("Too long bunch detected in Address=0x%x ! Expected <= %d 10-bit words, found %d !",
261 fHWAddress,fCount-fBunchDataIndex,fBunchLength));
262 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
263 fCount = fBunchLength = -1;
269 fStartTimeBin = fBunchData[fBunchDataIndex++];
271 fBunchDataPointer = &fBunchData[fBunchDataIndex];
273 fBunchDataIndex += fBunchLength;
278 //_____________________________________________________________________________
279 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
281 // This method returns the 32 bit word at a given
282 // position inside the raw data payload.
283 // The 'index' points to the beginning of the word.
284 // The method is supposed to be endian (platform)
287 index = (index << 2);
289 word |= fData[index++];
290 word |= fData[index++] << 8;
291 word |= fData[index++] << 16;
292 word |= fData[index++] << 24;
297 ///_____________________________________________________________________________
298 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
300 // Read the RCU trailer according
301 // to the RCU formware version
303 // Cross-check with version found in the
306 fRCUTrailerData = NULL;
310 Int_t index = fRawReader->GetDataSize()/4;
312 // First read 32-bit word with the
313 // trailer size (7 bits), RCU ID (9 bits) and
314 // RCU firmware version (8 bits?)
315 // The two major bit should be 11 (identifies
316 // the end of the trailer)
317 UInt_t word = Get32bitWord(--index);
319 if ((word >> 30) != 3) {
320 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
321 AliError("Last RCU trailer word not found!");
325 UChar_t ver = (word >> 16) & 0xFF;
327 // Wrong RCU firmware version detected
328 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
330 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
334 fRCUId = (Int_t)((word >> 7) & 0x1FF);
335 Int_t trailerSize = (word & 0x7F);
337 // Now read the beginning of the trailer
338 // where the payload size is written
339 if (trailerSize < 2) {
340 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
342 AliWarning(Form("Invalid trailer size found (%d bytes) !",
348 fRCUTrailerSize = trailerSize*4;
350 for (; trailerSize > 0; trailerSize--) {
351 word = Get32bitWord(--index);
352 if ((word >> 30) != 2) {
353 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
354 AliWarning("Missing RCU trailer identifier pattern!");
357 Int_t parCode = (word >> 26) & 0xF;
358 Int_t parData = word & 0x3FFFFFF;
362 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
363 fFECERRB = ((parData & 0x1FFF)) << 7;
367 fERRREG2 = parData & 0x1FF;
371 fERRREG3 = parData & 0x1FFFFFF;
375 fActiveFECsA = parData & 0xFFFF;
379 fActiveFECsB = parData & 0xFFFF;
383 fAltroCFG1 = parData & 0xFFFFF;
387 fAltroCFG2 = parData & 0x1FFFFFF;
390 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
391 AliWarning(Form("Undefined parameter code %d, ignore it !",
398 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
400 fRawReader->GetDataSize()));
401 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
403 fRawReader->GetDataSize()));
406 fRCUTrailerData = fData + index*4;
408 // Now read the payload size
409 // (First word in the RCU trailer)
410 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
412 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
413 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
414 fRawReader->GetDataSize(),
417 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 !",
418 fRawReader->GetDataSize(),
426 //_____________________________________________________________________________
427 Int_t AliAltroRawStreamV3::GetBranch() const
429 // The method provides the RCU branch index (0 or 1)
430 // for the current hardware address.
431 // In case the hardware address has not been yet
432 // initialized, the method returns -1
433 if (fHWAddress == -1) return -1;
435 return ((fHWAddress >> 11) & 0x1);
438 //_____________________________________________________________________________
439 Int_t AliAltroRawStreamV3::GetFEC() const
441 // The method provides the front-end card index
442 // for the current hardware address.
443 // In case the hardware address has not been yet
444 // initialized, the method returns -1
445 if (fHWAddress == -1) return -1;
447 return ((fHWAddress >> 7) & 0xF);
450 //_____________________________________________________________________________
451 Int_t AliAltroRawStreamV3::GetAltro() const
453 // The method provides the altro chip index
454 // for the current hardware address.
455 // In case the hardware address has not been yet
456 // initialized, the method returns -1
457 if (fHWAddress == -1) return -1;
459 return ((fHWAddress >> 4) & 0x7);
462 //_____________________________________________________________________________
463 Int_t AliAltroRawStreamV3::GetChannel() const
465 // The method provides the channel index
466 // for the current hardware address.
467 // In case the hardware address has not been yet
468 // initialized, the method returns -1
469 if (fHWAddress == -1) return -1;
471 return (fHWAddress & 0xF);
474 //_____________________________________________________________________________
475 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
477 // Return a pointer to the RCU trailer
478 // data. Should be called always after
479 // the RCU trailer was already processed
480 // in the GetPosition() method
481 if (!fRCUTrailerSize || !fRCUTrailerData) {
482 AliError("No valid RCU trailer data is found !");
487 data = fRCUTrailerData;
492 //_____________________________________________________________________________
493 void AliAltroRawStreamV3::PrintRCUTrailer() const
495 // Prints the contents of
496 // the RCU trailer data
497 printf("RCU trailer:\n===========\n");
498 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
499 printf("ERRREG2: 0x%x\n",fERRREG2);
500 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
501 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
502 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
503 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
504 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
505 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
506 printf("GlitchFilter: %d\n",GetGlitchFilter());
507 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
508 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
509 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
510 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
511 printf("Sparse readout: %d\n",GetSparseRO());
512 printf("Sampling time: %e s\n",GetTSample());
513 printf("L1 Phase: %e s\n",GetL1Phase());
514 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
515 printf("===========\n");
518 //_____________________________________________________________________________
519 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
521 // Select the raw data for specific
523 AliDebug(1,Form("Selecting raw data for detector %d",detId));
524 fRawReader->Select(detId);
527 //_____________________________________________________________________________
528 void AliAltroRawStreamV3::SelectRawData(const char *detName)
530 // Select the raw data for specific
532 AliDebug(1,Form("Selecting raw data for detector %s",detName));
533 fRawReader->Select(detName);
536 //_____________________________________________________________________________
537 Double_t AliAltroRawStreamV3::GetTSample() const
539 // Returns the sampling time
540 // in seconds. In case the rcu trailer
541 // was note read, return an invalid number (0)
543 if (!fRCUTrailerData) return 0.;
545 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
546 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
551 tSample = 2.0*kLHCTimeSample;
555 tSample = 4.0*kLHCTimeSample;
559 tSample = 8.0*kLHCTimeSample;
562 AliWarning(Form("Invalid sampling frequency value %d !",
571 //_____________________________________________________________________________
572 Double_t AliAltroRawStreamV3::GetL1Phase() const
574 // Returns the L1 phase w.r.t to the
576 if (!fRCUTrailerData) return 0.;
578 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
579 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
581 Double_t tSample = GetTSample();
582 if (phase >= tSample) {
583 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
591 //_____________________________________________________________________________
592 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
594 // Signal a mapping error
595 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
596 // classes in order to log an error related to bad altro mapping
598 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);