Skip to content

Commit 84c8c65

Browse files
committed
Migrate Code to New Random Service Interface
Migrate FastSimulation code 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. These objects are only available during the event or beginLuminosityBlock method.
1 parent 9c40f48 commit 84c8c65

File tree

6 files changed

+76
-102
lines changed

6 files changed

+76
-102
lines changed

FastSimulation/MaterialEffects/interface/NuclearInteractionSimulator.h

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class NuclearInteractionSimulator : public MaterialEffectsSimulator
9797
std::ofstream myOutputFile;
9898
unsigned myOutputBuffer;
9999

100+
bool currentValuesWereSet;
101+
100102
// DaqMonitorBEInterface * dbe;
101103
// MonitorElement* hAfter;
102104
// MonitorElement* hAfter2;

FastSimulation/MaterialEffects/src/NuclearInteractionSimulator.cc

+27-37
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ NuclearInteractionSimulator::NuclearInteractionSimulator(
4242
theRatios(ratios),
4343
theIDMap(idMap),
4444
theDistAlgo(distAlgo),
45-
theDistCut(distCut)
46-
45+
theDistCut(distCut),
46+
currentValuesWereSet(false)
4747
{
48-
49-
gROOT->cd();
50-
5148
std::string fullPath;
5249

5350
// Prepare the map of files
@@ -86,8 +83,8 @@ NuclearInteractionSimulator::NuclearInteractionSimulator(
8683
}
8784

8885
// Read the information from a previous run (to keep reproducibility)
89-
bool input = this->read(inputFile);
90-
if ( input )
86+
currentValuesWereSet = this->read(inputFile);
87+
if ( currentValuesWereSet )
9188
std::cout << "***WARNING*** You are reading nuclear-interaction information from the file "
9289
<< inputFile << " created in an earlier run."
9390
<< std::endl;
@@ -132,33 +129,12 @@ NuclearInteractionSimulator::NuclearInteractionSimulator(
132129
//
133130
theNumberOfEntries[iname][iene] = theTrees[iname][iene]->GetEntries();
134131

135-
// Add some randomness (if there was no input file)
136-
if ( !input ) {
137-
// RANDOM_NUMBER_ERROR
138-
// Random numbers should only be generated in the beginLuminosityBlock method
139-
// or event methods of a module
140-
RandomEngineAndDistribution random;
141-
theCurrentEntry[iname][iene] = (unsigned) (theNumberOfEntries[iname][iene] * random.flatShoot());
132+
if(currentValuesWereSet) {
133+
theTrees[iname][iene]->GetEntry(theCurrentEntry[iname][iene]);
134+
unsigned NInteractions = theNUEvents[iname][iene]->nInteractions();
135+
theNumberOfInteractions[iname][iene] = NInteractions;
142136
}
143-
theTrees[iname][iene]->GetEntry(theCurrentEntry[iname][iene]);
144-
unsigned NInteractions = theNUEvents[iname][iene]->nInteractions();
145-
theNumberOfInteractions[iname][iene] = NInteractions;
146-
// Add some randomness (if there was no input file)
147-
if ( !input ) {
148-
// RANDOM_NUMBER_ERROR
149-
// Random numbers should only be generated in the beginLuminosityBlock method
150-
// or event methods of a module
151-
RandomEngineAndDistribution random;
152-
theCurrentInteraction[iname][iene] = (unsigned) (theNumberOfInteractions[iname][iene] * random.flatShoot());
153-
}
154-
/*
155-
std::cout << "File " << theFileNames[iname][iene]
156-
<< " is opened with " << theNumberOfEntries[iname][iene]
157-
<< " entries and will be read from Entry/Interaction "
158-
<< theCurrentEntry[iname][iene] << "/" << theCurrentInteraction[iname][iene]
159-
<< std::endl;
160-
*/
161-
137+
162138
//
163139
// Compute the corresponding cm energies of the nuclear interactions
164140
XYZTLorentzVector Proton(0.,0.,0.,0.986);
@@ -178,7 +154,6 @@ NuclearInteractionSimulator::NuclearInteractionSimulator(
178154
ien4 = 0;
179155
while ( thePionEN[ien4] < 4.0 ) ++ien4;
180156

181-
// Return Loot in the same state as it was when entering.
182157
gROOT->cd();
183158

184159
// Information (Should be on LogInfo)
@@ -212,15 +187,26 @@ NuclearInteractionSimulator::~NuclearInteractionSimulator() {
212187
// Close the output file
213188
myOutputFile.close();
214189

215-
// And return Loot in the same state as it was when entering.
216-
gROOT->cd();
217-
218190
// dbe->save("test.root");
219191

220192
}
221193

222194
void NuclearInteractionSimulator::compute(ParticlePropagator& Particle, RandomEngineAndDistribution const* random)
223195
{
196+
if(!currentValuesWereSet) {
197+
currentValuesWereSet = true;
198+
for ( unsigned iname=0; iname<thePionNA.size(); ++iname ) {
199+
for ( unsigned iene=0; iene<thePionEN.size(); ++iene ) {
200+
theCurrentEntry[iname][iene] = (unsigned) (theNumberOfEntries[iname][iene] * random->flatShoot());
201+
202+
theTrees[iname][iene]->GetEntry(theCurrentEntry[iname][iene]);
203+
unsigned NInteractions = theNUEvents[iname][iene]->nInteractions();
204+
theNumberOfInteractions[iname][iene] = NInteractions;
205+
206+
theCurrentInteraction[iname][iene] = (unsigned) (theNumberOfInteractions[iname][iene] * random->flatShoot());
207+
}
208+
}
209+
}
224210

225211
// Read a Nuclear Interaction in a random manner
226212

@@ -511,6 +497,10 @@ void NuclearInteractionSimulator::compute(ParticlePropagator& Particle, RandomEn
511497
}
512498
*/
513499

500+
// ERROR The way this loops through the events breaks
501+
// replay. Which events are retrieved depends on
502+
// which previous events were processed.
503+
514504
// Increment for next time
515505
++aCurrentInteraction[ene];
516506

FastSimulation/PileUpProducer/plugins/PileUpProducer.cc

+37-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "FWCore/Framework/interface/Event.h"
2+
#include "FWCore/Framework/interface/LuminosityBlock.h"
23
#include "FWCore/Framework/interface/MakerMacros.h"
34
#include "FWCore/ServiceRegistry/interface/Service.h"
5+
#include "FWCore/Utilities/interface/Exception.h"
46
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
57

68
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
@@ -24,7 +26,7 @@
2426
#include <sys/stat.h>
2527
#include <cmath>
2628

27-
PileUpProducer::PileUpProducer(edm::ParameterSet const & p) : hprob(0)
29+
PileUpProducer::PileUpProducer(edm::ParameterSet const & p) : hprob(0), currentValuesWereSet(false)
2830
{
2931

3032
// This producer produces an object PileupMixingContent, needed by PileupSummaryInfo
@@ -138,14 +140,11 @@ PileUpProducer::~PileUpProducer() {
138140

139141
void PileUpProducer::beginRun(edm::Run const&, edm::EventSetup const&)
140142
{
141-
142-
gROOT->cd();
143-
144143
std::string fullPath;
145144

146145
// Read the information from a previous run (to keep reproducibility)
147-
bool input = this->read(inputFile);
148-
if ( input )
146+
currentValuesWereSet = this->read(inputFile);
147+
if ( currentValuesWereSet )
149148
std::cout << "***WARNING*** You are reading pile-up information from the file "
150149
<< inputFile << " created in an earlier run."
151150
<< std::endl;
@@ -181,37 +180,36 @@ void PileUpProducer::beginRun(edm::Run const&, edm::EventSetup const&)
181180
theBranches[file]->SetAddress(&thePUEvents[file]);
182181
//
183182
theNumberOfEntries[file] = theTrees[file]->GetEntries();
184-
// RANDOM_NUMBER_ERROR
185-
// Random numbers should only be generated in the beginLuminosityBlock method
186-
// or event methods of a module
187-
RandomEngineAndDistribution random;
188-
// Add some randomness (if there was no input file)
189-
if ( !input )
190-
theCurrentEntry[file]
191-
= (unsigned) (theNumberOfEntries[file] * random.flatShoot());
192-
193-
theTrees[file]->GetEntry(theCurrentEntry[file]);
194-
unsigned NMinBias = thePUEvents[file]->nMinBias();
195-
theNumberOfMinBiasEvts[file] = NMinBias;
196-
// Add some randomness (if there was no input file)
197-
if ( !input )
198-
theCurrentMinBiasEvt[file] =
199-
(unsigned) (theNumberOfMinBiasEvts[file] * random.flatShoot());
200-
201-
/*
202-
std::cout << "File " << theFileNames[file]
203-
<< " is opened with " << theNumberOfEntries[file]
204-
<< " entries and will be read from Entry/Event "
205-
<< theCurrentEntry[file] << "/" << theCurrentMinBiasEvt[file]
206-
<< std::endl;
207-
*/
183+
184+
if ( currentValuesWereSet ) {
185+
theTrees[file]->GetEntry(theCurrentEntry[file]);
186+
unsigned NMinBias = thePUEvents[file]->nMinBias();
187+
theNumberOfMinBiasEvts[file] = NMinBias;
188+
}
208189
}
209-
210-
// Return Loot in the same state as it was when entering.
211190
gROOT->cd();
212-
213191
}
214-
192+
193+
void PileUpProducer::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
194+
if ( !currentValuesWereSet ) {
195+
currentValuesWereSet = true;
196+
197+
RandomEngineAndDistribution random(lumi.index());
198+
199+
for ( unsigned file=0; file < theNumberOfFiles; ++file ) {
200+
theCurrentEntry[file] =
201+
(unsigned) (theNumberOfEntries[file] * random.flatShoot());
202+
203+
theTrees[file]->GetEntry(theCurrentEntry[file]);
204+
unsigned NMinBias = thePUEvents[file]->nMinBias();
205+
theNumberOfMinBiasEvts[file] = NMinBias;
206+
207+
theCurrentMinBiasEvt[file] =
208+
(unsigned) (theNumberOfMinBiasEvts[file] * random.flatShoot());
209+
}
210+
}
211+
}
212+
215213
void PileUpProducer::endRun(edm::Run const&, edm::EventSetup const&)
216214
{
217215
// Close all local files
@@ -226,10 +224,6 @@ void PileUpProducer::endRun(edm::Run const&, edm::EventSetup const&)
226224

227225
// Close the output file
228226
myOutputFile.close();
229-
230-
// And return Loot in the same state as it was when entering.
231-
gROOT->cd();
232-
233227
}
234228

235229
void PileUpProducer::produce(edm::Event & iEvent, const edm::EventSetup & es)
@@ -383,7 +377,11 @@ void PileUpProducer::produce(edm::Event & iEvent, const edm::EventSetup & es)
383377

384378
}
385379
// End of particle loop
386-
380+
381+
// ERROR The way this loops through the pileup events breaks
382+
// replay. Which events are retrieved for pileup depends on
383+
// which previous events were processed.
384+
387385
// Increment for next time
388386
++theCurrentMinBiasEvt[file];
389387

FastSimulation/PileUpProducer/plugins/PileUpProducer.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include <fstream>
1010
#include "TH1F.h"
1111

12-
class ParameterSet;
13-
class Event;
14-
class EventSetup;
12+
namespace edm {
13+
class Event;
14+
class EventSetup;
15+
class LuminosityBlock;
16+
class ParameterSet;
17+
class Run;
18+
}
1519

1620
class TFile;
1721
class TTree;
@@ -28,6 +32,7 @@ class PileUpProducer : public edm::EDProducer
2832
explicit PileUpProducer(edm::ParameterSet const & p);
2933
virtual ~PileUpProducer();
3034
virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
35+
virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
3136
virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
3237
virtual void produce(edm::Event & e, const edm::EventSetup & c) override;
3338

@@ -66,6 +71,8 @@ class PileUpProducer : public edm::EDProducer
6671
std::vector<double> dataProb;
6772
int varSize;
6873
int probSize;
74+
75+
bool currentValuesWereSet;
6976
};
7077

7178
#endif

FastSimulation/Utilities/interface/RandomEngineAndDistribution.h

-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ class RandomEngineAndDistribution {
2222
RandomEngineAndDistribution(edm::StreamID const&);
2323
RandomEngineAndDistribution(edm::LuminosityBlockIndex const&);
2424

25-
// This is strongly deprecated, it exists for backward compatibility
26-
// for cases where the above two functions cannot be used easily.
27-
// It is the intent fix all those cases and delete this function
28-
// as soon as possible.
29-
RandomEngineAndDistribution();
30-
3125
~RandomEngineAndDistribution();
3226

3327
CLHEP::HepRandomEngine& theEngine() const { return *engine_; }

FastSimulation/Utilities/src/RandomEngineAndDistribution.cc

-17
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,5 @@ RandomEngineAndDistribution::RandomEngineAndDistribution(edm::LuminosityBlockInd
4141
rootEngine_ = ( (edm::TRandomAdaptor*) engine_ )->getRootEngine();
4242
}
4343

44-
RandomEngineAndDistribution::RandomEngineAndDistribution() :
45-
engine_(nullptr),
46-
rootEngine_(nullptr) {
47-
edm::Service<edm::RandomNumberGenerator> rng;
48-
if ( ! rng.isAvailable() ) {
49-
throw cms::Exception("Configuration") <<
50-
"RandomNumberGenerator service is not available.\n"
51-
"You must add the service in the configuration file\n"
52-
"or remove the module that requires it.";
53-
}
54-
engine_ = &rng->getEngine();
55-
56-
// Get the TRandom3 egine, to benefit from Root functional random generation
57-
if ( engine_->name() == "TRandom3" )
58-
rootEngine_ = ( (edm::TRandomAdaptor*) engine_ )->getRootEngine();
59-
}
60-
6144
RandomEngineAndDistribution::~RandomEngineAndDistribution() {
6245
}

0 commit comments

Comments
 (0)