Logo ROOT   6.07/09
Reference Guide
pythiaExample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_pythia
3 /// Using Pythia6 with ROOT
4 ///
5 /// To make an event sample (of size 100) do
6 ///
7 /// ~~~{.cpp}
8 /// shell> root
9 /// root [0] .L pythiaExample.C
10 /// root [1] makeEventSample(1000)
11 /// ~~~
12 ///
13 /// To start the tree view on the generated tree, do
14 ///
15 /// ~~~{.cpp}
16 /// shell> root
17 /// root [0] .L pythiaExample.C
18 /// root [1] showEventSample()
19 /// ~~~
20 ///
21 /// The following session:
22 ///
23 /// ~~~{.cpp}
24 /// shell> root
25 /// root [0] .x pythiaExample.C(500)
26 /// ~~~
27 ///
28 /// will execute makeEventSample(500) and showEventSample()
29 ///
30 /// Alternatively, you can compile this to a program
31 /// and then generate 1000 events with
32 ///
33 /// ~~~{.cpp}
34 /// ./pythiaExample 1000
35 /// ~~~
36 ///
37 /// To use the program to start the viewer, do
38 ///
39 /// ~~~{.cpp}
40 /// ./pythiaExample -1
41 /// ~~~
42 ///
43 /// NOTE 1: To run this example, you must have a version of ROOT
44 /// compiled with the Pythia6 version enabled and have Pythia6 installed.
45 /// The statement `gSystem->Load("$HOME/pythia6/libPythia6");` (see below)
46 /// assumes that the directory containing the Pythia6 library
47 /// is in the pythia6 subdirectory of your $HOME. Locations
48 /// that can specify this, are:
49 ///
50 /// ~~~{.cpp}
51 /// Root.DynamicPath resource in your ROOT configuration file
52 /// (/etc/root/system.rootrc or ~/.rootrc).
53 /// Runtime load paths set on the executable (Using GNU ld,
54 /// specified with flag `-rpath').
55 /// Dynamic loader search path as specified in the loaders
56 /// configuration file (On GNU/Linux this file is
57 /// etc/ld.so.conf).
58 /// For Un*x: Any directory mentioned in LD_LIBRARY_PATH
59 /// For Windows: Any directory mentioned in PATH
60 /// ~~~
61 ///
62 /// NOTE 2: The example can also be run with ACLIC:
63 ///
64 /// ~~~{.cpp}
65 /// root > gSystem->Load("libEG");
66 /// root > gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
67 /// root > gSystem->Load("libEGPythia6");
68 /// root > .x pythiaExample.C+
69 /// ~~~
70 ///
71 /// \macro_code
72 ///
73 /// \author Christian Holm Christensen
74 
75 #include "TApplication.h"
76 #include "TPythia6.h"
77 #include "TFile.h"
78 #include "TError.h"
79 #include "TTree.h"
80 #include "TClonesArray.h"
81 #include "TH1.h"
82 #include "TF1.h"
83 #include "TStyle.h"
84 #include "TLatex.h"
85 #include "TCanvas.h"
86 #include "Riostream.h"
87 #include <cstdlib>
88 using namespace std;
89 
90 
91 #define FILENAME "pythia.root"
92 #define TREENAME "tree"
93 #define BRANCHNAME "particles"
94 #define HISTNAME "ptSpectra"
95 #define PDGNUMBER 211
96 
97 // This function just load the needed libraries if we're executing from
98 // an interactive session.
99 void loadLibraries()
100 {
101 #ifdef __CINT__
102  // Load the Event Generator abstraction library, Pythia 6
103  // library, and the Pythia 6 interface library.
104  gSystem->Load("libEG");
105  gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
106  gSystem->Load("libEGPythia6");
107 #endif
108 }
109 
110 // nEvents is how many events we want.
111 int makeEventSample(Int_t nEvents)
112 {
113  // Load needed libraries
114  loadLibraries();
115 
116  // Create an instance of the Pythia event generator ...
117  TPythia6* pythia = new TPythia6;
118 
119  // ... and initialise it to run p+p at sqrt(200) GeV in CMS
120  pythia->Initialize("cms", "p", "p", 200);
121 
122  // Open an output file
123  TFile* file = TFile::Open(FILENAME, "RECREATE");
124  if (!file || !file->IsOpen()) {
125  Error("makeEventSample", "Couldn;t open file %s", FILENAME);
126  return 1;
127  }
128 
129  // Make a tree in that file ...
130  TTree* tree = new TTree(TREENAME, "Pythia 6 tree");
131 
132  // ... and register a the cache of pythia on a branch (It's a
133  // TClonesArray of TMCParticle objects. )
134  TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
135  tree->Branch(BRANCHNAME, &particles);
136 
137  // Now we make some events
138  for (Int_t i = 0; i < nEvents; i++) {
139  // Show how far we got every 100'th event.
140  if (i % 100 == 0)
141  cout << "Event # " << i << endl;
142 
143  // Make one event.
144  pythia->GenerateEvent();
145 
146  // Maybe you want to have another branch with global event
147  // information. In that case, you should process that here.
148  // You can also filter out particles here if you want.
149 
150  // Now we're ready to fill the tree, and the event is over.
151  tree->Fill();
152  }
153 
154  // Show tree structure
155  tree->Print();
156 
157  // After the run is over, we may want to do some summary plots:
158  TH1D* hist = new TH1D(HISTNAME, "p_{#perp} spectrum for #pi^{+}",
159  100, 0, 3);
160  hist->SetXTitle("p_{#perp}");
161  hist->SetYTitle("dN/dp_{#perp}");
162  char expression[64];
163  sprintf(expression,"sqrt(pow(%s.fPx,2)+pow(%s.fPy,2))>>%s",
164  BRANCHNAME, BRANCHNAME, HISTNAME);
165  char selection[64];
166  sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
167  tree->Draw(expression,selection);
168 
169  // Normalise to the number of events, and the bin sizes.
170  hist->Sumw2();
171  hist->Scale(3 / 100. / hist->Integral());
172  hist->Fit("expo", "QO+", "", .25, 1.75);
173  TF1* func = hist->GetFunction("expo");
174  func->SetParNames("A", "- 1 / T");
175  // and now we flush and close the file
176  file->Write();
177  file->Close();
178 
179  return 0;
180 }
181 
182 // Show the Pt spectra, and start the tree viewer.
183 int showEventSample()
184 {
185  // Load needed libraries
186  loadLibraries();
187 
188  // Open the file
189  TFile* file = TFile::Open(FILENAME, "READ");
190  if (!file || !file->IsOpen()) {
191  Error("showEventSample", "Couldn;t open file %s", FILENAME);
192  return 1;
193  }
194 
195  // Get the tree
196  TTree* tree = (TTree*)file->Get(TREENAME);
197  if (!tree) {
198  Error("showEventSample", "couldn't get TTree %s", TREENAME);
199  return 2;
200  }
201 
202  // Start the viewer.
203  tree->StartViewer();
204 
205  // Get the histogram
206  TH1D* hist = (TH1D*)file->Get(HISTNAME);
207  if (!hist) {
208  Error("showEventSample", "couldn't get TH1D %s", HISTNAME);
209  return 4;
210  }
211 
212  // Draw the histogram in a canvas
213  gStyle->SetOptStat(1);
214  TCanvas* canvas = new TCanvas("canvas", "canvas");
215  canvas->SetLogy();
216  hist->Draw("e1");
217  TF1* func = hist->GetFunction("expo");
218 
219  char expression[64];
220  sprintf(expression,"T #approx %5.1f", -1000 / func->GetParameter(1));
221  TLatex* latex = new TLatex(1.5, 1e-4, expression);
222  latex->SetTextSize(.1);
223  latex->SetTextColor(4);
224  latex->Draw();
225 
226  return 0;
227 }
228 
229 void pythiaExample(Int_t n=1000) {
230  makeEventSample(n);
231  showEventSample();
232 }
233 
234 #ifndef __CINT__
235 int main(int argc, char** argv)
236 {
237  TApplication app("app", &argc, argv);
238 
239  Int_t n = 100;
240  if (argc > 1)
241  n = strtol(argv[1], NULL, 0);
242 
243  int retVal = 0;
244  if (n > 0)
245  retVal = makeEventSample(n);
246  else {
247  retVal = showEventSample();
248  app.Run();
249  }
250 
251  return retVal;
252 }
253 #endif
254 
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5893
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:3182
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1818
int Int_t
Definition: RtypesCore.h:41
virtual void SetYTitle(const char *title)
Definition: TH1.h:414
void GenerateEvent()
generate event and copy the information from /HEPEVT/ to fPrimaries
Definition: TPythia6.cxx:297
STL namespace.
TPythia is an interface class to F77 version of Pythia 6.2.
Definition: TPythia6.h:90
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3871
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8694
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6689
virtual Bool_t IsOpen() const
Returns kTRUE in case file is open and kFALSE if file is not open.
Definition: TFile.cxx:1379
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
To draw Mathematical Formula.
Definition: TLatex.h:25
void Error(const char *location, const char *msgfmt,...)
const int nEvents
Definition: testRooFit.cxx:42
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7081
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2853
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2268
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
The Canvas class.
Definition: TCanvas.h:41
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:366
double func(double *x, double *p)
Definition: stressTF1.cxx:213
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:359
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1651
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void SetXTitle(const char *title)
Definition: TH1.h:413
Definition: file.py:1
1-Dim function class
Definition: TF1.h:149
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8087
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:1257
#define NULL
Definition: Rtypes.h:82
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:301
virtual TObjArray * GetListOfParticles() const
Definition: TGenerator.h:178
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
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:3565
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8118
const Int_t n
Definition: legend1.C:16
int main(int argc, char **argv)
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:904
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5347