3 // Emcal jet finder task.
5 // Authors: C.Loizides, S.Aiola
8 #include "AliEmcalJetTask.h"
11 #include <TClonesArray.h>
13 #include <TLorentzVector.h>
16 #include "AliAnalysisManager.h"
17 #include "AliCentrality.h"
18 #include "AliEMCALGeometry.h"
19 #include "AliESDEvent.h"
20 #include "AliEmcalJet.h"
21 #include "AliEmcalParticle.h"
22 #include "AliFJWrapper.h"
23 #include "AliMCEvent.h"
24 #include "AliVCluster.h"
25 #include "AliVEvent.h"
26 #include "AliVParticle.h"
28 ClassImp(AliEmcalJetTask)
30 //________________________________________________________________________
31 AliEmcalJetTask::AliEmcalJetTask() :
32 AliAnalysisTaskSE("AliEmcalJetTask"),
33 fTracksName("Tracks"),
34 fCaloName("CaloClusters"),
38 fMCConstSel(kAllJets),
44 fPhiMax(TMath::TwoPi()),
63 // Default constructor.
66 //________________________________________________________________________
67 AliEmcalJetTask::AliEmcalJetTask(const char *name) :
68 AliAnalysisTaskSE(name),
69 fTracksName("Tracks"),
70 fCaloName("CaloClusters"),
72 fJetType(kAKT|kFullJet|kRX1Jet),
74 fMCConstSel(kAllJets),
80 fPhiMax(TMath::TwoPi()),
99 // Standard constructor.
101 fBranchNames="ESD:AliESDRun.,AliESDHeader.,PrimaryVertex.";
104 //________________________________________________________________________
105 AliEmcalJetTask::~AliEmcalJetTask()
110 //________________________________________________________________________
111 void AliEmcalJetTask::UserCreateOutputObjects()
113 // Create user objects.
115 fJets = new TClonesArray("AliEmcalJet");
116 fJets->SetName(fJetsName);
119 //________________________________________________________________________
120 void AliEmcalJetTask::UserExec(Option_t *)
122 // Main loop, called for each event.
133 //________________________________________________________________________
134 void AliEmcalJetTask::Terminate(Option_t *)
136 // Called once at the end of the analysis.
139 //________________________________________________________________________
140 void AliEmcalJetTask::FindJets()
144 if (!fTracks && !fClus)
148 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
149 if ((fJetType & kAKT) != 0) {
151 jalgo = fastjet::antikt_algorithm;
152 AliDebug(1,"Using AKT algorithm");
155 AliDebug(1,"Using KT algorithm");
158 if ((fJetType & kR020Jet) != 0)
160 else if ((fJetType & kR030Jet) != 0)
162 else if ((fJetType & kR040Jet) != 0)
166 AliFJWrapper fjw(name, name);
167 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
168 fjw.SetGhostArea(fGhostArea);
170 fjw.SetAlgorithm(jalgo);
174 // get primary vertex
175 Double_t vertex[3] = {0, 0, 0};
176 fEvent->GetPrimaryVertex()->GetXYZ(vertex);
178 AliDebug(2,Form("Jet type = %d", fJetType));
180 if ((fIsMcPart || ((fJetType & kFullJet) != 0) || ((fJetType & kChargedJet) != 0)) && fTracks) {
181 const Int_t Ntracks = fTracks->GetEntries();
182 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
183 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(iTracks));
187 if (((fJetType & kChargedJet) != 0) && (t->Charge() == 0))
189 if (((fJetType & kNeutralJet) != 0) && (t->Charge() != 0))
192 if (fIsMcPart || t->GetLabel() != 0) {
193 if (fMCConstSel == kNone) {
194 AliDebug(2,Form("Skipping track %d because bit mask is 0.", iTracks));
197 if (fMCConstSel != kAllJets) {
198 if (t->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
199 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
203 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
208 if (fConstSel == kNone) {
209 AliDebug(2,Form("Skipping track %d because bit mask is 0.", iTracks));
212 if (fConstSel != kAllJets) {
213 if (t->TestBits(fConstSel) != (Int_t)fConstSel) {
214 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
218 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
222 if (t->Pt() < fMinJetTrackPt)
224 Double_t eta = t->Eta();
225 Double_t phi = t->Phi();
226 if ((eta<fEtaMin) || (eta>fEtaMax) ||
227 (phi<fPhiMin) || (phi>fPhiMax))
230 // offset of 100 for consistency with cluster ids
231 AliDebug(2,Form("Track %d accepted (label = %d, pt = %f)", iTracks, t->GetLabel(), t->Pt()));
232 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->P(), iTracks + 100);
236 if ((((fJetType & kFullJet) != 0) || ((fJetType & kNeutralJet) != 0)) && fClus) {
237 const Int_t Nclus = fClus->GetEntries();
238 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
240 Double_t cEta=0,cPhi=0,cPt=0;
241 Double_t cPx=0,cPy=0,cPz=0;
243 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(iClus));
247 c = ep->GetCluster();
251 if (c->GetLabel() > 0) {
252 if (fMCConstSel == kNone) {
253 AliDebug(2,Form("Skipping cluster %d because bit mask is 0.", iClus));
256 if (fMCConstSel != kAllJets) {
257 if (ep->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
258 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
262 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
267 if (fConstSel == kNone) {
268 AliDebug(2,Form("Skipping cluster %d because bit mask is 0.", iClus));
271 if (fConstSel != kAllJets) {
272 if (ep->TestBits(fConstSel) != (Int_t)fConstSel) {
273 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
277 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
289 c = static_cast<AliVCluster*>(fClus->At(iClus));
293 if (c->GetLabel() > 0) {
294 if (fMCConstSel == kNone) {
295 AliDebug(2,Form("Skipping cluster %d because bit mask is 0.", iClus));
298 if (fMCConstSel != kAllJets) {
299 if (c->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
300 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
304 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
309 if (fConstSel == kNone) {
310 AliDebug(2,Form("Skipping cluster %d because bit mask is 0.", iClus));
313 if (fConstSel != kAllJets) {
314 if (c->TestBits(fConstSel) != (Int_t)fConstSel) {
315 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
319 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
325 c->GetMomentum(nP, vertex);
335 if (cPt < fMinJetClusPt)
337 if ((cEta<fEtaMin) || (cEta>fEtaMax) ||
338 (cPhi<fPhiMin) || (cPhi>fPhiMax))
340 // offset of 100 to skip ghost particles uid = -1
341 AliDebug(2,Form("Cluster %d accepted (label = %d)", iClus, c->GetLabel()));
342 fjw.AddInputVector(cPx, cPy, cPz, TMath::Sqrt(cPx*cPx+cPy*cPy+cPz*cPz), -iClus - 100);
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(2,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 || t->GetLabel() != 0) // 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() > 0) // MC particle
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 // get input collections
562 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
565 fEvent = InputEvent();
567 AliError(Form("%s: Could not retrieve event! Returning", GetName()));
571 // add jets to event if not yet there
573 if (!(fEvent->FindListObject(fJetsName)))
574 fEvent->AddObject(fJets);
576 AliError(Form("%s: Object with name %s already in event! Returning", GetName(), fJetsName.Data()));
580 if (fTracksName == "Tracks")
581 am->LoadBranch("Tracks");
582 if (!fTracks && !fTracksName.IsNull()) {
583 fTracks = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fTracksName));
585 AliError(Form("%s: Pointer to tracks %s == 0", GetName(), fTracksName.Data()));
590 TClass cls(fTracks->GetClass()->GetName());
591 if (cls.InheritsFrom("AliMCParticle"))
595 if (fCaloName == "CaloClusters")
596 am->LoadBranch("CaloClusters");
597 if (!fClus && !fCaloName.IsNull()) {
598 fClus = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fCaloName));
600 AliError(Form("%s: Pointer to clus %s == 0", GetName(), fCaloName.Data()));
605 TClass cls(fClus->GetClass()->GetName());
606 if (cls.InheritsFrom("AliEmcalParticle"))