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