File indexing completed on 2024-04-06 12:10:16
0001 #ifndef CalibratedPhotonProducer_h
0002 #define CalibratedPhotonProducer_h
0003
0004 #include "DataFormats/Common/interface/Handle.h"
0005 #include "FWCore/Framework/interface/stream/EDProducer.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Framework/interface/ESHandle.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010
0011 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0012 #include "DataFormats/PatCandidates/interface/Photon.h"
0013 #include "EgammaAnalysis/ElectronTools/interface/PhotonEnergyCalibratorRun2.h"
0014
0015 #include <memory>
0016
0017 #include <TRandom2.h>
0018 #include <random>
0019 #include <vector>
0020
0021 template <typename T>
0022 class CalibratedPhotonProducerRun2T : public edm::stream::EDProducer<> {
0023 public:
0024 explicit CalibratedPhotonProducerRun2T(const edm::ParameterSet &);
0025 ~CalibratedPhotonProducerRun2T() override;
0026 void produce(edm::Event &, const edm::EventSetup &) override;
0027
0028 private:
0029 edm::EDGetTokenT<edm::View<T> > thePhotonToken;
0030 PhotonEnergyCalibratorRun2 theEnCorrectorRun2;
0031 std::unique_ptr<TRandom> theSemiDeterministicRng;
0032 };
0033
0034 template <typename T>
0035 CalibratedPhotonProducerRun2T<T>::CalibratedPhotonProducerRun2T(const edm::ParameterSet &conf)
0036 : thePhotonToken(consumes<edm::View<T> >(conf.getParameter<edm::InputTag>("photons"))),
0037 theEnCorrectorRun2(conf.getParameter<bool>("isMC"),
0038 conf.getParameter<bool>("isSynchronization"),
0039 conf.getParameter<std::string>("correctionFile")) {
0040 if (conf.existsAs<bool>("semiDeterministic") && conf.getParameter<bool>("semiDeterministic")) {
0041 theSemiDeterministicRng = std::make_unique<TRandom2>();
0042 theEnCorrectorRun2.initPrivateRng(theSemiDeterministicRng.get());
0043 }
0044 produces<std::vector<T> >();
0045 }
0046
0047 template <typename T>
0048 CalibratedPhotonProducerRun2T<T>::~CalibratedPhotonProducerRun2T() {}
0049
0050 template <typename T>
0051 void CalibratedPhotonProducerRun2T<T>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0052 edm::Handle<edm::View<T> > in;
0053 iEvent.getByToken(thePhotonToken, in);
0054
0055 if (theSemiDeterministicRng && !in->empty()) {
0056 const auto &first = in->front();
0057 std::seed_seq seeder = {int(iEvent.id().event()),
0058 int(iEvent.id().luminosityBlock()),
0059 int(iEvent.id().run()),
0060 int(in->size()),
0061 int(std::numeric_limits<int>::max() * first.phi() / M_PI) & 0xFFF,
0062 int(first.pdgId())};
0063 uint32_t seed = 0, tries = 10;
0064 do {
0065 seeder.generate(&seed, &seed + 1);
0066 tries++;
0067 } while (seed == 0 && tries < 10);
0068 theSemiDeterministicRng->SetSeed(seed ? seed : iEvent.id().event());
0069 }
0070
0071 std::unique_ptr<std::vector<T> > out(new std::vector<T>());
0072 out->reserve(in->size());
0073
0074 for (const T &ele : *in) {
0075 out->push_back(ele);
0076 theEnCorrectorRun2.calibrate(out->back(), iEvent.id().run(), iEvent.streamID());
0077 }
0078
0079 iEvent.put(std::move(out));
0080 }
0081
0082 typedef CalibratedPhotonProducerRun2T<reco::Photon> CalibratedPhotonProducerRun2;
0083 typedef CalibratedPhotonProducerRun2T<pat::Photon> CalibratedPatPhotonProducerRun2;
0084
0085 #include "FWCore/Framework/interface/MakerMacros.h"
0086
0087 DEFINE_FWK_MODULE(CalibratedPhotonProducerRun2);
0088 DEFINE_FWK_MODULE(CalibratedPatPhotonProducerRun2);
0089
0090 #endif