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_(iConfig.getParameter<bool>("printWarning"))
30 {
31  // initialize configurables
32  jetsToken_ = consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>( "jetSource" ));
33  sort_ = iConfig.getParameter<bool>( "sort" );
34  addJetCorrFactors_ = iConfig.getParameter<bool>( "addJetCorrFactors" );
35  if( addJetCorrFactors_ ) {
36  jetCorrFactorsTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "jetCorrFactorsSource" ), [this](edm::InputTag const & tag){return mayConsume<edm::ValueMap<JetCorrFactors> >(tag);});
37  }
38  addBTagInfo_ = iConfig.getParameter<bool>( "addBTagInfo" );
39  addDiscriminators_ = iConfig.getParameter<bool>( "addDiscriminators" );
40  discriminatorTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "discriminatorSources" );
41  discriminatorTokens_ = edm::vector_transform(discriminatorTags_, [this](edm::InputTag const & tag){return mayConsume<reco::JetFloatAssociation::Container>(tag);});
42  addTagInfos_ = iConfig.getParameter<bool>( "addTagInfos" );
43  tagInfoTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "tagInfoSources" );
44  tagInfoTokens_ =edm::vector_transform(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(); it != ed; ++it) {
49  std::string label = it->label();
50  std::string::size_type pos = label.find("JetTags");
51  if ((pos != std::string::npos) && (pos != label.length() - 7)) {
52  label.erase(pos+7); // trim a tail after "JetTags"
53  }
54  if(!it->instance().empty()) {
55  label = (label+std::string(":")+it->instance());
56  }
57  discriminatorLabels_.push_back(label);
58  }
59  }
60  if (tagInfoTags_.empty()) {
61  addTagInfos_ = false;
62  } else {
63  for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed; ++it) {
64  std::string label = it->label();
65  std::string::size_type pos = label.find("TagInfos");
66  if ((pos != std::string::npos) && (pos != label.length() - 8)) {
67  label.erase(pos+8); // trim a tail after "TagInfos"
68  }
69  tagInfoLabels_.push_back(label);
70  }
71  }
72  if (!addBTagInfo_) { addDiscriminators_ = false; addTagInfos_ = false; }
73  // Check to see if the user wants to add user data
74  if ( useUserData_ ) {
75  userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
76  }
77  // produces vector of jets
78  produces<std::vector<Jet> >();
79  produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
80 }
81 
82 
84 
85 }
86 
87 
89 {
90  // Get the vector of jets
92  iEvent.getByToken(jetsToken_, jets);
93 
94  // read in the jet correction factors ValueMap
95  std::vector<edm::ValueMap<JetCorrFactors> > jetCorrs;
96  if (addJetCorrFactors_) {
97  for ( size_t i = 0; i < jetCorrFactorsTokens_.size(); ++i ) {
99  iEvent.getByToken(jetCorrFactorsTokens_[i], jetCorr);
100  jetCorrs.push_back( *jetCorr );
101  }
102  }
103 
104  // Get the vector of jet tags with b-tagging info
105  std::vector<edm::Handle<reco::JetFloatAssociation::Container> > jetDiscriminators;
107  jetDiscriminators.resize(discriminatorTokens_.size());
108  for (size_t i = 0; i < discriminatorTokens_.size(); ++i) {
109  iEvent.getByToken(discriminatorTokens_[i], jetDiscriminators[i]);
110  }
111  }
112  std::vector<edm::Handle<edm::View<reco::BaseTagInfo> > > jetTagInfos;
113  if (addBTagInfo_ && addTagInfos_) {
114  jetTagInfos.resize(tagInfoTokens_.size());
115  for (size_t i = 0; i < tagInfoTokens_.size(); ++i) {
116  iEvent.getByToken(tagInfoTokens_[i], jetTagInfos[i]);
117  }
118  }
119 
120  // loop over jets
121  auto patJets = std::make_unique<std::vector<Jet>>();
122 
123  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
124 
126 
127  for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
128 
129  // construct the Jet from the ref -> save ref to original object
130  unsigned int idx = itJet - jets->begin();
131  const edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
132  const edm::RefToBase<Jet> patJetRef(jetRef.castTo<JetRef>());
133  Jet ajet( patJetRef );
134 
135  if (addJetCorrFactors_) {
136  // undo previous jet energy corrections
137  ajet.setP4(ajet.correctedP4(0));
138  // clear previous JetCorrFactors
139  ajet.jec_.clear();
140  // add additional JetCorrs to the jet
141  for ( unsigned int i=0; i<jetCorrFactorsTokens_.size(); ++i ) {
142  const JetCorrFactors& jcf = jetCorrs[i][jetRef];
143  // uncomment for debugging
144  // jcf.print();
145  ajet.addJECFactors(jcf);
146  }
147  std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
148  if(std::find(levels.begin(), levels.end(), "L2L3Residual")!=levels.end()){
149  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"));
150  }
151  else if(std::find(levels.begin(), levels.end(), "L3Absolute")!=levels.end()){
152  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"));
153  }
154  else{
155  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
156  if(printWarning_){
157  edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
158  << "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
159  << " uncorrected."; printWarning_=false;
160  }
161  }
162  }
163 
164  // add b-tag info if available & required
165  if (addBTagInfo_) {
166  if (addDiscriminators_) {
167  for (size_t k=0; k<jetDiscriminators.size(); ++k) {
168  float value = (*jetDiscriminators[k])[jetRef];
169  ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
170  }
171  }
172  if (addTagInfos_) {
173  for (size_t k=0; k<jetTagInfos.size(); ++k) {
174  const edm::View<reco::BaseTagInfo> & taginfos = *jetTagInfos[k];
175  // This is not associative, so we have to search the jet
177  // Try first by 'same index'
178  if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
179  match = taginfos.ptrAt(idx);
180  } else {
181  // otherwise fail back to a simple search
182  for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end(); itTI != edTI; ++itTI) {
183  if (itTI->jet() == jetRef) { match = taginfos.ptrAt( itTI - taginfos.begin() ); break; }
184  }
185  }
186  if (match.isNonnull()) {
187  tagInfosOut->push_back( match->clone() );
188  // set the "forward" ptr to the thinned collection
189  edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr ( h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1 );
190  // set the "backward" ptr to the original collection for association
191  const edm::Ptr<reco::BaseTagInfo>& tagInfoBackPtr ( match );
192  // make FwdPtr
193  TagInfoFwdPtrCollection::value_type tagInfoFwdPtr( tagInfoForwardPtr, tagInfoBackPtr ) ;
194  ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr );
195  }
196  }
197  }
198  }
199 
200  if ( useUserData_ ) {
201  userDataHelper_.add( ajet, iEvent, iSetup );
202  }
203 
204  // reassign the original object reference to preserve reference to the original jet the input PAT jet was derived from
205  // (this needs to be done at the end since cloning the input PAT jet would interfere with adding UserData)
206  ajet.refToOrig_ = patJetRef->originalObjectRef();
207 
208  patJets->push_back(ajet);
209  }
210 
211  // sort jets in pt
212  if (sort_) {
213  std::sort(patJets->begin(), patJets->end(), pTComparator_);
214  }
215 
216  // put genEvt in Event
217  iEvent.put(std::move(patJets));
218 
219  iEvent.put(std::move(tagInfosOut), "tagInfos" );
220 
221 }
222 
223 // ParameterSet description for module
225 {
227  iDesc.setComment("PAT jet producer module");
228 
229  // input source
230  iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
231 
232  // sort inputs (by pt)
233  iDesc.add<bool>("sort", true);
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 
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:63
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
void addJECFactors(const JetCorrFactors &jec)
add more sets of energy correction factors
Definition: Jet.h:177
virtual BaseTagInfo * clone(void) const
clone
Definition: BaseTagInfo.h:19
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Ptr< value_type > ptrAt(size_type i) const
~PATJetUpdater() override
edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_
Definition: PATJetUpdater.h:48
std::vector< edm::InputTag > tagInfoTags_
Definition: PATJetUpdater.h:59
PATJetUpdater(const edm::ParameterSet &iConfig)
size_type size() const
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)
char const * label
std::vector< edm::EDGetTokenT< edm::View< reco::BaseTagInfo > > > tagInfoTokens_
Definition: PATJetUpdater.h:60
void setComment(std::string const &value)
std::vector< edm::EDGetTokenT< reco::JetFloatAssociation::Container > > discriminatorTokens_
Definition: PATJetUpdater.h:56
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const_iterator begin() const
Definition: Jet.py:1
Produces pat::Jet&#39;s.
Definition: PATJetUpdater.h:34
Class for the storage of jet correction factors.
vector< PseudoJet > jets
std::vector< edm::InputTag > discriminatorTags_
Definition: PATJetUpdater.h:55
Definition: value.py:1
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
RefProd< PROD > getRefBeforePut()
Definition: Event.h:150
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:168
std::vector< pat::JetCorrFactors > jec_
Definition: Jet.h:580
int k[5][pyjets_maxn]
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
std::vector< std::string > discriminatorLabels_
Definition: PATJetUpdater.h:57
REF castTo() const
Definition: RefToBase.h:289
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:66
std::vector< std::string > tagInfoLabels_
Definition: PATJetUpdater.h:61
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
std::vector< edm::EDGetTokenT< edm::ValueMap< JetCorrFactors > > > jetCorrFactorsTokens_
Definition: PATJetUpdater.h:51
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:511
void addTagInfo(const std::string &label, const TagInfoFwdPtrCollection::value_type &info)