2 // **************************************************************************
3 // * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 // * Author: The ALICE Off-line Project. *
6 // * Contributors are mentioned in the code where appropriate. *
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 // **************************************************************************
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 //-----------------------------------------------------------------------------------
26 #include <TClonesArray.h>
27 #include "AliJetReader.h"
28 #include "AliDAJetHeader.h"
29 #include "AliDAJetFinder.h"
32 ClassImp(AliDAJetFinder)
35 //-----------------------------------------------------------------------------------
36 AliDAJetFinder::AliDAJetFinder():
56 //-----------------------------------------------------------------------------------
57 AliDAJetFinder::~AliDAJetFinder()
68 //-----------------------------------------------------------------------------------
69 void AliDAJetFinder::FindJets()
71 // Find the jets in current event
73 Float_t betaStop=100.;
80 DoubleClusters(nc,nk);
85 }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
87 Int_t *xx=new Int_t[fNin];
88 EndDetAnn(nk,xx,dEtSum);
94 //-----------------------------------------------------------------------------------
95 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum)
97 //Initialise the variables used by the algorithm
99 TClonesArray *lvArray = fReader->GetMomentumArray();
100 fNin = lvArray->GetEntries();
101 fNclustMax= ((AliDAJetHeader*)fHeader)->GetFixedCl() ?
102 ((AliDAJetHeader*)fHeader)->GetNclustMax()
104 TMath::Max((Int_t)TMath::Sqrt(fNin),5);
105 fXEta=new Double_t[fNin]; fXPhi=new Double_t[fNin];
106 fPx = new TVectorD(fNin);
107 for (Int_t iIn=0; iIn<fNin; iIn++){
108 TLorentzVector *lv=(TLorentzVector*)lvArray->At(iIn);
109 fXEta[iIn] = lv->Eta();
110 fXPhi[iIn] = lv->Phi()<0 ? lv->Phi() + 2*TMath::Pi() : lv->Phi();
111 (*fPx)(iIn)=lv->Pt();
114 for (Int_t iIn=0; iIn<fNin; iIn++) (*fPx)(iIn)=(*fPx)(iIn)/dEtSum;
116 Int_t njdim=2*fNclustMax+1;
117 fPyx = new TMatrixD(fNin,njdim);
118 fY = new TMatrixD(4,njdim);
119 fPy= new TVectorD(njdim);
120 fY->Zero();fPyx->Zero();fPy->Zero();
122 TMatrixDColumn(*fPyx,0)=1;
123 Double_t ypos=0,xpos=0;
124 for (Int_t iIn=0; iIn<fNin; iIn++){
125 (*fY)(0,0)+=(*fPx)(iIn)*fXEta[iIn];
126 ypos+=(*fPx)(iIn)*TMath::Sin(fXPhi[iIn]);
127 xpos+=(*fPx)(iIn)*TMath::Cos(fXPhi[iIn]);
129 (*fY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
133 //-----------------------------------------------------------------------------------
134 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk)
136 for(Int_t iClust=0; iClust<nc; iClust++){
137 (*fPy)(iClust)=(*fPy)(iClust)/2;
138 (*fPy)(nc+iClust)=(*fPy)(iClust);
139 for(Int_t iComp=0; iComp<3; iComp++) (*fY)(iComp,nc+iClust)=(*fY)(iComp,iClust);
144 //-----------------------------------------------------------------------------------
145 void AliDAJetFinder::Annealing(Int_t nk)
147 // Main part of the algorithm
148 const Double_t pi=TMath::Pi();
149 TVectorD *py = new TVectorD(nk);
150 TVectorD *p = new TVectorD(nk);
151 TMatrixD *y = new TMatrixD(4,nk);
152 TMatrixD *y1 = new TMatrixD(4,nk);
153 TMatrixD *ry = new TMatrixD(2,nk);
154 Double_t Dist(TVectorD,TVectorD);
156 Double_t df[2]={((AliDAJetHeader*)fHeader)->GetEtaCut(),pi};
158 Double_t *m = new Double_t[nk];
162 for (Int_t iClust=0; iClust<nk; iClust++){
163 for (Int_t i=0; i<3; i++)(*y1)(i,iClust)=(*fY)(i,iClust);
164 (*py)(iClust)=(*fPy)(iClust);
166 //perturbation of codevectors
167 Double_t seed=1000000*gRandom->Rndm(24);
168 ry->Randomize(-0.5,0.5,seed);
169 for (Int_t i=0; i<2; i++){
170 for (Int_t iClust=0; iClust<nk/2; iClust++)
171 (*y1)(i,iClust)+=((*ry)(i,iClust)+TMath::Sign(0.5,(*ry)(i,iClust)))*fDelta*df[i];
172 for (Int_t iClust=nk/2; iClust<nk; iClust++)
173 (*y1)(i,iClust)-=((*ry)(i,iClust-nk/2)+TMath::Sign(0.5,(*ry)(i,iClust-nk/2)))*fDelta*df[i];
176 //recalculate conditional probabilities
178 for (Int_t iIn=0; iIn<fNin; iIn++){
179 vPart(0)=fXEta[iIn]; vPart(1)=fXPhi[iIn];
180 for(Int_t iClust=0; iClust<nk; iClust++){
181 (*fPyx)(iIn,iClust)=-log((*py)(iClust))+fBeta*Dist(vPart,TMatrixDColumn(*y1,iClust));
182 m[iClust]=(*fPyx)(iIn,iClust);
185 Double_t minPyx=TMath::MinElement(nk,m);
186 for (Int_t iClust=0; iClust<nk; iClust++){
187 (*fPyx)(iIn,iClust)-=minPyx;
188 (*fPyx)(iIn,iClust)=exp(-(*fPyx)(iIn,iClust));
189 pyxNorm+=(*fPyx)(iIn,iClust);
191 for (Int_t iClust=0; iClust<nk; iClust++) (*fPyx)(iIn,iClust)/=pyxNorm;
195 //recalculate codevectors
196 for (Int_t iClust=0; iClust<nk; iClust++){
197 Double_t xpos=0,ypos=0,pxy;
198 for (Int_t iIn=0; iIn<fNin; iIn++) (*p)(iClust)+=(*fPx)(iIn)*(*fPyx)(iIn,iClust);
199 for (Int_t iIn=0; iIn<fNin; iIn++){
200 pxy=(*fPx)(iIn)*(*fPyx)(iIn,iClust)/(*p)(iClust);
201 ypos+=pxy*TMath::Sin(fXPhi[iIn]);
202 xpos+=pxy*TMath::Cos(fXPhi[iIn]);
203 (*y)(0,iClust)+=pxy*fXEta[iIn];
205 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
207 //verify codevectors' stability
209 for (Int_t iClust=0; iClust<nk; iClust++){
210 chi1=TMath::CosH((*y1)(0,iClust)-(*y)(0,iClust))-TMath::Cos((*y1)(1,iClust)-(*y)(1,iClust));
211 chi1/=(2*TMath::CosH((*y1)(0,iClust))*TMath::CosH((*y)(0,iClust)));
213 if (chi1>chi) chi=chi1;
215 chi=TMath::Sqrt(chi);
216 for (Int_t iClust=0; iClust<nk; iClust++){
217 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)=(*y)(i,iClust);
218 (*py)(iClust)=(*p)(iClust);
220 if (nloop>fNloopMax){
221 if (chi<fEpsMax || nloop>500) break;
224 }while (chi>fEpsMax);
225 for (Int_t iClust=0; iClust<nk; iClust++){ //set codevectors and probability equal to those calculated
226 for (Int_t i=0; i<2; i++) (*fY)(i,iClust)=(*y)(i,iClust);
227 (*fPy)(iClust)=(*p)(iClust);
237 //-----------------------------------------------------------------------------------
238 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk)
240 static Bool_t growcl=true;
242 if (nk==2) growcl=true;
244 //verify if two codevectors are equal within fAvDist
245 Int_t *nSame = new Int_t[nk];
246 Int_t **iSame = new Int_t*[nk];
247 Int_t **cont = new Int_t*[nk];
248 for (Int_t iClust=0; iClust<nk; iClust++) cont[iClust]=new Int_t[nk],iSame[iClust]=new Int_t[nk];
249 for (Int_t iClust=0; iClust<nk; iClust++){
250 iSame[iClust][iClust]=1;
251 for (Int_t iClust1=iClust+1; iClust1<nk; iClust1++){
252 Double_t eta = (*fY)(0,iClust) ; Double_t phi = (*fY)(1,iClust);
253 Double_t eta1 = (*fY)(0,iClust1); Double_t phi1 = (*fY)(1,iClust1);
254 Double_t distCl=(TMath::CosH(eta-eta1)-TMath::Cos(phi-phi1))/(2*TMath::CosH(eta)*TMath::CosH(eta1));
255 if (distCl < fAvDist) iSame[iClust][iClust1]=iSame[iClust1][iClust]=1;
258 ReduceClusters(iSame,nk,nc,cont,nSame);
259 if (nc >= fNclustMax) growcl=false;
260 //recalculate the nc distinct codevectors
261 TMatrixD *pyx = new TMatrixD(fNin,2*nc);
262 TVectorD *py = new TVectorD(nk);
263 TMatrixD *y1 = new TMatrixD(3,nk);
264 for (Int_t iClust=0; iClust<nc; iClust++){
265 for(Int_t j=0; j<nSame[iClust]; j++){
266 Int_t iClust1 = cont[iClust][j];
267 for (Int_t iIn=0; iIn<fNin; iIn++) (*pyx)(iIn,iClust)+=(*fPyx)(iIn,iClust1);
268 (*py)(iClust)+=(*fPy)(iClust1);
269 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)+=(*fY)(i,iClust1);
271 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
274 for (Int_t iClust=0; iClust<nc; iClust++){
275 for (Int_t iIn=0; iIn<fNin; iIn++) (*fPyx)(iIn,iClust)=(*pyx)(iIn,iClust);
276 for (Int_t iComp=0; iComp<2; iComp++) (*fY)(iComp,iClust)=(*y1)(iComp,iClust);
277 (*fPy)(iClust)=(*py)(iClust);
280 if (growcl) DoubleClusters(nc,nk);
292 //-----------------------------------------------------------------------------------
293 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut)
295 Int_t *nSame = new Int_t[nc];
296 Int_t *iperm = new Int_t[nc];
297 Int_t *go = new Int_t[nc];
298 for (Int_t iCl=0; iCl<nc; iCl++){
300 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl];
304 TMath::Sort(nc,nSame,iperm,true);
306 for (Int_t iCl=0; iCl<nc; iCl++){
310 for (Int_t jCl=0; jCl<nc; jCl++){
311 if (iSame[k][jCl] == 1){
327 //-----------------------------------------------------------------------------------
328 void AliDAJetFinder::EndDetAnn(Int_t &nk,Int_t *xx,Double_t etx)
330 //now assign each particle to only one cluster
331 Double_t *clusters=new Double_t[nk];
332 for (Int_t iIn=0; iIn<fNin; iIn++){
333 for (Int_t iClust=0; iClust<nk; iClust++) clusters[iClust]=(*fPyx)(iIn,iClust);
334 xx[iIn]=TMath::LocMax(nk,clusters);
338 //recalculate codevectors, having all p(y|x)=0 or 1
342 for (Int_t iIn=0; iIn<fNin; iIn++){
343 Int_t iClust=xx[iIn];
344 (*fPyx)(iIn,iClust)=1;
345 (*fPy)(iClust)+=(*fPx)(iIn);
346 (*fY)(0,iClust)+=(*fPx)(iIn)*fXEta[iIn];
347 (*fY)(3,iClust)+=(*fPx)(iIn)*etx;
350 for (Int_t iClust=0; iClust<nk; iClust++){
351 if ((*fPy)(iClust)>0){
352 Double_t xpos=0,ypos=0,pxy;
353 for (Int_t iIn=0; iIn<fNin; iIn++){
354 pxy=(*fPx)(iIn)*(*fPyx)(iIn,iClust)/(*fPy)(iClust);
355 ypos+=pxy*TMath::Sin(fXPhi[iIn]);
356 xpos+=pxy*TMath::Cos(fXPhi[iIn]);
357 if (xx[iIn]==iClust) xx[iIn]=k;
359 (*fY)(0,k)=(*fY)(0,iClust)/(*fPy)(iClust);
360 (*fY)(1,k)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
361 (*fY)(3,k)=(*fY)(3,iClust);
368 //-----------------------------------------------------------------------------------
369 void AliDAJetFinder::StoreJets(Int_t nk,Int_t *xx)
371 //evaluate significant clusters properties
372 const Double_t pi=TMath::Pi();
373 Double_t dMeanDist=TMath::Sqrt(4*((AliDAJetHeader*)fHeader)->GetEtaCut()*pi/fNin);
374 Bool_t *isJet = new Bool_t[nk];
375 Double_t *etNoBg= new Double_t[nk];
376 Double_t *dDeltaEta=new Double_t[nk];
377 Double_t *dDeltaPhi=new Double_t[nk];
378 Double_t *surf = new Double_t[nk];
379 Double_t *etDens= new Double_t[nk];
380 for (Int_t iClust=0; iClust<nk; iClust++){ //clusters loop
382 Double_t dEtaMin=10.,dEtaMax=-10.,dPhiMin=10.,dPhiMax=0.;
383 for (Int_t iIn=0; iIn<fNin; iIn++){
384 if (xx[iIn]!=iClust) continue;
385 if (fXEta[iIn] < dEtaMin) dEtaMin=fXEta[iIn];
386 if (fXEta[iIn] > dEtaMax) dEtaMax=fXEta[iIn];
387 Double_t dPhi=fXPhi[iIn]-(*fY)(1,iClust);
388 if (dPhi > pi ) dPhi-=2*pi;
389 else if (dPhi < (-1)*pi) dPhi+=2*pi;
390 if (dPhi < dPhiMin) dPhiMin=dPhi;
391 else if (dPhi > dPhiMax) dPhiMax=dPhi;
393 dDeltaEta[iClust]=dEtaMax-dEtaMin+dMeanDist;
394 dDeltaPhi[iClust]=dPhiMax-dPhiMin+dMeanDist;
395 surf[iClust]=0.25*pi*dDeltaEta[iClust]*dDeltaPhi[iClust];
396 etDens[iClust]=(*fY)(3,iClust)/surf[iClust];
399 if (((AliDAJetHeader*)fHeader)->GetSelJets()){
400 for (Int_t iClust=0; iClust<nk; iClust++){
402 Double_t etDensMed=0.;
403 Double_t etDensSqr=0.;
405 for (Int_t iClust1=0; iClust1<nk; iClust1++){
407 etDensMed+=etDens[iClust1];
408 etDensSqr+=TMath::Power(etDens[iClust1],2);
412 etDensMed/=TMath::Max(norm,1);
413 etDensSqr/=TMath::Max(norm,1);
414 Double_t deltaEtDens=TMath::Sqrt(etDensSqr-TMath::Power(etDensMed,2));
415 if ((*fY)(3,iClust) > (etDensMed+deltaEtDens)*surf[iClust]) isJet[iClust]=kTRUE;
416 etNoBg[iClust]=(*fY)(3,iClust)-etDensMed*surf[iClust];
419 for (Int_t iClust=0; iClust<nk; iClust++){
421 Double_t etDensMed=0;
422 Double_t extSurf=4*((AliDAJetHeader*)fHeader)->GetEtaCut()*pi;
423 for (Int_t iClust1=0; iClust1<nk; iClust1++){
424 if (!isJet[iClust1]) etDensMed+=(*fY)(3,iClust1);
425 else extSurf-=surf[iClust1];
428 etNoBg[iClust]=(*fY)(3,iClust)-etDensMed*surf[iClust];
429 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
430 isJet[iClust]=kFALSE;
436 for (Int_t iClust=0; iClust<nk; iClust++) isJet[iClust]=true;
441 //now add selected jets to the list
442 Int_t *inJet = new Int_t[fNin];
444 Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
445 if (fromAod) refs = fReader->GetReferences();
446 for (Int_t iClust=0; iClust<nk; iClust++){ //clusters loop
447 if (isJet[iClust]){ //choose cluster
449 px = (*fY)(3,iClust)*TMath::Cos((*fY)(1,iClust));
450 py = (*fY)(3,iClust)*TMath::Sin((*fY)(1,iClust));
451 pz = (*fY)(3,iClust)/TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-(*fY)(0,iClust))));
452 en = TMath::Sqrt(px * px + py * py + pz * pz);
453 AliAODJet jet(px, py, pz, en);
455 for (Int_t iIn=0; iIn<fNin; iIn++) if (xx[iIn]==iClust) jet.AddTrack(refs->At(iIn));
457 printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iClust,jet.Eta(),jet.Phi(),jet.Pt());
460 delete [] dDeltaEta; delete [] dDeltaPhi;
466 //-----------------------------------------------------------------------------------
467 Double_t Dist(TVectorD x,TVectorD y)
470 const Double_t pi=TMath::Pi();
471 Double_t dphi=TMath::Abs(x(1)-y(1));
472 if (dphi > pi) dphi=2*pi-dphi;
473 Double_t dist=pow(x(0)-y(0),2)+pow(dphi,2);