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