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";
60  } else {
61  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
62  }
63  }
64 
65  addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
66  if (cfg.exists("jetCorrLabelUpToL3")) {
67  jetCorrLabelUpToL3_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3");
68  jetCorrTokenUpToL3_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3_);
69  }
70  if (cfg.exists("jetCorrLabelUpToL3Res") && addResidualJES_) {
71  jetCorrLabelUpToL3Res_ = cfg.getParameter<edm::InputTag>("jetCorrLabelUpToL3Res");
72  jetCorrTokenUpToL3Res_ = mayConsume<reco::JetCorrector>(jetCorrLabelUpToL3Res_);
73  }
74  jetCorrEtaMax_ = (cfg.exists("jetCorrEtaMax")) ? cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
75 
76  shiftBy_ = cfg.getParameter<double>("shiftBy");
77 
78  verbosity_ = (cfg.exists("verbosity")) ? cfg.getParameter<int>("verbosity") : 0;
79 
80  produces<JetCollection>();
81  }
82  ~ShiftedJetProducerT() override {
83  delete jetCorrParameters_;
84  delete jecUncertainty_;
85  }
86 
87 private:
88  void produce(edm::Event& evt, const edm::EventSetup& es) override {
89  if (verbosity_) {
90  std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
91  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
92  std::cout << " src = " << src_.label() << std::endl;
93  }
94 
95  edm::Handle<JetCollection> originalJets;
96  evt.getByToken(srcToken_, originalJets);
97  edm::Handle<reco::JetCorrector> jetCorrUpToL3;
98  evt.getByToken(jetCorrTokenUpToL3_, jetCorrUpToL3);
99  edm::Handle<reco::JetCorrector> jetCorrUpToL3Res;
100  if (evt.isRealData() && addResidualJES_) {
101  evt.getByToken(jetCorrTokenUpToL3Res_, jetCorrUpToL3Res);
102  }
103  auto shiftedJets = std::make_unique<JetCollection>();
104 
105  if (!jetCorrPayloadName_.empty()) {
107  es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet);
108  const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
109  delete jecUncertainty_;
110  jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
111  }
112 
113  for (typename JetCollection::const_iterator originalJet = originalJets->begin(); originalJet != originalJets->end();
114  ++originalJet) {
115  reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
116  if (verbosity_) {
117  std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta()
118  << ", phi = " << originalJetP4.phi() << std::endl;
119  }
120 
121  double shift = 0.;
122  if (jecUncertaintyValue_ != -1.) {
124  } else {
125  jecUncertainty_->setJetEta(originalJetP4.eta());
126  jecUncertainty_->setJetPt(originalJetP4.pt());
127 
129  }
130  if (verbosity_) {
131  std::cout << "shift = " << shift << std::endl;
132  }
133 
134  if (evt.isRealData() && addResidualJES_) {
135  const static pat::RawJetExtractorT<T> rawJetExtractor{};
136  reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
137  if (rawJetP4.E() > 1.e-1) {
138  reco::Candidate::LorentzVector corrJetP4upToL3 =
140  ? jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_.label(), jetCorrEtaMax_, &rawJetP4)
141  : jetCorrExtractor_(*originalJet, jetCorrUpToL3.product(), jetCorrEtaMax_, &rawJetP4);
142  reco::Candidate::LorentzVector corrJetP4upToL3Res =
144  ? jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_.label(), jetCorrEtaMax_, &rawJetP4)
145  : jetCorrExtractor_(*originalJet, jetCorrUpToL3Res.product(), jetCorrEtaMax_, &rawJetP4);
146  if (corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1) {
147  double residualJES = (corrJetP4upToL3Res.E() / corrJetP4upToL3.E()) - 1.;
148  shift = sqrt(shift * shift + residualJES * residualJES);
149  }
150  }
151  }
152 
153  shift *= shiftBy_;
154  if (verbosity_) {
155  std::cout << "shift*shiftBy = " << shift << std::endl;
156  }
157 
158  T shiftedJet(*originalJet);
159  shiftedJet.setP4((1. + shift) * originalJetP4);
160  if (verbosity_) {
161  std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta()
162  << ", phi = " << shiftedJet.phi() << std::endl;
163  }
164 
165  shiftedJets->push_back(shiftedJet);
166  }
167 
168  evt.put(std::move(shiftedJets));
169  }
170 
172 
175 
181 
183  edm::InputTag jetCorrLabelUpToL3_; // L1+L2+L3 correction
185  edm::InputTag jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
187  double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
188  // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
189  // reported in
190  // https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
191  // https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
192  Textractor jetCorrExtractor_;
193 
195 
196  double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
197 
198  int verbosity_; // flag to enabled/disable debug output
199 };
200 
201 #endif
ShiftedJetProducerT::jecUncertaintyValue_
double jecUncertaintyValue_
Definition: ShiftedJetProducerT.h:194
ShiftedJetProducerT::shiftBy_
double shiftBy_
Definition: ShiftedJetProducerT.h:196
ShiftedJetProducerT::jetCorrLabelUpToL3_
edm::InputTag jetCorrLabelUpToL3_
Definition: ShiftedJetProducerT.h:183
edm::Handle::product
T const * product() const
Definition: Handle.h:70
ESHandle.h
edm::EDGetTokenT< JetCollection >
ShiftedJetProducerT::produce
void produce(edm::Event &evt, const edm::EventSetup &es) override
Definition: ShiftedJetProducerT.h:88
edm
HLT enums.
Definition: AlignableModifier.h:19
ShiftedJetProducerT::jetCorrExtractor_
Textractor jetCorrExtractor_
Definition: ShiftedJetProducerT.h:192
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pat::RawJetExtractorT
Definition: RawJetExtractorT.h:9
EDProducer.h
ShiftedJetProducerT::addResidualJES_
bool addResidualJES_
Definition: ShiftedJetProducerT.h:182
ShiftedJetProducerT::moduleLabel_
std::string moduleLabel_
Definition: ShiftedJetProducerT.h:171
ShiftedJetProducerT::~ShiftedJetProducerT
~ShiftedJetProducerT() override
Definition: ShiftedJetProducerT.h:82
edm::Handle
Definition: AssociativeIterator.h:50
ShiftedJetProducerT::jetCorrLabelUpToL3Res_
edm::InputTag jetCorrLabelUpToL3Res_
Definition: ShiftedJetProducerT.h:185
ShiftedJetProducerT::src_
edm::InputTag src_
Definition: ShiftedJetProducerT.h:173
JetCorrectionUncertainty::setJetEta
void setJetEta(float fEta)
Definition: JetCorrectionUncertainty.cc:204
ShiftedJetProducerT
Definition: ShiftedJetProducerT.h:37
FileInPath.h
ShiftedJetProducerT::JetCollection
std::vector< T > JetCollection
Definition: ShiftedJetProducerT.h:38
JetCorrectionsRecord.h
edm::InputTag::label
std::string const & label() const
Definition: InputTag.h:36
edm::FileInPath
Definition: FileInPath.h:64
JetCorrectionUncertainty.h
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
JetCorrectorParameters.h
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
RawJetExtractorT.h
edm::ESHandle
Definition: DTSurvey.h:22
PATJetCorrExtractor.h
ShiftedJetProducerT::jetCorrInputFileName_
edm::FileInPath jetCorrInputFileName_
Definition: ShiftedJetProducerT.h:176
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
JetCorrector.h
JetCorrectionsRecord
Definition: JetCorrectionsRecord.h:11
JetCorrectionUncertainty::setJetPt
void setJetPt(float fPt)
Definition: JetCorrectionUncertainty.cc:209
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::FileInPath::location
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:161
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
Event.h
ShiftedJetProducerT::jetCorrUncertaintyTag_
std::string jetCorrUncertaintyTag_
Definition: ShiftedJetProducerT.h:178
ShiftedJetProducerT::jecUncertainty_
JetCorrectionUncertainty * jecUncertainty_
Definition: ShiftedJetProducerT.h:180
edm::EventBase::isRealData
bool isRealData() const
Definition: EventBase.h:62
ShiftedJetProducerT::jetCorrParameters_
JetCorrectorParameters * jetCorrParameters_
Definition: ShiftedJetProducerT.h:179
ShiftedJetProducerT::verbosity_
int verbosity_
Definition: ShiftedJetProducerT.h:198
ShiftedJetProducerT::jetCorrEtaMax_
double jetCorrEtaMax_
Definition: ShiftedJetProducerT.h:187
ShiftedJetProducerT::ShiftedJetProducerT
ShiftedJetProducerT(const edm::ParameterSet &cfg)
Definition: ShiftedJetProducerT.h:41
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:132
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:57
get
#define get
JetCorrectionUncertainty
Definition: JetCorrectionUncertainty.h:11
InputTag.h
looper.cfg
cfg
Definition: looper.py:297
JetCorrExtractorT.h
JetCorrectionUncertainty::getUncertainty
float getUncertainty(bool fDirection)
Definition: JetCorrectionUncertainty.cc:83
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::FileInPath::Unknown
Definition: FileInPath.h:66
edm::shift
static unsigned const int shift
Definition: LuminosityBlockID.cc:7
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
JetCorrectorParameters
Definition: JetCorrectorParameters.h:25
ShiftedJetProducerT::jetCorrTokenUpToL3Res_
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3Res_
Definition: ShiftedJetProducerT.h:186
EventSetup.h
cms::Exception
Definition: Exception.h:70
ShiftedJetProducerT::jetCorrPayloadName_
std::string jetCorrPayloadName_
Definition: ShiftedJetProducerT.h:177
ParameterSet.h
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
edm::Event
Definition: Event.h:73
ShiftedJetProducerT::srcToken_
edm::EDGetTokenT< JetCollection > srcToken_
Definition: ShiftedJetProducerT.h:174
edm::InputTag
Definition: InputTag.h:15
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163
ShiftedJetProducerT::jetCorrTokenUpToL3_
edm::EDGetTokenT< reco::JetCorrector > jetCorrTokenUpToL3_
Definition: ShiftedJetProducerT.h:184