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():
51 //-----------------------------------------------------------------------------------
52 AliDAJetFinder::~AliDAJetFinder()
57 //-----------------------------------------------------------------------------------
58 void AliDAJetFinder::FindJets()
60 // Find the jets in current event
62 Float_t betaStop=100.;
66 TVectorD *vPx = new TVectorD();
67 TVectorD *vPy = new TVectorD();
68 TMatrixD *mPyx= new TMatrixD();
69 TMatrixD *mY = new TMatrixD();
70 InitDetAnn(dEtSum,xData,vPx,vPy,mPyx,mY);
74 DoubleClusters(nc,nk,vPy,mY);
77 Annealing(nk,xData,vPx,vPy,mPyx,mY);
78 NumCl(nc,nk,vPy,mPyx,mY);
79 }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
81 Int_t *xx=new Int_t[fNin];
82 EndDetAnn(nk,xData,xx,dEtSum,vPx,vPy,mPyx,mY);
83 StoreJets(nk,xData,xx,mY);
86 delete [] xData[0], delete [] xData[1];
94 //-----------------------------------------------------------------------------------
95 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
97 //Initialise the variables used by the algorithm
99 fNclustMax= ((AliDAJetHeader*)fHeader)->GetFixedCl() ?
100 ((AliDAJetHeader*)fHeader)->GetNclustMax() :
101 TMath::Max((Int_t)TMath::Sqrt(fNin),5);
102 TClonesArray *lvArray = fReader->GetMomentumArray();
103 Int_t nEntr = lvArray->GetEntries();
105 for (Int_t iEn=0; iEn<nEntr; iEn++) if (fReader->GetCutFlag(iEn)==1) fNin++;
106 Double_t *xEta = new Double_t[fNin];
107 Double_t *xPhi = new Double_t[fNin];
108 xData[0]=xEta; xData[1]=xPhi;
111 for (Int_t iEn=0; iEn<nEntr; iEn++){
112 if (fReader->GetCutFlag(iEn)==0) continue;
113 TLorentzVector *lv=(TLorentzVector*)lvArray->At(iEn);
114 xEta[iIn] = lv->Eta();
115 xPhi[iIn] = lv->Phi()<0 ? lv->Phi() + 2*TMath::Pi() : lv->Phi();
116 (*vPx)(iIn)=lv->Pt();
120 for (Int_t iIn=0; iIn<fNin; iIn++) (*vPx)(iIn)=(*vPx)(iIn)/dEtSum;
122 Int_t njdim=2*fNclustMax+1;
123 mPyx->ResizeTo(fNin,njdim);
124 mY->ResizeTo(4,njdim);
125 vPy->ResizeTo(njdim);
126 mY->Zero();mPyx->Zero();vPy->Zero();
128 TMatrixDColumn(*mPyx,0)=1;
129 Double_t ypos=0,xpos=0;
130 for (Int_t iIn=0; iIn<fNin; iIn++){
131 (*mY)(0,0)+=(*vPx)(iIn)*xEta[iIn];
132 ypos+=(*vPx)(iIn)*TMath::Sin(xPhi[iIn]);
133 xpos+=(*vPx)(iIn)*TMath::Cos(xPhi[iIn]);
135 (*mY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
138 //-----------------------------------------------------------------------------------
139 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk,TVectorD *vPy,TMatrixD *mY)
141 for(Int_t iClust=0; iClust<nc; iClust++){
142 (*vPy)(iClust)=(*vPy)(iClust)/2;
143 (*vPy)(nc+iClust)=(*vPy)(iClust);
144 for(Int_t iComp=0; iComp<3; iComp++) (*mY)(iComp,nc+iClust)=(*mY)(iComp,iClust);
149 //-----------------------------------------------------------------------------------
150 void AliDAJetFinder::Annealing(Int_t nk,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
152 // Main part of the algorithm
153 const Double_t pi=TMath::Pi();
154 TVectorD *py = new TVectorD(nk);
155 TVectorD *p = new TVectorD(nk);
156 TMatrixD *y = new TMatrixD(4,nk);
157 TMatrixD *y1 = new TMatrixD(4,nk);
158 TMatrixD *ry = new TMatrixD(2,nk);
159 Double_t *xEta = xData[0];
160 Double_t *xPhi = xData[1];
161 Double_t Dist(TVectorD,TVectorD);
163 Double_t df[2]={fReader->GetReaderHeader()->GetFiducialEtaMax(),pi};
165 Double_t *m = new Double_t[nk];
169 for (Int_t iClust=0; iClust<nk; iClust++){
170 for (Int_t i=0; i<3; i++)(*y1)(i,iClust)=(*mY)(i,iClust);
171 (*py)(iClust)=(*vPy)(iClust);
173 //perturbation of codevectors
174 Double_t seed=1000000*gRandom->Rndm(24);
175 ry->Randomize(-0.5,0.5,seed);
176 for (Int_t i=0; i<2; i++){
177 for (Int_t iClust=0; iClust<nk/2; iClust++)
178 (*y1)(i,iClust)+=((*ry)(i,iClust)+TMath::Sign(0.5,(*ry)(i,iClust)))*fDelta*df[i];
179 for (Int_t iClust=nk/2; iClust<nk; iClust++)
180 (*y1)(i,iClust)-=((*ry)(i,iClust-nk/2)+TMath::Sign(0.5,(*ry)(i,iClust-nk/2)))*fDelta*df[i];
183 //recalculate conditional probabilities
185 for (Int_t iIn=0; iIn<fNin; iIn++){
186 vPart(0)=xEta[iIn]; vPart(1)=xPhi[iIn];
187 for(Int_t iClust=0; iClust<nk; iClust++){
188 (*mPyx)(iIn,iClust)=-log((*py)(iClust))+fBeta*Dist(vPart,TMatrixDColumn(*y1,iClust));
189 m[iClust]=(*mPyx)(iIn,iClust);
192 Double_t minPyx=TMath::MinElement(nk,m);
193 for (Int_t iClust=0; iClust<nk; iClust++){
194 (*mPyx)(iIn,iClust)-=minPyx;
195 (*mPyx)(iIn,iClust)=exp(-(*mPyx)(iIn,iClust));
196 pyxNorm+=(*mPyx)(iIn,iClust);
198 for (Int_t iClust=0; iClust<nk; iClust++) (*mPyx)(iIn,iClust)/=pyxNorm;
202 //recalculate codevectors
203 for (Int_t iClust=0; iClust<nk; iClust++){
204 Double_t xpos=0,ypos=0,pxy;
205 for (Int_t iIn=0; iIn<fNin; iIn++) (*p)(iClust)+=(*vPx)(iIn)*(*mPyx)(iIn,iClust);
206 for (Int_t iIn=0; iIn<fNin; iIn++){
207 pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*p)(iClust);
208 ypos+=pxy*TMath::Sin(xPhi[iIn]);
209 xpos+=pxy*TMath::Cos(xPhi[iIn]);
210 (*y)(0,iClust)+=pxy*xEta[iIn];
212 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
214 //verify codevectors' stability
216 for (Int_t iClust=0; iClust<nk; iClust++){
217 chi1=TMath::CosH((*y1)(0,iClust)-(*y)(0,iClust))-TMath::Cos((*y1)(1,iClust)-(*y)(1,iClust));
218 chi1/=(2*TMath::CosH((*y1)(0,iClust))*TMath::CosH((*y)(0,iClust)));
220 if (chi1>chi) chi=chi1;
222 chi=TMath::Sqrt(chi);
223 for (Int_t iClust=0; iClust<nk; iClust++){
224 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)=(*y)(i,iClust);
225 (*py)(iClust)=(*p)(iClust);
227 if (nloop>fNloopMax){
228 if (chi<fEpsMax || nloop>500) break;
231 }while (chi>fEpsMax);
232 for (Int_t iClust=0; iClust<nk; iClust++){ //set codevectors and probability equal to those calculated
233 for (Int_t i=0; i<2; i++) (*mY)(i,iClust)=(*y)(i,iClust);
234 (*vPy)(iClust)=(*p)(iClust);
244 //-----------------------------------------------------------------------------------
245 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
247 static Bool_t growcl=true;
249 if (nk==2) growcl=true;
251 //verify if two codevectors are equal within fAvDist
252 Int_t *nSame = new Int_t[nk];
253 Int_t **iSame = new Int_t*[nk];
254 Int_t **cont = new Int_t*[nk];
255 for (Int_t iClust=0; iClust<nk; iClust++) cont[iClust]=new Int_t[nk],iSame[iClust]=new Int_t[nk];
256 for (Int_t iClust=0; iClust<nk; iClust++){
257 iSame[iClust][iClust]=1;
258 for (Int_t iClust1=iClust+1; iClust1<nk; iClust1++){
259 Double_t eta = (*mY)(0,iClust) ; Double_t phi = (*mY)(1,iClust);
260 Double_t eta1 = (*mY)(0,iClust1); Double_t phi1 = (*mY)(1,iClust1);
261 Double_t distCl=(TMath::CosH(eta-eta1)-TMath::Cos(phi-phi1))/(2*TMath::CosH(eta)*TMath::CosH(eta1));
262 if (distCl < fAvDist) iSame[iClust][iClust1]=iSame[iClust1][iClust]=1;
263 else iSame[iClust][iClust1]=iSame[iClust1][iClust]=0;
266 ReduceClusters(iSame,nk,nc,cont,nSame);
267 if (nc >= fNclustMax) growcl=false;
268 //recalculate the nc distinct codevectors
269 TMatrixD *pyx = new TMatrixD(fNin,2*nc);
270 TVectorD *py = new TVectorD(nk);
271 TMatrixD *y1 = new TMatrixD(3,nk);
272 for (Int_t iClust=0; iClust<nc; iClust++){
273 for(Int_t j=0; j<nSame[iClust]; j++){
274 Int_t iClust1 = cont[iClust][j];
275 for (Int_t iIn=0; iIn<fNin; iIn++) (*pyx)(iIn,iClust)+=(*mPyx)(iIn,iClust1);
276 (*py)(iClust)+=(*vPy)(iClust1);
277 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)+=(*mY)(i,iClust1);
279 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
281 for (Int_t iClust=0; iClust<nk; iClust++) delete [] cont[iClust], delete [] iSame[iClust];
286 for (Int_t iClust=0; iClust<nc; iClust++){
287 for (Int_t iIn=0; iIn<fNin; iIn++) (*mPyx)(iIn,iClust)=(*pyx)(iIn,iClust);
288 for (Int_t iComp=0; iComp<2; iComp++) (*mY)(iComp,iClust)=(*y1)(iComp,iClust);
289 (*vPy)(iClust)=(*py)(iClust);
292 if (growcl) DoubleClusters(nc,nk,vPy,mY);
301 //-----------------------------------------------------------------------------------
302 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut)
304 Int_t *nSame = new Int_t[nc];
305 Int_t *iperm = new Int_t[nc];
306 Int_t *go = new Int_t[nc];
307 for (Int_t iCl=0; iCl<nc; iCl++){
309 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl], cont[iCl][jCl]=0;
313 TMath::Sort(nc,nSame,iperm,true);
315 for (Int_t iCl=0; iCl<nc; iCl++){
319 for (Int_t jCl=0; jCl<nc; jCl++){
320 if (iSame[k][jCl] == 1){
336 //-----------------------------------------------------------------------------------
337 void AliDAJetFinder::EndDetAnn(Int_t &nk,Double_t **xData,Int_t *xx,Double_t etx,
338 TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
340 //now assign each particle to only one cluster
341 Double_t *clusters=new Double_t[nk];
342 for (Int_t iIn=0; iIn<fNin; iIn++){
343 for (Int_t iClust=0; iClust<nk; iClust++) clusters[iClust]=(*mPyx)(iIn,iClust);
344 xx[iIn]=TMath::LocMax(nk,clusters);
348 //recalculate codevectors, having all p(y|x)=0 or 1
349 Double_t *xEta = xData[0];
350 Double_t *xPhi = xData[1];
354 for (Int_t iIn=0; iIn<fNin; iIn++){
355 Int_t iClust=xx[iIn];
356 (*mPyx)(iIn,iClust)=1;
357 (*vPy)(iClust)+=(*vPx)(iIn);
358 (*mY)(0,iClust)+=(*vPx)(iIn)*xEta[iIn];
359 (*mY)(3,iClust)+=(*vPx)(iIn)*etx;
362 for (Int_t iClust=0; iClust<nk; iClust++){
363 if ((*vPy)(iClust)>0){
364 Double_t xpos=0,ypos=0,pxy;
365 for (Int_t iIn=0; iIn<fNin; iIn++){
366 pxy=(*vPx)(iIn)*(*mPyx)(iIn,iClust)/(*vPy)(iClust);
367 ypos+=pxy*TMath::Sin(xPhi[iIn]);
368 xpos+=pxy*TMath::Cos(xPhi[iIn]);
369 if (xx[iIn]==iClust) xx[iIn]=k;
371 (*mY)(0,k)=(*mY)(0,iClust)/(*vPy)(iClust);
372 (*mY)(1,k)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
373 (*mY)(3,k)=(*mY)(3,iClust);
380 //-----------------------------------------------------------------------------------
381 void AliDAJetFinder::StoreJets(Int_t nk,Double_t **xData,Int_t *xx,TMatrixD *mY)
383 //evaluate significant clusters properties
384 const Double_t pi=TMath::Pi();
385 AliJetReaderHeader *rHeader=fReader->GetReaderHeader();
386 Float_t dFiducialEta=rHeader->GetFiducialEtaMax()-rHeader->GetFiducialEtaMin();
387 Double_t dMeanDist=TMath::Sqrt(2*dFiducialEta*pi/fNin);
388 Double_t *xEta = xData[0];
389 Double_t *xPhi = xData[1];
390 Bool_t *isJet = new Bool_t[nk];
391 Double_t *etNoBg= new Double_t[nk];
392 Double_t *dDeltaEta=new Double_t[nk];
393 Double_t *dDeltaPhi=new Double_t[nk];
394 Double_t *surf = new Double_t[nk];
395 Double_t *etDens= new Double_t[nk];
396 for (Int_t iClust=0; iClust<nk; iClust++){ //clusters loop
398 Double_t dEtaMin=10.,dEtaMax=-10.,dPhiMin=10.,dPhiMax=0.;
399 for (Int_t iIn=0; iIn<fNin; iIn++){
400 if (xx[iIn]!=iClust) continue;
401 if (xEta[iIn] < dEtaMin) dEtaMin=xEta[iIn];
402 if (xEta[iIn] > dEtaMax) dEtaMax=xEta[iIn];
403 Double_t dPhi=xPhi[iIn]-(*mY)(1,iClust);
404 if (dPhi > pi ) dPhi-=2*pi;
405 else if (dPhi < (-1)*pi) dPhi+=2*pi;
406 if (dPhi < dPhiMin) dPhiMin=dPhi;
407 else if (dPhi > dPhiMax) dPhiMax=dPhi;
409 dDeltaEta[iClust]=dEtaMax-dEtaMin+dMeanDist;
410 dDeltaPhi[iClust]=dPhiMax-dPhiMin+dMeanDist;
411 surf[iClust]=0.25*pi*dDeltaEta[iClust]*dDeltaPhi[iClust];
412 etDens[iClust]=(*mY)(3,iClust)/surf[iClust];
415 if (((AliDAJetHeader*)fHeader)->GetSelJets()){
416 for (Int_t iClust=0; iClust<nk; iClust++){
418 Double_t etDensMed=0.;
419 Double_t etDensSqr=0.;
421 for (Int_t iClust1=0; iClust1<nk; iClust1++){
423 etDensMed+=etDens[iClust1];
424 etDensSqr+=TMath::Power(etDens[iClust1],2);
428 etDensMed/=TMath::Max(norm,1);
429 etDensSqr/=TMath::Max(norm,1);
430 Double_t deltaEtDens=TMath::Sqrt(etDensSqr-TMath::Power(etDensMed,2));
431 if ((*mY)(3,iClust) > (etDensMed+deltaEtDens)*surf[iClust]) isJet[iClust]=kTRUE;
432 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
435 for (Int_t iClust=0; iClust<nk; iClust++){
437 Double_t etDensMed=0;
438 Double_t extSurf=2*dFiducialEta*pi;
439 for (Int_t iClust1=0; iClust1<nk; iClust1++){
440 if (!isJet[iClust1]) etDensMed+=(*mY)(3,iClust1);
441 else extSurf-=surf[iClust1];
444 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
445 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
446 isJet[iClust]=kFALSE;
452 for (Int_t iClust=0; iClust<nk; iClust++) isJet[iClust]=true;
457 //now add selected jets to the list
458 Int_t *inJet = new Int_t[fNin];
460 Bool_t fromAod = !strcmp(fReader->ClassName(),"AliJetAODReader");
461 if (fromAod) refs = fReader->GetReferences();
462 for (Int_t iClust=0; iClust<nk; iClust++){ //clusters loop
463 if (isJet[iClust]){ //choose cluster
465 px = (*mY)(3,iClust)*TMath::Cos((*mY)(1,iClust));
466 py = (*mY)(3,iClust)*TMath::Sin((*mY)(1,iClust));
467 pz = (*mY)(3,iClust)/TMath::Tan(2.0 * TMath::ATan(TMath::Exp(-(*mY)(0,iClust))));
468 en = TMath::Sqrt(px * px + py * py + pz * pz);
469 AliAODJet jet(px, py, pz, en);
472 Int_t nEntr = fReader->GetMomentumArray()->GetEntries();
473 for (Int_t iEn=0; iEn<nEntr; iEn++){
474 if (fReader->GetCutFlag(iEn)==0) continue;
475 if (xx[iIn]==iClust) jet.AddTrack(refs->At(iEn));
480 printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iClust,jet.Eta(),jet.Phi(),jet.Pt());
483 delete [] dDeltaEta; delete [] dDeltaPhi;
489 //-----------------------------------------------------------------------------------
490 Double_t Dist(TVectorD x,TVectorD y)
493 const Double_t pi=TMath::Pi();
494 Double_t dphi=TMath::Abs(x(1)-y(1));
495 if (dphi > pi) dphi=2*pi-dphi;
496 Double_t dist=pow(x(0)-y(0),2)+pow(dphi,2);