Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:14:12

0001 #ifndef noiBinVec__h
0002 #define noiBinVec__h
0003 
0004 #ifndef tuClass__h
0005 // David Stewart, Dec 1 2022
0006 // Stripped down version of a convenience class that provide's *double and int for histograms binning
0007 // initialized with vector<double>
0008 struct tuBinVec {
0009     // data members
0010     double*        ptr;
0011     int            size;
0012     vector<double> vec;
0013 
0014     void build_ptr() {
0015         size = vec.size();
0016         ptr = new double[size];
0017         for (int i{0}; i<size; ++i) ptr[i] = vec[i];
0018     };
0019 
0020     tuBinVec(vector<double> V) {
0021         // range repeate will add a range leading to the next number
0022         // it is triggered by a repeat value of the last number, followed by the number
0023         // of bins
0024         //   example:
0025         //         0, 0, 5, 1. 2. 3. = 0 .2 .4 .6 .8 1.0 2. 3.
0026         vec.push_back(V[0]);
0027         int S = V.size();
0028         int i{1}; 
0029         while (i<(int)S) {
0030             if ( V[i] == V[i-1] ) {
0031                 if (i>(S-3)) throw std::runtime_error( "fatal in tuBinVec with range_repeat");
0032                 double step = (V[i+2]-V[i])/V[i+1];
0033                 for (int k{1}; k<=V[i+1]; ++k) vec.push_back(V[i]+k*step);
0034                 i+=3;
0035             } else {
0036                 vec.push_back(V[i]);
0037                 ++i;
0038             }
0039         }
0040         build_ptr();
0041     };
0042 
0043     operator int () const { return size-1; };
0044     operator double* () const { return ptr; };
0045     operator vector<double> () { return vec; };
0046     int bin_from_0(double val) const {
0047         auto loc = upper_bound(vec.begin(), vec.end(), val);
0048         return loc-vec.begin()-1;
0049     };
0050     int bin_from_1(double val) const {
0051         return bin_from_0(val) + 1;
0052     };
0053 };
0054 #endif // endif noiclass noibinvec definition
0055 
0056 #endif