macro to convert Raw to Digits
[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"
c41ceaac 36#include "AliDAQ.h"
dc7ca31d 37
38ClassImp(AliT0RawData)
39
40//_____________________________________________________________________________
c41ceaac 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
dc7ca31d 55{
56 /*
57- 48 channels (2 words each as in TOF DDL) for :
58word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
59and the same but for amplified signal. Now I wrote the same time because
60CDF are not ready and differences didn't measured yet.
61
62- 48 channel for amplitude: very preliminary, QTC features are not
63known now, preliminary i put as T1 time signal for this PMT in first
64channel and T1+A in second, where A=Log(Amplitude);
65and 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
71uncertances
72 */
73
dc7ca31d 74 //open the output file
75 char fileName[15];
c41ceaac 76 strcpy(fileName,AliDAQ::DdlFileName("T0",0)); //The name of the output file
dc7ca31d 77 fFile = new AliFstream(fileName);
c41ceaac 78 memset(fBuffer,0,512*sizeof(UInt_t));
79
dc7ca31d 80 //get event number
81 AliRunLoader *runloader = AliRunLoader::GetRunLoader();
82 if (runloader) {
83 fEventNumber = runloader->GetEventNumber();
84 }
85}
86
87//_____________________________________________________________________________
c41ceaac 88
89AliT0RawData::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
dc7ca31d 103{
104 //
105 // AliT0rawData copy constructor
106 //
107
108 ((AliT0RawData &) r).Copy(*this);
109
110}
111
112//_____________________________________________________________________________
113AliT0RawData::~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//_____________________________________________________________________________
129AliT0RawData &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//_____________________________________________________________________________
141void 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;
088c40b6 151
152 // Get the digits array
153
c41ceaac 154 fDigits->GetTimeCFD(*fTimeCFD);
155 fDigits->GetQT0(*fADC1);
156 fDigits->GetTimeLED(*fTimeLED);
157 fDigits->GetQT1(*fADC0);
dc7ca31d 158 Int_t meantime = fDigits->MeanTime();
159 Int_t timediff = fDigits->TimeDiff();
160 Int_t mult0=fDigits->SumMult();
161 Int_t mult1=fDigits->SumMult();
c41ceaac 162 Int_t timeA = fDigits->BestTimeC();
163 Int_t timeC = fDigits->BestTimeA();
088c40b6 164
165
166 TArrayI *allData = new TArrayI(110);
dc7ca31d 167 Int_t i=0;
088c40b6 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 // cout<<i<<" led "<<fTimeLED->At(i-1)<<" cfd "<<fTimeCFD->At(i-1)<<" qt0 "<<fADC0->At(i-1)<<" qt1 "<<fADC1->At(i-1)<<endl;
dc7ca31d 175 }
088c40b6 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<<" new Event "<<endl;
189 // for (Int_t ia=0; ia<110; ia++) cout<<ia<<" "<<allData->At(ia)<<endl;
dc7ca31d 190 //space for DRM header
191 fIndex += 4;
192
193 //space for 1st TRM header
194 fIndex ++;
195 positionOfTRMHeader= fIndex;
196
197 //space for chain header
198 fIndex ++;
199
200 // Loop through all PMT
201 Int_t chain=0;
202 Int_t iTDC = 0;
203 Int_t channel=0;
204 Int_t trm1words=0;
205 Int_t fWordsIn1stTRM=0;
206 //LED
088c40b6 207 for (Int_t det = 0; det < 55; det++) {
dc7ca31d 208 time = allData->At(det);
209
210 if (time >0) {
211 FillTime(channel, iTDC, time);
212 trm1words++;
213 }
214 if (channel < 6) channel +=2;
215 else {
216 channel = 0;
217 iTDC++;
218 if (iTDC>15) { chain++; iTDC=0;}
219 }
088c40b6 220 // cout<<det<<" "<<time<<" "<<iTDC<<" "<<channel<<" "<<endl;
dc7ca31d 221 }
222
223 WriteTrailer(0,0,fEventNumber,1); // 1st chain trailer
224 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
225 fWordsIn1stTRM = trm1words + 4;
088c40b6 226 // WriteTRMDataHeader(3, trm1words , positionOfTRMHeader);
227 WriteTRMDataHeader(0, trm1words , positionOfTRMHeader);
dc7ca31d 228
229
230 //space for 2st TRM header
231 fIndex ++;
232 positionOfTRMHeader= fIndex;
233
234 //space for chain header
235 fIndex ++;
236
237
238 chain=0;
239 iTDC = 0;
240 channel=0;
241 Int_t trm2words=0;
088c40b6 242 for (Int_t det = 55; det < 108; det++) {
dc7ca31d 243 time = allData->At(det);
244
245 if (time >0) {
246 FillTime(channel, iTDC, time);
247 trm2words++;}
248 if (channel < 6) channel +=2;
249 else {
250 channel = 0;
251 iTDC++;
252 if (iTDC>15) { chain++; iTDC=0;}
253 }
088c40b6 254 // cout<<det<<" "<<time<<" "<<channel<<" "<<iTDC<<endl;
255 }
dc7ca31d 256
257 WriteTrailer(0,0,fEventNumber,1); // 1st chain trailer
258 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
088c40b6 259 // WriteTRMDataHeader(5,trm2words,positionOfTRMHeader);
260 WriteTRMDataHeader(1,trm2words,positionOfTRMHeader);
dc7ca31d 261
262 WriteTrailer(1,fEventNumber,0,5); //DRM trailer
263 WriteDRMDataHeader();
264
265}
266//------------------------------------------------------------------------------
267void AliT0RawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit)
268{
269
270 // Build mask
271 Int_t len=StopBit-StartBit+1;
272 UInt_t mask=0;
273 for(Int_t jb=0; jb<len; mask|=1<<jb++);
274 // Check consistency
275 if(Word > mask){
276 Error("PackWord", "Word to be filled is not within desired length\n"
277 "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
278 return;
279 }
280 BaseWord=(BaseWord&~(mask<<StartBit))|Word<<StartBit;
281
282}
283
284
285//_____________________________________________________________________________
286
287void AliT0RawData::WriteDRMDataHeader()
288{
289//Write a (dummy or real) DDL DRM data header,
290//set the compression bit if compressed
291// UInt_t drmheader[4];
292 UInt_t word;
293 UInt_t baseWord=0;
294 //fill DRM headers
295 //DRM Global Header
296 word = 1;
297 PackWord(baseWord,word,0, 3); // 0001
298 word = fIndex ;
299
300 PackWord(baseWord,word,4, 20); // event words
301 word=124;
302 PackWord(baseWord,word,21,27); // DRM ID for T0 - 124
303 word=4;
304 PackWord(baseWord,word,28,31); // 0100 marks header
305 fBuffer[0]= baseWord;
306
307 //DRM status header 1
308 word = 1;
309 PackWord(baseWord,word,0, 3); // 0001
310 word = 1;
311 PackWord(baseWord,word,4, 14); // slotID now 0000000001
312 word = 1;
313 PackWord(baseWord,word,15, 15); //if 1 LHC clock is coorectly recieved from CPDM
314 word=0;
315 PackWord(baseWord,word,16,27); // reserve for future use
316 word=4;
317 PackWord(baseWord,word,28,31); // 0100 marks header
318 fBuffer[1] = baseWord;
319
320 word=0;
321 baseWord=0;
322
323 //DRM status header 2
324 word = 1;
325 PackWord(baseWord,word, 0, 3); // 0001
326 word = 3;
327 PackWord(baseWord,word, 4, 14); //enable slotID now 00000000011
328 word = 0;
329 PackWord(baseWord,word, 15, 15); // something
330 word=0;
331 PackWord(baseWord,word, 16, 27); // fault ID for simulation 0
332 word=4;
333 PackWord(baseWord,word,28,31); // 0100 marks header
334 fBuffer[2]= baseWord;
335
336
337 word=0;
338 baseWord=0;
339 //DRM status header 3
340 word = 1;
341 PackWord(baseWord,word,0, 3); // 0001
342 word = 0;
343 PackWord(baseWord,word,4, 27); // TTC event counter
344 word=4;
345 PackWord(baseWord,word,28,31); // 0100 marks header
346 fBuffer[3]= baseWord;
347
348
349 word=0;
350 baseWord=0;
351
352}
353
354//_____________________________________________________________________________
355
356void AliT0RawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
357 Int_t positionOfTRMHeader)
358{
359//Write a (dummy or real) DDL TRM data header,
360//set the compression bit if compressed
361// UInt_t trmheader;
362 UInt_t word;
363 UInt_t baseWord=0;
364 //fill TRM headers
365 //TRM Global Header
366 word = slotID;
367 PackWord(baseWord,word,0, 3); // slotID
368 word = nWordsInTRM;
369 //+this word - DRM header
370
371 PackWord(baseWord,word,4, 16); // event words
372 word=0;
373 PackWord(baseWord,word,17,18); // ACQ
374 word=0;
375 PackWord(baseWord,word,19,19); // L SEY inside LUT
376 word=0;
377 PackWord(baseWord,word,20,27); // MBZ
378 word=4;
379 PackWord(baseWord,word,28,31); // 0100 marks header
380 fBuffer[positionOfTRMHeader] = baseWord;
088c40b6 381 cout<<" TRM header "<<baseWord<<endl;
dc7ca31d 382 word=0;
383 baseWord=0;
384
385}
386
387//_____________________________________________________________________________
388
389void AliT0RawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
390{
391//Write a (dummy or real) DDL Chain data header,
392//set the compression bit if compressed
393// chainNumber 00 or 10
394 UInt_t word;
395 UInt_t baseWord=0;
396 //fill TRM headers
397 //TRM Global Header
398 word = slotID; // ask Tatiana 7 or 9
399 PackWord(baseWord,word,0, 3); // slotID
400 word = 0;
401 PackWord(baseWord,word,4, 15); // bunchID
402 word=0;
403 PackWord(baseWord,word,16,23); // PB24 temperature
404 word=0;
405 PackWord(baseWord,word,24,26); // PB24 ID
406 word=0;
407 PackWord(baseWord,word,27,27); // TS
408 word=chainNumber;
409 PackWord(baseWord,word,28,31); // 0100 marks header
410 fBuffer[4] = baseWord;
411
412 word=0;
413 baseWord=0;
414
415}
416//_____________________________________________________________________________
417
418void AliT0RawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
419{
420//Write a (dummy or real) DDL data header,
421//set the compression bit if compressed
422
423 AliRawDataHeaderSim header;
424
425 if (dummy) {
426 //if size=0 it means that this data header is a dummy data header
427 fDataHeaderPos = fFile->Tellp();
428 fFile->WriteBuffer((char*)(&header), sizeof(header));
429 } else {
430 UInt_t currentFilePos = fFile->Tellp();
431 fFile->Seekp(fDataHeaderPos);
432 header.fSize = currentFilePos-fDataHeaderPos;
433 header.SetAttribute(0); // valid data
434 if (compressed) header.SetAttribute(1);
435 fFile->WriteBuffer((char*)(&header), sizeof(header));
436 fFile->Seekp(currentFilePos);
437 }
438
439}
440
441//___ __________________________________________________________________________
442
443
444void AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
445{
446
447 UInt_t word;
448 UInt_t baseWord=0;
449 word = slot;
450 PackWord(baseWord,word,0, 3); // 0001
451 word=word1;
452 PackWord(baseWord,word,4, 15); // CRC ?
453 word = word2;
454 PackWord(baseWord,word,16,27); // event counter
455 word=word3;
456 PackWord(baseWord,word,28,31); // marks trailer
457 fIndex++;
458 fBuffer[fIndex] = baseWord;
459
460 word=0;
461 baseWord=0;
462
463}
464
465//---------------------------------------------------------------------------------------
466//---------------------------------------------------------------------------------------
467void AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
468{
469 UInt_t word;
470 UInt_t baseWord=0;
471
472 word=time;
473 PackWord(baseWord,word, 0, 20); // Time
474
475 word=ch;
476 PackWord(baseWord,word, 21, 23); // number of channel
477 word=iTDC;
478 PackWord(baseWord,word, 24, 27); // TDC ID
479
480 word=0;
481 PackWord(baseWord,word, 28, 28); // E = 0 in simulation
482 word=0;
483 PackWord(baseWord,word, 29, 30); // PS bit data 00
484 word=1;
485 PackWord(baseWord,word, 31, 31); // 1
486 fIndex++;
487 fBuffer[fIndex]=baseWord;
488 word=0;
489 baseWord=0;
490
491}
492//---------------------------------------------------------------------------------------
493
494Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
495{
496 //This method creates the Raw data files for T0 detector
497
498
499 // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector()
500 //max number of digits per DDL file times 2
501 // UInt_t fBuffer[kSize];
502 // UInt_t baseWord;
503 // UInt_t word;
504
505 fIndex=-1;
506
507 AliRawDataHeaderSim header;
dc7ca31d 508 WriteDataHeader(kTRUE, kFALSE);
509 GetDigits(fDigits);
510 //write packing digits
c41ceaac 511
512
dc7ca31d 513 fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
514 //write real data header on its place
515 WriteDataHeader(kFALSE, kFALSE);
516
517
518 //end for
519
520 return 0;
521
522}