|
| 1 | +#include <boost/program_options.hpp> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string> |
| 4 | +#include <time.h> |
| 5 | + |
| 6 | +#include "Pythia8/Pythia.h" |
| 7 | +#include "Pythia8Plugins/HepMC2.h" |
| 8 | + |
| 9 | +using namespace Pythia8; |
| 10 | + |
| 11 | +int main(int argc, char **argv) |
| 12 | +{ |
| 13 | + |
| 14 | + int nevents, pdg, seed; |
| 15 | + std::string config, output, background_config; |
| 16 | + double eta_min, phi_min, p_min; |
| 17 | + double eta_max, phi_max, p_max; |
| 18 | + int npart; |
| 19 | + double xProd, yProd, zProd; |
| 20 | + bool verbose, decay; |
| 21 | + |
| 22 | + /** process arguments **/ |
| 23 | + namespace po = boost::program_options; |
| 24 | + po::options_description desc("Options"); |
| 25 | + try { |
| 26 | + desc.add_options() |
| 27 | + ("help", "Print help messages") |
| 28 | + ("nevents,n", po::value<int>(&nevents)->default_value(10), "Number of events to be generated") |
| 29 | + ("pdg,p", po::value<int>(&pdg)->required(), "PDG code of the particle") |
| 30 | + ("npart", po::value<int>(&npart)->default_value(1), "Number of particles per event in a box") |
| 31 | + ("etamin", po::value<double>(&eta_min)->default_value(0.), "Minimum eta") |
| 32 | + ("etamax", po::value<double>(&eta_max)->default_value(0.), "Maximum eta") |
| 33 | + ("phimin", po::value<double>(&phi_min)->default_value(0.), "Minimum phi") |
| 34 | + ("phimax", po::value<double>(&phi_max)->default_value(0.), "Maximum phi") |
| 35 | + ("pmin", po::value<double>(&p_min)->default_value(0.), "Minimum momentum") |
| 36 | + ("pmax", po::value<double>(&p_max)->default_value(0.), "Maximum momentum") |
| 37 | + ("xProd", po::value<double>(&xProd)->default_value(0.), "Production vertex in the x-direction") |
| 38 | + ("yProd", po::value<double>(&yProd)->default_value(0.), "Production vertex in the y-direction") |
| 39 | + ("zProd", po::value<double>(&zProd)->default_value(0.), "Production vertex in the z-direction") |
| 40 | + ("config,c", po::value<std::string>(&config), "Configuration file") |
| 41 | + ("background-config", po::value<std::string>(&background_config), "Background configuration file") |
| 42 | + ("output,o", po::value<std::string>(&output)->default_value("pythia-gun.hepmc"), "Output HepMC file") |
| 43 | + ("decay,D", po::bool_switch(&decay)->default_value(false), "Decay particle at production vertex") |
| 44 | + ("verbose,V", po::bool_switch(&verbose)->default_value(false), "Verbose event listing") |
| 45 | + ("seed", po::value<int>(&seed)->default_value(1), "initial seed") |
| 46 | + ; |
| 47 | + |
| 48 | + po::variables_map vm; |
| 49 | + po::store(po::parse_command_line(argc, argv, desc), vm); |
| 50 | + po::notify(vm); |
| 51 | + |
| 52 | + if (vm.count("help")) { |
| 53 | + std::cout << desc << std::endl; |
| 54 | + return 1; |
| 55 | + } |
| 56 | + } |
| 57 | + catch(std::exception& e) { |
| 58 | + std::cerr << "Error: " << e.what() << std::endl; |
| 59 | + std::cout << desc << std::endl; |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + HepMC::Pythia8ToHepMC ToHepMC; |
| 64 | + HepMC::IO_GenEvent ascii_io(output, std::ios::out); |
| 65 | + |
| 66 | + // pythia |
| 67 | + Pythia pythia; |
| 68 | + |
| 69 | + // check valid pdg code |
| 70 | + if (!pythia.particleData.isLepton(pdg) && |
| 71 | + !pythia.particleData.isHadron(pdg) && |
| 72 | + !pythia.particleData.isResonance(pdg)) { |
| 73 | + if (abs(pdg) < 1000000000) { |
| 74 | + std::cout << "Error: invalid PDG code \"" << pdg << "\"" << std::endl; |
| 75 | + return 1; |
| 76 | + } else { |
| 77 | + std::cout << "PDG code \"" << pdg << "\" stands for a nucleous" << std::endl; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // config |
| 82 | + pythia.readString("ProcessLevel:all = off"); |
| 83 | + // pythia.readString("SoftQCD:elastic on"); |
| 84 | + if (!config.empty() && !pythia.readFile(config)) { |
| 85 | + std::cout << "Error: could not read config file \"" << config << "\"" << std::endl; |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + std::cout<<"Random:seed =" + std::to_string(seed)<<std::endl; |
| 90 | + pythia.readString("Random:setSeed = on"); |
| 91 | + pythia.readString("Random:seed =" + std::to_string(seed)); |
| 92 | + // init |
| 93 | + pythia.init(); |
| 94 | + const double m = pythia.particleData.m0(pdg); |
| 95 | + |
| 96 | + // the particle |
| 97 | + Particle particle; |
| 98 | + particle.id(pdg); |
| 99 | + particle.status(11); |
| 100 | + particle.m(m); |
| 101 | + particle.xProd(xProd); |
| 102 | + particle.yProd(yProd); |
| 103 | + particle.zProd(zProd); |
| 104 | + |
| 105 | + // background interface |
| 106 | + Pythia8::Pythia *pythia_bkg = nullptr; |
| 107 | + if (!background_config.empty()) { |
| 108 | + std::cout << "Background: configure from " << background_config << std::endl; |
| 109 | + pythia_bkg = new Pythia8::Pythia; |
| 110 | + if (!pythia_bkg->readFile(background_config)) { |
| 111 | + std::cout << "Error: could not read config file \"" << background_config << "\"" << std::endl; |
| 112 | + return 1; |
| 113 | + } |
| 114 | + pythia_bkg->init(); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + // event loop |
| 120 | + double eta, phi, p, pt, pl, e; |
| 121 | + srand (time(NULL)); |
| 122 | + for (int iev = 0; iev < nevents; ++iev) { |
| 123 | + |
| 124 | + // reset, add particle and decay |
| 125 | + pythia.event.reset(); |
| 126 | + for(int ipart = 0; ipart < npart; ipart++){ |
| 127 | + eta = eta_min + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (eta_max - eta_min))); |
| 128 | + phi = phi_min + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (phi_max - phi_min))); |
| 129 | + p = p_min + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (p_max - p_min))); |
| 130 | + e = exp(2. * eta); |
| 131 | + pl = p * (e - 1.) / (1. + e); |
| 132 | + pt = sqrt(p * p - pl * pl); |
| 133 | + particle.e(sqrt(p * p + m * m)); |
| 134 | + particle.px(pt * cos(phi)); |
| 135 | + particle.py(pt * sin(phi)); |
| 136 | + particle.pz(pl); |
| 137 | + pythia.event.append(particle); |
| 138 | + if (decay) pythia.moreDecays(); |
| 139 | + pythia.next(); |
| 140 | + } |
| 141 | + |
| 142 | + // print verbose |
| 143 | + if (verbose) pythia.event.list(1); |
| 144 | + |
| 145 | + // background |
| 146 | + if (pythia_bkg) { |
| 147 | + pythia_bkg->next(); |
| 148 | + pythia.event += pythia_bkg->event; |
| 149 | + } |
| 150 | + |
| 151 | + // write HepMC |
| 152 | + HepMC::GenEvent *hepmcevt = new HepMC::GenEvent(); |
| 153 | + ToHepMC.fill_next_event(pythia, hepmcevt); |
| 154 | + ascii_io << hepmcevt; |
| 155 | + delete hepmcevt; |
| 156 | + } |
| 157 | + |
| 158 | + // print statistics |
| 159 | + pythia.stat(); |
| 160 | + if (pythia_bkg) { |
| 161 | + pythia_bkg->stat(); |
| 162 | + delete pythia_bkg; |
| 163 | + } |
| 164 | + |
| 165 | + return 0; |
| 166 | + |
| 167 | +} |
0 commit comments