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