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 
19 #include <fstream>
20 
22 template<class T>
23 class Selector {
24 
25  public:
26  typedef T data_type;
28  typedef std::pair<index_type, size_t> cut_flow_item;
29  typedef std::vector<cut_flow_item> cut_flow_map;
30  typedef std::map<index_type, int> int_map;
31  typedef std::map<index_type, double> double_map;
32 
34  Selector( ) {
35  bits_.clear();
36  intCuts_.clear();
37  doubleCuts_.clear();
38  cutFlow_.clear();
40  }
41  virtual ~Selector() {}
42 
44  virtual void push_back( std::string const & s) {
45  bits_.push_back(s);
46  index_type i(&bits_,s);
47  // don't need to check to see if the key is already there,
48  // bits_ does that.
49  cutFlow_.push_back( cut_flow_item(i, 0) );
50  }
51 
52 
54  virtual void push_back( std::string const & s, int cut) {
55  bits_.push_back(s);
56  index_type i(&bits_,s);
57  intCuts_[i] = cut;
58  // don't need to check to see if the key is already there,
59  // bits_ does that.
60  cutFlow_.push_back( cut_flow_item(i,0) );
61  }
62 
64  virtual void push_back( std::string const & s, double cut) {
65  bits_.push_back(s);
66  index_type i(&bits_,s);
67  doubleCuts_[i] = cut;
68  // don't need to check to see if the key is already there,
69  // bits_ does that.
70  cutFlow_.push_back( cut_flow_item(i,0) );
71  }
72 
74  virtual bool operator()( T const & t, pat::strbitset & ret ) = 0;
75 
77  virtual bool operator()( T const & t )
78  {
79  retInternal_.set(false);
82  return (bool)retInternal_;
83  }
84 
85 
87  virtual bool operator()( T const & t, edm::EventBase const & e, pat::strbitset & ret)
88  {
89  return operator()(t, ret);
90  }
91 
93  virtual bool operator()( T const & t, edm::EventBase const & e)
94  {
95  retInternal_.set(false);
96  operator()(t, e, retInternal_);
98  return (bool)retInternal_;
99  }
100 
101 
103  void set(std::string const & s, bool val = true) {
104  set( index_type(&bits_,s), val);
105  }
106  void set(index_type const & i, bool val = true) {
107  bits_[i] = val;
108  }
109 
111  void set(std::string const & s, int cut, bool val = true) {
112  set( index_type(&bits_,s), cut);
113  }
114  void set(index_type const & i, int cut, bool val = true) {
115  bits_[i] = val;
116  intCuts_[i] = cut;
117  }
118 
120  void set(std::string const & s, double cut, bool val = true) {
121  set( index_type(&bits_,s), cut);
122  }
123  void set(index_type const & i, double cut, bool val = true) {
124  bits_[i] = val;
125  doubleCuts_[i] = cut;
126  }
127 
129  void clear(std::string const & s) {
130  clear(index_type(&bits_,s));
131  }
132 
133  void clear(index_type const & i) {
134  bits_[i] = false;
135  }
136 
140  bool operator[] ( std::string const & s ) const {
141  return bits_[s];
142  }
143 
144  bool operator[] ( index_type const & i ) const {
145  return bits_[i];
146  }
147 
149  bool considerCut( std::string const & s ) const {
150  return bits_[s] == true;
151  }
152  bool considerCut( index_type const & i ) const {
153  return bits_[i] == true;
154  }
155 
157  bool ignoreCut( std::string const & s ) const {
158  return bits_[s] == false;
159  }
160  bool ignoreCut( index_type const & i ) const {
161  return bits_[i] == false;
162  }
163 
165  void setIgnoredCuts( std::vector<std::string> const & bitsToIgnore ) {
166  for ( std::vector<std::string>::const_iterator ignoreBegin = bitsToIgnore.begin(),
167  ignoreEnd = bitsToIgnore.end(), ibit = ignoreBegin;
168  ibit != ignoreEnd; ++ibit ) {
169  set(*ibit, false );
170  }
171  }
172 
174  void passCut( pat::strbitset & ret, std::string const & s ) {
175  passCut( ret, index_type(&bits_,s));
176  }
177 
178  void passCut( pat::strbitset & ret, index_type const & i ) {
179  ret[i] = true;
180  cut_flow_map::iterator found = cutFlow_.end();
181  for ( cut_flow_map::iterator cutsBegin = cutFlow_.begin(),
182  cutsEnd = cutFlow_.end(), icut = cutsBegin;
183  icut != cutsEnd && found == cutsEnd; ++icut ) {
184  if ( icut->first == i ) {
185  found = icut;
186  }
187  }
188  ++(found->second);
189  }
190 
192  int cut( index_type const & i, int val ) const {
193  return intCuts_.find( i )->second;
194  };
196  double cut( index_type const & i, double val ) const {
197  return doubleCuts_.find( i )->second;
198  };
199 
201  int cut( std::string s, int val ) const {
202  return cut( index_type(&bits_,s), val);
203  };
205  double cut( std::string s, double val ) const {
206  return cut( index_type(&bits_,s), val);
207  };
208 
211  pat::strbitset ret = bits_;
212  ret.set(false);
213  for ( cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(),
214  cutsEnd = cutFlow_.end(), icut = cutsBegin;
215  icut != cutsEnd; ++icut ) {
216  if ( ignoreCut(icut->first) ) ret[icut->first] = true;
217  }
218  return ret;
219  }
220 
222  void setIgnored( pat::strbitset & ret ) {
223  for ( cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(),
224  cutsEnd = cutFlow_.end(), icut = cutsBegin;
225  icut != cutsEnd; ++icut ) {
226  if ( ignoreCut(icut->first) ) ret[icut->first] = true;
227  }
228  }
229 
231  void print(std::ostream & out) const {
232  for ( cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(),
233  cutsEnd = cutFlow_.end(), icut = cutsBegin;
234  icut != cutsEnd; ++icut ) {
235  char buff[1000];
236  if ( considerCut( icut->first ) ) {
237  sprintf(buff, "%6lu : %20s %10lu",
238  static_cast<unsigned long>(icut - cutsBegin),
239  icut->first.str().c_str(),
240  static_cast<unsigned long>(icut->second) );
241  } else {
242  sprintf(buff, "%6lu : %20s %10s",
243  static_cast<unsigned long>(icut - cutsBegin),
244  icut->first.str().c_str(),
245  "off" );
246  }
247  out << buff << std::endl;
248  }
249  }
250 
252  void printActiveCuts(std::ostream & out) const {
253  bool already_printed_one = false;
254  for ( cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(),
255  cutsEnd = cutFlow_.end(), icut = cutsBegin;
256  icut != cutsEnd; ++icut ) {
257  if ( considerCut( icut->first ) ) {
258  if( already_printed_one ) out << ", ";
259  out << icut->first;
260  already_printed_one = true;
261  }
262  }
263  out << std::endl;
264  }
265 
267  double getPasses( std::string const & s ) const {
268  return getPasses( index_type(&bits_,s) );
269  }
270  double getPasses( index_type const &i ) const {
271  cut_flow_map::const_iterator found = cutFlow_.end();
272  for ( cut_flow_map::const_iterator cutsBegin = cutFlow_.begin(),
273  cutsEnd = cutFlow_.end(), icut = cutsBegin;
274  icut != cutsEnd && found == cutsEnd; ++icut ) {
275  if ( icut->first == i ) {
276  found = icut;
277  }
278  }
279  return found->second;
280  }
281 
282 
283  protected:
286  int_map intCuts_;
287  double_map doubleCuts_;
288  cut_flow_map cutFlow_;
289 };
290 
291 #endif
bool considerCut(std::string const &s) const
consider the cut at index "s"
Definition: Selector.h:149
virtual bool operator()(T const &t, edm::EventBase const &e)
This provides an alternative signature that includes extra information.
Definition: Selector.h:93
virtual bool operator()(T const &t, pat::strbitset &ret)=0
This provides the interface for base classes to select objects.
bool considerCut(index_type const &i) const
Definition: Selector.h:152
std::map< index_type, int > int_map
Definition: Selector.h:30
T data_type
Definition: Selector.h:26
virtual ~Selector()
Definition: Selector.h:41
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:87
double cut(index_type const &i, double val) const
Access the double cut values at index "s".
Definition: Selector.h:196
void passCut(pat::strbitset &ret, index_type const &i)
Definition: Selector.h:178
pat::strbitset::index_type index_type
Definition: Selector.h:27
void setIgnored(pat::strbitset &ret)
set ignored bits
Definition: Selector.h:222
void printActiveCuts(std::ostream &out) const
Print the cuts being considered.
Definition: Selector.h:252
pat::strbitset retInternal_
internal ret if users don&#39;t care about return bits
Definition: Selector.h:285
std::pair< index_type, size_t > cut_flow_item
Definition: Selector.h:28
void clear(std::string const &s)
Turn off a given selection cut.
Definition: Selector.h:129
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:64
bool ignoreCut(index_type const &i) const
Definition: Selector.h:160
int cut(std::string s, int val) const
Access the int cut values at index "s".
Definition: Selector.h:201
pat::strbitset bits_
the bitset indexed by strings
Definition: Selector.h:284
void clear(index_type const &i)
Definition: Selector.h:133
void passCut(pat::strbitset &ret, std::string const &s)
Passing cuts.
Definition: Selector.h:174
double getPasses(index_type const &i) const
Definition: Selector.h:270
bool ignoreCut(std::string const &s) const
ignore the cut at index "s"
Definition: Selector.h:157
std::vector< cut_flow_item > cut_flow_map
Definition: Selector.h:29
virtual void push_back(std::string const &s)
This is the registration of an individual cut string.
Definition: Selector.h:44
Functor that operates on <T>
Definition: Selector.h:23
cut_flow_map cutFlow_
map of cut flows in "human" order
Definition: Selector.h:288
void clear()
clear the bitset and map
Definition: strbitset.h:75
Selector()
Constructor clears the bits.
Definition: Selector.h:34
void print(std::ostream &out) const
Print the cut flow.
Definition: Selector.h:231
std::map< index_type, double > double_map
Definition: Selector.h:31
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:144
pat::strbitset getBitTemplate() const
Get an empty bitset with the proper names.
Definition: Selector.h:210
double cut(std::string s, double val) const
Access the double cut values at index "s".
Definition: Selector.h:205
virtual bool operator()(T const &t)
This provides an alternative signature without the second ret.
Definition: Selector.h:77
bool operator[](std::string const &s) const
Definition: Selector.h:140
void setIgnoredCuts(std::vector< std::string > const &bitsToIgnore)
set the bits to ignore from a vector
Definition: Selector.h:165
long double T
double_map doubleCuts_
the double-value cut map
Definition: Selector.h:287
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:54
void push_back(std::string s)
Definition: strbitset.h:99
int_map intCuts_
the int-value cut map
Definition: Selector.h:286
int cut(index_type const &i, int val) const
Access the int cut values at index "s".
Definition: Selector.h:192
double getPasses(std::string const &s) const
Return the number of passing cases.
Definition: Selector.h:267