39 #include "Randomize.hh" 44 typedef std::vector<std::shared_ptr<SimProducer>>
Producers;
94 class StaticRandomEngineSetUnset {
97 explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine);
98 ~StaticRandomEngineSetUnset();
100 CLHEP::HepRandomEngine* currentEngine() {
return m_currentEngine; }
103 CLHEP::HepRandomEngine* m_currentEngine;
104 CLHEP::HepRandomEngine* m_previousEngine;
109 : m_handoff{
p.getUntrackedParameter<
int>(
"workerThreadStackSize", 10 * 1024 * 1024)}, m_psetID{
p.id()} {
110 m_verbose =
p.getParameter<
int>(
"EventVerbose");
112 StaticRandomEngineSetUnset random(
nullptr);
115 m_handoff.runAndWait([
this, &
p,
token]() {
117 StaticRandomEngineSetUnset random(
nullptr);
118 m_runManagerWorker = std::make_unique<RunManagerMTWorker>(
p, consumesCollector());
120 m_masterThread = (
nullptr != ms) ? ms : s_masterThread;
122 m_masterThread->callConsumes(consumesCollector());
125 produces<edm::SimTrackContainer>().setBranchAlias(
"SimTracks");
126 produces<edm::SimVertexContainer>().setBranchAlias(
"SimVertices");
128 auto trackHits =
p.getParameter<std::vector<std::string>>(
"TrackHits");
129 for (
auto const&
ss : trackHits) {
130 produces<edm::PSimHitContainer>(
ss);
133 auto caloHits =
p.getParameter<std::vector<std::string>>(
"CaloHits");
134 for (
auto const&
ss : caloHits) {
135 produces<edm::PCaloHitContainer>(
ss);
139 auto&
producers = m_runManagerWorker->producers();
141 ptr->registerProducts(producesCollector());
144 <<
"OscarMTProducer is constructed with hit collections:" << trackHits.size() <<
" tracking type; " 145 << caloHits.size() <<
" calo type; " <<
producers.size() <<
" watcher type.";
158 StaticRandomEngineSetUnset random(
nullptr);
159 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::initializeGlobalCache";
161 auto ret = std::make_unique<OscarMTMasterThread>(iConfig);
173 StaticRandomEngineSetUnset random(
nullptr);
174 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::globalBeginRun";
178 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::globalBeginRun done";
179 return std::shared_ptr<int>();
183 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::globalEndRun";
184 if (
nullptr != iContext->
global()) {
185 iContext->
global()->endRun();
190 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::globalEndJob";
199 <<
"At least two different OscarMTProducer instances have been" 200 "loaded into the job and they have different configurations.\n" 201 " All OscarMTProducers in a job must have exactly the same configuration.";
204 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::beginRun threadID=" <<
id;
211 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::beginRun done threadID=" <<
id;
215 StaticRandomEngineSetUnset random(
nullptr);
217 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::endRun threadID=" <<
id;
223 edm::LogVerbatim(
"SimG4CoreApplication") <<
"OscarMTProducer::endRun done threadID=" <<
id;
227 StaticRandomEngineSetUnset random(
e.streamID());
228 auto engine = random.currentEngine();
232 <<
"Produce event " <<
e.id() <<
" stream " <<
e.streamID() <<
" threadID=" <<
id;
243 StaticRandomEngineSetUnset random(engine);
253 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " <<
p2->size() <<
" SimVertex objects";
256 for (
int i = 0;
i <
nn; ++
i) {
260 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " <<
p1->size() <<
" SimTrack objects";
263 for (
int i = 0;
i <
nn; ++
i) {
264 edm::LogVerbatim(
"Track") <<
" " <<
i <<
". " << (*p1)[
i] <<
" " << (*p1)[
i].crossedBoundary() <<
" " 265 << (*p1)[
i].getIDAtBoundary();
272 for (
auto const&
tracker : sTk) {
273 const std::vector<std::string>&
v =
tracker->getNames();
274 for (
auto const&
name :
v) {
277 if (0 <
m_verbose && product !=
nullptr && !product->empty())
278 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " << product->size() <<
" tracker hits <" <<
name <<
">";
282 for (
auto const&
calo : sCalo) {
283 const std::vector<std::string>&
v =
calo->getNames();
284 for (
auto const&
name :
v) {
287 if (0 <
m_verbose && product !=
nullptr && !product->empty())
288 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " << product->size() <<
" calo hits <" <<
name <<
">";
295 prod.get()->produce(
e, es);
299 <<
"Event is produced event " <<
e.id() <<
" streamID=" <<
e.streamID() <<
" threadID=" <<
id;
304 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
edm::StreamID const& streamID) {
308 <<
"The OscarMTProducer module requires the RandomNumberGeneratorService\n" 309 "which is not present in the configuration file. You must add the service\n" 310 "in the configuration file if you want to run OscarMTProducer";
312 m_currentEngine = &(rng->
getEngine(streamID));
314 m_previousEngine = G4Random::getTheEngine();
315 G4Random::setTheEngine(m_currentEngine);
318 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
319 m_currentEngine = engine;
320 m_previousEngine = G4Random::getTheEngine();
321 G4Random::setTheEngine(m_currentEngine);
324 StaticRandomEngineSetUnset::~StaticRandomEngineSetUnset() { G4Random::setTheEngine(m_previousEngine); }
Log< level::Info, true > LogVerbatim
RunManagerMT * runManagerMasterPtr() const
std::vector< PCaloHit > PCaloHitContainer
static std::shared_ptr< int > globalBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const OscarMTMasterThread *masterThread)
std::unique_ptr< RunManagerMTWorker > m_runManagerWorker
ret
prodAgent to be discontinued
OscarMTProducer(edm::ParameterSet const &p, const OscarMTMasterThread *)
void beginRun(const edm::EventSetup &iSetup) const
~OscarMTProducer() override
std::vector< std::shared_ptr< SimProducer > > Producers
static edm::ParameterSetID s_psetID
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
static const OscarMTMasterThread * s_masterThread
RunManagerMT & runManagerMaster() const
ParameterSetID id() const
const OscarMTMasterThread * m_masterThread
const edm::ParameterSetID m_psetID
static ServiceRegistry & instance()
#define DEFINE_FWK_MODULE(type)
void produce(edm::Event &e, const edm::EventSetup &c) override
void load(edm::SimTrackContainer &c) const
static void globalEndRun(const edm::Run &iRun, const edm::EventSetup &iSetup, const RunContext *iContext)
static std::unique_ptr< OscarMTMasterThread > initializeGlobalCache(const edm::ParameterSet &iConfig)
void endRun(const edm::Run &r, const edm::EventSetup &c) override
std::vector< SimVertex > SimVertexContainer
std::vector< PSimHit > PSimHitContainer
omt::ThreadHandoff m_handoff
static void globalEndJob(OscarMTMasterThread *masterThread)
std::vector< SimTrack > SimTrackContainer
ServiceToken presentToken() const
void beginRun(const edm::Run &r, const edm::EventSetup &c) override