1 // $Id: AliJetEmbeddingFromAODTask.cxx $
3 // Jet embedding from AOD task.
5 // Author: S.Aiola, C.Loizides
7 #include "AliJetEmbeddingFromAODTask.h"
9 // C++ standard library
15 #include <TClonesArray.h>
16 #include <TObjArray.h>
17 #include <TObjString.h>
21 #include <TStreamerInfo.h>
24 #include <TLorentzVector.h>
27 #include "AliVEvent.h"
28 #include "AliAODTrack.h"
29 #include "AliESDtrack.h"
30 #include "AliPicoTrack.h"
31 #include "AliVTrack.h"
32 #include "AliVCluster.h"
33 #include "AliVCaloCells.h"
34 #include "AliAODMCParticle.h"
35 #include "AliCentrality.h"
36 #include "AliVHeader.h"
37 #include "AliVVertex.h"
38 #include "AliAODHeader.h"
39 #include "AliFJWrapper.h"
41 #include "AliInputEventHandler.h"
43 ClassImp(AliJetEmbeddingFromAODTask)
45 //________________________________________________________________________
46 AliJetEmbeddingFromAODTask::AliJetEmbeddingFromAODTask() :
47 AliJetModelBaseTask("AliJetEmbeddingFromAODTask"),
49 fRandomAccess(kFALSE),
56 fAODMCParticlesName(),
59 fTriggerMask(AliVEvent::kAny),
66 fJetConstituentMinPt(0),
70 fJetParticleLevel(kTRUE),
71 fIncludeNoITS(kFALSE),
72 fCutMaxFractionSharedTPCClusters(0.4),
73 fUseNegativeLabels(kTRUE),
96 // Default constructor.
97 SetSuffix("AODEmbedding");
98 fAODfilterBits[0] = -1;
99 fAODfilterBits[1] = -1;
108 //________________________________________________________________________
109 AliJetEmbeddingFromAODTask::AliJetEmbeddingFromAODTask(const char *name, Bool_t drawqa) :
110 AliJetModelBaseTask(name, drawqa),
112 fRandomAccess(kFALSE),
113 fAODTreeName("aodTree"),
114 fAODHeaderName("header"),
115 fAODVertexName("vertices"),
116 fAODTrackName("tracks"),
118 fAODCellsName("emcalCells"),
119 fAODMCParticlesName(AliAODMCParticle::StdBranchName()),
122 fTriggerMask(AliVEvent::kAny),
129 fJetConstituentMinPt(0),
133 fJetParticleLevel(kTRUE),
134 fIncludeNoITS(kFALSE),
135 fCutMaxFractionSharedTPCClusters(0.4),
136 fUseNegativeLabels(kTRUE),
141 fEsdTreeMode(kFALSE),
143 fCurrentAODFileID(0),
145 fPicoTrackVersion(0),
153 fCurrentAODEntry(-1),
154 fHistFileMatching(0),
155 fHistAODFileError(0),
159 // Standard constructor.
160 SetSuffix("AODEmbedding");
161 fAODfilterBits[0] = -1;
162 fAODfilterBits[1] = -1;
171 //________________________________________________________________________
172 AliJetEmbeddingFromAODTask::~AliJetEmbeddingFromAODTask()
176 if (fCurrentAODFile) {
177 fCurrentAODFile->Close();
178 delete fCurrentAODFile;
182 //________________________________________________________________________
183 void AliJetEmbeddingFromAODTask::UserCreateOutputObjects()
188 AliJetModelBaseTask::UserCreateOutputObjects();
190 fHistFileMatching = new TH2C("fHistFileMatching", "fHistFileMatching", fTotalFiles, -0.5, fTotalFiles - 0.5, fFileList->GetEntriesFast(), -0.5, fFileList->GetEntriesFast() -0.5);
191 fHistFileMatching->GetXaxis()->SetTitle("File no. (PYTHIA)");
192 fHistFileMatching->GetYaxis()->SetTitle("File no. (Embedded AOD)");
193 fHistFileMatching->GetZaxis()->SetTitle("counts");
194 fOutput->Add(fHistFileMatching);
196 fHistAODFileError = new TH1C("fHistAODFileError", "fHistAODFileError", fFileList->GetEntriesFast(), -0.5, fFileList->GetEntriesFast() -0.5);
197 fHistAODFileError->GetXaxis()->SetTitle("File no. (Embedded AOD)");
198 fHistAODFileError->GetYaxis()->SetTitle("counts");
199 fOutput->Add(fHistAODFileError);
201 fHistNotEmbedded = new TH1C("fHistNotEmbedded", "fHistNotEmbedded", fTotalFiles, -0.5, fTotalFiles - 0.5);
202 fHistNotEmbedded->GetXaxis()->SetTitle("File no. (PYTHIA)");
203 fHistNotEmbedded->GetYaxis()->SetTitle("counts");
204 fOutput->Add(fHistNotEmbedded);
206 fHistEmbeddingQA = new TH1F("fHistEmbeddingQA", "fHistEmbeddingQA", 2, 0, 2);
207 fHistEmbeddingQA->GetXaxis()->SetTitle("Event state");
208 fHistEmbeddingQA->GetYaxis()->SetTitle("counts");
209 fHistEmbeddingQA->GetXaxis()->SetBinLabel(1, "OK");
210 fHistEmbeddingQA->GetXaxis()->SetBinLabel(2, "Not embedded");
211 fOutput->Add(fHistEmbeddingQA);
213 PostData(1, fOutput);
216 //________________________________________________________________________
217 Bool_t AliJetEmbeddingFromAODTask::UserNotify()
219 TString path(fInputHandler->GetTree()->GetCurrentFile()->GetPath());
220 if (path.EndsWith("/"))
221 path.Remove(path.Length()-1);
222 path.Remove(path.Last('/'));
223 path.Remove(0, path.Last('/')+1);
225 AliWarning(Form("Could not extract file number from path %s", path.Data()));
228 fCurrentFileID = path.Atoi();
229 if (!fRandomAccess) {
230 fCurrentAODFileID = fFileList->GetEntriesFast() * fCurrentFileID / fTotalFiles-1;
231 AliInfo(Form("Start embedding from file ID %d", fCurrentAODFileID));
236 //________________________________________________________________________
237 Bool_t AliJetEmbeddingFromAODTask::ExecOnce()
239 if (fAODTreeName.Contains("aod"))
240 fEsdTreeMode = kFALSE;
242 fEsdTreeMode = kTRUE;
244 return AliJetModelBaseTask::ExecOnce();
247 //________________________________________________________________________
248 Bool_t AliJetEmbeddingFromAODTask::OpenNextFile()
250 if (fCurrentAODFile) {
251 fCurrentAODFile->Close();
252 delete fCurrentAODFile;
258 while ((!fCurrentAODFile || fCurrentAODFile->IsZombie()) && i < fAttempts) {
259 if (i > 0 && fHistAODFileError) {
260 fHistAODFileError->Fill(fCurrentAODFileID);
263 fCurrentAODFile = GetNextFile();
267 if (!fCurrentAODFile || fCurrentAODFile->IsZombie())
270 const TList *clist = fCurrentAODFile->GetStreamerInfoCache();
272 TStreamerInfo *cinfo = static_cast<TStreamerInfo*>(clist->FindObject("AliPicoTrack"));
274 fPicoTrackVersion = cinfo->GetClassVersion();
276 fPicoTrackVersion = 0;
279 fCurrentAODTree = static_cast<TTree*>(fCurrentAODFile->Get(fAODTreeName));
280 if (!fCurrentAODTree)
283 if (!fAODHeaderName.IsNull())
284 fCurrentAODTree->SetBranchAddress(fAODHeaderName, &fAODHeader);
286 if (!fAODVertexName.IsNull())
287 fCurrentAODTree->SetBranchAddress(fAODVertexName, &fAODVertex);
289 if (!fAODTrackName.IsNull())
290 fCurrentAODTree->SetBranchAddress(fAODTrackName, &fAODTracks);
292 if (!fAODClusName.IsNull())
293 fCurrentAODTree->SetBranchAddress(fAODClusName, &fAODClusters);
295 if (!fAODCellsName.IsNull())
296 fCurrentAODTree->SetBranchAddress(fAODCellsName, &fAODCaloCells);
298 if (!fAODMCParticlesName.IsNull())
299 fCurrentAODTree->SetBranchAddress(fAODMCParticlesName, &fAODMCParticles);
302 fCurrentAODEntry = TMath::Nint(gRandom->Rndm()*fCurrentAODTree->GetEntries());
304 fCurrentAODEntry = 0;
306 AliDebug(3,Form("Will start embedding from entry %d", fCurrentAODEntry));
308 if (fHistFileMatching)
309 fHistFileMatching->Fill(fCurrentFileID, fCurrentAODFileID-1);
314 //________________________________________________________________________
315 TFile* AliJetEmbeddingFromAODTask::GetNextFile()
318 fCurrentAODFileID = TMath::Nint(gRandom->Rndm()*fFileList->GetEntriesFast());
322 if (fCurrentAODFileID >= fFileList->GetEntriesFast()) {
323 AliError("No more file in the list!");
327 TObjString *objFileName = static_cast<TObjString*>(fFileList->At(fCurrentAODFileID));
328 TString fileName(objFileName->GetString());
330 if (fileName.BeginsWith("alien://") && !gGrid) {
331 AliInfo("Trying to connect to AliEn ...");
332 TGrid::Connect("alien://");
335 TString baseFileName(fileName);
336 if (baseFileName.Contains(".zip#")) {
337 Ssiz_t pos = baseFileName.Last('#');
338 baseFileName.Remove(pos);
341 if (gSystem->AccessPathName(baseFileName)) {
342 AliDebug(3,Form("File %s does not exist!", baseFileName.Data()));
346 AliDebug(3,Form("Trying to open file %s...", fileName.Data()));
347 TFile *file = TFile::Open(fileName);
350 AliDebug(3,Form("Unable to open file: %s!", fileName.Data()));
355 //________________________________________________________________________
356 Bool_t AliJetEmbeddingFromAODTask::GetNextEntry()
361 if (!fCurrentAODFile || !fCurrentAODTree || fCurrentAODEntry >= fCurrentAODTree->GetEntries()) {
366 fCurrentAODTree->GetEntry(fCurrentAODEntry);
370 if (attempts == 1000)
371 AliWarning("After 1000 attempts no event has been accepted by the event selection (trigger, centrality...)!");
373 } while (!IsAODEventSelected() && fCurrentAODTree);
375 return (fCurrentAODTree!=0);
378 //________________________________________________________________________
379 Bool_t AliJetEmbeddingFromAODTask::IsAODEventSelected()
381 // AOD event selection.
383 if (!fEsdTreeMode && fAODHeader) {
384 AliAODHeader *aodHeader = static_cast<AliAODHeader*>(fAODHeader);
387 if (fTriggerMask != AliVEvent::kAny) {
388 UInt_t offlineTrigger = aodHeader->GetOfflineTrigger();
390 if ((offlineTrigger & fTriggerMask) == 0) {
391 AliDebug(2,Form("Event rejected due to physics selection. Event trigger mask: %d, trigger mask selection: %d.",
392 offlineTrigger, fTriggerMask));
397 // Centrality selection
398 if (fMinCentrality >= 0) {
399 AliCentrality *cent = aodHeader->GetCentralityP();
400 Float_t centVal = cent->GetCentralityPercentile("V0M");
401 if (centVal < fMinCentrality || centVal >= fMaxCentrality) {
402 AliDebug(2,Form("Event rejected due to centrality selection. Event centrality: %f, centrality range selection: %f to %f",
403 centVal, fMinCentrality, fMaxCentrality));
411 Double_t vert[3]={0};
412 ((AliVVertex*)fAODVertex->At(0))->GetXYZ(vert);
413 if (TMath::Abs(vert[2]) > fZVertexCut) {
414 AliDebug(2,Form("Event rejected due to Z vertex selection. Event Z vertex: %f, Z vertex cut: %f",
415 vert[2], fZVertexCut));
424 if (fJetParticleLevel) {
426 jet = GetLeadingJet(fAODMCParticles);
428 AliWarning("Particle level jets selected, but not MC particles found. The jet event selection will be skipped.");
433 if (fAODTracks || fAODClusters)
434 jet = GetLeadingJet(fAODTracks, fAODClusters);
436 AliWarning("Detector level jets selected, but not tracks or clusters found. The jet event selection will be skipped.");
441 AliDebug(1, Form("Leading jet pt = %f", jet.Pt()));
442 if (jet.Pt() < fJetMinPt)
449 //________________________________________________________________________
450 void AliJetEmbeddingFromAODTask::Run()
452 if (!GetNextEntry()) {
453 if (fHistNotEmbedded)
454 fHistNotEmbedded->Fill(fCurrentFileID);
455 if (fHistEmbeddingQA)
456 fHistEmbeddingQA->Fill("Not embedded", 1);
457 AliError("Unable to get the AOD event to embed. Nothing will be embedded.");
461 if (fHistEmbeddingQA)
462 fHistEmbeddingQA->Fill("OK", 1);
464 if (fOutMCParticles) {
466 if (fCopyArray && fMCParticles)
469 if (fAODMCParticles) {
470 AliDebug(3, Form("%d MC particles will be processed for embedding.", fAODMCParticles->GetEntriesFast()));
471 for (Int_t i = 0; i < fAODMCParticles->GetEntriesFast(); i++) {
472 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fAODMCParticles->At(i));
474 AliError(Form("Could not find MC particle %d in branch %s of tree %s!", i, fAODMCParticlesName.Data(), fAODTreeName.Data()));
478 AliDebug(3, Form("Processing MC particle %d with pT = %f, eta = %f, phi = %f", i, part->Pt(), part->Eta(), part->Phi()));
480 if (!part->IsPhysicalPrimary())
483 if (part->Pt() < fPtMin || part->Pt() > fPtMax ||
484 part->Eta() < fEtaMin || part->Eta() > fEtaMax ||
485 part->Phi() < fPhiMin || part->Phi() > fPhiMax)
488 AddMCParticle(part, i);
489 AliDebug(3, "Embedded!");
496 if (fCopyArray && fTracks)
499 AliDebug(3, Form("Start embedding with %d tracks.", fOutTracks->GetEntriesFast()));
502 AliDebug(3, Form("%d tracks will be processed for embedding.", fAODTracks->GetEntriesFast()));
503 for (Int_t i = 0; i < fAODTracks->GetEntriesFast(); i++) {
504 AliVTrack *track = static_cast<AliVTrack*>(fAODTracks->At(i));
506 AliError(Form("Could not find track %d in branch %s of tree %s!", i, fAODTrackName.Data(), fAODTreeName.Data()));
510 AliDebug(3, Form("Processing track %d with pT = %f, eta = %f, phi = %f, label = %d", i, track->Pt(), track->Eta(), track->Phi(), track->GetLabel()));
513 Bool_t isEmc = kFALSE;
515 AliAODTrack *aodtrack = static_cast<AliAODTrack*>(track);
516 if (aodtrack->TestFilterBit(fAODfilterBits[0])) {
519 else if (aodtrack->TestFilterBit(fAODfilterBits[1])) {
520 if ((aodtrack->GetStatus()&AliESDtrack::kITSrefit)==0) {
524 AliDebug(3, "Track not embedded because ITS refit failed.");
532 else { /*not a good track*/
533 AliDebug(3, "Track not embedded because not an hybrid track.");
536 if (fCutMaxFractionSharedTPCClusters > 0) {
537 Double_t frac = Double_t(aodtrack->GetTPCnclsS()) / Double_t(aodtrack->GetTPCncls());
538 if (frac > fCutMaxFractionSharedTPCClusters)
541 if (TMath::Abs(aodtrack->GetTrackEtaOnEMCal()) < 0.75 &&
542 aodtrack->GetTrackPhiOnEMCal() > 70 * TMath::DegToRad() &&
543 aodtrack->GetTrackPhiOnEMCal() < 190 * TMath::DegToRad())
546 else if (fPicoTrackVersion > 0) { /*not AOD mode, let's see if it is PicoTrack*/
547 AliPicoTrack *ptrack = dynamic_cast<AliPicoTrack*>(track);
549 if (fPicoTrackVersion >= 3)
550 type = ptrack->GetTrackType();
552 type = ptrack->GetLabel();
553 isEmc = ptrack->IsEMCAL();
557 if (track->Pt() < fPtMin || track->Pt() > fPtMax ||
558 track->Eta() < fEtaMin || track->Eta() > fEtaMax ||
559 track->Phi() < fPhiMin || track->Phi() > fPhiMax) {
560 AliDebug(3, "Track not embedded because out of limits.");
564 if (fTrackEfficiency < 1) {
565 Double_t r = gRandom->Rndm();
566 if (fTrackEfficiency < r) {
567 AliDebug(3, "Track not embedded because of artificial inefiiciency.");
574 if (fUseNegativeLabels)
575 label = track->GetLabel();
577 label = TMath::Abs(track->GetLabel());
580 AliDebug(1,Form("%s: Track %d with label==0", GetName(), i));
583 AddTrack(track->Pt(), track->Eta(), track->Phi(), type, track->GetTrackEtaOnEMCal(), track->GetTrackPhiOnEMCal(), isEmc, label);
584 AliDebug(3, "Track embedded!");
591 if (fCopyArray && fClusters)
595 for (Int_t i = 0; i < fAODClusters->GetEntriesFast(); i++) {
596 AliVCluster *clus = static_cast<AliVCluster*>(fAODClusters->At(i));
598 AliError(Form("Could not find cluster %d in branch %s of tree %s!", i, fAODClusName.Data(), fAODTreeName.Data()));
602 if (!clus->IsEMCAL())
606 Double_t vert[3] = {0,0,0};
607 clus->GetMomentum(vect,vert);
609 if (vect.Pt() < fPtMin || vect.Pt() > fPtMax ||
610 vect.Eta() < fEtaMin || vect.Eta() > fEtaMax ||
611 vect.Phi() < fPhiMin || vect.Phi() > fPhiMax)
616 label = clus->GetLabel();
618 AliDebug(1,Form("%s: Clus %d with label<=0", GetName(), i));
627 Double_t totalEnergy = 0;
628 Int_t totalCells = 0;
631 totalCells += fCaloCells->GetNumberOfCells();
633 totalCells += fAODCaloCells->GetNumberOfCells();
635 SetNumberOfOutCells(totalCells);
641 for (Short_t i = 0; i < fAODCaloCells->GetNumberOfCells(); i++) {
645 Short_t cellNum = -1;
648 fAODCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
652 AliDebug(1,Form("%s: Cell %d with label<=0", GetName(), i));
658 AliDebug(2,Form("Adding cell with amplitude %f, absolute ID %d, time %f, mc label %d", amp, cellNum, time, mclabel));
659 AddCell(amp, cellNum, time, mclabel);
663 AliDebug(2,Form("Added cells = %d (energy = %f), total cells = %d", fAddedCells, totalEnergy, totalCells));
667 //________________________________________________________________________
668 TLorentzVector AliJetEmbeddingFromAODTask::GetLeadingJet(TClonesArray *tracks, TClonesArray *clusters)
671 fastjet::JetAlgorithm jalgo(fastjet::kt_algorithm);
674 jalgo = fastjet::antikt_algorithm;
678 AliFJWrapper fjw(name, name);
679 fjw.SetAreaType(fastjet::active_area_explicit_ghosts);
680 fjw.SetGhostArea(1); // set a very large ghost area to speed up jet finding
681 fjw.SetR(fJetRadius);
682 fjw.SetAlgorithm(jalgo);
687 const Int_t Ntracks = tracks->GetEntries();
688 for (Int_t iTracks = 0; iTracks < Ntracks; ++iTracks) {
689 AliVParticle *t = static_cast<AliVParticle*>(tracks->At(iTracks));
693 AliAODMCParticle *aodmcpart = dynamic_cast<AliAODMCParticle*>(t);
694 if (aodmcpart && !aodmcpart->IsPhysicalPrimary())
697 if ((fJetType == 1 && t->Charge() == 0) ||
698 (fJetType == 2 && t->Charge() != 0))
701 if (t->Pt() < fJetConstituentMinPt)
704 fjw.AddInputVector(t->Px(), t->Py(), t->Pz(), t->P(), iTracks + 100);
708 if (clusters && fJetType != 1) {
709 Double_t vert[3]={0};
711 ((AliVVertex*)fAODVertex->At(0))->GetXYZ(vert);
713 const Int_t Nclus = clusters->GetEntries();
714 for (Int_t iClus = 0; iClus < Nclus; ++iClus) {
715 AliVCluster *c = static_cast<AliVCluster*>(clusters->At(iClus));
723 c->GetMomentum(nP, vert);
725 if (nP.Pt() < fJetConstituentMinPt)
728 fjw.AddInputVector(nP.Px(), nP.Py(), nP.Pz(), nP.P(), -iClus - 100);
735 std::vector<fastjet::PseudoJet> jets_incl = fjw.GetInclusiveJets();
736 AliDebug(1,Form("%d jets found", (Int_t)jets_incl.size()));
740 Int_t njets = jets_incl.size();
743 //std::vector<fastjet::PseudoJet> jets_incl_sorted = fastjet::sorted_by_pt(jets_incl);
744 for (Int_t i = 0; i < njets; i++) {
745 if (jet.Pt() >= jets_incl[i].perp())
747 if (jets_incl[i].eta() < fJetMinEta || jets_incl[i].eta() > fJetMaxEta || jets_incl[i].phi() < fJetMinPhi || jets_incl[i].phi() > fJetMaxPhi)
749 jet.SetPxPyPzE(jets_incl[i].px(), jets_incl[i].py(), jets_incl[i].pz(), jets_incl[i].E());