]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliAltroRawStreamV3.cxx
Initial version of the new RCU format decoder. The user interface is the same as...
[u/mrichter/AliRoot.git] / RAW / AliAltroRawStreamV3.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 ///////////////////////////////////////////////////////////////////////////////
17 ///
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.
24 ///
25 ///
26 /// cvetan.cheshkov@cern.ch 1/04/2009
27 ///////////////////////////////////////////////////////////////////////////////
28
29 #include "AliAltroRawStreamV3.h"
30 #include "AliRawReader.h"
31 #include "AliLog.h"
32
33 ClassImp(AliAltroRawStreamV3)
34
35
36 //_____________________________________________________________________________
37 AliAltroRawStreamV3::AliAltroRawStreamV3(AliRawReader* rawReader) :
38   fIsShortDataHeader(kFALSE),
39   fDDLNumber(-1),
40   fRCUId(-1),
41   fHWAddress(-1),
42   fRawReader(rawReader),
43   fData(NULL),
44   fPosition(-1),
45   fCount(-1),
46   fStartTimeBin(-1),
47   fBunchLength(-1),
48   fBadChannel(kFALSE),
49   fPayloadSize(-1),
50   fRCUTrailerData(NULL),
51   fRCUTrailerSize(0),
52   fFECERRA(0),
53   fFECERRB(0),
54   fERRREG2(0),
55   fERRREG3(0),
56   fERRREG4(0),
57   fActiveFECsA(0),
58   fActiveFECsB(0),
59   fAltroCFG1(0),
60   fAltroCFG2(0)
61 {
62   // Constructor
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;
66 }
67
68 //_____________________________________________________________________________
69 AliAltroRawStreamV3::~AliAltroRawStreamV3()
70 {
71 // destructor
72 // nothing to do
73 }
74
75 //_____________________________________________________________________________
76 AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
77   TObject(stream),
78   fIsShortDataHeader(stream.fIsShortDataHeader),
79   fDDLNumber(stream.fDDLNumber),
80   fRCUId(stream.fRCUId),
81   fHWAddress(stream.fHWAddress),
82   fRawReader(stream.fRawReader),
83   fData(stream.fData),
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)
101 {
102   // Copy constructor
103   // Copy the bunch data array
104   for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
105 }
106
107 //_____________________________________________________________________________
108 AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
109 {
110   // assignment operator
111   // ... 
112   if(&stream == this) return *this;
113
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;
137
138   for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
139
140   return *this;
141 }
142
143 //_____________________________________________________________________________
144 void AliAltroRawStreamV3::Reset()
145 {
146 // Complete reset of raw stream params
147 // Reset of the raw-reader as well
148
149   fDDLNumber = fRCUId = fHWAddress = -1;
150   fPosition = fCount = -1;
151   fBunchLength = fStartTimeBin = -1;
152   fBadChannel = kFALSE;
153   fPayloadSize = -1;
154
155   fRCUTrailerData = NULL;
156   fRCUTrailerSize = 0;
157
158   fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fERRREG4 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
159
160   if (fRawReader) fRawReader->Reset();
161
162 }
163
164 //_____________________________________________________________________________
165 Bool_t AliAltroRawStreamV3::NextDDL()
166 {
167 // Read the next DDL payload (CDH + RCU trailer)
168 // Updates the information which is coming from these
169 // two sources
170
171   fPosition = 0;
172   // Get next DDL payload
173   // return wtih false in case no more data payloads
174   // are found
175   do {
176     if (!fRawReader->ReadNextData(fData)) return kFALSE;
177   } while (fRawReader->GetDataSize() == 0);
178
179   fDDLNumber = fRawReader->GetDDLID();
180
181   UChar_t rcuVer = fRawReader->GetBlockAttributes();
182
183   if (!ReadRCUTrailer(rcuVer)) return kFALSE;
184
185   return kTRUE;
186 }
187
188 //_____________________________________________________________________________
189 Bool_t AliAltroRawStreamV3::NextChannel()
190 {
191   // Read the next Altro channel from the
192   // raw-data stream
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
196   fCount = -1;
197   fBadChannel = kFALSE;
198
199   UInt_t word = 0;
200   do {
201     word = Get32bitWord(fPosition++);
202     if (fPosition >= fPayloadSize) return kFALSE;
203   }
204   while (((word >> 30) & 0x3) != 1);
205
206   // check for readout errors
207   if ((word >> 29) & 0x1) {
208     fBadChannel = kTRUE;
209   }
210
211   // extract channel payload and hw address
212   if (!fBadChannel) fCount = (word >> 16) & 0x3FF; 
213   fHWAddress = word & 0xFFF;
214
215   return kTRUE;
216 }
217
218 //_____________________________________________________________________________
219 Bool_t AliAltroRawStreamV3::NextBunch()
220 {
221   // Read next altro bunch from the
222   // raw-data stream.
223   // Updates the start/end time-bins
224   // and the array with altro samples
225   fBunchLength = fStartTimeBin = -1;
226
227   if ((fCount <= 0) || fBadChannel) return kFALSE;
228
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",
233                     fHWAddress,word));
234     fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
235     fPosition--;
236     fCount = -1;
237     return kFALSE;
238   }
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;
246     return kFALSE;
247   }
248
249   fCount -= fBunchLength;
250
251   fBunchLength -= 2;
252
253   fStartTimeBin = (word >> 10) & 0x3FF;
254
255   Int_t isample = 0;
256
257   fBunchData[isample++] = word & 0x3FF; // first sample
258
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",
265                       fHWAddress,word));
266       fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
267       fCount = -1;
268       fPosition--;
269       return kTRUE;
270     }
271     fBunchData[isample++] = (word >> 20) & 0x3FF;
272     fBunchData[isample++] = (word >> 10) & 0x3FF;
273     fBunchData[isample++] = word & 0x3FF;
274   }
275
276   return kTRUE;
277 }
278
279 //_____________________________________________________________________________
280 UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
281 {
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)
286   // independent.
287
288   index = (index << 2); 
289   UInt_t word  = 0;
290   word |= fData[index++];
291   word |= fData[index++] << 8;
292   word |= fData[index++] << 16;
293   word |= fData[index++] << 24;
294
295   return word;
296 }
297
298 ///_____________________________________________________________________________
299 Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
300 {
301   // Read the RCU trailer according
302   // to the RCU formware version
303   // specified in CDH
304   // Cross-check with version found in the
305   // trailer
306
307   fRCUTrailerData = NULL;
308   fRCUTrailerSize = 0;
309   fPayloadSize = -1;
310
311   Int_t index = fRawReader->GetDataSize()/4;
312
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);
319
320   if ((word >> 30) != 3) {
321     fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
322     AliError("Last RCU trailer word not found!");
323     return kFALSE;
324   }
325
326   UChar_t ver = (word >> 16) & 0xFF;
327   if (ver != rcuVer) {
328     // Wrong RCU firmware version detected
329     fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
330                                                  ver,rcuVer));
331     AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
332                     ver,rcuVer));
333   }
334
335   fRCUId = (Int_t)((word >> 7) & 0x1FF);
336   Int_t trailerSize = (word & 0x7F);
337
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",
342                                                      trailerSize*4));
343     AliWarning(Form("Invalid trailer size found (%d bytes) !",
344                     trailerSize*4));
345     return kFALSE;
346   }
347
348   trailerSize -= 2;
349   fRCUTrailerSize = trailerSize*4;
350
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!");
356       continue;
357     }
358     Int_t parCode = (word >> 26) & 0xF;
359     Int_t parData = word & 0x3FFFFFF;
360     switch (parCode) {
361     case 1:
362       // ERR_REG1
363       fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
364       fFECERRB = ((parData & 0x1FFF)) << 7;
365       break;
366     case 2:
367       // ERR_REG2
368       fERRREG2 = parData & 0x1FF;
369       break;
370     case 3:
371       // ERR_REG3
372       fERRREG3 = parData & 0xFFF;
373       break;
374     case 4:
375       // ERR_REG4
376       fERRREG4 = parData & 0xFFF;
377       break;
378     case 5:
379       // FEC_RO_A
380       fActiveFECsA = parData & 0xFFFF;
381       break;
382     case 6:
383       // FEC_RO_B
384       fActiveFECsB = parData & 0xFFFF;
385       break;
386     case 7:
387       // RDO_CFG1
388       fAltroCFG1 = parData & 0xFFFFF;
389       break;
390     case 8:
391       // RDO_CFG2
392       fAltroCFG2 = parData & 0x1FFFFFF;
393      break;
394     default:
395       fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
396       AliWarning(Form("Undefined parameter code %d, ignore it !",
397                       parCode));
398       break;
399     }
400   }
401
402   if (index < 1) {
403     fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
404                                                      fRCUTrailerSize+8,
405                                                      fRawReader->GetDataSize()));
406     AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
407                     fRCUTrailerSize,
408                     fRawReader->GetDataSize()));
409   }
410
411   fRCUTrailerData = fData + index*4;
412
413   // Now read the payload size
414   // (First word in the RCU trailer)
415   fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
416
417   if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
418     fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
419                                                          fRawReader->GetDataSize(),
420                                                          fRCUTrailerSize+8,
421                                                          fPayloadSize*4));
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(),
424                     fRCUTrailerSize+8,
425                     fPayloadSize*4));
426   }
427
428   return kTRUE;
429 }
430
431 //_____________________________________________________________________________
432 Int_t AliAltroRawStreamV3::GetBranch() const
433 {
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;
439
440   return ((fHWAddress >> 11) & 0x1);
441 }
442
443 //_____________________________________________________________________________
444 Int_t AliAltroRawStreamV3::GetFEC() const
445 {
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;
451
452   return ((fHWAddress >> 7) & 0xF);
453 }
454
455 //_____________________________________________________________________________
456 Int_t AliAltroRawStreamV3::GetAltro() const
457 {
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;
463
464   return ((fHWAddress >> 4) & 0x7);
465 }
466
467 //_____________________________________________________________________________
468 Int_t AliAltroRawStreamV3::GetChannel() const
469 {
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;
475
476   return (fHWAddress & 0xF);
477 }
478
479 //_____________________________________________________________________________
480 Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
481 {
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 !");
488     data = NULL;
489     return kFALSE;
490   }
491
492   data = fRCUTrailerData;
493
494   return kTRUE;
495 }
496
497 //_____________________________________________________________________________
498 void AliAltroRawStreamV3::PrintRCUTrailer() const
499 {
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");
521 }
522
523 //_____________________________________________________________________________
524 void AliAltroRawStreamV3::SelectRawData(Int_t detId)
525 {
526   // Select the raw data for specific
527   // detector id
528   AliDebug(1,Form("Selecting raw data for detector %d",detId));
529   fRawReader->Select(detId);
530 }
531
532 //_____________________________________________________________________________
533 void AliAltroRawStreamV3::SelectRawData(const char *detName)
534 {
535   // Select the raw data for specific
536   // detector name
537   AliDebug(1,Form("Selecting raw data for detector %s",detName));
538   fRawReader->Select(detName);
539 }
540
541 //_____________________________________________________________________________
542 Double_t AliAltroRawStreamV3::GetTSample() const
543 {
544   // Returns the sampling time
545   // in seconds. In case the rcu trailer
546   // was note read, return an invalid number (0)
547
548   if (!fRCUTrailerData) return 0.;
549
550   const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
551   UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
552   Double_t tSample;
553   switch (fq) {
554   case 0:
555     // 20 MHz
556     tSample = 2.0*kLHCTimeSample;
557     break;
558   case 1:
559     // 10 Mhz
560     tSample = 4.0*kLHCTimeSample;
561     break;
562   case 2:
563     // 5 MHz
564     tSample = 8.0*kLHCTimeSample;
565     break;
566   default:
567     AliWarning(Form("Invalid sampling frequency value %d !",
568                       fq));
569     tSample = 0.;
570     break;
571   }
572
573   return tSample;
574 }
575
576 //_____________________________________________________________________________
577 Double_t AliAltroRawStreamV3::GetL1Phase() const
578 {
579   // Returns the L1 phase w.r.t to the
580   // LHC clock
581   if (!fRCUTrailerData) return 0.;
582
583   const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
584   Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
585
586   Double_t tSample = GetTSample();
587   if (phase >= tSample) {
588     AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
589                     phase,tSample));
590     phase = 0.;
591   }
592
593   return phase;
594 }
595
596 //_____________________________________________________________________________
597 void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
598 {
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
602
603   if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);
604 }