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