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