pythiaExample.C

Go to the documentation of this file.
00001 //____________________________________________________________________
00002 //
00003 // Using Pythia6 with ROOT
00004 // To make an event sample (of size 100) do
00005 //
00006 //    shell> root
00007 //    root [0] .L pythiaExample.C
00008 //    root [1] makeEventSample(1000)
00009 //
00010 // To start the tree view on the generated tree, do
00011 //
00012 //    shell> root
00013 //    root [0] .L pythiaExample.C
00014 //    root [1] showEventSample()
00015 //
00016 //
00017 // The following session:
00018 //    shell> root
00019 //    root [0] .x pythiaExample.C(500)
00020 // will execute makeEventSample(500) and showEventSample()
00021 //
00022 // Alternatively, you can compile this to a program
00023 // and then generate 1000 events with
00024 //
00025 //    ./pythiaExample 1000
00026 //
00027 // To use the program to start the viewer, do
00028 //
00029 //    ./pythiaExample -1
00030 //
00031 // NOTE 1: To run this example, you must have a version of ROOT
00032 // compiled with the Pythia6 version enabled and have Pythia6 installed.
00033 // The statement gSystem->Load("$HOME/pythia6/libPythia6");  (see below)
00034 // assumes that the directory containing the Pythia6 library
00035 // is in the pythia6 subdirectory of your $HOME.  Locations
00036 // that can specify this, are:
00037 //
00038 //  Root.DynamicPath resource in your ROOT configuration file
00039 //    (/etc/root/system.rootrc or ~/.rootrc).
00040 //  Runtime load paths set on the executable (Using GNU ld,
00041 //    specified with flag `-rpath').
00042 //  Dynamic loader search path as specified in the loaders
00043 //    configuration file (On GNU/Linux this file is
00044 //    etc/ld.so.conf).
00045 //  For Un*x: Any directory mentioned in LD_LIBRARY_PATH
00046 //  For Windows: Any directory mentioned in PATH
00047 //
00048 // NOTE 2: The example can also be run with ACLIC:
00049 //  root > gSystem->Load("libEG");
00050 //  root > gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
00051 //  root > gSystem->Load("libEGPythia6");
00052 //  root > .x pythiaExample.C+
00053 //
00054 //
00055 //____________________________________________________________________
00056 //
00057 // Author: Christian Holm Christensen <cholm@hilux15.nbi.dk>
00058 // Update: 2002-08-16 16:40:27+0200
00059 // Copyright: 2002 (C) Christian Holm Christensen
00060 // Copyright (C) 2006, Rene Brun and Fons Rademakers.
00061 // For the licensing terms see $ROOTSYS/LICENSE.
00062 //
00063 #ifndef __CINT__
00064 #include "TApplication.h"
00065 #include "TPythia6.h"
00066 #include "TFile.h"
00067 #include "TError.h"
00068 #include "TTree.h"
00069 #include "TClonesArray.h"
00070 #include "TH1.h"
00071 #include "TF1.h"
00072 #include "TStyle.h"
00073 #include "TLatex.h"
00074 #include "TCanvas.h"
00075 #include "Riostream.h"
00076 #include <cstdlib>
00077 using namespace std;
00078 #endif
00079 
00080 #define FILENAME   "pythia.root"
00081 #define TREENAME   "tree"
00082 #define BRANCHNAME "particles"
00083 #define HISTNAME   "ptSpectra"
00084 #define PDGNUMBER  211
00085 
00086 // This function just load the needed libraries if we're executing from
00087 // an interactive session.
00088 void loadLibraries()
00089 {
00090 #ifdef __CINT__
00091   // Load the Event Generator abstraction library, Pythia 6
00092   // library, and the Pythia 6 interface library.
00093   gSystem->Load("libEG");
00094   gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
00095   gSystem->Load("libEGPythia6");
00096 #endif
00097 }
00098 
00099 // nEvents is how many events we want.
00100 int makeEventSample(Int_t nEvents)
00101 {
00102   // Load needed libraries
00103   loadLibraries();
00104 
00105   // Create an instance of the Pythia event generator ...
00106   TPythia6* pythia = new TPythia6;
00107 
00108   // ... and initialise it to run p+p at sqrt(200) GeV in CMS
00109   pythia->Initialize("cms", "p", "p", 200);
00110 
00111   // Open an output file
00112   TFile* file = TFile::Open(FILENAME, "RECREATE");
00113   if (!file || !file->IsOpen()) {
00114     Error("makeEventSample", "Couldn;t open file %s", FILENAME);
00115     return 1;
00116   }
00117 
00118   // Make a tree in that file ...
00119   TTree* tree = new TTree(TREENAME, "Pythia 6 tree");
00120 
00121   // ... and register a the cache of pythia on a branch (It's a
00122   // TClonesArray of TMCParticle objects. )
00123   TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
00124   tree->Branch(BRANCHNAME, &particles);
00125 
00126   // Now we make some events
00127   for (Int_t i = 0; i < nEvents; i++) {
00128     // Show how far we got every 100'th event.
00129     if (i % 100 == 0)
00130       cout << "Event # " << i << endl;
00131 
00132     // Make one event.
00133     pythia->GenerateEvent();
00134 
00135     // Maybe you want to have another branch with global event
00136     // information.  In that case, you should process that here.
00137     // You can also filter out particles here if you want.
00138 
00139     // Now we're ready to fill the tree, and the event is over.
00140     tree->Fill();
00141   }
00142 
00143   // Show tree structure
00144   tree->Print();
00145 
00146   // After the run is over, we may want to do some summary plots:
00147   TH1D* hist = new TH1D(HISTNAME, "p_{#perp}  spectrum for  #pi^{+}",
00148                         100, 0, 3);
00149   hist->SetXTitle("p_{#perp}");
00150   hist->SetYTitle("dN/dp_{#perp}");
00151   char expression[64];
00152   sprintf(expression,"sqrt(pow(%s.fPx,2)+pow(%s.fPy,2))>>%s",
00153                   BRANCHNAME, BRANCHNAME, HISTNAME);
00154   char selection[64];
00155   sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
00156   tree->Draw(expression,selection);
00157 
00158   // Normalise to the number of events, and the bin sizes.
00159   hist->Sumw2();
00160   hist->Scale(3 / 100. / hist->Integral());
00161   hist->Fit("expo", "QO+", "", .25, 1.75);
00162   TF1* func = hist->GetFunction("expo");
00163   func->SetParNames("A", "- 1 / T");
00164   // and now we flush and close the file
00165   file->Write();
00166   file->Close();
00167 
00168   return 0;
00169 }
00170 
00171 // Show the Pt spectra, and start the tree viewer.
00172 int showEventSample()
00173 {
00174   // Load needed libraries
00175   loadLibraries();
00176 
00177   // Open the file
00178   TFile* file = TFile::Open(FILENAME, "READ");
00179   if (!file || !file->IsOpen()) {
00180     Error("showEventSample", "Couldn;t open file %s", FILENAME);
00181     return 1;
00182   }
00183 
00184   // Get the tree
00185   TTree* tree = (TTree*)file->Get(TREENAME);
00186   if (!tree) {
00187     Error("showEventSample", "couldn't get TTree %s", TREENAME);
00188     return 2;
00189   }
00190 
00191   // Start the viewer.
00192   tree->StartViewer();
00193 
00194   // Get the histogram
00195   TH1D* hist = (TH1D*)file->Get(HISTNAME);
00196   if (!hist) {
00197     Error("showEventSample", "couldn't get TH1D %s", HISTNAME);
00198     return 4;
00199   }
00200 
00201   // Draw the histogram in a canvas
00202   gStyle->SetOptStat(1);
00203   TCanvas* canvas = new TCanvas("canvas", "canvas");
00204   canvas->SetLogy();
00205   hist->Draw("e1");
00206   TF1* func = hist->GetFunction("expo");
00207 
00208   char expression[64];
00209   sprintf(expression,"T #approx %5.1f", -1000 / func->GetParameter(1));
00210   TLatex* latex = new TLatex(1.5, 1e-4, expression);
00211   latex->SetTextSize(.1);
00212   latex->SetTextColor(4);
00213   latex->Draw();
00214 
00215   return 0;
00216 }
00217 
00218 void pythiaExample(Int_t n=1000) {
00219    makeEventSample(n);
00220    showEventSample();
00221 }
00222 
00223 #ifndef __CINT__
00224 int main(int argc, char** argv)
00225 {
00226   TApplication app("app", &argc, argv);
00227 
00228   Int_t n = 100;
00229   if (argc > 1)
00230     n = strtol(argv[1], NULL, 0);
00231 
00232   int retVal = 0;
00233   if (n > 0)
00234     retVal = makeEventSample(n);
00235   else {
00236     retVal = showEventSample();
00237     app.Run();
00238   }
00239 
00240   return retVal;
00241 }
00242 #endif
00243 
00244 //____________________________________________________________________
00245 //
00246 // EOF
00247 //
00248 

Generated on Tue Jul 5 15:44:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1