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