]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSTrackSegmentMakerv1.cxx
New functions MakeTrackSegmentsCPV(), FillOneModule()
[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 "AliPHOSIndexToObject.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 = 10. ;   
56   //clusters are sorted in "rows" and "columns" of width geom->GetCrystalSize(0),
57   fDelta = fR0 + fGeom->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   gMinuit->SetPrintLevel(-1) ;           // No Printout
77   gMinuit->SetFCN(UnfoldingChiSquare) ;  // To set the address of the minimization function 
78   gMinuit->SetObjectFit(emcRP) ;         // To tranfer pointer to UnfoldingChiSquare
79
80   // filling initial values for fit parameters
81   AliPHOSDigit * digit ;
82
83   Int_t ierflg  = 0; 
84   Int_t index   = 0 ;
85   Int_t nDigits = (Int_t) nPar / 3 ;
86
87   Int_t iDigit ;
88
89
90   for(iDigit = 0; iDigit < nDigits; iDigit++){
91     digit = (AliPHOSDigit *) maxAt[iDigit]; 
92
93     Int_t relid[4] ;
94     Float_t x ;
95     Float_t z ;
96     fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
97     fGeom->RelPosInModule(relid, x, z) ;
98
99     Float_t energy = maxAtEnergy[iDigit] ;
100
101     gMinuit->mnparm(index, "x",  x, 0.1, 0, 0, ierflg) ;
102     index++ ;   
103     if(ierflg != 0){ 
104       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : x = " << x << endl ;
105       return kFALSE;
106     }
107     gMinuit->mnparm(index, "z",  z, 0.1, 0, 0, ierflg) ;
108     index++ ;   
109     if(ierflg != 0){
110       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : z = " << z << endl ;
111       return kFALSE;
112     }
113     gMinuit->mnparm(index, "Energy",  energy , 0.05*energy, 0., 4.*energy, ierflg) ;
114     index++ ;   
115     if(ierflg != 0){
116       cout << "PHOS Unfolding>  Unable to set initial value for fit procedure : energy = " << energy << endl ;      
117       return kFALSE;
118     }
119   }
120
121   Double_t p0 = 0.1 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
122                       //  depends on it. 
123   Double_t p1 = 1.0 ;
124   Double_t p2 = 0.0 ;
125
126   gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ;   // force TgMinuit to reduce function calls  
127   gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ;   // force TMinuit to use my gradient  
128   gMinuit->SetMaxIterations(5);
129   gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ;  // No Warnings
130
131   gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ;    // minimize 
132
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
144   return kTRUE;
145
146 }
147
148 //____________________________________________________________________________
149 void  AliPHOSTrackSegmentMakerv1::FillOneModule(AliPHOSRecPoint::RecPointsList * emcIn, 
150                                                 TArrayI * emcOut, 
151                                                 AliPHOSRecPoint::RecPointsList * ppsdIn, 
152                                                 TArrayI * ppsdOutUp,
153                                                 TArrayI * ppsdOutLow, 
154                                                 Int_t & phosmod, 
155                                                 Int_t & emcStopedAt, 
156                                                 Int_t & ppsdStopedAt)
157 {
158   // Fill xxxOut arrays with clusters from one PHOS module EMC+PPSD
159  
160   AliPHOSEmcRecPoint *  emcRecPoint  ; 
161   AliPHOSPpsdRecPoint * ppsdRecPoint ;
162   Int_t index ;
163   
164   Int_t nEmcUnfolded = emcIn->GetEntries() ;
165   emcOut->Set(nEmcUnfolded);
166   Int_t inEmcOut = 0 ;
167   for(index = emcStopedAt; index < nEmcUnfolded; index++){
168
169     emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
170     
171     if(emcRecPoint->GetPHOSMod() != phosmod )  
172       break ;
173     
174     emcOut->AddAt(emcRecPoint->GetIndexInList(),inEmcOut) ;
175     inEmcOut++ ; 
176   }
177   emcOut->Set(inEmcOut) ;
178
179   emcStopedAt = index ;
180
181   ppsdOutLow->Set(ppsdIn->GetEntries()) ;
182   ppsdOutUp->Set(ppsdIn->GetEntries()) ;
183   Int_t inPpsdLow = 0;
184   Int_t inPpsdUp = 0;
185   for(index = ppsdStopedAt; index < ppsdIn->GetEntries(); index++){
186     ppsdRecPoint = (AliPHOSPpsdRecPoint *) ppsdIn->At(index) ;
187     if(ppsdRecPoint->GetPHOSMod() != phosmod )   
188       break ;
189     if(ppsdRecPoint->GetUp() ) 
190       ppsdOutUp->AddAt(index,inPpsdUp++) ;
191     else  
192       ppsdOutLow->AddAt(index,inPpsdLow++) ;
193   }
194   ppsdOutLow->Set(inPpsdLow);
195   ppsdOutUp->Set(inPpsdUp);
196   ppsdStopedAt = index ;
197    
198 }
199 //____________________________________________________________________________
200 void  AliPHOSTrackSegmentMakerv1::FillOneModule(AliPHOSRecPoint::RecPointsList * emcIn, 
201                                                 TArrayI * emcOut, 
202                                                 AliPHOSRecPoint::RecPointsList * cpvIn, 
203                                                 TArrayI * cpvOut,
204                                                 Int_t & phosmod, 
205                                                 Int_t & emcStopedAt, 
206                                                 Int_t & cpvStopedAt)
207 {
208   // Fill xxxOut arrays with clusters from one PHOS module EMC+CPV
209  
210   AliPHOSEmcRecPoint * emcRecPoint  ; 
211   AliPHOSEmcRecPoint * cpvRecPoint ;
212   Int_t index ;
213   
214   Int_t nEmcUnfolded = emcIn->GetEntries() ;
215   emcOut->Set(nEmcUnfolded);
216   Int_t inEmcOut = 0 ;
217   for(index = emcStopedAt; index < nEmcUnfolded; index++){
218     emcRecPoint = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
219     if(emcRecPoint->GetPHOSMod() != phosmod )  
220       break ;
221     
222     emcOut->AddAt(emcRecPoint->GetIndexInList(),inEmcOut) ;
223     inEmcOut++ ; 
224   }
225   emcOut->Set(inEmcOut) ;
226   emcStopedAt = index ;
227
228   cpvOut->Set(cpvIn->GetEntries()) ;
229   Int_t inCpvOut = 0;
230   for(index = cpvStopedAt; index < cpvIn->GetEntries(); index++){
231     cpvRecPoint = (AliPHOSEmcRecPoint *) cpvIn->At(index) ;
232     if(cpvRecPoint->GetPHOSMod() != phosmod )   
233       break ;
234     else  
235       cpvOut->AddAt(index,inCpvOut++) ;
236   }
237   cpvOut->Set(inCpvOut);
238   cpvStopedAt = index ;
239    
240 }
241 //____________________________________________________________________________
242 Float_t  AliPHOSTrackSegmentMakerv1::GetDistanceInPHOSPlane(AliPHOSEmcRecPoint * emcclu,AliPHOSPpsdRecPoint * PpsdClu, Bool_t &toofar)
243 {
244   // Calculates the distance between the EMC RecPoint and the PPSD RecPoint
245  
246   Float_t r = fR0 ;
247  
248   TVector3 vecEmc ;
249   TVector3 vecPpsd ;
250   
251   emcclu->GetLocalPosition(vecEmc) ;
252   PpsdClu->GetLocalPosition(vecPpsd)  ; 
253   if(emcclu->GetPHOSMod() == PpsdClu->GetPHOSMod()){ 
254     if(vecPpsd.X() >= vecEmc.X() - fDelta ){ 
255       if(vecPpsd.Z() >= vecEmc.Z() - fDelta ){
256         // Correct to difference in CPV and EMC position due to different distance to center.
257         // we assume, that particle moves from center
258         Float_t dCPV = fGeom->GetIPtoOuterCoverDistance();
259         Float_t dEMC = fGeom->GetIPtoCrystalSurface() ;
260         dEMC         = dEMC / dCPV ;
261         vecPpsd = dEMC * vecPpsd  - vecEmc ; 
262         r = vecPpsd.Mag() ;
263       } // if  zPpsd >= zEmc - fDelta
264       toofar = kFALSE ;
265     } // if  xPpsd >= xEmc - fDelta
266     else 
267       toofar = kTRUE ;
268   } 
269   else 
270     toofar = kTRUE ;
271   
272   return r ;
273 }
274
275 //____________________________________________________________________________
276 void  AliPHOSTrackSegmentMakerv1::MakeLinks(TArrayI * emcRecPoints, TArrayI * ppsdRecPointsUp, 
277                                      TArrayI * ppsdRecPointsLow, TClonesArray * linklowArray, 
278                                      TClonesArray *linkupArray) 
279
280   // Finds distances (links) between all EMC and PPSD clusters, which are not further apart from each other than fR0 
281   
282   
283   AliPHOSPpsdRecPoint * ppsdlow ; 
284   AliPHOSPpsdRecPoint * ppsdup ;
285   AliPHOSEmcRecPoint * emcclu ;
286
287   Int_t iLinkLow = 0 ;
288   Int_t iLinkUp  = 0 ;
289   
290   Int_t iEmcRP;
291   
292   for(iEmcRP = 0; iEmcRP < emcRecPoints->GetSize(); iEmcRP++ ) {
293     emcclu = (AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emcRecPoints->At(iEmcRP),"emc") ;
294     Bool_t toofar ;
295     
296     Int_t iPpsdLow ;
297     
298     for(iPpsdLow = 0; iPpsdLow < ppsdRecPointsLow->GetSize();iPpsdLow++ ) {
299       
300       ppsdlow = (AliPHOSPpsdRecPoint *) fPlease->GimeRecPoint(ppsdRecPointsLow->At(iPpsdLow),"ppsd") ;
301       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdlow, toofar) ;
302       
303       if(toofar) 
304         break ;  
305       if(r < fR0){
306         new( (*linklowArray)[iLinkLow++]) AliPHOSLink(r, iEmcRP, iPpsdLow) ;
307       }
308     }
309     
310     Int_t iPpsdUp = 0 ;    
311     for(iPpsdUp = 0; iPpsdUp < ppsdRecPointsUp->GetSize();iPpsdUp++ ) { 
312       
313       ppsdup = (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdRecPointsUp->At(iPpsdUp),"ppsd") ;
314       Float_t r = GetDistanceInPHOSPlane(emcclu, ppsdup, toofar) ;
315       
316       if(toofar)
317         break ;  
318       if(r < fR0) { 
319         new( (*linkupArray)[iLinkUp++]) AliPHOSLink(r, iEmcRP, iPpsdUp) ;
320       }      
321     }
322   } 
323   
324   linklowArray->Sort() ; //first links with smallest distances
325   linkupArray->Sort() ;
326 }
327
328 //____________________________________________________________________________
329 void  AliPHOSTrackSegmentMakerv1::MakePairs(TArrayI * emcRecPoints, 
330                                             TArrayI * ppsdRecPointsUp, 
331                                             TArrayI * ppsdRecPointsLow, 
332                                             TClonesArray * linklowArray, 
333                                             TClonesArray * linkupArray, 
334                                             AliPHOSTrackSegment::TrackSegmentsList * trsl) 
335
336
337   // Finds the smallest links and makes pairs of PPSD and EMC clusters with smallest distance 
338   
339   TIter nextLow(linklowArray) ;
340   TIter nextUp(linkupArray) ;
341   
342   AliPHOSLink * linkLow ;
343   AliPHOSLink * linkUp ;
344
345   Int_t emc ;
346   Int_t ppsdLow ;
347   Int_t ppsdUp ;
348
349   AliPHOSPpsdRecPoint * nullpointer = 0 ;
350   ppsdUp = 0 ;
351
352   while ( (linkLow =  (AliPHOSLink *)nextLow() ) ){
353   
354     emc = emcRecPoints->At(linkLow->GetEmc()) ;
355     ppsdLow = ppsdRecPointsLow->At(linkLow->GetPpsd()) ;
356     if( (emc >= 0) && (ppsdLow >= 0) ){    // RecPoints not removed yet 
357
358       new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *)fPlease->GimeRecPoint(emc,"emc"), 
359                                                           nullpointer, 
360                                                           (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdLow,"ppsd") ) ;
361       ((AliPHOSTrackSegment* )trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);    
362       //replace index of emc to negative and shifted index of TS      
363       emcRecPoints->AddAt(-2 - fNTrackSegments,linkLow->GetEmc()) ;  
364       //replace index of emc to negative and shifted index of TS      
365       ppsdRecPointsLow->AddAt(-2 - fNTrackSegments,linkLow->GetPpsd()) ; 
366       fNTrackSegments++ ;
367
368     } 
369   } 
370          
371   while ( (linkUp =  (AliPHOSLink *)nextUp() ) ){  
372     emc = emcRecPoints->At(linkUp->GetEmc()) ;
373     if(emc != -1){ //without ppsd Up yet 
374
375       ppsdUp = ppsdRecPointsUp->At(linkUp->GetPpsd()) ;
376
377       if(emc >= 0){ //without ppsd Low => create new TS
378
379         fNTrackSegments = trsl->GetEntries() ; 
380         new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"), 
381                                                             (AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"), 
382                                                             nullpointer) ;
383         ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
384         fNTrackSegments++ ;
385       }
386       else{ // append ppsd Up to existing TS
387         ((AliPHOSTrackSegment *)trsl->At(-2-emc))->SetPpsdUpRecPoint((AliPHOSPpsdRecPoint *)fPlease->GimeRecPoint(ppsdUp,"ppsd"));
388       }
389       emcRecPoints->AddAt(-1,linkUp->GetEmc()) ; //Mark that PPSD Up found 
390       
391     } 
392   }      
393
394   Int_t iEmcRP ;
395   for(iEmcRP = 0; iEmcRP <emcRecPoints->GetSize() ; iEmcRP++ ){
396     emc = emcRecPoints->At(iEmcRP) ;
397     if(emc >=0 ){
398       ppsdUp = 0;
399       ppsdLow = 0;
400       new( (*trsl)[fNTrackSegments] ) AliPHOSTrackSegment((AliPHOSEmcRecPoint *) fPlease->GimeRecPoint(emc,"emc"), 
401                                                           nullpointer, nullpointer ) ;
402       ((AliPHOSTrackSegment *) trsl->At(fNTrackSegments))->SetIndexInList(fNTrackSegments);
403       fNTrackSegments++;    
404     }
405     
406   }
407   
408 }
409
410 //____________________________________________________________________________
411 void  AliPHOSTrackSegmentMakerv1::MakeTrackSegments(DigitsList * dl, 
412                                                     AliPHOSRecPoint::RecPointsList * emcl, 
413                                                     AliPHOSRecPoint::RecPointsList * ppsdl, 
414                                                     AliPHOSTrackSegment::TrackSegmentsList * trsl)
415 {
416   // Makes the track segments out of the list of EMC and PPSD Recpoints and stores them in a list
417   
418   Int_t phosmod      = 1 ;
419   Int_t emcStopedAt  = 0 ; 
420   Int_t ppsdStopedAt = 0 ; 
421
422   fNTrackSegments = 0 ; 
423
424   
425   TArrayI * emcRecPoints     = new TArrayI(1000) ;  // these arrays keep indexes 
426   TArrayI * ppsdRecPointsUp  = new TArrayI(1000) ;  // of RecPoints, which are 
427   TArrayI * ppsdRecPointsLow = new TArrayI(1000) ;  // kept in TClonesArray's emcl and ppsdl
428   
429   
430   TClonesArray * linklowArray = new TClonesArray("AliPHOSLink", 100);
431   TClonesArray * linkupArray  = new TClonesArray("AliPHOSLink", 100); 
432
433
434   if(fUnfoldFlag){
435     UnfoldAll(dl, emcl) ;      // Unfolds all EMC clusters
436   }
437
438
439   while(phosmod <= fGeom->GetNModules() ){
440     
441     FillOneModule(emcl, emcRecPoints, ppsdl, ppsdRecPointsUp, ppsdRecPointsLow, phosmod, emcStopedAt, ppsdStopedAt) ;
442     
443     MakeLinks(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray) ; 
444     
445     MakePairs(emcRecPoints, ppsdRecPointsUp, ppsdRecPointsLow, linklowArray, linkupArray, trsl) ;
446     
447     emcRecPoints->Reset() ;
448     
449     ppsdRecPointsUp->Reset() ;
450     
451     ppsdRecPointsLow->Reset() ;
452     
453     linkupArray->Clear() ;
454     
455     linklowArray->Clear() ;
456     
457     phosmod++ ; 
458   }
459
460   delete emcRecPoints ; 
461   emcRecPoints = 0 ; 
462   
463   delete ppsdRecPointsUp ; 
464   ppsdRecPointsUp = 0 ; 
465
466   delete ppsdRecPointsLow ; 
467   ppsdRecPointsLow = 0 ; 
468
469   delete linkupArray ; 
470   linkupArray = 0  ; 
471
472   delete linklowArray ; 
473   linklowArray = 0 ; 
474 }
475
476 //____________________________________________________________________________
477 void  AliPHOSTrackSegmentMakerv1::MakeTrackSegmentsCPV(DigitsList * dl, 
478                                                        AliPHOSRecPoint::RecPointsList * emcl, 
479                                                        AliPHOSRecPoint::RecPointsList * cpvl)
480 {
481   // Unfold clusters in EMC and CPV and refill reconstructed point lists emcl and ppsdl
482   // Yuri Kharlov. 19 October 2000
483   
484   fNTrackSegments = 0 ; 
485
486   TArrayI * emcRecPoints     = new TArrayI(1000) ;  // these arrays keep indexes 
487   TArrayI * cpvRecPoints     = new TArrayI(1000) ;  // of RecPoints, which are kept in emcl and ppsdl
488   
489   if(fUnfoldFlag){
490     UnfoldAll(dl, emcl) ;   // Unfolds all EMC clusters
491     UnfoldAll(dl, cpvl) ;   // Unfolds all CPV clusters
492   }
493
494 //    Int_t phosmod      = 1 ;
495 //    Int_t emcStopedAt  = 0 ; 
496 //    Int_t cpvStopedAt  = 0 ; 
497 //    while(phosmod <= fGeom->GetNModules() ){
498 //      FillOneModule(emcl, emcRecPoints, ppsdl, cpvRecPoints, phosmod, emcStopedAt, cpvStopedAt) ;
499 //      emcRecPoints->Reset() ;
500 //      cpvRecPoints->Reset() ;
501 //      phosmod++ ; 
502 //    }
503
504   delete emcRecPoints ; emcRecPoints = 0 ; 
505   delete cpvRecPoints ; cpvRecPoints = 0 ; 
506 }
507
508 //____________________________________________________________________________
509 Double_t  AliPHOSTrackSegmentMakerv1::ShowerShape(Double_t r)
510
511   // Shape of the shower (see PHOS TDR)
512   // If you change this function, change also the gradien evaluation  in ChiSquare()
513
514   Double_t r4    = r*r*r*r ;
515   Double_t r295  = TMath::Power(r, 2.95) ;
516   Double_t shape = TMath::Exp( -r4 * (1. / (2.32 + 0.26 * r4) + 0.0316 / (1 + 0.0652 * r295) ) ) ;
517   return shape ;
518 }
519
520 //____________________________________________________________________________
521 void  AliPHOSTrackSegmentMakerv1::UnfoldAll(DigitsList * dl, AliPHOSRecPoint::RecPointsList * emcIn) 
522 {
523   // Performs unfolding of all EMC clusters, sorts them and resets indexes in RecPoints
524
525   AliPHOSEmcRecPoint *  emcRecPoint  ; 
526   Int_t index ;
527   Int_t nEmcUnfolded = emcIn->GetEntries() ;
528   
529   for(index = 0 ; index < nEmcUnfolded; index++){
530
531     emcRecPoint   = (AliPHOSEmcRecPoint *) emcIn->At(index) ;
532     
533     Int_t nMultipl        = emcRecPoint->GetMultiplicity() ; 
534     Int_t   * maxAt       = new Int_t[nMultipl] ;
535     Float_t * maxAtEnergy = new Float_t[nMultipl] ;
536     Int_t nMax = emcRecPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy) ;
537     
538     if( nMax > 1 ) {     // if cluster is very flat (no pronounced maximum) then nMax = 0 
539       UnfoldClusters(dl, emcIn, emcRecPoint, nMax, maxAt, maxAtEnergy) ;
540       emcIn->Remove(emcRecPoint); 
541       emcIn->Compress() ;
542       index-- ;
543       nEmcUnfolded-- ;
544     }
545     
546     delete[] maxAt ; 
547     delete[] maxAtEnergy ; 
548   } //Unfolding finished
549
550   emcIn->Sort() ;
551
552   // to set index to new and correct index of old RecPoints
553   for( index = 0 ; index < emcIn->GetEntries() ; index++){
554     ((AliPHOSEmcRecPoint *) emcIn->At(index))->SetIndexInList(index) ;   
555   }
556
557   emcIn->Sort() ;
558
559 }
560 //____________________________________________________________________________
561 void  AliPHOSTrackSegmentMakerv1::UnfoldClusters(DigitsList * dl, 
562                                                  AliPHOSRecPoint::RecPointsList * emcIn, 
563                                                  AliPHOSEmcRecPoint * iniEmc, 
564                                                  Int_t nMax, 
565                                                  int * maxAt, 
566                                                  Float_t * maxAtEnergy)
567
568   // Performs the unfolding of a cluster with nMax overlapping showers 
569   // This is time consuming (use the (Un)SetUnfolFlag()  )
570
571   Int_t nPar = 3 * nMax ;
572   Float_t * fitparameters = new Float_t[nPar] ;
573
574
575   Bool_t rv = FindFit(iniEmc, maxAt, maxAtEnergy, nPar, fitparameters) ;
576   if( !rv ) {
577     // Fit failed, return and remove cluster
578     delete[] fitparameters ; 
579     return ;
580   }
581
582
583   Float_t xDigit ;
584   Float_t zDigit ;
585   Int_t relid[4] ;
586
587   Int_t nDigits = iniEmc->GetMultiplicity() ;  
588   Float_t xpar  ;
589   Float_t zpar  ;
590   Float_t epar  ;
591   Float_t distance ;
592   Float_t ratio ;
593   Float_t * efit = new Float_t[nDigits] ;
594   Int_t iparam ;
595   Int_t iDigit ;
596   
597   AliPHOSDigit * digit ;
598   AliPHOSEmcRecPoint * emcRP ;  
599   Int_t * emcDigits = iniEmc->GetDigitsList() ;
600   Float_t * emcEnergies = iniEmc->GetEnergiesList() ;
601
602   Int_t iRecPoint = emcIn->GetEntries() ;
603
604   for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
605     digit = fPlease->GimeDigit( emcDigits[iDigit] ) ;   
606     fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
607     fGeom->RelPosInModule(relid, xDigit, zDigit) ;
608     efit[iDigit] = 0;
609     iparam = 0 ;
610     
611     while(iparam < nPar ){
612       xpar = fitparameters[iparam] ;
613       zpar = fitparameters[iparam+1] ;
614       epar = fitparameters[iparam+2] ;
615       iparam += 3 ;
616       distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
617       distance =  TMath::Sqrt(distance) ;
618       efit[iDigit] += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
619     }
620   }
621
622   iparam = 0 ;
623   Float_t eDigit ;
624
625
626   while(iparam < nPar ){
627     xpar = fitparameters[iparam] ;
628     zpar = fitparameters[iparam+1] ;
629     epar = fitparameters[iparam+2] ;
630     iparam += 3 ;
631
632     if(iRecPoint >= emcIn->GetSize())
633       emcIn->Expand(2*iRecPoint) ;
634     (*emcIn)[iRecPoint] = new AliPHOSEmcRecPoint( iniEmc->GetLogWeightCut(), iniEmc->GetLocMaxCut() ) ;
635     
636     emcRP = (AliPHOSEmcRecPoint *) emcIn->At(iRecPoint);
637     iRecPoint++ ;
638
639     for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
640       digit = fPlease->GimeDigit( emcDigits[iDigit] ) ; 
641       fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
642       fGeom->RelPosInModule(relid, xDigit, zDigit) ;
643       distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
644       distance =  TMath::Sqrt(distance) ;
645       ratio = epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) / efit[iDigit] ; 
646       eDigit = emcEnergies[iDigit] * ratio ;
647       emcRP->AddDigit( *digit, eDigit ) ;
648     }
649
650   }
651   
652   delete[] fitparameters ; 
653   delete[] efit ; 
654
655 }
656
657 //______________________________________________________________________________
658 void UnfoldingChiSquare(Int_t & nPar, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
659 {
660   // Calculates th Chi square for the cluster unfolding minimization
661   // Number of parameters, Gradient, Chi squared, parameters, what to do
662   
663   AliPHOSEmcRecPoint * emcRP = (AliPHOSEmcRecPoint *) gMinuit->GetObjectFit() ; // EmcRecPoint to fit
664
665   Int_t * emcDigits     = emcRP->GetDigitsList() ;
666
667   Int_t nOfDigits = emcRP->GetDigitsMultiplicity() ; 
668
669   Float_t * emcEnergies = emcRP->GetEnergiesList() ;
670
671   AliPHOSGeometry * geom = AliPHOSGeometry::GetInstance() ;
672
673   AliPHOSIndexToObject * please = AliPHOSIndexToObject::GetInstance() ;
674
675   fret = 0. ;     
676   Int_t iparam ;
677
678   if(iflag == 2)
679     for(iparam = 0 ; iparam < nPar ; iparam++)    
680       Grad[iparam] = 0 ; // Will evaluate gradient
681   
682   Double_t efit ;    
683
684   AliPHOSDigit * digit ;
685   Int_t iDigit ;
686
687   for( iDigit = 0 ; iDigit < nOfDigits ; iDigit++) {
688
689     digit = please->GimeDigit( emcDigits[iDigit] ) ; 
690
691     Int_t relid[4] ;
692     Float_t xDigit ;
693     Float_t zDigit ;
694
695     geom->AbsToRelNumbering(digit->GetId(), relid) ;
696
697     geom->RelPosInModule(relid, xDigit, zDigit) ;
698
699      if(iflag == 2){  // calculate gradient
700        Int_t iParam = 0 ;
701        efit = 0 ;
702        while(iParam < nPar ){
703          Double_t distance = (xDigit - x[iParam]) * (xDigit - x[iParam]) ;
704          iParam++ ; 
705          distance += (zDigit - x[iParam]) * (zDigit - x[iParam]) ; 
706          distance = TMath::Sqrt( distance ) ; 
707          iParam++ ;      
708          efit += x[iParam] * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
709          iParam++ ;
710        }
711        Double_t sum = 2. * (efit - emcEnergies[iDigit]) / emcEnergies[iDigit] ; // Here we assume, that sigma = sqrt(E) 
712        iParam = 0 ;
713        while(iParam < nPar ){
714          Double_t xpar = x[iParam] ;
715          Double_t zpar = x[iParam+1] ;
716          Double_t epar = x[iParam+2] ;
717          Double_t dr = TMath::Sqrt( (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) );
718          Double_t shape = sum * AliPHOSTrackSegmentMakerv1::ShowerShape(dr) ;
719          Double_t r4 = dr*dr*dr*dr ;
720          Double_t r295 = TMath::Power(dr,2.95) ;
721          Double_t deriv =-4. * dr*dr * ( 2.32 / ( (2.32 + 0.26 * r4) * (2.32 + 0.26 * r4) ) +
722                                          0.0316 * (1. + 0.0171 * r295) / ( ( 1. + 0.0652 * r295) * (1. + 0.0652 * r295) ) ) ;
723          
724          Grad[iParam] += epar * shape * deriv * (xpar - xDigit) ;  // Derivative over x    
725          iParam++ ; 
726          Grad[iParam] += epar * shape * deriv * (zpar - zDigit) ;  // Derivative over z         
727          iParam++ ; 
728          Grad[iParam] += shape ;                                  // Derivative over energy             
729          iParam++ ; 
730        }
731      }
732      efit = 0;
733      iparam = 0 ;
734
735      while(iparam < nPar ){
736        Double_t xpar = x[iparam] ;
737        Double_t zpar = x[iparam+1] ;
738        Double_t epar = x[iparam+2] ;
739        iparam += 3 ;
740        Double_t distance = (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar)  ;
741        distance =  TMath::Sqrt(distance) ;
742        efit += epar * AliPHOSTrackSegmentMakerv1::ShowerShape(distance) ;
743      }
744
745      fret += (efit-emcEnergies[iDigit])*(efit-emcEnergies[iDigit])/emcEnergies[iDigit] ; 
746      // Here we assume, that sigma = sqrt(E)
747   }
748
749 }