CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauCleaner.cc
Go to the documentation of this file.
1 /*
2  * RecoTauCleaner
3  *
4  * Author: Evan K. Friis, UC Davis
5  *
6  * Given a input collection of PFTaus, produces an output collection of PFTaus
7  * such that no two PFTaus come from the same PFJet. If multiple taus in the
8  * collection come from the same PFJet, (dirty) they are ordered according to a
9  * list of cleaners. Each cleaner is a RecoTauCleanerPlugin, and returns a
10  * double corresponding to the 'quality' for a given tau - an example would be
11  * the level of isolation. The set of dirty taus is then ranked
12  * lexicographically by these cleaners, and the best one is placed in the
13  * output collection.
14  *
15  */
16 
17 #include <boost/ptr_container/ptr_vector.hpp>
18 #include <algorithm>
19 #include <memory>
20 
26 
29 
33 
40 
42 
43 template <typename Prod>
47  std::shared_ptr<Cleaner> plugin_;
48  float tolerance_;
49  };
50  typedef std::vector<std::unique_ptr<CleanerEntryType>> CleanerList;
51  // Define our output type - i.e. reco::PFTau OR reco::PFTauRef
52  typedef typename Prod::value_type output_type;
53 
54  // Predicate that determines if two taus 'overlap' i.e. share a base PFJet
56  public:
57  bool operator()(const reco::PFTauRef& a, const reco::PFTauRef& b) const { return (a->jetRef() == b->jetRef()); }
58  };
59 
60 public:
61  explicit RecoTauCleanerImpl(const edm::ParameterSet& pset);
62  ~RecoTauCleanerImpl() override;
63  void produce(edm::Event& evt, const edm::EventSetup& es) override;
64 
65  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
66 
67 private:
69  CleanerList cleaners_;
70  // Optional selection on the output of the taus
71  std::unique_ptr<const StringCutObjectSelector<reco::PFTau>> outputSelector_;
74 };
75 
76 template <typename Prod>
78  tauSrc_ = pset.getParameter<edm::InputTag>("src");
79  tau_token = consumes<reco::PFTauCollection>(tauSrc_);
80  // Build our list of quality plugins
81  typedef std::vector<edm::ParameterSet> VPSet;
82  // Get each of our tau builders
83  const VPSet& cleaners = pset.getParameter<VPSet>("cleaners");
84  for (VPSet::const_iterator cleanerPSet = cleaners.begin(); cleanerPSet != cleaners.end(); ++cleanerPSet) {
85  auto cleanerEntry = std::make_unique<CleanerEntryType>();
86  // Get plugin name
87  const std::string& pluginType = cleanerPSet->getParameter<std::string>("plugin");
88  // Build the plugin
89  cleanerEntry->plugin_ = RecoTauCleanerPluginFactory::get()->create(pluginType, *cleanerPSet, consumesCollector());
90  cleanerEntry->tolerance_ = cleanerPSet->getParameter<double>("tolerance");
91  cleaners_.emplace_back(std::move(cleanerEntry));
92  }
93 
94  // Check if we want to apply a final output selection
95  std::string selection = pset.getParameter<std::string>("outputSelection");
96  if (!selection.empty()) {
97  outputSelector_ = std::make_unique<StringCutObjectSelector<reco::PFTau>>(selection);
98  }
99 
100  // Enable/disable debug output
101  verbosity_ = pset.getParameter<int>("verbosity");
102 
103  // Build the predicate that ranks our taus.
104  produces<Prod>();
105 }
106 
107 template <typename Prod>
109 
110 namespace {
111  // Template to convert a ref to desired output type
112  template <typename T>
113  const T convert(const reco::PFTauRef& tau);
114 
115  //template<> const edm::RefToBase<reco::PFTau>
116  //convert<edm::RefToBase<reco::PFTau> >(const reco::PFTauRef &tau) {
117  // return edm::RefToBase<reco::PFTau>(tau);
118  //}
119 
120  template <>
121  const reco::PFTauRef convert<reco::PFTauRef>(const reco::PFTauRef& tau) {
122  return tau;
123  }
124 
125  template <>
126  const reco::PFTau convert<reco::PFTau>(const reco::PFTauRef& tau) {
127  return *tau;
128  }
129 } // namespace
130 
131 namespace {
132  template <typename T>
133  std::string format_vT(const std::vector<T>& vT) {
134  std::ostringstream os;
135  os << "{ ";
136  unsigned numEntries = vT.size();
137  for (unsigned iEntry = 0; iEntry < numEntries; ++iEntry) {
138  os << vT[iEntry];
139  if (iEntry < (numEntries - 1))
140  os << ", ";
141  }
142  os << " }";
143  return os.str();
144  }
145 
146  struct PFTauRankType {
147  PFTauRankType(const reco::PFTauRef& tauRef) : idx_(tauRef.key()), tauRef_(tauRef) {}
148  ~PFTauRankType() {}
149  void print(const std::string& label) const {
150  std::cout << label << " (" << tauRef_.id() << ":" << tauRef_.key() << ", idx = " << idx_ << "):";
151  assert(tauRef_.key() == idx_);
152  std::cout << " Pt = " << tauRef_->pt() << ", eta = " << tauRef_->eta() << ", phi = " << tauRef_->phi()
153  << ", mass = " << tauRef_->mass() << " (decayMode = " << tauRef_->decayMode() << ")";
154  std::cout << std::endl;
155  std::cout << "associated jet:";
156  if (tauRef_->jetRef().isNonnull()) {
157  std::cout << " Pt = " << tauRef_->jetRef()->pt() << ", eta = " << tauRef_->jetRef()->eta()
158  << ", phi = " << tauRef_->jetRef()->phi() << ", mass = " << tauRef_->jetRef()->mass()
159  << ", area = " << tauRef_->jetRef()->jetArea();
160  } else
161  std::cout << " N/A";
162  std::cout << std::endl;
163  const std::vector<reco::PFRecoTauChargedHadron>& signalTauChargedHadronCandidates =
164  tauRef_->signalTauChargedHadronCandidates();
165  size_t numChargedHadrons = signalTauChargedHadronCandidates.size();
166  for (size_t iChargedHadron = 0; iChargedHadron < numChargedHadrons; ++iChargedHadron) {
167  const reco::PFRecoTauChargedHadron& chargedHadron = signalTauChargedHadronCandidates.at(iChargedHadron);
168  std::cout << " chargedHadron #" << iChargedHadron << ":" << std::endl;
169  chargedHadron.print(std::cout);
170  }
171  const std::vector<reco::RecoTauPiZero>& signalPiZeroCandidates = tauRef_->signalPiZeroCandidates();
172  size_t numPiZeros = signalPiZeroCandidates.size();
173  std::cout << "signalPiZeroCandidates = " << numPiZeros << std::endl;
174  for (size_t iPiZero = 0; iPiZero < numPiZeros; ++iPiZero) {
175  const reco::RecoTauPiZero& piZero = signalPiZeroCandidates.at(iPiZero);
176  std::cout << " piZero #" << iPiZero << ": Pt = " << piZero.pt() << ", eta = " << piZero.eta()
177  << ", phi = " << piZero.phi() << ", mass = " << piZero.mass() << std::endl;
178  }
179  const auto& isolationCands = tauRef_->isolationCands();
180  size_t numCands = isolationCands.size();
181  std::cout << "isolationCands = " << numCands << std::endl;
182  for (size_t iCand = 0; iCand < numCands; ++iCand) {
183  const auto& cand = isolationCands.at(iCand);
184  std::cout << " pfCand #" << iCand << " (" << cand.id() << ":" << cand.key() << "):"
185  << " Pt = " << cand->pt() << ", eta = " << cand->eta() << ", phi = " << cand->phi() << std::endl;
186  }
187  std::cout << " ranks = " << format_vT(ranks_) << std::endl;
188  std::cout << " tolerances = " << format_vT(tolerances_) << std::endl;
189  }
190  size_t idx_;
191  reco::PFTauRef tauRef_;
192  size_t N_;
193  std::vector<float> ranks_;
194  std::vector<float> tolerances_;
195  };
196 
197  bool isHigherRank(const PFTauRankType* tau1, const PFTauRankType* tau2) {
198  //std::cout << "<isHigherRank>:" << std::endl;
199  //std::cout << "tau1 @ " << tau1;
200  //tau1->print("");
201  //std::cout << "tau2 @ " << tau2;
202  //tau2->print("");
203  assert(tau1->N_ == tau1->ranks_.size());
204  assert(tau1->N_ == tau2->ranks_.size());
205  assert(tau1->N_ == tau1->tolerances_.size());
206  for (size_t i = 0; i < tau1->N_; ++i) {
207  const float& val1 = tau1->ranks_[i];
208  const float& val2 = tau2->ranks_[i];
209  double av = 0.5 * (val1 + val2);
210  double thresh = av * tau1->tolerances_[i];
211  if (val1 < (val2 - thresh))
212  return true;
213  else if (val2 < (val1 - thresh))
214  return false;
215  }
216  return true;
217  }
218 } // namespace
219 
220 template <typename Prod>
222  if (verbosity_) {
223  std::cout << "<RecoTauCleanerImpl::produce>:" << std::endl;
224  }
225 
226  // Update all our cleaners with the event info if they need it
227  for (typename CleanerList::iterator cleaner = cleaners_.begin(); cleaner != cleaners_.end(); ++cleaner) {
228  (*cleaner)->plugin_->setup(evt, es);
229  }
230 
231  // Get the input collection of all taus. Some are from the same PFJet. We must clean them.
233  evt.getByToken(tau_token, inputTaus);
234 
235  // Sort the input tau refs according to our predicate
236  std::list<PFTauRankType*> rankedTaus;
237  size_t N = inputTaus->size();
238  for (size_t idx = 0; idx < N; ++idx) {
239  reco::PFTauRef inputRef(inputTaus, idx);
240  PFTauRankType* rankedTau = new PFTauRankType(inputRef);
241  rankedTau->N_ = cleaners_.size();
242  rankedTau->ranks_.reserve(rankedTau->N_);
243  rankedTau->tolerances_.reserve(rankedTau->N_);
244  for (typename CleanerList::const_iterator cleaner = cleaners_.begin(); cleaner != cleaners_.end(); ++cleaner) {
245  rankedTau->ranks_.push_back((*(*cleaner)->plugin_)(inputRef));
246  rankedTau->tolerances_.push_back((*cleaner)->tolerance_);
247  }
248  if (verbosity_) {
249  std::ostringstream os;
250  os << "rankedTau #" << idx;
251  rankedTau->print(os.str());
252  }
253  rankedTaus.push_back(rankedTau);
254  }
255  rankedTaus.sort(isHigherRank);
256 
257  // Make an STL algorithm friendly vector of refs
258  typedef std::vector<reco::PFTauRef> PFTauRefs;
259  PFTauRefs dirty(inputTaus->size());
260  size_t idx_sorted = 0;
261  for (std::list<PFTauRankType*>::const_iterator rankedTau = rankedTaus.begin(); rankedTau != rankedTaus.end();
262  ++rankedTau) {
263  dirty[idx_sorted] = (*rankedTau)->tauRef_;
264  if (verbosity_) {
265  std::cout << "dirty[" << idx_sorted << "] = " << dirty[idx_sorted].id() << ":" << dirty[idx_sorted].key()
266  << std::endl;
267  }
268  delete (*rankedTau);
269  ++idx_sorted;
270  }
271 
272  // Clean the taus, ensuring that only one tau per jet is produced
273  PFTauRefs cleanTaus = reco::tau::cleanOverlaps<PFTauRefs, RemoveDuplicateJets>(dirty);
274 
275  // create output collection
276  auto output = std::make_unique<Prod>();
277  //output->reserve(cleanTaus.size());
278 
279  // Copy clean refs into output
280  for (PFTauRefs::const_iterator tau = cleanTaus.begin(); tau != cleanTaus.end(); ++tau) {
281  // If we are applying an output selection, check if it passes
282  bool selected = true;
283  if (outputSelector_.get() && !(*outputSelector_)(**tau)) {
284  selected = false;
285  }
286  if (selected) {
287  output->push_back(convert<output_type>(*tau));
288  }
289  }
290  evt.put(std::move(output));
291 }
292 
295 
296 template <>
298  // RecoTauCleaner
300  desc.add<std::string>("outputSelection", "");
301  {
302  // this description is the validator for all PSets in the cleaners VPSet passed to the plugin from the python configuration
303  edm::ParameterSetDescription vps_description_for_cleaners;
304  // the common parameters for all cleaners
305  // no default value is provided -- the user has to provide the values for these parameters
306  vps_description_for_cleaners.add<std::string>("plugin");
307  vps_description_for_cleaners.add<double>("tolerance", 0);
308  vps_description_for_cleaners.add<std::string>("name");
309 
310  // the following parameters are not common for all cleaners, they are needed for few PSets, therefore they are added as optional
311  // these optional parameters are used in the default cleaners vector
312  vps_description_for_cleaners.addOptional<int>("passForCharge");
313  vps_description_for_cleaners.addOptional<double>("selectionFailValue");
314  vps_description_for_cleaners.addOptional<std::vector<unsigned int>>("nprongs");
315  vps_description_for_cleaners.addOptional<edm::InputTag>("src");
316  vps_description_for_cleaners.addOptional<double>("minTrackPt");
317  vps_description_for_cleaners.addOptional<std::string>("selection");
318  vps_description_for_cleaners.addOptional<std::string>("selectionPassFunction");
319  // more PSets for cleaners can be found in
320  // RecoTauTag/RecoTau/python/RecoTauCleanerPlugins.py
321  // however, at this moment (2018-11-09) they do not have any new optional parameters
322 
323  // the cleaner defaults, as in RecoTauTag/RecoTau/python/RecoTauCleaner_cfi.py
324  std::vector<edm::ParameterSet> default_cleaners;
325  default_cleaners.reserve(7);
326  {
327  edm::ParameterSet cleaner_Charge;
328  cleaner_Charge.addParameter<std::string>("name", "Charge");
329  cleaner_Charge.addParameter<std::string>("plugin", "RecoTauChargeCleanerPlugin");
330  cleaner_Charge.addParameter<int>("passForCharge", 1);
331  cleaner_Charge.addParameter<double>("selectionFailValue", 0);
332  cleaner_Charge.addParameter<std::vector<unsigned int>>("nprongs",
333  {
334  1,
335  3,
336  });
337  cleaner_Charge.addParameter<double>("tolerance", 0);
338  default_cleaners.push_back(cleaner_Charge);
339  }
340  {
341  edm::ParameterSet temp2;
342  temp2.addParameter<std::string>("name", "HPS_Select");
343  temp2.addParameter<std::string>("plugin", "RecoTauDiscriminantCleanerPlugin");
344  temp2.addParameter<edm::InputTag>("src", edm::InputTag("hpsSelectionDiscriminator"));
345  temp2.addParameter<double>("tolerance", 0);
346  default_cleaners.push_back(temp2);
347  }
348  {
349  edm::ParameterSet temp2;
350  temp2.addParameter<std::string>("name", "killSoftTwoProngTaus");
351  temp2.addParameter<std::string>("plugin", "RecoTauSoftTwoProngTausCleanerPlugin");
352  temp2.addParameter<double>("minTrackPt", 5.0);
353  temp2.addParameter<double>("tolerance", 0);
354  default_cleaners.push_back(temp2);
355  }
356  {
357  edm::ParameterSet temp2;
358  temp2.addParameter<std::string>("name", "ChargedHadronMultiplicity");
359  temp2.addParameter<std::string>("plugin", "RecoTauChargedHadronMultiplicityCleanerPlugin");
360  temp2.addParameter<double>("tolerance", 0);
361  default_cleaners.push_back(temp2);
362  }
363  {
364  edm::ParameterSet temp2;
365  temp2.addParameter<std::string>("name", "Pt");
366  temp2.addParameter<std::string>("plugin", "RecoTauStringCleanerPlugin");
367  temp2.addParameter<std::string>("selectionPassFunction", "-pt()");
368  temp2.addParameter<std::string>("selection", "leadPFCand().isNonnull()");
369  temp2.addParameter<double>("selectionFailValue", 1000.0);
370  temp2.addParameter<double>("tolerance", 0.01);
371  default_cleaners.push_back(temp2);
372  }
373  {
374  edm::ParameterSet temp2;
375  temp2.addParameter<std::string>("name", "StripMultiplicity");
376  temp2.addParameter<std::string>("plugin", "RecoTauStringCleanerPlugin");
377  temp2.addParameter<std::string>("selectionPassFunction", "-signalPiZeroCandidates().size()");
378  temp2.addParameter<std::string>("selection", "leadPFCand().isNonnull()");
379  temp2.addParameter<double>("selectionFailValue", 1000.0);
380  temp2.addParameter<double>("tolerance", 0);
381  default_cleaners.push_back(temp2);
382  }
383  {
384  edm::ParameterSet temp2;
385  temp2.addParameter<std::string>("name", "CombinedIsolation");
386  temp2.addParameter<std::string>("plugin", "RecoTauStringCleanerPlugin");
387  temp2.addParameter<std::string>("selectionPassFunction",
388  "isolationPFChargedHadrCandsPtSum() + isolationPFGammaCandsEtSum()");
389  temp2.addParameter<std::string>("selection", "leadPFCand().isNonnull()");
390  temp2.addParameter<double>("selectionFailValue", 1000.0);
391  temp2.addParameter<double>("tolerance", 0);
392  default_cleaners.push_back(temp2);
393  }
394 
395  desc.addVPSet("cleaners", vps_description_for_cleaners, default_cleaners);
396  }
397 
398  desc.add<int>("verbosity", 0);
399  desc.add<edm::InputTag>("src", edm::InputTag("combinatoricRecoTaus"));
400  descriptions.add("RecoTauCleaner", desc);
401 }
402 
403 template <>
405  // there was no cfi file for this plugin
406 }
407 
409 
T getParameter(std::string const &) const
RecoTauCleanerImpl< reco::PFTauRefVector > RecoTauRefCleaner
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
double eta() const final
momentum pseudorapidity
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
bool operator()(const reco::PFTauRef &a, const reco::PFTauRef &b) const
edm::InputTag tauSrc_
std::vector< std::unique_ptr< CleanerEntryType > > CleanerList
selection
main part
Definition: corrVsCorr.py:100
double pt() const final
transverse momentum
key_type key() const
Accessor for product key.
Definition: Ref.h:250
RecoTauCleanerImpl< reco::PFTauCollection > RecoTauCleaner
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
edm::EDGetTokenT< reco::PFTauCollection > tau_token
void print(std::ostream &stream=std::cout) const
char const * label
CleanerList cleaners_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Long64_t numEntries(TFile *hdl, std::string const &trname)
Definition: CollUtil.cc:50
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:124
ParameterDescriptionBase * add(U const &iLabel, T const &value)
def convert(infile, ofile)
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
reco::tau::RecoTauCleanerPlugin Cleaner
double a
Definition: hdecay.h:119
std::unique_ptr< const StringCutObjectSelector< reco::PFTau > > outputSelector_
void produce(edm::Event &evt, const edm::EventSetup &es) override
long double T
double phi() const final
momentum azimuthal angle
Prod::value_type output_type
~RecoTauCleanerImpl() override
def move(src, dest)
Definition: eostools.py:511
RecoTauCleanerImpl(const edm::ParameterSet &pset)
double mass() const final
mass
std::shared_ptr< Cleaner > plugin_