CMS 3D CMS Logo

Selector.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_SelectorUtils_Selector_h
2 #define PhysicsTools_SelectorUtils_Selector_h
3 
18 #include <fstream>
19 
21 template <class T>
22 class Selector {
23 public:
24  typedef T data_type;
26  typedef std::pair<index_type, size_t> cut_flow_item;
27  typedef std::vector<cut_flow_item> cut_flow_map;
28  typedef std::map<index_type, int> int_map;
29  typedef std::map<index_type, double> double_map;
30 
33  bits_.clear();
34  intCuts_.clear();
35  doubleCuts_.clear();
36  cutFlow_.clear();
38  }
39  virtual ~Selector() {}
40 
42  virtual void push_back(std::string const &s) {
43  bits_.push_back(s);
44  index_type i(&bits_, s);
45  // don't need to check to see if the key is already there,
46  // bits_ does that.
47  cutFlow_.push_back(cut_flow_item(i, 0));
48  }
49 
51  virtual void push_back(std::string const &s, int cut) {
52  bits_.push_back(s);
53  index_type i(&bits_, s);
54  intCuts_[i] = cut;
55  // don't need to check to see if the key is already there,
56  // bits_ does that.
57  cutFlow_.push_back(cut_flow_item(i, 0));
58  }
59 
61  virtual void push_back(std::string const &s, double cut) {
62  bits_.push_back(s);
63  index_type i(&bits_, s);
64  doubleCuts_[i] = cut;
65  // don't need to check to see if the key is already there,
66  // bits_ does that.
67  cutFlow_.push_back(cut_flow_item(i, 0));
68  }
69 
71  virtual bool operator()(T const &t, pat::strbitset &ret) = 0;
72 
74  virtual bool operator()(T const &t) {
75  retInternal_.set(false);
78  return (bool)retInternal_;
79  }
80 
82  virtual bool operator()(T const &t, edm::EventBase const &e, pat::strbitset &ret) { return operator()(t, ret); }
83 
85  virtual bool operator()(T const &t, edm::EventBase const &e) {
86  retInternal_.set(false);
89  return (bool)retInternal_;
90  }
91 
93  void set(std::string const &s, bool val = true) { set(index_type(&bits_, s), val); }
94  void set(index_type const &i, bool val = true) { bits_[i] = val; }
95 
97  void set(std::string const &s, int cut, bool val = true) { set(index_type(&bits_, s), cut); }
98  void set(index_type const &i, int cut, bool val = true) {
99  bits_[i] = val;
100  intCuts_[i] = cut;
101  }
102 
104  void set(std::string const &s, double cut, bool val = true) { set(index_type(&bits_, s), cut); }
105  void set(index_type const &i, double cut, bool val = true) {
106  bits_[i] = val;
107  doubleCuts_[i] = cut;
108  }
109 
111  void clear(std::string const &s) { clear(index_type(&bits_, s)); }
112 
113  void clear(index_type const &i) { bits_[i] = false; }
114 
118  bool operator[](std::string const &s) const { return bits_[s]; }
119 
120  bool operator[](index_type const &i) const { return bits_[i]; }
121 
123  bool considerCut(std::string const &s) const { return bits_[s] == true; }
124  bool considerCut(index_type const &i) const { return bits_[i] == true; }
125 
127  bool ignoreCut(std::string const &s) const { return bits_[s] == false; }
128  bool ignoreCut(index_type const &i) const { return bits_[i] == false; }
129 
131  void setIgnoredCuts(std::vector<std::string> const &bitsToIgnore) {
132  for (std::vector<std::string>::const_iterator ignoreBegin = bitsToIgnore.begin(),
133  ignoreEnd = bitsToIgnore.end(),
134  ibit = ignoreBegin;
135  ibit != ignoreEnd;
136  ++ibit) {
137  set(*ibit, false);
138  }
139  }
140 
143 
145  ret[i] = true;
146  cut_flow_map::iterator found = cutFlow_.end();
147  for (cut_flow_map::iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
148  icut != cutsEnd && found == cutsEnd;
149  ++icut) {
150  if (icut->first == i) {
151  found = icut;
152  }
153  }
154  ++(found->second);
155  }
156 
158  int cut(index_type const &i, int val) const { return intCuts_.find(i)->second; };
160  double cut(index_type const &i, double val) const { return doubleCuts_.find(i)->second; };
161 
163  int cut(std::string s, int val) const { return cut(index_type(&bits_, s), val); };
165  double cut(std::string s, double val) const { return cut(index_type(&bits_, s), val); };
166 
170  ret.set(false);
171  for (cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
172  icut != cutsEnd;
173  ++icut) {
174  if (ignoreCut(icut->first))
175  ret[icut->first] = true;
176  }
177  return ret;
178  }
179 
182  for (cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
183  icut != cutsEnd;
184  ++icut) {
185  if (ignoreCut(icut->first))
186  ret[icut->first] = true;
187  }
188  }
189 
191  void print(std::ostream &out) const {
192  for (cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
193  icut != cutsEnd;
194  ++icut) {
195  char buff[1000];
196  if (considerCut(icut->first)) {
197  sprintf(buff,
198  "%6lu : %20s %10lu",
199  static_cast<unsigned long>(icut - cutsBegin),
200  icut->first.str().c_str(),
201  static_cast<unsigned long>(icut->second));
202  } else {
203  sprintf(
204  buff, "%6lu : %20s %10s", static_cast<unsigned long>(icut - cutsBegin), icut->first.str().c_str(), "off");
205  }
206  out << buff << std::endl;
207  }
208  }
209 
211  void printActiveCuts(std::ostream &out) const {
212  bool already_printed_one = false;
213  for (cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
214  icut != cutsEnd;
215  ++icut) {
216  if (considerCut(icut->first)) {
217  if (already_printed_one)
218  out << ", ";
219  out << icut->first;
220  already_printed_one = true;
221  }
222  }
223  out << std::endl;
224  }
225 
227  double getPasses(std::string const &s) const { return getPasses(index_type(&bits_, s)); }
228  double getPasses(index_type const &i) const {
229  cut_flow_map::const_iterator found = cutFlow_.end();
230  for (cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(), cutsEnd = cutFlow_.end(), icut = cutsBegin;
231  icut != cutsEnd && found == cutsEnd;
232  ++icut) {
233  if (icut->first == i) {
234  found = icut;
235  }
236  }
237  return found->second;
238  }
239 
240 protected:
246 };
247 
248 #endif
bool ignoreCut(std::string const &s) const
ignore the cut at index "s"
Definition: Selector.h:127
virtual bool operator()(T const &t, edm::EventBase const &e)
This provides an alternative signature that includes extra information.
Definition: Selector.h:85
virtual bool operator()(T const &t, pat::strbitset &ret)=0
This provides the interface for base classes to select objects.
std::map< index_type, int > int_map
Definition: Selector.h:28
void printActiveCuts(std::ostream &out) const
Print the cuts being considered.
Definition: Selector.h:211
bool operator[](std::string const &s) const
Definition: Selector.h:118
T data_type
Definition: Selector.h:24
virtual ~Selector()
Definition: Selector.h:39
ret
prodAgent to be discontinued
virtual bool operator()(T const &t, edm::EventBase const &e, pat::strbitset &ret)
This provides an alternative signature that includes extra information.
Definition: Selector.h:82
void passCut(pat::strbitset &ret, index_type const &i)
Definition: Selector.h:144
pat::strbitset::index_type index_type
Definition: Selector.h:25
void setIgnored(pat::strbitset &ret)
set ignored bits
Definition: Selector.h:181
pat::strbitset retInternal_
internal ret if users don&#39;t care about return bits
Definition: Selector.h:242
std::pair< index_type, size_t > cut_flow_item
Definition: Selector.h:26
void clear(std::string const &s)
Turn off a given selection cut.
Definition: Selector.h:111
double cut(std::string s, double val) const
Access the double cut values at index "s".
Definition: Selector.h:165
double getPasses(std::string const &s) const
Return the number of passing cases.
Definition: Selector.h:227
void print(std::ostream &out) const
Print the cut flow.
Definition: Selector.h:191
virtual void push_back(std::string const &s, double cut)
This is the registration of an individual cut string, with a double cut value.
Definition: Selector.h:61
pat::strbitset bits_
the bitset indexed by strings
Definition: Selector.h:241
void clear(index_type const &i)
Definition: Selector.h:113
void passCut(pat::strbitset &ret, std::string const &s)
Passing cuts.
Definition: Selector.h:142
double getPasses(index_type const &i) const
Definition: Selector.h:228
std::vector< cut_flow_item > cut_flow_map
Definition: Selector.h:27
virtual void push_back(std::string const &s)
This is the registration of an individual cut string.
Definition: Selector.h:42
Functor that operates on <T>
Definition: Selector.h:22
cut_flow_map cutFlow_
map of cut flows in "human" order
Definition: Selector.h:245
void clear()
clear the bitset and map
Definition: strbitset.h:67
Selector()
Constructor clears the bits.
Definition: Selector.h:32
pat::strbitset getBitTemplate() const
Get an empty bitset with the proper names.
Definition: Selector.h:168
std::map< index_type, double > double_map
Definition: Selector.h:29
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:126
int cut(index_type const &i, int val) const
Access the int cut values at index "s".
Definition: Selector.h:158
bool considerCut(std::string const &s) const
consider the cut at index "s"
Definition: Selector.h:123
virtual bool operator()(T const &t)
This provides an alternative signature without the second ret.
Definition: Selector.h:74
void setIgnoredCuts(std::vector< std::string > const &bitsToIgnore)
set the bits to ignore from a vector
Definition: Selector.h:131
bool ignoreCut(index_type const &i) const
Definition: Selector.h:128
bool considerCut(index_type const &i) const
Definition: Selector.h:124
bool operator[](index_type const &i) const
Definition: Selector.h:120
long double T
double cut(index_type const &i, double val) const
Access the double cut values at index "s".
Definition: Selector.h:160
double_map doubleCuts_
the double-value cut map
Definition: Selector.h:244
virtual void push_back(std::string const &s, int cut)
This is the registration of an individual cut string, with an int cut value.
Definition: Selector.h:51
int cut(std::string s, int val) const
Access the int cut values at index "s".
Definition: Selector.h:163
void push_back(std::string s)
Definition: strbitset.h:90
int_map intCuts_
the int-value cut map
Definition: Selector.h:243
buff
***.cc ################