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