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 **************************************************************************/
20 ///////////////////////////////////////////////////////////////////////////////
22 // TRD raw data conversion class //
24 ///////////////////////////////////////////////////////////////////////////////
28 #include "AliTRDrawData.h"
29 #include "AliTRDdigitsManager.h"
30 #include "AliTRDgeometryFull.h"
31 #include "AliTRDparameter.h"
32 #include "AliTRDdataArrayI.h"
33 #include "AliTRDarrayI.h"
35 ClassImp(AliTRDrawData)
37 //_____________________________________________________________________________
38 AliTRDrawData::AliTRDrawData():TObject()
42 fDigitsManager = NULL;
46 //_____________________________________________________________________________
47 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
50 // AliTRDrawData copy constructor
53 ((AliTRDrawData &) r).Copy(*this);
57 //_____________________________________________________________________________
58 AliTRDrawData::~AliTRDrawData()
65 delete fDigitsManager;
66 fDigitsManager = NULL;
71 //_____________________________________________________________________________
72 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
75 // Assignment operator
78 if (this != &r) ((AliTRDrawData &) r).Copy(*this);
83 //_____________________________________________________________________________
84 void AliTRDrawData::Copy(TObject &r)
90 ((AliTRDrawData &) r).fDebug = fDebug;
91 ((AliTRDrawData &) r).fDigitsManager = NULL;
95 //_____________________________________________________________________________
96 Bool_t AliTRDrawData::OpenInput(const Char_t *name)
99 // Opens a ROOT-file with the TRD digits
102 // Create the digits manager
103 if (fDigitsManager) {
104 delete fDigitsManager;
106 fDigitsManager = new AliTRDdigitsManager();
107 fDigitsManager->SetDebug(fDebug);
109 // Open the input file
110 return fDigitsManager->Open(name);
114 //_____________________________________________________________________________
115 Bool_t AliTRDrawData::Digit2Raw(const Char_t *name1, const Char_t *name2)
118 // Convert the digits to raw data byte stream. The output is written
119 // into the the binary files <name1> and <name2>.
121 // The pseudo raw data format is currently defined like this:
123 // LDC header (8 bytes)
126 // Number of detectors with data (not yet implemented)
129 // Subevent (= single chamber) header (8 bytes)
131 // Detector number (2 bytes)
132 // Number of data bytes (2 bytes)
133 // Number of pads with data (2 bytes)
139 const Int_t kLDCHeaderLength = 8;
140 const Int_t kSubeventHeaderLength = 8;
142 const Int_t kLDCDummyFlag = 0xAA;
143 const Int_t kSubeventDummyFlag = 0xBB;
146 int headerSubevent[2];
148 Int_t ntotalbyte[2] = { 0 };
151 unsigned char *byte_p;
152 unsigned char *header_p;
154 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
155 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
156 ,"TRD parameter class");
157 AliTRDdataArrayI *digits;
160 Info("Digit2Raw","Open the LDC output files %s, %s"
163 ofstream *outputFile1 = new ofstream(name1, ios::out | ios::binary);
164 ofstream *outputFile2 = new ofstream(name2, ios::out | ios::binary);
165 ofstream *outputFile;
167 if (!fDigitsManager) {
168 Error("Digit2Raw","No input file open\n");
172 // Read in the digit arrays
173 if (!fDigitsManager->ReadDigits()) {
177 // Count the number of chambers with data
182 Info("Digit2Raw","Write the LDC headers");
185 // Write the LDC header 1
186 byte_p = (unsigned char *) headerLDC;
188 *byte_p++ = kLDCDummyFlag;
190 *byte_p++ = ndetLDC0;
196 outputFile1->write(header_p,kLDCHeaderLength);
197 ntotalbyte[0] += kLDCHeaderLength;
200 Info("Digit2Raw","LDC header 0 = %d, %d",headerLDC[0],headerLDC[1]);
203 // Write the LDC header 1
204 byte_p = (unsigned char *) headerLDC;
206 *byte_p++ = kLDCDummyFlag;
208 *byte_p++ = ndetLDC1;
214 outputFile2->write(header_p,kLDCHeaderLength);
215 ntotalbyte[1] += kLDCHeaderLength;
218 Info("Digit2Raw","LDC header 1 = %d, %d",headerLDC[0],headerLDC[1]);
221 // Loop through all detectors
222 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
224 Int_t cham = geo->GetChamber(det);
225 Int_t plan = geo->GetPlane(det);
226 Int_t sect = geo->GetSector(det);
227 Int_t rowMax = par->GetRowMax(plan,cham,sect);
228 Int_t colMax = par->GetColMax(plan);
229 Int_t timeMax = par->GetTimeMax();
230 Int_t bufferMax = rowMax*colMax*timeMax;
231 int *buffer = new int[bufferMax];
235 byte_p = (unsigned char *) buffer;
237 // Determine the LDC (resp. output file)
240 outputFile = outputFile1;
244 outputFile = outputFile2;
248 // Get the digits array
249 digits = fDigitsManager->GetDigits(det);
252 // Loop through the detector pixel
253 for (Int_t col = 0; col < colMax; col++) {
254 for (Int_t row = 0; row < rowMax; row++) {
256 // Check whether data exists for this pad
257 Bool_t dataflag = kFALSE;
258 for (Int_t time = 0; time < timeMax; time++) {
259 Int_t data = digits->GetDataUnchecked(row,col,time);
270 // The pad row number
272 // The pad column number
277 for (Int_t time = 0; time < timeMax; time++) {
279 Int_t data = digits->GetDataUnchecked(row,col,time);
283 if ((nzero == 256) ||
284 (time == timeMax-1)) {
298 // High byte (MSB always set)
299 *byte_p++ = ((data >> 8) | 128);
301 *byte_p++ = (data & 0xff);
313 // Fill the end of the buffer with zeros
320 Info("Digit2Raw","LDC = %d, det = %d, nbyte = %d (%d)",ldc,det,nbyte,bufferMax);
323 // Write the subevent header
324 byte_p = (unsigned char *) headerSubevent;
326 *byte_p++ = kSubeventDummyFlag;
327 *byte_p++ = (det & 0xff);
328 *byte_p++ = (det >> 8);
329 *byte_p++ = (nbyte & 0xff);
330 *byte_p++ = (nbyte >> 8);
331 *byte_p++ = (npads & 0xff);
332 *byte_p++ = (npads >> 8);
334 outputFile->write(header_p,kSubeventHeaderLength);
336 // Write the buffer to the file
337 byte_p = (unsigned char *) buffer;
338 outputFile->write(byte_p,nbyte);
340 ntotalbyte[ldc] += nbyte + kSubeventHeaderLength;
347 Info("Digit2Raw","Total size: LDC0 = %d, LDC1 = %d",ntotalbyte[0],ntotalbyte[1]);
350 outputFile1->close();
351 outputFile2->close();
362 //_____________________________________________________________________________
363 Bool_t AliTRDrawData::Raw2Digit(const Char_t *name1, const Char_t *name2)
366 const Int_t kLDCHeaderLength = 8;
367 const Int_t kSubeventHeaderLength = 8;
369 const Int_t kNldc = 2;
370 const Char_t *name = 0;
373 int headerSubevent[2];
377 unsigned char *byte_p;
378 ifstream *inputFile = 0;
379 AliTRDdataArrayI *digits = 0;
381 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
382 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
383 ,"TRD parameter class");
385 // Create the digits manager
386 if (fDigitsManager) {
387 delete fDigitsManager;
389 fDigitsManager = new AliTRDdigitsManager();
390 fDigitsManager->SetDebug(fDebug);
391 fDigitsManager->CreateArrays();
393 for (Int_t ldc = 0; ldc < kNldc; ldc++) {
402 Info("Raw2Digit","Open the LDC input file %s",name);
404 inputFile = new ifstream(name, ios::in | ios::binary);
406 // Read the LDC header
407 byte_p = (unsigned char *) headerLDC;
408 inputFile->read(byte_p,kLDCHeaderLength);
411 Info("Raw2Digit","LDC header no. %d:",ldc);
412 Info("Raw2Digit","\tflag = %d",*byte_p++);
413 Info("Raw2Digit","\tldc no = %d",*byte_p++);
414 Info("Raw2Digit","\tndet = %d",*byte_p++);
415 Info("Raw2Digit","\tempty = %d",*byte_p++);
416 Info("Raw2Digit","\tempty = %d",*byte_p++);
417 Info("Raw2Digit","\tempty = %d",*byte_p++);
418 Info("Raw2Digit","\tempty = %d",*byte_p++);
419 Info("Raw2Digit","\tempty = %d",*byte_p++);
422 // Loop through the subevents
423 byte_p = (unsigned char *) headerSubevent;
424 while (inputFile->read(byte_p,kSubeventHeaderLength)) {
426 Int_t flag = *byte_p++;
427 Int_t detl = *byte_p++;
428 Int_t deth = *byte_p++;
429 Int_t det = detl + (deth << 8);
430 Int_t nbytel = *byte_p++;
431 Int_t nbyteh = *byte_p++;
432 nbyte = nbytel + (nbyteh << 8);
433 Int_t npadsl = *byte_p++;
434 Int_t npadsh = *byte_p++;
435 npads = npadsl + (npadsh << 8);
437 Info("Raw2Digit","Subevent header:");
438 Info("Raw2Digit","\tflag = %d",flag);
439 Info("Raw2Digit","\tdet = %d",det);
440 Info("Raw2Digit","\tnbyte = %d",nbyte);
441 Info("Raw2Digit","\tnpads = %d",npads);
442 Info("Raw2Digit","\tempty = %d",*byte_p++);
445 // Create the data buffer
446 Int_t cham = geo->GetChamber(det);
447 Int_t plan = geo->GetPlane(det);
448 Int_t sect = geo->GetSector(det);
449 Int_t rowMax = par->GetRowMax(plan,cham,sect);
450 Int_t colMax = par->GetColMax(plan);
451 Int_t timeMax = par->GetTimeMax();
452 Int_t bufferMax = rowMax*colMax*timeMax;
453 int *buffer = new int[bufferMax];
454 byte_p = (unsigned char *) buffer;
455 memset(buffer,0,bufferMax*sizeof(int));
457 // Add a container for the digits of this detector
458 digits = fDigitsManager->GetDigits(det);
459 // Allocate memory space for the digits buffer
460 if (digits->GetNtime() == 0) {
461 digits->Allocate(rowMax,colMax,timeMax);
465 inputFile->read(byte_p,nbyte);
474 // Decompress the data
477 // The pad row number
478 Int_t row = (*byte_p++) - 1;
479 // The pad column number
480 Int_t col = (*byte_p++) - 1;
485 while ((time < timeMax) &&
493 // signal for given timebim
496 signal = low + (high << 8);
497 if ((row < 0) || (col < 0) || (time < 0) ||
498 (row >= rowMax) || (col >= colMax) || (time >= timeMax)) {
500 ,"row=%d(%d) col=%d(%d) time=%d(%d)"
501 ,row,rowMax,col,colMax,time,timeMax);
504 digits->SetDataUnchecked(row,col,time,signal);
528 digits->Compress(1,0);
532 byte_p = (unsigned char *) headerSubevent;