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