Little changes to make g++ version 3.2 compile the src library. Problems remaining...
[u/mrichter/AliRoot.git] / HLT / src / AliLevel3.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>, Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright &copy ASV
5
6 #include "AliL3StandardIncludes.h"
7
8 #ifndef no_root
9 #include <TFile.h>
10 #include <TDirectory.h>
11 #include <TClonesArray.h>
12 #include <TStopwatch.h>
13 #endif
14
15 #include "AliL3Logging.h"
16 #include "AliLevel3.h"
17 #include "AliL3ConfMapper.h"
18 #include "AliL3Vertex.h"
19 #include "AliL3VertexFinder.h"
20 #include "AliL3TrackMerger.h"
21 #include "AliL3GlobalMerger.h"
22 #include "AliL3InterMerger.h"
23 #include "AliL3ConfMapPoint.h"
24 #include "AliL3ConfMapTrack.h"
25 #include "AliL3Transform.h"
26 #include "AliL3ClustFinderNew.h"
27 #include "AliL3DigitData.h"
28 #include "AliL3TrackArray.h"
29 #include "AliL3MemHandler.h"
30 #ifdef use_aliroot
31 #include "AliL3FileHandler.h"
32 #endif
33 #include "AliL3Benchmark.h"
34 #include "AliL3DigitData.h"
35 #include "AliL3TrackSegmentData.h"
36 #include "AliL3SpacePointData.h"
37 #include "AliL3VertexData.h"
38
39 //_____________________________________________________________
40 //
41 //  AliLevel3
42 //
43 //  Interface class for Level3 tracker.
44 //  Tracking is done by calling constructor with input,output 
45 //  given as argument. 
46 //  You must always remember to set the tracking parameters. E.g.:
47 // 
48 //  AliLevel3 *level3 = new AliLevel3(inputfile,outputfile);
49 //  level3->SetTrackerParam(); //Sets default tracking parameters
50 //  level3->ProcessSector(2,2);  //Does tracking on sector 2 (actually 2+38)
51 //Begin_Html
52 /*
53 <img src="tpcsectorsnb.gif">
54 */
55 //End_Html
56
57 ClassImp(AliLevel3)
58
59 AliLevel3::AliLevel3()
60 {
61   fInputFile=0;
62 }
63
64 #ifdef no_root
65 AliLevel3::AliLevel3(Char_t *infile)
66 {
67   //Constructor. Calls constructor of the tracker, vertexfinder and merger classes.
68   
69   fInputFile = fopen(infile,"r");
70   
71   if(!fInputFile)
72     {
73       LOG(AliL3Log::kError,"AliLevel3::AliLevel3","File Open")
74         <<"Inputfile "<<infile<<" does not exist!"<<ENDLOG;
75       return;
76     }
77 }
78 #else
79 AliLevel3::AliLevel3(Char_t *infile)
80 {
81   //Constructor. Calls constructor of the tracker, vertexfinder and merger classes.
82   
83   fInputFile = new TFile(infile,"READ");
84   
85   if(!fInputFile->IsOpen())
86     {
87       LOG(AliL3Log::kError,"AliLevel3::AliLevel3","File Open")
88         <<"Inputfile "<<infile<<" does not exist!"<<ENDLOG;
89       return;
90     }
91 }
92 #endif
93
94 #ifndef no_root
95 AliLevel3::AliLevel3(TFile *in)
96 {
97   fInputFile  =  in;
98   if(!in){
99     LOG(AliL3Log::kError,"AliLevel3::AliLevel3","File Open")
100     <<"Pointer to InFile 0x0!"<<ENDLOG;
101     return;
102   }  
103   
104   if(!fInputFile->IsOpen())
105     {
106     LOG(AliL3Log::kError,"AliLevel3::AliLevel3","File Open")
107     <<"Inputfile does not exist"<<ENDLOG;
108       return;
109     }
110 }
111 #endif
112
113 void AliLevel3::Init(Char_t *path,Bool_t binary,Int_t npatches)
114 {
115   if(!binary && !fInputFile)
116     {
117       LOG(AliL3Log::kError,"AliLevel3::Init","Files")
118         <<"You have not supplied the input rootfile; use the appropriate ctor!"<<ENDLOG;
119       return;
120     }
121   
122   AliL3Transform::Init(path);//Initialize the detector parameters.
123   fWriteOut = kFALSE;
124   fUseBinary = binary;
125   SetPath(path);
126   fGlobalMerger = 0;
127
128   fDoRoi = kFALSE;
129   fDoNonVertex = kFALSE;
130   fFindVertex = kFALSE;
131   SetClusterFinderParam();
132
133   fEta[0] = 0.;
134   fEta[1] = 0.9;
135
136   fEvent=0;
137
138   switch(npatches){
139   case 1:
140     fNPatch = 1;        //number of patches change row in process
141     fRow[0][0] = 0;     // first row
142     fRow[0][1] = 175;   // last row
143     break;
144   case 2:
145     fNPatch = 2;        //number of patches change row in process
146     fRow[0][0] = 0;     // first row
147     fRow[0][1] = 54;
148     fRow[1][0] = 55;
149     fRow[1][1] = 175;   // last row
150     break;
151   case 5:
152     fNPatch = 5;        //number of patches change row in process
153     fRow[0][0] = 0;     // first row
154     fRow[0][1] = 45;
155     fRow[1][0] = 46;
156     fRow[1][1] = 77;
157     fRow[2][0] = 78;
158     fRow[2][1] = 109;
159     fRow[3][0] = 110; 
160     fRow[3][1] = 141;
161     fRow[4][0] = 142;
162     fRow[4][1] = 175;   // last row
163     break;
164   default: //should match entries in AliL3Transform
165     fNPatch = 6;        //number of patches change row in process
166     fRow[0][0] = 0;     // first row
167     fRow[0][1] = 31;
168     fRow[1][0] = 32;
169     fRow[1][1] = 63;
170     fRow[2][0] = 64;
171     fRow[2][1] = 91;
172     fRow[3][0] = 92;
173     fRow[3][1] = 119;
174     fRow[4][0] = 120;
175     fRow[4][1] = 143;   
176     fRow[5][0] = 144;
177     fRow[5][1] = 175;   // last row 
178   }
179
180   fVertexFinder = new AliL3VertexFinder();
181   fVertex = new AliL3Vertex();
182   fTracker = new AliL3ConfMapper();
183   fTrackMerger = new AliL3TrackMerger(fNPatch);
184   fInterMerger = new AliL3InterMerger();
185 #ifdef use_aliroot
186   fFileHandler = new AliL3FileHandler();
187   fFileHandler->SetAliInput(fInputFile);
188 #else
189   fFileHandler = new AliL3MemHandler();
190 #endif
191   fBenchmark = new AliL3Benchmark();
192 }
193
194 void AliLevel3::DoBench(char* name){
195   fBenchmark->Analyze(name);
196 }
197
198 void AliLevel3::DoMc(char* file){
199 #ifdef use_aliroot
200   if(!fFileHandler->IsDigit())
201     fFileHandler->SetMCOutput(file);
202 #endif
203 }
204
205 AliLevel3::~AliLevel3(){
206   //Destructor
207   if(fVertexFinder) delete fVertexFinder;
208   if(fVertex) delete fVertex;
209   if(fTracker) delete fTracker;
210   if(fTrackMerger) delete fTrackMerger;
211   if(fInterMerger) delete fInterMerger;
212   if(fFileHandler) delete fFileHandler;
213 }
214
215 void AliLevel3::SetClusterFinderParam(Float_t fXYError, Float_t fZError, Bool_t deconv)
216 {
217   fXYClusterError=fXYError;
218   fZClusterError=fZError;
219   fClusterDeconv=deconv;
220 }
221
222 void AliLevel3::SetTrackerParam(Int_t phi_segments, Int_t eta_segments,
223                                 Int_t trackletlength, Int_t tracklength,
224                                 Int_t rowscopetracklet, Int_t rowscopetrack,
225                                 Double_t min_pt_fit, Double_t maxangle,
226                                 Double_t goodDist, Double_t hitChi2Cut,
227                                 Double_t goodHitChi2, Double_t trackChi2Cut,
228                                 Int_t maxdist,Double_t maxphi,Double_t maxeta,Bool_t vertexconstraint)
229 {
230   //Set parameters input to the tracker
231   //If no arguments are given, default parameters will be used
232   
233   fTracker->SetNSegments(phi_segments,eta_segments);
234   fTracker->SetMaxDca(min_pt_fit);
235   fTracker->SetTrackCuts(hitChi2Cut,goodHitChi2,trackChi2Cut,maxdist,vertexconstraint);
236   fTracker->SetTrackletCuts(maxangle,goodDist,vertexconstraint);
237   if(vertexconstraint)
238     fTracker->MainVertexSettings(trackletlength,tracklength,rowscopetracklet,rowscopetrack,maxphi,maxeta);
239   else
240     fTracker->NonVertexSettings(trackletlength,tracklength,rowscopetracklet,rowscopetrack);
241   
242   fTracker->SetParamDone(true);
243   fTracker->InitVolumes();
244 }
245
246 void AliLevel3::ProcessEvent(Int_t first,Int_t last,Int_t event){
247   //Do tracking on all slices in region [first,last]
248   //Slices numbering in TPC goes from 0-35, which means that one slice
249   //corresponds to inner+outer sector.E.g. slice 2 corresponds to
250   //inner=2 + outer=38.
251   fGlobalMerger= new AliL3GlobalMerger(first,last);  
252   fEvent=event;
253   for(Int_t i=first; i<=last; i++){
254     ProcessSlice(i);
255     fGlobalMerger->SetVertex(fVertex);
256     fGlobalMerger->InitSlice(i);
257     fGlobalMerger->FillTracks(fNTrackData,fTrackData);
258     fFileHandler->Free();   //free the memory
259     fNTrackData=0;
260     fTrackData=0;
261   }
262   fBenchmark->Start("Global Merger");
263   //fGlobalMerger->AddAllTracks();
264   //fGlobalMerger->Merge();
265   fGlobalMerger->SlowMerge();
266   fBenchmark->Stop("Global Merger");
267
268   if(fWriteOut) WriteResults(); 
269   delete fGlobalMerger; fGlobalMerger = 0;
270 }
271
272 void AliLevel3::ProcessSlice(Int_t slice){
273   char name[256];
274   Bool_t UseCF = kFALSE;
275 #ifdef use_aliroot
276   UseCF = fFileHandler->IsDigit();
277 #else
278   if(fUseBinary)
279     UseCF = kTRUE; //In case you are not using aliroot
280 #endif
281   const Int_t maxpoints=100000;
282   const Int_t pointsize = maxpoints * sizeof(AliL3SpacePointData);
283   AliL3MemHandler *memory = new AliL3MemHandler();
284
285   fTrackMerger->Reset();
286   fTrackMerger->SetRows(fRow[0]);
287   for(Int_t patch=fNPatch-1;patch>=0;patch--){
288     fFileHandler->Init(slice,patch,&fRow[patch][0]);
289     UInt_t npoints=0;
290     AliL3SpacePointData *points =0;
291     UInt_t ndigits=0;
292     AliL3DigitRowData *digits =0;
293     if(UseCF){
294       if(fUseBinary){
295         if(!fDoRoi){ 
296           if(1){     //Binary to Memory
297             fFileHandler->Free();
298             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
299             if(!fFileHandler->SetBinaryInput(name)) return;
300             digits= (AliL3DigitRowData *)fFileHandler->CompBinary2Memory(ndigits);
301             fFileHandler->CloseBinaryInput(); 
302           }
303
304           if(0){     //Binary to Memory with Benchmark 
305             fFileHandler->Free();
306             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
307             if(!memory->SetBinaryInput(name)) return;
308             UInt_t compsize=memory->GetFileSize();
309             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
310             memory->CompBinary2CompMemory(ndigits,comp);
311             memory->CloseBinaryInput();
312             UInt_t datasize=memory->GetMemorySize(ndigits,comp);
313             digits=(AliL3DigitRowData *)fFileHandler->Allocate(datasize);
314             fBenchmark->Start("Unpacker"); 
315             fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
316             fBenchmark->Stop("Unpacker");
317             memory->Free();
318           }
319   
320           if(0){     //Binary to Memory with Random
321             fFileHandler->Free();
322             fFileHandler->ResetRandom();
323             fFileHandler->SetRandomCluster(100);
324             fFileHandler->SetNGenerate(100);
325             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
326             if(!memory->SetBinaryInput(name)) return;
327             UInt_t compsize=memory->GetFileSize();
328             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
329             memory->CompBinary2CompMemory(ndigits,comp);
330             memory->CloseBinaryInput();
331             UInt_t dsize=memory->GetMemorySize(ndigits,comp);
332             UInt_t rsize=fFileHandler->GetRandomSize();       
333             digits=(AliL3DigitRowData*)fFileHandler->Allocate(dsize+rsize);
334             fBenchmark->Start("Unpacker");
335             fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
336             fBenchmark->Stop("Unpacker");
337             memory->Free();
338           }
339         }
340
341         else{     //Binary to Memory with Roi
342           fFileHandler->Free();
343           Int_t sli[2]={0,0};
344           fFileHandler->SetROI(fEta,sli);
345           sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
346           if(!memory->SetBinaryInput(name)) return;
347           UInt_t compsize=memory->GetFileSize();
348           UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
349           memory->CompBinary2CompMemory(ndigits,comp);
350           memory->CloseBinaryInput();
351           UInt_t datasize=memory->GetMemorySize(ndigits,comp);
352           digits=(AliL3DigitRowData *)fFileHandler->Allocate(datasize);
353           fBenchmark->Start("Unpacker"); 
354           datasize = fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
355           fBenchmark->Stop("Unpacker"); 
356           memory->Free();
357         }
358       }//end UseBinary
359       else{
360 #ifdef use_aliroot
361         fBenchmark->Start("Dummy Unpacker");
362         sprintf(name,"digits_%d_%d.raw",slice,patch);
363         fBenchmark->Stop("Dummy Unpacker");
364
365         if(0){    //Ali to Binary
366           fFileHandler->SetBinaryOutput(name);
367           fFileHandler->AliDigits2CompBinary();
368           fFileHandler->CloseBinaryOutput();
369         }
370   
371         if(1){     //Ali to Memory
372           digits=(AliL3DigitRowData *)fFileHandler->AliDigits2Memory(ndigits,fEvent);
373           if(fWriteOut){   //Memory to Binary
374             fFileHandler->SetBinaryOutput(name);
375             fFileHandler->Memory2CompBinary(ndigits,digits);
376             fFileHandler->CloseBinaryOutput();
377           }
378         }
379 #endif
380       }//end else UseBinary
381
382       points = (AliL3SpacePointData *) memory->Allocate(pointsize);
383   
384       fClusterFinder = new AliL3ClustFinderNew();
385       fClusterFinder->InitSlice(slice,patch,fRow[patch][0],fRow[patch][1],maxpoints);
386       fClusterFinder->SetDeconv(fClusterDeconv);
387       fClusterFinder->SetXYError(fXYClusterError);
388       fClusterFinder->SetZError(fZClusterError);
389       if((fXYClusterError>0)&&(fZClusterError>0))
390         fClusterFinder->SetCalcErr(kFALSE);
391       fClusterFinder->SetOutputArray(points);
392       fClusterFinder->Read(ndigits,digits);
393       fBenchmark->Start("Cluster Finder");
394       fClusterFinder->ProcessDigits();
395       fBenchmark->Stop("Cluster Finder");
396       npoints = fClusterFinder->GetNumberOfClusters();
397       delete fClusterFinder;
398       fClusterFinder = 0;
399       fFileHandler->Free();
400       LOG(AliL3Log::kInformational,"AliLevel3::ProcessSlice","Cluster Finder")
401         <<AliL3Log::kDec<<"Found "<<npoints<<" Points"<<ENDLOG;
402     }//end UseCF
403     else{// if not use Clusterfinder
404       if(fUseBinary){//Binary to Memory
405         memory->Free();
406         sprintf(name,"/%spoints_%d_%d.raw",fPath,slice,patch);
407         if(!memory->SetBinaryInput(name)) return;
408         points = (AliL3SpacePointData *) memory->Allocate();
409         memory->Binary2Memory(npoints,points);
410         memory->CloseBinaryInput();
411         LOG(AliL3Log::kInformational,"AliLevel3::ProcessSlice","Read Cluster")
412         <<AliL3Log::kDec<<"Found "<<npoints<<" Points in File"<<ENDLOG;
413       }
414 #ifdef use_aliroot
415       else{
416         points = fFileHandler->AliPoints2Memory(npoints);
417       }
418 #endif
419       fBenchmark->Start("Dummy Unpacker");
420       fBenchmark->Stop("Dummy Unpacker");
421       fBenchmark->Start("Dummy CF");
422       fBenchmark->Stop("Dummy CF");
423     }
424    
425     if(patch == fNPatch-1){
426       // Vertex
427       if(fFindVertex){
428       // Vertex Finder
429       
430         fBenchmark->Start("Vertex Finder Read");
431         fVertexFinder->Reset();
432         fVertexFinder->Read(npoints,points);
433         fBenchmark->Stop("Vertex Finder Read"); 
434         fBenchmark->Start("Vertex Finder");
435         fVertexFinder->Analyze();
436         AliL3VertexData vertex[1];
437         fVertexFinder->Write(vertex);
438         fVertex->Read(vertex);
439         fBenchmark->Stop("Vertex Finder"); 
440       }
441       else{
442         //use 0,0,0 for vertex
443         fVertex->SetZero();
444       }
445       fTrackMerger->SetVertex(fVertex);
446     }
447     fTracker->InitSector(slice,fRow[patch]);//,fEta);
448     fTracker->SetVertex(fVertex);
449     fBenchmark->Start("Tracker Read Hits");
450     fTracker->ReadHits(npoints,points);
451     fBenchmark->Stop("Tracker Read Hits");
452     fBenchmark->Start("MainVertexTracking A"); 
453     fTracker->MainVertexTracking();
454     //fTracker->MainVertexTracking_a();
455     fBenchmark->Stop("MainVertexTracking A");
456     //fBenchmark->Start("MainVertexTracking B"); 
457     //fTracker->MainVertexTracking_b();
458     //fBenchmark->Stop("MainVertexTracking B");
459     fBenchmark->Start("Tracking fit");
460     if(fDoNonVertex)
461       fTracker->NonVertexTracking();//Do a second pass for nonvertex tracks
462     fTracker->FillTracks();
463     fBenchmark->Stop("Tracking fit");
464
465     if(fWriteOut) 
466        WriteSpacePoints(npoints, points, slice, patch); //do after Tracking
467
468     //free memory
469     if(UseCF)
470       memory->Free();
471     else
472       fFileHandler->Free();
473
474     UInt_t ntracks0 =0;
475     AliL3TrackSegmentData *trackdata0  = 
476          (AliL3TrackSegmentData *) memory->Allocate(fTracker->GetTracks());
477     memory->TrackArray2Memory(ntracks0,trackdata0,fTracker->GetTracks());
478     /*
479     //write tracks
480     if(fWriteOut){
481       sprintf(name,"%stracks_tr_%d_%d.raw",fWriteOutPath,slice,patch);
482       memory->SetBinaryOutput(name);
483       memory->Memory2Binary(ntracks0,trackdata0);
484       memory->CloseBinaryOutput();
485     }
486     */
487     fInterMerger->Reset();
488     fInterMerger->Init(fRow[patch],patch);
489
490     fInterMerger->FillTracks(ntracks0,trackdata0);
491     fBenchmark->Start("Inter Merger");
492     // fInterMerger->Merge();
493 //    fInterMerger->SlowMerge();
494     
495     fBenchmark->Stop("Inter Merger");
496     /*
497     //write inter merged tracks
498     if(fWriteOut){
499       sprintf(name,"%stracks_im_%d_%d.raw",fWriteOutPath,slice,patch);
500       WriteTracks(name,fInterMerger,'i'); //write output of intermerger
501       }
502     */
503     memory->Free();
504     
505     UInt_t ntracks1 =0;
506     AliL3TrackSegmentData *trackdata1 =
507       (AliL3TrackSegmentData *) memory->Allocate(fInterMerger->GetInTracks(0));
508     memory->TrackArray2Memory(ntracks1,trackdata1,fInterMerger->GetInTracks(0));
509
510     fTrackMerger->InitSector(slice,patch);
511     fTrackMerger->FillTracks(ntracks1,trackdata1);
512
513     memory->Free();
514   }
515   fBenchmark->Start("Patch Merger");
516   fTrackMerger->SlowMerge();
517   //fTrackMerger->AddAllTracks();
518   //fTrackMerger->Merge();
519   fBenchmark->Stop("Patch Merger");
520   /*
521   //write merged tracks
522   if(fWriteOut){
523     sprintf(name,"%stracks_tm_%d.raw",fWriteOutPath,slice);
524     WriteTracks(name,fTrackMerger,'o'); //write output of trackmerger
525   }
526   */
527   fTrackData = (AliL3TrackSegmentData *) 
528                          fFileHandler->Allocate(fTrackMerger->GetOutTracks());
529
530   fFileHandler->TrackArray2Memory(fNTrackData,fTrackData,
531                                                 fTrackMerger->GetOutTracks());
532
533   delete memory;
534 }
535
536 void AliLevel3::WriteSpacePoints(UInt_t npoints,AliL3SpacePointData *points,
537                                                       Int_t slice,Int_t patch){
538   char name[256];
539   sprintf(name,"%spoints_%d_%d.raw",fWriteOutPath,slice,patch);
540   AliL3MemHandler * memory = new AliL3MemHandler();
541   memory->SetBinaryOutput(name);
542   memory->Transform(npoints,points,slice);
543   memory->Memory2Binary(npoints,points);
544   memory->CloseBinaryOutput();
545   delete  memory;
546 }
547
548 Int_t AliLevel3::WriteTracks(char *filename,AliL3Merger *merger,char opt){
549   AliL3MemHandler *memory = new AliL3MemHandler();
550   memory->SetBinaryOutput(filename);
551   if(opt=='a'||opt=='i'){  //add intracks
552     for(Int_t i=0;i<merger->GetNIn();i++){
553       AliL3TrackArray *tr=merger->GetInTracks(i);
554       memory->TrackArray2Binary(tr);
555     }
556   }
557
558   if(opt=='o'||opt=='a'){
559     AliL3TrackArray *tr=merger->GetOutTracks();
560     memory->TrackArray2Binary(tr);
561   }
562
563   memory->CloseBinaryOutput();
564  
565   return 1;
566 }
567
568 void AliLevel3::WriteResults()
569 {
570   //Write the resulting tracks to outputfile
571   Char_t fname[256];
572   sprintf(fname,"%stracks.raw",fWriteOutPath);
573   WriteTracks(fname,fGlobalMerger,'a');
574   //WriteTracks("tracks.raw",fGlobalMerger,'a');
575   sprintf(fname,"%stracks_gl.raw",fWriteOutPath);
576   WriteTracks(fname,fGlobalMerger,'o');
577   //WriteTracks("tracks_gl.raw",fGlobalMerger,'o');
578 }