CMS 3D CMS Logo

PFTauSelectorDefinition.h
Go to the documentation of this file.
1 #ifndef RecoTauTag_RecoTau_PFTauSelectorDefinition
2 #define RecoTauTag_RecoTau_PFTauSelectorDefinition
3 
8 
14 
16 
17 #include <memory>
18 #include <iostream>
19 
23  typedef std::vector<const reco::PFTau*> container;
24  typedef container::const_iterator const_iterator;
25 
26  struct DiscCutPair {
29  double cut;
30  };
34  std::vector<std::string> rawLabels;
35  std::vector<std::pair<int, double>> rawCuts;
36  std::vector<std::string> wpLabels;
37  std::vector<int> wpCuts;
38  };
39  typedef std::vector<DiscCutPair> DiscCutPairVec;
40  typedef std::vector<DiscContainerCutPair> DiscContainerCutPairVec;
41 
43  auto const& discriminators = cfg.getParameter<std::vector<edm::ParameterSet>>("discriminators");
44  auto const& discriminatorContainers = cfg.getParameter<std::vector<edm::ParameterSet>>("discriminatorContainers");
45  // Build each of our cuts
46  for (auto const& pset : discriminators) {
47  DiscCutPair newCut;
48  newCut.inputToken = iC.consumes<reco::PFTauDiscriminator>(pset.getParameter<edm::InputTag>("discriminator"));
49  newCut.cut = pset.getParameter<double>("selectionCut");
50  discriminators_.push_back(newCut);
51  }
52  for (auto const& pset : discriminatorContainers) {
53  DiscContainerCutPair newCut;
54  newCut.inputToken =
55  iC.consumes<reco::TauDiscriminatorContainer>(pset.getParameter<edm::InputTag>("discriminator"));
56  auto const& rawLabels = pset.getParameter<std::vector<std::string>>("rawValues");
57  auto const& rawCutValues = pset.getParameter<std::vector<double>>("selectionCuts");
58  if (rawLabels.size() != rawCutValues.size()) {
59  throw cms::Exception("PFTauSelectorBadHandle")
60  << "unequal number of TauIDContainer raw value indices and cut values given to PFTauSelector.";
61  }
62  for (size_t i = 0; i < rawLabels.size(); i++) {
63  newCut.rawCuts.push_back(std::pair<int, double>(-99, rawCutValues[i]));
64  newCut.rawLabels.push_back(rawLabels[i]);
65  }
66  newCut.wpLabels = pset.getParameter<std::vector<std::string>>("workingPoints");
67  newCut.wpCuts.resize(newCut.wpLabels.size());
68  discriminatorContainers_.push_back(newCut);
69  }
70 
71  // Build a string cut if desired
72  cut_ = std::make_unique<StringCutObjectSelector<reco::PFTau>>(cfg.getParameter<std::string>("cut"));
73  }
74 
75  const_iterator begin() const { return selected_.begin(); }
76  const_iterator end() const { return selected_.end(); }
77 
78  void select(const HandleToCollection& hc, const edm::Event& e, const edm::EventSetup& s) {
79  selected_.clear();
80 
81  if (!hc.isValid()) {
82  throw cms::Exception("PFTauSelectorBadHandle")
83  << "an invalid PFTau handle with ProductID" << hc.id() << " passed to PFTauSelector.";
84  }
85 
86  // Load each discriminator
87  for (auto& disc : discriminators_) {
88  e.getByToken(disc.inputToken, disc.handle);
89  }
90  for (auto& disc : discriminatorContainers_) {
91  e.getByToken(disc.inputToken, disc.handle);
92  }
93  // Retrieve ID container indices if config history changes, in particular for the first event.
94  if (phID_ != e.processHistoryID()) {
95  phID_ = e.processHistoryID();
96  for (auto& disc : discriminatorContainers_) {
97  auto const& psetsFromProvenance = edm::parameterSet(disc.handle.provenance()->stable(), e.processHistory());
98  // find raw value indices
99  if (psetsFromProvenance.exists("rawValues")) {
100  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("rawValues");
101  for (size_t i = 0; i < disc.rawLabels.size(); ++i) {
102  bool found = false;
103  for (size_t j = 0; j < idlist.size(); ++j) {
104  if (disc.rawLabels[i] == idlist[j]) {
105  found = true;
106  disc.rawCuts[i].first = j;
107  }
108  }
109  if (!found)
110  throw cms::Exception("Configuration")
111  << "PFTauSelector: Requested working point '" << disc.rawLabels[i] << "' not found!\n";
112  }
113  } else if (psetsFromProvenance.exists("IDdefinitions")) {
114  auto const idlist = psetsFromProvenance.getParameter<std::vector<edm::ParameterSet>>("IDdefinitions");
115  for (size_t i = 0; i < disc.rawLabels.size(); ++i) {
116  bool found = false;
117  for (size_t j = 0; j < idlist.size(); ++j) {
118  if (disc.rawLabels[i] == idlist[j].getParameter<std::string>("IDname")) {
119  found = true;
120  disc.rawCuts[i].first = j;
121  }
122  }
123  if (!found)
124  throw cms::Exception("Configuration")
125  << "PFTauSelector: Requested working point '" << disc.rawLabels[i] << "' not found!\n";
126  }
127  } else if (psetsFromProvenance.exists("VSjetWP")) {
128  // This else statement was added in the update of the HLT paths to use DeepTauId instead of charged isolation. Because the DeepTauId module is shared
129  // with offline and its psetsFromProvenance do not contain any of the above but only VSxWP, this selector would automatically go to the else statement
130  // that follows this one and fail. It is left empty because at HLT the raw values are not used and no action is needed here.
131  } else
132  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID list found in provenace config!\n";
133  // find working point indices
134  if (psetsFromProvenance.exists("workingPoints")) {
135  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("workingPoints");
136  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
137  bool found = false;
138  for (size_t j = 0; j < idlist.size(); ++j) {
139  if (disc.wpLabels[i] == idlist[j]) {
140  found = true;
141  disc.wpCuts[i] = j;
142  }
143  }
144  if (!found)
145  throw cms::Exception("Configuration")
146  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
147  }
148  } else if (psetsFromProvenance.exists("IDWPdefinitions")) {
149  auto const idlist = psetsFromProvenance.getParameter<std::vector<edm::ParameterSet>>("IDWPdefinitions");
150  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
151  bool found = false;
152  for (size_t j = 0; j < idlist.size(); ++j) {
153  if (disc.wpLabels[i] == idlist[j].getParameter<std::string>("IDname")) {
154  found = true;
155  disc.wpCuts[i] = j;
156  }
157  }
158  if (!found)
159  throw cms::Exception("Configuration")
160  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
161  }
162  } else if (psetsFromProvenance.exists("VSjetWP")) {
163  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("VSjetWP");
164  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
165  bool found = false;
166  for (size_t j = 0; j < idlist.size(); ++j) {
167  if (disc.wpLabels[i] == idlist[j]) {
168  found = true;
169  disc.wpCuts[i] = j;
170  }
171  }
172  if (!found)
173  throw cms::Exception("Configuration")
174  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
175  }
176  } else
177  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID WP list found in provenace config!\n";
178  }
179  }
180 
181  const size_t nTaus = hc->size();
182  for (size_t iTau = 0; iTau < nTaus; ++iTau) {
183  bool passed = true;
184  reco::PFTauRef tau(hc, iTau);
185  // Check if it passed all the discrimiantors
186  for (auto const& disc : discriminators_) {
187  // Check this discriminator passes
188  if (!((*disc.handle)[tau] > disc.cut)) {
189  passed = false;
190  break;
191  }
192  }
193  if (passed) { // if passed so far, check other discriminators
194  for (auto const& disc : discriminatorContainers_) {
195  for (auto const& rawCut : disc.rawCuts) {
196  if (!((*disc.handle)[tau].rawValues.at(rawCut.first) > rawCut.second)) {
197  passed = false;
198  break;
199  }
200  }
201  if (!passed)
202  break;
203  for (auto const& wpCut : disc.wpCuts) {
204  if (!((*disc.handle)[tau].workingPoints.at(wpCut))) {
205  passed = false;
206  break;
207  }
208  }
209  if (!passed)
210  break;
211  }
212  }
213 
214  if (passed && cut_.get()) {
215  passed = (*cut_)(*tau);
216  }
217 
218  if (passed)
219  selected_.push_back(tau.get());
220  }
221  } // end select()
222 
223  size_t size() const { return selected_.size(); }
224 
225 private:
230 
231  std::unique_ptr<StringCutObjectSelector<reco::PFTau>> cut_;
232 };
233 
234 #endif
PFTauSelectorDefinition(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
reco::PFTauCollection collection
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::vector< PFTau > PFTauCollection
collection of PFTau objects
Definition: PFTauFwd.h:9
ProductID id() const
Definition: HandleBase.cc:29
const_iterator end() const
void select(const HandleToCollection &hc, const edm::Event &e, const edm::EventSetup &s)
edm::Handle< reco::TauDiscriminatorContainer > handle
ParameterSet const & parameterSet(StableProvenance const &provenance, ProcessHistory const &history)
Definition: Provenance.cc:11
container::const_iterator const_iterator
std::vector< DiscContainerCutPair > DiscContainerCutPairVec
std::unique_ptr< StringCutObjectSelector< reco::PFTau > > cut_
const_iterator begin() const
edm::Handle< reco::PFTauDiscriminator > handle
DiscContainerCutPairVec discriminatorContainers_
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken
bool isValid() const
Definition: HandleBase.h:70
std::vector< DiscCutPair > DiscCutPairVec
edm::Handle< collection > HandleToCollection
edm::EDGetTokenT< reco::TauDiscriminatorContainer > inputToken
std::vector< const reco::PFTau * > container
std::vector< std::pair< int, double > > rawCuts