AliDCSSensor: Updated Eval method to return first/last value of fit if specified...
[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();
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),
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;
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 {
237 itrm= lookvalue->GetTRM();
238 if (itrm != oldtrm ) {
239 WriteChainDataTrailer(1); // 1st chain trailer
240 fIndex++;
241 WriteChainDataHeader(2, 1); //
242 WriteChainDataTrailer(3); // 2st chain trailer
243 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
244
245
246 trm1words = fIndex - startTRM;
247 WriteTRMDataHeader(oldtrm, trm1words , positionOfTRMHeader);
248 //space for 2st TRM header
249 startTRM=fIndex;
250 fIndex ++;
251 positionOfTRMHeader= fIndex;
252 fIndex ++;
253 oldtrm=itrm;
254 }
255 chain = lookvalue->GetChain();
256 iTDC = lookvalue->GetTDC();
257 channel = lookvalue->GetChannel();
29d3e0eb 258 FillTime(channel, iTDC, time);
259 }
260 else
261 {
262 cout<<" no lookup value for key "<<det<<endl;
263 // break;
264 }
dc7ca31d 265 }
29d3e0eb 266
dc7ca31d 267 }
29d3e0eb 268
5325480c 269 WriteChainDataTrailer(1); // 1st chain trailer
270 fIndex++;
271 WriteChainDataHeader(2, 1); //
272 WriteChainDataTrailer(3); // 2st chain trailer
dc7ca31d 273 WriteTrailer(15,0,fEventNumber,5); // 1st TRM trailer
29d3e0eb 274
275
256d4943 276 trm1words = fIndex - startTRM;
dc7ca31d 277 //space for 2st TRM header
29d3e0eb 278
279 WriteTRMDataHeader(1, trm1words , positionOfTRMHeader);
280
281 //DRM trailer
282 WriteTrailer(1,0,fEventNumber,5); // 1st TRM trailer
5325480c 283
284 WriteDRMDataHeader();
29d3e0eb 285
5325480c 286 delete allData;
29d3e0eb 287
dc7ca31d 288}
289//------------------------------------------------------------------------------
290void AliT0RawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit)
291{
292
293 // Build mask
294 Int_t len=StopBit-StartBit+1;
295 UInt_t mask=0;
296 for(Int_t jb=0; jb<len; mask|=1<<jb++);
297 // Check consistency
298 if(Word > mask){
299 Error("PackWord", "Word to be filled is not within desired length\n"
300 "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
301 return;
302 }
303 BaseWord=(BaseWord&~(mask<<StartBit))|Word<<StartBit;
304
305}
306
307
308//_____________________________________________________________________________
309
310void AliT0RawData::WriteDRMDataHeader()
311{
312//Write a (dummy or real) DDL DRM data header,
313//set the compression bit if compressed
314// UInt_t drmheader[4];
315 UInt_t word;
316 UInt_t baseWord=0;
317 //fill DRM headers
318 //DRM Global Header
319 word = 1;
320 PackWord(baseWord,word,0, 3); // 0001
321 word = fIndex ;
322
323 PackWord(baseWord,word,4, 20); // event words
324 word=124;
325 PackWord(baseWord,word,21,27); // DRM ID for T0 - 124
326 word=4;
327 PackWord(baseWord,word,28,31); // 0100 marks header
328 fBuffer[0]= baseWord;
329
5325480c 330
dc7ca31d 331 //DRM status header 1
332 word = 1;
333 PackWord(baseWord,word,0, 3); // 0001
334 word = 1;
335 PackWord(baseWord,word,4, 14); // slotID now 0000000001
336 word = 1;
337 PackWord(baseWord,word,15, 15); //if 1 LHC clock is coorectly recieved from CPDM
338 word=0;
339 PackWord(baseWord,word,16,27); // reserve for future use
340 word=4;
341 PackWord(baseWord,word,28,31); // 0100 marks header
342 fBuffer[1] = baseWord;
343
5325480c 344
345 word=0;
346 baseWord=0;
dc7ca31d 347
348 //DRM status header 2
349 word = 1;
350 PackWord(baseWord,word, 0, 3); // 0001
351 word = 3;
352 PackWord(baseWord,word, 4, 14); //enable slotID now 00000000011
353 word = 0;
354 PackWord(baseWord,word, 15, 15); // something
355 word=0;
356 PackWord(baseWord,word, 16, 27); // fault ID for simulation 0
357 word=4;
358 PackWord(baseWord,word,28,31); // 0100 marks header
359 fBuffer[2]= baseWord;
5325480c 360
dc7ca31d 361
362
363 word=0;
364 baseWord=0;
365 //DRM status header 3
366 word = 1;
367 PackWord(baseWord,word,0, 3); // 0001
368 word = 0;
369 PackWord(baseWord,word,4, 27); // TTC event counter
370 word=4;
371 PackWord(baseWord,word,28,31); // 0100 marks header
372 fBuffer[3]= baseWord;
373
5325480c 374 // new DRM format
375 fBuffer[4]= baseWord;
376 fBuffer[5]= baseWord;
377
dc7ca31d 378 word=0;
379 baseWord=0;
380
381}
382
383//_____________________________________________________________________________
384
385void AliT0RawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
386 Int_t positionOfTRMHeader)
387{
388//Write a (dummy or real) DDL TRM data header,
389//set the compression bit if compressed
390// UInt_t trmheader;
391 UInt_t word;
392 UInt_t baseWord=0;
393 //fill TRM headers
394 //TRM Global Header
395 word = slotID;
396 PackWord(baseWord,word,0, 3); // slotID
397 word = nWordsInTRM;
398 //+this word - DRM header
399
400 PackWord(baseWord,word,4, 16); // event words
401 word=0;
402 PackWord(baseWord,word,17,18); // ACQ
403 word=0;
404 PackWord(baseWord,word,19,19); // L SEY inside LUT
405 word=0;
406 PackWord(baseWord,word,20,27); // MBZ
407 word=4;
408 PackWord(baseWord,word,28,31); // 0100 marks header
409 fBuffer[positionOfTRMHeader] = baseWord;
5325480c 410
dc7ca31d 411 word=0;
412 baseWord=0;
413
414}
415
416//_____________________________________________________________________________
417
418void AliT0RawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
419{
420//Write a (dummy or real) DDL Chain data header,
421//set the compression bit if compressed
422// chainNumber 00 or 10
423 UInt_t word;
424 UInt_t baseWord=0;
425 //fill TRM headers
426 //TRM Global Header
427 word = slotID; // ask Tatiana 7 or 9
428 PackWord(baseWord,word,0, 3); // slotID
429 word = 0;
430 PackWord(baseWord,word,4, 15); // bunchID
431 word=0;
432 PackWord(baseWord,word,16,23); // PB24 temperature
433 word=0;
434 PackWord(baseWord,word,24,26); // PB24 ID
435 word=0;
436 PackWord(baseWord,word,27,27); // TS
437 word=chainNumber;
438 PackWord(baseWord,word,28,31); // 0100 marks header
5325480c 439 fBuffer[fIndex] = baseWord;
440
441 word=0;
442 baseWord=0;
443
444}
445//_____________________________________________________________________________
446
447void AliT0RawData::WriteChainDataTrailer(UInt_t chainNumber )
448{
449//Write a (dummy or real) DDL Chain data trailer
450//set the compression bit if compressed
451// chainNumber 00 or 10
452 UInt_t word;
453 UInt_t baseWord=0;
454 word = 0; // ask Tatiana 7 or 9
455 PackWord(baseWord,word,0, 3); // status
456 word = 0;
457 PackWord(baseWord,word,4, 15); // MBZ
458 word=fEventNumber;
459 PackWord(baseWord,word,16,27); // event counter
460 word=chainNumber;
461 PackWord(baseWord,word,28,31); // chain number
462 fIndex++;
463 fBuffer[fIndex] = baseWord;
dc7ca31d 464
465 word=0;
466 baseWord=0;
467
468}
469//_____________________________________________________________________________
470
471void AliT0RawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
472{
473//Write a (dummy or real) DDL data header,
474//set the compression bit if compressed
475
476 AliRawDataHeaderSim header;
477
478 if (dummy) {
479 //if size=0 it means that this data header is a dummy data header
480 fDataHeaderPos = fFile->Tellp();
481 fFile->WriteBuffer((char*)(&header), sizeof(header));
482 } else {
483 UInt_t currentFilePos = fFile->Tellp();
484 fFile->Seekp(fDataHeaderPos);
485 header.fSize = currentFilePos-fDataHeaderPos;
486 header.SetAttribute(0); // valid data
487 if (compressed) header.SetAttribute(1);
488 fFile->WriteBuffer((char*)(&header), sizeof(header));
489 fFile->Seekp(currentFilePos);
490 }
491
492}
493
494//___ __________________________________________________________________________
495
496
497void AliT0RawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
498{
499
500 UInt_t word;
501 UInt_t baseWord=0;
502 word = slot;
503 PackWord(baseWord,word,0, 3); // 0001
504 word=word1;
505 PackWord(baseWord,word,4, 15); // CRC ?
506 word = word2;
507 PackWord(baseWord,word,16,27); // event counter
508 word=word3;
509 PackWord(baseWord,word,28,31); // marks trailer
510 fIndex++;
511 fBuffer[fIndex] = baseWord;
dc7ca31d 512 word=0;
513 baseWord=0;
514
515}
516
517//---------------------------------------------------------------------------------------
518//---------------------------------------------------------------------------------------
519void AliT0RawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
520{
521 UInt_t word;
522 UInt_t baseWord=0;
523
524 word=time;
525 PackWord(baseWord,word, 0, 20); // Time
526
527 word=ch;
528 PackWord(baseWord,word, 21, 23); // number of channel
529 word=iTDC;
530 PackWord(baseWord,word, 24, 27); // TDC ID
531
532 word=0;
533 PackWord(baseWord,word, 28, 28); // E = 0 in simulation
534 word=0;
535 PackWord(baseWord,word, 29, 30); // PS bit data 00
536 word=1;
537 PackWord(baseWord,word, 31, 31); // 1
538 fIndex++;
539 fBuffer[fIndex]=baseWord;
540 word=0;
541 baseWord=0;
dc7ca31d 542}
543//---------------------------------------------------------------------------------------
544
545Int_t AliT0RawData::RawDataT0(AliT0digit *fDigits)
546{
547 //This method creates the Raw data files for T0 detector
548
549
550 // const Int_t kSize=512; //2*AliTOFGeometry::NpadXSector()
551 //max number of digits per DDL file times 2
552 // UInt_t fBuffer[kSize];
553 // UInt_t baseWord;
554 // UInt_t word;
555
556 fIndex=-1;
557
94c27e4f 558
dc7ca31d 559 WriteDataHeader(kTRUE, kFALSE);
560 GetDigits(fDigits);
561 //write packing digits
c41ceaac 562
563
dc7ca31d 564 fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
565 //write real data header on its place
566 WriteDataHeader(kFALSE, kFALSE);
567
568
569 //end for
570
571 return 0;
572
573}