Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup geant4 user code #37987

Merged
merged 1 commit into from May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions SimG4Core/Application/interface/RunManagerMT.h
Expand Up @@ -100,6 +100,7 @@ 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
11 changes: 4 additions & 7 deletions SimG4Core/Application/interface/RunManagerMTWorker.h
Expand Up @@ -9,6 +9,7 @@

#include "MagneticField/Engine/interface/MagneticField.h"
#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
#include "SimG4Core/Notification/interface/G4SimEvent.h"

#include <memory>
#include <unordered_map>
Expand All @@ -26,7 +27,6 @@ class Generator;
class RunManagerMT;

class G4Event;
class G4SimEvent;
class G4Run;
class SimTrackManager;
class CustomUIsession;
Expand All @@ -53,14 +53,12 @@ class RunManagerMTWorker {
void beginRun(const edm::EventSetup&);
void endRun();

std::unique_ptr<G4SimEvent> produce(const edm::Event& inpevt,
const edm::EventSetup& es,
RunManagerMT& runManagerMaster);
G4SimEvent* produce(const edm::Event& inpevt, const edm::EventSetup& es, RunManagerMT& runManagerMaster);

void abortEvent();
void abortRun(bool softAbort = false);

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

void Connect(RunAction*);
void Connect(EventAction*);
Expand Down Expand Up @@ -101,7 +99,6 @@ class RunManagerMTWorker {
bool m_dumpMF{false};

const int m_thread_index{-1};
int m_EvtMgrVerbosity{0};

edm::ParameterSet m_pField;
edm::ParameterSet m_pRunAction;
Expand All @@ -116,7 +113,7 @@ class RunManagerMTWorker {
TLSData* m_tls{nullptr};

CustomUIsession* m_UIsession{nullptr};
G4SimEvent* m_simEvent{nullptr};
G4SimEvent m_simEvent;
std::unique_ptr<CMSSteppingVerbose> m_sVerbose;
std::unordered_map<std::string, std::unique_ptr<SensitiveDetectorMakerBase>> m_sdMakers;
};
Expand Down
8 changes: 4 additions & 4 deletions SimG4Core/Application/interface/TrackingAction.h
Expand Up @@ -3,7 +3,6 @@

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "SimG4Core/Notification/interface/SimActivityRegistry.h"
#include "SimG4Core/Notification/interface/TrackInformationExtractor.h"

#include "G4UserTrackingAction.hh"

Expand All @@ -12,6 +11,7 @@ class TrackWithHistory;
class BeginOfTrack;
class EndOfTrack;
class CMSSteppingVerbose;
class TrackInformation;

class TrackingAction : public G4UserTrackingAction {
public:
Expand All @@ -29,11 +29,11 @@ class TrackingAction : public G4UserTrackingAction {
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal;

private:
TrackInformationExtractor extractor_;
EventAction* eventAction_;
TrackWithHistory* currentTrack_;
CMSSteppingVerbose* steppingVerbose_;
const G4Track* g4Track_;
const G4Track* g4Track_ = nullptr;
TrackInformation* trkInfo_ = nullptr;
TrackWithHistory* currentTrack_ = nullptr;
bool checkTrack_;
bool doFineCalo_;
bool saveCaloBoundaryInformation_;
Expand Down
2 changes: 2 additions & 0 deletions SimG4Core/Application/plugins/BuildFile.xml
Expand Up @@ -4,7 +4,9 @@
<use name="SimDataFormats/Track"/>
<use name="SimDataFormats/TrackingHit"/>
<use name="SimDataFormats/Vertex"/>
<use name="SimG4Core/Notification"/>
<use name="geant4core"/>
<use name="clhep"/>
<use name="hepmc"/>
<library file="*.cc" name="SimG4CoreApplicationPlugins">
<use name="SimG4Core/Application"/>
Expand Down
39 changes: 32 additions & 7 deletions SimG4Core/Application/plugins/OscarMTProducer.cc
Expand Up @@ -22,6 +22,8 @@
#include "SimG4Core/Application/interface/RunManagerMT.h"
#include "SimG4Core/Application/interface/RunManagerMTWorker.h"
#include "SimG4Core/Notification/interface/G4SimEvent.h"
#include "SimG4Core/Notification/interface/G4SimVertex.h"
#include "SimG4Core/Notification/interface/G4SimTrack.h"

#include "SimG4Core/SensitiveDetector/interface/SensitiveTkDetector.h"
#include "SimG4Core/SensitiveDetector/interface/SensitiveCaloDetector.h"
Expand Down Expand Up @@ -60,6 +62,7 @@ class OscarMTProducer : public edm::stream::EDProducer<edm::GlobalCache<OscarMTM
omt::ThreadHandoff m_handoff;
std::unique_ptr<RunManagerMTWorker> m_runManagerWorker;
const OscarMTMasterThread* m_masterThread = nullptr;
int m_verbose;
};

namespace edm {
Expand Down Expand Up @@ -99,6 +102,7 @@ namespace {

OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread* ms)
: m_handoff{p.getUntrackedParameter<int>("workerThreadStackSize", 10 * 1024 * 1024)} {
m_verbose = p.getParameter<int>("EventVerbose");
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);

Expand Down Expand Up @@ -239,13 +243,15 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) {
void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
StaticRandomEngineSetUnset random(e.streamID());
auto engine = random.currentEngine();
edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID();
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
if (0 < m_verbose) {
edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID();
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
}

auto& sTk = m_runManagerWorker->sensTkDetectors();
auto& sCalo = m_runManagerWorker->sensCaloDetectors();

std::unique_ptr<G4SimEvent> evt;
G4SimEvent* evt = nullptr;
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, &e, &es, &evt, token, engine]() {
edm::ServiceRegistry::Operate guard{token};
Expand All @@ -258,6 +264,23 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
evt->load(*p1);
evt->load(*p2);

if (0 < m_verbose) {
edm::LogVerbatim("SimG4CoreApplication") << "Produced " << p2->size() << " SimVertex objects";
if (1 < m_verbose) {
int nn = p2->size();
for (int i = 0; i < nn; ++i) {
edm::LogVerbatim("Vertex") << " " << (*p2)[i] << " " << (*p2)[i].processType();
}
}
edm::LogVerbatim("SimG4CoreApplication") << "Produced " << p1->size() << " SimTrack objects";
if (1 < m_verbose) {
int nn = p1->size();
for (int i = 0; i < nn; ++i) {
edm::LogVerbatim("Track") << " " << i << ". " << (*p1)[i] << " " << (*p1)[i].crossedBoundary() << " "
<< (*p1)[i].getIDAtBoundary();
}
}
}
e.put(std::move(p1));
e.put(std::move(p2));

Expand All @@ -266,7 +289,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
for (auto& name : v) {
std::unique_ptr<edm::PSimHitContainer> product(new edm::PSimHitContainer);
tracker->fillHits(*product, name);
if (product != nullptr && !product->empty())
if (0 < m_verbose && product != nullptr && !product->empty())
edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " tracker hits <" << name << ">";
e.put(std::move(product), name);
}
Expand All @@ -276,7 +299,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
for (auto& name : v) {
std::unique_ptr<edm::PCaloHitContainer> product(new edm::PCaloHitContainer);
calo->fillHits(*product, name);
if (product != nullptr && !product->empty())
if (0 < m_verbose && product != nullptr && !product->empty())
edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " calo hits <" << name << ">";
e.put(std::move(product), name);
}
Expand All @@ -286,8 +309,10 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
for (auto& prod : producers) {
prod.get()->produce(e, es);
}
edm::LogVerbatim("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID();
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
if (0 < m_verbose) {
edm::LogVerbatim("SimG4CoreApplication") << "Event is produced " << e.id() << " stream " << e.streamID();
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();
}
}

StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {
Expand Down
4 changes: 3 additions & 1 deletion SimG4Core/Application/python/g4SimHits_cfi.py
Expand Up @@ -77,8 +77,9 @@
g4SimHits = cms.EDProducer("OscarMTProducer",
g4GeometryDD4hepSource = cms.bool(False),
NonBeamEvent = cms.bool(False),
G4EventManagerVerbosity = cms.untracked.int32(0),
EventVerbose = cms.int32(0),
UseMagneticField = cms.bool(True),
UseCommandBaseScorer = cms.bool(False),
StoreRndmSeeds = cms.bool(False),
RestoreRndmSeeds = cms.bool(False),
PhysicsTablesDirectory = cms.untracked.string('PhysicsTables'),
Expand Down Expand Up @@ -107,6 +108,7 @@
NodeNames = cms.vstring('World')
),
G4Commands = cms.vstring(),
G4CommandsEndRun = cms.vstring(),
SteppingVerbosity = cms.untracked.int32(0),
StepVerboseThreshold = cms.untracked.double(0.1), # in GeV
VerboseEvents = cms.untracked.vint32(),
Expand Down
1 change: 0 additions & 1 deletion SimG4Core/Application/src/EventAction.cc
Expand Up @@ -8,7 +8,6 @@

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include <fstream>
#include "Randomize.hh"

EventAction::EventAction(const edm::ParameterSet& p,
Expand Down
16 changes: 16 additions & 0 deletions SimG4Core/Application/src/RunManagerMT.cc
Expand Up @@ -30,6 +30,7 @@

#include "G4Timer.hh"
#include "G4GeometryManager.hh"
#include "G4ScoringManager.hh"
#include "G4StateManager.hh"
#include "G4ApplicationState.hh"
#include "G4MTRunManagerKernel.hh"
Expand Down Expand Up @@ -74,6 +75,7 @@ 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 @@ -186,6 +188,12 @@ void RunManagerMT::initG4(const DDCompactView* pDD,

edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";

// Enable couple transportation
bool scorer = m_p.getParameter<bool>("UseCommandBaseScorer");
if (scorer) {
G4ScoringManager* scManager = G4ScoringManager::GetScoringManager();
scManager->SetVerboseLevel(1);
}
// Geant4 UI commands before initialisation of physics
if (!m_G4Commands.empty()) {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
Expand Down Expand Up @@ -271,6 +279,14 @@ void RunManagerMT::Connect(RunAction* runAction) {
void 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();
}
Expand Down
35 changes: 15 additions & 20 deletions SimG4Core/Application/src/RunManagerMTWorker.cc
Expand Up @@ -21,7 +21,6 @@
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"

#include "SimG4Core/Notification/interface/G4SimEvent.h"
#include "SimG4Core/Notification/interface/SimActivityRegistry.h"
#include "SimG4Core/Notification/interface/BeginOfJob.h"
#include "SimG4Core/Notification/interface/CMSSteppingVerbose.h"
Expand Down Expand Up @@ -150,7 +149,6 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co
m_pUseMagneticField(iConfig.getParameter<bool>("UseMagneticField")),
m_LHCTransport(iConfig.getParameter<bool>("LHCTransport")),
m_thread_index{get_new_thread_index()},
m_EvtMgrVerbosity(iConfig.getUntrackedParameter<int>("G4EventManagerVerbosity", 0)),
m_pField(iConfig.getParameter<edm::ParameterSet>("MagneticField")),
m_pRunAction(iConfig.getParameter<edm::ParameterSet>("RunAction")),
m_pEventAction(iConfig.getParameter<edm::ParameterSet>("EventAction")),
Expand Down Expand Up @@ -378,8 +376,9 @@ void RunManagerMTWorker::initializeUserActions() {
m_tls->userRunAction->SetMaster(false);
Connect(m_tls->userRunAction.get());

G4int ver = m_p.getParameter<int>("EventVerbose");
G4EventManager* eventManager = m_tls->kernel->GetEventManager();
eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
eventManager->SetVerboseLevel(ver);

EventAction* userEventAction =
new EventAction(m_pEventAction, m_tls->runInterface.get(), m_tls->trackManager.get(), m_sVerbose.get());
Expand Down Expand Up @@ -456,7 +455,6 @@ void RunManagerMTWorker::terminateRun() {
m_tls->userRunAction.reset();
}
m_tls->currentEvent.reset();
m_simEvent = nullptr;

if (m_tls->kernel) {
m_tls->kernel->RunTermination();
Expand All @@ -465,9 +463,9 @@ void RunManagerMTWorker::terminateRun() {
m_tls->runTerminated = true;
}

std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt,
const edm::EventSetup& es,
RunManagerMT& runManagerMaster) {
G4SimEvent* RunManagerMTWorker::produce(const edm::Event& inpevt,
const edm::EventSetup& es,
RunManagerMT& runManagerMaster) {
// The initialization and begin/end run is a bit convoluted due to
// - Geant4 deals per-thread
// - OscarMTProducer deals per-stream
Expand All @@ -494,16 +492,15 @@ std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt

m_tls->currentEvent.reset(generateEvent(inpevt));

auto simEvent = std::make_unique<G4SimEvent>();
m_simEvent = simEvent.get();
m_simEvent->hepEvent(m_generator.genEvent());
m_simEvent->weight(m_generator.eventWeight());
m_simEvent.clear();
m_simEvent.hepEvent(m_generator.genEvent());
m_simEvent.weight(m_generator.eventWeight());
if (m_generator.genVertex() != nullptr) {
auto genVertex = m_generator.genVertex();
m_simEvent->collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
genVertex->y() / CLHEP::cm,
genVertex->z() / CLHEP::cm,
genVertex->t() / CLHEP::second));
m_simEvent.collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
genVertex->y() / CLHEP::cm,
genVertex->z() / CLHEP::cm,
genVertex->t() / CLHEP::second));
}
if (m_tls->currentEvent->GetNumberOfPrimaryVertex() == 0) {
throw cms::Exception("EventCorruption")
Expand All @@ -513,9 +510,9 @@ std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt
} else {
edm::LogVerbatim("SimG4CoreApplication")
<< "RunManagerMTWorker::produce: start EventID=" << inpevt.id().event() << " StreamID=" << inpevt.streamID()
<< " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent->weight() << "; "
<< " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent.weight() << "; "
<< m_tls->currentEvent->GetNumberOfPrimaryVertex() << " vertices for Geant4; generator produced "
<< m_simEvent->nGenParts() << " particles.";
<< m_simEvent.nGenParts() << " particles.";

m_tls->kernel->GetEventManager()->ProcessOneEvent(m_tls->currentEvent.get());
}
Expand All @@ -527,8 +524,7 @@ std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt
sd->reset();
}
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce: ended Event " << inpevt.id().event();
m_simEvent = nullptr;
return simEvent;
return &m_simEvent;
}

void RunManagerMTWorker::abortEvent() {
Expand Down Expand Up @@ -558,7 +554,6 @@ void RunManagerMTWorker::abortRun(bool softAbort) {

G4Event* RunManagerMTWorker::generateEvent(const edm::Event& inpevt) {
m_tls->currentEvent.reset();
m_simEvent = nullptr;

// 64 bits event ID in CMSSW converted into Geant4 event ID
G4int evtid = (G4int)inpevt.id().event();
Expand Down