EffC++ warnings corrected. (M. Lopez Noriega)
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
CommitLineData
5990c064 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
88cb7938 16/* $Id$ */
5990c064 17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// TRD raw data conversion class //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
a2cb5b3d 24#include <Riostream.h>
5990c064 25
2745a409 26#include "AliDAQ.h"
27#include "AliRawDataHeader.h"
28#include "AliRawReader.h"
29#include "AliLog.h"
30
5990c064 31#include "AliTRDrawData.h"
32#include "AliTRDdigitsManager.h"
bd0f8685 33#include "AliTRDgeometry.h"
5990c064 34#include "AliTRDdataArrayI.h"
b864d801 35#include "AliTRDRawStream.h"
3551db50 36#include "AliTRDCommonParam.h"
37#include "AliTRDcalibDB.h"
5990c064 38
39ClassImp(AliTRDrawData)
40
41//_____________________________________________________________________________
42AliTRDrawData::AliTRDrawData():TObject()
43{
b864d801 44 //
45 // Default constructor
46 //
5990c064 47
5990c064 48}
49
50//_____________________________________________________________________________
51AliTRDrawData::~AliTRDrawData()
52{
53 //
54 // Destructor
55 //
56
5990c064 57}
58
59//_____________________________________________________________________________
b864d801 60Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
5990c064 61{
62 //
63 // Convert the digits to raw data byte stream. The output is written
b864d801 64 // into the the binary files TRD_<DDL number>.ddl.
5990c064 65 //
66 // The pseudo raw data format is currently defined like this:
67 //
b864d801 68 // DDL data header
5990c064 69 //
70 // Subevent (= single chamber) header (8 bytes)
71 // FLAG
72 // Detector number (2 bytes)
73 // Number of data bytes (2 bytes)
74 // Number of pads with data (2 bytes)
75 // 1 empty byte
76 //
77 // Data bank
78 //
79
362c9d61 80 const Int_t kNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
5990c064 81 const Int_t kSubeventHeaderLength = 8;
5990c064 82 const Int_t kSubeventDummyFlag = 0xBB;
e3e1266c 83 Int_t headerSubevent[3];
5990c064 84
bc33499b 85 ofstream **outputFile = new ofstream* [kNumberOfDDLs];
86 UInt_t *bHPosition = new UInt_t [kNumberOfDDLs];
87 Int_t *ntotalbyte = new Int_t [kNumberOfDDLs];
5990c064 88 Int_t nbyte = 0;
89 Int_t npads = 0;
b864d801 90 unsigned char *bytePtr;
91 unsigned char *headerPtr;
5990c064 92
b864d801 93 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
5990c064 94
95 // Read in the digit arrays
b864d801 96 if (!digitsManager->ReadDigits(digitsTree)) {
97 delete digitsManager;
5990c064 98 return kFALSE;
99 }
100
bd0f8685 101 AliTRDgeometry *geo = new AliTRDgeometry();
102 AliTRDdataArrayI *digits;
5990c064 103
3551db50 104 AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
2745a409 105 if (!commonParam) {
106 AliError("Could not get common parameters\n");
3551db50 107 return 0;
108 }
109
110 AliTRDcalibDB* calibration = AliTRDcalibDB::Instance();
111 if (!calibration)
112 {
2745a409 113 AliError("Could not get calibration object\n");
3551db50 114 return kFALSE;
115 }
116
b864d801 117 // the event header
118 AliRawDataHeader header;
5990c064 119
b864d801 120 // Open the output files
121 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
122 char name[20];
362c9d61 123 strcpy(name,AliDAQ::DdlFileName("TRD",iDDL));
b864d801 124#ifndef __DECCXX
125 outputFile[iDDL] = new ofstream(name, ios::binary);
126#else
127 outputFile[iDDL] = new ofstream(name);
128#endif
129
130 // Write a dummy data header
131 bHPosition[iDDL] = outputFile[iDDL]->tellp();
132 outputFile[iDDL]->write((char*)(&header),sizeof(header));
133 ntotalbyte[iDDL] = 0;
5990c064 134 }
135
136 // Loop through all detectors
137 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
138
139 Int_t cham = geo->GetChamber(det);
140 Int_t plan = geo->GetPlane(det);
141 Int_t sect = geo->GetSector(det);
3551db50 142 Int_t rowMax = commonParam->GetRowMax(plan,cham,sect);
143 Int_t colMax = commonParam->GetColMax(plan);
144 Int_t timeTotal = calibration->GetNumberOfTimeBins();
928e9fae 145 Int_t bufferMax = rowMax*colMax*timeTotal;
e3e1266c 146 Int_t *buffer = new Int_t[bufferMax];
5990c064 147
b864d801 148 npads = 0;
149 nbyte = 0;
150 bytePtr = (unsigned char *) buffer;
5990c064 151
b864d801 152 Int_t iDDL = sect;
5990c064 153
154 // Get the digits array
b864d801 155 digits = digitsManager->GetDigits(det);
5990c064 156 digits->Expand();
157
158 // Loop through the detector pixel
159 for (Int_t col = 0; col < colMax; col++) {
160 for (Int_t row = 0; row < rowMax; row++) {
161
162 // Check whether data exists for this pad
163 Bool_t dataflag = kFALSE;
928e9fae 164 for (Int_t time = 0; time < timeTotal; time++) {
5990c064 165 Int_t data = digits->GetDataUnchecked(row,col,time);
166 if (data) {
167 dataflag = kTRUE;
168 break;
169 }
170 }
171
172 if (dataflag) {
173
174 npads++;
175
176 // The pad row number
b864d801 177 *bytePtr++ = row + 1;
5990c064 178 // The pad column number
b864d801 179 *bytePtr++ = col + 1;
5990c064 180 nbyte += 2;
181
182 Int_t nzero = 0;
928e9fae 183 for (Int_t time = 0; time < timeTotal; time++) {
5990c064 184
185 Int_t data = digits->GetDataUnchecked(row,col,time);
186
187 if (!data) {
188 nzero++;
189 if ((nzero == 256) ||
928e9fae 190 (time == timeTotal-1)) {
b864d801 191 *bytePtr++ = 0;
192 *bytePtr++ = nzero-1;
5990c064 193 nbyte += 2;
194 nzero = 0;
195 }
196 }
197 else {
198 if (nzero) {
b864d801 199 *bytePtr++ = 0;
200 *bytePtr++ = nzero-1;
5990c064 201 nbyte += 2;
202 nzero = 0;
203 }
204 // High byte (MSB always set)
b864d801 205 *bytePtr++ = ((data >> 8) | 128);
5990c064 206 // Low byte
b864d801 207 *bytePtr++ = (data & 0xff);
5990c064 208 nbyte += 2;
209 }
210
211 }
212
213 }
214
215 }
216
217 }
218
219 // Fill the end of the buffer with zeros
220 while (nbyte % 4) {
b864d801 221 *bytePtr++ = 0;
5990c064 222 nbyte++;
223 }
224
2745a409 225 AliDebug(1,Form("det = %d, nbyte = %d (%d)",det,nbyte,bufferMax));
5990c064 226
227 // Write the subevent header
b864d801 228 bytePtr = (unsigned char *) headerSubevent;
229 headerPtr = bytePtr;
230 *bytePtr++ = kSubeventDummyFlag;
231 *bytePtr++ = (det & 0xff);
232 *bytePtr++ = (det >> 8);
233 *bytePtr++ = (nbyte & 0xff);
234 *bytePtr++ = (nbyte >> 8);
928e9fae 235 *bytePtr++ = (nbyte >> 16);
b864d801 236 *bytePtr++ = (npads & 0xff);
237 *bytePtr++ = (npads >> 8);
b864d801 238 outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
5990c064 239
240 // Write the buffer to the file
b864d801 241 bytePtr = (unsigned char *) buffer;
242 outputFile[iDDL]->write((char*)bytePtr,nbyte);
5990c064 243
b864d801 244 ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
5990c064 245
246 delete buffer;
247
248 }
249
b864d801 250 // Update the data headers and close the output files
251 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
2745a409 252
b864d801 253 header.fSize = UInt_t(outputFile[iDDL]->tellp()) - bHPosition[iDDL];
254 header.SetAttribute(0); // valid data
255 outputFile[iDDL]->seekp(bHPosition[iDDL]);
256 outputFile[iDDL]->write((char*)(&header),sizeof(header));
257
258 outputFile[iDDL]->close();
259 delete outputFile[iDDL];
2745a409 260
b864d801 261 }
5990c064 262
263 delete geo;
b864d801 264 delete digitsManager;
5990c064 265
bc33499b 266 delete [] outputFile;
267 delete [] bHPosition;
268 delete [] ntotalbyte;
269
5990c064 270 return kTRUE;
271
272}
273
274//_____________________________________________________________________________
b864d801 275AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
5990c064 276{
b864d801 277 //
278 // Read the raw data digits and put them into the returned digits manager
279 //
5990c064 280
5990c064 281 AliTRDdataArrayI *digits = 0;
928e9fae 282 AliTRDdataArrayI *track0 = 0;
283 AliTRDdataArrayI *track1 = 0;
284 AliTRDdataArrayI *track2 = 0;
5990c064 285
bd0f8685 286 AliTRDgeometry *geo = new AliTRDgeometry();
3551db50 287
288 AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
2745a409 289 if (!commonParam) {
290 AliError("Could not get common parameters\n");
3551db50 291 return 0;
292 }
293
294 AliTRDcalibDB* calibration = AliTRDcalibDB::Instance();
2745a409 295 if (!calibration) {
296 AliError("Could not get calibration object\n");
3551db50 297 return 0;
298 }
5990c064 299
300 // Create the digits manager
b864d801 301 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
b864d801 302 digitsManager->CreateArrays();
5990c064 303
3551db50 304 AliTRDRawStream input(rawReader);
5990c064 305
b864d801 306 // Loop through the digits
307 while (input.Next()) {
5990c064 308
b864d801 309 Int_t det = input.GetDetector();
310 Int_t npads = input.GetNPads();
5990c064 311
b864d801 312 if (input.IsNewDetector()) {
313
314 if (digits) digits->Compress(1,0);
928e9fae 315 if (track0) track0->Compress(1,0);
316 if (track1) track1->Compress(1,0);
317 if (track2) track2->Compress(1,0);
5990c064 318
2745a409 319 AliDebug(2,"Subevent header:");
320 AliDebug(2,Form("\tdet = %d",det));
321 AliDebug(2,Form("\tnpads = %d",npads));
5990c064 322
323 // Create the data buffer
324 Int_t cham = geo->GetChamber(det);
325 Int_t plan = geo->GetPlane(det);
326 Int_t sect = geo->GetSector(det);
3551db50 327 Int_t rowMax = commonParam->GetRowMax(plan,cham,sect);
328 Int_t colMax = commonParam->GetColMax(plan);
329 Int_t timeTotal = calibration->GetNumberOfTimeBins();
5990c064 330
331 // Add a container for the digits of this detector
b864d801 332 digits = digitsManager->GetDigits(det);
928e9fae 333 track0 = digitsManager->GetDictionary(det,0);
334 track1 = digitsManager->GetDictionary(det,1);
335 track2 = digitsManager->GetDictionary(det,2);
5990c064 336 // Allocate memory space for the digits buffer
337 if (digits->GetNtime() == 0) {
928e9fae 338 digits->Allocate(rowMax,colMax,timeTotal);
339 track0->Allocate(rowMax,colMax,timeTotal);
340 track1->Allocate(rowMax,colMax,timeTotal);
341 track2->Allocate(rowMax,colMax,timeTotal);
5990c064 342 }
343
5990c064 344 }
928e9fae 345
b864d801 346 digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
347 input.GetTime(),input.GetSignal());
928e9fae 348 track0->SetDataUnchecked(input.GetRow(),input.GetColumn(),
349 input.GetTime(), -1);
350 track1->SetDataUnchecked(input.GetRow(),input.GetColumn(),
351 input.GetTime(), -1);
352 track2->SetDataUnchecked(input.GetRow(),input.GetColumn(),
353 input.GetTime(), -1);
5990c064 354 }
355
b864d801 356 if (digits) digits->Compress(1,0);
928e9fae 357 if (track0) track0->Compress(1,0);
358 if (track1) track1->Compress(1,0);
359 if (track2) track2->Compress(1,0);
b864d801 360
5990c064 361 delete geo;
5990c064 362
b864d801 363 return digitsManager;
5990c064 364
365}