time delay calibration numbers
[u/mrichter/AliRoot.git] / T0 / AliT0RawData.cxx
CommitLineData
dc7ca31d 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
37ClassImp(AliT0RawData)
38
39//_____________________________________________________________________________
40AliT0RawData::AliT0RawData():TObject()
41{
42 /*
43- 48 channels (2 words each as in TOF DDL) for :
44word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
45and the same but for amplified signal. Now I wrote the same time because
46CDF are not ready and differences didn't measured yet.
47
48- 48 channel for amplitude: very preliminary, QTC features are not
49known now, preliminary i put as T1 time signal for this PMT in first
50channel and T1+A in second, where A=Log(Amplitude);
51and the same for amplified but A=Log(10*Amplitude).
52
53- T0-A and T0-C 2 channels
54- T0A-T0C vertex information
55- Time Meaner where T0C TOF increase to the T0A TOF distance
56- 6 multiplicity signals the same way as amplitude and with the same
57uncertances
58 */
59
60 fIndex=-1;
61 fDigits = NULL;
62
63 fTimeCFD = new TArrayI(24);
64 fADC1 = new TArrayI(24);
65 fTimeLED = new TArrayI(24);
66 fADC0 = new TArrayI(24);
67 fFile = NULL;
68 fDataHeaderPos = 0;
69 fDRMDataHeaderPos = 0;
70 memset(fBuffer,0,512*sizeof(UInt_t));
71
72 //open the output file
73 char fileName[15];
74 sprintf(fileName,"T0_%d.ddl", 0xd00);
75 fFile = new AliFstream(fileName);
76 //get event number
77 AliRunLoader *runloader = AliRunLoader::GetRunLoader();
78 if (runloader) {
79 fEventNumber = runloader->GetEventNumber();
80 }
81}
82
83//_____________________________________________________________________________
84AliT0RawData::AliT0RawData(const AliT0RawData &r):TObject()
85{
86 //
87 // AliT0rawData copy constructor
88 //
89
90 ((AliT0RawData &) r).Copy(*this);
91
92}
93
94//_____________________________________________________________________________
95AliT0RawData::~AliT0RawData()
96{
97 //
98 // Destructor
99 //
100 if (fDigits) {
101 delete fDigits;
102 fDigits = NULL;
103 }
104 delete fTimeCFD;
105 delete fADC1;
106 delete fTimeLED;
107 delete fADC0;
108}
109
110//_____________________________________________________________________________
111AliT0RawData &AliT0RawData::operator=(const AliT0RawData &r)
112{
113 //
114 // Assignment operator
115 //
116
117 if (this != &r) ((AliT0RawData &) r).Copy(*this);
118 return *this;
119
120}
121
122//_____________________________________________________________________________
123void AliT0RawData::GetDigits(AliT0digit *fDigits)
124{
125
126 //This method packs the T0 digits in a proper 32 bits structure
127
128 //read T0 digits and fill TDC and ADC arrays
129
130
131 // Int_t error=0;
132 Int_t time, positionOfTRMHeader;
088c40b6 133
134 // Get the digits array
135
dc7ca31d 136 fDigits->GetTime(*fTimeCFD);
137 fDigits->GetADC(*fADC1);
138 fDigits->GetTimeAmp(*fTimeLED);
139 fDigits->GetADCAmp(*fADC0);
140 Int_t meantime = fDigits->MeanTime();
141 Int_t timediff = fDigits->TimeDiff();
142 Int_t mult0=fDigits->SumMult();
143 Int_t mult1=fDigits->SumMult();
144 Int_t timeA = fDigits->BestTimeLeft();
145 Int_t timeC = fDigits->BestTimeRight();
088c40b6 146
147
148 TArrayI *allData = new TArrayI(110);
dc7ca31d 149 Int_t i=0;
088c40b6 150 allData->AddAt(0,0);
151 for (i=1; i<25; i++) {
152 allData->AddAt(fTimeLED->At(i-1),i);
153 allData->AddAt(fTimeCFD->At(i-1),i+24);
154 allData->AddAt(fADC0->At(i-1),i+54);
155 allData->AddAt(fADC1->At(i-1),i+78);
156 // cout<<i<<" led "<<fTimeLED->At(i-1)<<" cfd "<<fTimeCFD->At(i-1)<<" qt0 "<<fADC0->At(i-1)<<" qt1 "<<fADC1->At(i-1)<<endl;
dc7ca31d 157 }
088c40b6 158 allData->AddAt(meantime,49);
159 allData->AddAt(timediff,50);
160 allData->AddAt(timediff,103); //trigger vertex
161 allData->AddAt(timeA,51);
162 allData->AddAt(timeA,104); //trigger T0A
163 allData->AddAt(timeC,52);
164 allData->AddAt(timeC,105); //trigger T0C
165 allData->AddAt(mult0,53);
166 allData->AddAt(mult1,106); //trigger central
167 allData->AddAt(mult1,54);
168 allData->AddAt(mult1,107); //trigger semi-central
169
170 // cout<<" new Event "<<endl;
171 // for (Int_t ia=0; ia<110; ia++) cout<<ia<<" "<<allData->At(ia)<<endl;
dc7ca31d 172 //space for DRM header
173 fIndex += 4;
174
175 //space for 1st TRM header
176 fIndex ++;
177 positionOfTRMHeader= fIndex;
178
179 //space for chain header
180 fIndex ++;
181
182 // Loop through all PMT
183 Int_t chain=0;
184 Int_t iTDC = 0;
185 Int_t channel=0;
186 Int_t trm1words=0;
187 Int_t fWordsIn1stTRM=0;
188 //LED
088c40b6 189 for (Int_t det = 0; det < 55; det++) {
dc7ca31d 190 time = allData->At(det);
191
192 if (time >0) {
193 FillTime(channel, iTDC, time);
194 trm1words++;
195 }
196 if (channel < 6) channel +=2;
197 else {
198 channel = 0;
199 iTDC++;
200 if (iTDC>15) { chain++; iTDC=0;}
201 }
088c40b6 202 // cout<<det<<" "<<time<<" "<<iTDC<<" "<<channel<<" "<<endl;
dc7ca31d 203 }
204
205 WriteTrailer(0,0,fEventNumber,1); // 1st chain trailer
206 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
207 fWordsIn1stTRM = trm1words + 4;
088c40b6 208 // WriteTRMDataHeader(3, trm1words , positionOfTRMHeader);
209 WriteTRMDataHeader(0, trm1words , positionOfTRMHeader);
dc7ca31d 210
211
212 //space for 2st TRM header
213 fIndex ++;
214 positionOfTRMHeader= fIndex;
215
216 //space for chain header
217 fIndex ++;
218
219
220 chain=0;
221 iTDC = 0;
222 channel=0;
223 Int_t trm2words=0;
088c40b6 224 for (Int_t det = 55; det < 108; det++) {
dc7ca31d 225 time = allData->At(det);
226
227 if (time >0) {
228 FillTime(channel, iTDC, time);
229 trm2words++;}
230 if (channel < 6) channel +=2;
231 else {
232 channel = 0;
233 iTDC++;
234 if (iTDC>15) { chain++; iTDC=0;}
235 }
088c40b6 236 // cout<<det<<" "<<time<<" "<<channel<<" "<<iTDC<<endl;
237 }
dc7ca31d 238
239 WriteTrailer(0,0,fEventNumber,1); // 1st chain trailer
240 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
088c40b6 241 // WriteTRMDataHeader(5,trm2words,positionOfTRMHeader);
242 WriteTRMDataHeader(1,trm2words,positionOfTRMHeader);
dc7ca31d 243
244 WriteTrailer(1,fEventNumber,0,5); //DRM trailer
245 WriteDRMDataHeader();
246
247}
248//------------------------------------------------------------------------------
249void AliT0RawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit)
250{
251
252 // Build mask
253 Int_t len=StopBit-StartBit+1;
254 UInt_t mask=0;
255 for(Int_t jb=0; jb<len; mask|=1<<jb++);
256 // Check consistency
257 if(Word > mask){
258 Error("PackWord", "Word to be filled is not within desired length\n"
259 "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
260 return;
261 }
262 BaseWord=(BaseWord&~(mask<<StartBit))|Word<<StartBit;
263
264}
265
266
267//_____________________________________________________________________________
268
269void AliT0RawData::WriteDRMDataHeader()
270{
271//Write a (dummy or real) DDL DRM data header,
272//set the compression bit if compressed
273// UInt_t drmheader[4];
274 UInt_t word;
275 UInt_t baseWord=0;
276 //fill DRM headers
277 //DRM Global Header
278 word = 1;
279 PackWord(baseWord,word,0, 3); // 0001
280 word = fIndex ;
281
282 PackWord(baseWord,word,4, 20); // event words
283 word=124;
284 PackWord(baseWord,word,21,27); // DRM ID for T0 - 124
285 word=4;
286 PackWord(baseWord,word,28,31); // 0100 marks header
287 fBuffer[0]= baseWord;
288
289 //DRM status header 1
290 word = 1;
291 PackWord(baseWord,word,0, 3); // 0001
292 word = 1;
293 PackWord(baseWord,word,4, 14); // slotID now 0000000001
294 word = 1;
295 PackWord(baseWord,word,15, 15); //if 1 LHC clock is coorectly recieved from CPDM
296 word=0;
297 PackWord(baseWord,word,16,27); // reserve for future use
298 word=4;
299 PackWord(baseWord,word,28,31); // 0100 marks header
300 fBuffer[1] = baseWord;
301
302 word=0;
303 baseWord=0;
304
305 //DRM status header 2
306 word = 1;
307 PackWord(baseWord,word, 0, 3); // 0001
308 word = 3;
309 PackWord(baseWord,word, 4, 14); //enable slotID now 00000000011
310 word = 0;
311 PackWord(baseWord,word, 15, 15); // something
312 word=0;
313 PackWord(baseWord,word, 16, 27); // fault ID for simulation 0
314 word=4;
315 PackWord(baseWord,word,28,31); // 0100 marks header
316 fBuffer[2]= baseWord;
317
318
319 word=0;
320 baseWord=0;
321 //DRM status header 3
322 word = 1;
323 PackWord(baseWord,word,0, 3); // 0001
324 word = 0;
325 PackWord(baseWord,word,4, 27); // TTC event counter
326 word=4;
327 PackWord(baseWord,word,28,31); // 0100 marks header
328 fBuffer[3]= baseWord;
329
330
331 word=0;
332 baseWord=0;
333
334}
335
336//_____________________________________________________________________________
337
338void AliT0RawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
339 Int_t positionOfTRMHeader)
340{
341//Write a (dummy or real) DDL TRM data header,
342//set the compression bit if compressed
343// UInt_t trmheader;
344 UInt_t word;
345 UInt_t baseWord=0;
346 //fill TRM headers
347 //TRM Global Header
348 word = slotID;
349 PackWord(baseWord,word,0, 3); // slotID
350 word = nWordsInTRM;
351 //+this word - DRM header
352
353 PackWord(baseWord,word,4, 16); // event words
354 word=0;
355 PackWord(baseWord,word,17,18); // ACQ
356 word=0;
357 PackWord(baseWord,word,19,19); // L SEY inside LUT
358 word=0;
359 PackWord(baseWord,word,20,27); // MBZ
360 word=4;
361 PackWord(baseWord,word,28,31); // 0100 marks header
362 fBuffer[positionOfTRMHeader] = baseWord;
088c40b6 363 cout<<" TRM header "<<baseWord<<endl;
dc7ca31d 364 word=0;
365 baseWord=0;
366
367}
368
369//_____________________________________________________________________________
370
371void AliT0RawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
372{
373//Write a (dummy or real) DDL Chain data header,
374//set the compression bit if compressed
375// chainNumber 00 or 10
376 UInt_t word;
377 UInt_t baseWord=0;
378 //fill TRM headers
379 //TRM Global Header
380 word = slotID; // ask Tatiana 7 or 9
381 PackWord(baseWord,word,0, 3); // slotID
382 word = 0;
383 PackWord(baseWord,word,4, 15); // bunchID
384 word=0;
385 PackWord(baseWord,word,16,23); // PB24 temperature
386 word=0;
387 PackWord(baseWord,word,24,26); // PB24 ID
388 word=0;
389 PackWord(baseWord,word,27,27); // TS
390 word=chainNumber;
391 PackWord(baseWord,word,28,31); // 0100 marks header
392 fBuffer[4] = baseWord;
393
394 word=0;
395 baseWord=0;
396
397}
398//_____________________________________________________________________________
399
400void AliT0RawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
401{
402//Write a (dummy or real) DDL data header,
403//set the compression bit if compressed
404
405 AliRawDataHeaderSim header;
406
407 if (dummy) {
408 //if size=0 it means that this data header is a dummy data header
409 fDataHeaderPos = fFile->Tellp();
410 fFile->WriteBuffer((char*)(&header), sizeof(header));
411 } else {
412 UInt_t currentFilePos = fFile->Tellp();
413 fFile->Seekp(fDataHeaderPos);
414 header.fSize = currentFilePos-fDataHeaderPos;
415 header.SetAttribute(0); // valid data
416 if (compressed) header.SetAttribute(1);
417 fFile->WriteBuffer((char*)(&header), sizeof(header));
418 fFile->Seekp(currentFilePos);
419 }
420
421}
422
423//___ __________________________________________________________________________
424
425
426void AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
427{
428
429 UInt_t word;
430 UInt_t baseWord=0;
431 word = slot;
432 PackWord(baseWord,word,0, 3); // 0001
433 word=word1;
434 PackWord(baseWord,word,4, 15); // CRC ?
435 word = word2;
436 PackWord(baseWord,word,16,27); // event counter
437 word=word3;
438 PackWord(baseWord,word,28,31); // marks trailer
439 fIndex++;
440 fBuffer[fIndex] = baseWord;
441
442 word=0;
443 baseWord=0;
444
445}
446
447//---------------------------------------------------------------------------------------
448//---------------------------------------------------------------------------------------
449void AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
450{
451 UInt_t word;
452 UInt_t baseWord=0;
453
454 word=time;
455 PackWord(baseWord,word, 0, 20); // Time
456
457 word=ch;
458 PackWord(baseWord,word, 21, 23); // number of channel
459 word=iTDC;
460 PackWord(baseWord,word, 24, 27); // TDC ID
461
462 word=0;
463 PackWord(baseWord,word, 28, 28); // E = 0 in simulation
464 word=0;
465 PackWord(baseWord,word, 29, 30); // PS bit data 00
466 word=1;
467 PackWord(baseWord,word, 31, 31); // 1
468 fIndex++;
469 fBuffer[fIndex]=baseWord;
470 word=0;
471 baseWord=0;
472
473}
474//---------------------------------------------------------------------------------------
475
476Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
477{
478 //This method creates the Raw data files for T0 detector
479
480
481 // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector()
482 //max number of digits per DDL file times 2
483 // UInt_t fBuffer[kSize];
484 // UInt_t baseWord;
485 // UInt_t word;
486
487 fIndex=-1;
488
489 AliRawDataHeaderSim header;
490 //loop over TOF DDL files
491 //write Dummy DATA HEADER
492 WriteDataHeader(kTRUE, kFALSE);
493 GetDigits(fDigits);
494 //write packing digits
495 fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
496 //write real data header on its place
497 WriteDataHeader(kFALSE, kFALSE);
498
499
500 //end for
501
502 return 0;
503
504}