few mistyping fixed
[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
94249139 24//#include <Riostream.h>
25//#include <TTree.h>
29d3e0eb 26#include <TMap.h>
dc7ca31d 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"
29d3e0eb 37#include "AliT0LookUpValue.h"
94249139 38#include "AliT0LookUpKey.h"
dc7ca31d 39
40ClassImp(AliT0RawData)
41
42//_____________________________________________________________________________
c41ceaac 43 AliT0RawData::AliT0RawData():TObject(),
29d3e0eb 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),
29d3e0eb 55 fParam(0),
56 fLookUp(0)
57
c41ceaac 58
dc7ca31d 59{
60 /*
61- 48 channels (2 words each as in TOF DDL) for :
62word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
63and the same but for amplified signal. Now I wrote the same time because
64CDF are not ready and differences didn't measured yet.
65
66- 48 channel for amplitude: very preliminary, QTC features are not
67known now, preliminary i put as T1 time signal for this PMT in first
68channel and T1+A in second, where A=Log(Amplitude);
69and 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
75uncertances
76 */
77
dc7ca31d 78 //open the output file
79 char fileName[15];
c41ceaac 80 strcpy(fileName,AliDAQ::DdlFileName("T0",0)); //The name of the output file
dc7ca31d 81 fFile = new AliFstream(fileName);
c41ceaac 82 memset(fBuffer,0,512*sizeof(UInt_t));
83
dc7ca31d 84 //get event number
33c3c91a 85 AliRunLoader *runloader = AliRunLoader::Instance();
dc7ca31d 86 if (runloader) {
87 fEventNumber = runloader->GetEventNumber();
88 }
29d3e0eb 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();
69776907 97 TMapIter iter(lookup);
29d3e0eb 98
99 for( Int_t iline=0; iline<106; iline++)
100 {
69776907 101 lookvalue = ( AliT0LookUpValue*) iter.Next();
256d4943 102 lookkey = (AliT0LookUpKey*) lookup->GetValue(lookvalue);
103 fLookUp.Add(lookkey, lookvalue);
29d3e0eb 104 lookkey= new AliT0LookUpKey();
105 lookvalue= new AliT0LookUpValue();
106 }
256d4943 107
dc7ca31d 108}
109
110//_____________________________________________________________________________
c41ceaac 111
112AliT0RawData::AliT0RawData(const AliT0RawData &r):TObject(),
29d3e0eb 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),
29d3e0eb 124 fParam(0),
125 fLookUp(0)
c41ceaac 126
dc7ca31d 127{
128 //
129 // AliT0rawData copy constructor
130 //
131
132 ((AliT0RawData &) r).Copy(*this);
133
134}
135
136//_____________________________________________________________________________
137AliT0RawData::~AliT0RawData()
138{
139 //
140 // Destructor
141 //
dc7ca31d 142 delete fTimeCFD;
143 delete fADC1;
144 delete fTimeLED;
145 delete fADC0;
8ab584c1 146 delete fFile;
dc7ca31d 147}
148
149//_____________________________________________________________________________
150AliT0RawData &AliT0RawData::operator=(const AliT0RawData &r)
151{
152 //
153 // Assignment operator
154 //
155
156 if (this != &r) ((AliT0RawData &) r).Copy(*this);
157 return *this;
158
159}
160
161//_____________________________________________________________________________
162void AliT0RawData::GetDigits(AliT0digit *fDigits)
5bf55199 163//void AliT0RawData::GetDigits(fDigits)
dc7ca31d 164{
165
166 //This method packs the T0 digits in a proper 32 bits structure
167
168 //read T0 digits and fill TDC and ADC arrays
169
170
171 // Int_t error=0;
172 Int_t time, positionOfTRMHeader;
088c40b6 173
174 // Get the digits array
175
c41ceaac 176 fDigits->GetTimeCFD(*fTimeCFD);
177 fDigits->GetQT0(*fADC1);
178 fDigits->GetTimeLED(*fTimeLED);
179 fDigits->GetQT1(*fADC0);
dc7ca31d 180 Int_t meantime = fDigits->MeanTime();
181 Int_t timediff = fDigits->TimeDiff();
182 Int_t mult0=fDigits->SumMult();
183 Int_t mult1=fDigits->SumMult();
c41ceaac 184 Int_t timeA = fDigits->BestTimeC();
185 Int_t timeC = fDigits->BestTimeA();
088c40b6 186
187
d0bcd1fb 188 // TArrayI *allData = new TArrayI(110);
189 Int_t allData[110][1];
190 for (Int_t i=0; i<110; i++) allData[i][0] = 0;
191
192 allData[0][0]=0;
193 for (Int_t i=1; i<13; i++) {
194 allData[i][0] = fTimeCFD->At(i-1);
195 allData[i+12][0] = fTimeLED->At(i-1);
196 allData[i+56][0] = fTimeCFD->At(i-1+12);
197 allData[i+68][0] = fTimeLED->At(i-1+12);
198 }
199
200 for (Int_t iii=0; iii<12; iii++) {
201 allData[2*iii+25][0] = fADC1->At(iii);
202 allData[2*iii+26][0] = fADC0->At(iii);
dc7ca31d 203 }
d0bcd1fb 204 for (Int_t ii=12; ii<24; ii++) {
205 allData[2*ii+57][0] = fADC1->At(ii);
206 allData[2*ii+58][0] = fADC0->At(ii);
207 }
208
209 allData[49][0] = meantime;
210 allData[50][0] = timediff;
211 allData[51][0] = timeA;
212 allData[52][0] = timeC;
213 allData[53][0] = mult0;
214 allData[54][0] = mult1;
215 allData[55][0] = mult0;
216 allData[56][0] = mult1;
217
218 // cout.setf( ios_base::hex, ios_base::basefield );
dc7ca31d 219 //space for DRM header
5325480c 220 fIndex += 6;
dc7ca31d 221
29d3e0eb 222
5325480c 223 Int_t startTRM=fIndex;
dc7ca31d 224 //space for 1st TRM header
225 fIndex ++;
226 positionOfTRMHeader= fIndex;
dc7ca31d 227 //space for chain header
228 fIndex ++;
d8079a1a 229 WriteChainDataHeader(1, 1); //
230
231 // fIndex++;
dc7ca31d 232 // Loop through all PMT
233 Int_t chain=0;
234 Int_t iTDC = 0;
235 Int_t channel=0;
236 Int_t trm1words=0;
d8079a1a 237 Int_t itrm=7;
238 Int_t inside =0;
53111f83 239 Int_t isData = 0;
256d4943 240 AliT0LookUpKey * lookkey = new AliT0LookUpKey();
241 AliT0LookUpValue * lookvalue ;//= new AliT0LookUpValue(trm,tdc,chain,channel);
29d3e0eb 242 for (Int_t det = 0; det < 105; det++) {
d0bcd1fb 243 time = allData[det][0];
21bbda77 244 if (time >0 && time !=99999) {
29d3e0eb 245 lookkey->SetKey(det);
246 lookvalue = (AliT0LookUpValue*) fLookUp.GetValue((TObject*)lookkey);
247 if (lookvalue )
248 {
53111f83 249 isData++;
29d3e0eb 250 itrm= lookvalue->GetTRM();
d0bcd1fb 251 if (det >56 &&inside == 0) {
29d3e0eb 252 WriteChainDataTrailer(1); // 1st chain trailer
253 fIndex++;
d8079a1a 254 WriteChainDataHeader(2, 1);
255 // fIndex++;
256 inside++;
29d3e0eb 257 }
258 chain = lookvalue->GetChain();
259 iTDC = lookvalue->GetTDC();
260 channel = lookvalue->GetChannel();
29d3e0eb 261 FillTime(channel, iTDC, time);
d0bcd1fb 262 AliDebug(1,Form("look %i itrm %i , chain %i , iTDC %i, channel %i time %i", det,itrm,chain,iTDC,channel, time));
29d3e0eb 263 }
264 else
265 {
266 cout<<" no lookup value for key "<<det<<endl;
267 // break;
268 }
dc7ca31d 269 }
29d3e0eb 270
dc7ca31d 271 }
53111f83 272 if (inside==0) {
273 WriteChainDataTrailer(1); // 1st chain trailer
274 fIndex++;
275 WriteChainDataHeader(2, 1);
276 }
d8079a1a 277 // WriteChainDataHeader(2, 1); //
5325480c 278 WriteChainDataTrailer(3); // 2st chain trailer
dc7ca31d 279 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
29d3e0eb 280
281
256d4943 282 trm1words = fIndex - startTRM;
dc7ca31d 283 //space for 2st TRM header
29d3e0eb 284
d8079a1a 285 WriteTRMDataHeader(itrm, trm1words , positionOfTRMHeader);
29d3e0eb 286
287 //DRM trailer
d8079a1a 288 WriteTrailer(1,0,fEventNumber,5);
5325480c 289
290 WriteDRMDataHeader();
29d3e0eb 291
dc7ca31d 292}
dc7ca31d 293
294//_____________________________________________________________________________
295
296void AliT0RawData::WriteDRMDataHeader()
297{
298//Write a (dummy or real) DDL DRM data header,
299//set the compression bit if compressed
300// UInt_t drmheader[4];
301 UInt_t word;
302 UInt_t baseWord=0;
303 //fill DRM headers
304 //DRM Global Header
305 word = 1;
62b80e9d 306 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
dc7ca31d 307 word = fIndex ;
308
62b80e9d 309 AliBitPacking::PackWord(word,baseWord,4, 20); // event words
dc7ca31d 310 word=124;
62b80e9d 311 AliBitPacking::PackWord(word,baseWord, 21, 27); // event words
dc7ca31d 312 word=4;
62b80e9d 313 AliBitPacking::PackWord(word,baseWord,28, 31);// 0100 marks header
dc7ca31d 314 fBuffer[0]= baseWord;
315
5325480c 316
dc7ca31d 317 //DRM status header 1
318 word = 1;
62b80e9d 319 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
dc7ca31d 320 word = 1;
62b80e9d 321 AliBitPacking::PackWord(word,baseWord,4, 14); // slotID now 0000000001
dc7ca31d 322 word = 1;
62b80e9d 323 AliBitPacking::PackWord(word,baseWord,15, 15); //if 1 LHC clock is coorectly recieved from CPDM
dc7ca31d 324 word=0;
62b80e9d 325 AliBitPacking::PackWord(word,baseWord,16,27); // reserve for future use
dc7ca31d 326 word=4;
62b80e9d 327 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
328 fBuffer[1] = baseWord;
329
5325480c 330 word=0;
331 baseWord=0;
62b80e9d 332
333 //DRM status header 2
334 word = 1;
335 AliBitPacking::PackWord(word,baseWord, 0, 3); // 0001
336 word = 3;
337 AliBitPacking::PackWord(word,baseWord, 4, 14); //enable slotID now 00000000011
338 word = 0;
339 AliBitPacking::PackWord(word,baseWord, 15, 15); // something
340 word=0;
341 AliBitPacking::PackWord(word,baseWord, 16, 27); // fault ID for simulation 0
342 word=4;
343 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
344 fBuffer[2]= baseWord;
345
346 word=0;
347 baseWord=0;
348 //DRM status header 3
349 word = 1;
350 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
dc7ca31d 351 word = 0;
62b80e9d 352 AliBitPacking::PackWord(word,baseWord,4, 27); // TTC event counter
dc7ca31d 353 word=4;
62b80e9d 354 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
dc7ca31d 355 fBuffer[3]= baseWord;
356
5325480c 357 // new DRM format
358 fBuffer[4]= baseWord;
359 fBuffer[5]= baseWord;
62b80e9d 360
dc7ca31d 361 word=0;
362 baseWord=0;
363
364}
365
366//_____________________________________________________________________________
367
368void 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;
62b80e9d 379 AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
dc7ca31d 380 word = nWordsInTRM;
381 //+this word - DRM header
382
62b80e9d 383 AliBitPacking::PackWord(word,baseWord,4, 16); // event words
dc7ca31d 384 word=0;
62b80e9d 385 AliBitPacking::PackWord(word,baseWord,17,18); // ACQ
dc7ca31d 386 word=0;
62b80e9d 387 AliBitPacking::PackWord(word,baseWord,19,19); // L SEY inside LUT
dc7ca31d 388 word=0;
62b80e9d 389 AliBitPacking::PackWord(word,baseWord,20,27); // MBZ
dc7ca31d 390 word=4;
62b80e9d 391 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
dc7ca31d 392 fBuffer[positionOfTRMHeader] = baseWord;
62b80e9d 393
dc7ca31d 394 word=0;
395 baseWord=0;
396
397}
398
399//_____________________________________________________________________________
400
401void 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
62b80e9d 411 AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
dc7ca31d 412 word = 0;
62b80e9d 413 AliBitPacking::PackWord(word,baseWord,4, 15); // bunchID
dc7ca31d 414 word=0;
62b80e9d 415 AliBitPacking::PackWord(word,baseWord,16,23); // PB24 temperature
dc7ca31d 416 word=0;
62b80e9d 417 AliBitPacking::PackWord(word,baseWord,24,26); // PB24 ID
dc7ca31d 418 word=0;
62b80e9d 419 AliBitPacking::PackWord(word,baseWord,27,27); // TS
dc7ca31d 420 word=chainNumber;
62b80e9d 421 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
5325480c 422 fBuffer[fIndex] = baseWord;
d8079a1a 423 //cout<<" chain header "<<baseWord<<" number "<<chainNumber<<endl;
5325480c 424 word=0;
425 baseWord=0;
426
427}
428//_____________________________________________________________________________
429
430void 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
62b80e9d 438 AliBitPacking::PackWord(word,baseWord,0, 3); // status
5325480c 439 word = 0;
62b80e9d 440 AliBitPacking::PackWord(word,baseWord,4, 15); // MBZ
5325480c 441 word=fEventNumber;
62b80e9d 442 AliBitPacking::PackWord(word,baseWord,16,27); // event counter
5325480c 443 word=chainNumber;
62b80e9d 444 AliBitPacking::PackWord(word,baseWord,28,31); // chain number
5325480c 445 fIndex++;
446 fBuffer[fIndex] = baseWord;
62b80e9d 447
dc7ca31d 448 word=0;
449 baseWord=0;
450
451}
452//_____________________________________________________________________________
453
454void 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
480void AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
481{
94249139 482//Write a (dummy or real) DDL Chain data trailer
dc7ca31d 483
484 UInt_t word;
485 UInt_t baseWord=0;
486 word = slot;
62b80e9d 487 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
dc7ca31d 488 word=word1;
62b80e9d 489 AliBitPacking::PackWord(word,baseWord,4, 15); // CRC ?
dc7ca31d 490 word = word2;
62b80e9d 491 AliBitPacking::PackWord(word,baseWord,16,27); // event counter
dc7ca31d 492 word=word3;
62b80e9d 493 AliBitPacking::PackWord(word,baseWord,28,31); // marks trailer
dc7ca31d 494 fIndex++;
495 fBuffer[fIndex] = baseWord;
62b80e9d 496
dc7ca31d 497 word=0;
498 baseWord=0;
499
500}
501
502//---------------------------------------------------------------------------------------
503//---------------------------------------------------------------------------------------
504void AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
505{
94249139 506 // put all time fields thother in 1 word
507
dc7ca31d 508 UInt_t word;
509 UInt_t baseWord=0;
510
511 word=time;
62b80e9d 512 AliBitPacking::PackWord(word,baseWord, 0, 20); // Time
dc7ca31d 513
514 word=ch;
62b80e9d 515 AliBitPacking::PackWord(word,baseWord, 21, 23); // number of channel
dc7ca31d 516 word=iTDC;
62b80e9d 517 AliBitPacking::PackWord(word,baseWord, 24, 27); // TDC ID
dc7ca31d 518
519 word=0;
62b80e9d 520 AliBitPacking::PackWord(word,baseWord, 28, 28); // E = 0 in simulation
dc7ca31d 521 word=0;
62b80e9d 522 AliBitPacking::PackWord(word,baseWord, 29, 30); // PS bit data 00
dc7ca31d 523 word=1;
62b80e9d 524 AliBitPacking::PackWord(word,baseWord, 31, 31); // 1
dc7ca31d 525 fIndex++;
526 fBuffer[fIndex]=baseWord;
62b80e9d 527
528 word=0;
dc7ca31d 529 baseWord=0;
dc7ca31d 530}
531//---------------------------------------------------------------------------------------
532
533Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
5bf55199 534 //Int_t AliT0RawData::RawDataT0(*fDigits)
dc7ca31d 535{
536 //This method creates the Raw data files for T0 detector
537
538
539 // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector()
540 //max number of digits per DDL file times 2
541 // UInt_t fBuffer[kSize];
542 // UInt_t baseWord;
543 // UInt_t word;
544
545 fIndex=-1;
546
94c27e4f 547
dc7ca31d 548 WriteDataHeader(kTRUE, kFALSE);
549 GetDigits(fDigits);
550 //write packing digits
c41ceaac 551
552
dc7ca31d 553 fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
554 //write real data header on its place
555 WriteDataHeader(kFALSE, kFALSE);
556
557
558 //end for
559
560 return 0;
561
562}