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