Skip to content

Commit ddbcd74

Browse files
committed
Migrate More to New Random Service Interface
Migrate SimWatchers and Hector related simulation code to use the new interface of the random number generator service designed to work with the multithreaded Framework. The SimWatchers are different than other simulation code because the call stack goes through GEANT functions and engines cannot be percolated. So SimWatchers use the global CLHEP engine (which is the same thing the current version of GEANT does). This CLHEP global will be a thread_local as soon as planned changes are made to CLHEP. The Hector code is different because it has TRandom3 hard coded in its interface, although it turns out that does not really affect the migration.
1 parent 8b13632 commit ddbcd74

File tree

12 files changed

+99
-102
lines changed

12 files changed

+99
-102
lines changed

SimG4CMS/Calo/BuildFile.xml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<use name="FWCore/ServiceRegistry"/>
1818
<use name="CommonTools/UtilAlgos"/>
1919
<use name="boost"/>
20+
<use name="clhep"/>
2021
<use name="geant4core"/>
2122
<use name="hepmc"/>
2223
<use name="root"/>

SimG4CMS/Calo/interface/HcalQie.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111

1212
#include <vector>
1313

14+
namespace CLHEP {
15+
class HepRandomEngine;
16+
}
17+
1418
class HcalQie {
1519

1620
public:
1721

1822
HcalQie(edm::ParameterSet const & p);
1923
virtual ~HcalQie();
2024

21-
std::vector<int> getCode(int, const std::vector<CaloHit>&);
25+
std::vector<int> getCode(int, const std::vector<CaloHit>&, CLHEP::HepRandomEngine*);
2226
double getEnergy(const std::vector<int>&);
2327

2428
private:

SimG4CMS/Calo/interface/HcalTestAnalysis.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class BeginOfRun;
2727
class BeginOfEvent;
2828
class EndOfEvent;
2929

30+
namespace CLHEP {
31+
class HepRandomEngine;
32+
}
33+
3034
class HcalTestAnalysis : public SimWatcher,
3135
public Observer<const BeginOfJob *>,
3236
public Observer<const BeginOfRun *>,
@@ -50,7 +54,7 @@ class HcalTestAnalysis : public SimWatcher,
5054
std::vector<int> layerGrouping(int);
5155
std::vector<int> towersToAdd(int centre, int nadd);
5256
void fill(const EndOfEvent * ev);
53-
void qieAnalysis();
57+
void qieAnalysis(CLHEP::HepRandomEngine*);
5458
void layerAnalysis();
5559
double timeOfFlight(int det, int layer, double eta);
5660

SimG4CMS/Calo/src/HcalQie.cc

+5-18
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55

66
#include "SimG4CMS/Calo/interface/HcalQie.h"
77

8-
#include "FWCore/ServiceRegistry/interface/Service.h"
9-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
108
#include "CLHEP/Random/RandGaussQ.h"
119
#include "CLHEP/Random/RandPoissonQ.h"
12-
#include "FWCore/Utilities/interface/Exception.h"
1310
#include "CLHEP/Units/GlobalPhysicalConstants.h"
1411

1512
#include <iostream>
@@ -296,27 +293,16 @@ double HcalQie::getShape(double time) {
296293
}
297294

298295

299-
std::vector<int> HcalQie::getCode(int nht, const std::vector<CaloHit>& hitbuf) {
296+
std::vector<int> HcalQie::getCode(int nht, const std::vector<CaloHit>& hitbuf,
297+
CLHEP::HepRandomEngine* engine) {
300298

301299
const double bunchSpace=25.;
302300
int nmax = (bmax_ > numOfBuckets ? bmax_ : numOfBuckets);
303301
std::vector<double> work(nmax);
304302

305-
edm::Service<edm::RandomNumberGenerator> rng;
306-
if ( ! rng.isAvailable()) {
307-
throw cms::Exception("Configuration")
308-
<< "HcalQIE requires the RandomNumberGeneratorService\n"
309-
<< "which is not present in the configuration file. "
310-
<< "You must add the service\n in the configuration file or "
311-
<< "remove the modules that require it.";
312-
}
313-
CLHEP::RandGaussQ randGauss(rng->getEngine(), baseline,sigma);
314-
CLHEP::RandPoissonQ randPoisson(rng->getEngine());
315-
316-
317303
// Noise in the channel
318304
for (int i=0; i<numOfBuckets; i++)
319-
work[i] = randGauss.fire();
305+
work[i] = CLHEP::RandGaussQ::shoot(engine, baseline, sigma);
320306

321307
#ifdef DebugLog
322308
LogDebug("HcalSim") << "HcalQie::getCode: Noise with baseline " << baseline
@@ -348,7 +334,8 @@ std::vector<int> HcalQie::getCode(int nht, const std::vector<CaloHit>& hitbuf) {
348334
}
349335

350336
double avpe = ehit/eDepPerPE;
351-
double photo = randPoisson.fire(avpe);
337+
CLHEP::RandPoissonQ randPoissonQ(*engine, avpe);
338+
double photo = randPoissonQ.fire();
352339
etot += ehit;
353340
photons+= photo;
354341
#ifdef DebugLog

SimG4CMS/Calo/src/HcalTestAnalysis.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "G4HCofThisEvent.hh"
2424
#include "CLHEP/Units/GlobalSystemOfUnits.h"
2525
#include "CLHEP/Units/GlobalPhysicalConstants.h"
26+
#include "CLHEP/Random/Random.h"
2627

2728
#include <cmath>
2829
#include <iostream>
@@ -320,7 +321,8 @@ void HcalTestAnalysis::update(const EndOfEvent * evt) {
320321
LogDebug("HcalSim") << "HcalTestAnalysis:: --- after Fill";
321322

322323
// Qie analysis
323-
qieAnalysis();
324+
CLHEP::HepRandomEngine* engine = CLHEP::HepRandom::getTheEngine();
325+
qieAnalysis(engine);
324326
LogDebug("HcalSim") << "HcalTestAnalysis:: --- after QieAnalysis";
325327

326328
// Layers tuples filling
@@ -471,7 +473,7 @@ void HcalTestAnalysis::fill(const EndOfEvent * evt) {
471473
}
472474

473475
//-----------------------------------------------------------------------------
474-
void HcalTestAnalysis::qieAnalysis() {
476+
void HcalTestAnalysis::qieAnalysis(CLHEP::HepRandomEngine* engine) {
475477

476478
//Fill tuple with hit information
477479
int hittot = caloHitCache.size();
@@ -546,7 +548,7 @@ void HcalTestAnalysis::qieAnalysis() {
546548
}
547549
}
548550

549-
std::vector<int> cd = myqie->getCode(nhit,hits);
551+
std::vector<int> cd = myqie->getCode(nhit, hits, engine);
550552
double eqie = myqie->getEnergy(cd);
551553

552554
LogDebug("HcalSim") << "HcalTestAnalysis::Qie: Energy in layer "

SimG4CMS/HcalTestBeam/interface/HcalTB04Analysis.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class EndOfEvent;
4444

4545
class PHcalTB04Info;
4646

47+
namespace CLHEP {
48+
class HepRandomEngine;
49+
}
50+
4751
class HcalTB04Analysis : public SimProducer,
4852
public Observer<const BeginOfRun *>,
4953
public Observer<const BeginOfEvent *>,
@@ -72,8 +76,8 @@ class HcalTB04Analysis : public SimProducer,
7276

7377
//User methods
7478
void fillBuffer(const EndOfEvent * evt);
75-
void qieAnalysis();
76-
void xtalAnalysis();
79+
void qieAnalysis(CLHEP::HepRandomEngine*);
80+
void xtalAnalysis(CLHEP::HepRandomEngine*);
7781
void finalAnalysis();
7882
void fillEvent(PHcalTB04Info&);
7983

SimG4CMS/HcalTestBeam/plugins/HcalTB02Analysis.cc

+7-12
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@
2727

2828
#include "FWCore/Framework/interface/Event.h"
2929
#include "FWCore/MessageLogger/interface/MessageLogger.h"
30-
#include "FWCore/ServiceRegistry/interface/Service.h"
3130
#include "FWCore/Framework/interface/MakerMacros.h"
3231
#include "FWCore/PluginManager/interface/ModuleDef.h"
33-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
3432
#include "CLHEP/Random/RandGaussQ.h"
35-
#include "FWCore/Utilities/interface/Exception.h"
3633

3734
#include "G4SDManager.hh"
3835
#include "G4VProcess.hh"
3936
#include "G4HCofThisEvent.hh"
4037
#include "CLHEP/Units/GlobalSystemOfUnits.h"
4138
#include "CLHEP/Units/GlobalPhysicalConstants.h"
39+
#include "CLHEP/Random/Random.h"
40+
41+
namespace CLHEP {
42+
class HepRandomEngine;
43+
}
4244

4345
//
4446
// constructors and destructor
@@ -90,15 +92,8 @@ void HcalTB02Analysis::update(const BeginOfEvent * evt) {
9092

9193
void HcalTB02Analysis::update(const EndOfEvent * evt) {
9294

93-
edm::Service<edm::RandomNumberGenerator> rng;
94-
if ( ! rng.isAvailable()) {
95-
throw cms::Exception("Configuration")
96-
<< "HcalTB02Analysis requires the RandomNumberGeneratorService\n"
97-
<< "which is not present in the configuration file. "
98-
<< "You must add the service\n in the configuration file or "
99-
<< "remove the modules that require it.";
100-
}
101-
CLHEP::RandGaussQ randGauss(rng->getEngine());
95+
CLHEP::HepRandomEngine* engine = CLHEP::HepRandom::getTheEngine();
96+
CLHEP::RandGaussQ randGauss(*engine);
10297

10398
// Look for the Hit Collection
10499
LogDebug("HcalTBSim") << "HcalTB02Analysis::Fill event "

SimG4CMS/HcalTestBeam/plugins/HcalTB04Analysis.cc

+9-18
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@
4141
#include "FWCore/PluginManager/interface/ModuleDef.h"
4242
#include "FWCore/MessageLogger/interface/MessageLogger.h"
4343

44-
#include "FWCore/ServiceRegistry/interface/Service.h"
45-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
4644
#include "CLHEP/Random/RandGaussQ.h"
47-
#include "FWCore/Utilities/interface/Exception.h"
4845

4946
#include "G4SDManager.hh"
5047
#include "G4VProcess.hh"
5148
#include "G4HCofThisEvent.hh"
5249
#include "CLHEP/Units/GlobalSystemOfUnits.h"
5350
#include "CLHEP/Units/GlobalPhysicalConstants.h"
51+
#include "CLHEP/Random/Random.h"
5452

5553
//
5654
// constructors and destructor
@@ -373,13 +371,14 @@ void HcalTB04Analysis::update(const EndOfEvent * evt) {
373371
//QIE analysis
374372
LogDebug("HcalTBSim") << "HcalTB04Analysis::Do QIE analysis with "
375373
<< hcalHitCache.size() << " hits";
376-
qieAnalysis();
374+
CLHEP::HepRandomEngine* engine = CLHEP::HepRandom::getTheEngine();
375+
qieAnalysis(engine);
377376

378377
//Energy in Crystal Matrix
379378
if (!hcalOnly) {
380379
LogDebug("HcalTBSim") << "HcalTB04Analysis::Do Xtal analysis with "
381380
<< ecalHitCache.size() << " hits";
382-
xtalAnalysis();
381+
xtalAnalysis(engine);
383382
}
384383

385384
//Final Analysis
@@ -654,7 +653,7 @@ void HcalTB04Analysis::fillBuffer(const EndOfEvent * evt) {
654653

655654
}
656655

657-
void HcalTB04Analysis::qieAnalysis() {
656+
void HcalTB04Analysis::qieAnalysis(CLHEP::HepRandomEngine* engine) {
658657

659658
int hittot = hcalHitCache.size();
660659
if (hittot<=0) hittot = 1;
@@ -682,7 +681,7 @@ void HcalTB04Analysis::qieAnalysis() {
682681
}
683682
k1 += nhit;
684683
nhit++;
685-
std::vector<int> cd = myQie->getCode(nhit,hits);
684+
std::vector<int> cd = myQie->getCode(nhit, hits, engine);
686685
double eq = myQie->getEnergy(cd);
687686
LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex << id
688687
<< std::dec << " registers " << esim << " energy "
@@ -700,7 +699,7 @@ void HcalTB04Analysis::qieAnalysis() {
700699
// Towers with no hit
701700
for (int k2 = 0; k2 < nTower; k2++) {
702701
if (todo[k2] == 0) {
703-
std::vector<int> cd = myQie->getCode(0,hits);
702+
std::vector<int> cd = myQie->getCode(0, hits, engine);
704703
double eq = myQie->getEnergy(cd);
705704
esimh[k2] = 0;
706705
eqie[k2] = eq;
@@ -714,17 +713,9 @@ void HcalTB04Analysis::qieAnalysis() {
714713
}
715714
}
716715

717-
void HcalTB04Analysis::xtalAnalysis() {
716+
void HcalTB04Analysis::xtalAnalysis(CLHEP::HepRandomEngine* engine) {
718717

719-
edm::Service<edm::RandomNumberGenerator> rng;
720-
if ( ! rng.isAvailable()) {
721-
throw cms::Exception("Configuration")
722-
<< "HcalTB04Analysis requires the RandomNumberGeneratorService\n"
723-
<< "which is not present in the configuration file. "
724-
<< "You must add the service\n in the configuration file or "
725-
<< "remove the modules that require it.";
726-
}
727-
CLHEP::RandGaussQ randGauss(rng->getEngine());
718+
CLHEP::RandGaussQ randGauss(*engine);
728719

729720
// Crystal Data
730721
std::vector<int> iok(nCrystal,0);

SimG4Core/Application/plugins/OscarProducer.cc

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ namespace {
4040
// static engine, thus we want to ensure that the one
4141
// we use for OscarProducer is unique to OscarProducer
4242
//
43+
// !!! This not only sets the random engine used by GEANT.
44+
// There are a few SimWatchers/SimProducers that generate
45+
// random number and also use the global CLHEP random engine
46+
// set by this code. If we ever change this design be careful
47+
// not to forget about them!!!
48+
4349
class StaticRandomEngineSetUnset {
4450
public:
4551
StaticRandomEngineSetUnset(edm::StreamID const&);

SimTransport/HectorProducer/interface/Hector.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <string>
3636
#include <map>
3737

38-
#include "TRandom3.h"
38+
class TRandom3;
3939

4040
class Hector {
4141

@@ -52,11 +52,11 @@ class Hector {
5252
/*!Adds the stable protons from the event \a ev to a beamline*/
5353
void add( const HepMC::GenEvent * ev , const edm::EventSetup & es);
5454
/*!propagate the particles through a beamline to FP420*/
55-
void filterFP420();
55+
void filterFP420(TRandom3*);
5656
/*!propagate the particles through a beamline to ZDC*/
57-
void filterZDC();
57+
void filterZDC(TRandom3*);
5858
/*!propagate the particles through a beamline to ZDC*/
59-
void filterD1();
59+
void filterD1(TRandom3*);
6060

6161
int getDirect( unsigned int part_n ) const;
6262

@@ -133,8 +133,5 @@ class Hector {
133133
bool m_ZDCTransport;
134134

135135
std::vector<LHCTransportLink> theCorrespondenceMap;
136-
137-
TRandom3* rootEngine_;
138-
139136
};
140137
#endif

0 commit comments

Comments
 (0)