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.;
63 fDebug = fHeader->GetDebug();
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;
75 DoubleClusters(nc,nk,vPy,mY);
78 Annealing(nk,xData,vPx,vPy,mPyx,mY);
79 NumCl(nc,nk,vPy,mPyx,mY);
80 }while((fBeta<betaStop || nc<4) && nc<fNclustMax);
82 Int_t *xx=new Int_t[fNin];
83 EndDetAnn(nk,xData,xx,dEtSum,vPx,vPy,mPyx,mY);
84 StoreJets(nk,xData,xx,mY);
87 delete [] xData[0], delete [] xData[1];
95 //-----------------------------------------------------------------------------------
96 void AliDAJetFinder::InitDetAnn(Double_t &dEtSum,Double_t **xData,TVectorD *vPx,TVectorD *vPy,TMatrixD *mPyx,TMatrixD *mY)
98 //Initialise the variables used by the algorithm
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();
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;
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();
121 for (iIn=0; iIn<fNin; iIn++) (*vPx)(iIn)=(*vPx)(iIn)/dEtSum;
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();
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]);
136 (*mY)(1,0)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*TMath::Pi();
139 //-----------------------------------------------------------------------------------
140 void AliDAJetFinder::DoubleClusters(Int_t nc,Int_t &nk, TVectorD *vPy, TMatrixD *mY) const
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);
150 //-----------------------------------------------------------------------------------
151 void AliDAJetFinder::Annealing(Int_t nk,Double_t **xData, TVectorD *vPx, TVectorD *vPy, TMatrixD *mPyx, TMatrixD *mY)
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);
164 Double_t df[2]={fReader->GetReaderHeader()->GetFiducialEtaMax(),pi};
166 Double_t *m = new Double_t[nk];
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);
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];
184 //recalculate conditional probabilities
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);
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);
199 for (Int_t iClust=0; iClust<nk; iClust++) (*mPyx)(iIn,iClust)/=pyxNorm;
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];
213 (*y)(1,iClust)=(atan2(ypos,xpos)>0) ? atan2(ypos,xpos) : atan2(ypos,xpos)+2*pi;
215 //verify codevectors' stability
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)));
221 if (chi1>chi) chi=chi1;
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);
228 if (nloop>fNloopMax){
229 if (chi<fEpsMax || nloop>500) break;
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);
245 //-----------------------------------------------------------------------------------
246 void AliDAJetFinder::NumCl(Int_t &nc,Int_t &nk,TVectorD *vPy, TMatrixD *mPyx,TMatrixD *mY)
248 static Bool_t growcl=true;
250 if (nk==2) growcl=true;
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;
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);
280 for (Int_t i=0; i<2; i++) (*y1)(i,iClust)/=nSame[iClust];
282 for (Int_t iClust=0; iClust<nk; iClust++) delete [] cont[iClust], delete [] iSame[iClust];
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);
293 if (growcl) DoubleClusters(nc,nk,vPy,mY);
302 //-----------------------------------------------------------------------------------
303 void AliDAJetFinder::ReduceClusters(Int_t **iSame,Int_t nc,Int_t &ncout,Int_t **cont,Int_t *nSameOut) const
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++){
310 for (Int_t jCl=0; jCl<nc; jCl++) nSame[iCl]+=iSame[iCl][jCl], cont[iCl][jCl]=0;
314 TMath::Sort(nc,nSame,iperm,true);
316 for (Int_t iCl=0; iCl<nc; iCl++){
320 for (Int_t jCl=0; jCl<nc; jCl++){
321 if (iSame[k][jCl] == 1){
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)
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);
349 //recalculate codevectors, having all p(y|x)=0 or 1
350 Double_t *xEta = xData[0];
351 Double_t *xPhi = xData[1];
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;
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;
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);
381 //-----------------------------------------------------------------------------------
382 void AliDAJetFinder::StoreJets(Int_t nk,Double_t **xData, Int_t *xx, TMatrixD *mY)
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
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;
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];
416 if (((AliDAJetHeader*)fHeader)->GetSelJets()){
417 for (Int_t iClust=0; iClust<nk; iClust++){
419 Double_t etDensMed=0.;
420 Double_t etDensSqr=0.;
422 for (Int_t iClust1=0; iClust1<nk; iClust1++){
424 etDensMed+=etDens[iClust1];
425 etDensSqr+=TMath::Power(etDens[iClust1],2);
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];
436 for (Int_t iClust=0; iClust<nk; 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];
445 etNoBg[iClust]=(*mY)(3,iClust)-etDensMed*surf[iClust];
446 if (etNoBg[iClust]<((AliDAJetHeader*)fHeader)->GetEtMin()){
447 isJet[iClust]=kFALSE;
453 for (Int_t iClust=0; iClust<nk; iClust++) isJet[iClust]=true;
458 //now add selected jets to the list
459 Int_t *iSort = new Int_t[nk];
460 TMath::Sort(nk,etNoBg,iSort,true);
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
467 if (isJet[iCl]){ //choose cluster
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);
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));
484 if (fDebug > 0) printf("jet %d, Eta: %f, Phi: %f, Et: %f\n",iCl,jet.Eta(),jet.Phi(),jet.Pt());
487 delete [] dDeltaEta; delete [] dDeltaPhi;
493 //-----------------------------------------------------------------------------------
494 Double_t Dist(TVectorD x,TVectorD y)
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);