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