Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noiPtrDbl.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file noiPtrDbl.h
1 #ifndef noiPtrDbl__h
2 #define noiPtrDbl__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 noiPtrDbl {
9  // internal
10  vector<double> vec{};
11  double* ptr{nullptr};
12  int size{0};
13 
14  operator int () { return size; };
15  operator double* () { return ptr; };
16  operator vector<double> () { return vec; };
17 
18  noiPtrDbl(TAxis* ax, double bin_loc=0.5, bool get_widths=false) {
19  int n_bins = ax->GetNbins();
20  if (get_widths) {
21  for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinWidth(i)*bin_loc);
22  } else if (bin_loc == 0.5) {
23  for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinCenter(i));
24  } else if (bin_loc == 0.) {
25  for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinLowEdge(i));
26  } else if (bin_loc == 1.) {
27  for (int i{1}; i<=n_bins; ++i) vec.push_back(ax->GetBinUpEdge(i));
28  } else {
29  for (int i{1}; i<=n_bins; ++i) {
30  double W = ax->GetBinWidth(i);
31  double L = ax->GetBinLowEdge(i);
32  vec.push_back(L+W*bin_loc);
33  }
34  }
35  size = vec.size();
36  ptr = new double[size];
37  for (int i{0}; i<size; ++i) ptr[i] = vec[i];
38  }
39  noiPtrDbl(TH1* hg, bool get_errors=false) {
40  if (get_errors) {
41  for (auto i{1}; i<= hg->GetXaxis()->GetNbins(); ++i) {
42  vec.push_back(hg->GetBinError(i));
43  }
44  } else {
45  for (auto i{1}; i<= hg->GetXaxis()->GetNbins(); ++i) {
46  vec.push_back(hg->GetBinContent(i));
47  }
48  }
49  size = vec.size();
50  ptr = new double[size];
51  for (int i{0}; i<size; ++i) ptr[i] = vec[i];
52  }
53 };
54 
55 TGraphAsymmErrors* TGASE_errors (TH1D* hg, array<double,4> x_rat={-1,-1,0.5}) {
56  int size;
57  noiPtrDbl x {hg->GetXaxis()};
58  noiPtrDbl err_x {hg->GetXaxis(), 0.5, true};
59 
60  noiPtrDbl y {hg};
61  noiPtrDbl err_y {hg,true};
62 
63  noiPtrDbl err_y_lo = err_y;
64  noiPtrDbl err_y_hi = err_y;
65 
66  auto tgase = new TGraphAsymmErrors (x.size,x,y,err_x,err_x,err_y_lo,err_y_hi);
67  /* tgase->SetMarkerColor(hg->GetMarkerColor()); */
68  /* tgase->SetMarkerSize(hg->GetMarkerSize()); */
69  /* tgase->SetMarkerStyle(hg->GetMarkerStyle()); */
70  /* tgase->SetLineColor(hg->GetLineColor()); */
71  /* tgase->SetLineStyle(hg->GetLineStyle()); */
72  size = x.size;
73 
74  if (x_rat[0]>=0) {
75  double r_left { x_rat[0] };
76  double r_right { x_rat[1] };
77  double r_center { x_rat[2] }; // relative position between left and right
78  double offset_c { x_rat[3] };
79 
80  double* x = tgase->GetX();
81  for (int i{0}; i<size; ++i) {
82  double deltaX = tgase->GetErrorXlow(i) + tgase->GetErrorXhigh(i);
83  double anchor = x[i]-tgase->GetErrorXlow(i);
84  double p_left = anchor + r_left * deltaX;
85  double p_right = anchor + r_right * deltaX;
86  double p_center = anchor + r_center * deltaX + offset_c;
87  tgase->SetPointEXlow (i, p_center-p_left);
88  tgase->SetPointEXhigh(i, p_right-p_center);
89  tgase->SetPoint(i,p_center,tgase->GetY()[i]);
90  }
91  }
92  return tgase;
93 };
94 
95 #endif // endif noiclass noibinvec definition
96 
97 #endif