]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALJetFinderAlgoOmni.cxx
Fixed problems with air medium definition
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALJetFinderAlgoOmni.cxx
1
2 //THIS Also includes summing ALL cells in the jetcone towards the jet energy NOT just those above threshold!!!!!
3
4
5 /**************************************************************************
6  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
7  *                                                                        *
8  * Author: The ALICE Off-line Project.                                    *
9  * Contributors are mentioned in the code where appropriate.              *
10  *                                                                        *
11  * Permission to use, copy, modify and distribute this software and its   *
12  * documentation strictly for non-commercial purposes is hereby granted   *
13  * without fee, provided that the above copyright notice appears in all   *
14  * copies and that both the copyright notice and this permission notice   *
15  * appear in the supporting documentation. The authors make no claims     *
16  * about the suitability of this software for any purpose. It is          *
17  * provided "as is" without express or implied warranty.                  *
18  **************************************************************************/
19
20 /*
21  
22 $Log$
23 Revision 1.13  2004/03/26 01:00:38  mhorner
24 Implementing Marco's optimisations
25
26 Revision 1.12  2004/03/15 19:59:37  mhorner
27 Pyhtia comparison extended
28
29 Revision 1.11  2004/03/09 17:06:38  mhorner
30 Made more robust
31
32 Revision 1.10  2004/03/06 01:56:09  mhorner
33 Pythai comp code
34
35 Revision 1.9  2004/02/23 20:37:32  mhorner
36 changed geometry call
37
38 Revision 1.8  2004/01/29 23:28:44  mhorner
39 Jet Finder - hard coded geom parameters removed
40
41 Revision 1.7  2004/01/21 22:27:47  mhorner
42 Cleaning up coding conventions
43
44 Revision 1.6  2003/10/28 13:54:30  schutz
45 Compilation warnings fixed
46
47 Revision 1.5  2003/09/23 13:31:41  mhorner
48 Changed coordinate system
49
50 Revision 1.4  2003/09/19 13:16:20  mhorner
51 Added additional jet energy info
52
53
54 Revision 1.3  2003/09/04 12:49:56  mhorner
55 Changed hadron correction and added saving EMCAL and track contributions
56
57 */
58
59
60 //*--Author: Sarah Blyth (LBL)
61 //*--Based on UA1 jet algorithm from LUND JETSET called from EMC-erj
62
63 #include "TTask.h"
64 #include "AliEMCALJetFinderInput.h"
65 #include "AliEMCALJetFinderOutput.h"
66 #include "AliEMCALJetFinderAlgo.h"
67 #include "AliEMCALJetFinderAlgoOmni.h"
68 #include "AliEMCALJetFinderAlgoUA1Unit.h"
69 #include "AliEMCALGetter.h"
70 #include "AliEMCALGeometry.h"
71 #include "AliEMCAL.h"
72 #include "AliEMCALDigit.h"
73 #include "TParticle.h"
74 #include "AliRun.h"
75 #include "AliEMCALJet.h"
76 #include "TMath.h"
77
78
79 ClassImp(AliEMCALJetFinderAlgoOmni)
80
81   AliEMCALJetFinderAlgoOmni::AliEMCALJetFinderAlgoOmni()
82 {
83   //Default constructor
84 if (fDebug>0) Info("AliEMCALJetFinderAlgoOmni","Beginning Default Constructor");
85
86 // AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
87 //  AliEMCALGeometry * geom = gime->EMCALGeometry();
88   AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance("EMCAL_55_25","EMCAL");
89   fNumIter           = 0;
90   fNumUnits          = geom->GetNTowers();     //Number of towers in EMCAL
91   fESeed             = 5.0;       //Default value
92   fConeRad           = 0.3;       //Default value
93   fJetEMin           = 10.0;      //Default value
94   fEtMin             = 0.0;      //Default value
95   fMinMove           = 0.05;      //From original UA1 JetFinder
96   fMaxMove           = 0.15;      //From original UA1 JetFinder
97   fBGMaxMove         = 0.035;     //From original UA1 JetFinder
98   fPtCut             = 0;         
99   fHadCorr           = 0;       
100   fEBGTotal          = 1.0;       //Set to 1 so that no div by zero in first FindJets() loop
101   fEBGTotalOld       = 0.0;
102   fEBGAve            = 0.0;
103   fEnergy            = 0.0;
104   fJetEta            = 0.0;
105   fJetPhi            = 0.0;
106   fEtaInit           = 0.0;
107   fPhiInit           = 0.0;
108   fEtaB              = 0.0;
109   fPhiB              = 0.0;
110   fJetESum           = 0.0;
111   fJetEtaSum         = 0.0;
112   fJetPhiSum         = 0.0;
113   fDEta              = 0.0;
114   fDPhi              = 0.0;
115   fDistP             = 0.0;
116   fDistI             = 0.0;
117   fTempE             = 0.0;
118   fRad               = 2.0;      //Set to 2 to start 
119   fNumInCone         = 0;
120   fNumJets           = 0;
121   fArrayInitialised  = 0;        //Set to FALSE to start
122   fBGType            = kRatio;   //Set Ratio method as default BG subtraction method 
123   fBGPar             = -1.0;      //Set to 1 to start
124 }
125
126  AliEMCALJetFinderAlgoOmni::~AliEMCALJetFinderAlgoOmni()
127    {
128      //Destructor
129      if (fDebug>0) Info("AliEMCALJetFinderAlgoOmni","Beginning Destructor");
130      delete[] fUnit;
131      delete[] fUnitNoCuts;
132    }
133
134  void AliEMCALJetFinderAlgoOmni::SetJetFindingParameters
135                                (Int_t numUnits, Float_t eSeed, Float_t coneRad, Float_t jetEMin, Float_t etMin, 
136                                Float_t minMove, Float_t maxMove, Float_t bgMaxMove)
137    {
138      //Sets parameters for the JetFinding algorithm
139      if (fDebug>1) Info("SetJetFindingParameters","Setting parameters for JetFinding");
140
141      SetNumUnits(numUnits);
142      SetJetESeed(eSeed);
143      SetConeRad(coneRad);
144      SetJetEMin(jetEMin);
145      SetEtMin(etMin);
146      SetMinMove(minMove);
147      SetMaxMove(maxMove);
148      SetBGMaxMove(bgMaxMove);
149    }
150
151  void AliEMCALJetFinderAlgoOmni::SetJetFindingParameters
152                                (Int_t numUnits, Float_t eSeed, Float_t coneRad, Float_t jetEMin, Float_t etMin)
153    {
154      //Sets fewer parameters for the JetFinding algorithm
155      if (fDebug>1) Info("SetJetFindingParameters","Setting parameters for JetFinding");
156
157      SetNumUnits(numUnits);
158      SetJetESeed(eSeed);
159      SetConeRad(coneRad);
160      SetJetEMin(jetEMin);
161      SetEtMin(etMin);
162      SetMinMove(fMinMove);
163      SetMaxMove(fMaxMove);
164      SetBGMaxMove(fBGMaxMove);
165    }
166
167  void AliEMCALJetFinderAlgoOmni::InitUnitArray()
168    {
169      //Initialises unit arrays
170      if(fArrayInitialised) delete [] fUnit;
171      if(fArrayInitialised) delete [] fUnitNoCuts;
172      fUnit = new AliEMCALJetFinderAlgoUA1Unit[fNumUnits];
173      fUnitNoCuts = new AliEMCALJetFinderAlgoUA1Unit[fNumUnits];
174      fArrayInitialised = 1;
175    }
176
177  void AliEMCALJetFinderAlgoOmni::FillUnitArray(AliEMCALJetFinderAlgoUA1FillUnitFlagType_t flag)
178    {
179            // Fill the unit array 
180      if (fDebug>1) Info("FillUnitArray","Beginning FillUnitArray");
181          //    AliEMCAL* pEMCAL = (AliEMCAL*) gAlice->GetModule("EMCAL");
182          //   if (pEMCAL){ 
183          //          AliEMCALGeometry* geom =  AliEMCALGeometry::GetInstance(pEMCAL->GetTitle(), "");
184          //     }else
185          //    {
186
187      AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
188      AliEMCALGeometry * geom;
189      if (gime)
190       geom = gime->EMCALGeometry();
191      else
192       geom = AliEMCALGeometry::GetInstance("EMCAL_55_25","EMCAL");
193
194         //    }
195          
196      AliEMCALJetFinderAlgoUA1FillUnitFlagType_t option = flag;
197      Int_t         numTracks, numDigits;
198     
199      //Loops over all elements in the AliEMCALJetFinderAlgoUA1Unit array and 
200      //fills the objects with relevant values from the Data Input object
201      if (fDebug>10) Info("FillUnitArray","Filling array with Unit objects");
202      if (fDebug>15) Info("FillUnitArray","NTracks %i NDigits %i",fInputPointer->GetNTracks(),fInputPointer->GetNDigits());
203          numTracks = fInputPointer->GetNTracks();
204          numDigits = fInputPointer->GetNDigits();
205          TParticle         *myPart;
206          AliEMCALDigit     *myDigit;
207      if (!fPythiaComparison)
208      {
209          //Fill units with Track info if appropriate
210          if(option==kFillTracksOnly || option ==kFillAll) 
211            {          
212             for(Int_t j=0; j<numTracks; j++)
213             {
214              myPart = fInputPointer->GetTrack(j);
215              Float_t eta = myPart->Eta();
216              Float_t  phi = myPart->Phi();
217              Int_t towerID = geom->TowerIndexFromEtaPhi(eta,180.0/TMath::Pi()*phi);
218              Float_t  pT = myPart->Pt();
219              Float_t unitEnergy = fUnit[towerID-1].GetUnitEnergy(); 
220              Float_t unitEnergyNoCuts = fUnitNoCuts[towerID-1].GetUnitEnergy();
221
222              
223              //OLD WAY:   //Do Hadron Correction
224               if(fHadCorr != 0)
225                {
226                  Double_t   fullP = myPart->P();
227                  Double_t   hCEnergy = fHadCorr->GetEnergy(fullP, (Double_t)eta);
228                  unitEnergy -= hCEnergy*TMath::Sin(myPart->Theta());
229                  unitEnergyNoCuts -= hCEnergy*TMath::Sin(myPart->Theta());
230                  fUnit[towerID-1].SetUnitEnergy(unitEnergy);
231                  fUnitNoCuts[towerID-1].SetUnitEnergy(unitEnergyNoCuts);
232                } //end Hadron Correction loop 
233               
234     
235              /*
236               //Do Hadron Correction with propagate phi for the track
237               if(fHadCorr != 0)
238                 {
239                   Bool_t curl = 1;
240                   Float_t deltaPhi;
241                   TParticlePDG *pdg = myPart->GetPDG();
242                   if(pdg->Charge() < 0)
243                     {
244                       deltaPhi = PropagatePhi(myPart->Pt(), -1.0, curl);
245                     }
246                   else{
247                     deltaPhi = PropagatePhi(myPart->Pt(), 1.0, curl);
248                   }
249                   phi += deltaPhi;
250                   //Get new tower id for cell that track would curve into
251                   Int_t towerID2;
252                   if(phi>(TMath::Pi()/180.0)*geom->GetArm1PhiMax() || phi<(TMath::Pi()/180.0)*geom->GetArm1PhiMin())
253                     {
254                       towerID2 = -1;
255                     }
256                   else{
257                       towerID2 = geom->TowerIndexFromEtaPhi(eta,180.0/TMath::Pi()*phi);
258                     }
259                   
260                   if(towerID2 != -1)
261                     {
262                       //Find unit energy of new tower
263                       Float_t unitEnergy2 = fUnit[towerID2-1].GetUnitEnergy();
264                       Float_t unitEnergy2NoCuts = fUnitNoCuts[towerID2-1].GetUnitEnergy();
265                       Double_t   fullP = myPart->P();
266                       Double_t   hCEnergy = fHadCorr->GetEnergy(fullP, (Double_t)eta);
267                       unitEnergy2 -= hCEnergy*TMath::Sin(myPart->Theta());
268                       unitEnergy2NoCuts -= hCEnergy*TMath::Sin(myPart->Theta());
269                       fUnit[towerID2-1].SetUnitEnergy(unitEnergy2);
270                       fUnitNoCuts[towerID2-1].SetUnitEnergy(unitEnergy2NoCuts);
271                     }//end if for towerID2
272                 }//end Hadron Correction loop
273              */
274
275               fUnitNoCuts[towerID-1].SetUnitEnergy(unitEnergyNoCuts + pT);
276              //Do Pt cut on tracks
277              if(fPtCut != 0 && pT < fPtCut) continue;
278
279              fUnit[towerID-1].SetUnitEnergy(unitEnergy+pT);
280
281              }//end tracks loop
282            }//end Tracks condition
283
284
285          //Fill units with Digit info if appropriate
286          if(option ==kFillDigitsOnly || option ==kFillAll)
287            {
288             for(Int_t k=0; k<numDigits; k++)
289             {
290              myDigit = fInputPointer->GetDigit(k);
291              if (fDebug>10) Info("FillUnitArray","getting digits %i %i numdigits",k,numDigits );
292              Int_t towerID = myDigit->GetId();
293              Int_t amplitude = myDigit->GetAmp();     //Gets the integer valued amplitude of the digit
294              Float_t amp = (Float_t)amplitude;        //Need to typecast to Float_t before doing real energy conversion
295              Float_t digitEnergy = amp/10000000.0;    //Factor of 10 million needed to convert!
296              Float_t unitEnergy = fUnit[towerID-1].GetUnitEnergy() + digitEnergy;
297              Float_t unitEnergyNoCuts = fUnitNoCuts[towerID-1].GetUnitEnergy() + digitEnergy;
298              fUnit[towerID-1].SetUnitEnergy(unitEnergy);
299              fUnitNoCuts[towerID-1].SetUnitEnergy(unitEnergyNoCuts);
300             }//end digits loop
301            }//end digits condition
302
303          //Set all unit flags, Eta, Phi
304          for(Int_t i=0; i<fNumUnits; i++)
305            {
306              if (fDebug>10) Info("FillUnitArray","Setting all units outside jets");
307              //Set all units to be outside a jet initially
308              fUnit[i].SetUnitFlag(kOutJet);           
309              fUnit[i].SetUnitID(i+1);
310              Float_t eta;
311              Float_t phi;
312              geom->EtaPhiFromIndex(fUnit[i].GetUnitID(), eta, phi);
313              fUnit[i].SetUnitEta(eta);
314              fUnit[i].SetUnitPhi(phi*TMath::Pi()/180.0);
315              //Set all units to be outside a jet initially
316              fUnitNoCuts[i].SetUnitFlag(kOutJet);          
317              fUnitNoCuts[i].SetUnitID(i+1);
318              eta = 0.0;
319              phi = 0.0;
320              geom->EtaPhiFromIndex(fUnitNoCuts[i].GetUnitID(), eta, phi);
321              fUnitNoCuts[i].SetUnitEta(eta);
322              fUnitNoCuts[i].SetUnitPhi(phi*TMath::Pi()/180.0);
323              //      if(i>13000) cout<<"!!!!!!!!!!!!!!!!!For unit0, eta="<<eta<<" and phi="<<phi*TMath::Pi()/180.0<<" and ID="<<fUnit[i].GetUnitID()<<endl;
324              //  if(fUnit[i].GetUnitEnergy()>0) cout<<"Unit ID "<<fUnit[i].GetUnitID() <<"with eta="<<eta<<" and phi="<<phi*TMath::Pi()/180.0<<" has energy="<<fUnit[i].GetUnitEnergy()<<endl;
325            }//end loop over all units in array (same as all towers in EMCAL)
326
327      }
328      if (fPythiaComparison)
329      {
330              for(Int_t j=0; j<numTracks; j++)                            
331              {
332                      myPart = fInputPointer->GetTrack(j);
333                      fUnit[j].SetUnitID(j);
334                      fUnit[j].SetUnitFlag(kOutJet);
335                      fUnit[j].SetUnitEta(myPart->Eta());
336                      fUnit[j].SetUnitPhi(myPart->Phi());
337                      if (myPart->Energy()*TMath::Sin(myPart->Theta()) > fPtCut)
338                      {
339                              fUnit[j].SetUnitEnergy(myPart->Energy()*TMath::Sin(myPart->Theta()));
340                      }else
341                      {
342                              fUnit[j].SetUnitEnergy(0.00);
343                      }
344                      fUnitNoCuts[j].SetUnitID(j);
345                      fUnitNoCuts[j].SetUnitFlag(kOutJet);
346                      fUnitNoCuts[j].SetUnitEta(myPart->Eta());
347                      fUnitNoCuts[j].SetUnitPhi(myPart->Phi());
348                      fUnitNoCuts[j].SetUnitEnergy(myPart->Energy()*TMath::Sin(myPart->Theta()));
349              }
350              for(Int_t k=numTracks; k < fNumUnits; k++)
351              {
352                      fUnit[k].SetUnitID(k);
353                      fUnit[k].SetUnitFlag(kBelowMinEt);
354                      fUnit[k].SetUnitEta(0.0);
355                      fUnit[k].SetUnitPhi(0.0);
356                      fUnit[k].SetUnitEnergy(0.0);                    
357                      fUnitNoCuts[k].SetUnitID(k);
358                      fUnitNoCuts[k].SetUnitFlag(kBelowMinEt);
359                      fUnitNoCuts[k].SetUnitEta(0.0);
360                      fUnitNoCuts[k].SetUnitPhi(0.0);
361                      fUnitNoCuts[k].SetUnitEnergy(0.0);                   
362              }
363      }
364
365
366
367    }
368
369
370  void AliEMCALJetFinderAlgoOmni::Sort(AliEMCALJetFinderAlgoUA1Unit *unit, Int_t length)
371  {
372    //Calls the recursive quicksort method to sort unit objects in decending order of Energy
373    if (fDebug>1) Info("Sort","Sorting Unit objects");
374    QS(unit, 0, length-1);
375  }
376   
377
378  void AliEMCALJetFinderAlgoOmni::QS(AliEMCALJetFinderAlgoUA1Unit *unit, Int_t left, Int_t right)
379  {
380   //Sorts the AliEMCALJetFinderAlgoUA1Unit objects in decending order of Energy
381    if (fDebug>111) Info("QS","QuickSorting Unit objects");   
382
383    Int_t    i;
384    Int_t    j;
385    AliEMCALJetFinderAlgoUA1Unit  unitFirst;
386    AliEMCALJetFinderAlgoUA1Unit  unitSecond;
387
388    i = left;
389    j = right;
390    unitFirst = unit[(left+right)/2];
391
392  do
393   {
394     while( (unit[i].GetUnitEnergy() > unitFirst.GetUnitEnergy()) && (i < right)) i++;
395     while( (unitFirst.GetUnitEnergy() > unit[j].GetUnitEnergy()) && (j > left)) j--;
396
397     if(i <= j)
398       {
399         unitSecond = unit[i];
400         unit[i] = unit[j];
401         unit[j] = unitSecond;
402         i++;
403         j--;
404       }//end if
405   }while(i <= j);
406
407  if(left < j) QS(unit, left, j);
408  if(i < right) QS(unit, i, right);
409  }
410
411
412  void AliEMCALJetFinderAlgoOmni::FindBG()
413  {
414    if(fBGType == kRatio) RatioBG();
415    else if(fBGType == kCone) ConeBG();
416    else if(fBGType == kConstant) ConstantBG();
417  }
418
419  void AliEMCALJetFinderAlgoOmni::RatioBG()
420    {
421      //Finds the background energy for the iteration
422      //using the Ratio method
423      if (fDebug>1) Info("FindBG","Finding Average Background"); 
424      //Store BGEperCell from previous iteration!
425      fEBGTotalOld = fEBGTotal;
426      fEBGTotal          = 0.0;
427      Int_t numCone      = 0;
428
429      //If user has not set fBGPar, set it to the default
430      //for TPC = 90% efficiency, PtCut = 2GeV/c, timecut = 30ns
431      if(fBGPar == -1) fBGPar = 0.4685;
432
433      //Loop over all unit objects in the Unit array and link to same
434      //unit ID in NoCuts Unit array
435      for(Int_t i=0; i<fNumUnits; i++)
436        {
437          if(fUnit[i].GetUnitFlag() != kInJet)
438            {
439              Int_t id = fUnit[i].GetUnitID();
440              fEBGTotal += fUnitNoCuts[id-1].GetUnitEnergy();  
441            }
442          else numCone++;
443        }//end for
444
445      fEBGTotal *= fBGPar;
446      fEBGAve = fEBGTotal / (fNumUnits - numCone);
447      if (fDebug>5) Info("FindBG","Average BG is %f: ",fEBGAve); 
448
449      for(Int_t count=0; count<fNumUnits;count++)
450        {
451          fUnit[count].SetUnitFlag(kOutJet);
452        }//end for
453    }
454
455  void AliEMCALJetFinderAlgoOmni::ConeBG()
456    {
457      //Finds the background energy for the iteration
458      //using all energy not contained inside a jet
459      if (fDebug>1) Info("FindBG","Finding Average Background"); 
460      //Store old value of BGEperCell!
461      fEBGTotalOld = fEBGTotal;
462      fEBGTotal          = 0.0;
463      Int_t numCone      = 0;
464
465      //Loop over all unit objects in the array and sum the energy of those not in a jet
466      for(Int_t i=0; i<fNumUnits; i++)
467        {
468          if(fUnit[i].GetUnitFlag() != kInJet)
469            fEBGTotal += fUnit[i].GetUnitEnergy();
470          else numCone++;
471        }//end for
472
473      fEBGAve = fEBGTotal / (fNumUnits - numCone);
474      if (fDebug>5) Info("FindBG","Average BG is %f: ",fEBGAve);      
475
476      for(Int_t count=0; count<fNumUnits;count++)
477        {
478          fUnit[count].SetUnitFlag(kOutJet);
479        }//end for
480    }
481
482  void AliEMCALJetFinderAlgoOmni::ConstantBG()
483    {     
484      //Finds the background energy for the iteration
485      //using all energy not contained inside a jet
486      if (fDebug>1) Info("FindBG","Finding Average Background"); 
487
488      //If user has not set fBGPar, set it to the default
489      //for TPC = 90% efficiency, PtCut = 2GeV/c, timecut = 30ns
490      if(fBGPar == -1) fBGPar = 0.03378;
491
492      fEBGAve = fBGPar;
493      if (fDebug>5) Info("FindBG","Average BG is %f: ",fEBGAve);      
494
495      fEBGTotal          = 0.0;
496      Int_t numCone      = 0;
497      for(Int_t count=0; count<fNumUnits;count++)
498        {
499          if(fUnit[count].GetUnitFlag() == kInJet)
500            {
501              numCone++;
502            }
503          fUnit[count].SetUnitFlag(kOutJet);
504        }//end for
505      fEBGTotal = fEBGAve * (fNumUnits-numCone);
506      fEBGTotalOld = fEBGTotal;
507    }
508
509  void AliEMCALJetFinderAlgoOmni::FindJetEtaPhi(Int_t counter)
510    {
511      //Finds the eta and phi of the jet axis
512      if (fDebug>10) Info("FindJetEtaPhi","Finding Jet Eta and Phi");
513
514      fDEta = fUnit[counter].GetUnitEta() - fEtaInit;
515      fDPhi = fUnit[counter].GetUnitPhi() - fPhiInit;
516
517      fEnergy = fUnit[counter].GetUnitEnergy() - fEBGAve;
518      fJetEtaSum += fEnergy * fDEta;
519      fJetPhiSum += fEnergy * fDPhi;
520      fJetESum += fEnergy;
521      if (fJetESum >1.0e-7)
522      {
523              fJetEta = fEtaInit + (fJetEtaSum / fJetESum);
524              fJetPhi = fPhiInit + (fJetPhiSum / fJetESum);
525      }
526    }
527
528
529  void AliEMCALJetFinderAlgoOmni::FindJetEnergy()
530    {
531      //Finds the energy of the jet after the final axis has been found
532      if (fDebug>1) Info("FindJetEnergy","Finding Jet Energy");
533
534      for(Int_t i=0; i<fNumUnits; i++)
535        {
536          //Loop over all unit objects in the array and find if within cone radius
537          Float_t dEta = fUnit[i].GetUnitEta() - fJetEta;
538          Float_t dPhi = fUnit[i].GetUnitPhi() - fJetPhi;
539          Float_t rad;
540          if ((dEta*dEta) + (dPhi*dPhi)>1.e-7)
541          {
542                  rad = TMath::Sqrt( (dEta*dEta) + (dPhi*dPhi) );
543          }else
544          {
545                  rad=0.0;
546          }
547
548          if(fUnit[i].GetUnitFlag()==kOutJet && rad<= fConeRad)
549            {
550              fUnit[i].SetUnitFlag(kInCurrentJet);
551              Float_t energy = fUnit[i].GetUnitEnergy() - fEBGAve;
552              fJetESum += energy;                             
553              fJetEtaSum += energy * dEta;
554              fJetPhiSum += energy * dPhi;
555              fNumInCone++;                     //Increment the number of cells in the jet cone
556            }//end if
557        }//end for
558    }
559
560
561  void AliEMCALJetFinderAlgoOmni::StoreJetInfo()
562    {
563      //Stores the resulting jet information in appropriate storage structure (TO BE DECIDED!!!!)
564      if (fDebug>1) Info("StoreJetInfo","Storing Jet Information");
565      AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
566      AliEMCALGeometry * geom;
567      if (gime)
568       geom = gime->EMCALGeometry();
569      else
570       geom = AliEMCALGeometry::GetInstance("EMCAL_55_25","EMCAL");
571      //Store:
572      //fJetESum is the final jet energy (background has been subtracted)
573      //fJetEta is the final jet Eta
574      //fJetPhi is the final jet Phi
575      //fNumInCone is the final number of cells included in the jet cone
576      //fEtaInit is the eta of the initiator cell
577      //fPhiInit is the phi of the initiator cell
578
579      AliEMCALJet jet(fJetESum,fJetPhi,fJetEta);
580
581       cout<<"For iteration "<<fNumIter <<" and Jet number " <<fNumJets <<endl;
582       cout<<"The jet energy is: " <<fJetESum <<endl;
583       cout<<"The jet eta is ---->" <<fJetEta <<endl;
584       cout<<"The jet phi is ---->" <<fJetPhi <<endl;
585
586      Int_t             numberTracks = fInputPointer->GetNTracks();
587      Int_t             numberDigits = fInputPointer->GetNDigits();
588      AliEMCALDigit     *myD;
589      TParticle         *myP;
590      Int_t             numTracksInCone = 0;
591      Float_t           trackEnergy = 0.0;
592      Float_t           trackEnergyPtCut =0.0; 
593      Float_t           emcalEnergy = 0.0;
594      Float_t           emcalEnergyBGSub = 0.0;
595
596      for(Int_t counter=0; counter<numberTracks; counter++)
597        {
598         myP = fInputPointer->GetTrack(counter);
599         Float_t eta = myP->Eta();
600         Float_t phi = myP->Phi(); 
601         Float_t deta = fJetEta-eta;
602         Float_t dphi = fJetPhi -phi;
603         Float_t rad = TMath::Sqrt( (deta*deta) + (dphi*dphi));
604         if(rad<=fConeRad) numTracksInCone++;
605        }//end for
606
607      Float_t    *pTArray = new Float_t[numTracksInCone];
608      Float_t    *etaArray = new Float_t[numTracksInCone];
609      Float_t    *phiArray = new Float_t[numTracksInCone];
610      Int_t      *pdgArray = new Int_t[numTracksInCone];
611      Int_t             index = 0;
612
613      for(Int_t counter2=0; counter2<numberTracks; counter2++)
614        {
615          myP = fInputPointer->GetTrack(counter2);
616          Float_t eta = myP->Eta();
617          Float_t phi = myP->Phi(); 
618          Float_t deta = fJetEta-eta;
619          Float_t dphi = fJetPhi -phi;
620          Float_t rad ;
621          if ((deta*deta) + (dphi*dphi)>1.e-7)
622          {
623                  rad = TMath::Sqrt( (deta*deta) + (dphi*dphi) );
624          }else
625          {
626                  rad=0.0;
627          }
628
629          if(rad<=fConeRad)
630            {
631              pTArray[index] = myP->Pt();
632              //Calculate track contribution within jetcone
633              trackEnergy += myP->Pt();
634              if(myP->Pt() >= fPtCut) trackEnergyPtCut += myP->Pt();
635              etaArray[index] = eta;
636              phiArray[index] = phi;
637              pdgArray[index] = myP->GetPdgCode();
638              index++;
639              if(fHadCorr != 0)
640              {
641                      Double_t   fullP = myP->P();
642                      Double_t   hCEnergy = fHadCorr->GetEnergy(fullP, (Double_t)eta);
643                      emcalEnergy -= hCEnergy*TMath::Sin(myP->Theta());
644                      emcalEnergyBGSub -= hCEnergy*TMath::Sin(myP->Theta());
645              } //end Hadron Correction loop 
646                            
647            }//end if
648        }//end for
649
650      //Loop over digits to find EMCal contribution within jetcone
651      for(Int_t counter3=0; counter3<numberDigits; counter3++)
652        {
653          myD = fInputPointer->GetDigit(counter3);
654          //GET DIGIT ETA, PHI so that can check if inside R!
655          Float_t eta = 0.0;
656          Float_t phi = 0.0;
657          Int_t iID = myD->GetId();
658          geom->EtaPhiFromIndex(iID, eta, phi);
659          Float_t deta = fJetEta-eta;
660          Float_t dphi = fJetPhi -(TMath::Pi()/180.0)*phi;
661          //Float_t rad = TMath::Sqrt( (deta*deta) + (dphi*dphi));
662          Float_t rad ;
663          if ((deta*deta) + (dphi*dphi)>1.e-7)
664          {
665                  rad = TMath::Sqrt( (deta*deta) + (dphi*dphi) );
666          }else
667          {
668                  rad=0.0;
669          }
670
671          if(rad<=fConeRad)
672            {
673          Int_t amplitude = myD->GetAmp();     //Gets the integer valued amplitude of the digit
674          Float_t amp = (Float_t)amplitude;        //Need to typecast to Float_t before doing real energy conversion
675          Float_t digitEnergy = amp/10000000.0;    //Factor of 10 million needed to convert!
676          emcalEnergy += digitEnergy;
677          emcalEnergyBGSub += (digitEnergy - fEBGAve);
678            }//end if
679        }//end count3 for
680
681      //Save in JET object
682      jet.SetTrackList(numTracksInCone,pTArray, etaArray, phiArray, pdgArray);
683      jet.SetEMCALEnergy(emcalEnergy);
684      jet.SetEMCALEnergyBGSub(emcalEnergyBGSub);
685      jet.SetTrackEnergy(trackEnergy);
686      jet.SetTrackEnergyPtCut(trackEnergyPtCut);
687      fOutputPointer->AddJet(&jet);
688      delete[] pTArray;
689      delete[] etaArray;
690      delete[] phiArray;
691      delete[] pdgArray;
692    }
693
694
695  void AliEMCALJetFinderAlgoOmni::FindJets()
696    {
697      //Runs the complete UA1 JetFinding algorithm to find jets!
698      if (fDebug>1) Info("FindJets","Starting Jet Finding!!!");
699
700      //If the array of JetFinderUnit objects has not been initialised then initialise with default settings
701      if(!fArrayInitialised) 
702       {
703        InitUnitArray();
704        FillUnitArray(kFillAll);
705       }//end if
706      if (fDebug>1) Info("FindJets","Unit array filled");
707
708      //Step 1. Sort the array in descending order of Energy
709      Sort(fUnit,fNumUnits);
710
711      //Step 2. Set the number of iterations and Number of jets found to zero to start
712      fNumIter = 0;
713      fNumJets = 0;
714
715      //Step 3. Begin the iteration loop to find jets
716      //Need to iterate the algorithm while number of iterations<2 OR number of iterations<10 AND 
717      //the value of the average background has changed more than specified amount
718      //Min iterations = 2, Max iterations = 10
719      //while(fNumIter<2 || (fNumIter <10 && ( (fEBGTotal-fEBGTotalOld)/fEBGTotal) > fBGMaxMove) )
720
721      while(fNumIter<2 || (fNumIter <10 && ( fEBGTotal-fEBGTotalOld) > fEBGTotal*fBGMaxMove) )
722        {
723         if (fDebug>1) Info("FindJets","Starting BIG iteration ---> %i",fNumIter);
724
725          //Step 4. Find the value of the average background energy
726          FindBG();
727          fOutputPointer->Reset(kResetJets); //Reset output object to store info for new iteration
728          fNumJets=0;
729
730          //Loop over the array of unit objects and flag those with energy below MinCellEt
731          Int_t numbelow = 0;
732          for(Int_t j=0; j<fNumUnits; j++)
733            {
734              if( (fUnit[j].GetUnitEnergy()-fEBGAve) < fEtMin)
735                 {       
736                   //          fUnit[j].SetUnitFlag(kBelowMinEt);    TAKING OUT kBelow flag
737                   numbelow++;
738                 }//end if
739            }//end for
740          //cout<<"THERE WERE "<<numbelow<<" units with E <EtMin!!!!!!!!!!!!!!!"<<endl;
741
742          //Do quick check if there are no jets upfront
743          // if(fUnit[0].GetUnitFlag() == kBelowMinEt)
744          if( (fUnit[0].GetUnitEnergy()-fEBGAve) < fEtMin)
745            {
746             cout <<"There are no jets for this event!" <<endl;
747             break;
748            }//end if
749
750          //Step 5. Begin with the first jet candidate cell (JET SEED LOOP)
751          if (fDebug>5) Info("FindJets","Beginning JET SEED LOOP");
752          for(Int_t count=0; count<fNumUnits; count++)
753            {
754
755 //CHECK CONDITION HERE _ NOT SURE IF SHOULD MAYBE BE: GetUnitEnergy()-fEBGAve >fESeed?????????????????????????????
756              if(fUnit[count].GetUnitEnergy()>=fESeed && fUnit[count].GetUnitFlag()==kOutJet)
757                {
758                  fEnergy = fUnit[count].GetUnitEnergy() - fEBGAve;
759                  fJetEta = fUnit[count].GetUnitEta();
760                  fJetPhi = fUnit[count].GetUnitPhi();
761                  Int_t seedID = fUnit[count].GetUnitID();
762                  if (fDebug>5) Info("FindJets","Inside first candidate jet seed loop for time : %i", count);
763                  if (fDebug>5) Info("FindJets","Found candidate energy %f ",fEnergy);
764                  if (fDebug>5) Info("FindJets","Found candidate eta %f ", fJetEta);
765                  if (fDebug>5) Info("FindJets","Found candidate phi %f ", fJetPhi);
766                  if (fDebug>5) Info("FindJets","Found candidate ID %i", seedID);
767
768                  fEtaInit = fJetEta;
769                  fPhiInit = fJetPhi;
770                  fEtaB = fJetEta;
771                  fPhiB = fJetPhi;
772                  Int_t testflag = 1;
773                  do
774                    {
775                  fJetESum = 0.0;
776                  fJetEtaSum = 0.0;
777                  fJetPhiSum = 0.0;
778        
779          //Step 6. Find Jet Eta and Phi
780                  //Loop over all units in the array to find the ones in the jet cone and determine contrib to Jet eta, phi
781                      for(Int_t count1=0; count1<fNumUnits; count1++)               
782                        {
783                          if(fUnit[count1].GetUnitID() == seedID && testflag)
784                          {
785                                  testflag=0;
786                                  continue;   //skip unit if the jetseed to avoid doublecounting
787                          }
788                          if(fUnit[count1].GetUnitFlag() == kOutJet)
789                            {
790                              fDEta = fUnit[count1].GetUnitEta() - fJetEta;
791                              fDPhi = fUnit[count1].GetUnitPhi() - fJetPhi;
792                              if ( (fDEta*fDEta) + (fDPhi*fDPhi) >1.e-7)
793                              {
794                                      fRad = TMath::Sqrt( (fDEta*fDEta) + (fDPhi*fDPhi) );
795                              }else
796                              {
797                                      fRad=0.000;
798                              }
799                              if(fRad <= fConeRad)
800                                {
801                                  FindJetEtaPhi(count1); 
802                                }//end if
803                            }//end if
804                        }//end for (Jet Eta, Phi LOOP)
805                              
806                       //Find the distance cone centre moved from previous cone centre
807                       if (fDebug>10) Info("FindJets","Checking if cone move small enough");
808                       if (((fJetEta-fEtaB)*(fJetEta-fEtaB)) + ((fJetPhi-fPhiB)*(fJetPhi-fPhiB))  >1.e-7)
809                       {
810                               fDistP = TMath::Sqrt( ((fJetEta-fEtaB)*(fJetEta-fEtaB)) + ((fJetPhi-fPhiB)*(fJetPhi-fPhiB)) );
811                       }else
812                       {
813                               fDistP = 0.00;
814                       }
815                       //     if(fDistP <= fMinMove) break;
816                              
817
818                       //Find the distance cone centre is from initiator cell
819                       if (fDebug>10) Info("FindJets","Checking if cone move too large");
820                       if ( ((fJetEtaSum)*(fJetEtaSum))+((fJetPhiSum)*(fJetPhiSum)) >1.e-7)
821                       {
822                               fDistI = TMath::Sqrt( ((fJetEtaSum/fJetESum)*(fJetEtaSum/fJetESum)) + ((fJetPhiSum/fJetESum)*
823                                                                                                     (fJetPhiSum/fJetESum)));
824                       }else
825                       {
826                               fDistI = 0.00;
827                       }
828
829                       if(fDistP>fMinMove && fDistI<fMaxMove)
830                         {
831                           fEtaB = fJetEta;
832                           fPhiB = fJetPhi;
833                         }//end if
834                  
835                    }while(fDistP>fMinMove && fDistI<fMaxMove);
836                           
837                  fJetEta = fEtaB;
838                  fJetPhi = fPhiB;
839
840
841        //Step 7. Find the Jet Energy
842                  if (fDebug>1) Info("FindJets","Looking for Jet energy");
843                  fJetESum = 0.0;
844                  fJetEtaSum = 0.0;
845                  fJetPhiSum = 0.0;
846                  fNumInCone = 0;
847                  FindJetEnergy();
848
849                  //cout<<"Number of cells in jet cone is: "<<fNumInCone<<endl;
850
851        //Step 8. Check if the jet is a valid jet
852                  //Check if cluster energy is above Min allowed to be a jet
853 //DID NOT DO THE COSH COMPARISON HERE -> NEED TO CHECK WHICH COMPARISON IS BEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
854                  if (fDebug>5) Info("FindJets","Checking cluster is valid jet");
855                  if(fJetESum < fJetEMin)
856                    {
857                      for(Int_t count2=0; count2<fNumUnits; count2++)
858                        {
859                          if(fUnit[count2].GetUnitFlag()==kInCurrentJet || fUnit[count2].GetUnitFlag()==kOutJet)
860                            fUnit[count2].SetUnitFlag(kOutJet);
861                        }//end for
862                    if (fDebug>10) Info("FindJets","NOT a valid jet cell");
863                   }else
864                     {
865                      for(Int_t count2=0; count2<fNumUnits; count2++)
866                        {
867                          if(fUnit[count2].GetUnitFlag()==kInCurrentJet)
868                            {
869                              //      cout<<"Setting unit #"<<count2 <<" to be officially in a jet!"<<endl;
870                            fUnit[count2].SetUnitFlag(kInJet);
871                            }
872                        }//end for                       
873
874  //NEED TO CHECK FINAL WEIRD ITERATION OF ETA AND PHI CHANGES!!!!!!!!!
875                      //  fJetPhi += fJetPhiSum/fJetESum;        //CHECK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
876                      //  fJetEta += fJetEtaSum/fJetESum;        //CHECK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
877
878                      fNumJets++;              //Incrementing number of jets found
879                      StoreJetInfo();          //Storing jet info
880
881                  }//end if (check cluster above Min Jet Energy)
882                }//end if (Jet Seed condition)
883            }//end (JET SEED LOOP)
884
885 if (fDebug>5) Info("FindJets","End of BIG iteration number %i",fNumIter);
886 // this->Dump();
887          fNumIter++;
888        }//end 10 iteration WHILE LOOP
889  }
890
891
892
893
894
895
896
897
898
899
900