]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSTrackSegmentMakerv1.cxx
fc531e801bc70b52dbcb398b0cad8de6191260cd
[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
16 //_________________________________________________________________________
17 // Algorithm class to construct track segments connection RecPoints in 
18 // EMCA and Ppsd. Unfolds also the clusters in EMCA. 
19 //*-- Author : D. Peressounko  SUBATECH 
20 //////////////////////////////////////////////////////////////////////////////
21
22 // --- ROOT system ---
23
24 #include "TObjArray.h"
25 #include "TClonesArray.h"
26 #include "TObjectTable.h"
27
28 // --- Standard library ---
29
30 #include <iostream>
31 #include <cassert>
32
33 // --- AliRoot header files ---
34
35 #include "AliPHOSTrackSegmentMakerv1.h"
36 #include "AliPHOSTrackSegment.h"
37 #include "AliPHOSLink.h"
38 #include "AliPHOSv0.h"
39 #include "AliRun.h"
40
41 extern void UnfoldingChiSquare(Int_t &NPar, Double_t *Grad, Double_t & fret, Double_t *x, Int_t iflag) ; 
42
43 ClassImp( AliPHOSTrackSegmentMakerv1) 
44
45
46 //____________________________________________________________________________
47  AliPHOSTrackSegmentMakerv1::AliPHOSTrackSegmentMakerv1() 
48 {
49   // ctor
50   fR0 = 4. ;   
51   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
52   //clusters are sorted in "rows" and "columns" of width geom->GetCrystalSize(0),
53   fDelta = fR0 + geom->GetCrystalSize(0) ;
54   fMinuit = new TMinuit(100) ; 
55 }
56
57 //____________________________________________________________________________
58  AliPHOSTrackSegmentMakerv1::~AliPHOSTrackSegmentMakerv1()
59
60   // dtor
61   delete fMinuit ; 
62 }
63 //____________________________________________________________________________
64 Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * maxAt, Float_t * maxAtEnergy,
65                                     Int_t NPar, Float_t * FitParameters)
66
67   // gObjectTable->Print() ; 
68   // Calls TMinuit for fitting cluster with several maxima 
69   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
70   assert( NPar < 100 ) ; 
71
72   gMinuit->SetPrintLevel(-1) ;           // No Printout
73   gMinuit->SetFCN(UnfoldingChiSquare) ;  // To set the address of the minimization function 
74   gMinuit->SetObjectFit(emcRP) ;         // To tranfer pointer to UnfoldingChiSquare
75
76   // filling initial values for fit parameters
77   AliPHOSDigit * digit ;
78
79   Int_t ierflg  = 0; 
80   Int_t index   = 0 ;
81   Int_t NDigits = (Int_t) NPar / 3 ;
82
83   Int_t iDigit ;
84
85
86   for(iDigit = 0 ; iDigit < NDigits ; iDigit++){
87     digit = (AliPHOSDigit *) maxAt[iDigit]; 
88
89     Int_t RelId[4] ;
90     Float_t x ;
91     Float_t z ;
92     geom->AbsToRelNumbering(digit->GetId(), RelId) ;
93     geom->RelPosInModule(RelId, x, z) ;
94
95     Float_t Energy = maxAtEnergy[iDigit] ;
96
97     gMinuit->mnparm(index, "x",  x, 0.1, 0, 0, ierflg) ;
98     index++ ;   
99     if(ierflg != 0){ 
100       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : x = " << x << endl ;
101       return kFALSE;
102     }
103     gMinuit->mnparm(index, "z",  z, 0.1, 0, 0, ierflg) ;
104     index++ ;   
105     if(ierflg != 0){
106       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : z = " << z << endl ;
107       return kFALSE;
108     }
109     gMinuit->mnparm(index, "Energy",  Energy , 0.05*Energy, 0., 4.*Energy, ierflg) ;
110     index++ ;   
111     if(ierflg != 0){
112       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : Energy = " << Energy << endl ;      
113       return kFALSE;
114 }
115   }
116
117   Double_t p0 = 0.1 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
118                       //  depends on it. 
119   Double_t p1 = 1.0 ;
120   Double_t p2 = 0.0 ;
121
122   gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ;   // force TgMinuit to reduce function calls  
123   gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ;   // force TMinuit to use my gradient  
124   gMinuit->SetMaxIterations(5);
125   gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ;  // No Warnings
126   gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ;    // minimize 
127   if(ierflg == 4){  // Minimum not found   
128     cout << "PHOS Unfolding>  Fit not converged, cluster abandoned "<< endl ;      
129     return kFALSE ;
130   }            
131   for(index = 0; index < NPar; index++){
132     Double_t err ;
133     Double_t val ;
134     gMinuit->GetParameter(index, val, err) ;    // Returns value and error of parameter index
135     FitParameters[index] = val ;
136    }
137   return kTRUE;
138
139 }
140
141 //____________________________________________________________________________
142 void  AliPHOSTrackSegmentMakerv1::FillOneModule(DigitsList * Dl, RecPointsList * emcIn, TObjArray * emcOut, 
143                                         RecPointsList * ppsdIn, TObjArray * ppsdOutUp,
144                                         TObjArray * ppsdOutLow, Int_t & PHOSMod, Int_t & emcStopedAt, 
145                                         Int_t & ppsdStopedAt)
146 {
147   // Unfold clusters and fill xxxOut arrays with clusters from one PHOS module
148  
149   AliPHOSEmcRecPoint *  emcRecPoint  ; 
150   AliPHOSPpsdRecPoint * ppsdRecPoint ;
151   Int_t index ;
152   cout << "Fill 1" << endl ;
153   Int_t NemcUnfolded = emcIn->GetEntries() ;
154   for(index = emcStopedAt; index < NemcUnfolded; index++){
155     emcRecPoint = (AliPHOSEmcRecPoint *) (*emcIn)[index] ;
156     cout << "Fill 2" << endl ;
157    
158     if(emcRecPoint->GetPHOSMod() != PHOSMod )  
159        break ;
160     
161     
162     Int_t NMultipl = emcRecPoint->GetMultiplicity() ; 
163     int maxAt[NMultipl] ;
164     Float_t maxAtEnergy[NMultipl] ;
165     Int_t Nmax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
166
167     
168
169     if(Nmax <= 1)     // if cluster is very flat, so that no prononsed maximum, then Nmax = 0 
170       emcOut->Add(emcRecPoint) ;
171     else {
172       UnfoldClusters(Dl, emcIn, emcRecPoint, Nmax, maxAt, maxAtEnergy, emcOut) ;
173       emcIn->Remove(emcRecPoint); 
174       cout << "Fill 3" << endl ;
175       emcIn->Compress() ;
176       NemcUnfolded-- ;
177       index-- ;
178     }
179   }
180   emcStopedAt = index ;
181
182   for(index = ppsdStopedAt; index < ppsdIn->GetEntries(); index++){
183     ppsdRecPoint = (AliPHOSPpsdRecPoint *) (*ppsdIn)[index] ;
184     if(ppsdRecPoint->GetPHOSMod() != PHOSMod )   
185       break ;
186     if(ppsdRecPoint->GetUp() ) 
187       ppsdOutUp->Add(ppsdRecPoint) ;
188     else  
189       ppsdOutLow->Add(ppsdRecPoint) ;
190   }
191   ppsdStopedAt = index ;
192   
193   PHOSMod ++ ; 
194   
195   emcOut->Sort() ;
196   ppsdOutUp->Sort() ;
197   ppsdOutLow->Sort() ; 
198   
199 }
200 //____________________________________________________________________________
201 Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * EmcClu,AliPHOSPpsdRecPoint * PpsdClu, Bool_t &TooFar)
202 {
203   Float_t R = fR0 ;
204  
205   TVector3 vecEmc ;
206   TVector3 vecPpsd ;
207   
208   EmcClu->GetLocalPosition(vecEmc) ;
209   PpsdClu->GetLocalPosition(vecPpsd)  ; 
210   if(EmcClu->GetPHOSMod() == PpsdClu->GetPHOSMod()){ 
211     if(vecPpsd.X() >= vecEmc.X() - fDelta ){ 
212       if(vecPpsd.Z() >= vecEmc.Z() - fDelta ){
213         AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
214         //Correct to difference in CPV and EMC position due to different distance to center.
215         //we assume, that particle moves from center
216         Float_t DCPV = geom->GetIPtoOuterCoverDistance();
217         Float_t DEMC = geom->GetIPtoCrystalSurface() ;
218         DEMC         = DEMC / DCPV ;
219         vecPpsd = DEMC * vecPpsd  - vecEmc ; 
220         R = vecPpsd.Mag() ;
221       } // if  zPpsd >= zEmc - fDelta
222       TooFar = kFALSE ;
223     } // if  xPpsd >= xEmc - fDelta
224     else 
225       TooFar = kTRUE ;
226   } 
227   else 
228     TooFar = kTRUE ;
229   
230   return R ;
231 }
232
233 //____________________________________________________________________________
234 void  AliPHOSTrackSegmentMakerv1::MakeLinks(TObjArray * EmcRecPoints, TObjArray * PpsdRecPointsUp, 
235                                      TObjArray * PpsdRecPointsLow, TClonesArray * LinkLowArray, 
236                                      TClonesArray *LinkUpArray) 
237
238   //Finds distanses (links) between all EMC and PPSD clusters, which are not further from each other than fR0 
239
240   TIter nextEmc(EmcRecPoints) ;
241   Int_t iEmcClu = 0 ; 
242   
243   AliPHOSPpsdRecPoint * PpsdLow ; 
244   AliPHOSPpsdRecPoint * PpsdUp ;
245   AliPHOSEmcRecPoint * EmcClu ;
246   
247   Int_t iLinkLow = 0 ;
248   Int_t iLinkUp  = 0 ;
249   
250   while( (EmcClu = (AliPHOSEmcRecPoint*)nextEmc() ) ) {
251     Bool_t TooFar ;
252     TIter nextPpsdLow(PpsdRecPointsLow ) ;
253     Int_t iPpsdLow = 0 ;
254     
255     while( (PpsdLow = (AliPHOSPpsdRecPoint*)nextPpsdLow() ) ) { 
256       Float_t R = GetDistanceInPHOSPlane(EmcClu, PpsdLow, TooFar) ;
257       
258       if(TooFar) 
259         break ;  
260       if(R < fR0) 
261         new( (*LinkLowArray)[iLinkLow++]) AliPHOSLink(R, iEmcClu, iPpsdLow) ;
262       
263       iPpsdLow++ ;
264       
265     }
266     
267     TIter nextPpsdUp(PpsdRecPointsUp ) ;
268     Int_t iPpsdUp = 0 ;
269     
270     while( (PpsdUp = (AliPHOSPpsdRecPoint*)nextPpsdUp() ) ) { 
271       Float_t R = GetDistanceInPHOSPlane(EmcClu, PpsdUp, TooFar) ;
272       
273       if(TooFar)
274         break ;  
275       if(R < fR0) 
276         new( (*LinkUpArray)[iLinkUp++]) AliPHOSLink(R, iEmcClu, iPpsdUp) ;
277       
278       iPpsdUp++ ;
279       
280     }
281     
282     iEmcClu++ ; 
283     
284   } // while nextEmC
285   
286   LinkLowArray->Sort() ; //first links with smallest distances
287   LinkUpArray->Sort() ;
288 }
289     
290 //____________________________________________________________________________
291 void  AliPHOSTrackSegmentMakerv1::MakePairs(TObjArray * EmcRecPoints, TObjArray * PpsdRecPointsUp, 
292                                     TObjArray * PpsdRecPointsLow, TClonesArray * LinkLowArray, 
293                                     TClonesArray * LinkUpArray, TrackSegmentsList * trsl) 
294 { // Finds the smallest links and makes pairs of PPSD and EMC clusters with smallest distance 
295   TIter nextLow(LinkLowArray) ;
296   TIter nextUp(LinkUpArray) ;
297   
298   AliPHOSLink * linkLow ;
299   AliPHOSLink * linkUp ;
300
301   AliPHOSEmcRecPoint * emc ;
302   AliPHOSPpsdRecPoint * ppsdLow ;
303   AliPHOSPpsdRecPoint * ppsdUp ;
304
305   AliPHOSRecPoint * NullPointer = 0 ;
306
307   while ( (linkLow =  (AliPHOSLink *)nextLow() ) ){
308     emc = (AliPHOSEmcRecPoint *) EmcRecPoints->At(linkLow->GetEmc()) ;
309     ppsdLow = (AliPHOSPpsdRecPoint *) PpsdRecPointsLow->At(linkLow->GetPpsd()) ;
310     if((emc)&&(ppsdLow)){ // RecPoints not removed yet 
311          ppsdUp = 0 ;
312          
313          while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){  
314            if(linkLow->GetEmc() == linkUp->GetEmc() ){
315              ppsdUp = (AliPHOSPpsdRecPoint *) PpsdRecPointsUp->At(linkUp->GetPpsd()) ;
316              break ;
317            }
318         
319          } // while nextUp
320          
321          nextUp.Reset();
322          AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
323          trsl->Add(subtr) ;  
324          EmcRecPoints->AddAt(NullPointer,linkLow->GetEmc()) ;     
325          PpsdRecPointsLow->AddAt(NullPointer,linkLow->GetPpsd()) ;
326          
327          if(ppsdUp)  
328            PpsdRecPointsUp->AddAt(NullPointer,linkUp->GetPpsd()) ;
329          
330     } // if NLocMax
331   } 
332    
333   TIter nextEmc(EmcRecPoints) ;          
334   nextEmc.Reset() ;
335
336   while( (emc = (AliPHOSEmcRecPoint*)nextEmc()) ){ //to create pairs if no PpsdLow
337     ppsdLow = 0 ; 
338     ppsdUp  = 0 ;
339     
340     while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){
341       
342       if(EmcRecPoints->IndexOf(emc) == linkUp->GetEmc() ){
343         ppsdUp = (AliPHOSPpsdRecPoint *) PpsdRecPointsUp->At(linkUp->GetPpsd()) ;
344         break ;
345       }
346       
347     }
348     AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
349     trsl->Add(subtr) ;   
350  
351     if(ppsdUp)  
352       PpsdRecPointsUp->AddAt(NullPointer,linkUp->GetPpsd()) ;
353   }
354      
355 }
356
357 //____________________________________________________________________________
358 void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * DL, RecPointsList * emcl, 
359                                         RecPointsList * ppsdl, TrackSegmentsList * trsl)
360 {
361   // main function, does the job
362
363   Int_t PHOSMod      = 1 ;
364   Int_t emcStopedAt  = 0 ; 
365   Int_t ppsdStopedAt = 0 ; 
366   
367   TObjArray * EmcRecPoints     = new TObjArray(100) ;  // these arrays keep pointers 
368   TObjArray * PpsdRecPointsUp  = new TObjArray(100) ;  // to RecPoints, which are 
369   TObjArray * PpsdRecPointsLow = new TObjArray(100) ;  // kept in TClonesArray's emcl and ppsdl
370   
371   
372   TClonesArray * LinkLowArray = new TClonesArray("AliPHOSLink", 100);
373   TClonesArray * LinkUpArray  = new TClonesArray("AliPHOSLink", 100); 
374   
375   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
376   
377   while(PHOSMod <= geom->GetNModules() ){
378
379     cout << PHOSMod << " Track1 " << endl ;
380     
381     FillOneModule(DL, emcl, EmcRecPoints, ppsdl, PpsdRecPointsUp, PpsdRecPointsLow, PHOSMod , emcStopedAt, ppsdStopedAt) ;
382     cout << PHOSMod << " Track2 " << endl ;
383     MakeLinks(EmcRecPoints, PpsdRecPointsUp, PpsdRecPointsLow, LinkLowArray, LinkUpArray) ; 
384
385     cout << PHOSMod << " Track3 " << endl ;
386     MakePairs(EmcRecPoints, PpsdRecPointsUp, PpsdRecPointsLow, LinkLowArray, LinkUpArray, trsl) ;
387  
388     EmcRecPoints->Clear() ;
389  
390     PpsdRecPointsUp->Clear() ;
391   
392     PpsdRecPointsLow->Clear() ;
393
394     LinkUpArray->Clear();
395    
396     LinkLowArray->Clear();
397    
398   }
399   delete EmcRecPoints ; 
400   EmcRecPoints = 0 ; 
401
402   delete PpsdRecPointsUp ; 
403   PpsdRecPointsUp = 0 ; 
404
405   delete PpsdRecPointsLow ; 
406   PpsdRecPointsLow = 0 ; 
407
408   delete LinkUpArray ; 
409   LinkUpArray = 0  ; 
410
411   delete LinkLowArray ; 
412   LinkLowArray = 0 ; 
413 }
414
415 //____________________________________________________________________________
416 Double_t  AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
417
418 // If you change this function, change also gradiend evaluation  in ChiSquare()
419   Double_t r4 = r*r*r*r ;
420   Double_t r295 = TMath::Power(r, 2.95) ;
421   Double_t shape = TMath::Exp( -r4 * (1. / (2.32 + 0.26 * r4) + 0.0316 / (1 + 0.0652 * r295) ) ) ;
422   return shape ;
423 }
424
425 //____________________________________________________________________________
426 void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * DL, RecPointsList * emcIn,  AliPHOSEmcRecPoint * iniEmc, 
427                                          Int_t Nmax, int * maxAt, Float_t * maxAtEnergy, TObjArray * emcList)
428
429   // fits cluster with Nmax overlapping showers 
430   
431   Int_t NPar = 3 * Nmax ;
432   Float_t FitParameters[NPar] ;
433   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
434
435   Bool_t rv = FindFit(iniEmc, maxAt, maxAtEnergy, NPar, FitParameters) ;
436   if( !rv )  // Fit failed, return and remove cluster
437     return ;
438   
439   Float_t xDigit ;
440   Float_t zDigit ;
441   Int_t RelId[4] ;
442
443   Int_t Ndigits = iniEmc->GetMultiplicity() ;  
444   Float_t xpar  ;
445   Float_t zpar  ;
446   Float_t Epar  ;
447   Float_t Distance ;
448   Float_t Ratio ;
449   Float_t Efit[Ndigits] ;
450   Int_t iparam ;
451   Int_t iDigit ;
452   
453   AliPHOSDigit * digit ;
454   AliPHOSEmcRecPoint * emcRP ;  
455   int * emcDigits = iniEmc->GetDigitsList() ;
456   Float_t * emcEnergies = iniEmc->GetEnergiesList() ;
457
458   Int_t iRecPoint = emcIn->GetEntries() ;
459
460   for(iDigit = 0 ; iDigit < Ndigits ; iDigit ++){
461     digit = (AliPHOSDigit *) emcDigits[iDigit];
462     geom->AbsToRelNumbering(digit->GetId(), RelId) ;
463     geom->RelPosInModule(RelId, xDigit, zDigit) ;
464     Efit[iDigit] = 0;
465     iparam = 0 ;
466     
467     while(iparam < NPar ){
468       xpar = FitParameters[iparam] ;
469       zpar = FitParameters[iparam+1] ;
470       Epar = FitParameters[iparam+2] ;
471       iparam += 3 ;
472       Distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
473       Distance =  TMath::Sqrt(Distance) ;
474       Efit[iDigit] += Epar * ShowerShape(Distance) ;
475     }
476
477   }
478
479   iparam = 0 ;
480   Float_t eDigit ;
481
482   while(iparam < NPar ){
483     xpar = FitParameters[iparam] ;
484     zpar = FitParameters[iparam+1] ;
485     Epar = FitParameters[iparam+2] ;
486     iparam += 3 ;
487     new ((*emcIn)[iRecPoint]) AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
488     emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint++);
489
490     for(iDigit = 0 ; iDigit < Ndigits ; iDigit ++){
491       digit = (AliPHOSDigit *) emcDigits[iDigit];
492       geom->AbsToRelNumbering(digit->GetId(), RelId) ;
493       geom->RelPosInModule(RelId, xDigit, zDigit) ;
494       Distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
495       Distance =  TMath::Sqrt(Distance) ;
496       Ratio = Epar * ShowerShape(Distance) / Efit[iDigit] ; 
497       eDigit = emcEnergies[iDigit] * Ratio ;
498       emcRP->AddDigit( *digit, eDigit ) ;
499     }
500
501     emcList->Add(emcRP) ;
502
503   }
504 }
505 //______________________________________________________________________________
506 void UnfoldingChiSquare(Int_t &NPar, Double_t *Grad, Double_t & fret, Double_t *x, Int_t iflag)
507 {
508
509 // Number of paramters, Gradient , Chi squared, parameters, what to do
510
511   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
512
513   AliPHOSEmcRecPoint * emcRP = (AliPHOSEmcRecPoint *) gMinuit->GetObjectFit() ; // EmcRecPoint to fit
514   int * emcDigits = emcRP->GetDigitsList() ;
515   Float_t * emcEnergies = emcRP->GetEnergiesList() ;
516   fret = 0. ;     
517   Int_t iparam ;
518
519   if(iflag == 2)
520     for(iparam = 0 ; iparam < NPar ; iparam++)    
521       Grad[iparam] = 0 ; // Will evaluate gradient
522   
523   Double_t Efit ;  
524   
525   AliPHOSDigit * digit ;
526   Int_t iDigit = 0 ;
527
528   while ( (digit = (AliPHOSDigit *)emcDigits[iDigit] )){
529     Int_t RelId[4] ;
530     Float_t xDigit ;
531     Float_t zDigit ;
532     geom->AbsToRelNumbering(digit->GetId(), RelId) ;
533     geom->RelPosInModule(RelId, xDigit, zDigit) ;
534     
535      if(iflag == 2){  // calculate gradient
536        Int_t iParam = 0 ;
537        Efit = 0 ;
538        while(iParam < NPar ){
539          Double_t Distance = (xDigit - x[iParam]) * (xDigit - x[iParam]) ;
540          iParam++ ; 
541          Distance += (zDigit - x[iParam]) * (zDigit - x[iParam]) ; 
542          Distance = TMath::Sqrt( Distance ) ; 
543          iParam++ ;      
544          Efit += x[iParam] * AliPHOSTrackSegmentMakerv1::ShowerShape(Distance) ;
545          iParam++ ;
546        }
547        Double_t sum = 2. * (Efit - emcEnergies[iDigit]) / emcEnergies[iDigit] ; // Here we assume, that sigma = sqrt(E) 
548        iParam = 0 ;
549        while(iParam < NPar ){
550          Double_t xpar = x[iParam] ;
551          Double_t zpar = x[iParam+1] ;
552          Double_t Epar = x[iParam+2] ;
553          Double_t dr = TMath::Sqrt( (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) );
554          Double_t shape = sum * AliPHOSTrackSegmentMakerv1::ShowerShape(dr) ;
555          Double_t r4 = dr*dr*dr*dr ;
556          Double_t r295 = TMath::Power(dr,2.95) ;
557          Double_t deriv =-4. * dr*dr * ( 2.32 / ( (2.32 + 0.26 * r4) * (2.32 + 0.26 * r4) ) +
558                                          0.0316 * (1. + 0.0171 * r295) / ( ( 1. + 0.0652 * r295) * (1. + 0.0652 * r295) ) ) ;
559          
560          Grad[iParam] += Epar * shape * deriv * (xpar - xDigit) ;  // Derivative over x    
561          iParam++ ; 
562          Grad[iParam] += Epar * shape * deriv * (zpar - zDigit) ;  // Derivative over z         
563          iParam++ ; 
564          Grad[iParam] += shape ;                                  // Derivative over energy             
565          iParam++ ; 
566        }
567      }
568      Efit = 0;
569      iparam = 0 ;
570      while(iparam < NPar ){
571        Double_t xpar = x[iparam] ;
572        Double_t zpar = x[iparam+1] ;
573        Double_t Epar = x[iparam+2] ;
574        iparam += 3 ;
575        Double_t Distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
576        Distance =  TMath::Sqrt(Distance) ;
577        Efit += Epar * AliPHOSTrackSegmentMakerv1::ShowerShape(Distance) ;
578      }
579      fret += (Efit-emcEnergies[iDigit])*(Efit-emcEnergies[iDigit])/emcEnergies[iDigit] ; 
580      // Here we assume, that sigma = sqrt(E)
581      iDigit++ ;
582   }
583 }