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 
25 
32 
34 
35 #include <TMath.h>
36 
37 #include <string>
38 
39 template <typename T, typename Textractor>
41 {
42  typedef std::vector<T> JetCollection;
43 
44  public:
45 
47  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
48  src_(cfg.getParameter<edm::InputTag>("src")),
51  jecUncertainty_(0),
53  {
54  if ( cfg.exists("jecUncertaintyValue") ) {
55  jecUncertaintyValue_ = cfg.getParameter<double>("jecUncertaintyValue");
56  } else {
57  jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
58  if ( cfg.exists("jetCorrInputFileName") ) {
59  jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
60  if ( jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT")
61  << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
62  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
63  << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl;
66  } else {
67  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
68  << " from DB/SQLlite file." << std::endl;
69  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
70  }
71  }
72 
73  addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
74  jetCorrLabelUpToL3_ = ( cfg.exists("jetCorrLabelUpToL3") ) ?
75  cfg.getParameter<std::string>("jetCorrLabelUpToL3") : "";
76  jetCorrLabelUpToL3Res_ = ( cfg.exists("jetCorrLabelUpToL3Res") ) ?
77  cfg.getParameter<std::string>("jetCorrLabelUpToL3Res") : "";
78  jetCorrEtaMax_ = ( cfg.exists("jetCorrEtaMax") ) ?
79  cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
80 
81  shiftBy_ = cfg.getParameter<double>("shiftBy");
82 
83  verbosity_ = ( cfg.exists("verbosity") ) ?
84  cfg.getParameter<int>("verbosity") : 0;
85 
86  produces<JetCollection>();
87  }
89  {
90  delete jetCorrParameters_;
91  delete jecUncertainty_;
92  }
93 
94  private:
95 
96  void produce(edm::Event& evt, const edm::EventSetup& es)
97  {
98  if ( verbosity_ ) {
99  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
100  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
101  std::cout << " src = " << src_.label() << std::endl;
102  }
103 
104  edm::Handle<JetCollection> originalJets;
105  evt.getByLabel(src_, originalJets);
106 
107  std::auto_ptr<JetCollection> shiftedJets(new JetCollection);
108 
109  if ( jetCorrPayloadName_ != "" ) {
111  es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet);
112  const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
113  delete jecUncertainty_;
114  jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
115  }
116 
117  for ( typename JetCollection::const_iterator originalJet = originalJets->begin();
118  originalJet != originalJets->end(); ++originalJet ) {
119  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
120  if ( verbosity_ ) {
121  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta() << ", phi = " << originalJetP4.phi() << std::endl;
122  }
123 
124  double shift = 0.;
125  if ( jecUncertaintyValue_ != -1. ) {
126  shift = jecUncertaintyValue_;
127  } else {
128  jecUncertainty_->setJetEta(originalJetP4.eta());
129  jecUncertainty_->setJetPt(originalJetP4.pt());
130 
131  shift = jecUncertainty_->getUncertainty(true);
132  }
133  if ( verbosity_ ) {
134  std::cout << "shift = " << shift << std::endl;
135  }
136 
137  if ( addResidualJES_ ) {
139  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
140  if ( rawJetP4.E() > 1.e-1 ) {
141  reco::Candidate::LorentzVector corrJetP4upToL3 =
142  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
143  reco::Candidate::LorentzVector corrJetP4upToL3Res =
144  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
145  if ( corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1 ) {
146  double residualJES = (corrJetP4upToL3Res.E()/corrJetP4upToL3.E()) - 1.;
147  shift = TMath::Sqrt(shift*shift + residualJES*residualJES);
148  }
149  }
150  }
151 
152  shift *= shiftBy_;
153  if ( verbosity_ ) {
154  std::cout << "shift*shiftBy = " << shift << std::endl;
155  }
156 
157  T shiftedJet(*originalJet);
158  shiftedJet.setP4((1. + shift)*originalJetP4);
159  if ( verbosity_ ) {
160  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta() << ", phi = " << shiftedJet.phi() << std::endl;
161  }
162 
163  shiftedJets->push_back(shiftedJet);
164  }
165 
166  evt.put(shiftedJets);
167  }
168 
169  std::string moduleLabel_;
170 
172 
174  std::string jetCorrPayloadName_;
178 
180  std::string jetCorrLabelUpToL3_; // L1+L2+L3 correction
181  std::string jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
182  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
183  // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
184  // reported in
185  // https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
186  // https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
187  Textractor jetCorrExtractor_;
188 
190 
191  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
192 
193  int verbosity_; // flag to enabled/disable debug output
194 };
195 
196 #endif
197 
198 
199 
JetCorrectorParameters * jetCorrParameters_
T getParameter(std::string const &) const
void produce(edm::Event &evt, const edm::EventSetup &es)
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string jetCorrUncertaintyTag_
ShiftedJetProducerT(const edm::ParameterSet &cfg)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
JetCorrectionUncertainty * jecUncertainty_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
const T & get() const
Definition: EventSetup.h:55
std::string jetCorrLabelUpToL3Res_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:38
std::string const & label() const
Definition: InputTag.h:25
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:171
std::vector< T > JetCollection
long double T