This commit was generated by cvs2svn to compensate for changes in r3176,
[u/mrichter/AliRoot.git] / HLT / src / AliL3FileHandler.cxx
1
2 //Author:        Uli Frankenfeld
3 //Last Modified: 17.12.2000
4
5 #include <math.h>
6 #include <iostream.h>
7 #include <TFile.h>
8 #include <TTree.h>
9 #include <stdio.h>
10
11 #include "AliL3Transform.h"
12 #include "AliL3Logging.h"
13 #include "AliL3MemHandler.h"
14 #include "AliL3FileHandler.h"
15
16 #include "AliTPCClustersArray.h"
17 #include "AliTPCcluster.h"
18 #include "AliTPCClustersRow.h"
19 #include "AliTPCParam.h"
20 #include "AliSimDigits.h"
21
22 #include "AliL3DigitData.h"
23 #include "AliL3TrackSegmentData.h"
24 #include "AliL3SpacePointData.h"
25 #include "AliL3TrackArray.h"
26 //_____________________________________________________________
27 //
28 // The L3 Binary File handler 
29 //
30
31 ClassImp(AliL3FileHandler)
32
33 AliL3FileHandler::AliL3FileHandler(){
34   //Default constructor
35   fInAli = 0;
36   fParam = 0;
37   fTransformer = 0;
38   fMC =0;
39 }
40
41
42 AliL3FileHandler::~AliL3FileHandler(){
43   //Destructor
44   if(fTransformer) delete fTransformer;
45   if(fMC) CloseMCOutput();
46 }
47
48 Bool_t AliL3FileHandler::SetMCOutput(char *name){
49   fMC = fopen(name,"w");
50   if(!fMC){
51     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
52     <<"Pointer to File = 0x0 "<<ENDLOG;
53     return kFALSE;
54   }
55   return kTRUE;
56 }
57
58 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
59   fMC = file;
60   if(!fMC){
61     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
62     <<"Pointer to File = 0x0 "<<ENDLOG;
63     return kFALSE;
64   }
65   return kTRUE;
66 }
67
68 void AliL3FileHandler::CloseMCOutput(){
69   if(!fMC){
70     LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
71     <<"Nothing to Close"<<ENDLOG;
72     return;
73   }
74   fclose(fMC);
75   fMC =0;
76 }
77
78 Bool_t AliL3FileHandler::SetAliInput(){
79   if(!fInAli->IsOpen()){
80     LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
81     <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
82     return kFALSE;
83   }
84   fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
85   if(!fParam){ 
86     LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
87     <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
88      return kFALSE;
89   }
90   fTransformer = new AliL3Transform();
91   return kTRUE;
92 }
93
94 Bool_t AliL3FileHandler::SetAliInput(char *name){
95   fInAli= new TFile(name,"READ");
96   if(!fInAli){
97     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
98     <<"Pointer to TFile = 0x0 "<<ENDLOG;
99     return kFALSE;
100   }
101   return SetAliInput();
102 }
103
104 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
105   fInAli=file;
106   if(!fInAli){
107     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
108     <<"Pointer to TFile = 0x0 "<<ENDLOG;
109     return kFALSE;
110   }
111   return SetAliInput();
112 }
113
114 void AliL3FileHandler::CloseAliInput(){
115   if(!fInAli){
116     LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
117     <<"Nothing to Close"<<ENDLOG;
118      return;
119   }
120   if(fInAli->IsOpen()) fInAli->Close();
121   delete fInAli;
122   fInAli = 0;
123 }
124
125 Bool_t AliL3FileHandler::IsDigit(){
126   if(!fInAli){
127     LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
128     <<"Pointer to TFile = 0x0 "<<ENDLOG;
129     return kTRUE;  //may you are use binary input which is Digits!!
130   }
131   TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60");
132   if(t){
133     LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
134     <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
135     return kTRUE;
136   }
137   else{
138     LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
139     <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
140     return kFALSE;
141   }
142 }
143
144 ///////////////////////////////////////// Digit IO  
145 Bool_t AliL3FileHandler::AliDigits2Binary(){
146   Bool_t out = kTRUE;
147   UInt_t nrow;
148   AliL3DigitRowData* data = AliDigits2Memory(nrow);
149   out = Memory2Binary(nrow,data);
150   Free();
151   return out;
152 }
153
154
155 Bool_t AliL3FileHandler::AliDigits2CompBinary(){
156   Bool_t out = kTRUE;
157   UInt_t ndigits=0;
158   AliL3DigitRowData* digits = AliDigits2Memory(ndigits);
159   out = Memory2CompBinary(ndigits,digits);
160   Free();
161   return out;
162 }
163
164
165 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow){
166   AliL3DigitRowData *data = 0;
167   nrow=0;
168   if(!fInAli){
169     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
170     <<"No Input avalible: no object TFile"<<ENDLOG;
171     return 0; 
172   }
173   if(!fInAli->IsOpen()){
174     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
175     <<"No Input avalible: TFile not opend"<<ENDLOG;
176     return 0;
177   }
178
179   TDirectory *savedir = gDirectory;
180   fInAli->cd();
181   TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60");
182   if(!t){
183     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Binary","AliRoot")
184     <<"No Digit Tree inside!"<<ENDLOG;
185     return 0;
186   }
187   AliSimDigits digarr, *dummy=&digarr;
188   t->GetBranch("Segment")->SetAddress(&dummy);
189   UShort_t dig;
190   Int_t time,pad,sector,row;
191   Int_t nrows=0;
192   Int_t ndigitcount=0;
193   Int_t entries = (Int_t)t->GetEntries();
194   Int_t ndigits[entries];
195   Int_t lslice,lrow;
196   for(Int_t n=0; n<t->GetEntries(); n++)
197     {
198       t->GetEvent(n);
199       fParam->AdjustSectorRow(digarr.GetID(),sector,row);
200       fTransformer->Sector2Slice(lslice,lrow,sector,row);
201       if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
202 //      if(fSlice != lslice) continue;
203
204       Float_t xyz[3];
205       ndigits[lrow] = 0;
206       digarr.First();
207       do {
208         time=digarr.CurrentRow();
209         pad=digarr.CurrentColumn();
210         dig = digarr.GetDigit(time,pad);
211         if(dig<=fParam->GetZeroSup()) continue;
212         if(time < fParam->GetMaxTBin()-1 && time > 0)
213           if(digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()
214              && digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup())
215             continue;
216
217         fTransformer->Raw2Local(xyz,sector,row,pad,time);
218         if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
219           continue;
220
221         ndigits[lrow]++; //for this row only
222         ndigitcount++;  //total number of digits to be published
223
224       } while (digarr.Next());
225
226       nrows++;
227     }
228   Int_t size = sizeof(AliL3DigitData)*ndigitcount
229                                       + nrows*sizeof(AliL3DigitRowData);
230
231   LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
232   <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
233
234   data=(AliL3DigitRowData*) Allocate(size);
235   nrow = (UInt_t)nrows;
236   AliL3DigitRowData *tempPt = data;
237   for(Int_t n=0; n<t->GetEntries(); n++)
238     {
239       t->GetEvent(n);
240
241       Float_t xyz[3];
242       fParam->AdjustSectorRow(digarr.GetID(),sector,row);
243       fTransformer->Sector2Slice(lslice,lrow,sector,row);
244 //      if(fSlice != lslice) continue;
245       if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
246       tempPt->fRow = lrow;
247       tempPt->fNDigit = ndigits[lrow];
248
249       Int_t localcount=0;
250       digarr.First();
251       do {
252         dig=digarr.CurrentDigit();
253         if (dig<=fParam->GetZeroSup()) continue;
254         time=digarr.CurrentRow();
255         pad=digarr.CurrentColumn();
256         if(time < fParam->GetMaxTBin()-1 && time > 0)
257           if(digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
258              digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
259
260         //Exclude data outside cone:
261         fTransformer->Raw2Local(xyz,sector,row,pad,time);
262         if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
263           continue;
264
265         if(localcount >= ndigits[lrow])
266           LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
267           <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
268           <<ndigits[lrow]<<ENDLOG;
269
270         tempPt->fDigitData[localcount].fCharge=dig;
271         tempPt->fDigitData[localcount].fPad=pad;
272         tempPt->fDigitData[localcount].fTime=time;
273         localcount++;
274       } while (digarr.Next());
275
276       Byte_t *tmp = (Byte_t*)tempPt;
277       Int_t size = sizeof(AliL3DigitRowData)
278                                       + ndigits[lrow]*sizeof(AliL3DigitData);
279       tmp += size;
280       tempPt = (AliL3DigitRowData*)tmp;
281     }
282   savedir->cd(); 
283   return data;
284 }
285
286 ///////////////////////////////////////// Point IO  
287 Bool_t AliL3FileHandler::AliPoints2Binary(){
288   Bool_t out = kTRUE;
289   UInt_t npoint;
290   AliL3SpacePointData *data = AliPoints2Memory(npoint);
291   out = Memory2Binary(npoint,data);
292   Free();
293   return out;
294 }
295
296 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
297   AliL3SpacePointData *data = 0;
298   npoint=0;
299   if(!fInAli){
300     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
301     <<"No Input avalible: no object TFile"<<ENDLOG;
302     return 0;
303   }
304   if(!fInAli->IsOpen()){
305     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
306     <<"No Input avalible: TFile not opend"<<ENDLOG;
307     return 0;
308   }
309   TDirectory *savedir = gDirectory;
310   fInAli->cd();
311
312   AliTPCClustersArray carray;
313   carray.Setup(fParam);
314   carray.SetClusterType("AliTPCcluster");
315   Bool_t clusterok = carray.ConnectTree("Segment Tree");
316   if(!clusterok) return 0;
317
318   AliTPCClustersRow ** clusterrow = 
319                new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
320   Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
321   Int_t *sects = new int[  (int)carray.GetTree()->GetEntries()];
322   Int_t sum=0;
323
324   Int_t lslice,lrow;
325   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
326     AliSegmentID *s = carray.LoadEntry(i);
327     Int_t sector,row;
328     fParam->AdjustSectorRow(s->GetID(),sector,row);
329     rows[i] = row;
330     sects[i] = sector;
331     clusterrow[i] = 0;
332     fTransformer->Sector2Slice(lslice,lrow,sector,row);
333     if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
334     clusterrow[i] = carray.GetRow(sector,row);
335     if(clusterrow[i])
336       sum+=clusterrow[i]->GetArray()->GetEntriesFast();
337   }
338   UInt_t size = sum*sizeof(AliL3SpacePointData);
339
340   LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
341   <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
342
343   data = (AliL3SpacePointData *) Allocate(size);
344   npoint = sum;
345   UInt_t n=0; 
346   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
347     if(!clusterrow[i]) continue;
348     Int_t row = rows[i];
349     Int_t sector = sects[i];
350     fTransformer->Sector2Slice(lslice,lrow,sector,row);
351     Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
352     for(Int_t j = 0;j<entries_in_row;j++){
353       AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
354       data[n].fZ = c->GetZ();
355       data[n].fY = c->GetY();
356       data[n].fX = fParam->GetPadRowRadii(sector,row);
357       data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
358       data[n].fPadRow = lrow;
359       data[n].fXYErr = c->GetSigmaY2();
360       data[n].fZErr = c->GetSigmaZ2();
361       if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
362       n++;
363     }
364   }
365   for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
366     Int_t row = rows[i];
367     Int_t sector = sects[i];
368     if(carray.GetRow(sector,row))
369       carray.ClearRow(sector,row);
370   }
371
372   delete [] clusterrow;
373   delete [] rows;
374   delete [] sects;
375   savedir->cd();   
376
377   return data;
378 }
379