Commit 4669fd83 authored by Rafael Peretti Pezzi's avatar Rafael Peretti Pezzi

Working macros for processing ALICE ESD files

parents
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
* *
* Author: The ALICE Off-line Project. *
* Contributors are mentioned in the code where appropriate. *
* *
* Permission to use, copy, modify and distribute this software and its *
* documentation strictly for non-commercial purposes is hereby granted *
* without fee, provided that the above copyright notice appears in all *
* copies and that both the copyright notice and this permission notice *
* appear in the supporting documentation. The authors make no claims *
* about the suitability of this software for any purpose. It is *
* provided "as is" without express or implied warranty. *
**************************************************************************/
/* AddMyTask
*
* empty task which can serve as a starting point for building an analysis
* as an example, one histogram is filled
*/
// Author: Redmer A. Bertens, Utrecht University, 2012
class AliAnalysisDataContainer;
AliAnalysisTaskMyTask* AddMyTask(TString name = "name")
{
// get the manager via the static access member. since it's static, you don't need
// an instance of the class to call the function
AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
if (!mgr) {
return 0x0;
}
// get the input event handler, again via a static method.
// this handler is part of the managing system and feeds events
// to your task
if (!mgr->GetInputEventHandler()) {
return 0x0;
}
// by default, a file is open for writing. here, we get the filename
TString fileName = AliAnalysisManager::GetCommonFileName();
fileName += ":MyTask"; // create a subfolder in the file
// now we create an instance of your task
AliAnalysisTaskMyTask* task = new AliAnalysisTaskMyTask(name.Data());
if(!task) return 0x0;
// add your task to the manager
mgr->AddTask(task);
// your task needs input: here we connect the manager to your task
mgr->ConnectInput(task,0,mgr->GetCommonInputContainer());
// same for the output
mgr->ConnectOutput(task,1,mgr->CreateContainer("MyOutputContainer", TList::Class(), AliAnalysisManager::kOutputContainer, fileName.Data()));
// in the end, this macro returns a pointer to your task. this will be convenient later on
// when you will run your analysis in an analysis train on grid
return task;
}
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
* *
* Author: The ALICE Off-line Project. *
* Contributors are mentioned in the code where appropriate. *
* *
* Permission to use, copy, modify and distribute this software and its *
* documentation strictly for non-commercial purposes is hereby granted *
* without fee, provided that the above copyright notice appears in all *
* copies and that both the copyright notice and this permission notice *
* appear in the supporting documentation. The authors make no claims *
* about the suitability of this software for any purpose. It is *
* provided "as is" without express or implied warranty. *
**************************************************************************/
/* AliAnaysisTaskMyTask
*
* empty task which can serve as a starting point for building an analysis
* as an example, one histogram is filled
*/
#include "TChain.h"
#include "TH1F.h"
#include "TList.h"
#include "AliAnalysisTask.h"
#include "AliAnalysisManager.h"
#include "AliESDEvent.h"
#include "AliESDInputHandler.h"
#include "AliAnalysisTaskMyTask.h"
#include "AliESDtrack.h"
//From Javier Task
#include "AliVEvent.h"
#include "TLorentzVector.h"
#include "TParticle.h"
#include "AliESDtrack.h"
#include "AliAODTrack.h"
#include "Riostream.h"
Int_t esd_event_id = 0;
class AliAnalysisTaskMyTask; // your analysis class
using namespace std; // std namespace: so you can do things like 'cout'
ClassImp(AliAnalysisTaskMyTask) // classimp: necessary for root
AliAnalysisTaskMyTask::AliAnalysisTaskMyTask() : AliAnalysisTaskSE(),
fESD(0), fOutputList(0), fHistPt(0), fHistEvents(0)
{
// default constructor, don't allocate memory here!
// this is used by root for IO purposes, it needs to remain empty
}
//_____________________________________________________________________________
AliAnalysisTaskMyTask::AliAnalysisTaskMyTask(const char* name) : AliAnalysisTaskSE(name),
fESD(0), fOutputList(0), fHistPt(0), fHistEvents(0)
{
// constructor
DefineInput(0, TChain::Class()); // define the input of the analysis: in this case we take a 'chain' of events
// this chain is created by the analysis manager, so no need to worry about it,
// it does its work automatically
DefineOutput(1, TList::Class()); // define the ouptut of the analysis: in this case it's a list of histograms
// you can add more output objects by calling DefineOutput(2, classname::Class())
// if you add more output objects, make sure to call PostData for all of them, and to
// make changes to your AddTask macro!
}
//_____________________________________________________________________________
AliAnalysisTaskMyTask::~AliAnalysisTaskMyTask()
{
// destructor
if(fOutputList) {
delete fOutputList; // at the end of your task, it is deleted from memory by calling this function
}
}
//_____________________________________________________________________________
void AliAnalysisTaskMyTask::UserCreateOutputObjects()
{
// create output objects
//
// this function is called ONCE at the start of your analysis (RUNTIME)
// here you ceate the histograms that you want to use
//
// the histograms are in this case added to a tlist, this list is in the end saved
// to an output file
//
fOutputList = new TList(); // this is a list which will contain all of your histograms
// at the end of the analysis, the contents of this list are written
// to the output file
fOutputList->SetOwner(kTRUE); // memory stuff: the list is owner of all objects it contains and will delete them
// if requested (dont worry about this now)
// example of a histogram
fHistPt = new TH1F("fHistPt", "fHistPt", 100, 0, 10); // create your histogra
fOutputList->Add(fHistPt); // don't forget to add it to the list! the list will be written to file, so if you want
// your histogram in the output file, add it to the list!
// Histograms for dimuons
fHistEvents = new TH1F("fHistEvents","fHistEvents;N_{events}",100,0.,10000.);
fOutputList->Add(fHistEvents);
PostData(1, fOutputList); // postdata will notify the analysis manager of changes / updates to the
// fOutputList object. the manager will in the end take care of writing your output to file
// so it needs to know what's in the output
}
//_____________________________________________________________________________
void AliAnalysisTaskMyTask::UserExec(Option_t *)
{
// user exec
// this function is called once for each event
// the manager will take care of reading the events from file, and with the static function InputEvent() you
// have access to the current event.
// once you return from the UserExec function, the manager will retrieve the next event from the chain
Int_t Event=0;
Int_t TrigEvent=0;
ofstream summaryfile, eventsdetail;
summaryfile.open ("events_summary.txt",std::ofstream::app);
eventsdetail.open ("events_detail.txt",std::ofstream::app);
fESD = dynamic_cast<AliESDEvent*>(InputEvent()); // get an event (called fESD) from the input file
// there's another event format (ESD) which works in a similar wya
// but is more cpu/memory unfriendly. for now, we'll stick with aod's
if(!fESD) return; // if the pointer to the event is empty (getting it failed) skip this event
// example part: i'll show how to loop over the tracks in an event
// and extract some information from them which we'll store in a histogram
Int_t iTracks(fESD->GetNumberOfTracks()); // see how many tracks there are in the event
summaryfile << " Event " << esd_event_id << ": iTracks = " << iTracks << endl;
eventsdetail << "********** Event " << esd_event_id << ": iTracks = " << iTracks << " **********" << endl;
for(Int_t i(0); i < iTracks; i++) { // loop ove rall these tracks
AliESDtrack* track = static_cast<AliESDtrack*>(fESD->GetTrack(i)); // get a track (type AliESDtrack) from the event
if(!track) continue; // if we failed, skip this track
eventsdetail << esd_event_id << " " << i << " " << track->Pt() << endl;
fHistPt->Fill(track->Pt()); // plot the pt value of the track in a histogram
}
Event++;
esd_event_id++;
fHistEvents->Fill(Event);
summaryfile.close();
eventsdetail.close();
// continue until all the tracks are processed
PostData(1, fOutputList); // stream the results the analysis of this event to
// the output manager which will take care of writing
// it to a file
}
//_____________________________________________________________________________
void AliAnalysisTaskMyTask::Terminate(Option_t *)
{
// terminate
// called at the END of the analysis (when all events are processed)
}
//_____________________________________________________________________________
/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. */
/* See cxx source for full Copyright notice */
/* $Id$ */
#ifndef AliAnalysisTaskMyTask_H
#define AliAnalysisTaskMyTask_H
#include "AliAnalysisTaskSE.h"
class AliAnalysisTaskMyTask : public AliAnalysisTaskSE
{
public:
AliAnalysisTaskMyTask();
AliAnalysisTaskMyTask(const char *name);
virtual ~AliAnalysisTaskMyTask();
virtual void UserCreateOutputObjects();
virtual void UserExec(Option_t* option);
virtual void Terminate(Option_t* option);
private:
AliESDEvent* fESD; //! input event
TList* fOutputList; //! output list
TH1F* fHistPt; //! dummy histogram
TH1F* fHistEvents; // Number of events
AliAnalysisTaskMyTask(const AliAnalysisTaskMyTask&); // not implemented
AliAnalysisTaskMyTask& operator=(const AliAnalysisTaskMyTask&); // not implemented
ClassDef(AliAnalysisTaskMyTask, 1);
};
#endif
# Sample macros for processing AliESDs from CERN Open Data Portal
Execute with aliroot (root5)
`$ aliroot runAnalysis.C`
## Credits
This example was based on works by Redmer A. Bertens (Analysis tutorial)
// Exemplo obtido de https://stackoverflow.com/questions/28970124/cern-root-exporting-data-to-plain-text
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include <fstream>
using namespace std;
void dumpTreeTotxt(){
TFile *f=new TFile("AliESDs.root"); // opens the root file
TTree *tr=(TTree*)f->Get("esdTree"); // creates the TTree object
//tr->Scan(); // prints the content on the screen
float a,b,c; // create variables of the same type as the branches you want to access
tr->SetBranchAddress("AliESDRun",&a); // for all the TTree branches you need this
// tr->SetBranchAddress("AliESDHeader",&b);
//tr->SetBranchAddress("nserr",&c);
ofstream myfile;
myfile.open ("example.txt");
//myfile << "TS ns nserr\n";
for (int i=0;i<tr->GetEntries();i++){
// loop over the tree
cout << "Event: " << i << endl; //print to the screen
myfile << "Event: " << i << endl; //write to file
tr->GetEntry(i);
}
myfile.close();
}
void runAnalysis()
{
ofstream summaryfile, eventsdetail;
summaryfile.open ("events_summary.txt");
summaryfile << " " << endl;
summaryfile.close();
eventsdetail.open ("events_detail.txt");
eventsdetail << " " << endl;
eventsdetail.close();
// since we will compile a class, tell root where to look for headers
gROOT->ProcessLine(".include $ROOTSYS/include");
gROOT->ProcessLine(".include $ALICE_ROOT/include");
// create the analysis manager
AliAnalysisManager *mgr = new AliAnalysisManager("testAnalysis");
AliESDInputHandler *esdH = new AliESDInputHandler();
mgr->SetInputEventHandler(esdH);
// compile the class (locally)
gROOT->LoadMacro("AliAnalysisTaskMyTask.cxx++g");
// load the addtask macro
gROOT->LoadMacro("AddMyTask.C");
// create an instance of your analysis task
AliAnalysisTaskMyTask *task = AddMyTask();
if(!mgr->InitAnalysis()) return;
mgr->SetDebugLevel(2);
mgr->PrintStatus();
mgr->SetUseProgressBar(1, 25);
// if you want to run locally, we need to define some input
TChain* chain = new TChain("esdTree");
// add a few files to the chain (change this so that your local files are added)
chain->Add("AliESDs.root");
//chain->Add("../root_files/AliAOD.Muons2.root");
//chain->Add("../root_files/AliAOD.Muons3.root");
//chain->Add("../root_files/AliAOD.Muons4.root");
// start the analysis locally, reading the events from the tchain
mgr->StartAnalysis("local", chain);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment