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