CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
25 using namespace pat;
26 
27 
29  useUserData_(iConfig.exists("userData")),
30  printWarning_(true)
31 {
32  // initialize configurables
33  jetsToken_ = consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>( "jetSource" ));
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  discriminatorLabels_.push_back(label);
55  }
56  }
57  if (tagInfoTags_.empty()) {
58  addTagInfos_ = false;
59  } else {
60  for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed; ++it) {
61  std::string label = it->label();
62  std::string::size_type pos = label.find("TagInfos");
63  if ((pos != std::string::npos) && (pos != label.length() - 8)) {
64  label.erase(pos+8); // trim a tail after "TagInfos"
65  }
66  tagInfoLabels_.push_back(label);
67  }
68  }
69  if (!addBTagInfo_) { addDiscriminators_ = false; addTagInfos_ = false; }
70  // Check to see if the user wants to add user data
71  if ( useUserData_ ) {
72  userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
73  }
74  // produces vector of jets
75  produces<std::vector<Jet> >();
76  produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
77 }
78 
79 
80 PATJetUpdater::~PATJetUpdater() {
81 
82 }
83 
84 
86 {
87  // Get the vector of jets
89  iEvent.getByToken(jetsToken_, jets);
90 
91  // read in the jet correction factors ValueMap
92  std::vector<edm::ValueMap<JetCorrFactors> > jetCorrs;
93  if (addJetCorrFactors_) {
94  for ( size_t i = 0; i < jetCorrFactorsTokens_.size(); ++i ) {
96  iEvent.getByToken(jetCorrFactorsTokens_[i], jetCorr);
97  jetCorrs.push_back( *jetCorr );
98  }
99  }
100 
101  // Get the vector of jet tags with b-tagging info
102  std::vector<edm::Handle<reco::JetFloatAssociation::Container> > jetDiscriminators;
104  jetDiscriminators.resize(discriminatorTokens_.size());
105  for (size_t i = 0; i < discriminatorTokens_.size(); ++i) {
106  iEvent.getByToken(discriminatorTokens_[i], jetDiscriminators[i]);
107  }
108  }
109  std::vector<edm::Handle<edm::View<reco::BaseTagInfo> > > jetTagInfos;
110  if (addBTagInfo_ && addTagInfos_) {
111  jetTagInfos.resize(tagInfoTokens_.size());
112  for (size_t i = 0; i < tagInfoTokens_.size(); ++i) {
113  iEvent.getByToken(tagInfoTokens_[i], jetTagInfos[i]);
114  }
115  }
116 
117  // loop over jets
118  std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() );
119 
120  std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );
121 
123 
124  for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
125 
126  // construct the Jet from the ref -> save ref to original object
127  unsigned int idx = itJet - jets->begin();
128  edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
129  edm::Ptr<reco::Jet> jetPtr = jets->ptrAt(idx);
130  Jet ajet( edm::RefToBase<Jet>(jetRef.castTo<JetRef>()) );
131 
132  if (addJetCorrFactors_) {
133  unsigned int setindex = ajet.availableJECSets().size();
134  // Undo previous jet energy corrections
135  ajet.setP4(ajet.correctedP4(0));
136  // add additional JetCorrs to the jet
137  for ( unsigned int i=0; i<jetCorrFactorsTokens_.size(); ++i ) {
138  const JetCorrFactors& jcf = jetCorrs[i][jetRef];
139  // uncomment for debugging
140  // jcf.print();
141  ajet.addJECFactors(jcf);
142  }
143  std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
144  if(std::find(levels.begin(), levels.end(), "L2L3Residual")!=levels.end()){
145  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"), JetCorrFactors::NONE, setindex);
146  }
147  else if(std::find(levels.begin(), levels.end(), "L3Absolute")!=levels.end()){
148  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"), JetCorrFactors::NONE, setindex);
149  }
150  else{
151  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"), JetCorrFactors::NONE, setindex);
152  if(printWarning_){
153  edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
154  << "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
155  << " uncorrected."; printWarning_=false;
156  }
157  }
158  }
159 
160  // add b-tag info if available & required
161  if (addBTagInfo_) {
162  if (addDiscriminators_) {
163  for (size_t k=0; k<jetDiscriminators.size(); ++k) {
164  float value = (*jetDiscriminators[k])[jetRef];
165  ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
166  }
167  }
168  if (addTagInfos_) {
169  for (size_t k=0; k<jetTagInfos.size(); ++k) {
170  const edm::View<reco::BaseTagInfo> & taginfos = *jetTagInfos[k];
171  // This is not associative, so we have to search the jet
173  // Try first by 'same index'
174  if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
175  match = taginfos.ptrAt(idx);
176  } else {
177  // otherwise fail back to a simple search
178  for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end(); itTI != edTI; ++itTI) {
179  if (itTI->jet() == jetRef) { match = taginfos.ptrAt( itTI - taginfos.begin() ); break; }
180  }
181  }
182  if (match.isNonnull()) {
183  tagInfosOut->push_back( match->clone() );
184  // set the "forward" ptr to the thinned collection
185  edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr ( h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1 );
186  // set the "backward" ptr to the original collection for association
187  edm::Ptr<reco::BaseTagInfo> tagInfoBackPtr ( match );
188  // make FwdPtr
189  TagInfoFwdPtrCollection::value_type tagInfoFwdPtr( tagInfoForwardPtr, tagInfoBackPtr ) ;
190  ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr );
191  }
192  }
193  }
194  }
195 
196  if ( useUserData_ ) {
197  userDataHelper_.add( ajet, iEvent, iSetup );
198  }
199 
200  patJets->push_back(ajet);
201  }
202 
203  // sort jets in pt
204  std::sort(patJets->begin(), patJets->end(), pTComparator_);
205 
206  // put genEvt in Event
207  iEvent.put(patJets);
208 
209  iEvent.put( tagInfosOut, "tagInfos" );
210 
211 }
212 
213 // ParameterSet description for module
215 {
217  iDesc.setComment("PAT jet producer module");
218 
219  // input source
220  iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
221 
222  // tag info
223  iDesc.add<bool>("addTagInfos", true);
224  std::vector<edm::InputTag> emptyVInputTags;
225  iDesc.add<std::vector<edm::InputTag> >("tagInfoSources", emptyVInputTags);
226 
227  // jet energy corrections
228  iDesc.add<bool>("addJetCorrFactors", true);
229  iDesc.add<std::vector<edm::InputTag> >("jetCorrFactorsSource", emptyVInputTags);
230 
231  // btag discriminator tags
232  iDesc.add<bool>("addBTagInfo",true);
233  iDesc.add<bool>("addDiscriminators", true);
234  iDesc.add<std::vector<edm::InputTag> >("discriminatorSources", emptyVInputTags);
235 
236  // Check to see if the user wants to add user data
237  edm::ParameterSetDescription userDataPSet;
239  iDesc.addOptional("userData", userDataPSet);
240 
241  descriptions.add("PATJetUpdater", iDesc);
242 }
243 
245 
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:155
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.
int i
Definition: DBlmapReader.cc:9
GreaterByPt< Jet > pTComparator_
Definition: PATJetUpdater.h:62
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
void addJECFactors(const JetCorrFactors &jec)
add more sets of energy correction factors
Definition: Jet.h:171
const std::vector< std::string > availableJECSets() const
-— methods for jet corrections -—
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
Ptr< value_type > ptrAt(size_type i) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_
Definition: PATJetUpdater.h:48
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:7
void addBDiscriminatorPair(const std::pair< std::string, float > &thePair)
method to add a algolabel-discriminator pair
uint16_t size_type
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
Produces pat::Jet&#39;s.
Definition: PATJetUpdater.h:34
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
Class for the storage of jet correction factors.
vector< PseudoJet > jets
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
RefProd< PROD > getRefBeforePut()
Definition: Event.h:141
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:169
std::vector< std::string > discriminatorLabels_
Definition: PATJetUpdater.h:56
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
REF castTo() const
Definition: RefToBase.h:271
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
Analysis-level calorimeter jet class.
Definition: Jet.h:77
void add(std::string const &label, ParameterSetDescription const &psetDescription)
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:137
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
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
std::vector< edm::EDGetTokenT< edm::ValueMap< JetCorrFactors > > > jetCorrFactorsTokens_
Definition: PATJetUpdater.h:50
void addTagInfo(const std::string &label, const TagInfoFwdPtrCollection::value_type &info)