Functions | Variables
gNNBarOscEvGen.cxx File Reference
#include <cassert>
#include <cstdlib>
#include <string>
#include <vector>
#include <sstream>
#include <TSystem.h>
#include "Framework/Algorithm/AlgFactory.h"
#include "Framework/EventGen/EventRecord.h"
#include "Framework/EventGen/EventGeneratorI.h"
#include "Framework/EventGen/EventRecordVisitorI.h"
#include "Framework/EventGen/GMCJMonitor.h"
#include "Framework/Messenger/Messenger.h"
#include "Framework/Ntuple/NtpWriter.h"
#include "Physics/NNBarOscillation/NNBarOscMode.h"
#include "Physics/NNBarOscillation/NNBarOscUtils.h"
#include "Framework/Numerical/RandomGen.h"
#include "Framework/ParticleData/PDGCodes.h"
#include "Framework/ParticleData/PDGUtils.h"
#include "Framework/ParticleData/PDGLibrary.h"
#include "Framework/Utils/StringUtils.h"
#include "Framework/Utils/UnitUtils.h"
#include "Framework/Utils/PrintUtils.h"
#include "Framework/Utils/AppInit.h"
#include "Framework/Utils/RunOpt.h"
#include "Framework/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
 
NNBarOscMode_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 339 of file gNNBarOscEvGen.cxx.

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

Definition at line 183 of file gNNBarOscEvGen.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_nnbar_osc", 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_nnbar_osc", 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_nnbar_osc", pNOTICE) << "Done!";
240 
241  return 0;
242 }
void RandGen(long int seed)
Definition: AppInit.cxx:30
void GetCommandLineArgs(int argc, char **argv)
const EventRecordVisitorI * NeutronOscGenerator(void)
virtual void ProcessEventRecord(GHepRecord *event_rec) const =0
Defines the EventRecordVisitorI interface. Concrete implementations of this interface use the &#39;Visito...
int SelectAnnihilationMode(int pdg_code)
string gOptEvFilePrefix
Summary information for an interaction.
Definition: Interaction.h:56
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
int gOptNev
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
#define pINFO
Definition: Messenger.h:62
Long_t gOptRunNu
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:37
NtpMCFormat_t kDefOptNtpFormat
A utility class to facilitate creating the GENIE MC Ntuple from the output GENIE GHEP event records...
Definition: NtpWriter.h:39
long int gOptRanSeed
void MesgThresholds(string inpfile)
Definition: AppInit.cxx:99
int SelectInitState(void)
#define pNOTICE
Definition: Messenger.h:61
Event finding and building.
const EventRecordVisitorI * NeutronOscGenerator ( void  )

Definition at line 323 of file gNNBarOscEvGen.cxx.

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

Definition at line 565 of file gNNBarOscEvGen.cxx.

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

Definition at line 244 of file gNNBarOscEvGen.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("gevgen_nnbar_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_nnbar_osc", 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:59
std::string string
Definition: nybbler.cc:12
#define pFATAL
Definition: Messenger.h:56
A singleton holding random number generator classes. All random number generation in GENIE should tak...
Definition: RandomGen.h:29
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
p
Definition: test.py:223
NNBarOscMode_t gOptDecayMode
TRandom3 & RndNum(void) const
rnd number generator used by MC integrators & other numerical methods
Definition: RandomGen.h:77
bool gAbortingInErr
Definition: Messenger.cxx:34
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void SetSeed(long int seed)
Definition: RandomGen.cxx:82
int SelectInitState ( void  )

Definition at line 309 of file gNNBarOscEvGen.cxx.

310 {
311  if (gOptTgtMix.size() > 1) {
312  LOG("gevgen_nnbar_osc", 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:59
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
map< int, double > gOptTgtMix
bool gAbortingInErr
Definition: Messenger.cxx:34

Variable Documentation

NNBarOscMode_t gOptDecayMode = kNONull

Definition at line 172 of file gNNBarOscEvGen.cxx.

string gOptEvFilePrefix = kDefOptEvFilePrefix

Definition at line 173 of file gNNBarOscEvGen.cxx.

double gOptGeomDUnits = 0

Definition at line 179 of file gNNBarOscEvGen.cxx.

double gOptGeomLUnits = 0

Definition at line 178 of file gNNBarOscEvGen.cxx.

int gOptNev = 10

Definition at line 171 of file gNNBarOscEvGen.cxx.

long int gOptRanSeed = -1

Definition at line 180 of file gNNBarOscEvGen.cxx.

string gOptRootGeom

Definition at line 176 of file gNNBarOscEvGen.cxx.

string gOptRootGeomTopVol = ""

Definition at line 177 of file gNNBarOscEvGen.cxx.

Long_t gOptRunNu = 1000

Definition at line 170 of file gNNBarOscEvGen.cxx.

map<int,double> gOptTgtMix

Definition at line 175 of file gNNBarOscEvGen.cxx.

bool gOptUsingRootGeom = false

Definition at line 174 of file gNNBarOscEvGen.cxx.

string kDefOptEvFilePrefix = "gntp"

Definition at line 167 of file gNNBarOscEvGen.cxx.

string kDefOptGeomDUnits = "g_cm3"

Definition at line 165 of file gNNBarOscEvGen.cxx.

string kDefOptGeomLUnits = "mm"

Definition at line 164 of file gNNBarOscEvGen.cxx.

NtpMCFormat_t kDefOptNtpFormat = kNFGHEP

Definition at line 166 of file gNNBarOscEvGen.cxx.