LBNERunAction.cc
Go to the documentation of this file.
1 //
2 // LBNERunAction.cc
3 //
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <ios>
7 #include <iostream>
8 #include <fstream>
9 #include <string>
10 
11 #include "G4Run.hh"
12 #include "G4UImanager.hh"
13 #include "G4VVisManager.hh"
14 #include "LBNERunAction.hh"
16 #include "LBNEAnalysis.hh"
17 #include "LBNETrajectory.hh"
18 #include "Randomize.hh"
19 #include "LBNERunManager.hh"
21 #include "LBNESteppingAction.hh"
22 #include "G4ProcessTable.hh"
23 #include "G4ExceptionSeverity.hh"
24 #include "LBNEQuickPiToNu.hh"
25 #include "LBNFDeDxMap.hh"
26 
27 //------------------------------------------------------------------------------
29 fVerboseLevel(1),
30 fDoComputeEDepInGraphite(false),
31 fDoComputeEDepInArgonGas(false),
32 fMeanEDepInGraphite(0.),
33 fRMSEDepInGraphite(0.),
34 fMeanEDepInArgonGasHorn1(0.),
35 fRMSEDepInArgonGasHorn1(0.),
36 fMeanEDepInArgonGasHorn2(0.),
37 fRMSEDepInArgonGasHorn2(0.),
38 fDoComputeEDepInHorns(false),
39 fG4MARSDeDxMap(0)
40 {
41 
42  if(fVerboseLevel > 0)
43  {
44  std::cout << "LBNERunAction Constructor Called." << std::endl;
45  }
46 
47 
49 }
50 
51 //------------------------------------------------------------------------------
53 {
54  if(fVerboseLevel > 0)
55  {
56  std::cout << "LBNERunAction Destructor Called." << std::endl;
57  }
58 
59  delete runMessenger;
60 }
61 
62 //------------------------------------------------------------------------------
63 void LBNERunAction::BeginOfRunAction(const G4Run* aRun)
64 {
65  if(fVerboseLevel > 0)
66  {
67  std::cout << "LBNERunAction::BeginOfRunAction() Called." << std::endl;
68  }
69 
70 
71  LBNERunManager* theRunManager = dynamic_cast<LBNERunManager*>(G4RunManager::GetRunManager());
72 
73  G4String spaces = " ";
74  //std::cout << std::endl;
75  std::cout << "LBNERunAction::BeginOfRunAction() - Initializing Run "
76  << aRun->GetRunID() << "..." << std::endl;
77 
78  //std::cout << spaces << "Preparing \"" << fLBNEData -> GetSimulation() << "\" Simulation." << std::endl;
79 
80  //
81  //Random number generator
82  //
83  G4String randomFile="rndm/beginOfRun_";
84  char runN[4];
85  snprintf(runN, 4, "%03d",aRun->GetRunID());
86  randomFile.append(runN);
87  randomFile.append(".rndm");
88  CLHEP::HepRandom::saveEngineStatus(randomFile);
89  std::cout << spaces << "Intializing Random Number generator named "
90  << CLHEP::HepRandom::getTheEngine()->name() << " ... " << std::endl
91  << spaces << " Seed = " << CLHEP::HepRandom::getTheSeed() << std::endl
92  << spaces << " Saving Random engine status in "<< randomFile << std::endl;
93  //
94 
96  fRMSEDepInGraphite = 0.;
101  //
102  //Open input ntuples set Number of Events to loop over
103  //
104 // LBNEPrimaryGeneratorAction* primaryGeneratorAction = dynamic_cast<LBNEPrimaryGeneratorAction*>(pRunManager->userPrimaryGeneratorAction);
105 // const LBNEPrimaryGeneratorAction* primaryGeneratorAction =
106 // dynamic_cast<const LBNEPrimaryGeneratorAction*>(pRunManager->GetUserPrimaryGeneratorAction());
107 
108  LBNEPrimaryGeneratorAction* primaryGeneratorAction =
109  (theRunManager->GetLBNEPrimaryGeneratorAction());
110 
111 
112  if(!primaryGeneratorAction)
113  {
114  std::cout << spaces << "PROBLEM: INVALID LBNEPrimaryGeneratorAction POINTER" << std::endl;
115 // fLBNEData -> SetOKToRun(false);
116  return;
117  }
118 
119 // if(fLBNEData->GetSimulation() == "Standard Neutrino Beam" ||
120 // fLBNEData->GetSimulation() == "Target Tracking" ||
121 // fLBNEData->GetSimulation() == "Horn 1 Tracking" ||
122 // fLBNEData->GetSimulation() == "Horn 2 Tracking" )
123  {
124 
125  if (theRunManager->GetUseFlukaInput() || theRunManager->GetUseMarsInput())
126  {
127  G4bool ntpOpen = primaryGeneratorAction->OpenNtuple(theRunManager->GetNptInputFileName());
128 //
129  if(!ntpOpen)
130  {
131  std::ostringstream mStrStr;
132  mStrStr << "PROBLEM: FAILED TO OPEN INPUT NTUPLE. Fatal " << std::endl;
133  G4String mStr(mStrStr.str());
134  G4Exception("LBNERunAction::BeginOfRunAction", " ", RunMustBeAborted, mStr.c_str());
135 
136  return;
137  }
138 //
139  std::cout << spaces << "Successfully opened input ntuple \""
140  << theRunManager->GetNptInputFileName() << "\"" << std::endl;
141 //
142  theRunManager->SetNumberOfEventsLBNE(primaryGeneratorAction->GetNoOfPrimaries());
143  // Note there are public accessors in geant4 v4.9.6 inside the G4RunManager... but not in v4.9.3...
144 //
145  }
146  else
147  {
148  primaryGeneratorAction->SetProtonBeam();
149 
150  }
151 
152  }
153  //
154 
155  //
156  //If still ok to run open output file
157  //
158 
160  if(theRunManager -> GetCreateOutput())
161  if(!(analysis->CreateOutput())) {
162  std::ostringstream mStrStr;
163  mStrStr << "PROBLEM: FAILED TO OPEN OUTPUT NTUPLE. Fatal " << std::endl;
164  G4String mStr(mStrStr.str());
165  G4Exception("LBNERunAction::BeginOfRunAction", " ", RunMustBeAborted, mStr.c_str());
166  }
167 
168  if(theRunManager -> GetCreateDk2NuOutput()) {
169  if(!(analysis->CreateDk2NuOutput())) {
170  std::ostringstream mStrStr;
171  mStrStr << "PROBLEM: FAILED TO OPEN Dk2Nu OUTPUT NTUPLE. Fatal " << std::endl;
172  G4String mStr(mStrStr.str());
173  G4Exception("LBNERunAction::BeginOfRunAction", " ", RunMustBeAborted, mStr.c_str());
174  }
175  analysis->fillDkMeta();
176  }
177 
178  std::cout << std::endl;
179  const bool doLBNEQuickPiToNu = false; // default should be false!. It will slow down execution otherise.
180  if (doLBNEQuickPiToNu) {
182 
183  std::ostringstream fNameStr;
184  fNameStr << theRunManager->GetOutputNtpFileName() << "_";
185  fNameStr.setf(std::ios::right); fNameStr.fill('0'); fNameStr.width(3);
186  fNameStr << theRunManager->GetCurrentRun()->GetRunID();
187  std::string fName(fNameStr.str());
188  std::cerr << " QuickPitoNu file name " << fName << std::endl;
190  } // if usefull
191 
192  if (fDoComputeEDepInHorns) {
193  std::string aNameHH("HornAConceptDesignJan2017");
194  fG4MARSDeDxMap = new LBNFDeDxMap(aNameHH);
195  }
196 
197  std::cout << "LBNERunAction::BeginOfRunAction() - ...completed run initialization. " << std::endl;
198  std::cout << std::endl;
199 
200 
201 }
202 
203 //------------------------------------------------------------------------------
204 void LBNERunAction::EndOfRunAction(const G4Run* aRun)
205 {
206 // const bool doLBNEQuickPiToNu = true; // default should be false!. Historical..
208 
209  LBNERunManager* theRunManager = dynamic_cast<LBNERunManager*>(G4RunManager::GetRunManager());
210  if(theRunManager->GetVerboseLevel() > 0)
211  {
212  std::cout << "LBNERunAction::EndOfRunAction() Called." << std::endl;
213  }
214 
215  const LBNESteppingAction *pStep = dynamic_cast<const LBNESteppingAction*>(theRunManager->GetUserSteppingAction());
216  std::cout << " End of Run. Number of tracks killed because stuck "
217  << pStep->GetNumTracksKilledAsStuck() << std::endl;
218 
219 
220 
221  G4String spaces = " ";
222  std::cout << std::endl;
223  std::cout << "LBNERunAction::EndOfRunAction() - Terminating Run "
224  << aRun->GetRunID() << "..." << std::endl;
225 
226  //
227  //Random number generator
228  //
229  G4String randomFile="rndm/endOfRun_";
230  char runN[4];
231  snprintf(runN, 4, "%03d",aRun->GetRunID());
232  randomFile.append(runN);
233  randomFile.append(".rndm");
234  CLHEP::HepRandom::saveEngineStatus(randomFile);
235  std::cout << spaces << "Closing Random Number generator... " << std::endl
236  << spaces << " Seed = " << CLHEP::HepRandom::getTheSeed() << std::endl
237  << spaces << " Random engine status saved in "<< randomFile << std::endl;
238 
239  //
240  //Close Input and Output Ntuple
241  //
242 
243  std::cout << spaces << "Closing Input File... " << std::endl;
244  LBNEPrimaryGeneratorAction* primaryGeneratorAction = (theRunManager->GetLBNEPrimaryGeneratorAction());
245 
246  if(primaryGeneratorAction)
247  {
248  primaryGeneratorAction->CloseNtuple();
249  }
250 
251  if(theRunManager -> GetCreateOutput())
252  {
253  std::cout << spaces << "Closing Output File... " << std::endl;
255  analysis->CloseOutput();
256  }
257  if(theRunManager -> GetCreateDk2NuOutput())
258  {
259  std::cout << spaces << "Closing Dk2Nu Output File... " << std::endl;
261  analysis->CloseDk2NuOutput();
262  }
263  //
265  int numEvts = aRun->GetNumberOfEvent();
266  fMeanEDepInGraphite /= numEvts;
267  double aa = fRMSEDepInGraphite;
268  fRMSEDepInGraphite = (std::sqrt(aa - fMeanEDepInGraphite*fMeanEDepInGraphite*numEvts)/(numEvts - 1));
269  std::cout << "LBNERunAction::EndOfRunAction .. Average energy deposition in target [GeV] "
270  << fMeanEDepInGraphite/CLHEP::GeV << " +- " << fRMSEDepInGraphite/CLHEP::GeV << std::endl;
271  }
273  int numEvts = aRun->GetNumberOfEvent();
274  fMeanEDepInArgonGasHorn1 /= numEvts;
275  double aa = fRMSEDepInArgonGasHorn1;
276  fRMSEDepInArgonGasHorn1 = (std::sqrt(aa - fMeanEDepInArgonGasHorn1*fMeanEDepInArgonGasHorn1*numEvts)/(numEvts - 1));
277  std::cout << "LBNERunAction::EndOfRunAction .. Average energy deposition in Argon Gas Horn1 [MeV] "
278  << fMeanEDepInArgonGasHorn1/CLHEP::MeV << " +- " << fRMSEDepInArgonGasHorn1/CLHEP::MeV << std::endl;
279  fMeanEDepInArgonGasHorn2 /= numEvts;
281  fRMSEDepInArgonGasHorn2 = (std::sqrt(aa - fMeanEDepInArgonGasHorn2*fMeanEDepInArgonGasHorn2*numEvts)/(numEvts - 1));
282  std::cout << "LBNERunAction::EndOfRunAction .. Average energy deposition in Argon Gas Horn2 [MeV] "
283  << fMeanEDepInArgonGasHorn2/CLHEP::MeV << " +- " << fRMSEDepInArgonGasHorn2/CLHEP::MeV << std::endl;
284  }
285 
286  if (fDoComputeEDepInHorns) {
287  std::string aDir("./");
288  const char* aDirGG = getenv("_CONDOR_SCRATCH_DIR");
289  if (aDirGG != 0) aDir = std::string(aDirGG);
290  fG4MARSDeDxMap->dumpASCII(aDir, aRun->GetNumberOfEvent());
291  std::cerr << " Before deleting fG4MARSDeDxMap " << std::endl;
292 // delete(fG4MARSDeDxMap);
293 // std::cerr << " After deleting fG4MARSDeDxMap " << std::endl;
294  }
295 
296  std::cout << "LBNERunAction::EndOfRunAction() - ...completed run termination. " << std::endl;
297  std::cout << std::endl;
298 
299 /*
300 
301  G4cout << G4endl;
302  G4cout << G4endl;
303  G4cout << "********************************************************************" << G4endl;
304  G4cout << "********************************************************************" << G4endl;
305  G4cout << "LBNERunAction::EndOfRunAction..." << G4endl;
306  G4cout << "********************************************************************" << G4endl;
307 
308  G4String randomFile="rndm/endOfRun_";
309  char runN[4];
310  sprintf(runN,"%04d",aRun->GetRunID());
311  randomFile.append(runN);
312  randomFile.append(".rndm");
313  CLHEP::HepRandom::saveEngineStatus(randomFile);
314  G4cout << " Random engine status at the end of the run saved in "<<randomFile<<G4endl;
315  LBNEAnalysis* analysis = LBNEAnalysis::getInstance();
316  analysis->finish();
317 
318  G4cout << "********************************************************************" << G4endl;
319  G4cout << "LBNERunAction::EndOfRunAction - Completed." << G4endl;
320  G4cout << "********************************************************************" << G4endl;
321  G4cout << "********************************************************************" << G4endl;
322  G4cout << G4endl;
323  G4cout << G4endl;
324 */
325 }
326 
327 
328 //----------------------------------------------------------------------------------
329 //----------------------------------------------------------------------------------
330 //----------------------------------------------------------------------------------
331 //----------------------------------------------------------------------------------
332 //----------------------------------------------------------------------------------
333 
334 //------------------------------------------------------------------------------
336 {
337  //check the simulation
338  // Obsolte..
339  /*
340  G4bool knownSim = false;
341  const vstring_t simvec = fLBNEData -> GetListOfSimulations();
342  const G4String simulation = fLBNEData -> GetSimulation();
343 
344  for(vstring_t::const_iterator sit = simvec.begin(); sit != simvec.end(); ++sit)
345  {
346  if (simulation == *sit) { knownSim = true; break;}
347  }
348 
349  if(knownSim) { fLBNEData -> SetOKToRun(true);}
350  else
351  {
352  std::cout << "LBNERunAction::CheckOKToRun() - PROBLEM: Simulation "
353  << "\"" << simulation << "\"" << " unknown. Currently "
354  << "implemented simulations are " << std::endl;
355 
356  for(vstring_t::const_iterator sit = simvec.begin(); sit != simvec.end(); ++sit)
357  {
358  std::cout << " " << *sit << std::endl;
359  }
360  std::cout << std::endl;
361 
362  fLBNEData -> SetOKToRun(false);
363  }
364  */
365  //
366 }
367 void LBNERunAction::CheckMemoryUsage(double VMlimit) const { // in kb (I think..)
368 
369 // http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
370  using std::ios_base;
371  using std::ifstream;
372  using std::string;
373 
374  double vm_usage = 0.0;
375 // double resident_set = 0.0;
376 
377  // 'file' stat seems to give the most reliable results
378  //
379  ifstream stat_stream("/proc/self/stat",ios_base::in);
380 
381  // dummy vars for leading entries in stat that we don't care about
382  //
383  string pid, comm, state, ppid, pgrp, session, tty_nr;
384  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
385  string utime, stime, cutime, cstime, priority, nice;
386  string O, itrealvalue, starttime;
387 
388  // the two fields we want
389  //
390  unsigned long vsize;
391  long rss;
392 
393  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
394  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
395  >> utime >> stime >> cutime >> cstime >> priority >> nice
396  >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
397 
398  stat_stream.close();
399 
400 // long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
401 // Don't care about resident memory..
402 //
403  vm_usage = vsize / 1024.0;
404 // std::cerr << " Current memory usage " << vm_usage << std::endl;
405  if (vm_usage > VMlimit) {
406  std::cerr << " Current memory usage " << vm_usage << " Too much above limit " << VMlimit << std::endl;
407  sleep(10);
408  exit(2);
409  }
410 
411 }
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427 
428 
429 
double fRMSEDepInArgonGasHorn1
void fillDkMeta()
bool fDoComputeEDepInArgonGas
double fRMSEDepInGraphite
bool fDoComputeEDepInHorns
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
LBNFDeDxMap * fG4MARSDeDxMap
LBNEPrimaryGeneratorAction * GetLBNEPrimaryGeneratorAction()
std::string string
Definition: nybbler.cc:12
void dumpASCII(const std::string &aDirectory, int nEvt)
Definition: LBNFDeDxMap.cc:344
void CloseDk2NuOutput()
void SetNumberOfEventsLBNE(int n)
bool GetUseMarsInput() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:45
LBNERunActionMessenger * runMessenger
static LBNEAnalysis * getInstance()
bool fDoComputeEDepInGraphite
double fMeanEDepInArgonGasHorn2
std::string getenv(std::string const &name)
Definition: getenv.cc:15
G4bool OpenNtuple(G4String ntupleName)
double fRMSEDepInArgonGasHorn2
static LBNEQuickPiToNuVect * Instance()
double fMeanEDepInArgonGasHorn1
double fMeanEDepInGraphite
void CheckMemoryUsage(double VMLimit) const
G4String GetOutputNtpFileName() const
void EndOfRunAction(const G4Run *)
void BeginOfRunAction(const G4Run *)
void open(const std::string &prefix)
int GetNumTracksKilledAsStuck() const
int GetVerboseLevel() const
G4bool CreateDk2NuOutput()
bool GetUseFlukaInput() const
G4bool CreateOutput()
G4String GetNptInputFileName() const
QTextStream & endl(QTextStream &s)
void CloseOutput()