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