]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawFastStream.cxx
Correct the baseline subtraction in clusterizer
[u/mrichter/AliRoot.git] / TRD / AliTRDrawFastStream.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: AliTRDrawFastStream.cxx 27797 2008-08-05 14:37:22Z cblume $ */
17
18 ////////////////////////////////////////////////////////////////////////////////
19 //                                                                            //
20 // This class provides access to TRD digits in raw data in a way of streaming //
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: M. Ploskon (ploskon@ikf.uni-frankfurt.de)                          //  
28 // Author: MinJung Kweon (minjung@physi.uni-heidelberg.de)                    //
29 //                                                                            //
30 ////////////////////////////////////////////////////////////////////////////////
31
32 #include "TString.h"
33 #include "TFile.h"
34 #include "TTreeStream.h"
35
36 #include "AliTRDrawFastStream.h"
37 #include "AliTRDgeometry.h"
38 #include "AliTRDfeeParam.h"
39 #include "AliTRDdigitsManager.h"
40 #include "AliTRDarrayDictionary.h"
41 #include "AliTRDarrayADC.h"
42 #include "AliTRDSignalIndex.h"
43 #include "AliTRDdigitsParam.h"
44 #include "AliTRDrawTPStream.h"
45
46 #include "AliLog.h"
47 #include "AliRawReader.h"
48
49 #define END_OF_TRACKLET_MARKEROLD 0xaaaaaaaa
50 #define END_OF_TRACKLET_MARKERNEW 0x10001000
51 #define ENDOFRAWDATAMARKER 0x00000000
52 #define WORD_SIZE sizeof(UInt_t)           // size of a word in bytes
53 #define EXTRA_LEAD_WORDS 24
54 #define CDH_WORDS 8
55
56 #define IS_BIT_SET(w,b) ( ((w) >> (b)) & 0x1 ) // 1 if bit b is set in word w
57 #define GET_VALUE_AT(w,m,s) (( (w) >> (s)) & (m) )      // get value of word w rshifted by s and mask with m
58
59 // SM index word masks:
60 #define SM_HEADER_SIZE(w) GET_VALUE_AT(w,0xffff,16) 
61 #define TRACKLETS_ENABLED(w) IS_BIT_SET(w,5)
62 #define STACK_MASK(w) ((w) & 0x1f)
63
64 // Stack word masks
65 #define STACK_HEADER_SIZE(w) GET_VALUE_AT(w,0xffff,16)
66 #define STACK_LINK_WORD(w) ((w) & 0xfff)
67 #define LINK0_DATA_TYPE_FLAG(w) (GET_VALUE_AT(w,0x3,4) == (0x0) ? 0 : 1) // 0 if physics data
68 #define LINK1_DATA_TYPE_FLAG(w) (GET_VALUE_AT(w,0x3,20) == (0x0) ? 0 : 1) // 0 if physics data
69 #define LINK0_MONITOR_FLAG(w) (GET_VALUE_AT(w,0xf,0) == (0x0) ? 0 : 1) // 0 if OK
70 #define LINK1_MONITOR_FLAG(w) (GET_VALUE_AT(w,0xf,16) == (0x0) ? 0 : 1) // 0 if OK
71
72 // HC word masks
73 //#define HC_HEADER_MASK_ERR(w) ( ((w) & (0x80000003)) == (0x80000001) ? 0 : 1) // 0 if OK!!!
74 #define HC_HEADER_MASK_ERR(w) ( ((w) & (0x3)) == (0x1) ? 0 : 1) // 0 if OK!!!
75
76 // HC word 0
77 #define HC_SPECIAL_RAW_VERSION(w) IS_BIT_SET(w,31)
78 #define HC_MAJOR_RAW_VERSION(w) GET_VALUE_AT(w,0x7f,24)
79 #define HC_MAJOR_RAW_VERSION_OPT(w) GET_VALUE_AT(w,0x7,24)
80 #define HC_MINOR_RAW_VERSION(w) GET_VALUE_AT(w,0x7f,17)
81 #define HC_EXTRA_WORDS(w) GET_VALUE_AT(w,0x7,14)
82 #define HC_DCS_BOARD(w) GET_VALUE_AT(w,0xfff<<20,20)
83 #define HC_SM_NUMBER(w) GET_VALUE_AT(w,0x1f,9)
84 #define HC_LAYER_NUMBER(w) GET_VALUE_AT(w,0x7,6)
85 #define HC_STACK_NUMBER(w) GET_VALUE_AT(w,0x7,3)
86 #define HC_SIDE_NUMBER(w) IS_BIT_SET(w,2)
87
88 // HC word 1
89 #define HC_NTIMEBINS(w) GET_VALUE_AT(w,0x3f,26)
90 #define HC_BUNCH_CROSS_COUNTER(w) GET_VALUE_AT(w,0xffff,10)
91 #define HC_PRETRIGGER_COUNTER(w) GET_VALUE_AT(w,0xf,6)
92 #define HC_PRETRIGGER_PHASE(w) GET_VALUE_AT(w,0xf,6)
93
94 // MCM word and ADC mask
95 #define MCM_HEADER_MASK_ERR(w) ( ((w) & (0xf)) == (0xc) ? 0 : 1) // 0 if OK!!!
96 #define MCM_ADCMASK_MASK_ERR(w) ( ((w) & (0xf)) == (0xc) ? 0 : 1) // 0 if OK!!!
97 #define MCM_MCM_NUMBER(w) GET_VALUE_AT(w,0x0f,24)
98 #define MCM_ROB_NUMBER(w) GET_VALUE_AT(w,0x7,28)
99 #define MCM_EVENT_COUNTER(w) GET_VALUE_AT(w,0x00fffff,4)
100 #define MCM_ADCMASK_VAL(w) GET_VALUE_AT(w,0x1fffff,4)
101 #define MCM_ADCMASK_NADC(w) GET_VALUE_AT(w,0x1f,25)
102
103 #define MCM_DUMMY_ADCMASK_VAL 0x015fffffc  // updated 
104 #define ADCDATA_VAL1 0x2  // updated 
105 #define ADCDATA_VAL2 0x3  // updated 
106
107 //--------------------------------------------------------
108 #define ADC_WORD_MASK(w) ((w) & 0x3)
109 //--------------------------------------------------------
110 ClassImp(AliTRDrawFastStream)
111
112 Bool_t AliTRDrawFastStream::fgExtraSkip = kFALSE;
113 Bool_t AliTRDrawFastStream::fgSkipCDH = kFALSE;
114 Bool_t AliTRDrawFastStream::fgWarnError = kTRUE;
115 Bool_t AliTRDrawFastStream::fgCleanDataOnly = kFALSE;
116 Bool_t AliTRDrawFastStream::fgDebugFlag = kTRUE;
117 Bool_t AliTRDrawFastStream::fgEnableMemoryReset = kTRUE;
118 Bool_t AliTRDrawFastStream::fgStackNumberChecker = kTRUE;
119 Bool_t AliTRDrawFastStream::fgStackLinkNumberChecker = kFALSE;
120 Bool_t AliTRDrawFastStream::fgSkipData = kTRUE;
121 Bool_t AliTRDrawFastStream::fgEnableDecodeConfigData = kFALSE;
122 Int_t AliTRDrawFastStream::fgDumpHead = -1;
123 Short_t AliTRDrawFastStream::fgMCMordering[] =
124   {
125     12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3 
126   };
127 Short_t AliTRDrawFastStream::fgROBordering[] =
128   {
129     0, 1, 2, 3
130   };
131 Bool_t  AliTRDrawFastStream::fDumpingEnable = kFALSE;
132 Int_t  AliTRDrawFastStream::fDumpingSM = -1;
133 Int_t  AliTRDrawFastStream::fDumpingStack = -1;
134 Int_t  AliTRDrawFastStream::fDumpingLayer = -1;
135 Int_t  AliTRDrawFastStream::fDumpingROB = -1;
136 Int_t  AliTRDrawFastStream::fDumpingMCM = -1;
137
138 Int_t  AliTRDrawFastStream::fgLastHC = -1;
139 Int_t  AliTRDrawFastStream::fgLastROB = -1;
140 Int_t  AliTRDrawFastStream::fgLastIndex = -1;
141
142 //--------------------------------------------------------
143 AliTRDrawFastStream::AliTRDrawFastStream()
144   : AliTRDrawStreamBase()
145   , fSM()
146   , fStack(0)
147   , fHC(0)
148   , fLastHC(0)
149   , fMCM()
150   , fpPos(0)
151   , fpBegin(0)
152   , fpEnd(0)
153   , fWordLength(0)
154   , fpPosTemp(0)
155   , fGlobalNTimeBins(0)
156   , fIsTimeBinSet(kFALSE)
157   , fIsGlobalDigitsParamSet(kFALSE)
158   , fStackNumber(-1)
159   , fStackLinkNumber(-1)
160   , fLinkTrackletCounter(-1)
161   , fEndOfTrackletCount(-1)
162   , fNWordsCounter(-1)
163   , fMaskADCword(0)
164   , fTbinADC(0)
165   , fEventCounter(0)
166   , fLastEventCounter(0)
167   , fSharedPadsOn(kTRUE)
168   , fMaxADCgeom(0)
169   , fADCnumber(0)
170   , fCOL(0)
171   , fExtendedCOL(0)
172   , fIsShared(0)
173   , fBufferRead(0)
174   , fGeometry(0)
175   , fRawReader(0)
176   , fTRDfeeParam(0)
177 {
178   //
179   // default constructor
180   //
181
182   if (Init() == kFALSE) {
183     AliWarning("Unable to Init.");        
184   }
185 }
186
187 //--------------------------------------------------------
188 AliTRDrawFastStream::AliTRDrawFastStream(AliRawReader *rawReader)
189   : AliTRDrawStreamBase(rawReader)
190   , fSM()
191   , fStack(0)
192   , fHC(0)
193   , fLastHC(0)
194   , fMCM()
195   , fpPos(0)
196   , fpBegin(0)
197   , fpEnd(0)
198   , fWordLength(0)
199   , fpPosTemp(0)
200   , fGlobalNTimeBins(0)
201   , fIsTimeBinSet(kFALSE)
202   , fIsGlobalDigitsParamSet(kFALSE)
203   , fStackNumber(-1)
204   , fStackLinkNumber(-1)
205   , fLinkTrackletCounter(-1)
206   , fEndOfTrackletCount(-1)
207   , fNWordsCounter(-1)
208   , fMaskADCword(0)
209   , fTbinADC(0)
210   , fEventCounter(0)
211   , fLastEventCounter(0)
212   , fSharedPadsOn(kTRUE)
213   , fMaxADCgeom(0)
214   , fADCnumber(0)
215   , fCOL(0)
216   , fExtendedCOL(0)
217   , fIsShared(0)
218   , fBufferRead(0)
219   , fGeometry(0)
220   , fRawReader(rawReader)
221   , fTRDfeeParam(0)
222 {
223   //
224   // default constructor
225   //
226   if (fRawReader) {
227     if (Init() == kFALSE) {
228       AliWarning("Unable to Init. Try setting up the reader with SetReader or buffer with Init(void *, UInt_t )");        
229     }
230   }
231   else {
232     AliWarning("Unable to setup reader. Use SetReader(AliRawReader*).");
233   }
234 }
235
236 //------------------------------------------------------------
237 AliTRDrawFastStream::AliTRDrawFastStream(const AliTRDrawFastStream& /*st*/)
238   : AliTRDrawStreamBase()
239   , fSM()
240   , fStack(0)
241   , fHC(0)
242   , fLastHC(0)
243   , fMCM()
244   , fpPos(0)
245   , fpBegin(0)
246   , fpEnd(0)
247   , fWordLength(0)
248   , fpPosTemp(0)
249   , fGlobalNTimeBins(0)
250   , fIsTimeBinSet(kFALSE)
251   , fIsGlobalDigitsParamSet(kFALSE)
252   , fStackNumber(-1)
253   , fStackLinkNumber(-1)
254   , fLinkTrackletCounter(-1)
255   , fEndOfTrackletCount(-1)
256   , fNWordsCounter(-1)
257   , fMaskADCword(0)
258   , fTbinADC(0)
259   , fEventCounter(0)
260   , fLastEventCounter(0)
261   , fSharedPadsOn(kTRUE)
262   , fMaxADCgeom(0)
263   , fADCnumber(0)
264   , fCOL(0)
265   , fExtendedCOL(0)
266   , fIsShared(0)
267   , fBufferRead(0)
268   , fGeometry(0)
269   , fRawReader(0)
270   , fTRDfeeParam(0)
271 {
272   //
273   // Copy constructor
274   // 
275   AliError("Not implemeneted.");
276 }
277
278 //------------------------------------------------------------
279 Bool_t AliTRDrawFastStream::SetRawVersion(Int_t fraw)
280 {
281   //
282   // function provided for backward compatibility
283   //
284   AliWarning("Raw data version is read from raw data stream! No point of setting it in here.");
285   fraw = 0; // avoid warnings
286   return kFALSE;
287 }
288
289 //------------------------------------------------------------
290 AliTRDrawFastStream::~AliTRDrawFastStream()
291 {
292   //
293   // destructor
294   //
295   delete fGeometry;
296 }
297
298 //------------------------------------------------------------
299
300 AliTRDrawFastStream &
301 AliTRDrawFastStream::operator=(const AliTRDrawFastStream &)
302 {
303   //
304   // we are not using this functionality
305   //
306   AliFatal("May not use.");
307   return *this;
308 }
309
310 //___________________________________________________________
311 void AliTRDrawFastStream::SwapOnEndian()
312 {
313   //
314   // Check the endian and swap if needed
315   //
316   int itemp = 1;
317   char* ptemp = (char*) &itemp;
318   if (ptemp[0] != 1)
319     {
320       if (fgDebugFlag) AliDebug(8, "Swapping.");
321
322       fpPos = fpBegin;
323       UInt_t iutmp = 0;
324       while (fpPos < fpEnd)
325            {
326              fpPos += 1;
327              iutmp = (((*fpPos & 0x000000ffU) << 24) | ((*fpPos & 0x0000ff00U) <<  8) |
328              ((*fpPos & 0x00ff0000U) >>  8) | ((*fpPos & 0xff000000U) >> 24));
329              // here we override the value in the buffer!
330              *fpPos = iutmp;      
331            }
332       fpPos = fpBegin;
333     }
334 }
335
336 //------------------------------------------------------------
337 Bool_t AliTRDrawFastStream::SkipWords(UInt_t iw)
338 {
339   //
340   // Skip words corresponding to iw
341   //
342   if ( fpPos + iw < fpEnd ) {
343     fpPos += iw;
344     return kTRUE;
345   }
346   else {
347     if (fgWarnError) AliWarning(Form("Skip %d words failed. %d available", iw, fpEnd - fpPos - 1));
348     return kFALSE;
349   }
350
351   return kTRUE;
352 }
353
354 //------------------------------------------------------------
355 Bool_t AliTRDrawFastStream::SetReader(AliRawReader *reader)
356 {
357
358   if (reader != 0) {
359     fRawReader = reader;
360     if (fRawReader) {
361       return Init();
362     }
363     else {
364       AliWarning("Unable to setup reader.");
365       return kFALSE;
366     }
367   }
368   else {
369     AliWarning("AliRawReader argument is 0.");
370     fRawReader = 0;
371   }
372
373   return kFALSE;
374 }
375
376 //------------------------------------------------------------
377 Int_t AliTRDrawFastStream::NextBuffer()
378 {
379   //
380   // return -1 if no more buffers available
381   // return  0 if SMHeader decoding failed 
382   // return  1 if SMHeader dedoding is OK
383   // 
384   if (fRawReader != 0) {
385     UChar_t *buffer = 0;
386     UInt_t length = 0;
387     Bool_t kBufferSet = fRawReader->ReadNextData(buffer);
388     if (kBufferSet == kTRUE) {
389       if (fgDebugFlag)  AliDebug(9, "Buffer is set.");
390       length = fRawReader->GetDataSize();
391       if (fgExtraSkip == kTRUE) {
392         buffer += EXTRA_LEAD_WORDS * WORD_SIZE;
393         length -= EXTRA_LEAD_WORDS * WORD_SIZE;
394       }
395
396       if (fgSkipCDH == kTRUE) {
397         buffer += CDH_WORDS * WORD_SIZE;
398         length -= CDH_WORDS * WORD_SIZE;              
399       }
400
401       if (length > 0) {
402         if (fgDebugFlag)  AliDebug(9, Form("Buffer length : %d", length));
403         if (fgEnableMemoryReset) ResetMemory(); 
404         if (DecodeSMHeader((void*)buffer, length) == kTRUE)
405           return 1;
406         else
407           return 0;
408       }
409     }
410     else {
411       return -1;
412     }
413   }
414
415   return -1;
416 }
417
418 //------------------------------------------------------------
419 void AliTRDrawFastStream::ResetCounters()
420 {
421   //
422   // reset some global counters
423   //
424   fBufferRead = kFALSE; // kFALSE if no buffer read
425
426   fSM.fActiveStacks = 0;
427   fSM.fNexpectedHalfChambers = 0;
428
429   fLastEventCounter = 0;
430   fEventCounter = 0;
431
432   ResetIterators();
433 }
434
435 //------------------------------------------------------------
436 void AliTRDrawFastStream::ResetIterators()
437 {
438   //
439   // reset data which should be reset every sm
440   //
441   fStackNumber = 0;     
442   fStackLinkNumber = 0; 
443 }
444
445 //------------------------------------------------------------
446 void AliTRDrawFastStream::ResetPerSM()
447 {
448   //
449   // reset every SM
450   //
451   fSM.fHeaderSize = 0;
452   fSM.fTrackletEnable = kFALSE;
453   fSM.fCorrupted = 0;
454   fSM.fNexpectedHalfChambers = 0;
455   fSM.fNexpectedHalfChambers = 0;
456   fSM.fPos = NULL;
457   for (Int_t i=0; i<5; i++) {
458      fSM.fStackActive[i] = kFALSE;
459   }
460 }
461
462 //------------------------------------------------------------
463 void AliTRDrawFastStream::ResetPerStack()
464 {
465   //
466   // reset every Stack
467   //
468   fStack->fHeaderSize = 0;
469   fStack->fActiveLinks = 0;
470   fStack->fPos = NULL;
471   for (Int_t i=0; i<12; i++) {
472      fStack->fLinksActive[i] = kFALSE;
473      fStack->fLinksDataType[i] = 0;
474      fStack->fLinksMonitor[i] = 0;
475      fStack->fLinkMonitorError[i] = 0;
476   }
477 }
478
479 //------------------------------------------------------------
480 void AliTRDrawFastStream::ResetPerHC()
481 {
482   //
483   // reset every HC
484   //
485   fEventCounter = 0;
486   fHC->fNTracklets = 0;
487   fHC->fSpecialRawV = 0;
488   fHC->fRawVMajor = 0;
489   fHC->fRawVMajorOpt = 0;
490   fHC->fRawVMinor = 0;
491   fHC->fNExtraWords = 0;
492   fHC->fDCSboard = 0;
493   fHC->fTimeBins = 0;
494   fHC->fBunchCrossCounter = 0;
495   fHC->fPreTriggerCounter = 0;
496   fHC->fPreTriggerPhase = 0;
497   fHC->fMCMmax = 0;
498
499   fHC->fSM = 0;
500   fHC->fStack = 0;
501   fHC->fStackHCheader = 0;
502   fHC->fLayer = 0;
503   fHC->fLayerHCheader = 0;
504   fHC->fSide = 0;
505   fHC->fSideHCheader = 0;
506   fHC->fDET = 0;
507   fHC->fROC = 0;
508   fHC->fRowMax = 0;
509   fHC->fColMax = 0;
510
511   fHC->fH0Corrupted = 0;
512   fHC->fH1Corrupted = 0;
513   fHC->fCorrupted = 0;
514   fHC->fEOTECorrupted = kFALSE;
515   fHC->fBufferCorrupted = kFALSE;
516   fHC->fDataCorrupted = kFALSE;
517
518   fHC->fNErrors= 0;
519   memset(fHC->fErrorCodes, 0, 1411*sizeof(UShort_t)); // initialize error container
520   memset(fHC->fTrackletWords, 0, MAXTRACKLETSPERHC*sizeof(UInt_t)); // initialize tracklet container
521 }
522
523 //------------------------------------------------------------
524 void AliTRDrawFastStream::ResetPerMCM()
525 {
526   //
527   // reset every MCM
528   //
529   fMCM.fROB = 0;
530   fMCM.fMCM = 0;
531   fMCM.fROW = 0;
532   fMCM.fEvCounter = 0;
533   fMCM.fADCMask = 0;
534   fMCM.fADCMaskWord = 0;
535   fMCM.fADCmax = 0;
536   fMCM.fADCcount = 0;
537   fMCM.fMCMADCWords = 0;
538   fMCM.fSingleADCwords = 0;
539   fMCM.fMCMhdCorrupted = 0;
540   fMCM.fADCmaskCorrupted = 0;
541   fMCM.fDataCorrupted = kFALSE;
542   fMCM.fPos = NULL;
543   fMCM.fAdcDataPos = NULL;
544   fMCM.fADCcounter = 0;
545
546   memset(fMCM.fADCchannel, 0, TRDMAXADC*sizeof(UInt_t));
547 }
548
549 //------------------------------------------------------------
550 void AliTRDrawFastStream::ResetMemory()
551 {
552   //
553   // initialize all the data members to prevent read data
554   // from previous buffer
555   //
556   ResetPerSM();
557   for (Int_t istack=0; istack<5; istack++) {
558      fStack = &fSM.fStacks[istack];
559      ResetPerStack();
560      for (Int_t ilink=0; ilink<12; ilink++) {
561         fHC = &fStack->fHalfChambers[ilink];
562         ResetPerHC();
563      }
564   }
565 }
566
567 //------------------------------------------------------------
568 Bool_t AliTRDrawFastStream::Next()
569 {
570   //
571   // returns with true on next adc read, returns false on errors and end of buffer
572   // 
573   if (fBufferRead) {
574     while (fStackNumber < 5 && fSM.fActiveStacks > 0) {
575       if (fSM.fStackActive[fStackNumber] == kTRUE) {
576         fStack = &fSM.fStacks[fStackNumber];
577         while (fStackLinkNumber < 12) {
578           if (fStack->fLinksActive[fStackLinkNumber] == kTRUE) {
579           //if (fStack->fLinksActive[fStackLinkNumber] == kTRUE && fStack->fLinksMonitor[fStackLinkNumber] == 0)
580             fHC = &fStack->fHalfChambers[fStackLinkNumber];
581                         //ResetPerHC(); // [mj - you don't need to do? ]
582             if (!fHC) {
583               AliError(Form("HC missing at stack %d link %d", fStackNumber, fStackLinkNumber));
584               return kFALSE;
585             }
586             fStackLinkNumber++;
587             return kTRUE;
588           } //link active ?
589           else fStackLinkNumber++;
590         } //stack link number loop 
591       } //stack active ?
592       fStackNumber++;
593       fStackLinkNumber = 0;
594     } //stack number loop
595   } //fBufferRead
596
597   // go for the next buffer 
598   if (fRawReader) {
599     Int_t nextBuff = NextBuffer();
600     while (nextBuff != -1) {
601       if (nextBuff > 0) {
602         fBufferRead = kTRUE;
603         return Next();            
604       }
605       nextBuff = NextBuffer();
606     }
607   }
608
609   return kFALSE;
610 }
611
612 //------------------------------------------------------------
613 Int_t AliTRDrawFastStream::NextChamber(AliTRDdigitsManager *digitsManager, UInt_t **trackletContainer, UShort_t **errorCodeContainer) 
614 {
615   
616   //
617   // Fills single chamber digit array 
618   // Return value is the detector number
619   //
620   // first of all, you do the SM header decoding only at the beginning of the SM data reading
621   // then, every HC, you call Next() which points next HC. then, there you decode the given HC 
622   // and at the same time, put the digit into digitmanager 
623   //
624   AliTRDarrayADC *digits = 0;
625   AliTRDarrayDictionary *track0 = 0;
626   AliTRDarrayDictionary *track1 = 0;
627   AliTRDarrayDictionary *track2 = 0; 
628   AliTRDSignalIndex *indexes = 0;
629   AliTRDdigitsParam *digitsparam = 0;
630
631   Int_t lastdet = -1;
632   Int_t det     = -1;
633   Int_t side    = -1;
634   //Int_t it = 0;
635   Int_t ntracklets = 0;
636   Int_t nErrors = 0;
637
638   if (trackletContainer){
639     for (Int_t i = 0; i < 2; i++)
640        memset(trackletContainer[i], 0, MAXTRACKLETSPERHC*sizeof(UInt_t));
641   }
642
643   if (errorCodeContainer){
644     for (Int_t i = 0; i < 2; i++)
645        memset(errorCodeContainer[i], 0, 1411*sizeof(UShort_t));
646   }
647
648   while ( Next() ) { // loop over HC 
649
650     // get this information from the GTU header
651     det    = GetDet();
652     side   = GetSide();
653
654     if (det != lastdet) {
655       // If new detector found
656       if (lastdet == -1) {lastdet = det; fLastHC = fHC;}
657       else {fStackLinkNumber--; fHC = fLastHC ; return lastdet;}
658
659       if (det < 0 || det >= AliTRDgeometry::kNdet) continue;
660
661       // Add a container for the digits of this detector
662       digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
663
664       if (digitsManager->UsesDictionaries()) {
665         track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
666         track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
667         track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
668       }
669
670       if (!digits) return -1;
671
672       Int_t rowMax = GetRowMax();
673       Int_t colMax = GetColMax();
674       Int_t ntbins = GetGlobalNTimeBins(); 
675
676       // Set number of timebin into digitparam
677       if (!fIsGlobalDigitsParamSet){
678         digitsparam = (AliTRDdigitsParam *) digitsManager->GetDigitsParam();
679         digitsparam->SetCheckOCDB(kFALSE);
680         digitsparam->SetNTimeBins(ntbins);
681         digitsparam->SetADCbaseline(10);
682         digitsparam->SetPretiggerPhase(det,GetPreTriggerPhase());\r\r
683         fIsGlobalDigitsParamSet = kTRUE;
684       } 
685       if(!digitsparam){\r\r
686         digitsparam = (AliTRDdigitsParam *) digitsManager->GetDigitsParam();\r\r
687         digitsparam->SetPretiggerPhase(det,GetPreTriggerPhase());\r\r
688       } \r\r
689
690       // Allocate memory space for the digits buffer
691       if (digits->GetNtime() == 0) {
692         digits->Allocate(rowMax, colMax, ntbins);
693         if (digitsManager->UsesDictionaries()) {
694           track0->Allocate(rowMax, colMax, ntbins);
695           track1->Allocate(rowMax, colMax, ntbins);
696           track2->Allocate(rowMax, colMax, ntbins);
697         }
698       }
699
700       indexes = digitsManager->GetIndexes(det);
701       indexes->SetSM(GetSM());
702       indexes->SetStack(GetStack());
703       indexes->SetLayer(GetLayer());
704       indexes->SetDetNumber(det);
705       if (indexes->IsAllocated() == kFALSE)
706         indexes->Allocate(rowMax, colMax, ntbins);
707     }
708
709     if (fSM.fTrackletEnable == kTRUE) { 
710       if (DecodeTracklets() == kFALSE) {
711         SeekEndOfData();
712
713         if (fgWarnError) AliError(Form("Tracklet decoding failed stack %d link %d", GetStack(), fStackLinkNumber));
714
715         // copy error codes in memory into error container
716         if (errorCodeContainer) {
717           nErrors = GetNErrors();
718           if(nErrors > 0) memcpy(errorCodeContainer[side], GetErrorCodes(), sizeof(UShort_t) * 1411); // [mj temp - optimize] 
719         }
720         
721         continue; // if it fails to decode tracklets of this HC, it skips further decoding and goes to next HC  
722       }     
723     }   
724
725     // decode hc data
726     fgLastROB   = -1; // to check mcm number odering 
727     fgLastIndex = -1 ; // to check mcm number odering 
728     if (DecodeHC(digitsManager, digits, track0, track1, track2, indexes) == kFALSE) {
729       // encode HC error code
730       fHC->fErrorCodes[2] += fHC->fH0Corrupted;
731       fHC->fErrorCodes[2] += (fHC->fH1Corrupted << 2);
732       fHC->fErrorCodes[2] += (fHC->fCorrupted << 3);
733       fHC->fErrorCodes[2] += ((fHC->fBufferCorrupted & 1) << 6);  
734       fHC->fErrorCodes[2] += ((fHC->fEOTECorrupted & 1) << 7);  
735       fHC->fErrorCodes[2] += ((fHC->fDataCorrupted & 1) << 8);  
736
737             if (fHC->fEOTECorrupted != kTRUE)  SeekEndOfData();
738
739 /*
740       if (fgWarnError) {
741         AliError(Form("Failed HC : %s", DumpHCinfoH0(fHC)));
742         AliError(Form("Failed HC : %s", DumpHCinfoH1(fHC)));
743       }
744 */ // [mj temp]
745           }
746     else SeekEndOfData(); // make sure that finish off with the end of data markers
747
748     // copy tracklets in memory into tracklet container
749     if (trackletContainer) {
750       ntracklets = GetNTracklets();
751       // copy tracklet words to trackletContainer array 
752       if(ntracklets > 0) memcpy(trackletContainer[side], GetTrackletWords(), sizeof(UInt_t) * ntracklets); 
753     }
754
755     // copy error codes in memory into error container
756     if (errorCodeContainer) {
757       nErrors = GetNErrors();
758       if(nErrors > 0) memcpy(errorCodeContainer[side], GetErrorCodes(), sizeof(UShort_t) * 1411); 
759     }
760
761   }// end of while 
762
763   return det;
764 }
765
766 //------------------------------------------------------------
767 Bool_t AliTRDrawFastStream::Init()
768 {
769   //
770   // Initialize geometry and fee parameters 
771   //
772   TDirectory *saveDir = gDirectory; 
773   
774   if (!fGeometry) {
775     fGeometry = new AliTRDgeometry();
776   }
777   
778   if (!fGeometry) {
779     AliError("Geometry FAILED!");
780     return kFALSE;
781   }
782
783   fTRDfeeParam = AliTRDfeeParam::Instance();
784   if (!fTRDfeeParam) {
785     AliError("AliTRDfeeParam FAILED!");
786     return kFALSE;
787   }
788
789   fMaxADCgeom = (Int_t)fGeometry->ADCmax();
790
791   ResetCounters(); // fBufferRead is set to kFALSE - important
792
793   saveDir->cd();
794
795   return kTRUE;
796 }
797
798 //------------------------------------------------------------
799 Bool_t AliTRDrawFastStream::InitBuffer(void *buffer, UInt_t length)
800 {
801   // 
802   // set initial information about the buffer
803   //
804   if (fgDebugFlag)  AliDebug(5, Form("Equipment ID: %d",fRawReader->GetEquipmentId()));
805   if (fRawReader->GetEquipmentId()<1024 || fRawReader->GetEquipmentId()>1041) 
806     return kFALSE; 
807
808   ResetCounters();
809
810   fpBegin = (UInt_t *)buffer;
811
812   if (WORD_SIZE == 0) {
813     AliFatal("Strange word size. size of UInt_t == 0");
814     return kFALSE;
815   }
816
817   fWordLength = length/WORD_SIZE;
818   fpEnd = fpBegin + fWordLength;
819   fpPos = fpBegin;
820
821   if (fpBegin == 0 || length <= 0) {
822     AliError(Form("Buffer size or pointer is strange. pointer to the buffer is 0x%08x of size %d", fpBegin, length));
823     return kFALSE;
824   }
825
826   SwapOnEndian();
827
828   if (fgDumpHead >= 0) {
829     if ( fgDumpHead == 0 ) { // dump all words
830       AliInfo(Form("---------- Dumping all words from the beginnig of the buffer ----------"));
831       if (DumpWords(fpBegin, fWordLength) == kFALSE) AliError("Dump failed. Not enough data.");
832     } 
833     else { 
834       AliInfo(Form("---------- Dumping %u words from the beginnig of the buffer ----------",fgDumpHead));
835       if (DumpWords(fpBegin, fgDumpHead) == kFALSE) AliError("Dump failed. Not enough data.");
836     }
837     AliInfo(Form("---------- Dumping ended ----------------------------------------------"));
838   }
839
840   return kTRUE;
841 }
842
843 //------------------------------------------------------------
844 Bool_t AliTRDrawFastStream::DecodeSMHeader(void *buffer, UInt_t length)
845 {
846   // 
847   // decode one sm data in buffer
848   // 
849   ResetIterators(); 
850
851   if (InitBuffer(buffer, length) == kFALSE) {
852     if (fgWarnError) AliError("InitBuffer failed.");      
853     return kFALSE;
854   }
855
856   if (DecodeGTUheader()== kFALSE)
857     return kFALSE;
858
859   for (Int_t istack = 0; istack < 5; istack++) {
860      fStackNumber = istack; 
861      if (fSM.fStackActive[istack] == kFALSE) continue;
862       
863      fStack = &fSM.fStacks[istack];
864      
865      fgLastHC  = -1; // to check rob number odering 
866      for (Int_t ilink = 0; ilink < 12; ilink++) {
867         fStackLinkNumber = ilink; 
868         if (fStack->fLinksActive[ilink] == kFALSE) continue;
869     
870         // check GTU link monitor 
871         if (!(fStack->fLinksDataType[ilink] == 0 && fStack->fLinksMonitor[ilink] == 0)) {
872           fStack->fLinkMonitorError[ilink] = 1;
873           fStack->fLinkMonitorError[ilink] += fNWordsCounter; // counts words of given hc having link monitor error
874           //continue;
875         }
876
877             if (fpPos >= fpEnd) {
878           if (fRawReader) fRawReader->AddMajorErrorLog(kLinkDataMissing, "Link data missing");          
879           if (fgWarnError) AliError("Link data missing.");
880           break;
881         }
882
883               // HLT streamer set det number using SM header 
884         fHC = &fStack->fHalfChambers[ilink];
885               fHC->fSM = fRawReader->GetEquipmentId() - 1024;
886               fHC->fStack = fStackNumber;
887               fHC->fLayer = Int_t(fStackLinkNumber/2.);
888               fHC->fSide = fStackLinkNumber%2;
889               fHC->fDET = fGeometry->GetDetector(fHC->fLayer, fHC->fStack, fHC->fSM);
890               fHC->fRowMax = fGeometry->GetRowMax(fHC->fLayer, fHC->fStack, fHC->fSM);
891               fHC->fROC    = fGeometry->GetDetectorSec(fHC->fLayer, fHC->fStack);
892               fHC->fColMax = fGeometry->GetColMax(fHC->fROC);
893            }
894   }     
895
896   // set number of timebin to be used in the digit container 
897   if (!fIsTimeBinSet) {
898     fpPosTemp = fpPos;
899     SetGlobalNTimebins();
900     fIsTimeBinSet = kTRUE;
901   }
902
903   ResetIterators(); // need to do it again for Next() function
904
905   return kTRUE;
906 }
907
908 //------------------------------------------------------------
909 Bool_t AliTRDrawFastStream::DecodeGTUheader()
910 {
911   //
912   // decode Supermodule Index Word
913   //
914   DecodeSMInfo(fpPos, &fSM);
915
916   if (fgDebugFlag)  AliDebug(5, DumpSMInfo(&fSM));
917
918   fpPos++;
919   if (fpPos < fpEnd) {
920     // fSM.fHeaderSize represent additional Supermodule header size which contains additional information regarding hardware design.
921     // For the moment, we skip decoding these words 
922     if (SkipWords(fSM.fHeaderSize) == kTRUE) {
923       for (Int_t istack = 0; istack < 5; istack++) {
924          if (fSM.fStackActive[istack] == kFALSE)
925            continue;
926
927          fStack = &fSM.fStacks[istack];
928
929          // Decode Stack Index Word of given stack
930          DecodeStackInfo(fpPos, fStack);
931          fpPos++;
932
933          fSM.fNexpectedHalfChambers += fStack->fActiveLinks;
934         
935          if (fgDebugFlag)  AliDebug(5, DumpStackInfo(fStack));
936         
937          if (SkipWords(fStack->fHeaderSize-6) == kFALSE) { // 6 is the 6 stack header words for 12 links 
938            if (fRawReader) fRawReader->AddMajorErrorLog(kDecodeStackInfo, "Stack header words missing");
939            return kFALSE;
940          }
941          for (Int_t iword=0; iword<6; iword++) { // decode 6 stack header words
942             // Decode Stack Header Word of given stack
943             DecodeStackHeader(fpPos, fStack, iword); 
944             fpPos++;
945          } // iword
946       } // istack
947     }
948     else {
949       return kFALSE;
950     }
951   }
952   else {
953     if (fgWarnError) AliWarning("No additional sm headers and stack index words present.");
954     if (fRawReader) fRawReader->AddMajorErrorLog(kDecodeStackInfo, "Stack info missing");
955     return kFALSE;
956   }
957
958   if (fpPos < fpEnd) {
959     if (fgDebugFlag)  AliDebug(5, "GTU headers are OK.");
960   }
961   else {
962     if (fgWarnError) AliWarning("No data just after GTU headers.");
963     if (fRawReader) fRawReader->AddMajorErrorLog(kMissingData, "Missing sm data");
964     return kFALSE;
965   }
966
967   if (fgDebugFlag)  AliDebug(5, Form("Expected half chambers from GTU header: %d", fSM.fNexpectedHalfChambers));
968
969   return kTRUE;
970 }
971
972 //--------------------------------------------------------
973 void AliTRDrawFastStream::DecodeStackInfo(const UInt_t *word, struct AliTRDrawStack *st) const
974 {
975   //
976   // decode Stack #i Index Word
977   // The Stack #i Index Word is a 32-Bit word with following structure
978   // ssssssss ssssssss vvvv mmmm mmmmmmmm
979   // s: Size of the Stack #i Header, v: Supermodule Header Version, m: Link Mask
980   //
981   st->fPos = (UInt_t*)word;
982
983   UInt_t vword = *word;
984   st->fHeaderSize = STACK_HEADER_SIZE(vword);
985
986   UInt_t linkMask = STACK_LINK_WORD(vword);
987   st->fActiveLinks = 0;
988   for (Int_t i = 0; i < 12; i++) {
989      if (IS_BIT_SET(linkMask,i) > 0) {
990        st->fLinksActive[i] = kTRUE;
991        st->fActiveLinks++;
992      }
993      else {
994        st->fLinksActive[i] = kFALSE;
995      }
996   }
997 }
998
999 //--------------------------------------------------------
1000 void AliTRDrawFastStream::DecodeStackHeader(const UInt_t *word, struct AliTRDrawStack *st, Int_t iword) const
1001 {
1002   //
1003   // decode stack header
1004   //
1005   st->fPos = (UInt_t*)word;
1006
1007   UInt_t vword = *word;
1008   st->fLinksDataType[2*iword]    = LINK0_DATA_TYPE_FLAG(vword);
1009   st->fLinksMonitor[2*iword]     = LINK0_MONITOR_FLAG(vword);
1010   st->fLinksDataType[2*iword+1]  = LINK1_DATA_TYPE_FLAG(vword);
1011   st->fLinksMonitor[2*iword+1]   = LINK1_MONITOR_FLAG(vword);
1012 }
1013
1014 //------------------------------------------------------------
1015 Bool_t AliTRDrawFastStream::DecodeTracklets()
1016 {
1017   //
1018   // decode tracklets
1019   //
1020   fLinkTrackletCounter = 0; // tracklet counter of this link 
1021   fEndOfTrackletCount = 0;  // tracklet endmarker counter of this link
1022   fHC->fNTracklets = 0;     // number of tracklet of this link, should be less than 256
1023
1024   if (fgDebugFlag)  AliDebug(10, Form("Decode tracklets at 0x%08x : 0x%08x", fpPos, *fpPos));
1025
1026   while (!(*fpPos == END_OF_TRACKLET_MARKEROLD || *fpPos == END_OF_TRACKLET_MARKERNEW) && fpPos < fpEnd) {
1027     if (fgDebugFlag)  AliDebug(10, Form("Tracklet found at 0x%08x : 0x%08x", fpPos, *fpPos));
1028
1029     fLinkTrackletCounter++;
1030
1031     if (fLinkTrackletCounter > MAXTRACKLETSPERHC) {
1032       if (fgDebugFlag) AliDebug(11,Form("Max number of tracklets exceeded %d > %d. Tracklets are wrong either GTU header has problem",
1033                                         fLinkTrackletCounter, MAXTRACKLETSPERHC));
1034       if (fRawReader) fRawReader->AddMajorErrorLog(kTrackletOverflow,"Too many tracklets"); 
1035       fHC->fErrorCodes[1] = 1;          
1036       return kFALSE;
1037     }
1038
1039     fHC->fTrackletWords[fLinkTrackletCounter-1] = UInt_t(*fpPos); //store tracklet words into memory 
1040     fpPos++;
1041   }
1042
1043   fHC->fNTracklets = fLinkTrackletCounter;
1044
1045   while ((*fpPos == END_OF_TRACKLET_MARKEROLD || *fpPos == END_OF_TRACKLET_MARKERNEW) && fpPos < fpEnd) {
1046     if (fgDebugFlag)  AliDebug(10, Form("EoTracklets found at 0x%08x : 0x%08x", fpPos, *fpPos));
1047
1048     fEndOfTrackletCount++;
1049     fpPos++;
1050   }
1051
1052   if (fEndOfTrackletCount < 2) {
1053     if (fgDebugFlag) AliDebug(11,"End of tracklets word missing"); 
1054     if (fRawReader) fRawReader->AddMajorErrorLog(kEOTrackeltsMissing, "End of tracklets word missing"); 
1055     fHC->fErrorCodes[1] += 2;          
1056     return kFALSE;
1057   }
1058
1059   return kTRUE;
1060 }
1061
1062 //--------------------------------------------------------
1063 void AliTRDrawFastStream::DecodeSMInfo(const UInt_t *word, struct AliTRDrawSM *sm) const
1064 {
1065   //
1066   // decode Supermodule Index Word
1067   // The Supermodule Index Word is a 32-Bit word wit following structure
1068   // ssssssss ssssssss vvvv rrrr r d t mmmm
1069   // s: Size of the Supermodule Header, v: Supermodule Header Version, r: Reserved for future use
1070   // d: Track Data Enabled Bit, t: Tracklet Data Enabled Bit, m: Stack Mask 
1071   //
1072   sm->fPos = (UInt_t*)word; 
1073
1074   UInt_t vword = *word;
1075   sm->fHeaderSize = SM_HEADER_SIZE(vword);
1076     
1077   if (TRACKLETS_ENABLED(vword) > 0)
1078     sm->fTrackletEnable = kTRUE;
1079   else
1080     sm->fTrackletEnable = kFALSE;
1081     
1082   UInt_t stackMask = STACK_MASK(vword);
1083   sm->fActiveStacks = 0;
1084   for (Int_t i = 0; i < 5; i++)
1085     {
1086       if (IS_BIT_SET(stackMask,i) > 0)
1087         {
1088           sm->fStackActive[i] = kTRUE;
1089           sm->fActiveStacks++;
1090         }
1091       else
1092         {
1093           sm->fStackActive[i] = kFALSE;
1094         }
1095     }
1096 }
1097
1098 //------------------------------------------------------------
1099 Bool_t AliTRDrawFastStream::DecodeHC(AliTRDdigitsManager *digitsManager, AliTRDarrayADC *digits, 
1100                              AliTRDarrayDictionary *track0, AliTRDarrayDictionary *track1, AliTRDarrayDictionary *track2, 
1101                              AliTRDSignalIndex *indexes)
1102 {
1103   //
1104   // decode hc header and data
1105   //
1106   if (fpPos >= fpEnd) {
1107     fHC->fCorrupted += 1; 
1108     if (fgWarnError) AliError("No data(including HC header) in the buffer");
1109     return kFALSE;;
1110   }
1111
1112   if (DecodeHCheader() == kFALSE) {
1113     if (fgWarnError) AliWarning(Form("HC Header decode failed. H0 Error: %d H1 Error: %d",fHC->fH0Corrupted,fHC->fH1Corrupted));
1114     return kFALSE;
1115   }
1116   else {
1117     fpPos++;
1118     if (fpPos >= fpEnd) {
1119       fHC->fCorrupted += 2; 
1120       if (fgWarnError) AliError("No data right after HC header in the buffer");
1121       return kFALSE;
1122     }
1123   }
1124
1125   if ((fHC->fRawVMajor & 64) == 64) { // test pattern data
1126     AliTRDrawTPStream *tpStream = new AliTRDrawTPStream(fHC->fRawVMajorOpt, fpPos);
1127     if (tpStream->DecodeTPdata() == kFALSE) {
1128       if (fgWarnError) AliError("Failed to decode test pattern data");
1129       return kFALSE;
1130     }
1131     return kTRUE;
1132   }
1133
1134   fHC->fMCMmax = 0; // count number of mcms in a hc 
1135   while (*fpPos != ENDOFRAWDATAMARKER && fpPos < fpEnd) {
1136
1137     ResetPerMCM(); // reset for every mcm 
1138
1139     if (fHC->fMCMmax > TRDMAXMCM) {
1140       fHC->fCorrupted += 4; 
1141       if (fgDebugFlag) AliDebug(11,"More mcm data than expected");
1142       return kFALSE;
1143     }
1144
1145     if (DecodeMCMheader() == kFALSE) {
1146
1147       // encode mcm level error codes
1148       fHC->fErrorCodes[fHC->fMCMmax+2] += fMCM.fMCMhdCorrupted;
1149       fHC->fErrorCodes[fHC->fMCMmax+2] += (fMCM.fADCmaskCorrupted << 4);
1150       fHC->fErrorCodes[fHC->fMCMmax+2] += ((fMCM.fDataCorrupted & 1) << 6);
1151       fHC->fErrorCodes[fHC->fMCMmax+2] += (fMCM.fMCM << 7);  // encode MCM number
1152       fHC->fErrorCodes[fHC->fMCMmax+2] += (fMCM.fROB << 11); // encode ROB number
1153
1154       fHC->fMCMmax++; // increase mcm counter to match with expected rob/mcm number
1155
1156       // in case we decide to keep reading data, skip this mcm data and find next mcm header
1157       if (fMCM.fADCmaskCorrupted < 2) {
1158         if (SkipMCMdata(fMCM.fADCcount*fMCM.fSingleADCwords) == kFALSE)
1159           return kFALSE;
1160         continue;
1161       }
1162       else {
1163         if (SeekNextMCMheader() == kFALSE)
1164           return kFALSE;
1165         continue;
1166       }
1167           }
1168
1169     fHC->fMCMmax++;
1170
1171     if (fMCM.fADCmax > 0) {
1172       fpPos++;
1173       if (fpPos >= fpEnd) {
1174         fHC->fBufferCorrupted = kTRUE; 
1175         if (fgDebugFlag)  AliDebug(11, Form("Buffer ends in the middle of data"));
1176         return kFALSE;
1177       }
1178
1179       fADCnumber = 0;
1180       for (Int_t iadc = 0; iadc < fMCM.fADCmax; iadc++) {
1181          fADCnumber = fMCM.fADCchannel[iadc];
1182            fExtendedCOL = fTRDfeeParam->GetExtendedPadColFromADC(fMCM.fROB, fMCM.fMCM, fADCnumber);
1183            fCOL = fTRDfeeParam->GetPadColFromADC(fMCM.fROB, fMCM.fMCM, fADCnumber);
1184
1185          if (fADCnumber <= 1 || fADCnumber == fMaxADCgeom - 1)  // if adc number = 0, 1, 20
1186                        fIsShared = kTRUE;
1187                else 
1188            fIsShared = kFALSE;
1189
1190          if (fpPos + fMCM.fSingleADCwords >= fpEnd) {
1191            fHC->fBufferCorrupted = kTRUE;
1192            if (fgDebugFlag) AliDebug(11,"ADC (10 words) expected. Not enough data in the buffer.");
1193            return kFALSE;
1194          }
1195
1196          //if (GetGlobalNTimeBins() < 31){
1197          if (fHC->fTimeBins < 31){
1198            if (DecodeADC(digitsManager, digits, track0, track1, track2, indexes) == kFALSE) {
1199              return kFALSE;
1200            }
1201          }
1202          //else if (GetGlobalNTimeBins() > 32) {
1203          else if (fHC->fTimeBins > 32) {
1204            if (DecodeADCExtended(digitsManager, digits, track0, track1, track2, indexes) == kFALSE) {
1205              return kFALSE;
1206            }
1207          }
1208          else { // nsamples = 31, 32 are not implemented in the TRAP and should never happen  
1209            if (fgWarnError) AliError("nsamples are 31 or 32. These are not implemented in the TRAP and should never happen!");
1210          }
1211
1212       } // iadc
1213     }
1214     else { // if there is no adc activated
1215       fpPos++;
1216     }
1217   } // mcm loop
1218
1219   if (fpPos >= fpEnd) {
1220     fHC->fBufferCorrupted = kTRUE; 
1221     if (fgDebugFlag) AliDebug(11,"We are at the end of buffer. Not enough data in the buffer.");
1222     return kFALSE;
1223   }
1224
1225   return kTRUE;
1226 }
1227
1228 //------------------------------------------------------------
1229 Bool_t AliTRDrawFastStream::DecodeHCheader()
1230 {
1231   //
1232   // decode the half chamber header
1233   // if it fails to decode HC header for both H0 and H1, return kFALSE
1234   //
1235   if (DecodeHCwordH0(fpPos, fHC) == kFALSE)
1236     return kFALSE;
1237
1238   if (fHC->fNExtraWords > 0) {
1239     fpPos++;
1240     if (fpPos < fpEnd) {
1241       if (DecodeHCwordH1(fpPos, fHC) == kFALSE)
1242         return kFALSE;
1243     }
1244     else {
1245       fHC->fBufferCorrupted = kTRUE;
1246       if (fgDebugFlag) AliDebug(11,"Expected HC header word H1. Fail due to buffer END.");
1247       return kFALSE;
1248     }
1249   }
1250
1251   if (fgDebugFlag)  AliDebug(5, DumpHCinfoH0(fHC));
1252   if (fgDebugFlag)  AliDebug(5, DumpHCinfoH1(fHC));
1253
1254   if (IsHCheaderOK() == kFALSE) {
1255     fHC->fH0Corrupted += 2;
1256     if (fgDebugFlag) AliDebug(11,Form("H0 Header Insane. Word 0x%08x", *fHC->fPos));
1257     return kFALSE;
1258   }
1259
1260   return kTRUE;
1261 }
1262
1263 //--------------------------------------------------------
1264 Bool_t AliTRDrawFastStream::DecodeHCwordH0(const UInt_t *word, struct AliTRDrawHC *hc) const
1265 {
1266   //
1267   // decode the hc header word 0
1268   //
1269   UInt_t vword = *word;
1270   hc->fPos[0] = (UInt_t*)word;
1271
1272   hc->fH0Corrupted = HC_HEADER_MASK_ERR(vword);
1273   if (hc->fH0Corrupted > 0) {
1274     if (fgDebugFlag) AliDebug(11,Form("H0 Header Mask Error. Word 0x%08x",*fHC->fPos ));
1275     return kFALSE;
1276   }
1277   hc->fSpecialRawV =  HC_SPECIAL_RAW_VERSION(vword);
1278   hc->fRawVMajor = HC_MAJOR_RAW_VERSION(vword);
1279   hc->fRawVMajorOpt = HC_MAJOR_RAW_VERSION_OPT(vword);
1280   hc->fRawVMinor = HC_MINOR_RAW_VERSION(vword);
1281   hc->fNExtraWords = HC_EXTRA_WORDS(vword);
1282   hc->fDCSboard = HC_DCS_BOARD(vword);
1283   hc->fSMHCheader = HC_SM_NUMBER(vword);
1284   hc->fStackHCheader = HC_STACK_NUMBER(vword);
1285   hc->fLayerHCheader = HC_LAYER_NUMBER(vword);
1286   hc->fSideHCheader = HC_SIDE_NUMBER(vword);
1287
1288   return kTRUE;
1289 }
1290
1291 //--------------------------------------------------------
1292 Bool_t AliTRDrawFastStream::DecodeHCwordH1(const UInt_t *word, struct AliTRDrawHC *hc) const
1293 {
1294   //
1295   // decode the hc header word 1
1296   //
1297   UInt_t vword = *word;
1298   hc->fPos[1] = (UInt_t*)word;
1299
1300   hc->fH1Corrupted = HC_HEADER_MASK_ERR(vword);
1301   if (hc->fH1Corrupted > 0) {
1302     if (fgDebugFlag) AliDebug(11,Form("H1 Header Mask Error. Word 0x%08x", *(fHC->fPos+1) ));
1303     return kFALSE;
1304   }
1305   hc->fTimeBins = HC_NTIMEBINS(vword);
1306   hc->fBunchCrossCounter = HC_BUNCH_CROSS_COUNTER(vword);
1307   hc->fPreTriggerCounter = HC_PRETRIGGER_COUNTER(vword);
1308   hc->fPreTriggerPhase = HC_PRETRIGGER_PHASE(vword);
1309
1310   return kTRUE;
1311 }
1312
1313 //------------------------------------------------------------
1314 Bool_t AliTRDrawFastStream::IsHCheaderOK()
1315 {
1316   //
1317   // check insanity of half chamber header
1318   //
1319   if (fHC->fStackHCheader < 0 || fHC->fStackHCheader > 4) {
1320     if (fgDebugFlag) AliDebug(11,Form("Wrong Stack %d", fHC->fStackHCheader));
1321       return kFALSE;
1322   }
1323
1324   if (fHC->fLayerHCheader < 0 || fHC->fLayerHCheader >= AliTRDgeometry::kNlayer) {
1325     if (fgDebugFlag) AliDebug(11,Form("Wrong layer %d", fHC->fLayerHCheader));
1326       return kFALSE;
1327   }
1328
1329   if (fHC->fSideHCheader < 0 || fHC->fSideHCheader > 1) {
1330     if (fgDebugFlag) AliDebug(11,Form("Wrong Side %d", fHC->fSideHCheader));
1331       return kFALSE;
1332   } 
1333     
1334   if (fHC->fSMHCheader != fHC->fSM) {
1335     if (fgDebugFlag) AliDebug(11,Form("Missmatch: SM number between HC header %d and GTU link mask %d",
1336                                       fHC->fSMHCheader, fHC->fSM));
1337     return kFALSE;
1338   }
1339
1340   if (fgStackNumberChecker) {
1341     if (fHC->fStackHCheader != fHC->fStack) {
1342       if (fgDebugFlag) AliDebug(11,Form("Missmatch: Stack number between HC header %d and GTU link mask %d",
1343                                         fHC->fStackHCheader, fHC->fStack));
1344       return kFALSE;
1345     }
1346   }
1347
1348   if (fgStackLinkNumberChecker) {
1349     if (fHC->fLayerHCheader * 2 + fHC->fSideHCheader != fHC->fLayer * 2 + fHC->fSide) {
1350       if (fgDebugFlag) AliDebug(11,Form("Missmatch: Layer number between HC header %d and GTU link mask %d | %s",
1351                                         fHC->fLayerHCheader, fHC->fLayer, DumpStackInfo(fStack)));
1352       return kFALSE;
1353     }
1354   }
1355
1356   // SLOW GEOM : consistancy check with geometry
1357   if (fHC->fDET < 0 || fHC->fDET >= AliTRDgeometry::kNdet) {
1358     if (fgDebugFlag) AliDebug(11,Form("Wrong detector %d", fHC->fDET));
1359     if (fRawReader) fRawReader->AddMajorErrorLog(kHCHeaderWrongDet, "Wrong Det");
1360     return kFALSE;
1361   }
1362
1363   if (fHC->fSM != fGeometry->GetSector(fHC->fDET) || fHC->fSM <0 || fHC->fSM >= AliTRDgeometry::kNsector) {
1364     if (fgDebugFlag) AliDebug(11,Form("Wrong SM(sector) %d (Geometry says: %d) Stack=%d Layer=%d Det=%d",
1365                                       fHC->fSM, fGeometry->GetSector(fHC->fDET), fHC->fStack, fHC->fLayer, fHC->fDET));
1366     if (fRawReader) fRawReader->AddMajorErrorLog(kHCHeaderWrongSM, "Wrong SM");
1367     return kFALSE;
1368   }
1369
1370   if (fHC->fROC < 0) {
1371     if (fRawReader) fRawReader->AddMajorErrorLog(kHCHeaderWrongROC, "Wrong ROC");
1372     return kFALSE;
1373   }
1374
1375   if (fHC->fRowMax < 1) {
1376     if (fRawReader) fRawReader->AddMajorErrorLog(kHCHeaderWrongROC, "Wrong ROC Row Max");
1377     return kFALSE;
1378   }
1379
1380   if (fHC->fColMax < 1) {
1381     if (fRawReader) fRawReader->AddMajorErrorLog(kHCHeaderWrongROC, "Wrong ROC Col Max");
1382     return kFALSE;
1383   }
1384
1385   return kTRUE;
1386 }
1387
1388 //------------------------------------------------------------
1389 Bool_t AliTRDrawFastStream::DecodeMCMheader()
1390 {   
1391   //
1392   // decode the mcm header
1393   //
1394   DecodeMCMheader(fpPos, &fMCM);
1395
1396   if (fHC->fEOTECorrupted == kTRUE) {
1397     fpPos--;
1398     return kFALSE;
1399   }
1400
1401   fMCM.fROW = fTRDfeeParam->GetPadRowFromMCM(fMCM.fROB, fMCM.fMCM);
1402
1403   if ((fHC->fRawVMajor > 2 && fHC->fRawVMajor <5) || ((fHC->fRawVMajor & 32) == 32)) { //cover old and new version definition of ZS data
1404     fpPos++;
1405     if ( fpPos < fpEnd ) {
1406       DecodeMask(fpPos, &fMCM);
1407       if (fHC->fEOTECorrupted == kTRUE) {
1408         fpPos--;
1409         return kFALSE;
1410       }
1411       MCMADCwordsWithTbins(fHC->fTimeBins, &fMCM);
1412       fMCM.fAdcDataPos = fpPos + 1;
1413     }
1414     else {
1415       if (fgDebugFlag) AliDebug(11,"Expected ADC mask word. Fail due to buffer END.");
1416       if (fRawReader) fRawReader->AddMajorErrorLog(kMCMADCMaskMissing,"Missing");
1417       fHC->fBufferCorrupted = kTRUE;
1418       return kFALSE;
1419     }
1420   }
1421   else {
1422     UInt_t dummyMask = MCM_DUMMY_ADCMASK_VAL;
1423     DecodeMask(&dummyMask, &fMCM);
1424     MCMADCwordsWithTbins(fHC->fTimeBins, &fMCM);
1425     fMCM.fAdcDataPos = fpPos + 1;
1426   }
1427   if (IsMCMheaderOK() == kFALSE)
1428       return kFALSE;
1429
1430   return kTRUE;
1431 }
1432
1433 //--------------------------------------------------------
1434 void AliTRDrawFastStream::DecodeMCMheader(const UInt_t *word, struct AliTRDrawMCM *mcm) const
1435 {
1436   //
1437   // decode the mcm header
1438   //
1439   UInt_t vword = *word;
1440
1441   if (vword == END_OF_TRACKLET_MARKERNEW) {
1442     if (fgWarnError) AliError(Form("There should be MCM header. We meet END_OF_TRACKLET_MARKER 0x%08x",vword));
1443     fHC->fEOTECorrupted = kTRUE; //to finish data reading of this HC
1444   }
1445
1446   mcm->fMCMhdCorrupted = MCM_HEADER_MASK_ERR(vword); //if MCM header mask has error
1447   if (fgDebugFlag && mcm->fMCMhdCorrupted != 0) { 
1448     fHC->fDataCorrupted = kTRUE;
1449     AliDebug(11,Form("Wrong MCM header mask 0x%08x.\n", *fpPos));
1450   }
1451
1452   mcm->fROB = MCM_ROB_NUMBER(vword);
1453   mcm->fMCM = MCM_MCM_NUMBER(vword);
1454   mcm->fEvCounter = MCM_EVENT_COUNTER(vword);
1455   mcm->fPos = (UInt_t*)word;
1456 }
1457
1458 //--------------------------------------------------------
1459 UInt_t AliTRDrawFastStream::GetMCMadcMask(const UInt_t *word, struct AliTRDrawMCM *mcm) const
1460 {
1461   //
1462   // get the adc mask
1463   //
1464   UInt_t vword = *word;
1465
1466   mcm->fADCMask   = 0;
1467   mcm->fADCcount  = 0;
1468   mcm->fADCMaskWord = vword;
1469
1470   if (vword == END_OF_TRACKLET_MARKERNEW) {
1471     if (fgWarnError) AliError(Form("There should be MCMadcMask. We meet END_OF_TRACKLET_MARKER 0x%08x",vword));
1472     fHC->fEOTECorrupted = kTRUE; //to finish data reading of this HC
1473   }
1474
1475   if ( MCM_ADCMASK_MASK_ERR(vword) == 0 ) {
1476     mcm->fADCMask  = MCM_ADCMASK_VAL(vword);
1477     mcm->fADCcount = MCM_ADCMASK_NADC(~vword);
1478   }
1479   else {
1480     mcm->fADCMask = 0xffffffff;
1481     mcm->fADCmaskCorrupted = 1; // mcm adc mask error
1482     fHC->fDataCorrupted = kTRUE;
1483     if (fgDebugFlag) AliDebug(11,Form("Wrong ADC Mask word 0x%08x.\n", *fpPos));
1484   }
1485
1486   return mcm->fADCMask;
1487 }
1488
1489 //--------------------------------------------------------
1490 void AliTRDrawFastStream::DecodeMask(const UInt_t *word, struct AliTRDrawMCM *mcm) const
1491 {
1492   //
1493   // decode the adc mask - adcs to be read out
1494   //
1495   mcm->fMCMADCWords = 0;
1496   mcm->fSingleADCwords = 0;
1497   mcm->fADCmax = 0;
1498   mcm->fADCMask = GetMCMadcMask(word, mcm);
1499
1500   if (mcm->fADCMask > 0) {
1501     for (Int_t i = 0; i < TRDMAXADC; i++) {
1502        mcm->fADCchannel[mcm->fADCmax] = 0;
1503        if ( IS_BIT_SET(mcm->fADCMask,i) ) {
1504          mcm->fADCchannel[mcm->fADCmax] = i;
1505          mcm->fADCmax++;
1506        }
1507     }
1508   }
1509   if (mcm->fADCcount != mcm->fADCmax && fHC->fRawVMajor >= 32) { // backward compatibility
1510     mcm->fADCmaskCorrupted += 2;
1511     fHC->fDataCorrupted = kTRUE;
1512     if (fgDebugFlag) AliDebug(11,Form("ADC counts from ADCMask are different %d %d : ADCMask word 0x%08x\n", 
1513                                       mcm->fADCcount, mcm->fADCmax, *fMCM.fPos));
1514   }
1515 }
1516
1517 //--------------------------------------------------------
1518 void AliTRDrawFastStream::MCMADCwordsWithTbins(UInt_t fTbins, struct AliTRDrawMCM *mcm) const
1519 {
1520   //
1521   //  count the expected mcm words for a given tbins
1522   //
1523   mcm->fMCMADCWords = ( mcm->fADCmax ) * ( fTbins / 3 );
1524   mcm->fSingleADCwords = 0;
1525   if (mcm->fADCmax > 0) {
1526     mcm->fSingleADCwords = mcm->fMCMADCWords/mcm->fADCmax;
1527   }
1528   if (fTbins > 32) mcm->fSingleADCwords = 10; // if the timebin is more than 30, then fix the number of adc words to 10
1529 }
1530
1531 //------------------------------------------------------------
1532 Bool_t AliTRDrawFastStream::IsMCMheaderOK()
1533 {
1534   //
1535   // check the mcm header
1536   //
1537   if (fgLastROB != fMCM.fROB) {
1538     fgLastIndex = 0;
1539     if (fgLastROB== -1) fgLastROB = fMCM.fROB;
1540   }
1541   else {
1542     Int_t matchingcounter = 0; 
1543     for (Int_t i=fgLastIndex+1; i<16; i++) {
1544        if ( fMCM.fMCM == fgMCMordering[i] ) {
1545          fgLastIndex = i;
1546          matchingcounter++;
1547          break;
1548        }
1549     }
1550     if (matchingcounter == 0) {
1551       fMCM.fMCMhdCorrupted += 2;
1552       AliDebug(11,Form("MCM number from last MCM is larger: MCM # from current MCM %d \n", fMCM.fMCM));
1553     }
1554   } 
1555   
1556   if ( fgLastHC == fHC->fLayer*2 + fHC->fSide ) {
1557     if ( fMCM.fROB < fgLastROB ) {
1558       if((fMCM.fMCMhdCorrupted & 2) == 0) fMCM.fMCMhdCorrupted += 2;
1559       AliDebug(11,Form("ROB number from last MCM is larger: ROB # from current MCM %d \n", fMCM.fROB));
1560     }
1561     else fgLastROB = fMCM.fROB;
1562   }
1563
1564   fgLastHC = fHC->fLayer*2 + fHC->fSide;
1565
1566   if (fEventCounter == 0) {
1567     fEventCounter = fMCM.fEvCounter;
1568   }
1569
1570   if (fEventCounter != fMCM.fEvCounter) {
1571     fMCM.fMCMhdCorrupted += 4;
1572     if (fgDebugFlag) AliDebug(11,Form("Event number(%d) of current MCM is different from that(%d) of reference MCM %s.\n"
1573                                       , fMCM.fEvCounter, fEventCounter, DumpMCMinfo(&fMCM)));
1574   }
1575
1576   if (fEventCounter < fLastEventCounter) {
1577     fMCM.fMCMhdCorrupted += 8;
1578     if (fgDebugFlag) AliDebug(11,Form("Event from the past? Current %d Last %d %s.\n", fEventCounter, fLastEventCounter, DumpMCMinfo(&fMCM)));
1579   }
1580
1581   if ( fMCM.fADCmaskCorrupted > 0 )
1582     return kFALSE;
1583
1584   if ( fMCM.fMCMhdCorrupted > 0 )
1585     return kFALSE;
1586
1587   return kTRUE;
1588 }
1589
1590 //------------------------------------------------------------
1591 Bool_t AliTRDrawFastStream::DecodeADC(AliTRDdigitsManager *digitsManager, AliTRDarrayADC *digits, 
1592                               AliTRDarrayDictionary *track0, AliTRDarrayDictionary *track1, AliTRDarrayDictionary *track2, 
1593                               AliTRDSignalIndex *indexes)
1594 {
1595   //
1596   // decode single ADC channel
1597   //
1598   if(fADCnumber%2==1) fMaskADCword = ADC_WORD_MASK(ADCDATA_VAL1);
1599   if(fADCnumber%2==0) fMaskADCword = ADC_WORD_MASK(ADCDATA_VAL2);
1600
1601   fTbinADC = 0;
1602   Bool_t isWritten = kFALSE;
1603
1604   for (Int_t iw = 0; iw < fMCM.fSingleADCwords; iw++) {
1605      if (HC_HEADER_MASK_ERR(*fpPos) == 0 || *fpPos == END_OF_TRACKLET_MARKERNEW) {
1606        if (fgWarnError) AliError(Form("There should be ADC data. We meet HC header or END_OF_TRACKLET_MARKER 0x%08x",*fpPos));
1607        fHC->fEOTECorrupted = kTRUE; 
1608        fpPos--;
1609        return kFALSE;
1610      }
1611      if (fMaskADCword != ADC_WORD_MASK(*fpPos)) {
1612        if (fgDebugFlag) AliDebug(11,Form("Wrong ADC data mask! [Expected mask: 0x%08x  Current mask: 0x%08x] ADC channel number: %02d MCM= %s ",
1613                                          fMaskADCword, ADC_WORD_MASK(*fpPos), fADCnumber, DumpMCMinfo(&fMCM)));
1614       // encode adc level error codes
1615        Int_t index = 21*(fMCM.fMCM + 16*int(fMCM.fROB/2)) + fADCnumber;
1616        fHC->fErrorCodes[index+66] += 1; 
1617        if (!isWritten) { 
1618          fHC->fErrorCodes[index+66] += (fADCnumber << 4);; 
1619          fHC->fErrorCodes[index+66] += (fMCM.fMCM << 9);; 
1620          fHC->fErrorCodes[index+66] += (fMCM.fROB << 13);; 
1621          isWritten = kTRUE; 
1622        }
1623        fMCM.fDataCorrupted = kTRUE;
1624        fHC->fDataCorrupted = kTRUE;
1625        fpPos++;
1626        continue;
1627      }
1628      // decode and put into the digit container
1629      Int_t adcSignals[3];
1630      adcSignals[0] = ((*fpPos & 0x00000ffc) >>  2) - fgCommonAdditive;
1631      adcSignals[1] = ((*fpPos & 0x003ff000) >> 12) - fgCommonAdditive;
1632      adcSignals[2] = ((*fpPos & 0xffc00000) >> 22) - fgCommonAdditive;
1633
1634      if(GetCol() < 0 || (!fSharedPadsOn & fIsShared)) {fpPos++; continue;};     
1635      for (Int_t i = 0; i < 3; i++) {
1636         if (adcSignals[i] > 0) { 
1637                 if (fSharedPadsOn) 
1638             digits->SetDataByAdcCol(GetRow(), GetExtendedCol(), fTbinADC + i, adcSignals[i]);
1639                 else 
1640             digits->SetData(GetRow(), GetCol(), fTbinADC + i, adcSignals[i]);
1641                 indexes->AddIndexRC(GetRow(), GetCol());
1642               } 
1643         if (digitsManager->UsesDictionaries()) {
1644           track0->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1645           track1->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1646           track2->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1647         }
1648      } // i
1649      fTbinADC += 3;
1650      fpPos++;
1651   } // iw
1652
1653   return kTRUE;
1654 }
1655
1656 //------------------------------------------------------------
1657 Bool_t AliTRDrawFastStream::DecodeADCExtended(AliTRDdigitsManager *digitsManager, AliTRDarrayADC *digits, 
1658                               AliTRDarrayDictionary *track0, AliTRDarrayDictionary *track1, AliTRDarrayDictionary *track2, 
1659                               AliTRDSignalIndex *indexes)
1660 {
1661   //
1662   // decode single ADC channel
1663   //
1664   if(fADCnumber%2==1) fMaskADCword = ADC_WORD_MASK(ADCDATA_VAL1);
1665   if(fADCnumber%2==0) fMaskADCword = ADC_WORD_MASK(ADCDATA_VAL2);
1666
1667   Bool_t isWritten = kFALSE; //for error code recording
1668
1669   fTbinADC = ((*fpPos & 0x000000fc) >>  2);
1670   fMCM.fSingleADCwords  = ((*fpPos & 0x00000f00) >>  8);
1671   
1672   Int_t adcFirst2Signals[2];
1673   adcFirst2Signals[0] = ((*fpPos & 0x003ff000) >> 12) - fgCommonAdditive;
1674   adcFirst2Signals[1] = ((*fpPos & 0xffc00000) >> 22) - fgCommonAdditive;
1675
1676   for (Int_t i = 0; i < 2; i++) {
1677      if (adcFirst2Signals[i] > 0) {
1678        if (fSharedPadsOn)
1679          digits->SetDataByAdcCol(GetRow(), GetExtendedCol(), fTbinADC + i, adcFirst2Signals[i]);
1680        else
1681          digits->SetData(GetRow(), GetCol(), fTbinADC + i, adcFirst2Signals[i]);
1682          indexes->AddIndexRC(GetRow(), GetCol());
1683      }
1684      if (digitsManager->UsesDictionaries()) {
1685        track0->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1686        track1->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1687        track2->SetData(GetRow(), GetCol(), fTbinADC + i, 0);
1688      }
1689   } // i
1690
1691   fpPos++;
1692   for (Int_t iw = 0; iw < fMCM.fSingleADCwords-1; iw++) {
1693      if (HC_HEADER_MASK_ERR(*fpPos) == 0 || *fpPos == END_OF_TRACKLET_MARKERNEW) {
1694        if (fgWarnError) AliError(Form("There should be ADC data. We meet HC header or END_OF_TRACKLET_MARKER 0x%08x",*fpPos));
1695        fHC->fEOTECorrupted = kTRUE; 
1696        fpPos--;
1697        return kFALSE;
1698      }
1699      if (fMaskADCword != ADC_WORD_MASK(*fpPos)) {
1700        if (fgDebugFlag) AliDebug(11,Form("Wrong ADC data mask! [Expected mask: 0x%08x  Current mask: 0x%08x] ADC channel number: %02d MCM= %s ",
1701                                          fMaskADCword, ADC_WORD_MASK(*fpPos), fADCnumber, DumpMCMinfo(&fMCM)));
1702       // encode adc level error codes
1703        Int_t index = 21*(fMCM.fMCM + 16*int(fMCM.fROB/2)) + fADCnumber;
1704        fHC->fErrorCodes[index+66] += 1; 
1705        if (!isWritten) { 
1706          fHC->fErrorCodes[index+66] += (fADCnumber << 4);; 
1707          fHC->fErrorCodes[index+66] += (fMCM.fMCM << 9);; 
1708          fHC->fErrorCodes[index+66] += (fMCM.fROB << 13);; 
1709          isWritten = kTRUE; 
1710        }
1711        fMCM.fDataCorrupted = kTRUE;
1712        fHC->fDataCorrupted = kTRUE;
1713        fpPos++;
1714        continue;
1715      }
1716      // decode and put into the digit container
1717      Int_t adcSignals[3];
1718      adcSignals[0] = ((*fpPos & 0x00000ffc) >>  2) - fgCommonAdditive;
1719      adcSignals[1] = ((*fpPos & 0x003ff000) >> 12) - fgCommonAdditive;
1720      adcSignals[2] = ((*fpPos & 0xffc00000) >> 22) - fgCommonAdditive;
1721
1722      if(GetCol() < 0 || (!fSharedPadsOn & fIsShared)) {fpPos++; continue;};     
1723      for (Int_t i = 0; i < 3; i++) {
1724         if (adcSignals[i] > 0) { 
1725                 if (fSharedPadsOn) 
1726             digits->SetDataByAdcCol(GetRow(), GetExtendedCol(), fTbinADC + 2 + i, adcSignals[i]);
1727                 else 
1728             digits->SetData(GetRow(), GetCol(), fTbinADC + 2 + i, adcSignals[i]);
1729                 indexes->AddIndexRC(GetRow(), GetCol());
1730               } 
1731         if (digitsManager->UsesDictionaries()) {
1732           track0->SetData(GetRow(), GetCol(), fTbinADC + 2 + i, 0);
1733           track1->SetData(GetRow(), GetCol(), fTbinADC + 2 + i, 0);
1734           track2->SetData(GetRow(), GetCol(), fTbinADC + 2 + i, 0);
1735         }
1736      } // i
1737      fTbinADC += 3;
1738      fpPos++;
1739   } // iw
1740
1741   return kTRUE;
1742 }
1743
1744 //------------------------------------------------------------
1745 Bool_t AliTRDrawFastStream::SeekEndOfData()
1746 {
1747   //
1748   // go to end of data marker
1749   //
1750   Int_t fEndOfDataCount = 0;
1751   fNWordsCounter = 0;
1752
1753   while ( *fpPos != ENDOFRAWDATAMARKER && fpPos < fpEnd ) {
1754     fpPos++;
1755     fNWordsCounter++;
1756   }
1757   while (*fpPos == ENDOFRAWDATAMARKER && fpPos < fpEnd ) {
1758     fEndOfDataCount++;
1759     fpPos++;
1760   }
1761
1762   return kTRUE;
1763 }
1764
1765 //------------------------------------------------------------
1766 Bool_t AliTRDrawFastStream::SeekNextMCMheader()
1767 {
1768   //
1769   // go to mcm marker
1770   //
1771   fpPos++;
1772
1773   while ( *fpPos != ENDOFRAWDATAMARKER && fpPos < fpEnd ) {
1774     if (MCM_HEADER_MASK_ERR(*fpPos) == 0 && MCM_HEADER_MASK_ERR(*(fpPos+1)) == 0) {
1775       if (fgDebugFlag) AliDebug(11,Form("Found : Pos 0x%08x : Val 0x%08x", fpPos, *fpPos));
1776       return kTRUE;
1777     }
1778     if ( *fpPos == END_OF_TRACKLET_MARKERNEW) {
1779       fHC->fEOTECorrupted = kTRUE;
1780       return kFALSE;
1781     }
1782     fpPos++;
1783   }
1784
1785   SeekEndOfData();
1786   return kFALSE;
1787 }
1788
1789 //------------------------------------------------------------
1790 Bool_t AliTRDrawFastStream::SkipMCMdata(UInt_t iw)
1791 {
1792   //
1793   // skip mcm data words due to corruption
1794   //
1795   if (fgDebugFlag) AliDebug(11,Form("Skip %d words due to MCM header corruption.",iw));
1796   UInt_t iwcounter = 0;
1797   while ( *fpPos != ENDOFRAWDATAMARKER && iwcounter < iw) {
1798     if ( *fpPos == END_OF_TRACKLET_MARKERNEW) {
1799       if (fgDebugFlag) AliDebug(11,"Met END_OF_TRACKLET_MARKERNEW");
1800       fHC->fEOTECorrupted = kTRUE;
1801       return kFALSE;
1802     }
1803     fpPos++;
1804     iwcounter++;
1805   } // while
1806
1807   if (iwcounter == iw) {
1808     fpPos++;
1809     return kTRUE;
1810   }
1811
1812   if (fgDebugFlag) AliDebug(11,"Met ENDOFRAWDATAMARKER");
1813   return kFALSE;
1814 }
1815
1816 //------------------------------------------------------------
1817 Bool_t AliTRDrawFastStream::SetGlobalNTimebins()
1818 {
1819   Int_t nHCs=0;
1820   while (SetNTimebins()==kFALSE){
1821     if (fgDebugFlag) AliDebug(11,Form("Failed to get number of time bin information from the %sth HC",nHCs));
1822     nHCs++;
1823   }
1824
1825   return kTRUE;
1826 }
1827
1828 //------------------------------------------------------------
1829 Bool_t AliTRDrawFastStream::SetNTimebins()
1830 {
1831   // goes to the HC header position
1832   while (!(*fpPosTemp == END_OF_TRACKLET_MARKERNEW) && fpPosTemp < fpEnd) {
1833     fpPosTemp++;
1834   }
1835   while (*fpPosTemp == END_OF_TRACKLET_MARKERNEW) {
1836     fpPosTemp++;
1837   }
1838   // skip H0 
1839   fpPosTemp++;
1840
1841   UInt_t vword = *fpPosTemp;
1842
1843   // get the number of time bins 
1844   if (HC_HEADER_MASK_ERR(vword) == 0) {
1845     fGlobalNTimeBins = HC_NTIMEBINS(vword);
1846     if (fGlobalNTimeBins > 64 || fGlobalNTimeBins < 10) return kFALSE; // minimal protection
1847   }
1848   else
1849     return kFALSE;
1850
1851   return kTRUE;
1852 }
1853
1854 //------------------------------------------------------------
1855 Bool_t AliTRDrawFastStream::DumpWords(UInt_t *px, UInt_t iw, UInt_t marker)
1856 {
1857   //
1858   // dump given number of words for debugging
1859   //
1860   TString tsreturn = Form("\n[ Dump Sequence at 0x%08x ] : ", px);
1861   for (UInt_t i = 0; i < iw; i++) {
1862      if ( iw != 0 && px + iw > fpEnd) 
1863        return kFALSE;
1864      if (i % 8 == 0) tsreturn += "\n                              ";
1865      if (marker != 0 && marker == px[i]) tsreturn += Form(" *>0x%08x<* ", px[i]);
1866      else tsreturn += Form("0x%08x ", px[i]);
1867   }
1868   tsreturn += "\n";
1869
1870   AliInfo(tsreturn.Data());
1871
1872   return kTRUE;
1873 }
1874
1875 //--------------------------------------------------------
1876 const char *AliTRDrawFastStream::DumpSMInfo(const struct AliTRDrawSM *sm)
1877 {
1878   //
1879   // format the string with the sm info
1880   //
1881   return Form("[ SM Info 0x%08x] : Hsize %d TrackletEnable %d Stacks %d %d %d %d %d",
1882               *sm->fPos, sm->fHeaderSize, sm->fTrackletEnable,
1883               sm->fStackActive[0], sm->fStackActive[1], sm->fStackActive[2],
1884               sm->fStackActive[3], sm->fStackActive[4]);      
1885 }
1886
1887 //--------------------------------------------------------
1888 const char *AliTRDrawFastStream::DumpStackInfo(const struct AliTRDrawStack *st)
1889 {
1890   //
1891   // format the string with the stack info
1892   //
1893   return Form("[ Stack Info 0x%08x ] : Hsize %d Links Active %d %d %d %d %d %d %d %d %d %d %d %d",
1894               *st->fPos, st->fHeaderSize,
1895               st->fLinksActive[0], st->fLinksActive[1], st->fLinksActive[2], st->fLinksActive[3],
1896               st->fLinksActive[4], st->fLinksActive[5], st->fLinksActive[6], st->fLinksActive[7],
1897               st->fLinksActive[8], st->fLinksActive[9], st->fLinksActive[10], st->fLinksActive[11]);
1898
1899 }
1900 //--------------------------------------------------------
1901 const char *AliTRDrawFastStream::DumpHCinfoH0(const struct AliTRDrawHC *hc)
1902 {
1903   //
1904   // dump the hc header word 0 in strings
1905   //
1906   if (!hc)
1907     return Form("Unable to dump. Null received as parameter!?!");
1908   else
1909     return Form("[ HC[0] at 0x%08x ] : 0x%08x Info is : RawV %d SM %d Stack %d Layer %d Side %d DCSboard %d",
1910                 hc->fPos[0], *(hc->fPos[0]), hc->fRawVMajor, hc->fSM, hc->fStack, hc->fLayer, hc->fSide, hc->fDCSboard);
1911 }
1912
1913 //--------------------------------------------------------
1914 const char *AliTRDrawFastStream::DumpHCinfoH1(const struct AliTRDrawHC *hc)
1915 {
1916   //
1917   // dump the hc header word 1 in strings
1918   //
1919   if (!hc)
1920     return Form("Unable to dump. Null received as parameter!?!");
1921   else
1922     return Form("[ HC[1] at 0x%08x ] : 0x%08x Info is : TBins %d BCcount %d PreTrigCount %d PreTrigPhase %d",
1923                 hc->fPos[1], *(hc->fPos[1]), hc->fTimeBins, hc->fBunchCrossCounter, hc->fPreTriggerCounter, hc->fPreTriggerPhase);
1924 }
1925
1926 //--------------------------------------------------------
1927 const char *AliTRDrawFastStream::DumpMCMinfo(const struct AliTRDrawMCM *mcm)
1928 {
1929   //
1930   // dump mcm info in strings
1931   //
1932   if (!mcm)
1933     return Form("Unable to dump. Null received as parameter!?!");
1934   else
1935     return Form("[ MCM 0x%08x ] : ROB %d MCM %d EvCounter %d", *(mcm->fPos), mcm->fROB, mcm->fMCM, mcm->fEvCounter);
1936 }
1937
1938 //--------------------------------------------------------
1939 const char *AliTRDrawFastStream::DumpMCMadcMask(const struct AliTRDrawMCM *mcm)
1940 {
1941   //
1942   // mcm adc mask in strings
1943   //
1944   if (!mcm)
1945     return Form("Unable to dump. Null received as parameter!?!");
1946
1947   TString tsreturn = Form("[Word] : 0x%08x => [Mask] : 0x%08x : ", mcm->fADCMaskWord, mcm->fADCMask);
1948   for (Int_t i = 0; i < TRDMAXADC; i++) {
1949      tsreturn += Form("%d ", mcm->fADCchannel[i]);
1950   }
1951   tsreturn += "";
1952   return tsreturn.Data();
1953 }
1954
1955