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  sort_ = iConfig.getParameter<bool>("sort");
30  addJetCorrFactors_ = iConfig.getParameter<bool>("addJetCorrFactors");
31  if (addJetCorrFactors_) {
33  iConfig.getParameter<std::vector<edm::InputTag>>("jetCorrFactorsSource"),
34  [this](edm::InputTag const& tag) { return mayConsume<edm::ValueMap<JetCorrFactors>>(tag); });
35  }
36  addBTagInfo_ = iConfig.getParameter<bool>("addBTagInfo");
37  addDiscriminators_ = iConfig.getParameter<bool>("addDiscriminators");
38  discriminatorTags_ = iConfig.getParameter<std::vector<edm::InputTag>>("discriminatorSources");
40  return mayConsume<reco::JetFloatAssociation::Container>(tag);
41  });
42  addTagInfos_ = iConfig.getParameter<bool>("addTagInfos");
43  tagInfoTags_ = iConfig.getParameter<std::vector<edm::InputTag>>("tagInfoSources");
45  tagInfoTags_, [this](edm::InputTag const& tag) { return mayConsume<edm::View<reco::BaseTagInfo>>(tag); });
46  if (discriminatorTags_.empty()) {
47  addDiscriminators_ = false;
48  } else {
49  for (std::vector<edm::InputTag>::const_iterator it = discriminatorTags_.begin(), ed = discriminatorTags_.end();
50  it != ed;
51  ++it) {
52  std::string label = it->label();
53  std::string::size_type pos = label.find("JetTags");
54  if ((pos != std::string::npos) && (pos != label.length() - 7)) {
55  label.erase(pos + 7); // trim a tail after "JetTags"
56  }
57  if (!it->instance().empty()) {
58  label = (label + std::string(":") + it->instance());
59  }
60  discriminatorLabels_.push_back(label);
61  }
62  }
63  if (tagInfoTags_.empty()) {
64  addTagInfos_ = false;
65  } else {
66  for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed;
67  ++it) {
68  std::string label = it->label();
69  std::string::size_type pos = label.find("TagInfos");
70  if ((pos != std::string::npos) && (pos != label.length() - 8)) {
71  label.erase(pos + 8); // trim a tail after "TagInfos"
72  }
73  tagInfoLabels_.push_back(label);
74  }
75  }
76  if (!addBTagInfo_) {
77  addDiscriminators_ = false;
78  addTagInfos_ = false;
79  }
80  // Check to see if the user wants to add user data
81  if (useUserData_) {
82  userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
83  }
84  // produces vector of jets
85  produces<std::vector<Jet>>();
86  produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
87 }
88 
90 
92  // Get the vector of jets
94  iEvent.getByToken(jetsToken_, jets);
95 
96  // read in the jet correction factors ValueMap
97  std::vector<edm::ValueMap<JetCorrFactors>> jetCorrs;
98  if (addJetCorrFactors_) {
99  for (size_t i = 0; i < jetCorrFactorsTokens_.size(); ++i) {
101  iEvent.getByToken(jetCorrFactorsTokens_[i], jetCorr);
102  jetCorrs.push_back(*jetCorr);
103  }
104  }
105 
106  // Get the vector of jet tags with b-tagging info
107  std::vector<edm::Handle<reco::JetFloatAssociation::Container>> jetDiscriminators;
109  jetDiscriminators.resize(discriminatorTokens_.size());
110  for (size_t i = 0; i < discriminatorTokens_.size(); ++i) {
111  iEvent.getByToken(discriminatorTokens_[i], jetDiscriminators[i]);
112  }
113  }
114  std::vector<edm::Handle<edm::View<reco::BaseTagInfo>>> jetTagInfos;
115  if (addBTagInfo_ && addTagInfos_) {
116  jetTagInfos.resize(tagInfoTokens_.size());
117  for (size_t i = 0; i < tagInfoTokens_.size(); ++i) {
118  iEvent.getByToken(tagInfoTokens_[i], jetTagInfos[i]);
119  }
120  }
121 
122  // loop over jets
123  auto patJets = std::make_unique<std::vector<Jet>>();
124 
125  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
126 
128  iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
129 
130  for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
131  // construct the Jet from the ref -> save ref to original object
132  unsigned int idx = itJet - jets->begin();
133  const edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
134  const edm::RefToBase<Jet> patJetRef(jetRef.castTo<JetRef>());
135  Jet ajet(patJetRef);
136 
137  if (addJetCorrFactors_) {
138  // undo previous jet energy corrections
139  ajet.setP4(ajet.correctedP4(0));
140  // clear previous JetCorrFactors
141  ajet.jec_.clear();
142  // add additional JetCorrs to the jet
143  for (unsigned int i = 0; i < jetCorrFactorsTokens_.size(); ++i) {
144  const JetCorrFactors& jcf = jetCorrs[i][jetRef];
145  // uncomment for debugging
146  // jcf.print();
147  ajet.addJECFactors(jcf);
148  }
149  std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
150  if (std::find(levels.begin(), levels.end(), "L2L3Residual") != levels.end()) {
151  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"));
152  } else if (std::find(levels.begin(), levels.end(), "L3Absolute") != levels.end()) {
153  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"));
154  } else {
155  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
156  if (printWarning_) {
157  edm::LogWarning("L3Absolute not found")
158  << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
159  << "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
160  << " uncorrected.";
161  printWarning_ = false;
162  }
163  }
164  }
165 
166  // add b-tag info if available & required
167  if (addBTagInfo_) {
168  if (addDiscriminators_) {
169  for (size_t k = 0; k < jetDiscriminators.size(); ++k) {
170  float value = (*jetDiscriminators[k])[jetRef];
171  ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
172  }
173  }
174  if (addTagInfos_) {
175  for (size_t k = 0; k < jetTagInfos.size(); ++k) {
176  const edm::View<reco::BaseTagInfo>& taginfos = *jetTagInfos[k];
177  // This is not associative, so we have to search the jet
179  // Try first by 'same index'
180  if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
181  match = taginfos.ptrAt(idx);
182  } else {
183  // otherwise fail back to a simple search
184  for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end();
185  itTI != edTI;
186  ++itTI) {
187  if (itTI->jet() == jetRef) {
188  match = taginfos.ptrAt(itTI - taginfos.begin());
189  break;
190  }
191  }
192  }
193  if (match.isNonnull()) {
194  tagInfosOut->push_back(match->clone());
195  // set the "forward" ptr to the thinned collection
196  edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr(
197  h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1);
198  // set the "backward" ptr to the original collection for association
199  const edm::Ptr<reco::BaseTagInfo>& tagInfoBackPtr(match);
200  // make FwdPtr
201  TagInfoFwdPtrCollection::value_type tagInfoFwdPtr(tagInfoForwardPtr, tagInfoBackPtr);
202  ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr);
203  }
204  }
205  }
206  }
207 
208  if (useUserData_) {
209  userDataHelper_.add(ajet, iEvent, iSetup);
210  }
211 
212  // reassign the original object reference to preserve reference to the original jet the input PAT jet was derived from
213  // (this needs to be done at the end since cloning the input PAT jet would interfere with adding UserData)
214  ajet.refToOrig_ = patJetRef->originalObjectRef();
215 
216  patJets->push_back(ajet);
217  }
218 
219  // sort jets in pt
220  if (sort_) {
221  std::sort(patJets->begin(), patJets->end(), pTComparator_);
222  }
223 
224  // put genEvt in Event
225  iEvent.put(std::move(patJets));
226 
227  iEvent.put(std::move(tagInfosOut), "tagInfos");
228 }
229 
230 // ParameterSet description for module
233  iDesc.setComment("PAT jet producer module");
234 
235  // input source
236  iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
237 
238  // sort inputs (by pt)
239  iDesc.add<bool>("sort", true);
240 
241  // tag info
242  iDesc.add<bool>("addTagInfos", true);
243  std::vector<edm::InputTag> emptyVInputTags;
244  iDesc.add<std::vector<edm::InputTag>>("tagInfoSources", emptyVInputTags);
245 
246  // jet energy corrections
247  iDesc.add<bool>("addJetCorrFactors", true);
248  iDesc.add<std::vector<edm::InputTag>>("jetCorrFactorsSource", emptyVInputTags);
249 
250  // btag discriminator tags
251  iDesc.add<bool>("addBTagInfo", true);
252  iDesc.add<bool>("addDiscriminators", true);
253  iDesc.add<std::vector<edm::InputTag>>("discriminatorSources", emptyVInputTags);
254 
255  // silent warning if false
256  iDesc.add<bool>("printWarning", true);
257 
258  // Check to see if the user wants to add user data
259  edm::ParameterSetDescription userDataPSet;
261  iDesc.addOptional("userData", userDataPSet);
262 
263  descriptions.add("PATJetUpdater", iDesc);
264 }
265 
267 
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:366
pat::PATUserDataHelper
Assists in assimilating all pat::UserData into pat objects.
Definition: PATUserDataHelper.h:49
mps_fire.i
i
Definition: mps_fire.py:428
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:56
pat::PATJetUpdater::discriminatorLabels_
std::vector< std::string > discriminatorLabels_
Definition: PATJetUpdater.h:53
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:231
pos
Definition: PixelAliasList.h:18
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
pat::PATJetUpdater::discriminatorTokens_
std::vector< edm::EDGetTokenT< reco::JetFloatAssociation::Container > > discriminatorTokens_
Definition: PATJetUpdater.h:52
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
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
edm::Ref
Definition: AssociativeIterator.h:58
FileInPath.h
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
pat::PATJetUpdater::pTComparator_
GreaterByPt< Jet > pTComparator_
Definition: PATJetUpdater.h:59
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:458
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::PATJetUpdater::sort_
bool sort_
Definition: PATJetUpdater.h:45
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:46
dqmdumpme.k
k
Definition: dqmdumpme.py:60
ParameterSetDescription.h
pat::PATJetUpdater::addDiscriminators_
bool addDiscriminators_
Definition: PATJetUpdater.h:50
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::View
Definition: CaloClusterFwd.h:14
edm::ParameterSet
Definition: ParameterSet.h:47
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
CandAssociation.h
pat::PATJetUpdater::printWarning_
bool printWarning_
Definition: PATJetUpdater.h:64
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
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:47
value
Definition: value.py:1
edm::EventSetup
Definition: EventSetup.h:58
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:57
pat::PATJetUpdater::discriminatorTags_
std::vector< edm::InputTag > discriminatorTags_
Definition: PATJetUpdater.h:51
pat::PATJetUpdater::userDataHelper_
pat::PATUserDataHelper< pat::Jet > userDataHelper_
Definition: PATJetUpdater.h:62
edm::Ptr
Definition: AssociationVector.h:31
ValueMap.h
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:91
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:61
pat::PATJetUpdater::tagInfoTags_
std::vector< edm::InputTag > tagInfoTags_
Definition: PATJetUpdater.h:55
pat::Jet::addTagInfo
void addTagInfo(const std::string &label, const TagInfoFwdPtrCollection::value_type &info)
pat::PATJetUpdater::addTagInfos_
bool addTagInfos_
Definition: PATJetUpdater.h:54
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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:89
edm::InputTag
Definition: InputTag.h:15
label
const char * label
Definition: PFTauDecayModeTools.cc:11
jets_cff.levels
levels
Definition: jets_cff.py:21
edm::OwnVector< reco::BaseTagInfo >
pat::PATJetUpdater::addBTagInfo_
bool addBTagInfo_
Definition: PATJetUpdater.h:49