Introduction of the online monitoring code into the alimdc package. Fixed some memory...
[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   fReader = 0;
60   if(fTPCStream) delete fTPCStream;
61   fTPCStream = 0;
62 }
63
64
65 #ifdef use_newio
66 Bool_t AliL3DDLDataFileHandler::SetReaderInput(AliRawEvent *rawevent)
67 {
68   // sets the input of the reader
69   fEvent=-1;
70   fFilename="";
71   if(fReader) delete fReader;
72   fReader=new AliRawReaderRoot(rawevent);
73   if(fTPCStream) delete fTPCStream;
74   fTPCStream=new AliTPCRawStream(fReader);
75
76   return kTRUE;
77 }
78
79 Bool_t AliL3DDLDataFileHandler::SetReaderInput(Char_t *name,Int_t event)
80 {
81   // sets the input of the reader
82   fEvent=event;
83   if(fReader) delete fReader;
84   if(fTPCStream) delete fTPCStream;
85   if(event>=0){
86     fFilename=name;
87     fReader=new AliRawReaderRoot(name,event);
88   } else {
89     fFilename="";
90     fReader=new AliRawReaderDate((void *)name);
91   }
92   fTPCStream=new AliTPCRawStream(fReader);
93
94   return kTRUE;
95 }
96 #else
97 Bool_t AliL3DDLDataFileHandler::SetReaderInput(Char_t *name, Bool_t add)
98 {
99   // sets the input of the reader
100   if(fReader){
101     LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::SetReaderInput","File Open")
102       <<"Reader ptr is already in use"<<ENDLOG;
103     return kFALSE;
104   }
105  
106   fReader=new AliL3DDLRawReaderFile(name,add);
107   fTPCStream=new AliL3DDLTPCRawStream(fReader);
108   
109   return kTRUE;
110 }
111 Bool_t AliL3DDLDataFileHandler::SetReaderInput(AliL3DDLRawReaderFile *rf)
112 {
113   // sets the input of the reader
114   if(fReader){
115     LOG(AliL3Log::kError,"AliL3RawDataFileHandler::SetReaderInput","File Open")
116       <<"Reader ptr is already in use, delete it first"<<ENDLOG;
117     return kFALSE;
118   }
119
120   //Open the raw data file with given file.
121   fReader = rf;
122   fTPCStream=new AliL3DDLTPCRawStream(fReader);
123
124   return kTRUE;
125 }
126 #endif
127
128 void AliL3DDLDataFileHandler::CloseReaderInput()
129 {
130   // closes the input of the reader
131   if(!fReader){
132     LOG(AliL3Log::kWarning,"AliL3RawDataFileHandler::CloseReaderInput","File Close")
133       <<"Nothing to Close"<<ENDLOG;
134     return;
135   }
136
137   delete fReader;
138   delete fTPCStream;
139   fReader = 0;
140   fTPCStream = 0;
141 }
142
143 #ifdef use_newio
144 Bool_t AliL3DDLDataFileHandler::IsDigit(Int_t /*i*/) const
145 {
146   // dummy
147   return kTRUE;
148 }
149 #endif
150
151 #ifndef fast_raw
152 AliL3DigitRowData * AliL3DDLDataFileHandler::DDLData2Memory(UInt_t &nrow,Int_t event)
153 {
154   // transfers the DDL data to the memory
155 #ifdef use_newio
156   if((fEvent>=0)&&(event!=fEvent)){
157     fEvent=event;
158     if(fReader) delete fReader;
159     if(fTPCStream) delete fTPCStream;
160     fReader=new AliRawReaderRoot(fFilename,event);
161     fTPCStream=new AliTPCRawStream(fReader);
162   }
163 #endif
164   AliL3DigitRowData *data = 0;
165   nrow=0;
166
167   if(!fReader){
168     LOG(AliL3Log::kWarning,"AliL3DDLDataFileHandler::DDLData2Memory","File")
169     <<"No Input avalible: no object AliL3DDLRawReaderFile"<<ENDLOG;
170     return 0; 
171   }
172   
173   Int_t nrows=fRowMax-fRowMin+1;
174   Int_t ndigitcount=0;
175   Int_t * ndigits = new Int_t[nrows];
176   UShort_t ***charges=new UShort_t**[nrows];
177   for(Int_t r=fRowMin;r<=fRowMax;r++){
178     Int_t lrow=r-fRowMin;
179     charges[lrow]=new UShort_t*[AliL3Transform::GetNPads(r)];
180     for(Int_t k=0;k<AliL3Transform::GetNPads(r);k++){
181       charges[lrow][k]=new UShort_t[AliL3Transform::GetNTimeBins()];
182       for(Int_t j=0;j<AliL3Transform::GetNTimeBins();j++) charges[lrow][k][j]=0;
183     }
184   }
185
186   Int_t ddlsToSearch=0;
187   Int_t ddls[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
188   Int_t ddlid=-1,lddlid=-1;
189   for(Int_t r=fRowMin;r<=fRowMax;r++){
190     ndigits[r-fRowMin] = 0; //now digits on row
191
192     Int_t patch=AliL3Transform::GetPatch(r);
193     Int_t sector,row;
194     AliL3Transform::Slice2Sector(fSlice,r,sector,row);
195
196     if(sector<36) //taken from AliTPCBuffer160.cxx
197       ddlid=sector*2+patch;
198     else
199       ddlid=70+(sector-36)*4+patch;
200
201     if((lddlid!=ddlid-1)&&(r==30)){ //dont forget the split row on the last ddl
202       ddls[ddlsToSearch++]=ddlid-1;     
203       lddlid=ddlid-1;
204     }
205
206     if((lddlid==-1)||(ddlid!=lddlid)){
207       ddls[ddlsToSearch++]=ddlid;
208       lddlid=ddlid;
209     }
210     if((r==90)||(r==139)){ //dont forget the split row on the next ddl
211       ddls[ddlsToSearch++]=ddlid+1;     
212       lddlid=ddlid+1;
213     }
214   }
215
216   //  for(Int_t i=0;i<ddlsToSearch;i++) cout << ddls[i] <<endl;
217
218   if(ddls[0]>ddls[ddlsToSearch-1]) {
219     Int_t tempddl = ddls[0];
220     ddls[0] = ddls[ddlsToSearch-1];
221     ddls[ddlsToSearch-1] = tempddl;
222   }
223 #ifdef use_newio
224     fReader->Reset();
225     fReader->Select(0,ddls[0],ddls[ddlsToSearch-1]);
226     fTPCStream->Reset();
227 #else
228     fTPCStream->SetDDLID(ddls[i]); //ddl to read out
229 #endif
230     Int_t zerosup = AliL3Transform::GetZeroSup();
231     Int_t adcsat = AliL3Transform::GetADCSat();
232     Int_t slice,srow;
233     Int_t lrow;
234
235     while (fTPCStream->Next()){
236
237       if(fTPCStream->IsNewSector() || fTPCStream->IsNewRow()) {
238         Int_t sector=fTPCStream->GetSector();
239         Int_t row=fTPCStream->GetRow();
240         AliL3Transform::Sector2Slice(slice,srow,sector,row);
241         if(slice!=fSlice){
242           LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Slice")
243             <<AliL3Log::kDec<<"Found slice "<<slice<<", expected "<<fSlice<<ENDLOG;
244           continue;
245         }
246         lrow=srow-fRowMin;
247       }
248
249       //test row criteria (patch boundaries)
250       if((srow<fRowMin)||(srow>fRowMax))continue;
251
252       Int_t pad=fTPCStream->GetPad();
253       if(fTPCStream->IsNewPad()) {
254         if((pad<0)||(pad>=AliL3Transform::GetNPads(srow))){
255           LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Pad")
256             <<AliL3Log::kDec<<"Pad value out of bounds "<<pad<<" "
257             <<AliL3Transform::GetNPads(srow)<<ENDLOG;
258           continue;
259         }
260       }
261
262       Int_t time=fTPCStream->GetTime();
263       if((time<0)||(time>=AliL3Transform::GetNTimeBins())){
264         LOG(AliL3Log::kError,"AliL3DDLDataFileHandler::DDLDigits2Memory","Time")
265           <<AliL3Log::kDec<<"Time out of bounds "<<time<<" "
266           <<AliL3Transform::GetNTimeBins()<<ENDLOG;
267         continue;
268       }
269
270       //store digit
271       UShort_t dig=fTPCStream->GetSignal();
272       if(dig <= zerosup) continue;
273       if(dig >= adcsat) dig = adcsat;
274
275       ndigits[lrow]++; //for this row only
276       ndigitcount++;   //total number of digits to be published
277
278       charges[lrow][pad][time]=dig;
279     }
280
281   Int_t size = sizeof(AliL3DigitData)*ndigitcount
282     + nrows*sizeof(AliL3DigitRowData);
283
284   LOG(AliL3Log::kDebug,"AliL3DDLDataFileHandler::DDLDigits2Memory","Digits")
285     <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
286   
287   data=(AliL3DigitRowData*) Allocate(size);
288   nrow = (UInt_t)nrows;
289   AliL3DigitRowData *tempPt = data;
290
291   for(Int_t r=fRowMin;r<=fRowMax;r++){
292     Int_t lrow=r-fRowMin;
293     tempPt->fRow = r;
294     tempPt->fNDigit = ndigits[lrow];
295   
296     Int_t localcount=0;
297     for(Int_t pad=0;pad<AliL3Transform::GetNPads(r);pad++){
298       for(Int_t time=0;time<AliL3Transform::GetNTimeBins();time++){
299         UShort_t dig=charges[lrow][pad][time];
300         if(!dig) continue;
301
302         if(localcount >= ndigits[lrow])
303           LOG(AliL3Log::kFatal,"AliL3DDLDataFileHandler::DDLDigits2Binary","Memory")
304             <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
305             <<ndigits[lrow]<<ENDLOG;
306         
307
308         tempPt->fDigitData[localcount].fCharge=dig;
309         tempPt->fDigitData[localcount].fPad=pad;
310         tempPt->fDigitData[localcount].fTime=time;
311 #ifdef do_mc
312         tempPt->fDigitData[localcount].fTrackID[0] = 0;
313         tempPt->fDigitData[localcount].fTrackID[1] = 0;
314         tempPt->fDigitData[localcount].fTrackID[2] = 0;
315 #endif
316         localcount++;
317       }
318     }
319
320     if(localcount != ndigits[lrow])
321       LOG(AliL3Log::kFatal,"AliL3DDLDataFileHandler::DDLDigits2Binary","Memory")
322         <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
323         <<ndigits[lrow]<<ENDLOG;
324
325
326     Byte_t *tmp = (Byte_t*)tempPt;
327     Int_t size = sizeof(AliL3DigitRowData)
328                                       + ndigits[lrow]*sizeof(AliL3DigitData);
329     tmp += size;
330     tempPt = (AliL3DigitRowData*)tmp;
331   }
332
333   //delete charge array
334   for(Int_t r=fRowMin;r<=fRowMax;r++){
335     Int_t lrow=r-fRowMin;
336     for(Int_t k=0;k<AliL3Transform::GetNPads(r);k++)
337         delete charges[lrow][k];
338     delete charges[lrow];
339   }
340   delete charges;
341   delete [] ndigits;
342
343   return data;
344 }
345 #else
346 AliL3DigitRowData * AliL3DDLDataFileHandler::DDLData2Memory(UInt_t &nrow,Int_t event)
347 {
348   // transfers the DDL data to the memory
349 #ifdef use_newio
350   if((fEvent>=0)&&(event!=fEvent)){
351     fEvent=event;
352     if(fReader) delete fReader;
353     if(fTPCStream) delete fTPCStream;
354     fReader=new AliRawReaderRoot(fFilename,event);
355     fTPCStream=new AliTPCRawStream(fReader);
356   }
357 #endif
358   AliL3DigitRowData *data = 0;
359   nrow=0;
360
361   if(!fReader){
362     LOG(AliL3Log::kWarning,"AliL3DDLDataFileHandler::DDLData2Memory","File")
363     <<"No Input avalible: no object AliL3DDLRawReaderFile"<<ENDLOG;
364     return 0; 
365   }
366   
367   Int_t nrows=fRowMax-fRowMin+1;
368
369   Int_t ddlsToSearch=0;
370   Int_t ddls[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
371   Int_t ddlid=-1,lddlid=-1;
372   for(Int_t r=fRowMin;r<=fRowMax;r++){
373
374     Int_t patch=AliL3Transform::GetPatch(r);
375     Int_t sector,row;
376     AliL3Transform::Slice2Sector(fSlice,r,sector,row);
377
378     if(sector<36) //taken from AliTPCBuffer160.cxx
379       ddlid=sector*2+patch;
380     else
381       ddlid=70+(sector-36)*4+patch;
382
383     if((lddlid==-1)||(ddlid!=lddlid)){
384       ddls[ddlsToSearch++]=ddlid;
385       lddlid=ddlid;
386     }
387   }
388
389   //  for(Int_t i=0;i<ddlsToSearch;i++) cout << ddls[i] <<endl;
390
391 #ifdef use_newio
392   fReader->Reset();
393   fReader->Select(0,ddls[0],ddls[ddlsToSearch-1]);
394   fTPCStream->Reset();
395 #else
396   fTPCStream->SetDDLID(ddls[i]); //ddl to read out
397 #endif
398
399   nrow = (UInt_t)nrows;
400
401   return data;
402 }
403 #endif
404
405 Bool_t AliL3DDLDataFileHandler::DDLData2CompBinary(Int_t event)
406 {
407   // transfers the DDL data to the memory and converts it 
408   // to comp binary 
409   Bool_t out = kTRUE;
410   UInt_t ndigits=0;
411   AliL3DigitRowData *digits=0;
412   digits = DDLData2Memory(ndigits,event);
413   out = Memory2CompBinary(ndigits,digits);
414   Free();
415   return out;
416 }