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