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);
254 <<
"Produced " <<
p2->size() <<
" SimVertecies: position(cm), time(s), parentID, vertexID, processType";
257 for (
int i = 0;
i <
nn; ++
i) {
258 edm::LogVerbatim(
"Vertex") <<
" " <<
i <<
". " << (*p2)[
i] <<
" " << (*p2)[
i].processType();
262 <<
"Produced " <<
p1->size()
263 <<
" SimTracks: pdg, 4-momentum(GeV), vertexID, mcTruthID, flagBoundary, trackID at boundary";
266 for (
int i = 0;
i <
nn; ++
i) {
267 edm::LogVerbatim(
"Track") <<
" " <<
i <<
". " << (*p1)[
i] <<
" " << (*p1)[
i].crossedBoundary() <<
" " 268 << (*p1)[
i].getIDAtBoundary();
275 for (
auto const&
tracker : sTk) {
276 const std::vector<std::string>&
v =
tracker->getNames();
277 for (
auto const&
name :
v) {
280 if (0 <
m_verbose && product !=
nullptr && !product->empty())
281 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " << product->size() <<
" tracker hits <" <<
name <<
">";
285 for (
auto const&
calo : sCalo) {
286 const std::vector<std::string>&
v =
calo->getNames();
287 for (
auto const&
name :
v) {
290 if (0 <
m_verbose && product !=
nullptr && !product->empty())
291 edm::LogVerbatim(
"SimG4CoreApplication") <<
"Produced " << product->size() <<
" calo hits <" <<
name <<
">";
298 prod.get()->produce(
e, es);
302 <<
"Event is produced event " <<
e.id() <<
" streamID=" <<
e.streamID() <<
" threadID=" <<
id;
307 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(
edm::StreamID const& streamID) {
311 <<
"The OscarMTProducer module requires the RandomNumberGeneratorService\n" 312 "which is not present in the configuration file. You must add the service\n" 313 "in the configuration file if you want to run OscarMTProducer";
315 m_currentEngine = &(rng->
getEngine(streamID));
317 m_previousEngine = G4Random::getTheEngine();
318 G4Random::setTheEngine(m_currentEngine);
321 StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine) {
322 m_currentEngine = engine;
323 m_previousEngine = G4Random::getTheEngine();
324 G4Random::setTheEngine(m_currentEngine);
327 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
void load(edm::SimTrackContainer &) 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
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