MaCh3  2.2.3
Reference Guide
Functions
Plotting Utility Functions

Functions

TH1D MaCh3Plotting::TGraphToTH1D (TGraph graph, const std::string &newName="", const std::string &newTitle="")
 This handy little function lets you interpret a TGraph as a TH1D. More...
 
std::vector< std::vector< double > > MaCh3Plotting::TGraphToVector (TGraph graph)
 This handy little function lets you interpret a TGraph as a vector containing the same data. More...
 
std::vector< std::vector< double > > MaCh3Plotting::TGraphToVector (TGraph2D graph)
 This handy little function lets you interpret a 2d TGraph as a vector containing the same data. More...
 

Detailed Description

Function Documentation

◆ TGraphToTH1D()

TH1D MaCh3Plotting::TGraphToTH1D ( TGraph  graph,
const std::string &  newName,
const std::string &  newTitle 
)

This handy little function lets you interpret a TGraph as a TH1D.

It will go through your provided graph and make a histogram binning by taking the midpoints of all the graph points x values, then fill the histogram with the graphs y values. For the first and last points it will extend the binning out of the graph bounds using the width between the outermost and second outermost points. This can be useful if e.g. you want to draw cumulative stacks of LLH scans.

Parameters
graphThe graph you want to convert.
newNameThe new name you want to give to the histogram. If not specified, will just use the name of the graph.
newTitleThe new title you want to give to the histogram. If not specified, will just use the title of the graph.

Definition at line 10 of file plottingUtils.cpp.

10  {
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 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:27
Custom exception class for MaCh3 errors.

◆ TGraphToVector() [1/2]

std::vector< std::vector< double > > MaCh3Plotting::TGraphToVector ( TGraph  graph)

This handy little function lets you interpret a TGraph as a vector containing the same data.

Parameters
graphThe graph you want to convert.
Returns
A vector of vectors containing the data from the initial graph. The first vector is the x axis, the 2nd the y axis

Definition at line 64 of file plottingUtils.cpp.

64  {
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 }

◆ TGraphToVector() [2/2]

std::vector< std::vector< double > > MaCh3Plotting::TGraphToVector ( TGraph2D  graph)

This handy little function lets you interpret a 2d TGraph as a vector containing the same data.

Parameters
graphThe graph you want to convert.
Returns
A vector of vectors containing the data from the initial graph. The first vector is the x axis, the 2nd the y axis, the 3rd is the z axis

Definition at line 88 of file plottingUtils.cpp.

88  {
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 }