Building a Graph from an ASCII File


In this example we will read the points from a file and produce a simple graph.

Create the canvas on which we'll draw the graph.

In [1]:
TCanvas c;
c.SetGrid();

Create the graph of expected points by reading from an ASCII file.

In [2]:
.! curl https://raw.githubusercontent.com/root-mirror/training/master/2016/macro2_input_expected.txt -o macro2_input_expected.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   221  100   221    0     0    774      0 --:--:-- --:--:-- --:--:--   775
In [3]:
TGraphErrors graph_expected("./macro2_input_expected.txt", "%lg %lg %lg");

graph_expected.SetTitle(
    "Measurement XYZ and Expectation;"
    "lenght [cm];"
    "Arb.Units");
graph_expected.SetFillColor(kYellow);
graph_expected.Draw("E3AL"); // E3 draws the band

Create the graph of measured points, also reading from a file.

In [4]:
TGraphErrors graph("../macro2_input.txt", "%lg %lg %lg");

graph.SetMarkerStyle(kCircle);
graph.SetFillColor(0);
graph.Draw("PESame");

Draw the legend.

In [5]:
TLegend leg(.1, .7, .3, .9, "Lab. Lesson 2");
leg.SetFillColor(0);
leg.AddEntry(&graph_expected, "Expected Points");
leg.AddEntry(&graph, "Measured Points");
leg.Draw("Same");

Display in the notebook what is in our canvas as interactive javascript graphics.

In [6]:
%jsroot on
c.Draw();

Print graph and error values.

In [7]:
graph.Print();
x[0]=1, y[0]=6, ex[0]=0, ey[0]=5
x[1]=2, y[1]=12, ex[1]=0, ey[1]=5
x[2]=3, y[2]=14, ex[2]=0, ey[2]=4.7
x[3]=4, y[3]=20, ex[3]=0, ey[3]=4.5
x[4]=5, y[4]=22, ex[4]=0, ey[4]=4.2
x[5]=6, y[5]=24, ex[5]=0, ey[5]=5.1
x[6]=7, y[6]=35, ex[6]=0, ey[6]=2.9
x[7]=8, y[7]=45, ex[7]=0, ey[7]=4.1
x[8]=9, y[8]=44, ex[8]=0, ey[8]=4.8
x[9]=10, y[9]=53, ex[9]=0, ey[9]=5.43