Skip to content

Commit cd28f66

Browse files
committed
Migrate Generators to New Random Service Interface
Migrate generators to use the new interface of the random number generator service designed to work with the multithreaded Framework. The main interface change is to require a StreamID or LuminosityBlockIndex argument to the getEngine function. In most cases, this required moving code from the constructor to the event or beginLuminosityBlock method. Then a pointer to the engine was percolated or passed in some way to the point where it was used. In some cases, I added declarations of shared resources where it was obvious they were needed, but I did not thoroughly check for and declare all shared resources that will eventually be needed. Note that this was done in two parts. The first pull request was already merged into 7_0_X some time ago. It took care of Pythia 8 and ExternalDecays. The rest of the generators are taken care of in this pull request. Note that the content of this pull request is almost identical to pull requests 1982 and 2150. Pull request 1982 was signed by all L2's. The only differences are related to resolving conflicts that occurred when unrelated development was merged in. It was also rebased twice.
1 parent 88d89de commit cd28f66

File tree

100 files changed

+716
-538
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+716
-538
lines changed

FWCore/Concurrency/interface/SharedResourceNames.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace edm {
2828
static const std::string kPhotos;
2929
static const std::string kTauola;
3030
static const std::string kEvtGen;
31+
static const std::string kHerwig6;
3132
};
3233

3334
// Each time the following function is called, it returns a different

FWCore/Concurrency/src/SharedResourceNames.cc

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const std::string edm::SharedResourceNames::kPythia8 = "Pythia8";
1010
const std::string edm::SharedResourceNames::kPhotos = "Photos";
1111
const std::string edm::SharedResourceNames::kTauola = "Tauola";
1212
const std::string edm::SharedResourceNames::kEvtGen = "EvtGen";
13+
const std::string edm::SharedResourceNames::kHerwig6 = "Herwig6";
1314

1415
static std::atomic<unsigned int> counter;
1516

FWCore/ServiceRegistry/interface/RandomEngineSentry.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,45 @@ namespace edm {
2626
template <class T> class RandomEngineSentry {
2727
public:
2828

29-
explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine): t_(t) {
29+
explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine): t_(t), engine_(engine) {
3030
if(t) {
3131
t->setRandomEngine(engine);
3232
}
3333
}
3434

35-
explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t) {
35+
explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t), engine_(nullptr) {
3636
if(t) {
3737
Service<RandomNumberGenerator> rng;
3838
if (!rng.isAvailable()) {
3939
throw cms::Exception("Configuration")
4040
<< "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
4141
"You must configure the service if you want an engine.\n";
4242
}
43-
CLHEP::HepRandomEngine& engine = rng->getEngine(streamID);
44-
t->setRandomEngine(&engine);
43+
engine_ = &rng->getEngine(streamID);
44+
t->setRandomEngine(engine_);
4545
}
4646
}
4747

48-
explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi): t_(t) {
48+
explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi): t_(t), engine_(nullptr) {
4949
if(t) {
5050
Service<RandomNumberGenerator> rng;
5151
if (!rng.isAvailable()) {
5252
throw cms::Exception("Configuration")
5353
<< "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
5454
"You must configure the service if you want an engine.\n";
5555
}
56-
CLHEP::HepRandomEngine& engine = rng->getEngine(lumi);
57-
t->setRandomEngine(&engine);
56+
engine_ = &rng->getEngine(lumi);
57+
t->setRandomEngine(engine_);
5858
}
5959
}
6060

6161
~RandomEngineSentry() { if(t_) t_->setRandomEngine(nullptr); }
6262

63+
CLHEP::HepRandomEngine* randomEngine() const { return engine_; }
64+
6365
private:
6466
T* t_;
67+
CLHEP::HepRandomEngine* engine_;
6568
};
6669
}
6770
#endif

GeneratorInterface/AMPTInterface/interface/AMPTHadronizer.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "FWCore/ParameterSet/interface/ParameterSet.h"
55
#include "GeneratorInterface/Core/interface/BaseHadronizer.h"
6-
#include "CLHEP/Random/RandomEngine.h"
76

87
#include <map>
98
#include <string>
@@ -16,7 +15,9 @@ namespace HepMC {
1615
class GenVertex;
1716
}
1817

19-
extern CLHEP::HepRandomEngine* _amptRandomEngine;
18+
namespace CLHEP {
19+
class HepRandomEngine;
20+
}
2021

2122
namespace gen
2223
{
@@ -50,6 +51,8 @@ namespace gen
5051

5152
private:
5253

54+
virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override;
55+
5356
void add_heavy_ion_rec(HepMC::GenEvent *evt);
5457
HepMC::GenParticle* build_ampt( int index, int barcode );
5558
HepMC::GenVertex* build_ampt_vertex(int i, int id);

GeneratorInterface/AMPTInterface/src/AMPTHadronizer.cc

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "FWCore/Framework/interface/Run.h"
88
#include "FWCore/MessageLogger/interface/MessageLogger.h"
99
#include "FWCore/ParameterSet/interface/ParameterSet.h"
10-
#include "FWCore/ServiceRegistry/interface/Service.h"
11-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
12-
#include "FWCore/Utilities/interface/EDMException.h"
13-
#include "GeneratorInterface/Core/interface/RNDMEngineAccess.h"
1410

1511
#include "GeneratorInterface/AMPTInterface/interface/AMPTHadronizer.h"
1612
#include "GeneratorInterface/AMPTInterface/interface/AMPTWrapper.h"
@@ -30,14 +26,14 @@ using namespace edm;
3026
using namespace std;
3127
using namespace gen;
3228

33-
CLHEP::HepRandomEngine* _amptRandomEngine;
29+
static CLHEP::HepRandomEngine* amptRandomEngine;
3430

3531
extern "C"
3632
{
3733
float gen::ranart_(int *idummy)
3834
{
3935
if(0) idummy = idummy;
40-
float rannum = _amptRandomEngine->flat();
36+
float rannum = amptRandomEngine->flat();
4137
return rannum;
4238
}
4339
}
@@ -47,7 +43,7 @@ extern "C"
4743
float gen::ran1_(int *idummy)
4844
{
4945
if(0) idummy = idummy;
50-
return _amptRandomEngine->flat();
46+
return amptRandomEngine->flat();
5147
}
5248
}
5349

@@ -100,17 +96,19 @@ AMPTHadronizer::AMPTHadronizer(const ParameterSet &pset) :
10096
cosphi0_(1.),
10197
rotate_(pset.getParameter<bool>("rotateEventPlane"))
10298
{
103-
// Default constructor
104-
edm::Service<RandomNumberGenerator> rng;
105-
_amptRandomEngine = &(rng->getEngine());
10699
}
107100

108-
109101
//_____________________________________________________________________
110102
AMPTHadronizer::~AMPTHadronizer()
111103
{
112104
}
113105

106+
//_____________________________________________________________________
107+
void AMPTHadronizer::doSetRandomEngine(CLHEP::HepRandomEngine* v)
108+
{
109+
amptRandomEngine = v;
110+
}
111+
114112
//_____________________________________________________________________
115113
void AMPTHadronizer::add_heavy_ion_rec(HepMC::GenEvent *evt)
116114
{

GeneratorInterface/BeamHaloGenerator/interface/BeamHaloProducer.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@
1010

1111
#include "FWCore/Framework/interface/one/EDProducer.h"
1212
#include "FWCore/Framework/interface/Event.h"
13+
#include "FWCore/Framework/interface/LuminosityBlock.h"
1314
#include "FWCore/Framework/interface/Run.h"
1415
#include "FWCore/ParameterSet/interface/ParameterSet.h"
1516

17+
namespace CLHEP {
18+
class HepRandomEngine;
19+
}
20+
1621
namespace edm
1722
{
18-
class BeamHaloProducer : public one::EDProducer<EndRunProducer> {
23+
class BeamHaloProducer : public one::EDProducer<EndRunProducer, one::WatchLuminosityBlocks> {
1924
public:
2025

2126
/// Constructor
2227
BeamHaloProducer(const ParameterSet &);
2328
/// Destructor
2429
virtual ~BeamHaloProducer();
2530

31+
void setRandomEngine(CLHEP::HepRandomEngine* v);
32+
2633
private:
2734
bool call_ki_bhg_init(long& seed);
2835
bool call_bh_set_parameters(int* ival, float* fval,const std::string cval_string);
@@ -31,8 +38,10 @@ namespace edm
3138

3239
private:
3340

34-
virtual void produce(Event & e, const EventSetup & es);
35-
virtual void endRunProduce(Run & r, const EventSetup & es);
41+
virtual void produce(Event & e, const EventSetup & es) override;
42+
virtual void endRunProduce(Run & r, const EventSetup & es) override;
43+
virtual void beginLuminosityBlock(LuminosityBlock const&, EventSetup const&) override;
44+
virtual void endLuminosityBlock(LuminosityBlock const&, EventSetup const&) override { }
3645

3746
void clear();
3847

@@ -46,6 +55,8 @@ namespace edm
4655
float EG_MIN_;
4756
float EG_MAX_;
4857
std::string G3FNAME_;
58+
59+
bool isInitialized_;
4960
};
5061

5162
}

GeneratorInterface/BeamHaloGenerator/src/BeamHaloProducer.cc

+22-14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#include "FWCore/Framework/interface/Event.h"
55
#include "FWCore/Framework/interface/Run.h"
6-
#include "FWCore/ServiceRegistry/interface/Service.h"
6+
#include "FWCore/ServiceRegistry/interface/RandomEngineSentry.h"
77
#include "FWCore/Utilities/interface/Exception.h"
8-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
98

109
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
1110
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
@@ -58,7 +57,8 @@ BeamHaloProducer::~BeamHaloProducer() {
5857

5958

6059
BeamHaloProducer::BeamHaloProducer( const ParameterSet & pset) :
61-
evt(0)
60+
evt(0),
61+
isInitialized_(false)
6262
{
6363

6464
int iparam[8];
@@ -83,17 +83,6 @@ BeamHaloProducer::BeamHaloProducer( const ParameterSet & pset) :
8383
cparam = pset.getUntrackedParameter<std::string>("G3FNAME","input.txt");
8484
call_bh_set_parameters(iparam,fparam,cparam);
8585

86-
87-
// -- Seed for randomnumbers
88-
Service<RandomNumberGenerator> rng;
89-
_BeamHalo_randomEngine = &(rng->getEngine());
90-
long seed = (long)(rng->mySeed());
91-
92-
93-
// -- initialisation
94-
call_ki_bhg_init(seed);
95-
96-
9786
produces<HepMCProduct>();
9887
produces<GenEventInfoProduct>();
9988
produces<GenRunInfoProduct, InRun>();
@@ -106,7 +95,26 @@ void BeamHaloProducer::clear()
10695
{
10796
}
10897

98+
void BeamHaloProducer::setRandomEngine(CLHEP::HepRandomEngine* v) {
99+
_BeamHalo_randomEngine = v;
100+
}
101+
102+
void BeamHaloProducer::beginLuminosityBlock(LuminosityBlock const& lumi, EventSetup const&)
103+
{
104+
if(!isInitialized_) {
105+
isInitialized_ = true;
106+
RandomEngineSentry<BeamHaloProducer> randomEngineSentry(this, lumi.index());
107+
108+
// -- initialisation
109+
long seed = 1; // This seed is not actually used
110+
call_ki_bhg_init(seed);
111+
}
112+
}
113+
109114
void BeamHaloProducer::produce(Event & e, const EventSetup & es) {
115+
116+
RandomEngineSentry<BeamHaloProducer> randomEngineSentry(this, e.streamID());
117+
110118
// cout << "in produce " << endl;
111119

112120
// auto_ptr<HepMCProduct> bare_product(new HepMCProduct());

GeneratorInterface/CascadeInterface/BuildFile.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<use name="boost"/>
2+
<use name="FWCore/Concurrency"/>
23
<use name="FWCore/Framework"/>
34
<use name="SimDataFormats/GeneratorProducts"/>
45
<use name="GeneratorInterface/Core"/>

GeneratorInterface/CascadeInterface/plugins/Cascade2Hadronizer.cc

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "GeneratorInterface/CascadeInterface/plugins/Cascade2Hadronizer.h"
22

3-
#include "GeneratorInterface/Core/interface/RNDMEngineAccess.h"
4-
5-
63
#include "HepMC/GenEvent.h"
74
#include "HepMC/PdfInfo.h"
85
#include "HepMC/PythiaWrapper6_4.h"
@@ -12,17 +9,16 @@
129
#include "HepMC/IO_HEPEVT.h"
1310
#include "HepMC/IO_GenEvent.h"
1411

12+
#include "FWCore/Concurrency/interface/SharedResourceNames.h"
1513
#include "FWCore/MessageLogger/interface/MessageLogger.h"
1614
#include "GeneratorInterface/Core/interface/FortranCallback.h"
15+
#include "GeneratorInterface/Core/interface/FortranInstance.h"
1716

1817
HepMC::IO_HEPEVT hepevtio;
1918

2019
#include "HepPID/ParticleIDTranslations.hh"
2120

22-
#include "FWCore/ServiceRegistry/interface/Service.h"
23-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
24-
#include "CLHEP/Random/RandFlat.h"
25-
#include "FWCore/Utilities/interface/Exception.h"
21+
#include "FWCore/Utilities/interface/EDMException.h"
2622

2723
//-- Pythia6 routines and functionalities to pass around Pythia6 params
2824

@@ -31,20 +27,22 @@ HepMC::IO_HEPEVT hepevtio;
3127

3228
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
3329

30+
#include "CLHEP/Random/RandomEngine.h"
31+
3432
using namespace edm;
3533
using namespace std;
3634

3735
#define debug 0
3836

39-
CLHEP::RandFlat* fFlat_extern;
37+
static CLHEP::HepRandomEngine* cascade2RandomEngine;
4038

4139
extern "C" {
4240

4341
double dcasrn_(int *idummy) {
4442

4543
static int call = 0;
4644

47-
double rdm_nb = fFlat_extern->fire();
45+
double rdm_nb = cascade2RandomEngine->flat();
4846

4947
if(debug && ++call < 100) cout<<"dcasrn from c++, call: "<<call<<" random number: "<<rdm_nb<<endl;
5048

@@ -54,7 +52,7 @@ extern "C" {
5452

5553

5654
namespace gen {
57-
55+
5856
class Pythia6ServiceWithCallback : public Pythia6Service {
5957

6058
public:
@@ -82,16 +80,12 @@ namespace gen {
8280
double p[5][pyjets_maxn], v[5][pyjets_maxn];
8381
} pyjets_local;
8482

83+
const std::vector<std::string> Cascade2Hadronizer::theSharedResources = { edm::SharedResourceNames::kPythia6,
84+
gen::FortranInstance::kFortranInstance };
85+
8586
Cascade2Hadronizer::Cascade2Hadronizer(edm::ParameterSet const& pset)
8687
: BaseHadronizer(pset),
8788
fPy6Service(new Pythia6ServiceWithCallback(pset)), //-- this will store py6 parameters for further settings
88-
89-
//-- fRandomEngine(&getEngineReference()),
90-
91-
//-- defined in GeneratorInterface/Core/src/RNDMEngineAccess.cc
92-
//-- CLHEP::HepRandomEngine& gen::getEngineReference()
93-
//-- { edm::Service<edm::RandomNumberGenerator> rng;
94-
//-- return rng->getEngine(); }
9589

9690
fComEnergy(pset.getParameter<double>("comEnergy")),
9791
fextCrossSection(pset.getUntrackedParameter<double>("crossSection",-1.)),
@@ -107,12 +101,6 @@ namespace gen {
107101

108102
fParameters = pset.getParameter<ParameterSet>("Cascade2Parameters");
109103

110-
edm::Service<edm::RandomNumberGenerator> rng;
111-
if(debug) cout<<"seed: "<<rng->mySeed()<<endl;
112-
CLHEP::HepRandomEngine& engine = rng->getEngine();
113-
fFlat = new CLHEP::RandFlat(engine);
114-
fFlat_extern = fFlat;
115-
116104
fConvertToPDG = false;
117105
if(pset.exists("doPDGConvert"))
118106
fConvertToPDG = pset.getParameter<bool>("doPDGConvert");
@@ -143,6 +131,11 @@ namespace gen {
143131
if(fPy6Service != 0) delete fPy6Service;
144132
}
145133

134+
void Cascade2Hadronizer::doSetRandomEngine(CLHEP::HepRandomEngine* v) {
135+
cascade2RandomEngine = v;
136+
fPy6Service->setRandomEngine(v);
137+
}
138+
146139
void Cascade2Hadronizer::flushTmpStorage(){
147140

148141
pyjets_local.n = 0 ;

0 commit comments

Comments
 (0)