]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFRawStream.cxx
Coding convention violations: suppression
[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
19 Added lookup tables for
20          TRM number 3 in the left crates (TOF OR signals)
21          and detector elements (A.Silenzi)
22
23 Revision 1.19.1  2008/09/19  preghenella
24   Decode method updated:
25   it reads the CDH from the rawReader and sends it to the decoder;
26  LoadRawDataBuffers modified:
27      it corrects tof hit infos per ddlBC and deltaBC offsets
28      (in case of the static member fgApplyBCCorrections
29       has been setted to kTRUE);
30  Added static member fgApplyBCCorrections (kTRUE by default)
31  and the related static method ApplyBCCorrections;
32
33 Revision 1.19  2007/05/18 13:07:53  decaro
34 Error messages stored in the global raw-reader error log (Cvetan, Chiara)
35
36 Revision 1.18  2007/05/08 11:53:29  arcelli
37 Improved class flexibility for further use (R.Preghenella)
38
39 Revision 1.17  2007/05/03 08:53:50  decaro
40 Coding convention: RS3 violation -> suppression
41
42 Revision 1.16  2007/05/03 08:22:22  decaro
43 Coding convention: RN17 violation -> suppression
44
45 Revision 1.15  2007/04/30 15:22:06  arcelli
46 Change TOF digit Time, Tot etc to int type
47
48 Revision 1.14  2007/04/27 11:11:53  arcelli
49 updates for the new decoder
50
51 Revision 1.13  2007/03/16 11:46:35  decaro
52 Coding convention: RN17 rule violation -> suppression
53
54 Revision 1.12  2007/02/22 09:43:45  decaro
55 Added AliTOFRawStream::GetIndex method for online calibration (C.Zampolli)
56
57 Revision 1.11  2007/02/20 15:57:00  decaro
58 Raw data update: to read the TOF raw data defined in UNPACKED mode
59
60 Revision 1.10  2006/12/15 14:01:38  cvetan
61 Memory leak fixed
62
63 Revision 1.9  2006/10/13 11:22:27  arcelli
64 remove warnings due to uninitialized AliTOFtdcDigit data members
65
66 Revision 1.8  2006/08/22 13:30:17  arcelli
67 removal of effective c++ warnings (C.Zampolli)
68
69 Revision 1.7  2006/08/10 14:46:54  decaro
70 TOF raw data format: updated version
71
72 Revision 1.6.1  2006/06/28 A. De Caro, R. Preghenella:
73         Update TOF raw data format
74         according to the final version
75         (see the ALICE internal note in preparation
76          'ALICE TOF raw data format')
77         Added the methods for the correspoonding numbering
78          between the equipment IDs and the volume IDs:
79            Equip2VolNPlate(...)
80            Equip2VolNStrip(...)
81            Equip2VolNPad(...)
82
83 Revision 0.02  2005/07/28 A. De Caro:
84         Update format TOF raw data
85                (temporary solution) 
86         Correction of few wrong corrispondences
87                between 'software' and 'hardware' numberings
88
89 Revision 0.01  2005/07/22 A. De Caro
90         Implement methods Next()
91                           GetSector(),
92                           GetPlate(),
93                           GetStrip(),
94                           GetPadZ(),
95                           GetPadX()
96 */
97
98 ////////////////////////////////////////////////////////////////////////
99 //                                                                    //
100 //     This class provides access to TOF raw data in DDL files.       //
101 //                                                                    //
102 //      It loops over all TOF raw data given by the AliRawReader.     //
103 //                                                                    //
104 ////////////////////////////////////////////////////////////////////////
105
106
107 #include "Riostream.h"
108
109 #include "TClonesArray.h"
110 #include "TStopwatch.h"
111
112 #include "AliDAQ.h"
113 #include "AliLog.h"
114 #include "AliRawReader.h"
115
116 #include "AliTOFGeometry.h"
117 #include "AliTOFrawData.h"
118 #include "AliTOFRawMap.h"
119 #include "AliTOFRawStream.h"
120 #include "AliTOFdigit.h"
121 #include "AliTOFSDigit.h"
122 //#include "AliTOFCableLengthMap.h"
123
124 #include "AliTOFHitData.h"
125
126 #include "AliRawEventHeaderBase.h"
127 #include "AliRawDataHeader.h"
128
129 ClassImp(AliTOFRawStream)
130
131 const Int_t AliTOFRawStream::fgkddlBCshift[72] = 
132 {
133   0, 0, 0, 0,
134   0, 0, 0, 0,
135   0, 0, 0, 0,
136   0, 0, 0, 0,
137   0, 0, 0, 0,
138   0, 0, 0, 0,
139   0, 0, 0, 0,
140   0, 0, 0, 0,
141   0, 0, 0, 0,
142   0, 0, 0, 0,
143   0, 0, 0, 0,
144   0, 0, 0, 0,
145   0, 0, 0, 0,
146   0, 0, 0, 0,
147   0, 0, 0, 0,
148   0, 0, 0, 0,
149   0, 0, 0, 0,
150   0, 0, 0, 0
151 };
152
153 const Int_t AliTOFRawStream::fgkStrip0MapCrate0[]=
154   {3,5,1,9,11,7,15,17,13,2,-1,0,7,9,5,13,15,11,0,-1,17,-1,5,3};
155 const Int_t AliTOFRawStream::fgkStrip1MapCrate0[]=
156   {4,0,2,10,6,8,16,12,14,3,18,1,8,4,6,14,10,12,1,16,18,2,6,4};
157 const Int_t AliTOFRawStream::fgkStrip0MapCrate1[]=
158   {0,2,4,6,8,10,12,14,16,-1,0,2,4,6,8,10,12,14,-1,17,0,2,4,6};
159 const Int_t AliTOFRawStream::fgkStrip1MapCrate1[]=
160   {1,3,5,7,9,11,13,15,17,18,1,3,5,7,9,11,13,15,16,18,1,3,5,7};
161 const Int_t AliTOFRawStream::fgkStrip0MapCrate2[]=
162   {15,13,17,9,7,11,3,1,5,16,-1,18,11,9,13,5,3,7,14,-1,1,-1,9,11};
163 const Int_t AliTOFRawStream::fgkStrip1MapCrate2[]=
164   {14,18,16,8,12,10,2,6,4,15,0,17,10,14,12,4,8,6,13,2,0,12,8,10};
165 const Int_t AliTOFRawStream::fgkStrip0MapCrate3[]=
166   {18,16,14,12,10,8,6,4,2,-1,18,16,14,12,10,8,6,4,-1,1,14,12,10,8};
167 const Int_t AliTOFRawStream::fgkStrip1MapCrate3[]=
168   {17,15,13,11,9,7,5,3,1,0,17,15,13,11,9,7,5,3,2,0,13,11,9,7};
169
170
171 const Int_t AliTOFRawStream::fgkModule0MapCrate0[]=
172   {0,0,0,0,0,0,0,0,0,1,-1,1,1,1,1,1,1,1,2,-1,1,-1,2,2};
173 const Int_t AliTOFRawStream::fgkModule1MapCrate0[]=
174   {0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,2,1,1,2,2,2};
175 const Int_t AliTOFRawStream::fgkModule0MapCrate1[]=
176   {0,0,0,0,0,0,0,0,0,-1,1,1,1,1,1,1,1,1,-1,1,2,2,2,2,2};
177 const Int_t AliTOFRawStream::fgkModule1MapCrate1[]=
178   {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2};
179 const Int_t AliTOFRawStream::fgkModule0MapCrate2[]=
180   {4,4,4,4,4,4,4,4,4,3,-1,3,3,3,3,3,3,3,2,-1,3,-1,2,2};
181 const Int_t AliTOFRawStream::fgkModule1MapCrate2[]=
182   {4,4,4,4,4,4,4,4,4,3,4,3,3,3,3,3,3,3,2,3,3,2,2,2};
183 const Int_t AliTOFRawStream::fgkModule0MapCrate3[]=
184   {4,4,4,4,4,4,4,4,4,-1,3,3,3,3,3,3,3,3,-1,3,2,2,2,2,2};
185 const Int_t AliTOFRawStream::fgkModule1MapCrate3[]=
186   {4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2};
187
188 const Int_t AliTOFRawStream::fgkChannelMap0[5][19]=
189   {{1,2,2,0,0,1,4,5,5,3,3,4,7,8,8,6,6,7,10},
190    {11,11,9,9,13,14,14,12,12,13,16,17,17,15,15,16,19,20,20},
191    {18,18,21,23,23,22,22,23,23,22,22,21,21,20,20,-1,-1,-1,-1},
192    {19,19,18,17,17,16,16,15,15,14,14,13,13,12,12,11,11,10,10},
193    {9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1,0,0}
194   };
195
196 const Int_t AliTOFRawStream::fgkChainMap0[5][19]=
197 {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
198 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
199 {0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,-1,-1,-1,-1},
200 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
201 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
202 };
203
204 const Int_t AliTOFRawStream::fgkChannelMap24[5][19]=
205 {{0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9},
206 {10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,19,19},
207 {20,20,21,21,22,22,23,23,22,22,23,23,21,18,18,-1,-1,-1,-1},
208 {20,20,19,16,15,15,17,17,16,13,12,12,14,14,13,9,9,11,11},
209 {10,7,6,6,8,8,7,4,3,3,5,5,4,1,0,0,2,2,1}
210 };
211
212 const Int_t AliTOFRawStream::fgkChainMap24[5][19]=
213 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
214 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
215 {1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,-1,-1,-1,-1},
216 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
217 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
218 };
219
220 Bool_t AliTOFRawStream::fgApplyBCCorrections = kTRUE;
221 //_____________________________________________________________________________
222 AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
223   fRawReader(rawReader),
224   fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
225   fDecoder(new AliTOFDecoder()),
226   fDDL(-1),
227   fTRM(-1),
228   fTRMchain(-1),
229   fTDC(-1),
230   fTDCchannel(-1),
231   fTime(-1),
232   fToT(-1),
233   fLeadingEdge(-1),
234   fTrailingEdge(-1),
235   fErrorFlag(-1),
236   fSector(-1),
237   fPlate(-1),
238   fStrip(-1),
239   fPadX(-1),
240   fPadZ(-1),
241   fPackedDigits(0),
242   fWordType(-1),
243   fSlotID(-1),
244   fACQ(-1),
245   fPSbit(-1),
246   fTDCerrorFlag(-1),
247   fInsideDRM(kFALSE),
248   fInsideTRM(kFALSE),
249   fInsideLTM(kFALSE),
250   fInsideTRMchain0(kFALSE),
251   fInsideTRMchain1(kFALSE),
252   //fDataBuffer(),
253   //fPackedDataBuffer(),
254   fLocalEventCounterDRM(-1),
255   fLocalEventCounterLTM(-1),
256   //fLocalEventCounterTRM(),
257   //fLocalEventCounterChain(),
258   //fChainBunchID(),
259   //fCableLengthMap(new AliTOFCableLengthMap()),
260   fEventID(0),
261   fNewDecoderVersion(0)
262 {
263   //
264   // create an object to read TOF raw digits
265   //
266
267   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
268     ResetDataBuffer(i);
269     ResetPackedDataBuffer(i);
270   }
271
272   //fTOFrawData = new TClonesArray("AliTOFrawData",1000);
273   fTOFrawData->SetOwner();
274
275   fRawReader->Reset();
276   fRawReader->Select("TOF");
277
278   for (Int_t jj=0;jj<13;jj++) {
279     fLocalEventCounterTRM[jj] = -1;
280     for (Int_t ii=0;ii<2;ii++) {
281       fLocalEventCounterChain[jj][ii] = -1;
282       fChainBunchID[jj][ii] = -1;
283     }
284   }
285
286 }
287
288 //_____________________________________________________________________________
289 AliTOFRawStream::AliTOFRawStream():
290   fRawReader(0x0),
291   fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
292   fDecoder(new AliTOFDecoder()),
293   fDDL(-1),
294   fTRM(-1),
295   fTRMchain(-1),
296   fTDC(-1),
297   fTDCchannel(-1),
298   fTime(-1),
299   fToT(-1),
300   fLeadingEdge(-1),
301   fTrailingEdge(-1),
302   fErrorFlag(-1),
303   fSector(-1),
304   fPlate(-1),
305   fStrip(-1),
306   fPadX(-1),
307   fPadZ(-1),
308   fPackedDigits(0),
309   fWordType(-1),
310   fSlotID(-1),
311   fACQ(-1),
312   fPSbit(-1),
313   fTDCerrorFlag(-1),
314   fInsideDRM(kFALSE),
315   fInsideTRM(kFALSE),
316   fInsideLTM(kFALSE),
317   fInsideTRMchain0(kFALSE),
318   fInsideTRMchain1(kFALSE),
319   //fDataBuffer(),
320   //fPackedDataBuffer(),
321   fLocalEventCounterDRM(-1),
322   fLocalEventCounterLTM(-1),
323   //fLocalEventCounterTRM(),
324   //fLocalEventCounterChain(),
325   //fChainBunchID(),
326   //fCableLengthMap(new AliTOFCableLengthMap()),
327   fEventID(0),
328   fNewDecoderVersion(0)
329 {
330   //
331   // default ctr
332   //
333   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
334     ResetDataBuffer(i);
335     ResetPackedDataBuffer(i);
336   }
337
338   //fTOFrawData = new TClonesArray("AliTOFrawData",1000);
339   fTOFrawData->SetOwner();
340
341   for (Int_t j=0;j<13;j++){
342     fLocalEventCounterTRM[j] = -1;
343     for (Int_t k=0;k<2;k++){
344       fLocalEventCounterChain[j][k] = -1;
345       fChainBunchID[j][k] = -1;
346     }
347   }
348
349 }
350
351 //_____________________________________________________________________________
352 AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
353   TObject(stream),
354   fRawReader(stream.fRawReader),
355   fTOFrawData(stream.fTOFrawData),
356   fDecoder(new AliTOFDecoder()),
357   fDDL(stream.fDDL),
358   fTRM(stream.fTRM),
359   fTRMchain(stream.fTRMchain),
360   fTDC(stream.fTDC),
361   fTDCchannel(stream.fTDCchannel),
362   fTime(stream.fTime),
363   fToT(-stream.fToT),
364   fLeadingEdge(stream.fLeadingEdge),
365   fTrailingEdge(stream.fTrailingEdge),
366   fErrorFlag(stream.fErrorFlag),
367   fSector(stream.fSector),
368   fPlate(stream.fPlate),
369   fStrip(stream.fStrip),
370   fPadX(stream.fPadX),
371   fPadZ(stream.fPadZ),
372   fPackedDigits(stream.fPackedDigits),
373   fWordType(stream.fWordType),
374   fSlotID(stream.fSlotID),
375   fACQ(stream.fACQ),
376   fPSbit(stream.fPSbit),
377   fTDCerrorFlag(stream.fTDCerrorFlag),
378   fInsideDRM(stream.fInsideDRM),
379   fInsideTRM(stream.fInsideTRM),
380   fInsideLTM(stream.fInsideLTM),
381   fInsideTRMchain0(stream.fInsideTRMchain0),
382   fInsideTRMchain1(stream.fInsideTRMchain1),
383   //fDataBuffer(),
384   //fPackedDataBuffer(),
385   fLocalEventCounterDRM(stream.fLocalEventCounterDRM),
386   fLocalEventCounterLTM(stream.fLocalEventCounterLTM),
387   //fLocalEventCounterTRM(),
388   //fLocalEventCounterChain(),
389   //fChainBunchID(),
390   //fCableLengthMap(stream.fCableLengthMap),
391   fEventID(stream.fEventID),
392   fNewDecoderVersion(stream.fNewDecoderVersion)
393 {
394   //
395   // copy constructor
396   //
397
398   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
399     fDataBuffer[i] = stream.fDataBuffer[i];
400     fPackedDataBuffer[i] = stream.fPackedDataBuffer[i];
401   }
402
403   fTOFrawData = new TClonesArray(*stream.fTOFrawData);
404
405   for (Int_t j=0;j<13;j++){
406     fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];
407     for (Int_t k=0;k<2;k++){
408       fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];
409       fChainBunchID[j][k] = stream.fChainBunchID[j][k];
410     }
411   }
412
413 }
414
415 //_____________________________________________________________________________
416 AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
417 {
418   //
419   // assignment operator
420   //
421
422   if (this == &stream)
423     return *this;
424
425   TObject::operator=(stream);
426
427   fRawReader = stream.fRawReader;
428
429   fTOFrawData = stream.fTOFrawData;
430
431   fDDL = stream.fDDL;
432   fTRM = stream.fTRM;
433   fTRMchain = stream.fTRMchain;
434   fTDC = stream.fTDC;
435   fTDCchannel = stream.fTDCchannel;
436   fTime = stream.fTime;
437   fToT = stream.fToT;
438   fLeadingEdge = stream.fLeadingEdge;
439   fTrailingEdge = stream.fTrailingEdge;
440   fErrorFlag = stream.fErrorFlag;
441
442   fSector = stream.fSector;
443   fPlate = stream.fPlate;
444   fStrip = stream.fStrip;
445   fPadX = stream.fPadX;
446   fPadZ = stream.fPadZ;
447
448   fPackedDigits = stream.fPackedDigits;
449
450   fWordType = stream.fWordType;
451   fSlotID = stream.fSlotID;
452   fACQ = stream.fACQ;
453   fPSbit = stream.fPSbit;
454   fTDCerrorFlag = stream.fTDCerrorFlag;
455   fInsideDRM = stream.fInsideDRM;
456   fInsideTRM = stream.fInsideTRM;
457   fInsideLTM = stream.fInsideLTM;
458   fInsideTRMchain0 = stream.fInsideTRMchain0;
459   fInsideTRMchain1 = stream.fInsideTRMchain1;
460   
461   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){ 
462     fDataBuffer[i] = stream.fDataBuffer[i];
463     fPackedDataBuffer[i] = stream.fPackedDataBuffer[i];
464   }
465   
466   fTOFrawData = stream.fTOFrawData;
467
468   fLocalEventCounterDRM = stream.fLocalEventCounterDRM;
469   fLocalEventCounterLTM = stream.fLocalEventCounterLTM;
470   for (Int_t j=0;j<13;j++){
471     fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];
472     for (Int_t k=0;k<2;k++){
473       fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];
474       fChainBunchID[j][k] = stream.fChainBunchID[j][k];
475     }
476   }
477
478   //fCableLengthMap = stream.fCableLengthMap;
479
480   fEventID = stream.fEventID;
481   fNewDecoderVersion = stream.fNewDecoderVersion;
482
483   return *this;
484
485 }
486
487 //_____________________________________________________________________________
488 AliTOFRawStream::~AliTOFRawStream()
489 {
490 // destructor
491
492   fPackedDigits = 0;
493
494   delete fDecoder;
495
496   fTOFrawData->Clear();
497   delete fTOFrawData;
498
499   //delete fCableLengthMap;
500
501 }
502
503
504 //_____________________________________________________________________________
505
506 void AliTOFRawStream::LoadRawData(Int_t indexDDL)
507 {
508   //
509   // To load raw data
510   //
511
512   fEventID = (Int_t)fRawReader->GetBCID(); //bunch crossing
513
514   fTOFrawData->Clear();
515
516   TClonesArray &arrayTofRawData =  *fTOFrawData;
517
518   fPackedDigits = 0;
519
520   // create raw data map
521   AliTOFRawMap rawMap(fTOFrawData);
522   rawMap.Clear();
523
524   Int_t slot[4] = {-1, -1, -1, -1};
525
526   fLocalEventCounterDRM = -1;
527   fLocalEventCounterLTM = -1;
528   for (Int_t ii=0; ii<13; ii++)
529     fLocalEventCounterTRM[ii] = -1;
530   for (Int_t ii=0; ii<13; ii++)
531     for (Int_t jj=0; jj<2; jj++) {
532       fLocalEventCounterChain[ii][jj] = -1;
533       fChainBunchID[ii][jj] = -1;
534     }
535
536   fRawReader->Reset();
537   fRawReader->Select("TOF", indexDDL, indexDDL);
538     
539   Bool_t signal = kFALSE;
540
541   AliTOFrawData *rawDigit = NULL;
542
543   while(Next()) {
544
545     signal = (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1);
546     if (signal) {
547       AliDebug(2,Form("  %2i  %1i  %2i  %1i  %2i", fSector, fPlate, fStrip, fPadZ, fPadX));
548
549       slot[0] = fTRM;
550       slot[1] = fTRMchain;
551       slot[2] = fTDC;
552       slot[3] = fTDCchannel;
553
554       if (rawMap.TestHit(slot) != kEmpty) {
555
556         rawDigit = static_cast<AliTOFrawData*>(rawMap.GetHit(slot));
557
558         if (rawDigit->GetLeading()!=-1 && rawDigit->GetTrailing()==-1 &&
559             fLeadingEdge==-1 && fTrailingEdge!=-1) {
560
561           rawDigit->Update(fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
562         }
563         else if ( ((rawDigit->GetTOF()!=-1 || rawDigit->GetLeading()!=-1 || rawDigit->GetTrailing()!=-1) &&
564                    (fLeadingEdge!=-1 || fTrailingEdge!=-1 || fTime!=-1) )
565
566                   )
567           {
568
569             new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
570
571             rawMap.SetHit(slot);
572
573           }
574
575
576       }
577       else {
578
579         new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
580
581         rawMap.SetHit(slot);
582
583       } // else if (rawMap.TestHit(slot) == kEmpty)
584
585     } // if (signal)
586
587   } // closed -> while (Next())
588
589 }
590
591 //_____________________________________________________________________________
592 Bool_t AliTOFRawStream::Next()
593 {
594   //
595   // Read next 32-bit word in TOF raw data files
596   // returns kFALSE if there is no word left
597   //
598
599   UInt_t data;
600
601   Int_t dummy = 0;
602
603   if (!fRawReader->ReadNextInt(data)) return kFALSE;
604
605   if (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1) {
606     fSector = -1;
607     fPlate  = -1;
608     fStrip  = -1;
609     fPadZ   = -1;
610     fPadX   = -1;
611     fTime   = -1;
612     fToT    = -1;
613     fLeadingEdge  = -1;
614     fTrailingEdge = -1;
615   }
616
617   fDDL  = fRawReader->GetDDLID();
618
619   fWordType = GetField(data,WORD_TYPE_MASK,WORD_TYPE_POSITION);
620
621   switch (fWordType) { // switch word type
622
623   case GLOBAL_HEADER_TYPE: // global header
624     fSlotID = GetField(data, HEADER_SLOT_ID_MASK, HEADER_SLOT_ID_POSITION);
625     fTRM = fSlotID;
626
627
628     switch (fSlotID) { // switch global header slot ID
629
630     case DRM_ID_NUMBER: //DRM global header
631       if (fInsideDRM) { // unexpected DRM global headers -> exit
632         break;
633       }
634       fInsideDRM = kTRUE; // DRM global header accepted
635       break;
636
637     case LTM_ID_NUMBER: // LTM global header
638       if (fInsideLTM) { // unexpected LTM global headers -> exit
639         break;
640       }
641       fInsideLTM = kTRUE; // LTM global header accepted
642       break;
643
644     case  3: //TRM header
645     case  4: //TRM header
646     case  5: //TRM header
647     case  6: //TRM header
648     case  7: //TRM header
649     case  8: //TRM header
650     case  9: //TRM header
651     case 10: //TRM header
652     case 11: //TRM header
653     case 12: //TRM header
654       if (fInsideTRM) { // unexpected TRM global headers -> exit
655         break;
656       }
657       fInsideTRM = kTRUE; // TRM global header accepted
658       fACQ =  GetField(data,TRM_ACQ_BITS_MASK,TRM_ACQ_BITS_POSITION);
659       break;
660
661     default: // unexpected global header slot ID
662       break;
663
664     } //end switch global header slot id
665
666     break;
667
668
669   case GLOBAL_TRAILER_TYPE: // global trailer
670     fSlotID = GetField(data,HEADER_SLOT_ID_MASK,HEADER_SLOT_ID_POSITION);
671
672     switch (fSlotID) { // switch global trailer slot ID
673
674     case DRM_ID_NUMBER: // DRM global trailer
675       if (!fInsideDRM) { // unexpected DRM global trailers -> exit
676         break;
677       }
678       dummy = 0x0000fff0;
679       //AliInfo(Form("  DRM local event counter = %i", GetField(data,dummy,4)));
680       fLocalEventCounterDRM = GetField(data,dummy,4);
681       fInsideDRM = kFALSE; // DRM global trailer accepted
682       fInsideTRM = kFALSE;
683       fInsideLTM = kFALSE;
684       fInsideTRMchain0 = kFALSE;
685       fInsideTRMchain1 = kFALSE;
686       fSector = -1;
687       fPlate  = -1;
688       fStrip  = -1;
689       fPadZ   = -1;
690       fPadX   = -1;
691       fDDL        = -1;
692       fTRM        = -1;
693       fTDC        = -1;
694       fTRMchain   = -1;
695       fTDCchannel = -1;
696       fTime = -1;
697       fToT  = -1;
698       fLeadingEdge  = -1;
699       fTrailingEdge = -1;
700       fErrorFlag = -1;
701       fACQ   = -1;
702       fPSbit = -1;
703       fTDCerrorFlag = -1;
704       break;
705     case LTM_ID_NUMBER: // LTM global trailer
706       if (!fInsideLTM) { // unexpected LTM global trailer -> exit
707         break;
708       }
709       dummy = 0x0fff0000;
710       //AliInfo(Form("  LTM local event counter = %i", GetField(data,dummy,16)));
711       fLocalEventCounterLTM = GetField(data,dummy,16);
712       fInsideLTM = kFALSE; // LTM global trailer accepted
713       break;
714     case 15: //TRM global trailer
715       if (!fInsideTRM) { // unexpected TRM global trailers -> exit
716         break;
717       }
718       dummy = 0x0fff0000;
719       //AliInfo(Form("  TRM local event counter = %i", GetField(data,dummy,16)));
720       fLocalEventCounterTRM[fTRM] = GetField(data,dummy,16);
721       fInsideTRM = kFALSE; // TRM global trailer accepted
722       break;
723     default: // unexpected global trailer slot ID
724       break;
725     } //end switch global trailer slot id
726
727
728     break;
729
730
731   case ERROR_TYPE: // TDC error
732     fTDC          = GetField(data,TRM_TDC_ERROR_TDC_ID_MASK,TRM_TDC_ERROR_TDC_ID_POSITION);
733     fTDCerrorFlag = GetField(data,TRM_TDC_ERROR_FLAGS_MASK,TRM_TDC_ERROR_FLAGS_POSITION);
734     break;
735
736
737   case FILLER_TYPE: // filler
738     break;
739
740
741   default: // other word types
742
743     if (fInsideTRM) { // inside TRM
744
745       switch (fWordType) { // switch word type inside TRM
746       case TRM_CHAIN0_HEADER_TYPE: // TRM chain0 header
747         if (fInsideTRMchain0) { // unexpected TRM chain0 header
748           break;
749         }
750         fInsideTRMchain0 = kTRUE;
751         fTRMchain = 0;
752         dummy = 0x0000fff0;
753         //AliInfo(Form("  chain bunch ID = %i", GetField(data,dummy,4)));
754         fChainBunchID[fTRM][fTRMchain] = GetField(data,dummy,4);
755         break;
756       case TRM_CHAIN0_TRAILER_TYPE: // TRM chain0 trailer
757         if (!fInsideTRMchain0) { // unexpected TRM chain0 trailer
758           break;
759         }
760         dummy = 0x0fff0000;
761         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
762         fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
763         fInsideTRMchain0 = kFALSE;
764         fTRMchain = -1;
765         break;
766       case TRM_CHAIN1_HEADER_TYPE: // TRM chain1 header
767         if (fInsideTRMchain1) { // unexpected TRM chain1 header
768           break;
769         }
770         fInsideTRMchain1 = kTRUE;
771         fTRMchain = 1;
772         dummy = 0x0000fff0;
773         //AliInfo(Form("  chain bunch ID = %i", GetField(data,dummy,4)));
774         fChainBunchID[fTRM][fTRMchain] = GetField(data,dummy,4);
775         break;
776       case TRM_CHAIN1_TRAILER_TYPE: // TRM chain1 trailer
777         if (!fInsideTRMchain1) { // unexpected TRM chain1 trailer
778           break;
779         }
780         dummy = 0x0fff0000;
781         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
782         fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
783         fInsideTRMchain1 = kFALSE;
784         fTRMchain = -1;
785         break;
786       } // end switch word type inside TRM
787
788     } // end if (fInsideTRM)
789
790       
791     if (
792         ((fInsideTRMchain0&&!fInsideTRMchain1) || (!fInsideTRMchain0&&fInsideTRMchain1)) 
793         && fWordType!=TRM_CHAIN0_HEADER_TYPE && fWordType!=TRM_CHAIN0_TRAILER_TYPE
794         && fWordType!=TRM_CHAIN1_HEADER_TYPE && fWordType!=TRM_CHAIN1_TRAILER_TYPE
795         ){ // inside TRM chains
796
797       fPSbit      = GetField(data,TRM_PS_BITS_MASK,TRM_PS_BITS_POSITION);
798       fTDC        = GetField(data,TRM_TDC_ID_MASK,TRM_TDC_ID_POSITION);
799       fTDCchannel = GetField(data,TRM_CHAN_MASK,TRM_CHAN_POSITION);
800       fErrorFlag  = GetField(data,TRM_E_BIT_MASK,TRM_E_BIT_POSITION);
801
802       SetSector();
803       SetPlate();
804       SetStrip();
805       SetPadZ();
806       SetPadX();
807
808
809       switch (fPSbit) { // switch fPSbit bits inside TRM chains
810
811       case 0: // packing ok, digit time and TOT
812         fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
813         fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
814           /*-
815           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
816           ;
817         if (fgApplyBCCorrections) {
818           AliDebug(2,"Apply nominal DDL BC time-shift correction");
819           AliDebug(2,"Apply deltaBC time-shift correction");
820           AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
821           fTime += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
822         }
823         break;
824
825       case 1: // leading edge digit, long digit time, no TOT
826         //fToT  = -1;
827         //fTime  = -1;
828         fLeadingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
829           /*-
830           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
831           ;
832         if (fgApplyBCCorrections) {
833           AliDebug(2,"Apply nominal DDL BC time-shift correction");
834           AliDebug(2,"Apply deltaBC time-shift correction");
835           AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
836           fLeadingEdge += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
837         }
838         break;
839
840       case 2: // trailing edge digit, long digit time, no TOT
841         //fToT  = -1;
842         //fTime  = -1;
843         fTrailingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
844           /*-
845           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
846           ;
847         if (fgApplyBCCorrections) {
848           AliDebug(2,"Apply nominal DDL BC time-shift correction");
849           AliDebug(2,"Apply deltaBC time-shift correction");
850           AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
851           fTrailingEdge += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
852         }
853         break;
854
855       case 3: // TOT overflow
856         fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
857         fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
858           /*-
859           fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
860           ;
861         if (fgApplyBCCorrections) {
862           AliDebug(2,"Apply nominal DDL BC time-shift correction");
863           AliDebug(2,"Apply deltaBC time-shift correction");
864           AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
865           fTime += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
866         }
867         break;
868
869       } // end switch PS bits inside TRM chains
870
871     } // end if is inside TRM chains
872
873   } // end switch on fWordType
874
875
876   return kTRUE;
877   
878 }
879 //_____________________________________________________________________________
880
881 void AliTOFRawStream::SetSector()
882 {
883   //
884   // Evaluate the TOF sector number -> [ 0;17]
885   // corresponding to the TOF equipment IDs:
886   //                                  fDDL        -> [ 0;71]
887   //                                  fTRM        -> [ 3;12]
888   //                                  fTRMchain   -> [ 0; 1]
889   //                                  fTDC        -> [ 0;14]
890   //                                  fTDCchannel -> [ 0; 7]
891   //
892
893   Int_t iSector = -1;
894
895   if (!(fDDL==-1)) iSector = Int_t((Float_t)(fDDL)/AliTOFGeometry::NDDL());
896
897   fSector = iSector;
898
899 }
900 //_____________________________________________________________________________
901
902
903 void AliTOFRawStream::SetPlate()
904 {
905   //
906   // Evaluate the TOF plate number ->[ 0; 4]
907   // corresponding to the TOF equipment IDs:
908   //                                  fDDL        -> [ 0;71]
909   //                                  fTRM        -> [ 3;12]
910   //                                  fTRMchain   -> [ 0; 1]
911   //                                  fTDC        -> [ 0;14]
912   //                                  fTDCchannel -> [ 0; 7]
913   //
914
915   Int_t iPlate = -1;
916   if (!(fDDL==-1 || fTRM==-1 || fTDC==-1
917         || fSector==-1))
918     iPlate = Equip2VolNplate(GetDDLnumberPerSector(fDDL), fTRM, fTDC);
919
920   fPlate = iPlate;
921
922 }
923 //_____________________________________________________________________________
924
925 void AliTOFRawStream::SetStrip()
926 {
927   //
928   // Evaluate the TOF strip number per module -> [ 0; 14/18]
929   // corresponding to the TOF equipment IDs:
930   //                                  fDDL        -> [ 0;71]
931   //                                  fTRM        -> [ 3;12]
932   //                                  fTRMchain   -> [ 0; 1]
933   //                                  fTDC        -> [ 0;14]
934   //                                  fTDCchannel -> [ 0; 7]
935   //
936
937   Int_t iStrip = -1;
938
939   if (!(fDDL==-1 || fTRM==-1 || fTDC==-1
940         || fSector==-1 || fPlate==-1))
941     iStrip = Equip2VolNstrip(GetDDLnumberPerSector(fDDL), fTRM, fTDC);
942
943   fStrip = iStrip;
944
945 }
946 //_____________________________________________________________________________
947
948 void AliTOFRawStream::SetPadZ()
949 {
950   //
951   // Evaluate the TOF padRow number per strip -> [ 0; 1]
952   // corresponding to the TOF equipment IDs:
953   //                                  fDDL        -> [ 0;71]
954   //                                  fTRM        -> [ 3;12]
955   //                                  fTRMchain   -> [ 0; 1]
956   //                                  fTDC        -> [ 0;14]
957   //                                  fTDCchannel -> [ 0; 7]
958   //
959
960   Int_t iPadZ = -1;
961
962   if (!(fDDL==-1 || fTRM==-1 || fTRMchain==-1 || fTDC==-1 || fTDCchannel==-1
963         || fSector==-1 || fPlate==-1 || fStrip==-1))
964     {
965       Int_t iPadAlongTheStrip = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
966       if (iPadAlongTheStrip!=-1)
967         iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
968     }
969
970   //iPadZ = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)%AliTOFGeometry::NpadZ();
971   //iPadZ = Equip2VolNpadZ(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
972
973   fPadZ = iPadZ;
974
975 }
976 //_____________________________________________________________________________
977
978 void AliTOFRawStream::SetPadX()
979 {
980   //
981   // Evaluate the TOF pad number per strip padRow -> [ 0;47]
982   // corresponding to the TOF equipment IDs:
983   //                                  fDDL        -> [ 0;71]
984   //                                  fTRM        -> [ 3;12]
985   //                                  fTRMchain   -> [ 0; 1]
986   //                                  fTDC        -> [ 0;14]
987   //                                  fTDCchannel -> [ 0; 7]
988   //
989
990   Int_t iPadX = -1;
991
992   if (!(fDDL==-1 || fTRM==-1 || fTRMchain==-1 || fTDC==-1 || fTDCchannel==-1
993         || fSector==-1 || fPlate==-1 || fStrip==-1))
994     {
995       Int_t iPadAlongTheStrip = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
996       if (iPadAlongTheStrip!=-1)
997         iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
998     }
999
1000   //iPadX = (Int_t)(Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)/(Float_t(AliTOFGeometry::NpadZ())));
1001   //iPadX = Equip2VolNpadX(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
1002
1003   fPadX = iPadX;
1004
1005 }
1006
1007 //----------------------------------------------------------------------------
1008 Int_t AliTOFRawStream::GetField(UInt_t word, Int_t fieldMask, Int_t fieldPosition) const
1009 {
1010   // 
1011   // Returns 'word' masked by 'fieldMask' and shifted by 'fieldPosition'
1012   // 
1013
1014   return ((word & fieldMask) >> fieldPosition);
1015 }
1016
1017 //----------------------------------------------------------------------------
1018 Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC)
1019 {
1020   //
1021   // Returns the TOF plate number [0;4]
1022   // corresponding to the TOF equipment ID numbers:
1023   //                          iDDL -> DDL number per sector [0;3]
1024   //                          nTRM -> TRM number [3;12]
1025   //                          nTDC -> TDC number [0;14]
1026   //
1027
1028   Int_t iPlate = -1;
1029
1030   if (iDDL==0) {
1031
1032     if (nTRM>=4 && nTRM<7) {
1033       iPlate = 0;
1034     } else if (nTRM==7) {
1035       if (nTDC<12) iPlate = 0;
1036       else iPlate = 1;
1037     } else if (nTRM>=8 && nTRM<11) {
1038       iPlate = 1;
1039     } else if (nTRM==11) {
1040       if (nTDC<9) iPlate = 1;
1041       else iPlate = 2;
1042     }else if (nTRM==12) {
1043       iPlate = 2;
1044     } 
1045
1046   } else if (iDDL==1) {
1047
1048     if (nTRM==3) {
1049       if (nTDC<3) iPlate = 0;
1050     } else if (nTRM>=4 && nTRM<7) {
1051       iPlate = 0;
1052     } else if (nTRM==7) {
1053       if (nTDC<6) iPlate = 1;
1054       else iPlate = 0;
1055     } else if (nTRM>=8 && nTRM<11) {
1056       iPlate = 1;
1057     } else if (nTRM==11) {
1058       if (nTDC<9) iPlate = 2;
1059       else iPlate = 1;
1060     } else if (nTRM==12) {
1061       iPlate = 2;
1062     } 
1063
1064   } else if (iDDL==2) {
1065
1066     if (nTRM>=4 && nTRM<7) {
1067       iPlate = 4;
1068     } else if (nTRM==7) {
1069       if (nTDC<12) iPlate = 4;
1070       else iPlate = 3;
1071     } else if (nTRM>=8 && nTRM<11) {
1072       iPlate = 3;
1073     } else if (nTRM==11) {
1074       if (nTDC<9) iPlate = 3;
1075       else iPlate = 2;
1076     }else if (nTRM==12) {
1077       iPlate = 2;
1078     } 
1079
1080   }  else if (iDDL==3) {
1081
1082     if (nTRM==3) {
1083       if (nTDC<3) iPlate = 4;
1084     } else if (nTRM>=4 && nTRM<7) {
1085       iPlate = 4;
1086     } else if (nTRM==7) {
1087       if (nTDC<6) iPlate = 3;
1088       else iPlate = 4;
1089     } else if (nTRM>=8 && nTRM<11) {
1090       iPlate = 3;
1091     } else if (nTRM==11) {
1092       if (nTDC<9) iPlate = 2;
1093       else iPlate = 3;
1094     } else if (nTRM==12) {
1095       iPlate = 2;
1096     } 
1097
1098   }
1099
1100   return iPlate;
1101
1102 }
1103
1104 //----------------------------------------------------------------------------
1105 Int_t AliTOFRawStream::Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC)
1106 {
1107   //
1108   // Returns the TOF strip number per module:
1109   //                                [0;14], in the central plates,
1110   //                                [0;18], in the intermediate and external plates
1111   // corresponding to the TOF equipment ID numbers:
1112   //                                iDDL -> DDL number per sector [0;3]
1113   //                                nTRM -> TRM number [3;12]
1114   //                                nTDC -> TDC number [0;14]
1115   //
1116
1117   Int_t iStrip = -1;
1118
1119   if (iDDL==0) {
1120
1121     if (nTRM== 4) iStrip =  (Int_t)(nTDC/3.);
1122     else if (nTRM== 5) iStrip =  5 + (Int_t)(nTDC/3.);
1123     else if (nTRM== 6) iStrip = 10 + (Int_t)(nTDC/3.);
1124     else if (nTRM== 7) {
1125       if (nTDC<12) iStrip =  15 + (Int_t)(nTDC/3.);
1126       else iStrip = (Int_t)(nTDC/3.) -  4;
1127     }
1128     else if (nTRM== 8) iStrip =  1 + (Int_t)(nTDC/3.);
1129     else if (nTRM== 9) iStrip =  6 + (Int_t)(nTDC/3.);
1130     else if (nTRM==10) iStrip = 11 + (Int_t)(nTDC/3.);
1131     else if (nTRM==11) {
1132       if (nTDC<9) iStrip = 16 + (Int_t)(nTDC/3.);
1133       else iStrip = (Int_t)(nTDC/3.) -  3;
1134     }
1135     else if (nTRM==12) iStrip =  2 + (Int_t)(nTDC/3.);
1136
1137   } else if (iDDL==1) {
1138
1139     if (nTRM==3 && nTDC<3) iStrip = (Int_t)(nTDC/3.);
1140     else if (nTRM== 4) iStrip =  5 - (Int_t)(nTDC/3.);
1141     else if (nTRM== 5) iStrip = 10 - (Int_t)(nTDC/3.);
1142     else if (nTRM== 6) iStrip = 15 - (Int_t)(nTDC/3.);
1143     else if (nTRM== 7) {
1144       if (nTDC<6) iStrip =  1 - (Int_t)(nTDC/3.);
1145       else iStrip = 20 - (Int_t)(nTDC/3.);
1146     }
1147     else if (nTRM== 8) iStrip =  6 - (Int_t)(nTDC/3.);
1148     else if (nTRM== 9) iStrip = 11 - (Int_t)(nTDC/3.);
1149     else if (nTRM==10) iStrip = 16 - (Int_t)(nTDC/3.);
1150     else if (nTRM==11) {
1151       if (nTDC<9) iStrip =  2 - (Int_t)(nTDC/3.);
1152       else iStrip = 21 - (Int_t)(nTDC/3.);
1153     }
1154     else if (nTRM==12) iStrip =  7 - (Int_t)(nTDC/3.);
1155
1156   } else if (iDDL==2) {
1157
1158     if (nTRM== 4) iStrip =  18 - (Int_t)(nTDC/3.);
1159     else if (nTRM== 5) iStrip = 18 - ( 5 + (Int_t)(nTDC/3.));
1160     else if (nTRM== 6) iStrip = 18 - (10 + (Int_t)(nTDC/3.));
1161     else if (nTRM== 7) {
1162       if (nTDC<12) iStrip =  18 - (15 + (Int_t)(nTDC/3.));
1163       else iStrip = 18 - ((Int_t)(nTDC/3.) -  4);
1164     }
1165     else if (nTRM== 8) iStrip = 18 - ( 1 + (Int_t)(nTDC/3.));
1166     else if (nTRM== 9) iStrip = 18 - ( 6 + (Int_t)(nTDC/3.));
1167     else if (nTRM==10) iStrip = 18 - (11 + (Int_t)(nTDC/3.));
1168     else if (nTRM==11) {
1169       if (nTDC<9) iStrip = 18 - (16 + (Int_t)(nTDC/3.));
1170       else iStrip = 14 - ((Int_t)(nTDC/3.) -  3);
1171     }
1172     else if (nTRM==12) iStrip = 14 - ( 2 + (Int_t)(nTDC/3.));
1173
1174   } else if (iDDL==3) {
1175
1176     if (nTRM==3 && nTDC<3) iStrip = 18 - (Int_t)(nTDC/3.);
1177     else if (nTRM== 4) iStrip = 18 - ( 5 - (Int_t)(nTDC/3.));
1178     else if (nTRM== 5) iStrip = 18 - (10 - (Int_t)(nTDC/3.));
1179     else if (nTRM== 6) iStrip = 18 - (15 - (Int_t)(nTDC/3.));
1180     else if (nTRM== 7) {
1181       if (nTDC<6) iStrip =  18 - (1 - (Int_t)(nTDC/3.));
1182       else iStrip = 18 - (20 - (Int_t)(nTDC/3.));
1183     }
1184     else if (nTRM== 8) iStrip = 18 - ( 6 - (Int_t)(nTDC/3.));
1185     else if (nTRM== 9) iStrip = 18 - (11 - (Int_t)(nTDC/3.));
1186     else if (nTRM==10) iStrip = 18 - (16 - (Int_t)(nTDC/3.));
1187     else if (nTRM==11) {
1188       if (nTDC<9) iStrip = 14 - ( 2 - (Int_t)(nTDC/3.));
1189       else iStrip = 18 - (21 - (Int_t)(nTDC/3.));
1190     }
1191     else if (nTRM==12) iStrip = 14 - ( 7 - (Int_t)(nTDC/3.));
1192
1193   } 
1194
1195   return iStrip;
1196
1197 }
1198
1199 //----------------------------------------------------------------------------
1200 Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
1201                                      Int_t iCH)
1202 {
1203   //
1204   // Returns the TOF pad number per strip [0;95]
1205   // corresponding to the TOF equipment ID numbers:
1206   //                          iDDL -> DDL number per sector [0;3]
1207   //                        iChain -> TRM chain number [0;1]
1208   //                          nTDC -> TDC number [0;14]
1209   //                           iCH -> TDC channel number [0;7]
1210   //
1211
1212   Int_t iPadAlongTheStrip = -1;
1213
1214   // wrong
1215   //Int_t iTDClocal = nTDC%3 + (1-iChain)*3;
1216   //if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal;
1217   //else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal);
1218
1219   // right
1220   Int_t iTDClocal = -1;
1221   Int_t iTDClocal03 = nTDC%3 + (1-iChain)*3;
1222   Int_t iTDClocal12 = 2-nTDC%3 + iChain*3;
1223   if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal03;
1224   else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal12);
1225
1226   Int_t iCHlocal = iCH;
1227   if (iDDL==0 || iDDL==3) iCHlocal = 7 - iCH;
1228
1229   iPadAlongTheStrip = iTDClocal*AliTOFGeometry::NCh() + iCHlocal;
1230
1231   if (((iDDL==1 || iDDL==2) && iPadAlongTheStrip< AliTOFGeometry::NpadX()) ||
1232       ((iDDL==0 || iDDL==3) && iPadAlongTheStrip>=AliTOFGeometry::NpadX())) {
1233     std::cerr << "Warning -> AliTOFRawStream::Equip2VolNpad: Problems with the padX number!\n";
1234     //AliWarning("Problems with the padX number!");
1235   }
1236   return iPadAlongTheStrip;
1237
1238 }
1239
1240 //----------------------------------------------------------------------------
1241 Int_t AliTOFRawStream::Equip2VolNpadX(Int_t iDDL, Int_t iChain, Int_t nTDC,
1242                                       Int_t iCH)
1243 {
1244   //
1245   // Returns the TOF padX number [0;47]
1246   // corresponding to the TOF equipment ID numbers:
1247   //                          iDDL -> DDL number per sector [0;3]
1248   //                        iChain -> TRM chain number [0;1]
1249   //                          nTDC -> TDC number [0;14]
1250   //                           iCH -> TDC channel number [0;7]
1251   //
1252
1253   Int_t iPadX = (Int_t)(AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)/
1254                         (Float_t(AliTOFGeometry::NpadZ())));
1255
1256   return iPadX;
1257
1258 }
1259
1260 //----------------------------------------------------------------------------
1261 Int_t AliTOFRawStream::Equip2VolNpadZ(Int_t iDDL, Int_t iChain, Int_t nTDC,
1262                                       Int_t iCH)
1263 {
1264   //
1265   // Returns the TOF padZ number [0;1]
1266   // corresponding to the TOF equipment ID numbers:
1267   //                          iDDL -> DDL number per sector [0;3]
1268   //                        iChain -> TRM chain number [0;1]
1269   //                          nTDC -> TDC number [0;14]
1270   //                           iCH -> TDC channel number [0;7]
1271   //
1272
1273   Int_t iPadZ  = AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
1274
1275   return iPadZ;
1276
1277 }
1278
1279 //----------------------------------------------------------------------------
1280 Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL)
1281 {
1282   //
1283   // Returns the sector number [0;17]
1284   // corresponing to the assigned DRM/DDL number [0;71]
1285   //
1286
1287   Int_t iSector = Int_t((Float_t)(nDDL)/AliTOFGeometry::NDDL());
1288
1289   return iSector;
1290
1291 }
1292 //----------------------------------------------------------------------------
1293 Int_t AliTOFRawStream::GetDDLnumberPerSector(Int_t nDDL)
1294 {
1295   //
1296   // Return the DRM/DDL number per sector [0;3]
1297   // corresponing to the assigned DRM/DDL number [0;71]
1298   //
1299
1300   Int_t iDDL = nDDL%AliTOFGeometry::NDDL();
1301
1302   return iDDL;
1303
1304 }
1305
1306 //----------------------------------------------------------------------------
1307 void AliTOFRawStream::EquipmentId2VolumeId(AliTOFHitData *hitData, Int_t *volume) const
1308 {
1309   EquipmentId2VolumeId(hitData->GetDDLID(),hitData->GetSlotID(),hitData->GetChain(),hitData->GetTDC(),hitData->GetChan(),volume);
1310 }
1311 //----------------------------------------------------------------------------
1312 void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
1313                                            Int_t nTDC, Int_t iCH,
1314                                            Int_t *volume)
1315 {
1316   //
1317   // To convert:
1318   //            nDDL   (variable in [0;71]) -> number of the DDL file 
1319   //            nTRM   (variable in [3;12]) -> number of the TRM slot
1320   //            iChain (variable in [0; 1]) -> number of the TRM chain
1321   //            nTDC   (variable in [0;14]) -> number of the TDC
1322   //            iCH    (variable in [0; 7]) -> number of the TDC channel
1323   //
1324   // in:
1325   //      sector number, i.e. volume[0] (variable in [0,17])
1326   //      plate  number, i.e. volume[1] (variable in [0, 5])
1327   //      strip  number, i.e. volume[2] (variable in [0,14/18])
1328   //      padX   number, i.e. volume[3] (variable in [0,47])
1329   //      padZ   number, i.e. volume[4] (variable in [0, 1])
1330   //
1331
1332   for (Int_t ii=0; ii<5; ii++) volume[ii] = -1;
1333
1334   Int_t iDDL = GetDDLnumberPerSector(nDDL);
1335
1336   if (iDDL%2==1 && nTRM==3 && nTDC/3>0) { // Signal not coming from a TOF pad but -probably- from a TOF OR signal
1337     //printf("Info -> AliTOFRawStream::EquipmentId2VolumeId: Signal not coming from a TOF pad but -probably- from a TOF OR signal (%2d %2d %2d)\n", nDDL, nTRM, nTDC);
1338     return;
1339   }
1340
1341   Int_t iSector = GetSectorNumber(nDDL);
1342
1343   Int_t iPlate = Equip2VolNplate(iDDL, nTRM, nTDC);
1344   if (iPlate==-1) {
1345     /*if (fRawReader)
1346       fRawReader->AddMajorErrorLog(kPlateError,"plate = -1");*/
1347     printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the plate number (%2d %2d %2d)!\n",
1348            nDDL, nTRM, nTDC);
1349   }
1350
1351   Int_t iStrip = Equip2VolNstrip(iDDL, nTRM, nTDC);
1352   if (iStrip==-1) {
1353     /*if (fRawReader)
1354       fRawReader->AddMajorErrorLog(kStripError,"strip = -1");*/
1355     printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the strip number (%2d %2d %2d)!\n",
1356            nDDL, nTRM, nTDC);
1357   }
1358
1359   Int_t iPadAlongTheStrip  = Equip2VolNpad(iDDL, iChain, nTDC, iCH);
1360   if (iPadAlongTheStrip==-1) {
1361     /*if (fRawReader)
1362       fRawReader->AddMajorErrorLog(kPadAlongStripError,"pad = -1");*/
1363     printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the pad number along the strip (%2d %1d %2d %1d)!\n",
1364            nDDL, iChain, nTDC, iCH);
1365   }
1366   
1367   Int_t iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
1368   Int_t iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
1369
1370   //Int_t iPadX  = (Int_t)(Equip2VolNpad(iDDL, iChain, nTDC, iCH)/(Float_t(AliTOFGeometry::NpadZ())));
1371   //Int_t iPadZ  = Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
1372
1373   //Int_t iPadX  = Equip2VolNpadX(iDDL, iChain, nTDC, iCH);
1374   //Int_t iPadZ  = Equip2VolNpadZ(iDDL, iChain, nTDC, iCH);
1375
1376   volume[0] = iSector;
1377   volume[1] = iPlate;
1378   volume[2] = iStrip;
1379   volume[3] = iPadX;
1380   volume[4] = iPadZ;
1381
1382 }
1383 //-----------------------------------------------------------------------------
1384 Bool_t AliTOFRawStream::DecodeDDL(Int_t nDDLMin, Int_t nDDLMax, Int_t verbose = 0) {
1385   //
1386   // To decode raw data for DDL number in [nDDLmin; nDDLmax]
1387   //
1388
1389   //check and fix valid DDL range
1390   if (nDDLMin < 0){
1391     nDDLMin = 0;
1392     fRawReader->AddMinorErrorLog(kDDLMinError);
1393     AliWarning("Wrong DDL range: setting first DDL ID to 0");
1394   }
1395   if (nDDLMax > 71){
1396     nDDLMax = 71;
1397     fRawReader->AddMinorErrorLog(kDDLMaxError);
1398     AliWarning("Wrong DDL range: setting last DDL ID to 71");
1399   }  
1400
1401   //select required DDLs
1402   fRawReader->Select("TOF", nDDLMin, nDDLMax);
1403
1404   if (verbose)
1405     AliInfo(Form("Selected TOF DDL range: %d-%d", nDDLMin, nDDLMax));
1406
1407   return(Decode(verbose));
1408 }
1409 //-----------------------------------------------------------------------------
1410 Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
1411   //
1412   // New decoder method
1413   //
1414
1415   Int_t currentEquipment;
1416   Int_t currentDDL;
1417   const AliRawDataHeader *currentCDH;
1418
1419   //pointers
1420   UChar_t *data = 0x0;
1421   
1422   //loop and read DDL headers 
1423   while(fRawReader->ReadHeader()){
1424
1425     //memory leak prevention (actually data should be always 0x0 here)
1426     if (data != 0x0)
1427       delete [] data;
1428
1429     //get equipment infos
1430     currentEquipment = fRawReader->GetEquipmentId();
1431     currentDDL = fRawReader->GetDDLID();
1432     currentCDH = fRawReader->GetDataHeader();
1433     const Int_t kDataSize = fRawReader->GetDataSize();
1434     const Int_t kDataWords = kDataSize / 4;
1435     data = new UChar_t[kDataSize];
1436     
1437     if (verbose)
1438       AliInfo(Form("Found equipment # %d header (DDL # %d): %d bytes (%d words)", currentEquipment, currentDDL, kDataSize, kDataWords));
1439     
1440     if (verbose)
1441       AliInfo(Form("Reading equipment #%d (DDL # %d) data...", currentEquipment, currentDDL));
1442     
1443     //read equipment payload
1444     if (!fRawReader->ReadNext(data, kDataSize))
1445       {
1446         fRawReader->AddMajorErrorLog(kDDLdataReading);
1447         if (verbose)
1448           AliWarning("Error while reading DDL data. Go to next equipment");
1449         delete [] data;
1450         data = 0x0;
1451         continue;
1452       }
1453     
1454     if (verbose)
1455       AliInfo(Form("Equipment # %d (DDL # %d) data has been readed", currentEquipment, currentDDL));
1456     
1457     
1458     //set up the decoder
1459     fDecoder->SetVerbose(verbose);
1460     fDecoder->SetDataBuffer(&fDataBuffer[currentDDL]);
1461     fDecoder->SetPackedDataBuffer(&fPackedDataBuffer[currentDDL]);
1462     
1463     //start decoding
1464     if (fDecoder->Decode((UInt_t *)data, kDataWords, currentCDH) == kTRUE) {
1465       fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",currentDDL));
1466       AliWarning(Form("Error while decoding DDL # %d: decoder returned with errors", currentDDL));
1467       ResetDataBuffer(currentDDL);
1468       ResetPackedDataBuffer(currentDDL);
1469     }
1470     
1471     delete [] data;
1472     data = 0x0;
1473   }
1474   
1475   //reset reader
1476   fRawReader->Reset();
1477
1478   if (verbose)
1479     AliInfo("All done");
1480     
1481   return kFALSE;
1482   
1483 }
1484 //---------------------------------------------------------------------------
1485 void
1486 AliTOFRawStream::ResetBuffers()
1487 {
1488   //
1489   // To reset the buffers
1490   //
1491
1492   for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls("TOF"); iDDL++){
1493     ResetDataBuffer(iDDL);
1494     ResetPackedDataBuffer(iDDL);
1495   }
1496 }
1497   
1498 //---------------------------------------------------------------------------
1499 Bool_t
1500 AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
1501 {
1502   //
1503   // To load the buffers
1504   //
1505
1506   fTOFrawData->Clear();
1507   fPackedDigits = 0;
1508   
1509   if (verbose > 0)
1510     AliInfo(Form("Decoding raw data for DDL # %d ...", indexDDL));
1511
1512   if (DecodeDDL(indexDDL, indexDDL, verbose) != 0){ //decode DDL
1513     fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",indexDDL));
1514     AliWarning(Form("Error while decoding DDL # %d", indexDDL));
1515     return kTRUE;
1516   }
1517   
1518   if (verbose > 0)
1519     AliInfo(Form("Done. %d packed %s been found.", fPackedDataBuffer[indexDDL].GetEntries(), fPackedDataBuffer[indexDDL].GetEntries() > 1 ? "hits have" : "hit has"));
1520   
1521   AliTOFHitData *hitData; //hit data pointer
1522   
1523   if (verbose > 0)
1524     AliInfo("Filling TClonesArray ...");
1525
1526   if (verbose > 0)
1527     if (fgApplyBCCorrections) {
1528       AliInfo("Apply nominal DDL BC time-shift correction");
1529       AliInfo("Apply deltaBC time-shift correction");
1530     }
1531
1532   //loop over DDL packed hits
1533   for (Int_t iHit = 0; iHit < fPackedDataBuffer[indexDDL].GetEntries(); iHit++){
1534     hitData = fPackedDataBuffer[indexDDL].GetHit(iHit); //get hit data
1535     Int_t   hitACQ = hitData->GetACQ();
1536     Int_t   hitPS = hitData->GetPS();
1537     Int_t   hitSlotID = hitData->GetSlotID();
1538     Int_t   hitChain = hitData->GetChain();
1539     Int_t   hitTDC = hitData->GetTDC();
1540     Int_t   hitChan = hitData->GetChan();
1541     Int_t   hitTimeBin = hitData->GetTimeBin();
1542     Int_t   hitTOTBin = hitData->GetTOTBin();
1543
1544     if (fgApplyBCCorrections) {
1545       /* DDL BC shift time correction */
1546       hitTimeBin += 1024 * fgkddlBCshift[indexDDL];
1547       /* deltaBC shift time correction */
1548       hitTimeBin += 1024 * (hitData->GetDeltaBunchID() - 37);
1549     }
1550
1551     Int_t hitLeading = hitData->GetTimeBin();
1552     Int_t hitTrailing = -1;
1553     Int_t hitError = -1;
1554     
1555     TClonesArray &arrayTofRawData =  *fTOFrawData;
1556     new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(hitSlotID, hitChain, hitTDC, hitChan, hitTimeBin, hitTOTBin, hitLeading, hitTrailing, hitPS, hitACQ, hitError);
1557   }
1558
1559   if (verbose > 0)
1560     AliInfo("Done.");
1561
1562   if (verbose > 0)
1563     AliInfo("Resetting buffers ...");
1564
1565   fDataBuffer[indexDDL].Reset();
1566   fPackedDataBuffer[indexDDL].Reset();
1567
1568   if (verbose > 0)
1569     AliInfo("Done.");
1570
1571   return kFALSE;
1572 }
1573
1574 //---------------------------------------------------------------------------
1575 void AliTOFRawStream::Geant2EquipmentId(Int_t vol[], Int_t eqId[])
1576 {
1577   //
1578   // To convert:
1579   //      nSector number -vol[0]- (variable in [0,17])
1580   //      nPlate  number -vol[1]- (variable in [0, 5])
1581   //      nStrip  number -vol[2]- (variable in [0,14/18])
1582   //      nPadZ   number -vol[3]- (variable in [0, 1])
1583   //      nPadX   number -vol[4]- (variable in [0,47])
1584   // in:
1585   //      nDDL     -eqId[0]- (variable in [0;71]) -> number of the DDL
1586   //      nTRM     -eqId[1]- (variable in [3;12]) -> number of the TRM
1587   //      nTDC     -eqId[2]- (variable in [0;14]) -> number of the TDC
1588   //      nChain   -eqId[3]- (variable in [0; 1]) -> number of the chain
1589   //      nChannel -eqId[4]- (variable in [0; 8]) -> number of the channel
1590   //
1591
1592   eqId[0] = Geant2DDL(vol);
1593   eqId[1] = Geant2TRM(vol);
1594   eqId[2] = Geant2TDC(vol);
1595   eqId[3] = Geant2Chain(vol);
1596   eqId[4] = Geant2Channel(vol);
1597
1598 }
1599
1600 //---------------------------------------------------------------------------
1601 Int_t AliTOFRawStream::Geant2DDL(Int_t vol[])
1602 {
1603   //
1604   // To convert:
1605   //      nSector number -vol[0]- (variable in [0,17])
1606   //      nPlate  number -vol[1]- (variable in [0, 5])
1607   //      nStrip  number -vol[2]- (variable in [0,14/18])
1608   //      nPadZ   number -vol[3]- (variable in [0, 1])
1609   //      nPadX   number -vol[4]- (variable in [0,47])
1610   // in:
1611   //      nDDL   (variable in [0;71]) -> number of the DDL
1612   //
1613
1614
1615   Int_t iDDL = -1;
1616
1617   if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
1618     printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
1619     return iDDL;
1620   }
1621   if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
1622     printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
1623     return iDDL;
1624   }
1625   if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
1626     printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
1627     return iDDL;
1628   }
1629   if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
1630     printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
1631   if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
1632     printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
1633   if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
1634     printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
1635     return iDDL;
1636   }
1637
1638   Int_t nSector = vol[0];
1639   Int_t nPlate  = vol[1];
1640   Int_t nStrip  = vol[2];
1641   Int_t nPadX   = vol[4];
1642
1643   if ( nPadX<24 && ( nPlate==0 || nPlate==1 || (nPlate==2 && nStrip<7) ) )
1644     iDDL = 0;
1645   else if ( nPadX>=24 && ( nPlate==0 || nPlate==1 || (nPlate==2 && nStrip<8) ) )
1646     iDDL = 1;
1647   else if ( nPadX>=24 && ( nPlate==3 || nPlate==4 || (nPlate==2 && nStrip>7) ) )
1648     iDDL = 2;
1649   else if ( nPadX<24 && ( nPlate==3 || nPlate==4 || (nPlate==2 && nStrip>6) ) )
1650     iDDL = 3;
1651
1652   return 4*nSector+iDDL;
1653
1654 }
1655
1656 //---------------------------------------------------------------------------
1657 Int_t AliTOFRawStream::Geant2TRM(Int_t vol[])
1658 {
1659   //
1660   // To convert:
1661   //      nSector number -vol[0]- (variable in [0,17])
1662   //      nPlate  number -vol[1]- (variable in [0, 5])
1663   //      nStrip  number -vol[2]- (variable in [0,14/18])
1664   //      nPadZ   number -vol[3]- (variable in [0, 1])
1665   //      nPadX   number -vol[4]- (variable in [0,47])
1666   // in:
1667   //      nTRM   (variable in [3;12]) -> number of the TRM slot
1668   //
1669
1670   Int_t nTRM = -1;
1671
1672   if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
1673     printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
1674     return nTRM;
1675   }
1676   if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
1677     printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
1678     return nTRM;
1679   }
1680   if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
1681     printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
1682     return nTRM;
1683   }
1684   if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ()) {
1685     printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
1686     return nTRM;
1687   }
1688   if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX()) {
1689     printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
1690     return nTRM;
1691   }
1692
1693   if ( vol[3]>=AliTOFGeometry::NpadZ() )
1694     {
1695       printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
1696       return nTRM;
1697     }
1698
1699   Int_t nPlate  = vol[1];
1700   Int_t nStrip  = vol[2];
1701
1702   Int_t iDDL = Geant2DDL(vol)%4;
1703
1704   switch (iDDL) {
1705
1706   case 0:
1707
1708     if (nPlate==0) {
1709       if (nStrip<= 4) nTRM =  4;
1710       else if (nStrip> 4 && nStrip<= 9) nTRM =  5;
1711       else if (nStrip> 9 && nStrip<=14) nTRM =  6;
1712       else if (nStrip>14) nTRM =  7;
1713     }
1714     else if (nPlate==1) {
1715       if (nStrip== 0) nTRM =  7;
1716       else if (nStrip> 0 && nStrip<= 5) nTRM =  8;
1717       else if (nStrip> 5 && nStrip<=10) nTRM =  9;
1718       else if (nStrip>10 && nStrip<=15) nTRM = 10;
1719       else if (nStrip>15) nTRM = 11;
1720     }
1721     else if (nPlate==2) {
1722       if (nStrip<= 1) nTRM = 11;
1723       else if (nStrip> 1 && nStrip< 7) nTRM = 12;
1724     }
1725
1726     break;
1727   case 1:
1728
1729     if (nPlate==0) {
1730       if (nStrip== 0) nTRM =  3;
1731       else if (nStrip> 0 && nStrip<= 5) nTRM =  4;
1732       else if (nStrip> 5 && nStrip<=10) nTRM =  5;
1733       else if (nStrip>10 && nStrip<=15) nTRM =  6;
1734       else if (nStrip>15) nTRM =  7;
1735     }
1736     else if (nPlate==1) {
1737       if (nStrip<=1) nTRM = 7;
1738       else if (nStrip> 1 && nStrip<= 6) nTRM =  8;
1739       else if (nStrip> 6 && nStrip<=11) nTRM =  9;
1740       else if (nStrip>11 && nStrip<=16) nTRM = 10;
1741       else if (nStrip>16) nTRM = 11;
1742     }
1743     else if (nPlate==2) {
1744       if (nStrip<= 2) nTRM = 11;
1745       else if (nStrip> 2 && nStrip<= 7) nTRM = 12;
1746     }
1747
1748     break;
1749   case 2:
1750
1751     if (nPlate==4) {
1752       if (nStrip>=14) nTRM =  4;
1753       else if (nStrip<14 && nStrip>= 9) nTRM =  5;
1754       else if (nStrip< 9 && nStrip>= 4) nTRM =  6;
1755       else if (nStrip< 4) nTRM =  7;
1756     }
1757     else if (nPlate==3) {
1758       if (nStrip==18) nTRM =  7;
1759       else if (nStrip<18 && nStrip>=13) nTRM =  8;
1760       else if (nStrip<13 && nStrip>= 8) nTRM =  9;
1761       else if (nStrip< 8 && nStrip>= 3) nTRM = 10;
1762       else if (nStrip< 3) nTRM = 11;
1763     }
1764     else if (nPlate==2) {
1765       if (nStrip>=13) nTRM = 11;
1766       else if (nStrip<13 && nStrip>= 8) nTRM = 12;
1767     }
1768
1769     break;
1770   case 3:
1771
1772     if (nPlate==4) {
1773       if (nStrip==18) nTRM =  3;
1774       else if (nStrip<18 && nStrip>=13) nTRM =  4;
1775       else if (nStrip<13 && nStrip>= 8) nTRM =  5;
1776       else if (nStrip< 8 && nStrip>= 3) nTRM =  6;
1777       else if (nStrip< 3) nTRM =  7;
1778     }
1779     else if (nPlate==3) {
1780       if (nStrip>=17) nTRM = 7;
1781       else if (nStrip<17 && nStrip>=12) nTRM =  8;
1782       else if (nStrip<12 && nStrip>= 7) nTRM =  9;
1783       else if (nStrip< 7 && nStrip>= 2) nTRM = 10;
1784       else if (nStrip< 2) nTRM = 11;
1785     }
1786     else if (nPlate==2) {
1787       if (nStrip>=12) nTRM = 11;
1788       else if (nStrip <12 && nStrip>= 7) nTRM = 12;
1789     }
1790
1791     break;
1792
1793   }
1794
1795   return nTRM;
1796
1797 }
1798
1799 //---------------------------------------------------------------------------
1800 Int_t AliTOFRawStream::Geant2TDC(Int_t vol[])
1801 {
1802   //
1803   // To convert:
1804   //      nSector number -vol[0]- (variable in [0,17])
1805   //      nPlate  number -vol[1]- (variable in [0, 5])
1806   //      nStrip  number -vol[2]- (variable in [0,14/18])
1807   //      nPadZ   number -vol[3]- (variable in [0, 1])
1808   //      nPadX   number -vol[4]- (variable in [0,47])
1809   // in:
1810   //      nTDC   (variable in [0;14]) -> number of the TDC
1811   //
1812
1813   Int_t nTDC = -1;
1814
1815   if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
1816     printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
1817     return nTDC;
1818   }
1819   if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
1820     printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
1821     return nTDC;
1822   }
1823   if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
1824     printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
1825     return nTDC;
1826   }
1827   if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
1828     printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
1829   if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
1830     printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
1831   if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
1832     printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
1833     return nTDC;
1834   }
1835
1836   Int_t nPlate  = vol[1];
1837   Int_t nStrip  = vol[2];
1838   Int_t iPadX   = vol[4];
1839
1840   Int_t iDDL = Geant2DDL(vol)%4;
1841
1842   switch (iDDL) {
1843
1844   case 0:
1845
1846     if (nPlate==0) {
1847       if (nStrip<= 4) nTDC = (3*(nStrip)+2-(iPadX/4)%3);
1848       else if (nStrip> 4 && nStrip<= 9) nTDC = (3*(nStrip- 5)+2-(iPadX/4)%3);
1849       else if (nStrip> 9 && nStrip<=14) nTDC = (3*(nStrip-10)+2-(iPadX/4)%3);
1850       else if (nStrip>14) nTDC =  (3*(nStrip-15)+2-(iPadX/4)%3);
1851     }
1852     else if (nPlate==1) {
1853       if (nStrip== 0) nTDC =  (3*(nStrip+ 4)+2-(iPadX/4)%3);
1854       else if (nStrip> 0 && nStrip<= 5) nTDC = (3*(nStrip- 1)+2-(iPadX/4)%3);
1855       else if (nStrip> 5 && nStrip<=10) nTDC = (3*(nStrip- 6)+2-(iPadX/4)%3);
1856       else if (nStrip>10 && nStrip<=15) nTDC = (3*(nStrip-11)+2-(iPadX/4)%3);
1857       else if (nStrip>15) nTDC = (3*(nStrip-16)+2-(iPadX/4)%3);
1858     }
1859     else if (nPlate==2) {
1860       if (nStrip<= 1) nTDC = (3*(nStrip+ 3)+2-(iPadX/4)%3);
1861       else if (nStrip> 1 && nStrip< 7) nTDC = (3*(nStrip- 2)+2-(iPadX/4)%3);
1862     }
1863
1864     break;
1865   case 1:
1866
1867     if (nPlate==0) {
1868       if (nStrip== 0) nTDC = (3*(nStrip)+(iPadX/4)%3);
1869       else if (nStrip> 0 && nStrip<= 5) nTDC = (3*( 5-nStrip)+(iPadX/4)%3);
1870       else if (nStrip> 5 && nStrip<=10) nTDC = (3*(10-nStrip)+(iPadX/4)%3);
1871       else if (nStrip>10 && nStrip<=15) nTDC = (3*(15-nStrip)+(iPadX/4)%3);
1872       else if (nStrip>15) nTDC = (3*(20-nStrip)+(iPadX/4)%3);
1873     }
1874     else if (nPlate==1) {
1875       if (nStrip<= 1) nTDC = (3*( 1-nStrip)+(iPadX/4)%3);
1876       else if (nStrip> 1 && nStrip<= 6) nTDC = (3*( 6-nStrip)+(iPadX/4)%3);
1877       else if (nStrip> 6 && nStrip<=11) nTDC = (3*(11-nStrip)+(iPadX/4)%3);
1878       else if (nStrip>11 && nStrip<=16) nTDC = (3*(16-nStrip)+(iPadX/4)%3);
1879       else if (nStrip>16) nTDC = (3*(21-nStrip)+(iPadX/4)%3);
1880     }
1881     else if (nPlate==2) {
1882       if (nStrip<= 2) nTDC = (3*( 2-nStrip)+(iPadX/4)%3);
1883       else if (nStrip> 2 && nStrip<= 7) nTDC = (3*( 7-nStrip)+(iPadX/4)%3);
1884     }
1885
1886     break;
1887   case 2:
1888
1889     if (nPlate==4) {
1890       if (nStrip>=14) nTDC = (3*(18-nStrip)+((iPadX/4)%3));
1891       else if (nStrip<14 && nStrip>= 9) nTDC = (3*(13-nStrip)+((iPadX/4)%3));
1892       else if (nStrip< 9 && nStrip>= 4) nTDC = (3*( 8-nStrip)+((iPadX/4)%3));
1893       else if (nStrip< 4) nTDC = (3*( 3-nStrip)+((iPadX/4)%3));
1894     }
1895     else if (nPlate==3) {
1896       if (nStrip==18) nTDC = (3*(22-nStrip)+((iPadX/4)%3));
1897       else if (nStrip<18 && nStrip>=13) nTDC = (3*(17-nStrip)+((iPadX/4)%3));
1898       else if (nStrip<13 && nStrip>= 8) nTDC = (3*(12-nStrip)+((iPadX/4)%3));
1899       else if (nStrip< 8 && nStrip>= 3) nTDC = (3*( 7-nStrip)+((iPadX/4)%3));
1900       else if (nStrip< 3) nTDC = (3*( 2-nStrip)+((iPadX/4)%3));
1901     }
1902     else if (nPlate==2) {
1903       if (nStrip>=13) nTDC = (3*(17-nStrip)+((iPadX/4)%3));
1904       else if (nStrip<13 && nStrip>= 8) nTDC = (3*(12-nStrip)+((iPadX/4)%3));
1905     }
1906
1907     break;
1908   case 3:
1909
1910     if (nPlate==4) {
1911       if (nStrip==18) nTDC = (3*(nStrip-18)+2-(iPadX/4)%3);
1912       else if (nStrip<18 && nStrip>=13) nTDC = (3*(nStrip-13)+2-(iPadX/4)%3);
1913       else if (nStrip<13 && nStrip>= 8) nTDC = (3*(nStrip- 8)+2-(iPadX/4)%3);
1914       else if (nStrip< 8 && nStrip>= 3) nTDC = (3*(nStrip- 3)+2-(iPadX/4)%3);
1915       else if (nStrip< 3) nTDC = (3*(nStrip+ 2)+2-(iPadX/4)%3);
1916     }
1917     else if (nPlate==3) {
1918       if (nStrip>=17) nTDC = (3*(nStrip-17)+2-(iPadX/4)%3);
1919       else if (nStrip<17 && nStrip>=12) nTDC = (3*(nStrip-12)+2-(iPadX/4)%3);
1920       else if (nStrip<12 && nStrip>= 7) nTDC = (3*(nStrip- 7)+2-(iPadX/4)%3);
1921       else if (nStrip< 7 && nStrip>= 2) nTDC = (3*(nStrip- 2)+2-(iPadX/4)%3);
1922       else if (nStrip< 2) nTDC = (3*(nStrip+ 3)+2-(iPadX/4)%3);
1923     }
1924     else if (nPlate==2) {
1925       if (nStrip>=12) nTDC = (3*(nStrip-12)+2-(iPadX/4)%3);
1926       else if (nStrip <12 && nStrip>= 7) nTDC = (3*(nStrip- 7)+2-(iPadX/4)%3);
1927     }
1928
1929     break;
1930
1931   }
1932
1933   return nTDC;
1934
1935 }
1936
1937 //---------------------------------------------------------------------------
1938 Int_t AliTOFRawStream::Geant2Chain(Int_t vol[])
1939 {
1940   //
1941   // To convert:
1942   //      nSector number -vol[0]- (variable in [0,17])
1943   //      nPlate  number -vol[1]- (variable in [0, 5])
1944   //      nStrip  number -vol[2]- (variable in [0,14/18])
1945   //      nPadZ   number -vol[3]- (variable in [0, 1])
1946   //      nPadX   number -vol[4]- variable in [0,47])
1947   // in:
1948   //      nChain (variable in [0; 1]) -> number of the TRM chain
1949   //
1950
1951   Int_t nChain = -1;
1952
1953   if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
1954     printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
1955     return nChain;
1956   }
1957   if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
1958     printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
1959     return nChain;
1960   }
1961   if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
1962     printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
1963     return nChain;
1964   }
1965   if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
1966     printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
1967   if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
1968     printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
1969   if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
1970     printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
1971     return nChain;
1972   }
1973
1974   Int_t iPadX = vol[4];
1975
1976   if (iPadX<12 || iPadX>=36) nChain = 0;
1977   else nChain = 1;
1978
1979   return nChain;
1980
1981 }
1982
1983 //---------------------------------------------------------------------------
1984 Int_t AliTOFRawStream::Geant2Channel(Int_t vol[])
1985 {
1986   //
1987   // To convert:
1988   //      nSector number -vol[0]- (variable in [0,17])
1989   //      nPlate  number -vol[1]- (variable in [0, 5])
1990   //      nStrip  number -vol[2]- (variable in [0,14/18])
1991   //      nPadZ   number -vol[3]- (variable in [0, 1])
1992   //      nPadX   number -vol[4]- (variable in [0,47])
1993   // in:
1994   //      nChannel (variable in [0; 7]) -> number of the TDC channel
1995   //
1996
1997   Int_t nChannel = -1;
1998
1999   if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
2000     printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
2001     return nChannel;
2002   }
2003   if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
2004     printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
2005     return nChannel;
2006   }
2007   if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
2008     printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
2009     return nChannel;
2010   }
2011   if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
2012     printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
2013   if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
2014     printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
2015   if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
2016     printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
2017     return nChannel;
2018   }
2019
2020   Int_t iPadZ   = vol[3];
2021   Int_t iPadX   = vol[4];
2022
2023   Int_t iDDL = Geant2DDL(vol)%4;
2024
2025   switch (iDDL) {
2026
2027   case 0:
2028     nChannel = ((2*(23-iPadX) + (1-iPadZ)))%8;
2029     break;
2030   case 1:
2031     nChannel = ((2*(iPadX-24) + (iPadZ)))%8;
2032     break;
2033   case 2:
2034     nChannel = ((2*(iPadX-24) + (iPadZ)))%8;
2035     break;
2036   case 3:
2037     nChannel = ((2*(23-iPadX) + (1-iPadZ)))%8;
2038     break;
2039   }
2040
2041   return nChannel;
2042
2043 }
2044
2045 //____________________________________________________________________________
2046 void AliTOFRawStream::Raw2Digits(AliRawReader* rawReader, TClonesArray * const digitsArray)
2047 {
2048   //
2049   // Converts raw data to digits for TOF
2050   //
2051
2052   TStopwatch stopwatch;
2053   stopwatch.Start();
2054
2055   //TClonesArray *fDigits = new TClonesArray("AliTOFdigit", 4000);
2056   //digitsTree->Branch("TOF", &fDigits);
2057   TClonesArray &aDigits = *digitsArray;
2058
2059   Int_t inholes = 0;
2060
2061   Clear();
2062   SetRawReader(rawReader);
2063
2064   //ofstream ftxt;
2065   //if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
2066
2067   TClonesArray staticRawData("AliTOFrawData",10000);
2068   staticRawData.Clear();
2069   TClonesArray * clonesRawData = &staticRawData;
2070
2071   Int_t dummy = -1;
2072   Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
2073   Int_t digit[4];
2074
2075   const Int_t kMaxNumberOfTracksPerDigit = 3;
2076   Int_t tracks[kMaxNumberOfTracksPerDigit];
2077   for (Int_t ii=0; ii<kMaxNumberOfTracksPerDigit; ii++)
2078     tracks[ii] = -1;
2079   Int_t last = -1;
2080
2081   Int_t indexDDL = 0;
2082   Int_t iRawData = 0;
2083   AliTOFrawData *tofRawDatum = 0;
2084   for (indexDDL=0; indexDDL<AliDAQ::NumberOfDdls("TOF"); indexDDL++) {
2085
2086     rawReader->Reset();
2087     if (fNewDecoderVersion) {
2088       AliInfo("Using New Decoder \n"); 
2089       LoadRawDataBuffers(indexDDL, 0);
2090     }
2091     else
2092       LoadRawData(indexDDL);
2093
2094     clonesRawData = GetRawData();
2095     if (clonesRawData->GetEntriesFast()!=0) AliInfo(Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
2096     for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
2097
2098       tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
2099
2100       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
2101       if (tofRawDatum->GetTOF()==-1) continue;
2102
2103       EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
2104                            tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
2105
2106       dummy = detectorIndex[3];
2107       detectorIndex[3] = detectorIndex[4];//padz
2108       detectorIndex[4] = dummy;//padx
2109
2110       digit[0] = tofRawDatum->GetTOF();
2111       digit[1] = tofRawDatum->GetTOT();
2112       digit[2] = tofRawDatum->GetTOT();
2113       digit[3] = -1;//tofRawDatum->GetTOF(); //tofND
2114
2115       dummy = detectorIndex[3];
2116       detectorIndex[3] = detectorIndex[4];//padx
2117       detectorIndex[4] = dummy;//padz
2118
2119       // Do not reconstruct anything in the holes
2120       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
2121         if (detectorIndex[1]==2) { // plate with holes
2122           inholes++;
2123           continue;
2124         }
2125       }
2126
2127       last = digitsArray->GetEntriesFast();
2128       new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, digit);
2129       /*
2130       if (fVerbose==2) {
2131         if (indexDDL<10) ftxt << "  " << indexDDL;
2132         else         ftxt << " " << indexDDL;
2133         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
2134         else         ftxt << " " << tofRawDatum->GetTRM();
2135         ftxt << "  " << tofRawDatum->GetTRMchain();
2136         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
2137         else         ftxt << " " << tofRawDatum->GetTDC();
2138         ftxt << "  " << tofRawDatum->GetTDCchannel();
2139
2140         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
2141         else              ftxt  << "  -> " << detectorIndex[0];
2142         ftxt << "  " << detectorIndex[1];
2143         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
2144         else              ftxt << " " << detectorIndex[2];
2145         ftxt << "  " << detectorIndex[4];
2146         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
2147         else              ftxt << " " << detectorIndex[3];
2148
2149         if (digit[1]<10)ftxt << "        " << digit[1];
2150         else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
2151         else ftxt << "      " << digit[1];
2152         if (digit[0]<10) ftxt << "      " << digit[0] << endl;
2153         else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
2154         else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
2155         else ftxt << "   " << digit[3] << endl;
2156       }
2157       */
2158       AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
2159                        last,
2160                        detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
2161                        digit[0], digit[1], digit[3]));
2162
2163       tofRawDatum = 0;
2164     } // loop on tofRawData array
2165
2166     clonesRawData->Clear();
2167
2168   } // DDL Loop
2169
2170   //if (fVerbose==2) ftxt.close();
2171
2172
2173   if (inholes) AliWarning(Form("Raw data in the TOF holes: %d",inholes));
2174
2175   Int_t nDigits = digitsArray->GetEntries();
2176   AliDebug(1, Form("Got %d TOF digits", nDigits));
2177   AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
2178                    stopwatch.RealTime(),stopwatch.CpuTime()));
2179
2180 }
2181
2182 //____________________________________________________________________________
2183 void AliTOFRawStream::Raw2SDigits(AliRawReader* rawReader, TClonesArray * const sdigitsArray)
2184 {
2185   //
2186   // Converts raw data to sdigits for TOF
2187   //
2188
2189   TStopwatch stopwatch;
2190   stopwatch.Start();
2191
2192   Int_t inholes = 0;
2193
2194   //if(!GetLoader()->TreeS()) {MakeTree("S");  MakeBranch("S");}
2195   TClonesArray &aSDigits = *sdigitsArray;
2196
2197   Clear();
2198   SetRawReader(rawReader);
2199
2200   //ofstream ftxt;
2201   //if (fVerbose==2) ftxt.open("TOFsdigitsRead.txt",ios::app);
2202
2203   TClonesArray staticRawData("AliTOFrawData",10000);
2204   staticRawData.Clear();
2205   TClonesArray * clonesRawData = &staticRawData;
2206
2207   Int_t dummy = -1;
2208   Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
2209   Int_t digit[2];
2210   Int_t track = -1;
2211   Int_t last = -1;
2212
2213   Int_t indexDDL = 0;
2214   Int_t iRawData = 0;
2215   AliTOFrawData *tofRawDatum = 0;
2216   for (indexDDL=0; indexDDL<AliDAQ::NumberOfDdls("TOF"); indexDDL++) {
2217
2218     rawReader->Reset();
2219     if (fNewDecoderVersion) {
2220       AliInfo("Using New Decoder \n"); 
2221       LoadRawDataBuffers(indexDDL, 0);
2222     }
2223     else
2224       LoadRawData(indexDDL);
2225
2226     clonesRawData = GetRawData();
2227     if (clonesRawData->GetEntriesFast()!=0) AliInfo(Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
2228     for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
2229
2230       tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
2231
2232       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
2233       if (tofRawDatum->GetTOF()==-1) continue;
2234
2235       EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
2236                            tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
2237
2238       dummy = detectorIndex[3];
2239       detectorIndex[3] = detectorIndex[4];//padz
2240       detectorIndex[4] = dummy;//padx
2241
2242       digit[0] = tofRawDatum->GetTOF();
2243       digit[1] = tofRawDatum->GetTOT();
2244
2245       dummy = detectorIndex[3];
2246       detectorIndex[3] = detectorIndex[4];//padx
2247       detectorIndex[4] = dummy;//padz
2248
2249       // Do not reconstruct anything in the holes
2250       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
2251         if (detectorIndex[1]==2) { // plate with holes
2252           inholes++;
2253           continue;
2254         }
2255       }
2256
2257       last = sdigitsArray->GetEntriesFast();
2258       new (aSDigits[last]) AliTOFSDigit(track, detectorIndex, digit);
2259       /*
2260       if (fVerbose==2) {
2261         if (indexDDL<10) ftxt << "  " << indexDDL;
2262         else         ftxt << " " << indexDDL;
2263         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
2264         else         ftxt << " " << tofRawDatum->GetTRM();
2265         ftxt << "  " << tofRawDatum->GetTRMchain();
2266         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
2267         else         ftxt << " " << tofRawDatum->GetTDC();
2268         ftxt << "  " << tofRawDatum->GetTDCchannel();
2269
2270         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
2271         else              ftxt  << "  -> " << detectorIndex[0];
2272         ftxt << "  " << detectorIndex[1];
2273         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
2274         else              ftxt << " " << detectorIndex[2];
2275         ftxt << "  " << detectorIndex[4];
2276         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
2277         else              ftxt << " " << detectorIndex[3];
2278
2279         if (digit[1]<10)ftxt << "        " << digit[1];
2280         else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
2281         else ftxt << "      " << digit[1];
2282         if (digit[0]<10) ftxt << "      " << digit[0] << endl;
2283         else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
2284         else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
2285         else ftxt << "   " << digit[3] << endl;
2286       }
2287       */
2288       AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
2289                        last,
2290                        detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
2291                        digit[0], digit[1], digit[3]));
2292
2293       tofRawDatum = 0;
2294     } // while loop
2295
2296     clonesRawData->Clear();
2297
2298   } // DDL Loop
2299
2300   //if (fVerbose==2) ftxt.close();
2301
2302   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
2303
2304   Int_t nSDigits = sdigitsArray->GetEntries();
2305   AliDebug(1, Form("Got %d TOF sdigits", nSDigits));
2306   AliDebug(1, Form("Execution time to read TOF raw data and fill TOF sdigit tree : R:%.2fs C:%.2fs",
2307                    stopwatch.RealTime(),stopwatch.CpuTime()));
2308
2309 }
2310
2311 void AliTOFRawStream::VolumeID2LTM(Int_t detind[],
2312                                    Int_t iDDL,
2313                                    Int_t iTRM,
2314                                    Int_t iChain,
2315                                    Int_t iTDC,
2316                                    Int_t iChannel) const {
2317   //
2318   // To convert the TOF trigger macropad ID (i.e. detind)
2319   // into TOF OR signals equipment ID (i.e. iDDL, iTRM, iChain, iTDC, iChannel)
2320   //
2321
2322   const Int_t kFirstTDCnumber = 12;
2323
2324   iDDL=-1, iTRM = 3 , iChain=-1, iTDC=-1, iChannel=-1;
2325   if (detind[1]==0 || detind[1]==1 || (detind[1]==2 && detind[2]<=7)) {
2326     if (detind[4]<24)
2327       iDDL = detind[0]*4;
2328     else
2329       iDDL = detind[0]*4;
2330   }
2331   else {
2332     if (detind[4]<24)
2333       iDDL = detind[0]*4+2;
2334     else
2335       iDDL = detind[0]*4+2;
2336   }
2337
2338   iChain=fgkChainMap24[detind[1]][detind[2]];
2339   iTDC=(Int_t)(fgkChannelMap24[detind[1]][detind[2]]/8)+kFirstTDCnumber;
2340   iChannel=fgkChannelMap24[detind[1]][detind[2]]%8;
2341   
2342 }
2343
2344 void AliTOFRawStream::LTM2VolumeID(Int_t iDDL,
2345                                    Int_t iTRM,
2346                                    Int_t iChain,
2347                                    Int_t iTDC,
2348                                    Int_t iChannel,
2349                                    Int_t detind0[], Int_t detind1[]) const {
2350   //
2351   // To convert the TOF OR signals equipment ID (i.e. iDDL, iTRM, iChain, iTDC, iChannel)
2352   // into TOF trigger macropad IDs (i.e. detind0 and detind1).
2353   // In general, a couple of neighbouring TOF semi-strip represents a TOF trigger macropad.
2354   //
2355
2356   const Int_t kFirstTDCnumber = 12;
2357
2358   Int_t iSector0=-1, iModule0=-1, iStrip0=-1, iPadX0=-1; // Le variabili del Volume ID
2359   Int_t iSector1=-1, iModule1=-1, iStrip1=-1, iPadX1=-1; // Le variabili del Volume ID
2360
2361   if( iDDL%2==1 && iTRM==3 && iTDC-kFirstTDCnumber>=0 && iTDC-kFirstTDCnumber<3 ) {
2362     iSector0 = (Int_t)(iDDL/4);
2363     iSector1 = (Int_t)(iDDL/4);
2364     Int_t iChan= iChannel+(iTDC-kFirstTDCnumber)*8;
2365     if(iDDL%4 == 0 || iDDL%4 == 1 ){
2366       if(iChain==0){      //CRATE 0
2367         iPadX0=0;
2368         iPadX1=0;
2369         iStrip0=fgkStrip0MapCrate0[iChan];
2370         iStrip1=fgkStrip1MapCrate0[iChan];
2371         iModule0=fgkModule0MapCrate0[iChan];
2372         iModule1=fgkModule1MapCrate0[iChan];
2373       }
2374       if(iChain==1){// CRATE 1
2375         iPadX0=24;
2376         iPadX1=24;
2377         iStrip0=fgkStrip0MapCrate1[iChan];
2378         iStrip1=fgkStrip1MapCrate1[iChan];
2379         iModule0=fgkModule0MapCrate1[iChan];
2380         iModule1=fgkModule1MapCrate1[iChan];
2381       }
2382
2383     }
2384     if(iDDL%4 == 2 || iDDL%4 == 3 ){
2385       if(iChain==1){// CRATE 3
2386         iPadX0=0;
2387         iPadX1=0;
2388         iStrip0=fgkStrip0MapCrate3[iChan];
2389         iStrip1=fgkStrip1MapCrate3[iChan];
2390         iModule0=fgkModule0MapCrate3[iChan];
2391         iModule1=fgkModule1MapCrate3[iChan];
2392       }
2393       if(iChain==0){// CRATE 2
2394         iPadX0=24;
2395         iPadX1=24;
2396         iStrip0=fgkStrip0MapCrate2[iChan];
2397         iStrip1=fgkStrip1MapCrate2[iChan];
2398         iModule0=fgkModule0MapCrate2[iChan];
2399         iModule1=fgkModule1MapCrate2[iChan];
2400       }
2401     }
2402   }
2403   detind1[0]=iSector1;
2404   detind1[1]=iModule1;
2405   detind1[2]=iStrip1;
2406   detind1[3]=iPadX1;
2407   detind1[4]=0;
2408   detind0[0]=iSector0;
2409   detind0[1]=iModule0;
2410   detind0[2]=iStrip0;
2411   detind0[3]=iPadX0;
2412   detind0[4]=0;
2413 }