]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawData.cxx
Extracting PHOS and EMCAL trackers from the correspondig reconstructors (Yu.Belikov)
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  TRD raw data conversion class                                            //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <Riostream.h>
25
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
34 ClassImp(AliTRDrawData)
35
36 //_____________________________________________________________________________
37 AliTRDrawData::AliTRDrawData():TObject()
38 {
39   //
40   // Default constructor
41   //
42
43   fDebug         = 0;
44
45 }
46
47 //_____________________________________________________________________________
48 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r):TObject()
49 {
50   //
51   // AliTRDrawData copy constructor
52   //
53
54   ((AliTRDrawData &) r).Copy(*this);
55
56 }
57
58 //_____________________________________________________________________________
59 AliTRDrawData::~AliTRDrawData()
60 {
61   //
62   // Destructor
63   //
64
65 }
66
67 //_____________________________________________________________________________
68 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
69 {
70   //
71   // Assignment operator
72   //
73
74   if (this != &r) ((AliTRDrawData &) r).Copy(*this);
75   return *this;
76
77 }
78
79 //_____________________________________________________________________________
80 void AliTRDrawData::Copy(TObject &r)
81 {
82   //
83   // Copy function
84   //
85
86   ((AliTRDrawData &) r).fDebug         = fDebug;
87
88 }
89
90 //_____________________________________________________________________________
91 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
92 {
93   //
94   // Convert the digits to raw data byte stream. The output is written
95   // into the the binary files TRD_<DDL number>.ddl.
96   //
97   // The pseudo raw data format is currently defined like this:
98   //
99   //          DDL data header
100   //
101   //          Subevent (= single chamber) header (8 bytes)
102   //                  FLAG
103   //                  Detector number (2 bytes)
104   //                  Number of data bytes (2 bytes)
105   //                  Number of pads with data (2 bytes)
106   //                  1 empty byte
107   //
108   //          Data bank
109   //
110
111   const Int_t kNumberOfDDLs         = 18;
112   const Int_t kSubeventHeaderLength = 8;
113   const Int_t kSubeventDummyFlag    = 0xBB;
114   int headerSubevent[2];
115
116   ofstream      *outputFile[kNumberOfDDLs];
117   UInt_t         bHPosition[kNumberOfDDLs];
118   Int_t          ntotalbyte[kNumberOfDDLs];
119   Int_t          nbyte = 0;
120   Int_t          npads = 0;
121   unsigned char *bytePtr;
122   unsigned char *headerPtr;
123
124   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
125   digitsManager->SetDebug(fDebug);
126
127   // Read in the digit arrays
128   if (!digitsManager->ReadDigits(digitsTree)) {
129     delete digitsManager;
130     return kFALSE;
131   }
132
133   AliTRDgeometryFull *geo = new AliTRDgeometryFull();
134   AliTRDparameter    *par = new AliTRDparameter("TRDparameter"
135                                                ,"TRD parameter class");
136   AliTRDdataArrayI   *digits;
137
138   // the event header
139   AliRawDataHeader header;
140
141   // Open the output files
142   for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
143     char name[20];
144     sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
145 #ifndef __DECCXX
146     outputFile[iDDL] = new ofstream(name, ios::binary);
147 #else
148     outputFile[iDDL] = new ofstream(name);
149 #endif
150
151     // Write a dummy data header
152     bHPosition[iDDL] = outputFile[iDDL]->tellp();
153     outputFile[iDDL]->write((char*)(&header),sizeof(header));
154     ntotalbyte[iDDL] = 0;
155   }
156
157   // Loop through all detectors
158   for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
159
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];
168
169     npads   = 0;
170     nbyte   = 0;
171     bytePtr = (unsigned char *) buffer;
172
173     Int_t iDDL = sect;
174
175     // Get the digits array
176     digits = digitsManager->GetDigits(det);
177     digits->Expand();
178
179     // Loop through the detector pixel
180     for (Int_t col = 0; col < colMax; col++) {
181       for (Int_t row = 0; row < rowMax; row++) {
182
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);
187           if (data) {
188             dataflag = kTRUE;
189             break;
190           }
191         }
192
193         if (dataflag) {
194
195           npads++;
196
197           // The pad row number
198           *bytePtr++ = row + 1;
199           // The pad column number
200           *bytePtr++ = col + 1;
201           nbyte += 2;
202
203           Int_t nzero = 0;
204           for (Int_t time = 0; time < timeMax; time++) {
205
206             Int_t data = digits->GetDataUnchecked(row,col,time);
207
208             if (!data) {
209               nzero++;
210               if ((nzero ==       256) || 
211                   (time  == timeMax-1)) {
212                 *bytePtr++ = 0;
213                 *bytePtr++ = nzero-1;
214                 nbyte += 2;
215                 nzero  = 0;
216               }
217             }
218             else {
219               if (nzero) {
220                 *bytePtr++ = 0;
221                 *bytePtr++ = nzero-1;
222                 nbyte += 2;
223                 nzero  = 0;
224               }
225               // High byte (MSB always set)
226               *bytePtr++ = ((data >> 8) | 128);
227               // Low byte
228               *bytePtr++ = (data & 0xff);
229               nbyte += 2;
230             }
231
232           }
233
234         }
235
236       }
237
238     }
239
240     // Fill the end of the buffer with zeros
241     while (nbyte % 4) {  
242       *bytePtr++ = 0;
243       nbyte++;
244     }
245
246     if (fDebug > 1) {
247       Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
248     }
249
250     // Write the subevent header
251     bytePtr    = (unsigned char *) headerSubevent;
252     headerPtr  = bytePtr;
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);
260     *bytePtr++ = 0;
261     outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
262
263     // Write the buffer to the file
264     bytePtr = (unsigned char *) buffer;
265     outputFile[iDDL]->write((char*)bytePtr,nbyte);
266
267     ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
268
269     delete buffer;
270
271   }
272
273   if (fDebug) {
274     for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
275       Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
276     }
277   }
278
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));
285
286     outputFile[iDDL]->close();
287     delete outputFile[iDDL];
288   }
289
290   delete geo;
291   delete par;
292   delete digitsManager;
293
294   return kTRUE;
295
296 }
297
298 //_____________________________________________________________________________
299 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
300 {
301   //
302   // Read the raw data digits and put them into the returned digits manager
303   //
304
305   AliTRDdataArrayI *digits    = 0;
306
307   AliTRDgeometryFull *geo = new AliTRDgeometryFull();
308   AliTRDparameter    *par = new AliTRDparameter("TRDparameter"
309                                                ,"TRD parameter class");
310
311   // Create the digits manager
312   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
313   digitsManager->SetDebug(fDebug);
314   digitsManager->CreateArrays();
315
316   AliTRDRawStream input(rawReader);
317
318   // Loop through the digits
319   while (input.Next()) {
320
321     Int_t det    = input.GetDetector();
322     Int_t npads  = input.GetNPads();
323
324     if (input.IsNewDetector()) {
325
326       if (digits) digits->Compress(1,0);
327
328       if (fDebug > 2) {
329         Info("Raw2Digits","Subevent header:");
330         Info("Raw2Digits","\tdet   = %d",det);
331         Info("Raw2Digits","\tnpads = %d",npads);
332       }      
333
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();
341
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);
347       }
348
349     } 
350     digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
351                              input.GetTime(),input.GetSignal());
352   }
353
354   if (digits) digits->Compress(1,0);
355
356   delete geo;
357   delete par;
358
359   return digitsManager;
360
361 }