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