]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0RawData.cxx
Fix for bug #60504: Error in AliEMCALRawUtils causing a interruption of simulation...
[u/mrichter/AliRoot.git] / T0 / AliT0RawData.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  T0 raw data conversion class                                            //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 //#include <Riostream.h>
25 //#include <TTree.h>
26 #include <TMap.h>
27 #include "AliT0.h"
28 #include "AliT0RawData.h"
29 #include "AliT0digit.h"
30 #include "AliBitPacking.h"
31 #include "AliRawDataHeader.h"
32 #include "AliRawDataHeaderSim.h"
33 #include "AliBitPacking.h"
34 #include "AliFstream.h"
35 #include "AliRunLoader.h"
36 #include "AliDAQ.h"
37 #include "AliT0LookUpValue.h"
38 #include "AliT0LookUpKey.h"
39
40 ClassImp(AliT0RawData)
41
42 //_____________________________________________________________________________
43   AliT0RawData::AliT0RawData():TObject(),
44                                fVerbose(0),      
45                                fIndex(-1) ,     
46                                fEventNumber(0), 
47                                fTimeCFD(new TArrayI(24)),    
48                                fADC1( new TArrayI(24)),     
49                                fTimeLED( new TArrayI(24)), 
50                                fADC0( new TArrayI(24)),     
51                                fFile(0x0),   
52                                fDataHeaderPos(0),
53                                fDRMDataHeaderPos(0),
54                                fTRMDataHeaderPos(0),
55                                fParam(0),
56                                fLookUp(0)
57   
58
59 {
60   /*
61 -  48 channels (2 words each as in TOF DDL) for :
62 word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
63 and the same but for amplified signal. Now I wrote the same time because
64 CDF are not ready and differences didn't measured yet.
65
66 -  48 channel for amplitude: very preliminary, QTC features are not
67 known now, preliminary i put as T1 time signal for this PMT in first
68 channel and T1+A in second, where A=Log(Amplitude);
69 and the same for amplified but A=Log(10*Amplitude).
70
71 - T0-A and T0-C 2 channels
72 - T0A-T0C vertex information
73 - Time Meaner where T0C TOF increase to the T0A TOF distance
74 - 6 multiplicity signals the same way as amplitude and with the same
75 uncertances
76   */
77
78   //open the output file
79   char fileName[15];
80   strcpy(fileName,AliDAQ::DdlFileName("T0",0)); //The name of the output file
81   fFile = new AliFstream(fileName);
82   memset(fBuffer,0,512*sizeof(UInt_t));
83
84   //get event number 
85   AliRunLoader *runloader = AliRunLoader::Instance();
86   if (runloader) {
87     fEventNumber = runloader->GetEventNumber();
88   }
89
90   // Inverse lookup table for simulation
91
92   fParam = AliT0Parameters::Instance();
93   fParam->Init();
94   AliT0LookUpKey* lookkey= new AliT0LookUpKey();
95   AliT0LookUpValue*  lookvalue= new AliT0LookUpValue();
96   TMap *lookup = fParam->GetMapLookup();
97   TMapIter iter(lookup);
98
99   for( Int_t iline=0; iline<106; iline++)
100     {
101       lookvalue = ( AliT0LookUpValue*) iter.Next();
102       lookkey = (AliT0LookUpKey*) lookup->GetValue(lookvalue);
103       fLookUp.Add(lookkey, lookvalue);
104       lookkey= new AliT0LookUpKey();
105       lookvalue= new AliT0LookUpValue();
106     }
107     
108 }
109
110 //_____________________________________________________________________________
111
112 AliT0RawData::AliT0RawData(const AliT0RawData &r):TObject(),
113                                                   fVerbose(0),      
114                                                   fIndex(-1) ,     
115                                                   fEventNumber(0), 
116                                                   fTimeCFD(new TArrayI(24)),    
117                                                   fADC1( new TArrayI(24)),     
118                                                   fTimeLED( new TArrayI(24)), 
119                                                   fADC0( new TArrayI(24)),     
120                                                   fFile(0x0),   
121                                                   fDataHeaderPos(0),
122                                                   fDRMDataHeaderPos(0),
123                                                   fTRMDataHeaderPos(0),
124                                                   fParam(0),
125                                                   fLookUp(0)
126
127 {
128   //
129   // AliT0rawData copy constructor
130   //
131
132   ((AliT0RawData &) r).Copy(*this);
133
134 }
135
136 //_____________________________________________________________________________
137 AliT0RawData::~AliT0RawData()
138 {
139   //
140   // Destructor
141   //
142   delete fTimeCFD;
143   delete fADC1;
144   delete fTimeLED;
145   delete fADC0;
146   delete fFile;
147 }
148
149 //_____________________________________________________________________________
150 AliT0RawData &AliT0RawData::operator=(const AliT0RawData &r)
151 {
152   //
153   // Assignment operator
154   //
155
156   if (this != &r) ((AliT0RawData &) r).Copy(*this);
157   return *this;
158
159 }
160
161 //_____________________________________________________________________________
162 void AliT0RawData::GetDigits(AliT0digit *fDigits)
163 //void AliT0RawData::GetDigits(fDigits)
164 {
165  
166   //This method packs the T0 digits in a proper 32 bits structure
167
168   //read T0 digits and fill TDC and ADC arrays
169
170
171   //  Int_t error=0;
172   Int_t time,  positionOfTRMHeader;
173   
174   // Get the digits array
175   
176   fDigits->GetTimeCFD(*fTimeCFD);
177   fDigits->GetQT0(*fADC1);
178   fDigits->GetTimeLED(*fTimeLED);
179   fDigits->GetQT1(*fADC0);
180   Int_t meantime = fDigits->MeanTime(); 
181   Int_t timediff = fDigits->TimeDiff(); 
182   Int_t mult0=fDigits->SumMult();
183   Int_t mult1=fDigits->SumMult();
184   Int_t timeA = fDigits->BestTimeC();
185   Int_t timeC = fDigits->BestTimeA();
186   
187   
188   //  TArrayI  *allData = new TArrayI(110);
189   Int_t allData[110][1];
190   for (Int_t i=0; i<110; i++) allData[i][0] = 0;
191
192   allData[0][0]=0;
193   for (Int_t i=1; i<13; i++) {
194     allData[i][0]    = fTimeCFD->At(i-1);
195     allData[i+12][0] = fTimeLED->At(i-1);
196     allData[i+56][0] = fTimeCFD->At(i-1+12);
197     allData[i+68][0] = fTimeLED->At(i-1+12);
198   }
199   
200   for (Int_t iii=0; iii<12; iii++) {
201     allData[2*iii+25][0] = fADC1->At(iii);
202     allData[2*iii+26][0] = fADC0->At(iii);
203   }
204   for (Int_t ii=12; ii<24; ii++) {
205     allData[2*ii+57][0] = fADC1->At(ii);
206     allData[2*ii+58][0] = fADC0->At(ii);
207   }
208   
209   allData[49][0] = meantime;
210   allData[50][0] = timediff;
211   allData[51][0] = timeA;
212   allData[52][0] = timeC;
213   allData[53][0] = mult0;
214   allData[54][0] = mult1;
215   allData[55][0] = mult0;
216   allData[56][0] = mult1;
217
218   //    cout.setf( ios_base::hex, ios_base::basefield );
219   //space for DRM header
220   fIndex += 6;
221
222
223   Int_t startTRM=fIndex;
224   //space for 1st TRM header
225   fIndex ++;
226   positionOfTRMHeader= fIndex;
227   //space for chain  header
228   fIndex ++;
229   WriteChainDataHeader(1, 1); // 
230
231   //  fIndex++;
232   // Loop through all PMT
233   Int_t chain=0; 
234   Int_t iTDC = 0;
235   Int_t channel=0;
236   Int_t trm1words=0;
237   Int_t itrm=7;
238   Int_t inside =0;
239   Int_t isData = 0;
240   AliT0LookUpKey * lookkey  = new AliT0LookUpKey();
241   AliT0LookUpValue * lookvalue ;//= new AliT0LookUpValue(trm,tdc,chain,channel);
242   for (Int_t det = 0; det < 105; det++) {
243     time = allData[det][0];
244     if (time >0 && time !=99999) {
245       lookkey->SetKey(det);
246       lookvalue = (AliT0LookUpValue*) fLookUp.GetValue((TObject*)lookkey);     
247       if (lookvalue ) 
248         {
249           isData++;
250           itrm= lookvalue->GetTRM();
251           if (det >56 &&inside == 0)  {
252             WriteChainDataTrailer(1); // 1st chain trailer
253             fIndex++;
254             WriteChainDataHeader(2, 1);
255             //      fIndex++;
256             inside++;
257           }         
258           chain = lookvalue->GetChain();
259           iTDC = lookvalue->GetTDC();
260           channel = lookvalue->GetChannel();
261           FillTime(channel,  iTDC,  time);
262           AliDebug(1,Form("look %i  itrm %i ,  chain %i , iTDC %i, channel %i time %i", det,itrm,chain,iTDC,channel, time));
263         }
264       else
265         {
266           cout<<" no lookup value for key "<<det<<endl;
267           //  break;
268         }
269     }
270     
271   }
272   if (inside==0) {
273     WriteChainDataTrailer(1); // 1st chain trailer
274     fIndex++;
275     WriteChainDataHeader(2, 1);
276   }
277     //  WriteChainDataHeader(2, 1); // 
278   WriteChainDataTrailer(3); // 2st chain trailer
279   WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
280   
281   
282   trm1words = fIndex - startTRM;
283   //space for 2st TRM header
284   
285   WriteTRMDataHeader(itrm, trm1words , positionOfTRMHeader);
286   
287   //DRM trailer
288   WriteTrailer(1,0,fEventNumber,5);
289     
290     WriteDRMDataHeader();
291     
292 }
293
294 //_____________________________________________________________________________
295
296 void  AliT0RawData::WriteDRMDataHeader()
297 {
298 //Write a (dummy or real) DDL DRM  data header, 
299 //set the compression bit if compressed
300 //  UInt_t drmheader[4];  
301   UInt_t word;
302   UInt_t baseWord=0;
303   //fill DRM headers
304   //DRM Global Header
305   word = 1;
306   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
307   word = fIndex ;
308
309   AliBitPacking::PackWord(word,baseWord,4, 20); // event words 
310   word=124;
311   AliBitPacking::PackWord(word,baseWord, 21, 27); // event words 
312   word=4;
313   AliBitPacking::PackWord(word,baseWord,28, 31);// 0100 marks header
314   fBuffer[0]=  baseWord;
315
316
317   //DRM status header 1
318   word = 1;
319   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
320   word = 1;
321   AliBitPacking::PackWord(word,baseWord,4, 14); // slotID now 0000000001
322   word = 1;
323   AliBitPacking::PackWord(word,baseWord,15, 15); //if 1  LHC clock is coorectly recieved from CPDM 
324   word=0;
325   AliBitPacking::PackWord(word,baseWord,16,27); // reserve for future use
326   word=4;
327   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
328   fBuffer[1] = baseWord;
329   
330    word=0;
331    baseWord=0;
332    
333    //DRM status header 2
334    word = 1;
335    AliBitPacking::PackWord(word,baseWord, 0, 3); // 0001 
336    word = 3;
337    AliBitPacking::PackWord(word,baseWord, 4, 14); //enable slotID now 00000000011
338    word = 0;
339    AliBitPacking::PackWord(word,baseWord, 15, 15); // something
340    word=0;
341    AliBitPacking::PackWord(word,baseWord, 16, 27); // fault ID for simulation 0
342    word=4;
343    AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
344    fBuffer[2]=  baseWord;
345         
346    word=0;
347    baseWord=0;
348    //DRM status header 3
349    word = 1;
350     AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
351     word = 0;
352     AliBitPacking::PackWord(word,baseWord,4, 27); // TTC event counter
353     word=4;
354     AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
355     fBuffer[3]=  baseWord;
356
357     // new DRM format
358     fBuffer[4]=  baseWord;
359     fBuffer[5]=  baseWord;
360    
361     word=0;
362     baseWord=0;
363     
364 }
365   
366 //_____________________________________________________________________________
367
368 void  AliT0RawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
369                                           Int_t  positionOfTRMHeader)
370 {
371 //Write a (dummy or real) DDL TRM  data header, 
372 //set the compression bit if compressed
373 //  UInt_t trmheader;  
374   UInt_t word;
375   UInt_t baseWord=0;
376   //fill TRM headers
377   //TRM Global Header
378   word = slotID;
379   AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
380   word = nWordsInTRM;
381  //+this word - DRM header 
382
383   AliBitPacking::PackWord(word,baseWord,4, 16); // event words 
384   word=0;
385   AliBitPacking::PackWord(word,baseWord,17,18); // ACQ
386   word=0;
387   AliBitPacking::PackWord(word,baseWord,19,19); //  L SEY inside LUT
388   word=0;
389   AliBitPacking::PackWord(word,baseWord,20,27); //  MBZ
390   word=4;
391   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
392   fBuffer[positionOfTRMHeader] =  baseWord;
393
394   word=0; 
395   baseWord=0;
396      
397 }
398
399 //_____________________________________________________________________________
400
401 void  AliT0RawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
402 {
403 //Write a (dummy or real) DDL Chain  data header, 
404 //set the compression bit if compressed
405 //  chainNumber 00 or 10
406   UInt_t word;
407   UInt_t baseWord=0;
408   //fill TRM headers
409   //TRM Global Header
410   word = slotID; // ask Tatiana 7 or 9 
411   AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
412   word = 0;
413   AliBitPacking::PackWord(word,baseWord,4, 15); // bunchID
414   word=0;
415   AliBitPacking::PackWord(word,baseWord,16,23); // PB24 temperature
416   word=0;
417   AliBitPacking::PackWord(word,baseWord,24,26); //  PB24 ID
418   word=0;
419   AliBitPacking::PackWord(word,baseWord,27,27); //  TS
420   word=chainNumber;
421   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
422   fBuffer[fIndex] =  baseWord;
423   //cout<<" chain header "<<baseWord<<" number "<<chainNumber<<endl;
424   word=0;
425   baseWord=0;     
426   
427 }
428 //_____________________________________________________________________________
429
430 void  AliT0RawData::WriteChainDataTrailer(UInt_t chainNumber )
431 {
432 //Write a (dummy or real) DDL Chain  data trailer 
433 //set the compression bit if compressed
434 //  chainNumber 00 or 10
435   UInt_t word;
436   UInt_t baseWord=0;
437   word = 0; // ask Tatiana 7 or 9 
438   AliBitPacking::PackWord(word,baseWord,0, 3); // status
439   word = 0;
440   AliBitPacking::PackWord(word,baseWord,4, 15); // MBZ
441   word=fEventNumber;
442   AliBitPacking::PackWord(word,baseWord,16,27); // event counter
443   word=chainNumber;
444   AliBitPacking::PackWord(word,baseWord,28,31); // chain number
445   fIndex++;
446   fBuffer[fIndex] =  baseWord;
447
448   word=0;
449   baseWord=0;     
450   
451 }
452 //_____________________________________________________________________________
453
454 void  AliT0RawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
455 {
456 //Write a (dummy or real) DDL data header, 
457 //set the compression bit if compressed
458
459   AliRawDataHeaderSim header;
460   
461   if (dummy) {
462     //if size=0 it means that this data header is a dummy data header
463     fDataHeaderPos = fFile->Tellp();
464     fFile->WriteBuffer((char*)(&header), sizeof(header));
465   } else {
466     UInt_t currentFilePos = fFile->Tellp();
467     fFile->Seekp(fDataHeaderPos);
468     header.fSize = currentFilePos-fDataHeaderPos;
469     header.SetAttribute(0);  // valid data
470     if (compressed) header.SetAttribute(1); 
471     fFile->WriteBuffer((char*)(&header), sizeof(header));
472     fFile->Seekp(currentFilePos);
473   }
474   
475 }
476
477 //___ __________________________________________________________________________
478
479
480 void  AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
481 {
482 //Write a (dummy or real) DDL Chain  data trailer 
483
484   UInt_t word;
485   UInt_t baseWord=0;
486   word = slot;
487   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
488   word=word1;
489   AliBitPacking::PackWord(word,baseWord,4, 15); // CRC ?
490   word = word2;
491   AliBitPacking::PackWord(word,baseWord,16,27); // event counter
492   word=word3;
493   AliBitPacking::PackWord(word,baseWord,28,31); //  marks trailer
494   fIndex++;
495   fBuffer[fIndex] =  baseWord;
496
497   word=0;
498   baseWord=0;
499
500 }
501
502 //---------------------------------------------------------------------------------------
503 //---------------------------------------------------------------------------------------
504 void  AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
505 {
506   //  put all time fields thother in 1 word
507
508   UInt_t word;
509   UInt_t baseWord=0;
510
511   word=time;
512   AliBitPacking::PackWord(word,baseWord, 0, 20); // Time 
513
514   word=ch;
515   AliBitPacking::PackWord(word,baseWord, 21, 23); // number of channel 
516   word=iTDC;
517   AliBitPacking::PackWord(word,baseWord, 24, 27); // TDC ID
518
519   word=0;
520   AliBitPacking::PackWord(word,baseWord, 28, 28); // E = 0 in simulation
521   word=0;
522   AliBitPacking::PackWord(word,baseWord, 29, 30); // PS bit data 00
523   word=1;
524   AliBitPacking::PackWord(word,baseWord, 31, 31); // 1
525   fIndex++;
526   fBuffer[fIndex]=baseWord;
527
528   word=0;
529   baseWord=0;
530 }
531 //---------------------------------------------------------------------------------------
532
533 Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
534   //Int_t AliT0RawData::RawDataT0(*fDigits)
535 {
536    //This method creates the Raw data files for T0 detector
537
538
539   // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector() 
540                           //max number of digits per DDL file times 2
541   //  UInt_t fBuffer[kSize];
542   //  UInt_t baseWord;
543   // UInt_t word;
544
545   fIndex=-1;
546  
547
548    WriteDataHeader(kTRUE, kFALSE);
549   GetDigits(fDigits);
550   //write packing digits
551   
552   
553   fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
554   //write real data header on its place
555    WriteDataHeader(kFALSE, kFALSE);
556   
557   
558   //end for
559   
560   return 0;  
561   
562 }