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) const
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;
141 // Open the output files
142 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
144 sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
146 outputFile[iDDL] = new ofstream(name, ios::binary);
148 outputFile[iDDL] = new ofstream(name);
151 // Write a dummy data header
152 bHPosition[iDDL] = outputFile[iDDL]->tellp();
153 outputFile[iDDL]->write((char*)(&header),sizeof(header));
154 ntotalbyte[iDDL] = 0;
157 // Loop through all detectors
158 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
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];
171 bytePtr = (unsigned char *) buffer;
175 // Get the digits array
176 digits = digitsManager->GetDigits(det);
179 // Loop through the detector pixel
180 for (Int_t col = 0; col < colMax; col++) {
181 for (Int_t row = 0; row < rowMax; row++) {
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);
197 // The pad row number
198 *bytePtr++ = row + 1;
199 // The pad column number
200 *bytePtr++ = col + 1;
204 for (Int_t time = 0; time < timeMax; time++) {
206 Int_t data = digits->GetDataUnchecked(row,col,time);
210 if ((nzero == 256) ||
211 (time == timeMax-1)) {
213 *bytePtr++ = nzero-1;
221 *bytePtr++ = nzero-1;
225 // High byte (MSB always set)
226 *bytePtr++ = ((data >> 8) | 128);
228 *bytePtr++ = (data & 0xff);
240 // Fill the end of the buffer with zeros
247 Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
250 // Write the subevent header
251 bytePtr = (unsigned char *) headerSubevent;
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);
261 outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
263 // Write the buffer to the file
264 bytePtr = (unsigned char *) buffer;
265 outputFile[iDDL]->write((char*)bytePtr,nbyte);
267 ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
274 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
275 Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
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));
286 outputFile[iDDL]->close();
287 delete outputFile[iDDL];
292 delete digitsManager;
298 //_____________________________________________________________________________
299 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
302 // Read the raw data digits and put them into the returned digits manager
305 AliTRDdataArrayI *digits = 0;
307 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
308 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
309 ,"TRD parameter class");
311 // Create the digits manager
312 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
313 digitsManager->SetDebug(fDebug);
314 digitsManager->CreateArrays();
316 AliTRDRawStream input(rawReader);
318 // Loop through the digits
319 while (input.Next()) {
321 Int_t det = input.GetDetector();
322 Int_t npads = input.GetNPads();
324 if (input.IsNewDetector()) {
326 if (digits) digits->Compress(1,0);
329 Info("Raw2Digits","Subevent header:");
330 Info("Raw2Digits","\tdet = %d",det);
331 Info("Raw2Digits","\tnpads = %d",npads);
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();
342 // Add a container for the digits of this detector
343 digits = digitsManager->GetDigits(det);
344 // Allocate memory space for the digits buffer
345 if (digits->GetNtime() == 0) {
346 digits->Allocate(rowMax,colMax,timeMax);
350 digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
351 input.GetTime(),input.GetSignal());
354 if (digits) digits->Compress(1,0);
359 return digitsManager;