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