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