Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce semi-deterministic smearing for e/gamma objects #20878

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
#include "EgammaAnalysis/ElectronTools/interface/ElectronEnergyCalibratorRun2.h"

#include <vector>
#include <random>
#include <TRandom2.h>

template<typename T>
class CalibratedElectronProducerRun2T: public edm::stream::EDProducer<>
{
public:
explicit CalibratedElectronProducerRun2T( const edm::ParameterSet & ) ;
virtual ~CalibratedElectronProducerRun2T();
virtual void produce( edm::Event &, const edm::EventSetup & ) override ;
~CalibratedElectronProducerRun2T() override;
void produce( edm::Event &, const edm::EventSetup & ) override ;

private:
edm::EDGetTokenT<edm::View<T> > theElectronToken;
Expand All @@ -33,6 +35,8 @@ class CalibratedElectronProducerRun2T: public edm::stream::EDProducer<>

EpCombinationTool theEpCombinationTool;
ElectronEnergyCalibratorRun2 theEnCorrectorRun2;
std::unique_ptr<TRandom> theSemiDeterministicRng;

};

template<typename T>
Expand All @@ -42,6 +46,10 @@ CalibratedElectronProducerRun2T<T>::CalibratedElectronProducerRun2T( const edm::
theEpCombinationTool(),
theEnCorrectorRun2(theEpCombinationTool, conf.getParameter<bool>("isMC"), conf.getParameter<bool>("isSynchronization"), conf.getParameter<std::string>("correctionFile"))
{
if (conf.existsAs<bool>("semiDeterministic") && conf.getParameter<bool>("semiDeterministic")) {
theSemiDeterministicRng.reset(new TRandom2());
theEnCorrectorRun2.initPrivateRng(theSemiDeterministicRng.get());
}
produces<std::vector<T> >();
}

Expand All @@ -63,6 +71,17 @@ CalibratedElectronProducerRun2T<T>::produce( edm::Event & iEvent, const edm::Eve
std::unique_ptr<std::vector<T> > out(new std::vector<T>());
out->reserve(in->size());

if (theSemiDeterministicRng && !in->empty()) { // no need to set a seed if in is empty
const auto & first = in->front();
std::seed_seq seeder = {int(iEvent.id().event()), int(iEvent.id().luminosityBlock()), int(iEvent.id().run()),
int(in->size()), int(std::numeric_limits<int>::max()*first.phi()/M_PI) & 0xFFF, int(first.pdgId())};
uint32_t seed = 0, tries = 10;
do {
seeder.generate(&seed,&seed+1); tries++;
} while (seed == 0 && tries < 10);
theSemiDeterministicRng->SetSeed(seed ? seed : iEvent.id().event());
}

for (const T &ele : *in) {
out->push_back(ele);
theEnCorrectorRun2.calibrate(out->back(), iEvent.id().run(), iEvent.streamID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@
#include "EgammaAnalysis/ElectronTools/interface/PhotonEnergyCalibratorRun2.h"

#include <vector>
#include <random>
#include <TRandom2.h>

template<typename T>
class CalibratedPhotonProducerRun2T: public edm::stream::EDProducer<> {
public:
explicit CalibratedPhotonProducerRun2T( const edm::ParameterSet & ) ;
virtual ~CalibratedPhotonProducerRun2T();
virtual void produce( edm::Event &, const edm::EventSetup & ) override ;
~CalibratedPhotonProducerRun2T() override;
void produce( edm::Event &, const edm::EventSetup & ) override ;

private:
edm::EDGetTokenT<edm::View<T> > thePhotonToken;
PhotonEnergyCalibratorRun2 theEnCorrectorRun2;
std::unique_ptr<TRandom> theSemiDeterministicRng;
};

template<typename T>
CalibratedPhotonProducerRun2T<T>::CalibratedPhotonProducerRun2T( const edm::ParameterSet & conf ) :
thePhotonToken(consumes<edm::View<T> >(conf.getParameter<edm::InputTag>("photons"))),
theEnCorrectorRun2(conf.getParameter<bool>("isMC"), conf.getParameter<bool>("isSynchronization"), conf.getParameter<std::string >("correctionFile")) {

if (conf.existsAs<bool>("semiDeterministic") && conf.getParameter<bool>("semiDeterministic")) {
theSemiDeterministicRng.reset(new TRandom2());
theEnCorrectorRun2.initPrivateRng(theSemiDeterministicRng.get());
}
produces<std::vector<T> >();
}

Expand All @@ -45,6 +52,17 @@ CalibratedPhotonProducerRun2T<T>::produce( edm::Event & iEvent, const edm::Event
edm::Handle<edm::View<T> > in;
iEvent.getByToken(thePhotonToken, in);

if (theSemiDeterministicRng && !in->empty()) { // no need to set a seed if in is empty
const auto & first = in->front();
std::seed_seq seeder = {int(iEvent.id().event()), int(iEvent.id().luminosityBlock()), int(iEvent.id().run()),
int(in->size()), int(std::numeric_limits<int>::max()*first.phi()/M_PI) & 0xFFF, int(first.pdgId())};
uint32_t seed = 0, tries = 10;
do {
seeder.generate(&seed,&seed+1); tries++;
} while (seed == 0 && tries < 10);
theSemiDeterministicRng->SetSeed(seed ? seed : iEvent.id().event());
}

std::unique_ptr<std::vector<T> > out(new std::vector<T>());
out->reserve(in->size());

Expand Down