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")) {
73  inputJets_ = cfg.getParameter<edm::InputTag>("src");
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");
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>(
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 
ConfigurationDescriptions.h
RecoTauJetRegionProducer
RecoTauGenericJetRegionProducer< reco::PFJet, reco::PFCandidate > RecoTauJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:252
RecoTauGenericJetRegionProducer::moduleLabel_
std::string moduleLabel_
Definition: RecoTauGenericJetRegionProducer.cc:53
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
MessageLogger.h
RecoTauGenericJetRegionProducer::maxJetAbsEta_
double maxJetAbsEta_
Definition: RecoTauGenericJetRegionProducer.cc:64
ESHandle.h
PFCandidate.h
edm::EDGetTokenT
Definition: EDGetToken.h:33
AssociationMap.h
gather_cfg.cout
cout
Definition: gather_cfg.py:144
RecoTauGenericJetRegionProducer::pfCandSrc_
edm::InputTag pfCandSrc_
Definition: RecoTauGenericJetRegionProducer.cc:56
PFJet.h
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
RecoTauGenericJetRegionProducer::~RecoTauGenericJetRegionProducer
~RecoTauGenericJetRegionProducer() override
Definition: RecoTauGenericJetRegionProducer.cc:45
EDProducer.h
RecoTauGenericJetRegionProducer::verbosity_
int verbosity_
Definition: RecoTauGenericJetRegionProducer.cc:67
singleTopDQM_cfi.jets
jets
Definition: singleTopDQM_cfi.py:42
RecoTauGenericJetRegionProducer::pfCandAssocMap_token
edm::EDGetTokenT< JetToCandidateAssociation > pfCandAssocMap_token
Definition: RecoTauGenericJetRegionProducer.cc:61
edm::RefVector
Definition: EDProductfwd.h:27
Association.h
ConeTools.h
RecoTauPatJetRegionProducer
RecoTauGenericJetRegionProducer< pat::Jet, pat::PackedCandidate > RecoTauPatJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:253
TtSemiLepJetCombMVATrainer_cfi.matching
matching
Definition: TtSemiLepJetCombMVATrainer_cfi.py:13
edm::Handle
Definition: AssociativeIterator.h:50
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
training_settings.idx
idx
Definition: training_settings.py:16
edm::Ref
Definition: AssociativeIterator.h:58
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
RecoTauCommonUtilities.h
RecoTauGenericJetRegionProducer::JetToCandidateAssociation
edm::AssociationMap< edm::OneToMany< std::vector< JetType >, std::vector< CandType >, unsigned int > > JetToCandidateAssociation
Definition: RecoTauGenericJetRegionProducer.cc:43
ParameterSetDescription.h
RecoTauGenericJetRegionProducer::pf_token
edm::EDGetTokenT< std::vector< CandType > > pf_token
Definition: RecoTauGenericJetRegionProducer.cc:59
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LogWarning
Definition: MessageLogger.h:141
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
edm::LogError
Definition: MessageLogger.h:183
Event.h
edm::makeRefToBaseProdFrom
RefToBaseProd< T > makeRefToBaseProdFrom(RefToBase< T > const &iRef, Event const &iEvent)
Definition: makeRefToBaseProdFrom.h:34
edm::AssociationMap
Definition: AssociationMap.h:48
PackedCandidate.h
reco::deltaR2
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
RecoTauGenericJetRegionProducer::JetMatchMap
edm::AssociationMap< edm::OneToOne< reco::JetView, reco::JetView > > JetMatchMap
Definition: RecoTauGenericJetRegionProducer.cc:41
RecoTauGenericJetRegionProducer::fillDescriptionsBase
static void fillDescriptionsBase(edm::ConfigurationDescriptions &descriptions, const std::string &name)
Definition: RecoTauGenericJetRegionProducer.cc:224
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:132
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:57
makeRefToBaseProdFrom.h
Jet.h
edm::Ptr
Definition: AssociationVector.h:31
looper.cfg
cfg
Definition: looper.py:297
RecoTauGenericJetRegionProducer::deltaR2_
double deltaR2_
Definition: RecoTauGenericJetRegionProducer.cc:65
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::OrphanHandle
Definition: EDProductfwd.h:39
RecoTauGenericJetRegionProducer::inputJets_
edm::InputTag inputJets_
Definition: RecoTauGenericJetRegionProducer.cc:55
RecoTauGenericJetRegionProducer::Jets_token
edm::EDGetTokenT< reco::CandidateView > Jets_token
Definition: RecoTauGenericJetRegionProducer.cc:60
fftjetcms::JetType
JetType
Definition: JetType.h:7
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
RecoTauGenericJetRegionProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::RefToBase< reco::Jet >
edm::Ref::key
key_type key() const
Accessor for product key.
Definition: Ref.h:250
cms::Exception
Definition: Exception.h:70
RecoTauGenericJetRegionProducer::RecoTauGenericJetRegionProducer
RecoTauGenericJetRegionProducer(const edm::ParameterSet &pset)
Definition: RecoTauGenericJetRegionProducer.cc:71
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
RecoTauGenericJetRegionProducer::minJetPt_
double minJetPt_
Definition: RecoTauGenericJetRegionProducer.cc:63
RecoTauGenericJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:39
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::Event
Definition: Event.h:73
edm::Event::get
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:337
edm::InputTag
Definition: InputTag.h:15
RecoTauGenericJetRegionProducer::pfCandAssocMapSrc_
edm::InputTag pfCandAssocMapSrc_
Definition: RecoTauGenericJetRegionProducer.cc:57
edm::ProductID
Definition: ProductID.h:27
RecoTauGenericJetRegionProducer::produce
void produce(edm::Event &evt, const edm::EventSetup &es) override
Definition: RecoTauGenericJetRegionProducer.cc:93
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
PFCandidateFwd.h
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37