- added TPCLib to doxygen docu, code corrections according to documentation and effC++
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPC.cxx
1 // @(#) $Id$
2 // Original: AliLevel3.cxx,v 1.42 2004/06/11 16:06:33 loizides 
3
4 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>, Uli Frankenfeld <mailto:franken@fi.uib.no>
5 //*-- Copyright &copy ALICE HLT Group
6
7 #ifndef no_root
8 #include <TFile.h>
9 #include <TDirectory.h>
10 #include <TClonesArray.h>
11 #include <TStopwatch.h>
12 #endif
13
14 #ifdef use_newio
15 #include <AliRunLoader.h>
16 #endif
17
18 #include "AliHLTTPCLogging.h"
19 #include "AliHLTTPC.h"
20 #include "AliHLTTPCConfMapper.h"
21 #include "AliHLTTPCVertex.h"
22 #include "AliHLTTPCVertexFinder.h"
23 #include "AliHLTTPCTrackMerger.h"
24 #include "AliHLTTPCGlobalMerger.h"
25 #include "AliHLTTPCInterMerger.h"
26 #include "AliHLTTPCConfMapPoint.h"
27 #include "AliHLTTPCConfMapTrack.h"
28 #include "AliHLTTPCTransform.h"
29 #include "AliHLTTPCClusterFinder.h"
30 #include "AliHLTTPCDigitReaderUnpacked.h"
31 #include "AliHLTTPCDigitData.h"
32 #include "AliHLTTPCTrackArray.h"
33 #include "AliHLTTPCMemHandler.h"
34 #include "AliHLTTPCFitter.h"
35 #ifdef use_aliroot
36 #include "AliHLTTPCFileHandler.h"
37 #endif
38 #include "AliHLTTPCBenchmark.h"
39 #include "AliHLTTPCDigitData.h"
40 #include "AliHLTTPCTrackSegmentData.h"
41 #include "AliHLTTPCSpacePointData.h"
42 #include "AliHLTTPCVertexData.h"
43 #include "AliHLTTPCDDLDataFileHandler.h"
44
45 /** \class AliHLTTPC
46 <pre>
47 //_____________________________________________________________
48 //
49 //  AliHLTTPC
50 //
51 //  Interface class for HLTTPC tracker.
52 //  For example how to use, see exa/runtracker.C (root)
53 //  or programs/runtracker.cxx (standalone program).
54 //Begin_Html 
55 //<img src="tpcsectorsnb.gif">
56 //End_Html
57 </pre>
58 */
59
60 ClassImp(AliHLTTPC)
61
62 AliHLTTPC::AliHLTTPC()
63 {
64   //Default constructor. Should also be used when input is from binary files.
65   //In that case the path to where the binary files are located has to be
66   //passed to the AliLevel::Init function.
67   
68   fVertexFinder=0;
69   fVertex=0;
70   fTracker=0;
71   fTrackMerger=0;
72   fInterMerger=0;
73   fFileHandler=0;
74   fGlobalMerger=0;
75   fInputFile=0;
76 #ifdef use_newio
77   fRunLoader=0;
78 #endif
79 }
80
81 AliHLTTPC::AliHLTTPC(Char_t *infile)
82 {
83   //Constructor to use for when input is anything else but binary files,
84   //meaning rootfiles or raw files.
85   
86   fVertexFinder=0;
87   fVertex=0;
88   fTracker=0;
89   fTrackMerger=0;
90   fInterMerger=0;
91   fFileHandler=0;
92   fGlobalMerger=0;
93   fInputFile = infile;
94 #ifdef use_newio
95   fRunLoader=0;
96 #endif
97 }
98
99 #ifdef use_newio
100 AliHLTTPC::AliHLTTPC(AliRunLoader *rl)
101 {
102   //Constructor to use when input is aliroot runloader
103   fVertexFinder=0;
104   fVertex=0;
105   fTracker=0;
106   fTrackMerger=0;
107   fInterMerger=0;
108   fFileHandler=0;
109   fGlobalMerger=0;
110   fInputFile=0;
111   fRunLoader = rl;
112 }
113 #endif
114
115 Bool_t AliHLTTPC::fgDoVertexFit = kTRUE;//Include the vertex in the final track fit
116
117 void AliHLTTPC::Init(Char_t *path,EFileType filetype,Int_t npatches)
118 {
119   //Init the whole standard tracker chain
120 #ifndef use_newio
121   if (filetype==kRunLoader){
122     LOG(AliHLTTPCLog::kError,"AliHLTTPC::Init","Files")
123         <<"You have not supplied the input rootfile; if you want "
124         <<"to run with RunLoader use -Duse_newio for compiling!"<<ENDLOG;
125   }
126 #endif
127
128   if((filetype!=kBinary) && (filetype!=kDate) 
129        && (filetype!=kRunLoader)&& !fInputFile)
130     {
131       LOG(AliHLTTPCLog::kError,"AliHLTTPC::Init","Files")
132         <<"You have not supplied the input rootfile; use the appropriate ctor!"<<ENDLOG;
133       return;
134     }
135 #if use_newio
136   if((filetype==kRunLoader) && !fRunLoader)
137     {
138       LOG(AliHLTTPCLog::kError,"AliHLTTPC::Init","Files")
139         <<"You have not supplied the input runloader; use the appropriate ctor!"<<ENDLOG;
140       return;
141     }
142 #endif
143   
144   fWriteOut = kFALSE;
145   fPileUp = kFALSE;
146   fNoCF=kFALSE;
147   fUseBinary = (filetype==kBinary);
148   SetPath(path);
149   
150   fDoRoi = kFALSE;
151   fDoNonVertex = kFALSE;
152   fFindVertex = kFALSE;
153   SetClusterFinderParam();
154
155   fEta[0] = 0.;
156   fEta[1] = 1.1;
157
158   fEvent=0;
159 #ifdef use_aliroot /*just to be sure*/
160   AliHLTTPCFileHandler::CleanStaticIndex();
161 #endif
162
163   switch(npatches){
164   case 0:
165     fNPatch = 1;
166     fRow[0][0] = AliHLTTPCTransform::GetFirstRow(3);
167     fRow[0][1] = AliHLTTPCTransform::GetLastRow(5);
168     break;
169   case 1:
170     fNPatch = 1;        //number of patches change row in process
171     fRow[0][0] = 0;
172     fRow[0][1] = AliHLTTPCTransform::GetLastRow(-1);
173     break;
174   case 2:
175     fNPatch = 2;        //number of patches change row in process
176     fRow[0][0] = 0;     // first row
177     fRow[0][1] = AliHLTTPCTransform::GetLastRow(1);
178     fRow[1][0] = AliHLTTPCTransform::GetFirstRow(2);
179     fRow[1][1] = AliHLTTPCTransform::GetLastRow(5);
180     break;
181   default: 
182     fNPatch = 6;        
183     fRow[0][0] = AliHLTTPCTransform::GetFirstRow(0);
184     fRow[0][1] = AliHLTTPCTransform::GetLastRow(0);
185     fRow[1][0] = AliHLTTPCTransform::GetFirstRow(1);
186     fRow[1][1] = AliHLTTPCTransform::GetLastRow(1);
187     fRow[2][0] = AliHLTTPCTransform::GetFirstRow(2);
188     fRow[2][1] = AliHLTTPCTransform::GetLastRow(2);
189     fRow[3][0] = AliHLTTPCTransform::GetFirstRow(3);
190     fRow[3][1] = AliHLTTPCTransform::GetLastRow(3);
191     fRow[4][0] = AliHLTTPCTransform::GetFirstRow(4);
192     fRow[4][1] = AliHLTTPCTransform::GetLastRow(4);
193     fRow[5][0] = AliHLTTPCTransform::GetFirstRow(5);
194     fRow[5][1] = AliHLTTPCTransform::GetLastRow(5);
195   }
196
197   fVertexFinder = new AliHLTTPCVertexFinder();
198   fVertex = new AliHLTTPCVertex();
199   fTracker = new AliHLTTPCConfMapper();
200   fTrackMerger = new AliHLTTPCTrackMerger(fNPatch);
201   fInterMerger = new AliHLTTPCInterMerger();
202   fGlobalMerger = new AliHLTTPCGlobalMerger();
203   SetMergerParameters();//Set default merger parameters
204 #ifdef use_aliroot
205   if(filetype==kRoot){
206     fFileHandler = new AliHLTTPCFileHandler(kTRUE); //static version
207     fFileHandler->SetAliInput(fInputFile);
208   }else if(filetype==kRaw){
209     fFileHandler = new AliHLTTPCDDLDataFileHandler();
210     fFileHandler->SetReaderInput(fInputFile);
211   }else if(filetype==kDate){
212     fFileHandler = new AliHLTTPCDDLDataFileHandler();
213     fFileHandler->SetReaderInput(fInputFile,-1);
214   }
215 #if use_newio
216   else if(filetype==kRunLoader){
217     fFileHandler = new AliHLTTPCFileHandler(kTRUE); //static version
218     fFileHandler->SetAliInput(fRunLoader);
219   }
220 #endif
221   else{
222     fFileHandler = new AliHLTTPCMemHandler();
223   }
224 #else
225   if(filetype==kRaw){
226     fFileHandler = new AliHLTTPCDDLDataFileHandler();
227     fFileHandler->SetReaderInput(fInputFile);
228   }else{
229     fFileHandler = new AliHLTTPCMemHandler();
230   }
231 #endif
232   fBenchmark = new AliHLTTPCBenchmark();
233 }
234
235 void AliHLTTPC::DoBench(char* name)
236
237   //dobench
238   fBenchmark->Analyze(name);
239   delete fBenchmark;
240   fBenchmark = new AliHLTTPCBenchmark();
241 }
242
243 void AliHLTTPC::DoMc(char* file)
244
245   //domc
246 #ifdef use_aliroot
247   if(!fFileHandler->IsDigit(fEvent))
248     fFileHandler->SetMCOutput(file);
249 #endif
250 }
251
252 AliHLTTPC::~AliHLTTPC()
253 {
254   //Destructor
255   if(fVertexFinder) delete fVertexFinder;
256   if(fVertex) delete fVertex;
257   if(fTracker) delete fTracker;
258   if(fTrackMerger) delete fTrackMerger;
259   if(fInterMerger) delete fInterMerger;
260   if(fFileHandler) delete fFileHandler;
261   if(fGlobalMerger) delete fGlobalMerger;
262 }
263
264 void AliHLTTPC::SetClusterFinderParam(Float_t fXYError, Float_t fZError, Bool_t deconv)
265
266   //set cluster finder parameter
267   fXYClusterError=fXYError;
268   fZClusterError=fZError;
269   fClusterDeconv=deconv;
270 }
271
272 void AliHLTTPC::SetTrackerParam(Int_t phi_segments, Int_t eta_segments,
273                                 Int_t trackletlength, Int_t tracklength,
274                                 Int_t rowscopetracklet, Int_t rowscopetrack,
275                                 Double_t min_pt_fit, Double_t maxangle,
276                                 Double_t goodDist, Double_t hitChi2Cut,
277                                 Double_t goodHitChi2, Double_t trackChi2Cut,
278                                 Int_t maxdist,Double_t maxphi,Double_t maxeta,
279                                Bool_t vertexconstraint)
280 {
281   //Set parameters input to the tracker
282   //If no arguments are given, default parameters will be used
283   
284   fTracker->SetNSegments(phi_segments,eta_segments);
285   fTracker->SetMaxDca(min_pt_fit);
286   fTracker->SetTrackCuts(hitChi2Cut,goodHitChi2,trackChi2Cut,maxdist,vertexconstraint);
287   fTracker->SetTrackletCuts(maxangle,goodDist,vertexconstraint);
288   if(vertexconstraint)
289     fTracker->MainVertexSettings(trackletlength,tracklength,rowscopetracklet,rowscopetrack,maxphi,maxeta);
290   else
291     fTracker->NonVertexSettings(trackletlength,tracklength,rowscopetracklet,rowscopetrack);
292   fTracker->InitVolumes();
293 }
294
295 void AliHLTTPC::SetMergerParameters(Double_t maxy,Double_t maxz,Double_t maxkappa,Double_t maxpsi,Double_t maxtgl)
296 {
297   //set global merger parameter
298   fGlobalMerger->SetParameter(maxy,maxz,maxkappa,maxpsi,maxtgl);
299 }
300
301 void AliHLTTPC::ProcessEvent(Int_t first,Int_t last,Int_t event)
302 {
303   //Do tracking on all slices in region [first,last]
304   //Slices numbering in TPC goes from 0-35, which means that one slice
305   //corresponds to inner+outer sector.E.g. slice 2 corresponds to
306   //inner=2 + outer=38.
307
308   fGlobalMerger->Setup(first,last);
309 #ifdef use_aliroot
310   if(fEvent!=event) AliHLTTPCFileHandler::CleanStaticIndex();
311 #endif
312   fEvent=event;
313   for(Int_t i=first; i<=last; i++){
314     ProcessSlice(i);
315     fGlobalMerger->SetVertex(fVertex);
316     fGlobalMerger->InitSlice(i);
317     fGlobalMerger->FillTracks(fNTrackData,fTrackData);
318     fFileHandler->Free();   //free the memory
319     fNTrackData=0;
320     fTrackData=0;
321   }
322   fBenchmark->Start("Global track merger");
323   //fGlobalMerger->AddAllTracks();
324   fGlobalMerger->Merge();
325   //fGlobalMerger->SlowMerge(fWriteOutPath);
326   fBenchmark->Stop("Global track merger");
327   
328   FitGlobalTracks();
329   
330   if(fWriteOut) WriteResults(); 
331   fFileHandler->FreeDigitsTree();
332 }
333
334 void AliHLTTPC::ProcessSlice(Int_t slice)
335 {
336   //process slice
337   char name[256];
338   Bool_t UseCF = kFALSE;
339 #ifdef use_aliroot
340   UseCF = fFileHandler->IsDigit(fEvent);
341 #endif
342   if(fUseBinary)
343     UseCF = kTRUE;   //In case you are not using aliroot
344   if(fNoCF == kTRUE) //In case you don't want to run with cluster finder
345     UseCF = kFALSE;
346
347   const Int_t kmaxpoints=120000;
348   const Int_t kpointsize = kmaxpoints * sizeof(AliHLTTPCSpacePointData);
349   AliHLTTPCMemHandler *memory = new AliHLTTPCMemHandler();
350
351   fTrackMerger->Reset();
352   fTrackMerger->SetRows(fRow[0]);
353   
354   for(Int_t patch=fNPatch-1;patch>=0;patch--){
355     fFileHandler->Init(slice,patch,&fRow[patch][0]);
356     UInt_t npoints=0;
357     AliHLTTPCSpacePointData *points =0;
358     UInt_t ndigits=0;
359     AliHLTTPCDigitRowData *digits =0;
360     UInt_t digitSize = 0;
361     if(UseCF){
362       if(fUseBinary) {
363         if(!fDoRoi) { 
364           if(1){     //Binary to Memory
365             fFileHandler->Free();
366             if(fNPatch == 1)
367               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,-1);
368             else
369               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,patch);
370             if(!fFileHandler->SetBinaryInput(name)) return;
371             if(fPileUp)
372               { //Read binary files which are not RLE
373                 digits = (AliHLTTPCDigitRowData*)fFileHandler->Allocate();
374                 fFileHandler->Binary2Memory(ndigits,digits, digitSize ); 
375                 digitSize = fFileHandler->GetFileSize();
376               }
377             else //Read RLE binary files
378                 {
379                 digits= (AliHLTTPCDigitRowData *)fFileHandler->CompBinary2Memory(ndigits, digitSize);
380                 }
381
382             fFileHandler->CloseBinaryInput(); 
383           }
384
385           if(0){     //Binary to Memory with Benchmark 
386             fFileHandler->Free();
387             if(fNPatch == 1)
388               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,-1);
389             else
390               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,patch);
391             if(!memory->SetBinaryInput(name)) return;
392             UInt_t compsize=memory->GetFileSize();
393             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
394             memory->CompBinary2CompMemory(ndigits,comp);
395             memory->CloseBinaryInput();
396             UInt_t datasize=memory->GetMemorySize(ndigits,comp);
397             digits=(AliHLTTPCDigitRowData *)fFileHandler->Allocate(datasize);
398             fBenchmark->Start("Unpacker"); 
399             fFileHandler->CompMemory2Memory(ndigits,digits,comp,digitSize); 
400             fBenchmark->Stop("Unpacker");
401             memory->Free();
402           }
403   
404           if(0){     //Binary to Memory with Random
405             fFileHandler->Free();
406             fFileHandler->ResetRandom();
407             fFileHandler->SetRandomCluster(100);
408             fFileHandler->SetNGenerate(100);
409             if(fNPatch == 1)
410               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,-1);
411             else
412               sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,patch);
413             if(!memory->SetBinaryInput(name)) return;
414             UInt_t compsize=memory->GetFileSize();
415             UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
416             memory->CompBinary2CompMemory(ndigits,comp);
417             memory->CloseBinaryInput();
418             UInt_t dsize=memory->GetMemorySize(ndigits,comp);
419             UInt_t rsize=fFileHandler->GetRandomSize();       
420             digits=(AliHLTTPCDigitRowData*)fFileHandler->Allocate(dsize+rsize);
421             fBenchmark->Start("Unpacker");
422             fFileHandler->CompMemory2Memory(ndigits,digits,comp,digitSize); 
423             fBenchmark->Stop("Unpacker");
424             memory->Free();
425           }
426         }
427
428         else{     //Binary to Memory with Roi
429           fFileHandler->Free();
430           Int_t sli[2]={0,0};
431           fFileHandler->SetROI(fEta,sli);
432           if(fNPatch==1)
433             sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,-1);
434           else
435             sprintf(name,"%sdigits_%d_%d_%d.raw",fPath,fEvent,slice,patch);
436           if(!memory->SetBinaryInput(name)) return;
437           UInt_t compsize=memory->GetFileSize();
438           UInt_t *comp=(UInt_t *)memory->Allocate(compsize);
439           memory->CompBinary2CompMemory(ndigits,comp);
440           memory->CloseBinaryInput();
441           UInt_t datasize=memory->GetMemorySize(ndigits,comp);
442           digits=(AliHLTTPCDigitRowData *)fFileHandler->Allocate(datasize);
443           fBenchmark->Start("Unpacker"); 
444           datasize = fFileHandler->CompMemory2Memory(ndigits,digits,comp,digitSize); 
445           fBenchmark->Stop("Unpacker"); 
446           memory->Free();
447         }
448       }//end UseBinary
449       else{
450 #ifdef use_aliroot
451         fBenchmark->Start("Dummy Unpacker");
452         if(fNPatch==1)
453           sprintf(name,"digits_%d_%d_%d.raw",fEvent,slice,-1);
454         else
455           sprintf(name,"digits_%d_%d_%d.raw",fEvent,slice,patch);
456         fBenchmark->Stop("Dummy Unpacker");
457
458         if(0){   //Ali to Binary
459           fFileHandler->SetBinaryOutput(name);
460           fFileHandler->AliDigits2CompBinary();
461           fFileHandler->CloseBinaryOutput();
462         }
463   
464         if(1){   //Ali to Memory
465           digits=(AliHLTTPCDigitRowData *)fFileHandler->AliAltroDigits2Memory(ndigits,fEvent);
466           if(0){ //Memory to Binary
467             fFileHandler->SetBinaryOutput(name);
468             fFileHandler->Memory2CompBinary(ndigits,digits);
469             fFileHandler->CloseBinaryOutput();
470           }
471         }
472 #endif
473       }//end else UseBinary
474
475       points = (AliHLTTPCSpacePointData *) memory->Allocate(kpointsize);
476       fClusterFinder = new AliHLTTPCClusterFinder();
477       fDigitReader = new AliHLTTPCDigitReaderUnpacked();
478       fClusterFinder->InitSlice(slice,patch,fRow[patch][0],fRow[patch][1],kmaxpoints);
479       fClusterFinder->SetDeconv(fClusterDeconv);
480       fClusterFinder->SetXYError(fXYClusterError);
481       fClusterFinder->SetZError(fZClusterError);
482       if((fXYClusterError>0)&&(fZClusterError>0))
483         fClusterFinder->SetCalcErr(kFALSE);
484       fClusterFinder->SetOutputArray(points);
485       fClusterFinder->SetReader( fDigitReader );
486       fBenchmark->Start("Cluster finder");
487       //fClusterFinder->Read(ndigits,digits);
488       fClusterFinder->Read( (void*)digits, (unsigned long) digitSize );
489       fClusterFinder->ProcessDigits();
490       fBenchmark->Stop("Cluster finder");
491       npoints = fClusterFinder->GetNumberOfClusters();
492       delete fClusterFinder;
493       fClusterFinder = 0;
494       fFileHandler->Free();
495       LOG(AliHLTTPCLog::kInformational,"AliHLTTPC::ProcessSlice","Cluster Finder")
496         <<AliHLTTPCLog::kDec<<"Found "<<npoints<<" Points"<<ENDLOG;
497     }//end UseCF
498     else{// if not use Clusterfinder
499       if(fUseBinary){//Binary to Memory
500         memory->Free();
501         if(fNPatch==1)
502           sprintf(name,"%s/points_%d_%d_%d.raw",fPath,fEvent,slice,-1);
503         else
504           sprintf(name,"%s/points_%d_%d_%d.raw",fPath,fEvent,slice,patch);
505         if(!memory->SetBinaryInput(name)) return;
506         points = (AliHLTTPCSpacePointData *) memory->Allocate();
507         memory->Binary2Memory(npoints,points);
508         memory->CloseBinaryInput();
509         LOG(AliHLTTPCLog::kInformational,"AliHLTTPC::ProcessSlice","Read Cluster")
510         <<AliHLTTPCLog::kDec<<"Found "<<npoints<<" Points in File"<<ENDLOG;
511       }
512 #ifdef use_aliroot
513       else{
514         points = fFileHandler->AliPoints2Memory(npoints);
515       }
516 #endif
517       fBenchmark->Start("Dummy Unpacker");
518       fBenchmark->Stop("Dummy Unpacker");
519       fBenchmark->Start("Dummy CF");
520       fBenchmark->Stop("Dummy CF");
521     }
522    
523     if(patch == fNPatch-1){
524       // Vertex
525       if(fFindVertex){
526       // Vertex Finder
527       
528         fBenchmark->Start("Vertex Finder Read");
529         fVertexFinder->Reset();
530         fVertexFinder->Read(npoints,points);
531         fBenchmark->Stop("Vertex Finder Read"); 
532         fBenchmark->Start("Vertex Finder");
533         fVertexFinder->Analyze();
534         AliHLTTPCVertexData vertex[1];
535         fVertexFinder->Write(vertex);
536         fVertex->Read(vertex);
537         fBenchmark->Stop("Vertex Finder"); 
538       }
539       else{
540         //use 0,0,0 for vertex
541         fVertex->SetZero();
542       }
543       fTrackMerger->SetVertex(fVertex);
544     }
545
546     fTracker->InitSector(slice,fRow[patch],fEta);
547     fTracker->SetVertex(fVertex);
548     fBenchmark->Start("Tracker setup"); 
549     fTracker->ReadHits(npoints,points);
550     fTracker->MainVertexTracking_a();
551     fBenchmark->Stop("Tracker setup");
552     fBenchmark->Start("Track follower");
553     fTracker->MainVertexTracking_b();
554     fBenchmark->Stop("Track follower");
555     if(fDoNonVertex)
556       fTracker->NonVertexTracking();//Do a second pass for nonvertex tracks
557     fBenchmark->Start("Sector track fitting");
558     fTracker->FillTracks();
559     fBenchmark->Stop("Sector track fitting");
560
561     if(fWriteOut) 
562        WriteSpacePoints(npoints, points, slice, patch); //do after Tracking
563
564     //free memory
565     if(UseCF)
566       memory->Free();
567     else
568       fFileHandler->Free();
569
570     UInt_t ntracks0 =0;
571     AliHLTTPCTrackSegmentData *trackdata0  = 
572          (AliHLTTPCTrackSegmentData *) memory->Allocate(fTracker->GetTracks());
573     memory->TrackArray2Memory(ntracks0,trackdata0,fTracker->GetTracks());
574
575     //write tracks
576     if(fWriteOut){
577       if(fNPatch==1)
578         sprintf(name,"%s/tracks_tr_%d_%d_%d.raw",fWriteOutPath,fEvent,slice,-1);
579       else
580         sprintf(name,"%s/tracks_tr_%d_%d_%d.raw",fWriteOutPath,fEvent,slice,patch);
581       memory->SetBinaryOutput(name);
582       memory->Memory2Binary(ntracks0,trackdata0);
583       memory->CloseBinaryOutput();
584     }
585
586     fInterMerger->Reset();
587     fInterMerger->Init(fRow[patch],patch);
588
589     fInterMerger->FillTracks(ntracks0,trackdata0);
590     
591     //fBenchmark->Start("Inter Merger");
592     // fInterMerger->Merge();
593     //    fInterMerger->SlowMerge();
594     
595     //fBenchmark->Stop("Inter Merger");
596     /*
597     //write inter merged tracks
598     if(fWriteOut){
599       sprintf(name,"%stracks_im_%d_%d.raw",fWriteOutPath,slice,patch);
600       WriteTracks(name,fInterMerger,'i'); //write output of intermerger
601       }
602     */
603     memory->Free();
604     
605     UInt_t ntracks1 =0;
606     AliHLTTPCTrackSegmentData *trackdata1 =
607       (AliHLTTPCTrackSegmentData *) memory->Allocate(fInterMerger->GetInTracks(0));
608     memory->TrackArray2Memory(ntracks1,trackdata1,fInterMerger->GetInTracks(0));
609
610     fTrackMerger->InitSector(slice,patch);
611     fTrackMerger->FillTracks(ntracks1,trackdata1);
612
613     memory->Free();
614   }
615   //fBenchmark->Start("Patch Merger");
616   //fTrackMerger->SlowMerge();
617   fTrackMerger->AddAllTracks();
618   //fTrackMerger->Merge();
619   //fBenchmark->Stop("Patch Merger");
620   /*
621   //write merged tracks
622   if(fWriteOut){
623     sprintf(name,"%stracks_tm_%d.raw",fWriteOutPath,slice);
624     WriteTracks(name,fTrackMerger,'o'); //write output of trackmerger
625   }
626   */
627   fTrackData = (AliHLTTPCTrackSegmentData *) 
628                          fFileHandler->Allocate(fTrackMerger->GetOutTracks());
629
630   fFileHandler->TrackArray2Memory(fNTrackData,fTrackData,
631                                                 fTrackMerger->GetOutTracks());
632
633   delete memory;
634 }
635
636 void AliHLTTPC::FitGlobalTracks()
637
638   //fit global tracks
639   AliHLTTPCFitter *fitter = new AliHLTTPCFitter(fVertex,AliHLTTPC::DoVertexFit());
640   if(fNPatch==1)
641     fitter->LoadClusters(fWriteOutPath,fEvent,kTRUE);
642   else
643     fitter->LoadClusters(fWriteOutPath,fEvent,kFALSE);
644   
645   fBenchmark->Start("Global track fitter");
646   AliHLTTPCTrackArray *tracks = fGlobalMerger->GetOutTracks();
647   for(Int_t i=0; i<tracks->GetNTracks(); i++)
648     {
649       AliHLTTPCTrack *tr = tracks->GetCheckedTrack(i);
650       if(!tr) continue;
651       fitter->FitHelix(tr);
652       tr->UpdateToFirstPoint();
653     }
654   fBenchmark->Stop("Global track fitter");
655   delete fitter;
656 }
657
658 void AliHLTTPC::WriteSpacePoints(UInt_t npoints,AliHLTTPCSpacePointData *points,
659                                  Int_t slice,Int_t patch) const
660
661   //write space points
662   char name[256];
663   if(fNPatch==1)
664     sprintf(name,"%s/points_%d_%d_%d.raw",fWriteOutPath,fEvent,slice,-1);
665   else
666     sprintf(name,"%s/points_%d_%d_%d.raw",fWriteOutPath,fEvent,slice,patch);
667   AliHLTTPCMemHandler * memory = new AliHLTTPCMemHandler();
668   memory->SetBinaryOutput(name);
669   memory->Transform(npoints,points,slice);
670   memory->Memory2Binary(npoints,points);
671   memory->CloseBinaryOutput();
672   delete  memory;
673 }
674
675 Int_t AliHLTTPC::WriteTracks(char *filename,AliHLTTPCMerger *merger,char opt) const
676
677   //write tracks
678   AliHLTTPCMemHandler *memory = new AliHLTTPCMemHandler();
679   memory->SetBinaryOutput(filename);
680   if(opt=='a'||opt=='i'){  //add intracks
681     for(Int_t i=0;i<merger->GetNIn();i++){
682       AliHLTTPCTrackArray *tr=merger->GetInTracks(i);
683       memory->TrackArray2Binary(tr);
684     }
685   }
686
687   if(opt=='o'||opt=='a'){
688     AliHLTTPCTrackArray *tr=merger->GetOutTracks();
689     memory->TrackArray2Binary(tr);
690   }
691
692   memory->CloseBinaryOutput();
693   delete memory;
694   return 1;
695 }
696
697 void AliHLTTPC::WriteResults()
698 {
699   //Write the resulting tracks to outputfile
700   Char_t fname[256];
701   sprintf(fname,"%s/tracks_%d.raw",fWriteOutPath,fEvent);
702   WriteTracks(fname,fGlobalMerger,'a');
703   //WriteTracks("tracks.raw",fGlobalMerger,'a');
704   sprintf(fname,"%s/tracks_gl_%d.raw",fWriteOutPath,fEvent);
705   WriteTracks(fname,fGlobalMerger,'o');
706   //WriteTracks("tracks_gl.raw",fGlobalMerger,'o');
707 }