ROOT usage

(This text has been contributed by Andreas Morsch, the second example by Rene Brun.)

Interfaces

ROOT provides two simple interfaces (wrappers) for PYTHIA 8. Both are located in the
    yourROOTinstallationPath/montecarlo/pythia8
directory. (Type which root if you want to find out where ROOT has been installed. It will print yourROOTinstallationPath/bin/root).

Installation of ROOT with PYTHIA 8 support

In order to use PYTHIA 8 with ROOT you need a ROOT version that has been installed from source. The reason is that the interfaces depend on PYTHIA header files that are not distributed with ROOT. Installing ROOT is not more difficult than the PYTHIA installation.

Define an environment variable for the path to your pythia8 installation directory

    export PYTHIA8=YourPathToPythia8
Before compiling ROOT configure ROOT running the yourROOTinstallationPath/configure command including the following options:
    --enable-pythia8 
    --with-pythia8-incdir=$PYTHIA8/include 
    --with-pythia8-libdir=$PYTHIA8/lib
In case ROOT has already been compiled before, it will only recompile the pythia8 module and build the library libEGPythia8.

An example

A basic example for generating minimum-bias events with PYTHIA 8 inside a ROOT macro, and filling some histograms with the kinematics of the final-state particles is provided in
    yourROOTinstallationDirectory/tutorials/pythia/pythia8.C
Note that before executing this script

Looking at the example code you will see that it is necessary to load three libraries before running the actual code:

    gSystem->Load("$PYTHIA8/lib/libpythia8"); // Pythia 8
    gSystem->Load("libEG"); // The library with the TGenerator interface
    gSystem->Load("libEGPythia8"); // The TPythia8 implementation

A second example

It is not necessary to run PYTHIA as a ROOT plug-in. One can also perform the generation and analysis of events in a completely standalone fashion, and only use ROOT for the histogramming step. One example, with a lightly modified version of main01.cc, is
//gSystem.Load("../libPythia8");
// File: main01.cc
// This is a simple test program. It fits on one slide in a talk. 
// It studies the charged multiplicity distribution at the LHC.
// Copyright C 2007 Torbjorn Sjostrand
//#include "Pythia.h"
#include "TH1.h"
using namespace Pythia8; 
int ex1() {
  // Generator. Process selection. LHC initialization. Histogram.
  Pythia pythia;
  pythia.readString("HardQCD:all = on");    
  pythia.readString("PhaseSpace:pTHatMin = 20.");  
  pythia.init( 2212, 2212, 14000.);
  TFile *file = TFile::Open("ex1.root","recreate");
  Event *event = &pythia.event;
  TTree *T = new TTree("T","ev1 Tree");
  T->Branch("event","Event",&event);
  TH1F *mult = new TH1F("mult","charged multiplicity", 100, -0.5, 799.5);
  // Begin event loop. Generate event. Skip if error. List first one.
  for (int iEvent = 0; iEvent < 100; ++iEvent) {
    if (!pythia.next()) continue;
    //if (iEvent < 1) {pythia.info.list(); pythia.event.list();} 
    // Find number of all final charged particles and fill histogram.
    int nCharged = 0;
    for (int i = 0; i < pythia.event.size(); ++i) 
      if (pythia.event[i].isFinal() && pythia.event[i].isCharged()) 
        ++nCharged; 
    mult->Fill( nCharged );
    T->Fill();
  // End of event loop. Statistics. Histogram. Done.
  }
  pythia.statistics();
  cout << mult; 
  T->Print();
  T->Write();
  delete file;
  return 0;
}