]> git.uio.no Git - u/mrichter/AliRoot.git/blob - JETAN/AliDAJetFinder.cxx
New class to extract pt distribution of given particle from pt distribution of a...
[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) return;
74
75         Int_t nc=1, nk;
76         DoubleClusters(nc,nk,vPy,mY);
77         do{                                     //loop over beta
78                 fBeta*=fAlpha;
79                 Annealing(nk,xData,vPx,vPy,mPyx,mY);
80                 NumCl(nc,nk,vPy,mPyx,mY);
81         }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
82
83         Int_t *xx=new Int_t[fNeff];
84         EndDetAnn(nk,xData,xx,dEtSum,vPx,vPy,mPyx,mY);
85         StoreJets(nk,xData,xx,mY);
86         delete [] xx;
87
88         delete [] xData[0], delete [] xData[1];
89         delete mPyx;
90         delete mY;
91         delete vPx;
92         delete vPy;
93
94 }
95
96 //-----------------------------------------------------------------------------------
97 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
98 {
99 //Initialise the variables used by the algorithm
100         fBeta=0.1;
101         fNclustMax = ((AliDAJetHeader*)fHeader)->GetFixedCl() ? 
102             ((AliDAJetHeader*)fHeader)->GetNclustMax() : 
103             TMath::Max((Int_t)TMath::Sqrt(fNin),5);
104         Float_t etaEff = ((AliDAJetHeader*)fHeader)->GetEtaEff();
105         TClonesArray *lvArray = fReader->GetMomentumArray();
106         Int_t nEntr = lvArray->GetEntries();
107         fNin=0;
108         for (Int_t iEn=0; iEn<nEntr; iEn++) if (fReader->GetCutFlag(iEn)==1) fNin++;
109
110         fNeff = ((AliDAJetHeader*)fHeader)->GetNeff();
111         fNeff = TMath::Max(fNeff,fNin);
112         Double_t *xEta = new Double_t[fNeff];
113         Double_t *xPhi = new Double_t[fNeff];
114         xData[0]=xEta; xData[1]=xPhi;
115         vPx->ResizeTo(fNeff);
116         Int_t iIn=0;
117         for (Int_t iEn=0; iEn<nEntr; iEn++){
118                 if (fReader->GetCutFlag(iEn)==0) continue;
119                 TLorentzVector *lv=(TLorentzVector*)lvArray->At(iEn);
120                 xEta[iIn] = lv->Eta();
121                 xPhi[iIn] = lv->Phi()<0 ? lv->Phi() + 2*TMath::Pi() : lv->Phi();
122                 (*vPx)(iIn)=lv->Pt();
123                 dEtSum+=(*vPx)(iIn);
124                 iIn++;
125         }
126         TRandom2 r;
127         r.SetSeed(0);
128         for (Int_t iIn=fNin; iIn<fNeff; iIn++){
129                 xEta[iIn]=r.Uniform(-1*etaEff,etaEff);
130                 xPhi[iIn]=r.Uniform(0.,2*TMath::Pi());
131                 (*vPx)(iIn)=r.Uniform(0.01,0.02);
132                 dEtSum+=(*vPx)(iIn);
133         }
134         for (iIn=0; iIn<fNeff; iIn++) (*vPx)(iIn)=(*vPx)(iIn)/dEtSum;
135
136         Int_t njdim=2*fNclustMax+1;
137         mPyx->ResizeTo(fNeff,njdim);
138         mY->ResizeTo(4,njdim);
139         vPy->ResizeTo(njdim);
140         mY->Zero();mPyx->Zero();vPy->Zero();
141         (*vPy)(0)=1;
142         TMatrixDColumn(*mPyx,0)=1;
143         Double_t ypos=0,xpos=0;
144         for (iIn=0; iIn<fNeff; iIn++){
145                 (*mY)(0,0)+=(*vPx)(iIn)*xEta[iIn];
146                 ypos+=(*vPx)(iIn)*TMath::Sin(xPhi[iIn]);
147                 xpos+=(*vPx)(iIn)*TMath::Cos(xPhi[iIn]);
148         }
149         (*mY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
150 }
151
152 //-----------------------------------------------------------------------------------
153 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk,  TVectorD *vPy,  TMatrixD *mY) const
154 {
155         for(Int_t iClust=0; iClust<nc; iClust++){
156                 (*vPy)(iClust)=(*vPy)(iClust)/2;
157                 (*vPy)(nc+iClust)=(*vPy)(iClust);
158                 for(Int_t iComp=0; iComp<3; iComp++) (*mY)(iComp,nc+iClust)=(*mY)(iComp,iClust);
159         }
160         nk=2*nc;
161 }
162
163 //-----------------------------------------------------------------------------------
164 void AliDAJetFinder::Annealing(Int_t nk,Double_t **xData,  TVectorD *vPx,  TVectorD *vPy,  TMatrixD *mPyx,  TMatrixD *mY)
165 {
166 // Main part of the algorithm
167         const Double_t pi=TMath::Pi();
168         TVectorD *py = new TVectorD(nk);
169         TVectorD *p  = new TVectorD(nk);
170         TMatrixD *y  = new TMatrixD(4,nk);
171         TMatrixD *y1 = new TMatrixD(4,nk);
172         TMatrixD *ry = new TMatrixD(2,nk);
173         Double_t *xEta = xData[0];
174         Double_t *xPhi = xData[1];
175         Double_t Dist(TVectorD,TVectorD);
176
177         Double_t df[2]={fReader->GetReaderHeader()->GetFiducialEtaMax(),pi};
178         TVectorD vPart(2);
179         Double_t *m = new Double_t[nk];
180         Double_t chi,chi1;
181         do{
182                 Int_t nloop=0;
183                 for (Int_t iClust=0; iClust<nk; iClust++){
184                         for (Int_t i=0; i<3; i++)(*y1)(i,iClust)=(*mY)(i,iClust);
185                         (*py)(iClust)=(*vPy)(iClust);
186                 }
187         //perturbation of codevectors
188                 Double_t seed=1000000*gRandom->Rndm(24);
189                 ry->Randomize(-0.5,0.5,seed);
190                 for (Int_t i=0; i<2; i++){
191                         for (Int_t iClust=0; iClust<nk/2; iClust++)
192                                 (*y1)(i,iClust)+=((*ry)(i,iClust)+TMath::Sign(0.5,(*ry)(i,iClust)))*fDelta*df[i];
193                         for (Int_t iClust=nk/2; iClust<nk; iClust++)
194                                 (*y1)(i,iClust)-=((*ry)(i,iClust-nk/2)+TMath::Sign(0.5,(*ry)(i,iClust-nk/2)))*fDelta*df[i];
195                 }
196                 do{
197         //recalculate conditional probabilities
198                         nloop++;
199                         for (Int_t iIn=0; iIn<fNeff; iIn++){
200                                 vPart(0)=xEta[iIn]; vPart(1)=xPhi[iIn];
201                                 for(Int_t iClust=0; iClust<nk; iClust++){
202                                         (*mPyx)(iIn,iClust)=-log((*py)(iClust))+fBeta*Dist(vPart,TMatrixDColumn(*y1,iClust));
203                                         m[iClust]=(*mPyx)(iIn,iClust);
204                                 }
205                                 Double_t pyxNorm=0;
206                                 Double_t minPyx=TMath::MinElement(nk,m);
207                                 for (Int_t iClust=0; iClust<nk; iClust++){
208                                         (*mPyx)(iIn,iClust)-=minPyx;
209                                         (*mPyx)(iIn,iClust)=exp(-(*mPyx)(iIn,iClust));
210                                         pyxNorm+=(*mPyx)(iIn,iClust);
211                                 }
212                                 for (Int_t iClust=0; iClust<nk; iClust++) (*mPyx)(iIn,iClust)/=pyxNorm;
213                         }
214                         p->Zero();
215                         y->Zero();
216         //recalculate codevectors
217                         for (Int_t iClust=0; iClust<nk; iClust++){
218                                 Double_t xpos=0,ypos=0,pxy;
219                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*p)(iClust)+=(*vPx)(iIn)*(*mPyx)(iIn,iClust);
220                                 for (Int_t iIn=0; iIn<fNeff; iIn++){
221                                         pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*p)(iClust);
222                                         ypos+=pxy*TMath::Sin(xPhi[iIn]);
223                                         xpos+=pxy*TMath::Cos(xPhi[iIn]);
224                                         (*y)(0,iClust)+=pxy*xEta[iIn];
225                                 }
226                                 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
227                         }
228         //verify codevectors' stability
229                         chi=0;
230                         for (Int_t iClust=0; iClust<nk; iClust++){
231                                 chi1=TMath::CosH((*y1)(0,iClust)-(*y)(0,iClust))-TMath::Cos((*y1)(1,iClust)-(*y)(1,iClust));
232                                 chi1/=(2*TMath::CosH((*y1)(0,iClust))*TMath::CosH((*y)(0,iClust)));
233                                 chi1*=chi1;
234                                 if (chi1>chi) chi=chi1;
235                         }
236                         chi=TMath::Sqrt(chi);
237                         for (Int_t iClust=0; iClust<nk; iClust++){
238                                 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)=(*y)(i,iClust);
239                                 (*py)(iClust)=(*p)(iClust);
240                         }
241                         if (nloop>fNloopMax){
242                                 if (chi<fEpsMax || nloop>500) break;
243                         }
244                 }while (chi>fEps);
245         }while (chi>fEpsMax);
246         for (Int_t iClust=0; iClust<nk; iClust++){                              //set codevectors and probability equal to those calculated
247                 for (Int_t i=0; i<2; i++) (*mY)(i,iClust)=(*y)(i,iClust);
248                 (*vPy)(iClust)=(*p)(iClust);
249         }
250     delete py;
251     delete p;
252     delete y;
253     delete y1;
254     delete ry;
255     delete [] m;
256 }
257
258 //-----------------------------------------------------------------------------------
259 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk,TVectorD *vPy,  TMatrixD *mPyx,TMatrixD *mY)
260 {
261         static Bool_t growcl=true;
262         
263         if (nk==2) growcl=true;
264         if (growcl){
265 //verify if two codevectors are equal within fAvDist
266                 Int_t *nSame = new Int_t[nk];
267                 Int_t **iSame = new Int_t*[nk];
268                 Int_t **cont = new Int_t*[nk];
269                 for (Int_t iClust=0; iClust<nk; iClust++) cont[iClust]=new Int_t[nk],iSame[iClust]=new Int_t[nk];
270                 for (Int_t iClust=0; iClust<nk; iClust++){
271                         iSame[iClust][iClust]=1;
272                         for (Int_t iClust1=iClust+1; iClust1<nk; iClust1++){
273                                 Double_t eta  = (*mY)(0,iClust) ; Double_t phi  = (*mY)(1,iClust);
274                                 Double_t eta1 = (*mY)(0,iClust1); Double_t phi1 = (*mY)(1,iClust1);
275                                 Double_t distCl=(TMath::CosH(eta-eta1)-TMath::Cos(phi-phi1))/(2*TMath::CosH(eta)*TMath::CosH(eta1));
276                                 if (distCl < fAvDist) iSame[iClust][iClust1]=iSame[iClust1][iClust]=1;
277                                 else iSame[iClust][iClust1]=iSame[iClust1][iClust]=0;
278                         }
279                 }
280                 ReduceClusters(iSame,nk,nc,cont,nSame);
281                 if (nc >= fNclustMax) growcl=false;
282 //recalculate the nc distinct codevectors
283                 TMatrixD *pyx = new TMatrixD(fNeff,2*nc);
284                 TVectorD *py = new TVectorD(nk);
285                 TMatrixD *y1  = new TMatrixD(3,nk);
286                 for (Int_t iClust=0; iClust<nc; iClust++){
287                         for(Int_t j=0; j<nSame[iClust]; j++){
288                                 Int_t iClust1 = cont[iClust][j];
289                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*pyx)(iIn,iClust)+=(*mPyx)(iIn,iClust1);
290                                 (*py)(iClust)+=(*vPy)(iClust1);
291                                 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)+=(*mY)(i,iClust1);
292                         }
293                         for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
294                 }
295                 for (Int_t iClust=0; iClust<nk; iClust++) delete [] cont[iClust], delete [] iSame[iClust];
296                 delete [] iSame;
297                 delete [] cont;
298                 delete [] nSame;
299                 if (nc > nk/2){
300                         for (Int_t iClust=0; iClust<nc; iClust++){
301                                 for (Int_t iIn=0; iIn<fNeff; iIn++) (*mPyx)(iIn,iClust)=(*pyx)(iIn,iClust);
302                                 for (Int_t iComp=0; iComp<2; iComp++) (*mY)(iComp,iClust)=(*y1)(iComp,iClust);
303                                 (*vPy)(iClust)=(*py)(iClust);
304                         }
305                         nk=nc;
306                         if (growcl) DoubleClusters(nc,nk,vPy,mY);
307                 }
308                 delete pyx;
309                 delete py;
310                 delete y1;
311         }
312
313 }
314
315 //-----------------------------------------------------------------------------------
316 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut) const
317 {
318         Int_t *nSame = new Int_t[nc];
319         Int_t *iperm = new Int_t[nc];
320         Int_t *go = new Int_t[nc];
321         for (Int_t iCl=0; iCl<nc; iCl++){
322                 nSame[iCl]=0;
323                 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl], cont[iCl][jCl]=0;
324                 iperm[iCl]=iCl;
325                 go[iCl]=1;
326         }
327         TMath::Sort(nc,nSame,iperm,true);
328         Int_t l=0;
329         for (Int_t iCl=0; iCl<nc; iCl++){
330                 Int_t k=iperm[iCl];
331                 if (go[k] == 1){
332                         Int_t m=0;
333                         for (Int_t jCl=0; jCl<nc; jCl++){
334                                 if (iSame[k][jCl] == 1){
335                                         cont[l][m]=jCl;
336                                         go[jCl]=0;
337                                         m++;
338                                 }
339                         }
340                         nSameOut[l]=m;
341                         l++;
342                 }
343         }
344         ncout=l;
345         delete [] nSame;
346         delete [] iperm;
347         delete [] go;
348 }
349
350 //-----------------------------------------------------------------------------------
351 void AliDAJetFinder::EndDetAnn(Int_t &nk,Double_t **xData,Int_t *xx,Double_t etx,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
352 {
353 //now assign each particle to only one cluster
354         Double_t *clusters=new Double_t[nk];
355         for (Int_t iIn=0; iIn<fNeff; iIn++){
356                 for (Int_t iClust=0; iClust<nk; iClust++) clusters[iClust]=(*mPyx)(iIn,iClust);
357                 xx[iIn]=TMath::LocMax(nk,clusters);
358         }
359         delete [] clusters;
360         
361 //recalculate codevectors, having all p(y|x)=0 or 1
362         Double_t *xEta = xData[0];
363         Double_t *xPhi = xData[1];
364         mY->Zero();
365         mPyx->Zero();
366         vPy->Zero();
367         for (Int_t iIn=0; iIn<fNin; iIn++){
368                 Int_t iClust=xx[iIn];
369                 (*mPyx)(iIn,iClust)=1;
370                 (*vPy)(iClust)+=(*vPx)(iIn);
371                 (*mY)(0,iClust)+=(*vPx)(iIn)*xEta[iIn];
372                 (*mY)(3,iClust)+=(*vPx)(iIn)*etx;
373         }
374         Int_t k=0;
375         for (Int_t iClust=0; iClust<nk; iClust++){
376                 if ((*vPy)(iClust)>0){
377                         Double_t xpos=0,ypos=0,pxy;
378                         for (Int_t iIn=0; iIn<fNin; iIn++){
379                                 pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*vPy)(iClust);
380                                 ypos+=pxy*TMath::Sin(xPhi[iIn]);
381                                 xpos+=pxy*TMath::Cos(xPhi[iIn]);
382                                 if (xx[iIn]==iClust) xx[iIn]=k;
383                         }
384                         (*mY)(0,k)=(*mY)(0,iClust)/(*vPy)(iClust);
385                         (*mY)(1,k)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
386                         (*mY)(3,k)=(*mY)(3,iClust);
387                         k++;
388                 }
389         }
390         nk=k;
391 }
392
393 //-----------------------------------------------------------------------------------
394 void AliDAJetFinder::StoreJets(Int_t nk, Double_t **xData, Int_t *xx, TMatrixD *mY)
395 {
396 //evaluate significant clusters properties
397         const Double_t pi=TMath::Pi();
398         AliJetReaderHeader *rHeader=fReader->GetReaderHeader();
399         Float_t dFidEtaMax = rHeader->GetFiducialEtaMax();
400         Float_t dFidEtaMin = rHeader->GetFiducialEtaMin();
401         Float_t dFiducialEta= dFidEtaMax - dFidEtaMin;
402         Double_t *xEta = xData[0];
403         Double_t *xPhi = xData[1];
404         Int_t nEff = 0;
405         for (Int_t i=0; i<fNeff; i++) if (xEta[i]<dFidEtaMax && xEta[i]>dFidEtaMin) nEff++;
406         Double_t dMeanDist=TMath::Sqrt(2*dFiducialEta*pi/nEff);
407         Bool_t   *isJet = new Bool_t[nk];
408         Double_t *etNoBg= new Double_t[nk];
409         Double_t *dDeltaEta=new Double_t[nk];
410         Double_t *dDeltaPhi=new Double_t[nk];
411         Double_t *surf  = new Double_t[nk];
412         Double_t *etDens= new Double_t[nk];
413         for (Int_t iClust=0; iClust<nk; iClust++){
414                 isJet[iClust]=false;
415                 Double_t dEtaMin=10.,dEtaMax=-10.,dPhiMin=10.,dPhiMax=0.;
416                 for (Int_t iIn=0; iIn<fNeff; iIn++){
417                         if (xx[iIn]!=iClust || xEta[iIn]>dFidEtaMax || xEta[iIn]<dFidEtaMin) continue;
418                         if (xEta[iIn] < dEtaMin) dEtaMin=xEta[iIn];
419                         if (xEta[iIn] > dEtaMax) dEtaMax=xEta[iIn];
420                         Double_t dPhi=xPhi[iIn]-(*mY)(1,iClust);
421                         if      (dPhi > pi     ) dPhi-=2*pi;
422                         else if (dPhi < (-1)*pi) dPhi+=2*pi;
423                         if      (dPhi < dPhiMin) dPhiMin=dPhi;
424                         else if (dPhi > dPhiMax) dPhiMax=dPhi;
425                 }
426                 dDeltaEta[iClust]=dEtaMax-dEtaMin+dMeanDist;
427                 dDeltaPhi[iClust]=dPhiMax-dPhiMin+dMeanDist;
428                 surf[iClust]=0.25*pi*dDeltaEta[iClust]*dDeltaPhi[iClust];
429                 etDens[iClust]=(*mY)(3,iClust)/surf[iClust];
430         }
431
432         if (((AliDAJetHeader*)fHeader)->GetSelJets()){
433                 for (Int_t iClust=0; iClust<nk; iClust++){
434                         if (!isJet[iClust] && (*mY)(0,iClust)<dFidEtaMax && (*mY)(0,iClust)>dFidEtaMin){
435                                 Double_t etDensMed=0.;
436                                 Double_t etDensSqr=0.;
437                                 Int_t norm=0;
438                                 for (Int_t iClust1=0; iClust1<nk; iClust1++){
439                                         if(iClust1!=iClust && (*mY)(0,iClust)<dFidEtaMax && (*mY)(0,iClust)>dFidEtaMin){
440                                                 etDensMed+=etDens[iClust1];
441                                                 etDensSqr+=TMath::Power(etDens[iClust1],2);
442                                                 norm++;
443                                         }
444                                 }
445                                 etDensMed/=TMath::Max(norm,1);
446                                 etDensSqr/=TMath::Max(norm,1);
447                                 Double_t deltaEtDens=TMath::Sqrt(etDensSqr-TMath::Power(etDensMed,2));
448                                 if ((*mY)(3,iClust) > (etDensMed+deltaEtDens)*surf[iClust]) isJet[iClust]=kTRUE;
449                                 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
450                         }
451                 }
452                 for (Int_t iClust=0; iClust<nk; iClust++){
453                         if (isJet[iClust]){
454                                 Double_t etDensMed=0;
455                                 Double_t extSurf=2*dFiducialEta*pi;
456                                 for (Int_t iClust1=0; iClust1<nk; iClust1++){
457                                         if (!isJet[iClust1]) etDensMed+=(*mY)(3,iClust1);
458                                         else extSurf-=surf[iClust1];
459                                 }
460                                 etDensMed/=extSurf;
461                                 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
462                                 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
463                                         isJet[iClust]=kFALSE;
464                                         iClust=-1;
465                                 }
466                         }
467                 }
468         } else {
469                 for (Int_t iClust=0; iClust<nk; iClust++){
470                         isJet[iClust]=true;
471                         etNoBg[iClust]=(*mY)(3,iClust);
472                 }
473         }
474         delete [] etDens;
475         delete [] surf;
476         
477 //now add selected jets to the list
478         Int_t *iSort = new Int_t[nk];
479         TMath::Sort(nk,etNoBg,iSort,true);
480         Int_t iCl = 0;
481         TRefArray *refs = 0;
482         Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
483         if (fromAod) refs = fReader->GetReferences();
484         for (Int_t iClust=0; iClust<nk; iClust++){                                                                      //clusters loop
485                 iCl=iSort[iClust];
486                 if (isJet[iCl]){                                                                                                                //choose cluster
487                         Float_t px,py,pz,en;
488                         px = (*mY)(3,iCl)*TMath::Cos((*mY)(1,iCl));
489                         py = (*mY)(3,iCl)*TMath::Sin((*mY)(1,iCl));
490                         pz = (*mY)(3,iCl)/TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-(*mY)(0,iCl))));
491                         en = TMath::Sqrt(px * px + py * py + pz * pz);
492                         AliAODJet jet(px, py, pz, en);
493                         if (fromAod){
494                                 Int_t iIn=0;
495                                 Int_t nEntr = fReader->GetMomentumArray()->GetEntries();
496                                 for (Int_t iEn=0; iEn<nEntr; iEn++){
497                                         if (fReader->GetCutFlag(iEn)==0) continue;
498                                         if (xx[iIn]==iCl) jet.AddTrack(refs->At(iEn));
499                                         iIn++;
500                                 }
501                         }
502                         AddJet(jet);
503                         if (fDebug > 0) printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iCl,jet.Eta(),jet.Phi(),jet.Pt());
504                 }
505         }
506         delete [] dDeltaEta; delete [] dDeltaPhi;
507         delete [] etNoBg;
508         delete [] isJet;
509         delete [] iSort;
510 }
511
512 //-----------------------------------------------------------------------------------
513 Double_t Dist(TVectorD x,TVectorD y)
514 {
515 // Squared distance
516         const Double_t pi=TMath::Pi();
517         Double_t dphi=TMath::Abs(x(1)-y(1));
518         if (dphi > pi) dphi=2*pi-dphi;
519         Double_t dist=pow(x(0)-y(0),2)+pow(dphi,2);
520         return dist;
521 }