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