1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // TRD raw data conversion class //
22 ///////////////////////////////////////////////////////////////////////////////
24 #include <Riostream.h>
26 #include "AliTRDrawData.h"
27 #include "AliTRDdigitsManager.h"
28 #include "AliTRDgeometryFull.h"
29 #include "AliTRDparameter.h"
30 #include "AliTRDdataArrayI.h"
31 #include "AliTRDRawStream.h"
32 #include "AliRawDataHeader.h"
34 ClassImp(AliTRDrawData)
36 //_____________________________________________________________________________
37 AliTRDrawData::AliTRDrawData():TObject()
40 // Default constructor
47 //_____________________________________________________________________________
48 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r):TObject()
51 // AliTRDrawData copy constructor
54 ((AliTRDrawData &) r).Copy(*this);
58 //_____________________________________________________________________________
59 AliTRDrawData::~AliTRDrawData()
67 //_____________________________________________________________________________
68 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
71 // Assignment operator
74 if (this != &r) ((AliTRDrawData &) r).Copy(*this);
79 //_____________________________________________________________________________
80 void AliTRDrawData::Copy(TObject &r)
86 ((AliTRDrawData &) r).fDebug = fDebug;
90 //_____________________________________________________________________________
91 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
94 // Convert the digits to raw data byte stream. The output is written
95 // into the the binary files TRD_<DDL number>.ddl.
97 // The pseudo raw data format is currently defined like this:
101 // Subevent (= single chamber) header (8 bytes)
103 // Detector number (2 bytes)
104 // Number of data bytes (2 bytes)
105 // Number of pads with data (2 bytes)
111 const Int_t kNumberOfDDLs = 18;
112 const Int_t kSubeventHeaderLength = 8;
113 const Int_t kSubeventDummyFlag = 0xBB;
114 int headerSubevent[2];
116 ofstream *outputFile[kNumberOfDDLs];
117 UInt_t bHPosition[kNumberOfDDLs];
118 Int_t ntotalbyte[kNumberOfDDLs];
121 unsigned char *bytePtr;
122 unsigned char *headerPtr;
124 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
125 digitsManager->SetDebug(fDebug);
127 // Read in the digit arrays
128 if (!digitsManager->ReadDigits(digitsTree)) {
129 delete digitsManager;
133 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
134 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
135 ,"TRD parameter class");
136 AliTRDdataArrayI *digits;
139 AliRawDataHeader header;
142 printf("Event header size: %d \n",sizeof(header));
145 // Open the output files
146 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
148 sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
150 outputFile[iDDL] = new ofstream(name, ios::binary);
152 outputFile[iDDL] = new ofstream(name);
155 // Write a dummy data header
156 bHPosition[iDDL] = outputFile[iDDL]->tellp();
157 outputFile[iDDL]->write((char*)(&header),sizeof(header));
158 ntotalbyte[iDDL] = 0;
161 // Loop through all detectors
162 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
164 Int_t cham = geo->GetChamber(det);
165 Int_t plan = geo->GetPlane(det);
166 Int_t sect = geo->GetSector(det);
167 Int_t rowMax = par->GetRowMax(plan,cham,sect);
168 Int_t colMax = par->GetColMax(plan);
169 Int_t timeMax = par->GetTimeMax();
170 Int_t bufferMax = rowMax*colMax*timeMax;
171 int *buffer = new int[bufferMax];
175 bytePtr = (unsigned char *) buffer;
179 // Get the digits array
180 digits = digitsManager->GetDigits(det);
183 // Loop through the detector pixel
184 for (Int_t col = 0; col < colMax; col++) {
185 for (Int_t row = 0; row < rowMax; row++) {
187 // Check whether data exists for this pad
188 Bool_t dataflag = kFALSE;
189 for (Int_t time = 0; time < timeMax; time++) {
190 Int_t data = digits->GetDataUnchecked(row,col,time);
201 // The pad row number
202 *bytePtr++ = row + 1;
203 // The pad column number
204 *bytePtr++ = col + 1;
208 for (Int_t time = 0; time < timeMax; time++) {
210 Int_t data = digits->GetDataUnchecked(row,col,time);
214 if ((nzero == 256) ||
215 (time == timeMax-1)) {
217 *bytePtr++ = nzero-1;
225 *bytePtr++ = nzero-1;
229 // High byte (MSB always set)
230 *bytePtr++ = ((data >> 8) | 128);
232 *bytePtr++ = (data & 0xff);
244 // Fill the end of the buffer with zeros
251 Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
254 // Write the subevent header
255 bytePtr = (unsigned char *) headerSubevent;
257 *bytePtr++ = kSubeventDummyFlag;
258 *bytePtr++ = (det & 0xff);
259 *bytePtr++ = (det >> 8);
260 *bytePtr++ = (nbyte & 0xff);
261 *bytePtr++ = (nbyte >> 8);
262 *bytePtr++ = (npads & 0xff);
263 *bytePtr++ = (npads >> 8);
265 outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
267 // Write the buffer to the file
268 bytePtr = (unsigned char *) buffer;
269 outputFile[iDDL]->write((char*)bytePtr,nbyte);
271 ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
278 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
279 Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
283 // Update the data headers and close the output files
284 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
285 header.fSize = UInt_t(outputFile[iDDL]->tellp()) - bHPosition[iDDL];
286 header.SetAttribute(0); // valid data
287 outputFile[iDDL]->seekp(bHPosition[iDDL]);
288 outputFile[iDDL]->write((char*)(&header),sizeof(header));
290 outputFile[iDDL]->close();
291 delete outputFile[iDDL];
296 delete digitsManager;
302 //_____________________________________________________________________________
303 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
306 // Read the raw data digits and put them into the returned digits manager
309 AliTRDdataArrayI *digits = 0;
311 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
312 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
313 ,"TRD parameter class");
315 // Create the digits manager
316 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
317 digitsManager->SetDebug(fDebug);
318 digitsManager->CreateArrays();
320 AliTRDRawStream input(rawReader);
322 // Loop through the digits
323 while (input.Next()) {
325 Int_t det = input.GetDetector();
326 Int_t npads = input.GetNPads();
328 if (input.IsNewDetector()) {
330 if (digits) digits->Compress(1,0);
333 Info("Raw2Digits","Subevent header:");
334 Info("Raw2Digits","\tdet = %d",det);
335 Info("Raw2Digits","\tnpads = %d",npads);
338 // Create the data buffer
339 Int_t cham = geo->GetChamber(det);
340 Int_t plan = geo->GetPlane(det);
341 Int_t sect = geo->GetSector(det);
342 Int_t rowMax = par->GetRowMax(plan,cham,sect);
343 Int_t colMax = par->GetColMax(plan);
344 Int_t timeMax = par->GetTimeMax();
346 // Add a container for the digits of this detector
347 digits = digitsManager->GetDigits(det);
348 // Allocate memory space for the digits buffer
349 if (digits->GetNtime() == 0) {
350 digits->Allocate(rowMax,colMax,timeMax);
354 digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
355 input.GetTime(),input.GetSignal());
358 if (digits) digits->Compress(1,0);
363 return digitsManager;