changes for FIT
[u/mrichter/AliRoot.git] / FIT / AliFITRawData.cxx
CommitLineData
c1c44db3 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// FIT raw data conversion class //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
24//#include <Riostream.h>
25//#include <TTree.h>
26#include <TMap.h>
27#include "AliFIT.h"
28#include "AliFITRawData.h"
29#include "AliFITDigit.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"
36#include "AliDAQ.h"
37
38using std::cout;
39using std::endl;
40ClassImp(AliFITRawData)
41
42//_____________________________________________________________________________
43 AliFITRawData::AliFITRawData():TObject(),
44 fFITdigitArray(NULL),
45 fVerbose(0),
46 fIndex(-1) ,
47 fEventNumber(0),
48 fDataHeaderPos(0),
49 fFile(0x0)
50
51{
52 /*
53 - 160 channels (2 words each as in TOF DDL) for :
54 word 1 :0-5bit number of PMT; word 2: 0-7 error sign, 8-31 TDC
55 and the same but for amplified signal. Now I wrote the same time because
56 CDF are not ready and differences didn't measured yet.
57
58 - 160x2 channel for amplitude: very preliminary, QTC features are not
59 known now, preliminary i put as T1 time signal for this PMT in first
60 channel and T1+A in second, where A=Log(Amplitude);
61 and the same for amplified but A=Log(10*Amplitude).
62
63 - Or FIT-A and Or FIT-C 2 channels
64 - FITA-FITC vertex information
65 - Time Meaner (FITA+FITC)/2
66 - 4 MPD multiplicity signals sum amp both sides
67 */
68 //open the output file
69 // char fileName[15];
70 TString fileName = Form("%s",AliDAQ::DdlFileName("FIT",0));
71 fFile = new AliFstream(fileName.Data());
72 memset(fBuffer,0,512*sizeof(UInt_t));
73
74 //get event number
75 AliRunLoader *runloader = AliRunLoader::Instance();
76 if (runloader) {
77 fEventNumber = runloader->GetEventNumber();
78 }
79
80}
81
82//_____________________________________________________________________________
83
84AliFITRawData::AliFITRawData(const AliFITRawData &r):TObject(),
85 fFITdigitArray(NULL),
86 fVerbose(0),
87 fIndex(-1) ,
88 fEventNumber(0),
89 fDataHeaderPos(0),
90 fFile(0x0)
91{
92 //
93 // AliFITrawData copy constructor
94 //
95
96 ((AliFITRawData &) r).Copy(*this);
97
98}
99
100//_____________________________________________________________________________
101AliFITRawData::~AliFITRawData()
102{
103 //
104 // Destructor
105 //
106}
107
108//_____________________________________________________________________________
109AliFITRawData &AliFITRawData::operator=(const AliFITRawData &r)
110{
111 //
112 // Assignment operator
113 //
114
115 if (this != &r) ((AliFITRawData &) r).Copy(*this);
116 return *this;
117
118}
119
120//_____________________________________________________________________________
121void AliFITRawData::GetDigits()
122{
123
124 //This method packs the FIT digits in a proper 32 bits structure
125
126 //read FIT digits and fill TDC and ADC arrays
127
128 Int_t digit = -1;
129 Int_t ndigits = fFITdigitArray->GetEntries();
130 AliDebug(2, Form(" Number of read digits = %d",ndigits));
131 AliFITDigit *digs;
132 for(Int_t i=0; i<500; i++) fAllData[i]=-1;
133 // loop on FIT digits
134 for (digit=0; digit<ndigits; digit++) {
135 digs = (AliFITDigit*)fFITdigitArray->UncheckedAt(digit);
136 Int_t pmt = digs->NPMT();
137 fAllData[pmt] = digs->TimeCFD();
138 fAllData[pmt+160] = digs->TimeQT0();
139 fAllData[pmt+320] = digs->TimeQT1();
140 }
141 // Int_t error=0;
142
143}
144//---------------------------------------------------------------------------------------
145
146Int_t AliFITRawData::RawDataFIT(TBranch* branch)
147{
148 //This method creates the Raw data files for FIT detector
149
150
151 //max number of digits per DDL file times 2
152 // UInt_t fBuffer[kSize];
153 // UInt_t baseWord;
154 // UInt_t word;
155 cout.setf( ios_base::hex, ios_base::basefield );
156
157 fIndex=-1;
158 Int_t ch[4] = {0,2,4,6};
159
160 fFITdigitArray = * (TClonesArray**) branch->GetAddress();
161
162 AliRawDataHeaderSim header;
163 WriteDataHeader(kTRUE, kFALSE);
164
165 branch->GetEvent();
166
167 GetDigits();
168
169 Int_t time, positionOfTRMHeader, iTDC, channel;
170 //space for DRM header
171 fIndex += 6;
172 Int_t trm1words=0;
173
174 for (Int_t itrm=0; itrm <4 ; itrm++) {
175 Int_t startTRM=fIndex;
176 //space for 1st TRM header
177 fIndex ++;
178 positionOfTRMHeader= fIndex;
179
180 for (Int_t chain=0; chain <2 ; chain++) {
181 // space for chain header
182 fIndex ++;
183 WriteChainDataHeader(chain+1, 1); //
184 //data TRM 1 chain 1
185 for (Int_t det = 0; det < 60; det++) {
186 time = fAllData[det + itrm*120 + chain*60];
187 if (time >0 && time !=999999) {
188 fIndex++;
189 iTDC = det / 4;
190 channel = ch[det - iTDC*4];
191 AliDebug(2, Form("det %i alldata %i trm %i chain %i tdc %i channel %i \n",det, det + itrm*120 + chain*60, itrm, chain, iTDC, det - iTDC*4) );
192 FillTime(channel,iTDC, time);
193 }
194 }
195 fIndex++;
196 WriteChainDataTrailer (chain+1);
197 }
198 fIndex++;
199 WriteTrailer(15, 0,fEventNumber,5); // 1st TRM trailer
200
201 trm1words = fIndex - startTRM;
202 WriteTRMDataHeader(itrm, trm1words , positionOfTRMHeader);
203 }
204
205 //DRM trailer
206 fIndex++;
207 WriteTrailer(1,0,fEventNumber,5);
208 WriteDRMDataHeader();
209
210 //write packing digits
211 fFile->WriteBuffer((char*) fBuffer,((fIndex+1)*sizeof(UInt_t)));
212 //write real data header on its place
213 WriteDataHeader(kFALSE, kFALSE);
214
215
216 //end for
217
218 return 0;
219
220}
221
222//_____________________________________________________________________________
223
224void AliFITRawData::WriteDRMDataHeader()
225{
226 //Write a (dummy or real) DDL DRM data header,
227 //set the compression bit if compressed
228 // UInt_t drmheader[4];
229 cout.setf( ios_base::hex, ios_base::basefield );
230 UInt_t word;
231 UInt_t baseWord=0;
232 //fill DRM headers
233 //DRM Global Header
234 word = 1;
235 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
236 word = fIndex ;
237 AliBitPacking::PackWord(word,baseWord,4, 20); // event words
238 word=124;
239 AliBitPacking::PackWord(word,baseWord, 21, 27); // event words
240 word=4;
241 AliBitPacking::PackWord(word,baseWord,28, 31);// 0100 marks header
242 fBuffer[0]= baseWord;
243 //DRM status header 1
244 word = 1;
245 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
246 word = 1;
247 AliBitPacking::PackWord(word,baseWord,4, 14); // slotID now 0000000001
248 word = 1;
249 AliBitPacking::PackWord(word,baseWord,15, 15); //if 1 LHC clock is coorectly recieved from CPDM
250 word=0;
251 AliBitPacking::PackWord(word,baseWord,16,27); // reserve for future use
252 word=4;
253 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
254 fBuffer[1] = baseWord;
255
256 word=0;
257 baseWord=0;
258
259 //DRM status header 2
260 word = 1;
261 AliBitPacking::PackWord(word,baseWord, 0, 3); // 0001
262 word = 3;
263 AliBitPacking::PackWord(word,baseWord, 4, 14); //enable slotID now 00000000011
264 word = 0;
265 AliBitPacking::PackWord(word,baseWord, 15, 15); // something
266 word=0;
267 AliBitPacking::PackWord(word,baseWord, 16, 27); // fault ID for simulation 0
268 word=4;
269 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
270 fBuffer[2]= baseWord;
271
272 word=0;
273 baseWord=0;
274 //DRM status header 3
275 word = 1;
276 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
277 word = 0;
278 AliBitPacking::PackWord(word,baseWord,4, 27); // TTC event counter
279 word=4;
280 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
281 fBuffer[3]= baseWord;
282
283 // new DRM format
284 fBuffer[4]= baseWord;
285 fBuffer[5]= baseWord;
286
287 word=0;
288 baseWord=0;
289
290}
291
292//_____________________________________________________________________________
293
294void AliFITRawData::WriteTRMDataHeader(UInt_t slotID, Int_t nWordsInTRM,
295 Int_t positionOfTRMHeader)
296{
297 //Write a (dummy or real) DDL TRM data header,
298 //set the compression bit if compressed
299 // UInt_t trmheader;
300 UInt_t word;
301 UInt_t baseWord=0;
302 //fill TRM headers
303 //TRM Global Header
304 word = slotID;
305 AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
306 word = nWordsInTRM;
307 //+this word - DRM header
308
309 AliBitPacking::PackWord(word,baseWord,4, 16); // event words
310 word=0;
311 AliBitPacking::PackWord(word,baseWord,17,18); // ACQ
312 word=0;
313 AliBitPacking::PackWord(word,baseWord,19,19); // L SEY inside LUT
314 word=0;
315 AliBitPacking::PackWord(word,baseWord,20,27); // MBZ
316 word=4;
317 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
318 fBuffer[positionOfTRMHeader] = baseWord;
319 word=0;
320 baseWord=0;
321}
322
323//_____________________________________________________________________________
324
325void AliFITRawData::WriteChainDataHeader(UInt_t chainNumber,UInt_t slotID)
326{
327 //Write a (dummy or real) DDL Chain data header,
328 //set the compression bit if compressed
329 // chainNumber 00 or 10
330 UInt_t word;
331 UInt_t baseWord=0;
332 //fill TRM headers
333 //TRM Global Header
334 word = slotID; // ask Tatiana 7 or 9
335 AliBitPacking::PackWord(word,baseWord,0, 3); // slotID
336 word = 0;
337 AliBitPacking::PackWord(word,baseWord,4, 15); // bunchID
338 word=0;
339 AliBitPacking::PackWord(word,baseWord,16,23); // PB24 temperature
340 word=0;
341 AliBitPacking::PackWord(word,baseWord,24,26); // PB24 ID
342 word=0;
343 AliBitPacking::PackWord(word,baseWord,27,27); // TS
344 word=chainNumber;
345 AliBitPacking::PackWord(word,baseWord,28,31); // 0100 marks header
346 fBuffer[fIndex] = baseWord;
347 word=0;
348 baseWord=0;
349}
350//_____________________________________________________________________________
351
352void AliFITRawData::WriteChainDataTrailer(UInt_t chainNumber )
353{
354 //Write a (dummy or real) DDL Chain data trailer
355 //set the compression bit if compressed
356 // chainNumber 00 or 10
357 UInt_t word;
358 UInt_t baseWord=0;
359 word = 0; // ask Tatiana 7 or 9
360 AliBitPacking::PackWord(word,baseWord,0, 3); // status
361 word = 0;
362 AliBitPacking::PackWord(word,baseWord,4, 15); // MBZ
363 word=fEventNumber;
364 AliBitPacking::PackWord(word,baseWord,16,27); // event counter
365 word=chainNumber;
366 AliBitPacking::PackWord(word,baseWord,28,31); // chain number
367 // fIndex++;
368 fBuffer[fIndex] = baseWord;
369
370 word=0;
371 baseWord=0;
372}
373//_____________________________________________________________________________
374
375void AliFITRawData::WriteDataHeader(Bool_t dummy, Bool_t compressed)
376{
377 //Write a (dummy or real) DDL data header,
378 //set the compression bit if compressed
379
380 AliRawDataHeaderSim header;
381
382 if (dummy) {
383 //if size=0 it means that this data header is a dummy data header
384 fDataHeaderPos = fFile->Tellp();
385 fFile->WriteBuffer((char*)(&header), sizeof(header));
386 } else {
387 UInt_t currentFilePos = fFile->Tellp();
388 fFile->Seekp(fDataHeaderPos);
389 header.fSize = currentFilePos-fDataHeaderPos;
390 header.SetAttribute(0); // valid data
391 if (compressed) header.SetAttribute(1);
392 fFile->WriteBuffer((char*)(&header), sizeof(header));
393 fFile->Seekp(currentFilePos);
394 }
395
396}
397
398//___ __________________________________________________________________________
399void AliFITRawData::WriteTrailer(UInt_t slot, Int_t word1, UInt_t word2, UInt_t word3)
400{
401 //Write a (dummy or real) DDL Chain data trailer
402
403 UInt_t word;
404 UInt_t baseWord=0;
405 word = slot;
406 AliBitPacking::PackWord(word,baseWord,0, 3); // 0001
407 word=word1;
408 AliBitPacking::PackWord(word,baseWord,4, 15); // CRC ?
409 word = word2;
410 AliBitPacking::PackWord(word,baseWord,16,27); // event counter
411 word=word3;
412 AliBitPacking::PackWord(word,baseWord,28,31); // marks trailer
413 fBuffer[fIndex] = baseWord;
414 word=0;
415 baseWord=0;
416
417}
418
419//---------------------------------------------------------------------------------------
420void AliFITRawData::FillTime(Int_t ch, Int_t iTDC, Int_t time)
421{
422 // put all time fields thother in 1 word
423
424 UInt_t word;
425 UInt_t baseWord=0;
426
427 word=time;
428 AliBitPacking::PackWord(word,baseWord, 0, 20); // Time
429
430 word=ch;
431 AliBitPacking::PackWord(word,baseWord, 21, 23); // number of channel
432 word=iTDC;
433 AliBitPacking::PackWord(word,baseWord, 24, 27); // TDC ID
434
435 word=0;
436 AliBitPacking::PackWord(word,baseWord, 28, 28); // E = 0 in simulation
437 word=0;
438 AliBitPacking::PackWord(word,baseWord, 29, 30); // PS bit data 00
439 word=1;
440 AliBitPacking::PackWord(word,baseWord, 31, 31); // 1
441 fBuffer[fIndex]=baseWord;
442 word=0;
443 baseWord=0;
444}