]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0RawData.cxx
Readme for calibration/performance train (Marian)
[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 }
147
148 //_____________________________________________________________________________
149 AliT0RawData &AliT0RawData::operator=(const AliT0RawData &r)
150 {
151   //
152   // Assignment operator
153   //
154
155   if (this != &r) ((AliT0RawData &) r).Copy(*this);
156   return *this;
157
158 }
159
160 //_____________________________________________________________________________
161 void AliT0RawData::GetDigits(AliT0digit *fDigits)
162 //void AliT0RawData::GetDigits(fDigits)
163 {
164  
165   //This method packs the T0 digits in a proper 32 bits structure
166
167   //read T0 digits and fill TDC and ADC arrays
168
169
170   //  Int_t error=0;
171   Int_t time,  positionOfTRMHeader;
172   
173   // Get the digits array
174   
175   fDigits->GetTimeCFD(*fTimeCFD);
176   fDigits->GetQT0(*fADC1);
177   fDigits->GetTimeLED(*fTimeLED);
178   fDigits->GetQT1(*fADC0);
179   Int_t meantime = fDigits->MeanTime(); 
180   Int_t timediff = fDigits->TimeDiff(); 
181   Int_t mult0=fDigits->SumMult();
182   Int_t mult1=fDigits->SumMult();
183   Int_t timeA = fDigits->BestTimeC();
184   Int_t timeC = fDigits->BestTimeA();
185   
186   
187   //  TArrayI  *allData = new TArrayI(110);
188   Int_t allData[110][1];
189   for (Int_t i=0; i<110; i++) allData[i][0] = 0;
190
191   allData[0][0]=0;
192   for (Int_t i=1; i<13; i++) {
193     allData[i][0]    = fTimeCFD->At(i-1);
194     allData[i+12][0] = fTimeLED->At(i-1);
195     allData[i+56][0] = fTimeCFD->At(i-1+12);
196     allData[i+68][0] = fTimeLED->At(i-1+12);
197   }
198   
199   for (Int_t iii=0; iii<12; iii++) {
200     allData[2*iii+25][0] = fADC1->At(iii);
201     allData[2*iii+26][0] = fADC0->At(iii);
202   }
203   for (Int_t ii=12; ii<24; ii++) {
204     allData[2*ii+57][0] = fADC1->At(ii);
205     allData[2*ii+58][0] = fADC0->At(ii);
206   }
207   
208   allData[49][0] = meantime;
209   allData[50][0] = timediff;
210   allData[51][0] = timeA;
211   allData[52][0] = timeC;
212   allData[53][0] = mult0;
213   allData[54][0] = mult1;
214   allData[55][0] = mult0;
215   allData[56][0] = mult1;
216
217   //    cout.setf( ios_base::hex, ios_base::basefield );
218   //space for DRM header
219   fIndex += 6;
220
221
222   Int_t startTRM=fIndex;
223   //space for 1st TRM header
224   fIndex ++;
225   positionOfTRMHeader= fIndex;
226   //space for chain  header
227   fIndex ++;
228   WriteChainDataHeader(1, 1); // 
229
230   //  fIndex++;
231   // Loop through all PMT
232   Int_t chain=0; 
233   Int_t iTDC = 0;
234   Int_t channel=0;
235   Int_t trm1words=0;
236   Int_t itrm=7;
237   Int_t inside =0;
238   Int_t isData = 0;
239   AliT0LookUpKey * lookkey  = new AliT0LookUpKey();
240   AliT0LookUpValue * lookvalue ;//= new AliT0LookUpValue(trm,tdc,chain,channel);
241   for (Int_t det = 0; det < 105; det++) {
242     time = allData[det][0];
243     if (time >0 && time !=99999) {
244       lookkey->SetKey(det);
245       lookvalue = (AliT0LookUpValue*) fLookUp.GetValue((TObject*)lookkey);     
246       if (lookvalue ) 
247         {
248           isData++;
249           itrm= lookvalue->GetTRM();
250           if (det >56 &&inside == 0)  {
251             WriteChainDataTrailer(1); // 1st chain trailer
252             fIndex++;
253             WriteChainDataHeader(2, 1);
254             //      fIndex++;
255             inside++;
256           }         
257           chain = lookvalue->GetChain();
258           iTDC = lookvalue->GetTDC();
259           channel = lookvalue->GetChannel();
260           FillTime(channel,  iTDC,  time);
261           AliDebug(1,Form("look %i  itrm %i ,  chain %i , iTDC %i, channel %i time %i", det,itrm,chain,iTDC,channel, time));
262         }
263       else
264         {
265           cout<<" no lookup value for key "<<det<<endl;
266           //  break;
267         }
268     }
269     
270   }
271   if (inside==0) {
272     WriteChainDataTrailer(1); // 1st chain trailer
273     fIndex++;
274     WriteChainDataHeader(2, 1);
275   }
276     //  WriteChainDataHeader(2, 1); // 
277   WriteChainDataTrailer(3); // 2st chain trailer
278   WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
279   
280   
281   trm1words = fIndex - startTRM;
282   //space for 2st TRM header
283   
284   WriteTRMDataHeader(itrm, trm1words , positionOfTRMHeader);
285   
286   //DRM trailer
287   WriteTrailer(1,0,fEventNumber,5);
288     
289     WriteDRMDataHeader();
290     
291 }
292
293 //_____________________________________________________________________________
294
295 void  AliT0RawData::WriteDRMDataHeader()
296 {
297 //Write a (dummy or real) DDL DRM  data header, 
298 //set the compression bit if compressed
299 //  UInt_t drmheader[4];  
300   UInt_t word;
301   UInt_t baseWord=0;
302   //fill DRM headers
303   //DRM Global Header
304   word = 1;
305   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
306   word = fIndex ;
307
308   AliBitPacking::PackWord(word,baseWord,4, 20); // event words 
309   word=124;
310   AliBitPacking::PackWord(word,baseWord, 21, 27); // event words 
311   word=4;
312   AliBitPacking::PackWord(word,baseWord,28, 31);// 0100 marks header
313   fBuffer[0]=  baseWord;
314
315
316   //DRM status header 1
317   word = 1;
318   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
319   word = 1;
320   AliBitPacking::PackWord(word,baseWord,4, 14); // slotID now 0000000001
321   word = 1;
322   AliBitPacking::PackWord(word,baseWord,15, 15); //if 1  LHC clock is coorectly recieved from CPDM 
323   word=0;
324   AliBitPacking::PackWord(word,baseWord,16,27); // reserve for future use
325   word=4;
326   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
327   fBuffer[1] = baseWord;
328   
329    word=0;
330    baseWord=0;
331    
332    //DRM status header 2
333    word = 1;
334    AliBitPacking::PackWord(word,baseWord, 0, 3); // 0001 
335    word = 3;
336    AliBitPacking::PackWord(word,baseWord, 4, 14); //enable slotID now 00000000011
337    word = 0;
338    AliBitPacking::PackWord(word,baseWord, 15, 15); // something
339    word=0;
340    AliBitPacking::PackWord(word,baseWord, 16, 27); // fault ID for simulation 0
341    word=4;
342    AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
343    fBuffer[2]=  baseWord;
344         
345    word=0;
346    baseWord=0;
347    //DRM status header 3
348    word = 1;
349     AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
350     word = 0;
351     AliBitPacking::PackWord(word,baseWord,4, 27); // TTC event counter
352     word=4;
353     AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
354     fBuffer[3]=  baseWord;
355
356     // new DRM format
357     fBuffer[4]=  baseWord;
358     fBuffer[5]=  baseWord;
359    
360     word=0;
361     baseWord=0;
362     
363 }
364   
365 //_____________________________________________________________________________
366
367 void  AliT0RawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
368                                           Int_t  positionOfTRMHeader)
369 {
370 //Write a (dummy or real) DDL TRM  data header, 
371 //set the compression bit if compressed
372 //  UInt_t trmheader;  
373   UInt_t word;
374   UInt_t baseWord=0;
375   //fill TRM headers
376   //TRM Global Header
377   word = slotID;
378   AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
379   word = nWordsInTRM;
380  //+this word - DRM header 
381
382   AliBitPacking::PackWord(word,baseWord,4, 16); // event words 
383   word=0;
384   AliBitPacking::PackWord(word,baseWord,17,18); // ACQ
385   word=0;
386   AliBitPacking::PackWord(word,baseWord,19,19); //  L SEY inside LUT
387   word=0;
388   AliBitPacking::PackWord(word,baseWord,20,27); //  MBZ
389   word=4;
390   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
391   fBuffer[positionOfTRMHeader] =  baseWord;
392
393   word=0; 
394   baseWord=0;
395      
396 }
397
398 //_____________________________________________________________________________
399
400 void  AliT0RawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
401 {
402 //Write a (dummy or real) DDL Chain  data header, 
403 //set the compression bit if compressed
404 //  chainNumber 00 or 10
405   UInt_t word;
406   UInt_t baseWord=0;
407   //fill TRM headers
408   //TRM Global Header
409   word = slotID; // ask Tatiana 7 or 9 
410   AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
411   word = 0;
412   AliBitPacking::PackWord(word,baseWord,4, 15); // bunchID
413   word=0;
414   AliBitPacking::PackWord(word,baseWord,16,23); // PB24 temperature
415   word=0;
416   AliBitPacking::PackWord(word,baseWord,24,26); //  PB24 ID
417   word=0;
418   AliBitPacking::PackWord(word,baseWord,27,27); //  TS
419   word=chainNumber;
420   AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
421   fBuffer[fIndex] =  baseWord;
422   //cout<<" chain header "<<baseWord<<" number "<<chainNumber<<endl;
423   word=0;
424   baseWord=0;     
425   
426 }
427 //_____________________________________________________________________________
428
429 void  AliT0RawData::WriteChainDataTrailer(UInt_t chainNumber )
430 {
431 //Write a (dummy or real) DDL Chain  data trailer 
432 //set the compression bit if compressed
433 //  chainNumber 00 or 10
434   UInt_t word;
435   UInt_t baseWord=0;
436   word = 0; // ask Tatiana 7 or 9 
437   AliBitPacking::PackWord(word,baseWord,0, 3); // status
438   word = 0;
439   AliBitPacking::PackWord(word,baseWord,4, 15); // MBZ
440   word=fEventNumber;
441   AliBitPacking::PackWord(word,baseWord,16,27); // event counter
442   word=chainNumber;
443   AliBitPacking::PackWord(word,baseWord,28,31); // chain number
444   fIndex++;
445   fBuffer[fIndex] =  baseWord;
446
447   word=0;
448   baseWord=0;     
449   
450 }
451 //_____________________________________________________________________________
452
453 void  AliT0RawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
454 {
455 //Write a (dummy or real) DDL data header, 
456 //set the compression bit if compressed
457
458   AliRawDataHeaderSim header;
459   
460   if (dummy) {
461     //if size=0 it means that this data header is a dummy data header
462     fDataHeaderPos = fFile->Tellp();
463     fFile->WriteBuffer((char*)(&header), sizeof(header));
464   } else {
465     UInt_t currentFilePos = fFile->Tellp();
466     fFile->Seekp(fDataHeaderPos);
467     header.fSize = currentFilePos-fDataHeaderPos;
468     header.SetAttribute(0);  // valid data
469     if (compressed) header.SetAttribute(1); 
470     fFile->WriteBuffer((char*)(&header), sizeof(header));
471     fFile->Seekp(currentFilePos);
472   }
473   
474 }
475
476 //___ __________________________________________________________________________
477
478
479 void  AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
480 {
481 //Write a (dummy or real) DDL Chain  data trailer 
482
483   UInt_t word;
484   UInt_t baseWord=0;
485   word = slot;
486   AliBitPacking::PackWord(word,baseWord,0, 3); // 0001 
487   word=word1;
488   AliBitPacking::PackWord(word,baseWord,4, 15); // CRC ?
489   word = word2;
490   AliBitPacking::PackWord(word,baseWord,16,27); // event counter
491   word=word3;
492   AliBitPacking::PackWord(word,baseWord,28,31); //  marks trailer
493   fIndex++;
494   fBuffer[fIndex] =  baseWord;
495
496   word=0;
497   baseWord=0;
498
499 }
500
501 //---------------------------------------------------------------------------------------
502 //---------------------------------------------------------------------------------------
503 void  AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
504 {
505   //  put all time fields thother in 1 word
506
507   UInt_t word;
508   UInt_t baseWord=0;
509
510   word=time;
511   AliBitPacking::PackWord(word,baseWord, 0, 20); // Time 
512
513   word=ch;
514   AliBitPacking::PackWord(word,baseWord, 21, 23); // number of channel 
515   word=iTDC;
516   AliBitPacking::PackWord(word,baseWord, 24, 27); // TDC ID
517
518   word=0;
519   AliBitPacking::PackWord(word,baseWord, 28, 28); // E = 0 in simulation
520   word=0;
521   AliBitPacking::PackWord(word,baseWord, 29, 30); // PS bit data 00
522   word=1;
523   AliBitPacking::PackWord(word,baseWord, 31, 31); // 1
524   fIndex++;
525   fBuffer[fIndex]=baseWord;
526
527   word=0;
528   baseWord=0;
529 }
530 //---------------------------------------------------------------------------------------
531
532 Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
533   //Int_t AliT0RawData::RawDataT0(*fDigits)
534 {
535    //This method creates the Raw data files for T0 detector
536
537
538   // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector() 
539                           //max number of digits per DDL file times 2
540   //  UInt_t fBuffer[kSize];
541   //  UInt_t baseWord;
542   // UInt_t word;
543
544   fIndex=-1;
545  
546
547    WriteDataHeader(kTRUE, kFALSE);
548   GetDigits(fDigits);
549   //write packing digits
550   
551   
552   fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
553   //write real data header on its place
554    WriteDataHeader(kFALSE, kFALSE);
555   
556   
557   //end for
558   
559   return 0;  
560   
561 }