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