]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSEvalRecPoint.cxx
Create ideal, residual and fully displaced alignment objects in one macro
[u/mrichter/AliRoot.git] / PHOS / AliPHOSEvalRecPoint.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 /* $Log:
18  */
19 //*-- Author: Boris Polichtchouk, IHEP
20 //////////////////////////////////////////////////////////////////////////////
21 // Reconstructed point operations for the Clusterization class for IHEP reconstruction.
22 // Performs clusterization (collects neighbouring active cells)
23 // It differs from AliPHOSClusterizerv1 in neighbour definition only
24
25 // ---- ROOT system ---
26 #include "TDirectory.h"
27 #include "TBranch.h"
28 #include "TTree.h"
29 #include "TROOT.h"
30 #include "TFolder.h"
31
32 // --- AliRoot header files ---
33 #include "AliLog.h"
34 #include "AliConfig.h"
35 #include "AliPHOSDigit.h" 
36 #include "AliPHOSClusterizer.h"
37 #include "AliPHOSEvalRecPoint.h"
38 #include "AliRun.h"
39 #include "AliPHOSLoader.h"
40 #include "AliPHOSRecCpvManager.h"
41 #include "AliPHOSRecEmcManager.h"
42 #include "AliPHOSDigitizer.h"
43 #include "AliPHOSGeometry.h" 
44
45 // --- Standard library ---
46
47
48 ClassImp(AliPHOSEvalRecPoint)
49
50   AliPHOSEvalRecPoint::AliPHOSEvalRecPoint(): fEventFolderName(AliConfig::GetDefaultEventFolderName())
51 {
52   // default ctor
53   fParent=-333;
54   fChi2Dof=-111;
55   fIsCpv = kTRUE;
56   fIsEmc = kFALSE;
57 }
58
59 AliPHOSEvalRecPoint::AliPHOSEvalRecPoint(Bool_t cpv, AliPHOSEvalRecPoint* parent) : AliPHOSCpvRecPoint()
60 {
61   // ctor
62   fParent=-333;
63   fChi2Dof=-111;
64
65 //    fParent=parent;
66   TObjArray* wPool = (TObjArray*)GetWorkingPool();
67   if(!wPool) {
68     Fatal("ctor", "Couldn't find working pool") ; 
69   }
70
71   fParent = wPool->IndexOf((TObject*)parent);
72   fChi2Dof = parent->Chi2Dof();
73
74   if(cpv) {
75     fIsEmc = kFALSE;
76     fIsCpv = kTRUE;
77   }
78   else {
79     fIsEmc = kTRUE;
80     fIsCpv = kFALSE;
81   }
82
83   // Add itself to working pool
84   AddToWorkingPool((TObject*)this);
85   
86 }
87
88 AliPHOSEvalRecPoint::AliPHOSEvalRecPoint(Int_t i, Bool_t cpv) : fEventFolderName(AliConfig::GetDefaultEventFolderName())
89 {
90   // ctor
91   fChi2Dof=-111;
92   fParent=-333;
93
94   AliPHOSEmcRecPoint* rp=0;
95
96   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
97
98   if(cpv) {
99     rp = (AliPHOSCpvRecPoint*)fLoader->CpvRecPoints()->At(i);
100     fIsEmc = kFALSE;
101     fIsCpv = kTRUE;
102   }
103   else {
104     rp = (AliPHOSEmcRecPoint*)fLoader->EmcRecPoints()->At(i);
105     fIsEmc = kTRUE;
106     fIsCpv = kFALSE;
107   }
108
109   Int_t* digits = rp->GetDigitsList();
110   Float_t* energies = rp->GetEnergiesList();
111   Int_t nDigits = rp->GetMultiplicity();
112   
113   for(Int_t iDigit=0; iDigit<nDigits; iDigit++) {
114     AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] );
115     Float_t eDigit = energies[iDigit];
116     this->AddDigit(*digit,eDigit);
117   }
118
119   TVector3 locpos;
120   rp->GetLocalPosition(locpos);
121   fLocPos = locpos; 
122
123   // Add itself to working pool
124   AddToWorkingPool((TObject*)this);
125    
126 }
127
128 AliPHOSClusterizer* AliPHOSEvalRecPoint::GetClusterizer()
129 {
130   // returns clusterizer task
131   TFolder* aliceF = (TFolder*)gROOT->FindObjectAny("YSAlice");
132   TFolder* wPoolF = (TFolder*)aliceF->FindObject("WhiteBoard/RecPoints/PHOS/SmP");
133   AliPHOSClusterizer* clu = (AliPHOSClusterizer*)wPoolF->FindObject("PHOS:clu-v1");
134   if(!clu) { 
135     Fatal("GetClusterizer", "Couldn't find Clusterizer") ; 
136   }
137
138   return clu;
139 }
140
141 Bool_t AliPHOSEvalRecPoint::TooClose(AliPHOSRecPoint* pt) const 
142 {
143   // check if a rec.point pt is too close to this one
144   TVector3 herPos;
145   TVector3 myPos;
146   pt->GetLocalPosition(herPos);
147   this->GetLocalPosition(myPos);
148   Float_t dx = herPos.X() - myPos.X();
149   Float_t dz = herPos.Z() - myPos.Z();
150   Float_t dr = TMath::Sqrt(dx*dx + dz*dz);
151
152   if(dr<GetReconstructionManager()->MergeGammasMinDistanceCut())
153     return kTRUE;
154   else
155     return kFALSE;
156
157 }
158
159 Bool_t AliPHOSEvalRecPoint::NeedToSplit() const 
160 {
161   // rec.point needs to be split
162   return kFALSE;
163 }
164
165 void AliPHOSEvalRecPoint::DeleteParent()
166 {
167   // delete parent
168   fParent=-333;
169 }
170
171 void AliPHOSEvalRecPoint::UpdateWorkingPool()
172 {
173   // update pool of rec.points
174   Int_t i; //loop variable
175   
176   for(i=0; i<this->InWorkingPool(); i++) {
177      AliPHOSEvalRecPoint* parent = (AliPHOSEvalRecPoint*)GetFromWorkingPool(i);
178      TObjArray children;
179      Int_t nChild = parent->HasChild(children);
180      for(Int_t iChild=0; iChild<nChild; iChild++)
181        {
182          ((AliPHOSEvalRecPoint*)children.At(iChild))->DeleteParent();
183        }
184
185      if(nChild) {
186        RemoveFromWorkingPool(parent);
187        delete parent;
188      }
189
190   }
191
192   for(i=0; i<this->InWorkingPool(); i++) {
193     AliPHOSEvalRecPoint* weak = (AliPHOSEvalRecPoint*)GetFromWorkingPool(i);
194     if (weak->KillWeakPoint()) delete weak;
195   }
196
197   for(i=0; i<this->InWorkingPool(); i++) {
198     AliPHOSEvalRecPoint* close = (AliPHOSEvalRecPoint*)GetFromWorkingPool(i);
199     close->MergeClosePoint();
200   }
201
202   for(i=0; i<this->InWorkingPool(); i++)
203     ((AliPHOSEvalRecPoint*)AliPHOSEvalRecPoint::GetFromWorkingPool(i))->SetIndexInList(i);
204
205 }
206
207 Int_t AliPHOSEvalRecPoint::HasChild(TObjArray& children)
208 {
209   // returns the number of children
210   for(Int_t iChild=0; iChild<InWorkingPool(); iChild++)
211     {
212       AliPHOSEvalRecPoint* child = (AliPHOSEvalRecPoint*)GetFromWorkingPool(iChild);
213       TObject* parent = (TObject*)child->Parent();
214       TObject* me = (TObject*)this;
215       if(parent==me) children.Add(child);
216     }
217
218   return children.GetEntries();
219 }
220
221 void AliPHOSEvalRecPoint::Init()
222 {
223   // initialization
224   AliPHOSClusterizer* clusterizer = GetClusterizer();
225   if(!clusterizer) {
226     Fatal("Init", "Cannot get clusterizer") ;
227   }
228
229   TClonesArray* digits = AliPHOSLoader::GetPHOSLoader(fEventFolderName)->Digits();
230   Float_t logWeight=0;  
231
232   if(this->IsEmc()) {
233     logWeight = clusterizer->GetEmcLogWeight();
234   }
235   else {
236     logWeight = clusterizer->GetCpvLogWeight();
237   }
238   
239   EvalLocalPosition(logWeight,digits); // evaluate initial position
240 }
241
242
243 void AliPHOSEvalRecPoint::MakeJob()
244 {
245   // Reconstruction algoritm implementation.
246
247   AliPHOSRecManager* recMng = GetReconstructionManager();
248
249   Init();
250
251   UnfoldLocalMaxima();
252   
253   TObjArray children;
254   Int_t nChild = HasChild(children);
255   
256   if(!nChild) {
257     EvaluatePosition();
258     if(Chi2Dof()>recMng->OneGamChisqCut()) SplitMergedShowers();
259   }
260
261   for(Int_t iChild=0; iChild<nChild; iChild++) {
262     AliPHOSEvalRecPoint* child = (AliPHOSEvalRecPoint*)children.At(iChild);
263     child->EvaluatePosition();
264
265     if(child->Chi2Dof()>recMng->OneGamChisqCut())
266       child->SplitMergedShowers();
267   }  
268
269
270
271 void AliPHOSEvalRecPoint::InitTwoGam(Float_t* gamma1, Float_t* gamma2)
272 {
273   //Compute start values for two gamma fit algorithm.
274   // gamma1[0], gamma1[1], gamma1[2] are Energy,x,z of the reconstructed "gammas".
275
276
277   TVector3 lpos; // start point choosing.
278   GetLocalPosition(lpos);
279   Float_t xx = lpos.Z();
280   Float_t yy = lpos.X();
281   Float_t e = GetEnergy();
282
283   AliInfo(Form("(x,z,e)[old] = (%f, %f, %f)", yy, xx, e)) ;
284
285 //    xx = XY(xx/e);
286 //    yy = XY(yy/e);
287
288
289   Float_t eDigit ;
290   AliPHOSDigit * digit ;
291   Int_t nDigits = GetMultiplicity();  
292   Int_t * digits = GetDigitsList();
293   Float_t * energies = GetEnergiesList();
294
295   Float_t ix ;
296   Float_t iy ;
297   Int_t relid[4] ; 
298
299   Float_t exx = 0;
300   Float_t eyy = 0;
301   Float_t exy = 0;
302   Float_t sqr;
303   Float_t cos2fi = 1.;
304
305   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
306   const AliPHOSGeometry* fGeom = fLoader->PHOSGeometry();
307
308   Int_t iDigit; //loop variable
309
310   for(iDigit = 0 ; iDigit < nDigits ; iDigit ++)
311     {
312       digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] ); 
313       eDigit = energies[iDigit];
314       fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
315       fGeom->RelPosInModule(relid, iy, ix);
316     
317       Float_t dx =  ix - xx;
318       Float_t dy =  iy - yy;
319       exx += eDigit*dx*dx;
320       eyy += eDigit*dy*dy;
321       exy += eDigit*dx*dy;
322       
323     }
324
325   sqr = TMath::Sqrt(4.*exy*exy + (exx-eyy)*(exx-eyy));
326   Float_t euu = (exx+eyy+sqr)/2.;
327   if(sqr>1.e-10) cos2fi = (exx - eyy)/sqr;
328   Float_t cosfi = TMath::Sqrt((1.+cos2fi)/2.);
329   Float_t sinfi = TMath::Sqrt((1.-cos2fi)/2.);
330   if(exy<0) sinfi = -sinfi;
331
332   Float_t eu3 = 0;
333   for(iDigit = 0 ; iDigit < nDigits ; iDigit ++)
334     {
335       digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] ); 
336       eDigit = energies[iDigit];
337       fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
338       fGeom->RelPosInModule(relid, iy, ix);
339     
340       Float_t dx =  ix - xx;
341       Float_t dy =  iy - yy;
342       Float_t du = dx*cosfi + dy*sinfi;
343       eu3 += eDigit*du*du*du;
344     }
345   
346   Float_t c = e*eu3*eu3/(euu*euu*euu)/2.;
347   Float_t sign = 1.;
348   if(eu3<0) sign = -1.;
349   Float_t r = 1.+ c + sign*TMath::Sqrt(2.*c + c*c);
350   Float_t u = 0;
351   if(TMath::Abs(r-1.)>0.1) u = eu3/euu*(r+1.)/(r-1.);
352   if(TMath::Abs(r-1.)<0.1) u = TMath::Sqrt(sqr/e/r)*(1.+r);
353   
354   Float_t e2c = e/(1.+r);
355   Float_t e1c = e-e2c;
356   Float_t u1 = -u/(1.+r);
357   Float_t u2 = u+u1;
358   
359   Float_t x1c = xx+u1*cosfi;
360   Float_t y1c = yy+u1*sinfi;
361   Float_t x2c = xx+u2*cosfi;
362   Float_t y2c = yy+u2*sinfi;
363
364 //    printf("e1c -> %f\n",e1c);
365 //    printf("x1c -> %f\n",x1c);
366 //    printf("y1c -> %f\n",y1c);
367 //    printf("e2c -> %f\n",e2c);
368 //    printf("x2c -> %f\n",x2c);
369 //    printf("y2c -> %f\n",y2c);
370
371   gamma1[0] = e1c;
372   gamma1[1] = y1c;
373   gamma1[2] = x1c;
374
375   gamma2[0] = e2c;
376   gamma2[1] = y2c;
377   gamma2[2] = x2c;
378   
379 }
380
381 void AliPHOSEvalRecPoint::TwoGam(Float_t* gamma1, Float_t* gamma2)
382 {
383   //Fitting algorithm for the two very closed gammas 
384   //that merged into the cluster with one maximum.
385   // Starting points gamma1 and gamma2 must be provided before ( see InitTwoGam)
386   //Set chisquare of the fit.
387
388
389   Float_t st0 = GetReconstructionManager()->TwoGamInitialStep();
390   Float_t chmin = GetReconstructionManager()->TwoGamChisqMin();
391   Float_t emin = GetReconstructionManager()->TwoGamEmin();
392   Float_t stpmin = GetReconstructionManager()->TwoGamStepMin();
393   Int_t nIter = GetReconstructionManager()->TwoGamNumOfIterations(); // Number of iterations.
394
395   Float_t chisq = 100.; //Initial chisquare.
396
397   Int_t nadc = GetMultiplicity();
398   if(nadc<3)
399       fChi2Dof= -111.;
400   
401   Int_t dof = nadc - 5;
402   if(dof<1) dof=1;
403   Float_t chstop = chmin*dof;
404
405   Float_t ch = 1.e+20;
406   Float_t st = st0;
407   Float_t grx1 = 0.;
408   Float_t gry1 = 0.;
409   Float_t grx2 = 0.;
410   Float_t gry2 = 0.;
411   Float_t gre = 0.;
412   Float_t gr = 1.;
413   Float_t ee1=0,xx1=0,yy1=0,ee2=0,xx2=0,yy2=0,cosi;
414
415   Float_t e1c = gamma1[0];
416   Float_t y1c = gamma1[1];
417   Float_t x1c = gamma1[2];
418
419   Float_t e2c = gamma2[0];
420   Float_t y2c = gamma2[1];
421   Float_t x2c = gamma2[2];
422
423   Float_t e = GetEnergy();
424
425   Float_t eDigit ;
426   AliPHOSDigit * digit ;
427   Int_t nDigits = GetMultiplicity();  
428   Int_t * digits = GetDigitsList();
429   Float_t * energies = GetEnergiesList();
430
431   Float_t ix ;
432   Float_t iy ;
433   Int_t relid[4] ; 
434
435   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
436   const AliPHOSGeometry* fGeom = fLoader->PHOSGeometry();
437
438   for(Int_t iter=0; iter<nIter; iter++)
439     {
440       Float_t chisqc = 0.;
441       Float_t grx1c = 0.;
442       Float_t gry1c = 0.;
443       Float_t grx2c = 0.;
444       Float_t gry2c = 0.;
445       Float_t grec = 0.;
446
447       for(Int_t iDigit = 0 ; iDigit < nDigits ; iDigit ++)
448         {
449           digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] ); 
450           eDigit = energies[iDigit];
451           fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
452           fGeom->RelPosInModule(relid, iy, ix);
453           
454           Float_t a1,gx1,gy1;
455           Float_t a2,gx2,gy2;
456           
457           Float_t dx1 =  x1c - ix;
458           Float_t dy1 =  y1c - iy;
459
460           GetReconstructionManager()->AG(e1c,dx1,dy1,a1,gx1,gy1);
461
462           Float_t dx2 =  x2c - ix;
463           Float_t dy2 =  y2c - iy;
464
465           GetReconstructionManager()->AG(e2c,dx2,dy2,a2,gx2,gy2);
466       
467           Float_t a = a1+a2;
468 //        Float_t D = Const*a*(1. - a/e);
469 //        if(D<0) D=0;
470
471 //  //            D = 9.; // ????
472           
473 //        Float_t da = a - eDigit;
474 //        chisqc += da*da/D;
475 //        Float_t dd = da/D;
476 //        dd = dd*(2.-dd*Const*(1.-2.*a/e));
477
478           Float_t dd;
479           chisqc += GetReconstructionManager()->TwoGamChi2(a,eDigit,e,dd);
480           
481           grx1c += gx1*dd;
482           gry1c += gy1*dd;
483           grx2c += gx2*dd;
484           gry2c += gy2*dd;
485           grec += (a1/e1c - a2/e2c)*e*dd;
486
487         }
488
489       Float_t grc = TMath::Sqrt(grx1c*grx1c + gry1c*gry1c + grx2c*grx2c + gry2c*gry2c);
490       if(grc<1.e-10) grc=1.e-10;
491
492       Float_t sc = 1. + chisqc/ch;
493       st = st/sc; 
494
495       if(chisqc>ch) 
496         goto loop20;
497       cosi = (grx1*grx1c + gry1*gry1c + grx2*grx2c + gry2*gry2c + gre*grec)/gr/grc;
498       st = st*sc/(1.4 - cosi);
499
500       ee1 = e1c;
501       xx1 = x1c;
502       yy1 = y1c;
503       ee2 = e2c;
504       xx2 = x2c;
505       yy2 = y2c;
506
507       ch = chisqc;
508
509       if(ch<chstop)
510         goto loop101;
511
512       grx1 = grx1c;
513       gry1 = gry1c;
514       grx2 = grx2c;
515       gry2 = gry2c;
516       gre = grec;
517       gr = grc;
518  
519     loop20: ;
520       Float_t step = st*gr;
521
522       AliInfo(Form("Iteration %d dof %d chisq/dof %f chstop/dof %f step %d stpmin %d",
523            iter, dof, ch/dof, chstop/dof, step, stpmin)) ;
524
525       
526       if(step<stpmin) 
527         goto loop101;
528
529       Float_t de = st*gre*e;
530       e1c = ee1 - de;
531       e2c = ee2 + de;
532       
533       if( (e1c>emin) && (e2c>emin) )
534         goto loop25;
535       st = st/2.;
536       goto loop20;
537       
538     loop25: ;
539       x1c = xx1 - st*grx1;
540       y1c = yy1 - st*gry1;
541       x2c = xx2 - st*grx2;
542       y2c = yy2 - st*gry2;
543
544
545     }
546
547  loop101: 
548
549 //    if(ch>chisq*(nadc-2)-delch)
550 //      return ch/dof;  
551   
552   chisq = ch/dof;
553   gamma1[0] = ee1;
554   gamma1[1] = yy1;
555   gamma1[2] = xx1;
556
557   gamma2[0] = ee2;
558   gamma2[1] = yy2;
559   gamma2[2] = xx2;
560
561   Float_t x1New = yy1;
562   Float_t z1New = xx1;
563   Float_t e1New = ee1;
564   Float_t x2New = yy2;
565   Float_t z2New = xx2;
566   Float_t e2New = ee2;
567
568   TString message ; 
569   message  = "     (x,z,e)[1 fit] = (%f, %f, %f)\n" ;  
570   message  = "     (x,z,e)[2 fit] = (%f, %f, %f)\n" ;  
571   AliInfo(Form(message.Data(), 
572        x1New, z1New, e1New, 
573        x2New, z2New, e2New)) ;
574
575   fChi2Dof = chisq;
576
577 }
578
579 void AliPHOSEvalRecPoint::UnfoldTwoMergedPoints(Float_t* gamma1, Float_t* gamma2)
580 {
581   //Unfold TWO merged rec. points in the case when cluster has only one maximum, 
582   //but it's fitting to the one gamma shower is too bad.
583   // Use TwoGam() to estimate the positions and energies of merged points.
584  
585   
586   Int_t nMax = 2;
587   Float_t* gamma;
588
589   Int_t* digits = GetDigitsList();
590   Int_t nDigits = GetMultiplicity();
591   Float_t* energies = GetEnergiesList();
592   Float_t* eFit = new Float_t[nDigits];
593
594   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
595   const AliPHOSGeometry* fGeom = fLoader->PHOSGeometry();
596
597   for(Int_t iDigit=0; iDigit<nDigits; iDigit++)
598     {
599       AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] ); 
600       Int_t relid[4] ;
601       fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
602       Float_t x,z;     
603       fGeom->RelPosInModule(relid, x, z);
604
605       Float_t gain = 0.;
606       for(Int_t iMax=0; iMax<nMax; iMax++)
607         {
608           if(iMax==0)
609             gamma = gamma1;
610           else
611             gamma = gamma2;
612
613           Float_t eMax = gamma[0];
614           Float_t xMax = gamma[1];
615           Float_t zMax = gamma[2];
616
617           Float_t dx = xMax - x;
618           Float_t dz = zMax - z;
619           Float_t amp,gx,gy;
620           GetReconstructionManager()->AG(eMax,dz,dx,amp,gx,gy); 
621           gain += amp;
622         }
623  
624       eFit[iDigit] = gain;
625     }
626
627
628   for( Int_t iMax=0; iMax<nMax; iMax++)
629     {      
630       if(iMax==0)
631         gamma = gamma1;
632       else
633         gamma = gamma2;
634
635       Float_t eMax = gamma[0];
636       Float_t xMax = gamma[1];
637       Float_t zMax = gamma[2];
638
639       AliPHOSEvalRecPoint* newRP = new AliPHOSEvalRecPoint(IsCPV(),this);
640       TVector3 newpos(xMax,0,zMax);
641       newRP->SetLocalPosition(newpos);
642
643       for( Int_t iDigit = 0 ; iDigit < nDigits ; iDigit ++)
644         {
645           AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] ); 
646           Float_t eDigit = energies[iDigit];
647           Int_t relid[4] ;
648           fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
649           Float_t ix,iz;
650           fGeom->RelPosInModule(relid, ix, iz);
651           
652           Float_t dx = xMax - ix;
653           Float_t dz = zMax - iz;
654           Float_t singleShowerGain,gxMax,gyMax;
655           GetReconstructionManager()->AG(eMax,dz,dx,singleShowerGain,gxMax,gyMax);
656           Float_t totalGain = eFit[iDigit];
657           Float_t ratio = singleShowerGain/totalGain; 
658           eDigit = eDigit*ratio;
659           newRP->AddDigit(*digit,eDigit);
660         }
661       AliInfo(Form("======= Split: daughter rec point %d =================", 
662                    iMax)) ;
663       newRP->Print();
664
665     }
666
667   delete[] eFit;
668   
669
670 }
671
672
673 void AliPHOSEvalRecPoint::EvaluatePosition() 
674 {
675   // One gamma fit algorithm.
676   // Set chisq/dof of the fit.
677   // gamma1[0], gamma1[1], gamma1[2] are Energy,x,z of the reconstructed gamma, respectively.
678
679   Int_t nDigits = GetMultiplicity();
680   if(nDigits<2)
681     return;
682
683   Int_t nIter = GetReconstructionManager()->OneGamNumOfIterations(); // number of iterations
684   Float_t st0 = GetReconstructionManager()->OneGamInitialStep(); // initial step
685 //    const Float_t stpMin = 0.005;
686   Float_t stpMin = GetReconstructionManager()->OneGamStepMin();
687   Float_t chmin =  GetReconstructionManager()->OneGamChisqMin();
688  
689   TVector3 locpos;
690   AliPHOSDigit* digit;
691   Float_t eDigit;
692   Int_t relid[4] ; 
693   Float_t ix, iy;
694
695   GetLocalPosition(locpos);
696   Float_t e = GetEnergy();
697   Float_t xc = locpos.Z();
698   Float_t yc = locpos.X();
699   Float_t dx,dy,gx,gy,grxc,gryc;
700   Float_t st = st0;
701   Float_t chisq = 1.e+20;
702   Float_t gr = 1.;
703   Float_t grx = 0.;
704   Float_t gry = 0.;
705   Int_t dof = GetMultiplicity() - 2;
706   if(dof<1)
707     dof = 1;
708   Float_t chstop = chmin*dof;
709   Float_t cosi,x1=0,y1=0;
710   Float_t chisqc;
711
712   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
713   const AliPHOSGeometry* fGeom = fLoader->PHOSGeometry();
714
715   for(Int_t iter=0; iter<nIter; iter++)
716     {
717  
718       chisqc = 0.;
719       grxc = 0.;
720       gryc = 0.;
721       for(Int_t iDigit = 0 ; iDigit < nDigits ; iDigit ++)
722         {
723
724           Float_t* energies = GetEnergiesList();
725           Int_t* digits = GetDigitsList();
726           digit = (AliPHOSDigit*)fLoader->Digits()->At( digits[iDigit] );
727           eDigit = energies[iDigit];
728           fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
729           fGeom->RelPosInModule(relid, iy, ix);
730       
731           dx =  xc - ix;
732           dy =  yc - iy;
733
734           if(!dx) dx=dy;
735           if(!dy) dy=dx;
736                   
737           Float_t a;
738           GetReconstructionManager()->AG(e,dx,dy,a,gx,gy);
739           Float_t dd;
740           AliInfo(Form("  (ix iy  xc yc  dx dy)   %f %f %f %f %f %f", 
741                        ix, iy, xc, yc, dx, dy)) ;
742           Float_t chi2dg = GetReconstructionManager()->OneGamChi2(a,eDigit,e,dd);
743
744           // Exclude digit with too large chisquare.
745           if(chi2dg > 10) {  continue; }
746
747           chisqc += chi2dg;
748           grxc += gx*dd;
749           gryc += gy*dd;
750
751         }
752
753       Float_t grc = TMath::Sqrt(grxc*grxc + gryc*gryc);
754       if(grc<1.e-10) grc=1.e-10;
755       Float_t sc = 1. + chisqc/chisq;
756        AliInfo(Form(" chisq: %f", chisq)) ;
757       st = st/sc;
758       if(chisqc>chisq)
759         goto loop20;
760       cosi = (grx*grxc + gry*gryc)/gr/grc;
761       st = st*sc/(1.4 - cosi);
762       x1 = xc;
763       y1 = yc;
764       grx = grxc;
765       gry = gryc;
766       gr = grc;
767       chisq = chisqc;
768
769       if(chisq<chstop)
770         goto loop101;
771   
772     loop20: ;
773       Float_t step = st*gr;
774
775       AliInfo(Form(" Iteration %d dof %d chisq/dof %f chstop/dof %f step %d stpMin %d",
776            iter, dof, chisq/dof, chisq/dof, chstop/dof, step, stpMin)) ;
777         
778
779       if(step<stpMin)
780         goto loop101;
781
782       if(step>1.)
783         st = 1./gr;
784       xc = x1 - st*grx;
785       yc = y1 - st*gry;
786     }
787
788
789  loop101:
790   chisq = chisq/dof;
791 //    if(chisq>Chsqcut)
792 //      {
793 //  //        TwoGam();
794 //      }
795
796   Float_t gamma1[3];
797   gamma1[0] = e;
798   gamma1[1] = y1;
799   gamma1[2] = x1;
800
801   TVector3 newpos(gamma1[1],0,gamma1[2]);
802   //SetLocalPosition(newpos);
803   
804   fLocPos=newpos;
805   fAmp=e;
806
807 //    TVector3 pos;
808 //    RP->GetLocalPosition(pos);
809
810   
811   
812   fChi2Dof = chisq;
813
814
815
816
817 Bool_t AliPHOSEvalRecPoint::KillWeakPoint()
818 {
819   // Remove this point from further procession
820   // if it's energy is too small.
821   
822   Float_t thr0 = GetReconstructionManager()->KillGamMinEnergy();
823   
824   if(GetEnergy()<thr0) {
825     AliInfo(Form("+++++++ Killing this rec point ++++++++++")) ;
826     RemoveFromWorkingPool(this);
827     return kTRUE;
828   }
829
830   return kFALSE;
831 }
832
833
834 void AliPHOSEvalRecPoint::SplitMergedShowers() 
835 {
836   // Split two very close rec. points.
837
838   if(GetMultiplicity()<2) 
839     return; 
840
841   Float_t gamma1[3];
842   Float_t gamma2[3];
843
844   InitTwoGam(gamma1,gamma2); // start points for fit
845   TwoGam(gamma1,gamma2); // evaluate the positions and energies
846   UnfoldTwoMergedPoints(gamma1,gamma2); // make the job
847   
848 }
849
850
851 void AliPHOSEvalRecPoint::MergeClosePoint() 
852 {
853   // merge rec.points if they are too close
854   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
855 //    AliPHOSDigitizer* digitizer = fLoader->Digitizer();
856 //    Float_t fPedestal = digitizer->GetPedestal();  // YVK 30.09.2001
857 //    Float_t fSlope = digitizer->GetSlope();
858   
859   for (Int_t i=0;i<InWorkingPool(); i++)
860     {
861       TObject* obj = GetFromWorkingPool(i);
862       if(!((TObject*)this)->IsEqual(obj))
863         {
864           AliPHOSRecPoint* rp = (AliPHOSRecPoint*)obj;
865           if(GetPHOSMod() == rp->GetPHOSMod())
866             {
867               if(TooClose(rp))
868                 {
869                   AliInfo(Form("+++++++ Merging point 1: ++++++")) ;
870                   this->Print();
871                   AliInfo(Form("+++++++ and point 2: ++++++++++")) ;
872                   ((AliPHOSEvalRecPoint*)rp)->Print();
873
874                   //merge two rec. points
875                   TVector3 lpos1;
876                   TVector3 lpos2;
877                   this->GetLocalPosition(lpos1);
878                   rp->GetLocalPosition(lpos2);
879
880                   Int_t* digits = rp->GetDigitsList();
881                   Float_t dE = rp->GetEnergy()/(rp->GetEnergy()+this->GetEnergy());
882                   Float_t newX = lpos1.X()*dE + lpos2.X()*(1.-dE); 
883                   Float_t newZ = lpos1.Z()*dE + lpos2.Z()*(1.-dE);
884                   Float_t newE = rp->GetEnergy()+this->GetEnergy();
885                   Float_t* energies = ((AliPHOSEmcRecPoint*)rp)->GetEnergiesList();
886
887                   for(Int_t iDigit=0; iDigit<rp->GetDigitsMultiplicity(); iDigit++)
888                     {
889                       AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At(digits[iDigit]);
890                       Float_t eDigit = energies[iDigit];
891                       this->AddDigit(*digit,eDigit);
892                     }
893
894                   TVector3 newpos(newX,0,newZ);
895                   fLocPos = newpos;
896                   fAmp = newE;
897                   RemoveFromWorkingPool(rp);
898                   delete rp;
899                   
900                   AliInfo(Form("++++++ Resulting point: ++++++++")) ;
901                   this->Print();
902
903                   break;
904                 } 
905             }
906         }
907     }
908
909 }
910
911 Int_t AliPHOSEvalRecPoint::UnfoldLocalMaxima() 
912 {
913   // Make unfolding in the reconstruction point with several local maxima.
914   // Return the number of local maxima.
915
916   // if multiplicity less then 2 - nothing to unfold
917   if(GetMultiplicity()<2) return 1; 
918
919   AliPHOSDigit * maxAt[1000];
920   Float_t maxAtEnergy[1000];  
921   Float_t locMaxCut, logWeight;
922   Int_t relid[4] ; 
923   Float_t xMax;
924   Float_t zMax;
925
926   AliPHOSClusterizer* clusterizer = GetClusterizer();
927   if(!clusterizer) {
928     AliFatal(Form("Cannot get clusterizer")) ;
929   }
930
931   if(this->IsEmc()) {
932     locMaxCut = clusterizer->GetEmcLocalMaxCut();
933     logWeight = clusterizer->GetEmcLogWeight();
934   }
935   else {
936     locMaxCut = clusterizer->GetCpvLocalMaxCut();
937     logWeight = clusterizer->GetCpvLogWeight();
938   }
939
940   AliPHOSLoader* fLoader = AliPHOSLoader::GetPHOSLoader(fEventFolderName);
941   const AliPHOSGeometry* fGeom = fLoader->PHOSGeometry();
942   TClonesArray* digits = fLoader->Digits();
943
944   // if number of local maxima less then 2 - nothing to unfold
945   Int_t nMax = GetNumberOfLocalMax(maxAt,maxAtEnergy,locMaxCut,digits); 
946   if(nMax<2) return nMax;
947
948   Int_t* digitsList = GetDigitsList();
949   Int_t nDigits = GetMultiplicity();
950   Float_t* energies = GetEnergiesList();
951   Float_t* eFit = new Float_t[nDigits];
952
953   Int_t iDigit; //loop variable
954
955   for(iDigit=0; iDigit<nDigits; iDigit++)
956     {
957       eFit[iDigit] = 0.;
958     }
959
960   for(iDigit=0; iDigit<nDigits; iDigit++)
961     {
962       
963       AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At( digitsList[iDigit] );
964       fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
965       Float_t x,z;
966       fGeom->RelPosInModule(relid, x, z);
967
968       for(Int_t iMax=0; iMax<nMax; iMax++)
969         {
970           AliPHOSDigit* digitMax = maxAt[iMax];
971           Float_t eMax = maxAtEnergy[iMax]; 
972           fGeom->AbsToRelNumbering(digitMax->GetId(), relid) ;
973           fGeom->RelPosInModule(relid, xMax, zMax);
974           Float_t dx = xMax - x;
975           Float_t dz = zMax - z;
976           Float_t amp,gx,gy;
977           GetReconstructionManager()->AG(eMax,dz,dx,amp,gx,gy); 
978 //        amp = amp + 0.5;
979           eFit[iDigit] += amp;
980         }
981     }
982
983
984   for(Int_t iMax=0; iMax<nMax; iMax++) 
985     {
986       AliPHOSDigit* digitMax = maxAt[iMax];
987       fGeom->AbsToRelNumbering(digitMax->GetId(), relid) ;
988       fGeom->RelPosInModule(relid, xMax, zMax);
989       Float_t eMax = maxAtEnergy[iMax];
990
991       AliPHOSEvalRecPoint* newRP = new AliPHOSEvalRecPoint(IsCPV(),this);    
992       newRP->AddDigit(*digitMax,maxAtEnergy[iMax]);
993
994       //Neighbous ( matrix 3x3 around the local maximum)
995       for(Int_t iDigit=0; iDigit<nDigits; iDigit++)
996         {     
997           AliPHOSDigit* digit = (AliPHOSDigit*)fLoader->Digits()->At( digitsList[iDigit] );
998           Float_t eDigit = energies[iDigit];
999           fGeom->AbsToRelNumbering(digit->GetId(), relid) ;
1000           Float_t ix,iz;
1001           fGeom->RelPosInModule(relid, ix, iz);
1002
1003           if(AreNeighbours(digitMax,digit))
1004             {
1005               Float_t dx = xMax - ix;
1006               Float_t dz = zMax - iz;
1007               Float_t singleShowerGain,gxMax,gyMax;
1008               GetReconstructionManager()->AG(eMax,dz,dx,singleShowerGain,gxMax,gyMax);
1009               Float_t totalGain = eFit[iDigit];
1010               Float_t ratio = singleShowerGain/totalGain; 
1011               AliInfo(Form(" ratio -> %f", ratio)) ;
1012               eDigit = eDigit*ratio;
1013               newRP->AddDigit(*digit,eDigit);
1014             }
1015         }
1016
1017       newRP->EvalLocalPosition(logWeight,digits);
1018       AliInfo(Form("======= Unfold: daughter rec point %d =================", 
1019                    iMax)) ;
1020       newRP->Print();
1021     }
1022
1023 //    RemoveFromWorkingPool(this);
1024
1025   delete[] eFit;
1026
1027   return nMax;
1028
1029 }
1030
1031 void AliPHOSEvalRecPoint::PrintPoint()
1032 {
1033   // print rec.point to stdout
1034   AliPHOSCpvRecPoint::Print();
1035
1036   TVector3 lpos;
1037   GetLocalPosition(lpos);
1038
1039   TString message ; 
1040   message  = "       Chi2/dof = %f" ;
1041   message += "       Local (x,z) = (%f, %f) in module %d" ; 
1042   AliInfo(Form(message.Data(), Chi2Dof(), lpos.X(), lpos.Z(), GetPHOSMod())) ;
1043
1044 }
1045
1046 AliPHOSRecManager* AliPHOSEvalRecPoint::GetReconstructionManager() const
1047 {
1048   // returns reconstruction manager
1049   TFolder* aliceF = (TFolder*)gROOT->FindObjectAny("YSAlice");
1050   TFolder* wPoolF = (TFolder*)aliceF->FindObject("WhiteBoard/RecPoints/PHOS/SmP");
1051   AliPHOSRecManager* recMng = (AliPHOSRecManager*)wPoolF->FindObject("AliPHOSRecManager");
1052   if(!recMng) { 
1053     AliFatal(Form("Couldn't find Reconstruction Manager")) ;  
1054   }
1055
1056   return recMng;
1057 }
1058
1059
1060 AliPHOSRecPoint* AliPHOSEvalRecPoint::Parent()
1061 {
1062   // returns a parent
1063   if(fParent<0) return NULL;
1064   else
1065     return (AliPHOSRecPoint*)GetFromWorkingPool(fParent);
1066 //    return fParent;
1067 }
1068
1069 Float_t AliPHOSEvalRecPoint::Chi2Dof() const 
1070 {
1071   // returns a chi^2 per degree of freedom
1072   return fChi2Dof;
1073 }
1074
1075 const TObject* AliPHOSEvalRecPoint::GetWorkingPool()
1076 {
1077   // returns a pool of rec.points
1078   TFolder* aliceF = (TFolder*)gROOT->FindObjectAny("YSAlice");
1079   TFolder* wPoolF = (TFolder*)aliceF->FindObject("WhiteBoard/RecPoints/PHOS/SmP");
1080   TObject* wPool = wPoolF->FindObject("SmartPoints");
1081   if(!wPool) { 
1082     AliFatal(Form("Couldn't find Working Pool")) ;  
1083   }
1084
1085   return wPool;
1086 }
1087
1088 void AliPHOSEvalRecPoint::AddToWorkingPool(TObject* obj)
1089 {
1090   // add a rec.point to a pool
1091   ((TObjArray*)GetWorkingPool())->Add(obj);
1092 }
1093
1094 TObject* AliPHOSEvalRecPoint::GetFromWorkingPool(Int_t i)
1095 {
1096   // return a rec.point from a pool at an index i
1097 //    return fWorkingPool->At(i);
1098   return ((TObjArray*)GetWorkingPool())->At(i);
1099 }
1100
1101 Int_t AliPHOSEvalRecPoint::InWorkingPool()
1102 {
1103   // return the number of rec.points in a pool
1104   return ((TObjArray*)GetWorkingPool())->GetEntries();
1105 }
1106
1107 void AliPHOSEvalRecPoint::RemoveFromWorkingPool(TObject* obj)
1108 {
1109   // remove a rec.point from a pool
1110   ((TObjArray*)GetWorkingPool())->Remove(obj);
1111   ((TObjArray*)GetWorkingPool())->Compress();
1112 }
1113
1114 void AliPHOSEvalRecPoint::PrintWorkingPool()
1115 {
1116   // print pool of rec.points to stdout
1117   ((TObjArray*)GetWorkingPool())->Print();
1118 }