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 
32 
33 #include <TMath.h>
34 
35 #include <string>
36 
37 template <typename T, typename Textractor>
39 {
40  typedef std::vector<T> JetCollection;
41 
42  public:
43 
45  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
46  src_(cfg.getParameter<edm::InputTag>("src")),
50  jecUncertainty_(0),
52  {
53  if ( cfg.exists("jecUncertaintyValue") ) {
54  jecUncertaintyValue_ = cfg.getParameter<double>("jecUncertaintyValue");
55  } else {
56  jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
57  if ( cfg.exists("jetCorrInputFileName") ) {
58  jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
59  if ( jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT")
60  << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
61  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
62  << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl;
65  } else {
66  std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
67  << " from DB/SQLlite file." << std::endl;
68  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
69  }
70  }
71 
72  addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
73  jetCorrLabelUpToL3_ = ( cfg.exists("jetCorrLabelUpToL3") ) ?
74  cfg.getParameter<std::string>("jetCorrLabelUpToL3") : "";
75  jetCorrLabelUpToL3Res_ = ( cfg.exists("jetCorrLabelUpToL3Res") ) ?
76  cfg.getParameter<std::string>("jetCorrLabelUpToL3Res") : "";
77  jetCorrEtaMax_ = ( cfg.exists("jetCorrEtaMax") ) ?
78  cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
79 
80  shiftBy_ = cfg.getParameter<double>("shiftBy");
81 
82  verbosity_ = ( cfg.exists("verbosity") ) ?
83  cfg.getParameter<int>("verbosity") : 0;
84 
85  produces<JetCollection>();
86  }
88  {
89  delete jetCorrParameters_;
90  delete jecUncertainty_;
91  }
92 
93  private:
94 
95  void produce(edm::Event& evt, const edm::EventSetup& es)
96  {
97  if ( verbosity_ ) {
98  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
99  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
100  std::cout << " src = " << src_.label() << std::endl;
101  }
102 
103  edm::Handle<JetCollection> originalJets;
104  evt.getByToken(srcToken_, originalJets);
105 
106  std::auto_ptr<JetCollection> shiftedJets(new JetCollection);
107 
108  if ( jetCorrPayloadName_ != "" ) {
110  es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet);
111  const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
112  delete jecUncertainty_;
113  jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
114  }
115 
116  for ( typename JetCollection::const_iterator originalJet = originalJets->begin();
117  originalJet != originalJets->end(); ++originalJet ) {
118  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
119  if ( verbosity_ ) {
120  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta() << ", phi = " << originalJetP4.phi() << std::endl;
121  }
122 
123  double shift = 0.;
124  if ( jecUncertaintyValue_ != -1. ) {
125  shift = jecUncertaintyValue_;
126  } else {
127  jecUncertainty_->setJetEta(originalJetP4.eta());
128  jecUncertainty_->setJetPt(originalJetP4.pt());
129 
130  shift = jecUncertainty_->getUncertainty(true);
131  }
132  if ( verbosity_ ) {
133  std::cout << "shift = " << shift << std::endl;
134  }
135 
136  if ( addResidualJES_ ) {
137  const static SmearedJetProducer_namespace::RawJetExtractorT<T> rawJetExtractor;
138  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
139  if ( rawJetP4.E() > 1.e-1 ) {
140  reco::Candidate::LorentzVector corrJetP4upToL3 =
141  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
142  reco::Candidate::LorentzVector corrJetP4upToL3Res =
143  jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
144  if ( corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1 ) {
145  double residualJES = (corrJetP4upToL3Res.E()/corrJetP4upToL3.E()) - 1.;
146  shift = TMath::Sqrt(shift*shift + residualJES*residualJES);
147  }
148  }
149  }
150 
151  shift *= shiftBy_;
152  if ( verbosity_ ) {
153  std::cout << "shift*shiftBy = " << shift << std::endl;
154  }
155 
156  T shiftedJet(*originalJet);
157  shiftedJet.setP4((1. + shift)*originalJetP4);
158  if ( verbosity_ ) {
159  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta() << ", phi = " << shiftedJet.phi() << std::endl;
160  }
161 
162  shiftedJets->push_back(shiftedJet);
163  }
164 
165  evt.put(shiftedJets);
166  }
167 
169 
172 
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 getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string jetCorrUncertaintyTag_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ShiftedJetProducerT(const edm::ParameterSet &cfg)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
JetCorrectionUncertainty * jecUncertainty_
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:41
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_