From 680ef7c20f0b1d3c687559473bb3f2360370f63e Mon Sep 17 00:00:00 2001 From: adrien-matta <a.matta@surrey.ac.uk> Date: Thu, 16 Apr 2015 10:35:32 +0100 Subject: [PATCH] * Adding Run Action * Adding Run number to output tree * Adding better progress display --- NPSimulation/Core/CMakeLists.txt | 2 +- NPSimulation/Core/EventAction.cc | 62 ++++++++++++++++++++++++++------ NPSimulation/Core/EventAction.hh | 40 +++++++++++++-------- NPSimulation/Core/RunAction.cc | 53 +++++++++++++++++++++++++++ NPSimulation/Core/RunAction.hh | 45 +++++++++++++++++++++++ NPSimulation/Core/Target.cc | 1 + NPSimulation/Simulation.cc | 6 ++++ 7 files changed, 184 insertions(+), 25 deletions(-) create mode 100644 NPSimulation/Core/RunAction.cc create mode 100644 NPSimulation/Core/RunAction.hh diff --git a/NPSimulation/Core/CMakeLists.txt b/NPSimulation/Core/CMakeLists.txt index 6b393c938..376f06e99 100644 --- a/NPSimulation/Core/CMakeLists.txt +++ b/NPSimulation/Core/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(NPSCore SHARED CalorimeterScorers.cc EventAction.cc EventGeneratorParticleDecay.cc ObsoleteGeneralScorers.cc PrimaryGeneratorAction.cc Target.cc Chamber.cc EventGeneratorBeam.cc EventGeneratorTwoBodyReaction.cc Particle.cc PrimaryGeneratorActionMessenger.cc NPSVDetector.cc DetectorConstruction.cc EventGeneratorGammaDecay.cc MaterialManager.cc ParticleStack.cc SiliconScorers.cc VEventGenerator.cc DetectorMessenger.cc EventGeneratorIsotropic.cc MyMagneticField.cc PhysicsList.cc SteppingVerbose.cc NPSDetectorFactory.cc) +add_library(NPSCore SHARED CalorimeterScorers.cc EventAction.cc EventGeneratorParticleDecay.cc ObsoleteGeneralScorers.cc PrimaryGeneratorAction.cc Target.cc Chamber.cc EventGeneratorBeam.cc EventGeneratorTwoBodyReaction.cc Particle.cc PrimaryGeneratorActionMessenger.cc NPSVDetector.cc DetectorConstruction.cc EventGeneratorGammaDecay.cc MaterialManager.cc ParticleStack.cc SiliconScorers.cc VEventGenerator.cc DetectorMessenger.cc EventGeneratorIsotropic.cc MyMagneticField.cc PhysicsList.cc SteppingVerbose.cc NPSDetectorFactory.cc RunAction.cc) target_link_libraries(NPSCore ${ROOT_LIBRARIES} ${Geant4_LIBRARIES} ${NPLib_LIBRARIES}) diff --git a/NPSimulation/Core/EventAction.cc b/NPSimulation/Core/EventAction.cc index d8d1316a0..78195ee83 100644 --- a/NPSimulation/Core/EventAction.cc +++ b/NPSimulation/Core/EventAction.cc @@ -35,27 +35,29 @@ #include<iostream> using namespace std; - -#include "G4THitsMap.hh" - +EventAction* EventAction::m_EventAction=0; //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... - -EventAction::EventAction(): m_printModulo(10000){ +EventAction::EventAction(){ + m_EventAction=this; + begin=clock(); + treated=0; + inter=0; + total=0; + mean_rate=0; + displayed=0; } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... - EventAction::~EventAction(){ } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... void EventAction::BeginOfEventAction(const G4Event* event){ - if ((event->GetEventID() + 1) % m_printModulo == 0) - cout << "\rEvent: " << event->GetEventID() + 1 << flush; + treated= event->GetEventID()+1; + ProgressDisplay(); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... - void EventAction::EndOfEventAction(const G4Event* event){ m_detector->ReadAllSensitive(event) ; RootOutput *pAnalysis = RootOutput::getInstance(); @@ -64,6 +66,46 @@ void EventAction::EndOfEventAction(const G4Event* event){ //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... void EventAction::SetDetector(DetectorConstruction* detector){ - m_detector = detector ; + m_detector = detector ; } +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +void EventAction::ProgressDisplay(){ + end = clock(); + if((end-begin)>CLOCKS_PER_SEC||treated>=total ){ + displayed++; + long double elapsed =(long double) (end-begin)/CLOCKS_PER_SEC; + double event_rate = inter/elapsed; + mean_rate += (event_rate-mean_rate)/(displayed); + double percent = 100*treated/total; + double remain = (total-treated)/mean_rate; + + char* timer; + double check; + if(remain>60) + check=asprintf(&timer,"%dmin",(int)(remain/60.)); + else + check=asprintf(&timer,"%ds",(int)(remain)); + + if(treated!=total) + printf("\r \033[1;31m ******* Progress: %.1f%% | Rate: %.1fk evt/s | Remain: %s *******\033[0m", percent,mean_rate/1000.,timer); + + else{ + printf("\r "); + printf("\r \033[1;32m ******* Progress: %.1f%% | Rate: %.1fk evt/s | Remain: %s *******\033[0m", percent,mean_rate/1000.,timer); + } + fflush(stdout); + inter=0; + begin = clock() ; + } + // treated++; + inter++; +} +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +void EventAction::SetRunLength(int length){ + total = length; +} +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +EventAction* EventAction::GetInstance(){ + return m_EventAction; +} diff --git a/NPSimulation/Core/EventAction.hh b/NPSimulation/Core/EventAction.hh index be9ff9baf..f534b29cd 100644 --- a/NPSimulation/Core/EventAction.hh +++ b/NPSimulation/Core/EventAction.hh @@ -30,24 +30,36 @@ // NPTool header #include "DetectorConstruction.hh" -//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... - -class EventAction : public G4UserEventAction -{ -public: - EventAction(); - virtual ~EventAction(); +// STL +#include<time.h> -public: - void BeginOfEventAction(const G4Event*) ; - void EndOfEventAction(const G4Event*) ; - void SetDetector(DetectorConstruction* detector) ; +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +class EventAction : public G4UserEventAction{ + public: + EventAction(); + virtual ~EventAction(); + public: + void BeginOfEventAction(const G4Event*); + void EndOfEventAction(const G4Event*); + void SetDetector(DetectorConstruction* detector); + void ProgressDisplay(); + void SetRunLength(int); + private: // Progress Display + clock_t begin; + clock_t end; + unsigned int treated; + unsigned int inter; + unsigned int total; + double mean_rate; + int displayed; + private: + DetectorConstruction* m_detector; + static EventAction* m_EventAction; -private: - DetectorConstruction* m_detector ; - G4int m_printModulo ; + public: + static EventAction* GetInstance(); }; //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... diff --git a/NPSimulation/Core/RunAction.cc b/NPSimulation/Core/RunAction.cc new file mode 100644 index 000000000..f5bc177ea --- /dev/null +++ b/NPSimulation/Core/RunAction.cc @@ -0,0 +1,53 @@ +/***************************************************************************** + * Copyright (C) 2009-2015 this file is part of the NPTool Project * + * * + * For the licensing terms see $NPTOOL/Licence/NPTool_Licence * + * For the list of contributors see $NPTOOL/Licence/Contributors * + *****************************************************************************/ + +/***************************************************************************** + * Original Author: Adrien MATTA contact address: a.matta@surrey.ac.uk * + * * + * Creation Date : April 2015 * + * Last update : * + *---------------------------------------------------------------------------* + * Decription: * + * A quite Standard Geant4 RunAction class. * + * Use to register the run number in the tree * + *---------------------------------------------------------------------------* + * Comment: * + * * + * * + *****************************************************************************/ +// NPS +#include "RunAction.hh" +#include "PrimaryGeneratorAction.hh" +#include "EventAction.hh" +// G4 +#include "G4Run.hh" +#include "G4RunManager.hh" +// NPL +#include "RootOutput.h" +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +RunAction::RunAction(): G4UserRunAction(){ + RootOutput::getInstance()->GetTree()->Branch("Run",&m_RunNumber,"Run/I"); +} + +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +RunAction::~RunAction(){ +} + +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +void RunAction::BeginOfRunAction(const G4Run* aRun){ + m_RunNumber = aRun->GetRunID()+1; + + //initialize event cumulative quantities + EventAction::GetInstance()->SetRunLength(aRun->GetNumberOfEventToBeProcessed()); +} + +//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... +void RunAction::EndOfRunAction(const G4Run* aRun){ + const G4Run* x = aRun; + x=0; +} + diff --git a/NPSimulation/Core/RunAction.hh b/NPSimulation/Core/RunAction.hh new file mode 100644 index 000000000..d92ee9507 --- /dev/null +++ b/NPSimulation/Core/RunAction.hh @@ -0,0 +1,45 @@ +#ifndef RunAction_h +#define RunAction_h 1 +/***************************************************************************** + * Copyright (C) 2009-2015 this file is part of the NPTool Project * + * * + * For the licensing terms see $NPTOOL/Licence/NPTool_Licence * + * For the list of contributors see $NPTOOL/Licence/Contributors * + *****************************************************************************/ + +/***************************************************************************** + * Original Author: Adrien MATTA contact address: a.matta@surrey.ac.uk * + * * + * Creation Date : April 2015 * + * Last update : * + *---------------------------------------------------------------------------* + * Decription: * + * A quite Standard Geant4 RunAction class. * + * Use to register the run number in the tree * + *---------------------------------------------------------------------------* + * Comment: * + * * + * * + *****************************************************************************/ +// G4 headers + +#include "G4UserRunAction.hh" +#include "globals.hh" + +class G4Run; + +class RunAction : public G4UserRunAction{ + public: + RunAction(); + virtual ~RunAction(); + + virtual void BeginOfRunAction(const G4Run*); + virtual void EndOfRunAction(const G4Run*); + private: + int m_RunNumber; + +}; + + +#endif + diff --git a/NPSimulation/Core/Target.cc b/NPSimulation/Core/Target.cc index 979d28764..ac0f327a2 100644 --- a/NPSimulation/Core/Target.cc +++ b/NPSimulation/Core/Target.cc @@ -532,6 +532,7 @@ G4double Target::SlowDownBeam(G4ParticleDefinition* Beam, IncidentEnergy -= de; } } + return IncidentEnergy; } diff --git a/NPSimulation/Simulation.cc b/NPSimulation/Simulation.cc index 612f92f47..5df10ecc9 100644 --- a/NPSimulation/Simulation.cc +++ b/NPSimulation/Simulation.cc @@ -24,6 +24,7 @@ // NPS headers #include "EventAction.hh" +#include "RunAction.hh" //NPL headers #include "NPOptionManager.h" @@ -97,6 +98,11 @@ int main(int argc, char** argv){ event_action->SetDetector(detector) ; runManager->SetUserAction(event_action) ; + /////////////////////////////////////////////////////////////// + /////////////////// Starting the Run Action /////////////////// + /////////////////////////////////////////////////////////////// + RunAction* run_action = new RunAction() ; + runManager->SetUserAction(run_action); #ifdef G4UI_USE #ifdef G4VIS_USE -- GitLab