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 {
41  public:
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"))
73 {
75  pfCandSrc_ = cfg.getParameter<edm::InputTag>("pfCandSrc");
76  pfCandAssocMapSrc_ = cfg.getParameter<edm::InputTag>("pfCandAssocMapSrc");
77 
78  pf_token = consumes<std::vector<CandType> >(pfCandSrc_);
79  Jets_token = consumes<reco::CandidateView>(inputJets_);
80  pfCandAssocMap_token = consumes<JetToCandidateAssociation>(pfCandAssocMapSrc_);
81 
82  double deltaR = cfg.getParameter<double>("deltaR");
83  deltaR2_ = deltaR*deltaR;
84  minJetPt_ = cfg.getParameter<double>("minJetPt");
85  maxJetAbsEta_ = cfg.getParameter<double>("maxJetAbsEta");
86 
87  verbosity_ = cfg.getParameter<int>("verbosity");
88 
89  produces<std::vector<JetType> >("jets");
90  produces<JetMatchMap>();
91 }
92 
93 template<class JetType, class CandType>
95 {
96  if ( verbosity_ ) {
97  std::cout << "<RecoTauJetRegionProducer::produce (moduleLabel = " << moduleLabel_ << ")>:" << std::endl;
98  std::cout << " inputJets = " << inputJets_ << std::endl;
99  std::cout << " pfCandSrc = " << pfCandSrc_ << std::endl;
100  std::cout << " pfCandAssocMapSrc_ = " << pfCandAssocMapSrc_ << std::endl;
101  }
102 
103  edm::Handle<std::vector<CandType> > pfCandsHandle;
104  evt.getByToken(pf_token, pfCandsHandle);
105 
106  // Build Ptrs for all the PFCandidates
107  typedef edm::Ptr<CandType> CandPtr;
108  std::vector<CandPtr> pfCands;
109  pfCands.reserve(pfCandsHandle->size());
110  for ( size_t icand = 0; icand < pfCandsHandle->size(); ++icand ) {
111  pfCands.push_back(CandPtr(pfCandsHandle, icand));
112  }
113 
114  // Get the jets
116  evt.getByToken(Jets_token, jetView);
117  // Convert to a vector of JetRefs
118  edm::RefVector<std::vector<JetType> > jets = reco::tau::castView<edm::RefVector<std::vector<JetType> > >(jetView);
119  size_t nJets = jets.size();
120 
121  // Get the association map matching jets to Candidates
122  // (needed for reconstruction of boosted taus)
124  std::vector<std::unordered_set<unsigned> > fastJetToPFCandMap;
125  if ( !pfCandAssocMapSrc_.label().empty() ) {
126  evt.getByToken(pfCandAssocMap_token, jetToPFCandMap);
127  fastJetToPFCandMap.resize(nJets);
128  for ( size_t ijet = 0; ijet < nJets; ++ijet ) {
129  // Get a ref to jet
130  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
131  const auto& pfCandsMappedToJet = (*jetToPFCandMap)[jetRef];
132  for ( const auto& pfCandMappedToJet : pfCandsMappedToJet ) {
133  fastJetToPFCandMap[ijet].emplace(pfCandMappedToJet.key());
134  }
135  }
136  }
137 
138  // Get the original product, so we can match against it - otherwise the
139  // indices don't match up.
140  edm::ProductID originalId = jets.id();
141  edm::Handle<std::vector<JetType> > originalJets;
142  size_t nOriginalJets = 0;
143  // We have to make sure that we have some selected jets, otherwise we don't
144  // actually have a valid product ID to the original jets.
145  if ( nJets ) {
146  try {
147  evt.get(originalId, originalJets);
148  } catch(const cms::Exception &e) {
149  edm::LogError("MissingOriginalCollection")
150  << "Can't get the original jets that made: " << inputJets_
151  << " that have product ID: " << originalId
152  << " from the event!!";
153  throw e;
154  }
155  nOriginalJets = originalJets->size();
156  }
157 
158  auto newJets = std::make_unique<std::vector<JetType> >();
159 
160  // Keep track of the indices of the current jet and the old (original) jet
161  // -1 indicates no match.
162  std::vector<int> matchInfo(nOriginalJets, -1);
163  newJets->reserve(nJets);
164  size_t nNewJets = 0;
165  for ( size_t ijet = 0; ijet < nJets; ++ijet ) {
166  // Get a ref to jet
167  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
168  if(jetRef->pt() - minJetPt_ < 1e-5) continue;
169  if(std::abs(jetRef->eta()) - maxJetAbsEta_ > -1e-5) continue;
170  // Make an initial copy.
171  newJets->emplace_back(*jetRef);
172  JetType& newJet = newJets->back();
173  // Clear out all the constituents
174  newJet.clearDaughters();
175  // Loop over all the PFCands
176  for ( const auto& pfCand : pfCands ) {
177  bool isMappedToJet = false;
178  if ( jetToPFCandMap.isValid() ) {
179  auto temp = jetToPFCandMap->find(jetRef);
180  if( temp == jetToPFCandMap->end() ) {
181  edm::LogWarning("WeirdCandidateMap") << "Candidate map for jet " << jetRef.key() << " is empty!";
182  continue;
183  }
184  isMappedToJet = fastJetToPFCandMap[ijet].count(pfCand.key());
185  } else {
186  isMappedToJet = true;
187  }
188  if ( reco::deltaR2(*jetRef, *pfCand) < deltaR2_ && isMappedToJet ) newJet.addDaughter(pfCand);
189  }
190  if ( verbosity_ ) {
191  std::cout << "jet #" << ijet << ": Pt = " << jetRef->pt() << ", eta = " << jetRef->eta() << ", 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() << ", phi = " << jetConstituent->phi() << std::endl;
197  ++idx;
198  }
199  }
200  // Match the index of the jet we just made to the index into the original
201  // collection.
202  //matchInfo[jetRef.key()] = ijet;
203  matchInfo[jetRef.key()] = nNewJets;
204  nNewJets++;
205  }
206 
207  // Put our new jets into the event
208  edm::OrphanHandle<std::vector<JetType> > newJetsInEvent = evt.put(std::move(newJets), "jets");
209 
210  // Create a matching between original jets -> extra collection
211  auto matching = (nJets !=0) ? std::make_unique<JetMatchMap>(edm::makeRefToBaseProdFrom(edm::RefToBase<reco::Jet>(jets[0]), evt), newJetsInEvent) : std::make_unique<JetMatchMap>();
212  for (size_t ijet = 0; ijet < nJets; ++ijet) {
213  matching->insert(edm::RefToBase<reco::Jet>(jets[ijet]), edm::RefToBase<reco::Jet>(edm::Ref<std::vector<JetType> >(newJetsInEvent, matchInfo[ijet])));
214  }
215  evt.put(std::move(matching));
216 }
217 
218 template<class JetType, class CandType>
219 void
221  // RecoTauGenericJetRegionProducer
223  desc.add<edm::InputTag>("src", edm::InputTag("ak4PFJets"));
224  desc.add<double>("deltaR", 0.8);
225  desc.add<edm::InputTag>("pfCandAssocMapSrc", edm::InputTag(""));
226  desc.add<int>("verbosity", 0);
227  desc.add<double>("maxJetAbsEta", 2.5);
228  desc.add<double>("minJetPt", 14.0);
229  desc.add<edm::InputTag>("pfCandSrc", edm::InputTag("particleFlow"));
230  descriptions.add(name, desc);
231 }
232 
233 
234 template<>
235 void
237  // RecoTauGenericJetRegionProducer
238  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauJetRegionProducer");
239 
240 }
241 
242 template<>
243 void
245  // RecoTauGenericJetRegionProducer
246  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauPatJetRegionProducer");
247 }
248 
251 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
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:517
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:263
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:122
vector< PseudoJet > jets
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:326
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
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:107
def move(src, dest)
Definition: eostools.py:511