]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PHOS/AliPHOSTrackSegmentMakerv1.cxx
New functions MakeTrackSegmentsCPV(), FillOneModule()
[u/mrichter/AliRoot.git] / PHOS / AliPHOSTrackSegmentMakerv1.cxx
index dc15d4501742f62c5e14a8f9ff78104b93f20000..1bf4acdb65c94c7fa39540ddfbeb07b227c1ae43 100644 (file)
@@ -36,6 +36,7 @@
 // --- AliRoot header files ---
 
 #include "AliPHOSTrackSegmentMakerv1.h"
+#include "AliPHOSIndexToObject.h"
 #include "AliPHOSTrackSegment.h"
 #include "AliPHOSLink.h"
 #include "AliPHOSv0.h"
@@ -51,10 +52,9 @@ ClassImp( AliPHOSTrackSegmentMakerv1)
 {
   // ctor
 
-  fR0 = 4. ;   
-  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
+  fR0 = 10. ;   
   //clusters are sorted in "rows" and "columns" of width geom->GetCrystalSize(0),
-  fDelta = fR0 + geom->GetCrystalSize(0) ;
+  fDelta = fR0 + fGeom->GetCrystalSize(0) ;
   fMinuit = new TMinuit(100) ;
   fUnfoldFlag = kTRUE ; 
 }
@@ -73,8 +73,6 @@ Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * ma
 { 
   // Calls TMinuit to fit the energy distribution of a cluster with several maxima 
 
-  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
-
   gMinuit->SetPrintLevel(-1) ;           // No Printout
   gMinuit->SetFCN(UnfoldingChiSquare) ;  // To set the address of the minimization function 
   gMinuit->SetObjectFit(emcRP) ;         // To tranfer pointer to UnfoldingChiSquare
@@ -95,8 +93,8 @@ Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * ma
     Int_t relid[4] ;
     Float_t x ;
     Float_t z ;
-    geom->AbsToRelNumbering(digit->GetId(), relid) ;
-    geom->RelPosInModule(relid, x, z) ;
+    fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
+    fGeom->RelPosInModule(relid, x, z) ;
 
     Float_t energy = maxAtEnergy[iDigit] ;
 
@@ -129,7 +127,9 @@ Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * ma
   gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ;   // force TMinuit to use my gradient  
   gMinuit->SetMaxIterations(5);
   gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ;  // No Warnings
+
   gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ;    // minimize 
+
   if(ierflg == 4){  // Minimum not found   
     cout << "PHOS Unfolding>  Fit not converged, cluster abandoned "<< endl ;      
     return kFALSE ;
@@ -140,63 +140,103 @@ Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * ma
     gMinuit->GetParameter(index, val, err) ;    // Returns value and error of parameter index
     fitparameters[index] = val ;
    }
+
   return kTRUE;
 
 }
 
 //____________________________________________________________________________
-void  AliPHOSTrackSegmentMakerv1::FillOneModule(DigitsList * dl, RecPointsList * emcIn, TObjArray * emcOut, 
-                                       RecPointsList * ppsdIn, TObjArray * ppsdOutUp,
-                                       TObjArray * ppsdOutLow, Int_t & phosmod, Int_t & emcStopedAt, 
-                                       Int_t & ppsdStopedAt)
+void  AliPHOSTrackSegmentMakerv1::FillOneModule(AliPHOSRecPoint::RecPointsList * emcIn, 
+                                               TArrayI * emcOut, 
+                                               AliPHOSRecPoint::RecPointsList * ppsdIn, 
+                                               TArrayI * ppsdOutUp,
+                                               TArrayI * ppsdOutLow, 
+                                               Int_t & phosmod, 
+                                               Int_t & emcStopedAt, 
+                                               Int_t & ppsdStopedAt)
 {
-  // Unfold clusters and fill xxxOut arrays with clusters from one PHOS module
+  // Fill xxxOut arrays with clusters from one PHOS module EMC+PPSD
  
   AliPHOSEmcRecPoint *  emcRecPoint  ; 
   AliPHOSPpsdRecPoint * ppsdRecPoint ;
   Int_t index ;
   
   Int_t nEmcUnfolded = emcIn->GetEntries() ;
+  emcOut->Set(nEmcUnfolded);
+  Int_t inEmcOut = 0 ;
   for(index = emcStopedAt; index < nEmcUnfolded; index++){
-    emcRecPoint = (AliPHOSEmcRecPoint *) (*emcIn)[index] ;
 
-    if(emcRecPoint->GetPHOSMod() != phosmod )  
-       break ;
+    emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
     
-    Int_t nMultipl = emcRecPoint->GetMultiplicity() ; 
-    Int_t * maxAt = new Int_t[nMultipl] ;
-    Float_t * maxAtEnergy = new Float_t[nMultipl] ;
-    Int_t nMax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
-
-    if(nMax <= 1 )     // if cluster is very flat (no pronounced maximum) then nMax = 0 
-      emcOut->Add(emcRecPoint) ;
-    else if (fUnfoldFlag) {
-      UnfoldClusters(dl, emcIn, emcRecPoint, nMax, maxAt, maxAtEnergy, emcOut) ;
-      emcIn->Remove(emcRecPoint); 
-      emcIn->Compress() ;
-      nEmcUnfolded-- ;
-      index-- ;
-    }
+    if(emcRecPoint->GetPHOSMod() != phosmod )  
+      break ;
     
-    delete[] maxAt ; 
-    delete[] maxAtEnergy ; 
+    emcOut->AddAt(emcRecPoint->GetIndexInList(),inEmcOut) ;
+    inEmcOut++ ; 
   }
+  emcOut->Set(inEmcOut) ;
+
   emcStopedAt = index ;
 
+  ppsdOutLow->Set(ppsdIn->GetEntries()) ;
+  ppsdOutUp->Set(ppsdIn->GetEntries()) ;
+  Int_t inPpsdLow = 0;
+  Int_t inPpsdUp = 0;
   for(index = ppsdStopedAt; index < ppsdIn->GetEntries(); index++){
-    ppsdRecPoint = (AliPHOSPpsdRecPoint *) (*ppsdIn)[index] ;
+    ppsdRecPoint = (AliPHOSPpsdRecPoint *) ppsdIn->At(index) ;
     if(ppsdRecPoint->GetPHOSMod() != phosmod )   
       break ;
     if(ppsdRecPoint->GetUp() ) 
-      ppsdOutUp->Add(ppsdRecPoint) ;
+      ppsdOutUp->AddAt(index,inPpsdUp++) ;
     else  
-      ppsdOutLow->Add(ppsdRecPoint) ;
+      ppsdOutLow->AddAt(index,inPpsdLow++) ;
   }
+  ppsdOutLow->Set(inPpsdLow);
+  ppsdOutUp->Set(inPpsdUp);
   ppsdStopedAt = index ;
    
-  emcOut->Sort() ;
-  ppsdOutUp->Sort() ;
-  ppsdOutLow->Sort() ;   
+}
+//____________________________________________________________________________
+void  AliPHOSTrackSegmentMakerv1::FillOneModule(AliPHOSRecPoint::RecPointsList * emcIn, 
+                                               TArrayI * emcOut, 
+                                               AliPHOSRecPoint::RecPointsList * cpvIn, 
+                                               TArrayI * cpvOut,
+                                               Int_t & phosmod, 
+                                               Int_t & emcStopedAt, 
+                                               Int_t & cpvStopedAt)
+{
+  // Fill xxxOut arrays with clusters from one PHOS module EMC+CPV
+  AliPHOSEmcRecPoint * emcRecPoint  ; 
+  AliPHOSEmcRecPoint * cpvRecPoint ;
+  Int_t index ;
+  
+  Int_t nEmcUnfolded = emcIn->GetEntries() ;
+  emcOut->Set(nEmcUnfolded);
+  Int_t inEmcOut = 0 ;
+  for(index = emcStopedAt; index < nEmcUnfolded; index++){
+    emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
+    if(emcRecPoint->GetPHOSMod() != phosmod )  
+      break ;
+    
+    emcOut->AddAt(emcRecPoint->GetIndexInList(),inEmcOut) ;
+    inEmcOut++ ; 
+  }
+  emcOut->Set(inEmcOut) ;
+  emcStopedAt = index ;
+
+  cpvOut->Set(cpvIn->GetEntries()) ;
+  Int_t inCpvOut = 0;
+  for(index = cpvStopedAt; index < cpvIn->GetEntries(); index++){
+    cpvRecPoint = (AliPHOSEmcRecPoint *) cpvIn->At(index) ;
+    if(cpvRecPoint->GetPHOSMod() != phosmod )   
+      break ;
+    else  
+      cpvOut->AddAt(index,inCpvOut++) ;
+  }
+  cpvOut->Set(inCpvOut);
+  cpvStopedAt = index ;
+   
 }
 //____________________________________________________________________________
 Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * emcclu,AliPHOSPpsdRecPoint * PpsdClu, Bool_t &toofar)
@@ -213,11 +253,10 @@ Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint *
   if(emcclu->GetPHOSMod() == PpsdClu->GetPHOSMod()){ 
     if(vecPpsd.X() >= vecEmc.X() - fDelta ){ 
       if(vecPpsd.Z() >= vecEmc.Z() - fDelta ){
-       AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
        // Correct to difference in CPV and EMC position due to different distance to center.
        // we assume, that particle moves from center
-       Float_t dCPV = geom->GetIPtoOuterCoverDistance();
-       Float_t dEMC = geom->GetIPtoCrystalSurface() ;
+       Float_t dCPV = fGeom->GetIPtoOuterCoverDistance();
+       Float_t dEMC = fGeom->GetIPtoCrystalSurface() ;
        dEMC         = dEMC / dCPV ;
         vecPpsd = dEMC * vecPpsd  - vecEmc ; 
         r = vecPpsd.Mag() ;
@@ -234,66 +273,65 @@ Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint *
 }
 
 //____________________________________________________________________________
-void  AliPHOSTrackSegmentMakerv1::MakeLinks(TObjArray * emcRecPoints, TObjArray * ppsdRecPointsUp, 
-                                    TObjArray * ppsdRecPointsLow, TClonesArray * linklowArray, 
+void  AliPHOSTrackSegmentMakerv1::MakeLinks(TArrayI * emcRecPoints, TArrayI * ppsdRecPointsUp, 
+                                    TArrayI * ppsdRecPointsLow, TClonesArray * linklowArray, 
                                     TClonesArray *linkupArray) 
 { 
   // Finds distances (links) between all EMC and PPSD clusters, which are not further apart from each other than fR0 
   
-  TIter nextEmc(emcRecPoints) ;
-  Int_t iEmcClu = 0 ; 
   
   AliPHOSPpsdRecPoint * ppsdlow ; 
   AliPHOSPpsdRecPoint * ppsdup ;
   AliPHOSEmcRecPoint * emcclu ;
-  
+
   Int_t iLinkLow = 0 ;
   Int_t iLinkUp  = 0 ;
   
-  while( (emcclu = (AliPHOSEmcRecPoint*)nextEmc() ) ) {
+  Int_t iEmcRP;
+  
+  for(iEmcRP = 0; iEmcRP < emcRecPoints->GetSize(); iEmcRP++ ) {
+    emcclu = (AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emcRecPoints->At(iEmcRP),"emc") ;
     Bool_t toofar ;
-    TIter nextPpsdLow(ppsdRecPointsLow ) ;
-    Int_t iPpsdLow = 0 ;
     
-    while( (ppsdlow = (AliPHOSPpsdRecPoint*)nextPpsdLow() ) ) { 
+    Int_t iPpsdLow ;
+    
+    for(iPpsdLow = 0; iPpsdLow < ppsdRecPointsLow->GetSize();iPpsdLow++ ) {
+      
+      ppsdlow = (AliPHOSPpsdRecPoint *) fPlease->GimeRecPoint(ppsdRecPointsLow->At(iPpsdLow),"ppsd") ;
       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdlow, toofar) ;
       
       if(toofar) 
        break ;  
-      if(r < fR0) 
-       new( (*linklowArray)[iLinkLow++]) AliPHOSLink(r, iEmcClu, iPpsdLow) ;
-      
-      iPpsdLow++ ;
-      
+      if(r < fR0){
+       new( (*linklowArray)[iLinkLow++]) AliPHOSLink(r, iEmcRP, iPpsdLow) ;
+      }
     }
     
-    TIter nextPpsdUp(ppsdRecPointsUp ) ;
-    Int_t iPpsdUp = 0 ;
-    
-    while( (ppsdup = (AliPHOSPpsdRecPoint*)nextPpsdUp() ) ) { 
+    Int_t iPpsdUp = 0 ;    
+    for(iPpsdUp = 0; iPpsdUp < ppsdRecPointsUp->GetSize();iPpsdUp++ ) { 
+      
+      ppsdup = (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdRecPointsUp->At(iPpsdUp),"ppsd") ;
       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdup, toofar) ;
       
       if(toofar)
        break ;  
-      if(r < fR0) 
-       new( (*linkupArray)[iLinkUp++]) AliPHOSLink(r, iEmcClu, iPpsdUp) ;
-      
-      iPpsdUp++ ;
-      
+      if(r < fR0) { 
+       new( (*linkupArray)[iLinkUp++]) AliPHOSLink(r, iEmcRP, iPpsdUp) ;
+      }      
     }
-    
-    iEmcClu++ ; 
-    
-  } // while nextEmC
+  } 
   
   linklowArray->Sort() ; //first links with smallest distances
   linkupArray->Sort() ;
 }
-    
+
 //____________________________________________________________________________
-void  AliPHOSTrackSegmentMakerv1::MakePairs(TObjArray * emcRecPoints, TObjArray * ppsdRecPointsUp, 
-                                   TObjArray * ppsdRecPointsLow, TClonesArray * linklowArray, 
-                                   TClonesArray * linkupArray, TrackSegmentsList * trsl) 
+void  AliPHOSTrackSegmentMakerv1::MakePairs(TArrayI * emcRecPoints, 
+                                           TArrayI * ppsdRecPointsUp, 
+                                           TArrayI * ppsdRecPointsLow, 
+                                           TClonesArray * linklowArray, 
+                                           TClonesArray * linkupArray, 
+                                           AliPHOSTrackSegment::TrackSegmentsList * trsl) 
 { 
 
   // Finds the smallest links and makes pairs of PPSD and EMC clusters with smallest distance 
@@ -304,110 +342,124 @@ void  AliPHOSTrackSegmentMakerv1::MakePairs(TObjArray * emcRecPoints, TObjArray
   AliPHOSLink * linkLow ;
   AliPHOSLink * linkUp ;
 
-  AliPHOSEmcRecPoint * emc ;
-  AliPHOSPpsdRecPoint * ppsdLow ;
-  AliPHOSPpsdRecPoint * ppsdUp ;
+  Int_t emc ;
+  Int_t ppsdLow ;
+  Int_t ppsdUp ;
 
-  AliPHOSRecPoint * nullpointer = 0 ;
+  AliPHOSPpsdRecPoint * nullpointer = 0 ;
+  ppsdUp = 0 ;
 
   while ( (linkLow =  (AliPHOSLink *)nextLow() ) ){
-    emc = (AliPHOSEmcRecPoint *) emcRecPoints->At(linkLow->GetEmc()) ;
-    ppsdLow = (AliPHOSPpsdRecPoint *) ppsdRecPointsLow->At(linkLow->GetPpsd()) ;
-    if( (emc) && (ppsdLow) ){ // RecPoints not removed yet 
-        ppsdUp = 0 ;
-        
-        while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){  
-          if(linkLow->GetEmc() == linkUp->GetEmc() ){
-            ppsdUp = (AliPHOSPpsdRecPoint *) ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
-            break ;
-          }
-       
-        } // while nextUp
-        
-        nextUp.Reset();
-//          AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
-//      trsl->Add(subtr) ;  
-        new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
-        fNTrackSegments++ ;
-        emcRecPoints->AddAt(nullpointer,linkLow->GetEmc()) ;     
-        ppsdRecPointsLow->AddAt(nullpointer,linkLow->GetPpsd()) ;
-        
-        if(ppsdUp)  
-          ppsdRecPointsUp->AddAt(nullpointer,linkUp->GetPpsd()) ;
-        
+  
+    emc = emcRecPoints->At(linkLow->GetEmc()) ;
+    ppsdLow = ppsdRecPointsLow->At(linkLow->GetPpsd()) ;
+    if( (emc >= 0) && (ppsdLow >= 0) ){    // RecPoints not removed yet 
+
+      new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *)fPlease->GimeRecPoint(emc,"emc"), 
+                                                         nullpointer, 
+                                                         (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdLow,"ppsd") ) ;
+      ((AliPHOSTrackSegment* )trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);    
+      //replace index of emc to negative and shifted index of TS      
+      emcRecPoints->AddAt(-2 - fNTrackSegments,linkLow->GetEmc()) ;  
+      //replace index of emc to negative and shifted index of TS      
+      ppsdRecPointsLow->AddAt(-2 - fNTrackSegments,linkLow->GetPpsd()) ; 
+      fNTrackSegments++ ;
+
     } 
   } 
-   
-  TIter nextEmc(emcRecPoints) ;          
-  nextEmc.Reset() ;
+        
+  while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){  
+    emc = emcRecPoints->At(linkUp->GetEmc()) ;
+    if(emc != -1){ //without ppsd Up yet 
 
-  while( (emc = (AliPHOSEmcRecPoint*)nextEmc()) ){ //to create pairs if no ppsdlow
-    ppsdLow = 0 ; 
-    ppsdUp  = 0 ;
-    
-    while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){
-      
-      if(emcRecPoints->IndexOf(emc) == linkUp->GetEmc() ){
-       ppsdUp = (AliPHOSPpsdRecPoint *) ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
-       break ;
+      ppsdUp = ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
+
+      if(emc >= 0){ //without ppsd Low => create new TS
+
+       fNTrackSegments = trsl->GetEntries() ; 
+       new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"), 
+                                                           (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"), 
+                                                           nullpointer) ;
+       ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
+       fNTrackSegments++ ;
       }
+      else{ // append ppsd Up to existing TS
+       ((AliPHOSTrackSegment *)trsl->At(-2-emc))->SetPpsdUpRecPoint((AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"));
+      }
+      emcRecPoints->AddAt(-1,linkUp->GetEmc()) ; //Mark that PPSD Up found 
       
+    } 
+  }     
+
+  Int_t iEmcRP ;
+  for(iEmcRP = 0; iEmcRP <emcRecPoints->GetSize() ; iEmcRP++ ){
+    emc = emcRecPoints->At(iEmcRP) ;
+    if(emc >=0 ){
+      ppsdUp = 0;
+      ppsdLow = 0;
+      new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"), 
+                                                         nullpointer, nullpointer ) ;
+      ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
+      fNTrackSegments++;    
     }
-//     AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
-//     trsl->Add(subtr) ;   
-    new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
-    fNTrackSegments++ ;
     
-
-    if(ppsdUp)  
-      ppsdRecPointsUp->AddAt(nullpointer,linkUp->GetPpsd()) ;
   }
-     
+  
 }
 
 //____________________________________________________________________________
-void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * dl, RecPointsList * emcl, 
-                                       RecPointsList * ppsdl, TrackSegmentsList * trsl)
+void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * dl, 
+                                                   AliPHOSRecPoint::RecPointsList * emcl, 
+                                                   AliPHOSRecPoint::RecPointsList * ppsdl, 
+                                                   AliPHOSTrackSegment::TrackSegmentsList * trsl)
 {
   // Makes the track segments out of the list of EMC and PPSD Recpoints and stores them in a list
-
+  
   Int_t phosmod      = 1 ;
   Int_t emcStopedAt  = 0 ; 
   Int_t ppsdStopedAt = 0 ; 
+
+  fNTrackSegments = 0 ; 
+
   
-  TObjArray * emcRecPoints     = new TObjArray(100) ;  // these arrays keep pointer
-  TObjArray * ppsdRecPointsUp  = new TObjArray(100) ;  // to RecPoints, which are 
-  TObjArray * ppsdRecPointsLow = new TObjArray(100) ;  // kept in TClonesArray's emcl and ppsdl
+  TArrayI * emcRecPoints     = new TArrayI(1000) ;  // these arrays keep indexe
+  TArrayI * ppsdRecPointsUp  = new TArrayI(1000) ;  // of RecPoints, which are 
+  TArrayI * ppsdRecPointsLow = new TArrayI(1000) ;  // kept in TClonesArray's emcl and ppsdl
   
   
   TClonesArray * linklowArray = new TClonesArray("AliPHOSLink", 100);
   TClonesArray * linkupArray  = new TClonesArray("AliPHOSLink", 100); 
-  
-  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
-  
-  while(phosmod <= geom->GetNModules() ){
-    
-    FillOneModule(dl, emcl, emcRecPoints, ppsdl, ppsdRecPointsUp, ppsdRecPointsLow, phosmod, emcStopedAt, ppsdStopedAt) ;
 
-    MakeLinks(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray) ; 
 
-    MakePairs(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray, trsl) ;
-    emcRecPoints->Clear() ;
-    ppsdRecPointsUp->Clear() ;
-  
-    ppsdRecPointsLow->Clear() ;
+  if(fUnfoldFlag){
+    UnfoldAll(dl, emcl) ;      // Unfolds all EMC clusters
+  }
 
+
+  while(phosmod <= fGeom->GetNModules() ){
+    
+    FillOneModule(emcl, emcRecPoints, ppsdl, ppsdRecPointsUp, ppsdRecPointsLow, phosmod, emcStopedAt, ppsdStopedAt) ;
+    
+    MakeLinks(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray) ; 
+    
+    MakePairs(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray, trsl) ;
+    
+    emcRecPoints->Reset() ;
+    
+    ppsdRecPointsUp->Reset() ;
+    
+    ppsdRecPointsLow->Reset() ;
+    
     linkupArray->Clear() ;
-   
+    
     linklowArray->Clear() ;
-   
+    
     phosmod++ ; 
   }
+
   delete emcRecPoints ; 
   emcRecPoints = 0 ; 
-
+  
   delete ppsdRecPointsUp ; 
   ppsdRecPointsUp = 0 ; 
 
@@ -421,6 +473,38 @@ void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * dl, RecPointsLi
   linklowArray = 0 ; 
 }
 
+//____________________________________________________________________________
+void  AliPHOSTrackSegmentMakerv1::MakeTrackSegmentsCPV(DigitsList * dl, 
+                                                      AliPHOSRecPoint::RecPointsList * emcl, 
+                                                      AliPHOSRecPoint::RecPointsList * cpvl)
+{
+  // Unfold clusters in EMC and CPV and refill reconstructed point lists emcl and ppsdl
+  // Yuri Kharlov. 19 October 2000
+  
+  fNTrackSegments = 0 ; 
+
+  TArrayI * emcRecPoints     = new TArrayI(1000) ;  // these arrays keep indexes 
+  TArrayI * cpvRecPoints     = new TArrayI(1000) ;  // of RecPoints, which are kept in emcl and ppsdl
+  
+  if(fUnfoldFlag){
+    UnfoldAll(dl, emcl) ;   // Unfolds all EMC clusters
+    UnfoldAll(dl, cpvl) ;   // Unfolds all CPV clusters
+  }
+
+//    Int_t phosmod      = 1 ;
+//    Int_t emcStopedAt  = 0 ; 
+//    Int_t cpvStopedAt  = 0 ; 
+//    while(phosmod <= fGeom->GetNModules() ){
+//      FillOneModule(emcl, emcRecPoints, ppsdl, cpvRecPoints, phosmod, emcStopedAt, cpvStopedAt) ;
+//      emcRecPoints->Reset() ;
+//      cpvRecPoints->Reset() ;
+//      phosmod++ ; 
+//    }
+
+  delete emcRecPoints ; emcRecPoints = 0 ; 
+  delete cpvRecPoints ; cpvRecPoints = 0 ; 
+}
+
 //____________________________________________________________________________
 Double_t  AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
 { 
@@ -434,15 +518,59 @@ Double_t  AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
 }
 
 //____________________________________________________________________________
-void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, RecPointsList * emcIn,  AliPHOSEmcRecPoint * iniEmc, 
-                                        Int_t nMax, int * maxAt, Float_t * maxAtEnergy, TObjArray * emcList)
+void  AliPHOSTrackSegmentMakerv1::UnfoldAll(DigitsList * dl, AliPHOSRecPoint::RecPointsList * emcIn) 
+{
+  // Performs unfolding of all EMC clusters, sorts them and resets indexes in RecPoints
+
+  AliPHOSEmcRecPoint *  emcRecPoint  ; 
+  Int_t index ;
+  Int_t nEmcUnfolded = emcIn->GetEntries() ;
+  
+  for(index = 0 ; index < nEmcUnfolded; index++){
+
+    emcRecPoint   = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
+    
+    Int_t nMultipl        = emcRecPoint->GetMultiplicity() ; 
+    Int_t   * maxAt       = new Int_t[nMultipl] ;
+    Float_t * maxAtEnergy = new Float_t[nMultipl] ;
+    Int_t nMax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
+    
+    if( nMax > 1 ) {     // if cluster is very flat (no pronounced maximum) then nMax = 0 
+      UnfoldClusters(dl, emcIn, emcRecPoint, nMax, maxAt, maxAtEnergy) ;
+      emcIn->Remove(emcRecPoint); 
+      emcIn->Compress() ;
+      index-- ;
+      nEmcUnfolded-- ;
+    }
+    
+    delete[] maxAt ; 
+    delete[] maxAtEnergy ; 
+  } //Unfolding finished
+
+  emcIn->Sort() ;
+
+  // to set index to new and correct index of old RecPoints
+  for( index = 0 ; index < emcIn->GetEntries() ; index++){
+    ((AliPHOSEmcRecPoint *) emcIn->At(index))->SetIndexInList(index) ;   
+  }
+
+  emcIn->Sort() ;
+
+}
+//____________________________________________________________________________
+void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, 
+                                                AliPHOSRecPoint::RecPointsList * emcIn, 
+                                                AliPHOSEmcRecPoint * iniEmc, 
+                                                Int_t nMax, 
+                                                int * maxAt, 
+                                                Float_t * maxAtEnergy)
 { 
   // Performs the unfolding of a cluster with nMax overlapping showers 
   // This is time consuming (use the (Un)SetUnfolFlag()  )
 
   Int_t nPar = 3 * nMax ;
   Float_t * fitparameters = new Float_t[nPar] ;
-  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
+
 
   Bool_t rv = FindFit(iniEmc, maxAt, maxAtEnergy, nPar, fitparameters) ;
   if( !rv ) {
@@ -450,7 +578,8 @@ void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, RecPointsList
     delete[] fitparameters ; 
     return ;
   }
-  
+
+
   Float_t xDigit ;
   Float_t zDigit ;
   Int_t relid[4] ;
@@ -473,9 +602,9 @@ void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, RecPointsList
   Int_t iRecPoint = emcIn->GetEntries() ;
 
   for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
-    digit = (AliPHOSDigit *) emcDigits[iDigit];
-    geom->AbsToRelNumbering(digit->GetId(), relid) ;
-    geom->RelPosInModule(relid, xDigit, zDigit) ;
+    digit = fPlease->GimeDigit( emcDigits[iDigit] ) ;   
+    fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
+    fGeom->RelPosInModule(relid, xDigit, zDigit) ;
     efit[iDigit] = 0;
     iparam = 0 ;
     
@@ -493,18 +622,24 @@ void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, RecPointsList
   iparam = 0 ;
   Float_t eDigit ;
 
+
   while(iparam < nPar ){
     xpar = fitparameters[iparam] ;
     zpar = fitparameters[iparam+1] ;
     epar = fitparameters[iparam+2] ;
     iparam += 3 ;
-    new ((*emcIn)[iRecPoint]) AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
-    emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint++);
+
+    if(iRecPoint >= emcIn->GetSize())
+      emcIn->Expand(2*iRecPoint) ;
+    (*emcIn)[iRecPoint] = new AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
+    
+    emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint);
+    iRecPoint++ ;
 
     for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
-      digit = (AliPHOSDigit *) emcDigits[iDigit];
-      geom->AbsToRelNumbering(digit->GetId(), relid) ;
-      geom->RelPosInModule(relid, xDigit, zDigit) ;
+      digit = fPlease->GimeDigit( emcDigits[iDigit] ) ; 
+      fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
+      fGeom->RelPosInModule(relid, xDigit, zDigit) ;
       distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
       distance =  TMath::Sqrt(distance) ;
       ratio = epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) / efit[iDigit] ; 
@@ -512,8 +647,6 @@ void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, RecPointsList
       emcRP->AddDigit( *digit, eDigit ) ;
     }
 
-    emcList->Add(emcRP) ;
-
   }
   
   delete[] fitparameters ; 
@@ -526,12 +659,19 @@ void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t
 {
   // Calculates th Chi square for the cluster unfolding minimization
   // Number of parameters, Gradient, Chi squared, parameters, what to do
-
-  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
-
+  
   AliPHOSEmcRecPoint * emcRP = (AliPHOSEmcRecPoint *) gMinuit->GetObjectFit() ; // EmcRecPoint to fit
+
   Int_t * emcDigits     = emcRP->GetDigitsList() ;
+
+  Int_t nOfDigits = emcRP->GetDigitsMultiplicity() ; 
+
   Float_t * emcEnergies = emcRP->GetEnergiesList() ;
+
+  AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
+
+  AliPHOSIndexToObject * please = AliPHOSIndexToObject::GetInstance() ;
+
   fret = 0. ;     
   Int_t iparam ;
 
@@ -539,18 +679,23 @@ void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t
     for(iparam = 0 ; iparam < nPar ; iparam++)    
       Grad[iparam] = 0 ; // Will evaluate gradient
   
-  Double_t efit ;  
-  
+  Double_t efit ;    
+
   AliPHOSDigit * digit ;
-  Int_t iDigit = 0 ;
+  Int_t iDigit ;
+
+  for( iDigit = 0 ; iDigit < nOfDigits ; iDigit++) {
+
+    digit = please->GimeDigit( emcDigits[iDigit] ) ; 
 
-  while ( (digit = (AliPHOSDigit *)emcDigits[iDigit] )){
     Int_t relid[4] ;
     Float_t xDigit ;
     Float_t zDigit ;
+
     geom->AbsToRelNumbering(digit->GetId(), relid) ;
+
     geom->RelPosInModule(relid, xDigit, zDigit) ;
-    
+
      if(iflag == 2){  // calculate gradient
        Int_t iParam = 0 ;
        efit = 0 ;
@@ -586,6 +731,7 @@ void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t
      }
      efit = 0;
      iparam = 0 ;
+
      while(iparam < nPar ){
        Double_t xpar = x[iparam] ;
        Double_t zpar = x[iparam+1] ;
@@ -595,8 +741,9 @@ void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t
        distance =  TMath::Sqrt(distance) ;
        efit += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
      }
+
      fret += (efit-emcEnergies[iDigit])*(efit-emcEnergies[iDigit])/emcEnergies[iDigit] ; 
      // Here we assume, that sigma = sqrt(E)
-     iDigit++ ;
   }
+
 }