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