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"
33 #include "AliRawReader.h"
35 ClassImp(AliTRDrawData)
37 //_____________________________________________________________________________
38 AliTRDrawData::AliTRDrawData():TObject()
41 // Default constructor
48 //_____________________________________________________________________________
49 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r):TObject()
52 // AliTRDrawData copy constructor
55 ((AliTRDrawData &) r).Copy(*this);
59 //_____________________________________________________________________________
60 AliTRDrawData::~AliTRDrawData()
68 //_____________________________________________________________________________
69 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
72 // Assignment operator
75 if (this != &r) ((AliTRDrawData &) r).Copy(*this);
80 //_____________________________________________________________________________
81 void AliTRDrawData::Copy(TObject &r) const
87 ((AliTRDrawData &) r).fDebug = fDebug;
91 //_____________________________________________________________________________
92 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
95 // Convert the digits to raw data byte stream. The output is written
96 // into the the binary files TRD_<DDL number>.ddl.
98 // The pseudo raw data format is currently defined like this:
102 // Subevent (= single chamber) header (8 bytes)
104 // Detector number (2 bytes)
105 // Number of data bytes (2 bytes)
106 // Number of pads with data (2 bytes)
112 const Int_t kNumberOfDDLs = 18;
113 const Int_t kSubeventHeaderLength = 8;
114 const Int_t kSubeventDummyFlag = 0xBB;
115 Int_t headerSubevent[3];
117 ofstream *outputFile[kNumberOfDDLs];
118 UInt_t bHPosition[kNumberOfDDLs];
119 Int_t ntotalbyte[kNumberOfDDLs];
122 unsigned char *bytePtr;
123 unsigned char *headerPtr;
125 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
126 digitsManager->SetDebug(fDebug);
128 // Read in the digit arrays
129 if (!digitsManager->ReadDigits(digitsTree)) {
130 delete digitsManager;
134 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
135 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
136 ,"TRD parameter class");
137 AliTRDdataArrayI *digits;
140 AliRawDataHeader header;
142 // Open the output files
143 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
145 sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
147 outputFile[iDDL] = new ofstream(name, ios::binary);
149 outputFile[iDDL] = new ofstream(name);
152 // Write a dummy data header
153 bHPosition[iDDL] = outputFile[iDDL]->tellp();
154 outputFile[iDDL]->write((char*)(&header),sizeof(header));
155 ntotalbyte[iDDL] = 0;
158 // Loop through all detectors
159 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
161 Int_t cham = geo->GetChamber(det);
162 Int_t plan = geo->GetPlane(det);
163 Int_t sect = geo->GetSector(det);
164 Int_t rowMax = par->GetRowMax(plan,cham,sect);
165 Int_t colMax = par->GetColMax(plan);
166 Int_t timeTotal = par->GetTimeTotal();
167 Int_t bufferMax = rowMax*colMax*timeTotal;
168 Int_t *buffer = new Int_t[bufferMax];
172 bytePtr = (unsigned char *) buffer;
176 // Get the digits array
177 digits = digitsManager->GetDigits(det);
180 // Loop through the detector pixel
181 for (Int_t col = 0; col < colMax; col++) {
182 for (Int_t row = 0; row < rowMax; row++) {
184 // Check whether data exists for this pad
185 Bool_t dataflag = kFALSE;
186 for (Int_t time = 0; time < timeTotal; time++) {
187 Int_t data = digits->GetDataUnchecked(row,col,time);
198 // The pad row number
199 *bytePtr++ = row + 1;
200 // The pad column number
201 *bytePtr++ = col + 1;
205 for (Int_t time = 0; time < timeTotal; time++) {
207 Int_t data = digits->GetDataUnchecked(row,col,time);
211 if ((nzero == 256) ||
212 (time == timeTotal-1)) {
214 *bytePtr++ = nzero-1;
222 *bytePtr++ = nzero-1;
226 // High byte (MSB always set)
227 *bytePtr++ = ((data >> 8) | 128);
229 *bytePtr++ = (data & 0xff);
241 // Fill the end of the buffer with zeros
248 Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
251 // Write the subevent header
252 bytePtr = (unsigned char *) headerSubevent;
254 *bytePtr++ = kSubeventDummyFlag;
255 *bytePtr++ = (det & 0xff);
256 *bytePtr++ = (det >> 8);
257 *bytePtr++ = (nbyte & 0xff);
258 *bytePtr++ = (nbyte >> 8);
259 *bytePtr++ = (nbyte >> 16);
260 *bytePtr++ = (npads & 0xff);
261 *bytePtr++ = (npads >> 8);
262 outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
264 // Write the buffer to the file
265 bytePtr = (unsigned char *) buffer;
266 outputFile[iDDL]->write((char*)bytePtr,nbyte);
268 ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
275 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
276 Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
280 // Update the data headers and close the output files
281 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
282 header.fSize = UInt_t(outputFile[iDDL]->tellp()) - bHPosition[iDDL];
283 header.SetAttribute(0); // valid data
284 outputFile[iDDL]->seekp(bHPosition[iDDL]);
285 outputFile[iDDL]->write((char*)(&header),sizeof(header));
287 outputFile[iDDL]->close();
288 delete outputFile[iDDL];
293 delete digitsManager;
299 //_____________________________________________________________________________
300 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
303 // Read the raw data digits and put them into the returned digits manager
306 AliTRDdataArrayI *digits = 0;
307 AliTRDdataArrayI *track0 = 0;
308 AliTRDdataArrayI *track1 = 0;
309 AliTRDdataArrayI *track2 = 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,par);
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);
331 if (track0) track0->Compress(1,0);
332 if (track1) track1->Compress(1,0);
333 if (track2) track2->Compress(1,0);
336 Info("Raw2Digits","Subevent header:");
337 Info("Raw2Digits","\tdet = %d",det);
338 Info("Raw2Digits","\tnpads = %d",npads);
341 // Create the data buffer
342 Int_t cham = geo->GetChamber(det);
343 Int_t plan = geo->GetPlane(det);
344 Int_t sect = geo->GetSector(det);
345 Int_t rowMax = par->GetRowMax(plan,cham,sect);
346 Int_t colMax = par->GetColMax(plan);
347 Int_t timeTotal = par->GetTimeTotal();
349 // Add a container for the digits of this detector
350 digits = digitsManager->GetDigits(det);
351 track0 = digitsManager->GetDictionary(det,0);
352 track1 = digitsManager->GetDictionary(det,1);
353 track2 = digitsManager->GetDictionary(det,2);
354 // Allocate memory space for the digits buffer
355 if (digits->GetNtime() == 0) {
356 digits->Allocate(rowMax,colMax,timeTotal);
357 track0->Allocate(rowMax,colMax,timeTotal);
358 track1->Allocate(rowMax,colMax,timeTotal);
359 track2->Allocate(rowMax,colMax,timeTotal);
364 digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
365 input.GetTime(),input.GetSignal());
366 track0->SetDataUnchecked(input.GetRow(),input.GetColumn(),
367 input.GetTime(), -1);
368 track1->SetDataUnchecked(input.GetRow(),input.GetColumn(),
369 input.GetTime(), -1);
370 track2->SetDataUnchecked(input.GetRow(),input.GetColumn(),
371 input.GetTime(), -1);
374 if (digits) digits->Compress(1,0);
375 if (track0) track0->Compress(1,0);
376 if (track1) track1->Compress(1,0);
377 if (track2) track2->Compress(1,0);
382 return digitsManager;