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