CMS 3D CMS Logo

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