Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noiBinVec.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file noiBinVec.h
1 #ifndef noiBinVec__h
2 #define noiBinVec__h
3 
4 #ifndef tuClass__h
5 // David Stewart, Dec 1 2022
6 // Stripped down version of a convenience class that provide's *double and int for histograms binning
7 // initialized with vector<double>
8 struct tuBinVec {
9  // data members
10  double* ptr;
11  int size;
12  vector<double> vec;
13 
14  void build_ptr() {
15  size = vec.size();
16  ptr = new double[size];
17  for (int i{0}; i<size; ++i) ptr[i] = vec[i];
18  };
19 
20  tuBinVec(vector<double> V) {
21  // range repeate will add a range leading to the next number
22  // it is triggered by a repeat value of the last number, followed by the number
23  // of bins
24  // example:
25  // 0, 0, 5, 1. 2. 3. = 0 .2 .4 .6 .8 1.0 2. 3.
26  vec.push_back(V[0]);
27  int S = V.size();
28  int i{1};
29  while (i<(int)S) {
30  if ( V[i] == V[i-1] ) {
31  if (i>(S-3)) throw std::runtime_error( "fatal in tuBinVec with range_repeat");
32  double step = (V[i+2]-V[i])/V[i+1];
33  for (int k{1}; k<=V[i+1]; ++k) vec.push_back(V[i]+k*step);
34  i+=3;
35  } else {
36  vec.push_back(V[i]);
37  ++i;
38  }
39  }
40  build_ptr();
41  };
42 
43  operator int () const { return size-1; };
44  operator double* () const { return ptr; };
45  operator vector<double> () { return vec; };
46  int bin_from_0(double val) const {
47  auto loc = upper_bound(vec.begin(), vec.end(), val);
48  return loc-vec.begin()-1;
49  };
50  int bin_from_1(double val) const {
51  return bin_from_0(val) + 1;
52  };
53 };
54 #endif // endif noiclass noibinvec definition
55 
56 #endif