Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:21:08

0001 #include <iostream>
0002 #include <sstream>
0003 #include <string>
0004 
0005 // GNU long option variation of getopt, for command line option parsing.
0006 #include <getopt.h>
0007 
0008 // ROOT headers.
0009 #include <TFile.h>
0010 #include <TObjString.h>
0011 #include <TTree.h>
0012 
0013 #include "factory.h"
0014 #include "pythia_commons.h"
0015 #include "pythia_erhic.h"
0016 
0017 // Forward declarations
0018 std::string initialise(int argc, char* argv[]);
0019 void write_run_data(TFile&);
0020 
0021 /**
0022  C++ wrapper for calling the eRHIC PYTHIA 6 implementation
0023  and building eRHIX C++ events to store in a ROOT tree.
0024  */
0025 int main(int argc, char* argv[]) {
0026    std::string filename = initialise(argc, argv);
0027 
0028    // Open the output file/tree and create an event factory,
0029    // which we use to add the event branch to the tree.
0030    TFile file(filename.c_str(), "recreate");
0031    TTree tree("EICTree", "Output direct from pythia!");
0032    Factory factory;
0033    factory.Branch(tree, "event");
0034 
0035    std::stringstream stream;
0036    // The PYTHIA6 generate() subroutine handles tracking the event
0037    // count. Continue looping until it returns a non-zero value,
0038    // indicating the requested number of events have been generated.
0039    while(__pythia6_MOD_generate() == 0) {
0040       erhic::EventPythia* event = factory.Create();
0041       event->SetN(tree.GetEntries() + 1);
0042       tree.Fill();
0043       // \todo Add radiative photon
0044       std::cout << std::endl;
0045    } // for
0046    file.Write(); // Writes the ROOT TTree
0047    write_run_data(file);
0048    __pythia6_MOD_finish();
0049    return 0;
0050 }
0051 
0052 /**
0053  Initialise the program, processing command line arguments.
0054  Returns the name of the ROOT file to generate.
0055  */
0056 std::string initialise(int argc, char* argv[]) {
0057    std::string filename("pythia.root");
0058    // Parse command line options.
0059    __pythia6_MOD_printascii = 1; // Generate ASCII output by default
0060    static struct option long_options[] = {
0061       // These set a flag.
0062       {"ascii", no_argument, &__pythia6_MOD_printascii, 1},
0063       {"noascii", no_argument, &__pythia6_MOD_printascii, 0},
0064       // These don't set a flag.
0065       // The arguments are processed below.
0066       {"out", required_argument, NULL, 'o'},
0067       {NULL, 0, NULL, 0}
0068    };
0069    // Loop through options.
0070    int option_index = 0;
0071    int code(0);
0072    while((code = getopt_long(argc, argv, "o:r:",
0073                              long_options, &option_index)) not_eq -1) {
0074       switch(code) {
0075          case 0:
0076             if(long_options[option_index].flag not_eq 0) {
0077                break;
0078             } // if
0079             printf("option %s", long_options[option_index].name);
0080             if(optarg) {
0081                printf (" with arg %s", optarg);
0082             } // if
0083             printf("\n");
0084             break;
0085          case 'o':
0086             filename = optarg;
0087             break;
0088          default:
0089             abort();
0090       } // switch
0091    } // while
0092    // Now that we have processed all command line arguments, call
0093    // the Fortran PYTHIA6 initialisation routine.
0094    // This handles processing options from the steer file.
0095    // If the return value is non-zero, exit.
0096    int result = __pythia6_MOD_initialise();
0097    if(0 not_eq result) {
0098       exit(result);
0099    } // if
0100    return filename;
0101 }
0102 
0103 /**
0104  Extract the following run data from PYTHIA and write as
0105  TObjStrings to the provided file with the names shown:
0106  <ul>
0107    <li>Total sampled cross section as "crossSection"</li>
0108    <li>Total number of generated events as "nEvents"</li>
0109    <li>Total number of event trials as "nTrials"</li>
0110  </ul>
0111 */
0112 void write_run_data(TFile& file) {
0113    TObjString text;
0114    std::stringstream stream;
0115    // Total cross section is stored in PYTHIA in pari(1) in millibarns.
0116    // Multiply by 1000 to get it in microbarn.
0117    stream << pari(1) * 1000.;
0118    text.SetString(stream.str().c_str());
0119    file.WriteObject(&text, "crossSection");
0120    // Total number of generated events is stored in PYTHIA in msti(5).
0121    stream.str("");
0122    stream.clear();
0123    stream << msti(5);
0124    text.SetString(stream.str().c_str());
0125    file.WriteObject(&text, "nEvents");
0126    // Total number of generateds is stored in PYTHIA in ngen(0, 3).
0127    stream.str("");
0128    stream.clear();
0129    stream << ngen(0, 3);
0130    text.SetString(stream.str().c_str());
0131    file.WriteObject(&text, "nTrials");
0132 }