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