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
etc/ld.so.conf).
For Un*x: Any directory mentioned in LD_LIBRARY_PATH
For Windows: Any directory mentioned in PATH

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+
#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
gSystem->Load("libpythia6_dummy");
#endif
}
// nEvents is how many events we want.
int makeEventSample(Int_t nEvents)
{
// Load needed libraries
loadLibraries();
// 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.
pythia->GenerateEvent();
// 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.
tree->Fill();
}
// Show tree structure
tree->Print();
// 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);
hist->SetXTitle("p_{#perp}");
hist->SetYTitle("dN/dp_{#perp}");
char expression[64];
sprintf(expression,"sqrt(pow(%s.fPx,2)+pow(%s.fPy,2))>>%s",
BRANCHNAME, BRANCHNAME, HISTNAME);
char selection[64];
sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
tree->Draw(expression,selection);
// Normalise to the number of events, and the bin sizes.
hist->Sumw2();
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
file->Write();
file->Close();
return 0;
}
// Show the Pt spectra, and start the tree viewer.
int showEventSample()
{
// Load needed libraries
loadLibraries();
// 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.
tree->StartViewer();
// 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");
canvas->SetLogy();
hist->Draw("e1");
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);
latex->SetTextSize(.1);
latex->SetTextColor(4);
latex->Draw();
return 0;
}
void pythiaExample(Int_t n=1000) {
makeEventSample(n);
showEventSample();
}
#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();
app.Run();
}
return retVal;
}
#endif
Author
Christian Holm Christensen

Definition in file pythiaExample.C.

n
const Int_t n
Definition: legend1.C:16
TPythia6::GenerateEvent
void GenerateEvent()
generate event and copy the information from /HEPEVT/ to fPrimaries
Definition: TPythia6.cxx:298
TH1::GetFunction
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8506
e
#define e(i)
Definition: RSha256.hxx:121
tree
Definition: tree.py:1
TF1::SetParNames
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
TTree
Definition: TTree.h:79
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
TStyle.h
TFile::Open
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:3946
TLatex
Definition: TLatex.h:18
TCanvas.h
TSystem::Load
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1852
TTree.h
TH1::SetXTitle
virtual void SetXTitle(const char *title)
Definition: TH1.h:410
TH1::SetYTitle
virtual void SetYTitle(const char *title)
Definition: TH1.h:411
TFile.h
TClonesArray.h
xmlio::Root
const char * Root
Definition: TXMLSetup.cxx:53
TApplication.h
TPad::SetLogy
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5905
TPythia6::Initialize
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
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
main
int main(int argc, char **argv)
Definition: histspeedtest.cxx:751
TGenerator::GetListOfParticles
virtual TObjArray * GetListOfParticles() const
Definition: TGenerator.h:176
TStyle::SetOptStat
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:1592
TFile
Definition: TFile.h:54
TPythia6.h
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
TPythia6
Definition: TPythia6.h:84
TF1.h
TCanvas
Definition: TCanvas.h:23
TLatex.h
file
Definition: file.py:1
TH1::Sumw2
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8475
TH1::Fit
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:3807
TApplication
Definition: TApplication.h:39
TF1
1-Dim function class
Definition: TF1.h:212
TF1::GetParameter
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:508
TH1::Integral
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7449
Riostream.h
TH1.h
TClonesArray
Definition: TClonesArray.h:29
ROOT
VSD Structures.
Definition: StringConv.hxx:21
TH1::Scale
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6245
int
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
TError.h