Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #ifndef noiPtrDbl__h
0002 #define noiPtrDbl__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 noiPtrDbl {
0009     // internal
0010     vector<double> vec{};
0011     double*        ptr{nullptr};
0012     int            size{0};
0013 
0014     operator int () { return size; };
0015     operator double* () { return ptr; };
0016     operator vector<double> () { return vec; };
0017 
0018     noiPtrDbl(TAxis* ax, double bin_loc=0.5, bool get_widths=false) {
0019         int n_bins = ax->GetNbins();
0020         if (get_widths) {
0021             for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinWidth(i)*bin_loc);
0022         } else if (bin_loc == 0.5) {
0023             for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinCenter(i));
0024         } else if (bin_loc == 0.) {
0025             for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinLowEdge(i));
0026         } else if (bin_loc == 1.) {
0027             for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinUpEdge(i));
0028         } else {
0029             for (int i{1}; i<=n_bins; ++i) {
0030                 double W = ax->GetBinWidth(i);
0031                 double L  = ax->GetBinLowEdge(i);
0032                 vec.push_back(L+W*bin_loc);
0033             }
0034         }
0035         size = vec.size();
0036         ptr = new double[size];
0037         for (int i{0}; i<size; ++i) ptr[i] = vec[i];
0038     }
0039     noiPtrDbl(TH1* hg, bool get_errors=false) {
0040         if (get_errors) {
0041             for (auto i{1}; i<= hg->GetXaxis()->GetNbins(); ++i) {
0042                 vec.push_back(hg->GetBinError(i));
0043             }
0044         } else {
0045             for (auto i{1}; i<= hg->GetXaxis()->GetNbins(); ++i) {
0046                 vec.push_back(hg->GetBinContent(i));
0047             }
0048         }
0049         size = vec.size();
0050         ptr = new double[size];
0051         for (int i{0}; i<size; ++i) ptr[i] = vec[i];
0052     }
0053 };
0054 
0055 TGraphAsymmErrors* TGASE_errors (TH1D* hg, array<double,4> x_rat={-1,-1,0.5}) {
0056     int size;
0057     noiPtrDbl x     {hg->GetXaxis()};
0058     noiPtrDbl err_x {hg->GetXaxis(), 0.5, true};
0059 
0060     noiPtrDbl y     {hg};
0061     noiPtrDbl err_y {hg,true};
0062 
0063     noiPtrDbl err_y_lo = err_y;
0064     noiPtrDbl err_y_hi = err_y;
0065     
0066     auto tgase = new TGraphAsymmErrors (x.size,x,y,err_x,err_x,err_y_lo,err_y_hi);
0067     /* tgase->SetMarkerColor(hg->GetMarkerColor()); */
0068     /* tgase->SetMarkerSize(hg->GetMarkerSize()); */
0069     /* tgase->SetMarkerStyle(hg->GetMarkerStyle()); */
0070     /* tgase->SetLineColor(hg->GetLineColor()); */
0071     /* tgase->SetLineStyle(hg->GetLineStyle()); */
0072     size = x.size;
0073 
0074     if (x_rat[0]>=0) {
0075         double r_left   { x_rat[0] };
0076         double r_right  { x_rat[1] };
0077         double r_center { x_rat[2] }; // relative position between left and right
0078         double offset_c { x_rat[3] };
0079 
0080         double* x = tgase->GetX();
0081         for (int i{0}; i<size; ++i) {
0082             double deltaX = tgase->GetErrorXlow(i) + tgase->GetErrorXhigh(i);
0083             double anchor  = x[i]-tgase->GetErrorXlow(i);
0084             double p_left  = anchor + r_left  * deltaX;
0085             double p_right = anchor + r_right * deltaX;
0086             double p_center = anchor + r_center * deltaX + offset_c;
0087             tgase->SetPointEXlow (i,  p_center-p_left);
0088             tgase->SetPointEXhigh(i,  p_right-p_center);
0089             tgase->SetPoint(i,p_center,tgase->GetY()[i]);
0090         }
0091     }
0092     return tgase;
0093 };
0094 
0095 #endif // endif noiclass noibinvec definition
0096 
0097 #endif