All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultiVar.cxx
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <map>
5 #include <set>
6 
7 #include <iostream>
8 
9 namespace ana
10 {
11  /// std::function can wrap a real function, function object, or lambda
12  template<class T> _MultiVar<T>::_MultiVar(const std::function<VarFunc_t>& fun)
13  : fFunc(fun), fID(fgNextID--)
14  {
15  }
16 
17  //----------------------------------------------------------------------
18  /// Helper for \ref MultiVar2D
19  template<class T> class MultiVar2DFunc
20  {
21  public:
22  MultiVar2DFunc(const _MultiVar<T>& a, const Binning binsa,
23  const _MultiVar<T>& b, const Binning binsb)
24  : fA(a), fBinsA(binsa),
25  fB(b), fBinsB(binsb)
26  {
27  }
28 
29  std::vector<double> operator()(const T* sr) const
30  {
31  std::vector<double> returnVec;
32 
33  const std::vector<double> vaVec = fA(sr);
34  const std::vector<double> vbVec = fB(sr);
35 
36  if(vaVec.size() != vbVec.size())
37  {
38  std::cout << "MultiVars need to be same size, these two are size "
39  << vaVec.size() << " " << vbVec.size() << "respectively." << std::endl;
40  std::abort();
41  }
42 
43  for(unsigned n = 0; n < vaVec.size(); ++n){
44  const double va = vaVec.at(n);
45  const double vb = vbVec.at(n);
46  // Since there are no overflow/underflow bins, check the range
47  if(va < fBinsA.Min() || vb < fBinsB.Min()){ returnVec.push_back(-1); continue;}
48  if(va > fBinsA.Max() || vb > fBinsB.Max()){ returnVec.push_back(fBinsA.NBins() * fBinsB.NBins()); continue;}
49 
50  // FindBin uses root convention, first bin is bin 1, bin 0 is underflow
51  const int ia = fBinsA.FindBin(va) - 1;
52  const int ib = fBinsB.FindBin(vb) - 1;
53 
54  const int i = ia*fBinsB.NBins()+ib;
55 
56  returnVec.push_back(i+.5);
57  }
58  return returnVec;
59  }
60  protected:
62  const Binning fBinsA;
64  const Binning fBinsB;
65  };
66 
67  //----------------------------------------------------------------------
68  template<class T> _MultiVar<T>
69  MultiVar2D(const _MultiVar<T>& a, const Binning& binsa,
70  const _MultiVar<T>& b, const Binning& binsb)
71  {
72  return _MultiVar<T>(MultiVar2DFunc<T>(a, binsa, b, binsb));
73  }
74 
75  // explicitly instantiate the template for the types we know we have
76  template MultiVar MultiVar2D(const MultiVar&, const Binning&, const MultiVar&, const Binning&);
77  template SpillMultiVar MultiVar2D(const SpillMultiVar&, const Binning&, const SpillMultiVar&, const Binning&);
78 
79  // explicitly instantiate the templates for the types we know we have
80  template class _MultiVar<caf::SRSpillProxy>;
81  template class _MultiVar<caf::SRSliceProxy>;
82 
83  // Stupid hack to avoid colliding with the IDs of actual Vars. Just count
84  // down through negative numbers.
85  template<class T> int _MultiVar<T>::fgNextID = -1;
86 
87 }
_MultiVar(const std::function< VarFunc_t > &fun)
std::function can wrap a real function, function object, or lambda
Definition: MultiVar.cxx:12
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
const _MultiVar< T > fB
Definition: MultiVar.cxx:63
const Binning fBinsA
Definition: MultiVar.cxx:62
Helper for MultiVar2D.
Definition: MultiVar.cxx:19
process_name opflashCryoW ana
const _MultiVar< T > fA
Definition: MultiVar.cxx:61
process_name gaushit a
std::vector< double > operator()(const T *sr) const
Definition: MultiVar.cxx:29
double Min() const
Definition: Binning.h:28
_MultiVar< caf::SRSpillProxy > SpillMultiVar
Definition: MultiVar.h:49
int FindBin(float x) const
Definition: Binning.cxx:100
_MultiVar< caf::SRSliceProxy > MultiVar
Definition: MultiVar.h:48
double Max() const
Definition: Binning.h:29
int NBins() const
Definition: Binning.h:27
_MultiVar< T > MultiVar2D(const _MultiVar< T > &a, const Binning &binsa, const _MultiVar< T > &b, const Binning &binsb)
Definition: MultiVar.cxx:69
MultiVar2DFunc(const _MultiVar< T > &a, const Binning binsa, const _MultiVar< T > &b, const Binning binsb)
Definition: MultiVar.cxx:22
BEGIN_PROLOG could also be cout
const Binning fBinsB
Definition: MultiVar.cxx:64