Skip to content

Commit

Permalink
Merge pull request #2457 from wddgit/migrateToNewRandomServiceInterface
Browse files Browse the repository at this point in the history
Multithreading fixes -- Migrate More Simulation to New Random Service Interface
  • Loading branch information
ktf committed Feb 14, 2014
2 parents 668322a + ddbcd74 commit cc49b77
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 102 deletions.
1 change: 1 addition & 0 deletions SimG4CMS/Calo/BuildFile.xml
Expand Up @@ -17,6 +17,7 @@
<use name="FWCore/ServiceRegistry"/>
<use name="CommonTools/UtilAlgos"/>
<use name="boost"/>
<use name="clhep"/>
<use name="geant4core"/>
<use name="hepmc"/>
<use name="root"/>
Expand Down
6 changes: 5 additions & 1 deletion SimG4CMS/Calo/interface/HcalQie.h
Expand Up @@ -11,14 +11,18 @@

#include <vector>

namespace CLHEP {
class HepRandomEngine;
}

class HcalQie {

public:

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

std::vector<int> getCode(int, const std::vector<CaloHit>&);
std::vector<int> getCode(int, const std::vector<CaloHit>&, CLHEP::HepRandomEngine*);
double getEnergy(const std::vector<int>&);

private:
Expand Down
6 changes: 5 additions & 1 deletion SimG4CMS/Calo/interface/HcalTestAnalysis.h
Expand Up @@ -27,6 +27,10 @@ class BeginOfRun;
class BeginOfEvent;
class EndOfEvent;

namespace CLHEP {
class HepRandomEngine;
}

class HcalTestAnalysis : public SimWatcher,
public Observer<const BeginOfJob *>,
public Observer<const BeginOfRun *>,
Expand All @@ -50,7 +54,7 @@ class HcalTestAnalysis : public SimWatcher,
std::vector<int> layerGrouping(int);
std::vector<int> towersToAdd(int centre, int nadd);
void fill(const EndOfEvent * ev);
void qieAnalysis();
void qieAnalysis(CLHEP::HepRandomEngine*);
void layerAnalysis();
double timeOfFlight(int det, int layer, double eta);

Expand Down
23 changes: 5 additions & 18 deletions SimG4CMS/Calo/src/HcalQie.cc
Expand Up @@ -5,11 +5,8 @@

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

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/RandGaussQ.h"
#include "CLHEP/Random/RandPoissonQ.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "CLHEP/Units/GlobalPhysicalConstants.h"

#include <iostream>
Expand Down Expand Up @@ -296,27 +293,16 @@ double HcalQie::getShape(double time) {
}


std::vector<int> HcalQie::getCode(int nht, const std::vector<CaloHit>& hitbuf) {
std::vector<int> HcalQie::getCode(int nht, const std::vector<CaloHit>& hitbuf,
CLHEP::HepRandomEngine* engine) {

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

edm::Service<edm::RandomNumberGenerator> rng;
if ( ! rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "HcalQIE requires the RandomNumberGeneratorService\n"
<< "which is not present in the configuration file. "
<< "You must add the service\n in the configuration file or "
<< "remove the modules that require it.";
}
CLHEP::RandGaussQ randGauss(rng->getEngine(), baseline,sigma);
CLHEP::RandPoissonQ randPoisson(rng->getEngine());


// Noise in the channel
for (int i=0; i<numOfBuckets; i++)
work[i] = randGauss.fire();
work[i] = CLHEP::RandGaussQ::shoot(engine, baseline, sigma);

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

double avpe = ehit/eDepPerPE;
double photo = randPoisson.fire(avpe);
CLHEP::RandPoissonQ randPoissonQ(*engine, avpe);
double photo = randPoissonQ.fire();
etot += ehit;
photons+= photo;
#ifdef DebugLog
Expand Down
8 changes: 5 additions & 3 deletions SimG4CMS/Calo/src/HcalTestAnalysis.cc
Expand Up @@ -23,6 +23,7 @@
#include "G4HCofThisEvent.hh"
#include "CLHEP/Units/GlobalSystemOfUnits.h"
#include "CLHEP/Units/GlobalPhysicalConstants.h"
#include "CLHEP/Random/Random.h"

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

// Qie analysis
qieAnalysis();
CLHEP::HepRandomEngine* engine = CLHEP::HepRandom::getTheEngine();
qieAnalysis(engine);
LogDebug("HcalSim") << "HcalTestAnalysis:: --- after QieAnalysis";

// Layers tuples filling
Expand Down Expand Up @@ -471,7 +473,7 @@ void HcalTestAnalysis::fill(const EndOfEvent * evt) {
}

//-----------------------------------------------------------------------------
void HcalTestAnalysis::qieAnalysis() {
void HcalTestAnalysis::qieAnalysis(CLHEP::HepRandomEngine* engine) {

//Fill tuple with hit information
int hittot = caloHitCache.size();
Expand Down Expand Up @@ -546,7 +548,7 @@ void HcalTestAnalysis::qieAnalysis() {
}
}

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

LogDebug("HcalSim") << "HcalTestAnalysis::Qie: Energy in layer "
Expand Down
8 changes: 6 additions & 2 deletions SimG4CMS/HcalTestBeam/interface/HcalTB04Analysis.h
Expand Up @@ -44,6 +44,10 @@ class EndOfEvent;

class PHcalTB04Info;

namespace CLHEP {
class HepRandomEngine;
}

class HcalTB04Analysis : public SimProducer,
public Observer<const BeginOfRun *>,
public Observer<const BeginOfEvent *>,
Expand Down Expand Up @@ -72,8 +76,8 @@ class HcalTB04Analysis : public SimProducer,

//User methods
void fillBuffer(const EndOfEvent * evt);
void qieAnalysis();
void xtalAnalysis();
void qieAnalysis(CLHEP::HepRandomEngine*);
void xtalAnalysis(CLHEP::HepRandomEngine*);
void finalAnalysis();
void fillEvent(PHcalTB04Info&);

Expand Down
19 changes: 7 additions & 12 deletions SimG4CMS/HcalTestBeam/plugins/HcalTB02Analysis.cc
Expand Up @@ -27,18 +27,20 @@

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/PluginManager/interface/ModuleDef.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/RandGaussQ.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "G4SDManager.hh"
#include "G4VProcess.hh"
#include "G4HCofThisEvent.hh"
#include "CLHEP/Units/GlobalSystemOfUnits.h"
#include "CLHEP/Units/GlobalPhysicalConstants.h"
#include "CLHEP/Random/Random.h"

namespace CLHEP {
class HepRandomEngine;
}

//
// constructors and destructor
Expand Down Expand Up @@ -90,15 +92,8 @@ void HcalTB02Analysis::update(const BeginOfEvent * evt) {

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

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

// Look for the Hit Collection
LogDebug("HcalTBSim") << "HcalTB02Analysis::Fill event "
Expand Down
27 changes: 9 additions & 18 deletions SimG4CMS/HcalTestBeam/plugins/HcalTB04Analysis.cc
Expand Up @@ -41,16 +41,14 @@
#include "FWCore/PluginManager/interface/ModuleDef.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/RandGaussQ.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "G4SDManager.hh"
#include "G4VProcess.hh"
#include "G4HCofThisEvent.hh"
#include "CLHEP/Units/GlobalSystemOfUnits.h"
#include "CLHEP/Units/GlobalPhysicalConstants.h"
#include "CLHEP/Random/Random.h"

//
// constructors and destructor
Expand Down Expand Up @@ -373,13 +371,14 @@ void HcalTB04Analysis::update(const EndOfEvent * evt) {
//QIE analysis
LogDebug("HcalTBSim") << "HcalTB04Analysis::Do QIE analysis with "
<< hcalHitCache.size() << " hits";
qieAnalysis();
CLHEP::HepRandomEngine* engine = CLHEP::HepRandom::getTheEngine();
qieAnalysis(engine);

//Energy in Crystal Matrix
if (!hcalOnly) {
LogDebug("HcalTBSim") << "HcalTB04Analysis::Do Xtal analysis with "
<< ecalHitCache.size() << " hits";
xtalAnalysis();
xtalAnalysis(engine);
}

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

}

void HcalTB04Analysis::qieAnalysis() {
void HcalTB04Analysis::qieAnalysis(CLHEP::HepRandomEngine* engine) {

int hittot = hcalHitCache.size();
if (hittot<=0) hittot = 1;
Expand Down Expand Up @@ -682,7 +681,7 @@ void HcalTB04Analysis::qieAnalysis() {
}
k1 += nhit;
nhit++;
std::vector<int> cd = myQie->getCode(nhit,hits);
std::vector<int> cd = myQie->getCode(nhit, hits, engine);
double eq = myQie->getEnergy(cd);
LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex << id
<< std::dec << " registers " << esim << " energy "
Expand All @@ -700,7 +699,7 @@ void HcalTB04Analysis::qieAnalysis() {
// Towers with no hit
for (int k2 = 0; k2 < nTower; k2++) {
if (todo[k2] == 0) {
std::vector<int> cd = myQie->getCode(0,hits);
std::vector<int> cd = myQie->getCode(0, hits, engine);
double eq = myQie->getEnergy(cd);
esimh[k2] = 0;
eqie[k2] = eq;
Expand All @@ -714,17 +713,9 @@ void HcalTB04Analysis::qieAnalysis() {
}
}

void HcalTB04Analysis::xtalAnalysis() {
void HcalTB04Analysis::xtalAnalysis(CLHEP::HepRandomEngine* engine) {

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

// Crystal Data
std::vector<int> iok(nCrystal,0);
Expand Down
6 changes: 6 additions & 0 deletions SimG4Core/Application/plugins/OscarProducer.cc
Expand Up @@ -40,6 +40,12 @@ namespace {
// static engine, thus we want to ensure that the one
// we use for OscarProducer is unique to OscarProducer
//
// !!! This not only sets the random engine used by GEANT.
// There are a few SimWatchers/SimProducers that generate
// random number and also use the global CLHEP random engine
// set by this code. If we ever change this design be careful
// not to forget about them!!!

class StaticRandomEngineSetUnset {
public:
StaticRandomEngineSetUnset(edm::StreamID const&);
Expand Down
11 changes: 4 additions & 7 deletions SimTransport/HectorProducer/interface/Hector.h
Expand Up @@ -35,7 +35,7 @@
#include <string>
#include <map>

#include "TRandom3.h"
class TRandom3;

class Hector {

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

int getDirect( unsigned int part_n ) const;

Expand Down Expand Up @@ -133,8 +133,5 @@ class Hector {
bool m_ZDCTransport;

std::vector<LHCTransportLink> theCorrespondenceMap;

TRandom3* rootEngine_;

};
#endif

0 comments on commit cc49b77

Please sign in to comment.