test
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  const edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
129  const edm::RefToBase<Jet> patJetRef(jetRef.castTo<JetRef>());
130  Jet ajet( patJetRef );
131 
132  if (addJetCorrFactors_) {
133  // undo previous jet energy corrections
134  ajet.setP4(ajet.correctedP4(0));
135  // clear previous JetCorrFactors
136  ajet.jec_.clear();
137  // add additional JetCorrs to the jet
138  for ( unsigned int i=0; i<jetCorrFactorsTokens_.size(); ++i ) {
139  const JetCorrFactors& jcf = jetCorrs[i][jetRef];
140  // uncomment for debugging
141  // jcf.print();
142  ajet.addJECFactors(jcf);
143  }
144  std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
145  if(std::find(levels.begin(), levels.end(), "L2L3Residual")!=levels.end()){
146  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"));
147  }
148  else if(std::find(levels.begin(), levels.end(), "L3Absolute")!=levels.end()){
149  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"));
150  }
151  else{
152  ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
153  if(printWarning_){
154  edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
155  << "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
156  << " uncorrected."; printWarning_=false;
157  }
158  }
159  }
160 
161  // add b-tag info if available & required
162  if (addBTagInfo_) {
163  if (addDiscriminators_) {
164  for (size_t k=0; k<jetDiscriminators.size(); ++k) {
165  float value = (*jetDiscriminators[k])[jetRef];
166  ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
167  }
168  }
169  if (addTagInfos_) {
170  for (size_t k=0; k<jetTagInfos.size(); ++k) {
171  const edm::View<reco::BaseTagInfo> & taginfos = *jetTagInfos[k];
172  // This is not associative, so we have to search the jet
174  // Try first by 'same index'
175  if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
176  match = taginfos.ptrAt(idx);
177  } else {
178  // otherwise fail back to a simple search
179  for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end(); itTI != edTI; ++itTI) {
180  if (itTI->jet() == jetRef) { match = taginfos.ptrAt( itTI - taginfos.begin() ); break; }
181  }
182  }
183  if (match.isNonnull()) {
184  tagInfosOut->push_back( match->clone() );
185  // set the "forward" ptr to the thinned collection
186  edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr ( h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1 );
187  // set the "backward" ptr to the original collection for association
188  edm::Ptr<reco::BaseTagInfo> tagInfoBackPtr ( match );
189  // make FwdPtr
190  TagInfoFwdPtrCollection::value_type tagInfoFwdPtr( tagInfoForwardPtr, tagInfoBackPtr ) ;
191  ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr );
192  }
193  }
194  }
195  }
196 
197  if ( useUserData_ ) {
198  userDataHelper_.add( ajet, iEvent, iSetup );
199  }
200 
201  // reassign the original object reference to preserve reference to the original jet the input PAT jet was derived from
202  // (this needs to be done at the end since cloning the input PAT jet would interfere with adding UserData)
203  ajet.refToOrig_ = patJetRef->originalObjectRef();
204 
205  patJets->push_back(ajet);
206  }
207 
208  // sort jets in pt
209  std::sort(patJets->begin(), patJets->end(), pTComparator_);
210 
211  // put genEvt in Event
212  iEvent.put(patJets);
213 
214  iEvent.put( tagInfosOut, "tagInfos" );
215 
216 }
217 
218 // ParameterSet description for module
220 {
222  iDesc.setComment("PAT jet producer module");
223 
224  // input source
225  iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
226 
227  // tag info
228  iDesc.add<bool>("addTagInfos", true);
229  std::vector<edm::InputTag> emptyVInputTags;
230  iDesc.add<std::vector<edm::InputTag> >("tagInfoSources", emptyVInputTags);
231 
232  // jet energy corrections
233  iDesc.add<bool>("addJetCorrFactors", true);
234  iDesc.add<std::vector<edm::InputTag> >("jetCorrFactorsSource", emptyVInputTags);
235 
236  // btag discriminator tags
237  iDesc.add<bool>("addBTagInfo",true);
238  iDesc.add<bool>("addDiscriminators", true);
239  iDesc.add<std::vector<edm::InputTag> >("discriminatorSources", emptyVInputTags);
240 
241  // Check to see if the user wants to add user data
242  edm::ParameterSetDescription userDataPSet;
244  iDesc.addOptional("userData", userDataPSet);
245 
246  descriptions.add("PATJetUpdater", iDesc);
247 }
248 
250 
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:156
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:172
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< pat::JetCorrFactors > jec_
Definition: Jet.h:547
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:286
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
Analysis-level calorimeter jet class.
Definition: Jet.h:78
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
edm::Ptr< reco::Candidate > refToOrig_
Definition: PATObject.h:421
void addTagInfo(const std::string &label, const TagInfoFwdPtrCollection::value_type &info)