EventGenerator.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5  or see $GENIE/LICENSE
6 
7  Author: Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
8  University of Liverpool & STFC Rutherford Appleton Lab
9 
10  For the class documentation see the corresponding header file.
11 
12  Important revisions after version 2.0.0 :
13  @ Mar 05, 2010 - CA
14  User options regarding whether to pass-through unphysical events take
15  precedence over processing instructions read from caught exception.
16  @ Feb 01, 2013 - CA
17  The GUNPHYSMASK env. var is no longer used. The bit-field mask is stored
18  in the GHEP record and GHepRecord::Accept() is now checked.
19 */
20 //____________________________________________________________________________
21 
22 #include <cassert>
23 #include <sstream>
24 #include <cstdlib>
25 #include <algorithm>
26 
27 #include <TMath.h>
28 #include <TBits.h>
29 #include <TSystem.h>
30 #include <TStopwatch.h>
31 
44 
45 using std::ostringstream;
46 
47 using namespace genie;
48 using namespace genie::utils;
49 using namespace genie::controls;
50 using namespace genie::exceptions;
51 
52 //___________________________________________________________________________
54 EventGeneratorI("genie::EventGenerator")
55 {
56  this->Init();
57 }
58 //___________________________________________________________________________
60 EventGeneratorI("genie::EventGenerator", config)
61 {
62  this->Init();
63 }
64 //___________________________________________________________________________
66 {
67  delete fWatch;
68  delete fFiltUnphysMask;
69 
70  if(fEVGModuleVec) delete fEVGModuleVec;
71  if(fEVGTime) delete fEVGTime;
72  if(fVldContext) delete fVldContext;
73 }
74 //___________________________________________________________________________
76 {
77  LOG("EventGenerator", pNOTICE) << "Generating Event...";
78 
79  //-- Clear previous virtual list folder
80  LOG("EventGenerator", pNOTICE) << "Clearing the GHepVirtualListFolder";
82  vlfolder->Clear();
83 
84  //-- Clean previous history + add the bootstrap record in the buffer
86  fRecHistory.AddSnapshot(-1, event_rec);
87 
88  //-- Initialize evg thread control flags
89  bool ffwd = false;
90  unsigned int nexceptions = 0;
91 
92  //-- Reset stop-watch
93  fWatch->Reset();
94 
95  string mesgh = "Event generation thread: " + this->Id().Key() +
96  " -> Running module: ";
97 
98  //-- Loop over the event record processing modules
99  int istep=0;
100  vector<const EventRecordVisitorI *>::const_iterator miter;
101 
102  for(miter = fEVGModuleVec->begin();
103  miter != fEVGModuleVec->end(); ++miter)
104  {
105  const EventRecordVisitorI * visitor = *miter; // generation module
106 
107  string mesg = mesgh + visitor->Id().Key();
108  LOG("EventGenerator", pNOTICE)
109  << utils::print::PrintFramedMesg(mesg,0,'~');
110  if(ffwd) {
111  LOG("EventGenerator", pNOTICE)
112  << "Fast Forward flag was set - Skipping processing step!";
113  continue;
114  }
115  try
116  {
117  fWatch->Start();
118  visitor->ProcessEventRecord(event_rec);
119  fWatch->Stop();
120  fRecHistory.AddSnapshot(istep, event_rec);
121  (*fEVGTime)[istep] = fWatch->CpuTime(); // sec
122  }
124  {
125  LOG("EventGenerator", pNOTICE)
126  << "An exception was thrown and caught by EventGenerator!";
127  LOG("EventGenerator", pNOTICE) << exception;
128 
129  nexceptions++;
130  if ( nexceptions > kMaxEVGThreadExceptions ) {
131  LOG("EventGenerator", pFATAL)
132  << "Caught max allowed number (" << kMaxEVGThreadExceptions
133  << ") of EVGThreadExceptions/thread. Aborting";
134  LOG("EventGenerator", pFATAL) << "Event : \n" << *event_rec ;
135  exit(1);
136  }
137 
138  //
139  // What should I do with this exception?
140  // Check whether the user wants to get this unphysical event anyway
141  // If not, follow instructions coming with the expection thrown:
142  // Either step back to a particular processing step and try re-generating
143  // the event or pass it through
144  //
145 
146  // check whether user wants this type of unphysical events passed through
147  bool accept = event_rec->Accept();
148  if(accept) {
149  LOG("EventGenerator", pWARN)
150  << "An unphysical event was generated and was accepted by the user";
151  break;
152  } else {
153  LOG("EventGenerator", pWARN)
154  << "An unphysical event was generated and was rejected";
155  }
156 
157  // now, follow instructions contained in the exception itself
158 
159  // make sure we are not asked to go at both directions...
160  assert( !(exception.FastForward() && exception.StepBack()) );
161 
162  ffwd = exception.FastForward();
163  if(exception.StepBack()) {
164 
165  // get return step (if return_step > current_step just ignore it)
166  if(exception.ReturnStep() >= 0 && exception.ReturnStep() <= istep) {
167 
168  int rstep = exception.ReturnStep();
169  LOG("EventGenerator", pNOTICE)
170  << "Return at processing step " << rstep;
171  advance(miter, rstep-istep-1);
172  istep = rstep;
173 
174  // restore the event record as it was just before the processing
175  // step we are about to return to
176  LOG("EventGenerator", pNOTICE)
177  << "Restoring GHEP as it was just before the return step";
178  event_rec->ResetRecord();
179  istep--;
180  GHepRecord * snapshot = fRecHistory[istep];
182  event_rec->Copy(*snapshot);
183  } // valid-return-step
184  } // step-back
185  } // catch exception
186 
187  istep++;
188  }
189 
190  LOG("EventGenerator", pNOTICE)
191  << utils::print::PrintFramedMesg("Thread Summary",0,'*');
192  LOG("EventGenerator", pNOTICE)
193  << "The EventRecord was visited by all EventRecordVisitors";
194 
195  LOG("EventGenerator", pINFO) << "** Event generation timing info **";
196  istep=0;
197  for(miter = fEVGModuleVec->begin();
198  miter != fEVGModuleVec->end(); ++miter){
199  const EventRecordVisitorI * visitor = *miter;
200 
201  BLOG("EventGenerator", pINFO)
202  << "module " << visitor->Id().Key() << " -> ~"
203  << TMath::Max(0.,(*fEVGTime)[istep++]) << " s";
204  }
205  LOG("EventGenerator", pNOTICE) << "Done generating event!";
206 }
207 //___________________________________________________________________________
209 {
210  return fIntListGen;
211 }
212 //___________________________________________________________________________
214 {
215  return fXSecModel;
216 }
217 //___________________________________________________________________________
219 {
220  Algorithm::Configure(config);
221  this->LoadConfig();
222 }
223 //___________________________________________________________________________
224 void EventGenerator::Configure(string param_set)
225 {
226  Algorithm::Configure(param_set);
227 
228  AddLowRegistry( AlgConfigPool::Instance() -> GlobalParameterList(), false ) ;
229 
230  this->LoadConfig();
231 }
232 //___________________________________________________________________________
234 {
235  return *fVldContext;
236 }
237 //___________________________________________________________________________
239 {
240  fWatch = new TStopwatch;
241  fVldContext = 0;
242  fEVGModuleVec = 0;
243  fEVGTime = 0;
244  fXSecModel = 0;
245  fIntListGen = 0;
246 
247  fFiltUnphysMask = new TBits(GHepFlags::NFlags());
248  fFiltUnphysMask->ResetAllBits(false);
249 
250  if (gSystem->Getenv("GUNPHYSMASK")) {
251  unsigned int i=0;
252  const char * bitfield = gSystem->Getenv("GUNPHYSMASK");
253 
254  while(bitfield[i]) {
255  bool flag = (bitfield[i]=='1');
256 
257  if(i<GHepFlags::NFlags()) fFiltUnphysMask->SetBitNumber(i,flag);
258  i++;
259  }
260  }
261  LOG("EventGenerator", pDEBUG)
262  << "Initializing unphysical event mask (" << GHepFlags::NFlags()
263  << "->0) = " << *fFiltUnphysMask;
264 }
265 //___________________________________________________________________________
267 {
268  if(fEVGModuleVec) delete fEVGModuleVec;
269  if(fEVGTime) delete fEVGTime;
270  if(fVldContext) delete fVldContext;
271 
272  LOG("EventGenerator", pDEBUG) << "Loading the generator validity context";
273 
274  string encoded_vld_context ;
275  GetParam("VldContext", encoded_vld_context ) ;
276  fVldContext = new GVldContext;
277  fVldContext->Decode( encoded_vld_context );
278 
279  LOG("EventGenerator", pDEBUG) << "Loading the event generation modules";
280 
281  int nsteps ;
282  GetParam("NModules", nsteps) ;
283  if(nsteps == 0) {
284  LOG("EventGenerator", pFATAL)
285  << "EventGenerator configuration declares null visitor list!";
286  }
287  assert(nsteps>0);
288 
289  fEVGModuleVec = new vector<const EventRecordVisitorI *> (nsteps);
290  fEVGTime = new vector<double>(nsteps);
291 
292  for(int istep = 0; istep < nsteps; istep++) {
293 
294  ostringstream keystream;
295  keystream << "Module-" << istep;
296  RgKey key = keystream.str();
297 
298  RgAlg temp_alg ;
299  GetParam( key, temp_alg ) ;
300 
301  SLOG("EventGenerator", pINFO)
302  << " -- Loading module " << istep << " : " << temp_alg ;
303 
304  const EventRecordVisitorI * visitor =
305  dynamic_cast<const EventRecordVisitorI *>(this->SubAlg(key));
306 
307  (*fEVGModuleVec)[istep] = visitor;
308  (*fEVGTime)[istep] = 0;
309  }
310 
311  //-- load the interaction list generator
312  RgKey ikey = "ILstGen";
313  RgAlg ialg ;
314  GetParam( ikey, ialg ) ;
315  LOG("EventGenerator", pINFO)
316  << " -- Loading the interaction list generator: " << ialg;
317  fIntListGen =
318  dynamic_cast<const InteractionListGeneratorI *> (this->SubAlg(ikey));
320 
321  //-- load the cross section model
322  RgKey xkey = "XSecModel@" + this->Id().Key();
323  RgAlg xalg ;
324  GetParam( xkey, xalg ) ;
325  LOG("EventGenerator", pINFO)
326  << " -- Loading the cross section model: " << xalg;
327  fXSecModel =
328  dynamic_cast<const XSecAlgorithmI *> (
329  this -> SubAlg( xkey ) ) ;
331 }
332 //___________________________________________________________________________
333 
334 
Cross Section Calculation Interface.
virtual void Copy(const GHepRecord &record)
Definition: GHepRecord.cxx:943
GHepRecordHistory fRecHistory
event record history
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
virtual void ProcessEventRecord(GHepRecord *event_rec) const =0
TBits * fFiltUnphysMask
mask for allowing unphysical events to pass through (if requested)
const InteractionListGeneratorI * IntListGenerator(void) const
Defines the InteractionListGeneratorI interface. Concrete implementations of this interface generate ...
Defines the EventRecordVisitorI interface. Concrete implementations of this interface use the &#39;Visito...
const int nsteps
#define pFATAL
Definition: Messenger.h:57
Defines the EventGeneratorI interface.
void ProcessEventRecord(GHepRecord *event_rec) const
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
vector< double > * fEVGTime
module timing info
Definition: config.py:1
#define BLOG(stream, priority)
A macro that returns the requested log4cpp::Category appending no additional information.
Definition: Messenger.h:215
TStopwatch * fWatch
stopwatch for module timing
void Configure(const Registry &config)
GENIE-defined C++ exceptions.
GVldContext * fVldContext
validity context
void PurgeRecentHistory(int start_step)
An exception thrown by EventRecordVisitorI when the normal processing sequence has to be disrupted (f...
static unsigned int NFlags(void)
Definition: GHepFlags.h:77
virtual bool Accept(void) const
Definition: GHepRecord.cxx:983
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:70
void AddSnapshot(int step, GHepRecord *r)
#define pINFO
Definition: Messenger.h:63
virtual void ResetRecord(void)
Definition: GHepRecord.cxx:910
A singleton class to manage all named GHepVirtualLists.
static GHepVirtualListFolder * Instance(void)
Misc GENIE control constants.
#define pWARN
Definition: Messenger.h:61
virtual const AlgId & Id(void) const
Get algorithm ID.
Definition: Algorithm.h:98
string RgKey
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:66
vector< const EventRecordVisitorI * > * fEVGModuleVec
list of modules
exit(0)
assert(nhit_max >=nhit_nbins)
string PrintFramedMesg(string mesg, unsigned int nl=1, const char f='*')
Definition: PrintUtils.cxx:171
static const unsigned int kMaxEVGThreadExceptions
Definition: Controls.h:34
int AddLowRegistry(Registry *rp, bool owns=true)
add registry with lowest priority, also update ownership
Definition: Algorithm.cxx:601
const GVldContext & ValidityContext(void) const
#define pNOTICE
Definition: Messenger.h:62
bool GetParam(const RgKey &name, T &p, bool is_top_call=true) const
const XSecAlgorithmI * CrossSectionAlg(void) const
#define SLOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a short string (using the FUNCTION and...
Definition: Messenger.h:85
GENIE&#39;s GHEP MC event record.
Definition: GHepRecord.h:46
string Key(void) const
Definition: AlgId.h:47
const InteractionListGeneratorI * fIntListGen
generates list of handled interactions
Root of GENIE utility namespaces.
Validity Context for an Event Generator.
Definition: GVldContext.h:38
static AlgConfigPool * Instance()
#define pDEBUG
Definition: Messenger.h:64
void Decode(string encoded_values)
Definition: GVldContext.cxx:48
const XSecAlgorithmI * fXSecModel
xsec model for events handled by thread
const Algorithm * SubAlg(const RgKey &registry_key) const
Definition: Algorithm.cxx:353