CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ShiftedPFCandidateProducerForPFNoPUMEt.cc
Go to the documentation of this file.
1 
36 
37 #include <string>
38 #include <vector>
39 
41 public:
43 
44 private:
45  void produce(edm::Event&, const edm::EventSetup&) override;
46 
48 
51 
56  std::unique_ptr<JetCorrectorParameters> jetCorrParameters_;
57  std::unique_ptr<JetCorrectionUncertainty> jecUncertainty_;
58 
60 
61  double minJetPt_;
62 
63  double shiftBy_;
64 
66 };
67 
68 namespace {
69  constexpr double dR2Match = 0.01 * 0.01;
70 }
71 
73  : srcPFCandidatesToken_(consumes<reco::PFCandidateCollection>(cfg.getParameter<edm::InputTag>("srcPFCandidates"))),
74  srcJetsToken_(consumes<reco::PFJetCollection>(cfg.getParameter<edm::InputTag>("srcJets"))) {
75  jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
76 
77  jecValidFileName_ = cfg.exists("jetCorrInputFileName");
78  if (jecValidFileName_) {
79  jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
81  throw cms::Exception("ShiftedJetProducerT")
82  << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
83  edm::LogInfo("ShiftedPFCandidateProducerForPFNoPUMEt")
84  << "Reading JEC parameters = " << jetCorrUncertaintyTag_ << " from file = " << jetCorrInputFileName_.fullPath()
85  << "." << std::endl;
87  std::make_unique<JetCorrectorParameters>(jetCorrInputFileName_.fullPath(), jetCorrUncertaintyTag_);
88  jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(*jetCorrParameters_);
89  } else {
90  edm::LogInfo("ShiftedPFCandidateProducerForPFNoPUMEt")
91  << "Reading JEC parameters = " << jetCorrUncertaintyTag_ << " from DB/SQLlite file." << std::endl;
92  jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
94  }
95 
96  minJetPt_ = cfg.getParameter<double>("minJetPt");
97 
98  shiftBy_ = cfg.getParameter<double>("shiftBy");
99 
100  unclEnUncertainty_ = cfg.getParameter<double>("unclEnUncertainty");
101 
102  produces<reco::PFCandidateCollection>();
103 }
104 
106  edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
107  evt.getByToken(srcPFCandidatesToken_, originalPFCandidates);
108 
110  evt.getByToken(srcJetsToken_, jets);
111 
112  std::vector<const reco::PFJet*> selectedJets;
113  for (reco::PFJetCollection::const_iterator jet = jets->begin(); jet != jets->end(); ++jet) {
114  if (jet->pt() > minJetPt_)
115  selectedJets.push_back(&(*jet));
116  }
117 
118  if (!jetCorrPayloadName_.empty()) {
119  JetCorrectorParametersCollection const& jetCorrParameterSet = es.getData(jetCorrPayloadToken_);
120  const JetCorrectorParameters& jetCorrParameters = (jetCorrParameterSet)[jetCorrUncertaintyTag_];
121  jecUncertainty_ = std::make_unique<JetCorrectionUncertainty>(jetCorrParameters);
122  }
123 
124  auto shiftedPFCandidates = std::make_unique<reco::PFCandidateCollection>();
125 
126  for (reco::PFCandidateCollection::const_iterator originalPFCandidate = originalPFCandidates->begin();
127  originalPFCandidate != originalPFCandidates->end();
128  ++originalPFCandidate) {
129  const reco::PFJet* jet_matched = nullptr;
130  for (auto jet : selectedJets) {
131  for (const auto& jetc : jet->getPFConstituents()) {
132  if (deltaR2(originalPFCandidate->p4(), jetc->p4()) < dR2Match) {
133  jet_matched = jet;
134  break;
135  }
136  }
137  if (jet_matched)
138  break;
139  }
140 
141  double shift = 0.;
142  if (jet_matched != nullptr) {
143  jecUncertainty_->setJetEta(jet_matched->eta());
144  jecUncertainty_->setJetPt(jet_matched->pt());
145 
146  shift = jecUncertainty_->getUncertainty(true);
147  } else {
148  shift = unclEnUncertainty_;
149  }
150 
151  shift *= shiftBy_;
152 
153  reco::Candidate::LorentzVector shiftedPFCandidateP4 = originalPFCandidate->p4();
154  shiftedPFCandidateP4 *= (1. + shift);
155 
156  reco::PFCandidate shiftedPFCandidate(*originalPFCandidate);
157  shiftedPFCandidate.setP4(shiftedPFCandidateP4);
158 
159  shiftedPFCandidates->push_back(shiftedPFCandidate);
160  }
161 
162  evt.put(std::move(shiftedPFCandidates));
163 }
164 
166 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
tuple cfg
Definition: looper.py:296
double pt() const final
transverse momentum
edm::EDGetTokenT< reco::PFCandidateCollection > srcPFCandidatesToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:57
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< reco::PFJetCollection > srcJetsToken_
Jets made from PFObjects.
Definition: PFJet.h:20
bool getData(T &iHolder) const
Definition: EventSetup.h:128
vector< PseudoJet > jets
edm::ESGetToken< JetCorrectorParametersCollection, JetCorrectionsRecord > jetCorrPayloadToken_
def move
Definition: eostools.py:511
LocationCode location() const
Where was the file found?
Definition: FileInPath.cc:159
std::unique_ptr< JetCorrectorParameters > jetCorrParameters_
Log< level::Info, false > LogInfo
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
std::unique_ptr< JetCorrectionUncertainty > jecUncertainty_
std::vector< PFJet > PFJetCollection
collection of PFJet objects
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
void produce(edm::Event &, const edm::EventSetup &) override
static unsigned int const shift
std::string fullPath() const
Definition: FileInPath.cc:161
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
void setP4(const LorentzVector &p4) final
set 4-momentum
double eta() const final
momentum pseudorapidity