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 "AliJetReaderHeader.h"
28 #include "AliJetReader.h"
29 #include "AliDAJetHeader.h"
30 #include "AliDAJetFinder.h"
32 ClassImp(AliDAJetFinder)
35 //-----------------------------------------------------------------------------------
36 AliDAJetFinder::AliDAJetFinder():
52 //-----------------------------------------------------------------------------------
53 AliDAJetFinder::~AliDAJetFinder()
58 //-----------------------------------------------------------------------------------
59 void AliDAJetFinder::FindJets()
61 // Find the jets in current event
63 Float_t betaStop=100.;
64 fDebug = fHeader->GetDebug();
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;
76 DoubleClusters(nc,nk,vPy,mY);
79 Annealing(nk,xData,vPx,vPy,mPyx,mY);
80 NumCl(nc,nk,vPy,mPyx,mY);
81 }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
83 Int_t *xx=new Int_t[fNeff];
84 EndDetAnn(nk,xData,xx,dEtSum,vPx,vPy,mPyx,mY);
85 StoreJets(nk,xData,xx,mY);
88 delete [] xData[0], delete [] xData[1];
96 //-----------------------------------------------------------------------------------
97 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
99 //Initialise the variables used by the algorithm
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();
108 for (Int_t iEn=0; iEn<nEntr; iEn++) if (fReader->GetCutFlag(iEn)==1) fNin++;
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);
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();
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);
134 for (iIn=0; iIn<fNeff; iIn++) (*vPx)(iIn)=(*vPx)(iIn)/dEtSum;
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();
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]);
149 (*mY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
152 //-----------------------------------------------------------------------------------
153 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk, TVectorD *vPy, TMatrixD *mY) const
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);
163 //-----------------------------------------------------------------------------------
164 void AliDAJetFinder::Annealing(Int_t nk,Double_t **xData, TVectorD *vPx, TVectorD *vPy, TMatrixD *mPyx, TMatrixD *mY)
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);
177 Double_t df[2]={fReader->GetReaderHeader()->GetFiducialEtaMax(),pi};
179 Double_t *m = new Double_t[nk];
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);
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];
197 //recalculate conditional probabilities
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);
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);
212 for (Int_t iClust=0; iClust<nk; iClust++) (*mPyx)(iIn,iClust)/=pyxNorm;
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];
226 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
228 //verify codevectors' stability
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)));
234 if (chi1>chi) chi=chi1;
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);
241 if (nloop>fNloopMax){
242 if (chi<fEpsMax || nloop>500) break;
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);
258 //-----------------------------------------------------------------------------------
259 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk,TVectorD *vPy, TMatrixD *mPyx,TMatrixD *mY)
261 static Bool_t growcl=true;
263 if (nk==2) growcl=true;
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;
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);
293 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
295 for (Int_t iClust=0; iClust<nk; iClust++) delete [] cont[iClust], delete [] iSame[iClust];
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);
306 if (growcl) DoubleClusters(nc,nk,vPy,mY);
315 //-----------------------------------------------------------------------------------
316 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut) const
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++){
323 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl], cont[iCl][jCl]=0;
327 TMath::Sort(nc,nSame,iperm,true);
329 for (Int_t iCl=0; iCl<nc; iCl++){
333 for (Int_t jCl=0; jCl<nc; jCl++){
334 if (iSame[k][jCl] == 1){
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)
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);
361 //recalculate codevectors, having all p(y|x)=0 or 1
362 Double_t *xEta = xData[0];
363 Double_t *xPhi = xData[1];
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;
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;
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);
393 //-----------------------------------------------------------------------------------
394 void AliDAJetFinder::StoreJets(Int_t nk, Double_t **xData, Int_t *xx, TMatrixD *mY)
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];
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++){
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;
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];
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.;
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);
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];
452 for (Int_t iClust=0; iClust<nk; 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];
461 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
462 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
463 isJet[iClust]=kFALSE;
469 for (Int_t iClust=0; iClust<nk; iClust++){
471 etNoBg[iClust]=(*mY)(3,iClust);
477 //now add selected jets to the list
478 Int_t *iSort = new Int_t[nk];
479 TMath::Sort(nk,etNoBg,iSort,true);
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
486 if (isJet[iCl]){ //choose cluster
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);
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));
503 if (fDebug > 0) printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iCl,jet.Eta(),jet.Phi(),jet.Pt());
506 delete [] dDeltaEta; delete [] dDeltaPhi;
512 //-----------------------------------------------------------------------------------
513 Double_t Dist(TVectorD x,TVectorD y)
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);