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 
18 
21 
29 
32 
33 #include <string>
34 #include <iostream>
35 
36 template <class JetType, class CandType>
38 public:
40  typedef edm::AssociationMap<edm::OneToMany<std::vector<JetType>, std::vector<CandType>, unsigned int> >
44 
45  void produce(edm::Event& evt, const edm::EventSetup& es) override;
46 
47  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
48  static void fillDescriptionsBase(edm::ConfigurationDescriptions& descriptions, const std::string& name);
49 
50 private:
52 
56 
60 
61  double minJetPt_;
62  double maxJetAbsEta_;
63  double deltaR2_;
64 
66 };
67 
68 template <class JetType, class CandType>
70  : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
71  inputJets_ = cfg.getParameter<edm::InputTag>("src");
72  pfCandSrc_ = cfg.getParameter<edm::InputTag>("pfCandSrc");
73  pfCandAssocMapSrc_ = cfg.getParameter<edm::InputTag>("pfCandAssocMapSrc");
74 
75  pf_token = consumes<std::vector<CandType> >(pfCandSrc_);
76  Jets_token = consumes<reco::CandidateView>(inputJets_);
77  pfCandAssocMap_token = consumes<JetToCandidateAssociation>(pfCandAssocMapSrc_);
78 
79  double deltaR = cfg.getParameter<double>("deltaR");
81  minJetPt_ = cfg.getParameter<double>("minJetPt");
82  maxJetAbsEta_ = cfg.getParameter<double>("maxJetAbsEta");
83 
84  verbosity_ = cfg.getParameter<int>("verbosity");
85 
86  produces<std::vector<JetType> >("jets");
87  produces<JetMatchMap>();
88 }
89 
90 template <class JetType, class CandType>
92  if (verbosity_) {
93  std::cout << "<RecoTauJetRegionProducer::produce (moduleLabel = " << moduleLabel_ << ")>:" << std::endl;
94  std::cout << " inputJets = " << inputJets_ << std::endl;
95  std::cout << " pfCandSrc = " << pfCandSrc_ << std::endl;
96  std::cout << " pfCandAssocMapSrc_ = " << pfCandAssocMapSrc_ << std::endl;
97  }
98 
99  edm::Handle<std::vector<CandType> > pfCandsHandle;
100  evt.getByToken(pf_token, pfCandsHandle);
101 
102  // Build Ptrs for all the PFCandidates
103  typedef edm::Ptr<CandType> CandPtr;
104  std::vector<CandPtr> pfCands;
105  pfCands.reserve(pfCandsHandle->size());
106  for (size_t icand = 0; icand < pfCandsHandle->size(); ++icand) {
107  pfCands.push_back(CandPtr(pfCandsHandle, icand));
108  }
109 
110  // Get the jets
112  evt.getByToken(Jets_token, jetView);
113  // Convert to a vector of JetRefs
114  edm::RefVector<std::vector<JetType> > jets = reco::tau::castView<edm::RefVector<std::vector<JetType> > >(jetView);
115  size_t nJets = jets.size();
116 
117  // Get the association map matching jets to Candidates
118  // (needed for reconstruction of boosted taus)
120  std::vector<std::unordered_set<unsigned> > fastJetToPFCandMap;
121  if (!pfCandAssocMapSrc_.label().empty()) {
122  evt.getByToken(pfCandAssocMap_token, jetToPFCandMap);
123  fastJetToPFCandMap.resize(nJets);
124  for (size_t ijet = 0; ijet < nJets; ++ijet) {
125  // Get a ref to jet
126  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
127  const auto& pfCandsMappedToJet = (*jetToPFCandMap)[jetRef];
128  for (const auto& pfCandMappedToJet : pfCandsMappedToJet) {
129  fastJetToPFCandMap[ijet].emplace(pfCandMappedToJet.key());
130  }
131  }
132  }
133 
134  // Get the original product, so we can match against it - otherwise the
135  // indices don't match up.
136  edm::ProductID originalId = jets.id();
137  edm::Handle<std::vector<JetType> > originalJets;
138  size_t nOriginalJets = 0;
139  // We have to make sure that we have some selected jets, otherwise we don't
140  // actually have a valid product ID to the original jets.
141  if (nJets) {
142  try {
143  evt.get(originalId, originalJets);
144  } catch (const cms::Exception& e) {
145  edm::LogError("MissingOriginalCollection") << "Can't get the original jets that made: " << inputJets_
146  << " that have product ID: " << originalId << " from the event!!";
147  throw e;
148  }
149  nOriginalJets = originalJets->size();
150  }
151 
152  auto newJets = std::make_unique<std::vector<JetType> >();
153 
154  // Keep track of the indices of the current jet and the old (original) jet
155  // -1 indicates no match.
156  std::vector<int> matchInfo(nOriginalJets, -1);
157  newJets->reserve(nJets);
158  size_t nNewJets = 0;
159  for (size_t ijet = 0; ijet < nJets; ++ijet) {
160  // Get a ref to jet
161  const edm::Ref<std::vector<JetType> >& jetRef = jets[ijet];
162  if (jetRef->pt() - minJetPt_ < 1e-5)
163  continue;
164  if (std::abs(jetRef->eta()) - maxJetAbsEta_ > -1e-5)
165  continue;
166  // Make an initial copy.
167  newJets->emplace_back(*jetRef);
168  JetType& newJet = newJets->back();
169  // Clear out all the constituents
170  newJet.clearDaughters();
171  // Loop over all the PFCands
172  for (const auto& pfCand : pfCands) {
173  bool isMappedToJet = false;
174  if (jetToPFCandMap.isValid()) {
175  auto temp = jetToPFCandMap->find(jetRef);
176  if (temp == jetToPFCandMap->end()) {
177  edm::LogWarning("WeirdCandidateMap") << "Candidate map for jet " << jetRef.key() << " is empty!";
178  continue;
179  }
180  isMappedToJet = fastJetToPFCandMap[ijet].count(pfCand.key());
181  } else {
182  isMappedToJet = true;
183  }
184  if (reco::deltaR2(*jetRef, *pfCand) < deltaR2_ && isMappedToJet)
185  newJet.addDaughter(pfCand);
186  }
187  if (verbosity_) {
188  std::cout << "jet #" << ijet << ": Pt = " << jetRef->pt() << ", eta = " << jetRef->eta()
189  << ", phi = " << jetRef->eta() << ","
190  << " mass = " << jetRef->mass() << ", area = " << jetRef->jetArea() << std::endl;
191  auto jetConstituents = newJet.daughterPtrVector();
192  int idx = 0;
193  for (const auto& jetConstituent : jetConstituents) {
194  std::cout << " constituent #" << idx << ": Pt = " << jetConstituent->pt() << ", eta = " << jetConstituent->eta()
195  << ", phi = " << jetConstituent->phi() << std::endl;
196  ++idx;
197  }
198  }
199  // Match the index of the jet we just made to the index into the original
200  // collection.
201  //matchInfo[jetRef.key()] = ijet;
202  matchInfo[jetRef.key()] = nNewJets;
203  nNewJets++;
204  }
205 
206  // Put our new jets into the event
207  edm::OrphanHandle<std::vector<JetType> > newJetsInEvent = evt.put(std::move(newJets), "jets");
208 
209  // Create a matching between original jets -> extra collection
210  auto matching = (nJets != 0)
211  ? std::make_unique<JetMatchMap>(
213  : std::make_unique<JetMatchMap>();
214  for (size_t ijet = 0; ijet < nJets; ++ijet) {
215  matching->insert(edm::RefToBase<reco::Jet>(jets[ijet]),
216  edm::RefToBase<reco::Jet>(edm::Ref<std::vector<JetType> >(newJetsInEvent, matchInfo[ijet])));
217  }
218  evt.put(std::move(matching));
219 }
220 
221 template <class JetType, class CandType>
223  edm::ConfigurationDescriptions& descriptions, const std::string& name) {
224  // RecoTauGenericJetRegionProducer
226  desc.add<edm::InputTag>("src", edm::InputTag("ak4PFJets"));
227  desc.add<double>("deltaR", 0.8);
228  desc.add<edm::InputTag>("pfCandAssocMapSrc", edm::InputTag(""));
229  desc.add<int>("verbosity", 0);
230  desc.add<double>("maxJetAbsEta", 2.5);
231  desc.add<double>("minJetPt", 14.0);
232  desc.add<edm::InputTag>("pfCandSrc", edm::InputTag("particleFlow"));
233  descriptions.add(name, desc);
234 }
235 
236 template <>
238  edm::ConfigurationDescriptions& descriptions) {
239  // RecoTauGenericJetRegionProducer
240  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauJetRegionProducer");
241 }
242 
243 template <>
245  edm::ConfigurationDescriptions& descriptions) {
246  // RecoTauGenericJetRegionProducer
247  RecoTauGenericJetRegionProducer::fillDescriptionsBase(descriptions, "RecoTauPatJetRegionProducer");
248 }
249 
252 
ConfigurationDescriptions.h
RecoTauJetRegionProducer
RecoTauGenericJetRegionProducer< reco::PFJet, reco::PFCandidate > RecoTauJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:250
RecoTauGenericJetRegionProducer::moduleLabel_
std::string moduleLabel_
Definition: RecoTauGenericJetRegionProducer.cc:51
MessageLogger.h
RecoTauGenericJetRegionProducer::maxJetAbsEta_
double maxJetAbsEta_
Definition: RecoTauGenericJetRegionProducer.cc:62
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:54
PFJet.h
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89285
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
RecoTauGenericJetRegionProducer::~RecoTauGenericJetRegionProducer
~RecoTauGenericJetRegionProducer() override
Definition: RecoTauGenericJetRegionProducer.cc:43
EDProducer.h
RecoTauGenericJetRegionProducer::verbosity_
int verbosity_
Definition: RecoTauGenericJetRegionProducer.cc:65
singleTopDQM_cfi.jets
jets
Definition: singleTopDQM_cfi.py:42
RecoTauGenericJetRegionProducer::pfCandAssocMap_token
edm::EDGetTokenT< JetToCandidateAssociation > pfCandAssocMap_token
Definition: RecoTauGenericJetRegionProducer.cc:59
edm::RefVector
Definition: EDProductfwd.h:27
Association.h
ConeTools.h
RecoTauPatJetRegionProducer
RecoTauGenericJetRegionProducer< pat::Jet, pat::PackedCandidate > RecoTauPatJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:251
TtSemiLepJetCombMVATrainer_cfi.matching
matching
Definition: TtSemiLepJetCombMVATrainer_cfi.py:13
edm::Handle
Definition: AssociativeIterator.h:50
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
edm::Ref
Definition: AssociativeIterator.h:58
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
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:535
RecoTauCommonUtilities.h
RecoTauGenericJetRegionProducer::JetToCandidateAssociation
edm::AssociationMap< edm::OneToMany< std::vector< JetType >, std::vector< CandType >, unsigned int > > JetToCandidateAssociation
Definition: RecoTauGenericJetRegionProducer.cc:41
ParameterSetDescription.h
RecoTauGenericJetRegionProducer::pf_token
edm::EDGetTokenT< std::vector< CandType > > pf_token
Definition: RecoTauGenericJetRegionProducer.cc:57
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::ParameterSet
Definition: ParameterSet.h:47
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:39
RecoTauGenericJetRegionProducer::fillDescriptionsBase
static void fillDescriptionsBase(edm::ConfigurationDescriptions &descriptions, const std::string &name)
Definition: RecoTauGenericJetRegionProducer.cc:222
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:58
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
makeRefToBaseProdFrom.h
Jet.h
edm::Ptr
Definition: AssociationVector.h:31
looper.cfg
cfg
Definition: looper.py:297
RecoTauGenericJetRegionProducer::deltaR2_
double deltaR2_
Definition: RecoTauGenericJetRegionProducer.cc:63
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
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:53
RecoTauGenericJetRegionProducer::Jets_token
edm::EDGetTokenT< reco::CandidateView > Jets_token
Definition: RecoTauGenericJetRegionProducer.cc:58
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:69
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
RecoTauGenericJetRegionProducer::minJetPt_
double minJetPt_
Definition: RecoTauGenericJetRegionProducer.cc:61
RecoTauGenericJetRegionProducer
Definition: RecoTauGenericJetRegionProducer.cc:37
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:342
edm::InputTag
Definition: InputTag.h:15
RecoTauGenericJetRegionProducer::pfCandAssocMapSrc_
edm::InputTag pfCandAssocMapSrc_
Definition: RecoTauGenericJetRegionProducer.cc:55
edm::ProductID
Definition: ProductID.h:27
RecoTauGenericJetRegionProducer::produce
void produce(edm::Event &evt, const edm::EventSetup &es) override
Definition: RecoTauGenericJetRegionProducer.cc:91
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
PFCandidateFwd.h
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37