Skip to content

Commit

Permalink
OscarMTProducer clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
civanch committed Aug 9, 2021
1 parent 68591f5 commit 000ba5c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 59 deletions.
40 changes: 36 additions & 4 deletions SimG4Core/Application/plugins/OscarMTProducer.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include "OscarMTProducer.h"
#include <iostream>
#include <memory>

#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Run.h"

#include "FWCore/PluginManager/interface/PluginManager.h"
#include "FWCore/Framework/interface/MakerMacros.h"
Expand All @@ -10,12 +17,16 @@
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "SimG4Core/Notification/interface/SimG4Exception.h"
#include "SimG4Core/Application/interface/OscarMTMasterThread.h"
#include "SimG4Core/Application/interface/RunManagerMT.h"
#include "SimG4Core/Application/interface/RunManagerMTWorker.h"

#include "SimG4Core/Notification/interface/SimG4Exception.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"
Expand All @@ -25,8 +36,29 @@

#include "Randomize.hh"

#include <iostream>
#include <memory>
// for some reason void doesn't compile
class OscarMTProducer : public edm::stream::EDProducer<edm::GlobalCache<OscarMTMasterThread>, edm::RunCache<int> > {
public:
typedef std::vector<std::shared_ptr<SimProducer> > Producers;

explicit OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread*);
~OscarMTProducer() override;

static std::unique_ptr<OscarMTMasterThread> initializeGlobalCache(const edm::ParameterSet& iConfig);
static std::shared_ptr<int> globalBeginRun(const edm::Run& iRun,
const edm::EventSetup& iSetup,
const OscarMTMasterThread* masterThread);
static void globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext);
static void globalEndJob(OscarMTMasterThread* masterThread);

void beginRun(const edm::Run& r, const edm::EventSetup& c) override;
void endRun(const edm::Run& r, const edm::EventSetup& c) override;
void produce(edm::Event& e, const edm::EventSetup& c) override;

private:
std::unique_ptr<RunManagerMTWorker> m_runManagerWorker;
const OscarMTMasterThread* m_masterThread = nullptr;
};

namespace edm {
class StreamID;
Expand Down
42 changes: 0 additions & 42 deletions SimG4Core/Application/plugins/OscarMTProducer.h

This file was deleted.

17 changes: 4 additions & 13 deletions SimG4Core/Application/src/OscarMTMasterThread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig)
} else if (m_masterThreadState == ThreadState::Destruct) {
edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
if (isG4Alive)
throw edm::Exception(edm::errors::LogicError)
<< "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
throw edm::Exception(edm::errors::LogicError) << "OscarMTMasterThread: Geant4 is still alive, master thread "
"state must be set to EndRun before Destruct";
break;
} else {
throw edm::Exception(edm::errors::LogicError)
Expand All @@ -83,7 +83,7 @@ OscarMTMasterThread::OscarMTMasterThread(const edm::ParameterSet& iConfig)
m_runManagerMaster.reset();
G4PhysicalVolumeStore::Clean();

edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: physics and geometry are cleaned";
lk2.unlock();
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is finished";
});
Expand Down Expand Up @@ -161,22 +161,13 @@ void OscarMTMasterThread::stopThread() {
return;
}
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: stop main thread";

// Release our instance of the shared master run manager, so that
// the G4 master thread can do the cleanup. Then notify the master
// thread, and join it.
std::unique_lock<std::mutex> lk2(m_threadMutex);
m_runManagerMaster.reset();
LogDebug("OscarMTMasterThread") << "Main thread: reseted shared_ptr";

m_masterThreadState = ThreadState::Destruct;
m_masterCanProceed = true;
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: notify";
m_notifyMasterCv.notify_one();
lk2.unlock();

LogDebug("OscarMTMasterThread") << "Main thread: joining master thread";
m_masterThread.join();
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: is done";
m_stopped = true;
}

0 comments on commit 000ba5c

Please sign in to comment.