3 // Emcal jet finder task.
5 // Authors: C.Loizides, S.Aiola
8 #include "AliEmcalJetTask.h"
11 #include <TClonesArray.h>
13 #include <TLorentzVector.h>
17 #include "AliAnalysisManager.h"
18 #include "AliCentrality.h"
19 #include "AliEMCALGeometry.h"
20 #include "AliESDEvent.h"
21 #include "AliEmcalJet.h"
22 #include "AliEmcalParticle.h"
23 #include "AliFJWrapper.h"
24 #include "AliMCEvent.h"
25 #include "AliVCluster.h"
26 #include "AliVEvent.h"
27 #include "AliVParticle.h"
32 ClassImp(AliEmcalJetTask)
34 //________________________________________________________________________
35 AliEmcalJetTask::AliEmcalJetTask() :
36 AliAnalysisTaskSE("AliEmcalJetTask"),
37 fTracksName("Tracks"),
38 fCaloName("CaloClusters"),
48 fPhiMax(TMath::TwoPi()),
59 fRecombScheme(fastjet::pt_scheme),
72 // Default constructor.
75 //________________________________________________________________________
76 AliEmcalJetTask::AliEmcalJetTask(const char *name) :
77 AliAnalysisTaskSE(name),
78 fTracksName("Tracks"),
79 fCaloName("CaloClusters"),
81 fJetType(kAKT|kFullJet|kRX1Jet),
89 fPhiMax(TMath::TwoPi()),
100 fRecombScheme(fastjet::pt_scheme),
101 fTrackEfficiency(1.),
113 // Standard constructor.
115 fBranchNames="ESD:AliESDRun.,AliESDHeader.,PrimaryVertex.";
118 //________________________________________________________________________
119 AliEmcalJetTask::~AliEmcalJetTask()
124 //________________________________________________________________________
125 void AliEmcalJetTask::UserCreateOutputObjects()
127 // Create user objects.
129 fJets = new TClonesArray("AliEmcalJet");
130 fJets->SetName(fJetsName);
133 //________________________________________________________________________
134 void AliEmcalJetTask::UserExec(Option_t *)
136 // Main loop, called for each event.
143 // clear the jet array (normally a null operation)
149 //________________________________________________________________________
150 void AliEmcalJetTask::Terminate(Option_t *)
152 // Called once at the end of the analysis.
155 //________________________________________________________________________
156 void AliEmcalJetTask::FindJets()
159 if (!fTracks && !fClus){
160 cout << "WARNING NO TRACKS OR CLUSTERS:" <<endl;
165 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
166 if ((fJetType & kAKT) != 0) {
168 jalgo = fastjet::antikt_algorithm;
169 AliDebug(1,"Using AKT algorithm");
172 AliDebug(1,"Using KT algorithm");
175 if ((fJetType & kR020Jet) != 0)
177 else if ((fJetType & kR030Jet) != 0)
179 else if ((fJetType & kR040Jet) != 0)
183 AliFJWrapper fjw(name, name);
184 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
185 fjw.SetGhostArea(fGhostArea);
187 fjw.SetAlgorithm(jalgo);
188 fjw.SetRecombScheme(static_cast<fastjet::RecombinationScheme>(fRecombScheme));
189 fjw.SetMaxRap(fEtaMax);
192 // get primary vertex
193 Double_t vertex[3] = {0, 0, 0};
194 if(fEvent->GetPrimaryVertex()) fEvent->GetPrimaryVertex()->GetXYZ(vertex);
196 AliDebug(2,Form("Jet type = %d", fJetType));
198 if ((fIsMcPart || ((fJetType & kFullJet) != 0) || ((fJetType & kChargedJet) != 0)) && fTracks) {
199 const Int_t Ntracks = fTracks->GetEntries();
200 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
201 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(iTracks));
205 if (((fJetType & kChargedJet) != 0) && (t->Charge() == 0)) {
206 AliDebug(2,Form("Skipping track %d because it is neutral.", iTracks));
209 if (((fJetType & kNeutralJet) != 0) && (t->Charge() != 0)) {
210 AliDebug(2,Form("Skipping track %d because it is charged.", iTracks));
214 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) {
215 if (t->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
216 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
220 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
224 if (t->TestBits(fConstSel) != (Int_t)fConstSel) {
225 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
229 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
232 if (t->Pt() < fMinJetTrackPt)
234 Double_t eta = t->Eta();
235 Double_t phi = t->Phi();
236 if ((eta<fEtaMin) || (eta>fEtaMax) ||
237 (phi<fPhiMin) || (phi>fPhiMax))
240 // artificial inefficiency
241 if (fTrackEfficiency < 1.) {
242 Double_t rnd = gRandom->Rndm();
243 if (fTrackEfficiency < rnd) {
244 AliDebug(2,Form("Track %d rejected due to artificial tracking inefficiency", iTracks));
249 // offset of 100 for consistency with cluster ids
250 AliDebug(2,Form("Track %d accepted (label = %d, pt = %f)", iTracks, t->GetLabel(), t->Pt()));
251 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->E(), iTracks + 100);
255 if ((((fJetType & kFullJet) != 0) || ((fJetType & kNeutralJet) != 0)) && fClus) {
256 const Int_t Nclus = fClus->GetEntries();
257 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
259 Double_t cEta=0,cPhi=0,cPt=0;
260 Double_t cPx=0,cPy=0,cPz=0;
262 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(iClus));
266 c = ep->GetCluster();
270 if (c->GetLabel() > fMinMCLabel) {
271 if (ep->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
272 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
276 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
280 if (ep->TestBits(fConstSel) != (Int_t)fConstSel) {
281 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
285 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
296 c = static_cast<AliVCluster*>(fClus->At(iClus));
300 if (c->GetLabel() > fMinMCLabel) {
301 if (c->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
302 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
306 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
310 if (c->TestBits(fConstSel) != (Int_t)fConstSel) {
311 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
315 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
320 c->GetMomentum(nP, vertex);
330 if (cPt < fMinJetClusPt)
332 if ((cEta<fEtaMin) || (cEta>fEtaMax) ||
333 (cPhi<fPhiMin) || (cPhi>fPhiMax))
335 // offset of 100 to skip ghost particles uid = -1
336 AliDebug(2,Form("Cluster %d accepted (label = %d)", iClus, c->GetLabel()));
337 fjw.AddInputVector(cPx, cPy, cPz, TMath::Sqrt(cPx*cPx+cPy*cPy+cPz*cPz), -iClus - 100);
341 // setting legacy mode
343 fjw.SetLegacyMode(kTRUE);
350 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
352 AliFatal(Form("%s: Can not create geometry", GetName()));
356 // loop over fastjet jets
357 std::vector<fastjet::PseudoJet> jets_incl = fjw.GetInclusiveJets();
358 // sort jets according to jet pt
359 static Int_t indexes[9999] = {-1};
360 GetSortedArray(indexes, jets_incl);
362 AliDebug(1,Form("%d jets found", (Int_t)jets_incl.size()));
363 for (UInt_t ijet=0, jetCount=0; ijet<jets_incl.size(); ++ijet) {
364 Int_t ij = indexes[ijet];
365 AliDebug(3,Form("Jet pt = %f, area = %f", jets_incl[ij].perp(), fjw.GetJetArea(ij)));
367 if (jets_incl[ij].perp()<fMinJetPt)
369 if (fjw.GetJetArea(ij)<fMinJetArea)
371 if ((jets_incl[ij].eta()<fJetEtaMin) || (jets_incl[ij].eta()>fJetEtaMax) ||
372 (jets_incl[ij].phi()<fJetPhiMin) || (jets_incl[ij].phi()>fJetPhiMax))
375 AliEmcalJet *jet = new ((*fJets)[jetCount])
376 AliEmcalJet(jets_incl[ij].perp(), jets_incl[ij].eta(), jets_incl[ij].phi(), jets_incl[ij].m());
378 // loop over constituents
379 std::vector<fastjet::PseudoJet> constituents(fjw.GetJetConstituents(ij));
380 jet->SetNumberOfTracks(constituents.size());
381 jet->SetNumberOfClusters(constituents.size());
385 Double_t neutralE = 0;
396 for(UInt_t ic = 0; ic < constituents.size(); ++ic) {
397 Int_t uid = constituents[ic].user_index();
398 AliDebug(3,Form("Processing constituent %d", uid));
399 if ((uid == -1) /*&& (constituents[ic].kt2() < 1e-25)*/) { //ghost particle
401 Double_t gphi = constituents[ic].phi();
403 gphi += TMath::TwoPi();
404 gphi *= TMath::RadToDeg();
405 Double_t geta = constituents[ic].eta();
406 if ((gphi > geom->GetArm1PhiMin()) && (gphi < geom->GetArm1PhiMax()) &&
407 (geta > geom->GetArm1EtaMin()) && (geta < geom->GetArm1EtaMax()))
409 } else if ((uid > 0) && fTracks) { // track constituent
410 Int_t tid = uid - 100;
411 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(tid));
413 AliError(Form("Could not find track %d",tid));
416 if (jetCount < fMarkConst) {
417 AliDebug(2,Form("Marking track %d with bit map %d", tid, fJetType));
420 Double_t cEta = t->Eta();
421 Double_t cPhi = t->Phi();
422 Double_t cPt = t->Pt();
423 Double_t cP = t->P();
424 if (t->Charge() == 0) {
434 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) // check if MC particle
438 cPhi += TMath::TwoPi();
439 cPhi *= TMath::RadToDeg();
440 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
441 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
446 jet->AddTrackAt(tid, nt);
448 } else if (fClus) { // cluster constituent
449 Int_t cid = -uid - 100;
451 Double_t cEta=0,cPhi=0,cPt=0,cP=0;
453 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(cid));
456 c = ep->GetCluster();
459 if (jetCount < fMarkConst)
460 ep->SetBit(fJetType);
466 c = static_cast<AliVCluster*>(fClus->At(cid));
469 if (jetCount < fMarkConst)
472 c->GetMomentum(nP, vertex);
483 if (c->GetLabel() > fMinMCLabel) // MC particle
484 mcpt += c->GetMCEnergyFraction() > 1e-6 ? cPt * c->GetMCEnergyFraction() : cPt;
487 cPhi += TMath::TwoPi();
488 cPhi *= TMath::RadToDeg();
489 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
490 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
495 jet->AddClusterAt(cid, nc);
499 AliError(Form("%s: No logical way to end up here.", GetName()));
502 } /* end of constituent loop */
504 jet->SetNumberOfTracks(nt);
505 jet->SetNumberOfClusters(nc);
506 jet->SortConstituents();
507 jet->SetMaxNeutralPt(maxNe);
508 jet->SetMaxChargedPt(maxCh);
509 jet->SetNEF(neutralE / jet->E());
510 fastjet::PseudoJet area(fjw.GetJetAreaVector(ij));
511 jet->SetArea(area.perp());
512 jet->SetAreaEta(area.eta());
513 jet->SetAreaPhi(area.phi());
514 jet->SetNumberOfCharged(ncharged);
515 jet->SetNumberOfNeutrals(nneutral);
518 jet->SetPtEmc(emcpt);
521 jet->SetAreaEmc(fjw.GetJetArea(ij) * gemc / gall);
524 if ((jet->Phi() > geom->GetArm1PhiMin() * TMath::DegToRad()) &&
525 (jet->Phi() < geom->GetArm1PhiMax() * TMath::DegToRad()) &&
526 (jet->Eta() > geom->GetArm1EtaMin()) &&
527 (jet->Eta() < geom->GetArm1EtaMax()))
528 jet->SetAxisInEmcal(kTRUE);
530 AliDebug(2,Form("Added jet n. %d, pt = %f, area = %f, constituents = %d", jetCount, jet->Pt(), jet->Area(), (Int_t)constituents.size()));
536 //________________________________________________________________________
537 Bool_t AliEmcalJetTask::GetSortedArray(Int_t indexes[], std::vector<fastjet::PseudoJet> array) const
539 // Get the leading jets.
541 static Float_t pt[9999] = {0};
543 const Int_t n = (Int_t)array.size();
548 for (Int_t i = 0; i < n; i++)
549 pt[i] = array[i].perp();
551 TMath::Sort(n, pt, indexes);
556 //________________________________________________________________________
557 Bool_t AliEmcalJetTask::DoInit()
559 // Init. Return true if successful.
561 if (fTrackEfficiency < 1.) {
562 if (gRandom) delete gRandom;
563 gRandom = new TRandom3(0);
566 // get input collections
567 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
570 fEvent = InputEvent();
572 AliError(Form("%s: Could not retrieve event! Returning", GetName()));
576 // add jets to event if not yet there
577 if (!(fEvent->FindListObject(fJetsName)))
578 fEvent->AddObject(fJets);
580 AliError(Form("%s: Object with name %s already in event! Returning", GetName(), fJetsName.Data()));
584 if (fTracksName == "Tracks")
585 am->LoadBranch("Tracks");
586 if (!fTracks && !fTracksName.IsNull()) {
587 fTracks = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fTracksName));
589 AliError(Form("%s: Pointer to tracks %s == 0", GetName(), fTracksName.Data()));
594 TClass cls(fTracks->GetClass()->GetName());
595 if (cls.InheritsFrom("AliMCParticle") || cls.InheritsFrom("AliAODMCParticle"))
599 if (fCaloName == "CaloClusters")
600 am->LoadBranch("CaloClusters");
601 if (!fClus && !fCaloName.IsNull()) {
602 fClus = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fCaloName));
604 AliError(Form("%s: Pointer to clus %s == 0", GetName(), fCaloName.Data()));
609 TClass cls(fClus->GetClass()->GetName());
610 if (cls.InheritsFrom("AliEmcalParticle"))