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 fRCUTrailerData(NULL),
63 // Create an object to read Altro raw digits in
64 // RCU version 3 and beyond format
65 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
68 //_____________________________________________________________________________
69 AliAltroRawStreamV3::~AliAltroRawStreamV3()
75 //_____________________________________________________________________________
76 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
78 fIsShortDataHeader(stream.fIsShortDataHeader),
79 fDDLNumber(stream.fDDLNumber),
80 fRCUId(stream.fRCUId),
81 fHWAddress(stream.fHWAddress),
82 fRawReader(stream.fRawReader),
84 fPosition(stream.fPosition),
85 fCount(stream.fCount),
86 fStartTimeBin(stream.fStartTimeBin),
87 fBunchLength(stream.fBunchLength),
88 fBadChannel(stream.fBadChannel),
89 fPayloadSize(stream.fPayloadSize),
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)
103 // Copy the bunch data array
104 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
107 //_____________________________________________________________________________
108 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
110 // assignment operator
112 if(&stream == this) return *this;
114 fIsShortDataHeader = stream.fIsShortDataHeader;
115 fDDLNumber = stream.fDDLNumber;
116 fRCUId = stream.fRCUId;
117 fHWAddress = stream.fHWAddress;
118 fRawReader = stream.fRawReader;
119 fData = stream.fData;
120 fPosition = stream.fPosition;
121 fCount = stream.fCount;
122 fStartTimeBin = stream.fStartTimeBin;
123 fBunchLength = stream.fBunchLength;
124 fBadChannel = stream.fBadChannel;
125 fPayloadSize = stream.fPayloadSize;
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;
138 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
143 //_____________________________________________________________________________
144 void AliAltroRawStreamV3::Reset()
146 // Complete reset of raw stream params
147 // Reset of the raw-reader as well
149 fDDLNumber = fRCUId = fHWAddress = -1;
150 fPosition = fCount = -1;
151 fBunchLength = fStartTimeBin = -1;
152 fBadChannel = kFALSE;
155 fRCUTrailerData = NULL;
158 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fERRREG4 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
160 if (fRawReader) fRawReader->Reset();
164 //_____________________________________________________________________________
165 Bool_t AliAltroRawStreamV3::NextDDL()
167 // Read the next DDL payload (CDH + RCU trailer)
168 // Updates the information which is coming from these
172 // Get next DDL payload
173 // return wtih false in case no more data payloads
176 if (!fRawReader->ReadNextData(fData)) return kFALSE;
177 } while (fRawReader->GetDataSize() == 0);
179 fDDLNumber = fRawReader->GetDDLID();
181 UChar_t rcuVer = fRawReader->GetBlockAttributes();
183 if (!ReadRCUTrailer(rcuVer)) return kFALSE;
188 //_____________________________________________________________________________
189 Bool_t AliAltroRawStreamV3::NextChannel()
191 // Read the next Altro channel from the
193 // Updates the channel hardware address member and
194 // channel data size. Sets the error flag in case
195 // RCU signals readout error in this channel
197 fBadChannel = kFALSE;
201 word = Get32bitWord(fPosition++);
202 if (fPosition >= fPayloadSize) return kFALSE;
204 while (((word >> 30) & 0x3) != 1);
206 // check for readout errors
207 if ((word >> 29) & 0x1) {
211 // extract channel payload and hw address
212 if (!fBadChannel) fCount = (word >> 16) & 0x3FF;
213 fHWAddress = word & 0xFFF;
218 //_____________________________________________________________________________
219 Bool_t AliAltroRawStreamV3::NextBunch()
221 // Read next altro bunch from the
223 // Updates the start/end time-bins
224 // and the array with altro samples
225 fBunchLength = fStartTimeBin = -1;
227 if ((fCount <= 0) || fBadChannel) return kFALSE;
229 UInt_t word = Get32bitWord(fPosition++);
230 if ((word >> 30) != 0) {
231 // Unexpected end of altro channel payload
232 AliWarning(Form("Unexpected end of payload in altro channel bunch header! Address=0x%x, word=0x%x",
234 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
239 fBunchLength = (word >> 20) & 0x3FF;
240 if (fBunchLength > fCount) {
241 // Too long bunch detected
242 AliWarning(Form("Too long bunch detected in Address=0x%x ! Exptected <= %d 10-bit words, found %d !",
243 fCount,fBunchLength));
244 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
245 fCount = fBunchLength = -1;
249 fCount -= fBunchLength;
253 fStartTimeBin = (word >> 10) & 0x3FF;
257 fBunchData[isample++] = word & 0x3FF; // first sample
259 Int_t nwords = (fBunchLength+1)/3;
260 for (Int_t iword = 0; iword < nwords; iword++) {
261 word = Get32bitWord(fPosition++);
262 if ((word >> 30) != 0) {
263 // Unexpected end of altro channel payload
264 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
266 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
271 fBunchData[isample++] = (word >> 20) & 0x3FF;
272 fBunchData[isample++] = (word >> 10) & 0x3FF;
273 fBunchData[isample++] = word & 0x3FF;
279 //_____________________________________________________________________________
280 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
282 // This method returns the 32 bit word at a given
283 // position inside the raw data payload.
284 // The 'index' points to the beginning of the word.
285 // The method is supposed to be endian (platform)
288 index = (index << 2);
290 word |= fData[index++];
291 word |= fData[index++] << 8;
292 word |= fData[index++] << 16;
293 word |= fData[index++] << 24;
298 ///_____________________________________________________________________________
299 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
301 // Read the RCU trailer according
302 // to the RCU formware version
304 // Cross-check with version found in the
307 fRCUTrailerData = NULL;
311 Int_t index = fRawReader->GetDataSize()/4;
313 // First read 32-bit word with the
314 // trailer size (7 bits), RCU ID (9 bits) and
315 // RCU firmware version (8 bits?)
316 // The two major bit should be 11 (identifies
317 // the end of the trailer)
318 UInt_t word = Get32bitWord(--index);
320 if ((word >> 30) != 3) {
321 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
322 AliError("Last RCU trailer word not found!");
326 UChar_t ver = (word >> 16) & 0xFF;
328 // Wrong RCU firmware version detected
329 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
331 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
335 fRCUId = (Int_t)((word >> 7) & 0x1FF);
336 Int_t trailerSize = (word & 0x7F);
338 // Now read the beginning of the trailer
339 // where the payload size is written
340 if (trailerSize < 2) {
341 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
343 AliWarning(Form("Invalid trailer size found (%d bytes) !",
349 fRCUTrailerSize = trailerSize*4;
351 for (; trailerSize > 0; trailerSize--) {
352 word = Get32bitWord(--index);
353 if ((word >> 30) != 2) {
354 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
355 AliWarning("Missing RCU trailer identifier pattern!");
358 Int_t parCode = (word >> 26) & 0xF;
359 Int_t parData = word & 0x3FFFFFF;
363 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
364 fFECERRB = ((parData & 0x1FFF)) << 7;
368 fERRREG2 = parData & 0x1FF;
372 fERRREG3 = parData & 0xFFF;
376 fERRREG4 = parData & 0xFFF;
380 fActiveFECsA = parData & 0xFFFF;
384 fActiveFECsB = parData & 0xFFFF;
388 fAltroCFG1 = parData & 0xFFFFF;
392 fAltroCFG2 = parData & 0x1FFFFFF;
395 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
396 AliWarning(Form("Undefined parameter code %d, ignore it !",
403 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
405 fRawReader->GetDataSize()));
406 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
408 fRawReader->GetDataSize()));
411 fRCUTrailerData = fData + index*4;
413 // Now read the payload size
414 // (First word in the RCU trailer)
415 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
417 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
418 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
419 fRawReader->GetDataSize(),
422 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 !",
423 fRawReader->GetDataSize(),
431 //_____________________________________________________________________________
432 Int_t AliAltroRawStreamV3::GetBranch() const
434 // The method provides the RCU branch index (0 or 1)
435 // for the current hardware address.
436 // In case the hardware address has not been yet
437 // initialized, the method returns -1
438 if (fHWAddress == -1) return -1;
440 return ((fHWAddress >> 11) & 0x1);
443 //_____________________________________________________________________________
444 Int_t AliAltroRawStreamV3::GetFEC() const
446 // The method provides the front-end card index
447 // for the current hardware address.
448 // In case the hardware address has not been yet
449 // initialized, the method returns -1
450 if (fHWAddress == -1) return -1;
452 return ((fHWAddress >> 7) & 0xF);
455 //_____________________________________________________________________________
456 Int_t AliAltroRawStreamV3::GetAltro() const
458 // The method provides the altro chip index
459 // for the current hardware address.
460 // In case the hardware address has not been yet
461 // initialized, the method returns -1
462 if (fHWAddress == -1) return -1;
464 return ((fHWAddress >> 4) & 0x7);
467 //_____________________________________________________________________________
468 Int_t AliAltroRawStreamV3::GetChannel() const
470 // The method provides the channel index
471 // for the current hardware address.
472 // In case the hardware address has not been yet
473 // initialized, the method returns -1
474 if (fHWAddress == -1) return -1;
476 return (fHWAddress & 0xF);
479 //_____________________________________________________________________________
480 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
482 // Return a pointer to the RCU trailer
483 // data. Should be called always after
484 // the RCU trailer was already processed
485 // in the GetPosition() method
486 if (!fRCUTrailerSize || !fRCUTrailerData) {
487 AliError("No valid RCU trailer data is found !");
492 data = fRCUTrailerData;
497 //_____________________________________________________________________________
498 void AliAltroRawStreamV3::PrintRCUTrailer() const
500 // Prints the contents of
501 // the RCU trailer data
502 printf("RCU trailer:\n===========\n");
503 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
504 printf("ERRREG2: 0x%x\n",fERRREG2);
505 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
506 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
507 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
508 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
509 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
510 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
511 printf("GlitchFilter: %d\n",GetGlitchFilter());
512 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
513 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
514 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
515 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
516 printf("Sparse readout: %d\n",GetSparseRO());
517 printf("Sampling time: %e s\n",GetTSample());
518 printf("L1 Phase: %e s\n",GetL1Phase());
519 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
520 printf("===========\n");
523 //_____________________________________________________________________________
524 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
526 // Select the raw data for specific
528 AliDebug(1,Form("Selecting raw data for detector %d",detId));
529 fRawReader->Select(detId);
532 //_____________________________________________________________________________
533 void AliAltroRawStreamV3::SelectRawData(const char *detName)
535 // Select the raw data for specific
537 AliDebug(1,Form("Selecting raw data for detector %s",detName));
538 fRawReader->Select(detName);
541 //_____________________________________________________________________________
542 Double_t AliAltroRawStreamV3::GetTSample() const
544 // Returns the sampling time
545 // in seconds. In case the rcu trailer
546 // was note read, return an invalid number (0)
548 if (!fRCUTrailerData) return 0.;
550 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
551 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
556 tSample = 2.0*kLHCTimeSample;
560 tSample = 4.0*kLHCTimeSample;
564 tSample = 8.0*kLHCTimeSample;
567 AliWarning(Form("Invalid sampling frequency value %d !",
576 //_____________________________________________________________________________
577 Double_t AliAltroRawStreamV3::GetL1Phase() const
579 // Returns the L1 phase w.r.t to the
581 if (!fRCUTrailerData) return 0.;
583 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
584 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
586 Double_t tSample = GetTSample();
587 if (phase >= tSample) {
588 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
596 //_____________________________________________________________________________
597 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
599 // Signal a mapping error
600 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
601 // classes in order to log an error related to bad altro mapping
603 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);