CMS 3D CMS Logo

RecoTauJetRegionProducer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauJetRegionProducer
3  *
4  * Given a set of PFJets, make new jets with the same p4 but collect all the
5  * PFCandidates from a cone of a given size into the constituents.
6  *
7  * Author: Evan K. Friis, UC Davis
8  *
9  */
10 
11 #include <boost/bind.hpp>
12 
18 
21 
28 
29 #include <string>
30 #include <iostream>
31 
33 {
34  public:
36  typedef edm::AssociationMap<edm::OneToMany<std::vector<reco::PFJet>, std::vector<reco::PFCandidate>, unsigned int> > JetToPFCandidateAssociation;
39 
40  void produce(edm::Event& evt, const edm::EventSetup& es) override;
41 
42  private:
44 
48 
52 
53  double minJetPt_;
54  double maxJetAbsEta_;
55  double deltaR2_;
56 
58 };
59 
61  : moduleLabel_(cfg.getParameter<std::string>("@module_label"))
62 {
64  pfCandSrc_ = cfg.getParameter<edm::InputTag>("pfCandSrc");
65  pfCandAssocMapSrc_ = cfg.getParameter<edm::InputTag>("pfCandAssocMapSrc");
66 
67  pf_token = consumes<reco::PFCandidateCollection>(pfCandSrc_);
68  Jets_token = consumes<reco::CandidateView>(inputJets_);
69  pfCandAssocMap_token = consumes<JetToPFCandidateAssociation>(pfCandAssocMapSrc_);
70 
71  double deltaR = cfg.getParameter<double>("deltaR");
72  deltaR2_ = deltaR*deltaR;
73  minJetPt_ = ( cfg.exists("minJetPt") ) ? cfg.getParameter<double>("minJetPt") : -1.0;
74  maxJetAbsEta_ = ( cfg.exists("maxJetAbsEta") ) ? cfg.getParameter<double>("maxJetAbsEta") : 99.0;
75 
76  verbosity_ = ( cfg.exists("verbosity") ) ?
77  cfg.getParameter<int>("verbosity") : 0;
78 
79  produces<reco::PFJetCollection>("jets");
80  produces<PFJetMatchMap>();
81 }
82 
84 {
85  if ( verbosity_ ) {
86  std::cout << "<RecoTauJetRegionProducer::produce (moduleLabel = " << moduleLabel_ << ")>:" << std::endl;
87  std::cout << " inputJets = " << inputJets_ << std::endl;
88  std::cout << " pfCandSrc = " << pfCandSrc_ << std::endl;
89  std::cout << " pfCandAssocMapSrc_ = " << pfCandAssocMapSrc_ << std::endl;
90  }
91 
93  evt.getByToken(pf_token, pfCandsHandle);
94 
95  // Build Ptrs for all the PFCandidates
96  typedef edm::Ptr<reco::PFCandidate> PFCandPtr;
97  std::vector<PFCandPtr> pfCands;
98  pfCands.reserve(pfCandsHandle->size());
99  for ( size_t icand = 0; icand < pfCandsHandle->size(); ++icand ) {
100  pfCands.push_back(PFCandPtr(pfCandsHandle, icand));
101  }
102 
103  // Get the jets
105  evt.getByToken(Jets_token, jetView);
106  // Convert to a vector of PFJetRefs
107  reco::PFJetRefVector jets = reco::tau::castView<reco::PFJetRefVector>(jetView);
108  size_t nJets = jets.size();
109 
110  // Get the association map matching jets to PFCandidates
111  // (needed for recinstruction of boosted taus)
113  std::vector<std::unordered_set<unsigned> > fastJetToPFCandMap;
114  if ( pfCandAssocMapSrc_.label() != "" ) {
115  evt.getByToken(pfCandAssocMap_token, jetToPFCandMap);
116  fastJetToPFCandMap.resize(nJets);
117  for ( size_t ijet = 0; ijet < nJets; ++ijet ) {
118  // Get a ref to jet
119  const reco::PFJetRef& jetRef = jets[ijet];
120  const auto& pfCandsMappedToJet = (*jetToPFCandMap)[jetRef];
121  for ( const auto& pfCandMappedToJet : pfCandsMappedToJet ) {
122  fastJetToPFCandMap[ijet].emplace(pfCandMappedToJet.key());
123  }
124  }
125  }
126 
127  // Get the original product, so we can match against it - otherwise the
128  // indices don't match up.
129  edm::ProductID originalId = jets.id();
131  size_t nOriginalJets = 0;
132  // We have to make sure that we have some selected jets, otherwise we don't
133  // actually have a valid product ID to the original jets.
134  if ( nJets ) {
135  try {
136  evt.get(originalId, originalJets);
137  } catch(const cms::Exception &e) {
138  edm::LogError("MissingOriginalCollection")
139  << "Can't get the original jets that made: " << inputJets_
140  << " that have product ID: " << originalId
141  << " from the event!!";
142  throw e;
143  }
144  nOriginalJets = originalJets->size();
145  }
146 
147  auto newJets = std::make_unique<reco::PFJetCollection>();
148 
149  // Keep track of the indices of the current jet and the old (original) jet
150  // -1 indicates no match.
151  std::vector<int> matchInfo(nOriginalJets, -1);
152  newJets->reserve(nJets);
153  size_t nNewJets = 0;
154  for ( size_t ijet = 0; ijet < nJets; ++ijet ) {
155  // Get a ref to jet
156  const reco::PFJetRef& jetRef = jets[ijet];
157  if(jetRef->pt() - minJetPt_ < 1e-5) continue;
158  if(std::abs(jetRef->eta()) - maxJetAbsEta_ > -1e-5) continue;
159  // Make an initial copy.
160  newJets->emplace_back(*jetRef);
161  reco::PFJet& newJet = newJets->back();
162  // Clear out all the constituents
163  newJet.clearDaughters();
164  // Loop over all the PFCands
165  for ( const auto& pfCand : pfCands ) {
166  bool isMappedToJet = false;
167  if ( jetToPFCandMap.isValid() ) {
168  auto temp = jetToPFCandMap->find(jetRef);
169  if( temp == jetToPFCandMap->end() ) {
170  edm::LogWarning("WeirdCandidateMap") << "Candidate map for jet " << jetRef.key() << " is empty!";
171  continue;
172  }
173  isMappedToJet = fastJetToPFCandMap[ijet].count(pfCand.key());
174  } else {
175  isMappedToJet = true;
176  }
177  if ( reco::deltaR2(*jetRef, *pfCand) < deltaR2_ && isMappedToJet ) newJet.addDaughter(pfCand);
178  }
179  if ( verbosity_ ) {
180  std::cout << "jet #" << ijet << ": Pt = " << jetRef->pt() << ", eta = " << jetRef->eta() << ", phi = " << jetRef->eta() << ","
181  << " mass = " << jetRef->mass() << ", area = " << jetRef->jetArea() << std::endl;
182  std::vector<reco::PFCandidatePtr> jetConstituents = newJet.getPFConstituents();
183  int idx = 0;
184  for ( std::vector<reco::PFCandidatePtr>::const_iterator jetConstituent = jetConstituents.begin();
185  jetConstituent != jetConstituents.end(); ++jetConstituent ) {
186  std::cout << " constituent #" << idx << ": Pt = " << (*jetConstituent)->pt() << ", eta = " << (*jetConstituent)->eta() << ", phi = " << (*jetConstituent)->phi() << std::endl;
187  }
188  }
189  // Match the index of the jet we just made to the index into the original
190  // collection.
191  //matchInfo[jetRef.key()] = ijet;
192  matchInfo[jetRef.key()] = nNewJets;
193  nNewJets++;
194  }
195 
196  // Put our new jets into the event
197  edm::OrphanHandle<reco::PFJetCollection> newJetsInEvent = evt.put(std::move(newJets), "jets");
198 
199  // Create a matching between original jets -> extra collection
200  auto matching = std::make_unique<PFJetMatchMap>(newJetsInEvent);
201  if ( nJets ) {
203  filler.insert(originalJets, matchInfo.begin(), matchInfo.end());
204  filler.fill();
205  }
206  evt.put(std::move(matching));
207 }
208 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
RecoTauJetRegionProducer(const edm::ParameterSet &pset)
const_iterator end() const
last iterator over the map (read only)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
edm::Association< reco::PFJetCollection > PFJetMatchMap
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< reco::PFCandidateCollection > pf_token
const_iterator find(const key_type &k) const
find element with specified reference key
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
bool exists(std::string const &parameterName) const
checks if a parameter exists
key_type key() const
Accessor for product key.
Definition: Ref.h:265
void produce(edm::Event &evt, const edm::EventSetup &es) override
Jets made from PFObjects.
Definition: PFJet.h:21
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:122
edm::EDGetTokenT< reco::CandidateView > Jets_token
vector< PseudoJet > jets
edm::EDGetTokenT< JetToPFCandidateAssociation > pfCandAssocMap_token
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:334
bool isValid() const
Definition: HandleBase.h:74
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
void clearDaughters()
clear daughter references
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
std::string const & label() const
Definition: InputTag.h:36
edm::AssociationMap< edm::OneToMany< std::vector< reco::PFJet >, std::vector< reco::PFCandidate >, unsigned int > > JetToPFCandidateAssociation
void addDaughter(const CandidatePtr &)
add a daughter via a reference
virtual std::vector< reco::PFCandidatePtr > getPFConstituents() const
get all constituents
Definition: PFJet.cc:52
size_type size() const
Size of the RefVector.
Definition: RefVector.h:107
def move(src, dest)
Definition: eostools.py:510