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