]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFRawStream.cxx
Decode and LoadRawDataBuffers methods updated and static member fgApplyBCCorrections...
[u/mrichter/AliRoot.git] / TOF / AliTOFRawStream.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 /*
17 $Log$
18 Revision 1.19.1  2008/09/19  preghenella
19   Decode method updated:
20   it reads the CDH from the rawReader and sends it to the decoder;
21  LoadRawDataBuffers modified:
22      it corrects tof hit infos per ddlBC and deltaBC offsets
23      (in case of the static member fgApplyBCCorrections
24       has been setted to kTRUE);
25  Added static member fgApplyBCCorrections (kTRUE by default)
26  and the related static method ApplyBCCorrections;
27
28 Revision 1.19  2007/05/18 13:07:53  decaro
29 Error messages stored in the global raw-reader error log (Cvetan, Chiara)
30
31 Revision 1.18  2007/05/08 11:53:29  arcelli
32 Improved class flexibility for further use (R.Preghenella)
33
34 Revision 1.17  2007/05/03 08:53:50  decaro
35 Coding convention: RS3 violation -> suppression
36
37 Revision 1.16  2007/05/03 08:22:22  decaro
38 Coding convention: RN17 violation -> suppression
39
40 Revision 1.15  2007/04/30 15:22:06  arcelli
41 Change TOF digit Time, Tot etc to int type
42
43 Revision 1.14  2007/04/27 11:11:53  arcelli
44 updates for the new decoder
45
46 Revision 1.13  2007/03/16 11:46:35  decaro
47 Coding convention: RN17 rule violation -> suppression
48
49 Revision 1.12  2007/02/22 09:43:45  decaro
50 Added AliTOFRawStream::GetIndex method for online calibration (C.Zampolli)
51
52 Revision 1.11  2007/02/20 15:57:00  decaro
53 Raw data update: to read the TOF raw data defined in UNPACKED mode
54
55 Revision 1.10  2006/12/15 14:01:38  cvetan
56 Memory leak fixed
57
58 Revision 1.9  2006/10/13 11:22:27  arcelli
59 remove warnings due to uninitialized AliTOFtdcDigit data members
60
61 Revision 1.8  2006/08/22 13:30:17  arcelli
62 removal of effective c++ warnings (C.Zampolli)
63
64 Revision 1.7  2006/08/10 14:46:54  decaro
65 TOF raw data format: updated version
66
67 Revision 1.6.1  2006/06/28 A. De Caro, R. Preghenella:
68         Update TOF raw data format
69         according to the final version
70         (see the ALICE internal note in preparation
71          'ALICE TOF raw data format')
72         Added the methods for the correspoonding numbering
73          between the equipment IDs and the volume IDs:
74            Equip2VolNPlate(...)
75            Equip2VolNStrip(...)
76            Equip2VolNPad(...)
77
78 Revision 0.02  2005/07/28 A. De Caro:
79         Update format TOF raw data
80                (temporary solution) 
81         Correction of few wrong corrispondences
82                between 'software' and 'hardware' numberings
83
84 Revision 0.01  2005/07/22 A. De Caro
85         Implement methods Next()
86                           GetSector(),
87                           GetPlate(),
88                           GetStrip(),
89                           GetPadZ(),
90                           GetPadX()
91 */
92
93 ////////////////////////////////////////////////////////////////////////
94 //                                                                    //
95 //     This class provides access to TOF raw data in DDL files.       //
96 //                                                                    //
97 //      It loops over all TOF raw data given by the AliRawReader.     //
98 //                                                                    //
99 ////////////////////////////////////////////////////////////////////////
100
101
102 #include "Riostream.h"
103
104 #include "TClonesArray.h"
105
106 #include "AliDAQ.h"
107 #include "AliLog.h"
108 #include "AliRawReader.h"
109
110 #include "AliTOFGeometry.h"
111 #include "AliTOFrawData.h"
112 #include "AliTOFRawMap.h"
113 #include "AliTOFRawStream.h"
114 //#include "AliTOFCableLengthMap.h"
115
116 #include "AliTOFHitData.h"
117
118 #include "AliRawEventHeaderBase.h"
119
120 ClassImp(AliTOFRawStream)
121
122 const Int_t AliTOFRawStream::fgkddlBCshift[72] = 
123 {
124   2, 2, -1, -1,
125   2, 2,  0,  0,
126   2, 2,  0,  0,
127   2, 2,  0,  0,
128   2, 2,  0,  0,
129   2, 2,  0,  0,
130   2, 2,  0,  0,
131   2, 2,  0,  0,
132   2, 2,  0,  0,
133   2, 2,  0,  0,
134   2, 2, -1, -1,
135   2, 2, -1, -1,
136   2, 2, -2, -2,
137   2, 2, -2, -2,
138   2, 2, -2, -2,
139   2, 2, -1, -1,
140   2, 2, -1, -1,
141   2, 2, -1, -1
142 };
143
144 Bool_t AliTOFRawStream::fgApplyBCCorrections = kTRUE;
145 //_____________________________________________________________________________
146 AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
147   fRawReader(rawReader),
148   fTOFrawData(0x0),
149   fDecoder(new AliTOFDecoder()),
150   fDDL(-1),
151   fTRM(-1),
152   fTRMchain(-1),
153   fTDC(-1),
154   fTDCchannel(-1),
155   fTime(-1),
156   fToT(-1),
157   fLeadingEdge(-1),
158   fTrailingEdge(-1),
159   fErrorFlag(-1),
160   fSector(-1),
161   fPlate(-1),
162   fStrip(-1),
163   fPadX(-1),
164   fPadZ(-1),
165   fPackedDigits(0),
166   fWordType(-1),
167   fSlotID(-1),
168   fACQ(-1),
169   fPSbit(-1),
170   fTDCerrorFlag(-1),
171   fInsideDRM(kFALSE),
172   fInsideTRM(kFALSE),
173   fInsideLTM(kFALSE),
174   fInsideTRMchain0(kFALSE),
175   fInsideTRMchain1(kFALSE),
176   fLocalEventCounterDRM(-1),
177   fLocalEventCounterLTM(-1),
178   fLocalEventCounterTRM(0x0),
179   fLocalEventCounterChain(0x0),
180   fCableLengthMap(0x0),
181   fEventID(0)
182 {
183   //
184   // create an object to read TOF raw digits
185   //
186
187   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
188     fDataBuffer[i]=new AliTOFHitDataBuffer();
189     fPackedDataBuffer[i]=new AliTOFHitDataBuffer();
190   }
191
192   fTOFrawData = new TClonesArray("AliTOFrawData",1000);
193   fTOFrawData->SetOwner();
194
195   fRawReader->Reset();
196   fRawReader->Select("TOF");
197
198   fLocalEventCounterTRM = new Int_t[13];//adc
199   fLocalEventCounterChain = new Int_t*[13];//adc
200   for (Int_t j=0;j<13;j++){//adc
201     fLocalEventCounterTRM[j] = -1;//adc
202     fLocalEventCounterChain[j] = new Int_t[2];//adc
203     for (Int_t k=0;k<2;k++){//adc
204       fLocalEventCounterChain[j][k] = -1;//adc
205     }//adc
206   }//adc
207
208   fCableLengthMap = new AliTOFCableLengthMap();
209
210   fEventID = fRawReader->GetBCID(); //bunch crossing
211 }
212
213 //_____________________________________________________________________________
214 AliTOFRawStream::AliTOFRawStream():
215   fRawReader(0x0), 
216   fTOFrawData(0x0),
217   fDecoder(new AliTOFDecoder()),
218   fDDL(-1),
219   fTRM(-1),
220   fTRMchain(-1),
221   fTDC(-1),
222   fTDCchannel(-1),
223   fTime(-1),
224   fToT(-1),
225   fLeadingEdge(-1),
226   fTrailingEdge(-1),
227   fErrorFlag(-1),
228   fSector(-1),
229   fPlate(-1),
230   fStrip(-1),
231   fPadX(-1),
232   fPadZ(-1),
233   fPackedDigits(0),
234   fWordType(-1),
235   fSlotID(-1),
236   fACQ(-1),
237   fPSbit(-1),
238   fTDCerrorFlag(-1),
239   fInsideDRM(kFALSE),
240   fInsideTRM(kFALSE),
241   fInsideLTM(kFALSE),
242   fInsideTRMchain0(kFALSE),
243   fInsideTRMchain1(kFALSE),
244   fLocalEventCounterDRM(-1),
245   fLocalEventCounterLTM(-1),
246   fLocalEventCounterTRM(0x0),
247   fLocalEventCounterChain(0x0),
248   fCableLengthMap(0x0),
249   fEventID(0)
250 {
251   //
252   // default ctr
253   //
254   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
255     fDataBuffer[i]=new AliTOFHitDataBuffer();
256     fPackedDataBuffer[i]=new AliTOFHitDataBuffer();
257   }
258
259   fTOFrawData = new TClonesArray("AliTOFrawData",1000);
260   fTOFrawData->SetOwner();
261
262   fLocalEventCounterTRM = new Int_t[13];//adc
263   fLocalEventCounterChain = new Int_t*[13];//adc
264   for (Int_t j=0;j<13;j++){//adc
265     fLocalEventCounterTRM[j] = -1;//adc
266     fLocalEventCounterChain[j] = new Int_t[2];//adc
267     for (Int_t k=0;k<2;k++){//adc
268       fLocalEventCounterChain[j][k] = -1;//adc
269     }//adc
270   }//adc
271
272   fCableLengthMap = new AliTOFCableLengthMap();
273
274 }
275
276 //_____________________________________________________________________________
277 AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
278   TObject(stream),
279   fRawReader(0x0),
280   fTOFrawData(0x0),
281   fDecoder(new AliTOFDecoder()),
282   fDDL(-1),
283   fTRM(-1),
284   fTRMchain(-1),
285   fTDC(-1),
286   fTDCchannel(-1),
287   fTime(-1),
288   fToT(-1),
289   fLeadingEdge(-1),
290   fTrailingEdge(-1),
291   fErrorFlag(-1),
292   fSector(-1),
293   fPlate(-1),
294   fStrip(-1),
295   fPadX(-1),
296   fPadZ(-1),
297   fPackedDigits(0),
298   fWordType(-1),
299   fSlotID(-1),
300   fACQ(-1),
301   fPSbit(-1),
302   fTDCerrorFlag(-1),
303   fInsideDRM(kFALSE),
304   fInsideTRM(kFALSE),
305   fInsideLTM(kFALSE),
306   fInsideTRMchain0(kFALSE),
307   fInsideTRMchain1(kFALSE),
308   fLocalEventCounterDRM(-1),
309   fLocalEventCounterLTM(-1),
310   fLocalEventCounterTRM(0x0),
311   fLocalEventCounterChain(0x0),
312   fCableLengthMap(0x0),
313   fEventID(0)
314 {
315   //
316   // copy constructor
317   //
318
319   fRawReader = stream.fRawReader;
320
321   fTOFrawData = stream.fTOFrawData;
322
323   fDDL = stream.fDDL;
324   fTRM = stream.fTRM;
325   fTRMchain = stream.fTRMchain;
326   fTDC = stream.fTDC;
327   fTDCchannel = stream.fTDCchannel;
328   fTime = stream.fTime;
329   fToT = stream.fToT;
330   fLeadingEdge = stream.fLeadingEdge;
331   fTrailingEdge = stream.fTrailingEdge;
332
333   fErrorFlag = stream.fErrorFlag;
334
335   fSector = stream.fSector;
336   fPlate = stream.fPlate;
337   fStrip = stream.fStrip;
338   fPadX = stream.fPadX;
339   fPadZ = stream.fPadZ;
340
341   fPackedDigits = stream.fPackedDigits;
342
343   fWordType = stream.fWordType;
344   fSlotID = stream.fSlotID;
345   fACQ = stream.fACQ;
346   fPSbit = stream.fPSbit;
347   fTDCerrorFlag = stream.fTDCerrorFlag;
348   fInsideDRM = stream.fInsideDRM;
349   fInsideTRM = stream.fInsideTRM;
350   fInsideLTM = stream.fInsideLTM;
351   fInsideTRMchain0 = stream.fInsideTRMchain0;
352   fInsideTRMchain1 = stream.fInsideTRMchain1;
353
354   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
355     fDataBuffer[i]= new AliTOFHitDataBuffer(*stream.fDataBuffer[i]);
356     fPackedDataBuffer[i]= new AliTOFHitDataBuffer(*stream.fPackedDataBuffer[i]);
357   }
358
359   fTOFrawData = new TClonesArray(*stream.fTOFrawData);
360
361   fLocalEventCounterDRM = stream.fLocalEventCounterDRM;//adc
362   fLocalEventCounterLTM = stream.fLocalEventCounterLTM;//adc
363   for (Int_t j=0;j<13;j++){//adc
364     fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];//adc
365     for (Int_t k=0;k<2;k++){//adc
366       fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];//adc
367     }//adc
368   }//adc
369
370   fCableLengthMap = stream.fCableLengthMap;
371
372   fEventID = stream.fEventID;
373
374 }
375
376 //_____________________________________________________________________________
377 AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
378 {
379   //
380   // assignment operator
381   //
382
383   fRawReader = stream.fRawReader;
384
385   fTOFrawData = stream.fTOFrawData;
386
387   fDDL = stream.fDDL;
388   fTRM = stream.fTRM;
389   fTRMchain = stream.fTRMchain;
390   fTDC = stream.fTDC;
391   fTDCchannel = stream.fTDCchannel;
392   fTime = stream.fTime;
393   fToT = stream.fToT;
394   fLeadingEdge = stream.fLeadingEdge;
395   fTrailingEdge = stream.fTrailingEdge;
396   fErrorFlag = stream.fErrorFlag;
397
398   fSector = stream.fSector;
399   fPlate = stream.fPlate;
400   fStrip = stream.fStrip;
401   fPadX = stream.fPadX;
402   fPadZ = stream.fPadZ;
403
404   fPackedDigits = stream.fPackedDigits;
405
406   fWordType = stream.fWordType;
407   fSlotID = stream.fSlotID;
408   fACQ = stream.fACQ;
409   fPSbit = stream.fPSbit;
410   fTDCerrorFlag = stream.fTDCerrorFlag;
411   fInsideDRM = stream.fInsideDRM;
412   fInsideTRM = stream.fInsideTRM;
413   fInsideLTM = stream.fInsideLTM;
414   fInsideTRMchain0 = stream.fInsideTRMchain0;
415   fInsideTRMchain1 = stream.fInsideTRMchain1;
416   
417   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){ 
418     fDataBuffer[i] = stream.fDataBuffer[i];
419     fPackedDataBuffer[i] = stream.fPackedDataBuffer[i];
420   }
421   
422   fTOFrawData = stream.fTOFrawData;
423
424   fLocalEventCounterDRM = stream.fLocalEventCounterDRM;//adc
425   fLocalEventCounterLTM = stream.fLocalEventCounterLTM;//adc
426   for (Int_t j=0;j<13;j++){//adc
427     fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];//adc
428     for (Int_t k=0;k<2;k++){//adc
429       fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];//adc
430     }//adc
431   }//adc
432
433   fCableLengthMap = stream.fCableLengthMap;
434
435   fEventID = stream.fEventID;
436
437   return *this;
438
439 }
440
441 //_____________________________________________________________________________
442 AliTOFRawStream::~AliTOFRawStream()
443 {
444 // destructor
445
446   fPackedDigits = 0;
447
448   for (Int_t i=0;i<72;i++){ 
449     delete fDataBuffer[i];
450     delete fPackedDataBuffer[i];
451   }
452
453   delete fDecoder;
454
455   fTOFrawData->Clear();
456   delete fTOFrawData;
457
458   delete [] fLocalEventCounterTRM;
459   for (Int_t ii=0; ii<2; ii++) 
460     delete [] fLocalEventCounterChain[ii];
461
462   delete fCableLengthMap;
463
464 }
465
466
467 //_____________________________________________________________________________
468
469 void AliTOFRawStream::LoadRawData(Int_t indexDDL)
470 {
471   //
472   // To load raw data
473   //
474
475   fTOFrawData->Clear();
476
477   TClonesArray &arrayTofRawData =  *fTOFrawData;
478
479   fPackedDigits = 0;
480
481   // create raw data map
482   AliTOFRawMap *rawMap = new AliTOFRawMap(fTOFrawData);
483   rawMap->Clear();
484
485   Int_t slot[4] = {-1, -1, -1, -1};
486
487   fLocalEventCounterDRM = -1;
488   fLocalEventCounterLTM = -1;
489   for (Int_t ii=0; ii<13; ii++)
490     fLocalEventCounterTRM[ii] = -1;
491   for (Int_t ii=0; ii<13; ii++)
492     for (Int_t jj=0; jj<2; jj++)
493       fLocalEventCounterChain[ii][jj] = -1;
494
495   fRawReader->Reset();
496   fRawReader->Select("TOF", indexDDL, indexDDL);
497     
498   Bool_t signal = kFALSE;
499
500   AliTOFrawData *rawDigit = NULL;
501
502   while(Next()) {
503
504     signal = (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1);
505     if (signal) {
506       AliDebug(2,Form("  %2i  %1i  %2i  %1i  %2i", fSector, fPlate, fStrip, fPadZ, fPadX));
507
508       slot[0] = fTRM;
509       slot[1] = fTRMchain;
510       slot[2] = fTDC;
511       slot[3] = fTDCchannel;
512
513       if (rawMap->TestHit(slot) != kEmpty) {
514
515         rawDigit = static_cast<AliTOFrawData*>(rawMap->GetHit(slot));
516
517         if (rawDigit->GetLeading()!=-1 && rawDigit->GetTrailing()==-1 &&
518             fLeadingEdge==-1 && fTrailingEdge!=-1) {
519
520           rawDigit->Update(fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
521         }
522         else if ( ((rawDigit->GetTOF()!=-1 || rawDigit->GetLeading()!=-1 || rawDigit->GetTrailing()!=-1) &&
523                    (fLeadingEdge!=-1 || fTrailingEdge!=-1 || fTime!=-1) )
524
525                   )
526           {
527
528             new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
529
530             rawMap->SetHit(slot);
531
532           }
533
534
535       }
536       else {
537
538         new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
539
540         rawMap->SetHit(slot);
541
542       } // else if (rawMap->TestHit(slot) == kEmpty)
543
544     } // if (signal)
545
546   } // closed -> while (Next())
547
548   rawMap->Delete();
549
550 }
551
552 //_____________________________________________________________________________
553 Bool_t AliTOFRawStream::Next()
554 {
555   //
556   // Read next 32-bit word in TOF raw data files
557   // returns kFALSE if there is no word left
558   //
559
560   UInt_t data;
561
562   Int_t dummy = 0;
563
564   if (!fRawReader->ReadNextInt(data)) return kFALSE;
565
566   if (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1) {
567     fSector = -1;
568     fPlate  = -1;
569     fStrip  = -1;
570     fPadZ   = -1;
571     fPadX   = -1;
572     fTime   = -1;
573     fToT    = -1;
574     fLeadingEdge  = -1;
575     fTrailingEdge = -1;
576   }
577
578   fDDL  = fRawReader->GetDDLID();
579
580   fWordType = GetField(data,WORD_TYPE_MASK,WORD_TYPE_POSITION);
581
582   switch (fWordType) { // switch word type
583
584   case GLOBAL_HEADER_TYPE: // global header
585     fSlotID = GetField(data, HEADER_SLOT_ID_MASK, HEADER_SLOT_ID_POSITION);
586     fTRM = fSlotID;
587
588
589     switch (fSlotID) { // switch global header slot ID
590
591     case DRM_ID_NUMBER: //DRM global header
592       if (fInsideDRM) { // unexpected DRM global headers -> exit
593         break;
594       }
595       fInsideDRM = kTRUE; // DRM global header accepted
596       break;
597
598     case LTM_ID_NUMBER: // LTM global header
599       if (fInsideLTM) { // unexpected LTM global headers -> exit
600         break;
601       }
602       fInsideLTM = kTRUE; // LTM global header accepted
603       break;
604
605     case  3: //TRM header
606     case  4: //TRM header
607     case  5: //TRM header
608     case  6: //TRM header
609     case  7: //TRM header
610     case  8: //TRM header
611     case  9: //TRM header
612     case 10: //TRM header
613     case 11: //TRM header
614     case 12: //TRM header
615       if (fInsideTRM) { // unexpected TRM global headers -> exit
616         break;
617       }
618       fInsideTRM = kTRUE; // TRM global header accepted
619       fACQ =  GetField(data,TRM_ACQ_BITS_MASK,TRM_ACQ_BITS_POSITION);
620       break;
621
622     default: // unexpected global header slot ID
623       break;
624
625     } //end switch global header slot id
626
627     break;
628
629
630   case GLOBAL_TRAILER_TYPE: // global trailer
631     fSlotID = GetField(data,HEADER_SLOT_ID_MASK,HEADER_SLOT_ID_POSITION);
632
633     switch (fSlotID) { // switch global trailer slot ID
634
635     case DRM_ID_NUMBER: // DRM global trailer
636       if (!fInsideDRM) { // unexpected DRM global trailers -> exit
637         break;
638       }
639       dummy = 0x0000fff0;
640       //AliInfo(Form("  DRM local event counter = %i", GetField(data,dummy,4)));
641       fLocalEventCounterDRM = GetField(data,dummy,4);//adc
642       fInsideDRM = kFALSE; // DRM global trailer accepted
643       fInsideTRM = kFALSE;
644       fInsideLTM = kFALSE;
645       fInsideTRMchain0 = kFALSE;
646       fInsideTRMchain1 = kFALSE;
647       fSector = -1;
648       fPlate  = -1;
649       fStrip  = -1;
650       fPadZ   = -1;
651       fPadX   = -1;
652       fDDL        = -1;
653       fTRM        = -1;
654       fTDC        = -1;
655       fTRMchain   = -1;
656       fTDCchannel = -1;
657       fTime = -1;
658       fToT  = -1;
659       fLeadingEdge  = -1;
660       fTrailingEdge = -1;
661       fErrorFlag = -1;
662       fACQ   = -1;
663       fPSbit = -1;
664       fTDCerrorFlag = -1;
665       break;
666     case LTM_ID_NUMBER: // LTM global trailer
667       if (!fInsideLTM) { // unexpected LTM global trailer -> exit
668         break;
669       }
670       dummy = 0x0fff0000;
671       //AliInfo(Form("  LTM local event counter = %i", GetField(data,dummy,16)));
672       fLocalEventCounterLTM = GetField(data,dummy,16);//adc
673       fInsideLTM = kFALSE; // LTM global trailer accepted
674       break;
675     case 15: //TRM global trailer
676       if (!fInsideTRM) { // unexpected TRM global trailers -> exit
677         break;
678       }
679       dummy = 0x0fff0000;
680       //AliInfo(Form("  TRM local event counter = %i", GetField(data,dummy,16)));
681       fLocalEventCounterTRM[fTRM] = GetField(data,dummy,16);//adc
682       fInsideTRM = kFALSE; // TRM global trailer accepted
683       break;
684     default: // unexpected global trailer slot ID
685       break;
686     } //end switch global trailer slot id
687
688
689     break;
690
691
692   case ERROR_TYPE: // TDC error
693     fTDC          = GetField(data,TRM_TDC_ERROR_TDC_ID_MASK,TRM_TDC_ERROR_TDC_ID_POSITION);
694     fTDCerrorFlag = GetField(data,TRM_TDC_ERROR_FLAGS_MASK,TRM_TDC_ERROR_FLAGS_POSITION);
695     break;
696
697
698   case FILLER_TYPE: // filler
699     break;
700
701
702   default: // other word types
703
704     if (fInsideTRM) { // inside TRM
705
706       switch (fWordType) { // switch word type inside TRM
707       case TRM_CHAIN0_HEADER_TYPE: // TRM chain0 header
708         if (fInsideTRMchain0) { // unexpected TRM chain0 header
709           break;
710         }
711         fInsideTRMchain0 = kTRUE;
712         fTRMchain = 0;
713         break;
714       case TRM_CHAIN0_TRAILER_TYPE: // TRM chain0 trailer
715         if (!fInsideTRMchain0) { // unexpected TRM chain0 trailer
716           break;
717         }
718         dummy = 0x0fff0000;
719         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
720         fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);//adc
721         fInsideTRMchain0 = kFALSE;
722         fTRMchain = -1;
723         break;
724       case TRM_CHAIN1_HEADER_TYPE: // TRM chain1 header
725         if (fInsideTRMchain1) { // unexpected TRM chain1 header
726           break;
727         }
728         fInsideTRMchain1 = kTRUE;
729         fTRMchain = 1;
730         break;
731       case TRM_CHAIN1_TRAILER_TYPE: // TRM chain1 trailer
732         if (!fInsideTRMchain1) { // unexpected TRM chain1 trailer
733           break;
734         }
735         dummy = 0x0fff0000;
736         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
737         fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);//adc
738         fInsideTRMchain1 = kFALSE;
739         fTRMchain = -1;
740         break;
741       } // end switch word type inside TRM
742
743     } // end if (fInsideTRM)
744
745       
746     if (
747         ((fInsideTRMchain0&&!fInsideTRMchain1) || (!fInsideTRMchain0&&fInsideTRMchain1)) 
748         && fWordType!=TRM_CHAIN0_HEADER_TYPE && fWordType!=TRM_CHAIN0_TRAILER_TYPE
749         && fWordType!=TRM_CHAIN1_HEADER_TYPE && fWordType!=TRM_CHAIN1_TRAILER_TYPE
750         ){ // inside TRM chains
751
752       fPSbit      = GetField(data,TRM_PS_BITS_MASK,TRM_PS_BITS_POSITION);
753       fTDC        = GetField(data,TRM_TDC_ID_MASK,TRM_TDC_ID_POSITION);
754       fTDCchannel = GetField(data,TRM_CHAN_MASK,TRM_CHAN_POSITION);
755       fErrorFlag  = GetField(data,TRM_E_BIT_MASK,TRM_E_BIT_POSITION);
756
757       SetSector();
758       SetPlate();
759       SetStrip();
760       SetPadZ();
761       SetPadX();
762
763
764       switch (fPSbit) { // switch fPSbit bits inside TRM chains
765
766       case 0: // packing ok, digit time and TOT
767         fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
768         fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
769           /*-
770           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
771           /*+
772           (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
773           +
774           (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
775           ;
776         break;
777
778       case 1: // leading edge digit, long digit time, no TOT
779         //fToT  = -1;
780         //fTime  = -1;
781         fLeadingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
782           /*-
783           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
784           /*+
785           (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
786           +
787           (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
788           ;
789         break;
790
791       case 2: // trailing edge digit, long digit time, no TOT
792         //fToT  = -1;
793         //fTime  = -1;
794         fTrailingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
795           /*-
796           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
797           /*+
798           (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
799           +
800           (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
801           ;
802         break;
803
804       case 3: // TOT overflow
805         fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
806         fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
807           /*-
808           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
809           /*+
810           (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
811           +
812           (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
813           ;
814         break;
815
816       } // end switch PS bits inside TRM chains
817
818     } // end if is inside TRM chains
819
820   } // end switch on fWordType
821
822
823   return kTRUE;
824   
825 }
826 //_____________________________________________________________________________
827
828 void AliTOFRawStream::SetSector()
829 {
830   //
831   // Evaluate the TOF sector number -> [ 0;17]
832   // corresponding to the TOF equipment IDs:
833   //                                  fDDL        -> [ 0;71]
834   //                                  fTRM        -> [ 3;12]
835   //                                  fTRMchain   -> [ 0; 1]
836   //                                  fTDC        -> [ 0;14]
837   //                                  fTDCchannel -> [ 0; 7]
838   //
839
840   Int_t iSector = -1;
841
842   if (!(fDDL==-1)) iSector = Int_t((Float_t)(fDDL)/AliTOFGeometry::NDDL());
843
844   fSector = iSector;
845
846 }
847 //_____________________________________________________________________________
848
849
850 void AliTOFRawStream::SetPlate()
851 {
852   //
853   // Evaluate the TOF plate number ->[ 0; 4]
854   // corresponding to the TOF equipment IDs:
855   //                                  fDDL        -> [ 0;71]
856   //                                  fTRM        -> [ 3;12]
857   //                                  fTRMchain   -> [ 0; 1]
858   //                                  fTDC        -> [ 0;14]
859   //                                  fTDCchannel -> [ 0; 7]
860   //
861
862   Int_t iPlate = -1;
863   if (!(fDDL==-1 || fTRM==-1 || fTDC==-1
864         || fSector==-1))
865     iPlate = Equip2VolNplate(GetDDLnumberPerSector(fDDL), fTRM, fTDC);
866
867   fPlate = iPlate;
868
869 }
870 //_____________________________________________________________________________
871
872 void AliTOFRawStream::SetStrip()
873 {
874   //
875   // Evaluate the TOF strip number per module -> [ 0; 14/18]
876   // corresponding to the TOF equipment IDs:
877   //                                  fDDL        -> [ 0;71]
878   //                                  fTRM        -> [ 3;12]
879   //                                  fTRMchain   -> [ 0; 1]
880   //                                  fTDC        -> [ 0;14]
881   //                                  fTDCchannel -> [ 0; 7]
882   //
883
884   Int_t iStrip = -1;
885
886   if (!(fDDL==-1 || fTRM==-1 || fTDC==-1
887         || fSector==-1 || fPlate==-1))
888     iStrip = Equip2VolNstrip(GetDDLnumberPerSector(fDDL), fTRM, fTDC);
889
890   fStrip = iStrip;
891
892 }
893 //_____________________________________________________________________________
894
895 void AliTOFRawStream::SetPadZ()
896 {
897   //
898   // Evaluate the TOF padRow number per strip -> [ 0; 1]
899   // corresponding to the TOF equipment IDs:
900   //                                  fDDL        -> [ 0;71]
901   //                                  fTRM        -> [ 3;12]
902   //                                  fTRMchain   -> [ 0; 1]
903   //                                  fTDC        -> [ 0;14]
904   //                                  fTDCchannel -> [ 0; 7]
905   //
906
907   Int_t iPadZ = -1;
908
909   if (!(fDDL==-1 || fTRM==-1 || fTRMchain==-1 || fTDC==-1 || fTDCchannel==-1
910         || fSector==-1 || fPlate==-1 || fStrip==-1))
911     {
912       Int_t iPadAlongTheStrip = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
913       if (iPadAlongTheStrip!=-1)
914         iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
915     }
916
917   //iPadZ = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)%AliTOFGeometry::NpadZ();
918   //iPadZ = Equip2VolNpadZ(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
919
920   fPadZ = iPadZ;
921
922 }
923 //_____________________________________________________________________________
924
925 void AliTOFRawStream::SetPadX()
926 {
927   //
928   // Evaluate the TOF pad number per strip padRow -> [ 0;47]
929   // corresponding to the TOF equipment IDs:
930   //                                  fDDL        -> [ 0;71]
931   //                                  fTRM        -> [ 3;12]
932   //                                  fTRMchain   -> [ 0; 1]
933   //                                  fTDC        -> [ 0;14]
934   //                                  fTDCchannel -> [ 0; 7]
935   //
936
937   Int_t iPadX = -1;
938
939   if (!(fDDL==-1 || fTRM==-1 || fTRMchain==-1 || fTDC==-1 || fTDCchannel==-1
940         || fSector==-1 || fPlate==-1 || fStrip==-1))
941     {
942       Int_t iPadAlongTheStrip = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
943       if (iPadAlongTheStrip!=-1)
944         iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
945     }
946
947   //iPadX = (Int_t)(Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)/(Float_t(AliTOFGeometry::NpadZ())));
948   //iPadX = Equip2VolNpadX(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
949
950   fPadX = iPadX;
951
952 }
953
954 //----------------------------------------------------------------------------
955 Int_t AliTOFRawStream::GetField(UInt_t word, Int_t fieldMask, Int_t fieldPosition) const
956 {
957   // 
958   // 
959   // 
960
961   return ((word & fieldMask) >> fieldPosition);
962 }
963
964 //----------------------------------------------------------------------------
965 Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC)
966 {
967   //
968   // Returns the TOF plate number [0;4]
969   // corresponding to the TOF equipment ID numbers:
970   //                          iDDL -> DDL number per sector [0;3]
971   //                          nTRM -> TRM number [3;12]
972   //                          nTDC -> TDC number [0;14]
973   //
974
975   Int_t iPlate = -1;
976   if (iDDL==0) {
977
978     if (nTRM>=4 && nTRM<7) {
979       iPlate = 0;
980     } else if (nTRM==7) {
981       if (nTDC<12) iPlate = 0;
982       else iPlate = 1;
983     } else if (nTRM>=8 && nTRM<11) {
984       iPlate = 1;
985     } else if (nTRM==11) {
986       if (nTDC<9) iPlate = 1;
987       else iPlate = 2;
988     }else if (nTRM==12) {
989       iPlate = 2;
990     } 
991
992   } else if (iDDL==1) {
993
994     if (nTRM==3) {
995       if (nTDC<3) iPlate = 0;
996     } else if (nTRM>=4 && nTRM<7) {
997       iPlate = 0;
998     } else if (nTRM==7) {
999       if (nTDC<6) iPlate = 1;
1000       else iPlate = 0;
1001     } else if (nTRM>=8 && nTRM<11) {
1002       iPlate = 1;
1003     } else if (nTRM==11) {
1004       if (nTDC<9) iPlate = 2;
1005       else iPlate = 1;
1006     } else if (nTRM==12) {
1007       iPlate = 2;
1008     } 
1009
1010   } else if (iDDL==2) {
1011
1012     if (nTRM>=4 && nTRM<7) {
1013       iPlate = 4;
1014     } else if (nTRM==7) {
1015       if (nTDC<12) iPlate = 4;
1016       else iPlate = 3;
1017     } else if (nTRM>=8 && nTRM<11) {
1018       iPlate = 3;
1019     } else if (nTRM==11) {
1020       if (nTDC<9) iPlate = 3;
1021       else iPlate = 2;
1022     }else if (nTRM==12) {
1023       iPlate = 2;
1024     } 
1025
1026   }  else if (iDDL==3) {
1027
1028     if (nTRM==3) {
1029       if (nTDC<3) iPlate = 4;
1030     } else if (nTRM>=4 && nTRM<7) {
1031       iPlate = 4;
1032     } else if (nTRM==7) {
1033       if (nTDC<6) iPlate = 3;
1034       else iPlate = 4;
1035     } else if (nTRM>=8 && nTRM<11) {
1036       iPlate = 3;
1037     } else if (nTRM==11) {
1038       if (nTDC<9) iPlate = 2;
1039       else iPlate = 3;
1040     } else if (nTRM==12) {
1041       iPlate = 2;
1042     } 
1043
1044   }
1045
1046   return iPlate;
1047
1048 }
1049
1050 //----------------------------------------------------------------------------
1051 Int_t AliTOFRawStream::Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC)
1052 {
1053   //
1054   // Returns the TOF strip number per module:
1055   //                                [0;14], in the central plates,
1056   //                                [0;18], in the intermediate and external plates
1057   // corresponding to the TOF equipment ID numbers:
1058   //                                iDDL -> DDL number per sector [0;3]
1059   //                                nTRM -> TRM number [3;12]
1060   //                                nTDC -> TDC number [0;14]
1061   //
1062
1063   Int_t iStrip = -1;
1064
1065   if (iDDL==0) {
1066
1067     if (nTRM== 4) iStrip =  (Int_t)(nTDC/3.);
1068     else if (nTRM== 5) iStrip =  5 + (Int_t)(nTDC/3.);
1069     else if (nTRM== 6) iStrip = 10 + (Int_t)(nTDC/3.);
1070     else if (nTRM== 7) {
1071       if (nTDC<12) iStrip =  15 + (Int_t)(nTDC/3.);
1072       else iStrip = (Int_t)(nTDC/3.) -  4;
1073     }
1074     else if (nTRM== 8) iStrip =  1 + (Int_t)(nTDC/3.);
1075     else if (nTRM== 9) iStrip =  6 + (Int_t)(nTDC/3.);
1076     else if (nTRM==10) iStrip = 11 + (Int_t)(nTDC/3.);
1077     else if (nTRM==11) {
1078       if (nTDC<9) iStrip = 16 + (Int_t)(nTDC/3.);
1079       else iStrip = (Int_t)(nTDC/3.) -  3;
1080     }
1081     else if (nTRM==12) iStrip =  2 + (Int_t)(nTDC/3.);
1082
1083   } else if (iDDL==1) {
1084
1085     if (nTRM==3 && nTDC<3) iStrip = (Int_t)(nTDC/3.);
1086     else if (nTRM== 4) iStrip =  5 - (Int_t)(nTDC/3.);
1087     else if (nTRM== 5) iStrip = 10 - (Int_t)(nTDC/3.);
1088     else if (nTRM== 6) iStrip = 15 - (Int_t)(nTDC/3.);
1089     else if (nTRM== 7) {
1090       if (nTDC<6) iStrip =  1 - (Int_t)(nTDC/3.);
1091       else iStrip = 20 - (Int_t)(nTDC/3.);
1092     }
1093     else if (nTRM== 8) iStrip =  6 - (Int_t)(nTDC/3.);
1094     else if (nTRM== 9) iStrip = 11 - (Int_t)(nTDC/3.);
1095     else if (nTRM==10) iStrip = 16 - (Int_t)(nTDC/3.);
1096     else if (nTRM==11) {
1097       if (nTDC<9) iStrip =  2 - (Int_t)(nTDC/3.);
1098       else iStrip = 21 - (Int_t)(nTDC/3.);
1099     }
1100     else if (nTRM==12) iStrip =  7 - (Int_t)(nTDC/3.);
1101
1102   } else if (iDDL==2) {
1103
1104     if (nTRM== 4) iStrip =  18 - (Int_t)(nTDC/3.);
1105     else if (nTRM== 5) iStrip = 18 - ( 5 + (Int_t)(nTDC/3.));
1106     else if (nTRM== 6) iStrip = 18 - (10 + (Int_t)(nTDC/3.));
1107     else if (nTRM== 7) {
1108       if (nTDC<12) iStrip =  18 - (15 + (Int_t)(nTDC/3.));
1109       else iStrip = 18 - ((Int_t)(nTDC/3.) -  4);
1110     }
1111     else if (nTRM== 8) iStrip = 18 - ( 1 + (Int_t)(nTDC/3.));
1112     else if (nTRM== 9) iStrip = 18 - ( 6 + (Int_t)(nTDC/3.));
1113     else if (nTRM==10) iStrip = 18 - (11 + (Int_t)(nTDC/3.));
1114     else if (nTRM==11) {
1115       if (nTDC<9) iStrip = 18 - (16 + (Int_t)(nTDC/3.));
1116       else iStrip = 14 - ((Int_t)(nTDC/3.) -  3);
1117     }
1118     else if (nTRM==12) iStrip = 14 - ( 2 + (Int_t)(nTDC/3.));
1119
1120   } else if (iDDL==3) {
1121
1122     if (nTRM==3 && nTDC<3) iStrip = 18 - (Int_t)(nTDC/3.);
1123     else if (nTRM== 4) iStrip = 18 - ( 5 - (Int_t)(nTDC/3.));
1124     else if (nTRM== 5) iStrip = 18 - (10 - (Int_t)(nTDC/3.));
1125     else if (nTRM== 6) iStrip = 18 - (15 - (Int_t)(nTDC/3.));
1126     else if (nTRM== 7) {
1127       if (nTDC<6) iStrip =  18 - (1 - (Int_t)(nTDC/3.));
1128       else iStrip = 18 - (20 - (Int_t)(nTDC/3.));
1129     }
1130     else if (nTRM== 8) iStrip = 18 - ( 6 - (Int_t)(nTDC/3.));
1131     else if (nTRM== 9) iStrip = 18 - (11 - (Int_t)(nTDC/3.));
1132     else if (nTRM==10) iStrip = 18 - (16 - (Int_t)(nTDC/3.));
1133     else if (nTRM==11) {
1134       if (nTDC<9) iStrip = 14 - ( 2 - (Int_t)(nTDC/3.));
1135       else iStrip = 18 - (21 - (Int_t)(nTDC/3.));
1136     }
1137     else if (nTRM==12) iStrip = 14 - ( 7 - (Int_t)(nTDC/3.));
1138
1139   } 
1140
1141   return iStrip;
1142
1143 }
1144
1145 //----------------------------------------------------------------------------
1146 Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
1147                                      Int_t iCH)
1148 {
1149   //
1150   // Returns the TOF pad number per strip [0;95]
1151   // corresponding to the TOF equipment ID numbers:
1152   //                          iDDL -> DDL number per sector [0;3]
1153   //                        iChain -> TRM chain number [0;1]
1154   //                          nTDC -> TDC number [0;14]
1155   //                           iCH -> TDC channel number [0;7]
1156   //
1157
1158   Int_t iPadAlongTheStrip = -1;
1159
1160   // wrong
1161   //Int_t iTDClocal = nTDC%3 + (1-iChain)*3;
1162   //if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal;
1163   //else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal);
1164
1165   // right
1166   Int_t iTDClocal = -1;
1167   Int_t iTDClocal03 = nTDC%3 + (1-iChain)*3;
1168   Int_t iTDClocal12 = 2-nTDC%3 + iChain*3;
1169   if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal03;
1170   else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal12);
1171
1172   Int_t iCHlocal = iCH;
1173   if (iDDL==0 || iDDL==3) iCHlocal = 7 - iCH;
1174
1175   iPadAlongTheStrip = iTDClocal*AliTOFGeometry::NCh() + iCHlocal;
1176
1177   if (((iDDL==1 || iDDL==2) && iPadAlongTheStrip< AliTOFGeometry::NpadX()) ||
1178       ((iDDL==0 || iDDL==3) && iPadAlongTheStrip>=AliTOFGeometry::NpadX())) {
1179     std::cerr << "Problems with the padX number!" << endl;
1180     //AliWarning("Problems with the padX number!");
1181   }
1182   return iPadAlongTheStrip;
1183
1184 }
1185
1186 //----------------------------------------------------------------------------
1187 Int_t AliTOFRawStream::Equip2VolNpadX(Int_t iDDL, Int_t iChain, Int_t nTDC,
1188                                       Int_t iCH)
1189 {
1190   //
1191   // Returns the TOF padX number [0;47]
1192   // corresponding to the TOF equipment ID numbers:
1193   //                          iDDL -> DDL number per sector [0;3]
1194   //                        iChain -> TRM chain number [0;1]
1195   //                          nTDC -> TDC number [0;14]
1196   //                           iCH -> TDC channel number [0;7]
1197   //
1198
1199   Int_t iPadX = (Int_t)(AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)/
1200                         (Float_t(AliTOFGeometry::NpadZ())));
1201
1202   return iPadX;
1203
1204 }
1205
1206 //----------------------------------------------------------------------------
1207 Int_t AliTOFRawStream::Equip2VolNpadZ(Int_t iDDL, Int_t iChain, Int_t nTDC,
1208                                       Int_t iCH)
1209 {
1210   //
1211   // Returns the TOF padZ number [0;1]
1212   // corresponding to the TOF equipment ID numbers:
1213   //                          iDDL -> DDL number per sector [0;3]
1214   //                        iChain -> TRM chain number [0;1]
1215   //                          nTDC -> TDC number [0;14]
1216   //                           iCH -> TDC channel number [0;7]
1217   //
1218
1219   Int_t iPadZ  = AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
1220
1221   return iPadZ;
1222
1223 }
1224
1225 //----------------------------------------------------------------------------
1226 Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL) const
1227 {
1228   //
1229   // Returns the sector number [0;17]
1230   // corresponing to the assigned DRM/DDL number [0;71]
1231   //
1232
1233   Int_t iSector = Int_t((Float_t)(nDDL)/AliTOFGeometry::NDDL());
1234
1235   return iSector;
1236
1237 }
1238 //----------------------------------------------------------------------------
1239 Int_t AliTOFRawStream::GetDDLnumberPerSector(Int_t nDDL) const
1240 {
1241   //
1242   // Return the DRM/DDL number per sector [0;3]
1243   // corresponing to the assigned DRM/DDL number [0;71]
1244   //
1245
1246   Int_t iDDL = nDDL%AliTOFGeometry::NDDL();
1247
1248   return iDDL;
1249
1250 }
1251
1252 //----------------------------------------------------------------------------
1253 void AliTOFRawStream::EquipmentId2VolumeId(AliTOFHitData *hitData, Int_t *volume) const
1254 {
1255   EquipmentId2VolumeId(hitData->GetDDLID(),hitData->GetSlotID(),hitData->GetChain(),hitData->GetTDC(),hitData->GetChan(),volume);
1256 }
1257 //----------------------------------------------------------------------------
1258 void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
1259                                         Int_t nTDC, Int_t iCH,
1260                                         Int_t *volume) const
1261 {
1262   //
1263   // To convert:
1264   //            nDDL   (variable in [0;71]) -> number of the DDL file 
1265   //            nTRM   (variable in [3;12]) -> number of the TRM slot
1266   //            iChain (variable in [0; 1]) -> number of the TRM chain
1267   //            nTDC   (variable in [0;14]) -> number of the TDC
1268   //            iCH    (variable in [0; 7]) -> number of the TDC channel
1269   //
1270   // in:
1271   //      sector number, i.e. volume[0] (variable in [0,17])
1272   //      plate  number, i.e. volume[1] (variable in [0, 5])
1273   //      strip  number, i.e. volume[2] (variable in [0,14/18])
1274   //      padX   number, i.e. volume[3] (variable in [0,47])
1275   //      padZ   number, i.e. volume[4] (variable in [0, 1])
1276   //
1277
1278   Int_t iDDL = GetDDLnumberPerSector(nDDL);
1279
1280   Int_t iSector = GetSectorNumber(nDDL);
1281
1282   Int_t iPlate = Equip2VolNplate(iDDL, nTRM, nTDC);
1283   if (iPlate==-1) {
1284     if (fRawReader)
1285       fRawReader->AddMajorErrorLog(kPlateError,"plate = -1");
1286     AliWarning("Problems with the plate number!");
1287   }
1288
1289   Int_t iStrip = Equip2VolNstrip(iDDL, nTRM, nTDC);
1290   if (iStrip==-1) {
1291     if (fRawReader)
1292       fRawReader->AddMajorErrorLog(kStripError,"strip = -1");
1293     AliWarning("Problems with the strip number!");
1294   }
1295
1296   Int_t iPadAlongTheStrip  = Equip2VolNpad(iDDL, iChain, nTDC, iCH);
1297   if (iPadAlongTheStrip==-1){
1298     if (fRawReader)
1299       fRawReader->AddMajorErrorLog(kPadAlongStripError,"pad = -1");
1300     AliWarning("Problems with the pad number along the strip!");
1301   }
1302   
1303   Int_t iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
1304   Int_t iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
1305
1306   //Int_t iPadX  = (Int_t)(Equip2VolNpad(iDDL, iChain, nTDC, iCH)/(Float_t(AliTOFGeometry::NpadZ())));
1307   //Int_t iPadZ  = Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
1308
1309   //Int_t iPadX  = Equip2VolNpadX(iDDL, iChain, nTDC, iCH);
1310   //Int_t iPadZ  = Equip2VolNpadZ(iDDL, iChain, nTDC, iCH);
1311
1312   volume[0] = iSector;
1313   volume[1] = iPlate;
1314   volume[2] = iStrip;
1315   volume[3] = iPadX;
1316   volume[4] = iPadZ;
1317
1318 }
1319 //-----------------------------------------------------------------------------
1320 Bool_t AliTOFRawStream::DecodeDDL(Int_t nDDLMin, Int_t nDDLMax, Int_t verbose = 0) {
1321   //
1322   // To decode raw data for DDL number in [nDDLmin; nDDLmax]
1323   //
1324
1325   //check and fix valid DDL range
1326   if (nDDLMin < 0){
1327     nDDLMin = 0;
1328     fRawReader->AddMinorErrorLog(kDDLMinError);
1329     AliWarning("Wrong DDL range: setting first DDL ID to 0");
1330   }
1331   if (nDDLMax > 71){
1332     nDDLMax = 71;
1333     fRawReader->AddMinorErrorLog(kDDLMaxError);
1334     AliWarning("Wrong DDL range: setting last DDL ID to 71");
1335   }  
1336
1337   //select required DDLs
1338   fRawReader->Select("TOF", nDDLMin, nDDLMax);
1339
1340   if (verbose)
1341     AliInfo(Form("Selected TOF DDL range: %d-%d", nDDLMin, nDDLMax));
1342
1343   return(Decode(verbose));
1344 }
1345 //-----------------------------------------------------------------------------
1346 Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
1347   //
1348   // New decoder method
1349   //
1350
1351   Int_t currentEquipment;
1352   Int_t currentDDL;
1353   const AliRawDataHeader *currentCDH;
1354
1355   //pointers
1356   UChar_t *data = 0x0;
1357   
1358   //loop and read DDL headers 
1359   while(fRawReader->ReadHeader()){
1360
1361     //memory leak prevention (actually data should be always 0x0 here)
1362     if (data != 0x0)
1363       delete [] data;
1364
1365     //get equipment infos
1366     currentEquipment = fRawReader->GetEquipmentId();
1367     currentDDL = fRawReader->GetDDLID();
1368     currentCDH = fRawReader->GetDataHeader();
1369     const Int_t kDataSize = fRawReader->GetDataSize();
1370     const Int_t kDataWords = kDataSize / 4;
1371     data = new UChar_t[kDataSize];
1372     
1373     if (verbose)
1374       AliInfo(Form("Found equipment # %d header (DDL # %d): %d bytes (%d words)", currentEquipment, currentDDL, kDataSize, kDataWords));
1375     
1376     if (verbose)
1377       AliInfo(Form("Reading equipment #%d (DDL # %d) data...", currentEquipment, currentDDL));
1378     
1379     //read equipment payload
1380     if (!fRawReader->ReadNext(data, kDataSize))
1381       {
1382         fRawReader->AddMajorErrorLog(kDDLdataReading);
1383         if (verbose)
1384           AliWarning("Error while reading DDL data. Go to next equipment");
1385         delete [] data;
1386         data = 0x0;
1387         continue;
1388       }
1389     
1390     if (verbose)
1391       AliInfo(Form("Equipment # %d (DDL # %d) data has been readed", currentEquipment, currentDDL));
1392     
1393     
1394     //set up the decoder
1395     fDecoder->SetVerbose(verbose);
1396     fDecoder->SetDataBuffer(fDataBuffer[currentDDL]);
1397     fDecoder->SetPackedDataBuffer(fPackedDataBuffer[currentDDL]);
1398     
1399     //start decoding
1400     if (fDecoder->Decode((UInt_t *)data, kDataWords, currentCDH) == kTRUE) {
1401       fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",currentDDL));
1402       AliWarning(Form("Error while decoding DDL # %d: decoder returned with errors", currentDDL));
1403     }
1404     
1405     delete [] data;
1406     data = 0x0;
1407   }
1408   
1409   //reset reader
1410   fRawReader->Reset();
1411
1412   if (verbose)
1413     AliInfo("All done");
1414     
1415   return kFALSE;
1416   
1417 }
1418 //---------------------------------------------------------------------------
1419 void
1420 AliTOFRawStream::ResetBuffers()
1421 {
1422   //
1423   // To reset the buffers
1424   //
1425
1426   for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls("TOF"); iDDL++){
1427     ResetDataBuffer(iDDL);
1428     ResetPackedDataBuffer(iDDL);
1429   }
1430 }
1431   
1432 //---------------------------------------------------------------------------
1433 Bool_t
1434 AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
1435 {
1436   //
1437   // To load the buffers
1438   //
1439
1440   fTOFrawData->Clear();
1441   fPackedDigits = 0;
1442   
1443   if (verbose > 0)
1444     AliInfo(Form("Decoding raw data for DDL # %d ...", indexDDL));
1445
1446   if (DecodeDDL(indexDDL, indexDDL, verbose) != 0){ //decode DDL
1447     fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",indexDDL));
1448     AliWarning(Form("Error while decoding DDL # %d", indexDDL));
1449     return kTRUE;
1450   }
1451   
1452   if (verbose > 0)
1453     AliInfo(Form("Done. %d packed %s been found.", fPackedDataBuffer[indexDDL]->GetEntries(), fPackedDataBuffer[indexDDL]->GetEntries() > 1 ? "hits have" : "hit has"));
1454   
1455   AliTOFHitData *hitData; //hit data pointer
1456   
1457   if (verbose > 0)
1458     AliInfo("Filling TClonesArray ...");
1459
1460   if (verbose > 0)
1461     if (fgApplyBCCorrections) {
1462       AliInfo("Apply nominal DDL BC time-shift correction");
1463       AliInfo("Apply deltaBC time-shift correction");
1464     }
1465
1466   //loop over DDL packed hits
1467   for (Int_t iHit = 0; iHit < fPackedDataBuffer[indexDDL]->GetEntries(); iHit++){
1468     hitData = fPackedDataBuffer[indexDDL]->GetHit(iHit); //get hit data
1469     Int_t   hitACQ = hitData->GetACQ();
1470     Int_t   hitPS = hitData->GetPS();
1471     Int_t   hitSlotID = hitData->GetSlotID();
1472     Int_t   hitChain = hitData->GetChain();
1473     Int_t   hitTDC = hitData->GetTDC();
1474     Int_t   hitChan = hitData->GetChan();
1475     Int_t   hitTimeBin = hitData->GetTimeBin();
1476     Int_t   hitTOTBin = hitData->GetTOTBin();
1477
1478     if (fgApplyBCCorrections) {
1479       /* DDL BC shift time correction */
1480       hitTimeBin += fgkddlBCshift[indexDDL];
1481       /* deltaBC shift time correction */
1482       hitTimeBin += hitData->GetDeltaBunchID();
1483     }
1484
1485     Int_t hitLeading = hitData->GetTimeBin();
1486     Int_t hitTrailing = -1;
1487     Int_t hitError = -1;
1488     
1489     TClonesArray &arrayTofRawData =  *fTOFrawData;
1490     new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(hitSlotID, hitChain, hitTDC, hitChan, hitTimeBin, hitTOTBin, hitLeading, hitTrailing, hitPS, hitACQ, hitError);
1491   }
1492
1493   if (verbose > 0)
1494     AliInfo("Done.");
1495
1496   if (verbose > 0)
1497     AliInfo("Resetting buffers ...");
1498
1499   fDataBuffer[indexDDL]->Reset();
1500   fPackedDataBuffer[indexDDL]->Reset();
1501
1502   if (verbose > 0)
1503     AliInfo("Done.");
1504
1505   return kFALSE;
1506 }
1507