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