CMS 3D CMS Logo

RecoTauGenericJetRegionProducer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauJetRegionProducer
3  *
4  * Given a set of Jets, make new jets with the same p4 but collect all the
5  * particle-flow candidates 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 
20 
23 
31 
34 
35 #include <string>
36 #include <iostream>
37 
38 template <class JetType, class CandType>
40 public:
42  typedef edm::AssociationMap<edm::OneToMany<std::vector<JetType>, std::vector<CandType>, unsigned int> >
46 
47  void produce(edm::Event& evt, const edm::EventSetup& es) override;
48 
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50  static void fillDescriptionsBase(edm::ConfigurationDescriptions& descriptions, const std::string& name);
51 
52 private:
54 
58 
62 
63  double minJetPt_;
64  double maxJetAbsEta_;
65  double deltaR2_;
66 
68 };
69 
70 template <class JetType, class CandType>
72  : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
74  pfCandSrc_ = cfg.getParameter<edm::InputTag>("pfCandSrc");
75  pfCandAssocMapSrc_ = cfg.getParameter<edm::InputTag>("pfCandAssocMapSrc");
76 
77  pf_token = consumes<std::vector<CandType> >(pfCandSrc_);
78  Jets_token = consumes<reco::CandidateView>(inputJets_);
79  pfCandAssocMap_token = consumes<JetToCandidateAssociation>(pfCandAssocMapSrc_);
80 
81  double deltaR = cfg.getParameter<double>("deltaR");
82  deltaR2_ = deltaR * deltaR;
83  minJetPt_ = cfg.getParameter<double>("minJetPt");
84  maxJetAbsEta_ = cfg.getParameter<double>("maxJetAbsEta");
85 
86  verbosity_ = cfg.getParameter<int>("verbosity");
87 
88  produces<std::vector<JetType> >("jets");
89  produces<JetMatchMap>();
90 }
91 
92 template <class JetType, class CandType>
94  if (verbosity_) {
95  std::cout << "<RecoTauJetRegionProducer::produce (moduleLabel = " << moduleLabel_ << ")>:" << std::endl;
96  std::cout << " inputJets = " << inputJets_ << std::endl;
97  std::cout << " pfCandSrc = " << pfCandSrc_ << std::endl;
98  std::cout << " pfCandAssocMapSrc_ = " << pfCandAssocMapSrc_ << std::endl;
99  }
100 
101  edm::Handle<std::vector<CandType> > pfCandsHandle;
102  evt.getByToken(pf_token, pfCandsHandle);
103 
104  // Build Ptrs for all the PFCandidates
105  typedef edm::Ptr<CandType> CandPtr;
106  std::vector<CandPtr> pfCands;
107  pfCands.reserve(pfCandsHandle->size());
108  for (size_t icand = 0; icand < pfCandsHandle->size(); ++icand) {
109  pfCands.push_back(CandPtr(pfCandsHandle, icand));
110  }
111 
112  // Get the jets
114  evt.getByToken(Jets_token, jetView);
115  // Convert to a vector of JetRefs
116  edm::RefVector<std::vector<JetType> > jets = reco::tau::castView<edm::RefVector<std::vector<JetType> > >(jetView);
117  size_t nJets = jets.size();
118 
119  // Get the association map matching jets to Candidates
120  // (needed for reconstruction of boosted taus)
122  std::vector<std::unordered_set<unsigned> > fastJetToPFCandMap;
123  if (!pfCandAssocMapSrc_.label().empty()) {
124  evt.getByToken(pfCandAssocMap_token, jetToPFCandMap);
125  fastJetToPFCandMap.resize(nJets);
126  for (size_t ijet = 0; ijet < nJets; ++ijet) {
127  // Get a ref to jet
128  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
129  const auto& pfCandsMappedToJet = (*jetToPFCandMap)[jetRef];
130  for (const auto& pfCandMappedToJet : pfCandsMappedToJet) {
131  fastJetToPFCandMap[ijet].emplace(pfCandMappedToJet.key());
132  }
133  }
134  }
135 
136  // Get the original product, so we can match against it - otherwise the
137  // indices don't match up.
138  edm::ProductID originalId = jets.id();
139  edm::Handle<std::vector<JetType> > originalJets;
140  size_t nOriginalJets = 0;
141  // We have to make sure that we have some selected jets, otherwise we don't
142  // actually have a valid product ID to the original jets.
143  if (nJets) {
144  try {
145  evt.get(originalId, originalJets);
146  } catch (const cms::Exception& e) {
147  edm::LogError("MissingOriginalCollection") << "Can't get the original jets that made: " << inputJets_
148  << " that have product ID: " << originalId << " from the event!!";
149  throw e;
150  }
151  nOriginalJets = originalJets->size();
152  }
153 
154  auto newJets = std::make_unique<std::vector<JetType> >();
155 
156  // Keep track of the indices of the current jet and the old (original) jet
157  // -1 indicates no match.
158  std::vector<int> matchInfo(nOriginalJets, -1);
159  newJets->reserve(nJets);
160  size_t nNewJets = 0;
161  for (size_t ijet = 0; ijet < nJets; ++ijet) {
162  // Get a ref to jet
163  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
164  if (jetRef->pt() - minJetPt_ < 1e-5)
165  continue;
166  if (std::abs(jetRef->eta()) - maxJetAbsEta_ > -1e-5)
167  continue;
168  // Make an initial copy.
169  newJets->emplace_back(*jetRef);
170  JetType& newJet = newJets->back();
171  // Clear out all the constituents
172  newJet.clearDaughters();
173  // Loop over all the PFCands
174  for (const auto& pfCand : pfCands) {
175  bool isMappedToJet = false;
176  if (jetToPFCandMap.isValid()) {
177  auto temp = jetToPFCandMap->find(jetRef);
178  if (temp == jetToPFCandMap->end()) {
179  edm::LogWarning("WeirdCandidateMap") << "Candidate map for jet " << jetRef.key() << " is empty!";
180  continue;
181  }
182  isMappedToJet = fastJetToPFCandMap[ijet].count(pfCand.key());
183  } else {
184  isMappedToJet = true;
185  }
186  if (reco::deltaR2(*jetRef, *pfCand) < deltaR2_ && isMappedToJet)
187  newJet.addDaughter(pfCand);
188  }
189  if (verbosity_) {
190  std::cout << "jet #" << ijet << ": Pt = " << jetRef->pt() << ", eta = " << jetRef->eta()
191  << ", phi = " << jetRef->eta() << ","
192  << " mass = " << jetRef->mass() << ", area = " << jetRef->jetArea() << std::endl;
193  auto jetConstituents = newJet.daughterPtrVector();
194  int idx = 0;
195  for (const auto& jetConstituent : jetConstituents) {
196  std::cout << " constituent #" << idx << ": Pt = " << jetConstituent->pt() << ", eta = " << jetConstituent->eta()
197  << ", phi = " << jetConstituent->phi() << std::endl;
198  ++idx;
199  }
200  }
201  // Match the index of the jet we just made to the index into the original
202  // collection.
203  //matchInfo[jetRef.key()] = ijet;
204  matchInfo[jetRef.key()] = nNewJets;
205  nNewJets++;
206  }
207 
208  // Put our new jets into the event
209  edm::OrphanHandle<std::vector<JetType> > newJetsInEvent = evt.put(std::move(newJets), "jets");
210 
211  // Create a matching between original jets -> extra collection
212  auto matching = (nJets != 0)
213  ? std::make_unique<JetMatchMap>(
214  edm::makeRefToBaseProdFrom(edm::RefToBase<reco::Jet>(jets[0]), evt), newJetsInEvent)
215  : std::make_unique<JetMatchMap>();
216  for (size_t ijet = 0; ijet < nJets; ++ijet) {
217  matching->insert(edm::RefToBase<reco::Jet>(jets[ijet]),
218  edm::RefToBase<reco::Jet>(edm::Ref<std::vector<JetType> >(newJetsInEvent, matchInfo[ijet])));
219  }
220  evt.put(std::move(matching));
221 }
222 
223 template <class JetType, class CandType>
225  edm::ConfigurationDescriptions& descriptions, const std::string& name) {
226  // RecoTauGenericJetRegionProducer
228  desc.add<edm::InputTag>("src", edm::InputTag("ak4PFJets"));
229  desc.add<double>("deltaR", 0.8);
230  desc.add<edm::InputTag>("pfCandAssocMapSrc", edm::InputTag(""));
231  desc.add<int>("verbosity", 0);
232  desc.add<double>("maxJetAbsEta", 2.5);
233  desc.add<double>("minJetPt", 14.0);
234  desc.add<edm::InputTag>("pfCandSrc", edm::InputTag("particleFlow"));
235  descriptions.add(name, desc);
236 }
237 
238 template <>
240  edm::ConfigurationDescriptions& descriptions) {
241  // RecoTauGenericJetRegionProducer
242  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauJetRegionProducer");
243 }
244 
245 template <>
247  edm::ConfigurationDescriptions& descriptions) {
248  // RecoTauGenericJetRegionProducer
249  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauPatJetRegionProducer");
250 }
251 
254 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
static void fillDescriptionsBase(edm::ConfigurationDescriptions &descriptions, const std::string &name)
edm::EDGetTokenT< JetToCandidateAssociation > pfCandAssocMap_token
const_iterator end() const
last iterator over the map (read only)
RecoTauGenericJetRegionProducer< reco::PFJet, reco::PFCandidate > RecoTauJetRegionProducer
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
RecoTauGenericJetRegionProducer< pat::Jet, pat::PackedCandidate > RecoTauPatJetRegionProducer
const_iterator find(const key_type &k) const
find element with specified reference key
void produce(edm::Event &evt, const edm::EventSetup &es) override
key_type key() const
Accessor for product key.
Definition: Ref.h:250
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:117
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< reco::CandidateView > Jets_token
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:334
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
edm::AssociationMap< edm::OneToMany< std::vector< JetType >, std::vector< CandType >, unsigned int > > JetToCandidateAssociation
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
edm::AssociationMap< edm::OneToOne< reco::JetView, reco::JetView > > JetMatchMap
void add(std::string const &label, ParameterSetDescription const &psetDescription)
RecoTauGenericJetRegionProducer(const edm::ParameterSet &pset)
std::string const & label() const
Definition: InputTag.h:36
edm::EDGetTokenT< std::vector< CandType > > pf_token
RefToBaseProd< T > makeRefToBaseProdFrom(RefToBase< T > const &iRef, Event const &iEvent)
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
def move(src, dest)
Definition: eostools.py:511