CP3-llbb Framework
ShiftedJetProducerWithSourcesT.h
1 #ifndef PhysicsTools_PatUtils_ShiftedJetProducerWithSourcesT_h
2 #define PhysicsTools_PatUtils_ShiftedJetProducerWithSourcesT_h
3 
4 #include "CommonTools/Utils/interface/PtComparator.h"
5 
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"
12 
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"
17 
18 template <typename T>
19 class ShiftedJetProducerWithSourcesT : public edm::stream::EDProducer<> {
20  typedef std::vector<T> JetCollection;
21 
22  public:
23 
24  explicit ShiftedJetProducerWithSourcesT(const edm::ParameterSet& cfg) :
25  m_enabled(cfg.getParameter<bool>("enabled")),
26  m_debug(cfg.getUntrackedParameter<bool>("debug", false)),
27  m_split_by_sources(cfg.getParameter<bool>("splitBySources")) {
28 
29  m_jets_token = consumes<JetCollection>(cfg.getParameter<edm::InputTag>("src"));
30 
31  if (m_enabled) {
32  shiftBy_ = cfg.getParameter<double>("shiftBy");
33 
34  m_tag = "Total";
35  if (cfg.exists("sourceTag")) {
36  m_tag = cfg.getParameter<std::string>("sourceTag");
37  }
38 
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);
44 
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));
50  }
51  }
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)");
54  }
55  }
56 
57  produces<JetCollection>();
58  if (m_split_by_sources) {
59  for (const auto& source: m_jec_sources)
60  produces<JetCollection>(source);
61  }
62  }
63 
64  private:
65 
66  void beginRun(edm::Run const &, edm::EventSetup const &setup) {
67  // Construct an object to obtain JEC uncertainty [1]
68  //[1] https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookJetEnergyCorrections?rev=137#JetCorUncertainties
69  if (! jecUncProvider) {
70  edm::ESHandle<JetCorrectorParametersCollection> jecParametersCollection;
71  setup.get<JetCorrectionsRecord>().get("AK4PFchs", jecParametersCollection);
72 
73  JetCorrectorParameters const &jecParameters = (*jecParametersCollection)["Uncertainty"];
74  jecUncProvider.reset(new JetCorrectionUncertainty(jecParameters));
75  }
76  }
77 
78  void produce(edm::Event& event, const edm::EventSetup& es) {
79 
80  edm::Handle<JetCollection> jets;
81  event.getByToken(m_jets_token, jets);
82 
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())));
88  }
89  }
90 
91  for (const auto& jet: *jets) {
92 
93  if (! m_enabled) {
94  shiftedJets->emplace_back(jet);
95  continue;
96  }
97 
98  jecUncProvider->setJetEta(jet.eta());
99  jecUncProvider->setJetPt(jet.pt());
100 
101  double const combinedJecUncertainty = shiftBy_ * std::abs(jecUncProvider->getUncertainty(true));
102 
103  T shiftedJet(jet);
104  shiftedJet.scaleEnergy(1 + combinedJecUncertainty);
105 
106  if (jecSourceUncProviders.size() > 0) {
107  for (const auto& it: jecSourceUncProviders) {
108  if (it.first == "Total")
109  continue;
110 
111  it.second->setJetEta(jet.eta());
112  it.second->setJetPt(jet.pt());
113 
114  float uncertainty = it.second->getUncertainty(true);
115 
116  T partialShiftedJet(jet);
117  partialShiftedJet.scaleEnergy(1 + shiftBy_ * uncertainty);
118  sourceShiftedJets[it.first]->emplace_back(partialShiftedJet);
119  }
120  }
121 
122  shiftedJets->emplace_back(shiftedJet);
123  }
124 
125  // Sort jets by pt
126  std::sort(shiftedJets->begin(), shiftedJets->end(), jetPtComparator);
127  event.put(std::move(shiftedJets));
128 
129  for (auto& it: sourceShiftedJets) {
130  std::sort(it.second->begin(), it.second->end(), jetPtComparator);
131  event.put(std::move(it.second), it.first);
132  }
133  }
134 
135  bool m_enabled;
136  bool m_debug;
137  std::string m_tag;
138  bool m_split_by_sources;
139 
140  edm::EDGetTokenT<JetCollection> m_jets_token;
141  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
142 
143  std::unique_ptr<JetCorrectionUncertainty> jecUncProvider;
144  std::map<std::string, std::unique_ptr<JetCorrectionUncertainty>> jecSourceUncProviders;
145 
146  GreaterByPt<T> jetPtComparator;
147 
148  // Updated list from https://hypernews.cern.ch/HyperNews/CMS/get/jes/648/1/1/1.html
149  std::vector<std::string> m_jec_sources = {
150  "AbsoluteFlavMap",
151  "AbsoluteMPFBias",
152  "AbsoluteScale",
153  "AbsoluteStat",
154  "FlavorQCD",
155  "Fragmentation",
156  "PileUpDataMC",
157  "PileUpPtBB",
158  "PileUpPtEC1",
159  "PileUpPtEC2",
160  "PileUpPtHF",
161  "PileUpPtRef",
162  "RelativeBal",
163  "RelativeFSR",
164  "RelativeJEREC1",
165  "RelativeJEREC2",
166  "RelativeJERHF",
167  "RelativePtBB",
168  "RelativePtEC1",
169  "RelativePtEC2",
170  "RelativePtHF",
171  "RelativeStatEC",
172  "RelativeStatFSR",
173  "RelativeStatHF",
174  "SinglePionECAL",
175  "SinglePionHCAL",
176  "TimePtEta"
177  };
178 };
179 
180 #endif
181 
182 
183 
Definition: ShiftedJetProducerWithSourcesT.h:19