#include "TChain.h" #include "TTree.h" #include "TH1F.h" #include "TCanvas.h" #include "AliAnalysisTask.h" #include "AliAnalysisManager.h" #include "AliESDEvent.h" #include "AliESDInputHandler.h" #include "AliMCEventHandler.h" #include "AliMCEvent.h" #include "AliAnalysisTaskPtMC.h" // example of an analysis task creating a p_t spectrum // Authors: Panos Cristakoglou, Jan Fiete Grosse-Oetringhaus, Christian Klein-Boesing ClassImp(AliAnalysisTaskPtMC) //________________________________________________________________________ AliAnalysisTaskPtMC::AliAnalysisTaskPtMC(const char *name) : AliAnalysisTask(name, ""), fESD(0), fHistPt(0) { // Constructor // Define input and output slots here // Input slot #0 works with a TChain DefineInput(0, TChain::Class()); // Output slot #0 writes into a TH1 container DefineOutput(0, TH1F::Class()); } //________________________________________________________________________ void AliAnalysisTaskPtMC::ConnectInputData(Option_t *) { // Connect ESD or AOD here // Called once TTree* tree = dynamic_cast (GetInputData(0)); if (!tree) { Printf("ERROR: Could not read chain from input slot 0"); } else { // Disable all branches, we want to process only MC tree->SetBranchStatus("*", kFALSE); AliESDInputHandler *esdH = dynamic_cast (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()); if (!esdH) { Printf("ERROR: Could not get ESDInputHandler"); } else fESD = esdH->GetEvent(); } } //________________________________________________________________________ void AliAnalysisTaskPtMC::CreateOutputObjects() { // Create histograms // Called once fHistPt = new TH1F("fHistPt", "P_{T} distribution", 15, 0.1, 3.1); fHistPt->GetXaxis()->SetTitle("P_{T} (GeV/c)"); fHistPt->GetYaxis()->SetTitle("dN/dP_{T} (c/GeV)"); fHistPt->SetMarkerStyle(kFullCircle); } //________________________________________________________________________ void AliAnalysisTaskPtMC::Exec(Option_t *) { // Main loop // Called for each event // Process MC truth, therefore we receive the AliAnalysisManager and ask it for the AliMCEventHandler // This handler can return the current MC event AliMCEventHandler* eventHandler = dynamic_cast (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler()); if (!eventHandler) { Printf("ERROR: Could not retrieve MC event handler"); return; } AliMCEvent* mcEvent = eventHandler->MCEvent(); if (!mcEvent) { Printf("ERROR: Could not retrieve MC event"); return; } Printf("MC particles: %d", mcEvent->GetNumberOfTracks()); for (Int_t iTracks = 0; iTracks < mcEvent->GetNumberOfTracks(); iTracks++) { AliMCParticle* track = mcEvent->GetTrack(iTracks); if (!track) { Printf("ERROR: Could not receive track %d (mc loop)", iTracks); continue; } fHistPt->Fill(track->Pt()); } //track loop // Post output data. PostData(0, fHistPt); } //________________________________________________________________________ void AliAnalysisTaskPtMC::Terminate(Option_t *) { // Draw result to the screen // Called once at the end of the query fHistPt = dynamic_cast (GetOutputData(0)); if (!fHistPt) { Printf("ERROR: fHistPt not available"); return; } TCanvas *c1 = new TCanvas("AliAnalysisTaskPtMC","Pt MC",10,10,510,510); c1->cd(1)->SetLogy(); fHistPt->DrawCopy("E"); }