]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSTrackSegmentMakerv1.cxx
Raw2SDigits() implemented
[u/mrichter/AliRoot.git] / PHOS / AliPHOSTrackSegmentMakerv1.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15 /* $Id$ */
16
17 /* History of cvs commits:
18  *
19  * $Log$
20  * Revision 1.80  2006/08/28 10:01:56  kharlov
21  * Effective C++ warnings fixed (Timur Pocheptsov)
22  *
23  * Revision 1.79  2006/04/25 12:41:15  hristov
24  * Moving non-persistent data to AliESDfriend (Yu.Belikov)
25  *
26  * Revision 1.78  2005/11/18 13:04:51  hristov
27  * Bug fix
28  *
29  * Revision 1.77  2005/11/17 23:34:36  hristov
30  * Corrected logics
31  *
32  * Revision 1.76  2005/11/17 22:29:12  hristov
33  * Faster version, no attempt to match tracks outside the PHOS acceptance
34  *
35  * Revision 1.75  2005/11/17 12:35:27  hristov
36  * Use references instead of objects. Avoid to create objects when they are not really needed
37  *
38  * Revision 1.74  2005/07/08 14:01:36  hristov
39  * Tracking in non-uniform nmagnetic field (Yu.Belikov)
40  *
41  * Revision 1.73  2005/05/28 14:19:05  schutz
42  * Compilation warnings fixed by T.P.
43  *
44  */
45
46 //_________________________________________________________________________
47 // Implementation version 1 of algorithm class to construct PHOS track segments
48 // Track segment for PHOS is list of 
49 //        EMC RecPoint + (possibly) CPV RecPoint
50 // To find TrackSegments we do the following: 
51 //  for each EMC RecPoints we look at
52 //   CPV RecPoints in the radious fRcpv. 
53 //  If there is such a CPV RecPoint, 
54 //   we make "Link" it is just indexes of EMC and CPV RecPoint and distance
55 //   between them in the PHOS plane. 
56 //  Then we sort "Links" and starting from the 
57 //   least "Link" pointing to the unassined EMC and CPV RecPoints assing them to 
58 //   new TrackSegment. 
59 // If there is no CPV RecPoint we make TrackSegment 
60 // consisting from EMC alone. There is no TrackSegments without EMC RecPoint.
61 //// In principle this class should be called from AliPHOSReconstructor, but 
62 // one can use it as well in standalone mode.
63 // Use  case:
64 //  root [0] AliPHOSTrackSegmentMakerv1 * t = new AliPHOSTrackSegmentMaker("galice.root", "tracksegmentsname", "recpointsname")
65 //  Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
66 //               // reads gAlice from header file "galice.root", uses recpoints stored in the branch names "recpointsname" (default = "Default")
67 //               // and saves recpoints in branch named "tracksegmentsname" (default = "recpointsname")                       
68 //  root [1] t->ExecuteTask()
69 //  root [3] t->SetTrackSegmentsBranch("max distance 5 cm")
70 //  root [4] t->ExecuteTask("deb all time") 
71 //                 
72 //*-- Author: Dmitri Peressounko (RRC Ki & SUBATECH) & Yves Schutz (SUBATECH) 
73 //
74
75 // --- ROOT system ---
76 #include "TTree.h"
77 #include "TBenchmark.h"
78
79 // --- Standard library ---
80 #include "Riostream.h"
81 // --- AliRoot header files ---
82 #include "AliPHOSGeometry.h"
83 #include "AliPHOSTrackSegmentMakerv1.h"
84 #include "AliPHOSTrackSegment.h"
85 #include "AliPHOSLink.h"
86 #include "AliPHOSGetter.h"
87 #include "AliESD.h"
88 #include "AliESDtrack.h"
89
90 ClassImp( AliPHOSTrackSegmentMakerv1) 
91
92
93 //____________________________________________________________________________
94 AliPHOSTrackSegmentMakerv1::AliPHOSTrackSegmentMakerv1() :
95   AliPHOSTrackSegmentMaker(),
96   fDefaultInit(kTRUE),
97   fWrite(kFALSE),
98   fNTrackSegments(0),
99   fRcpv(0.f),
100   fRtpc(0.f),
101   fLinkUpArray(0),
102   fEmcFirst(0),
103   fEmcLast(0),
104   fCpvFirst(0),
105   fCpvLast(0),
106   fModule(0),
107   fTrackSegmentsInRun(0)
108   
109 {
110   // default ctor (to be used mainly by Streamer)
111   InitParameters() ; 
112 }
113
114 //____________________________________________________________________________
115 AliPHOSTrackSegmentMakerv1::AliPHOSTrackSegmentMakerv1(const TString & alirunFileName, const TString & eventFolderName) :
116   AliPHOSTrackSegmentMaker(alirunFileName, eventFolderName),
117   fDefaultInit(kFALSE),
118   fWrite(kFALSE),
119   fNTrackSegments(0),
120   fRcpv(0.f),
121   fRtpc(0.f),
122   fLinkUpArray(0),
123   fEmcFirst(0),
124   fEmcLast(0),
125   fCpvFirst(0),
126   fCpvLast(0),
127   fModule(0),
128   fTrackSegmentsInRun(0)
129 {
130   // ctor
131   InitParameters() ; 
132   Init() ;
133   fESD = 0;
134 }
135
136
137 AliPHOSTrackSegmentMakerv1::AliPHOSTrackSegmentMakerv1(const AliPHOSTrackSegmentMakerv1 & tsm) :
138   AliPHOSTrackSegmentMaker(tsm),
139   fDefaultInit(kFALSE),
140   fWrite(kFALSE),
141   fNTrackSegments(0),
142   fRcpv(0.f),
143   fRtpc(0.f),
144   fLinkUpArray(0),
145   fEmcFirst(0),
146   fEmcLast(0),
147   fCpvFirst(0),
148   fCpvLast(0),
149   fModule(0),
150   fTrackSegmentsInRun(0)
151 {
152   // cpy ctor: no implementation yet
153   // requested by the Coding Convention
154   Fatal("cpy ctor", "not implemented") ;
155 }
156
157
158 //____________________________________________________________________________
159  AliPHOSTrackSegmentMakerv1::~AliPHOSTrackSegmentMakerv1()
160
161   // dtor
162   // fDefaultInit = kTRUE if TrackSegmentMaker created by default ctor (to get just the parameters)
163   if (!fDefaultInit)  
164     delete fLinkUpArray ;
165 }
166
167
168 //____________________________________________________________________________
169 const TString AliPHOSTrackSegmentMakerv1::BranchName() const 
170 {  
171  
172   return GetName() ;
173 }
174
175 //____________________________________________________________________________
176 void  AliPHOSTrackSegmentMakerv1::FillOneModule()
177 {
178   // Finds first and last indexes between which 
179   // clusters from one PHOS module are
180
181   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
182   
183   TObjArray * emcRecPoints = gime->EmcRecPoints() ; 
184   TObjArray * cpvRecPoints = gime->CpvRecPoints() ; 
185  
186   //First EMC clusters
187   Int_t totalEmc = emcRecPoints->GetEntriesFast() ;
188   for(fEmcFirst = fEmcLast; (fEmcLast < totalEmc) &&  
189         ((dynamic_cast<AliPHOSRecPoint *>(emcRecPoints->At(fEmcLast)))->GetPHOSMod() == fModule ); 
190       fEmcLast ++)  ;
191   
192   //Now CPV clusters
193   Int_t totalCpv = cpvRecPoints->GetEntriesFast() ;
194
195     for(fCpvFirst = fCpvLast; (fCpvLast < totalCpv) && 
196          ((dynamic_cast<AliPHOSRecPoint *>(cpvRecPoints->At(fCpvLast)))->GetPHOSMod() == fModule ); 
197        fCpvLast ++) ;
198       
199 }
200
201 //____________________________________________________________________________
202 Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * emcClu,AliPHOSCpvRecPoint * cpvClu, Int_t &trackindex) const
203 {
204   // Calculates the distance between the EMC RecPoint and the CPV RecPoint
205   // Clusters are sorted in "rows" and "columns" of width 1 cm
206
207   //Float_t delta = 1 ;  // Width of the rows in sorting of RecPoints (in cm)
208                        // if you change this value, change it as well in xxxRecPoint::Compare()
209   Float_t distance2Cpv   = fRcpv ;
210   Float_t distance2Track = fRtpc ; 
211
212   trackindex = -1 ; // closest track within fRCpv 
213
214   TVector3 vecEmc ;   // Local position of EMC recpoint
215   TVector3 vecCpv ;   // Local position of CPV recpoint propagated to EMC
216   TVector3 vecDist ;  // Distance between local positions of two points
217   
218   emcClu->GetLocalPosition(vecEmc) ;
219   cpvClu->GetLocalPosition(vecCpv) ;
220
221   //toofar = kTRUE ;
222   if(emcClu->GetPHOSMod() == cpvClu->GetPHOSMod()){ 
223
224     // Find EMC-CPV distance
225     distance2Cpv = (vecCpv - vecEmc).Mag() ;
226     
227     if (fESD != 0x0) {
228       AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
229       const AliPHOSGeometry * geom = gime->PHOSGeometry() ; 
230
231       Double_t rPHOS = geom->GetIPtoCrystalSurface();
232
233       //PH Acceptance boundaries for each PHOS module
234       Int_t nModules = geom->GetNModules();
235       Double_t * thmin = new Double_t[nModules];// theta min
236       Double_t * thmax = new Double_t[nModules];// theta max
237       Double_t * phmin = new Double_t[nModules];// phi min
238       Double_t * phmax = new Double_t[nModules];// phi max
239       
240       for (Int_t imod=0; imod<nModules; imod++) {
241         // Modules are numbered from 1 to 5 in AliPHOSGeometry
242         geom->EmcModuleCoverage(imod+1,
243                                 thmin[imod],thmax[imod],
244                                 phmin[imod],phmax[imod]);
245       }
246
247       // Extrapolate the global track direction if any to CPV and find the closest track
248       Int_t nTracks = fESD->GetNumberOfTracks();
249       Int_t iClosestTrack = -1;
250       Double_t minDistance = 1e6;
251       Double_t pxyz[3], xyz[3];
252       TVector3 inPHOS; //PH Used to calculate theta and phi
253
254       //PH Loop on tracks
255       AliESDtrack *track;
256       for (Int_t iTrack=0; iTrack<nTracks; iTrack++) {
257         track = fESD->GetTrack(iTrack);
258         if (!track->GetXYZAt(rPHOS, fESD->GetMagneticField(), xyz))
259            continue; //track coord on the cylinder of PHOS radius
260         if ((TMath::Abs(xyz[0])+TMath::Abs(xyz[1])+TMath::Abs(xyz[2]))<=0)
261            continue;
262         //PH Here one has to cut out the tracks which are not inside the PHOS
263         //PH acceptance
264         inPHOS.SetXYZ(xyz[0],xyz[1],xyz[2]);
265         Double_t inPhi = inPHOS.Phi();
266         Double_t inTheta = inPHOS.Theta();
267
268         Bool_t skip = kTRUE;
269         for (Int_t imod=0; imod<nModules; imod++) {
270           //PH Loop on modules to check if the track enters in the acceptance 
271           if (thmin[imod] < inTheta && thmax[imod] > inTheta && 
272               phmin[imod] < inPhi   && phmax[imod] > inPhi) {
273             skip = kFALSE;
274             break;
275           }
276         }
277         if (skip) continue; //PH Skip, if not in the PHOS acceptance
278
279         if (!track->GetPxPyPzAt(rPHOS, fESD->GetMagneticField(), pxyz))
280            continue; // track momentum ibid.
281         PropagateToPlane(vecDist,xyz,pxyz,"CPV",cpvClu->GetPHOSMod());
282         //      Info("GetDistanceInPHOSPlane","Track %d propagation to CPV = (%f,%f,%f)",
283         //     iTrack,vecDist.X(),vecDist.Y(),vecDist.Z());
284         vecDist -= vecCpv;
285         distance2Track = TMath::Sqrt(vecDist.X()*vecDist.X() + vecDist.Z()*vecDist.Z());
286         // Find the closest track to the EMC recpoint
287         if (distance2Track < minDistance) {
288           minDistance = distance2Track;
289           iClosestTrack = iTrack;
290         }
291       }
292
293       delete [] thmin;
294       delete [] thmax;
295       delete [] phmin;
296       delete [] phmax;
297
298       if (iClosestTrack != -1) {
299         track = fESD->GetTrack(iClosestTrack);
300         if (track->GetPxPyPzAt(rPHOS, fESD->GetMagneticField(), pxyz)) { // track momentum ibid.
301         TVector3 vecCpvGlobal; // Global position of the CPV recpoint
302         geom->GetGlobal((AliRecPoint*)cpvClu,vecCpvGlobal);
303         for (Int_t ixyz=0; ixyz<3; ixyz++)
304           xyz[ixyz] = vecCpvGlobal[ixyz];
305         PropagateToPlane(vecDist,xyz,pxyz,"EMC",cpvClu->GetPHOSMod());
306 //      Info("GetDistanceInPHOSPlane","Track %d propagation to EMC = (%f,%f,%f)",
307 //           iClosestTrack,vecDist.X(),vecDist.Y(),vecDist.Z());
308         vecDist -= vecEmc;
309         distance2Track = TMath::Sqrt(vecDist.X()*vecDist.X() + vecDist.Z()*vecDist.Z());
310         }
311       }
312 //     } else {
313 //       // If no ESD exists, than simply find EMC-CPV distance
314 //       distance = (vecCpv - vecEmc).Mag() ;
315     
316       //if(distance2Track < fRcpv + 2*delta )
317       if(distance2Track < fRtpc )
318         trackindex = iClosestTrack ; 
319       //      toofar = kFALSE ;
320     }
321     //     Info("GetDistanceInPHOSPlane","cpv-emc distance is %f cm",
322     //   distance);
323   }
324   
325   return distance2Cpv ;
326 }
327
328 //____________________________________________________________________________
329 void AliPHOSTrackSegmentMakerv1::PropagateToPlane(TVector3& globalIntersection,
330                                                   Double_t *x,
331                                                   Double_t *p,
332                                                   const char *det,
333                                                   Int_t moduleNumber) const
334 {
335   // Propagate a straight-line track from the origin point x
336   // along the direction p to the CPV or EMC module moduleNumber
337   // Returns a local position of such a propagation
338
339   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
340   const AliPHOSGeometry * geom = gime->PHOSGeometry() ; 
341   TVector3 moduleCenter;
342   geom->GetModuleCenter(moduleCenter,det,moduleNumber);
343   TVector3 vertex; vertex.SetXYZ(x[0],x[1],x[2]);
344   TVector3 direction; direction.SetXYZ(p[0],p[1],p[2]);
345
346 //   Info("PropagateToCPV","Center of the %s module %d is (%f,%f,%f)",
347 //        det,moduleNumber,moduleCenter[0],moduleCenter[1],moduleCenter[2]);
348
349   Double_t time = (moduleCenter.Mag2() - vertex.Dot(moduleCenter)) /
350     (direction.Dot(moduleCenter));
351   vertex += direction*time;
352   geom->Global2Local(globalIntersection,vertex,moduleNumber);
353 }
354
355 //____________________________________________________________________________
356 void  AliPHOSTrackSegmentMakerv1::Init()
357 {
358   // Make all memory allocations that are not possible in default constructor
359   
360   AliPHOSGetter* gime = AliPHOSGetter::Instance();
361   if(!gime)
362     gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName.Data());
363   
364   fLinkUpArray  = new TClonesArray("AliPHOSLink", 1000); 
365   if ( !gime->TrackSegmentMaker() ) {
366     gime->PostTrackSegmentMaker(this);
367   }
368 }
369
370 //____________________________________________________________________________
371 void  AliPHOSTrackSegmentMakerv1::InitParameters()
372 {
373   //Initializes parameters
374   fRcpv      = 10. ;
375   fRtpc      = 4. ;
376   fEmcFirst  = 0 ;    
377   fEmcLast   = 0 ;   
378   fCpvFirst  = 0 ;   
379   fCpvLast   = 0 ;   
380   fLinkUpArray = 0 ;
381   fWrite                   = kTRUE ;
382   fTrackSegmentsInRun       = 0 ; 
383   SetEventRange(0,-1) ;
384 }
385
386
387 //____________________________________________________________________________
388 void  AliPHOSTrackSegmentMakerv1::MakeLinks()const
389
390   // Finds distances (links) between all EMC and CPV clusters, 
391   // which are not further apart from each other than fRcpv 
392   // and sort them in accordance with this distance
393   
394   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
395   TObjArray * emcRecPoints = gime->EmcRecPoints() ; 
396   TObjArray * cpvRecPoints = gime->CpvRecPoints() ; 
397
398   fLinkUpArray->Clear() ;    
399
400   AliPHOSCpvRecPoint * cpv ;
401   AliPHOSEmcRecPoint * emcclu ;
402
403   Int_t iLinkUp  = 0 ;
404   
405   Int_t iEmcRP;
406   for(iEmcRP = fEmcFirst; iEmcRP < fEmcLast; iEmcRP++ ) {
407     emcclu = dynamic_cast<AliPHOSEmcRecPoint *>(emcRecPoints->At(iEmcRP)) ;
408
409     //Bool_t toofar ;        
410     Int_t iCpv = 0 ;    
411     for(iCpv = fCpvFirst; iCpv < fCpvLast;iCpv++ ) { 
412       
413       cpv = dynamic_cast<AliPHOSCpvRecPoint *>(cpvRecPoints->At(iCpv)) ;
414       Int_t track = -1 ; 
415       Float_t r = GetDistanceInPHOSPlane(emcclu, cpv, track) ;     
416       //      if(toofar)
417       //        continue ;       
418       if(r < fRcpv) { 
419         new ((*fLinkUpArray)[iLinkUp++])  AliPHOSLink(r, iEmcRP, iCpv, track) ;
420       }      
421     }
422   } 
423   
424   fLinkUpArray->Sort() ;  //first links with smallest distances
425 }
426
427 //____________________________________________________________________________
428 void  AliPHOSTrackSegmentMakerv1::MakePairs()
429
430   // Using the previously made list of "links", we found the smallest link - i.e. 
431   // link with the least distance between EMC and CPV and pointing to still 
432   // unassigned RecParticles. We assign these RecPoints to TrackSegment and 
433   // remove them from the list of "unassigned". 
434
435   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
436
437   TObjArray * emcRecPoints = gime->EmcRecPoints() ; 
438   TObjArray * cpvRecPoints = gime->CpvRecPoints() ; 
439   TClonesArray * trackSegments = gime->TrackSegments();
440     
441   //Make arrays to mark clusters already chosen
442   Int_t * emcExist = 0;
443   if(fEmcLast > fEmcFirst)
444     emcExist = new Int_t[fEmcLast-fEmcFirst] ;
445   
446   Int_t index;
447   for(index = 0; index <fEmcLast-fEmcFirst; index ++)
448     emcExist[index] = 1 ;
449   
450   Bool_t * cpvExist = 0;
451   if(fCpvLast > fCpvFirst)
452     cpvExist = new Bool_t[fCpvLast-fCpvFirst] ;
453   for(index = 0; index <fCpvLast-fCpvFirst; index ++)
454     cpvExist[index] = kTRUE ;
455   
456   
457   // Finds the smallest links and makes pairs of CPV and EMC clusters with smallest distance 
458   TIter nextUp(fLinkUpArray) ;
459   
460   AliPHOSLink * linkUp ;
461   
462   AliPHOSCpvRecPoint * nullpointer = 0 ;
463   
464   while ( (linkUp =  static_cast<AliPHOSLink *>(nextUp()) ) ){  
465
466     if(emcExist[linkUp->GetEmc()-fEmcFirst] != -1){
467
468       if(cpvExist[linkUp->GetCpv()-fCpvFirst]){ //CPV still exist
469          new ((* trackSegments)[fNTrackSegments]) 
470            AliPHOSTrackSegment(dynamic_cast<AliPHOSEmcRecPoint *>(emcRecPoints->At(linkUp->GetEmc())) , 
471                                dynamic_cast<AliPHOSCpvRecPoint *>(cpvRecPoints->At(linkUp->GetCpv())) , 
472                                linkUp->GetTrack()) ;
473          
474        (dynamic_cast<AliPHOSTrackSegment *>(trackSegments->At(fNTrackSegments)))->SetIndexInList(fNTrackSegments);
475        fNTrackSegments++ ;
476        emcExist[linkUp->GetEmc()-fEmcFirst] = -1 ; //Mark emc  that Cpv was found 
477        //mark CPV recpoint as already used 
478        cpvExist[linkUp->GetCpv()-fCpvFirst] = kFALSE ;
479       } //if CpvUp still exist
480     } 
481   }        
482
483   //look through emc recPoints left without CPV
484   if(emcExist){ //if there is emc rec point
485     Int_t iEmcRP ;
486     for(iEmcRP = 0; iEmcRP < fEmcLast-fEmcFirst  ; iEmcRP++ ){
487       if(emcExist[iEmcRP] > 0 ){
488        new ((*trackSegments)[fNTrackSegments])  
489          AliPHOSTrackSegment(dynamic_cast<AliPHOSEmcRecPoint *>(emcRecPoints->At(iEmcRP+fEmcFirst)), 
490                            nullpointer) ;
491        (dynamic_cast<AliPHOSTrackSegment *>(trackSegments->At(fNTrackSegments)))->SetIndexInList(fNTrackSegments);
492        fNTrackSegments++;    
493       } 
494     }
495   }
496   delete [] emcExist ; 
497   delete [] cpvExist ; 
498 }
499
500 //____________________________________________________________________________
501 void  AliPHOSTrackSegmentMakerv1::Exec(Option_t *option)
502 {
503   // Steering method to perform track segment construction for events
504   // in the range from fFirstEvent to fLastEvent.
505   // This range is optionally set by SetEventRange().
506   // if fLastEvent=-1 (by default), then process events until the end.
507   
508   if(strstr(option,"tim"))
509     gBenchmark->Start("PHOSTSMaker");
510  
511   if(strstr(option,"print")) {
512     Print() ; 
513     return ; 
514   }
515   
516   AliPHOSGetter * gime = AliPHOSGetter::Instance() ;  
517  
518   const AliPHOSGeometry * geom = gime->PHOSGeometry() ; 
519
520   if (fLastEvent == -1) 
521     fLastEvent = gime->MaxEvent() - 1 ;
522   else 
523     fLastEvent = TMath::Min(fFirstEvent,gime->MaxEvent());
524   Int_t nEvents   = fLastEvent - fFirstEvent + 1;
525
526   Int_t ievent ; 
527   for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
528     gime->Event(ievent,"R") ;
529    //Make some initializations 
530     fNTrackSegments = 0 ;
531     fEmcFirst = 0 ;    
532     fEmcLast  = 0 ;   
533     fCpvFirst = 0 ;   
534     fCpvLast  = 0 ;   
535     
536     gime->TrackSegments()->Clear();
537
538     //    if(!ReadRecPoints(ievent))   continue; //reads RecPoints for event ievent
539     
540     for(fModule = 1; fModule <= geom->GetNModules() ; fModule++ ) {
541       FillOneModule() ; 
542       MakeLinks() ;
543       MakePairs() ;
544     }
545
546     WriteTrackSegments() ;
547
548     if(strstr(option,"deb"))
549       PrintTrackSegments(option);
550     
551     //increment the total number of track segments per run 
552     fTrackSegmentsInRun += gime->TrackSegments()->GetEntriesFast() ; 
553   }
554   
555   if(strstr(option,"tim")){
556     gBenchmark->Stop("PHOSTSMaker");
557     Info("Exec", "took %f seconds for making TS %f seconds per event", 
558           gBenchmark->GetCpuTime("PHOSTSMaker"), 
559           gBenchmark->GetCpuTime("PHOSTSMaker")/nEvents) ;
560    }
561   if(fWrite) //do not unload in "on flight" mode
562     Unload();
563 }
564
565 //____________________________________________________________________________
566 void AliPHOSTrackSegmentMakerv1::Unload() 
567 {
568   // Unloads the task from the folder
569   AliPHOSGetter * gime = AliPHOSGetter::Instance() ;  
570   gime->PhosLoader()->UnloadRecPoints() ;
571   gime->PhosLoader()->UnloadTracks() ;
572 }
573
574 //____________________________________________________________________________
575 void AliPHOSTrackSegmentMakerv1::Print(const Option_t *)const
576 {
577   //  Print TrackSegmentMaker parameters
578
579   TString message("") ;
580   if( strcmp(GetName(), "") != 0 ) {
581     message = "\n======== AliPHOSTrackSegmentMakerv1 ========\n" ; 
582     message += "Making Track segments\n" ;
583     message += "with parameters:\n" ; 
584     message += "     Maximal EMC - CPV distance (cm) %f\n" ;
585     message += "============================================\n" ;
586     Info("Print", message.Data(),fRcpv) ;
587   }
588   else
589     Info("Print", "AliPHOSTrackSegmentMakerv1 not initialized ") ;
590 }
591
592 //____________________________________________________________________________
593 void AliPHOSTrackSegmentMakerv1::WriteTrackSegments()
594 {
595   // Writes found TrackSegments to TreeR. Creates branches 
596   // "PHOSTS" and "AliPHOSTrackSegmentMaker" with the same title.
597   // In the former branch found TrackSegments are stored, while 
598   // in the latter all parameters, with which TS were made. 
599   // ROOT does not allow overwriting existing branches, therefore
600   // first we check, if branches with the same title already exist.
601   // If yes - exits without writing.
602
603   AliPHOSGetter *gime = AliPHOSGetter::Instance() ; 
604
605   TClonesArray * trackSegments = gime->TrackSegments() ; 
606   trackSegments->Expand(trackSegments->GetEntriesFast()) ;
607
608   if(fWrite){ //We write TreeT
609     TTree * treeT = gime->TreeT();
610     
611     //First TS
612     Int_t bufferSize = 32000 ; 
613     TBranch * tsBranch = treeT->Branch("PHOSTS",&trackSegments,bufferSize);
614     tsBranch->Fill() ;  
615     
616     gime->WriteTracks("OVERWRITE");
617     gime->WriteTrackSegmentMaker("OVERWRITE");
618   }
619 }
620
621
622 //____________________________________________________________________________
623 void AliPHOSTrackSegmentMakerv1::PrintTrackSegments(Option_t * option)
624 {
625   // option deb - prints # of found TrackSegments
626   // option deb all - prints as well indexed of found RecParticles assigned to the TS
627
628   TClonesArray * trackSegments = AliPHOSGetter::Instance()->TrackSegments() ; 
629
630   Info("PrintTrackSegments", "Results from TrackSegmentMaker:") ; 
631   printf("nevent: %d\n", gAlice->GetEvNumber()) ; 
632   printf("        Found %d TrackSegments\n", trackSegments->GetEntriesFast() ); 
633   
634   if(strstr(option,"all")) {  // printing found TS
635     printf("TrackSegment #  EMC RP#  CPV RP#\n") ; 
636     Int_t index;
637     for (index = 0 ; index <trackSegments->GetEntriesFast() ; index++) {
638       AliPHOSTrackSegment * ts = (AliPHOSTrackSegment * )trackSegments->At(index) ; 
639       printf("   %d           %d        %d \n", ts->GetIndexInList(), ts->GetEmcIndex(), ts->GetCpvIndex() ) ; 
640     }   
641   }
642 }