]> git.uio.no Git - u/mrichter/AliRoot.git/blob - JETAN/AliDAJetFinder.cxx
Bug fixes by Martha for HighPT, cosmetic an mem leak fix for FF (Oliver)
[u/mrichter/AliRoot.git] / JETAN / AliDAJetFinder.cxx
1
2 //  **************************************************************************
3 //  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 //  *                                                                        *
5 //  * Author: The ALICE Off-line Project.                                    *
6 //  * Contributors are mentioned in the code where appropriate.              *
7 //  *                                                                        *
8 //  * Permission to use, copy, modify and distribute this software and its   *
9 //  * documentation strictly for non-commercial purposes is hereby granted   *
10 //  * without fee, provided that the above copyright notice appears in all   *
11 //  * copies and that both the copyright notice and this permission notice   *
12 //  * appear in the supporting documentation. The authors make no claims     *
13 //  * about the suitability of this software for any purpose. It is          *
14 //  * provided "as is" without express or implied warranty.                  *
15 //  **************************************************************************
16
17 //-----------------------------------------------------------------------------------
18 // Jet finder based on Deterministic Annealing
19 // For further informations about the DA working features see:
20 // Phys.Lett. B601 (2004) 56-63 (http://arxiv.org/abs/hep-ph/0407214)
21 // Author: Davide Perrino (davide.perrino@ba.infn.it, davide.perrino@cern.ch)
22 //-----------------------------------------------------------------------------------
23
24 #include <TMath.h>
25 #include <TRandom2.h>
26 #include <TClonesArray.h>
27 #include "AliJetReaderHeader.h"
28 #include "AliJetReader.h"
29 #include "AliDAJetHeader.h"
30 #include "AliDAJetFinder.h"
31
32 ClassImp(AliDAJetFinder)
33
34
35 //-----------------------------------------------------------------------------------
36 AliDAJetFinder::AliDAJetFinder():
37         AliJetFinder(),
38         fAlpha(1.01),
39         fDelta(1e-8),
40         fAvDist(1e-6),
41         fEps(1e-4),
42         fEpsMax(1e-2),
43         fNloopMax(100),
44         fBeta(0.1),
45         fNclustMax(0),
46         fNin(0),
47         fNeff(0)
48 {
49         // Constructor
50 }
51
52 //-----------------------------------------------------------------------------------
53 AliDAJetFinder::~AliDAJetFinder()
54 {
55         // Destructor
56 }
57
58 //-----------------------------------------------------------------------------------
59 void AliDAJetFinder::FindJets()  
60 {
61 // Find the jets in current event
62 // 
63         Float_t betaStop=100.;
64         fDebug = fHeader->GetDebug();
65
66         Double_t dEtSum=0;
67         Double_t *xData[2];
68         TVectorD *vPx = new TVectorD();
69         TVectorD *vPy = new TVectorD();
70         TMatrixD *mPyx= new TMatrixD();
71         TMatrixD *mY  = new TMatrixD();
72         InitDetAnn(dEtSum,xData,vPx,vPy,mPyx,mY);
73         if (fNin < fNclustMax){
74           delete [] xData[0], delete [] xData[1];
75           delete vPx;
76           delete vPy;
77           delete mPyx;
78           delete mY;
79           return;
80         }
81         Int_t nc=1, nk;
82         DoubleClusters(nc,nk,vPy,mY);
83         do{                                     //loop over beta
84                 fBeta*=fAlpha;
85                 Annealing(nk,xData,vPx,vPy,mPyx,mY);
86                 NumCl(nc,nk,vPy,mPyx,mY);
87         }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
88
89         Int_t *xx=new Int_t[fNeff];
90         EndDetAnn(nk,xData,xx,dEtSum,vPx,vPy,mPyx,mY);
91         StoreJets(nk,xData,xx,mY);
92         delete [] xx;
93
94         delete [] xData[0], delete [] xData[1];
95         delete mPyx;
96         delete mY;
97         delete vPx;
98         delete vPy;
99
100 }
101
102 //-----------------------------------------------------------------------------------
103 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
104 {
105 //Initialise the variables used by the algorithm
106         fBeta=0.1;
107         fNclustMax = ((AliDAJetHeader*)fHeader)->GetFixedCl() ? 
108             ((AliDAJetHeader*)fHeader)->GetNclustMax() : 
109             TMath::Max((Int_t)TMath::Sqrt(fNin),5);
110         Float_t etaEff = ((AliDAJetHeader*)fHeader)->GetEtaEff();
111         TClonesArray *lvArray = fReader->GetMomentumArray();
112         Int_t nEntr = lvArray->GetEntries();
113         fNin=0;
114         for (Int_t iEn=0; iEn<nEntr; iEn++) if (fReader->GetCutFlag(iEn)==1) fNin++;
115
116         fNeff = ((AliDAJetHeader*)fHeader)->GetNeff();
117         fNeff = TMath::Max(fNeff,fNin);
118         Double_t *xEta = new Double_t[fNeff];
119         Double_t *xPhi = new Double_t[fNeff];
120         xData[0]=xEta; xData[1]=xPhi;
121         vPx->ResizeTo(fNeff);
122         Int_t iIn=0;
123         for (Int_t iEn=0; iEn<nEntr; iEn++){
124                 if (fReader->GetCutFlag(iEn)==0) continue;
125                 TLorentzVector *lv=(TLorentzVector*)lvArray->At(iEn);
126                 xEta[iIn] = lv->Eta();
127                 xPhi[iIn] = lv->Phi()<0 ? lv->Phi() + 2*TMath::Pi() : lv->Phi();
128                 (*vPx)(iIn)=lv->Pt();
129                 dEtSum+=(*vPx)(iIn);
130                 iIn++;
131         }
132         TRandom2 r;
133         r.SetSeed(0);
134         for (iIn=fNin; iIn<fNeff; iIn++){
135                 xEta[iIn]=r.Uniform(-1*etaEff,etaEff);
136                 xPhi[iIn]=r.Uniform(0.,2*TMath::Pi());
137                 (*vPx)(iIn)=r.Uniform(0.01,0.02);
138                 dEtSum+=(*vPx)(iIn);
139         }
140         for (iIn=0; iIn<fNeff; iIn++) (*vPx)(iIn)=(*vPx)(iIn)/dEtSum;
141
142         Int_t njdim=2*fNclustMax+1;
143         mPyx->ResizeTo(fNeff,njdim);
144         mY->ResizeTo(4,njdim);
145         vPy->ResizeTo(njdim);
146         mY->Zero();mPyx->Zero();vPy->Zero();
147         (*vPy)(0)=1;
148         TMatrixDColumn(*mPyx,0)=1;
149         Double_t ypos=0,xpos=0;
150         for (iIn=0; iIn<fNeff; iIn++){
151                 (*mY)(0,0)+=(*vPx)(iIn)*xEta[iIn];
152                 ypos+=(*vPx)(iIn)*TMath::Sin(xPhi[iIn]);
153                 xpos+=(*vPx)(iIn)*TMath::Cos(xPhi[iIn]);
154         }
155         (*mY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
156 }
157
158 //-----------------------------------------------------------------------------------
159 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk,  TVectorD *vPy,  TMatrixD *mY) const
160 {
161         for(Int_t iClust=0; iClust<nc; iClust++){
162                 (*vPy)(iClust)=(*vPy)(iClust)/2;
163                 (*vPy)(nc+iClust)=(*vPy)(iClust);
164                 for(Int_t iComp=0; iComp<3; iComp++) (*mY)(iComp,nc+iClust)=(*mY)(iComp,iClust);
165         }
166         nk=2*nc;
167 }
168
169 //-----------------------------------------------------------------------------------
170 void AliDAJetFinder::Annealing(Int_t nk,Double_t **xData,  TVectorD *vPx,  TVectorD *vPy,  TMatrixD *mPyx,  TMatrixD *mY)
171 {
172 // Main part of the algorithm
173         const Double_t pi=TMath::Pi();
174         TVectorD *py = new TVectorD(nk);
175         TVectorD *p  = new TVectorD(nk);
176         TMatrixD *y  = new TMatrixD(4,nk);
177         TMatrixD *y1 = new TMatrixD(4,nk);
178         TMatrixD *ry = new TMatrixD(2,nk);
179         Double_t *xEta = xData[0];
180         Double_t *xPhi = xData[1];
181         Double_t Dist(TVectorD,TVectorD);
182
183         Double_t df[2]={fReader->GetReaderHeader()->GetFiducialEtaMax(),pi};
184         TVectorD vPart(2);
185         Double_t *m = new Double_t[nk];
186         Double_t chi,chi1;
187         do{
188                 Int_t nloop=0;
189                 for (Int_t iClust=0; iClust<nk; iClust++){
190                         for (Int_t i=0; i<3; i++)(*y1)(i,iClust)=(*mY)(i,iClust);
191                         (*py)(iClust)=(*vPy)(iClust);
192                 }
193         //perturbation of codevectors
194                 Double_t seed=1000000*gRandom->Rndm(24);
195                 ry->Randomize(-0.5,0.5,seed);
196                 for (Int_t i=0; i<2; i++){
197                         for (Int_t iClust=0; iClust<nk/2; iClust++)
198                                 (*y1)(i,iClust)+=((*ry)(i,iClust)+TMath::Sign(0.5,(*ry)(i,iClust)))*fDelta*df[i];
199                         for (Int_t iClust=nk/2; iClust<nk; iClust++)
200                                 (*y1)(i,iClust)-=((*ry)(i,iClust-nk/2)+TMath::Sign(0.5,(*ry)(i,iClust-nk/2)))*fDelta*df[i];
201                 }
202                 do{
203         //recalculate conditional probabilities
204                         nloop++;
205                         for (Int_t iIn=0; iIn<fNeff; iIn++){
206                                 vPart(0)=xEta[iIn]; vPart(1)=xPhi[iIn];
207                                 for(Int_t iClust=0; iClust<nk; iClust++){
208                                         (*mPyx)(iIn,iClust)=-log((*py)(iClust))+fBeta*Dist(vPart,TMatrixDColumn(*y1,iClust));
209                                         m[iClust]=(*mPyx)(iIn,iClust);
210                                 }
211                                 Double_t pyxNorm=0;
212                                 Double_t minPyx=TMath::MinElement(nk,m);
213                                 for (Int_t iClust=0; iClust<nk; iClust++){
214                                         (*mPyx)(iIn,iClust)-=minPyx;
215                                         (*mPyx)(iIn,iClust)=exp(-(*mPyx)(iIn,iClust));
216                                         pyxNorm+=(*mPyx)(iIn,iClust);
217                                 }
218                                 for (Int_t iClust=0; iClust<nk; iClust++) (*mPyx)(iIn,iClust)/=pyxNorm;
219                         }
220                         p->Zero();
221                         y->Zero();
222         //recalculate codevectors
223                         for (Int_t iClust=0; iClust<nk; iClust++){
224                                 Double_t xpos=0,ypos=0,pxy;
225                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*p)(iClust)+=(*vPx)(iIn)*(*mPyx)(iIn,iClust);
226                                 for (Int_t iIn=0; iIn<fNeff; iIn++){
227                                         pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*p)(iClust);
228                                         ypos+=pxy*TMath::Sin(xPhi[iIn]);
229                                         xpos+=pxy*TMath::Cos(xPhi[iIn]);
230                                         (*y)(0,iClust)+=pxy*xEta[iIn];
231                                 }
232                                 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
233                         }
234         //verify codevectors' stability
235                         chi=0;
236                         for (Int_t iClust=0; iClust<nk; iClust++){
237                                 chi1=TMath::CosH((*y1)(0,iClust)-(*y)(0,iClust))-TMath::Cos((*y1)(1,iClust)-(*y)(1,iClust));
238                                 chi1/=(2*TMath::CosH((*y1)(0,iClust))*TMath::CosH((*y)(0,iClust)));
239                                 chi1*=chi1;
240                                 if (chi1>chi) chi=chi1;
241                         }
242                         chi=TMath::Sqrt(chi);
243                         for (Int_t iClust=0; iClust<nk; iClust++){
244                                 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)=(*y)(i,iClust);
245                                 (*py)(iClust)=(*p)(iClust);
246                         }
247                         if (nloop>fNloopMax){
248                                 if (chi<fEpsMax || nloop>500) break;
249                         }
250                 }while (chi>fEps);
251         }while (chi>fEpsMax);
252         for (Int_t iClust=0; iClust<nk; iClust++){                              //set codevectors and probability equal to those calculated
253                 for (Int_t i=0; i<2; i++) (*mY)(i,iClust)=(*y)(i,iClust);
254                 (*vPy)(iClust)=(*p)(iClust);
255         }
256     delete py;
257     delete p;
258     delete y;
259     delete y1;
260     delete ry;
261     delete [] m;
262 }
263
264 //-----------------------------------------------------------------------------------
265 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk,TVectorD *vPy,  TMatrixD *mPyx,TMatrixD *mY)
266 {
267         static Bool_t growcl=true;
268         
269         if (nk==2) growcl=true;
270         if (growcl){
271 //verify if two codevectors are equal within fAvDist
272                 Int_t *nSame = new Int_t[nk];
273                 Int_t **iSame = new Int_t*[nk];
274                 Int_t **cont = new Int_t*[nk];
275                 for (Int_t iClust=0; iClust<nk; iClust++) cont[iClust]=new Int_t[nk],iSame[iClust]=new Int_t[nk];
276                 for (Int_t iClust=0; iClust<nk; iClust++){
277                         iSame[iClust][iClust]=1;
278                         for (Int_t iClust1=iClust+1; iClust1<nk; iClust1++){
279                                 Double_t eta  = (*mY)(0,iClust) ; Double_t phi  = (*mY)(1,iClust);
280                                 Double_t eta1 = (*mY)(0,iClust1); Double_t phi1 = (*mY)(1,iClust1);
281                                 Double_t distCl=(TMath::CosH(eta-eta1)-TMath::Cos(phi-phi1))/(2*TMath::CosH(eta)*TMath::CosH(eta1));
282                                 if (distCl < fAvDist) iSame[iClust][iClust1]=iSame[iClust1][iClust]=1;
283                                 else iSame[iClust][iClust1]=iSame[iClust1][iClust]=0;
284                         }
285                 }
286                 ReduceClusters(iSame,nk,nc,cont,nSame);
287                 if (nc >= fNclustMax) growcl=false;
288 //recalculate the nc distinct codevectors
289                 TMatrixD *pyx = new TMatrixD(fNeff,2*nc);
290                 TVectorD *py = new TVectorD(nk);
291                 TMatrixD *y1  = new TMatrixD(3,nk);
292                 for (Int_t iClust=0; iClust<nc; iClust++){
293                         for(Int_t j=0; j<nSame[iClust]; j++){
294                                 Int_t iClust1 = cont[iClust][j];
295                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*pyx)(iIn,iClust)+=(*mPyx)(iIn,iClust1);
296                                 (*py)(iClust)+=(*vPy)(iClust1);
297                                 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)+=(*mY)(i,iClust1);
298                         }
299                         for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
300                 }
301                 for (Int_t iClust=0; iClust<nk; iClust++) delete [] cont[iClust], delete [] iSame[iClust];
302                 delete [] iSame;
303                 delete [] cont;
304                 delete [] nSame;
305                 if (nc > nk/2){
306                         for (Int_t iClust=0; iClust<nc; iClust++){
307                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*mPyx)(iIn,iClust)=(*pyx)(iIn,iClust);
308                                 for (Int_t iComp=0; iComp<2; iComp++) (*mY)(iComp,iClust)=(*y1)(iComp,iClust);
309                                 (*vPy)(iClust)=(*py)(iClust);
310                         }
311                         nk=nc;
312                         if (growcl) DoubleClusters(nc,nk,vPy,mY);
313                 }
314                 delete pyx;
315                 delete py;
316                 delete y1;
317         }
318
319 }
320
321 //-----------------------------------------------------------------------------------
322 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut) const
323 {
324         Int_t *nSame = new Int_t[nc];
325         Int_t *iperm = new Int_t[nc];
326         Int_t *go = new Int_t[nc];
327         for (Int_t iCl=0; iCl<nc; iCl++){
328                 nSame[iCl]=0;
329                 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl], cont[iCl][jCl]=0;
330                 iperm[iCl]=iCl;
331                 go[iCl]=1;
332         }
333         TMath::Sort(nc,nSame,iperm,true);
334         Int_t l=0;
335         for (Int_t iCl=0; iCl<nc; iCl++){
336                 Int_t k=iperm[iCl];
337                 if (go[k] == 1){
338                         Int_t m=0;
339                         for (Int_t jCl=0; jCl<nc; jCl++){
340                                 if (iSame[k][jCl] == 1){
341                                         cont[l][m]=jCl;
342                                         go[jCl]=0;
343                                         m++;
344                                 }
345                         }
346                         nSameOut[l]=m;
347                         l++;
348                 }
349         }
350         ncout=l;
351         delete [] nSame;
352         delete [] iperm;
353         delete [] go;
354 }
355
356 //-----------------------------------------------------------------------------------
357 void AliDAJetFinder::EndDetAnn(Int_t &nk,Double_t **xData,Int_t *xx,Double_t etx,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
358 {
359 //now assign each particle to only one cluster
360         Double_t *clusters=new Double_t[nk];
361         for (Int_t iIn=0; iIn<fNeff; iIn++){
362                 for (Int_t iClust=0; iClust<nk; iClust++) clusters[iClust]=(*mPyx)(iIn,iClust);
363                 xx[iIn]=TMath::LocMax(nk,clusters);
364         }
365         delete [] clusters;
366         
367 //recalculate codevectors, having all p(y|x)=0 or 1
368         Double_t *xEta = xData[0];
369         Double_t *xPhi = xData[1];
370         mY->Zero();
371         mPyx->Zero();
372         vPy->Zero();
373         for (Int_t iIn=0; iIn<fNin; iIn++){
374                 Int_t iClust=xx[iIn];
375                 (*mPyx)(iIn,iClust)=1;
376                 (*vPy)(iClust)+=(*vPx)(iIn);
377                 (*mY)(0,iClust)+=(*vPx)(iIn)*xEta[iIn];
378                 (*mY)(3,iClust)+=(*vPx)(iIn)*etx;
379         }
380         Int_t k=0;
381         for (Int_t iClust=0; iClust<nk; iClust++){
382                 if ((*vPy)(iClust)>0){
383                         Double_t xpos=0,ypos=0,pxy;
384                         for (Int_t iIn=0; iIn<fNin; iIn++){
385                                 pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*vPy)(iClust);
386                                 ypos+=pxy*TMath::Sin(xPhi[iIn]);
387                                 xpos+=pxy*TMath::Cos(xPhi[iIn]);
388                                 if (xx[iIn]==iClust) xx[iIn]=k;
389                         }
390                         (*mY)(0,k)=(*mY)(0,iClust)/(*vPy)(iClust);
391                         (*mY)(1,k)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
392                         (*mY)(3,k)=(*mY)(3,iClust);
393                         k++;
394                 }
395         }
396         nk=k;
397 }
398
399 //-----------------------------------------------------------------------------------
400 void AliDAJetFinder::StoreJets(Int_t nk, Double_t **xData, Int_t *xx, TMatrixD *mY)
401 {
402 //evaluate significant clusters properties
403         const Double_t pi=TMath::Pi();
404         AliJetReaderHeader *rHeader=fReader->GetReaderHeader();
405         Float_t dFidEtaMax = rHeader->GetFiducialEtaMax();
406         Float_t dFidEtaMin = rHeader->GetFiducialEtaMin();
407         Float_t dFiducialEta= dFidEtaMax - dFidEtaMin;
408         Double_t *xEta = xData[0];
409         Double_t *xPhi = xData[1];
410         Int_t nEff = 0;
411         for (Int_t i=0; i<fNeff; i++) if (xEta[i]<dFidEtaMax && xEta[i]>dFidEtaMin) nEff++;
412         Double_t dMeanDist=TMath::Sqrt(2*dFiducialEta*pi/nEff);
413         Bool_t   *isJet = new Bool_t[nk];
414         Double_t *etNoBg= new Double_t[nk];
415         Double_t *dDeltaEta=new Double_t[nk];
416         Double_t *dDeltaPhi=new Double_t[nk];
417         Double_t *surf  = new Double_t[nk];
418         Double_t *etDens= new Double_t[nk];
419         for (Int_t iClust=0; iClust<nk; iClust++){
420                 isJet[iClust]=false;
421                 Double_t dEtaMin=10.,dEtaMax=-10.,dPhiMin=10.,dPhiMax=0.;
422                 for (Int_t iIn=0; iIn<fNeff; iIn++){
423                         if (xx[iIn]!=iClust || xEta[iIn]>dFidEtaMax || xEta[iIn]<dFidEtaMin) continue;
424                         if (xEta[iIn] < dEtaMin) dEtaMin=xEta[iIn];
425                         if (xEta[iIn] > dEtaMax) dEtaMax=xEta[iIn];
426                         Double_t dPhi=xPhi[iIn]-(*mY)(1,iClust);
427                         if      (dPhi > pi     ) dPhi-=2*pi;
428                         else if (dPhi < (-1)*pi) dPhi+=2*pi;
429                         if      (dPhi < dPhiMin) dPhiMin=dPhi;
430                         else if (dPhi > dPhiMax) dPhiMax=dPhi;
431                 }
432                 dDeltaEta[iClust]=dEtaMax-dEtaMin+dMeanDist;
433                 dDeltaPhi[iClust]=dPhiMax-dPhiMin+dMeanDist;
434                 surf[iClust]=0.25*pi*dDeltaEta[iClust]*dDeltaPhi[iClust];
435                 etDens[iClust]=(*mY)(3,iClust)/surf[iClust];
436         }
437
438         if (((AliDAJetHeader*)fHeader)->GetSelJets()){
439                 for (Int_t iClust=0; iClust<nk; iClust++){
440                         if (!isJet[iClust] && (*mY)(0,iClust)<dFidEtaMax && (*mY)(0,iClust)>dFidEtaMin){
441                                 Double_t etDensMed=0.;
442                                 Double_t etDensSqr=0.;
443                                 Int_t norm=0;
444                                 for (Int_t iClust1=0; iClust1<nk; iClust1++){
445                                         if(iClust1!=iClust && (*mY)(0,iClust)<dFidEtaMax && (*mY)(0,iClust)>dFidEtaMin){
446                                                 etDensMed+=etDens[iClust1];
447                                                 etDensSqr+=TMath::Power(etDens[iClust1],2);
448                                                 norm++;
449                                         }
450                                 }
451                                 etDensMed/=TMath::Max(norm,1);
452                                 etDensSqr/=TMath::Max(norm,1);
453                                 Double_t deltaEtDens=TMath::Sqrt(etDensSqr-TMath::Power(etDensMed,2));
454                                 if ((*mY)(3,iClust) > (etDensMed+deltaEtDens)*surf[iClust]) isJet[iClust]=kTRUE;
455                                 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
456                         }
457                 }
458                 for (Int_t iClust=0; iClust<nk; iClust++){
459                         if (isJet[iClust]){
460                                 Double_t etDensMed=0;
461                                 Double_t extSurf=2*dFiducialEta*pi;
462                                 for (Int_t iClust1=0; iClust1<nk; iClust1++){
463                                         if (!isJet[iClust1]) etDensMed+=(*mY)(3,iClust1);
464                                         else extSurf-=surf[iClust1];
465                                 }
466                                 etDensMed/=extSurf;
467                                 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
468                                 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
469                                         isJet[iClust]=kFALSE;
470                                         iClust=-1;
471                                 }
472                         }
473                 }
474         } else {
475                 for (Int_t iClust=0; iClust<nk; iClust++){
476                         isJet[iClust]=true;
477                         etNoBg[iClust]=(*mY)(3,iClust);
478                 }
479         }
480         delete [] etDens;
481         delete [] surf;
482         
483 //now add selected jets to the list
484         Int_t *iSort = new Int_t[nk];
485         TMath::Sort(nk,etNoBg,iSort,true);
486         Int_t iCl = 0;
487         TRefArray *refs = 0;
488         Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
489         if (fromAod) refs = fReader->GetReferences();
490         for (Int_t iClust=0; iClust<nk; iClust++){                                                                      //clusters loop
491                 iCl=iSort[iClust];
492                 if (isJet[iCl]){                                                                                                                //choose cluster
493                         Float_t px,py,pz,en;
494                         px = (*mY)(3,iCl)*TMath::Cos((*mY)(1,iCl));
495                         py = (*mY)(3,iCl)*TMath::Sin((*mY)(1,iCl));
496                         pz = (*mY)(3,iCl)/TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-(*mY)(0,iCl))));
497                         en = TMath::Sqrt(px * px + py * py + pz * pz);
498                         AliAODJet jet(px, py, pz, en);
499                         if (fromAod){
500                                 Int_t iIn=0;
501                                 Int_t nEntr = fReader->GetMomentumArray()->GetEntries();
502                                 for (Int_t iEn=0; iEn<nEntr; iEn++){
503                                         if (fReader->GetCutFlag(iEn)==0) continue;
504                                         if (xx[iIn]==iCl) jet.AddTrack(refs->At(iEn));
505                                         iIn++;
506                                 }
507                         }
508                         AddJet(jet);
509                         if (fDebug > 0) printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iCl,jet.Eta(),jet.Phi(),jet.Pt());
510                 }
511         }
512         delete [] dDeltaEta; delete [] dDeltaPhi;
513         delete [] etNoBg;
514         delete [] isJet;
515         delete [] iSort;
516 }
517
518 //-----------------------------------------------------------------------------------
519 Double_t Dist(TVectorD x,TVectorD y)
520 {
521 // Squared distance
522         const Double_t pi=TMath::Pi();
523         Double_t dphi=TMath::Abs(x(1)-y(1));
524         if (dphi > pi) dphi=2*pi-dphi;
525         Double_t dist=pow(x(0)-y(0),2)+pow(dphi,2);
526         return dist;
527 }