]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/src/AliL3FileHandler.cxx
Bugfix in AliDigits2RootFile, and AliDigits2Memory
[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 "AliTPCDigitsArray.h"
18 #include "AliTPCClustersArray.h"
19 #include "AliTPCcluster.h"
20 #include "AliTPCClustersRow.h"
21 #include "AliTPCParam.h"
22 #include "AliSimDigits.h"
23
24 #include "AliL3DigitData.h"
25 #include "AliL3TrackSegmentData.h"
26 #include "AliL3SpacePointData.h"
27 #include "AliL3TrackArray.h"
28 //_____________________________________________________________
29 //
30 // The L3 Binary File handler 
31 //
32
33 ClassImp(AliL3FileHandler)
34
35 AliL3FileHandler::AliL3FileHandler(){
36   //Default constructor
37   fInAli = 0;
38   fParam = 0;
39   fTransformer = 0;
40   fMC =0;
41 }
42
43
44 AliL3FileHandler::~AliL3FileHandler(){
45   //Destructor
46   if(fTransformer) delete fTransformer;
47   if(fMC) CloseMCOutput();
48 }
49
50 Bool_t AliL3FileHandler::SetMCOutput(char *name){
51   fMC = fopen(name,"w");
52   if(!fMC){
53     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
54     <<"Pointer to File = 0x0 "<<ENDLOG;
55     return kFALSE;
56   }
57   return kTRUE;
58 }
59
60 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
61   fMC = file;
62   if(!fMC){
63     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
64     <<"Pointer to File = 0x0 "<<ENDLOG;
65     return kFALSE;
66   }
67   return kTRUE;
68 }
69
70 void AliL3FileHandler::CloseMCOutput(){
71   if(!fMC){
72     LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
73     <<"Nothing to Close"<<ENDLOG;
74     return;
75   }
76   fclose(fMC);
77   fMC =0;
78 }
79
80 Bool_t AliL3FileHandler::SetAliInput(){
81   if(!fInAli->IsOpen()){
82     LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
83     <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
84     return kFALSE;
85   }
86   fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
87   if(!fParam){ 
88     LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
89     <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
90      return kFALSE;
91   }
92   fTransformer = new AliL3Transform();
93   return kTRUE;
94 }
95
96 Bool_t AliL3FileHandler::SetAliInput(char *name){
97   fInAli= new TFile(name,"READ");
98   if(!fInAli){
99     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
100     <<"Pointer to TFile = 0x0 "<<ENDLOG;
101     return kFALSE;
102   }
103   return SetAliInput();
104 }
105
106 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
107   fInAli=file;
108   if(!fInAli){
109     LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
110     <<"Pointer to TFile = 0x0 "<<ENDLOG;
111     return kFALSE;
112   }
113   return SetAliInput();
114 }
115
116 void AliL3FileHandler::CloseAliInput(){
117   if(!fInAli){
118     LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
119     <<"Nothing to Close"<<ENDLOG;
120      return;
121   }
122   if(fInAli->IsOpen()) fInAli->Close();
123   delete fInAli;
124   fInAli = 0;
125 }
126
127 Bool_t AliL3FileHandler::IsDigit(){
128   if(!fInAli){
129     LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
130     <<"Pointer to TFile = 0x0 "<<ENDLOG;
131     return kTRUE;  //may you are use binary input which is Digits!!
132   }
133   TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
134   if(t){
135     LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
136     <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
137     return kTRUE;
138   }
139   else{
140     LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
141     <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
142     return kFALSE;
143   }
144 }
145
146 ///////////////////////////////////////// Digit IO  
147 Bool_t AliL3FileHandler::AliDigits2Binary(){
148   Bool_t out = kTRUE;
149   UInt_t nrow;
150   AliL3DigitRowData* data = AliDigits2Memory(nrow);
151   out = Memory2Binary(nrow,data);
152   Free();
153   return out;
154 }
155
156
157 Bool_t AliL3FileHandler::AliDigits2CompBinary(){
158   Bool_t out = kTRUE;
159   UInt_t ndigits=0;
160   AliL3DigitRowData* digits = AliDigits2Memory(ndigits);
161   out = Memory2CompBinary(ndigits,digits);
162   Free();
163   return out;
164 }
165
166
167 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow){
168   AliL3DigitRowData *data = 0;
169   nrow=0;
170   if(!fInAli){
171     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
172     <<"No Input avalible: no object TFile"<<ENDLOG;
173     return 0; 
174   }
175   if(!fInAli->IsOpen()){
176     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
177     <<"No Input avalible: TFile not opend"<<ENDLOG;
178     return 0;
179   }
180
181   TDirectory *savedir = gDirectory;
182   fInAli->cd();
183   TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
184   if(!t){
185     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Binary","AliRoot")
186     <<"No Digit Tree inside!"<<ENDLOG;
187     return 0;
188   }
189   AliSimDigits digarr, *dummy=&digarr;
190   t->GetBranch("Segment")->SetAddress(&dummy);
191   UShort_t dig;
192   Int_t time,pad,sector,row;
193   Int_t nrows=0;
194   Int_t ndigitcount=0;
195   Int_t entries = (Int_t)t->GetEntries();
196   Int_t ndigits[entries];
197   Int_t lslice,lrow;
198   for(Int_t n=0; n<t->GetEntries(); n++)
199     {
200       t->GetEvent(n);
201       fParam->AdjustSectorRow(digarr.GetID(),sector,row);
202       fTransformer->Sector2Slice(lslice,lrow,sector,row);
203       if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
204       //      if(fSlice != lslice) continue;
205
206       Float_t xyz[3];
207       ndigits[lrow] = 0;
208       digarr.First();
209       do {
210         time=digarr.CurrentRow();
211         pad=digarr.CurrentColumn();
212         dig = digarr.GetDigit(time,pad);
213         if(dig<=fParam->GetZeroSup()) continue;
214         if(time < fParam->GetMaxTBin()-1 && time > 0)
215           if(digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()
216              && digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup())
217             continue;
218
219         fTransformer->Raw2Local(xyz,sector,row,pad,time);
220         if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
221           continue;
222
223         ndigits[lrow]++; //for this row only
224         ndigitcount++;  //total number of digits to be published
225
226       } while (digarr.Next());
227
228       nrows++;
229     }
230   Int_t size = sizeof(AliL3DigitData)*ndigitcount
231                                       + nrows*sizeof(AliL3DigitRowData);
232
233   LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
234   <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
235
236   data=(AliL3DigitRowData*) Allocate(size);
237   nrow = (UInt_t)nrows;
238   AliL3DigitRowData *tempPt = data;
239   for(Int_t n=0; n<t->GetEntries(); n++)
240     {
241       t->GetEvent(n);
242
243       Float_t xyz[3];
244       fParam->AdjustSectorRow(digarr.GetID(),sector,row);
245       fTransformer->Sector2Slice(lslice,lrow,sector,row);
246 //      if(fSlice != lslice) continue;
247       if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
248       tempPt->fRow = lrow;
249       tempPt->fNDigit = ndigits[lrow];
250
251       Int_t localcount=0;
252       digarr.First();
253       do {
254         //dig=digarr.CurrentDigit();
255         //if (dig<=fParam->GetZeroSup()) continue;
256         time=digarr.CurrentRow();
257         pad=digarr.CurrentColumn();
258         dig = digarr.GetDigit(time,pad);
259         if (dig <= fParam->GetZeroSup()) continue;
260         if(time < fParam->GetMaxTBin()-1 && time > 0)
261           if(digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
262              digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
263
264         //Exclude data outside cone:
265         fTransformer->Raw2Local(xyz,sector,row,pad,time);
266         if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
267           continue;
268
269         if(localcount >= ndigits[lrow])
270           LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
271             <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
272             <<ndigits[lrow]<<ENDLOG;
273         
274         tempPt->fDigitData[localcount].fCharge=dig;
275         tempPt->fDigitData[localcount].fPad=pad;
276         tempPt->fDigitData[localcount].fTime=time;
277         localcount++;
278       } while (digarr.Next());
279
280       Byte_t *tmp = (Byte_t*)tempPt;
281       Int_t size = sizeof(AliL3DigitRowData)
282                                       + ndigits[lrow]*sizeof(AliL3DigitData);
283       tmp += size;
284       tempPt = (AliL3DigitRowData*)tmp;
285     }
286   savedir->cd(); 
287   return data;
288 }
289
290 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
291 {
292   //Write digits to a new alirootfile.
293
294   if(!fInAli)
295     {
296       printf("AliL3FileHandler::AliDigits2RootFile : No input alirootfile\n");
297       return;
298     }
299   if(!fParam)
300     {
301       printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
302       return;
303     }
304   if(!fTransformer)
305     {
306       printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
307       return;
308     }
309   
310   //Get the original digitstree:
311   fInAli->cd();
312   AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
313   old_array->Setup(fParam);
314   old_array->SetClass("AliSimDigits");
315   Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
316   if(!ok)
317     {
318       printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
319       return;
320     }
321   
322   Bool_t create=kFALSE;
323   TFile *digFile;
324   
325   if(fPatch == 0)
326     {    
327       digFile = TFile::Open(new_digitsfile,"RECREATE");
328       create = kTRUE;
329       fParam->Write(fParam->GetTitle());
330     }
331   else
332     {
333       digFile = TFile::Open(new_digitsfile,"UPDATE");
334       create=kFALSE;
335     }
336   if(!digFile->IsOpen())
337     {
338       printf("AliL3FileHandler::AliDigits2RootFile : Error opening a new rootfile\n");
339       return;
340     }
341   
342   digFile->cd();
343     
344   //setup a new one, or connect it to the existing one:
345   AliTPCDigitsArray *arr = new AliTPCDigitsArray; 
346   arr->SetClass("AliSimDigits");
347   arr->Setup(fParam);
348   if(create)
349     arr->MakeTree();
350   else
351     {
352       Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
353       if(!ok)
354         {
355           printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
356           return;
357         }
358     }
359   for(Int_t i=fRowMin; i<=fRowMax; i++)
360     {
361       
362       if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
363             
364       Int_t sector,row;
365       fTransformer->Slice2Sector(fSlice,i,sector,row);
366       AliDigits * dig = arr->CreateRow(sector,row);
367       AliDigits *old_dig = old_array->LoadRow(sector,row);
368       if(!old_dig)
369         printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
370       
371       AliL3DigitData *digPt = rowPt->fDigitData;
372       for(UInt_t j=0; j<rowPt->fNDigit; j++)
373         {
374           UShort_t charge = digPt[j].fCharge;
375           UChar_t pad = digPt[j].fPad;
376           UShort_t time = digPt[j].fTime;
377           
378           if(charge == 0) //Only write the digits that has not been removed
379             continue;
380           dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
381           ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
382           ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
383           ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
384           
385         }
386       UpdateRowPointer(rowPt);
387       arr->StoreRow(sector,row);
388       arr->ClearRow(sector,row);  
389       old_array->ClearRow(sector,row);
390     }
391   digFile->cd();
392   char treeName[100];
393   sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
394   arr->GetTree()->Write(treeName,TObject::kOverwrite);
395   digFile->Close();
396   //arr->GetTree()->Delete();
397   //delete arr;
398 }
399
400 ///////////////////////////////////////// Point IO  
401 Bool_t AliL3FileHandler::AliPoints2Binary(){
402   Bool_t out = kTRUE;
403   UInt_t npoint;
404   AliL3SpacePointData *data = AliPoints2Memory(npoint);
405   out = Memory2Binary(npoint,data);
406   Free();
407   return out;
408 }
409
410 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
411   AliL3SpacePointData *data = 0;
412   npoint=0;
413   if(!fInAli){
414     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
415     <<"No Input avalible: no object TFile"<<ENDLOG;
416     return 0;
417   }
418   if(!fInAli->IsOpen()){
419     LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
420     <<"No Input avalible: TFile not opend"<<ENDLOG;
421     return 0;
422   }
423   TDirectory *savedir = gDirectory;
424   fInAli->cd();
425
426   AliTPCClustersArray carray;
427   carray.Setup(fParam);
428   carray.SetClusterType("AliTPCcluster");
429   Bool_t clusterok = carray.ConnectTree("Segment Tree");
430   if(!clusterok) return 0;
431
432   AliTPCClustersRow ** clusterrow = 
433                new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
434   Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
435   Int_t *sects = new int[  (int)carray.GetTree()->GetEntries()];
436   Int_t sum=0;
437
438   Int_t lslice,lrow;
439   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
440     AliSegmentID *s = carray.LoadEntry(i);
441     Int_t sector,row;
442     fParam->AdjustSectorRow(s->GetID(),sector,row);
443     rows[i] = row;
444     sects[i] = sector;
445     clusterrow[i] = 0;
446     fTransformer->Sector2Slice(lslice,lrow,sector,row);
447     if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
448     clusterrow[i] = carray.GetRow(sector,row);
449     if(clusterrow[i])
450       sum+=clusterrow[i]->GetArray()->GetEntriesFast();
451   }
452   UInt_t size = sum*sizeof(AliL3SpacePointData);
453
454   LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
455   <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
456
457   data = (AliL3SpacePointData *) Allocate(size);
458   npoint = sum;
459   UInt_t n=0; 
460   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
461     if(!clusterrow[i]) continue;
462     Int_t row = rows[i];
463     Int_t sector = sects[i];
464     fTransformer->Sector2Slice(lslice,lrow,sector,row);
465     Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
466     for(Int_t j = 0;j<entries_in_row;j++){
467       AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
468       data[n].fZ = c->GetZ();
469       data[n].fY = c->GetY();
470       data[n].fX = fParam->GetPadRowRadii(sector,row);
471       data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
472       data[n].fPadRow = lrow;
473       data[n].fXYErr = c->GetSigmaY2();
474       data[n].fZErr = c->GetSigmaZ2();
475       if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
476       n++;
477     }
478   }
479   for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
480     Int_t row = rows[i];
481     Int_t sector = sects[i];
482     if(carray.GetRow(sector,row))
483       carray.ClearRow(sector,row);
484   }
485
486   delete [] clusterrow;
487   delete [] rows;
488   delete [] sects;
489   savedir->cd();   
490
491   return data;
492 }
493