CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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")),
49  jecUncertainty_(0),
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";
60  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
61  << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl;
64  } else {
65  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
66  << " from DB/SQLlite file." << std::endl;
67  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
68  }
69  }
70 
71  addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
72  if ( cfg.exists("jetCorrLabelUpToL3") ) {
73  jetCorrLabelUpToL3_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3");
74  jetCorrTokenUpToL3_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3_);
75  }
76  if ( cfg.exists("jetCorrLabelUpToL3Res") && addResidualJES_ ) {
77  jetCorrLabelUpToL3Res_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3Res");
78  jetCorrTokenUpToL3Res_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3Res_);
79  }
80  jetCorrEtaMax_ = ( cfg.exists("jetCorrEtaMax") ) ?
81  cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
82 
83  shiftBy_ = cfg.getParameter<double>("shiftBy");
84 
85  verbosity_ = ( cfg.exists("verbosity") ) ?
86  cfg.getParameter<int>("verbosity") : 0;
87 
88  produces<JetCollection>();
89  }
91  {
92  delete jetCorrParameters_;
93  delete jecUncertainty_;
94  }
95 
96  private:
97 
98  void produce(edm::Event& evt, const edm::EventSetup& es)
99  {
100  if ( verbosity_ ) {
101  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
102  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
103  std::cout << " src = " << src_.label() << std::endl;
104  }
105 
106  edm::Handle<JetCollection> originalJets;
107  evt.getByToken(srcToken_, originalJets);
108  edm::Handle<reco::JetCorrector> jetCorrUpToL3;
109  evt.getByToken(jetCorrTokenUpToL3_, jetCorrUpToL3);
110  edm::Handle<reco::JetCorrector> jetCorrUpToL3Res;
111  if ( evt.isRealData() && addResidualJES_ ) {
112  evt.getByToken(jetCorrTokenUpToL3Res_, jetCorrUpToL3Res);
113  }
114  std::auto_ptr<JetCollection> shiftedJets(new JetCollection);
115 
116  if ( jetCorrPayloadName_ != "" ) {
118  es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet);
119  const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
120  delete jecUncertainty_;
121  jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
122  }
123 
124  for ( typename JetCollection::const_iterator originalJet = originalJets->begin();
125  originalJet != originalJets->end(); ++originalJet ) {
126  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
127  if ( verbosity_ ) {
128  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta() << ", phi = " << originalJetP4.phi() << std::endl;
129  }
130 
131  double shift = 0.;
132  if ( jecUncertaintyValue_ != -1. ) {
133  shift = jecUncertaintyValue_;
134  } else {
135  jecUncertainty_->setJetEta(originalJetP4.eta());
136  jecUncertainty_->setJetPt(originalJetP4.pt());
137 
138  shift = jecUncertainty_->getUncertainty(true);
139  }
140  if ( verbosity_ ) {
141  std::cout << "shift = " << shift << std::endl;
142  }
143 
144  if ( evt.isRealData() && addResidualJES_ ) {
145  const static SmearedJetProducer_namespace::RawJetExtractorT<T> rawJetExtractor;
146  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
147  if ( rawJetP4.E() > 1.e-1 ) {
148  reco::Candidate::LorentzVector corrJetP4upToL3 =
150  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_.label(), jetCorrEtaMax_, &rawJetP4) :
151  jetCorrExtractor_(*originalJet, jetCorrUpToL3.product(), jetCorrEtaMax_, &rawJetP4);
152  reco::Candidate::LorentzVector corrJetP4upToL3Res =
154  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_.label(), jetCorrEtaMax_, &rawJetP4) :
155  jetCorrExtractor_(*originalJet, jetCorrUpToL3Res.product(), jetCorrEtaMax_, &rawJetP4);
156  if ( corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1 ) {
157  double residualJES = (corrJetP4upToL3Res.E()/corrJetP4upToL3.E()) - 1.;
158  shift = sqrt(shift*shift + residualJES*residualJES);
159  }
160  }
161  }
162 
163  shift *= shiftBy_;
164  if ( verbosity_ ) {
165  std::cout << "shift*shiftBy = " << shift << std::endl;
166  }
167 
168  T shiftedJet(*originalJet);
169  shiftedJet.setP4((1. + shift)*originalJetP4);
170  if ( verbosity_ ) {
171  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta() << ", phi = " << shiftedJet.phi() << std::endl;
172  }
173 
174  shiftedJets->push_back(shiftedJet);
175  }
176 
177  evt.put(shiftedJets);
178  }
179 
181 
184 
190 
192  edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction
194  edm::InputTag jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
196  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
197  // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
198  // reported in
199  // https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
200  // https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
201  Textractor jetCorrExtractor_;
202 
204 
205  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
206 
207  int verbosity_; // flag to enabled/disable debug output
208 };
209 
210 #endif
211 
212 
213 
JetCorrectorParameters * jetCorrParameters_
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3_
T getParameter(std::string const &) const
void produce(edm::Event &evt, const edm::EventSetup &es)
edm::InputTag jetCorrLabelUpToL3Res_
tuple cfg
Definition: looper.py:259
edm::InputTag jetCorrLabelUpToL3_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool isRealData() const
Definition: EventBase.h:64
std::string jetCorrUncertaintyTag_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ShiftedJetProducerT(const edm::ParameterSet &cfg)
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3Res_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
T sqrt(T t)
Definition: SSEVec.h:48
JetCorrectionUncertainty * jecUncertainty_
T const * product() const
Definition: Handle.h:81
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
const T & get() const
Definition: EventSetup.h:55
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
std::string const & label() const
Definition: InputTag.h:42
static unsigned int const shift
tuple cout
Definition: gather_cfg.py:121
edm::FileInPath jetCorrInputFileName_
float getUncertainty(bool fDirection)
std::string fullPath() const
Definition: FileInPath.cc:165
std::vector< T > JetCollection
long double T
edm::EDGetTokenT< JetCollection > srcToken_