Functions | Variables
gNeutronOscEvGen.cxx File Reference
#include <cassert>
#include <cstdlib>
#include <string>
#include <vector>
#include <sstream>
#include <TSystem.h>
#include "Algorithm/AlgFactory.h"
#include "EVGCore/EventRecord.h"
#include "EVGCore/EventGeneratorI.h"
#include "EVGCore/EventRecordVisitorI.h"
#include "EVGDrivers/GMCJMonitor.h"
#include "Messenger/Messenger.h"
#include "Ntuple/NtpWriter.h"
#include "NeutronOsc/NeutronOscMode.h"
#include "NeutronOsc/NeutronOscUtils.h"
#include "Numerical/RandomGen.h"
#include "PDG/PDGCodes.h"
#include "PDG/PDGUtils.h"
#include "PDG/PDGLibrary.h"
#include "Utils/StringUtils.h"
#include "Utils/UnitUtils.h"
#include "Utils/PrintUtils.h"
#include "Utils/AppInit.h"
#include "Utils/RunOpt.h"
#include "Utils/CmdLnArgParser.h"

Go to the source code of this file.

Functions

void GetCommandLineArgs (int argc, char **argv)
 
void PrintSyntax (void)
 
int SelectAnnihilationMode (int pdg_code)
 
int SelectInitState (void)
 
const EventRecordVisitorINeutronOscGenerator (void)
 
int main (int argc, char **argv)
 

Variables

string kDefOptGeomLUnits = "mm"
 
string kDefOptGeomDUnits = "g_cm3"
 
NtpMCFormat_t kDefOptNtpFormat = kNFGHEP
 
string kDefOptEvFilePrefix = "gntp"
 
Long_t gOptRunNu = 1000
 
int gOptNev = 10
 
NeutronOscMode_t gOptDecayMode = kNONull
 
string gOptEvFilePrefix = kDefOptEvFilePrefix
 
bool gOptUsingRootGeom = false
 
map< int, double > gOptTgtMix
 
string gOptRootGeom
 
string gOptRootGeomTopVol = ""
 
double gOptGeomLUnits = 0
 
double gOptGeomDUnits = 0
 
long int gOptRanSeed = -1
 

Function Documentation

void GetCommandLineArgs ( int  argc,
char **  argv 
)

Definition at line 338 of file gNeutronOscEvGen.cxx.

339 {
340  LOG("gevgen_nosc", pINFO) << "Parsing command line arguments";
341 
342  // Common run options.
343  RunOpt::Instance()->ReadFromCommandLine(argc,argv);
344 
345  // Parse run options for this app
346 
347  CmdLnArgParser parser(argc,argv);
348 
349  // help?
350  bool help = parser.OptionExists('h');
351  if(help) {
352  PrintSyntax();
353  exit(0);
354  }
355 
356  // run number
357  if( parser.OptionExists('r') ) {
358  LOG("gevgen_nosc", pDEBUG) << "Reading MC run number";
359  gOptRunNu = parser.ArgAsLong('r');
360  } else {
361  LOG("gevgen_nosc", pDEBUG) << "Unspecified run number - Using default";
362  gOptRunNu = 1000;
363  } //-r
364 
365 
366  // number of events
367  if( parser.OptionExists('n') ) {
368  LOG("gevgen_nosc", pDEBUG)
369  << "Reading number of events to generate";
370  gOptNev = parser.ArgAsInt('n');
371  } else {
372  LOG("gevgen_nosc", pFATAL)
373  << "You need to specify the number of events";
374  PrintSyntax();
375  exit(0);
376  } //-n
377 
378  // decay mode
379  int mode = 0;
380  if( parser.OptionExists('m') ) {
381  LOG("gevgen_nosc", pDEBUG)
382  << "Reading annihilation mode";
383  mode = parser.ArgAsInt('m');
384  }
387  if(!valid_mode) {
388  LOG("gevgen_nosc", pFATAL)
389  << "You need to specify a valid annihilation mode";
390  PrintSyntax();
391  exit(0);
392  } //-m
393 
394  //
395  // geometry
396  //
397 
398  string geom = "";
399  string lunits, dunits;
400  if( parser.OptionExists('g') ) {
401  LOG("gevgen_nosc", pDEBUG) << "Getting input geometry";
402  geom = parser.ArgAsString('g');
403 
404  // is it a ROOT file that contains a ROOT geometry?
405  bool accessible_geom_file =
406  ! (gSystem->AccessPathName(geom.c_str()));
407  if (accessible_geom_file) {
408  gOptRootGeom = geom;
409  gOptUsingRootGeom = true;
410  }
411  } else {
412  LOG("gevgen_nosc", pFATAL)
413  << "No geometry option specified - Exiting";
414  PrintSyntax();
415  exit(1);
416  } //-g
417 
418  if(gOptUsingRootGeom) {
419  // using a ROOT geometry - get requested geometry units
420 
421  // legth units:
422  if( parser.OptionExists('L') ) {
423  LOG("gevgen_nosc", pDEBUG)
424  << "Checking for input geometry length units";
425  lunits = parser.ArgAsString('L');
426  } else {
427  LOG("gevgen_nosc", pDEBUG) << "Using default geometry length units";
428  lunits = kDefOptGeomLUnits;
429  } // -L
430  // density units:
431  if( parser.OptionExists('D') ) {
432  LOG("gevgen_nosc", pDEBUG)
433  << "Checking for input geometry density units";
434  dunits = parser.ArgAsString('D');
435  } else {
436  LOG("gevgen_nosc", pDEBUG) << "Using default geometry density units";
437  dunits = kDefOptGeomDUnits;
438  } // -D
441 
442  // check whether an event generation volume name has been
443  // specified -- default is the 'top volume'
444  if( parser.OptionExists('t') ) {
445  LOG("gevgen_nosc", pDEBUG) << "Checking for input volume name";
446  gOptRootGeomTopVol = parser.ArgAsString('t');
447  } else {
448  LOG("gevgen_nosc", pDEBUG) << "Using the <master volume>";
449  } // -t
450 
451  } // using root geom?
452 
453  else {
454  // User has specified a target mix.
455  // Decode the list of target pdf codes & their corresponding weight fraction
456  // (specified as 'pdg_code_1[fraction_1],pdg_code_2[fraction_2],...')
457  // See documentation on top section of this file.
458  //
459  gOptTgtMix.clear();
460  vector<string> tgtmix = utils::str::Split(geom,",");
461  if(tgtmix.size()==1) {
462  int pdg = atoi(tgtmix[0].c_str());
463  double wgt = 1.0;
464  gOptTgtMix.insert(map<int, double>::value_type(pdg, wgt));
465  } else {
466  vector<string>::const_iterator tgtmix_iter = tgtmix.begin();
467  for( ; tgtmix_iter != tgtmix.end(); ++tgtmix_iter) {
468  string tgt_with_wgt = *tgtmix_iter;
469  string::size_type open_bracket = tgt_with_wgt.find("[");
470  string::size_type close_bracket = tgt_with_wgt.find("]");
471  if (open_bracket ==string::npos ||
472  close_bracket==string::npos)
473  {
474  LOG("gevgen_nosc", pFATAL)
475  << "You made an error in specifying the target mix";
476  PrintSyntax();
477  exit(1);
478  }
479  string::size_type ibeg = 0;
480  string::size_type iend = open_bracket;
481  string::size_type jbeg = open_bracket+1;
482  string::size_type jend = close_bracket;
483  int pdg = atoi(tgt_with_wgt.substr(ibeg,iend-ibeg).c_str());
484  double wgt = atof(tgt_with_wgt.substr(jbeg,jend-jbeg).c_str());
485  LOG("gevgen_nosc", pDEBUG)
486  << "Adding to target mix: pdg = " << pdg << ", wgt = " << wgt;
487  gOptTgtMix.insert(map<int, double>::value_type(pdg, wgt));
488 
489  }// tgtmix_iter
490  } // >1 materials in mix
491  } // using tgt mix?
492 
493  // event file prefix
494  if( parser.OptionExists('o') ) {
495  LOG("gevgen_nosc", pDEBUG) << "Reading the event filename prefix";
496  gOptEvFilePrefix = parser.ArgAsString('o');
497  } else {
498  LOG("gevgen_nosc", pDEBUG)
499  << "Will set the default event filename prefix";
501  } //-o
502 
503 
504  // random number seed
505  if( parser.OptionExists("seed") ) {
506  LOG("gevgen_nosc", pINFO) << "Reading random number seed";
507  gOptRanSeed = parser.ArgAsLong("seed");
508  } else {
509  LOG("gevgen_nosc", pINFO) << "Unspecified random number seed - Using default";
510  gOptRanSeed = -1;
511  }
512 
513  //
514  // >>> print the command line options
515  //
516 
517  PDGLibrary * pdglib = PDGLibrary::Instance();
518 
519  ostringstream gminfo;
520  if (gOptUsingRootGeom) {
521  gminfo << "Using ROOT geometry - file: " << gOptRootGeom
522  << ", top volume: "
523  << ((gOptRootGeomTopVol.size()==0) ? "<master volume>" : gOptRootGeomTopVol)
524  << ", length units: " << lunits
525  << ", density units: " << dunits;
526  } else {
527  gminfo << "Using target mix - ";
529  for(iter = gOptTgtMix.begin(); iter != gOptTgtMix.end(); ++iter) {
530  int pdg_code = iter->first;
531  double wgt = iter->second;
532  TParticlePDG * p = pdglib->Find(pdg_code);
533  if(p) {
534  string name = p->GetName();
535  gminfo << "(" << name << ") -> " << 100*wgt << "% / ";
536  }//p?
537  }
538  }
539 
540  LOG("gevgen_nosc", pNOTICE)
541  << "\n\n"
542  << utils::print::PrintFramedMesg("gevgen_nosc job configuration");
543 
544  LOG("gevgen_nosc", pNOTICE)
545  << "\n @@ Run number: " << gOptRunNu
546  << "\n @@ Random number seed: " << gOptRanSeed
547  << "\n @@ Decay channel $ " << utils::neutron_osc::AsString(gOptDecayMode)
548  << "\n @@ Geometry $ " << gminfo.str()
549  << "\n @@ Statistics $ " << gOptNev << " events";
550 
551  //
552  // Temporary warnings...
553  //
554  if(gOptUsingRootGeom) {
555  LOG("gevgen_nosc", pWARN)
556  << "\n ** ROOT geometries not supported yet in neutron oscillation mode"
557  << "\n ** (they will be in the very near future)"
558  << "\n ** Please specify a `target mix' instead.";
559  gAbortingInErr = true;
560  exit(1);
561  }
562 }
bool gOptUsingRootGeom
string gOptEvFilePrefix
#define pFATAL
Definition: Messenger.h:47
long int gOptRanSeed
std::string AsString(TrkPoint_t trkpt)
NeutronOscMode_t gOptDecayMode
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
intermediate_table::const_iterator const_iterator
Long_t gOptRunNu
bool IsValidMode(NeutronOscMode_t ndm)
string kDefOptEvFilePrefix
double UnitFromString(string u)
#define pINFO
Definition: Messenger.h:53
#define pWARN
Definition: Messenger.h:51
string gOptRootGeomTopVol
p
Definition: test.py:228
void PrintSyntax(void)
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
Singleton class to load & serve a TDatabasePDG.
Definition: PDGLibrary.h:27
string kDefOptGeomDUnits
TParticlePDG * Find(int pdgc)
Definition: PDGLibrary.cxx:63
map< int, double > gOptTgtMix
string PrintFramedMesg(string mesg, unsigned int nl=1, const char f='*')
Definition: PrintUtils.cxx:171
string gOptRootGeom
double gOptGeomLUnits
Command line argument parser.
#define pNOTICE
Definition: Messenger.h:52
enum genie::ENeutronOscMode NeutronOscMode_t
bool gAbortingInErr
Definition: Messenger.cxx:56
int gOptNev
double gOptGeomDUnits
string kDefOptGeomLUnits
#define pDEBUG
Definition: Messenger.h:54
int main ( int  argc,
char **  argv 
)

Definition at line 183 of file gNeutronOscEvGen.cxx.

184 {
185  // Parse command line arguments
186  GetCommandLineArgs(argc,argv);
187 
188  // Init messenger and random number seed
189  utils::app_init::MesgThresholds(RunOpt::Instance()->MesgThresholdFiles());
191 
192  // Initialize an Ntuple Writer to save GHEP records into a TTree
194  ntpw.CustomizeFilenamePrefix(gOptEvFilePrefix);
195  ntpw.Initialize();
196 
197  // Create a MC job monitor for a periodically updated status file
198  GMCJMonitor mcjmonitor(gOptRunNu);
199  mcjmonitor.SetRefreshRate(RunOpt::Instance()->MCJobStatusRefreshRate());
200 
201  // Set GHEP print level
202  GHepRecord::SetPrintLevel(RunOpt::Instance()->EventRecordPrintLevel());
203 
204  // Get the nucleon decay generator
205  const EventRecordVisitorI * mcgen = NeutronOscGenerator();
206 
207  // Event loop
208  int ievent = 0;
209  while (1)
210  {
211  if(ievent == gOptNev) break;
212 
213  LOG("gevgen_nosc", pNOTICE)
214  << " *** Generating event............ " << ievent;
215 
216  EventRecord * event = new EventRecord;
217  int target = SelectInitState();
218  int decay = SelectAnnihilationMode(target);
219  Interaction * interaction = Interaction::NOsc(target,decay);
220  event->AttachSummary(interaction);
221 
222  // Simulate decay
223  mcgen->ProcessEventRecord(event);
224 
225  LOG("gevgen_nosc", pINFO)
226  << "Generated event: " << *event;
227 
228  // Add event at the output ntuple, refresh the mc job monitor & clean-up
229  ntpw.AddEventRecord(ievent, event);
230  mcjmonitor.Update(ievent,event);
231  delete event;
232 
233  ievent++;
234  } // event loop
235 
236  // Save the generated event tree & close the output file
237  ntpw.Save();
238 
239  LOG("gevgen_nosc", pNOTICE) << "Done!";
240 
241  return 0;
242 }
void RandGen(long int seed)
Definition: AppInit.cxx:37
const EventRecordVisitorI * NeutronOscGenerator(void)
virtual void ProcessEventRecord(GHepRecord *event_rec) const =0
int SelectAnnihilationMode(int pdg_code)
Defines the EventRecordVisitorI interface. Concrete implementations of this interface use the &#39;Visito...
string gOptEvFilePrefix
long int gOptRanSeed
Summary information for an interaction.
Definition: Interaction.h:53
Simple class to create & update MC job status files and env. vars. This is used to be able to keep tr...
Definition: GMCJMonitor.h:30
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
Long_t gOptRunNu
void GetCommandLineArgs(int argc, char **argv)
NtpMCFormat_t kDefOptNtpFormat
#define pINFO
Definition: Messenger.h:53
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:38
A utility class to facilitate creating the GENIE MC Ntuple from the output GENIE GHEP event records...
Definition: NtpWriter.h:40
int SelectInitState(void)
void MesgThresholds(string inpfile)
#define pNOTICE
Definition: Messenger.h:52
int gOptNev
Event finding and building.
const EventRecordVisitorI * NeutronOscGenerator ( void  )

Definition at line 323 of file gNeutronOscEvGen.cxx.

324 {
325  string sname = "genie::EventGenerator";
326  string sconfig = "NeutronOsc";
327  AlgFactory * algf = AlgFactory::Instance();
328  const EventRecordVisitorI * mcgen =
329  dynamic_cast<const EventRecordVisitorI *> (algf->GetAlgorithm(sname,sconfig));
330  if(!mcgen) {
331  LOG("gevgen_nosc", pFATAL) << "Couldn't instantiate the neutron oscillation generator";
332  gAbortingInErr = true;
333  exit(1);
334  }
335  return mcgen;
336 }
Defines the EventRecordVisitorI interface. Concrete implementations of this interface use the &#39;Visito...
#define pFATAL
Definition: Messenger.h:47
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
const Algorithm * GetAlgorithm(const AlgId &algid)
Definition: AlgFactory.cxx:86
The GENIE Algorithm Factory.
Definition: AlgFactory.h:40
bool gAbortingInErr
Definition: Messenger.cxx:56
void PrintSyntax ( void  )

Definition at line 564 of file gNeutronOscEvGen.cxx.

565 {
566  LOG("gevgen_nosc", pFATAL)
567  << "\n **Syntax**"
568  << "\n gevgen_nosc [-h] "
569  << "\n [-r run#]"
570  << "\n -m decay_mode"
571  << "\n -g geometry"
572  << "\n [-t top_volume_name_at_geom]"
573  << "\n [-L length_units_at_geom]"
574  << "\n [-D density_units_at_geom]"
575  << "\n -n n_of_events "
576  << "\n [-o output_event_file_prefix]"
577  << "\n [--seed random_number_seed]"
578  << "\n [--message-thresholds xml_file]"
579  << "\n [--event-record-print-level level]"
580  << "\n [--mc-job-status-refresh-rate rate]"
581  << "\n"
582  << " Please also read the detailed documentation at http://www.genie-mc.org"
583  << " or look at the source code: $GENIE/src/support/ndcy/EvGen/gNeutronOscEvGen.cxx"
584  << "\n";
585 }
#define pFATAL
Definition: Messenger.h:47
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
int SelectAnnihilationMode ( int  pdg_code)

Definition at line 244 of file gNeutronOscEvGen.cxx.

245 {
246  // if the mode is set to 'random' (the default), pick one at random!
247  if (gOptDecayMode == kNORandom) {
248  int mode;
249 
250  std::string pdg_string = std::to_string(static_cast<long long>(pdg_code));
251  if (pdg_string.size() != 10) {
252  LOG("neutron_osc", pERROR)
253  << "Expecting PDG code to be a 10-digit integer; instead, it's the following: " << pdg_string;
254  gAbortingInErr = true;
255  exit(1);
256  }
257 
258  // count number of protons & neutrons
259  int n_nucleons = std::stoi(pdg_string.substr(6,3)) - 1;
260  int n_protons = std::stoi(pdg_string.substr(3,3));
261 
262  // factor proton / neutron ratio into branching ratios
263  double proton_frac = ((double)n_protons) / ((double)n_nucleons);
264  double neutron_frac = 1 - proton_frac;
265 
266  // set branching ratios, taken from bubble chamber data
267  const int n_modes = 16;
268  double br [n_modes] = { 0.010, 0.080, 0.100, 0.220,
269  0.360, 0.160, 0.070, 0.020,
270  0.015, 0.065, 0.110, 0.280,
271  0.070, 0.240, 0.100, 0.100 };
272 
273  for (int i = 0; i < n_modes; i++) {
274  if (i < 7)
275  br[i] *= proton_frac;
276  else
277  br[i] *= neutron_frac;
278  }
279 
280  // randomly generate a number between 1 and 0
281  RandomGen * rnd = RandomGen::Instance();
282  rnd->SetSeed(0);
283  double p = rnd->RndNum().Rndm();
284 
285  // loop through all modes, figure out which one our random number corresponds to
286  double threshold = 0;
287  for (int i = 0; i < n_modes; i++) {
288  threshold += br[i];
289  if (p < threshold) {
290  // once we've found our mode, return it!
291  mode = i + 1;
292  return mode;
293  }
294  }
295 
296  // error message, in case the random number selection fails
297  LOG("gevgen_nosc", pFATAL) << "Random selection of final state failed!";
298  gAbortingInErr = true;
299  exit(1);
300  }
301 
302  // if specific annihilation mode specified, just use that
303  else {
304  int mode = (int) gOptDecayMode;
305  return mode;
306  }
307 }
#define pERROR
Definition: Messenger.h:50
std::string string
Definition: nybbler.cc:12
#define pFATAL
Definition: Messenger.h:47
A singleton holding random number generator classes. All random number generation in GENIE should tak...
Definition: RandomGen.h:30
NeutronOscMode_t gOptDecayMode
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
TRandom3 & RndNum(void) const
rnd number generator used by MC integrators & other numerical methods
Definition: RandomGen.h:78
p
Definition: test.py:228
bool gAbortingInErr
Definition: Messenger.cxx:56
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void SetSeed(long int seed)
Definition: RandomGen.cxx:90
int SelectInitState ( void  )

Definition at line 309 of file gNeutronOscEvGen.cxx.

310 {
311  if (gOptTgtMix.size() > 1) {
312  LOG("gevgen_nosc", pERROR)
313  << "Target mix not currently supported. You must specify a single target nucleus!";
314  gAbortingInErr = true;
315  exit(1);
316  }
317 
318  int pdg_code = gOptTgtMix.begin()->first;
319 
320  return pdg_code;
321 }
#define pERROR
Definition: Messenger.h:50
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:87
map< int, double > gOptTgtMix
bool gAbortingInErr
Definition: Messenger.cxx:56

Variable Documentation

NeutronOscMode_t gOptDecayMode = kNONull

Definition at line 172 of file gNeutronOscEvGen.cxx.

string gOptEvFilePrefix = kDefOptEvFilePrefix

Definition at line 173 of file gNeutronOscEvGen.cxx.

double gOptGeomDUnits = 0

Definition at line 179 of file gNeutronOscEvGen.cxx.

double gOptGeomLUnits = 0

Definition at line 178 of file gNeutronOscEvGen.cxx.

int gOptNev = 10

Definition at line 171 of file gNeutronOscEvGen.cxx.

long int gOptRanSeed = -1

Definition at line 180 of file gNeutronOscEvGen.cxx.

string gOptRootGeom

Definition at line 176 of file gNeutronOscEvGen.cxx.

string gOptRootGeomTopVol = ""

Definition at line 177 of file gNeutronOscEvGen.cxx.

Long_t gOptRunNu = 1000

Definition at line 170 of file gNeutronOscEvGen.cxx.

map<int,double> gOptTgtMix

Definition at line 175 of file gNeutronOscEvGen.cxx.

bool gOptUsingRootGeom = false

Definition at line 174 of file gNeutronOscEvGen.cxx.

string kDefOptEvFilePrefix = "gntp"

Definition at line 167 of file gNeutronOscEvGen.cxx.

string kDefOptGeomDUnits = "g_cm3"

Definition at line 165 of file gNeutronOscEvGen.cxx.

string kDefOptGeomLUnits = "mm"

Definition at line 164 of file gNeutronOscEvGen.cxx.

NtpMCFormat_t kDefOptNtpFormat = kNFGHEP

Definition at line 166 of file gNeutronOscEvGen.cxx.