]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALRecPoint.cxx
remove old alignment data object, to be replaced with new version
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRecPoint.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 //  Reconstructed Points for the EMCAL
18 //  A RecPoint is a cluster of digits
19 //*-- Author: Yves Schutz (SUBATECH)
20 //*-- Author: Dmitri Peressounko (RRC KI & SUBATECH)
21 //*-- Author: Heather Gray (LBL) merged AliEMCALRecPoint and AliEMCALTowerRecPoint 02/04
22
23 // --- ROOT system ---
24 class Riostream;
25 #include <TPad.h>
26 class TGraph;
27 class TPaveText;
28 #include <TClonesArray.h>
29 #include <TMath.h>
30
31 // --- Standard library ---
32
33 // --- AliRoot header files ---
34 //#include "AliGenerator.h"
35 class AliGenerator;
36 #include "AliRunLoader.h"
37 #include "AliRun.h"
38 class AliEMCAL;
39 #include "AliEMCALLoader.h"
40 #include "AliEMCALGeometry.h"
41 #include "AliEMCALHit.h"
42 #include "AliEMCALDigit.h"
43 #include "AliEMCALRecPoint.h"
44
45 ClassImp(AliEMCALRecPoint)
46
47 //____________________________________________________________________________
48 AliEMCALRecPoint::AliEMCALRecPoint()
49   : AliRecPoint()
50 {
51   // ctor
52   fClusterType = -1;
53   fMaxTrack = 0 ;
54   fMulDigit   = 0 ;  
55   fMaxParent = 0;
56   fMulParent = 0;
57   fAmp   = 0. ;   
58   fCoreEnergy = 0 ; 
59   fEnergyList = 0 ;
60   fTimeList = 0 ;
61   fAbsIdList  = 0;
62   fParentsList = 0;
63   fTime = 0. ;
64   //  fLocPos.SetX(1.e+6)  ;      //Local position should be evaluated
65   fCoreRadius = 10;        //HG Check this
66
67   AliRunLoader *rl = AliRunLoader::GetRunLoader();
68   fGeomPtr = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
69   //fGeomPtr = AliEMCALGeometry::GetInstance();
70   fGeomPtr->GetTransformationForSM(); // Global <-> Local
71 }
72
73 //____________________________________________________________________________
74 AliEMCALRecPoint::AliEMCALRecPoint(const char * opt) : AliRecPoint(opt)
75 {
76   // ctor
77   fClusterType = -1;
78   fMaxTrack = 1000 ;
79   fMaxParent = 1000;
80   fMulDigit   = 0 ; 
81   fMulParent = 0; 
82   fAmp   = 0. ;   
83   fCoreEnergy = 0 ; 
84   fEnergyList = 0 ;
85   fTimeList = 0 ;
86   fAbsIdList  = 0;
87   fParentsList = new Int_t[fMaxParent];
88   fTime = -1. ;
89   //fLocPos.SetX(1.e+6)  ;      //Local position should be evaluated
90   fCoreRadius = 10;        //HG Check this
91   //fGeomPtr = AliEMCALGeometry::GetInstance();
92   AliRunLoader *rl = AliRunLoader::GetRunLoader();
93   fGeomPtr = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
94   fGeomPtr->GetTransformationForSM(); // Global <-> Local
95 }
96 //____________________________________________________________________________
97 AliEMCALRecPoint::AliEMCALRecPoint(const AliEMCALRecPoint & rp) : AliRecPoint(rp)
98 {
99   //copy ctor
100   fGeomPtr = rp.fGeomPtr;
101   fClusterType = rp.fClusterType;
102   fCoreEnergy = rp.fCoreEnergy;
103   fLambda[0] = rp.fLambda[0];
104   fLambda[1] = rp.fLambda[1];
105   fDispersion = rp.fDispersion;
106   fEnergyList = new Float_t[rp.fMulDigit];
107   fTimeList = new Float_t[rp.fMulDigit];
108   fAbsIdList = new Int_t[rp.fMulDigit];
109   for(Int_t i = 0; i < rp.fMulDigit; i++) {
110     fEnergyList[i] = rp.fEnergyList[i];
111     fTimeList[i] = rp.fTimeList[i];
112     fAbsIdList[i] = rp.fAbsIdList[i];
113   }
114   fTime = rp.fTime;
115   fCoreRadius = rp.fCoreRadius;
116   fMulParent = rp.fMulParent;
117   fMaxParent = rp.fMaxParent;
118   fParentsList = new Int_t[rp.fMulParent];
119   for(Int_t i = 0; i < rp.fMulParent; i++) fParentsList[i] = rp.fParentsList[i];
120   fSuperModuleNumber = rp.fSuperModuleNumber;
121
122 }
123 //____________________________________________________________________________
124 AliEMCALRecPoint::~AliEMCALRecPoint()
125 {
126   // dtor
127   if ( fEnergyList )
128     delete[] fEnergyList ; 
129   if ( fTimeList )
130     delete[] fTimeList ; 
131   if ( fAbsIdList )
132     delete[] fAbsIdList ; 
133    if ( fParentsList)
134     delete[] fParentsList;
135 }
136
137 //____________________________________________________________________________
138 void AliEMCALRecPoint::AddDigit(AliEMCALDigit & digit, Float_t Energy)
139 {
140   // Adds a digit to the RecPoint
141   // and accumulates the total amplitude and the multiplicity 
142   
143   if(fEnergyList == 0)
144     fEnergyList =  new Float_t[fMaxDigit]; 
145   if(fTimeList == 0)
146     fTimeList =  new Float_t[fMaxDigit]; 
147   if(fAbsIdList == 0) {
148     fAbsIdList =  new Int_t[fMaxDigit];
149     fSuperModuleNumber = fGeomPtr->GetSuperModuleNumber(digit.GetId());
150   }
151
152   if ( fMulDigit >= fMaxDigit ) { // increase the size of the lists 
153     fMaxDigit*=2 ; 
154     Int_t * tempo = new Int_t[fMaxDigit]; 
155     Float_t * tempoE =  new Float_t[fMaxDigit];
156     Float_t * tempoT =  new Float_t[fMaxDigit];
157     Int_t * tempoId = new Int_t[fMaxDigit]; 
158
159     Int_t index ;     
160     for ( index = 0 ; index < fMulDigit ; index++ ){
161       tempo[index]   = fDigitsList[index] ;
162       tempoE[index]  = fEnergyList[index] ; 
163       tempoT[index]  = fTimeList[index] ; 
164       tempoId[index] = fAbsIdList[index] ; 
165     }
166     
167     delete [] fDigitsList ; 
168     fDigitsList =  new Int_t[fMaxDigit];
169  
170     delete [] fEnergyList ;
171     fEnergyList =  new Float_t[fMaxDigit];
172
173     delete [] fTimeList ;
174     fTimeList =  new Float_t[fMaxDigit];
175
176     delete [] fAbsIdList ;
177     fAbsIdList =  new Int_t[fMaxDigit];
178
179     for ( index = 0 ; index < fMulDigit ; index++ ){
180       fDigitsList[index] = tempo[index] ;
181       fEnergyList[index] = tempoE[index] ; 
182       fTimeList[index] = tempoT[index] ; 
183       fAbsIdList[index]  = tempoId[index] ; 
184     }
185  
186     delete [] tempo ;
187     delete [] tempoE ; 
188     delete [] tempoT ; 
189     delete [] tempoId ; 
190   } // if
191   
192   fDigitsList[fMulDigit]   = digit.GetIndexInList()  ; 
193   fEnergyList[fMulDigit]   = Energy ;
194   fTimeList[fMulDigit]     = digit.GetTime() ;
195   fAbsIdList[fMulDigit]    = digit.GetId();
196   fMulDigit++ ; 
197   fAmp += Energy ; 
198
199 }
200 //____________________________________________________________________________
201 Bool_t AliEMCALRecPoint::AreNeighbours(AliEMCALDigit * digit1, AliEMCALDigit * digit2 ) const
202 {
203   // Tells if (true) or not (false) two digits are neighbours
204   // A neighbour is defined as being two digits which share a corner
205   
206   static Bool_t areNeighbours = kFALSE ;
207   static Int_t nSupMod=0, nTower=0, nIphi=0, nIeta=0;
208   static int nSupMod1=0, nTower1=0, nIphi1=0, nIeta1=0;
209   static Int_t relid1[2] , relid2[2] ; // ieta, iphi
210   static Int_t rowdiff=0, coldiff=0;
211
212   areNeighbours = kFALSE ;
213
214   fGeomPtr->GetCellIndex(digit1->GetId(), nSupMod,nTower,nIphi,nIeta);
215   fGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod,nTower,nIphi,nIeta, relid1[0],relid1[1]);
216
217   fGeomPtr->GetCellIndex(digit2->GetId(), nSupMod1,nTower1,nIphi1,nIeta1);
218   fGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod1,nTower1,nIphi1,nIeta1, relid2[0],relid2[1]);
219   
220   rowdiff = TMath::Abs( relid1[0] - relid2[0] ) ;  
221   coldiff = TMath::Abs( relid1[1] - relid2[1] ) ;  
222
223   if (( coldiff <= 1 )  && ( rowdiff <= 1 ) && (coldiff + rowdiff > 0)) 
224   areNeighbours = kTRUE ;
225   
226   return areNeighbours;
227 }
228
229 //____________________________________________________________________________
230 Int_t AliEMCALRecPoint::Compare(const TObject * obj) const
231 {
232   // Compares two RecPoints according to their position in the EMCAL modules
233
234   Float_t delta = 1 ; //Width of "Sorting row". If you change this 
235                       //value (what is senseless) change as well delta in
236                       //AliEMCALTrackSegmentMakerv* and other RecPoints...
237   Int_t rv ; 
238
239   AliEMCALRecPoint * clu = (AliEMCALRecPoint *)obj ; 
240
241   TVector3 locpos1; 
242   GetLocalPosition(locpos1);
243   TVector3 locpos2;  
244   clu->GetLocalPosition(locpos2);  
245
246   Int_t rowdif = (Int_t)(TMath::Ceil(locpos1.X()/delta)-TMath::Ceil(locpos2.X()/delta)) ;
247   if (rowdif> 0) 
248     rv = 1 ;
249   else if(rowdif < 0) 
250     rv = -1 ;
251   else if(locpos1.Y()>locpos2.Y()) 
252     rv = -1 ;
253   else 
254     rv = 1 ; 
255
256   return rv ; 
257 }
258
259 //____________________________________________________________________________
260 Int_t AliEMCALRecPoint::DistancetoPrimitive(Int_t px, Int_t py)
261 {
262   // Compute distance from point px,py to  a AliEMCALRecPoint considered as a Tmarker
263   // Compute the closest distance of approach from point px,py to this marker.
264   // The distance is computed in pixels units.
265   // HG Still need to update -> Not sure what this should achieve
266
267   TVector3 pos(0.,0.,0.) ;
268   GetLocalPosition(pos) ;
269   Float_t x =  pos.X() ;
270   Float_t y =  pos.Y() ;
271   const Int_t kMaxDiff = 10;
272   Int_t pxm  = gPad->XtoAbsPixel(x);
273   Int_t pym  = gPad->YtoAbsPixel(y);
274   Int_t dist = (px-pxm)*(px-pxm) + (py-pym)*(py-pym);
275   
276   if (dist > kMaxDiff) return 9999;
277   return dist;
278 }
279
280 //___________________________________________________________________________
281  void AliEMCALRecPoint::Draw(Option_t *option)
282  {
283    // Draw this AliEMCALRecPoint with its current attributes
284    
285    AppendPad(option);
286  }
287
288 //______________________________________________________________________________
289 void AliEMCALRecPoint::ExecuteEvent(Int_t /*event*/, Int_t, Int_t)
290 {
291   // Execute action corresponding to one event
292   // This member function is called when a AliEMCALRecPoint is clicked with the locator
293   //
294   // If Left button is clicked on AliEMCALRecPoint, the digits are switched on    
295   // and switched off when the mouse button is released.
296
297   //  static Int_t pxold, pyold;
298
299   /* static TGraph *  digitgraph = 0 ;
300   static TPaveText* clustertext = 0 ;
301   
302   if (!gPad->IsEditable()) return;
303   
304   switch (event) {
305     
306     
307   case kButton1Down:{
308     AliEMCALDigit * digit ;
309     AliEMCALGeometry * emcalgeom =  (AliEMCALGetter::Instance())->EMCALGeometry() ;
310
311     Int_t iDigit;
312     Int_t relid[2] ;
313   
314     const Int_t kMulDigit=AliEMCALRecPoint::GetDigitsMultiplicity() ;
315     Float_t * xi = new Float_t [kMulDigit] ; 
316     Float_t * zi = new Float_t [kMulDigit] ;
317     
318     for(iDigit = 0; iDigit < kMulDigit; iDigit++) {
319       Fatal("AliEMCALRecPoint::ExecuteEvent", " -> Something wrong with the code"); 
320       digit = 0 ; //dynamic_cast<AliEMCALDigit *>((fDigitsList)[iDigit]);
321       emcalgeom->AbsToRelNumbering(digit->GetId(), relid) ;
322       emcalgeom->PosInAlice(relid, xi[iDigit], zi[iDigit]) ;
323     }
324     
325     if (!digitgraph) {
326       digitgraph = new TGraph(fMulDigit,xi,zi);
327       digitgraph-> SetMarkerStyle(5) ; 
328       digitgraph-> SetMarkerSize(1.) ;
329       digitgraph-> SetMarkerColor(1) ;
330       digitgraph-> Draw("P") ;
331     }
332     if (!clustertext) {
333       
334       TVector3 pos(0.,0.,0.) ;
335       GetLocalPosition(pos) ;
336       clustertext = new TPaveText(pos.X()-10,pos.Z()+10,pos.X()+50,pos.Z()+35,"") ;
337       Text_t  line1[40] ;
338       Text_t  line2[40] ;
339       sprintf(line1,"Energy=%1.2f GeV",GetEnergy()) ;
340       sprintf(line2,"%d Digits",GetDigitsMultiplicity()) ;
341       clustertext ->AddText(line1) ;
342       clustertext ->AddText(line2) ;
343       clustertext ->Draw("");
344     }
345     gPad->Update() ; 
346     Print("") ;
347     delete[] xi ; 
348     delete[] zi ; 
349    }
350   
351 break;
352   
353   case kButton1Up:
354     if (digitgraph) {
355       delete digitgraph  ;
356       digitgraph = 0 ;
357     }
358     if (clustertext) {
359       delete clustertext ;
360       clustertext = 0 ;
361     }
362     
363     break;
364     
365     }*/
366 }
367 //____________________________________________________________________________
368 void AliEMCALRecPoint::EvalAll(Float_t logWeight,TClonesArray * digits) 
369 {
370   // Evaluates all shower parameters
371
372   EvalLocalPosition(logWeight, digits) ;
373   //  printf("eval position done\n");
374   EvalElipsAxis(logWeight, digits) ;
375   //  printf("eval axis done\n");
376   EvalDispersion(logWeight, digits) ;
377   //  printf("eval dispersion done\n");
378   //EvalCoreEnergy(logWeight, digits);
379  // printf("eval energy done\n");
380   EvalTime(digits) ;
381   //  printf("eval time done\n");
382
383   EvalPrimaries(digits) ;
384   //  printf("eval pri done\n");
385   EvalParents(digits);
386   //  printf("eval parent done\n");
387 }
388
389 //____________________________________________________________________________
390 void  AliEMCALRecPoint::EvalDispersion(Float_t logWeight, TClonesArray * digits)
391 {
392   // Calculates the dispersion of the shower at the origin of the RecPoint
393
394   Double_t d = 0., wtot = 0., w = 0., xyzi[3], diff=0.;
395   Int_t iDigit=0, nstat=0, i=0;
396   AliEMCALDigit * digit ;
397
398   // Calculates the centre of gravity in the local EMCAL-module coordinates   
399   if (!fLocPos.Mag()) 
400     EvalLocalPosition(logWeight, digits) ;
401   
402   // Calculates the dispersion in coordinates 
403   for(iDigit=0; iDigit < fMulDigit; iDigit++) {
404     digit = (AliEMCALDigit *) digits->At(fDigitsList[iDigit])  ;
405
406     fGeomPtr->RelPosCellInSModule(digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
407     w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
408
409     if(w>0.0) {
410       wtot += w;
411       nstat++;
412       for(i=0; i<3; i++ ) {
413         diff = xyzi[i] - double(fLocPos[i]);
414         d   += w * diff*diff;
415       }
416     }
417   }
418   
419   if ( wtot > 0 && nstat>1) d /= wtot ;
420   else                      d = 0. ; 
421
422   fDispersion = TMath::Sqrt(d) ;
423 }
424
425 //____________________________________________________________________________
426 void AliEMCALRecPoint::EvalLocalPosition(Float_t logWeight, TClonesArray * digits)
427 {
428   // Calculates the center of gravity in the local EMCAL-module coordinates 
429   //  Info("Print", " logWeight %f : cluster energy %f ", logWeight, fAmp); // for testing
430   
431   AliEMCALDigit * digit;
432   Int_t i=0, nstat=0;
433   Double_t clXYZ[3]={0.,0.,0.}, clRmsXYZ[3]={0.,0.,0.}, xyzi[3], wtot=0., w=0.;
434
435   for(Int_t iDigit=0; iDigit<fMulDigit; iDigit++) {
436     digit = dynamic_cast<AliEMCALDigit *>(digits->At(fDigitsList[iDigit])) ;
437
438     fGeomPtr->RelPosCellInSModule(digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
439     // printf(" Id %i : Local x,y,z %f %f %f \n", digit->GetId(), xyzi[0], xyzi[1], xyzi[2]);
440
441     if(logWeight > 0.0) w = TMath::Max( 0., logWeight + TMath::Log( fEnergyList[iDigit] / fAmp ));
442     else                w = fEnergyList[iDigit]; // just energy
443
444     if(w>0.0) {
445       wtot += w ;
446       nstat++;
447       for(i=0; i<3; i++ ) {
448         clXYZ[i]    += (w*xyzi[i]);
449         clRmsXYZ[i] += (w*xyzi[i]*xyzi[i]);
450       }
451     }
452   }
453   //  cout << " wtot " << wtot << endl;
454   if ( wtot > 0 ) { 
455     //    xRMS   = TMath::Sqrt(x2m - xMean*xMean);
456     for(i=0; i<3; i++ ) {
457       clXYZ[i] /= wtot;
458       if(nstat>1) {
459         clRmsXYZ[i] /= (wtot*wtot);  
460         clRmsXYZ[i]  =  clRmsXYZ[i] - clXYZ[i]*clXYZ[i];
461         if(clRmsXYZ[i] > 0.0) {
462           clRmsXYZ[i] =  TMath::Sqrt(clRmsXYZ[i]);
463         } else      clRmsXYZ[i] = 0;
464       } else        clRmsXYZ[i] = 0;
465     }    
466   } else {
467     for(i=0; i<3; i++ ) {
468       clXYZ[i] = clRmsXYZ[i] = -1.;
469     }
470   }
471   // clRmsXYZ[i] ??
472   fLocPos.SetX(clXYZ[0]);
473   fLocPos.SetY(clXYZ[1]);
474   fLocPos.SetZ(clXYZ[2]);
475     
476 //  if (gDebug==2)
477 //    printf("EvalLocalPosition: eta,phi,r = %f,%f,%f", fLocPos.X(), fLocPos.Y(), fLocPos.Z()) ; 
478   fLocPosM = 0 ; // covariance matrix
479 }
480
481 //void AliEMCALRecPoint::EvalLocalPositionSimple()
482 //{ // Weight is proportional of cell energy 
483 //}
484
485 //______________________________________________________________________________
486 void AliEMCALRecPoint::EvalCoreEnergy(Float_t logWeight, TClonesArray * digits)
487 {
488   // This function calculates energy in the core, 
489   // i.e. within a radius rad = fCoreEnergy around the center. Beyond this radius
490   // in accordance with shower profile the energy deposition 
491   // should be less than 2%
492
493   AliEMCALDigit * digit ;
494   const Float_t kDeg2Rad = TMath::DegToRad() ;
495
496   Int_t iDigit;
497
498   if (!fLocPos.Mag()) {
499     EvalLocalPosition(logWeight, digits);
500   }
501   
502   for(iDigit=0; iDigit < fMulDigit; iDigit++) {
503     digit = (AliEMCALDigit *) ( digits->At(fDigitsList[iDigit]) ) ;
504     
505     Float_t ieta = 0.0;
506     Float_t iphi = 0.0;
507     //fGeomPtr->PosInAlice(digit->GetId(), ieta, iphi);
508     fGeomPtr->EtaPhiFromIndex(digit->GetId(),ieta, iphi) ;
509     iphi = iphi * kDeg2Rad;
510   
511     Float_t distance = TMath::Sqrt((ieta-fLocPos.X())*(ieta-fLocPos.X())+(iphi-fLocPos.Y())*(iphi-fLocPos.Y())) ;
512     if(distance < fCoreRadius)
513       fCoreEnergy += fEnergyList[iDigit] ;
514   }
515   
516 }
517 //____________________________________________________________________________
518 void  AliEMCALRecPoint::EvalElipsAxis(Float_t logWeight,TClonesArray * digits)
519 {
520   // Calculates the axis of the shower ellipsoid in eta and phi
521
522   Double_t wtot = 0. ;
523   Double_t x    = 0.;
524   Double_t z    = 0.;
525   Double_t dxx  = 0.;
526   Double_t dzz  = 0.;
527   Double_t dxz  = 0.;
528
529   const Float_t kDeg2Rad = TMath::DegToRad();
530   AliEMCALDigit * digit ;
531
532   TString gn(fGeomPtr->GetName());
533
534   Int_t iDigit;
535   
536   for(iDigit=0; iDigit<fMulDigit; iDigit++) {
537     digit = (AliEMCALDigit *) digits->At(fDigitsList[iDigit])  ;
538     Float_t etai = 0. ;
539     Float_t phii = 0. ; 
540     if(gn.Contains("SHISH")) { // have to be change - Feb 28, 2006
541     //copied for shish-kebab geometry, ieta,iphi is cast as float as eta,phi conversion
542     // for this geometry does not exist
543       int nSupMod=0, nTower=0, nIphi=0, nIeta=0;
544       int iphi=0, ieta=0;
545       fGeomPtr->GetCellIndex(digit->GetId(), nSupMod,nTower,nIphi,nIeta);
546       fGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod,nTower,nIphi,nIeta, iphi,ieta);
547       etai=(Float_t)ieta;
548       phii=(Float_t)iphi;
549     } else {
550       fGeomPtr->EtaPhiFromIndex(digit->GetId(), etai, phii);
551       phii = phii * kDeg2Rad;
552     }
553
554     Double_t w = TMath::Max(0.,logWeight+TMath::Log(fEnergyList[iDigit]/fAmp ) ) ;
555
556     dxx  += w * etai * etai ;
557     x    += w * etai ;
558     dzz  += w * phii * phii ;
559     z    += w * phii ; 
560
561     dxz  += w * etai * phii ; 
562
563     wtot += w ;
564   }
565
566   if ( wtot > 0 ) { 
567     dxx /= wtot ;
568     x   /= wtot ;
569     dxx -= x * x ;
570     dzz /= wtot ;
571     z   /= wtot ;
572     dzz -= z * z ;
573     dxz /= wtot ;
574     dxz -= x * z ;
575
576     fLambda[0] =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
577     if(fLambda[0] > 0)
578       fLambda[0] = TMath::Sqrt(fLambda[0]) ;
579     else
580       fLambda[0] = 0;
581     
582     fLambda[1] =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
583
584     if(fLambda[1] > 0) //To avoid exception if numerical errors lead to negative lambda.
585       fLambda[1] = TMath::Sqrt(fLambda[1]) ;
586     else
587       fLambda[1]= 0. ;
588   } else { 
589     fLambda[0]= 0. ;
590     fLambda[1]= 0. ;
591   }
592
593   //  printf("Evalaxis: lambdas  = %f,%f", fLambda[0],fLambda[1]) ; 
594
595 }
596
597 //______________________________________________________________________________
598 void  AliEMCALRecPoint::EvalPrimaries(TClonesArray * digits)
599 {
600   // Constructs the list of primary particles (tracks) which have contributed to this RecPoint
601   
602   AliEMCALDigit * digit ;
603   Int_t * tempo    = new Int_t[fMaxTrack] ;
604
605   Int_t index ;  
606   for ( index = 0 ; index < GetDigitsMultiplicity() ; index++ ) { // all digits
607     digit = dynamic_cast<AliEMCALDigit *>(digits->At( fDigitsList[index] )) ; 
608     Int_t nprimaries = digit->GetNprimary() ;
609     if ( nprimaries == 0 ) continue ;
610     Int_t * newprimaryarray = new Int_t[nprimaries] ;
611     Int_t ii ; 
612     for ( ii = 0 ; ii < nprimaries ; ii++)
613       newprimaryarray[ii] = digit->GetPrimary(ii+1) ; 
614
615     Int_t jndex ;
616     for ( jndex = 0 ; jndex < nprimaries ; jndex++ ) { // all primaries in digit
617       if ( fMulTrack > fMaxTrack ) {
618         fMulTrack = fMaxTrack ;
619         Error("GetNprimaries", "increase fMaxTrack ")  ;
620         break ;
621       }
622       Int_t newprimary = newprimaryarray[jndex] ;
623       Int_t kndex ;
624       Bool_t already = kFALSE ;
625       for ( kndex = 0 ; kndex < fMulTrack ; kndex++ ) { //check if not already stored
626         if ( newprimary == tempo[kndex] ){
627           already = kTRUE ;
628           break ;
629         }
630       } // end of check
631       if ( !already && (fMulTrack < fMaxTrack)) { // store it
632         tempo[fMulTrack] = newprimary ; 
633         fMulTrack++ ;
634       } // store it
635     } // all primaries in digit
636     delete [] newprimaryarray ; 
637   } // all digits
638
639   
640   fTracksList = new Int_t[fMulTrack] ;
641   for(index = 0; index < fMulTrack; index++)
642    fTracksList[index] = tempo[index] ;
643  
644   delete [] tempo ;
645
646 }
647
648 //______________________________________________________________________________
649 void  AliEMCALRecPoint::EvalParents(TClonesArray * digits)
650 {
651   // Constructs the list of parent particles (tracks) which have contributed to this RecPoint
652  
653   AliEMCALDigit * digit ;
654   Int_t * tempo    = new Int_t[fMaxParent] ;
655
656   Int_t index ;  
657   for ( index = 0 ; index < GetDigitsMultiplicity() ; index++ ) { // all digits
658     digit = dynamic_cast<AliEMCALDigit *>(digits->At( fDigitsList[index] )) ; 
659     Int_t nparents = digit->GetNiparent() ;
660     if ( nparents == 0 ) continue ;
661     Int_t * newparentarray = new Int_t[nparents] ;
662     Int_t ii ; 
663     for ( ii = 0 ; ii < nparents ; ii++)
664       newparentarray[ii] = digit->GetIparent(ii+1) ; 
665
666     Int_t jndex ;
667     for ( jndex = 0 ; jndex < nparents ; jndex++ ) { // all primaries in digit
668       if ( fMulParent > fMaxParent ) {
669         fMulTrack = - 1 ;
670         Error("GetNiparent", "increase fMaxParent")  ;
671         break ;
672       }
673       Int_t newparent = newparentarray[jndex] ;
674       Int_t kndex ;
675       Bool_t already = kFALSE ;
676       for ( kndex = 0 ; kndex < fMulParent ; kndex++ ) { //check if not already stored
677         if ( newparent == tempo[kndex] ){
678           already = kTRUE ;
679           break ;
680         }
681       } // end of check
682       if ( !already && (fMulTrack < fMaxTrack)) { // store it
683         tempo[fMulParent] = newparent ; 
684         fMulParent++ ;
685       } // store it
686     } // all parents in digit
687     delete [] newparentarray ; 
688   } // all digits
689
690   if (fMulParent>0) {
691     fParentsList = new Int_t[fMulParent] ;
692     for(index = 0; index < fMulParent; index++)
693       fParentsList[index] = tempo[index] ;
694   }
695  
696   delete [] tempo ;
697
698 }
699
700 //____________________________________________________________________________
701 void AliEMCALRecPoint::GetLocalPosition(TVector3 & lpos) const
702 {
703   // returns the position of the cluster in the local reference system of ALICE
704   
705   lpos.SetX(fLocPos.X()) ;
706   lpos.SetY(fLocPos.Y()) ;
707   lpos.SetZ(fLocPos.Z()) ;
708 }
709
710 //____________________________________________________________________________
711 void AliEMCALRecPoint::GetGlobalPosition(TVector3 & gpos) const
712 {
713   // returns the position of the cluster in the global reference system of ALICE
714   // These are now the Cartesian X, Y and Z
715   //  cout<<" geom "<<geom<<endl;
716   fGeomPtr->GetGlobal(fLocPos, gpos, fSuperModuleNumber);
717 }
718
719 //____________________________________________________________________________
720 Float_t AliEMCALRecPoint::GetMaximalEnergy(void) const
721 {
722   // Finds the maximum energy in the cluster
723   
724   Float_t menergy = 0. ;
725
726   Int_t iDigit;
727
728   for(iDigit=0; iDigit<fMulDigit; iDigit++) {
729  
730     if(fEnergyList[iDigit] > menergy) 
731       menergy = fEnergyList[iDigit] ;
732   }
733   return menergy ;
734 }
735
736 //____________________________________________________________________________
737 Int_t AliEMCALRecPoint::GetMultiplicityAtLevel(Float_t H) const
738 {
739   // Calculates the multiplicity of digits with energy larger than H*energy 
740   
741   Int_t multipl   = 0 ;
742   Int_t iDigit ;
743   for(iDigit=0; iDigit<fMulDigit; iDigit++) {
744
745     if(fEnergyList[iDigit] > H * fAmp) 
746       multipl++ ;
747   }
748   return multipl ;
749 }
750
751 //____________________________________________________________________________
752 Int_t  AliEMCALRecPoint::GetNumberOfLocalMax(AliEMCALDigit **  maxAt, Float_t * maxAtEnergy,
753                                                Float_t locMaxCut,TClonesArray * digits) const
754
755   // Calculates the number of local maxima in the cluster using fLocalMaxCut as the minimum
756   // energy difference between two local maxima
757
758   AliEMCALDigit * digit ;
759   AliEMCALDigit * digitN ;
760   
761   Int_t iDigitN ;
762   Int_t iDigit ;
763
764   for(iDigit = 0; iDigit < fMulDigit; iDigit++)
765     maxAt[iDigit] = (AliEMCALDigit*) digits->At(fDigitsList[iDigit])  ;
766   
767   for(iDigit = 0 ; iDigit < fMulDigit; iDigit++) {   
768     if(maxAt[iDigit]) {
769       digit = maxAt[iDigit] ;
770           
771       for(iDigitN = 0; iDigitN < fMulDigit; iDigitN++) {        
772         digitN = (AliEMCALDigit *) digits->At(fDigitsList[iDigitN]) ; 
773         
774         if ( AreNeighbours(digit, digitN) ) {
775           if (fEnergyList[iDigit] > fEnergyList[iDigitN] ) {    
776             maxAt[iDigitN] = 0 ;
777             // but may be digit too is not local max ?
778             if(fEnergyList[iDigit] < fEnergyList[iDigitN] + locMaxCut) 
779               maxAt[iDigit] = 0 ;
780           }
781           else {
782             maxAt[iDigit] = 0 ;
783             // but may be digitN too is not local max ?
784             if(fEnergyList[iDigit] > fEnergyList[iDigitN] - locMaxCut) 
785               maxAt[iDigitN] = 0 ; 
786           } 
787         } // if Areneighbours
788       } // while digitN
789     } // slot not empty
790   } // while digit
791   
792   iDigitN = 0 ;
793   for(iDigit = 0; iDigit < fMulDigit; iDigit++) { 
794     if(maxAt[iDigit] ){
795       maxAt[iDigitN] = maxAt[iDigit] ;
796       maxAtEnergy[iDigitN] = fEnergyList[iDigit] ;
797       iDigitN++ ; 
798     }
799   }
800   return iDigitN ;
801 }
802
803 //____________________________________________________________________________
804 Int_t AliEMCALRecPoint::GetPrimaryIndex() const  
805 {
806   // Get the primary track index in TreeK which deposits the most energy 
807   // in Digits which forms RecPoint. Kinematics, Hits and Digits must be 
808   // loaded before the call of the method.
809
810   AliRunLoader *rl = AliRunLoader::GetRunLoader(); 
811   if (!rl) 
812     AliError(Form(" No Runloader ")) ; 
813  
814   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>
815     (rl->GetDetectorLoader("EMCAL"));
816
817   // Get the list of digits forming this RecPoint
818   Int_t  nDigits   = fMulDigit   ;
819   Int_t *digitList = fDigitsList ;
820   
821   // Find the digit with maximum amplitude
822   AliEMCALDigit *digit = 0;
823   TClonesArray *digits = emcalLoader->Digits();
824   Int_t maxAmp = 0;
825   Int_t bestDigitIndex = -1;
826   for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {
827     digit = static_cast<AliEMCALDigit *>(digits->At(digitList[iDigit]));
828     if (digit->GetAmp() > maxAmp) {
829       maxAmp = digit->GetAmp();
830       bestDigitIndex = iDigit;
831     }
832   }
833
834   digit = static_cast<AliEMCALDigit *>(digits->At(digitList[bestDigitIndex]));  
835
836   // Get the list of hits producing this digit,
837   // find which hit has deposited more energy 
838   // and find the primary track.
839
840   AliEMCALHit *hit = 0;
841   TClonesArray *hits = emcalLoader->Hits();
842
843   Double_t maxedep  =  0;
844   Int_t    maxtrack = -1;
845   Int_t    nHits    = hits ->GetEntries();
846   Int_t    id       = digit->GetId();
847   for (Int_t iHit=0; iHit<nHits; iHit++) {
848     hit = static_cast<AliEMCALHit*> (hits->At(iHit)) ;
849     if(hit->GetId() == id){
850       Double_t edep  = hit->GetEnergy();
851       Int_t    track = hit->GetIparent();//Primary();
852       if(edep > maxedep){
853         maxedep  = edep;
854         maxtrack = track;
855       }
856     }
857   }
858   if (maxtrack != -1) return maxtrack; 
859   return -12345;                       // no track found :(
860 }
861
862 //____________________________________________________________________________
863 void AliEMCALRecPoint::EvalTime(TClonesArray * digits){
864   // time is set to the time of the digit with the maximum energy
865
866   Float_t maxE = 0;
867   Int_t maxAt = 0;
868   for(Int_t idig=0; idig < fMulDigit; idig++){
869     if(fEnergyList[idig] > maxE){
870       maxE = fEnergyList[idig] ;
871       maxAt = idig;
872     }
873   }
874   fTime = ((AliEMCALDigit*) digits->At(fDigitsList[maxAt]))->GetTime() ;
875   
876 }
877
878 //______________________________________________________________________________
879 void AliEMCALRecPoint::Paint(Option_t *)
880 {
881   // Paint this ALiRecPoint as a TMarker  with its current attributes
882   
883   TVector3 pos(0.,0.,0.)  ;
884   GetLocalPosition(pos)   ;
885   Coord_t x = pos.X()     ;
886   Coord_t y = pos.Z()     ;
887   Color_t markercolor = 1 ;
888   Size_t  markersize = 1. ;
889   Style_t markerstyle = 5 ;
890   
891   if (!gPad->IsBatch()) {
892     gVirtualX->SetMarkerColor(markercolor) ;
893     gVirtualX->SetMarkerSize (markersize)  ;
894     gVirtualX->SetMarkerStyle(markerstyle) ;
895   }
896   gPad->SetAttMarkerPS(markercolor,markerstyle,markersize) ;
897   gPad->PaintPolyMarker(1,&x,&y,"") ;
898 }
899
900 //______________________________________________________________________________
901 Float_t AliEMCALRecPoint::EtaToTheta(Float_t arg) const
902 {
903   //Converts Theta (Radians) to Eta(Radians)
904   return (2.*TMath::ATan(TMath::Exp(-arg)));
905 }
906
907 //______________________________________________________________________________
908 Float_t AliEMCALRecPoint::ThetaToEta(Float_t arg) const
909 {
910   //Converts Eta (Radians) to Theta(Radians)
911   return (-1 * TMath::Log(TMath::Tan(0.5 * arg)));
912 }
913
914 //____________________________________________________________________________
915 void AliEMCALRecPoint::Print(Option_t *) const
916 {
917   // Print the list of digits belonging to the cluster
918   return;
919   TString message ; 
920   message  = "AliEMCALRecPoint:\n" ;
921   message +=  " digits # = " ; 
922   Info("Print", message.Data()) ; 
923
924   Int_t iDigit;
925   for(iDigit=0; iDigit<fMulDigit; iDigit++)
926     printf(" %d ", fDigitsList[iDigit] ) ;  
927   printf("\n");
928
929   Info("Print", " Energies = ") ;
930   for(iDigit=0; iDigit<fMulDigit; iDigit++) 
931     printf(" %f ", fEnergyList[iDigit] ) ;
932   printf("\n");
933
934   Info("Print", "\n Abs Ids = ") ;
935   for(iDigit=0; iDigit<fMulDigit; iDigit++) 
936     printf(" %i ", fAbsIdList[iDigit] ) ;
937   printf("\n");
938
939   Info("Print", " Primaries  ") ;
940   for(iDigit = 0;iDigit < fMulTrack; iDigit++)
941     printf(" %d ", fTracksList[iDigit]) ;
942
943   printf("\n Local x %6.2f y %7.2f z %7.1f \n", fLocPos[0], fLocPos[1], fLocPos[2]);
944
945   message  = "       ClusterType     = %d" ;
946   message += "       Multiplicity    = %d" ;
947   message += "       Cluster Energy  = %f" ; 
948   message += "       Core energy     = %f" ; 
949   message += "       Core radius     = %f" ; 
950   message += "       Number of primaries %d" ; 
951   message += "       Stored at position %d" ; 
952   Info("Print", message.Data(), fClusterType, fMulDigit, fAmp, fCoreEnergy, fCoreRadius, fMulTrack, GetIndexInList() ) ;  
953 }