some histos added for TPC clusters
[u/mrichter/AliRoot.git] / RAW / AliAltroRawStreamV3.cxx
CommitLineData
f8e5b5a0 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
33ClassImp(AliAltroRawStreamV3)
34
35
36//_____________________________________________________________________________
37AliAltroRawStreamV3::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),
6d4dd848 50 fBunchDataPointer(NULL),
51 fBunchDataIndex(-1),
f8e5b5a0 52 fRCUTrailerData(NULL),
53 fRCUTrailerSize(0),
54 fFECERRA(0),
55 fFECERRB(0),
56 fERRREG2(0),
57 fERRREG3(0),
58 fERRREG4(0),
59 fActiveFECsA(0),
60 fActiveFECsB(0),
61 fAltroCFG1(0),
62 fAltroCFG2(0)
63{
64 // Constructor
65 // Create an object to read Altro raw digits in
66 // RCU version 3 and beyond format
67 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = 0;
68}
69
70//_____________________________________________________________________________
71AliAltroRawStreamV3::~AliAltroRawStreamV3()
72{
73// destructor
74// nothing to do
75}
76
77//_____________________________________________________________________________
78AliAltroRawStreamV3::AliAltroRawStreamV3(const AliAltroRawStreamV3& stream) :
79 TObject(stream),
80 fIsShortDataHeader(stream.fIsShortDataHeader),
81 fDDLNumber(stream.fDDLNumber),
82 fRCUId(stream.fRCUId),
83 fHWAddress(stream.fHWAddress),
84 fRawReader(stream.fRawReader),
85 fData(stream.fData),
86 fPosition(stream.fPosition),
87 fCount(stream.fCount),
88 fStartTimeBin(stream.fStartTimeBin),
89 fBunchLength(stream.fBunchLength),
90 fBadChannel(stream.fBadChannel),
91 fPayloadSize(stream.fPayloadSize),
6d4dd848 92 fBunchDataPointer(stream.fBunchDataPointer),
93 fBunchDataIndex(stream.fBunchDataIndex),
f8e5b5a0 94 fRCUTrailerData(stream.fRCUTrailerData),
95 fRCUTrailerSize(stream.fRCUTrailerSize),
96 fFECERRA(stream.fFECERRA),
97 fFECERRB(stream.fFECERRB),
98 fERRREG2(stream.fERRREG2),
99 fERRREG3(stream.fERRREG3),
100 fERRREG4(stream.fERRREG4),
101 fActiveFECsA(stream.fActiveFECsA),
102 fActiveFECsB(stream.fActiveFECsB),
103 fAltroCFG1(stream.fAltroCFG1),
104 fAltroCFG2(stream.fAltroCFG2)
105{
106 // Copy constructor
107 // Copy the bunch data array
108 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
109}
110
111//_____________________________________________________________________________
112AliAltroRawStreamV3& AliAltroRawStreamV3::operator = (const AliAltroRawStreamV3& stream)
113{
114 // assignment operator
115 // ...
116 if(&stream == this) return *this;
117
118 fIsShortDataHeader = stream.fIsShortDataHeader;
119 fDDLNumber = stream.fDDLNumber;
120 fRCUId = stream.fRCUId;
121 fHWAddress = stream.fHWAddress;
122 fRawReader = stream.fRawReader;
123 fData = stream.fData;
124 fPosition = stream.fPosition;
125 fCount = stream.fCount;
126 fStartTimeBin = stream.fStartTimeBin;
127 fBunchLength = stream.fBunchLength;
128 fBadChannel = stream.fBadChannel;
129 fPayloadSize = stream.fPayloadSize;
6d4dd848 130 fBunchDataPointer = stream.fBunchDataPointer;
131 fBunchDataIndex = stream.fBunchDataIndex;
f8e5b5a0 132 fRCUTrailerData = stream.fRCUTrailerData;
133 fRCUTrailerSize = stream.fRCUTrailerSize;
134 fFECERRA = stream.fFECERRA;
135 fFECERRB = stream.fFECERRB;
136 fERRREG2 = stream.fERRREG2;
137 fERRREG3 = stream.fERRREG3;
138 fERRREG4 = stream.fERRREG4;
139 fActiveFECsA = stream.fActiveFECsA;
140 fActiveFECsB = stream.fActiveFECsB;
141 fAltroCFG1 = stream.fAltroCFG1;
142 fAltroCFG2 = stream.fAltroCFG2;
143
144 for(Int_t i = 0; i < kMaxNTimeBins; i++) fBunchData[i] = stream.fBunchData[i];
145
146 return *this;
147}
148
149//_____________________________________________________________________________
150void AliAltroRawStreamV3::Reset()
151{
152// Complete reset of raw stream params
153// Reset of the raw-reader as well
154
155 fDDLNumber = fRCUId = fHWAddress = -1;
156 fPosition = fCount = -1;
157 fBunchLength = fStartTimeBin = -1;
158 fBadChannel = kFALSE;
159 fPayloadSize = -1;
6d4dd848 160 fBunchDataPointer = NULL;
161 fBunchDataIndex = -1;
f8e5b5a0 162
163 fRCUTrailerData = NULL;
164 fRCUTrailerSize = 0;
165
166 fFECERRA = fFECERRB = fERRREG2 = fERRREG3 = fERRREG4 = fActiveFECsA = fActiveFECsB = fAltroCFG1 = fAltroCFG2 = 0;
167
168 if (fRawReader) fRawReader->Reset();
169
170}
171
172//_____________________________________________________________________________
173Bool_t AliAltroRawStreamV3::NextDDL()
174{
175// Read the next DDL payload (CDH + RCU trailer)
176// Updates the information which is coming from these
177// two sources
178
179 fPosition = 0;
180 // Get next DDL payload
181 // return wtih false in case no more data payloads
182 // are found
183 do {
184 if (!fRawReader->ReadNextData(fData)) return kFALSE;
185 } while (fRawReader->GetDataSize() == 0);
186
187 fDDLNumber = fRawReader->GetDDLID();
188
189 UChar_t rcuVer = fRawReader->GetBlockAttributes();
190
191 if (!ReadRCUTrailer(rcuVer)) return kFALSE;
192
193 return kTRUE;
194}
195
196//_____________________________________________________________________________
197Bool_t AliAltroRawStreamV3::NextChannel()
198{
199 // Read the next Altro channel from the
200 // raw-data stream
201 // Updates the channel hardware address member and
202 // channel data size. Sets the error flag in case
203 // RCU signals readout error in this channel
204 fCount = -1;
205 fBadChannel = kFALSE;
6d4dd848 206 fBunchDataIndex = 0;
f8e5b5a0 207
208 UInt_t word = 0;
209 do {
210 word = Get32bitWord(fPosition++);
6d4dd848 211 if (fPosition > fPayloadSize) return kFALSE;
f8e5b5a0 212 }
6d4dd848 213 while ((word >> 30) != 1);
f8e5b5a0 214
215 // check for readout errors
6d4dd848 216 fBadChannel = (word >> 29) & 0x1;
f8e5b5a0 217
218 // extract channel payload and hw address
6d4dd848 219 fCount = (word >> 16) & 0x3FF;
f8e5b5a0 220 fHWAddress = word & 0xFFF;
221
6d4dd848 222 // Now unpack the altro data
223 // Revert the order of the samples
224 // inside the bunch so that the
225 // first time is first in the samples
226 // array
227 Int_t isample = 0;
228 Int_t nwords = (fCount+2)/3;
229 for (Int_t iword = 0; iword < nwords; iword++) {
230 word = Get32bitWord(fPosition++);
231 if ((word >> 30) != 0) {
232 // Unexpected end of altro channel payload
233 AliWarning(Form("Unexpected end of payload in altro channel payload! Address=0x%x, word=0x%x",
234 fHWAddress,word));
235 fRawReader->AddMinorErrorLog(kAltroPayloadErr,Form("hw=0x%x",fHWAddress));
236 fCount = -1;
237 fPosition--;
238 return kFALSE;
239 }
240 fBunchData[isample++] = (word >> 20) & 0x3FF;
241 fBunchData[isample++] = (word >> 10) & 0x3FF;
242 fBunchData[isample++] = word & 0x3FF;
243 }
244
f8e5b5a0 245 return kTRUE;
246}
247
248//_____________________________________________________________________________
249Bool_t AliAltroRawStreamV3::NextBunch()
250{
251 // Read next altro bunch from the
252 // raw-data stream.
253 // Updates the start/end time-bins
254 // and the array with altro samples
255 fBunchLength = fStartTimeBin = -1;
6d4dd848 256 fBunchDataPointer = NULL;
f8e5b5a0 257
6d4dd848 258 if ((fBunchDataIndex >= fCount) || fBadChannel) return kFALSE;
f8e5b5a0 259
6d4dd848 260 fBunchLength = fBunchData[fBunchDataIndex];
261 if ((fBunchDataIndex + fBunchLength) > fCount) {
f8e5b5a0 262 // Too long bunch detected
6d4dd848 263 AliWarning(Form("Too long bunch detected in Address=0x%x ! Expected <= %d 10-bit words, found %d !",
264 fHWAddress,fCount-fBunchDataIndex,fBunchLength));
f8e5b5a0 265 fRawReader->AddMinorErrorLog(kAltroBunchHeadErr,Form("hw=0x%x",fHWAddress));
266 fCount = fBunchLength = -1;
267 return kFALSE;
268 }
6d4dd848 269 fBunchDataIndex++;
f8e5b5a0 270 fBunchLength -= 2;
271
6d4dd848 272 fStartTimeBin = fBunchData[fBunchDataIndex++];
f8e5b5a0 273
6d4dd848 274 fBunchDataPointer = &fBunchData[fBunchDataIndex];
f8e5b5a0 275
6d4dd848 276 fBunchDataIndex += fBunchLength;
f8e5b5a0 277
278 return kTRUE;
279}
280
281//_____________________________________________________________________________
282UInt_t AliAltroRawStreamV3::Get32bitWord(Int_t index) const
283{
284 // This method returns the 32 bit word at a given
285 // position inside the raw data payload.
286 // The 'index' points to the beginning of the word.
287 // The method is supposed to be endian (platform)
288 // independent.
289
290 index = (index << 2);
291 UInt_t word = 0;
292 word |= fData[index++];
293 word |= fData[index++] << 8;
294 word |= fData[index++] << 16;
295 word |= fData[index++] << 24;
296
297 return word;
298}
299
300///_____________________________________________________________________________
301Bool_t AliAltroRawStreamV3::ReadRCUTrailer(UChar_t rcuVer)
302{
303 // Read the RCU trailer according
304 // to the RCU formware version
305 // specified in CDH
306 // Cross-check with version found in the
307 // trailer
308
309 fRCUTrailerData = NULL;
310 fRCUTrailerSize = 0;
311 fPayloadSize = -1;
312
313 Int_t index = fRawReader->GetDataSize()/4;
314
315 // First read 32-bit word with the
316 // trailer size (7 bits), RCU ID (9 bits) and
317 // RCU firmware version (8 bits?)
318 // The two major bit should be 11 (identifies
319 // the end of the trailer)
320 UInt_t word = Get32bitWord(--index);
321
322 if ((word >> 30) != 3) {
323 fRawReader->AddFatalErrorLog(kRCUTrailerErr,"");
324 AliError("Last RCU trailer word not found!");
325 return kFALSE;
326 }
327
328 UChar_t ver = (word >> 16) & 0xFF;
329 if (ver != rcuVer) {
330 // Wrong RCU firmware version detected
331 fRawReader->AddMajorErrorLog(kRCUVerErr,Form("%d!=%d",
332 ver,rcuVer));
333 AliDebug(1,Form("Wrong RCU firmware version detected: %d != %d",
334 ver,rcuVer));
335 }
336
337 fRCUId = (Int_t)((word >> 7) & 0x1FF);
338 Int_t trailerSize = (word & 0x7F);
339
340 // Now read the beginning of the trailer
341 // where the payload size is written
342 if (trailerSize < 2) {
343 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d bytes",
344 trailerSize*4));
345 AliWarning(Form("Invalid trailer size found (%d bytes) !",
346 trailerSize*4));
347 return kFALSE;
348 }
349
350 trailerSize -= 2;
351 fRCUTrailerSize = trailerSize*4;
352
353 for (; trailerSize > 0; trailerSize--) {
354 word = Get32bitWord(--index);
355 if ((word >> 30) != 2) {
356 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"missing 10");
357 AliWarning("Missing RCU trailer identifier pattern!");
358 continue;
359 }
360 Int_t parCode = (word >> 26) & 0xF;
361 Int_t parData = word & 0x3FFFFFF;
362 switch (parCode) {
363 case 1:
364 // ERR_REG1
365 fFECERRA = ((parData >> 13) & 0x1FFF) << 7;
366 fFECERRB = ((parData & 0x1FFF)) << 7;
367 break;
368 case 2:
369 // ERR_REG2
370 fERRREG2 = parData & 0x1FF;
371 break;
372 case 3:
373 // ERR_REG3
374 fERRREG3 = parData & 0xFFF;
375 break;
376 case 4:
377 // ERR_REG4
378 fERRREG4 = parData & 0xFFF;
379 break;
380 case 5:
381 // FEC_RO_A
382 fActiveFECsA = parData & 0xFFFF;
383 break;
384 case 6:
385 // FEC_RO_B
386 fActiveFECsB = parData & 0xFFFF;
387 break;
388 case 7:
389 // RDO_CFG1
390 fAltroCFG1 = parData & 0xFFFFF;
391 break;
392 case 8:
393 // RDO_CFG2
394 fAltroCFG2 = parData & 0x1FFFFFF;
395 break;
396 default:
397 fRawReader->AddMinorErrorLog(kRCUTrailerErr,"undef word");
398 AliWarning(Form("Undefined parameter code %d, ignore it !",
399 parCode));
400 break;
401 }
402 }
403
404 if (index < 1) {
405 fRawReader->AddMajorErrorLog(kRCUTrailerErr,Form("tr=%d raw=%d bytes",
406 fRCUTrailerSize+8,
407 fRawReader->GetDataSize()));
408 AliWarning(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
409 fRCUTrailerSize,
410 fRawReader->GetDataSize()));
411 }
412
413 fRCUTrailerData = fData + index*4;
414
415 // Now read the payload size
416 // (First word in the RCU trailer)
417 fPayloadSize = Get32bitWord(--index) & 0x3FFFFFF;
418
419 if ((fRawReader->GetDataSize() - fRCUTrailerSize - 8) != (fPayloadSize*4)) {
420 fRawReader->AddMajorErrorLog(kRCUTrailerSizeErr,Form("h=%d tr=%d rcu=%d bytes",
421 fRawReader->GetDataSize(),
422 fRCUTrailerSize+8,
423 fPayloadSize*4));
424 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 !",
425 fRawReader->GetDataSize(),
426 fRCUTrailerSize+8,
427 fPayloadSize*4));
428 }
429
430 return kTRUE;
431}
432
433//_____________________________________________________________________________
434Int_t AliAltroRawStreamV3::GetBranch() const
435{
436 // The method provides the RCU branch index (0 or 1)
437 // for the current hardware address.
438 // In case the hardware address has not been yet
439 // initialized, the method returns -1
440 if (fHWAddress == -1) return -1;
441
442 return ((fHWAddress >> 11) & 0x1);
443}
444
445//_____________________________________________________________________________
446Int_t AliAltroRawStreamV3::GetFEC() const
447{
448 // The method provides the front-end card index
449 // for the current hardware address.
450 // In case the hardware address has not been yet
451 // initialized, the method returns -1
452 if (fHWAddress == -1) return -1;
453
454 return ((fHWAddress >> 7) & 0xF);
455}
456
457//_____________________________________________________________________________
458Int_t AliAltroRawStreamV3::GetAltro() const
459{
460 // The method provides the altro chip index
461 // for the current hardware address.
462 // In case the hardware address has not been yet
463 // initialized, the method returns -1
464 if (fHWAddress == -1) return -1;
465
466 return ((fHWAddress >> 4) & 0x7);
467}
468
469//_____________________________________________________________________________
470Int_t AliAltroRawStreamV3::GetChannel() const
471{
472 // The method provides the channel index
473 // for the current hardware address.
474 // In case the hardware address has not been yet
475 // initialized, the method returns -1
476 if (fHWAddress == -1) return -1;
477
478 return (fHWAddress & 0xF);
479}
480
481//_____________________________________________________________________________
482Bool_t AliAltroRawStreamV3::GetRCUTrailerData(UChar_t*& data) const
483{
484 // Return a pointer to the RCU trailer
485 // data. Should be called always after
486 // the RCU trailer was already processed
487 // in the GetPosition() method
488 if (!fRCUTrailerSize || !fRCUTrailerData) {
489 AliError("No valid RCU trailer data is found !");
490 data = NULL;
491 return kFALSE;
492 }
493
494 data = fRCUTrailerData;
495
496 return kTRUE;
497}
498
499//_____________________________________________________________________________
500void AliAltroRawStreamV3::PrintRCUTrailer() const
501{
502 // Prints the contents of
503 // the RCU trailer data
504 printf("RCU trailer:\n===========\n");
505 printf("FECERRA: 0x%x\nFECERRB: 0x%x\n",fFECERRA,fFECERRB);
506 printf("ERRREG2: 0x%x\n",fERRREG2);
507 printf("#channels skipped due to address mismatch: %d\n",GetNChAddrMismatch());
508 printf("#channels skipped due to bad block length: %d\n",GetNChLengthMismatch());
509 printf("Active FECs (branch A): 0x%x\nActive FECs (branch B): 0x%x\n",fActiveFECsA,fActiveFECsB);
510 printf("Baseline corr: 0x%x\n",GetBaselineCorr());
511 printf("Number of presamples: %d\nNumber of postsamples: %d\n",GetNPresamples(),GetNPostsamples());
512 printf("Second baseline corr: %d\n",GetSecondBaselineCorr());
513 printf("GlitchFilter: %d\n",GetGlitchFilter());
514 printf("Number of non-ZS postsamples: %d\nNumber of non-ZS presamples: %d\n",GetNNonZSPostsamples(),GetNNonZSPresamples());
515 printf("Number of ALTRO buffers: %d\n",GetNAltroBuffers());
516 printf("Number of pretrigger samples: %d\n",GetNPretriggerSamples());
517 printf("Number of samples per channel: %d\n",GetNSamplesPerCh());
518 printf("Sparse readout: %d\n",GetSparseRO());
519 printf("Sampling time: %e s\n",GetTSample());
520 printf("L1 Phase: %e s\n",GetL1Phase());
521 printf("AltroCFG1: 0x%x\nAltroCFG2: 0x%x\n",GetAltroCFG1(),GetAltroCFG2());
522 printf("===========\n");
523}
524
525//_____________________________________________________________________________
526void AliAltroRawStreamV3::SelectRawData(Int_t detId)
527{
528 // Select the raw data for specific
529 // detector id
530 AliDebug(1,Form("Selecting raw data for detector %d",detId));
531 fRawReader->Select(detId);
532}
533
534//_____________________________________________________________________________
535void AliAltroRawStreamV3::SelectRawData(const char *detName)
536{
537 // Select the raw data for specific
538 // detector name
539 AliDebug(1,Form("Selecting raw data for detector %s",detName));
540 fRawReader->Select(detName);
541}
542
543//_____________________________________________________________________________
544Double_t AliAltroRawStreamV3::GetTSample() const
545{
546 // Returns the sampling time
547 // in seconds. In case the rcu trailer
548 // was note read, return an invalid number (0)
549
550 if (!fRCUTrailerData) return 0.;
551
552 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
553 UChar_t fq = (fAltroCFG2 >> 5) & 0xF;
554 Double_t tSample;
555 switch (fq) {
556 case 0:
557 // 20 MHz
558 tSample = 2.0*kLHCTimeSample;
559 break;
560 case 1:
561 // 10 Mhz
562 tSample = 4.0*kLHCTimeSample;
563 break;
564 case 2:
565 // 5 MHz
566 tSample = 8.0*kLHCTimeSample;
567 break;
568 default:
569 AliWarning(Form("Invalid sampling frequency value %d !",
570 fq));
571 tSample = 0.;
572 break;
573 }
574
575 return tSample;
576}
577
578//_____________________________________________________________________________
579Double_t AliAltroRawStreamV3::GetL1Phase() const
580{
581 // Returns the L1 phase w.r.t to the
582 // LHC clock
583 if (!fRCUTrailerData) return 0.;
584
585 const Double_t kLHCTimeSample = 25.0e-9; // LHC clocks runs at 40 MHz
586 Double_t phase = ((Double_t)(fAltroCFG2 & 0x1F))*kLHCTimeSample;
587
588 Double_t tSample = GetTSample();
589 if (phase >= tSample) {
590 AliWarning(Form("Invalid L1 trigger phase (%f >= %d) !",
591 phase,tSample));
592 phase = 0.;
593 }
594
595 return phase;
596}
597
598//_____________________________________________________________________________
599void AliAltroRawStreamV3::AddMappingErrorLog(const char *message)
600{
601 // Signal a mapping error
602 // The method can be used by the TPC,PHOS,EMCAL,FMD raw stream
603 // classes in order to log an error related to bad altro mapping
604
605 if (fRawReader) fRawReader->AddMinorErrorLog(kBadAltroMapping,message);
606}