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  if (cfg.exists("cut")) {
73  cut_.reset(new 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(), 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
130  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID list found in provenace config!\n";
131  // find working point indices
132  if (psetsFromProvenance.exists("workingPoints")) {
133  auto const idlist = psetsFromProvenance.getParameter<std::vector<std::string>>("workingPoints");
134  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
135  bool found = false;
136  for (size_t j = 0; j < idlist.size(); ++j) {
137  if (disc.wpLabels[i] == idlist[j]) {
138  found = true;
139  disc.wpCuts[i] = j;
140  }
141  }
142  if (!found)
143  throw cms::Exception("Configuration")
144  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
145  }
146  } else if (psetsFromProvenance.exists("IDWPdefinitions")) {
147  auto const idlist = psetsFromProvenance.getParameter<std::vector<edm::ParameterSet>>("IDWPdefinitions");
148  for (size_t i = 0; i < disc.wpLabels.size(); ++i) {
149  bool found = false;
150  for (size_t j = 0; j < idlist.size(); ++j) {
151  if (disc.wpLabels[i] == idlist[j].getParameter<std::string>("IDname")) {
152  found = true;
153  disc.wpCuts[i] = j;
154  }
155  }
156  if (!found)
157  throw cms::Exception("Configuration")
158  << "PFTauSelector: Requested working point '" << disc.wpLabels[i] << "' not found!\n";
159  }
160  } else
161  throw cms::Exception("Configuration") << "PFTauSelector: No suitable ID list found in provenace config!\n";
162  }
163  }
164 
165  const size_t nTaus = hc->size();
166  for (size_t iTau = 0; iTau < nTaus; ++iTau) {
167  bool passed = true;
168  reco::PFTauRef tau(hc, iTau);
169  // Check if it passed all the discrimiantors
170  for (auto const& disc : discriminators_) {
171  // Check this discriminator passes
172  if (!((*disc.handle)[tau] > disc.cut)) {
173  passed = false;
174  break;
175  }
176  }
177  if (passed) { // if passed so far, check other discriminators
178  for (auto const& disc : discriminatorContainers_) {
179  for (auto const& rawCut : disc.rawCuts) {
180  if (!((*disc.handle)[tau].rawValues.at(rawCut.first) > rawCut.second)) {
181  passed = false;
182  break;
183  }
184  }
185  if (!passed)
186  break;
187  for (auto const& wpCut : disc.wpCuts) {
188  if (!((*disc.handle)[tau].workingPoints.at(wpCut))) {
189  passed = false;
190  break;
191  }
192  }
193  if (!passed)
194  break;
195  }
196  }
197 
198  if (passed && cut_.get()) {
199  passed = (*cut_)(*tau);
200  }
201 
202  if (passed)
203  selected_.push_back(tau.get());
204  }
205  } // end select()
206 
207  size_t size() const { return selected_.size(); }
208 
209 private:
214 
215  std::unique_ptr<StringCutObjectSelector<reco::PFTau>> cut_;
216 };
217 
218 #endif
PFTauSelectorDefinition::discriminators_
DiscCutPairVec discriminators_
Definition: PFTauSelectorDefinition.h:211
ProcessHistoryID.h
mps_fire.i
i
Definition: mps_fire.py:355
PFTauDiscriminator.h
metsig::tau
Definition: SignAlgoResolutions.h:49
edm::EDGetTokenT< reco::PFTauDiscriminator >
PFTauSelectorDefinition::DiscCutPair::cut
double cut
Definition: PFTauSelectorDefinition.h:29
PDWG_TauSkim_cff.discriminators
discriminators
Definition: PDWG_TauSkim_cff.py:6
reco::PFTauDiscriminator
Definition: PFTauDiscriminator.h:12
PFTauSelectorDefinition::phID_
edm::ProcessHistoryID phID_
Definition: PFTauSelectorDefinition.h:213
PFTauSelectorDefinition::DiscContainerCutPair::wpLabels
std::vector< std::string > wpLabels
Definition: PFTauSelectorDefinition.h:36
PFTauSelectorDefinition::DiscContainerCutPairVec
std::vector< DiscContainerCutPair > DiscContainerCutPairVec
Definition: PFTauSelectorDefinition.h:40
PFTauSelectorDefinition::DiscCutPair::handle
edm::Handle< reco::PFTauDiscriminator > handle
Definition: PFTauSelectorDefinition.h:27
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
edm::Handle
Definition: AssociativeIterator.h:50
PFTauSelectorDefinition::DiscContainerCutPair::rawLabels
std::vector< std::string > rawLabels
Definition: PFTauSelectorDefinition.h:34
TtSemiLepEvtBuilder_cfi.disc
disc
Definition: TtSemiLepEvtBuilder_cfi.py:60
edm::Ref< PFTauCollection >
PFTauSelectorDefinition::HandleToCollection
edm::Handle< collection > HandleToCollection
Definition: PFTauSelectorDefinition.h:22
alignCSCRings.s
s
Definition: alignCSCRings.py:92
ProductProvenance.h
PFTauSelectorDefinition::DiscContainerCutPair::handle
edm::Handle< reco::TauDiscriminatorContainer > handle
Definition: PFTauSelectorDefinition.h:32
PFTauSelectorDefinition
Definition: PFTauSelectorDefinition.h:20
PDWG_TauSkim_cff.discriminatorContainers
discriminatorContainers
Definition: PDWG_TauSkim_cff.py:11
reco::PFTauCollection
std::vector< PFTau > PFTauCollection
collection of PFTau objects
Definition: PFTauFwd.h:9
PFTauSelectorDefinition::selected_
container selected_
Definition: PFTauSelectorDefinition.h:210
edm::Hash< ProcessHistoryType >
PFTauSelectorDefinition::DiscContainerCutPair::inputToken
edm::EDGetTokenT< reco::TauDiscriminatorContainer > inputToken
Definition: PFTauSelectorDefinition.h:33
PFTauSelectorDefinition::DiscContainerCutPair::rawCuts
std::vector< std::pair< int, double > > rawCuts
Definition: PFTauSelectorDefinition.h:35
PFTauSelectorDefinition::container
std::vector< const reco::PFTau * > container
Definition: PFTauSelectorDefinition.h:23
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
PFTauSelectorDefinition::collection
reco::PFTauCollection collection
Definition: PFTauSelectorDefinition.h:21
edm::ParameterSet
Definition: ParameterSet.h:36
PFTauSelectorDefinition::PFTauSelectorDefinition
PFTauSelectorDefinition(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
Definition: PFTauSelectorDefinition.h:42
Event.h
TauDiscriminatorContainer.h
PFTauSelectorDefinition::select
void select(const HandleToCollection &hc, const edm::Event &e, const edm::EventSetup &s)
Definition: PFTauSelectorDefinition.h:80
edm::EventSetup
Definition: EventSetup.h:57
PFTauSelectorDefinition::DiscCutPair::inputToken
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken
Definition: PFTauSelectorDefinition.h:28
looper.cfg
cfg
Definition: looper.py:297
PFTauSelectorDefinition::begin
const_iterator begin() const
Definition: PFTauSelectorDefinition.h:77
PFTauSelectorDefinition::DiscCutPairVec
std::vector< DiscCutPair > DiscCutPairVec
Definition: PFTauSelectorDefinition.h:39
PFTauSelectorDefinition::DiscContainerCutPair::wpCuts
std::vector< int > wpCuts
Definition: PFTauSelectorDefinition.h:37
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
StringCutObjectSelector.h
edm::ValueMap
Definition: ValueMap.h:107
StringCutObjectSelector< reco::PFTau >
Exception
Definition: hltDiff.cc:246
PFTauSelectorDefinition::DiscContainerCutPair
Definition: PFTauSelectorDefinition.h:31
edm::parameterSet
ParameterSet const & parameterSet(Provenance const &provenance, ProcessHistory const &history)
Definition: Provenance.cc:11
PFTau.h
PFTauSelectorDefinition::discriminatorContainers_
DiscContainerCutPairVec discriminatorContainers_
Definition: PFTauSelectorDefinition.h:212
TriggerAnalyzer.passed
passed
Definition: TriggerAnalyzer.py:62
Provenance.h
PFTauSelectorDefinition::const_iterator
container::const_iterator const_iterator
Definition: PFTauSelectorDefinition.h:24
ConsumesCollector.h
ParameterSet.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
PFTauSelectorDefinition::cut_
std::unique_ptr< StringCutObjectSelector< reco::PFTau > > cut_
Definition: PFTauSelectorDefinition.h:215
edm::Event
Definition: Event.h:73
edm::HandleBase::id
ProductID id() const
Definition: HandleBase.cc:13
edm::InputTag
Definition: InputTag.h:15
edm::ConsumesCollector
Definition: ConsumesCollector.h:39
PFTauSelectorDefinition::end
const_iterator end() const
Definition: PFTauSelectorDefinition.h:78
PFTauSelectorDefinition::size
size_t size() const
Definition: PFTauSelectorDefinition.h:207
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
PFTauSelectorDefinition::DiscCutPair
Definition: PFTauSelectorDefinition.h:26