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