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