Skip to content

Commit

Permalink
Merge pull request #38542 from civanch/geant4_start_stop
Browse files Browse the repository at this point in the history
Geant4 technical updates
  • Loading branch information
cmsbuild committed Jun 30, 2022
2 parents 6686652 + bfcb36a commit 3b20114
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
1 change: 0 additions & 1 deletion SimG4Core/Application/interface/RunManagerMT.h
Expand Up @@ -100,7 +100,6 @@ class RunManagerMT {
edm::ParameterSet m_pRunAction;
edm::ParameterSet m_g4overlap;
std::vector<std::string> m_G4Commands;
std::vector<std::string> m_G4CommandsEndRun;
edm::ParameterSet m_p;

std::unique_ptr<DDDWorld> m_world;
Expand Down
9 changes: 5 additions & 4 deletions SimG4Core/Application/interface/RunManagerMTWorker.h
Expand Up @@ -58,8 +58,6 @@ class RunManagerMTWorker {
void abortEvent();
void abortRun(bool softAbort = false);

inline G4SimEvent* simEvent() { return &m_simEvent; }

void Connect(RunAction*);
void Connect(EventAction*);
void Connect(TrackingAction*);
Expand All @@ -72,6 +70,9 @@ class RunManagerMTWorker {

void initializeG4(RunManagerMT* runManagerMaster, const edm::EventSetup& es);

inline G4SimEvent* simEvent() { return &m_simEvent; }
inline int getThreadIndex() const { return m_thread_index; }

private:
void initializeTLS();
void initializeUserActions();
Expand All @@ -83,8 +84,6 @@ class RunManagerMTWorker {

void DumpMagneticField(const G4Field*, const std::string&) const;

inline int getThreadIndex() const { return m_thread_index; }

Generator m_generator;
edm::EDGetTokenT<edm::HepMCProduct> m_InToken;
edm::EDGetTokenT<edm::HepMCProduct> m_LHCToken;
Expand All @@ -97,6 +96,7 @@ class RunManagerMTWorker {
bool m_hasWatchers{false};
bool m_LHCTransport{false};
bool m_dumpMF{false};
bool m_endOfRun{false};

const int m_thread_index{-1};

Expand All @@ -107,6 +107,7 @@ class RunManagerMTWorker {
edm::ParameterSet m_pTrackingAction;
edm::ParameterSet m_pSteppingAction;
edm::ParameterSet m_pCustomUIsession;
std::vector<std::string> m_G4CommandsEndRun;
edm::ParameterSet m_p;

struct TLSData;
Expand Down
19 changes: 11 additions & 8 deletions SimG4Core/Application/plugins/OscarMTProducer.cc
Expand Up @@ -217,34 +217,36 @@ void OscarMTProducer::globalEndJob(OscarMTMasterThread* masterThread) {
}

void OscarMTProducer::beginRun(const edm::Run&, const edm::EventSetup& es) {
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun";
int id = m_runManagerWorker->getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun threadID=" << id;
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, &es, token]() {
edm::ServiceRegistry::Operate guard{token};
m_runManagerWorker->beginRun(es);
m_runManagerWorker->initializeG4(m_masterThread->runManagerMasterPtr(), es);
});
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun done";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun done threadID=" << id;
}

void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) {
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun";
int id = m_runManagerWorker->getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun threadID=" << id;
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, token]() {
StaticRandomEngineSetUnset random(nullptr);
edm::ServiceRegistry::Operate guard{token};
m_runManagerWorker->endRun();
});
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun done";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun done threadID=" << id;
}

void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
StaticRandomEngineSetUnset random(e.streamID());
auto engine = random.currentEngine();
int id = m_runManagerWorker->getThreadIndex();
if (0 < m_verbose) {
edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID();
edm::LogVerbatim("SimG4CoreApplication")
<< "Produce event " << e.id() << " stream " << e.streamID() << " threadID=" << id;
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
}

Expand Down Expand Up @@ -310,7 +312,8 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
prod.get()->produce(e, es);
}
if (0 < m_verbose) {
edm::LogVerbatim("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID();
edm::LogVerbatim("SimG4CoreApplication")
<< "Event is produced event " << e.id() << " streamID=" << e.streamID() << " threadID=" << id;
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
}
}
Expand Down
3 changes: 2 additions & 1 deletion SimG4Core/Application/src/LowEnergyFastSimModel.cc
Expand Up @@ -15,6 +15,7 @@
#include "G4PhysicalConstants.hh"

constexpr G4double twomass = 2 * CLHEP::electron_mass_c2;
constexpr G4double scaleFactor = 1.015;

LowEnergyFastSimModel::LowEnergyFastSimModel(const G4String& name, G4Region* region, const edm::ParameterSet& parSet)
: G4VFastSimulationModel(name, region),
Expand Down Expand Up @@ -68,7 +69,7 @@ void LowEnergyFastSimModel::DoIt(const G4FastTrack& fastTrack, G4FastStep& fastS
fastStep.KillPrimaryTrack();
fastStep.SetPrimaryTrackPathLength(0.0);
auto track = fastTrack.GetPrimaryTrack();
G4double energy = track->GetKineticEnergy();
G4double energy = track->GetKineticEnergy() * scaleFactor;

const G4ThreeVector& pos = track->GetPosition();

Expand Down
14 changes: 4 additions & 10 deletions SimG4Core/Application/src/RunManagerMT.cc
Expand Up @@ -75,7 +75,6 @@ RunManagerMT::RunManagerMT(edm::ParameterSet const& p)
m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
m_G4CommandsEndRun(p.getParameter<std::vector<std::string> >("G4CommandsEndRun")),
m_p(p) {
m_currentRun = nullptr;
m_UIsession = new CustomUIsession();
Expand Down Expand Up @@ -277,22 +276,17 @@ void RunManagerMT::Connect(RunAction* runAction) {
}

void RunManagerMT::stopG4() {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4";
G4GeometryManager::GetInstance()->OpenGeometry();
m_stateManager->SetNewState(G4State_Quit);
// Geant4 UI commands after the run
if (!m_G4CommandsEndRun.empty()) {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested end of run UI commands: ";
for (const std::string& command : m_G4CommandsEndRun) {
edm::LogVerbatim("SimG4CoreApplication") << " " << command;
G4UImanager::GetUIpointer()->ApplyCommand(command);
}
}
if (!m_runTerminated) {
terminateRun();
}
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4 done";
}

void RunManagerMT::terminateRun() {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun";
if (nullptr != m_userRunAction) {
m_userRunAction->EndOfRunAction(m_currentRun);
delete m_userRunAction;
Expand All @@ -302,5 +296,5 @@ void RunManagerMT::terminateRun() {
m_kernel->RunTermination();
}
m_runTerminated = true;
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT:: terminateRun done";
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun done";
}
42 changes: 32 additions & 10 deletions SimG4Core/Application/src/RunManagerMTWorker.cc
Expand Up @@ -53,6 +53,7 @@
#include "G4TransportationManager.hh"
#include "G4Field.hh"
#include "G4FieldManager.hh"
#include "G4ScoringManager.hh"

#include <atomic>
#include <memory>
Expand All @@ -62,6 +63,7 @@
#include <vector>

static std::once_flag applyOnce;
static std::once_flag applyOnceEnd;

// from https://hypernews.cern.ch/HyperNews/CMS/get/edmFramework/3302/2.html
namespace {
Expand Down Expand Up @@ -156,6 +158,7 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co
m_pTrackingAction(iConfig.getParameter<edm::ParameterSet>("TrackingAction")),
m_pSteppingAction(iConfig.getParameter<edm::ParameterSet>("SteppingAction")),
m_pCustomUIsession(iConfig.getUntrackedParameter<edm::ParameterSet>("CustomUIsession")),
m_G4CommandsEndRun(iConfig.getParameter<std::vector<std::string>>("G4CommandsEndRun")),
m_p(iConfig) {
int thisID = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker for the thread " << thisID;
Expand All @@ -171,7 +174,6 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co
watcher->registerConsumes(iC);
}
}

if (m_LHCTransport) {
m_LHCToken = iC.consumes<edm::HepMCProduct>(edm::InputTag("LHCTransport"));
}
Expand All @@ -193,6 +195,8 @@ RunManagerMTWorker::~RunManagerMTWorker() {
}

void RunManagerMTWorker::beginRun(edm::EventSetup const& es) {
int id = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::beginRun for the thread " << id;
for (auto& maker : m_sdMakers) {
maker.second->beginRun(es);
}
Expand All @@ -204,11 +208,12 @@ void RunManagerMTWorker::beginRun(edm::EventSetup const& es) {
watcher->beginRun(es);
}
}
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::beginRun done for the thread " << id;
}

void RunManagerMTWorker::endRun() {
int thisID = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::endRun for the thread " << thisID;
int id = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::endRun for the thread " << id;
terminateRun();
}

Expand Down Expand Up @@ -323,6 +328,13 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm:
<< "RunManagerMTWorker::InitializeG4: Sensitive Detectors are built in thread " << thisID << " found "
<< m_tls->sensTkDets.size() << " Tk type SD, and " << m_tls->sensCaloDets.size() << " Calo type SD";

// Enable couple transportation
bool scorer = m_p.getParameter<bool>("UseCommandBaseScorer");
if (scorer) {
G4ScoringManager* scManager = G4ScoringManager::GetScoringManager();
scManager->SetVerboseLevel(1);
}

// Set the physics list for the worker, share from master
PhysicsList* physicsList = runManagerMaster->physicsListForWorker();

Expand Down Expand Up @@ -434,33 +446,43 @@ std::vector<std::shared_ptr<SimProducer>>& RunManagerMTWorker::producers() {
}

void RunManagerMTWorker::initializeRun() {
int thisID = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun " << thisID << " is started";
m_tls->currentRun = new G4Run();
G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
if (nullptr != m_tls->userRunAction) {
m_tls->userRunAction->BeginOfRunAction(m_tls->currentRun);
}
int id = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun done for thread " << id;
}

void RunManagerMTWorker::terminateRun() {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun ";
int id = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun for thread " << id;
if (nullptr == m_tls || m_tls->runTerminated) {
return;
}
int thisID = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started";

// Geant4 UI commands after the run
if (!m_G4CommandsEndRun.empty()) {
std::call_once(applyOnceEnd, [this]() { m_endOfRun = true; });
if (m_endOfRun) {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker: Requested end of run UI commands: ";
for (const std::string& command : m_G4CommandsEndRun) {
edm::LogVerbatim("SimG4CoreApplication") << " " << command;
G4UImanager::GetUIpointer()->ApplyCommand(command);
}
}
}
if (m_tls->userRunAction) {
m_tls->userRunAction->EndOfRunAction(m_tls->currentRun);
m_tls->userRunAction.reset();
}
m_tls->currentEvent.reset();

if (m_tls->kernel) {
m_tls->kernel->RunTermination();
}

m_tls->runTerminated = true;
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun done for thread " << id;
}

G4SimEvent* RunManagerMTWorker::produce(const edm::Event& inpevt,
Expand Down

0 comments on commit 3b20114

Please sign in to comment.