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 {
39  typedef std::vector<T> JetCollection;
40 
41  public:
42 
44  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
45  src_(cfg.getParameter<edm::InputTag>("src")),
46  srcToken_(consumes<JetCollection>(src_)),
51  {
52  if ( cfg.exists("jecUncertaintyValue") ) {
53  jecUncertaintyValue_ = cfg.getParameter<double>("jecUncertaintyValue");
54  } else {
55  jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
56  if ( cfg.exists("jetCorrInputFileName") ) {
57  jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
58  if ( jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT")
59  << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
62  } else {
63  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") ) ?
77  cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
78 
79  shiftBy_ = cfg.getParameter<double>("shiftBy");
80 
81  verbosity_ = ( cfg.exists("verbosity") ) ?
82  cfg.getParameter<int>("verbosity") : 0;
83 
84  produces<JetCollection>();
85  }
87  {
88  delete jetCorrParameters_;
89  delete jecUncertainty_;
90  }
91 
92  private:
93 
94  void produce(edm::Event& evt, const edm::EventSetup& es) override
95  {
96  if ( verbosity_ ) {
97  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
98  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
99  std::cout << " src = " << src_.label() << std::endl;
100  }
101 
102  edm::Handle<JetCollection> originalJets;
103  evt.getByToken(srcToken_, originalJets);
104  edm::Handle<reco::JetCorrector> jetCorrUpToL3;
105  evt.getByToken(jetCorrTokenUpToL3_, jetCorrUpToL3);
106  edm::Handle<reco::JetCorrector> jetCorrUpToL3Res;
107  if ( evt.isRealData() && addResidualJES_ ) {
108  evt.getByToken(jetCorrTokenUpToL3Res_, jetCorrUpToL3Res);
109  }
110  auto shiftedJets = std::make_unique<JetCollection>();
111 
112  if ( jetCorrPayloadName_ != "" ) {
114  es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet);
115  const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
116  delete jecUncertainty_;
117  jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
118  }
119 
120  for ( typename JetCollection::const_iterator originalJet = originalJets->begin();
121  originalJet != originalJets->end(); ++originalJet ) {
122  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
123  if ( verbosity_ ) {
124  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta() << ", phi = " << originalJetP4.phi() << std::endl;
125  }
126 
127  double shift = 0.;
128  if ( jecUncertaintyValue_ != -1. ) {
129  shift = jecUncertaintyValue_;
130  } else {
131  jecUncertainty_->setJetEta(originalJetP4.eta());
132  jecUncertainty_->setJetPt(originalJetP4.pt());
133 
134  shift = jecUncertainty_->getUncertainty(true);
135  }
136  if ( verbosity_ ) {
137  std::cout << "shift = " << shift << std::endl;
138  }
139 
140  if ( evt.isRealData() && addResidualJES_ ) {
141  const static pat::RawJetExtractorT<T> rawJetExtractor{};
142  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
143  if ( rawJetP4.E() > 1.e-1 ) {
144  reco::Candidate::LorentzVector corrJetP4upToL3 =
146  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_.label(), jetCorrEtaMax_, &rawJetP4) :
147  jetCorrExtractor_(*originalJet, jetCorrUpToL3.product(), jetCorrEtaMax_, &rawJetP4);
148  reco::Candidate::LorentzVector corrJetP4upToL3Res =
150  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_.label(), jetCorrEtaMax_, &rawJetP4) :
151  jetCorrExtractor_(*originalJet, jetCorrUpToL3Res.product(), jetCorrEtaMax_, &rawJetP4);
152  if ( corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1 ) {
153  double residualJES = (corrJetP4upToL3Res.E()/corrJetP4upToL3.E()) - 1.;
154  shift = sqrt(shift*shift + residualJES*residualJES);
155  }
156  }
157  }
158 
159  shift *= shiftBy_;
160  if ( verbosity_ ) {
161  std::cout << "shift*shiftBy = " << shift << std::endl;
162  }
163 
164  T shiftedJet(*originalJet);
165  shiftedJet.setP4((1. + shift)*originalJetP4);
166  if ( verbosity_ ) {
167  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta() << ", phi = " << shiftedJet.phi() << std::endl;
168  }
169 
170  shiftedJets->push_back(shiftedJet);
171  }
172 
173  evt.put(std::move(shiftedJets));
174  }
175 
177 
180 
186 
188  edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction
190  edm::InputTag jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
192  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
193  // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
194  // reported in
195  // https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
196  // https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
197  Textractor jetCorrExtractor_;
198 
200 
201  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
202 
203  int verbosity_; // flag to enabled/disable debug output
204 };
205 
206 #endif
207 
208 
209 
JetCorrectorParameters * jetCorrParameters_
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3_
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
edm::InputTag jetCorrLabelUpToL3Res_
edm::InputTag jetCorrLabelUpToL3_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
bool exists(std::string const &parameterName) const
checks if a parameter exists
#define nullptr
bool isRealData() const
Definition: EventBase.h:64
std::string jetCorrUncertaintyTag_
ShiftedJetProducerT(const edm::ParameterSet &cfg)
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3Res_
T sqrt(T t)
Definition: SSEVec.h:18
JetCorrectionUncertainty * jecUncertainty_
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:191
T const * product() const
Definition: Handle.h:81
void produce(edm::Event &evt, const edm::EventSetup &es) override
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::string const & label() const
Definition: InputTag.h:36
HLT enums.
T get() const
Definition: EventSetup.h:63
static unsigned int const shift
std::string fullPath() const
Definition: FileInPath.cc:197
edm::FileInPath jetCorrInputFileName_
float getUncertainty(bool fDirection)
std::vector< T > JetCollection
long double T
def move(src, dest)
Definition: eostools.py:510
edm::EDGetTokenT< JetCollection > srcToken_