Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-05 07:05:21

0001 #ifndef CalibratedElectronProducerRun2_h
0002 #define CalibratedElectronProducerRun2_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/GsfElectron.h"
0012 #include "DataFormats/PatCandidates/interface/Electron.h"
0013 
0014 #include "CondFormats/DataRecord/interface/GBRWrapperRcd.h"
0015 #include "CondFormats/GBRForest/interface/GBRForest.h"
0016 #include "EgammaAnalysis/ElectronTools/interface/EpCombinationTool.h"
0017 #include "EgammaAnalysis/ElectronTools/interface/ElectronEnergyCalibratorRun2.h"
0018 
0019 #include <memory>
0020 
0021 #include <TRandom2.h>
0022 #include <random>
0023 #include <vector>
0024 
0025 template <typename T>
0026 class CalibratedElectronProducerRun2T : public edm::stream::EDProducer<> {
0027 public:
0028   explicit CalibratedElectronProducerRun2T(const edm::ParameterSet &);
0029   ~CalibratedElectronProducerRun2T() override;
0030   void produce(edm::Event &, const edm::EventSetup &) override;
0031 
0032 private:
0033   edm::EDGetTokenT<edm::View<T> > theElectronToken;
0034   std::string theGBRForestName;
0035 
0036   EpCombinationTool theEpCombinationTool;
0037   ElectronEnergyCalibratorRun2 theEnCorrectorRun2;
0038   std::unique_ptr<TRandom> theSemiDeterministicRng;
0039 
0040   edm::ESGetToken<GBRForest, GBRWrapperRcd> gbrforestToken_;
0041 };
0042 
0043 template <typename T>
0044 CalibratedElectronProducerRun2T<T>::CalibratedElectronProducerRun2T(const edm::ParameterSet &conf)
0045     : theElectronToken(consumes<edm::View<T> >(conf.getParameter<edm::InputTag>("electrons"))),
0046       theGBRForestName(conf.getParameter<std::string>("gbrForestName")),
0047       theEpCombinationTool(),
0048       theEnCorrectorRun2(theEpCombinationTool,
0049                          conf.getParameter<bool>("isMC"),
0050                          conf.getParameter<bool>("isSynchronization"),
0051                          conf.getParameter<std::string>("correctionFile")) {
0052   if (conf.existsAs<bool>("semiDeterministic") && conf.getParameter<bool>("semiDeterministic")) {
0053     theSemiDeterministicRng = std::make_unique<TRandom2>();
0054     theEnCorrectorRun2.initPrivateRng(theSemiDeterministicRng.get());
0055   }
0056   gbrforestToken_ = esConsumes(edm::ESInputTag("", theGBRForestName));
0057   produces<std::vector<T> >();
0058 }
0059 
0060 template <typename T>
0061 CalibratedElectronProducerRun2T<T>::~CalibratedElectronProducerRun2T() {}
0062 
0063 template <typename T>
0064 void CalibratedElectronProducerRun2T<T>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0065   const GBRForest *theGBRForest = &iSetup.getData(gbrforestToken_);
0066   theEpCombinationTool.init(theGBRForest);
0067 
0068   edm::Handle<edm::View<T> > in;
0069   iEvent.getByToken(theElectronToken, in);
0070 
0071   std::unique_ptr<std::vector<T> > out(new std::vector<T>());
0072   out->reserve(in->size());
0073 
0074   if (theSemiDeterministicRng && !in->empty()) {  // no need to set a seed if in is empty
0075     const auto &first = in->front();
0076     std::seed_seq seeder = {int(iEvent.id().event()),
0077                             int(iEvent.id().luminosityBlock()),
0078                             int(iEvent.id().run()),
0079                             int(in->size()),
0080                             int(std::numeric_limits<int>::max() * first.phi() / M_PI) & 0xFFF,
0081                             int(first.pdgId())};
0082     uint32_t seed = 0, tries = 10;
0083     do {
0084       seeder.generate(&seed, &seed + 1);
0085       tries++;
0086     } while (seed == 0 && tries < 10);
0087     theSemiDeterministicRng->SetSeed(seed ? seed : iEvent.id().event());
0088   }
0089 
0090   for (const T &ele : *in) {
0091     out->push_back(ele);
0092     theEnCorrectorRun2.calibrate(out->back(), iEvent.id().run(), iEvent.streamID());
0093   }
0094 
0095   iEvent.put(std::move(out));
0096 }
0097 
0098 typedef CalibratedElectronProducerRun2T<reco::GsfElectron> CalibratedElectronProducerRun2;
0099 typedef CalibratedElectronProducerRun2T<pat::Electron> CalibratedPatElectronProducerRun2;
0100 
0101 #include "FWCore/Framework/interface/MakerMacros.h"
0102 
0103 DEFINE_FWK_MODULE(CalibratedElectronProducerRun2);
0104 DEFINE_FWK_MODULE(CalibratedPatElectronProducerRun2);
0105 
0106 #endif