]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0RawData.cxx
Take the absolute time of the hits into account in the digitization. Needed for pileu...
[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
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
38 ClassImp(AliT0RawData)
39
40 //_____________________________________________________________________________
41   AliT0RawData::AliT0RawData():TObject(),
42  fVerbose(0),      
43  fIndex(-1) ,     
44  fEventNumber(0), 
45  fTimeCFD(new TArrayI(24)),    
46  fADC1( new TArrayI(24)),     
47  fTimeLED( new TArrayI(24)), 
48  fADC0( new TArrayI(24)),     
49  fFile(0x0),   
50  fDataHeaderPos(0),
51  fDRMDataHeaderPos(0),
52  fTRMDataHeaderPos(0),
53  fDigits(0)  
54
55 {
56   /*
57 -  48 channels (2 words each as in TOF DDL) for :
58 word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
59 and the same but for amplified signal. Now I wrote the same time because
60 CDF are not ready and differences didn't measured yet.
61
62 -  48 channel for amplitude: very preliminary, QTC features are not
63 known now, preliminary i put as T1 time signal for this PMT in first
64 channel and T1+A in second, where A=Log(Amplitude);
65 and the same for amplified but A=Log(10*Amplitude).
66
67 - T0-A and T0-C 2 channels
68 - T0A-T0C vertex information
69 - Time Meaner where T0C TOF increase to the T0A TOF distance
70 - 6 multiplicity signals the same way as amplitude and with the same
71 uncertances
72   */
73
74   //open the output file
75   char fileName[15];
76   strcpy(fileName,AliDAQ::DdlFileName("T0",0)); //The name of the output file
77   fFile = new AliFstream(fileName);
78   memset(fBuffer,0,512*sizeof(UInt_t));
79
80   //get event number 
81   AliRunLoader *runloader = AliRunLoader::GetRunLoader();
82   if (runloader) {
83     fEventNumber = runloader->GetEventNumber();
84   }
85 }
86
87 //_____________________________________________________________________________
88
89 AliT0RawData::AliT0RawData(const AliT0RawData &r):TObject(),
90  fVerbose(0),      
91  fIndex(-1) ,     
92  fEventNumber(0), 
93  fTimeCFD(new TArrayI(24)),    
94  fADC1( new TArrayI(24)),     
95  fTimeLED( new TArrayI(24)), 
96  fADC0( new TArrayI(24)),     
97  fFile(0x0),   
98  fDataHeaderPos(0),
99  fDRMDataHeaderPos(0),
100  fTRMDataHeaderPos(0),
101  fDigits(0)  
102
103 {
104   //
105   // AliT0rawData copy constructor
106   //
107
108   ((AliT0RawData &) r).Copy(*this);
109
110 }
111
112 //_____________________________________________________________________________
113 AliT0RawData::~AliT0RawData()
114 {
115   //
116   // Destructor
117   //
118   if (fDigits) {
119     delete fDigits;
120     fDigits = NULL;
121   }
122   delete fTimeCFD;
123   delete fADC1;
124   delete fTimeLED;
125   delete fADC0;
126 }
127
128 //_____________________________________________________________________________
129 AliT0RawData &AliT0RawData::operator=(const AliT0RawData &r)
130 {
131   //
132   // Assignment operator
133   //
134
135   if (this != &r) ((AliT0RawData &) r).Copy(*this);
136   return *this;
137
138 }
139
140 //_____________________________________________________________________________
141 void AliT0RawData::GetDigits(AliT0digit *fDigits)
142 {
143  
144   //This method packs the T0 digits in a proper 32 bits structure
145
146   //read T0 digits and fill TDC and ADC arrays
147
148
149   //  Int_t error=0;
150   Int_t time,  positionOfTRMHeader;
151   
152   // Get the digits array
153   
154   fDigits->GetTimeCFD(*fTimeCFD);
155   fDigits->GetQT0(*fADC1);
156   fDigits->GetTimeLED(*fTimeLED);
157   fDigits->GetQT1(*fADC0);
158   Int_t meantime = fDigits->MeanTime(); 
159   Int_t timediff = fDigits->TimeDiff(); 
160   Int_t mult0=fDigits->SumMult();
161   Int_t mult1=fDigits->SumMult();
162   Int_t timeA = fDigits->BestTimeC();
163   Int_t timeC = fDigits->BestTimeA();
164   
165   
166   TArrayI  *allData = new TArrayI(110);
167   Int_t i=0;
168   allData->AddAt(0,0);
169   for (i=1; i<25; i++) {
170     allData->AddAt(fTimeLED->At(i-1),i);
171     allData->AddAt(fTimeCFD->At(i-1),i+24);
172     allData->AddAt(fADC0->At(i-1),i+54);
173     allData->AddAt(fADC1->At(i-1),i+78);
174
175   }
176   allData->AddAt(meantime,49);
177   allData->AddAt(timediff,50);
178   allData->AddAt(timediff,103); //trigger vertex
179   allData->AddAt(timeA,51);
180   allData->AddAt(timeA,104); //trigger T0A
181   allData->AddAt(timeC,52);
182   allData->AddAt(timeC,105); //trigger T0C
183   allData->AddAt(mult0,53);
184   allData->AddAt(mult1,106); //trigger central
185   allData->AddAt(mult1,54);
186   allData->AddAt(mult1,107); //trigger semi-central
187
188   // cout.setf( ios_base::hex, ios_base::basefield );
189   
190   //space for DRM header
191   fIndex += 6;
192
193   Int_t startTRM=fIndex;
194   //space for 1st TRM header
195   fIndex ++;
196   positionOfTRMHeader= fIndex;
197
198   //space for chain  header
199   fIndex ++;
200   WriteChainDataHeader(0, 0); // 
201   // Loop through all PMT
202   Int_t chain=0; 
203   Int_t iTDC = 0;
204   Int_t channel=0;
205   Int_t trm1words=0;
206
207   //LED
208   for (Int_t det = 0; det < 55; det++) {
209     time = allData->At(det);
210     if (time >0)  
211       FillTime(channel,  iTDC,  time); 
212     if (channel < 6) channel +=2;
213     else {
214       channel = 0; 
215       iTDC++;
216       if (iTDC>15) { chain++; iTDC=0;}
217     }
218   }
219   
220
221   WriteChainDataTrailer(1); // 1st chain trailer
222   fIndex++;
223   WriteChainDataHeader(2, 1); // 
224   WriteChainDataTrailer(3); // 2st chain trailer
225   WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
226   trm1words = fIndex - startTRM;
227   WriteTRMDataHeader(0, trm1words , positionOfTRMHeader);
228
229
230   //space for 2st TRM header
231   startTRM=fIndex;
232   fIndex ++;
233   positionOfTRMHeader= fIndex;
234  
235   // chain  header
236   fIndex ++;
237   WriteChainDataHeader(0, 0); // 
238   chain=0; 
239   iTDC = 0;
240   channel=0;
241
242   for (Int_t det = 55; det < 108; det++) {
243     time = allData->At(det);
244     
245     if (time >0)  
246       FillTime(channel,  iTDC,  time); 
247      
248     if (channel < 6) channel +=2;
249     else {
250       channel = 0; 
251       iTDC++;
252       if (iTDC>15) { chain++; iTDC=0;}
253     }
254     
255     
256   }
257     WriteChainDataTrailer(1); // 1st chain trailer
258     fIndex++;
259     WriteChainDataHeader(2, 1); // 
260     WriteChainDataTrailer(3); // 2st chain trailer
261     WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
262     trm1words = fIndex - startTRM;
263     WriteTRMDataHeader(1, trm1words , positionOfTRMHeader);
264     //DRM trailer
265     WriteTrailer(1,0,fEventNumber,5); // 1st TRM trailer
266     
267     WriteDRMDataHeader();
268
269     delete allData;
270
271 }
272 //------------------------------------------------------------------------------
273 void AliT0RawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit)
274 {
275   
276   // Build mask
277   Int_t len=StopBit-StartBit+1;
278   UInt_t mask=0;
279   for(Int_t jb=0; jb<len; mask|=1<<jb++);
280   // Check consistency
281   if(Word > mask){
282     Error("PackWord", "Word to be filled is not within desired length\n"
283           "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
284     return;
285   }
286   BaseWord=(BaseWord&~(mask<<StartBit))|Word<<StartBit;
287
288 }
289
290
291 //_____________________________________________________________________________
292
293 void  AliT0RawData::WriteDRMDataHeader()
294 {
295 //Write a (dummy or real) DDL DRM  data header, 
296 //set the compression bit if compressed
297 //  UInt_t drmheader[4];  
298   UInt_t word;
299   UInt_t baseWord=0;
300   //fill DRM headers
301   //DRM Global Header
302   word = 1;
303   PackWord(baseWord,word,0, 3); // 0001 
304   word = fIndex ;
305
306   PackWord(baseWord,word,4, 20); // event words 
307   word=124;
308   PackWord(baseWord,word,21,27); // DRM ID for T0 - 124
309   word=4;
310   PackWord(baseWord,word,28,31); // 0100 marks header
311   fBuffer[0]=  baseWord;
312
313
314   //DRM status header 1
315   word = 1;
316   PackWord(baseWord,word,0, 3); // 0001 
317   word = 1;
318   PackWord(baseWord,word,4, 14); // slotID now 0000000001
319   word = 1;
320   PackWord(baseWord,word,15, 15); //if 1  LHC clock is coorectly recieved from CPDM 
321   word=0;
322   PackWord(baseWord,word,16,27); // reserve for future use
323   word=4;
324   PackWord(baseWord,word,28,31); // 0100 marks header
325    fBuffer[1] = baseWord;
326
327
328    word=0;
329    baseWord=0;
330
331     //DRM status header 2
332     word = 1;
333     PackWord(baseWord,word, 0, 3); // 0001 
334     word = 3;
335     PackWord(baseWord,word, 4, 14); //enable slotID now 00000000011
336     word = 0;
337     PackWord(baseWord,word, 15, 15); // something
338     word=0;
339     PackWord(baseWord,word, 16, 27); // fault ID for simulation 0
340     word=4;
341     PackWord(baseWord,word,28,31); // 0100 marks header
342     fBuffer[2]=  baseWord;
343  
344
345     
346     word=0;
347     baseWord=0;
348     //DRM status header 3
349     word = 1;
350     PackWord(baseWord,word,0, 3); // 0001 
351     word = 0;
352     PackWord(baseWord,word,4, 27); // TTC event counter
353     word=4;
354     PackWord(baseWord,word,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   PackWord(baseWord,word,0, 3); // slotID
380   word = nWordsInTRM;
381  //+this word - DRM header 
382
383   PackWord(baseWord,word,4, 16); // event words 
384   word=0;
385   PackWord(baseWord,word,17,18); // ACQ
386   word=0;
387   PackWord(baseWord,word,19,19); //  L SEY inside LUT
388   word=0;
389   PackWord(baseWord,word,20,27); //  MBZ
390   word=4;
391   PackWord(baseWord,word,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   PackWord(baseWord,word,0, 3); // slotID
412   word = 0;
413   PackWord(baseWord,word,4, 15); // bunchID
414   word=0;
415   PackWord(baseWord,word,16,23); // PB24 temperature
416   word=0;
417   PackWord(baseWord,word,24,26); //  PB24 ID
418   word=0;
419   PackWord(baseWord,word,27,27); //  TS
420   word=chainNumber;
421   PackWord(baseWord,word,28,31); // 0100 marks header
422   fBuffer[fIndex] =  baseWord;
423
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   PackWord(baseWord,word,0, 3); // status
439   word = 0;
440   PackWord(baseWord,word,4, 15); // MBZ
441   word=fEventNumber;
442   PackWord(baseWord,word,16,27); // event counter
443   word=chainNumber;
444   PackWord(baseWord,word,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
483   UInt_t word;
484   UInt_t baseWord=0;
485   word = slot;
486   PackWord(baseWord,word,0, 3); // 0001 
487   word=word1;
488   PackWord(baseWord,word,4, 15); // CRC ?
489   word = word2;
490   PackWord(baseWord,word,16,27); // event counter
491   word=word3;
492   PackWord(baseWord,word,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   UInt_t word;
506   UInt_t baseWord=0;
507
508   word=time;
509   PackWord(baseWord,word, 0, 20); // Time 
510
511   word=ch;
512   PackWord(baseWord,word, 21, 23); // number of channel 
513   word=iTDC;
514   PackWord(baseWord,word, 24, 27); // TDC ID
515
516   word=0;
517   PackWord(baseWord,word, 28, 28); // E = 0 in simulation
518   word=0;
519   PackWord(baseWord,word, 29, 30); // PS bit data 00
520   word=1;
521   PackWord(baseWord,word, 31, 31); // 1
522   fIndex++;
523   fBuffer[fIndex]=baseWord;
524   word=0;
525   baseWord=0;
526         
527 }
528 //---------------------------------------------------------------------------------------
529
530 Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
531 {
532    //This method creates the Raw data files for T0 detector
533
534
535   // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector() 
536                           //max number of digits per DDL file times 2
537   //  UInt_t fBuffer[kSize];
538   //  UInt_t baseWord;
539   // UInt_t word;
540
541   fIndex=-1;
542  
543   AliRawDataHeaderSim header;
544    WriteDataHeader(kTRUE, kFALSE);
545   GetDigits(fDigits);
546   //write packing digits
547   
548   
549   fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
550   //write real data header on its place
551    WriteDataHeader(kFALSE, kFALSE);
552   
553   
554   //end for
555   
556   return 0;  
557   
558 }