All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Cut.cxx
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <map>
5 
6 namespace ana
7 {
8  //----------------------------------------------------------------------
9  template<class T> _Cut<T>::
10  _Cut(const std::function<CutFunc_t>& func,
11  const std::function<ExposureFunc_t>& liveFunc,
12  const std::function<ExposureFunc_t>& potFunc)
13  : fFunc(func), fLiveFunc(liveFunc), fPOTFunc(potFunc),
14  fID(fgNextID++)
15  {
16  }
17 
18  //----------------------------------------------------------------------
19  template<class T> int _Cut<T>::MaxID()
20  {
21  return fgNextID-1;
22  }
23 
24  //----------------------------------------------------------------------
25  std::function<ExposureFunc_t>
26  CombineExposures(const std::function<ExposureFunc_t>& a,
27  const std::function<ExposureFunc_t>& b)
28  {
29  if(!a && !b) return 0;
30  if(!a) return b;
31  if(!b) return a;
32 
33  return [a, b](const caf::SRSpill* spill){
34  const double va = a(spill);
35  const double vb = b(spill);
36 
37  if(va >= 0 && vb >= 0){
38  std::cout << "Inconsistent pot/livetime values of "
39  << va << " and " << vb
40  << " from two cuts being combined." << std::endl;
41  abort();
42  }
43 
44  return std::max(va, vb);
45  };
46  }
47 
48  //----------------------------------------------------------------------
49  template<class T> _Cut<T> operator&&(const _Cut<T>& a, const _Cut<T>& b)
50  {
51  // The same pairs of cuts are frequently and-ed together. Make sure those
52  // duplicates get the same IDs by remembering what we've done in the past.
53  static std::map<std::pair<int, int>, int> ids;
54  const std::pair<int, int> key(a.ID(), b.ID());
55 
56  if(ids.count(key)){
57  return _Cut<T>([a, b](const T* sr){return a(sr) && b(sr);},
60  ids[key]);
61  }
62  else{
63  const _Cut<T> ret([a, b](const T* sr){return a(sr) && b(sr);},
66  ids[key] = ret.ID();
67  return ret;
68  }
69  }
70 
71  // Make sure all versions get generated
72  template Cut operator&&<caf::SRSliceProxy>(const Cut& a, const Cut& b);
73  template SpillCut operator&&<caf::SRSpillProxy>(const SpillCut& a, const SpillCut& b);
74 
75  //----------------------------------------------------------------------
76  template<class T> _Cut<T> operator||(const _Cut<T>& a, const _Cut<T>& b)
77  {
78  static std::map<std::pair<int, int>, int> ids;
79  const std::pair<int, int> key(a.ID(), b.ID());
80  if(ids.count(key)){
81  return _Cut<T>([a, b](const T* sr){return a(sr) || b(sr);},
84  ids[key]);
85  }
86  else{
87  const _Cut<T> ret([a, b](const T* sr){return a(sr) || b(sr);},
90  ids[key] = ret.ID();
91  return ret;
92  }
93  }
94 
95  // Make sure all versions get generated
96  template Cut operator||<caf::SRSliceProxy>(const Cut& a, const Cut& b);
97  template SpillCut operator||<caf::SRSpillProxy>(const SpillCut& a, const SpillCut& b);
98 
99  //----------------------------------------------------------------------
100  template<class T> _Cut<T> operator!(const _Cut<T>& a)
101  {
102  static std::map<int, int> ids;
103  if(ids.count(a.ID())){
104  return _Cut<T>([a](const T* sr){return !a(sr);},
105  0, 0, ids[a.ID()]);
106  }
107  else{
108  const _Cut<T> ret([a](const T* sr){return !a(sr);});
109  ids[a.ID()] = ret.ID();
110  return ret;
111  }
112  }
113 
114  // Make sure all versions get generated
115  template Cut operator!<caf::SRSliceProxy>(const Cut& a);
116  template SpillCut operator!<caf::SRSpillProxy>(const SpillCut& a);
117 
118 
119  //----------------------------------------------------------------------
120  template<class T> _Cut<T>
121  operator>(const _Var<T>& v, double c)
122  {
123  return _Cut<T>([v, c](const T* sr){return v(sr) > c;});
124  }
125 
126  //----------------------------------------------------------------------
127  template<class T> _Cut<T>
128  operator>=(const _Var<T>& v, double c)
129  {
130  return _Cut<T>([v, c](const T* sr){return v(sr) >= c;});
131  }
132 
133  //----------------------------------------------------------------------
134  template<class T> _Cut<T>
135  operator<(const _Var<T>& v, double c)
136  {
137  return _Cut<T>([v, c](const T* sr){return v(sr) < c;});
138  }
139 
140  //----------------------------------------------------------------------
141  template<class T> _Cut<T>
142  operator<=(const _Var<T>& v, double c)
143  {
144  return _Cut<T>([v, c](const T* sr){return v(sr) <= c;});
145  }
146 
147  //----------------------------------------------------------------------
148  template<class T> _Cut<T>
149  operator==(const _Var<T>& v, double c)
150  {
151  return _Cut<T>([v, c](const T* sr){return v(sr) == c;});
152  }
153 
154  //----------------------------------------------------------------------
155  template<class T> _Cut<T>
156  operator!=(const _Var<T>& v, double c)
157  {
158  return !(v == c);
159  }
160 
161  //----------------------------------------------------------------------
162  template<class T> _Cut<T>
163  operator>(const _Var<T>& a, const _Var<T>& b)
164  {
165  return _Cut<T>([a, b](const T* sr){return a(sr) > b(sr);});
166  }
167 
168  //----------------------------------------------------------------------
169  template<class T> _Cut<T>
170  operator>=(const _Var<T>& a, const _Var<T>& b)
171  {
172  return _Cut<T>([a, b](const T* sr){return a(sr) >= b(sr);});
173  }
174 
175  //----------------------------------------------------------------------
176  template<class T> _Cut<T>
177  operator==(const _Var<T>& a, const _Var<T>& b)
178  {
179  return _Cut<T>([a, b](const T* sr){return a(sr) == b(sr);});
180  }
181 
182  // Build the rest up through simple logic
183 
184  template<class T> _Cut<T> operator>(double c, const _Var<T>& v){return v < c;}
185  template<class T> _Cut<T> operator<(double c, const _Var<T>& v){return v > c;}
186  template<class T> _Cut<T> operator>=(double c, const _Var<T>& v){return v <= c;}
187  template<class T> _Cut<T> operator<=(double c, const _Var<T>& v){return v >= c;}
188  template<class T> _Cut<T> operator!=(double c, const _Var<T>& v){return v != c;}
189 
190  template<class T> _Cut<T> operator<(const _Var<T>& a, const _Var<T>& b){return !(b >= a);}
191  template<class T> _Cut<T> operator<=(const _Var<T>& a, const _Var<T>& b){return !(b > a);}
192  template<class T> _Cut<T> operator!=(const _Var<T>& a, const _Var<T>& b){return !(b == a);}
193 
194  // Make sure all three versions get generated
195  template class _Cut<caf::SRSpillProxy>;
196  template class _Cut<caf::SRSliceProxy>;
197 
198  template<class T> int _Cut<T>::fgNextID = 0;
199 
200  // explicitly instantiate the templates for the types we know we have
201  template Cut operator>(const Var&, double);
202  template Cut operator<(const Var&, double);
203  template Cut operator>=(const Var&, double);
204  template Cut operator<=(const Var&, double);
205  template Cut operator==(const Var&, double);
206  template Cut operator!=(const Var&, double);
207 
208  template Cut operator>(const Var&, const Var&);
209  template Cut operator<(const Var&, const Var&);
210  template Cut operator>=(const Var&, const Var&);
211  template Cut operator<=(const Var&, const Var&);
212  template Cut operator==(const Var&, const Var&);
213  template Cut operator!=(const Var&, const Var&);
214 
215  template Cut operator>(double, const Var&);
216  template Cut operator<(double, const Var&);
217  template Cut operator>=(double, const Var&);
218  template Cut operator<=(double, const Var&);
219 
220  template SpillCut operator>(const SpillVar&, double);
221  template SpillCut operator<(const SpillVar&, double);
222  template SpillCut operator>=(const SpillVar&, double);
223  template SpillCut operator<=(const SpillVar&, double);
224  template SpillCut operator==(const SpillVar&, double);
225 
226  template SpillCut operator>(const SpillVar&, const SpillVar&);
227  template SpillCut operator<(const SpillVar&, const SpillVar&);
228  template SpillCut operator>=(const SpillVar&, const SpillVar&);
229  template SpillCut operator<=(const SpillVar&, const SpillVar&);
230  template SpillCut operator==(const SpillVar&, const SpillVar&);
231 
232  template SpillCut operator>(double, const SpillVar&);
233  template SpillCut operator<(double, const SpillVar&);
234  template SpillCut operator>=(double, const SpillVar&);
235  template SpillCut operator<=(double, const SpillVar&);
236 }
_Cut< T > operator<(const _Var< T > &v, double c)
Definition: Cut.cxx:135
std::function< ExposureFunc_t > fPOTFunc
Definition: Cut.h:79
_Cut< T > operator<=(const _Var< T > &v, double c)
Definition: Cut.cxx:142
static int MaxID()
Definition: Cut.cxx:19
_Cut< T > operator!(const _Cut< T > &a)
Definition: Cut.cxx:100
_Cut< T > operator&&(const _Cut< T > &a, const _Cut< T > &b)
Definition: Cut.cxx:49
std::function< ExposureFunc_t > CombineExposures(const std::function< ExposureFunc_t > &a, const std::function< ExposureFunc_t > &b)
Definition: Cut.cxx:26
_Cut(const std::function< CutFunc_t > &func, const std::function< ExposureFunc_t > &liveFunc=0, const std::function< ExposureFunc_t > &potFunc=0)
std::function can wrap a real function, function object, or lambda
Definition: Cut.cxx:10
_Cut< T > operator||(const _Cut< T > &a, const _Cut< T > &b)
Definition: Cut.cxx:76
process_name opflashCryoW ana
caf::Proxy< caf::SRSlice > SRSliceProxy
Definition: EpilogFwd.h:2
_Var< caf::SRSliceProxy > Var
Representation of a variable to be retrieved from a caf::StandardRecord object.
Definition: Var.h:73
process_name gaushit a
_Cut< T > operator>=(const _Var< T > &v, double c)
Definition: Cut.cxx:128
_Var< caf::SRSpillProxy > SpillVar
Equivalent of Var acting on caf::SRSpill.
Definition: Var.h:76
_Cut< caf::SRSliceProxy > Cut
Definition: Cut.h:95
caf::Proxy< caf::StandardRecord > SRSpillProxy
Definition: EpilogFwd.h:3
int ID() const
Cuts with the same definition will have the same ID.
Definition: Cut.h:57
std::function< ExposureFunc_t > fLiveFunc
Definition: Cut.h:79
_Cut< T > operator!=(const _Var< T > &v, double c)
Definition: Cut.cxx:156
_Cut< caf::SRSpillProxy > SpillCut
Equivalent of Cut acting on caf::SRSpill. For use in spill-by-spill data quality cuts.
Definition: Cut.h:99
_Cut< T > operator==(const _Var< T > &v, double c)
Definition: Cut.cxx:149
Template for Cut and SpillCut.
Definition: Cut.h:16
_Cut< T > operator>(const _Var< T > &v, double c)
Definition: Cut.cxx:121
Most useful for combining weights.
Definition: Var.h:23
BEGIN_PROLOG could also be cout