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