MaCh3  2.6.1
Reference Guide
PlottingUtils.cpp
Go to the documentation of this file.
1 #include "PlottingUtils.h"
2 
3 namespace M3 {
4 namespace Plotting {
10 TH1D TGraphToTH1D(const TGraph& graph, const std::string& newName, const std::string& newTitle) {
11  std::string name;
12  std::string title;
13 
14  if (newName == "")
15  name = graph.GetName();
16  else
17  name = newName;
18 
19  if (newTitle == "")
20  title = graph.GetTitle();
21  else
22  title = newTitle;
23 
24  int nPoints = graph.GetN();
25  if(nPoints < 2){
26  MACH3LOG_ERROR("Too few points in the graph.");
27  throw MaCh3Exception(__FILE__,__LINE__);
28  }
29  std::vector<double> pointsX(nPoints);
30  std::vector<double> pointsY(nPoints);
31 
32  // Get the points out
33  Double_t x, y;
34 
35  for (int pointId = 0; pointId < nPoints; pointId++)
36  {
37  graph.GetPoint(pointId, x, y);
38  pointsX[pointId] = x;
39  pointsY[pointId] = y;
40  }
41 
42  // get the bin edges
43  std::vector<double> binEdges(nPoints + 1);
44  binEdges[0] = pointsX[0] - (pointsX[1] - pointsX[0]) / 2.0;
45  binEdges[nPoints] = pointsX[nPoints - 1] + (pointsX[nPoints - 1] - pointsX[nPoints - 2]) / 2.0;
46 
47  for (int pointId = 1; pointId < nPoints; pointId++)
48  {
49  // take the midpoint of the two surrounding points
50  binEdges[pointId] = (pointsX[pointId] + pointsX[pointId - 1]) / 2.0;
51  }
52 
53  TH1D retHist = TH1D(name.c_str(), title.c_str(), nPoints, binEdges.data());
54 
55  for (int binId = 0; binId < nPoints; binId++)
56  {
57  retHist.SetBinContent(binId + 1, pointsY[binId]);
58  }
59 
60  return retHist;
61 }
62 
63 std::vector<std::vector<double>> TGraphToVector(const TGraph& graph) {
64  int nPoints = graph.GetN();
65  std::vector<std::vector<double>> ret(2);
66  std::vector<double> pointsX(nPoints);
67  std::vector<double> pointsY(nPoints);
68 
69  // Get the points out
70  Double_t x, y;
71 
72  for (int pointId = 0; pointId < nPoints; pointId++)
73  {
74  graph.GetPoint(pointId, x, y);
75  pointsX[pointId] = x;
76  pointsY[pointId] = y;
77  }
78 
79  ret[0] = pointsX;
80  ret[1] = pointsY;
81 
82  return ret;
83 }
84 
85 
86 std::vector<std::vector<double>> TGraphToVector(const TGraph2D& graph) {
87  int nPoints = graph.GetN();
88  std::vector<std::vector<double>> ret(3);
89  std::vector<double> pointsX(nPoints);
90  std::vector<double> pointsY(nPoints);
91  std::vector<double> pointsZ(nPoints);
92 
93  // Get the points out
94  Double_t x, y, z;
95 
96  for (int pointId = 0; pointId < nPoints; pointId++)
97  {
98  graph.GetPoint(pointId, x, y, z);
99  pointsX[pointId] = x;
100  pointsY[pointId] = y;
101  pointsZ[pointId] = z;
102  }
103 
104  ret[0] = pointsX;
105  ret[1] = pointsY;
106  ret[2] = pointsZ;
107 
108  return ret;
109 }
110 
111 void SetSymmetricRatioRange(const std::vector<std::unique_ptr<TH1D>>& RatioPlot)
112 {
113  double maxz = -999;
114  double minz = +999;
115 
116  for (int j = 0; j < static_cast<int>(RatioPlot.size()); j++) {
117  if (!RatioPlot[j]) continue;
118 
119  for (int i = 1; i < RatioPlot[0]->GetXaxis()->GetNbins(); i++) {
120  maxz = std::max(maxz, RatioPlot[j]->GetBinContent(i));
121  minz = std::min(minz, RatioPlot[j]->GetBinContent(i));
122  }
123  }
124 
125  maxz = maxz * 1.001;
126  minz = minz * 1.001;
127 
128  if (std::fabs(1 - maxz) > std::fabs(1 - minz))
129  RatioPlot[0]->GetYaxis()->SetRangeUser(1 - std::fabs(1 - maxz), 1 + std::fabs(1 - maxz));
130  else
131  RatioPlot[0]->GetYaxis()->SetRangeUser(1 - std::fabs(1 - minz), 1 + std::fabs(1 - minz));
132 }
133 
134 } // namespace Plotting
135 } // namespace M3
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Custom exception class used throughout MaCh3.
std::vector< std::vector< double > > TGraphToVector(const TGraph &graph)
This handy little function lets you interpret a TGraph as a vector containing the same data.
void SetSymmetricRatioRange(const std::vector< std::unique_ptr< TH1D >> &RatioPlot)
Set a symmetric Y-axis range around 1 for ratio plots.
TH1D TGraphToTH1D(const TGraph &graph, const std::string &newName, const std::string &newTitle)
This handy little function lets you interpret a TGraph as a TH1D.
Main namespace for MaCh3 software.