From 87a6606d458469ab38d192d9137618fdb0171393 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 5 Jul 2021 20:21:36 +0200 Subject: [PATCH 01/16] migrate event setup --- .../interface/OscarMTMasterThread.h | 28 ++++---- .../Application/interface/RunManagerMT.h | 1 - .../interface/RunManagerMTWorker.h | 7 ++ .../Application/plugins/OscarMTProducer.cc | 70 +++++++++++++++---- .../Application/src/OscarMTMasterThread.cc | 61 +++------------- .../Application/src/RunManagerMTWorker.cc | 19 +++-- 6 files changed, 95 insertions(+), 91 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 09cd989f21eac..7d3e3934bfa4f 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -4,8 +4,11 @@ #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/Framework/interface/ESWatcher.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" -#include "Geometry/Records/interface/IdealGeometryRecord.h" +#include "DetectorDescription/Core/interface/DDCompactView.h" +#include "DetectorDescription/DDCMS/interface/DDCompactView.h" + +#include "HepPDT/ParticleDataTable.hh" +#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" #include #include @@ -34,15 +37,15 @@ class OscarMTMasterThread { explicit OscarMTMasterThread(const edm::ParameterSet& iConfig); ~OscarMTMasterThread(); - void beginRun(const edm::EventSetup& iSetup) const; + void beginRun(const DDCompactView*, const cms::DDCompactView*, const HepPDT::ParticleDataTable*) const; void endRun() const; void stopThread(); inline RunManagerMT& runManagerMaster() const { return *m_runManagerMaster; } inline RunManagerMT* runManagerMasterPtr() const { return m_runManagerMaster.get(); } + inline bool isDD4Hep() const { return m_pGeoFromDD4hep; } private: - void readES(const edm::EventSetup& iSetup) const; enum class ThreadState { NotExist = 0, BeginRun = 1, EndRun = 2, Destruct = 3 }; @@ -52,12 +55,11 @@ class OscarMTMasterThread { std::thread m_masterThread; // ES products needed for Geant4 initialization - mutable edm::ESWatcher idealGeomRcdWatcher_; - mutable edm::ESWatcher idealMagRcdWatcher_; - mutable const DDCompactView* m_pDD; - mutable const cms::DDCompactView* m_pDD4hep; - mutable const HepPDT::ParticleDataTable* m_pTable; + mutable const DDCompactView* m_pDDD = nullptr; + mutable const cms::DDCompactView* m_pDD4Hep = nullptr; + mutable const HepPDT::ParticleDataTable* m_pTable = nullptr; + // status flags mutable std::mutex m_protectMutex; mutable std::mutex m_threadMutex; mutable std::condition_variable m_notifyMasterCv; @@ -65,10 +67,10 @@ class OscarMTMasterThread { mutable ThreadState m_masterThreadState; - mutable bool m_masterCanProceed; - mutable bool m_mainCanProceed; - mutable bool m_firstRun; - mutable bool m_stopped; + mutable bool m_masterCanProceed = false; + mutable bool m_mainCanProceed = false; + mutable bool m_firstRun = true; + mutable bool m_stopped = false; }; #endif diff --git a/SimG4Core/Application/interface/RunManagerMT.h b/SimG4Core/Application/interface/RunManagerMT.h index 093f0c48911b1..2c5c3e4692e80 100644 --- a/SimG4Core/Application/interface/RunManagerMT.h +++ b/SimG4Core/Application/interface/RunManagerMT.h @@ -59,7 +59,6 @@ class RunManagerMT { explicit RunManagerMT(edm::ParameterSet const&); ~RunManagerMT(); - // void initG4(const DDCompactView*, const cms::DDCompactView*, const MagneticField*, const HepPDT::ParticleDataTable*); void initG4(const DDCompactView*, const cms::DDCompactView*, const HepPDT::ParticleDataTable*); void initializeUserActions(); diff --git a/SimG4Core/Application/interface/RunManagerMTWorker.h b/SimG4Core/Application/interface/RunManagerMTWorker.h index e1c0e65586cb6..a8f8701d54c5d 100644 --- a/SimG4Core/Application/interface/RunManagerMTWorker.h +++ b/SimG4Core/Application/interface/RunManagerMTWorker.h @@ -7,6 +7,9 @@ #include "SimG4Core/Generators/interface/Generator.h" #include "SimDataFormats/Forward/interface/LHCTransportLinkContainer.h" +#include "MagneticField/Engine/interface/MagneticField.h" +#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" + #include #include #include @@ -19,6 +22,7 @@ namespace edm { class ConsumesCollector; class HepMCProduct; } // namespace edm + class Generator; class RunManagerMT; @@ -70,6 +74,8 @@ class RunManagerMTWorker { void initializeG4(RunManagerMT* runManagerMaster, const edm::EventSetup& es); + inline void SetMagFieldToken(edm::ESGetToken& ref) { m_MagField = ref; } + private: void initializeTLS(); void initializeUserActions(); @@ -88,6 +94,7 @@ class RunManagerMTWorker { edm::EDGetTokenT m_InToken; edm::EDGetTokenT m_LHCToken; edm::EDGetTokenT m_theLHCTlinkToken; + edm::ESGetToken m_MagField; bool m_nonBeam; bool m_pUseMagneticField; diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index 8b450dcf9264a..2cd088482cc27 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -64,6 +64,14 @@ namespace { }; } // namespace + +edm::ESGetToken OscarMTProducer::m_DD4Hep; +edm::ESGetToken OscarMTProducer::m_DDD; +edm::ESGetToken OscarMTProducer::m_PDT; +edm::ESGetToken OscarMTProducer::m_MagField; +G4Mutex OscarMTProducer::m_OscarMutex = G4MUTEX_INITIALIZER; +bool OscarMTProducer::m_hasToken = false; + OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread* ms) { // Random number generation not allowed here StaticRandomEngineSetUnset random(nullptr); @@ -71,6 +79,24 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster m_runManagerWorker = std::make_unique(p, consumesCollector()); m_masterThread = ms; + // Prepare tokens + bool isDD4Hep = p.getParameter("g4GeometryDD4hepSource"); + if(!m_hasToken) { + G4MUTEXLOCK(&m_OscarMutex); + if(!m_hasToken) { + if (isDD4Hep) { + m_DD4Hep = esConsumes(); + } else { + m_DDD = esConsumes(); + } + m_PDT = esConsumes(); + m_MagField = esConsumes(); + } + G4MUTEXUNLOCK(&m_OscarMutex); + } + m_runManagerWorker->SetMagFieldToken(m_MagField); + + // List of produced containers produces().setBranchAlias("SimTracks"); produces().setBranchAlias("SimVertices"); produces("TrackerHitsPixelBarrelLowTof"); @@ -128,11 +154,10 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster //register any products auto& producers = m_runManagerWorker->producers(); - - for (Producers::iterator itProd = producers.begin(); itProd != producers.end(); ++itProd) { - (*itProd)->registerProducts(producesCollector()); + for (auto & ptr : producers) { + ptr->registerProducts(producesCollector()); } - edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed"; + edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed DD4Hep: " << isDD4Hep; } OscarMTProducer::~OscarMTProducer() {} @@ -145,19 +170,29 @@ std::unique_ptr OscarMTProducer::initializeGlobalCache(cons return std::make_unique(iConfig); } -std::shared_ptr OscarMTProducer::globalBeginRun(const edm::Run& iRun, +std::shared_ptr OscarMTProducer::globalBeginRun(const edm::Run&, const edm::EventSetup& iSetup, const OscarMTMasterThread* masterThread) { // Random number generation not allowed here StaticRandomEngineSetUnset random(nullptr); - edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun"; - masterThread->beginRun(iSetup); + + // Prepare master thread + const DDCompactView* pDDD = nullptr; + const cms::DDCompactView* pDD4Hep = nullptr; + if (masterThread->isDD4Hep()) { + pDD4Hep = &(iSetup.getData(m_DD4Hep)); + } else { + pDDD = &(iSetup.getData(m_DDD)); + } + const HepPDT::ParticleDataTable* pPDT = &(iSetup.getData(m_PDT)); + masterThread->beginRun(pDDD, pDD4Hep, pPDT); + edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun done"; return std::shared_ptr(); } -void OscarMTProducer::globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext) { +void OscarMTProducer::globalEndRun(const edm::Run&, const edm::EventSetup&, const RunContext* iContext) { edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalEndRun"; iContext->global()->endRun(); } @@ -184,8 +219,9 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) { void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { StaticRandomEngineSetUnset random(e.streamID()); - edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID(); - LogDebug("SimG4CoreApplication") << "Before event rand= " << G4UniformRand(); + edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() + << " stream " << e.streamID() + << " rand= " << G4UniformRand(); auto& sTk = m_runManagerWorker->sensTkDetectors(); auto& sCalo = m_runManagerWorker->sensCaloDetectors(); @@ -197,7 +233,8 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what(); throw edm::Exception(edm::errors::EventCorruption) - << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n" + << "SimG4CoreApplication exception in generation of event " << e.id() + << " in stream " << e.streamID() << " \n" << simg4ex.what(); } @@ -215,7 +252,8 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { std::unique_ptr product(new edm::PSimHitContainer); tracker->fillHits(*product, name); if (product != nullptr && !product->empty()) - edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " traker hits <" << name << ">"; + edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() + << " tracker hits <" << name << ">"; e.put(std::move(product), name); } } @@ -226,7 +264,8 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { std::unique_ptr product(new edm::PCaloHitContainer); calo->fillHits(*product, name); if (product != nullptr && !product->empty()) - edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " calo hits <" << name << ">"; + edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() + << " calo hits <" << name << ">"; e.put(std::move(product), name); } } @@ -235,8 +274,9 @@ 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(); - LogDebug("SimG4CoreApplication") << "End of event rand= " << G4UniformRand(); + edm::LogVerbatim("SimG4CoreApplication") << "Event is produced " << e.id() + << " stream " << e.streamID() + << " rand= " << G4UniformRand(); } StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) { diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 575665607901d..44b50fda8a4a8 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -6,35 +6,20 @@ #include "SimG4Core/Geometry/interface/CustomUIsession.h" #include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/Framework/interface/ESHandle.h" #include "FWCore/Framework/interface/ESTransientHandle.h" #include "FWCore/Utilities/interface/EDMException.h" -#include "Geometry/Records/interface/IdealGeometryRecord.h" -#include "DetectorDescription/Core/interface/DDCompactView.h" -#include "DetectorDescription/DDCMS/interface/DDCompactView.h" - -#include "HepPDT/ParticleDataTable.hh" -#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" - #include "G4PhysicalVolumeStore.hh" OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) : m_pGeoFromDD4hep(iConfig.getParameter("g4GeometryDD4hepSource")), - m_pDD(nullptr), - m_pDD4hep(nullptr), - m_pTable(nullptr), - m_masterThreadState(ThreadState::NotExist), - m_masterCanProceed(false), - m_mainCanProceed(false), - m_firstRun(true), - m_stopped(false) { + m_masterThreadState(ThreadState::NotExist) { // Lock the mutex std::unique_lock lk(m_threadMutex); edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: creating master thread"; - // Create Genat4 master thread + // Create Geant4 master thread m_masterThread = std::thread([&]() { ///////////////// // Initialization @@ -71,7 +56,7 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) if (m_masterThreadState == ThreadState::BeginRun) { // Initialize Geant4 edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Initializing Geant4"; - m_runManagerMaster->initG4(m_pDD, m_pDD4hep, m_pTable); + m_runManagerMaster->initG4(m_pDDD, m_pDD4Hep, m_pTable); isG4Alive = true; } else if (m_masterThreadState == ThreadState::EndRun) { // Stop Geant4 @@ -119,17 +104,19 @@ OscarMTMasterThread::~OscarMTMasterThread() { } } -void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { +void OscarMTMasterThread::beginRun(const DDCompactView* pDDD, const cms::DDCompactView* pDD4Hep, const HepPDT::ParticleDataTable* pPDT) const { std::lock_guard lk(m_protectMutex); std::unique_lock lk2(m_threadMutex); - // Reading from ES must be done in the main (CMSSW) thread - readES(iSetup); - + edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::beginRun"; + m_pDDD = pDDD; + m_pDD4Hep = pDD4Hep; + m_pTable = pPDT; m_masterThreadState = ThreadState::BeginRun; m_masterCanProceed = true; m_mainCanProceed = false; + m_firstRun = false; edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for BeginRun"; m_notifyMasterCv.notify_one(); m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; }); @@ -176,33 +163,3 @@ void OscarMTMasterThread::stopThread() { edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished"; m_stopped = true; } - -void OscarMTMasterThread::readES(const edm::EventSetup& iSetup) const { - bool geomChanged = idealGeomRcdWatcher_.check(iSetup); - if (geomChanged && (!m_firstRun)) { - throw edm::Exception(edm::errors::Configuration) - << "[SimG4Core OscarMTMasterThread]\n" - << "The Geometry configuration is changed during the job execution\n" - << "this is not allowed, the geometry must stay unchanged"; - } - // Don't read from ES if not the first run, just as in - if (!m_firstRun) - return; - - // DDDWorld: get the DDCV from the ES and use it to build the World - if (m_pGeoFromDD4hep) { - edm::ESTransientHandle pDD; - iSetup.get().get(pDD); - m_pDD4hep = pDD.product(); - } else { - edm::ESTransientHandle pDD; - iSetup.get().get(pDD); - m_pDD = pDD.product(); - } - - edm::ESHandle fTable; - iSetup.get().get(fTable); - m_pTable = fTable.product(); - - m_firstRun = false; -} diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index 6d47628f2ddf7..dc1b0a48c2549 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -37,9 +37,6 @@ #include "SimG4Core/MagneticField/interface/FieldBuilder.h" #include "SimG4Core/MagneticField/interface/CMSFieldManager.h" -#include "MagneticField/Engine/interface/MagneticField.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h" #include "SimG4Core/Physics/interface/PhysicsList.h" @@ -317,19 +314,19 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: if (m_pUseMagneticField) { const GlobalPoint g(0., 0., 0.); - edm::ESHandle pMF; - es.get().get(pMF); - - sim::FieldBuilder fieldBuilder(pMF.product(), m_pField); CMSFieldManager* fieldManager = new CMSFieldManager(); tM->SetFieldManager(fieldManager); + + const MagneticField* pMF = &(es.getData(m_MagField)); + sim::FieldBuilder fieldBuilder(pMF, m_pField); fieldBuilder.build(fieldManager, tM->GetPropagatorInField()); std::string fieldFile = m_p.getUntrackedParameter("FileNameField", ""); if (!fieldFile.empty()) { std::call_once(applyOnce, []() { dumpMF = true; }); if (dumpMF) { - edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile; + edm::LogVerbatim("SimG4CoreApplication") + << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile; DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile); } } @@ -391,7 +388,8 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: timer.Stop(); edm::LogVerbatim("SimG4CoreApplication") - << "RunManagerMTWorker::initializeThread done for the thread " << thisID << " " << timer; + << "RunManagerMTWorker::initializeThread done for the thread " + << thisID << " " << timer; } void RunManagerMTWorker::initializeUserActions() { @@ -472,7 +470,8 @@ void RunManagerMTWorker::terminateRun() { return; } int thisID = getThreadIndex(); - edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started"; + edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " + << thisID << " is started"; if (m_tls->userRunAction) { m_tls->userRunAction->EndOfRunAction(m_tls->currentRun); m_tls->userRunAction.reset(); From ccc45f89496a1274952d98fa4737d5193851c30a Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 5 Jul 2021 20:32:34 +0200 Subject: [PATCH 02/16] code checks and remove unused headers --- .../interface/OscarMTMasterThread.h | 1 - .../Application/interface/RunManagerMT.h | 3 -- .../interface/RunManagerMTWorker.h | 4 +- .../Application/plugins/OscarMTProducer.cc | 40 ++++++++----------- .../Application/src/OscarMTMasterThread.cc | 6 +-- .../Application/src/RunManagerMTWorker.cc | 14 ++----- 6 files changed, 26 insertions(+), 42 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 7d3e3934bfa4f..23a9867b07d4f 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -46,7 +46,6 @@ class OscarMTMasterThread { inline bool isDD4Hep() const { return m_pGeoFromDD4hep; } private: - enum class ThreadState { NotExist = 0, BeginRun = 1, EndRun = 2, Destruct = 3 }; const bool m_pGeoFromDD4hep; diff --git a/SimG4Core/Application/interface/RunManagerMT.h b/SimG4Core/Application/interface/RunManagerMT.h index 2c5c3e4692e80..a290b6c93ae49 100644 --- a/SimG4Core/Application/interface/RunManagerMT.h +++ b/SimG4Core/Application/interface/RunManagerMT.h @@ -2,10 +2,7 @@ #define SimG4Core_RunManagerMT_H #include "FWCore/Framework/interface/Event.h" -#include "DataFormats/Common/interface/Handle.h" -#include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/Utilities/interface/InputTag.h" #include "SimG4Core/Geometry/interface/SensitiveDetectorCatalog.h" diff --git a/SimG4Core/Application/interface/RunManagerMTWorker.h b/SimG4Core/Application/interface/RunManagerMTWorker.h index a8f8701d54c5d..e8881065392c3 100644 --- a/SimG4Core/Application/interface/RunManagerMTWorker.h +++ b/SimG4Core/Application/interface/RunManagerMTWorker.h @@ -74,7 +74,7 @@ class RunManagerMTWorker { void initializeG4(RunManagerMT* runManagerMaster, const edm::EventSetup& es); - inline void SetMagFieldToken(edm::ESGetToken& ref) { m_MagField = ref; } + inline void SetMagFieldToken(edm::ESGetToken& ref) { m_MagField = ref; } private: void initializeTLS(); @@ -94,7 +94,7 @@ class RunManagerMTWorker { edm::EDGetTokenT m_InToken; edm::EDGetTokenT m_LHCToken; edm::EDGetTokenT m_theLHCTlinkToken; - edm::ESGetToken m_MagField; + edm::ESGetToken m_MagField; bool m_nonBeam; bool m_pUseMagneticField; diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index 2cd088482cc27..a8f6038bfefb0 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -64,11 +64,10 @@ namespace { }; } // namespace - -edm::ESGetToken OscarMTProducer::m_DD4Hep; -edm::ESGetToken OscarMTProducer::m_DDD; -edm::ESGetToken OscarMTProducer::m_PDT; -edm::ESGetToken OscarMTProducer::m_MagField; +edm::ESGetToken OscarMTProducer::m_DD4Hep; +edm::ESGetToken OscarMTProducer::m_DDD; +edm::ESGetToken OscarMTProducer::m_PDT; +edm::ESGetToken OscarMTProducer::m_MagField; G4Mutex OscarMTProducer::m_OscarMutex = G4MUTEX_INITIALIZER; bool OscarMTProducer::m_hasToken = false; @@ -81,13 +80,13 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster // Prepare tokens bool isDD4Hep = p.getParameter("g4GeometryDD4hepSource"); - if(!m_hasToken) { + if (!m_hasToken) { G4MUTEXLOCK(&m_OscarMutex); - if(!m_hasToken) { + if (!m_hasToken) { if (isDD4Hep) { - m_DD4Hep = esConsumes(); + m_DD4Hep = esConsumes(); } else { - m_DDD = esConsumes(); + m_DDD = esConsumes(); } m_PDT = esConsumes(); m_MagField = esConsumes(); @@ -96,7 +95,7 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster } m_runManagerWorker->SetMagFieldToken(m_MagField); - // List of produced containers + // List of produced containers produces().setBranchAlias("SimTracks"); produces().setBranchAlias("SimVertices"); produces("TrackerHitsPixelBarrelLowTof"); @@ -154,7 +153,7 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster //register any products auto& producers = m_runManagerWorker->producers(); - for (auto & ptr : producers) { + for (auto& ptr : producers) { ptr->registerProducts(producesCollector()); } edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed DD4Hep: " << isDD4Hep; @@ -219,9 +218,8 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) { void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { StaticRandomEngineSetUnset random(e.streamID()); - edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() - << " stream " << e.streamID() - << " rand= " << G4UniformRand(); + edm::LogVerbatim("SimG4CoreApplication") + << "Produce event " << e.id() << " stream " << e.streamID() << " rand= " << G4UniformRand(); auto& sTk = m_runManagerWorker->sensTkDetectors(); auto& sCalo = m_runManagerWorker->sensCaloDetectors(); @@ -233,8 +231,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { edm::LogWarning("SimG4CoreApplication") << "SimG4Exception caght! " << simg4ex.what(); throw edm::Exception(edm::errors::EventCorruption) - << "SimG4CoreApplication exception in generation of event " << e.id() - << " in stream " << e.streamID() << " \n" + << "SimG4CoreApplication exception in generation of event " << e.id() << " in stream " << e.streamID() << " \n" << simg4ex.what(); } @@ -252,8 +249,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { std::unique_ptr product(new edm::PSimHitContainer); tracker->fillHits(*product, name); if (product != nullptr && !product->empty()) - edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() - << " tracker hits <" << name << ">"; + edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " tracker hits <" << name << ">"; e.put(std::move(product), name); } } @@ -264,8 +260,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { std::unique_ptr product(new edm::PCaloHitContainer); calo->fillHits(*product, name); if (product != nullptr && !product->empty()) - edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() - << " calo hits <" << name << ">"; + edm::LogVerbatim("SimG4CoreApplication") << "Produced " << product->size() << " calo hits <" << name << ">"; e.put(std::move(product), name); } } @@ -274,9 +269,8 @@ 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() - << " rand= " << G4UniformRand(); + edm::LogVerbatim("SimG4CoreApplication") + << "Event is produced " << e.id() << " stream " << e.streamID() << " rand= " << G4UniformRand(); } StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) { diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 44b50fda8a4a8..bd2507fc16757 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -5,8 +5,6 @@ #include "SimG4Core/Application/interface/RunManagerMT.h" #include "SimG4Core/Geometry/interface/CustomUIsession.h" -#include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/Framework/interface/ESTransientHandle.h" #include "FWCore/Utilities/interface/EDMException.h" #include "G4PhysicalVolumeStore.hh" @@ -104,7 +102,9 @@ OscarMTMasterThread::~OscarMTMasterThread() { } } -void OscarMTMasterThread::beginRun(const DDCompactView* pDDD, const cms::DDCompactView* pDD4Hep, const HepPDT::ParticleDataTable* pPDT) const { +void OscarMTMasterThread::beginRun(const DDCompactView* pDDD, + const cms::DDCompactView* pDD4Hep, + const HepPDT::ParticleDataTable* pPDT) const { std::lock_guard lk(m_protectMutex); std::unique_lock lk2(m_threadMutex); diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index dc1b0a48c2549..2e4d75727cbee 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -16,7 +16,7 @@ #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" -#include "DataFormats/Common/interface/Handle.h" +//#include "DataFormats/Common/interface/Handle.h" #include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/thread_safety_macros.h" @@ -29,9 +29,6 @@ #include "SimG4Core/Watcher/interface/SimWatcherFactory.h" #include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/Framework/interface/ESHandle.h" -#include "FWCore/Framework/interface/ESTransientHandle.h" -#include "Geometry/Records/interface/IdealGeometryRecord.h" #include "SimG4Core/Geometry/interface/DDDWorld.h" #include "SimG4Core/MagneticField/interface/FieldBuilder.h" @@ -325,8 +322,7 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: if (!fieldFile.empty()) { std::call_once(applyOnce, []() { dumpMF = true; }); if (dumpMF) { - edm::LogVerbatim("SimG4CoreApplication") - << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile; + edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile; DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile); } } @@ -388,8 +384,7 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: timer.Stop(); edm::LogVerbatim("SimG4CoreApplication") - << "RunManagerMTWorker::initializeThread done for the thread " - << thisID << " " << timer; + << "RunManagerMTWorker::initializeThread done for the thread " << thisID << " " << timer; } void RunManagerMTWorker::initializeUserActions() { @@ -470,8 +465,7 @@ void RunManagerMTWorker::terminateRun() { return; } int thisID = getThreadIndex(); - edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " - << thisID << " is started"; + edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started"; if (m_tls->userRunAction) { m_tls->userRunAction->EndOfRunAction(m_tls->currentRun); m_tls->userRunAction.reset(); From 03f4773c918e30ce0e22f36e1cdedf675ecc5d50 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 5 Jul 2021 20:53:20 +0200 Subject: [PATCH 03/16] added forgotten --- .../Application/plugins/OscarMTProducer.cc | 14 ++++++------- .../Application/plugins/OscarMTProducer.h | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index a8f6038bfefb0..9882df1b0e18c 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -31,6 +31,13 @@ #include #include +edm::ESGetToken OscarMTProducer::m_DD4Hep; +edm::ESGetToken OscarMTProducer::m_DDD; +edm::ESGetToken OscarMTProducer::m_PDT; +edm::ESGetToken OscarMTProducer::m_MagField; +G4Mutex OscarMTProducer::m_OscarMutex = G4MUTEX_INITIALIZER; +bool OscarMTProducer::m_hasToken = false; + namespace edm { class StreamID; } @@ -64,13 +71,6 @@ namespace { }; } // namespace -edm::ESGetToken OscarMTProducer::m_DD4Hep; -edm::ESGetToken OscarMTProducer::m_DDD; -edm::ESGetToken OscarMTProducer::m_PDT; -edm::ESGetToken OscarMTProducer::m_MagField; -G4Mutex OscarMTProducer::m_OscarMutex = G4MUTEX_INITIALIZER; -bool OscarMTProducer::m_hasToken = false; - OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread* ms) { // Random number generation not allowed here StaticRandomEngineSetUnset random(nullptr); diff --git a/SimG4Core/Application/plugins/OscarMTProducer.h b/SimG4Core/Application/plugins/OscarMTProducer.h index bd6765444310b..b12214538ca49 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.h +++ b/SimG4Core/Application/plugins/OscarMTProducer.h @@ -3,13 +3,24 @@ #include "FWCore/Framework/interface/stream/EDProducer.h" #include "FWCore/Framework/interface/Event.h" -#include "DataFormats/Common/interface/Handle.h" #include "FWCore/Framework/interface/MakerMacros.h" #include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/Framework/interface/Run.h" #include "SimG4Core/Application/interface/OscarMTMasterThread.h" +#include "DetectorDescription/Core/interface/DDCompactView.h" +#include "DetectorDescription/DDCMS/interface/DDCompactView.h" +#include "Geometry/Records/interface/IdealGeometryRecord.h" + +#include "HepPDT/ParticleDataTable.hh" +#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" + +#include "MagneticField/Engine/interface/MagneticField.h" +#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" + +#include "G4Threading.hh" + #include class SimProducer; @@ -38,6 +49,13 @@ class OscarMTProducer : public edm::stream::EDProducer m_runManagerWorker; const OscarMTMasterThread* m_masterThread = nullptr; + + static edm::ESGetToken m_DD4Hep; + static edm::ESGetToken m_DDD; + static edm::ESGetToken m_PDT; + static edm::ESGetToken m_MagField; + static G4Mutex m_OscarMutex; + static bool m_hasToken; }; #endif From 3bf6f9a2e78d7caac41e403d2c2dc6ee79049294 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 12:01:02 +0200 Subject: [PATCH 04/16] implemented suggestings --- .../interface/OscarMTMasterThread.h | 10 ++- .../Application/plugins/OscarMTProducer.cc | 67 +++++++++---------- .../Application/plugins/OscarMTProducer.h | 17 ----- .../Application/src/OscarMTMasterThread.cc | 28 +++++--- 4 files changed, 58 insertions(+), 64 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 23a9867b07d4f..9aea20404a6ae 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -6,6 +6,7 @@ #include "DetectorDescription/Core/interface/DDCompactView.h" #include "DetectorDescription/DDCMS/interface/DDCompactView.h" +#include "Geometry/Records/interface/IdealGeometryRecord.h" #include "HepPDT/ParticleDataTable.hh" #include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" @@ -37,10 +38,14 @@ class OscarMTMasterThread { explicit OscarMTMasterThread(const edm::ParameterSet& iConfig); ~OscarMTMasterThread(); - void beginRun(const DDCompactView*, const cms::DDCompactView*, const HepPDT::ParticleDataTable*) const; + void beginRun(const edm::EventSetup& iSetup) const; void endRun() const; void stopThread(); + void SetTokens(edm::ESGetToken&, + edm::ESGetToken&, + edm::ESGetToken&) const; + inline RunManagerMT& runManagerMaster() const { return *m_runManagerMaster; } inline RunManagerMT* runManagerMasterPtr() const { return m_runManagerMaster.get(); } inline bool isDD4Hep() const { return m_pGeoFromDD4hep; } @@ -57,6 +62,9 @@ class OscarMTMasterThread { mutable const DDCompactView* m_pDDD = nullptr; mutable const cms::DDCompactView* m_pDD4Hep = nullptr; mutable const HepPDT::ParticleDataTable* m_pTable = nullptr; + mutable edm::ESGetToken m_DDD; + mutable edm::ESGetToken m_DD4Hep; + mutable edm::ESGetToken m_PDT; // status flags mutable std::mutex m_protectMutex; diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index 9882df1b0e18c..a8aad3f885dd6 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -1,41 +1,43 @@ -#include "FWCore/PluginManager/interface/PluginManager.h" +#include "OscarMTProducer.h" +#include "FWCore/PluginManager/interface/PluginManager.h" #include "FWCore/Framework/interface/MakerMacros.h" #include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "OscarMTProducer.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "FWCore/Utilities/interface/RandomNumberGenerator.h" +#include "FWCore/Utilities/interface/Exception.h" +#include "SimG4Core/Notification/interface/SimG4Exception.h" #include "SimG4Core/Application/interface/RunManagerMT.h" #include "SimG4Core/Application/interface/RunManagerMTWorker.h" #include "SimG4Core/Notification/interface/G4SimEvent.h" #include "SimG4Core/SensitiveDetector/interface/SensitiveTkDetector.h" #include "SimG4Core/SensitiveDetector/interface/SensitiveCaloDetector.h" +#include "SimG4Core/Watcher/interface/SimProducer.h" #include "SimDataFormats/Track/interface/SimTrackContainer.h" #include "SimDataFormats/Vertex/interface/SimVertexContainer.h" #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h" #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h" -#include "SimG4Core/Watcher/interface/SimProducer.h" +#include "Randomize.hh" -#include "FWCore/Utilities/interface/Exception.h" -#include "SimG4Core/Notification/interface/SimG4Exception.h" +#include "DetectorDescription/Core/interface/DDCompactView.h" +#include "DetectorDescription/DDCMS/interface/DDCompactView.h" +#include "Geometry/Records/interface/IdealGeometryRecord.h" -#include "FWCore/ServiceRegistry/interface/Service.h" -#include "FWCore/Utilities/interface/RandomNumberGenerator.h" -#include "Randomize.hh" +#include "HepPDT/ParticleDataTable.hh" +#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" -#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "MagneticField/Engine/interface/MagneticField.h" +#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" #include #include -edm::ESGetToken OscarMTProducer::m_DD4Hep; -edm::ESGetToken OscarMTProducer::m_DDD; -edm::ESGetToken OscarMTProducer::m_PDT; -edm::ESGetToken OscarMTProducer::m_MagField; -G4Mutex OscarMTProducer::m_OscarMutex = G4MUTEX_INITIALIZER; bool OscarMTProducer::m_hasToken = false; namespace edm { @@ -81,19 +83,21 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster // Prepare tokens bool isDD4Hep = p.getParameter("g4GeometryDD4hepSource"); if (!m_hasToken) { - G4MUTEXLOCK(&m_OscarMutex); - if (!m_hasToken) { - if (isDD4Hep) { - m_DD4Hep = esConsumes(); - } else { - m_DDD = esConsumes(); - } - m_PDT = esConsumes(); - m_MagField = esConsumes(); + m_hasToken = true; + edm::ESGetToken pDD4Hep; + edm::ESGetToken pDDD; + if (isDD4Hep) { + pDD4Hep = esConsumes(); + } else { + pDDD = esConsumes(); } - G4MUTEXUNLOCK(&m_OscarMutex); + edm::ESGetToken pPDT = + esConsumes(); + m_masterThread->SetTokens(pDDD, pDD4Hep, pPDT); } - m_runManagerWorker->SetMagFieldToken(m_MagField); + edm::ESGetToken pMagField = + esConsumes(); + m_runManagerWorker->SetMagFieldToken(pMagField); // List of produced containers produces().setBranchAlias("SimTracks"); @@ -175,18 +179,7 @@ std::shared_ptr OscarMTProducer::globalBeginRun(const edm::Run&, // Random number generation not allowed here StaticRandomEngineSetUnset random(nullptr); edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun"; - - // Prepare master thread - const DDCompactView* pDDD = nullptr; - const cms::DDCompactView* pDD4Hep = nullptr; - if (masterThread->isDD4Hep()) { - pDD4Hep = &(iSetup.getData(m_DD4Hep)); - } else { - pDDD = &(iSetup.getData(m_DDD)); - } - const HepPDT::ParticleDataTable* pPDT = &(iSetup.getData(m_PDT)); - masterThread->beginRun(pDDD, pDD4Hep, pPDT); - + masterThread->beginRun(iSetup); edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::globalBeginRun done"; return std::shared_ptr(); } diff --git a/SimG4Core/Application/plugins/OscarMTProducer.h b/SimG4Core/Application/plugins/OscarMTProducer.h index b12214538ca49..786ea1209dbab 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.h +++ b/SimG4Core/Application/plugins/OscarMTProducer.h @@ -9,18 +9,6 @@ #include "SimG4Core/Application/interface/OscarMTMasterThread.h" -#include "DetectorDescription/Core/interface/DDCompactView.h" -#include "DetectorDescription/DDCMS/interface/DDCompactView.h" -#include "Geometry/Records/interface/IdealGeometryRecord.h" - -#include "HepPDT/ParticleDataTable.hh" -#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" - -#include "MagneticField/Engine/interface/MagneticField.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - -#include "G4Threading.hh" - #include class SimProducer; @@ -50,11 +38,6 @@ class OscarMTProducer : public edm::stream::EDProducer m_runManagerWorker; const OscarMTMasterThread* m_masterThread = nullptr; - static edm::ESGetToken m_DD4Hep; - static edm::ESGetToken m_DDD; - static edm::ESGetToken m_PDT; - static edm::ESGetToken m_MagField; - static G4Mutex m_OscarMutex; static bool m_hasToken; }; diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index bd2507fc16757..445f20f3f27ee 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -102,17 +102,18 @@ OscarMTMasterThread::~OscarMTMasterThread() { } } -void OscarMTMasterThread::beginRun(const DDCompactView* pDDD, - const cms::DDCompactView* pDD4Hep, - const HepPDT::ParticleDataTable* pPDT) const { +void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { std::lock_guard lk(m_protectMutex); - std::unique_lock lk2(m_threadMutex); - edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::beginRun"; - m_pDDD = pDDD; - m_pDD4Hep = pDD4Hep; - m_pTable = pPDT; + + if (m_pGeoFromDD4hep) { + m_pDD4Hep = &(iSetup.getData(m_DD4Hep)); + } else { + m_pDDD = &(iSetup.getData(m_DDD)); + } + m_pTable = &(iSetup.getData(m_PDT)); + m_masterThreadState = ThreadState::BeginRun; m_masterCanProceed = true; m_mainCanProceed = false; @@ -127,8 +128,8 @@ void OscarMTMasterThread::beginRun(const DDCompactView* pDDD, void OscarMTMasterThread::endRun() const { std::lock_guard lk(m_protectMutex); - std::unique_lock lk2(m_threadMutex); + m_masterThreadState = ThreadState::EndRun; m_mainCanProceed = false; m_masterCanProceed = true; @@ -163,3 +164,12 @@ void OscarMTMasterThread::stopThread() { edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished"; m_stopped = true; } + +void OscarMTMasterThread::SetTokens( + edm::ESGetToken& rDDD, + edm::ESGetToken& rDD4Hep, + edm::ESGetToken& rPDT) const { + m_DDD = rDDD; + m_DD4Hep = rDD4Hep; + m_PDT = rPDT; +} From dd9c00b29026553b6af77bd94f8571ab7594e3a2 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 12:06:50 +0200 Subject: [PATCH 05/16] code-checks --- .../Application/interface/OscarMTMasterThread.h | 4 ++-- SimG4Core/Application/plugins/OscarMTProducer.cc | 4 ++-- SimG4Core/Application/src/OscarMTMasterThread.cc | 7 +++---- SimG4Core/Watcher/interface/SimProducer.h | 13 +++---------- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 9aea20404a6ae..11d271dd7fda4 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -42,7 +42,7 @@ class OscarMTMasterThread { void endRun() const; void stopThread(); - void SetTokens(edm::ESGetToken&, + void SetTokens(edm::ESGetToken&, edm::ESGetToken&, edm::ESGetToken&) const; @@ -62,7 +62,7 @@ class OscarMTMasterThread { mutable const DDCompactView* m_pDDD = nullptr; mutable const cms::DDCompactView* m_pDD4Hep = nullptr; mutable const HepPDT::ParticleDataTable* m_pTable = nullptr; - mutable edm::ESGetToken m_DDD; + mutable edm::ESGetToken m_DDD; mutable edm::ESGetToken m_DD4Hep; mutable edm::ESGetToken m_PDT; diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index a8aad3f885dd6..fe73b3ebb0cca 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -92,11 +92,11 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster pDDD = esConsumes(); } edm::ESGetToken pPDT = - esConsumes(); + esConsumes(); m_masterThread->SetTokens(pDDD, pDD4Hep, pPDT); } edm::ESGetToken pMagField = - esConsumes(); + esConsumes(); m_runManagerWorker->SetMagFieldToken(pMagField); // List of produced containers diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 445f20f3f27ee..4412fe1377d88 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -165,10 +165,9 @@ void OscarMTMasterThread::stopThread() { m_stopped = true; } -void OscarMTMasterThread::SetTokens( - edm::ESGetToken& rDDD, - edm::ESGetToken& rDD4Hep, - edm::ESGetToken& rPDT) const { +void OscarMTMasterThread::SetTokens(edm::ESGetToken& rDDD, + edm::ESGetToken& rDD4Hep, + edm::ESGetToken& rPDT) const { m_DDD = rDDD; m_DD4Hep = rDD4Hep; m_PDT = rPDT; diff --git a/SimG4Core/Watcher/interface/SimProducer.h b/SimG4Core/Watcher/interface/SimProducer.h index 8292ecd1199e6..b0b2420c7d151 100644 --- a/SimG4Core/Watcher/interface/SimProducer.h +++ b/SimG4Core/Watcher/interface/SimProducer.h @@ -64,13 +64,7 @@ namespace simproducer { class SimProducer : public SimWatcher { public: SimProducer() {} - // virtual ~SimProducer(); - // ---------- const member functions --------------------- - - // ---------- static member functions -------------------- - - // ---------- member functions --------------------------- virtual void produce(edm::Event &, const edm::EventSetup &) = 0; void registerProducts(edm::ProducesCollector producesCollector) { @@ -92,12 +86,11 @@ class SimProducer : public SimWatcher { m_info.push_back(std::make_shared>(instanceName)); } -private: - SimProducer(const SimProducer &) = delete; // stop default + SimProducer(const SimProducer &) = delete; + const SimProducer &operator=(const SimProducer &) = delete; - const SimProducer &operator=(const SimProducer &) = delete; // stop default +private: - // ---------- member data -------------------------------- std::vector> m_info; }; From 97aa342d86216632ba76b07225518cae3c176e26 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 12:56:01 +0200 Subject: [PATCH 06/16] code-checks --- SimG4Core/Watcher/interface/SimProducer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SimG4Core/Watcher/interface/SimProducer.h b/SimG4Core/Watcher/interface/SimProducer.h index b0b2420c7d151..24894f0b677e9 100644 --- a/SimG4Core/Watcher/interface/SimProducer.h +++ b/SimG4Core/Watcher/interface/SimProducer.h @@ -75,6 +75,9 @@ class SimProducer : public SimWatcher { }); } + SimProducer(const SimProducer &) = delete; + const SimProducer &operator=(const SimProducer &) = delete; + protected: template void produces() { @@ -86,9 +89,6 @@ class SimProducer : public SimWatcher { m_info.push_back(std::make_shared>(instanceName)); } - SimProducer(const SimProducer &) = delete; - const SimProducer &operator=(const SimProducer &) = delete; - private: std::vector> m_info; From 7c736293135589bace2bf835b095cfe4ae0275b3 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 13:27:55 +0200 Subject: [PATCH 07/16] provide regression --- SimG4Core/Application/plugins/OscarMTProducer.cc | 6 ++++-- SimG4Core/Application/src/RunManagerMTWorker.cc | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index fe73b3ebb0cca..e873e43fbd6c9 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -212,7 +212,8 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) { void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { StaticRandomEngineSetUnset random(e.streamID()); edm::LogVerbatim("SimG4CoreApplication") - << "Produce event " << e.id() << " stream " << e.streamID() << " rand= " << G4UniformRand(); + << "Produce event " << e.id() << " stream " << e.streamID(); + //edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand(); auto& sTk = m_runManagerWorker->sensTkDetectors(); auto& sCalo = m_runManagerWorker->sensCaloDetectors(); @@ -263,7 +264,8 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { prod.get()->produce(e, es); } edm::LogVerbatim("SimG4CoreApplication") - << "Event is produced " << e.id() << " stream " << e.streamID() << " rand= " << G4UniformRand(); + << "Event is produced " << e.id() << " stream " << e.streamID(); + //edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand(); } StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) { diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index 2e4d75727cbee..c740b1263b52f 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -311,11 +311,11 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: if (m_pUseMagneticField) { const GlobalPoint g(0., 0., 0.); - CMSFieldManager* fieldManager = new CMSFieldManager(); - tM->SetFieldManager(fieldManager); - const MagneticField* pMF = &(es.getData(m_MagField)); sim::FieldBuilder fieldBuilder(pMF, m_pField); + + CMSFieldManager* fieldManager = new CMSFieldManager(); + tM->SetFieldManager(fieldManager); fieldBuilder.build(fieldManager, tM->GetPropagatorInField()); std::string fieldFile = m_p.getUntrackedParameter("FileNameField", ""); From c150a5f6a8191974591ad0ee90c90fd0bf898d23 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 14:39:26 +0200 Subject: [PATCH 08/16] code-checks --- SimG4Core/Application/plugins/OscarMTProducer.cc | 6 ++---- SimG4Core/Watcher/interface/SimProducer.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index e873e43fbd6c9..dc3f6f29ffe82 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -211,8 +211,7 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) { void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { StaticRandomEngineSetUnset random(e.streamID()); - edm::LogVerbatim("SimG4CoreApplication") - << "Produce event " << e.id() << " stream " << e.streamID(); + edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID(); //edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand(); auto& sTk = m_runManagerWorker->sensTkDetectors(); @@ -263,8 +262,7 @@ 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") << "Event is produced " << e.id() << " stream " << e.streamID(); //edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand(); } diff --git a/SimG4Core/Watcher/interface/SimProducer.h b/SimG4Core/Watcher/interface/SimProducer.h index 24894f0b677e9..8e8b95ef44660 100644 --- a/SimG4Core/Watcher/interface/SimProducer.h +++ b/SimG4Core/Watcher/interface/SimProducer.h @@ -90,7 +90,6 @@ class SimProducer : public SimWatcher { } private: - std::vector> m_info; }; From 44fe54330b6247cc0f3cb9df0d8660f09943b882 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 16:13:34 +0200 Subject: [PATCH 09/16] removed last static member from OscarMTProducer --- .../interface/OscarMTMasterThread.h | 1 + .../Application/plugins/OscarMTProducer.cc | 24 ++++++++----------- .../Application/plugins/OscarMTProducer.h | 2 -- .../Application/src/OscarMTMasterThread.cc | 9 ++++--- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 11d271dd7fda4..271c62d72570f 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -74,6 +74,7 @@ class OscarMTMasterThread { mutable ThreadState m_masterThreadState; + mutable bool m_hasToken = false; mutable bool m_masterCanProceed = false; mutable bool m_mainCanProceed = false; mutable bool m_firstRun = true; diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index dc3f6f29ffe82..ba9e5d78d30cc 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -38,8 +38,6 @@ #include #include -bool OscarMTProducer::m_hasToken = false; - namespace edm { class StreamID; } @@ -82,19 +80,17 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster // Prepare tokens bool isDD4Hep = p.getParameter("g4GeometryDD4hepSource"); - if (!m_hasToken) { - m_hasToken = true; - edm::ESGetToken pDD4Hep; - edm::ESGetToken pDDD; - if (isDD4Hep) { - pDD4Hep = esConsumes(); - } else { - pDDD = esConsumes(); - } - edm::ESGetToken pPDT = - esConsumes(); - m_masterThread->SetTokens(pDDD, pDD4Hep, pPDT); + edm::ESGetToken pDD4Hep; + edm::ESGetToken pDDD; + if (isDD4Hep) { + pDD4Hep = esConsumes(); + } else { + pDDD = esConsumes(); } + edm::ESGetToken pPDT = + esConsumes(); + m_masterThread->SetTokens(pDDD, pDD4Hep, pPDT); + edm::ESGetToken pMagField = esConsumes(); m_runManagerWorker->SetMagFieldToken(pMagField); diff --git a/SimG4Core/Application/plugins/OscarMTProducer.h b/SimG4Core/Application/plugins/OscarMTProducer.h index 786ea1209dbab..5d9bc43ce25d7 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.h +++ b/SimG4Core/Application/plugins/OscarMTProducer.h @@ -37,8 +37,6 @@ class OscarMTProducer : public edm::stream::EDProducer m_runManagerWorker; const OscarMTMasterThread* m_masterThread = nullptr; - - static bool m_hasToken; }; #endif diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 4412fe1377d88..c1c5581b3e5ad 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -168,7 +168,10 @@ void OscarMTMasterThread::stopThread() { void OscarMTMasterThread::SetTokens(edm::ESGetToken& rDDD, edm::ESGetToken& rDD4Hep, edm::ESGetToken& rPDT) const { - m_DDD = rDDD; - m_DD4Hep = rDD4Hep; - m_PDT = rPDT; + if (!m_hasToken) { + m_DDD = rDDD; + m_DD4Hep = rDD4Hep; + m_PDT = rPDT; + m_hasToken = true; + } } From deffd8c8e5485bbafff0d6f57efb625b4df75006 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jul 2021 17:18:48 +0200 Subject: [PATCH 10/16] tokens moved to OscarMTMasterThread --- .../interface/OscarMTMasterThread.h | 7 ++--- .../interface/RunManagerMTWorker.h | 4 +-- .../Application/plugins/OscarMTProducer.cc | 30 ++---------------- .../Application/src/OscarMTMasterThread.cc | 31 ++++++++++--------- .../Application/src/RunManagerMTWorker.cc | 3 +- 5 files changed, 25 insertions(+), 50 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 271c62d72570f..651be50029173 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -18,7 +18,8 @@ namespace edm { class EventSetup; -} + class ConsumesCollector; +} // namespace edm class RunManagerMT; @@ -42,9 +43,7 @@ class OscarMTMasterThread { void endRun() const; void stopThread(); - void SetTokens(edm::ESGetToken&, - edm::ESGetToken&, - edm::ESGetToken&) const; + void callConsumes(edm::ConsumesCollector&& iC) const; inline RunManagerMT& runManagerMaster() const { return *m_runManagerMaster; } inline RunManagerMT* runManagerMasterPtr() const { return m_runManagerMaster.get(); } diff --git a/SimG4Core/Application/interface/RunManagerMTWorker.h b/SimG4Core/Application/interface/RunManagerMTWorker.h index e8881065392c3..fdc5f929b62c4 100644 --- a/SimG4Core/Application/interface/RunManagerMTWorker.h +++ b/SimG4Core/Application/interface/RunManagerMTWorker.h @@ -47,7 +47,7 @@ class SimProducer; class RunManagerMTWorker { public: - explicit RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& i); + explicit RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC); ~RunManagerMTWorker(); void beginRun(const edm::EventSetup&); @@ -74,8 +74,6 @@ class RunManagerMTWorker { void initializeG4(RunManagerMT* runManagerMaster, const edm::EventSetup& es); - inline void SetMagFieldToken(edm::ESGetToken& ref) { m_MagField = ref; } - private: void initializeTLS(); void initializeUserActions(); diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index ba9e5d78d30cc..cfce6e9fed2d6 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -25,16 +25,6 @@ #include "Randomize.hh" -#include "DetectorDescription/Core/interface/DDCompactView.h" -#include "DetectorDescription/DDCMS/interface/DDCompactView.h" -#include "Geometry/Records/interface/IdealGeometryRecord.h" - -#include "HepPDT/ParticleDataTable.hh" -#include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" - -#include "MagneticField/Engine/interface/MagneticField.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - #include #include @@ -77,23 +67,7 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster m_runManagerWorker = std::make_unique(p, consumesCollector()); m_masterThread = ms; - - // Prepare tokens - bool isDD4Hep = p.getParameter("g4GeometryDD4hepSource"); - edm::ESGetToken pDD4Hep; - edm::ESGetToken pDDD; - if (isDD4Hep) { - pDD4Hep = esConsumes(); - } else { - pDDD = esConsumes(); - } - edm::ESGetToken pPDT = - esConsumes(); - m_masterThread->SetTokens(pDDD, pDD4Hep, pPDT); - - edm::ESGetToken pMagField = - esConsumes(); - m_runManagerWorker->SetMagFieldToken(pMagField); + m_masterThread->callConsumes(consumesCollector()); // List of produced containers produces().setBranchAlias("SimTracks"); @@ -156,7 +130,7 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster for (auto& ptr : producers) { ptr->registerProducts(producesCollector()); } - edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed DD4Hep: " << isDD4Hep; + edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer is constructed"; } OscarMTProducer::~OscarMTProducer() {} diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index c1c5581b3e5ad..007d7aafb116b 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -1,12 +1,12 @@ #include #include "SimG4Core/Application/interface/OscarMTMasterThread.h" +#include "FWCore/Utilities/interface/EDMException.h" +#include "FWCore/Framework/interface/ConsumesCollector.h" #include "SimG4Core/Application/interface/RunManagerMT.h" #include "SimG4Core/Geometry/interface/CustomUIsession.h" -#include "FWCore/Utilities/interface/EDMException.h" - #include "G4PhysicalVolumeStore.hh" OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) @@ -15,7 +15,8 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) // Lock the mutex std::unique_lock lk(m_threadMutex); - edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: creating master thread"; + edm::LogVerbatim("SimG4CoreApplication") + << "OscarMTMasterThread: creating master thread DD4Hep: " << m_pGeoFromDD4hep; // Create Geant4 master thread m_masterThread = std::thread([&]() { @@ -102,6 +103,19 @@ OscarMTMasterThread::~OscarMTMasterThread() { } } +void OscarMTMasterThread::callConsumes(edm::ConsumesCollector&& iC) const { + if (m_hasToken) { + return; + } + if (m_pGeoFromDD4hep) { + m_DD4Hep = iC.esConsumes(); + } else { + m_DDD = iC.esConsumes(); + } + m_PDT = iC.esConsumes(); + m_hasToken = true; +} + void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { std::lock_guard lk(m_protectMutex); std::unique_lock lk2(m_threadMutex); @@ -164,14 +178,3 @@ void OscarMTMasterThread::stopThread() { edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished"; m_stopped = true; } - -void OscarMTMasterThread::SetTokens(edm::ESGetToken& rDDD, - edm::ESGetToken& rDD4Hep, - edm::ESGetToken& rPDT) const { - if (!m_hasToken) { - m_DDD = rDDD; - m_DD4Hep = rDD4Hep; - m_PDT = rPDT; - m_hasToken = true; - } -} diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index c740b1263b52f..b58de03742898 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -16,7 +16,6 @@ #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" -//#include "DataFormats/Common/interface/Handle.h" #include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/thread_safety_macros.h" @@ -170,6 +169,8 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co if (m_LHCTransport) { m_LHCToken = iC.consumes(edm::InputTag("LHCTransport")); } + m_MagField = iC.esConsumes(); + edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID; unsigned int k = 0; for (std::unordered_map>::const_iterator itr = From 3706a262d9dc3e0c1955fc3a2c1f0d236f2c2d83 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 8 Jul 2021 20:32:04 +0200 Subject: [PATCH 11/16] clean-up --- SimG4Core/Application/src/OscarMTMasterThread.cc | 16 +++++++++------- SimG4Core/Application/src/RunManagerMT.cc | 5 ----- SimG4Core/Application/src/RunManagerMTWorker.cc | 14 +++++++------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 007d7aafb116b..4f5fc75a226ce 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -48,6 +48,7 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) m_masterCanProceed = false; edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, starting wait"; m_notifyMasterCv.wait(lk2, [&] { return m_masterCanProceed; }); + //m_notifyMasterCv.wait(lk2, [&] { return false; }); // Act according to the state edm::LogVerbatim("OscarMTMasterThread") @@ -121,17 +122,18 @@ void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { std::unique_lock lk2(m_threadMutex); edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::beginRun"; - if (m_pGeoFromDD4hep) { - m_pDD4Hep = &(iSetup.getData(m_DD4Hep)); - } else { - m_pDDD = &(iSetup.getData(m_DDD)); + if(m_firstRun) { + if (m_pGeoFromDD4hep) { + m_pDD4Hep = &(*iSetup.getTransientHandle(m_DD4Hep)); + } else { + m_pDDD = &(*iSetup.getTransientHandle(m_DDD)); + } + m_pTable = &(*iSetup.getTransientHandle(m_PDT)); + m_firstRun = false; } - m_pTable = &(iSetup.getData(m_PDT)); - m_masterThreadState = ThreadState::BeginRun; m_masterCanProceed = true; m_mainCanProceed = false; - m_firstRun = false; edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for BeginRun"; m_notifyMasterCv.notify_one(); m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; }); diff --git a/SimG4Core/Application/src/RunManagerMT.cc b/SimG4Core/Application/src/RunManagerMT.cc index bc79172131078..22b69f68bf938 100644 --- a/SimG4Core/Application/src/RunManagerMT.cc +++ b/SimG4Core/Application/src/RunManagerMT.cc @@ -203,11 +203,6 @@ void RunManagerMT::initG4(const DDCompactView* pDD, throw edm::Exception(edm::errors::LogicError, "G4RunManagerKernel initialization failed!"); } - timer.Stop(); - G4cout.precision(4); - G4cout << "RunManagerMT: physics is initialized: " << timer << G4endl; - timer.Start(); - if (m_StorePhysicsTables) { std::ostringstream dir; dir << m_PhysicsTablesDir << '\0'; diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index b58de03742898..05ede0b54d1fb 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -27,13 +27,12 @@ #include "SimG4Core/Notification/interface/CMSSteppingVerbose.h" #include "SimG4Core/Watcher/interface/SimWatcherFactory.h" -#include "FWCore/Framework/interface/EventSetup.h" - #include "SimG4Core/Geometry/interface/DDDWorld.h" #include "SimG4Core/MagneticField/interface/FieldBuilder.h" #include "SimG4Core/MagneticField/interface/CMSFieldManager.h" #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h" +#include "DataFormats/GeometryVector/interface/GlobalPoint.h" #include "SimG4Core/Physics/interface/PhysicsList.h" @@ -169,8 +168,9 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co if (m_LHCTransport) { m_LHCToken = iC.consumes(edm::InputTag("LHCTransport")); } - m_MagField = iC.esConsumes(); - + if (m_pUseMagneticField) { + m_MagField = iC.esConsumes(); + } edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID; unsigned int k = 0; for (std::unordered_map>::const_iterator itr = @@ -310,11 +310,11 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: // setup the magnetic field if (m_pUseMagneticField) { - const GlobalPoint g(0., 0., 0.); + const GlobalPoint g(0.f, 0.f, 0.f); - const MagneticField* pMF = &(es.getData(m_MagField)); + const MagneticField* pMF = &(*es.getTransientHandle(m_MagField)); sim::FieldBuilder fieldBuilder(pMF, m_pField); - + CMSFieldManager* fieldManager = new CMSFieldManager(); tM->SetFieldManager(fieldManager); fieldBuilder.build(fieldManager, tM->GetPropagatorInField()); From 0ddcfd38918e0146abcd1e97ed7e3fdce2ea08db Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 8 Jul 2021 21:11:44 +0200 Subject: [PATCH 12/16] updated --- SimG4Core/Application/src/OscarMTMasterThread.cc | 2 +- SimG4Core/Application/src/RunManagerMTWorker.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 4f5fc75a226ce..beb53f4afd62d 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -122,7 +122,7 @@ void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { std::unique_lock lk2(m_threadMutex); edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::beginRun"; - if(m_firstRun) { + if (m_firstRun) { if (m_pGeoFromDD4hep) { m_pDD4Hep = &(*iSetup.getTransientHandle(m_DD4Hep)); } else { diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index 05ede0b54d1fb..d9f886bcd9587 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -314,7 +314,7 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: const MagneticField* pMF = &(*es.getTransientHandle(m_MagField)); sim::FieldBuilder fieldBuilder(pMF, m_pField); - + CMSFieldManager* fieldManager = new CMSFieldManager(); tM->SetFieldManager(fieldManager); fieldBuilder.build(fieldManager, tM->GetPropagatorInField()); From b7aadfbeec9caf3571ef31c50d599b13a3425044 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 9 Jul 2021 19:26:00 +0200 Subject: [PATCH 13/16] moved all consumes to the master thread --- SimG4Core/Application/interface/OscarMTMasterThread.h | 6 +++++- SimG4Core/Application/interface/RunManagerMT.h | 5 +++++ SimG4Core/Application/interface/RunManagerMTWorker.h | 4 ---- SimG4Core/Application/src/OscarMTMasterThread.cc | 9 ++++++++- SimG4Core/Application/src/RunManagerMTWorker.cc | 7 +++++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 651be50029173..61860652ff176 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -11,6 +11,9 @@ #include "HepPDT/ParticleDataTable.hh" #include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" +#include "MagneticField/Engine/interface/MagneticField.h" +#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" + #include #include #include @@ -47,12 +50,12 @@ class OscarMTMasterThread { inline RunManagerMT& runManagerMaster() const { return *m_runManagerMaster; } inline RunManagerMT* runManagerMasterPtr() const { return m_runManagerMaster.get(); } - inline bool isDD4Hep() const { return m_pGeoFromDD4hep; } private: enum class ThreadState { NotExist = 0, BeginRun = 1, EndRun = 2, Destruct = 3 }; const bool m_pGeoFromDD4hep; + const bool m_pUseMagneticField; std::shared_ptr m_runManagerMaster; std::thread m_masterThread; @@ -64,6 +67,7 @@ class OscarMTMasterThread { mutable edm::ESGetToken m_DDD; mutable edm::ESGetToken m_DD4Hep; mutable edm::ESGetToken m_PDT; + mutable edm::ESGetToken m_MF; // status flags mutable std::mutex m_protectMutex; diff --git a/SimG4Core/Application/interface/RunManagerMT.h b/SimG4Core/Application/interface/RunManagerMT.h index a290b6c93ae49..c98d0c0d7fa20 100644 --- a/SimG4Core/Application/interface/RunManagerMT.h +++ b/SimG4Core/Application/interface/RunManagerMT.h @@ -35,6 +35,7 @@ class G4Event; class G4StateManager; class G4GeometryManager; class RunAction; +class MagneticField; class SimRunInterface; @@ -79,6 +80,9 @@ class RunManagerMT { // with TLS. inline PhysicsList* physicsListForWorker() const { return m_physicsList.get(); } + inline void setMagField(const MagneticField* ptr) { m_pMagField = ptr; } + inline const MagneticField* getMagField() const { return m_pMagField; } + private: void terminateRun(); @@ -92,6 +96,7 @@ class RunManagerMT { G4Run* m_currentRun; G4StateManager* m_stateManager; G4GeometryManager* m_geometryManager; + const MagneticField* m_pMagField = nullptr; std::unique_ptr m_runInterface; diff --git a/SimG4Core/Application/interface/RunManagerMTWorker.h b/SimG4Core/Application/interface/RunManagerMTWorker.h index fdc5f929b62c4..d522c96673783 100644 --- a/SimG4Core/Application/interface/RunManagerMTWorker.h +++ b/SimG4Core/Application/interface/RunManagerMTWorker.h @@ -7,9 +7,6 @@ #include "SimG4Core/Generators/interface/Generator.h" #include "SimDataFormats/Forward/interface/LHCTransportLinkContainer.h" -#include "MagneticField/Engine/interface/MagneticField.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - #include #include #include @@ -92,7 +89,6 @@ class RunManagerMTWorker { edm::EDGetTokenT m_InToken; edm::EDGetTokenT m_LHCToken; edm::EDGetTokenT m_theLHCTlinkToken; - edm::ESGetToken m_MagField; bool m_nonBeam; bool m_pUseMagneticField; diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index beb53f4afd62d..d680855092fc3 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -11,6 +11,7 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) : m_pGeoFromDD4hep(iConfig.getParameter("g4GeometryDD4hepSource")), + m_pUseMagneticField(iConfig.getParameter("UseMagneticField")), m_masterThreadState(ThreadState::NotExist) { // Lock the mutex std::unique_lock lk(m_threadMutex); @@ -114,6 +115,9 @@ void OscarMTMasterThread::callConsumes(edm::ConsumesCollector&& iC) const { m_DDD = iC.esConsumes(); } m_PDT = iC.esConsumes(); + if (m_pUseMagneticField) { + m_MF = iC.esConsumes(); + } m_hasToken = true; } @@ -128,7 +132,10 @@ void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { } else { m_pDDD = &(*iSetup.getTransientHandle(m_DDD)); } - m_pTable = &(*iSetup.getTransientHandle(m_PDT)); + m_pTable = &iSetup.getData(m_PDT); + if(m_pUseMagneticField) { + m_runManagerMaster->setMagField(&iSetup.getData(m_MF)); + } m_firstRun = false; } m_masterThreadState = ThreadState::BeginRun; diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index d9f886bcd9587..81bf91d3e6965 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -19,6 +19,7 @@ #include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/thread_safety_macros.h" +#include "MagneticField/Engine/interface/MagneticField.h" #include "SimG4Core/Notification/interface/G4SimEvent.h" #include "SimG4Core/Notification/interface/SimActivityRegistry.h" @@ -168,9 +169,11 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co if (m_LHCTransport) { m_LHCToken = iC.consumes(edm::InputTag("LHCTransport")); } + /* if (m_pUseMagneticField) { m_MagField = iC.esConsumes(); } + */ edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID; unsigned int k = 0; for (std::unordered_map>::const_iterator itr = @@ -312,8 +315,8 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: if (m_pUseMagneticField) { const GlobalPoint g(0.f, 0.f, 0.f); - const MagneticField* pMF = &(*es.getTransientHandle(m_MagField)); - sim::FieldBuilder fieldBuilder(pMF, m_pField); + //const MagneticField* pMF = &es.getData(m_MagField); + sim::FieldBuilder fieldBuilder(runManagerMaster->getMagField(), m_pField); CMSFieldManager* fieldManager = new CMSFieldManager(); tM->SetFieldManager(fieldManager); From ce5dfbff961d9dadc604b3f26c90e53129bc2bf4 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 9 Jul 2021 19:28:46 +0200 Subject: [PATCH 14/16] code-checks --- SimG4Core/Application/src/OscarMTMasterThread.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index d680855092fc3..138b3b76abe86 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -133,7 +133,7 @@ void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { m_pDDD = &(*iSetup.getTransientHandle(m_DDD)); } m_pTable = &iSetup.getData(m_PDT); - if(m_pUseMagneticField) { + if (m_pUseMagneticField) { m_runManagerMaster->setMagField(&iSetup.getData(m_MF)); } m_firstRun = false; From 97e773a29f6e12e075928ae4a4310a3a2fca85a6 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 12 Jul 2021 20:21:06 +0200 Subject: [PATCH 15/16] return back mmagnetic field to worker --- SimG4Core/Application/interface/OscarMTMasterThread.h | 5 ----- SimG4Core/Application/interface/RunManagerMT.h | 5 ----- SimG4Core/Application/interface/RunManagerMTWorker.h | 5 +++++ SimG4Core/Application/src/OscarMTMasterThread.cc | 6 ------ SimG4Core/Application/src/RunManagerMTWorker.cc | 7 ++----- 5 files changed, 7 insertions(+), 21 deletions(-) diff --git a/SimG4Core/Application/interface/OscarMTMasterThread.h b/SimG4Core/Application/interface/OscarMTMasterThread.h index 61860652ff176..bb066beab35fd 100644 --- a/SimG4Core/Application/interface/OscarMTMasterThread.h +++ b/SimG4Core/Application/interface/OscarMTMasterThread.h @@ -11,9 +11,6 @@ #include "HepPDT/ParticleDataTable.hh" #include "SimGeneral/HepPDTRecord/interface/PDTRecord.h" -#include "MagneticField/Engine/interface/MagneticField.h" -#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - #include #include #include @@ -55,7 +52,6 @@ class OscarMTMasterThread { enum class ThreadState { NotExist = 0, BeginRun = 1, EndRun = 2, Destruct = 3 }; const bool m_pGeoFromDD4hep; - const bool m_pUseMagneticField; std::shared_ptr m_runManagerMaster; std::thread m_masterThread; @@ -67,7 +63,6 @@ class OscarMTMasterThread { mutable edm::ESGetToken m_DDD; mutable edm::ESGetToken m_DD4Hep; mutable edm::ESGetToken m_PDT; - mutable edm::ESGetToken m_MF; // status flags mutable std::mutex m_protectMutex; diff --git a/SimG4Core/Application/interface/RunManagerMT.h b/SimG4Core/Application/interface/RunManagerMT.h index c98d0c0d7fa20..a290b6c93ae49 100644 --- a/SimG4Core/Application/interface/RunManagerMT.h +++ b/SimG4Core/Application/interface/RunManagerMT.h @@ -35,7 +35,6 @@ class G4Event; class G4StateManager; class G4GeometryManager; class RunAction; -class MagneticField; class SimRunInterface; @@ -80,9 +79,6 @@ class RunManagerMT { // with TLS. inline PhysicsList* physicsListForWorker() const { return m_physicsList.get(); } - inline void setMagField(const MagneticField* ptr) { m_pMagField = ptr; } - inline const MagneticField* getMagField() const { return m_pMagField; } - private: void terminateRun(); @@ -96,7 +92,6 @@ class RunManagerMT { G4Run* m_currentRun; G4StateManager* m_stateManager; G4GeometryManager* m_geometryManager; - const MagneticField* m_pMagField = nullptr; std::unique_ptr m_runInterface; diff --git a/SimG4Core/Application/interface/RunManagerMTWorker.h b/SimG4Core/Application/interface/RunManagerMTWorker.h index d522c96673783..f42895f449894 100644 --- a/SimG4Core/Application/interface/RunManagerMTWorker.h +++ b/SimG4Core/Application/interface/RunManagerMTWorker.h @@ -7,6 +7,9 @@ #include "SimG4Core/Generators/interface/Generator.h" #include "SimDataFormats/Forward/interface/LHCTransportLinkContainer.h" +#include "MagneticField/Engine/interface/MagneticField.h" +#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" + #include #include #include @@ -89,6 +92,8 @@ class RunManagerMTWorker { edm::EDGetTokenT m_InToken; edm::EDGetTokenT m_LHCToken; edm::EDGetTokenT m_theLHCTlinkToken; + edm::ESGetToken m_MagField; + const MagneticField* m_pMagField = nullptr; bool m_nonBeam; bool m_pUseMagneticField; diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index 138b3b76abe86..e59a592260929 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -115,9 +115,6 @@ void OscarMTMasterThread::callConsumes(edm::ConsumesCollector&& iC) const { m_DDD = iC.esConsumes(); } m_PDT = iC.esConsumes(); - if (m_pUseMagneticField) { - m_MF = iC.esConsumes(); - } m_hasToken = true; } @@ -133,9 +130,6 @@ void OscarMTMasterThread::beginRun(const edm::EventSetup& iSetup) const { m_pDDD = &(*iSetup.getTransientHandle(m_DDD)); } m_pTable = &iSetup.getData(m_PDT); - if (m_pUseMagneticField) { - m_runManagerMaster->setMagField(&iSetup.getData(m_MF)); - } m_firstRun = false; } m_masterThreadState = ThreadState::BeginRun; diff --git a/SimG4Core/Application/src/RunManagerMTWorker.cc b/SimG4Core/Application/src/RunManagerMTWorker.cc index 81bf91d3e6965..7771f3f4a7e7e 100644 --- a/SimG4Core/Application/src/RunManagerMTWorker.cc +++ b/SimG4Core/Application/src/RunManagerMTWorker.cc @@ -19,7 +19,6 @@ #include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/thread_safety_macros.h" -#include "MagneticField/Engine/interface/MagneticField.h" #include "SimG4Core/Notification/interface/G4SimEvent.h" #include "SimG4Core/Notification/interface/SimActivityRegistry.h" @@ -169,11 +168,9 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::Co if (m_LHCTransport) { m_LHCToken = iC.consumes(edm::InputTag("LHCTransport")); } - /* if (m_pUseMagneticField) { m_MagField = iC.esConsumes(); } - */ edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID; unsigned int k = 0; for (std::unordered_map>::const_iterator itr = @@ -315,8 +312,8 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm: if (m_pUseMagneticField) { const GlobalPoint g(0.f, 0.f, 0.f); - //const MagneticField* pMF = &es.getData(m_MagField); - sim::FieldBuilder fieldBuilder(runManagerMaster->getMagField(), m_pField); + m_pMagField = &es.getData(m_MagField); + sim::FieldBuilder fieldBuilder(m_pMagField, m_pField); CMSFieldManager* fieldManager = new CMSFieldManager(); tM->SetFieldManager(fieldManager); From 3ee5f110f9ac3c028baa7d6f245b754027a13207 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 12 Jul 2021 20:27:36 +0200 Subject: [PATCH 16/16] fixed compilation --- SimG4Core/Application/src/OscarMTMasterThread.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/SimG4Core/Application/src/OscarMTMasterThread.cc b/SimG4Core/Application/src/OscarMTMasterThread.cc index e59a592260929..468e2196c0810 100644 --- a/SimG4Core/Application/src/OscarMTMasterThread.cc +++ b/SimG4Core/Application/src/OscarMTMasterThread.cc @@ -11,7 +11,6 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig) : m_pGeoFromDD4hep(iConfig.getParameter("g4GeometryDD4hepSource")), - m_pUseMagneticField(iConfig.getParameter("UseMagneticField")), m_masterThreadState(ThreadState::NotExist) { // Lock the mutex std::unique_lock lk(m_threadMutex);