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 "AliTRDarrayI.h"
33 ClassImp(AliTRDrawData)
35 //_____________________________________________________________________________
36 AliTRDrawData::AliTRDrawData():TObject()
40 fDigitsManager = NULL;
44 //_____________________________________________________________________________
45 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
48 // AliTRDrawData copy constructor
51 ((AliTRDrawData &) r).Copy(*this);
55 //_____________________________________________________________________________
56 AliTRDrawData::~AliTRDrawData()
63 delete fDigitsManager;
64 fDigitsManager = NULL;
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)
88 ((AliTRDrawData &) r).fDebug = fDebug;
89 ((AliTRDrawData &) r).fDigitsManager = NULL;
93 //_____________________________________________________________________________
94 Bool_t AliTRDrawData::OpenInput(const Char_t *name)
97 // Opens a ROOT-file with the TRD digits
100 // Create the digits manager
101 if (fDigitsManager) {
102 delete fDigitsManager;
104 fDigitsManager = new AliTRDdigitsManager();
105 fDigitsManager->SetDebug(fDebug);
107 // Open the input file
108 return fDigitsManager->Open(name);
112 //_____________________________________________________________________________
113 Bool_t AliTRDrawData::Digit2Raw(const Char_t *name1, const Char_t *name2)
116 // Convert the digits to raw data byte stream. The output is written
117 // into the the binary files <name1> and <name2>.
119 // The pseudo raw data format is currently defined like this:
121 // LDC header (8 bytes)
124 // Number of detectors with data (not yet implemented)
127 // Subevent (= single chamber) header (8 bytes)
129 // Detector number (2 bytes)
130 // Number of data bytes (2 bytes)
131 // Number of pads with data (2 bytes)
137 const Int_t kLDCHeaderLength = 8;
138 const Int_t kSubeventHeaderLength = 8;
140 const Int_t kLDCDummyFlag = 0xAA;
141 const Int_t kSubeventDummyFlag = 0xBB;
144 int headerSubevent[2];
146 Int_t ntotalbyte[2] = { 0 };
149 unsigned char *byte_p;
150 unsigned char *header_p;
152 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
153 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
154 ,"TRD parameter class");
155 AliTRDdataArrayI *digits;
158 Info("Digit2Raw","Open the LDC output files %s, %s"
161 ofstream *outputFile1 = new ofstream(name1, ios::out | ios::binary);
162 ofstream *outputFile2 = new ofstream(name2, ios::out | ios::binary);
163 ofstream *outputFile;
165 if (!fDigitsManager) {
166 Error("Digit2Raw","No input file open\n");
170 // Read in the digit arrays
171 if (!fDigitsManager->ReadDigits()) {
175 // Count the number of chambers with data
180 Info("Digit2Raw","Write the LDC headers");
183 // Write the LDC header 1
184 byte_p = (unsigned char *) headerLDC;
186 *byte_p++ = kLDCDummyFlag;
188 *byte_p++ = ndetLDC0;
194 outputFile1->write(header_p,kLDCHeaderLength);
195 ntotalbyte[0] += kLDCHeaderLength;
198 Info("Digit2Raw","LDC header 0 = %d, %d",headerLDC[0],headerLDC[1]);
201 // Write the LDC header 1
202 byte_p = (unsigned char *) headerLDC;
204 *byte_p++ = kLDCDummyFlag;
206 *byte_p++ = ndetLDC1;
212 outputFile2->write(header_p,kLDCHeaderLength);
213 ntotalbyte[1] += kLDCHeaderLength;
216 Info("Digit2Raw","LDC header 1 = %d, %d",headerLDC[0],headerLDC[1]);
219 // Loop through all detectors
220 for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
222 Int_t cham = geo->GetChamber(det);
223 Int_t plan = geo->GetPlane(det);
224 Int_t sect = geo->GetSector(det);
225 Int_t rowMax = par->GetRowMax(plan,cham,sect);
226 Int_t colMax = par->GetColMax(plan);
227 Int_t timeMax = par->GetTimeMax();
228 Int_t bufferMax = rowMax*colMax*timeMax;
229 int *buffer = new int[bufferMax];
233 byte_p = (unsigned char *) buffer;
235 // Determine the LDC (resp. output file)
238 outputFile = outputFile1;
242 outputFile = outputFile2;
246 // Get the digits array
247 digits = fDigitsManager->GetDigits(det);
250 // Loop through the detector pixel
251 for (Int_t col = 0; col < colMax; col++) {
252 for (Int_t row = 0; row < rowMax; row++) {
254 // Check whether data exists for this pad
255 Bool_t dataflag = kFALSE;
256 for (Int_t time = 0; time < timeMax; time++) {
257 Int_t data = digits->GetDataUnchecked(row,col,time);
268 // The pad row number
270 // The pad column number
275 for (Int_t time = 0; time < timeMax; time++) {
277 Int_t data = digits->GetDataUnchecked(row,col,time);
281 if ((nzero == 256) ||
282 (time == timeMax-1)) {
296 // High byte (MSB always set)
297 *byte_p++ = ((data >> 8) | 128);
299 *byte_p++ = (data & 0xff);
311 // Fill the end of the buffer with zeros
318 Info("Digit2Raw","LDC = %d, det = %d, nbyte = %d (%d)",ldc,det,nbyte,bufferMax);
321 // Write the subevent header
322 byte_p = (unsigned char *) headerSubevent;
324 *byte_p++ = kSubeventDummyFlag;
325 *byte_p++ = (det & 0xff);
326 *byte_p++ = (det >> 8);
327 *byte_p++ = (nbyte & 0xff);
328 *byte_p++ = (nbyte >> 8);
329 *byte_p++ = (npads & 0xff);
330 *byte_p++ = (npads >> 8);
332 outputFile->write(header_p,kSubeventHeaderLength);
334 // Write the buffer to the file
335 byte_p = (unsigned char *) buffer;
336 outputFile->write(byte_p,nbyte);
338 ntotalbyte[ldc] += nbyte + kSubeventHeaderLength;
345 Info("Digit2Raw","Total size: LDC0 = %d, LDC1 = %d",ntotalbyte[0],ntotalbyte[1]);
348 outputFile1->close();
349 outputFile2->close();
360 //_____________________________________________________________________________
361 Bool_t AliTRDrawData::Raw2Digit(const Char_t *name1, const Char_t *name2)
364 const Int_t kLDCHeaderLength = 8;
365 const Int_t kSubeventHeaderLength = 8;
367 const Int_t kNldc = 2;
368 const Char_t *name = 0;
371 int headerSubevent[2];
375 unsigned char *byte_p;
376 ifstream *inputFile = 0;
377 AliTRDdataArrayI *digits = 0;
379 AliTRDgeometryFull *geo = new AliTRDgeometryFull();
380 AliTRDparameter *par = new AliTRDparameter("TRDparameter"
381 ,"TRD parameter class");
383 // Create the digits manager
384 if (fDigitsManager) {
385 delete fDigitsManager;
387 fDigitsManager = new AliTRDdigitsManager();
388 fDigitsManager->SetDebug(fDebug);
389 fDigitsManager->CreateArrays();
391 for (Int_t ldc = 0; ldc < kNldc; ldc++) {
400 Info("Raw2Digit","Open the LDC input file %s",name);
402 inputFile = new ifstream(name, ios::in | ios::binary);
404 // Read the LDC header
405 byte_p = (unsigned char *) headerLDC;
406 inputFile->read(byte_p,kLDCHeaderLength);
409 Info("Raw2Digit","LDC header no. %d:",ldc);
410 Info("Raw2Digit","\tflag = %d",*byte_p++);
411 Info("Raw2Digit","\tldc no = %d",*byte_p++);
412 Info("Raw2Digit","\tndet = %d",*byte_p++);
413 Info("Raw2Digit","\tempty = %d",*byte_p++);
414 Info("Raw2Digit","\tempty = %d",*byte_p++);
415 Info("Raw2Digit","\tempty = %d",*byte_p++);
416 Info("Raw2Digit","\tempty = %d",*byte_p++);
417 Info("Raw2Digit","\tempty = %d",*byte_p++);
420 // Loop through the subevents
421 byte_p = (unsigned char *) headerSubevent;
422 while (inputFile->read(byte_p,kSubeventHeaderLength)) {
424 Int_t flag = *byte_p++;
425 Int_t detl = *byte_p++;
426 Int_t deth = *byte_p++;
427 Int_t det = detl + (deth << 8);
428 Int_t nbytel = *byte_p++;
429 Int_t nbyteh = *byte_p++;
430 nbyte = nbytel + (nbyteh << 8);
431 Int_t npadsl = *byte_p++;
432 Int_t npadsh = *byte_p++;
433 npads = npadsl + (npadsh << 8);
435 Info("Raw2Digit","Subevent header:");
436 Info("Raw2Digit","\tflag = %d",flag);
437 Info("Raw2Digit","\tdet = %d",det);
438 Info("Raw2Digit","\tnbyte = %d",nbyte);
439 Info("Raw2Digit","\tnpads = %d",npads);
440 Info("Raw2Digit","\tempty = %d",*byte_p++);
443 // Create the data buffer
444 Int_t cham = geo->GetChamber(det);
445 Int_t plan = geo->GetPlane(det);
446 Int_t sect = geo->GetSector(det);
447 Int_t rowMax = par->GetRowMax(plan,cham,sect);
448 Int_t colMax = par->GetColMax(plan);
449 Int_t timeMax = par->GetTimeMax();
450 Int_t bufferMax = rowMax*colMax*timeMax;
451 int *buffer = new int[bufferMax];
452 byte_p = (unsigned char *) buffer;
453 memset(buffer,0,bufferMax*sizeof(int));
455 // Add a container for the digits of this detector
456 digits = fDigitsManager->GetDigits(det);
457 // Allocate memory space for the digits buffer
458 if (digits->GetNtime() == 0) {
459 digits->Allocate(rowMax,colMax,timeMax);
463 inputFile->read(byte_p,nbyte);
472 // Decompress the data
475 // The pad row number
476 Int_t row = (*byte_p++) - 1;
477 // The pad column number
478 Int_t col = (*byte_p++) - 1;
483 while ((time < timeMax) &&
491 // signal for given timebim
494 signal = low + (high << 8);
495 if ((row < 0) || (col < 0) || (time < 0) ||
496 (row >= rowMax) || (col >= colMax) || (time >= timeMax)) {
498 ,"row=%d(%d) col=%d(%d) time=%d(%d)"
499 ,row,rowMax,col,colMax,time,timeMax);
502 digits->SetDataUnchecked(row,col,time,signal);
526 digits->Compress(1,0);
530 byte_p = (unsigned char *) headerSubevent;