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