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()),
71 // Default constructor.
74 //________________________________________________________________________
75 AliEmcalJetTask::AliEmcalJetTask(const char *name) :
76 AliAnalysisTaskSE(name),
77 fTracksName("Tracks"),
78 fCaloName("CaloClusters"),
80 fJetType(kAKT|kFullJet|kRX1Jet),
88 fPhiMax(TMath::TwoPi()),
100 fTrackEfficiency(1.),
111 // Standard constructor.
113 fBranchNames="ESD:AliESDRun.,AliESDHeader.,PrimaryVertex.";
116 //________________________________________________________________________
117 AliEmcalJetTask::~AliEmcalJetTask()
122 //________________________________________________________________________
123 void AliEmcalJetTask::UserCreateOutputObjects()
125 // Create user objects.
127 fJets = new TClonesArray("AliEmcalJet");
128 fJets->SetName(fJetsName);
131 //________________________________________________________________________
132 void AliEmcalJetTask::UserExec(Option_t *)
134 // Main loop, called for each event.
141 // clear the jet array (normally a null operation)
147 //________________________________________________________________________
148 void AliEmcalJetTask::Terminate(Option_t *)
150 // Called once at the end of the analysis.
153 //________________________________________________________________________
154 void AliEmcalJetTask::FindJets()
157 if (!fTracks && !fClus){
158 cout << "WARNING NO TRACKS OR CLUSTERS:" <<endl;
163 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
164 if ((fJetType & kAKT) != 0) {
166 jalgo = fastjet::antikt_algorithm;
167 AliDebug(1,"Using AKT algorithm");
170 AliDebug(1,"Using KT algorithm");
173 if ((fJetType & kR020Jet) != 0)
175 else if ((fJetType & kR030Jet) != 0)
177 else if ((fJetType & kR040Jet) != 0)
181 AliFJWrapper fjw(name, name);
182 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
183 fjw.SetGhostArea(fGhostArea);
185 fjw.SetAlgorithm(jalgo);
187 fjw.SetRecombScheme(static_cast<fastjet::RecombinationScheme>(fRecombScheme));
188 fjw.SetMaxRap(fEtaMax);
191 // get primary vertex
192 Double_t vertex[3] = {0, 0, 0};
193 fEvent->GetPrimaryVertex()->GetXYZ(vertex);
195 AliDebug(2,Form("Jet type = %d", fJetType));
197 if ((fIsMcPart || ((fJetType & kFullJet) != 0) || ((fJetType & kChargedJet) != 0)) && fTracks) {
198 const Int_t Ntracks = fTracks->GetEntries();
199 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
200 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(iTracks));
204 if (((fJetType & kChargedJet) != 0) && (t->Charge() == 0)) {
205 AliDebug(2,Form("Skipping track %d because it is neutral.", iTracks));
208 if (((fJetType & kNeutralJet) != 0) && (t->Charge() != 0)) {
209 AliDebug(2,Form("Skipping track %d because it is charged.", iTracks));
213 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) {
214 if (t->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
215 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
219 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
223 if (t->TestBits(fConstSel) != (Int_t)fConstSel) {
224 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
228 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
231 if (t->Pt() < fMinJetTrackPt)
233 Double_t eta = t->Eta();
234 Double_t phi = t->Phi();
235 if ((eta<fEtaMin) || (eta>fEtaMax) ||
236 (phi<fPhiMin) || (phi>fPhiMax))
239 // artificial inefficiency
240 if (fTrackEfficiency < 1.) {
241 Double_t rnd = gRandom->Rndm();
242 if (fTrackEfficiency < rnd) {
243 AliDebug(2,Form("Track %d rejected due to artificial tracking inefficiency", iTracks));
248 // offset of 100 for consistency with cluster ids
249 AliDebug(2,Form("Track %d accepted (label = %d, pt = %f)", iTracks, t->GetLabel(), t->Pt()));
250 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->P(), iTracks + 100);
254 if ((((fJetType & kFullJet) != 0) || ((fJetType & kNeutralJet) != 0)) && fClus) {
255 const Int_t Nclus = fClus->GetEntries();
256 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
258 Double_t cEta=0,cPhi=0,cPt=0;
259 Double_t cPx=0,cPy=0,cPz=0;
261 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(iClus));
265 c = ep->GetCluster();
269 if (c->GetLabel() > fMinMCLabel) {
270 if (ep->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
271 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
275 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
279 if (ep->TestBits(fConstSel) != (Int_t)fConstSel) {
280 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
284 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
295 c = static_cast<AliVCluster*>(fClus->At(iClus));
299 if (c->GetLabel() > fMinMCLabel) {
300 if (c->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
301 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
305 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
309 if (c->TestBits(fConstSel) != (Int_t)fConstSel) {
310 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
314 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
319 c->GetMomentum(nP, vertex);
329 if (cPt < fMinJetClusPt)
331 if ((cEta<fEtaMin) || (cEta>fEtaMax) ||
332 (cPhi<fPhiMin) || (cPhi>fPhiMax))
334 // offset of 100 to skip ghost particles uid = -1
335 AliDebug(2,Form("Cluster %d accepted (label = %d)", iClus, c->GetLabel()));
336 fjw.AddInputVector(cPx, cPy, cPz, TMath::Sqrt(cPx*cPx+cPy*cPy+cPz*cPz), -iClus - 100);
340 // setting legacy mode
342 fjw.SetLegacyMode(kTRUE);
349 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
351 AliFatal(Form("%s: Can not create geometry", GetName()));
355 // loop over fastjet jets
356 std::vector<fastjet::PseudoJet> jets_incl = fjw.GetInclusiveJets();
357 // sort jets according to jet pt
358 static Int_t indexes[9999] = {-1};
359 GetSortedArray(indexes, jets_incl);
361 AliDebug(1,Form("%d jets found", (Int_t)jets_incl.size()));
362 for (UInt_t ijet=0, jetCount=0; ijet<jets_incl.size(); ++ijet) {
363 Int_t ij = indexes[ijet];
364 AliDebug(3,Form("Jet pt = %f, area = %f", jets_incl[ij].perp(), fjw.GetJetArea(ij)));
366 if (jets_incl[ij].perp()<fMinJetPt)
368 if (fjw.GetJetArea(ij)<fMinJetArea)
370 if ((jets_incl[ij].eta()<fJetEtaMin) || (jets_incl[ij].eta()>fJetEtaMax) ||
371 (jets_incl[ij].phi()<fJetPhiMin) || (jets_incl[ij].phi()>fJetPhiMax))
374 AliEmcalJet *jet = new ((*fJets)[jetCount])
375 AliEmcalJet(jets_incl[ij].perp(), jets_incl[ij].eta(), jets_incl[ij].phi(), jets_incl[ij].m());
377 // loop over constituents
378 std::vector<fastjet::PseudoJet> constituents(fjw.GetJetConstituents(ij));
379 jet->SetNumberOfTracks(constituents.size());
380 jet->SetNumberOfClusters(constituents.size());
384 Double_t neutralE = 0;
395 for(UInt_t ic = 0; ic < constituents.size(); ++ic) {
396 Int_t uid = constituents[ic].user_index();
397 AliDebug(3,Form("Processing constituent %d", uid));
398 if ((uid == -1) /*&& (constituents[ic].kt2() < 1e-25)*/) { //ghost particle
400 Double_t gphi = constituents[ic].phi();
402 gphi += TMath::TwoPi();
403 gphi *= TMath::RadToDeg();
404 Double_t geta = constituents[ic].eta();
405 if ((gphi > geom->GetArm1PhiMin()) && (gphi < geom->GetArm1PhiMax()) &&
406 (geta > geom->GetArm1EtaMin()) && (geta < geom->GetArm1EtaMax()))
408 } else if ((uid > 0) && fTracks) { // track constituent
409 Int_t tid = uid - 100;
410 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(tid));
412 AliError(Form("Could not find track %d",tid));
415 if (jetCount < fMarkConst) {
416 AliDebug(2,Form("Marking track %d with bit map %d", tid, fJetType));
419 Double_t cEta = t->Eta();
420 Double_t cPhi = t->Phi();
421 Double_t cPt = t->Pt();
422 Double_t cP = t->P();
423 if (t->Charge() == 0) {
433 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) // check if MC particle
437 cPhi += TMath::TwoPi();
438 cPhi *= TMath::RadToDeg();
439 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
440 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
445 jet->AddTrackAt(tid, nt);
447 } else if (fClus) { // cluster constituent
448 Int_t cid = -uid - 100;
450 Double_t cEta=0,cPhi=0,cPt=0,cP=0;
452 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(cid));
455 c = ep->GetCluster();
458 if (jetCount < fMarkConst)
459 ep->SetBit(fJetType);
465 c = static_cast<AliVCluster*>(fClus->At(cid));
468 if (jetCount < fMarkConst)
471 c->GetMomentum(nP, vertex);
482 if (c->GetLabel() > fMinMCLabel) // MC particle
483 mcpt += c->GetMCEnergyFraction() > 1e-6 ? cPt * c->GetMCEnergyFraction() : cPt;
486 cPhi += TMath::TwoPi();
487 cPhi *= TMath::RadToDeg();
488 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
489 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
494 jet->AddClusterAt(cid, nc);
498 AliError(Form("%s: No logical way to end up here.", GetName()));
501 } /* end of constituent loop */
503 jet->SetNumberOfTracks(nt);
504 jet->SetNumberOfClusters(nc);
505 jet->SortConstituents();
506 jet->SetMaxNeutralPt(maxNe);
507 jet->SetMaxChargedPt(maxCh);
508 jet->SetNEF(neutralE / jet->E());
509 fastjet::PseudoJet area(fjw.GetJetAreaVector(ij));
510 jet->SetArea(area.perp());
511 jet->SetAreaEta(area.eta());
512 jet->SetAreaPhi(area.phi());
513 jet->SetNumberOfCharged(ncharged);
514 jet->SetNumberOfNeutrals(nneutral);
517 jet->SetPtEmc(emcpt);
520 jet->SetAreaEmc(fjw.GetJetArea(ij) * gemc / gall);
523 if ((jet->Phi() > geom->GetArm1PhiMin() * TMath::DegToRad()) &&
524 (jet->Phi() < geom->GetArm1PhiMax() * TMath::DegToRad()) &&
525 (jet->Eta() > geom->GetArm1EtaMin()) &&
526 (jet->Eta() < geom->GetArm1EtaMax()))
527 jet->SetAxisInEmcal(kTRUE);
529 AliDebug(2,Form("Added jet n. %d, pt = %f, area = %f, constituents = %d", jetCount, jet->Pt(), jet->Area(), (Int_t)constituents.size()));
535 //________________________________________________________________________
536 Bool_t AliEmcalJetTask::GetSortedArray(Int_t indexes[], std::vector<fastjet::PseudoJet> array) const
538 // Get the leading jets.
540 static Float_t pt[9999] = {0};
542 const Int_t n = (Int_t)array.size();
547 for (Int_t i = 0; i < n; i++)
548 pt[i] = array[i].perp();
550 TMath::Sort(n, pt, indexes);
555 //________________________________________________________________________
556 Bool_t AliEmcalJetTask::DoInit()
558 // Init. Return true if successful.
560 if (fTrackEfficiency < 1.) {
561 if (gRandom) delete gRandom;
562 gRandom = new TRandom3(0);
565 // get input collections
566 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
569 fEvent = InputEvent();
571 AliError(Form("%s: Could not retrieve event! Returning", GetName()));
575 // add jets to event if not yet there
576 if (!(fEvent->FindListObject(fJetsName)))
577 fEvent->AddObject(fJets);
579 AliError(Form("%s: Object with name %s already in event! Returning", GetName(), fJetsName.Data()));
583 if (fTracksName == "Tracks")
584 am->LoadBranch("Tracks");
585 if (!fTracks && !fTracksName.IsNull()) {
586 fTracks = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fTracksName));
588 AliError(Form("%s: Pointer to tracks %s == 0", GetName(), fTracksName.Data()));
593 TClass cls(fTracks->GetClass()->GetName());
594 if (cls.InheritsFrom("AliMCParticle") || cls.InheritsFrom("AliAODMCParticle"))
598 if (fCaloName == "CaloClusters")
599 am->LoadBranch("CaloClusters");
600 if (!fClus && !fCaloName.IsNull()) {
601 fClus = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fCaloName));
603 AliError(Form("%s: Pointer to clus %s == 0", GetName(), fCaloName.Data()));
608 TClass cls(fClus->GetClass()->GetName());
609 if (cls.InheritsFrom("AliEmcalParticle"))