GENIEGenerator
Loading...
Searching...
No Matches
gEvGen.cxx File Reference
#include <cstdlib>
#include <cassert>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <TFile.h>
#include <TTree.h>
#include <TSystem.h>
#include <TVector3.h>
#include <TH1.h>
#include <TF1.h>
#include "Framework/Conventions/XmlParserStatus.h"
#include "Framework/Conventions/GBuild.h"
#include "Framework/Conventions/Controls.h"
#include "Framework/EventGen/EventRecord.h"
#include "Framework/EventGen/GFluxI.h"
#include "Framework/EventGen/GEVGDriver.h"
#include "Framework/EventGen/GMCJDriver.h"
#include "Framework/EventGen/GMCJMonitor.h"
#include "Framework/Interaction/Interaction.h"
#include "Framework/Messenger/Messenger.h"
#include "Framework/Ntuple/NtpWriter.h"
#include "Framework/Ntuple/NtpMCFormat.h"
#include "Framework/Numerical/RandomGen.h"
#include "Framework/Numerical/Spline.h"
#include "Framework/ParticleData/PDGCodes.h"
#include "Framework/ParticleData/PDGUtils.h"
#include "Framework/Utils/AppInit.h"
#include "Framework/Utils/RunOpt.h"
#include "Framework/Utils/XSecSplineList.h"
#include "Framework/Utils/StringUtils.h"
#include "Framework/Utils/PrintUtils.h"
#include "Framework/Utils/SystemUtils.h"
#include "Framework/Utils/CmdLnArgParser.h"
Include dependency graph for gEvGen.cxx:

Go to the source code of this file.

Functions

void GetCommandLineArgs (int argc, char **argv)
void Initialize (void)
void PrintSyntax (void)
void GenerateEventsAtFixedInitState (void)
int main (int argc, char **argv)

Variables

int kDefOptNevents = 0
NtpMCFormat_t kDefOptNtpFormat = kNFGHEP
Long_t kDefOptRunNu = 0
int gOptNevents
double gOptNuEnergy
double gOptNuEnergyRange
int gOptNuPdgCode
map< int, double > gOptTgtMix
Long_t gOptRunNu
string gOptFlux
string gOptFluxFactors
bool gOptWeighted
bool gOptForceInt
bool gOptUsingFluxOrTgtMix = false
long int gOptRanSeed
string gOptInpXSecFile
string gOptOutFileName
string gOptStatFileName

Function Documentation

◆ GenerateEventsAtFixedInitState()

void GenerateEventsAtFixedInitState ( void )

Definition at line 293 of file gEvGen.cxx.

294{
295 int neutrino = gOptNuPdgCode;
296 int target = gOptTgtMix.begin()->first;
297 double Ev = gOptNuEnergy;
298 TLorentzVector nu_p4(0.,0.,Ev,Ev); // px,py,pz,E (GeV)
299
300 // Create init state
301 InitialState init_state(target, neutrino);
302
303 // Create/config event generation driver
304 GEVGDriver evg_driver;
306 evg_driver.SetUnphysEventMask(*RunOpt::Instance()->UnphysEventMask());
307 evg_driver.Configure(init_state);
308
309 // Initialize an Ntuple Writer
311
312 // If an output file name has been specified... use it
313 if (!gOptOutFileName.empty()){
314 ntpw.CustomizeFilename(gOptOutFileName);
315 }
316 ntpw.Initialize();
317
318
319 // Create an MC Job Monitor
320 GMCJMonitor mcjmonitor(gOptRunNu);
321 mcjmonitor.SetRefreshRate(RunOpt::Instance()->MCJobStatusRefreshRate());
322
323 // If a status file name has been given... use it
324 if (!gOptStatFileName.empty()){
325 mcjmonitor.CustomizeFilename(gOptStatFileName);
326 }
327
328
329 LOG("gevgen", pNOTICE)
330 << "\n ** Will generate " << gOptNevents << " events for \n"
331 << init_state << " at Ev = " << Ev << " GeV";
332
333 // Generate events / print the GHEP record / add it to the ntuple
334 int ievent = 0;
335 while (ievent < gOptNevents) {
336 LOG("gevgen", pNOTICE)
337 << " *** Generating event............ " << ievent;
338
339 // generate a single event
340 EventRecord * event = evg_driver.GenerateEvent(nu_p4);
341
342 if(!event) {
343 LOG("gevgen", pNOTICE)
344 << "Last attempt failed. Re-trying....";
345 continue;
346 }
347
348 LOG("gevgen", pNOTICE)
349 << "Generated Event GHEP Record: " << *event;
350
351 // add event at the output ntuple, refresh the mc job monitor & clean up
352 ntpw.AddEventRecord(ievent, event);
353 mcjmonitor.Update(ievent,event);
354 ievent++;
355 delete event;
356 }
357
358 // Save the generated MC events
359 ntpw.Save();
360}
#define pNOTICE
Definition Messenger.h:61
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE,...
Definition Messenger.h:96
A vector of EventGeneratorI objects.
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition EventRecord.h:37
GENIE Event Generation Driver. A minimalist user interface object for generating neutrino interaction...
Definition GEVGDriver.h:54
void SetUnphysEventMask(const TBits &mask)
void Configure(int nu_pdgc, int Z, int A)
EventRecord * GenerateEvent(const TLorentzVector &nu4p)
void SetEventGeneratorList(string listname)
Simple class to create & update MC job status files and env. vars. This is used to be able to keep tr...
Definition GMCJMonitor.h:31
Initial State information.
A utility class to facilitate creating the GENIE MC Ntuple from the output GENIE GHEP event records.
Definition NtpWriter.h:39
static RunOpt * Instance(void)
Definition RunOpt.cxx:54
long int gOptRanSeed
NtpMCFormat_t kDefOptNtpFormat
Long_t gOptRunNu
map< int, double > gOptTgtMix
int gOptNuPdgCode
Definition gEvGen.cxx:231
string gOptOutFileName
Definition gEvGen.cxx:241
int gOptNevents
Definition gEvGen.cxx:228
double gOptNuEnergy
Definition gEvGen.cxx:229
string gOptStatFileName
Definition gEvGen.cxx:242

References genie::NtpWriter::AddEventRecord(), genie::GEVGDriver::Configure(), genie::GMCJMonitor::CustomizeFilename(), genie::NtpWriter::CustomizeFilename(), genie::GEVGDriver::GenerateEvent(), gOptNevents, gOptNuEnergy, gOptNuPdgCode, gOptOutFileName, gOptRanSeed, gOptRunNu, gOptStatFileName, gOptTgtMix, genie::NtpWriter::Initialize(), genie::RunOpt::Instance(), kDefOptNtpFormat, LOG, pNOTICE, genie::NtpWriter::Save(), genie::GEVGDriver::SetEventGeneratorList(), genie::GMCJMonitor::SetRefreshRate(), genie::GEVGDriver::SetUnphysEventMask(), and genie::GMCJMonitor::Update().

Referenced by main().

◆ GetCommandLineArgs()

void GetCommandLineArgs ( int argc,
char ** argv )

Definition at line 644 of file gEvGen.cxx.

645{
646 LOG("gevgen", pINFO) << "Parsing command line arguments";
647
648 // Common run options. Set defaults and read.
651
652 // Parse run options for this app
653
654 CmdLnArgParser parser(argc,argv);
655
656 // help?
657 bool help = parser.OptionExists('h');
658 if(help) {
659 PrintSyntax();
660 exit(0);
661 }
662
663 // number of events
664 if( parser.OptionExists('n') ) {
665 LOG("gevgen", pINFO) << "Reading number of events to generate";
666 gOptNevents = parser.ArgAsInt('n');
667 } else {
668 LOG("gevgen", pINFO)
669 << "Unspecified number of events to generate - Using default";
671 }
672
673 // run number
674 if( parser.OptionExists('r') ) {
675 LOG("gevgen", pINFO) << "Reading MC run number";
676 gOptRunNu = parser.ArgAsLong('r');
677 } else {
678 LOG("gevgen", pINFO) << "Unspecified run number - Using default";
680 }
681
682 // Output file name
683 if( parser.OptionExists('o') ) {
684 LOG("gevgen", pINFO) << "Reading output file name";
685 gOptOutFileName = parser.ArgAsString('o');
686
688 // strip the output file format and replace with .status
689 if (gOptOutFileName.find_last_of(".") != string::npos)
691 gOptStatFileName.substr(0, gOptOutFileName.find_last_of("."));
692 gOptStatFileName .append(".status");
693 }
694
695 // flux functional form
696 bool using_flux = false;
697 if( parser.OptionExists('f') ) {
698 LOG("gevgen", pINFO) << "Reading flux function";
699 gOptFlux = parser.ArgAsString('f');
700 using_flux = true;
701 }
702 if (parser.OptionExists('F') ) {
703 LOG("gevgen", pINFO) << "Reading flux factors";
704 gOptFluxFactors = parser.ArgAsString('F');
705 }
706
707 if(parser.OptionExists('s')) {
708 LOG("gevgen", pWARN)
709 << "-s option no longer available. Please read the revised code documentation";
710 gAbortingInErr = true;
711 exit(1);
712 }
713
714
715 // generate weighted events option (only relevant if using a flux)
716 gOptWeighted = parser.OptionExists('w');
717
718 // force interaction of all injected events (only relevant if using a flux)
719 gOptForceInt = parser.OptionExists("force-flux-ray-interaction");
720
721 // neutrino energy
722 if( parser.OptionExists('e') ) {
723 LOG("gevgen", pINFO) << "Reading neutrino energy";
724 string nue = parser.ArgAsString('e');
725
726 // is it just a value or a range (comma separated set of values)
727 if(nue.find(",") != string::npos) {
728 // split the comma separated list
729 vector<string> nurange = utils::str::Split(nue, ",");
730 assert(nurange.size() == 2);
731 double emin = atof(nurange[0].c_str());
732 double emax = atof(nurange[1].c_str());
733 assert(emax>emin && emin>=0);
734 gOptNuEnergy = emin;
735 gOptNuEnergyRange = emax-emin;
736 if(!using_flux) {
737 LOG("gevgen", pWARN)
738 << "No flux was specified but an energy range was input!";
739 LOG("gevgen", pWARN)
740 << "Events will be generated at fixed E = " << gOptNuEnergy << " GeV";
742 }
743 } else {
744 gOptNuEnergy = atof(nue.c_str());
746 }
747 } else {
748 LOG("gevgen", pFATAL) << "Unspecified neutrino energy - Exiting";
749 PrintSyntax();
750 exit(1);
751 }
752
753 // neutrino PDG code
754 if( parser.OptionExists('p') ) {
755 LOG("gevgen", pINFO) << "Reading neutrino PDG code";
756 gOptNuPdgCode = parser.ArgAsInt('p');
757 } else {
758 LOG("gevgen", pFATAL) << "Unspecified neutrino PDG code - Exiting";
759 PrintSyntax();
760 exit(1);
761 }
762
763 // target mix (their PDG codes with their corresponding weights)
764 bool using_tgtmix = false;
765 if( parser.OptionExists('t') ) {
766 LOG("gevgen", pINFO) << "Reading target mix";
767 string stgtmix = parser.ArgAsString('t');
768 gOptTgtMix.clear();
769 vector<string> tgtmix = utils::str::Split(stgtmix,",");
770 if(tgtmix.size()==1) {
771 int pdg = atoi(tgtmix[0].c_str());
772 double wgt = 1.0;
773 gOptTgtMix.insert(map<int, double>::value_type(pdg, wgt));
774 } else {
775 using_tgtmix = true;
776 vector<string>::const_iterator tgtmix_iter = tgtmix.begin();
777 for( ; tgtmix_iter != tgtmix.end(); ++tgtmix_iter) {
778 string tgt_with_wgt = *tgtmix_iter;
779 string::size_type open_bracket = tgt_with_wgt.find("[");
780 string::size_type close_bracket = tgt_with_wgt.find("]");
781 string::size_type ibeg = 0;
782 string::size_type iend = open_bracket;
783 string::size_type jbeg = open_bracket+1;
784 string::size_type jend = close_bracket-1;
785 int pdg = atoi(tgt_with_wgt.substr(ibeg,iend).c_str());
786 double wgt = atof(tgt_with_wgt.substr(jbeg,jend).c_str());
787 LOG("Main", pNOTICE)
788 << "Adding to target mix: pdg = " << pdg << ", wgt = " << wgt;
789 gOptTgtMix.insert(map<int, double>::value_type(pdg, wgt));
790 }//tgtmix_iter
791 }//>1
792
793 } else {
794 LOG("gevgen", pFATAL) << "Unspecified target PDG code - Exiting";
795 PrintSyntax();
796 exit(1);
797 }
798
799 gOptUsingFluxOrTgtMix = using_flux || using_tgtmix;
800
801 // random number seed
802 if( parser.OptionExists("seed") ) {
803 LOG("gevgen", pINFO) << "Reading random number seed";
804 gOptRanSeed = parser.ArgAsLong("seed");
805 } else {
806 LOG("gevgen", pINFO) << "Unspecified random number seed - Using default";
807 gOptRanSeed = -1;
808 }
809
810 // input cross-section file
811 if( parser.OptionExists("cross-sections") ) {
812 LOG("gevgen", pINFO) << "Reading cross-section file";
813 gOptInpXSecFile = parser.ArgAsString("cross-sections");
814 } else {
815 LOG("gevgen", pINFO) << "Unspecified cross-section file";
816 gOptInpXSecFile = "";
817 }
818
819 //
820 // print-out the command line options
821 //
822 LOG("gevgen", pNOTICE)
823 << "\n"
824 << utils::print::PrintFramedMesg("gevgen job configuration");
825 LOG("gevgen", pNOTICE)
826 << "MC Run Number: " << gOptRunNu;
827 if(gOptRanSeed != -1) {
828 LOG("gevgen", pNOTICE)
829 << "Random number seed: " << gOptRanSeed;
830 } else {
831 LOG("gevgen", pNOTICE)
832 << "Random number seed was not set, using default";
833 }
834 LOG("gevgen", pNOTICE)
835 << "Number of events requested: " << gOptNevents;
836 if(gOptInpXSecFile.size() > 0) {
837 LOG("gevgen", pNOTICE)
838 << "Using cross-section splines read from: " << gOptInpXSecFile;
839 } else {
840 LOG("gevgen", pNOTICE)
841 << "No input cross-section spline file";
842 }
843 LOG("gevgen", pNOTICE)
844 << "Flux: " << gOptFlux << " factors " << gOptFluxFactors;
845 LOG("gevgen", pNOTICE)
846 << "Generate weighted events? " << gOptWeighted;
847 LOG("gevgen", pNOTICE)
848 << "Force interaction of all flux rays? " << gOptForceInt;
849 if(gOptNuEnergyRange>0) {
850 LOG("gevgen", pNOTICE)
851 << "Neutrino energy: ["
852 << gOptNuEnergy << ", " << gOptNuEnergy+gOptNuEnergyRange << "]";
853 } else {
854 LOG("gevgen", pNOTICE)
855 << "Neutrino energy: " << gOptNuEnergy;
856 }
857 LOG("gevgen", pNOTICE)
858 << "Neutrino code (PDG): " << gOptNuPdgCode;
859 LOG("gevgen", pNOTICE)
860 << "Target code (PDG) & weight fraction (in case of multiple targets): ";
861 map<int,double>::const_iterator iter;
862 for(iter = gOptTgtMix.begin(); iter != gOptTgtMix.end(); ++iter) {
863 int tgtpdgc = iter->first;
864 double wgt = iter->second;
865 LOG("gevgen", pNOTICE)
866 << " >> " << tgtpdgc << " (weight fraction = " << wgt << ")";
867 }
868 LOG("gevgen", pNOTICE) << "\n";
869
870 LOG("gevgen", pNOTICE) << *RunOpt::Instance();
871
872}
#define pINFO
Definition Messenger.h:62
#define pFATAL
Definition Messenger.h:56
#define pWARN
Definition Messenger.h:60
Command line argument parser.
void ReadFromCommandLine(int argc, char **argv)
Definition RunOpt.cxx:99
void EnableBareXSecPreCalc(bool flag)
Definition RunOpt.h:62
string gOptInpXSecFile
string gOptFlux
Definition gEvGen.cxx:234
bool gOptWeighted
Definition gEvGen.cxx:236
string gOptFluxFactors
Definition gEvGen.cxx:235
double gOptNuEnergyRange
Definition gEvGen.cxx:230
bool gOptForceInt
Definition gEvGen.cxx:237
Long_t kDefOptRunNu
Definition gEvGen.cxx:225
bool gOptUsingFluxOrTgtMix
Definition gEvGen.cxx:238
void PrintSyntax(void)
Definition gEvGen.cxx:874
int kDefOptNevents
Definition gEvGen.cxx:223
Utilities for improving the code readability when using PDG codes.
string PrintFramedMesg(string mesg, unsigned int nl=1, const char f=' *')
vector< string > Split(string input, string delim)
bool gAbortingInErr
Definition Messenger.cxx:34

References genie::CmdLnArgParser::ArgAsInt(), genie::CmdLnArgParser::ArgAsLong(), genie::CmdLnArgParser::ArgAsString(), genie::RunOpt::EnableBareXSecPreCalc(), genie::gAbortingInErr, gOptFlux, gOptFluxFactors, gOptForceInt, gOptInpXSecFile, gOptNevents, gOptNuEnergy, gOptNuEnergyRange, gOptNuPdgCode, gOptOutFileName, gOptRanSeed, gOptRunNu, gOptStatFileName, gOptTgtMix, gOptUsingFluxOrTgtMix, gOptWeighted, genie::RunOpt::Instance(), kDefOptNevents, kDefOptRunNu, LOG, genie::CmdLnArgParser::OptionExists(), pFATAL, pINFO, pNOTICE, genie::utils::print::PrintFramedMesg(), PrintSyntax(), pWARN, genie::RunOpt::ReadFromCommandLine(), and genie::utils::str::Split().

Referenced by main().

◆ Initialize()

void Initialize ( void )

Definition at line 273 of file gEvGen.cxx.

274{
275
276 if ( ! RunOpt::Instance()->Tune() ) {
277 LOG("gevgen", pFATAL) << " No TuneId in RunOption";
278 exit(-1);
279 }
281
282 // Initialization of random number generators, cross-section table,
283 // messenger thresholds, cache file
284 utils::app_init::MesgThresholds(RunOpt::Instance()->MesgThresholdFiles());
288
289 // Set GHEP print level
290 GHepRecord::SetPrintLevel(RunOpt::Instance()->EventRecordPrintLevel());
291}
static void SetPrintLevel(int print_level)
void BuildTune()
build tune and inform XSecSplineList
Definition RunOpt.cxx:92
void XSecTable(string inpfile, bool require_table)
Definition AppInit.cxx:38
void RandGen(long int seed)
Definition AppInit.cxx:30
void MesgThresholds(string inpfile)
Definition AppInit.cxx:99
void CacheFile(string inpfile)
Definition AppInit.cxx:117

References genie::RunOpt::BuildTune(), genie::utils::app_init::CacheFile(), gOptInpXSecFile, gOptRanSeed, genie::RunOpt::Instance(), LOG, genie::utils::app_init::MesgThresholds(), pFATAL, genie::utils::app_init::RandGen(), genie::GHepRecord::SetPrintLevel(), and genie::utils::app_init::XSecTable().

Referenced by main().

◆ main()

int main ( int argc,
char ** argv )

Definition at line 245 of file gEvGen.cxx.

246{
247 GetCommandLineArgs(argc,argv);
248 Initialize();
249
250 // throw on NaNs and Infs...
251#if defined(HAVE_FENV_H) && defined(HAVE_FEENABLEEXCEPT)
252 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
253#endif
254 //
255 // Generate neutrino events
256 //
257
259#ifdef __CAN_GENERATE_EVENTS_USING_A_FLUX_OR_TGTMIX__
260 GenerateEventsUsingFluxOrTgtMix();
261#else
262 LOG("gevgen", pERROR)
263 << "\n To be able to generate neutrino events from a flux and/or a target mix"
264 << "\n you need to add the following config options at your GENIE installation:"
265 << "\n --enable-flux-drivers --enable-geom-drivers \n" ;
266#endif
267 } else {
269 }
270 return 0;
271}
#define pERROR
Definition Messenger.h:59
void GenerateEventsAtFixedInitState(void)
Definition gEvGen.cxx:293
void Initialize(void)
Definition gEvGen.cxx:273
void GetCommandLineArgs(int argc, char **argv)
Definition gEvGen.cxx:644

References GenerateEventsAtFixedInitState(), GetCommandLineArgs(), gOptUsingFluxOrTgtMix, Initialize(), LOG, and pERROR.

◆ PrintSyntax()

void PrintSyntax ( void )

Definition at line 874 of file gEvGen.cxx.

875{
876 LOG("gevgen", pNOTICE)
877 << "\n\n" << "Syntax:" << "\n"
878 << "\n gevgen [-h]"
879 << "\n [-r run#]"
880 << "\n -n nev"
881 << "\n -e energy (or energy range) "
882 << "\n -p neutrino_pdg"
883 << "\n -t target_pdg "
884 << "\n [-f flux_description]"
885 << "\n [-o outfile_name]"
886 << "\n [-w]"
887 << "\n [--force-flux-ray-interaction]"
888 << "\n [--seed random_number_seed]"
889 << "\n [--cross-sections xml_file]"
891 << "\n";
892
893}
static std::string RunOptSyntaxString(bool include_generator_specific)
Definition RunOpt.cxx:157

References LOG, pNOTICE, and genie::RunOpt::RunOptSyntaxString().

Referenced by GetCommandLineArgs().

Variable Documentation

◆ gOptFlux

string gOptFlux

Definition at line 234 of file gEvGen.cxx.

Referenced by BuildSpectrum(), and GetCommandLineArgs().

◆ gOptFluxFactors

string gOptFluxFactors

Definition at line 235 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().

◆ gOptForceInt

bool gOptForceInt

Definition at line 237 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().

◆ gOptInpXSecFile

string gOptInpXSecFile

Definition at line 240 of file gEvGen.cxx.

◆ gOptNevents

int gOptNevents

Definition at line 228 of file gEvGen.cxx.

Referenced by GenerateEventsAtFixedInitState(), GetCommandLineArgs(), and main().

◆ gOptNuEnergy

double gOptNuEnergy

Definition at line 229 of file gEvGen.cxx.

Referenced by GenerateEventsAtFixedInitState(), and GetCommandLineArgs().

◆ gOptNuEnergyRange

double gOptNuEnergyRange

Definition at line 230 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().

◆ gOptNuPdgCode

int gOptNuPdgCode

Definition at line 231 of file gEvGen.cxx.

Referenced by GenerateEventsAtFixedInitState(), and GetCommandLineArgs().

◆ gOptOutFileName

◆ gOptRanSeed

long int gOptRanSeed

Definition at line 239 of file gEvGen.cxx.

◆ gOptRunNu

Long_t gOptRunNu

Definition at line 233 of file gEvGen.cxx.

◆ gOptStatFileName

string gOptStatFileName

Definition at line 242 of file gEvGen.cxx.

Referenced by GenerateEventsAtFixedInitState(), and GetCommandLineArgs().

◆ gOptTgtMix

map<int,double> gOptTgtMix

Definition at line 232 of file gEvGen.cxx.

◆ gOptUsingFluxOrTgtMix

bool gOptUsingFluxOrTgtMix = false

Definition at line 238 of file gEvGen.cxx.

Referenced by GetCommandLineArgs(), and main().

◆ gOptWeighted

bool gOptWeighted

Definition at line 236 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().

◆ kDefOptNevents

int kDefOptNevents = 0

Definition at line 223 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().

◆ kDefOptNtpFormat

NtpMCFormat_t kDefOptNtpFormat = kNFGHEP

Definition at line 224 of file gEvGen.cxx.

◆ kDefOptRunNu

Long_t kDefOptRunNu = 0

Definition at line 225 of file gEvGen.cxx.

Referenced by GetCommandLineArgs().