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