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()),
70 // Default constructor.
73 //________________________________________________________________________
74 AliEmcalJetTask::AliEmcalJetTask(const char *name) :
75 AliAnalysisTaskSE(name),
76 fTracksName("Tracks"),
77 fCaloName("CaloClusters"),
79 fJetType(kAKT|kFullJet|kRX1Jet),
87 fPhiMax(TMath::TwoPi()),
109 // Standard constructor.
111 fBranchNames="ESD:AliESDRun.,AliESDHeader.,PrimaryVertex.";
114 //________________________________________________________________________
115 AliEmcalJetTask::~AliEmcalJetTask()
120 //________________________________________________________________________
121 void AliEmcalJetTask::UserCreateOutputObjects()
123 // Create user objects.
125 fJets = new TClonesArray("AliEmcalJet");
126 fJets->SetName(fJetsName);
129 //________________________________________________________________________
130 void AliEmcalJetTask::UserExec(Option_t *)
132 // Main loop, called for each event.
139 // clear the jet array (normally a null operation)
145 //________________________________________________________________________
146 void AliEmcalJetTask::Terminate(Option_t *)
148 // Called once at the end of the analysis.
151 //________________________________________________________________________
152 void AliEmcalJetTask::FindJets()
155 if (!fTracks && !fClus){
156 cout << "WARNING NO TRACKS OR CLUSTERS:" <<endl;
161 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
162 if ((fJetType & kAKT) != 0) {
164 jalgo = fastjet::antikt_algorithm;
165 AliDebug(1,"Using AKT algorithm");
168 AliDebug(1,"Using KT algorithm");
171 if ((fJetType & kR020Jet) != 0)
173 else if ((fJetType & kR030Jet) != 0)
175 else if ((fJetType & kR040Jet) != 0)
179 AliFJWrapper fjw(name, name);
180 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
181 fjw.SetGhostArea(fGhostArea);
183 fjw.SetAlgorithm(jalgo);
185 fjw.SetRecombScheme(static_cast<fastjet::RecombinationScheme>(fRecombScheme));
186 fjw.SetMaxRap(fEtaMax);
189 // get primary vertex
190 Double_t vertex[3] = {0, 0, 0};
191 fEvent->GetPrimaryVertex()->GetXYZ(vertex);
193 AliDebug(2,Form("Jet type = %d", fJetType));
195 if ((fIsMcPart || ((fJetType & kFullJet) != 0) || ((fJetType & kChargedJet) != 0)) && fTracks) {
196 const Int_t Ntracks = fTracks->GetEntries();
197 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
198 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(iTracks));
202 if (((fJetType & kChargedJet) != 0) && (t->Charge() == 0)) {
203 AliDebug(2,Form("Skipping track %d because it is neutral.", iTracks));
206 if (((fJetType & kNeutralJet) != 0) && (t->Charge() != 0)) {
207 AliDebug(2,Form("Skipping track %d because it is charged.", iTracks));
211 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) {
212 if (t->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
213 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
217 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fMCConstSel, t->TestBits(TObject::kBitMask)));
221 if (t->TestBits(fConstSel) != (Int_t)fConstSel) {
222 AliDebug(2,Form("Skipping track %d because it does not match the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
226 AliDebug(2,Form("Track %d matches the bit mask (%d, %d)", iTracks, fConstSel, t->TestBits(TObject::kBitMask)));
229 if (t->Pt() < fMinJetTrackPt)
231 Double_t eta = t->Eta();
232 Double_t phi = t->Phi();
233 if ((eta<fEtaMin) || (eta>fEtaMax) ||
234 (phi<fPhiMin) || (phi>fPhiMax))
237 // artificial inefficiency
238 if (fTrackEfficiency < 1.) {
239 Double_t rnd = gRandom->Rndm();
240 if (fTrackEfficiency < rnd) {
241 AliDebug(2,Form("Track %d rejected due to artificial tracking inefficiency", iTracks));
246 // offset of 100 for consistency with cluster ids
247 AliDebug(2,Form("Track %d accepted (label = %d, pt = %f)", iTracks, t->GetLabel(), t->Pt()));
248 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->P(), iTracks + 100);
252 if ((((fJetType & kFullJet) != 0) || ((fJetType & kNeutralJet) != 0)) && fClus) {
253 const Int_t Nclus = fClus->GetEntries();
254 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
256 Double_t cEta=0,cPhi=0,cPt=0;
257 Double_t cPx=0,cPy=0,cPz=0;
259 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(iClus));
263 c = ep->GetCluster();
267 if (c->GetLabel() > fMinMCLabel) {
268 if (ep->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
269 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
273 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, ep->TestBits(TObject::kBitMask)));
277 if (ep->TestBits(fConstSel) != (Int_t)fConstSel) {
278 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
282 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, ep->TestBits(TObject::kBitMask)));
293 c = static_cast<AliVCluster*>(fClus->At(iClus));
297 if (c->GetLabel() > fMinMCLabel) {
298 if (c->TestBits(fMCConstSel) != (Int_t)fMCConstSel) {
299 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
303 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fMCConstSel, c->TestBits(TObject::kBitMask)));
307 if (c->TestBits(fConstSel) != (Int_t)fConstSel) {
308 AliDebug(2,Form("Skipping cluster %d because it does not match the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
312 AliDebug(2,Form("Cluster %d matches the bit mask (%d, %d)", iClus, fConstSel, c->TestBits(TObject::kBitMask)));
317 c->GetMomentum(nP, vertex);
327 if (cPt < fMinJetClusPt)
329 if ((cEta<fEtaMin) || (cEta>fEtaMax) ||
330 (cPhi<fPhiMin) || (cPhi>fPhiMax))
332 // offset of 100 to skip ghost particles uid = -1
333 AliDebug(2,Form("Cluster %d accepted (label = %d)", iClus, c->GetLabel()));
334 fjw.AddInputVector(cPx, cPy, cPz, TMath::Sqrt(cPx*cPx+cPy*cPy+cPz*cPz), -iClus - 100);
342 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
344 AliFatal(Form("%s: Can not create geometry", GetName()));
348 // loop over fastjet jets
349 std::vector<fastjet::PseudoJet> jets_incl = fjw.GetInclusiveJets();
350 // sort jets according to jet pt
351 static Int_t indexes[9999] = {-1};
352 GetSortedArray(indexes, jets_incl);
354 AliDebug(1,Form("%d jets found", (Int_t)jets_incl.size()));
355 for (UInt_t ijet=0, jetCount=0; ijet<jets_incl.size(); ++ijet) {
356 Int_t ij = indexes[ijet];
357 AliDebug(3,Form("Jet pt = %f, area = %f", jets_incl[ij].perp(), fjw.GetJetArea(ij)));
359 if (jets_incl[ij].perp()<fMinJetPt)
361 if (fjw.GetJetArea(ij)<fMinJetArea)
363 if ((jets_incl[ij].eta()<fJetEtaMin) || (jets_incl[ij].eta()>fJetEtaMax) ||
364 (jets_incl[ij].phi()<fJetPhiMin) || (jets_incl[ij].phi()>fJetPhiMax))
367 AliEmcalJet *jet = new ((*fJets)[jetCount])
368 AliEmcalJet(jets_incl[ij].perp(), jets_incl[ij].eta(), jets_incl[ij].phi(), jets_incl[ij].m());
370 // loop over constituents
371 std::vector<fastjet::PseudoJet> constituents(fjw.GetJetConstituents(ij));
372 jet->SetNumberOfTracks(constituents.size());
373 jet->SetNumberOfClusters(constituents.size());
377 Double_t neutralE = 0;
388 for(UInt_t ic = 0; ic < constituents.size(); ++ic) {
389 Int_t uid = constituents[ic].user_index();
390 AliDebug(3,Form("Processing constituent %d", uid));
391 if ((uid == -1) /*&& (constituents[ic].kt2() < 1e-25)*/) { //ghost particle
393 Double_t gphi = constituents[ic].phi();
395 gphi += TMath::TwoPi();
396 gphi *= TMath::RadToDeg();
397 Double_t geta = constituents[ic].eta();
398 if ((gphi > geom->GetArm1PhiMin()) && (gphi < geom->GetArm1PhiMax()) &&
399 (geta > geom->GetArm1EtaMin()) && (geta < geom->GetArm1EtaMax()))
401 } else if ((uid > 0) && fTracks) { // track constituent
402 Int_t tid = uid - 100;
403 AliVParticle *t = static_cast<AliVParticle*>(fTracks->At(tid));
405 AliError(Form("Could not find track %d",tid));
408 if (jetCount < fMarkConst) {
409 AliDebug(2,Form("Marking track %d with bit map %d", tid, fJetType));
412 Double_t cEta = t->Eta();
413 Double_t cPhi = t->Phi();
414 Double_t cPt = t->Pt();
415 Double_t cP = t->P();
416 if (t->Charge() == 0) {
426 if (fIsMcPart || TMath::Abs(t->GetLabel()) > fMinMCLabel) // check if MC particle
430 cPhi += TMath::TwoPi();
431 cPhi *= TMath::RadToDeg();
432 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
433 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
438 jet->AddTrackAt(tid, nt);
440 } else if (fClus) { // cluster constituent
441 Int_t cid = -uid - 100;
443 Double_t cEta=0,cPhi=0,cPt=0,cP=0;
445 AliEmcalParticle *ep = static_cast<AliEmcalParticle*>(fClus->At(cid));
448 c = ep->GetCluster();
451 if (jetCount < fMarkConst)
452 ep->SetBit(fJetType);
458 c = static_cast<AliVCluster*>(fClus->At(cid));
461 if (jetCount < fMarkConst)
464 c->GetMomentum(nP, vertex);
475 if (c->GetLabel() > fMinMCLabel) // MC particle
476 mcpt += c->GetMCEnergyFraction() > 1e-6 ? cPt * c->GetMCEnergyFraction() : cPt;
479 cPhi += TMath::TwoPi();
480 cPhi *= TMath::RadToDeg();
481 if ((cPhi > geom->GetArm1PhiMin()) && (cPhi < geom->GetArm1PhiMax()) &&
482 (cEta > geom->GetArm1EtaMin()) && (cEta < geom->GetArm1EtaMax())) {
487 jet->AddClusterAt(cid, nc);
491 AliError(Form("%s: No logical way to end up here.", GetName()));
494 } /* end of constituent loop */
496 jet->SetNumberOfTracks(nt);
497 jet->SetNumberOfClusters(nc);
498 jet->SortConstituents();
499 jet->SetMaxNeutralPt(maxNe);
500 jet->SetMaxChargedPt(maxCh);
501 jet->SetNEF(neutralE / jet->E());
502 fastjet::PseudoJet area(fjw.GetJetAreaVector(ij));
503 jet->SetArea(area.perp());
504 jet->SetAreaEta(area.eta());
505 jet->SetAreaPhi(area.phi());
506 jet->SetNumberOfCharged(ncharged);
507 jet->SetNumberOfNeutrals(nneutral);
510 jet->SetPtEmc(emcpt);
513 jet->SetAreaEmc(fjw.GetJetArea(ij) * gemc / gall);
516 if ((jet->Phi() > geom->GetArm1PhiMin() * TMath::DegToRad()) &&
517 (jet->Phi() < geom->GetArm1PhiMax() * TMath::DegToRad()) &&
518 (jet->Eta() > geom->GetArm1EtaMin()) &&
519 (jet->Eta() < geom->GetArm1EtaMax()))
520 jet->SetAxisInEmcal(kTRUE);
522 AliDebug(2,Form("Added jet n. %d, pt = %f, area = %f, constituents = %d", jetCount, jet->Pt(), jet->Area(), (Int_t)constituents.size()));
528 //________________________________________________________________________
529 Bool_t AliEmcalJetTask::GetSortedArray(Int_t indexes[], std::vector<fastjet::PseudoJet> array) const
531 // Get the leading jets.
533 static Float_t pt[9999] = {0};
535 const Int_t n = (Int_t)array.size();
540 for (Int_t i = 0; i < n; i++)
541 pt[i] = array[i].perp();
543 TMath::Sort(n, pt, indexes);
548 //________________________________________________________________________
549 Bool_t AliEmcalJetTask::DoInit()
551 // Init. Return true if successful.
553 if (fTrackEfficiency < 1.) {
554 if (gRandom) delete gRandom;
555 gRandom = new TRandom3(0);
558 // get input collections
559 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
562 fEvent = InputEvent();
564 AliError(Form("%s: Could not retrieve event! Returning", GetName()));
568 // add jets to event if not yet there
569 if (!(fEvent->FindListObject(fJetsName)))
570 fEvent->AddObject(fJets);
572 AliError(Form("%s: Object with name %s already in event! Returning", GetName(), fJetsName.Data()));
576 if (fTracksName == "Tracks")
577 am->LoadBranch("Tracks");
578 if (!fTracks && !fTracksName.IsNull()) {
579 fTracks = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fTracksName));
581 AliError(Form("%s: Pointer to tracks %s == 0", GetName(), fTracksName.Data()));
586 TClass cls(fTracks->GetClass()->GetName());
587 if (cls.InheritsFrom("AliMCParticle") || cls.InheritsFrom("AliAODMCParticle"))
591 if (fCaloName == "CaloClusters")
592 am->LoadBranch("CaloClusters");
593 if (!fClus && !fCaloName.IsNull()) {
594 fClus = dynamic_cast<TClonesArray*>(fEvent->FindListObject(fCaloName));
596 AliError(Form("%s: Pointer to clus %s == 0", GetName(), fCaloName.Data()));
601 TClass cls(fClus->GetClass()->GetName());
602 if (cls.InheritsFrom("AliEmcalParticle"))