CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  if (cfg.exists("cut")) {
73  cut_ = std::make_unique<StringCutObjectSelector<reco::PFTau>>(cfg.getParameter<std::string>("cut"));
74  }
75  }
76 
77  const_iterator begin() const { return selected_.begin(); }
78  const_iterator end() const { return selected_.end(); }
79 
80  void select(const HandleToCollection& hc, const edm::Event& e, const edm::EventSetup& s) {
81  selected_.clear();
82 
83  if (!hc.isValid()) {
84  throw cms::Exception("PFTauSelectorBadHandle")
85  << "an invalid PFTau handle with ProductID" << hc.id() << " passed to PFTauSelector.";
86  }
87 
88  // Load each discriminator
89  for (auto& disc : discriminators_) {
90  e.getByToken(disc.inputToken, disc.handle);
91  }
92  for (auto& disc : discriminatorContainers_) {
93  e.getByToken(disc.inputToken, disc.handle);
94  }
95  // Retrieve ID container indices if config history changes, in particular for the first event.
96  if (phID_ != e.processHistoryID()) {
97  phID_ = e.processHistoryID();
98  for (auto& disc : discriminatorContainers_) {
99  auto const& psetsFromProvenance = edm::parameterSet(disc.handle.provenance()->stable(), e.processHistory());
100  // find raw value indices
101  if (psetsFromProvenance.exists("rawValues")) {
102  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("rawValues");
103  for (size_t i = 0; i < disc.rawLabels.size(); ++i) {
104  bool found = false;
105  for (size_t j = 0; j < idlist.size(); ++j) {
106  if (disc.rawLabels[i] == idlist[j]) {
107  found = true;
108  disc.rawCuts[i].first = j;
109  }
110  }
111  if (!found)
112  throw cms::Exception("Configuration")
113  << "PFTauSelector: Requested working point '" << disc.rawLabels[i] << "' not found!\n";
114  }
115  } else if (psetsFromProvenance.exists("IDdefinitions")) {
116  auto const idlist = psetsFromProvenance.getParameter<std::vector<edm::ParameterSet>>("IDdefinitions");
117  for (size_t i = 0; i < disc.rawLabels.size(); ++i) {
118  bool found = false;
119  for (size_t j = 0; j < idlist.size(); ++j) {
120  if (disc.rawLabels[i] == idlist[j].getParameter<std::string>("IDname")) {
121  found = true;
122  disc.rawCuts[i].first = j;
123  }
124  }
125  if (!found)
126  throw cms::Exception("Configuration")
127  << "PFTauSelector: Requested working point '" << disc.rawLabels[i] << "' not found!\n";
128  }
129  } else if (psetsFromProvenance.exists("VSjetWP")) {
130  // 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
131  // with offline and its psetsFromProvenance do not contain any of the above but only VSxWP, this selector would automatically go to the else statement
132  // 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.
133  } else
134  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID list found in provenace config!\n";
135  // find working point indices
136  if (psetsFromProvenance.exists("workingPoints")) {
137  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("workingPoints");
138  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
139  bool found = false;
140  for (size_t j = 0; j < idlist.size(); ++j) {
141  if (disc.wpLabels[i] == idlist[j]) {
142  found = true;
143  disc.wpCuts[i] = j;
144  }
145  }
146  if (!found)
147  throw cms::Exception("Configuration")
148  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
149  }
150  } else if (psetsFromProvenance.exists("IDWPdefinitions")) {
151  auto const idlist = psetsFromProvenance.getParameter<std::vector<edm::ParameterSet>>("IDWPdefinitions");
152  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
153  bool found = false;
154  for (size_t j = 0; j < idlist.size(); ++j) {
155  if (disc.wpLabels[i] == idlist[j].getParameter<std::string>("IDname")) {
156  found = true;
157  disc.wpCuts[i] = j;
158  }
159  }
160  if (!found)
161  throw cms::Exception("Configuration")
162  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
163  }
164  } else if (psetsFromProvenance.exists("VSjetWP")) {
165  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("VSjetWP");
166  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
167  bool found = false;
168  for (size_t j = 0; j < idlist.size(); ++j) {
169  if (disc.wpLabels[i] == idlist[j]) {
170  found = true;
171  disc.wpCuts[i] = j;
172  }
173  }
174  if (!found)
175  throw cms::Exception("Configuration")
176  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
177  }
178  } else
179  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID WP list found in provenace config!\n";
180  }
181  }
182 
183  const size_t nTaus = hc->size();
184  for (size_t iTau = 0; iTau < nTaus; ++iTau) {
185  bool passed = true;
186  reco::PFTauRef tau(hc, iTau);
187  // Check if it passed all the discrimiantors
188  for (auto const& disc : discriminators_) {
189  // Check this discriminator passes
190  if (!((*disc.handle)[tau] > disc.cut)) {
191  passed = false;
192  break;
193  }
194  }
195  if (passed) { // if passed so far, check other discriminators
196  for (auto const& disc : discriminatorContainers_) {
197  for (auto const& rawCut : disc.rawCuts) {
198  if (!((*disc.handle)[tau].rawValues.at(rawCut.first) > rawCut.second)) {
199  passed = false;
200  break;
201  }
202  }
203  if (!passed)
204  break;
205  for (auto const& wpCut : disc.wpCuts) {
206  if (!((*disc.handle)[tau].workingPoints.at(wpCut))) {
207  passed = false;
208  break;
209  }
210  }
211  if (!passed)
212  break;
213  }
214  }
215 
216  if (passed && cut_.get()) {
217  passed = (*cut_)(*tau);
218  }
219 
220  if (passed)
221  selected_.push_back(tau.get());
222  }
223  } // end select()
224 
225  size_t size() const { return selected_.size(); }
226 
227 private:
232 
233  std::unique_ptr<StringCutObjectSelector<reco::PFTau>> cut_;
234 };
235 
236 #endif
PFTauSelectorDefinition(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
reco::PFTauCollection collection
const_iterator begin() const
std::vector< PFTau > PFTauCollection
collection of PFTau objects
Definition: PFTauFwd.h:9
tuple cfg
Definition: looper.py:296
ProductID id() const
Definition: HandleBase.cc:29
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
bool exists(std::string const &parameterName) const
checks if a parameter exists
void select(const HandleToCollection &hc, const edm::Event &e, const edm::EventSetup &s)
edm::Handle< reco::TauDiscriminatorContainer > handle
container::const_iterator const_iterator
std::vector< DiscContainerCutPair > DiscContainerCutPairVec
std::unique_ptr< StringCutObjectSelector< reco::PFTau > > cut_
ParameterSet const & parameterSet(StableProvenance const &provenance, ProcessHistory const &history)
Definition: Provenance.cc:11
const_iterator end() const
edm::Handle< reco::PFTauDiscriminator > handle
tuple discriminatorContainers
DiscContainerCutPairVec discriminatorContainers_
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
bool isValid() const
Definition: HandleBase.h:70
ProcessHistory const & processHistory() const override
Definition: Event.cc:250
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ProcessHistoryID const & processHistoryID() const
Definition: Event.cc:116
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