]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSTrackSegmentMakerv1.cxx
Limiting the number of primaries to 3 in the integer array
[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   fUnfoldFlag = kTRUE ; 
56 }
57
58 //____________________________________________________________________________
59  AliPHOSTrackSegmentMakerv1::~AliPHOSTrackSegmentMakerv1()
60
61   // dtor
62   delete fMinuit ; 
63 }
64 //____________________________________________________________________________
65 Bool_t  AliPHOSTrackSegmentMakerv1::FindFit(AliPHOSEmcRecPoint * emcRP, int * maxAt, Float_t * maxAtEnergy,
66                                     Int_t nPar, Float_t * fitparameters)
67
68   // Calls TMinuit for fitting cluster with several maxima 
69
70   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
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   
153   Int_t nEmcUnfolded = emcIn->GetEntries() ;
154   for(index = emcStopedAt; index < nEmcUnfolded; index++){
155     emcRecPoint = (AliPHOSEmcRecPoint *) (*emcIn)[index] ;
156
157     if(emcRecPoint->GetPHOSMod() != phosmod )  
158        break ;
159     
160     Int_t nMultipl = emcRecPoint->GetMultiplicity() ; 
161     Int_t maxAt[nMultipl] ;
162     Float_t maxAtEnergy[nMultipl] ;
163     Int_t nMax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
164
165     if(nMax <= 1 )     // if cluster is very flat (no pronounced maximum) then nMax = 0 
166       emcOut->Add(emcRecPoint) ;
167     else if (fUnfoldFlag) {
168       UnfoldClusters(Dl, emcIn, emcRecPoint, nMax, maxAt, maxAtEnergy, emcOut) ;
169       emcIn->Remove(emcRecPoint); 
170       emcIn->Compress() ;
171       nEmcUnfolded-- ;
172       index-- ;
173     }
174   }
175   emcStopedAt = index ;
176
177   for(index = ppsdStopedAt; index < ppsdIn->GetEntries(); index++){
178     ppsdRecPoint = (AliPHOSPpsdRecPoint *) (*ppsdIn)[index] ;
179     if(ppsdRecPoint->GetPHOSMod() != phosmod )   
180       break ;
181     if(ppsdRecPoint->GetUp() ) 
182       ppsdOutUp->Add(ppsdRecPoint) ;
183     else  
184       ppsdOutLow->Add(ppsdRecPoint) ;
185   }
186   ppsdStopedAt = index ;
187    
188   emcOut->Sort() ;
189   ppsdOutUp->Sort() ;
190   ppsdOutLow->Sort() ;   
191 }
192 //____________________________________________________________________________
193 Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * emcclu,AliPHOSPpsdRecPoint * PpsdClu, Bool_t &toofar)
194 {
195   Float_t r = fR0 ;
196  
197   TVector3 vecEmc ;
198   TVector3 vecPpsd ;
199   
200   emcclu->GetLocalPosition(vecEmc) ;
201   PpsdClu->GetLocalPosition(vecPpsd)  ; 
202   if(emcclu->GetPHOSMod() == PpsdClu->GetPHOSMod()){ 
203     if(vecPpsd.X() >= vecEmc.X() - fDelta ){ 
204       if(vecPpsd.Z() >= vecEmc.Z() - fDelta ){
205         AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
206         // Correct to difference in CPV and EMC position due to different distance to center.
207         // we assume, that particle moves from center
208         Float_t dCPV = geom->GetIPtoOuterCoverDistance();
209         Float_t dEMC = geom->GetIPtoCrystalSurface() ;
210         dEMC         = dEMC / dCPV ;
211         vecPpsd = dEMC * vecPpsd  - vecEmc ; 
212         r = vecPpsd.Mag() ;
213       } // if  zPpsd >= zEmc - fDelta
214       toofar = kFALSE ;
215     } // if  xPpsd >= xEmc - fDelta
216     else 
217       toofar = kTRUE ;
218   } 
219   else 
220     toofar = kTRUE ;
221   
222   return r ;
223 }
224
225 //____________________________________________________________________________
226 void  AliPHOSTrackSegmentMakerv1::MakeLinks(TObjArray * emcRecPoints, TObjArray * ppsdRecPointsUp, 
227                                      TObjArray * ppsdRecPointsLow, TClonesArray * linklowArray, 
228                                      TClonesArray *linkupArray) 
229
230   //Finds distanses (links) between all EMC and PPSD clusters, which are not further from each other than fR0 
231
232   TIter nextEmc(emcRecPoints) ;
233   Int_t iEmcClu = 0 ; 
234   
235   AliPHOSPpsdRecPoint * ppsdlow ; 
236   AliPHOSPpsdRecPoint * ppsdup ;
237   AliPHOSEmcRecPoint * emcclu ;
238   
239   Int_t iLinkLow = 0 ;
240   Int_t iLinkUp  = 0 ;
241   
242   while( (emcclu = (AliPHOSEmcRecPoint*)nextEmc() ) ) {
243     Bool_t toofar ;
244     TIter nextPpsdLow(ppsdRecPointsLow ) ;
245     Int_t iPpsdLow = 0 ;
246     
247     while( (ppsdlow = (AliPHOSPpsdRecPoint*)nextPpsdLow() ) ) { 
248       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdlow, toofar) ;
249       
250       if(toofar) 
251         break ;  
252       if(r < fR0) 
253         new( (*linklowArray)[iLinkLow++]) AliPHOSLink(r, iEmcClu, iPpsdLow) ;
254       
255       iPpsdLow++ ;
256       
257     }
258     
259     TIter nextPpsdUp(ppsdRecPointsUp ) ;
260     Int_t iPpsdUp = 0 ;
261     
262     while( (ppsdup = (AliPHOSPpsdRecPoint*)nextPpsdUp() ) ) { 
263       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdup, toofar) ;
264       
265       if(toofar)
266         break ;  
267       if(r < fR0) 
268         new( (*linkupArray)[iLinkUp++]) AliPHOSLink(r, iEmcClu, iPpsdUp) ;
269       
270       iPpsdUp++ ;
271       
272     }
273     
274     iEmcClu++ ; 
275     
276   } // while nextEmC
277   
278   linklowArray->Sort() ; //first links with smallest distances
279   linkupArray->Sort() ;
280 }
281     
282 //____________________________________________________________________________
283 void  AliPHOSTrackSegmentMakerv1::MakePairs(TObjArray * emcRecPoints, TObjArray * ppsdRecPointsUp, 
284                                     TObjArray * ppsdRecPointsLow, TClonesArray * linklowArray, 
285                                     TClonesArray * linkupArray, TrackSegmentsList * trsl) 
286
287
288   // Finds the smallest links and makes pairs of PPSD and EMC clusters with smallest distance 
289   
290   TIter nextLow(linklowArray) ;
291   TIter nextUp(linkupArray) ;
292   
293   AliPHOSLink * linkLow ;
294   AliPHOSLink * linkUp ;
295
296   AliPHOSEmcRecPoint * emc ;
297   AliPHOSPpsdRecPoint * ppsdLow ;
298   AliPHOSPpsdRecPoint * ppsdUp ;
299
300   AliPHOSRecPoint * nullpointer = 0 ;
301
302   while ( (linkLow =  (AliPHOSLink *)nextLow() ) ){
303     emc = (AliPHOSEmcRecPoint *) emcRecPoints->At(linkLow->GetEmc()) ;
304     ppsdLow = (AliPHOSPpsdRecPoint *) ppsdRecPointsLow->At(linkLow->GetPpsd()) ;
305     if( (emc) && (ppsdLow) ){ // RecPoints not removed yet 
306          ppsdUp = 0 ;
307          
308          while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){  
309            if(linkLow->GetEmc() == linkUp->GetEmc() ){
310              ppsdUp = (AliPHOSPpsdRecPoint *) ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
311              break ;
312            }
313         
314          } // while nextUp
315          
316          nextUp.Reset();
317          AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
318          trsl->Add(subtr) ;  
319          emcRecPoints->AddAt(nullpointer,linkLow->GetEmc()) ;     
320          ppsdRecPointsLow->AddAt(nullpointer,linkLow->GetPpsd()) ;
321          
322          if(ppsdUp)  
323            ppsdRecPointsUp->AddAt(nullpointer,linkUp->GetPpsd()) ;
324          
325     } 
326   } 
327    
328   TIter nextEmc(emcRecPoints) ;          
329   nextEmc.Reset() ;
330
331   while( (emc = (AliPHOSEmcRecPoint*)nextEmc()) ){ //to create pairs if no ppsdlow
332     ppsdLow = 0 ; 
333     ppsdUp  = 0 ;
334     
335     while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){
336       
337       if(emcRecPoints->IndexOf(emc) == linkUp->GetEmc() ){
338         ppsdUp = (AliPHOSPpsdRecPoint *) ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
339         break ;
340       }
341       
342     }
343     AliPHOSTrackSegment * subtr = new AliPHOSTrackSegment(emc, ppsdUp, ppsdLow ) ;
344     trsl->Add(subtr) ;   
345  
346     if(ppsdUp)  
347       ppsdRecPointsUp->AddAt(nullpointer,linkUp->GetPpsd()) ;
348   }
349      
350 }
351
352 //____________________________________________________________________________
353 void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * DL, RecPointsList * emcl, 
354                                         RecPointsList * ppsdl, TrackSegmentsList * trsl)
355 {
356   // main function, does the job
357
358   Int_t phosmod      = 1 ;
359   Int_t emcStopedAt  = 0 ; 
360   Int_t ppsdStopedAt = 0 ; 
361   
362   TObjArray * emcRecPoints     = new TObjArray(100) ;  // these arrays keep pointers 
363   TObjArray * ppsdRecPointsUp  = new TObjArray(100) ;  // to RecPoints, which are 
364   TObjArray * ppsdRecPointsLow = new TObjArray(100) ;  // kept in TClonesArray's emcl and ppsdl
365   
366   
367   TClonesArray * linklowArray = new TClonesArray("AliPHOSLink", 100);
368   TClonesArray * linkupArray  = new TClonesArray("AliPHOSLink", 100); 
369   
370   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
371   
372   while(phosmod <= geom->GetNModules() ){
373     
374     FillOneModule(DL, emcl, emcRecPoints, ppsdl, ppsdRecPointsUp, ppsdRecPointsLow, phosmod, emcStopedAt, ppsdStopedAt) ;
375
376     MakeLinks(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray) ; 
377
378     MakePairs(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray, trsl) ;
379  
380     emcRecPoints->Clear() ;
381  
382     ppsdRecPointsUp->Clear() ;
383   
384     ppsdRecPointsLow->Clear() ;
385
386     linkupArray->Clear() ;
387    
388     linklowArray->Clear() ;
389    
390     phosmod++ ; 
391   }
392   delete emcRecPoints ; 
393   emcRecPoints = 0 ; 
394
395   delete ppsdRecPointsUp ; 
396   ppsdRecPointsUp = 0 ; 
397
398   delete ppsdRecPointsLow ; 
399   ppsdRecPointsLow = 0 ; 
400
401   delete linkupArray ; 
402   linkupArray = 0  ; 
403
404   delete linklowArray ; 
405   linklowArray = 0 ; 
406 }
407
408 //____________________________________________________________________________
409 Double_t  AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
410
411 // If you change this function, change also gradiend evaluation  in ChiSquare()
412   Double_t r4    = r*r*r*r ;
413   Double_t r295  = TMath::Power(r, 2.95) ;
414   Double_t shape = TMath::Exp( -r4 * (1. / (2.32 + 0.26 * r4) + 0.0316 / (1 + 0.0652 * r295) ) ) ;
415   return shape ;
416 }
417
418 //____________________________________________________________________________
419 void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * DL, RecPointsList * emcIn,  AliPHOSEmcRecPoint * iniEmc, 
420                                          Int_t nMax, int * maxAt, Float_t * maxAtEnergy, TObjArray * emcList)
421
422   // fits cluster with nMax overlapping showers 
423   
424   Int_t nPar = 3 * nMax ;
425   Float_t fitparameters[nPar] ;
426   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
427
428   Bool_t rv = FindFit(iniEmc, maxAt, maxAtEnergy, nPar, fitparameters) ;
429   if( !rv )  // Fit failed, return and remove cluster
430     return ;
431   
432   Float_t xDigit ;
433   Float_t zDigit ;
434   Int_t relid[4] ;
435
436   Int_t nDigits = iniEmc->GetMultiplicity() ;  
437   Float_t xpar  ;
438   Float_t zpar  ;
439   Float_t epar  ;
440   Float_t distance ;
441   Float_t ratio ;
442   Float_t efit[nDigits] ;
443   Int_t iparam ;
444   Int_t iDigit ;
445   
446   AliPHOSDigit * digit ;
447   AliPHOSEmcRecPoint * emcRP ;  
448   Int_t * emcDigits = iniEmc->GetDigitsList() ;
449   Float_t * emcEnergies = iniEmc->GetEnergiesList() ;
450
451   Int_t iRecPoint = emcIn->GetEntries() ;
452
453   for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
454     digit = (AliPHOSDigit *) emcDigits[iDigit];
455     geom->AbsToRelNumbering(digit->GetId(), relid) ;
456     geom->RelPosInModule(relid, xDigit, zDigit) ;
457     efit[iDigit] = 0;
458     iparam = 0 ;
459     
460     while(iparam < nPar ){
461       xpar = fitparameters[iparam] ;
462       zpar = fitparameters[iparam+1] ;
463       epar = fitparameters[iparam+2] ;
464       iparam += 3 ;
465       distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
466       distance =  TMath::Sqrt(distance) ;
467       efit[iDigit] += epar * ShowerShape(distance) ;
468     }
469   }
470
471   iparam = 0 ;
472   Float_t eDigit ;
473
474   while(iparam < nPar ){
475     xpar = fitparameters[iparam] ;
476     zpar = fitparameters[iparam+1] ;
477     epar = fitparameters[iparam+2] ;
478     iparam += 3 ;
479     new ((*emcIn)[iRecPoint]) AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
480     emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint++);
481
482     for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
483       digit = (AliPHOSDigit *) emcDigits[iDigit];
484       geom->AbsToRelNumbering(digit->GetId(), relid) ;
485       geom->RelPosInModule(relid, xDigit, zDigit) ;
486       distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
487       distance =  TMath::Sqrt(distance) ;
488       ratio = epar * ShowerShape(distance) / efit[iDigit] ; 
489       eDigit = emcEnergies[iDigit] * ratio ;
490       emcRP->AddDigit( *digit, eDigit ) ;
491     }
492
493     emcList->Add(emcRP) ;
494
495   }
496 }
497
498 //______________________________________________________________________________
499 void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
500 {
501
502 // Number of parameters, Gradient, Chi squared, parameters, what to do
503
504   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
505
506   AliPHOSEmcRecPoint * emcRP = (AliPHOSEmcRecPoint *) gMinuit->GetObjectFit() ; // EmcRecPoint to fit
507   Int_t * emcDigits     = emcRP->GetDigitsList() ;
508   Float_t * emcEnergies = emcRP->GetEnergiesList() ;
509   fret = 0. ;     
510   Int_t iparam ;
511
512   if(iflag == 2)
513     for(iparam = 0 ; iparam < nPar ; iparam++)    
514       Grad[iparam] = 0 ; // Will evaluate gradient
515   
516   Double_t efit ;  
517   
518   AliPHOSDigit * digit ;
519   Int_t iDigit = 0 ;
520
521   while ( (digit = (AliPHOSDigit *)emcDigits[iDigit] )){
522     Int_t relid[4] ;
523     Float_t xDigit ;
524     Float_t zDigit ;
525     geom->AbsToRelNumbering(digit->GetId(), relid) ;
526     geom->RelPosInModule(relid, xDigit, zDigit) ;
527     
528      if(iflag == 2){  // calculate gradient
529        Int_t iParam = 0 ;
530        efit = 0 ;
531        while(iParam < nPar ){
532          Double_t distance = (xDigit - x[iParam]) * (xDigit - x[iParam]) ;
533          iParam++ ; 
534          distance += (zDigit - x[iParam]) * (zDigit - x[iParam]) ; 
535          distance = TMath::Sqrt( distance ) ; 
536          iParam++ ;      
537          efit += x[iParam] * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
538          iParam++ ;
539        }
540        Double_t sum = 2. * (efit - emcEnergies[iDigit]) / emcEnergies[iDigit] ; // Here we assume, that sigma = sqrt(E) 
541        iParam = 0 ;
542        while(iParam < nPar ){
543          Double_t xpar = x[iParam] ;
544          Double_t zpar = x[iParam+1] ;
545          Double_t epar = x[iParam+2] ;
546          Double_t dr = TMath::Sqrt( (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) );
547          Double_t shape = sum * AliPHOSTrackSegmentMakerv1::ShowerShape(dr) ;
548          Double_t r4 = dr*dr*dr*dr ;
549          Double_t r295 = TMath::Power(dr,2.95) ;
550          Double_t deriv =-4. * dr*dr * ( 2.32 / ( (2.32 + 0.26 * r4) * (2.32 + 0.26 * r4) ) +
551                                          0.0316 * (1. + 0.0171 * r295) / ( ( 1. + 0.0652 * r295) * (1. + 0.0652 * r295) ) ) ;
552          
553          Grad[iParam] += epar * shape * deriv * (xpar - xDigit) ;  // Derivative over x    
554          iParam++ ; 
555          Grad[iParam] += epar * shape * deriv * (zpar - zDigit) ;  // Derivative over z         
556          iParam++ ; 
557          Grad[iParam] += shape ;                                  // Derivative over energy             
558          iParam++ ; 
559        }
560      }
561      efit = 0;
562      iparam = 0 ;
563      while(iparam < nPar ){
564        Double_t xpar = x[iparam] ;
565        Double_t zpar = x[iparam+1] ;
566        Double_t epar = x[iparam+2] ;
567        iparam += 3 ;
568        Double_t distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
569        distance =  TMath::Sqrt(distance) ;
570        efit += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
571      }
572      fret += (efit-emcEnergies[iDigit])*(efit-emcEnergies[iDigit])/emcEnergies[iDigit] ; 
573      // Here we assume, that sigma = sqrt(E)
574      iDigit++ ;
575   }
576 }