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