Logo ROOT  
Reference Guide
pythiaExample.C File Reference

Detailed Description

Using Pythia6 with ROOT.

To make an event sample (of size 100) do

shell> root
root [0] .L pythiaExample.C
root [1] makeEventSample(1000)

To start the tree view on the generated tree, do

shell> root
root [0] .L pythiaExample.C
root [1] showEventSample()

The following session:

shell> root
root [0] .x pythiaExample.C(500)

will execute makeEventSample(500) and showEventSample()

Alternatively, you can compile this to a program and then generate 1000 events with

./pythiaExample 1000

To use the program to start the viewer, do

./pythiaExample -1

NOTE 1: To run this example, you must have a version of ROOT compiled with the Pythia6 version enabled and have Pythia6 installed. The statement gSystem->Load("$HOME/pythia6/libPythia6"); (see below) assumes that the directory containing the Pythia6 library is in the pythia6 subdirectory of your $HOME. Locations that can specify this, are:

Root.DynamicPath resource in your ROOT configuration file
(/etc/root/system.rootrc or ~/.rootrc).
Runtime load paths set on the executable (Using GNU ld,
specified with flag `-rpath').
Dynamic loader search path as specified in the loaders
configuration file (On GNU/Linux this file is
For Un*x: Any directory mentioned in LD_LIBRARY_PATH
For Windows: Any directory mentioned in PATH
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: file.py:1
const char * Root
Definition: TXMLSetup.cxx:47

NOTE 2: The example can also be run with ACLIC:

root > gSystem->Load("libEG");
root > gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
root > gSystem->Load("libEGPythia6");
root > .x pythiaExample.C+
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1855
#include "TApplication.h"
#include "TPythia6.h"
#include "TFile.h"
#include "TError.h"
#include "TTree.h"
#include "TClonesArray.h"
#include "TH1.h"
#include "TF1.h"
#include "TStyle.h"
#include "TLatex.h"
#include "TCanvas.h"
#include "Riostream.h"
#include <cstdlib>
using namespace std;
#define FILENAME "pythia.root"
#define TREENAME "tree"
#define BRANCHNAME "particles"
#define HISTNAME "ptSpectra"
#define PDGNUMBER 211
// This function just load the needed libraries if we're executing from
// an interactive session.
void loadLibraries()
#ifdef R__MACOSX
// nEvents is how many events we want.
int makeEventSample(Int_t nEvents)
// Load needed libraries
// Create an instance of the Pythia event generator ...
TPythia6* pythia = new TPythia6;
// ... and initialise it to run p+p at sqrt(200) GeV in CMS
pythia->Initialize("cms", "p", "p", 200);
// Open an output file
TFile* file = TFile::Open(FILENAME, "RECREATE");
if (!file || !file->IsOpen()) {
Error("makeEventSample", "Couldn;t open file %s", FILENAME);
return 1;
// Make a tree in that file ...
TTree* tree = new TTree(TREENAME, "Pythia 6 tree");
// ... and register a the cache of pythia on a branch (It's a
// TClonesArray of TMCParticle objects. )
TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
tree->Branch(BRANCHNAME, &particles);
// Now we make some events
for (Int_t i = 0; i < nEvents; i++) {
// Show how far we got every 100'th event.
if (i % 100 == 0)
cout << "Event # " << i << endl;
// Make one event.
// Maybe you want to have another branch with global event
// information. In that case, you should process that here.
// You can also filter out particles here if you want.
// Now we're ready to fill the tree, and the event is over.
// Show tree structure
// After the run is over, we may want to do some summary plots:
TH1D* hist = new TH1D(HISTNAME, "p_{#perp} spectrum for #pi^{+}",
100, 0, 3);
char expression[64];
char selection[64];
sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
// Normalise to the number of events, and the bin sizes.
hist->Scale(3 / 100. / hist->Integral());
hist->Fit("expo", "QO+", "", .25, 1.75);
TF1* func = hist->GetFunction("expo");
func->SetParNames("A", "-1/T");
// and now we flush and close the file
return 0;
// Show the Pt spectra, and start the tree viewer.
int showEventSample()
// Load needed libraries
// Open the file
TFile* file = TFile::Open(FILENAME, "READ");
if (!file || !file->IsOpen()) {
Error("showEventSample", "Couldn;t open file %s", FILENAME);
return 1;
// Get the tree
TTree* tree = (TTree*)file->Get(TREENAME);
if (!tree) {
Error("showEventSample", "couldn't get TTree %s", TREENAME);
return 2;
// Start the viewer.
// Get the histogram
TH1D* hist = (TH1D*)file->Get(HISTNAME);
if (!hist) {
Error("showEventSample", "couldn't get TH1D %s", HISTNAME);
return 4;
// Draw the histogram in a canvas
TCanvas* canvas = new TCanvas("canvas", "canvas");
TF1* func = hist->GetFunction("expo");
char expression[64];
sprintf(expression,"T #approx %5.1f", -1000 / func->GetParameter(1));
TLatex* latex = new TLatex(1.5, 1e-4, expression);
return 0;
void pythiaExample(Int_t n=1000) {
#ifndef __CINT__
int main(int argc, char** argv)
TApplication app("app", &argc, argv);
Int_t n = 100;
if (argc > 1)
n = strtol(argv[1], NULL, 0);
int retVal = 0;
if (n > 0)
retVal = makeEventSample(n);
else {
retVal = showEventSample();
return retVal;
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:45
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
int main(int argc, char *argv[])
Definition: cef_main.cxx:54
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
The Canvas class.
Definition: TCanvas.h:23
An array of clone (identical) objects.
Definition: TClonesArray.h:29
1-Dim function class
Definition: TF1.h:213
virtual void SetParNames(const char *name0="p0", const char *name1="p1", const char *name2="p2", const char *name3="p3", const char *name4="p4", const char *name5="p5", const char *name6="p6", const char *name7="p7", const char *name8="p8", const char *name9="p9", const char *name10="p10")
Set up to 10 parameter names.
Definition: TF1.cxx:3482
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:512
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:4011
virtual TObjArray * GetListOfParticles() const
Definition: TGenerator.h:176
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
virtual void SetXTitle(const char *title)
Definition: TH1.h:413
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3893
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7829
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8894
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3074
virtual void SetYTitle(const char *title)
Definition: TH1.h:414
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6566
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8863
To draw Mathematical Formula.
Definition: TLatex.h:18
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
void SetLogy(Int_t value=1) override
Set Lin/Log scale for Y.
Definition: TPad.cxx:5947
TPythia is an interface class to F77 version of Pythia 6.2
Definition: TPythia6.h:84
void Initialize(const char *frame, const char *beam, const char *target, float win)
Calls PyInit with the same parameters after performing some checking, sets correct title.
Definition: TPythia6.cxx:437
void GenerateEvent()
generate event and copy the information from /HEPEVT/ to fPrimaries
Definition: TPythia6.cxx:298
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1589
A TTree represents a columnar dataset.
Definition: TTree.h:79
const Int_t n
Definition: legend1.C:16
Definition: tree.py:1
Christian Holm Christensen

Definition in file pythiaExample.C.