MaCh3  2.2.3
Reference Guide
plottingUtils.cpp
Go to the documentation of this file.
1 #include "plottingUtils.h"
2 
3 namespace MaCh3Plotting {
4 
10 TH1D TGraphToTH1D(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 
64 std::vector<std::vector<double>> TGraphToVector(TGraph graph) {
65 
66  int nPoints = graph.GetN();
67  std::vector<std::vector<double>> ret(2);
68  std::vector<double> pointsX(nPoints);
69  std::vector<double> pointsY(nPoints);
70 
71  // Get the points out
72  Double_t x, y;
73 
74  for (int pointId = 0; pointId < nPoints; pointId++)
75  {
76  graph.GetPoint(pointId, x, y);
77  pointsX[pointId] = x;
78  pointsY[pointId] = y;
79  }
80 
81  ret[0] = pointsX;
82  ret[1] = pointsY;
83 
84  return ret;
85 }
86 
87 
88 std::vector<std::vector<double>> TGraphToVector(TGraph2D graph) {
89 
90  int nPoints = graph.GetN();
91  std::vector<std::vector<double>> ret(3);
92  std::vector<double> pointsX(nPoints);
93  std::vector<double> pointsY(nPoints);
94  std::vector<double> pointsZ(nPoints);
95 
96  // Get the points out
97  Double_t x, y, z;
98 
99  for (int pointId = 0; pointId < nPoints; pointId++)
100  {
101  graph.GetPoint(pointId, x, y, z);
102  pointsX[pointId] = x;
103  pointsY[pointId] = y;
104  pointsZ[pointId] = z;
105  }
106 
107  ret[0] = pointsX;
108  ret[1] = pointsY;
109  ret[2] = pointsZ;
110 
111  return ret;
112 }
113 
114 } // namespace MaCh3Plotting
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:27
Custom exception class for MaCh3 errors.
std::vector< std::vector< double > > TGraphToVector(TGraph graph)
This handy little function lets you interpret a TGraph as a vector containing the same data.
TH1D TGraphToTH1D(TGraph graph, const std::string &newName, const std::string &newTitle)
This handy little function lets you interpret a TGraph as a TH1D.