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