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 "AliTRDdataArrayI.h"
30 #include "AliTRDRawStream.h"
31 #include "AliRawDataHeader.h"
32 #include "AliRawReader.h"
33 #include "AliTRDCommonParam.h"
34 #include "AliTRDcalibDB.h"
36 ClassImp(AliTRDrawData)
38 //_____________________________________________________________________________
39 AliTRDrawData::AliTRDrawData():TObject()
42 // Default constructor
49 //_____________________________________________________________________________
50 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r):TObject()
53 // AliTRDrawData copy constructor
56 ((AliTRDrawData &) r).Copy(*this);
60 //_____________________________________________________________________________
61 AliTRDrawData::~AliTRDrawData()
69 //_____________________________________________________________________________
70 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
73 // Assignment operator
76 if (this != &r) ((AliTRDrawData &) r).Copy(*this);
81 //_____________________________________________________________________________
82 void AliTRDrawData::Copy(TObject &r) const
88 ((AliTRDrawData &) r).fDebug = fDebug;
92 //_____________________________________________________________________________
93 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
96 // Convert the digits to raw data byte stream. The output is written
97 // into the the binary files TRD_<DDL number>.ddl.
99 // The pseudo raw data format is currently defined like this:
103 // Subevent (= single chamber) header (8 bytes)
105 // Detector number (2 bytes)
106 // Number of data bytes (2 bytes)
107 // Number of pads with data (2 bytes)
113 const Int_t kNumberOfDDLs = 18;
114 const Int_t kSubeventHeaderLength = 8;
115 const Int_t kSubeventDummyFlag = 0xBB;
116 Int_t headerSubevent[3];
118 ofstream *outputFile[kNumberOfDDLs];
119 UInt_t bHPosition[kNumberOfDDLs];
120 Int_t ntotalbyte[kNumberOfDDLs];
123 unsigned char *bytePtr;
124 unsigned char *headerPtr;
126 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
127 digitsManager->SetDebug(fDebug);
129 // Read in the digit arrays
130 if (!digitsManager->ReadDigits(digitsTree)) {
131 delete digitsManager;
135 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
136 AliTRDdataArrayI *digits;
138 AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
141 printf("<AliTRDrawData::Digits2Raw> ");
142 printf("Could not get common params\n");
146 AliTRDcalibDB* calibration = AliTRDcalibDB::Instance();
149 printf("<AliTRDdigitizer::Digits2Raw> ");
150 printf("Could not get calibration object\n");
155 AliRawDataHeader header;
157 // Open the output files
158 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
160 sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
162 outputFile[iDDL] = new ofstream(name, ios::binary);
164 outputFile[iDDL] = new ofstream(name);
167 // Write a dummy data header
168 bHPosition[iDDL] = outputFile[iDDL]->tellp();
169 outputFile[iDDL]->write((char*)(&header),sizeof(header));
170 ntotalbyte[iDDL] = 0;
173 // Loop through all detectors
174 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
176 Int_t cham = geo->GetChamber(det);
177 Int_t plan = geo->GetPlane(det);
178 Int_t sect = geo->GetSector(det);
179 Int_t rowMax = commonParam->GetRowMax(plan,cham,sect);
180 Int_t colMax = commonParam->GetColMax(plan);
181 Int_t timeTotal = calibration->GetNumberOfTimeBins();
182 Int_t bufferMax = rowMax*colMax*timeTotal;
183 Int_t *buffer = new Int_t[bufferMax];
187 bytePtr = (unsigned char *) buffer;
191 // Get the digits array
192 digits = digitsManager->GetDigits(det);
195 // Loop through the detector pixel
196 for (Int_t col = 0; col < colMax; col++) {
197 for (Int_t row = 0; row < rowMax; row++) {
199 // Check whether data exists for this pad
200 Bool_t dataflag = kFALSE;
201 for (Int_t time = 0; time < timeTotal; time++) {
202 Int_t data = digits->GetDataUnchecked(row,col,time);
213 // The pad row number
214 *bytePtr++ = row + 1;
215 // The pad column number
216 *bytePtr++ = col + 1;
220 for (Int_t time = 0; time < timeTotal; time++) {
222 Int_t data = digits->GetDataUnchecked(row,col,time);
226 if ((nzero == 256) ||
227 (time == timeTotal-1)) {
229 *bytePtr++ = nzero-1;
237 *bytePtr++ = nzero-1;
241 // High byte (MSB always set)
242 *bytePtr++ = ((data >> 8) | 128);
244 *bytePtr++ = (data & 0xff);
256 // Fill the end of the buffer with zeros
263 Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
266 // Write the subevent header
267 bytePtr = (unsigned char *) headerSubevent;
269 *bytePtr++ = kSubeventDummyFlag;
270 *bytePtr++ = (det & 0xff);
271 *bytePtr++ = (det >> 8);
272 *bytePtr++ = (nbyte & 0xff);
273 *bytePtr++ = (nbyte >> 8);
274 *bytePtr++ = (nbyte >> 16);
275 *bytePtr++ = (npads & 0xff);
276 *bytePtr++ = (npads >> 8);
277 outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
279 // Write the buffer to the file
280 bytePtr = (unsigned char *) buffer;
281 outputFile[iDDL]->write((char*)bytePtr,nbyte);
283 ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
290 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
291 Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
295 // Update the data headers and close the output files
296 for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
297 header.fSize = UInt_t(outputFile[iDDL]->tellp()) - bHPosition[iDDL];
298 header.SetAttribute(0); // valid data
299 outputFile[iDDL]->seekp(bHPosition[iDDL]);
300 outputFile[iDDL]->write((char*)(&header),sizeof(header));
302 outputFile[iDDL]->close();
303 delete outputFile[iDDL];
307 delete digitsManager;
313 //_____________________________________________________________________________
314 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
317 // Read the raw data digits and put them into the returned digits manager
320 AliTRDdataArrayI *digits = 0;
321 AliTRDdataArrayI *track0 = 0;
322 AliTRDdataArrayI *track1 = 0;
323 AliTRDdataArrayI *track2 = 0;
325 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
327 AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
330 printf("<AliTRDrawData::Raw2Digits> ");
331 printf("Could not get common params\n");
335 AliTRDcalibDB* calibration = AliTRDcalibDB::Instance();
338 printf("<AliTRDdigitizer::Raw2Digits> ");
339 printf("Could not get calibration object\n");
343 // Create the digits manager
344 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
345 digitsManager->SetDebug(fDebug);
346 digitsManager->CreateArrays();
348 AliTRDRawStream input(rawReader);
350 // Loop through the digits
351 while (input.Next()) {
353 Int_t det = input.GetDetector();
354 Int_t npads = input.GetNPads();
356 if (input.IsNewDetector()) {
358 if (digits) digits->Compress(1,0);
359 if (track0) track0->Compress(1,0);
360 if (track1) track1->Compress(1,0);
361 if (track2) track2->Compress(1,0);
364 Info("Raw2Digits","Subevent header:");
365 Info("Raw2Digits","\tdet = %d",det);
366 Info("Raw2Digits","\tnpads = %d",npads);
369 // Create the data buffer
370 Int_t cham = geo->GetChamber(det);
371 Int_t plan = geo->GetPlane(det);
372 Int_t sect = geo->GetSector(det);
373 Int_t rowMax = commonParam->GetRowMax(plan,cham,sect);
374 Int_t colMax = commonParam->GetColMax(plan);
375 Int_t timeTotal = calibration->GetNumberOfTimeBins();
377 // Add a container for the digits of this detector
378 digits = digitsManager->GetDigits(det);
379 track0 = digitsManager->GetDictionary(det,0);
380 track1 = digitsManager->GetDictionary(det,1);
381 track2 = digitsManager->GetDictionary(det,2);
382 // Allocate memory space for the digits buffer
383 if (digits->GetNtime() == 0) {
384 digits->Allocate(rowMax,colMax,timeTotal);
385 track0->Allocate(rowMax,colMax,timeTotal);
386 track1->Allocate(rowMax,colMax,timeTotal);
387 track2->Allocate(rowMax,colMax,timeTotal);
392 digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
393 input.GetTime(),input.GetSignal());
394 track0->SetDataUnchecked(input.GetRow(),input.GetColumn(),
395 input.GetTime(), -1);
396 track1->SetDataUnchecked(input.GetRow(),input.GetColumn(),
397 input.GetTime(), -1);
398 track2->SetDataUnchecked(input.GetRow(),input.GetColumn(),
399 input.GetTime(), -1);
402 if (digits) digits->Compress(1,0);
403 if (track0) track0->Compress(1,0);
404 if (track1) track1->Compress(1,0);
405 if (track2) track2->Compress(1,0);
409 return digitsManager;