4c66ae5a383262ae38f0e289662cdcd5e2402e90
[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   fWriteOut = kFALSE;
103   fGlobalMerger=0;
104   fTransformer = new AliL3Transform(path);
105   fDoRoi = kFALSE;
106   fDoNonVertex = kFALSE;
107   fClusterDeconv = kTRUE;
108   fEta[0] = 0.;
109   fEta[1] = 0.9;
110   fUseBinary = binary;
111   SetPath(path);
112   fFindVertex =kFALSE;
113   fEvent=0;
114
115   switch(npatches){
116   case 1:
117     fNPatch = 1;        //number of patches change row in process
118     fRow[0][0] = 0;     // first row
119     fRow[0][1] = 175;   // last row
120     break;
121   case 2:
122     fNPatch = 2;        //number of patches change row in process
123     fRow[0][0] = 0;     // first row
124     fRow[0][1] = 54;
125     fRow[1][0] = 55;
126     fRow[1][1] = 175;   // last row
127     break;
128   case 5:
129     fNPatch = 5;        //number of patches change row in process
130     fRow[0][0] = 0;     // first row
131     fRow[0][1] = 45;
132     fRow[1][0] = 46;
133     fRow[1][1] = 77;
134     fRow[2][0] = 78;
135     fRow[2][1] = 109;
136     fRow[3][0] = 110; 
137     fRow[3][1] = 141;
138     fRow[4][0] = 142;
139     fRow[4][1] = 175;   // last row
140     break;
141   default:
142     fNPatch = 6;        //number of patches change row in process
143     fRow[0][0] = 0;     // first row
144     fRow[0][1] = 31;
145     fRow[1][0] = 32;
146     fRow[1][1] = 63;
147     fRow[2][0] = 64;
148     fRow[2][1] = 91;
149     fRow[3][0] = 92;
150     fRow[3][1] = 119;
151     fRow[4][0] = 120;
152     fRow[4][1] = 143;   
153     fRow[5][0] = 144;
154     fRow[5][1] = 175;   // last row 
155   }
156
157   fVertexFinder = new AliL3VertexFinder();
158   fVertex = new AliL3Vertex();
159   fTracker = new AliL3ConfMapper();
160   fTrackMerger = new AliL3TrackMerger(fNPatch);
161   fInterMerger = new AliL3InterMerger();
162 #ifdef use_aliroot
163   fFileHandler = new AliL3FileHandler();
164   fFileHandler->SetAliInput(fInputFile);
165 #else
166   fFileHandler = new AliL3MemHandler();
167 #endif
168   fBenchmark = new AliL3Benchmark();
169 }
170
171 void AliLevel3::DoBench(char* name){
172   fBenchmark->Analyze(name);
173 }
174
175 void AliLevel3::DoMc(char* file){
176 #ifdef use_aliroot
177   if(!fFileHandler->IsDigit())
178     fFileHandler->SetMCOutput(file);
179 #endif
180 }
181
182 AliLevel3::~AliLevel3(){
183   //Destructor
184   if(fVertexFinder)  delete fVertexFinder;
185   if(fVertex)  delete fVertex;
186   if(fTracker) delete fTracker;
187   if(fTransformer) delete fTransformer;
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->SetTransformer(fTransformer);
228     fGlobalMerger->InitSlice(i);
229     fGlobalMerger->FillTracks(fNTrackData,fTrackData);
230     fFileHandler->Free();   //free the memory
231     fNTrackData=0;
232     fTrackData=0;
233   }
234   fBenchmark->Start("Global Merger");
235   //fGlobalMerger->AddAllTracks();
236   //fGlobalMerger->Merge();
237   fGlobalMerger->SlowMerge();
238   fBenchmark->Stop("Global Merger");
239
240   if(fWriteOut) WriteResults(); 
241   delete fGlobalMerger; fGlobalMerger = 0;
242 }
243
244 void AliLevel3::ProcessSlice(Int_t slice){
245   char name[256];
246   Bool_t UseCF = kFALSE;
247 #ifdef use_aliroot
248   UseCF = fFileHandler->IsDigit();
249 #else
250   if(fUseBinary)
251     UseCF = kTRUE; //In case you are not using aliroot
252 #endif
253   const Int_t maxpoints=100000;
254   const Int_t pointsize = maxpoints * sizeof(AliL3SpacePointData);
255   AliL3MemHandler *memory = new AliL3MemHandler();
256   
257   fTrackMerger->Reset();
258   fTrackMerger->SetTransformer(fTransformer);
259   fTrackMerger->SetRows(fRow[0]);
260   for(Int_t patch=fNPatch-1;patch>=0;patch--){
261     fFileHandler->Init(slice,patch,fRow[patch]);
262     fFileHandler->SetTransformer(fTransformer);
263     UInt_t npoints=0;
264     AliL3SpacePointData *points =0;
265     UInt_t ndigits=0;
266     AliL3DigitRowData *digits =0;
267     if(UseCF){
268       if(fUseBinary){
269         if(!fDoRoi){ 
270           if(1){     //Binary to Memory
271             fFileHandler->Free();
272             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
273             if(!fFileHandler->SetBinaryInput(name)) return;
274             digits= (AliL3DigitRowData *)fFileHandler->CompBinary2Memory(ndigits);
275             fFileHandler->CloseBinaryInput(); 
276           }
277
278           if(0){     //Binary to Memory with Benchmark 
279             fFileHandler->Free();
280             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
281             if(!memory->SetBinaryInput(name)) return;
282             UInt_t compsize=memory->GetFileSize();
283             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
284             memory->CompBinary2CompMemory(ndigits,comp);
285             memory->CloseBinaryInput();
286             UInt_t datasize=memory->GetMemorySize(ndigits,comp);
287             digits=(AliL3DigitRowData *)fFileHandler->Allocate(datasize);
288             fBenchmark->Start("Unpacker"); 
289             fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
290             fBenchmark->Stop("Unpacker");
291             memory->Free();
292           }
293   
294           if(0){     //Binary to Memory with Random
295             fFileHandler->Free();
296             fFileHandler->ResetRandom();
297             fFileHandler->SetRandomCluster(100);
298             fFileHandler->SetNGenerate(100);
299             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
300             if(!memory->SetBinaryInput(name)) return;
301             UInt_t compsize=memory->GetFileSize();
302             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
303             memory->CompBinary2CompMemory(ndigits,comp);
304             memory->CloseBinaryInput();
305             UInt_t dsize=memory->GetMemorySize(ndigits,comp);
306             UInt_t rsize=fFileHandler->GetRandomSize();       
307             digits=(AliL3DigitRowData*)fFileHandler->Allocate(dsize+rsize);
308             fBenchmark->Start("Unpacker");
309             fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
310             fBenchmark->Stop("Unpacker");
311             memory->Free();
312           }
313         }
314
315         else{     //Binary to Memory with Roi
316           fFileHandler->Free();
317           Int_t sli[2]={0,0};
318           fFileHandler->SetROI(fEta,sli);
319           sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,patch);
320           if(!memory->SetBinaryInput(name)) return;
321           UInt_t compsize=memory->GetFileSize();
322           UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
323           memory->CompBinary2CompMemory(ndigits,comp);
324           memory->CloseBinaryInput();
325           UInt_t datasize=memory->GetMemorySize(ndigits,comp);
326           digits=(AliL3DigitRowData *)fFileHandler->Allocate(datasize);
327           fBenchmark->Start("Unpacker"); 
328           datasize = fFileHandler->CompMemory2Memory(ndigits,digits,comp); 
329           fBenchmark->Stop("Unpacker"); 
330           memory->Free();
331         }
332       }//end UseBinary
333       else{
334 #ifdef use_aliroot
335         fBenchmark->Start("Dummy Unpacker");
336         sprintf(name,"digits_%d_%d.raw",slice,patch);
337         fBenchmark->Stop("Dummy Unpacker");
338
339         if(0){    //Ali to Binary
340           fFileHandler->SetBinaryOutput(name);
341           fFileHandler->AliDigits2CompBinary();
342           fFileHandler->CloseBinaryOutput();
343         }
344   
345         if(1){     //Ali to Memory
346           digits=(AliL3DigitRowData *)fFileHandler->AliDigits2Memory(ndigits,fEvent);
347           if(fWriteOut){   //Memory to Binary
348             fFileHandler->SetBinaryOutput(name);
349             fFileHandler->Memory2CompBinary(ndigits,digits);
350             fFileHandler->CloseBinaryOutput();
351           }
352         }
353 #endif
354       }//end else UseBinary
355
356       points = (AliL3SpacePointData *) memory->Allocate(pointsize);
357   
358       fClusterFinder = new AliL3ClustFinderNew(fTransformer);
359       fClusterFinder->InitSlice(slice,patch,fRow[patch][0],fRow[patch][1]
360                                 ,maxpoints);
361       fClusterFinder->SetDeconv(fClusterDeconv);
362       fClusterFinder->SetXYError(0.2);
363       fClusterFinder->SetZError(0.3);
364       fClusterFinder->SetOutputArray(points);
365       fClusterFinder->Read(ndigits,digits);
366       fBenchmark->Start("Cluster Finder");
367       fClusterFinder->ProcessDigits();
368       fBenchmark->Stop("Cluster Finder");
369       npoints = fClusterFinder->GetNumberOfClusters();
370       delete fClusterFinder;
371       fClusterFinder =0;
372       fFileHandler->Free();
373       LOG(AliL3Log::kInformational,"AliLevel3::ProcessSlice","Cluster Finder")
374         <<AliL3Log::kDec<<"Found "<<npoints<<" Points"<<ENDLOG;
375     
376     
377     }//end UseCF
378     else{// if not use Clusterfinder
379       if(fUseBinary){//Binary to Memory
380         memory->Free();
381         sprintf(name,"/%spoints_%d_%d.raw",fPath,slice,patch);
382         if(!memory->SetBinaryInput(name)) return;
383         points = (AliL3SpacePointData *) memory->Allocate();
384         memory->Binary2Memory(npoints,points);
385         memory->CloseBinaryInput();
386         LOG(AliL3Log::kInformational,"AliLevel3::ProcessSlice","Read Cluster")
387         <<AliL3Log::kDec<<"Found "<<npoints<<" Points in File"<<ENDLOG;
388       }
389 #ifdef use_aliroot
390       else{
391         points = fFileHandler->AliPoints2Memory(npoints);
392       }
393 #endif
394       fBenchmark->Start("Dummy Unpacker");
395       fBenchmark->Stop("Dummy Unpacker");
396       fBenchmark->Start("Dummy CF");
397       fBenchmark->Stop("Dummy CF");
398     }
399    
400     if(patch == fNPatch-1){
401       // Vertex
402       if(fFindVertex){
403       // Vertex Finder
404       
405         fBenchmark->Start("Vertex Finder Read");
406         fVertexFinder->Reset();
407         fVertexFinder->Read(npoints,points);
408         fBenchmark->Stop("Vertex Finder Read"); 
409         fBenchmark->Start("Vertex Finder");
410         fVertexFinder->Analyze();
411         AliL3VertexData vertex[1];
412         fVertexFinder->Write(vertex);
413         fVertex->Read(vertex);
414         fBenchmark->Stop("Vertex Finder"); 
415       }
416       else{
417         //use 0,0,0 for vertex
418         fVertex->SetZero();
419       }
420       fTrackMerger->SetVertex(fVertex);
421     }
422     fTracker->InitSector(slice,fRow[patch]);//,fEta);
423     fTracker->SetVertex(fVertex);
424     fBenchmark->Start("Tracker Read Hits");
425     fTracker->ReadHits(npoints,points);
426     fBenchmark->Stop("Tracker Read Hits");
427     fBenchmark->Start("MainVertexTracking A"); 
428     fTracker->MainVertexTracking();
429     //fTracker->MainVertexTracking_a();
430     fBenchmark->Stop("MainVertexTracking A");
431     //fBenchmark->Start("MainVertexTracking B"); 
432     //fTracker->MainVertexTracking_b();
433     //fBenchmark->Stop("MainVertexTracking B");
434     fBenchmark->Start("Tracking fit");
435     if(fDoNonVertex)
436       fTracker->NonVertexTracking();//Do a second pass for nonvertex tracks
437     fTracker->FillTracks();
438     fBenchmark->Stop("Tracking fit");
439
440     if(fWriteOut) 
441        WriteSpacePoints(npoints, points, slice, patch); //do after Tracking
442
443     //free memory
444     if(UseCF)
445       memory->Free();
446     else
447       fFileHandler->Free();
448
449     UInt_t ntracks0 =0;
450     AliL3TrackSegmentData *trackdata0  = 
451          (AliL3TrackSegmentData *) memory->Allocate(fTracker->GetTracks());
452     memory->TrackArray2Memory(ntracks0,trackdata0,fTracker->GetTracks());
453     /*
454     //write tracks
455     if(fWriteOut){
456       sprintf(name,"%stracks_tr_%d_%d.raw",fWriteOutPath,slice,patch);
457       memory->SetBinaryOutput(name);
458       memory->Memory2Binary(ntracks0,trackdata0);
459       memory->CloseBinaryOutput();
460     }
461     */
462     fInterMerger->Reset();
463     fInterMerger->SetTransformer(fTransformer);
464     fInterMerger->Init(fRow[patch],patch);
465
466     fInterMerger->FillTracks(ntracks0,trackdata0);
467     fBenchmark->Start("Inter Merger");
468     // fInterMerger->Merge();
469 //    fInterMerger->SlowMerge();
470     
471     fBenchmark->Stop("Inter Merger");
472     /*
473     //write inter merged tracks
474     if(fWriteOut){
475       sprintf(name,"%stracks_im_%d_%d.raw",fWriteOutPath,slice,patch);
476       WriteTracks(name,fInterMerger,'i'); //write output of intermerger
477       }
478     */
479     memory->Free();
480     
481     UInt_t ntracks1 =0;
482     AliL3TrackSegmentData *trackdata1 =
483       (AliL3TrackSegmentData *) memory->Allocate(fInterMerger->GetInTracks(0));
484     memory->TrackArray2Memory(ntracks1,trackdata1,fInterMerger->GetInTracks(0));
485
486     fTrackMerger->InitSector(slice,patch);
487     fTrackMerger->FillTracks(ntracks1,trackdata1);
488
489     memory->Free();
490   }
491   fBenchmark->Start("Patch Merger");
492 //  fTrackMerger->SlowMerge();
493   //fTrackMerger->AddAllTracks();
494   fTrackMerger->Merge();
495   fBenchmark->Stop("Patch Merger");
496   /*
497   //write merged tracks
498   if(fWriteOut){
499     sprintf(name,"%stracks_tm_%d.raw",fWriteOutPath,slice);
500     WriteTracks(name,fTrackMerger,'o'); //write output of trackmerger
501   }
502   */
503   fTrackData = (AliL3TrackSegmentData *) 
504                          fFileHandler->Allocate(fTrackMerger->GetOutTracks());
505
506   fFileHandler->TrackArray2Memory(fNTrackData,fTrackData,
507                                                 fTrackMerger->GetOutTracks());
508
509   delete memory;
510 }
511
512 void AliLevel3::WriteSpacePoints(UInt_t npoints,AliL3SpacePointData *points,
513                                                       Int_t slice,Int_t patch){
514   char name[256];
515   sprintf(name,"%spoints_%d_%d.raw",fWriteOutPath,slice,patch);
516   AliL3MemHandler * memory = new AliL3MemHandler();
517   memory->SetTransformer(fTransformer);
518   memory->SetBinaryOutput(name);
519   memory->Transform(npoints,points,slice);
520   memory->Memory2Binary(npoints,points);
521   memory->CloseBinaryOutput();
522   delete  memory;
523 }
524
525
526 Int_t AliLevel3::WriteTracks(char *filename,AliL3Merger *merger,char opt){
527   AliL3MemHandler *memory = new AliL3MemHandler();
528   memory->SetBinaryOutput(filename);
529   if(opt=='a'||opt=='i'){  //add intracks
530     for(Int_t i=0;i<merger->GetNIn();i++){
531       AliL3TrackArray *tr=merger->GetInTracks(i);
532       memory->TrackArray2Binary(tr);
533     }
534   }
535
536   if(opt=='o'||opt=='a'){
537     AliL3TrackArray *tr=merger->GetOutTracks();
538     memory->TrackArray2Binary(tr);
539   }
540
541   memory->CloseBinaryOutput();
542  
543   return 1;
544 }
545
546 void AliLevel3::WriteResults()
547 {
548   //Write the resulting tracks to outputfile
549   Char_t fname[256];
550   sprintf(fname,"%stracks.raw",fWriteOutPath);
551   WriteTracks(fname,fGlobalMerger,'a');
552   //WriteTracks("tracks.raw",fGlobalMerger,'a');
553   sprintf(fname,"%stracks_gl.raw",fWriteOutPath);
554   WriteTracks(fname,fGlobalMerger,'o');
555   //WriteTracks("tracks_gl.raw",fGlobalMerger,'o');
556 }