CMS 3D CMS Logo

ShiftedJetProducerT.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PatUtils_ShiftedJetProducerT_h
2 #define PhysicsTools_PatUtils_ShiftedJetProducerT_h
3 
23 
30 
33 
34 #include <string>
35 
36 template <typename T, typename Textractor>
38  typedef std::vector<T> JetCollection;
39 
40 public:
42  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
43  src_(cfg.getParameter<edm::InputTag>("src")),
44  srcToken_(consumes<JetCollection>(src_)),
46  jetCorrParameters_(nullptr),
47  jecUncertainty_(nullptr),
49  if (cfg.exists("jecUncertaintyValue")) {
50  jecUncertaintyValue_ = cfg.getParameter<double>("jecUncertaintyValue");
51  } else {
52  jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
53  if (cfg.exists("jetCorrInputFileName")) {
54  jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
56  throw cms::Exception("ShiftedJetProducerT")
57  << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
59  std::make_unique<JetCorrectorParameters>(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_);
60  jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(*jetCorrParameters_);
61  } else {
62  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
64  }
65  }
66 
67  addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
68  if (cfg.exists("jetCorrLabelUpToL3")) {
69  jetCorrLabelUpToL3_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3");
70  jetCorrTokenUpToL3_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3_);
71  }
72  if (cfg.exists("jetCorrLabelUpToL3Res") && addResidualJES_) {
73  jetCorrLabelUpToL3Res_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3Res");
74  jetCorrTokenUpToL3Res_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3Res_);
75  }
76  jetCorrEtaMax_ = (cfg.exists("jetCorrEtaMax")) ? cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
77 
78  shiftBy_ = cfg.getParameter<double>("shiftBy");
79 
80  verbosity_ = (cfg.exists("verbosity")) ? cfg.getParameter<int>("verbosity") : 0;
81 
82  produces<JetCollection>();
83  }
84 
85 private:
86  void produce(edm::Event& evt, const edm::EventSetup& es) override {
87  if (verbosity_) {
88  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
89  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
90  std::cout << " src = " << src_.label() << std::endl;
91  }
92 
93  edm::Handle<JetCollection> originalJets;
94  evt.getByToken(srcToken_, originalJets);
95  edm::Handle<reco::JetCorrector> jetCorrUpToL3;
96  evt.getByToken(jetCorrTokenUpToL3_, jetCorrUpToL3);
97  edm::Handle<reco::JetCorrector> jetCorrUpToL3Res;
98  if (evt.isRealData() && addResidualJES_) {
99  evt.getByToken(jetCorrTokenUpToL3Res_, jetCorrUpToL3Res);
100  }
101  auto shiftedJets = std::make_unique<JetCollection>();
102 
103  if (!jetCorrPayloadName_.empty()) {
104  const JetCorrectorParametersCollection& jetCorrParameterSet = es.getData(jetCorrPayloadToken_);
105  const JetCorrectorParameters& jetCorrParameters = (jetCorrParameterSet)[jetCorrUncertaintyTag_];
106  jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(jetCorrParameters);
107  }
108 
109  for (typename JetCollection::const_iterator originalJet = originalJets->begin(); originalJet != originalJets->end();
110  ++originalJet) {
111  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
112  if (verbosity_) {
113  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta()
114  << ", phi = " << originalJetP4.phi() << std::endl;
115  }
116 
117  double shift = 0.;
118  if (jecUncertaintyValue_ != -1.) {
120  } else {
121  jecUncertainty_->setJetEta(originalJetP4.eta());
122  jecUncertainty_->setJetPt(originalJetP4.pt());
123 
124  shift = jecUncertainty_->getUncertainty(true);
125  }
126  if (verbosity_) {
127  std::cout << "shift = " << shift << std::endl;
128  }
129 
130  if (evt.isRealData() && addResidualJES_) {
131  const static pat::RawJetExtractorT<T> rawJetExtractor{};
132  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
133  if (rawJetP4.E() > 1.e-1) {
134  reco::Candidate::LorentzVector corrJetP4upToL3 =
136  ? jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_.label(), jetCorrEtaMax_, &rawJetP4)
137  : jetCorrExtractor_(*originalJet, jetCorrUpToL3.product(), jetCorrEtaMax_, &rawJetP4);
138  reco::Candidate::LorentzVector corrJetP4upToL3Res =
140  ? jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_.label(), jetCorrEtaMax_, &rawJetP4)
141  : jetCorrExtractor_(*originalJet, jetCorrUpToL3Res.product(), jetCorrEtaMax_, &rawJetP4);
142  if (corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1) {
143  double residualJES = (corrJetP4upToL3Res.E() / corrJetP4upToL3.E()) - 1.;
144  shift = sqrt(shift * shift + residualJES * residualJES);
145  }
146  }
147  }
148 
149  shift *= shiftBy_;
150  if (verbosity_) {
151  std::cout << "shift*shiftBy = " << shift << std::endl;
152  }
153 
154  T shiftedJet(*originalJet);
155  shiftedJet.setP4((1. + shift) * originalJetP4);
156  if (verbosity_) {
157  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta()
158  << ", phi = " << shiftedJet.phi() << std::endl;
159  }
160 
161  shiftedJets->push_back(shiftedJet);
162  }
163 
164  evt.put(std::move(shiftedJets));
165  }
166 
168 
171 
176  std::unique_ptr<JetCorrectorParameters> jetCorrParameters_;
177  std::unique_ptr<JetCorrectionUncertainty> jecUncertainty_;
178 
180  edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction
182  edm::InputTag jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
184  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
185  // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
186  // reported in
187  // https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
188  // https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
189  Textractor jetCorrExtractor_;
190 
192 
193  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
194 
195  int verbosity_; // flag to enabled/disable debug output
196 };
197 
198 #endif
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::InputTag jetCorrLabelUpToL3Res_
edm::InputTag jetCorrLabelUpToL3_
std::string fullPath() const
Definition: FileInPath.cc:161
T const * product() const
Definition: Handle.h:70
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:540
std::string const & label() const
Definition: InputTag.h:36
edm::ESGetToken< JetCorrectorParametersCollection, JetCorrectionsRecord > jetCorrPayloadToken_
std::string jetCorrUncertaintyTag_
std::unique_ptr< JetCorrectionUncertainty > jecUncertainty_
ShiftedJetProducerT(const edm::ParameterSet &cfg)
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3Res_
T sqrt(T t)
Definition: SSEVec.h:19
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
bool getData(T &iHolder) const
Definition: EventSetup.h:122
std::unique_ptr< JetCorrectorParameters > jetCorrParameters_
void produce(edm::Event &evt, const edm::EventSetup &es) override
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
HLT enums.
bool isRealData() const
Definition: EventBase.h:66
static unsigned int const shift
edm::FileInPath jetCorrInputFileName_
std::vector< T > JetCollection
long double T
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< JetCollection > srcToken_