Logo ROOT   6.07/09
Reference Guide
pythia8.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_pythia
3 /// pythia8 basic example
4 ///
5 /// to run, do:
6 ///
7 /// ~~~{.cpp}
8 /// root > .x pythia8.C
9 /// ~~~
10 ///
11 /// Note that before executing this script,
12 ///
13 /// - the env variable PYTHIA8 must point to the pythia8100 (or newer) directory
14 /// - the env variable PYTHIA8DATA must be defined and it must point to $PYTHIA8/xmldoc
15 ///
16 /// \macro_code
17 ///
18 /// \author Andreas Morsch
19 
20 void pythia8(Int_t nev = 100, Int_t ndeb = 1)
21 {
22  const char *p8dataenv = gSystem->Getenv("PYTHIA8DATA");
23  if (!p8dataenv) {
24  const char *p8env = gSystem->Getenv("PYTHIA8");
25  if (!p8env) {
26  Error("pythia8.C",
27  "Environment variable PYTHIA8 must contain path to pythia directory!");
28  return;
29  }
30  TString p8d = p8env;
31  p8d += "/xmldoc";
32  gSystem->Setenv("PYTHIA8DATA", p8d);
33  }
34 
35  const char* path = gSystem->ExpandPathName("$PYTHIA8DATA");
36  if (gSystem->AccessPathName(path)) {
37  Error("pythia8.C",
38  "Environment variable PYTHIA8DATA must contain path to $PYTHIA8/xmldoc directory !");
39  return;
40  }
41 
42 // Load libraries
43 #ifndef G__WIN32 // Pythia8 is a static library on Windows
44  if (gSystem->Getenv("PYTHIA8")) {
45  gSystem->Load("$PYTHIA8/lib/libpythia8");
46  } else {
47  gSystem->Load("libpythia8");
48  }
49 #endif
50  gSystem->Load("libEG");
51  gSystem->Load("libEGPythia8");
52 // Histograms
53  TH1F* etaH = new TH1F("etaH", "Pseudorapidity", 120, -12., 12.);
54  TH1F* ptH = new TH1F("ptH", "pt", 50, 0., 10.);
55 
56 
57 // Array of particles
58  TClonesArray* particles = new TClonesArray("TParticle", 1000);
59 // Create pythia8 object
60  TPythia8* pythia8 = new TPythia8();
61 
62 // Configure
63  pythia8->ReadString("HardQCD:all = on");
64 
65 
66 // Initialize
67 
68  pythia8->Initialize(2212 /* p */, 2212 /* p */, 14000. /* TeV */);
69 
70 // Event loop
71  for (Int_t iev = 0; iev < nev; iev++) {
72  pythia8->GenerateEvent();
73  if (iev < ndeb) pythia8->EventListing();
74  pythia8->ImportParticles(particles,"All");
75  Int_t np = particles->GetEntriesFast();
76 // Particle loop
77  for (Int_t ip = 0; ip < np; ip++) {
78  TParticle* part = (TParticle*) particles->At(ip);
79  Int_t ist = part->GetStatusCode();
80  // Positive codes are final particles.
81  if (ist <= 0) continue;
82  Int_t pdg = part->GetPdgCode();
84  if (charge == 0.) continue;
85  Float_t eta = part->Eta();
86  Float_t pt = part->Pt();
87 
88  etaH->Fill(eta);
89  if (pt > 0.) ptH->Fill(pt, 1./(2. * pt));
90  }
91  }
92 
93  pythia8->PrintStatistics();
94 
95  TCanvas* c1 = new TCanvas("c1","Pythia8 test example",800,800);
96  c1->Divide(1, 2);
97  c1->cd(1);
98  etaH->Scale(5./Float_t(nev));
99  etaH->Draw();
100  etaH->SetXTitle("#eta");
101  etaH->SetYTitle("dN/d#eta");
102 
103  c1->cd(2);
104  gPad->SetLogy();
105  ptH->Scale(5./Float_t(nev));
106  ptH->Draw();
107  ptH->SetXTitle("p_{t} [GeV/c]");
108  ptH->SetYTitle("dN/dp_{t}^{2} [GeV/c]^{-2}");
109  }
void PrintStatistics() const
Print end of run statistics.
Definition: TPythia8.cxx:355
Double_t Charge() const
Definition: TParticlePDG.h:72
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
Double_t Pt() const
Definition: TParticle.h:122
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5893
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3127
virtual Int_t ImportParticles(TClonesArray *particles, Option_t *option="")
Import particles from Pythia stack.
Definition: TPythia8.cxx:193
float Float_t
Definition: RtypesCore.h:53
Double_t Eta() const
Definition: TParticle.h:124
return c1
Definition: legend1.C:41
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
Description of the dynamic properties of a particle.
Definition: TParticle.h:34
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
TParticlePDG * GetParticle(Int_t pdgCode) const
Get a pointer to the particle object according to the MC code number.
Bool_t Initialize(Int_t idAin, Int_t idBin, Double_t ecms)
Initialization.
Definition: TPythia8.cxx:146
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1818
Basic string class.
Definition: TString.h:137
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
virtual void SetYTitle(const char *title)
Definition: TH1.h:414
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
static TDatabasePDG * Instance()
static function
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1627
void Error(const char *location, const char *msgfmt,...)
virtual void Setenv(const char *name, const char *value)
Set environment variable.
Definition: TSystem.cxx:1611
TPaveText * pt
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2853
Int_t GetStatusCode() const
Definition: TParticle.h:89
Int_t GetPdgCode() const
Definition: TParticle.h:90
The Canvas class.
Definition: TCanvas.h:41
void EventListing() const
Event listing.
Definition: TPythia8.cxx:363
TPythia8 is an interface class to C++ version of Pythia 8.1 event generators, written by T...
Definition: TPythia8.h:76
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void SetXTitle(const char *title)
Definition: TH1.h:413
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1089
#define gPad
Definition: TVirtualPad.h:289
virtual void GenerateEvent()
Generate the next event.
Definition: TPythia8.cxx:184
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1243
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
void ReadString(const char *string) const
Configuration.
Definition: TPythia8.cxx:299