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