Skip to content

Commit

Permalink
Threaded Random Number Interface for OscarProducer
Browse files Browse the repository at this point in the history
Modify OscarProducer to use the new interface for
the random number service that requires a StreamID
argument to the getEngine function. Modify the module
to be a "One" type module with shared resources
declared for GEANT and the CLHEPRandomEngine.
It is also "watching" runs. Note that with this
change it will now seg fault if any random numbers
were generated in the constructor or beginRun.
SimProducer is fixed to deal with the changed module
type. We are presuming the static code checking would
reveal any other threading problems related to making
this a "One" type module. This is for GEANT 4.9.X.
We will need to revisit this when we move to 4.10.X,
which will be the multithreaded GEANT.

The only changes in Validation are the addition of
a missing forward declaration revealed by the other
changes.
  • Loading branch information
wddgit committed Nov 21, 2013
1 parent 3daea6f commit 33097ef
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 47 deletions.
28 changes: 28 additions & 0 deletions FWCore/Concurrency/interface/SharedResourceNames.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef FWCore_Concurrency_SharedResourceNames_h
#define FWCore_Concurrency_SharedResourceNames_h
//
// Package: Concurrency
// Class : ShareResourceNames
//
/**\class edm::SharedResourceNames
Description: Contains the names of external shared resources.
*/
//
// Original Author: W. David Dagenhart
// Created: 19 November 2013
//

#include <string>

namespace edm {
class SharedResourceNames {
public:
// GEANT 4.9.X needs to be declared a shared resource
// In the future, 4.10.X and later might not need to be
static const std::string kGEANT;
static const std::string kCLHEPRandomEngine;
};
}
#endif
4 changes: 4 additions & 0 deletions FWCore/Concurrency/src/SharedResourceNames.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "FWCore/Concurrency/interface/SharedResourceNames.h"

const std::string edm::SharedResourceNames::kGEANT = "GEANT";
const std::string edm::SharedResourceNames::kCLHEPRandomEngine = "CLHEPRandomEngine";
19 changes: 6 additions & 13 deletions SimG4Core/Application/interface/OscarProducer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#ifndef SimG4Core_OscarProducer_H
#define SimG4Core_OscarProducer_H

#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/one/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
// #include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventSetup.h"
Expand All @@ -13,28 +12,22 @@

#include "SimG4Core/Application/interface/CustomUIsession.h"

namespace CLHEP {
class HepRandomEngine;
}
#include <memory>

class OscarProducer : public edm::EDProducer
class OscarProducer : public edm::one::EDProducer<edm::one::SharedResources, edm::one::WatchRuns>
{
public:
typedef std::vector<boost::shared_ptr<SimProducer> > Producers;

explicit OscarProducer(edm::ParameterSet const & p);
virtual ~OscarProducer();
virtual void beginRun(const edm::Run & r,const edm::EventSetup& c) override;
virtual void beginJob();
virtual void endJob();
virtual void endRun(const edm::Run & r,const edm::EventSetup& c) override { }
virtual void produce(edm::Event & e, const edm::EventSetup& c) override;
protected:
RunManager* m_runManager;
std::unique_ptr<RunManager> m_runManager;
Producers m_producers;
CustomUIsession* m_UIsession;

private:
CLHEP::HepRandomEngine* m_engine;
std::unique_ptr<CustomUIsession> m_UIsession;
};

#endif
1 change: 1 addition & 0 deletions SimG4Core/Application/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<use name="FWCore/Concurrency"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="SimDataFormats/CaloHit"/>
Expand Down
53 changes: 23 additions & 30 deletions SimG4Core/Application/plugins/OscarProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/Random.h"

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

#include <iostream>

namespace edm {
class StreamID;
}

namespace {
//
// this machinery allows to set CLHEP static engine
Expand All @@ -37,7 +42,7 @@ namespace {
//
class StaticRandomEngineSetUnset {
public:
StaticRandomEngineSetUnset();
StaticRandomEngineSetUnset(edm::StreamID const&);
explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine * engine);
~StaticRandomEngineSetUnset();
CLHEP::HepRandomEngine* getEngine() const;
Expand All @@ -48,10 +53,13 @@ namespace {
}

OscarProducer::OscarProducer(edm::ParameterSet const & p)
{
StaticRandomEngineSetUnset random;
m_engine = random.getEngine();

{
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);

usesResource(edm::SharedResourceNames::kGEANT);
usesResource(edm::SharedResourceNames::kCLHEPRandomEngine);

produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
produces<edm::PSimHitContainer>("TrackerHitsPixelBarrelLowTof");
Expand Down Expand Up @@ -95,7 +103,7 @@ OscarProducer::OscarProducer(edm::ParameterSet const & p)
produces<edm::PCaloHitContainer>("WedgeHits");

//m_runManager = RunManager::init(p);
m_runManager = new RunManager(p);
m_runManager.reset(new RunManager(p));

//register any products
m_producers= m_runManager->producers();
Expand All @@ -107,35 +115,23 @@ OscarProducer::OscarProducer(edm::ParameterSet const & p)
}

//UIsession manager for message handling
m_UIsession = new CustomUIsession();
m_UIsession.reset(new CustomUIsession());

}

OscarProducer::~OscarProducer()
{
//this is causing a seg fault when an exception occurs while constructing
// an HcalSD. Need to check for memory problems.
if (m_runManager!=0) delete m_runManager;
if (m_UIsession!=0) delete m_UIsession;

}
{ }

void OscarProducer::beginRun(const edm::Run & r, const edm::EventSetup & es)
{
m_runManager->initG4(es);
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);
m_runManager->initG4(es);
}


void OscarProducer::beginJob()
{
StaticRandomEngineSetUnset random(m_engine);
}

void OscarProducer::endJob() { }

void OscarProducer::produce(edm::Event & e, const edm::EventSetup & es)
{
StaticRandomEngineSetUnset random(m_engine);
StaticRandomEngineSetUnset random(e.streamID());

std::vector<SensitiveTkDetector*>& sTk = m_runManager->sensTkDetectors();
std::vector<SensitiveCaloDetector*>& sCalo = m_runManager->sensCaloDetectors();
Expand Down Expand Up @@ -191,18 +187,16 @@ void OscarProducer::produce(edm::Event & e, const edm::EventSetup & es)
}


StaticRandomEngineSetUnset::StaticRandomEngineSetUnset() {

using namespace edm;
Service<RandomNumberGenerator> rng;
StaticRandomEngineSetUnset::StaticRandomEngineSetUnset(edm::StreamID const& streamID) {

edm::Service<edm::RandomNumberGenerator> rng;
if ( ! rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "The OscarProducer module requires the RandomNumberGeneratorService\n"
"which is not present in the configuration file. You must add the service\n"
"in the configuration file if you want to run OscarProducer";
}
m_currentEngine = &(rng->getEngine());
m_currentEngine = &(rng->getEngine(streamID));

m_previousEngine = CLHEP::HepRandom::getTheEngine();
CLHEP::HepRandom::setTheEngine(m_currentEngine);
Expand All @@ -224,4 +218,3 @@ CLHEP::HepRandomEngine*
StaticRandomEngineSetUnset::getEngine() const { return m_currentEngine; }

DEFINE_FWK_MODULE(OscarProducer);

4 changes: 4 additions & 0 deletions SimG4Core/HelpfulWatchers/src/BeginOfTrackCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "SimG4Core/Notification/interface/BeginOfTrack.h"

// forward declarations
namespace edm {
class ParameterSet;
}

namespace simwatcher {
class BeginOfTrackCounter : public SimProducer,
public Observer<const BeginOfTrack*>
Expand Down
14 changes: 10 additions & 4 deletions SimG4Core/Watcher/interface/SimProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
//

// system include files
#include <algorithm>
#include <string>
#include <vector>
#include "boost/bind.hpp"
#include "boost/shared_ptr.hpp"

// user include files
#include "SimG4Core/Watcher/interface/SimWatcher.h"
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/ProducerBase.h"

namespace edm {
class Event;
class EventSetup;
}

// forward declarations
namespace simproducer {
Expand All @@ -40,7 +46,7 @@ namespace simproducer {
const std::string& instanceName() const {
return m_instanceName; }

virtual void registerProduct(edm::EDProducer*) const = 0;
virtual void registerProduct(edm::ProducerBase*) const = 0;
private:
std::string m_instanceName;
};
Expand All @@ -51,7 +57,7 @@ namespace simproducer {
ProductInfo(const std::string& iInstanceName) :
ProductInfoBase(iInstanceName) {}

void registerProduct(edm::EDProducer* iProd) const {
void registerProduct(edm::ProducerBase* iProd) const {
(*iProd). template produces<T>(this->instanceName());
}
};
Expand All @@ -71,7 +77,7 @@ class SimProducer : public SimWatcher
// ---------- member functions ---------------------------
virtual void produce(edm::Event&, const edm::EventSetup&) = 0;

void registerProducts(edm::EDProducer& iProd) {
void registerProducts(edm::ProducerBase& iProd) {
std::for_each(m_info.begin(), m_info.end(),
boost::bind(&simproducer::ProductInfoBase::registerProduct,_1, &iProd));
}
Expand Down
4 changes: 4 additions & 0 deletions Validation/EcalHits/interface/EcalSimHitsValidProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class G4Step;
class EndOfEvent;
class PEcalValidInfo;

namespace edm {
class ParameterSet;
}

class EcalSimHitsValidProducer : public SimProducer,
public Observer<const BeginOfEvent*>,
public Observer<const G4Step*>,
Expand Down
4 changes: 4 additions & 0 deletions Validation/Geometry/interface/MaterialBudgetAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class EndOfEvent;
class G4StepPoint;
class G4VTouchable;

namespace edm {
class ParameterSet;
}

class MaterialBudgetAction : public SimProducer,
public Observer<const BeginOfRun*>,
public Observer<const BeginOfTrack*>,
Expand Down
4 changes: 4 additions & 0 deletions Validation/HcalHits/interface/SimG4HcalValidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class BeginOfRun;
class BeginOfEvent;
class EndOfEvent;

namespace edm {
class ParameterSet;
}

class SimG4HcalValidation : public SimProducer,
public Observer<const BeginOfJob *>,
public Observer<const BeginOfRun *>,
Expand Down

0 comments on commit 33097ef

Please sign in to comment.