]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawData.cxx
Replace int by Int_t
[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 #include "AliRawReader.h"
34
35 ClassImp(AliTRDrawData)
36
37 //_____________________________________________________________________________
38 AliTRDrawData::AliTRDrawData():TObject()
39 {
40   //
41   // Default constructor
42   //
43
44   fDebug         = 0;
45
46 }
47
48 //_____________________________________________________________________________
49 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r):TObject()
50 {
51   //
52   // AliTRDrawData copy constructor
53   //
54
55   ((AliTRDrawData &) r).Copy(*this);
56
57 }
58
59 //_____________________________________________________________________________
60 AliTRDrawData::~AliTRDrawData()
61 {
62   //
63   // Destructor
64   //
65
66 }
67
68 //_____________________________________________________________________________
69 AliTRDrawData &AliTRDrawData::operator=(const AliTRDrawData &r)
70 {
71   //
72   // Assignment operator
73   //
74
75   if (this != &r) ((AliTRDrawData &) r).Copy(*this);
76   return *this;
77
78 }
79
80 //_____________________________________________________________________________
81 void AliTRDrawData::Copy(TObject &r) const
82 {
83   //
84   // Copy function
85   //
86
87   ((AliTRDrawData &) r).fDebug         = fDebug;
88
89 }
90
91 //_____________________________________________________________________________
92 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree)
93 {
94   //
95   // Convert the digits to raw data byte stream. The output is written
96   // into the the binary files TRD_<DDL number>.ddl.
97   //
98   // The pseudo raw data format is currently defined like this:
99   //
100   //          DDL data header
101   //
102   //          Subevent (= single chamber) header (8 bytes)
103   //                  FLAG
104   //                  Detector number (2 bytes)
105   //                  Number of data bytes (2 bytes)
106   //                  Number of pads with data (2 bytes)
107   //                  1 empty byte
108   //
109   //          Data bank
110   //
111
112   const Int_t kNumberOfDDLs         = 18;
113   const Int_t kSubeventHeaderLength = 8;
114   const Int_t kSubeventDummyFlag    = 0xBB;
115   Int_t       headerSubevent[3];
116
117   ofstream      *outputFile[kNumberOfDDLs];
118   UInt_t         bHPosition[kNumberOfDDLs];
119   Int_t          ntotalbyte[kNumberOfDDLs];
120   Int_t          nbyte = 0;
121   Int_t          npads = 0;
122   unsigned char *bytePtr;
123   unsigned char *headerPtr;
124
125   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
126   digitsManager->SetDebug(fDebug);
127
128   // Read in the digit arrays
129   if (!digitsManager->ReadDigits(digitsTree)) {
130     delete digitsManager;
131     return kFALSE;
132   }
133
134   AliTRDgeometryFull *geo = new AliTRDgeometryFull();
135   AliTRDparameter    *par = new AliTRDparameter("TRDparameter"
136                                                ,"TRD parameter class");
137   AliTRDdataArrayI   *digits;
138
139   // the event header
140   AliRawDataHeader header;
141
142   // Open the output files
143   for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
144     char name[20];
145     sprintf(name, "TRD_%d.ddl", iDDL + AliTRDRawStream::kDDLOffset);
146 #ifndef __DECCXX
147     outputFile[iDDL] = new ofstream(name, ios::binary);
148 #else
149     outputFile[iDDL] = new ofstream(name);
150 #endif
151
152     // Write a dummy data header
153     bHPosition[iDDL] = outputFile[iDDL]->tellp();
154     outputFile[iDDL]->write((char*)(&header),sizeof(header));
155     ntotalbyte[iDDL] = 0;
156   }
157
158   // Loop through all detectors
159   for (Int_t det = 0; det < AliTRDgeometry::Ndet(); det++) {
160
161     Int_t cham      = geo->GetChamber(det);
162     Int_t plan      = geo->GetPlane(det);
163     Int_t sect      = geo->GetSector(det);
164     Int_t rowMax    = par->GetRowMax(plan,cham,sect);
165     Int_t colMax    = par->GetColMax(plan);
166     Int_t timeTotal = par->GetTimeTotal();
167     Int_t bufferMax = rowMax*colMax*timeTotal;
168     Int_t *buffer   = new Int_t[bufferMax];
169
170     npads   = 0;
171     nbyte   = 0;
172     bytePtr = (unsigned char *) buffer;
173
174     Int_t iDDL = sect;
175
176     // Get the digits array
177     digits = digitsManager->GetDigits(det);
178     digits->Expand();
179
180     // Loop through the detector pixel
181     for (Int_t col = 0; col < colMax; col++) {
182       for (Int_t row = 0; row < rowMax; row++) {
183
184         // Check whether data exists for this pad
185         Bool_t dataflag = kFALSE;
186         for (Int_t time = 0; time < timeTotal; time++) {
187           Int_t data = digits->GetDataUnchecked(row,col,time);
188           if (data) {
189             dataflag = kTRUE;
190             break;
191           }
192         }
193
194         if (dataflag) {
195
196           npads++;
197
198           // The pad row number
199           *bytePtr++ = row + 1;
200           // The pad column number
201           *bytePtr++ = col + 1;
202           nbyte += 2;
203
204           Int_t nzero = 0;
205           for (Int_t time = 0; time < timeTotal; time++) {
206
207             Int_t data = digits->GetDataUnchecked(row,col,time);
208
209             if (!data) {
210               nzero++;
211               if ((nzero ==       256) || 
212                   (time  == timeTotal-1)) {
213                 *bytePtr++ = 0;
214                 *bytePtr++ = nzero-1;
215                 nbyte += 2;
216                 nzero  = 0;
217               }
218             }
219             else {
220               if (nzero) {
221                 *bytePtr++ = 0;
222                 *bytePtr++ = nzero-1;
223                 nbyte += 2;
224                 nzero  = 0;
225               }
226               // High byte (MSB always set)
227               *bytePtr++ = ((data >> 8) | 128);
228               // Low byte
229               *bytePtr++ = (data & 0xff);
230               nbyte += 2;
231             }
232
233           }
234
235         }
236
237       }
238
239     }
240
241     // Fill the end of the buffer with zeros
242     while (nbyte % 4) {  
243       *bytePtr++ = 0;
244       nbyte++;
245     }
246
247     if (fDebug > 1) {
248       Info("Digits2Raw","det = %d, nbyte = %d (%d)",det,nbyte,bufferMax);
249     }
250
251     // Write the subevent header
252     bytePtr    = (unsigned char *) headerSubevent;
253     headerPtr  = bytePtr;
254     *bytePtr++ = kSubeventDummyFlag;
255     *bytePtr++ = (det   & 0xff);
256     *bytePtr++ = (det   >> 8);
257     *bytePtr++ = (nbyte & 0xff);
258     *bytePtr++ = (nbyte >> 8);
259     *bytePtr++ = (nbyte >> 16);
260     *bytePtr++ = (npads & 0xff);
261     *bytePtr++ = (npads >> 8);
262     outputFile[iDDL]->write((char*)headerPtr,kSubeventHeaderLength);
263
264     // Write the buffer to the file
265     bytePtr = (unsigned char *) buffer;
266     outputFile[iDDL]->write((char*)bytePtr,nbyte);
267
268     ntotalbyte[iDDL] += nbyte + kSubeventHeaderLength;
269
270     delete buffer;
271
272   }
273
274   if (fDebug) {
275     for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
276       Info("Digits2Raw","Total size: DDL %d = %d",iDDL,ntotalbyte[iDDL]);
277     }
278   }
279
280   // Update the data headers and close the output files
281   for (Int_t iDDL = 0; iDDL < kNumberOfDDLs; iDDL++) {
282     header.fSize = UInt_t(outputFile[iDDL]->tellp()) - bHPosition[iDDL];
283     header.SetAttribute(0);  // valid data
284     outputFile[iDDL]->seekp(bHPosition[iDDL]);
285     outputFile[iDDL]->write((char*)(&header),sizeof(header));
286
287     outputFile[iDDL]->close();
288     delete outputFile[iDDL];
289   }
290
291   delete geo;
292   delete par;
293   delete digitsManager;
294
295   return kTRUE;
296
297 }
298
299 //_____________________________________________________________________________
300 AliTRDdigitsManager* AliTRDrawData::Raw2Digits(AliRawReader* rawReader)
301 {
302   //
303   // Read the raw data digits and put them into the returned digits manager
304   //
305
306   AliTRDdataArrayI *digits    = 0;
307   AliTRDdataArrayI *track0    = 0;
308   AliTRDdataArrayI *track1    = 0;
309   AliTRDdataArrayI *track2    = 0; 
310
311   AliTRDgeometryFull *geo = new AliTRDgeometryFull();
312   AliTRDparameter    *par = new AliTRDparameter("TRDparameter"
313                                                ,"TRD parameter class");
314
315   // Create the digits manager
316   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
317   digitsManager->SetDebug(fDebug);
318   digitsManager->CreateArrays();
319
320   AliTRDRawStream input(rawReader,par);
321
322   // Loop through the digits
323   while (input.Next()) {
324
325     Int_t det    = input.GetDetector();
326     Int_t npads  = input.GetNPads();
327
328     if (input.IsNewDetector()) {
329
330       if (digits) digits->Compress(1,0);
331       if (track0) track0->Compress(1,0);
332       if (track1) track1->Compress(1,0);
333       if (track2) track2->Compress(1,0);
334
335       if (fDebug > 2) {
336         Info("Raw2Digits","Subevent header:");
337         Info("Raw2Digits","\tdet   = %d",det);
338         Info("Raw2Digits","\tnpads = %d",npads);
339       }      
340
341       // Create the data buffer
342       Int_t cham      = geo->GetChamber(det);
343       Int_t plan      = geo->GetPlane(det);
344       Int_t sect      = geo->GetSector(det);
345       Int_t rowMax    = par->GetRowMax(plan,cham,sect);
346       Int_t colMax    = par->GetColMax(plan);
347       Int_t timeTotal = par->GetTimeTotal();
348
349       // Add a container for the digits of this detector
350       digits = digitsManager->GetDigits(det);
351       track0 = digitsManager->GetDictionary(det,0);
352       track1 = digitsManager->GetDictionary(det,1);
353       track2 = digitsManager->GetDictionary(det,2);
354       // Allocate memory space for the digits buffer
355       if (digits->GetNtime() == 0) {
356         digits->Allocate(rowMax,colMax,timeTotal);
357         track0->Allocate(rowMax,colMax,timeTotal);
358         track1->Allocate(rowMax,colMax,timeTotal);
359         track2->Allocate(rowMax,colMax,timeTotal);
360       }
361
362     } 
363
364     digits->SetDataUnchecked(input.GetRow(),input.GetColumn(),
365                              input.GetTime(),input.GetSignal());
366     track0->SetDataUnchecked(input.GetRow(),input.GetColumn(),
367                              input.GetTime(),               -1);
368     track1->SetDataUnchecked(input.GetRow(),input.GetColumn(),
369                              input.GetTime(),               -1);
370     track2->SetDataUnchecked(input.GetRow(),input.GetColumn(),
371                              input.GetTime(),               -1);
372   }
373
374   if (digits) digits->Compress(1,0);
375   if (track0) track0->Compress(1,0);
376   if (track1) track1->Compress(1,0);
377   if (track2) track2->Compress(1,0);
378
379   delete geo;
380   delete par;
381
382   return digitsManager;
383
384 }