ROOT logo
//____________________________________________________________________
//
// 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
//
// 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+
//
//
//____________________________________________________________________
//
// Author: Christian Holm Christensen <cholm@hilux15.nbi.dk>
// Update: 2002-08-16 16:40:27+0200
// Copyright: 2002 (C) Christian Holm Christensen
// Copyright (C) 2006, Rene Brun and Fons Rademakers.
// For the licensing terms see $ROOTSYS/LICENSE.
//
#ifndef __CINT__
#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;
#endif

#define FILENAME   "pythia.root"
#define TREENAME   "tree"
#define BRANCHNAME "particles"
#define HISTNAME   "ptSpectra"
#define PDGNUMBER  211

// This funtion just load the needed libraries if we're executing from
// an interactive session.
void loadLibraries()
{
#ifdef __CINT__
  // Load the Event Generator abstraction library, Pythia 6
  // library, and the Pythia 6 interface library.
  gSystem->Load("libEG");
  gSystem->Load("$ROOTSYS/../pythia6/libPythia6"); //change to your setup
  gSystem->Load("libEGPythia6");
#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
  gStyle->SetOptStat(1);
  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

//____________________________________________________________________
//
// EOF
//

 pythiaExample.C:1
 pythiaExample.C:2
 pythiaExample.C:3
 pythiaExample.C:4
 pythiaExample.C:5
 pythiaExample.C:6
 pythiaExample.C:7
 pythiaExample.C:8
 pythiaExample.C:9
 pythiaExample.C:10
 pythiaExample.C:11
 pythiaExample.C:12
 pythiaExample.C:13
 pythiaExample.C:14
 pythiaExample.C:15
 pythiaExample.C:16
 pythiaExample.C:17
 pythiaExample.C:18
 pythiaExample.C:19
 pythiaExample.C:20
 pythiaExample.C:21
 pythiaExample.C:22
 pythiaExample.C:23
 pythiaExample.C:24
 pythiaExample.C:25
 pythiaExample.C:26
 pythiaExample.C:27
 pythiaExample.C:28
 pythiaExample.C:29
 pythiaExample.C:30
 pythiaExample.C:31
 pythiaExample.C:32
 pythiaExample.C:33
 pythiaExample.C:34
 pythiaExample.C:35
 pythiaExample.C:36
 pythiaExample.C:37
 pythiaExample.C:38
 pythiaExample.C:39
 pythiaExample.C:40
 pythiaExample.C:41
 pythiaExample.C:42
 pythiaExample.C:43
 pythiaExample.C:44
 pythiaExample.C:45
 pythiaExample.C:46
 pythiaExample.C:47
 pythiaExample.C:48
 pythiaExample.C:49
 pythiaExample.C:50
 pythiaExample.C:51
 pythiaExample.C:52
 pythiaExample.C:53
 pythiaExample.C:54
 pythiaExample.C:55
 pythiaExample.C:56
 pythiaExample.C:57
 pythiaExample.C:58
 pythiaExample.C:59
 pythiaExample.C:60
 pythiaExample.C:61
 pythiaExample.C:62
 pythiaExample.C:63
 pythiaExample.C:64
 pythiaExample.C:65
 pythiaExample.C:66
 pythiaExample.C:67
 pythiaExample.C:68
 pythiaExample.C:69
 pythiaExample.C:70
 pythiaExample.C:71
 pythiaExample.C:72
 pythiaExample.C:73
 pythiaExample.C:74
 pythiaExample.C:75
 pythiaExample.C:76
 pythiaExample.C:77
 pythiaExample.C:78
 pythiaExample.C:79
 pythiaExample.C:80
 pythiaExample.C:81
 pythiaExample.C:82
 pythiaExample.C:83
 pythiaExample.C:84
 pythiaExample.C:85
 pythiaExample.C:86
 pythiaExample.C:87
 pythiaExample.C:88
 pythiaExample.C:89
 pythiaExample.C:90
 pythiaExample.C:91
 pythiaExample.C:92
 pythiaExample.C:93
 pythiaExample.C:94
 pythiaExample.C:95
 pythiaExample.C:96
 pythiaExample.C:97
 pythiaExample.C:98
 pythiaExample.C:99
 pythiaExample.C:100
 pythiaExample.C:101
 pythiaExample.C:102
 pythiaExample.C:103
 pythiaExample.C:104
 pythiaExample.C:105
 pythiaExample.C:106
 pythiaExample.C:107
 pythiaExample.C:108
 pythiaExample.C:109
 pythiaExample.C:110
 pythiaExample.C:111
 pythiaExample.C:112
 pythiaExample.C:113
 pythiaExample.C:114
 pythiaExample.C:115
 pythiaExample.C:116
 pythiaExample.C:117
 pythiaExample.C:118
 pythiaExample.C:119
 pythiaExample.C:120
 pythiaExample.C:121
 pythiaExample.C:122
 pythiaExample.C:123
 pythiaExample.C:124
 pythiaExample.C:125
 pythiaExample.C:126
 pythiaExample.C:127
 pythiaExample.C:128
 pythiaExample.C:129
 pythiaExample.C:130
 pythiaExample.C:131
 pythiaExample.C:132
 pythiaExample.C:133
 pythiaExample.C:134
 pythiaExample.C:135
 pythiaExample.C:136
 pythiaExample.C:137
 pythiaExample.C:138
 pythiaExample.C:139
 pythiaExample.C:140
 pythiaExample.C:141
 pythiaExample.C:142
 pythiaExample.C:143
 pythiaExample.C:144
 pythiaExample.C:145
 pythiaExample.C:146
 pythiaExample.C:147
 pythiaExample.C:148
 pythiaExample.C:149
 pythiaExample.C:150
 pythiaExample.C:151
 pythiaExample.C:152
 pythiaExample.C:153
 pythiaExample.C:154
 pythiaExample.C:155
 pythiaExample.C:156
 pythiaExample.C:157
 pythiaExample.C:158
 pythiaExample.C:159
 pythiaExample.C:160
 pythiaExample.C:161
 pythiaExample.C:162
 pythiaExample.C:163
 pythiaExample.C:164
 pythiaExample.C:165
 pythiaExample.C:166
 pythiaExample.C:167
 pythiaExample.C:168
 pythiaExample.C:169
 pythiaExample.C:170
 pythiaExample.C:171
 pythiaExample.C:172
 pythiaExample.C:173
 pythiaExample.C:174
 pythiaExample.C:175
 pythiaExample.C:176
 pythiaExample.C:177
 pythiaExample.C:178
 pythiaExample.C:179
 pythiaExample.C:180
 pythiaExample.C:181
 pythiaExample.C:182
 pythiaExample.C:183
 pythiaExample.C:184
 pythiaExample.C:185
 pythiaExample.C:186
 pythiaExample.C:187
 pythiaExample.C:188
 pythiaExample.C:189
 pythiaExample.C:190
 pythiaExample.C:191
 pythiaExample.C:192
 pythiaExample.C:193
 pythiaExample.C:194
 pythiaExample.C:195
 pythiaExample.C:196
 pythiaExample.C:197
 pythiaExample.C:198
 pythiaExample.C:199
 pythiaExample.C:200
 pythiaExample.C:201
 pythiaExample.C:202
 pythiaExample.C:203
 pythiaExample.C:204
 pythiaExample.C:205
 pythiaExample.C:206
 pythiaExample.C:207
 pythiaExample.C:208
 pythiaExample.C:209
 pythiaExample.C:210
 pythiaExample.C:211
 pythiaExample.C:212
 pythiaExample.C:213
 pythiaExample.C:214
 pythiaExample.C:215
 pythiaExample.C:216
 pythiaExample.C:217
 pythiaExample.C:218
 pythiaExample.C:219
 pythiaExample.C:220
 pythiaExample.C:221
 pythiaExample.C:222
 pythiaExample.C:223
 pythiaExample.C:224
 pythiaExample.C:225
 pythiaExample.C:226
 pythiaExample.C:227
 pythiaExample.C:228
 pythiaExample.C:229
 pythiaExample.C:230
 pythiaExample.C:231
 pythiaExample.C:232
 pythiaExample.C:233
 pythiaExample.C:234
 pythiaExample.C:235
 pythiaExample.C:236
 pythiaExample.C:237
 pythiaExample.C:238
 pythiaExample.C:239
 pythiaExample.C:240
 pythiaExample.C:241
 pythiaExample.C:242
 pythiaExample.C:243
 pythiaExample.C:244
 pythiaExample.C:245
 pythiaExample.C:246
 pythiaExample.C:247
 pythiaExample.C:248
 pythiaExample.C:249
thumb
thumb
thumb