1 #ifndef PhysicsTools_PatUtils_ShiftedJetProducerWithSourcesT_h
2 #define PhysicsTools_PatUtils_ShiftedJetProducerWithSourcesT_h
4 #include "CommonTools/Utils/interface/PtComparator.h"
6 #include "FWCore/Framework/interface/stream/EDProducer.h"
7 #include "FWCore/Framework/interface/Event.h"
8 #include "FWCore/Framework/interface/EventSetup.h"
9 #include "FWCore/ParameterSet/interface/ParameterSet.h"
10 #include "FWCore/ParameterSet/interface/FileInPath.h"
11 #include "FWCore/Utilities/interface/InputTag.h"
13 #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
14 #include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
15 #include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
16 #include "FWCore/Framework/interface/ESHandle.h"
20 typedef std::vector<T> JetCollection;
25 m_enabled(cfg.getParameter<
bool>(
"enabled")),
26 m_debug(cfg.getUntrackedParameter<
bool>(
"debug",
false)),
27 m_split_by_sources(cfg.getParameter<
bool>(
"splitBySources")) {
29 m_jets_token = consumes<JetCollection>(cfg.getParameter<edm::InputTag>(
"src"));
32 shiftBy_ = cfg.getParameter<
double>(
"shiftBy");
35 if (cfg.exists(
"sourceTag")) {
36 m_tag = cfg.getParameter<std::string>(
"sourceTag");
39 if (cfg.exists(
"sources")) {
40 const std::string sourcesFile = cfg.getParameter<edm::FileInPath>(
"sources").fullPath();
41 JetCorrectorParameters parameters(sourcesFile, m_tag);
42 std::unique_ptr<JetCorrectionUncertainty> uncertainty(
new JetCorrectionUncertainty(parameters));
43 jecUncProvider = std::move(uncertainty);
45 if (m_split_by_sources) {
46 for (
const auto& source: m_jec_sources) {
47 JetCorrectorParameters source_parameters(sourcesFile, source);
48 std::unique_ptr<JetCorrectionUncertainty> source_uncertainty(
new JetCorrectionUncertainty(source_parameters));
49 jecSourceUncProviders.emplace(source, std::move(source_uncertainty));
52 }
else if (m_tag !=
"Total") {
53 throw edm::Exception(edm::errors::LogicError,
"You must provide a 'sources' text file for any tag different than 'Total' (only total uncertainty is stored in the Global Tag)");
57 produces<JetCollection>();
58 if (m_split_by_sources) {
59 for (
const auto& source: m_jec_sources)
60 produces<JetCollection>(source);
66 void beginRun(edm::Run
const &, edm::EventSetup
const &setup) {
69 if (! jecUncProvider) {
70 edm::ESHandle<JetCorrectorParametersCollection> jecParametersCollection;
71 setup.get<JetCorrectionsRecord>().get(
"AK4PFchs", jecParametersCollection);
73 JetCorrectorParameters
const &jecParameters = (*jecParametersCollection)[
"Uncertainty"];
74 jecUncProvider.reset(
new JetCorrectionUncertainty(jecParameters));
78 void produce(edm::Event& event,
const edm::EventSetup& es) {
80 edm::Handle<JetCollection> jets;
81 event.getByToken(m_jets_token, jets);
83 std::unique_ptr<JetCollection> shiftedJets(
new JetCollection());
84 std::map<std::string, std::unique_ptr<JetCollection>> sourceShiftedJets;
85 if (jecSourceUncProviders.size() > 0) {
86 for (
const auto& source: m_jec_sources) {
87 sourceShiftedJets.emplace(source, std::move(std::unique_ptr<JetCollection>(
new JetCollection())));
91 for (
const auto& jet: *jets) {
94 shiftedJets->emplace_back(jet);
98 jecUncProvider->setJetEta(jet.eta());
99 jecUncProvider->setJetPt(jet.pt());
101 double const combinedJecUncertainty = shiftBy_ * std::abs(jecUncProvider->getUncertainty(
true));
104 shiftedJet.scaleEnergy(1 + combinedJecUncertainty);
106 if (jecSourceUncProviders.size() > 0) {
107 for (
const auto& it: jecSourceUncProviders) {
108 if (it.first ==
"Total")
111 it.second->setJetEta(jet.eta());
112 it.second->setJetPt(jet.pt());
114 float uncertainty = it.second->getUncertainty(
true);
116 T partialShiftedJet(jet);
117 partialShiftedJet.scaleEnergy(1 + shiftBy_ * uncertainty);
118 sourceShiftedJets[it.first]->emplace_back(partialShiftedJet);
122 shiftedJets->emplace_back(shiftedJet);
126 std::sort(shiftedJets->begin(), shiftedJets->end(), jetPtComparator);
127 event.put(std::move(shiftedJets));
129 for (
auto& it: sourceShiftedJets) {
130 std::sort(it.second->begin(), it.second->end(), jetPtComparator);
131 event.put(std::move(it.second), it.first);
138 bool m_split_by_sources;
140 edm::EDGetTokenT<JetCollection> m_jets_token;
143 std::unique_ptr<JetCorrectionUncertainty> jecUncProvider;
144 std::map<std::string, std::unique_ptr<JetCorrectionUncertainty>> jecSourceUncProviders;
146 GreaterByPt<T> jetPtComparator;
149 std::vector<std::string> m_jec_sources = {
Definition: ShiftedJetProducerWithSourcesT.h:19