ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifndef __CINT__
76 #include "TApplication.h"
77 #include "TPythia6.h"
78 #include "TFile.h"
79 #include "TError.h"
80 #include "TTree.h"
81 #include "TClonesArray.h"
82 #include "TH1.h"
83 #include "TF1.h"
84 #include "TStyle.h"
85 #include "TLatex.h"
86 #include "TCanvas.h"
87 #include "Riostream.h"
88 #include <cstdlib>
89 using namespace std;
90 #endif
91 
92 #define FILENAME "pythia.root"
93 #define TREENAME "tree"
94 #define BRANCHNAME "particles"
95 #define HISTNAME "ptSpectra"
96 #define PDGNUMBER 211
97 
98 // This function just load the needed libraries if we're executing from
99 // an interactive session.
100 void loadLibraries()
101 {
102 #ifdef __CINT__
103  // Load the Event Generator abstraction library, Pythia 6
104  // library, and the Pythia 6 interface library.
105  gSystem->Load("libEG");
106  gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
107  gSystem->Load("libEGPythia6");
108 #endif
109 }
110 
111 // nEvents is how many events we want.
112 int makeEventSample(Int_t nEvents)
113 {
114  // Load needed libraries
115  loadLibraries();
116 
117  // Create an instance of the Pythia event generator ...
118  TPythia6* pythia = new TPythia6;
119 
120  // ... and initialise it to run p+p at sqrt(200) GeV in CMS
121  pythia->Initialize("cms", "p", "p", 200);
122 
123  // Open an output file
124  TFile* file = TFile::Open(FILENAME, "RECREATE");
125  if (!file || !file->IsOpen()) {
126  Error("makeEventSample", "Couldn;t open file %s", FILENAME);
127  return 1;
128  }
129 
130  // Make a tree in that file ...
131  TTree* tree = new TTree(TREENAME, "Pythia 6 tree");
132 
133  // ... and register a the cache of pythia on a branch (It's a
134  // TClonesArray of TMCParticle objects. )
135  TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
136  tree->Branch(BRANCHNAME, &particles);
137 
138  // Now we make some events
139  for (Int_t i = 0; i < nEvents; i++) {
140  // Show how far we got every 100'th event.
141  if (i % 100 == 0)
142  cout << "Event # " << i << endl;
143 
144  // Make one event.
145  pythia->GenerateEvent();
146 
147  // Maybe you want to have another branch with global event
148  // information. In that case, you should process that here.
149  // You can also filter out particles here if you want.
150 
151  // Now we're ready to fill the tree, and the event is over.
152  tree->Fill();
153  }
154 
155  // Show tree structure
156  tree->Print();
157 
158  // After the run is over, we may want to do some summary plots:
159  TH1D* hist = new TH1D(HISTNAME, "p_{#perp} spectrum for #pi^{+}",
160  100, 0, 3);
161  hist->SetXTitle("p_{#perp}");
162  hist->SetYTitle("dN/dp_{#perp}");
163  char expression[64];
164  sprintf(expression,"sqrt(pow(%s.fPx,2)+pow(%s.fPy,2))>>%s",
165  BRANCHNAME, BRANCHNAME, HISTNAME);
166  char selection[64];
167  sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
168  tree->Draw(expression,selection);
169 
170  // Normalise to the number of events, and the bin sizes.
171  hist->Sumw2();
172  hist->Scale(3 / 100. / hist->Integral());
173  hist->Fit("expo", "QO+", "", .25, 1.75);
174  TF1* func = hist->GetFunction("expo");
175  func->SetParNames("A", "- 1 / T");
176  // and now we flush and close the file
177  file->Write();
178  file->Close();
179 
180  return 0;
181 }
182 
183 // Show the Pt spectra, and start the tree viewer.
184 int showEventSample()
185 {
186  // Load needed libraries
187  loadLibraries();
188 
189  // Open the file
190  TFile* file = TFile::Open(FILENAME, "READ");
191  if (!file || !file->IsOpen()) {
192  Error("showEventSample", "Couldn;t open file %s", FILENAME);
193  return 1;
194  }
195 
196  // Get the tree
197  TTree* tree = (TTree*)file->Get(TREENAME);
198  if (!tree) {
199  Error("showEventSample", "couldn't get TTree %s", TREENAME);
200  return 2;
201  }
202 
203  // Start the viewer.
204  tree->StartViewer();
205 
206  // Get the histogram
207  TH1D* hist = (TH1D*)file->Get(HISTNAME);
208  if (!hist) {
209  Error("showEventSample", "couldn't get TH1D %s", HISTNAME);
210  return 4;
211  }
212 
213  // Draw the histogram in a canvas
214  gStyle->SetOptStat(1);
215  TCanvas* canvas = new TCanvas("canvas", "canvas");
216  canvas->SetLogy();
217  hist->Draw("e1");
218  TF1* func = hist->GetFunction("expo");
219 
220  char expression[64];
221  sprintf(expression,"T #approx %5.1f", -1000 / func->GetParameter(1));
222  TLatex* latex = new TLatex(1.5, 1e-4, expression);
223  latex->SetTextSize(.1);
224  latex->SetTextColor(4);
225  latex->Draw();
226 
227  return 0;
228 }
229 
230 void pythiaExample(Int_t n=1000) {
231  makeEventSample(n);
232  showEventSample();
233 }
234 
235 #ifndef __CINT__
236 int main(int argc, char** argv)
237 {
238  TApplication app("app", &argc, argv);
239 
240  Int_t n = 100;
241  if (argc > 1)
242  n = strtol(argv[1], NULL, 0);
243 
244  int retVal = 0;
245  if (n > 0)
246  retVal = makeEventSample(n);
247  else {
248  retVal = showEventSample();
249  app.Run();
250  }
251 
252  return retVal;
253 }
254 #endif
255 
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6174
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
int Int_t
Definition: RtypesCore.h:41
virtual void SetYTitle(const char *title)
Definition: TH1.h:409
void GenerateEvent()
generate event and copy the information from /HEPEVT/ to fPrimaries
Definition: TPythia6.cxx:297
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:3851
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8473
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
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:33
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:7378
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
tuple main
Definition: hsum.py:20
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
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:1623
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void SetXTitle(const char *title)
Definition: TH1.h:408
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8350
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:1252
#define NULL
Definition: Rtypes.h:82
virtual TObjArray * GetListOfParticles() const
Definition: TGenerator.h:178
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:3607
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8382