Loading [MathJax]/extensions/tex2jax.js
Logo ROOT  
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
Definition: file.py:1
const char * Root
Definition: TXMLSetup.cxx:46

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:556
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1850
#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
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:43
void Error(const char *location, const char *msgfmt,...)
R__EXTERN TStyle * gStyle
Definition: TStyle.h:410
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:27
An array of clone (identical) objects.
Definition: TClonesArray.h:32
1-Dim function class
Definition: TF1.h:210
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:3464
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:506
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
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:3942
virtual TObjArray * GetListOfParticles() const
Definition: TGenerator.h:176
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
virtual void SetXTitle(const char *title)
Definition: TH1.h:409
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:3808
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7450
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8507
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
virtual void SetYTitle(const char *title)
Definition: TH1.h:410
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6246
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8476
To draw Mathematical Formula.
Definition: TLatex.h:18
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:195
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5904
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:436
void GenerateEvent()
generate event and copy the information from /HEPEVT/ to fPrimaries
Definition: TPythia6.cxx:297
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:1590
A TTree represents a columnar dataset.
Definition: TTree.h:78
int main(int argc, char **argv)
const Int_t n
Definition: legend1.C:16
Definition: tree.py:1
Author
Christian Holm Christensen

Definition in file pythiaExample.C.