]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliL3DDLDataFileHandler.cxx
Time performance improvements in HLT hough transform and huffman decompression of...
[u/mrichter/AliRoot.git] / HLT / misc / AliL3DDLDataFileHandler.cxx
1 // @(#) $Id$
2
3 // Author: C. Loizides <loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #include "AliL3StandardIncludes.h"
7
8 #include "AliL3RootTypes.h"
9 #include "AliL3Logging.h"
10 #include "AliL3Transform.h"
11 #include "AliL3MemHandler.h"
12 #include "AliL3DigitData.h"
13 #ifdef use_newio
14 #include "../RAW/AliRawReaderRoot.h"
15 #include "../RAW/AliRawReaderDate.h"
16 #else
17 #include "AliL3DDLTPCRawStream.h"
18 #include "AliL3DDLRawReaderFile.h"
19 #endif
20 #include "AliL3DDLDataFileHandler.h"
21
22 #if __GNUC__ == 3
23 using namespace std;
24 #endif
25
26 /** \class AliL3DDLDataFileHandler 
27 <pre>
28 //_____________________________________________________________
29 // AliL3DDLDataFileHandler
30 //
31 //  This class does converts from the DDL format of offline
32 //  into the memory I/O handling of the HLT binary files.
33 //  
34 //  Examples: see ddl2binary in exa and the general 
35 //            AliL3MemHandler class description
36 //
37 </pre>
38 */
39
40 ClassImp(AliL3DDLDataFileHandler)
41
42 AliL3DDLDataFileHandler::AliL3DDLDataFileHandler()
43 {
44   // default constructor
45   fReader=0;
46   fTPCStream=0;
47 }
48
49 AliL3DDLDataFileHandler::~AliL3DDLDataFileHandler()
50 {
51   // destructor
52   FreeAll();
53 }
54
55 void AliL3DDLDataFileHandler::FreeAll()
56 {
57   // frees all heap memory
58   if(fReader) delete fReader;
59   if(fTPCStream) delete fTPCStream;
60   fReader = 0;
61   fTPCStream = 0;
62 }
63
64
65 #ifdef use_newio
66 Bool_t AliL3DDLDataFileHandler::SetReaderInput(Char_t *name,Int_t event)
67 {
68   // sets the input of the reader
69   fEvent=event;
70   if(fReader) delete fReader;
71   if(fTPCStream) delete fTPCStream;
72   if(event>=0){
73     fFilename=name;
74     fReader=new AliRawReaderRoot(name,event);
75   } else {
76     fFilename="";
77     fReader=new AliRawReaderDate((void *)name);
78   }
79   fTPCStream=new AliTPCRawStream(fReader);
80
81   return kTRUE;
82 }
83 #else
84 Bool_t AliL3DDLDataFileHandler::SetReaderInput(Char_t *name, Bool_t add)
85 {
86   // sets the input of the reader
87   if(fReader){
88     LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::SetReaderInput","File Open")
89       <<"Reader ptr is already in use"<<ENDLOG;
90     return kFALSE;
91   }
92  
93   fReader=new AliL3DDLRawReaderFile(name,add);
94   fTPCStream=new AliL3DDLTPCRawStream(fReader);
95   
96   return kTRUE;
97 }
98 Bool_t AliL3DDLDataFileHandler::SetReaderInput(AliL3DDLRawReaderFile *rf)
99 {
100   // sets the input of the reader
101   if(fReader){
102     LOG(AliL3Log::kError,"AliL3RawDataFileHandler::SetReaderInput","File Open")
103       <<"Reader ptr is already in use, delete it first"<<ENDLOG;
104     return kFALSE;
105   }
106
107   //Open the raw data file with given file.
108   fReader = rf;
109   fTPCStream=new AliL3DDLTPCRawStream(fReader);
110
111   return kTRUE;
112 }
113 #endif
114
115 void AliL3DDLDataFileHandler::CloseReaderInput()
116 {
117   // closes the input of the reader
118   if(!fReader){
119     LOG(AliL3Log::kWarning,"AliL3RawDataFileHandler::CloseReaderInput","File Close")
120       <<"Nothing to Close"<<ENDLOG;
121     return;
122   }
123
124   delete fReader;
125   delete fTPCStream;
126   fReader = 0;
127   fTPCStream = 0;
128 }
129
130 #ifdef use_newio
131 Bool_t AliL3DDLDataFileHandler::IsDigit(Int_t /*i*/) const
132 {
133   // dummy
134   return kTRUE;
135 }
136 #endif
137
138 #ifndef fast_raw
139 AliL3DigitRowData * AliL3DDLDataFileHandler::DDLData2Memory(UInt_t &nrow,Int_t event)
140 {
141   // transfers the DDL data to the memory
142 #ifdef use_newio
143   if((fEvent>=0)&&(event!=fEvent)){
144     fEvent=event;
145     if(fReader) delete fReader;
146     if(fTPCStream) delete fTPCStream;
147     fReader=new AliRawReaderRoot(fFilename,event);
148     fTPCStream=new AliTPCRawStream(fReader);
149   }
150 #endif
151   AliL3DigitRowData *data = 0;
152   nrow=0;
153
154   if(!fReader){
155     LOG(AliL3Log::kWarning,"AliL3DDLDataFileHandler::DDLData2Memory","File")
156     <<"No Input avalible: no object AliL3DDLRawReaderFile"<<ENDLOG;
157     return 0; 
158   }
159   
160   Int_t nrows=fRowMax-fRowMin+1;
161   Int_t ndigitcount=0;
162   Int_t * ndigits = new Int_t[nrows];
163   UShort_t ***charges=new UShort_t**[nrows];
164   for(Int_t r=fRowMin;r<=fRowMax;r++){
165     Int_t lrow=r-fRowMin;
166     charges[lrow]=new UShort_t*[AliL3Transform::GetNPads(r)];
167     for(Int_t k=0;k<AliL3Transform::GetNPads(r);k++){
168       charges[lrow][k]=new UShort_t[AliL3Transform::GetNTimeBins()];
169       for(Int_t j=0;j<AliL3Transform::GetNTimeBins();j++) charges[lrow][k][j]=0;
170     }
171   }
172
173   Int_t ddlsToSearch=0;
174   Int_t ddls[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
175   Int_t ddlid=-1,lddlid=-1;
176   for(Int_t r=fRowMin;r<=fRowMax;r++){
177     ndigits[r-fRowMin] = 0; //now digits on row
178
179     Int_t patch=AliL3Transform::GetPatch(r);
180     Int_t sector,row;
181     AliL3Transform::Slice2Sector(fSlice,r,sector,row);
182
183     if(sector<36) //taken from AliTPCBuffer160.cxx
184       ddlid=sector*2+patch;
185     else
186       ddlid=70+(sector-36)*4+patch;
187
188     if((lddlid!=ddlid-1)&&(r==30)){ //dont forget the split row on the last ddl
189       ddls[ddlsToSearch++]=ddlid-1;     
190       lddlid=ddlid-1;
191     }
192
193     if((lddlid==-1)||(ddlid!=lddlid)){
194       ddls[ddlsToSearch++]=ddlid;
195       lddlid=ddlid;
196     }
197     if((r==90)||(r==139)){ //dont forget the split row on the next ddl
198       ddls[ddlsToSearch++]=ddlid+1;     
199       lddlid=ddlid+1;
200     }
201   }
202
203   //  for(Int_t i=0;i<ddlsToSearch;i++) cout << ddls[i] <<endl;
204
205   if(ddls[0]>ddls[ddlsToSearch-1]) {
206     Int_t tempddl = ddls[0];
207     ddls[0] = ddls[ddlsToSearch-1];
208     ddls[ddlsToSearch-1] = tempddl;
209   }
210 #ifdef use_newio
211     fReader->Reset();
212     fReader->Select(0,ddls[0],ddls[ddlsToSearch-1]);
213     fTPCStream->Reset();
214 #else
215     fTPCStream->SetDDLID(ddls[i]); //ddl to read out
216 #endif
217     Int_t zerosup = AliL3Transform::GetZeroSup();
218     Int_t adcsat = AliL3Transform::GetADCSat();
219     Int_t slice,srow;
220     Int_t lrow;
221
222     while (fTPCStream->Next()){
223
224       if(fTPCStream->IsNewSector() || fTPCStream->IsNewRow()) {
225         Int_t sector=fTPCStream->GetSector();
226         Int_t row=fTPCStream->GetRow();
227         AliL3Transform::Sector2Slice(slice,srow,sector,row);
228         if(slice!=fSlice){
229           LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Slice")
230             <<AliL3Log::kDec<<"Found slice "<<slice<<", expected "<<fSlice<<ENDLOG;
231           continue;
232         }
233         lrow=srow-fRowMin;
234       }
235
236       //test row criteria (patch boundaries)
237       if((srow<fRowMin)||(srow>fRowMax))continue;
238
239       Int_t pad=fTPCStream->GetPad();
240       if(fTPCStream->IsNewPad()) {
241         if((pad<0)||(pad>=AliL3Transform::GetNPads(srow))){
242           LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Pad")
243             <<AliL3Log::kDec<<"Pad value out of bounds "<<pad<<" "
244             <<AliL3Transform::GetNPads(srow)<<ENDLOG;
245           continue;
246         }
247       }
248
249       Int_t time=fTPCStream->GetTime();
250       if((time<0)||(time>=AliL3Transform::GetNTimeBins())){
251         LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Time")
252           <<AliL3Log::kDec<<"Time out of bounds "<<time<<" "
253           <<AliL3Transform::GetNTimeBins()<<ENDLOG;
254         continue;
255       }
256
257       //store digit
258       UShort_t dig=fTPCStream->GetSignal();
259       if(dig <= zerosup) continue;
260       if(dig >= adcsat) dig = adcsat;
261
262       ndigits[lrow]++; //for this row only
263       ndigitcount++;   //total number of digits to be published
264
265       charges[lrow][pad][time]=dig;
266     }
267
268   Int_t size = sizeof(AliL3DigitData)*ndigitcount
269     + nrows*sizeof(AliL3DigitRowData);
270
271   LOG(AliL3Log::kDebug,"AliL3DDLDataFileHandler::DDLDigits2Memory","Digits")
272     <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
273   
274   data=(AliL3DigitRowData*) Allocate(size);
275   nrow = (UInt_t)nrows;
276   AliL3DigitRowData *tempPt = data;
277
278   for(Int_t r=fRowMin;r<=fRowMax;r++){
279     Int_t lrow=r-fRowMin;
280     tempPt->fRow = r;
281     tempPt->fNDigit = ndigits[lrow];
282   
283     Int_t localcount=0;
284     for(Int_t pad=0;pad<AliL3Transform::GetNPads(r);pad++){
285       for(Int_t time=0;time<AliL3Transform::GetNTimeBins();time++){
286         UShort_t dig=charges[lrow][pad][time];
287         if(!dig) continue;
288
289         if(localcount >= ndigits[lrow])
290           LOG(AliL3Log::kFatal,"AliL3DDLDataFileHandler::DDLDigits2Binary","Memory")
291             <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
292             <<ndigits[lrow]<<ENDLOG;
293         
294
295         tempPt->fDigitData[localcount].fCharge=dig;
296         tempPt->fDigitData[localcount].fPad=pad;
297         tempPt->fDigitData[localcount].fTime=time;
298 #ifdef do_mc
299         tempPt->fDigitData[localcount].fTrackID[0] = 0;
300         tempPt->fDigitData[localcount].fTrackID[1] = 0;
301         tempPt->fDigitData[localcount].fTrackID[2] = 0;
302 #endif
303         localcount++;
304       }
305     }
306
307     if(localcount != ndigits[lrow])
308       LOG(AliL3Log::kFatal,"AliL3DDLDataFileHandler::DDLDigits2Binary","Memory")
309         <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
310         <<ndigits[lrow]<<ENDLOG;
311
312
313     Byte_t *tmp = (Byte_t*)tempPt;
314     Int_t size = sizeof(AliL3DigitRowData)
315                                       + ndigits[lrow]*sizeof(AliL3DigitData);
316     tmp += size;
317     tempPt = (AliL3DigitRowData*)tmp;
318   }
319
320   //delete charge array
321   for(Int_t r=fRowMin;r<=fRowMax;r++){
322     Int_t lrow=r-fRowMin;
323     for(Int_t k=0;k<AliL3Transform::GetNPads(r);k++)
324         delete charges[lrow][k];
325     delete charges[lrow];
326   }
327   delete charges;
328   delete [] ndigits;
329
330   return data;
331 }
332 #else
333 AliL3DigitRowData * AliL3DDLDataFileHandler::DDLData2Memory(UInt_t &nrow,Int_t event)
334 {
335   // transfers the DDL data to the memory
336 #ifdef use_newio
337   if((fEvent>=0)&&(event!=fEvent)){
338     fEvent=event;
339     if(fReader) delete fReader;
340     if(fTPCStream) delete fTPCStream;
341     fReader=new AliRawReaderRoot(fFilename,event);
342     fTPCStream=new AliTPCRawStream(fReader);
343   }
344 #endif
345   AliL3DigitRowData *data = 0;
346   nrow=0;
347
348   if(!fReader){
349     LOG(AliL3Log::kWarning,"AliL3DDLDataFileHandler::DDLData2Memory","File")
350     <<"No Input avalible: no object AliL3DDLRawReaderFile"<<ENDLOG;
351     return 0; 
352   }
353   
354   Int_t nrows=fRowMax-fRowMin+1;
355
356   Int_t ddlsToSearch=0;
357   Int_t ddls[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
358   Int_t ddlid=-1,lddlid=-1;
359   for(Int_t r=fRowMin;r<=fRowMax;r++){
360
361     Int_t patch=AliL3Transform::GetPatch(r);
362     Int_t sector,row;
363     AliL3Transform::Slice2Sector(fSlice,r,sector,row);
364
365     if(sector<36) //taken from AliTPCBuffer160.cxx
366       ddlid=sector*2+patch;
367     else
368       ddlid=70+(sector-36)*4+patch;
369
370     if((lddlid==-1)||(ddlid!=lddlid)){
371       ddls[ddlsToSearch++]=ddlid;
372       lddlid=ddlid;
373     }
374   }
375
376   //  for(Int_t i=0;i<ddlsToSearch;i++) cout << ddls[i] <<endl;
377
378 #ifdef use_newio
379   fReader->Reset();
380   fReader->Select(0,ddls[0],ddls[ddlsToSearch-1]);
381   fTPCStream->Reset();
382 #else
383   fTPCStream->SetDDLID(ddls[i]); //ddl to read out
384 #endif
385
386   nrow = (UInt_t)nrows;
387
388   return data;
389 }
390 #endif
391
392 Bool_t AliL3DDLDataFileHandler::DDLData2CompBinary(Int_t event)
393 {
394   // transfers the DDL data to the memory and converts it 
395   // to comp binary 
396   Bool_t out = kTRUE;
397   UInt_t ndigits=0;
398   AliL3DigitRowData *digits=0;
399   digits = DDLData2Memory(ndigits,event);
400   out = Memory2CompBinary(ndigits,digits);
401   Free();
402   return out;
403 }