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