]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDRawStreamV2.cxx
Bug fix in multiplicity limits.
[u/mrichter/AliRoot.git] / TRD / AliTRDRawStreamV2.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // This class provides access to TRD digits in raw data.                     //
21 //                                                                           //
22 // It loops over all TRD digits in the raw data given by the AliRawReader.   //
23 // The Next method goes to the next digit. If there are no digits left       //
24 // it returns kFALSE.                                                        //
25 // Several getters provide information about the current digit.              //
26 //                                                                           //
27 // Author: C. Lippmann (C.Lippmann@gsi.de)                                   //
28 //                                                                           //
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include "AliLog.h"
32 #include "AliRawReader.h"
33
34 #include "AliTRDRawStreamV2.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDcalibDB.h"
37 #include "AliTRDfeeParam.h"
38 #include "AliTRDdigitsManager.h"
39 #include "AliTRDarrayDictionary.h"
40 #include "AliTRDarrayADC.h"
41 #include "AliTRDSignalIndex.h"
42
43 ClassImp(AliTRDRawStreamV2)
44
45 UInt_t AliTRDRawStreamV2::fgDumpHead = 0;
46
47 //_____________________________________________________________________________
48 AliTRDRawStreamV2::AliTRDRawStreamV2() 
49   :AliTRDrawStreamBase()
50 //  :TObject()
51   ,fGeo(NULL) 
52   ,fSig()
53   ,fADC(0)
54   ,fTB(0)
55   ,fEv(0)
56   ,fROB(0)
57   ,fMCM(0)
58   ,fSM(0)
59   ,fLAYER(0)
60   ,fSTACK(0)
61   ,fROC(0)
62   ,fSIDE(0)
63   ,fDCS(0)
64   ,fROW(0)
65   ,fCOL(0)
66   ,fDET(0)
67   ,fLastDET(-1)
68   ,fBCctr(0)
69   ,fPTctr(0)
70   ,fPTphase(0)
71   ,fRVmajor(0)
72   ,fRVminor(0)
73   ,fHCHWords(0)
74   ,fTBins(0)
75   ,fTCon(0)
76   ,fPEDon(0)
77   ,fGAINon(0)
78   ,fXTon(0)
79   ,fNonLinOn(0)
80   ,fBypass(0)
81   ,fCommonAdditive(0)
82   ,fZeroSuppressed(0)
83   ,fHCHctr1(0)
84   ,fHCHctr2(0)
85   ,fMCMHctr1(0)
86   ,fMCMHctr2(0)
87   ,fGTUctr1(0)
88   ,fGTUctr2(0)
89   ,fHCdataCtr(0)
90   ,fTracklPID(0.)
91   ,fTracklDefL(0.)
92   ,fTracklPadPos(0.)
93   ,fTracklPadRow(0)
94   ,fGTUlinkMask()
95   ,fMCMWordCrt(0)
96   ,fMCMWordsExpected(0)
97   ,fRawReader(NULL)
98   ,fRawVersion(2)
99   ,fRawDigitThreshold(0)
100   ,fNextStatus(0)
101   ,fLastStatus(0)
102   ,fTbSwitch(0)
103   ,fTbSwitchCtr(0)
104   ,fTimeWords(0)
105   ,fWordCtr(0)
106   ,fRowMax(0)
107   ,fColMax(0)
108   ,fADCmask()
109   ,fLastADCmask(0)
110   ,fChamberDone()
111   ,fRetVal(0)
112   ,fEqID(0)
113   ,fDataSize(0)
114   ,fSizeOK(kFALSE)
115   ,fCountBytes(0)
116   ,fBufSize(0)
117   ,fkBufferSet(kFALSE)
118   ,fPos(NULL)
119   ,fpBegin(NULL)
120   ,fpEnd(NULL)
121   ,fDataWord(NULL)
122   ,fTimeBinsCalib(0)
123   ,fADClookup()
124   ,fNActiveADCs(0)
125   ,fEndOfDataFlag(kFALSE)
126 {
127   //
128   // Default constructor
129   //
130
131   for (Int_t i = 0; i < 540; i++) {
132     fChamberDone[i] = 0;
133   }
134
135 }
136
137 //_____________________________________________________________________________
138 AliTRDRawStreamV2::AliTRDRawStreamV2(AliRawReader *rawReader) 
139   :AliTRDrawStreamBase(rawReader)
140 //  :TObject()
141   ,fGeo(NULL) 
142   ,fADC(0)
143   ,fTB(0)
144   ,fEv(0)
145   ,fROB(0)
146   ,fMCM(0)
147   ,fSM(0)
148   ,fLAYER(0)
149   ,fSTACK(0)
150   ,fROC(0)
151   ,fSIDE(0)
152   ,fDCS(0)
153   ,fROW(0)
154   ,fCOL(0)
155   ,fDET(0)
156   ,fLastDET(-1)
157   ,fBCctr(0)
158   ,fPTctr(0)
159   ,fPTphase(0)
160   ,fRVmajor(0)
161   ,fRVminor(0)
162   ,fHCHWords(0)
163   ,fTBins(0)
164   ,fTCon(0)
165   ,fPEDon(0)
166   ,fGAINon(0)
167   ,fXTon(0)
168   ,fNonLinOn(0)
169   ,fBypass(0)
170   ,fCommonAdditive(0)
171   ,fZeroSuppressed(0)
172   ,fHCHctr1(0)
173   ,fHCHctr2(0)
174   ,fMCMHctr1(0)
175   ,fMCMHctr2(0)
176   ,fGTUctr1(0)
177   ,fGTUctr2(0)
178   ,fHCdataCtr(0)
179   ,fTracklPID(0.)
180   ,fTracklDefL(0.)
181   ,fTracklPadPos(0.)
182   ,fTracklPadRow(0)
183   ,fGTUlinkMask()
184   ,fMCMWordCrt(0)
185   ,fMCMWordsExpected(0)
186   ,fRawReader(rawReader)
187   ,fRawVersion(2)
188   ,fRawDigitThreshold(0)
189   ,fNextStatus(0)
190   ,fLastStatus(0)
191   ,fTbSwitch(0)
192   ,fTbSwitchCtr(0)
193   ,fTimeWords(0)
194   ,fWordCtr(0)
195   ,fRowMax(0)
196   ,fColMax(0)
197   ,fADCmask()
198   ,fLastADCmask(0)
199   ,fChamberDone()
200   ,fRetVal(0)
201   ,fEqID(0)
202   ,fDataSize(0)
203   ,fSizeOK(kFALSE)
204   ,fCountBytes(0)
205   ,fBufSize(0)
206   ,fkBufferSet(kFALSE)
207   ,fPos(NULL)
208   ,fpBegin(NULL)
209   ,fpEnd(NULL)
210   ,fDataWord(NULL)
211   ,fTimeBinsCalib(0)
212   ,fADClookup()
213   ,fNActiveADCs(0)
214   ,fEndOfDataFlag(kFALSE)
215 {
216   //
217   // Create an object to read TRD raw digits
218   //
219
220   fRawReader->Select("TRD");
221
222   for (Int_t i = 0; i < 540; i++) {
223     fChamberDone[i] = 0;
224   }
225
226 }
227
228 //_____________________________________________________________________________
229 AliTRDRawStreamV2::AliTRDRawStreamV2(const AliTRDRawStreamV2& stream)
230   :AliTRDrawStreamBase(stream)
231 //  :TObject()
232   ,fGeo(NULL)
233   ,fSig()
234   ,fADC(-1)
235   ,fTB(-1)
236   ,fEv(-1)
237   ,fROB(-1)
238   ,fMCM(-1)
239   ,fSM(-1)
240   ,fLAYER(-1)
241   ,fSTACK(-1)
242   ,fROC(-1)
243   ,fSIDE(-1)
244   ,fDCS(-1)
245   ,fROW(-1)
246   ,fCOL(-1)
247   ,fDET(0)
248   ,fLastDET(-1)
249   ,fBCctr(-1)
250   ,fPTctr(-1)
251   ,fPTphase(-1)
252   ,fRVmajor(-1)
253   ,fRVminor(-1)
254   ,fHCHWords(-1)
255   ,fTBins(-1)
256   ,fTCon(0)
257   ,fPEDon(0)
258   ,fGAINon(0)
259   ,fXTon(0)
260   ,fNonLinOn(-1)
261   ,fBypass(-1)
262   ,fCommonAdditive(-1)
263   ,fZeroSuppressed(0)
264   ,fHCHctr1(-1)
265   ,fHCHctr2(-1)
266   ,fMCMHctr1(-1)
267   ,fMCMHctr2(-1)
268   ,fGTUctr1(-1)
269   ,fGTUctr2(-1)
270   ,fHCdataCtr(-1)
271   ,fTracklPID(-1.)
272   ,fTracklDefL(-1.)
273   ,fTracklPadPos(-1.)
274   ,fTracklPadRow(-1)
275   ,fGTUlinkMask()
276   ,fMCMWordCrt(0)
277   ,fMCMWordsExpected(0)
278   ,fRawReader(NULL)
279   ,fRawVersion(-1)
280   ,fRawDigitThreshold(0)
281   ,fNextStatus(0)
282   ,fLastStatus(0)
283   ,fTbSwitch(0)
284   ,fTbSwitchCtr(0)
285   ,fTimeWords(0)
286   ,fWordCtr(0)
287   ,fRowMax(-1)
288   ,fColMax(-1)
289   ,fADCmask()
290   ,fLastADCmask(0)
291   ,fChamberDone()
292   ,fRetVal(0)
293   ,fEqID(0)
294   ,fDataSize(0)
295   ,fSizeOK(kFALSE)
296   ,fCountBytes(0)
297   ,fBufSize(0)
298   ,fkBufferSet(kFALSE)
299   ,fPos(NULL)
300   ,fpBegin(NULL)
301   ,fpEnd(NULL)
302   ,fDataWord(NULL)
303   ,fTimeBinsCalib(0)
304   ,fADClookup()
305   ,fNActiveADCs(0)
306   ,fEndOfDataFlag(kFALSE)
307 {
308   //
309   // Copy constructor
310   //
311
312   AliFatal("Copy constructor not implemented");
313
314 }
315
316 //_____________________________________________________________________________
317 AliTRDRawStreamV2& AliTRDRawStreamV2::operator = (const AliTRDRawStreamV2& 
318                                               /* stream */)
319 {
320   //
321   // Assigment operator
322   //
323
324   Fatal("operator =", "assignment operator not implemented");
325   return *this;
326
327 }
328
329 //_____________________________________________________________________________
330 AliTRDRawStreamV2::~AliTRDRawStreamV2()
331 {
332   //
333   // Destructor
334   //
335
336   if (fGeo) {  
337     delete fGeo;
338   }
339
340 }
341
342 //_____________________________________________________________________________
343 void AliTRDRawStreamV2::SetRawReader(AliRawReader *rawReader) 
344 {
345   //
346   // Set the rawreader
347   //
348
349   if (rawReader)
350     {
351       fRawReader = rawReader;
352     }
353 }
354
355 //_____________________________________________________________________________
356 Bool_t AliTRDRawStreamV2::SetRawVersion(Int_t rv)
357 {
358   //
359   // Set the raw data version
360   //
361
362   if ( rv >= 0 && rv <= 3 ) {
363     fRawVersion = rv;
364     return kTRUE;
365   }
366
367   return kFALSE;
368
369 }
370
371 //____________________________________________________________________________
372 Bool_t AliTRDRawStreamV2::Init()
373 {
374   //
375   // Initialization
376   //
377
378   if (!AliTRDcalibDB::Instance()) {
379     AliError("Could not get calibration object");
380     return kFALSE;
381   }
382
383   if (!fGeo) {
384     fGeo = new AliTRDgeometry();
385   }
386   
387   fTimeBinsCalib = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
388   //AliDebug(2, Form("Number of Timebins read from CDB: %d", fTimeBinsCalib));
389
390   // The number of data words needed for this number of time bins (there
391   // are 3 time bins in one word)
392   fTimeWords = (fTimeBinsCalib-1)/3 + 1;
393
394   fTbSwitch    = 3;
395   fTbSwitchCtr = 0;
396
397   fHCHctr1 = fHCHctr2 =  0;
398   fGTUctr1 = fGTUctr2 = -1;
399
400   fHCdataCtr = 0;
401   fWordCtr   = 0;  
402
403   fDET     = 0;
404   fLastDET     = -1;
405   fRetVal = 0;
406   fEqID     = 0;
407   fDataSize = 0;
408   fSizeOK = kFALSE;
409   
410   fLastStatus = kStart;
411   fNextStatus = kStart;
412
413   fCountBytes = 0;
414   fBufSize = 0;
415   fDataWord = NULL;
416   fPos = NULL;
417   fpBegin = NULL;
418   fpEnd = NULL;
419   fWordCtr = 0;
420   fkBufferSet = kFALSE;
421
422   fMCMWordCrt = 0;
423   fMCMWordsExpected = 0;
424
425   fEndOfDataFlag = kFALSE;
426   // set all ADC active
427   // should be 1111 1111 1111 1111 1111 1 = 21 bits active (0-20)
428   fNActiveADCs = ChannelsToRead(0x1fffff); 
429
430   fLastADCmask = 0;
431
432   return kTRUE;
433 }
434
435 //____________________________________________________________________________
436 void AliTRDRawStreamV2::SwapOnEndian()
437 {
438   //
439   // Check the endian and swap if needed
440   //
441
442   int itemp = 1;
443   char* ptemp = (char*) &itemp;
444   fpBegin = (UInt_t*)fPos;
445   fpEnd = fpBegin + (fBufSize/sizeof(UInt_t));
446   if (ptemp[0] != 1)
447     {
448       // need to swap...
449       // assume we are at the begining of the buffer!
450       //AliDebug(5, "Swapping.");
451       //UInt_t *pbegin = (UInt_t*)fPos;
452       UInt_t iutmp = 0;
453       for (UInt_t i = 0; i < fBufSize / fgkSizeWord; i++)
454         {
455           fDataWord = fpBegin + i;
456           iutmp = (((*fDataWord & 0x000000ffU) << 24) | ((*fDataWord & 0x0000ff00U) <<  8) |
457                    ((*fDataWord & 0x00ff0000U) >>  8) | ((*fDataWord & 0xff000000U) >> 24));
458           // here we override the value in the buffer!
459           *fDataWord = iutmp;
460         }
461       fDataWord = fpBegin;
462     }
463
464   // dump words - debugging perpose
465   if (fgDumpHead > 0){
466     AliInfo(Form("---------- Dumping %u words from the beginnig of the buffer ----------",fgDumpHead));
467     if (DumpWords(fpBegin, fgDumpHead) == kFALSE){AliError("Dump failed. Not enough data.");}
468     AliInfo(Form("---------- Dumping ended ----------------------------------------------"));
469   }
470   
471 }
472 //____________________________________________________________________________
473 Int_t AliTRDRawStreamV2::NextData()
474 {
475   //
476   // Updates the next data word pointer
477   //
478
479   if (fCountBytes + fgkSizeWord >= fBufSize)
480     {
481       fkBufferSet = fRawReader->ReadNextData(fPos);
482       if (fkBufferSet == kTRUE)
483         {
484           fBufSize = fRawReader->GetDataSize();
485           fCountBytes = 0;        
486           fDataWord = (UInt_t*)fPos;
487           SwapOnEndian();
488           ChangeStatus(kNextSM);
489           fWordCtr = 0;
490           return kNextSM;
491         }
492       else
493         {
494           ChangeStatus(kStop);
495           return kNoMoreData;
496         }
497     }
498   else
499     {
500       fPos += fgkSizeWord;
501       fCountBytes += fgkSizeWord;         
502       fDataWord = (UInt_t*)fPos;
503       fWordCtr++;
504       return kWordOK;
505     }
506 }
507
508 //============================================================================
509 // Decoding functions
510 //============================================================================
511
512 //____________________________________________________________________________
513 void AliTRDRawStreamV2::DecodeHCheader(Int_t timeBins)
514 {
515   //
516   // Decode the HC header (fRawVersion == 2, 3, 4, ???)
517   //
518
519   fRVmajor = (*fDataWord >> 24) & 0x7f;
520   fRVminor = (*fDataWord >> 17) & 0x7f;
521
522   if (fRVmajor < 2 || fRVmajor > 4)
523     AliError(Form(" Unsupported raw version: %d", fRawVersion))
524   
525   if ( fRawVersion != fRVmajor ) {
526     
527     AliWarning("===============================================================================");
528     AliWarning(Form("Mismatch between fRawVersion (%d) and fRVmajor from HC header (%d)"
529                     ,fRawVersion,fRVmajor));
530     AliWarning(Form("Setting fRawVersion to %d", fRVmajor));
531     AliWarning("===============================================================================");
532     fRawVersion = fRVmajor;
533
534   }
535
536   //
537   // check for zero suppression
538   if ( fRawVersion >= 3 || fRawVersion <= 4 ) fZeroSuppressed = kTRUE;
539   else                                        fZeroSuppressed = kFALSE;
540   
541   // 1st word (h[0])
542   if ( (*fDataWord & 0x3) == 1 ) {
543
544     fHCHWords = (*fDataWord >> 14) & 0x7;
545     fSM       = (*fDataWord >>  9) & 0x1f;
546     fLAYER    = (*fDataWord >>  6) & 0x7;
547     fSTACK    = (*fDataWord >>  3) & 0x7;
548     fSIDE     = (*fDataWord >>  2) & 0x1;
549
550     fROC      = fGeo->GetDetectorSec(fLAYER, fSTACK);
551
552     //AliDebug(3, Form("0x%08x: HC header: sm=%d; roc=%d; side=%x", *fDataWord, fSM, fROC, fSIDE+10));
553     //AliDebug(5, Form("0x%08x: HC header: expecting %d HC words", *fDataWord, fHCHWords));
554
555     if ((fSM    <  0) || 
556         (fSM    > 17) || 
557         (fLAYER <  0) || 
558         (fLAYER >  5) || 
559         (fSTACK <  0) || 
560         (fSTACK >  4) || 
561         (fSIDE  <  0) || 
562         (fSIDE  >  1)) 
563       {
564         AliWarning(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
565                         *fDataWord, fDCS, fSM, fLAYER, fSTACK));
566         fRawReader->AddMajorErrorLog(kHCHeaderCorrupt,Form("0x%08x:dcs=%d; sm=%d; layer=%d; stack=%d.",
567                                                            *fDataWord, fDCS, fSM, fLAYER, fSTACK));
568       } 
569     else 
570       {
571         fHCHctr1++;
572         fHCHctr2++;
573       }
574   } 
575   else 
576     { 
577       AliWarning(Form("0x%08x: No HC header when it was expected.", *fDataWord)); 
578       fRawReader->AddMajorErrorLog(kHCHeaderMissing,Form("0x%08x", *fDataWord));
579     }
580
581   // 2nd word (h[1])
582   if ( fHCHWords >= 1 ) 
583     {
584       // read one more word
585       if (NextData() != kWordOK)
586         {
587           AliWarning("Next HC word missing");
588           fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC word missing"); 
589           fNextStatus = kNextHC;
590           return;
591         }
592
593       if ( (*fDataWord & 0x3) == 1 ) 
594         {
595           
596           fBCctr   =  (*fDataWord >> 16);
597           fPTctr   =  (*fDataWord >> 12) & 0xf;
598           fPTphase =  (*fDataWord >>  8) & 0xf;
599           fTBins   = ((*fDataWord >>  2) & 0x3f) + 1;
600           fTimeWords = (fTBins - 1)/3 + 1;      
601           
602 //        AliDebug(3, Form("0x%08x: HC header 2: BCctr=%d PTctr=%d PTph=%d TB=%d"
603 //                         , *fDataWord, fBCctr, fPTctr, fPTphase, fTBins));
604
605           if( fTBins != timeBins ) 
606             {         
607               AliWarning("===============================================================================");
608               AliError(Form("Mismatch between nNTB from CDB (%d) and from HC header (%d)"
609                             , timeBins, fTBins));
610               AliWarning(Form("We will use the value from the raw data (HC header): %d", fTBins));
611               AliWarning("===============================================================================");          
612
613               fTimeWords = (fTBins - 1)/3 + 1;  
614             }
615         }      
616     }
617
618   // 3nd word (h[2])
619   if ( fHCHWords >= 2 ) {
620     // read one more word
621     if (NextData() != kWordOK)
622       {
623         AliWarning("Next HC word missing");
624         fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC word missing"); 
625         fNextStatus = kNextHC;
626         return;
627       }
628     if ( (*fDataWord & 0x3) == 1 ) {
629        
630       fTCon     = (*fDataWord >> 29) & 0x1;
631       fPEDon    = (*fDataWord >> 31) & 0x1;
632       fGAINon   = (*fDataWord >> 30) & 0x1;
633       fXTon     = (*fDataWord >> 28) & 0x1;
634       fNonLinOn = (*fDataWord >> 27) & 0x1;
635       fBypass   = (*fDataWord >> 26) & 0x1;
636
637       fCommonAdditive = (*fDataWord >> 20) & 0x3f;
638
639 //       AliDebug(3, Form("0x%08x: HC header 3: TC=%d, PED=%d, GAIN=%d, XT=%d, NonLin=%d, Bypass=%d, Add=%d"
640 //                    , fTCon, fPEDon, fGAINon, fXTon, fNonLinOn, fBypass, fCommonAdditive));
641     }
642   }
643
644 }  
645
646 //____________________________________________________________________________
647 Int_t AliTRDRawStreamV2::ChannelsToRead(Int_t ADCmask)
648 {
649   //
650   // Return the channels to read
651   //
652
653   memset(fADClookup, -1, 32 * sizeof(Int_t));
654   fADClookup[0] = 0; // count entries
655   fADClookup[1] = 2; // index - data start at 2
656   UInt_t mask = 0;
657   for (Int_t i = 0; i < 30; i++)
658     {
659       mask = 1 << i;
660       if ((ADCmask & mask))
661         {
662           //AliDebug(9, Form("fDataWord=0x%08x mask=0x%08x i=%d", *fDataWord, mask, i));
663           fADClookup[fADClookup[1]] = i;
664           ++fADClookup[0];
665           ++fADClookup[1];
666         }
667     }
668
669   // test the iteration - comment out for production
670   // begin of comment out section
671   char schannels[512];
672   sprintf(schannels, "ADC Channels to read: ");
673   fADClookup[1] = 2;
674   while(fADClookup[1] - 2 < fADClookup[0])
675     {
676       //AliDebug(9, Form("max=%d index=%d adc=%d", fADClookup[0], fADClookup[1], fADClookup[fADClookup[1]]));
677       strcat(schannels, Form("%d ", fADClookup[fADClookup[1]]));
678       fADClookup[1]++;
679     }
680   //AliDebug(9, Form("%s", schannels));
681   //AliDebug(9, Form("ADC channels = %d", fADClookup[0]));
682   // end of comment out section
683
684   fADClookup[1] = 2;
685   return fADClookup[0];
686 }
687
688 //____________________________________________________________________________
689 void AliTRDRawStreamV2::DecodeTracklet()
690 {
691   //
692   // Decode the Tracklet
693   //
694   // this function is not tested yet on real tracklets
695   //
696
697   if ( fRawVersion < 1 || fRawVersion > 3 ) 
698     {
699       AliError(Form(" Unsupported raw version: %d", fRawVersion));      
700     }
701
702   fTracklPID    = (*fDataWord >> 24) & 0xff;
703   fTracklPadRow = (*fDataWord >> 20) & 0xf;    // 0:15
704   fTracklDefL   = (*fDataWord >> 13) & 0x7f;
705   fTracklPadPos = (*fDataWord)       & 0x1fff;
706
707   fTracklPID    /= (Float_t)((1<<8) - 1);                      // 0:1 (steps of 0.39%)
708   fTracklDefL    = (fTracklDefL  - ((1<< 7)-1)/2.) * 140.e-4;  // -0.889:0.889cm 
709   fTracklPadPos  = (fTracklPadPos - ((1<<13)-1)/2.) * 160.e-4; // -65.528:65.528 cm
710
711   //AliDebug(4, Form("0x%08x: Tracklet found: SM%d L%dS%d side %x: PadRow=%d PadPos=%f DefL=%f PID=%f"
712   //              , *fDataWord, fSM, fLAYER, fSTACK, fSIDE+10
713   //                , fTracklPadRow, fTracklPadPos, fTracklDefL, fTracklPID));
714
715   if( ((fSTACK == 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC0())) ||
716       ((fSTACK != 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC1())) ) {
717     AliWarning(Form("Strange Row read from Tracklet Word: %d", fTracklPadRow));
718     fRawReader->AddMajorErrorLog(kTrackletRowMismatch,Form("Word: %d", fTracklPadRow));
719   }
720
721 }
722
723 //____________________________________________________________________________
724 void AliTRDRawStreamV2::DecodeMCMheader()
725 {
726   //
727   // Decode the MCM header
728   //
729
730   if ( fRawVersion < 1 || fRawVersion > 3 ) 
731     {
732       AliError(Form(" Unsupported raw version: %d", fRawVersion));      
733     }
734
735   fMCM  = (*fDataWord & 0xff000000) >> 24;
736   fEv   = (*fDataWord & 0x00fffff0) >> 4;
737
738   fROB  = fMCM / 16;
739   fMCM  = fMCM % 16;
740
741   fROW  = AliTRDfeeParam::Instance()->GetPadRowFromMCM(fROB, fMCM);
742
743 //   AliDebug(4, Form("0x%08x: SM%d L%dS%d. MCM Header: fROB=%d fMCM=%02d fEv=%02d"
744 //                , *fDataWord, fSM, fLAYER, fSTACK, fROB, fMCM, fEv));
745
746   if ( fROB % 2 == 0 && fSIDE == 1 ) {
747     AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
748                    , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
749     fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
750                                                        , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
751   }
752   if ( fROB % 2 != 0 && fSIDE == 0 ) {
753     AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
754                    , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
755     fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
756                                                        , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
757   }
758   if ( (fSTACK == 2 && fROW >= fGeo->RowmaxC0()) ||
759        (fSTACK != 2 && fROW >= fGeo->RowmaxC1()) || fROW < 0 ) {
760     AliWarning(Form("SM%d L%dS%d: Wrong Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
761                    , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
762     fRawReader->AddMajorErrorLog(kWrongPadrow,Form("SM%d L%dS%d: Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
763                                                   , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
764   }
765   
766   fMCMHctr1++;
767   fMCMHctr2++;
768
769   fMCMWordCrt = 1; // MCM header
770
771   // AdcMask for Zero supressed data
772   if ( fRawVersion == 3 ) 
773     {
774       // read one more word
775       if (NextData() != kWordOK)
776         {
777           AliWarning("MCM ADC mask missing");
778           fRawReader->AddMajorErrorLog(kMCMADCMaskMissing,"Missing"); 
779           fNextStatus = kNextHC;
780           return;
781         }
782       else
783         {
784           ++fMCMWordCrt;
785
786           for ( Int_t ctr = 0; ctr < fGeo->ADCmax(); ctr++ ) {
787             if ( (*fDataWord >> (11+ctr)) == 0x1 ) fADCmask[ctr] = kTRUE;
788             else                                  fADCmask[ctr] = kFALSE;
789           }
790           
791           //AliDebug(4, Form("0x%08x: ADC mask", *fDataWord));
792           fNActiveADCs = ChannelsToRead(*fDataWord);      
793         }
794     }
795
796   if (fRawVersion <= 2)
797     {
798       // no zero suppression
799       // raw version 2:
800       // 1 MCM header + 21 * ( Ntimebin/3)
801       // If NTimebin = 30, it is 211 words. 
802       //fMCMWordsExpected = 1 + 21 * (fTBins / 3);
803       //fNActiveADCs = 21;
804       fNActiveADCs = ChannelsToRead(0x1fffff); // should be 1111 1111 1111 1111 1111 1 = 21 bits active (0-20)
805       //fMCMWordsExpected = 1 + fNActiveADCs * ((fTBins-1) / 3. + 1.);      
806       //directly get it like that:
807       fMCMWordsExpected = 1 + fNActiveADCs * fTBins / 3;      
808     }
809
810   if (fRawVersion >= 3)
811     {
812       // raw version 3:
813       // 1 MCM header + 1 ADC mask + NofActiveADCs * ( Ntimebin/3 )
814       //directly get it like that:
815       fMCMWordsExpected = 1 + 1 + (fTBins * fNActiveADCs) / 3;
816     }
817   
818   //AliDebug(5, Form("We expect %d MCM words. We read %d so far.", fMCMWordsExpected, fMCMWordCrt));
819 }
820 //____________________________________________________________________________
821 Bool_t AliTRDRawStreamV2::DecodeNextRawWord()
822 {
823   //
824   // Decode the next raw data word
825   //
826
827   //AliDebug(8, Form("-----------------------------------------"));
828   //AliDebug(8, Form("DATA IS 0x%x", *fDataWord));
829
830   if (fADClookup[1] - 2 > fADClookup[0])
831     {
832 //       AliDebug(8, Form("Overflow Index ADC = %d Max Index = %d Value = %d. Done with ADCs in this MCM. Is this already MCM header 0x%x?", 
833 //                     fADClookup[1] - 2, fADClookup[0], fADClookup[fADClookup[1]], *fDataWord));
834       fTbSwitchCtr = 0;
835       fMCMWordsExpected = 0;
836       AliWarning("Trying to recover. Fall back to DecodeMCM.");
837       DecodeMCM();
838       //ChangeStatus(kNextMCM);
839       return kFALSE;
840     }
841
842   if ( (*fDataWord & 0x00000003) != 0x2 && (*fDataWord & 0x00000003) != 0x3) {
843     AliWarning(Form("Data %08x : Data Word ends neither with b11 nor b10", (Int_t)*fDataWord));
844     fRawReader->AddMinorErrorLog(kDataMaskError,Form("Data %08x", (Int_t)*fDataWord));
845     fMCMWordsExpected = 0;
846     AliWarning("Trying to recover. Fall back to DecodeMCM.");
847     DecodeMCM();
848     //ChangeStatus(kNextMCM);
849     return kFALSE;
850   }
851
852   if ( (*fDataWord & 0x3) != fLastADCmask || fTbSwitchCtr > fTimeWords) 
853     {    
854       fADC = fADClookup[fADClookup[1]];
855 //       AliDebug(8, Form("Next fADC = %d at index = %d MCM Word Number: %d Max MCM Words is %d", 
856 //                     fADC, fADClookup[1] - 2, fMCMWordCrt, fMCMWordsExpected));
857       ++fADClookup[1];
858       fTB = 0;    
859       fTbSwitchCtr = 0;
860       fLastStatus = kNextData;
861       fLastADCmask = (*fDataWord) & 0x3;
862     }
863
864   ++fTbSwitchCtr;
865
866   //decode data here
867   Bool_t kIsDataOK = kFALSE;
868
869   // We have only 21 ADC channels.
870   if ( fADC > (Int_t)fGeo->ADCmax() - 1 ) 
871     {
872       AliWarning(Form("Data %08x : Data is strange. fADC is already %d", (Int_t)*fDataWord, (Int_t)fADC));
873       fRawReader->AddMinorErrorLog(kADCChannelOverflow,Form("Data %08x : fADC=%d", (Int_t)*fDataWord, (Int_t)fADC));
874     }
875   else
876     {
877       // There are 18 pads connected to each MCM ADC channels 2...19. The other channels cross to other
878       // MCMs and are good for online tracking in the MCM.
879       if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax() - 1 ) 
880         {
881           
882           // Get Pad column
883           // fCOL = fFee->GetPadColFromADC(fROB, fMCM, fADC);
884           fCOL = AliTRDfeeParam::Instance()->GetPadColFromADC(fROB, fMCM, fADC);
885           
886           // We have only 144 Pad Columns
887           //if ( fCOL > fColMax-1 || fCOL < 0 ) 
888           if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) 
889             {
890               // Decode 32 bit data words with information from 3 time bins and copy the data
891               fSig[0] = (*fDataWord & 0x00000ffc) >> 2;
892               fSig[1] = (*fDataWord & 0x003ff000) >> 12;
893               fSig[2] = (*fDataWord & 0xffc00000) >> 22;
894               
895               // Print data to screen:
896               //AliDebug(5, Form("DATA : 0x%x", *fDataWord));
897               //          AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
898               //                           fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));            
899               kIsDataOK = kTRUE;
900             }
901           else
902             {
903               AliWarning(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
904                               fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
905               fRawReader->AddMajorErrorLog(kWrongPadcolumn,Form("SM%d L%dS%d: column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
906                                                                 fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
907               kIsDataOK = kFALSE;
908             }
909         }
910       else 
911         {      
912           //AliDebug(5, Form("fADC not accepted %d - DATA : 0x%x", fADC, *fDataWord));
913           fCOL = -1;
914           kIsDataOK = kFALSE;
915         }
916     }// if fADC is ok
917
918   ++fMCMWordCrt;
919   //AliDebug(5, Form("We expect %d MCM words. We read %d so far.", fMCMWordsExpected, fMCMWordCrt));
920
921   // all mcm data processed go to next one
922   if ( fMCMWordCrt >= fMCMWordsExpected)
923     {
924       ChangeStatus(kNextMCM);
925     }
926
927   return kIsDataOK;
928 }
929
930 //____________________________________________________________________________
931 Bool_t AliTRDRawStreamV2::DecodeMCM()
932 {
933   //
934   // Decode a single MCM
935   //
936
937   if( ((*fDataWord & 0x80000000) == 0x0) && ((*fDataWord & 0x0000000f) == 0xC) )
938     { // MCM Header
939       DecodeMCMheader();
940       if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 ) 
941         {
942           AliWarning("Wrong fMCM or fROB. Skip this data");
943           fRawReader->AddMajorErrorLog(kWrongMCMorROB,Form("MCM=%d, ROB=%d",fMCM,fROB));
944           ChangeStatus(kNextHC);
945           return kFALSE;
946         }
947
948       fTbSwitch    = 3;  // For first adc channel we expect: (*fDataWord & 3) = 3
949       fTbSwitchCtr = 0;  // 
950       fADC = fTB   = 0;  // Reset Counter
951       fLastADCmask = 0; // Reset
952
953       if (fMCMWordCrt < fMCMWordsExpected)
954         {
955           ChangeStatus(kNextData);
956         }
957       else
958         {
959           ChangeStatus(kNextMCM);
960         }
961       return kTRUE;
962     }
963
964   if ( *fDataWord == fgkEndofrawdatamarker ) 
965     {  // End of half-chamber data, finished
966       fGTUctr1 = -1;
967       ChangeStatus(kNextHC);
968       fEndOfDataFlag = kTRUE;
969       //AliDebug(5, "Expecting MCM header but got End-Of-Raw-Data Marker");
970       if (fMCMWordsExpected == 0 || fMCMWordsExpected == fMCMWordCrt)
971         return kTRUE;
972       else
973         {
974           //AliDebug(5, Form("MCM words missing? %d [expected=%d got=%d] ", fMCMWordsExpected - fMCMWordCrt, fMCMWordsExpected, fMCMWordCrt));    
975           //AliWarning(Form("MCM words missing? %d [expected=%d got=%d] ", fMCMWordsExpected - fMCMWordCrt, fMCMWordsExpected, fMCMWordCrt));     
976           return kFALSE;
977         }
978     }
979
980   //AliDebug(3, Form("Expecting MCM header but got 0x%x. Going to Next MCM header.", *fDataWord));
981   AliWarning(Form("Expecting MCM header but got 0x%x. Fall back: Next MCM header.", *fDataWord));
982   ChangeStatus(kNextMCM);      
983
984   return kFALSE;
985 }
986
987 //____________________________________________________________________________
988 Bool_t AliTRDRawStreamV2::DecodeHC()
989 {
990   //
991   // Decode a half chamber
992   //
993
994   if ( fNextStatus == kNextHC )
995     {
996       //AliDebug(5, "kNextHC");
997       //
998       // 1) Find end_of_tracklet_marker
999       //
1000       // GTU Link Mask?
1001       if (DecodeGTUlinkMask())
1002         {
1003           return kTRUE;
1004         }
1005       
1006       // endoftrackletmarker?
1007       if ( *fDataWord == fgkEndoftrackletmarker ) 
1008         {
1009           //AliDebug(3, "End-of-tracklet-marker found");
1010           //AliDebug(5, Form("Data 0x%x", *fDataWord));
1011           ChangeStatus(kSeekNonEoTracklet);
1012           return kTRUE;
1013         } 
1014       else 
1015         {
1016           // Tracklets found
1017           //AliDebug(3, "Tracklet found");
1018           //AliDebug(5, Form("Tracklet data 0x%x", *fDataWord));
1019           DecodeTracklet();
1020           return kTRUE;
1021         }
1022     } // if next HC
1023
1024   if (fNextStatus == kSeekNonEoTracklet)
1025     {
1026       //AliDebug(5, "kSeekNonEoTracklet");
1027
1028       //
1029       // 2) Look for non-end_of_tracklet_marker
1030       //
1031       //printf("Word %d: 0x%08x\n", fWordCtr, *fDataWord); 
1032       
1033       if ( *fDataWord != fgkEndoftrackletmarker ) 
1034         {
1035           ChangeStatus(kDecodeHC);
1036           //AliDebug(3, "NON end-of-tracklet-marker found");
1037           //AliDebug(5, Form("Data 0x%x", *fDataWord));
1038           //// no do not continue - this should be the hcheader
1039         }
1040       else
1041         {
1042           //just go on and find the non-end_of_tracklet_marker
1043           return kTRUE;
1044         }
1045     }
1046
1047   if ( fNextStatus == kDecodeHC )
1048     {
1049       //AliDebug(5, "kDecodeHC");
1050       
1051       //
1052       // 3) This Word must be Half Chamber Header
1053       //
1054       if ( (*fDataWord & 0x00000003) == 1 ) 
1055         { // HC header
1056           DecodeHCheader(fTimeBinsCalib); // This is the new header!
1057           fLastDET = fDET;
1058           fDET    = fGeo->GetDetector(fLAYER, fSTACK, fSM);
1059           fRowMax = fGeo->GetRowMax(fLAYER,fSTACK,fSM);
1060           fColMax = fGeo->GetColMax(fROC);
1061           
1062           fMCMHctr2 = 0;
1063           fHCdataCtr = 0;
1064           fChamberDone[fDET]++;
1065           //AliDebug(6, Form("--------------      DET %d fChamberDone[fDET]=%d", fDET, fChamberDone[fDET]));
1066
1067           ChangeStatus(kNextMCM);
1068           return kTRUE;
1069         } //HC header
1070       else
1071         {
1072           AliWarning(Form("Expecting HC header mask but got 0x%x. Fall back: Next HC.", *fDataWord));
1073           ChangeStatus(kNextHC);
1074           // before we went to //ChangeStatus(kNextSM);
1075         }
1076     } // if decode HC
1077   
1078   return kFALSE;
1079 }
1080
1081 //____________________________________________________________________________
1082 Bool_t AliTRDRawStreamV2::DecodeGTUlinkMask()
1083 {
1084   //
1085   // Decode the link masks sent by the GTU. These marke the active optical links
1086   // between GTU and Super Module. Up to now only fully active links are found
1087   // (0xfff = 12 active links).
1088   //
1089
1090   if ( (*fDataWord & 0xfffff000) ==  0xe0000000 )
1091     {
1092       if ( fRawVersion < 1 || fRawVersion > 3 ) 
1093         {
1094           AliError(Form(" Unsupported raw version: %d", fRawVersion));      
1095         }
1096       
1097       if ( fGTUctr1 == -1 ) fGTUctr2++;
1098       fGTUctr1++;
1099
1100       if ( (fGTUctr1 >= 0) && (fGTUctr1 < 5) && (fGTUctr2 >= 0) && (fGTUctr2 < 18) ) 
1101         {
1102           fGTUlinkMask[fGTUctr2][fGTUctr1] = (*fDataWord & 0xfff);
1103         }
1104      
1105       //AliDebug(5, Form("GTU link mask 0x%x decoded 0x%x", *fDataWord, fGTUlinkMask[fGTUctr2][fGTUctr1]));
1106       return kTRUE;
1107     }
1108
1109   return kFALSE;
1110 }
1111
1112 //____________________________________________________________________________
1113 void AliTRDRawStreamV2::ChangeStatus(Int_t kstat)
1114 {
1115   //
1116   // Change the status
1117   //
1118
1119   fLastStatus = fNextStatus;
1120   fNextStatus = kstat;  
1121 }
1122
1123 //____________________________________________________________________________
1124 Bool_t AliTRDRawStreamV2::DecodeSM()
1125 {
1126   //
1127   // Decode a supermodule
1128   //
1129
1130   fDET     = 0;
1131   fRetVal = 0;
1132   fEqID     = 0;
1133   fDataSize = 0;
1134   fSizeOK = kFALSE;
1135   
1136   // After reading the first word check for size of this data and get Eq. ID
1137   if ( fWordCtr == 1 ) 
1138     {
1139       fDataSize = fRawReader->GetDataSize()/4;  // Size of this payload in 32bit words
1140       fEqID     = fRawReader->GetEquipmentId(); // Get Equipment ID
1141       if ( fDataSize > 0 ) fSizeOK = kTRUE;
1142       //AliDebug(3, Form("fDataSize=%d fEqID=%d", fDataSize, fEqID));
1143     }
1144   
1145   // GTU Link Mask?
1146   if ( DecodeGTUlinkMask() ) 
1147     {
1148       ChangeStatus(kNextHC);
1149       return kTRUE;
1150     } 
1151   else 
1152     {
1153       AliWarning(Form("Equipment %d: First data word is not GTU Link Mask! Fall back: None. Stop.", fEqID));
1154       fRawReader->AddMajorErrorLog(kGTULinkMaskMissing,Form("Equipment %d",fEqID));
1155       ChangeStatus(kStop);
1156     }       
1157
1158   return kFALSE;
1159 }
1160
1161 //____________________________________________________________________________
1162 Bool_t AliTRDRawStreamV2::Next()
1163 {
1164   //
1165   // Updates the next data word pointer
1166   //
1167
1168   if (fNextStatus == kStart)
1169     {
1170       Init();
1171     }
1172
1173   while (fNextStatus != kStop)
1174     { // !kStop
1175       NextData();
1176       
1177       switch (fNextStatus)
1178         {
1179         case kNextData:
1180           {
1181           if (DecodeNextRawWord() == kTRUE)
1182             {
1183               fTB += 3;
1184               if (fSig[0] > fRawDigitThreshold || fSig[1] > fRawDigitThreshold || fSig[2] > fRawDigitThreshold) 
1185                 return kTRUE;
1186             }
1187           }; break;
1188         case kNextMCM:
1189           {
1190             if (DecodeMCM() == kFALSE)
1191               AliWarning(Form("Decode MCM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));      
1192           }; break;
1193         case kNextHC:
1194         case kSeekNonEoTracklet:
1195         case kDecodeHC:
1196           {
1197             if (DecodeHC() == kFALSE)
1198               {
1199                 AliWarning(Form("Decode HC unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));     
1200               }
1201             else
1202               {
1203                 //the hc header should be decoded by now
1204                 if (fLastStatus == kDecodeHC)
1205                   {
1206                     fLastDET = fDET;
1207                     fChamberDone[fDET]++;
1208                   }
1209               }
1210           }; break;
1211         case kNextSM:
1212           {
1213             if (DecodeSM() == kFALSE)
1214                 AliWarning(Form("Decode SM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));     
1215           }; break;
1216         case kStop:
1217           ; break;
1218         default:
1219           AliWarning(Form("Unknown state %d. Last state %d. Current Word 0x%x at pos 0x%x", fNextStatus, fLastStatus, *fDataWord, fPos));  
1220           ChangeStatus(kStop);
1221         };
1222
1223     } // not kStop
1224
1225   //AliDebug(1, Form("That's all folks! %d", fSM));
1226   return kFALSE;
1227 }
1228
1229 //____________________________________________________________________________
1230 Int_t AliTRDRawStreamV2::NextChamber(AliTRDdigitsManager *man, UInt_t** /*trackletContainer*/)
1231 {
1232   //
1233   // Fills single chamber digit array 
1234   // Return value is the detector number
1235   //
1236
1237   AliTRDarrayADC *digits = 0;
1238   AliTRDarrayDictionary *track0 = 0;
1239   AliTRDarrayDictionary *track1 = 0;
1240   AliTRDarrayDictionary *track2 = 0; 
1241   AliTRDSignalIndex *indexes = 0;
1242           
1243   if (fNextStatus == kStart)
1244     {
1245       Init();
1246     }
1247
1248 //   while (fNextStatus != kStop)
1249 //     { // !kStop
1250 //       NextData();
1251 //       // catch 3 things
1252 //       // 1) if end of raw data - if chamber complete return
1253 //       // 2) fill the data with signals if data decoded ok
1254 //       // 3) initialize (destroy old) after the det has changed -> just after HC header decoding
1255 //     } // not kStop
1256
1257   while (fNextStatus != kStop)
1258     { // !kStop
1259       NextData();
1260
1261       switch (fNextStatus)
1262         {
1263           
1264         case kNextData:
1265           {
1266             if (DecodeNextRawWord() == kTRUE)
1267               {
1268                 for (Int_t it = 0; it < 3; it++)
1269                   {
1270                     if ( fTB + it < fTBins )
1271                       {
1272                         if ( fSig[it] > fRawDigitThreshold )
1273                           {
1274                             digits->SetData(fROW, fCOL, fTB + it, fSig[it]);
1275                             indexes->AddIndexTBin(fROW, fCOL, fTB + it);
1276                             if (man->UsesDictionaries())
1277                               {
1278                                 track0->SetData(fROW, fCOL, fTB + it, 0);
1279                                 track1->SetData(fROW, fCOL, fTB + it, 0);
1280                                 track2->SetData(fROW, fCOL, fTB + it, 0);
1281                               } // if dictionaries
1282                           } // signal above zero
1283                       } // check the tbins range
1284                   } // for each tbin of current 3             
1285                 fTB += 3;
1286               }
1287             else
1288               {
1289                 // can be here as a fall back from decode raw data calling decodemcm
1290                 if (fEndOfDataFlag == kTRUE)
1291                   {
1292                     if (fChamberDone[fDET] == 2)
1293                       {
1294                         return fDET;
1295                       }                         
1296                     fEndOfDataFlag = kFALSE;
1297                   }             
1298               }
1299           }; break;
1300
1301         case kNextMCM:
1302           {
1303             if (DecodeMCM() == kFALSE)
1304               {
1305                 AliWarning(Form("Decode MCM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));    
1306               }
1307             // default place for end of raw data...
1308             if (fEndOfDataFlag == kTRUE)
1309               {
1310                 if (fChamberDone[fDET] == 2)
1311                   {
1312                     return fDET;
1313                   }                             
1314                 fEndOfDataFlag = kFALSE;
1315               }      
1316           }; break;
1317
1318         case kNextHC:
1319         case kSeekNonEoTracklet:
1320         case kDecodeHC:
1321           {
1322             if (DecodeHC() == kFALSE)
1323               {
1324                 AliWarning(Form("Decode HC unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));     
1325               }
1326             else
1327               {
1328                 //the hc header should be decoded by now
1329                 if (fLastStatus == kDecodeHC)
1330                   {
1331 //                  AliDebug(4, Form("???? New DET ???? %d last %d", fDET, fLastDET));
1332                     // allocate stuff for the new det
1333                     //man->ResetArrays();
1334                     digits = (AliTRDarrayADC *) man->GetDigits(fDET);
1335                     track0 = (AliTRDarrayDictionary *) man->GetDictionary(fDET,0);
1336                     track1 = (AliTRDarrayDictionary *) man->GetDictionary(fDET,1);
1337                     track2 = (AliTRDarrayDictionary *) man->GetDictionary(fDET,2);
1338                     
1339                     // Allocate memory space for the digits buffer
1340                     if (digits->GetNtime() == 0) 
1341                       {
1342 //                      AliDebug(5, Form("Alloc digits for det %d rows %d cols %d tbins %d", fDET, fRowMax, fColMax, fTBins));
1343                         digits->Allocate(fRowMax, fColMax, fTBins);
1344                         if (man->UsesDictionaries())
1345                           {
1346                             track0->Allocate(fRowMax, fColMax, fTBins);
1347                             track1->Allocate(fRowMax, fColMax, fTBins);
1348                             track2->Allocate(fRowMax, fColMax, fTBins);
1349                           }
1350                       }
1351                     
1352                     indexes = man->GetIndexes(fDET);
1353                     indexes->SetSM(fSM);
1354                     indexes->SetStack(fSTACK);
1355                     indexes->SetLayer(fLAYER);
1356                     indexes->SetDetNumber(fDET);
1357                     
1358                     if (indexes->IsAllocated() == kFALSE)
1359                       {
1360 //                      AliDebug(4, "Allocating indexes");            
1361                         indexes->Allocate(fRowMax, fColMax, fTBins);
1362                       }
1363                   } // is the HC header already decoded?
1364               } // decode hc ok
1365           }; break;
1366
1367         case kNextSM:
1368           {
1369             if (DecodeSM() == kFALSE)
1370               AliWarning(Form("Decode SM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));       
1371           }; break;
1372
1373         case kStop:
1374           ; break;
1375
1376         default:
1377           AliWarning(Form("Unknown state %d. Last state %d. Current Word 0x%x at pos 0x%x", fNextStatus, fLastStatus, *fDataWord, fPos));  
1378           ChangeStatus(kStop);
1379         };
1380
1381     } // not kStop
1382
1383   // we do not return chambers for which the end-of-data was not received twice (for each HC)
1384
1385   //AliDebug(1, Form("That's all folks! %d", fSM));
1386   //return kFALSE;
1387   return -1;
1388 }
1389
1390 //____________________________________________________________________________
1391 Bool_t
1392 AliTRDRawStreamV2::DumpWords(UInt_t *px, UInt_t iw, UInt_t marker)
1393 {
1394   
1395   TString tsreturn = Form("\n[ Dump Sequence at 0x%08x ] : ", px);
1396   for (UInt_t i = 0; i < iw; i++)
1397     {
1398       if (px + iw >= fpEnd)
1399         return kFALSE;
1400       
1401       if (i % 8 == 0)
1402         tsreturn += "\n                              ";
1403       if (marker != 0 && marker == px[i])
1404         tsreturn += Form(" *>0x%08x<* ", px[i]);
1405       else
1406         tsreturn += Form("0x%08x ", px[i]);
1407     }
1408   tsreturn += "\n";
1409   
1410   AliInfo(tsreturn.Data());
1411   
1412   return kTRUE;
1413 }