CMS 3D CMS Logo

PATJetUpdater.cc
Go to the documentation of this file.
1 //
2 
4 
7 
11 
13 
16 
18 
19 #include <vector>
20 #include <memory>
21 #include <algorithm>
22 
23 using namespace pat;
24 
26  : useUserData_(iConfig.exists("userData")), printWarning_(iConfig.getParameter<bool>("printWarning")) {
27  // initialize configurables
28  jetsToken_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jetSource"));
29  addJetCorrFactors_ = iConfig.getParameter<bool>("addJetCorrFactors");
30  if (addJetCorrFactors_) {
32  iConfig.getParameter<std::vector<edm::InputTag>>("jetCorrFactorsSource"),
33  [this](edm::InputTag const& tag) { return mayConsume<edm::ValueMap<JetCorrFactors>>(tag); });
34  }
35  addBTagInfo_ = iConfig.getParameter<bool>("addBTagInfo");
36  addDiscriminators_ = iConfig.getParameter<bool>("addDiscriminators");
37  discriminatorTags_ = iConfig.getParameter<std::vector<edm::InputTag>>("discriminatorSources");
39  return mayConsume<reco::JetFloatAssociation::Container>(tag);
40  });
41  addTagInfos_ = iConfig.getParameter<bool>("addTagInfos");
42  tagInfoTags_ = iConfig.getParameter<std::vector<edm::InputTag>>("tagInfoSources");
44  tagInfoTags_, [this](edm::InputTag const& tag) { return mayConsume<edm::View<reco::BaseTagInfo>>(tag); });
45  if (discriminatorTags_.empty()) {
46  addDiscriminators_ = false;
47  } else {
48  for (std::vector<edm::InputTag>::const_iterator it = discriminatorTags_.begin(), ed = discriminatorTags_.end();
49  it != ed;
50  ++it) {
51  std::string label = it->label();
52  std::string::size_type pos = label.find("JetTags");
53  if ((pos != std::string::npos) && (pos != label.length() - 7)) {
54  label.erase(pos + 7); // trim a tail after "JetTags"
55  }
56  if (!it->instance().empty()) {
57  label = (label + std::string(":") + it->instance());
58  }
59  discriminatorLabels_.push_back(label);
60  }
61  }
62  if (tagInfoTags_.empty()) {
63  addTagInfos_ = false;
64  } else {
65  for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed;
66  ++it) {
67  std::string label = it->label();
68  std::string::size_type pos = label.find("TagInfos");
69  if ((pos != std::string::npos) && (pos != label.length() - 8)) {
70  label.erase(pos + 8); // trim a tail after "TagInfos"
71  }
72  tagInfoLabels_.push_back(label);
73  }
74  }
75  if (!addBTagInfo_) {
76  addDiscriminators_ = false;
77  addTagInfos_ = false;
78  }
79  // Check to see if the user wants to add user data
80  if (useUserData_) {
81  userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
82  }
83  // produces vector of jets
84  produces<std::vector<Jet>>();
85  produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
86 }
87 
89 
91  // Get the vector of jets
93  iEvent.getByToken(jetsToken_, jets);
94 
95  // read in the jet correction factors ValueMap
96  std::vector<edm::ValueMap<JetCorrFactors>> jetCorrs;
97  if (addJetCorrFactors_) {
98  for (size_t i = 0; i < jetCorrFactorsTokens_.size(); ++i) {
100  iEvent.getByToken(jetCorrFactorsTokens_[i], jetCorr);
101  jetCorrs.push_back(*jetCorr);
102  }
103  }
104 
105  // Get the vector of jet tags with b-tagging info
106  std::vector<edm::Handle<reco::JetFloatAssociation::Container>> jetDiscriminators;
108  jetDiscriminators.resize(discriminatorTokens_.size());
109  for (size_t i = 0; i < discriminatorTokens_.size(); ++i) {
110  iEvent.getByToken(discriminatorTokens_[i], jetDiscriminators[i]);
111  }
112  }
113  std::vector<edm::Handle<edm::View<reco::BaseTagInfo>>> jetTagInfos;
114  if (addBTagInfo_ && addTagInfos_) {
115  jetTagInfos.resize(tagInfoTokens_.size());
116  for (size_t i = 0; i < tagInfoTokens_.size(); ++i) {
117  iEvent.getByToken(tagInfoTokens_[i], jetTagInfos[i]);
118  }
119  }
120 
121  // loop over jets
122  auto patJets = std::make_unique<std::vector<Jet>>();
123 
124  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
125 
127  iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
128 
129  for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
130  // construct the Jet from the ref -> save ref to original object
131  unsigned int idx = itJet - jets->begin();
132  const edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
133  const edm::RefToBase<Jet> patJetRef(jetRef.castTo<JetRef>());
134  Jet ajet(patJetRef);
135 
136  if (addJetCorrFactors_) {
137  // undo previous jet energy corrections
138  ajet.setP4(ajet.correctedP4(0));
139  // clear previous JetCorrFactors
140  ajet.jec_.clear();
141  // add additional JetCorrs to the jet
142  for (unsigned int i = 0; i < jetCorrFactorsTokens_.size(); ++i) {
143  const JetCorrFactors& jcf = jetCorrs[i][jetRef];
144  // uncomment for debugging
145  // jcf.print();
146  ajet.addJECFactors(jcf);
147  }
148  std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
149  if (std::find(levels.begin(), levels.end(), "L2L3Residual") != levels.end()) {
150  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"));
151  } else if (std::find(levels.begin(), levels.end(), "L3Absolute") != levels.end()) {
152  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"));
153  } else {
154  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
155  if (printWarning_) {
156  edm::LogWarning("L3Absolute not found")
157  << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
158  << "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
159  << " uncorrected.";
160  printWarning_ = false;
161  }
162  }
163  }
164 
165  // add b-tag info if available & required
166  if (addBTagInfo_) {
167  if (addDiscriminators_) {
168  for (size_t k = 0; k < jetDiscriminators.size(); ++k) {
169  float value = (*jetDiscriminators[k])[jetRef];
170  ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
171  }
172  }
173  if (addTagInfos_) {
174  for (size_t k = 0; k < jetTagInfos.size(); ++k) {
175  const edm::View<reco::BaseTagInfo>& taginfos = *jetTagInfos[k];
176  // This is not associative, so we have to search the jet
178  // Try first by 'same index'
179  if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
180  match = taginfos.ptrAt(idx);
181  } else {
182  // otherwise fail back to a simple search
183  for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end();
184  itTI != edTI;
185  ++itTI) {
186  if (itTI->jet() == jetRef) {
187  match = taginfos.ptrAt(itTI - taginfos.begin());
188  break;
189  }
190  }
191  }
192  if (match.isNonnull()) {
193  tagInfosOut->push_back(match->clone());
194  // set the "forward" ptr to the thinned collection
195  edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr(
196  h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1);
197  // set the "backward" ptr to the original collection for association
198  const edm::Ptr<reco::BaseTagInfo>& tagInfoBackPtr(match);
199  // make FwdPtr
200  TagInfoFwdPtrCollection::value_type tagInfoFwdPtr(tagInfoForwardPtr, tagInfoBackPtr);
201  ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr);
202  }
203  }
204  }
205  }
206 
207  if (useUserData_) {
208  userDataHelper_.add(ajet, iEvent, iSetup);
209  }
210 
211  // reassign the original object reference to preserve reference to the original jet the input PAT jet was derived from
212  // (this needs to be done at the end since cloning the input PAT jet would interfere with adding UserData)
213  ajet.refToOrig_ = patJetRef->originalObjectRef();
214 
215  patJets->push_back(ajet);
216  }
217 
218  // sort jets in pt
219  std::sort(patJets->begin(), patJets->end(), pTComparator_);
220 
221  // put genEvt in Event
222  iEvent.put(std::move(patJets));
223 
224  iEvent.put(std::move(tagInfosOut), "tagInfos");
225 }
226 
227 // ParameterSet description for module
230  iDesc.setComment("PAT jet producer module");
231 
232  // input source
233  iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
234 
235  // tag info
236  iDesc.add<bool>("addTagInfos", true);
237  std::vector<edm::InputTag> emptyVInputTags;
238  iDesc.add<std::vector<edm::InputTag>>("tagInfoSources", emptyVInputTags);
239 
240  // jet energy corrections
241  iDesc.add<bool>("addJetCorrFactors", true);
242  iDesc.add<std::vector<edm::InputTag>>("jetCorrFactorsSource", emptyVInputTags);
243 
244  // btag discriminator tags
245  iDesc.add<bool>("addBTagInfo", true);
246  iDesc.add<bool>("addDiscriminators", true);
247  iDesc.add<std::vector<edm::InputTag>>("discriminatorSources", emptyVInputTags);
248 
249  // silent warning if false
250  iDesc.add<bool>("printWarning", true);
251 
252  // Check to see if the user wants to add user data
253  edm::ParameterSetDescription userDataPSet;
255  iDesc.addOptional("userData", userDataPSet);
256 
257  descriptions.add("PATJetUpdater", iDesc);
258 }
259 
261 
ConfigurationDescriptions.h
edm::RefProd
Definition: EDProductfwd.h:25
pat::Jet::addBDiscriminatorPair
void addBDiscriminatorPair(const std::pair< std::string, float > &thePair)
method to add a algolabel-discriminator pair
edm::View::begin
const_iterator begin() const
electrons_cff.bool
bool
Definition: electrons_cff.py:372
pat::PATUserDataHelper
Assists in assimilating all pat::UserData into pat objects.
Definition: PATUserDataHelper.h:49
mps_fire.i
i
Definition: mps_fire.py:355
pat::Jet::initializeJEC
void initializeJEC(unsigned int level, const JetCorrFactors::Flavor &flavor=JetCorrFactors::NONE, unsigned int set=0)
initialize the jet to a given JEC level during creation starting from Uncorrected
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
MessageLogger.h
pat::PATJetUpdater::tagInfoTokens_
std::vector< edm::EDGetTokenT< edm::View< reco::BaseTagInfo > > > tagInfoTokens_
Definition: PATJetUpdater.h:55
pat::PATJetUpdater::discriminatorLabels_
std::vector< std::string > discriminatorLabels_
Definition: PATJetUpdater.h:52
pat::PATUserDataHelper::fillDescription
static void fillDescription(edm::ParameterSetDescription &iDesc)
Definition: PATUserDataHelper.h:135
pat::PATJetUpdater::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PATJetUpdater.cc:228
pos
Definition: PixelAliasList.h:18
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
pat::PATJetUpdater::discriminatorTokens_
std::vector< edm::EDGetTokenT< reco::JetFloatAssociation::Container > > discriminatorTokens_
Definition: PATJetUpdater.h:51
singleTopDQM_cfi.jets
jets
Definition: singleTopDQM_cfi.py:42
Association.h
pat::Jet::jec_
std::vector< pat::JetCorrFactors > jec_
Definition: Jet.h:622
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::Handle
Definition: AssociativeIterator.h:50
training_settings.idx
idx
Definition: training_settings.py:16
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
edm::Ref
Definition: AssociativeIterator.h:58
FileInPath.h
pat::PATJetUpdater::pTComparator_
GreaterByPt< Jet > pTComparator_
Definition: PATJetUpdater.h:58
pat::JetCorrFactors
Class for the storage of jet correction factors.
Definition: JetCorrFactors.h:37
MakerMacros.h
PATJetUpdater.h
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Jet
Definition: Jet.py:1
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
pat::PATObject::refToOrig_
edm::Ptr< reco::Candidate > refToOrig_
Definition: PATObject.h:463
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
pat::PATUserDataHelper::add
void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup)
Definition: PATUserDataHelper.h:114
pat::Jet::addJECFactors
void addJECFactors(const JetCorrFactors &jec)
add more sets of energy correction factors
Definition: Jet.h:191
pat::PATJetUpdater::addJetCorrFactors_
bool addJetCorrFactors_
Definition: PATJetUpdater.h:45
dqmdumpme.k
k
Definition: dqmdumpme.py:60
ParameterSetDescription.h
pat::PATJetUpdater::addDiscriminators_
bool addDiscriminators_
Definition: PATJetUpdater.h:49
pat::PATJetUpdater::PATJetUpdater
PATJetUpdater(const edm::ParameterSet &iConfig)
Definition: PATJetUpdater.cc:25
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::vector_transform
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
edm::View::size
size_type size() const
edm::LogWarning
Definition: MessageLogger.h:141
edm::View
Definition: CaloClusterFwd.h:14
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
CandAssociation.h
pat::PATJetUpdater::printWarning_
bool printWarning_
Definition: PATJetUpdater.h:63
match
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
iEvent
int iEvent
Definition: GenABIO.cc:224
pat::PATJetUpdater::jetCorrFactorsTokens_
std::vector< edm::EDGetTokenT< edm::ValueMap< JetCorrFactors > > > jetCorrFactorsTokens_
Definition: PATJetUpdater.h:46
value
Definition: value.py:1
edm::EventSetup
Definition: EventSetup.h:57
pat::Jet::correctedP4
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:164
pat
Definition: HeavyIon.h:7
edm::RefToBase::castTo
REF castTo() const
Definition: RefToBase.h:257
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
hemisphereProducer_cfi.patJets
patJets
Definition: hemisphereProducer_cfi.py:5
pat::PATJetUpdater::tagInfoLabels_
std::vector< std::string > tagInfoLabels_
Definition: PATJetUpdater.h:56
pat::PATJetUpdater::discriminatorTags_
std::vector< edm::InputTag > discriminatorTags_
Definition: PATJetUpdater.h:50
pat::PATJetUpdater::userDataHelper_
pat::PATUserDataHelper< pat::Jet > userDataHelper_
Definition: PATJetUpdater.h:61
edm::Ptr
Definition: AssociationVector.h:31
ValueMap.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
reco::LeafCandidate::setP4
void setP4(const LorentzVector &p4) final
set 4-momentum
Definition: LeafCandidate.h:158
eostools.move
def move(src, dest)
Definition: eostools.py:511
pat::PATJetUpdater::produce
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: PATJetUpdater.cc:90
pat::PATJetUpdater::jetsToken_
edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_
Definition: PATJetUpdater.h:44
metsig::jet
Definition: SignAlgoResolutions.h:47
transform.h
pat::PATJetUpdater::useUserData_
bool useUserData_
Definition: PATJetUpdater.h:60
pat::PATJetUpdater::tagInfoTags_
std::vector< edm::InputTag > tagInfoTags_
Definition: PATJetUpdater.h:54
pat::Jet::addTagInfo
void addTagInfo(const std::string &label, const TagInfoFwdPtrCollection::value_type &info)
pat::PATJetUpdater::addTagInfos_
bool addTagInfos_
Definition: PATJetUpdater.h:53
edm::RefToBase< reco::Jet >
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
JetCorrFactors.h
edm::View::end
const_iterator end() const
pat::PATJetUpdater
Produces pat::Jet's.
Definition: PATJetUpdater.h:33
edm::Event
Definition: Event.h:73
edm::View::ptrAt
Ptr< value_type > ptrAt(size_type i) const
pat::PATJetUpdater::~PATJetUpdater
~PATJetUpdater() override
Definition: PATJetUpdater.cc:88
edm::InputTag
Definition: InputTag.h:15
label
const char * label
Definition: PFTauDecayModeTools.cc:11
jets_cff.levels
levels
Definition: jets_cff.py:23
edm::OwnVector< reco::BaseTagInfo >
pat::PATJetUpdater::addBTagInfo_
bool addBTagInfo_
Definition: PATJetUpdater.h:48