Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
macro1.C
Go to the documentation of this file.
1// Builds a graph with errors, displays it and saves it as
2// image. First, include some header files
3// (not necessary for Cling)
4
5#include "TCanvas.h"
6#include "TROOT.h"
7#include "TGraphErrors.h"
8#include "TF1.h"
9#include "TLegend.h"
10#include "TArrow.h"
11#include "TLatex.h"
12
13void macro1(){
14 // The values and the errors on the Y axis
15 const int n_points=10;
16 double x_vals[n_points]=
17 {1,2,3,4,5,6,7,8,9,10};
18 double y_vals[n_points]=
19 {6,12,14,20,22,24,35,45,44,53};
20 double y_errs[n_points]=
21 {5,5,4.7,4.5,4.2,5.1,2.9,4.1,4.8,5.43};
22
23 // Instance of the graph
24 TGraphErrors graph(n_points,x_vals,y_vals,nullptr,y_errs);
25 graph.SetTitle("Measurement XYZ;length [cm];Arb.Units");
26
27 // Make the plot estetically better
28 graph.SetMarkerStyle(kOpenCircle);
29 graph.SetMarkerColor(kBlue);
30 graph.SetLineColor(kBlue);
31
32 // The canvas on which we'll draw the graph
33 auto mycanvas = new TCanvas();
34
35 // Draw the graph !
36 graph.DrawClone("APE");
37
38 // Define a linear function
39 TF1 f("Linear law","[0]+x*[1]",.5,10.5);
40 // Let's make the function line nicer
41 f.SetLineColor(kRed); f.SetLineStyle(2);
42 // Fit it to the graph and draw it
43 graph.Fit(&f);
44 f.DrawClone("Same");
45
46 // Build and Draw a legend
47 TLegend leg(.1,.7,.3,.9,"Lab. Lesson 1");
48 leg.SetFillColor(0);
49 graph.SetFillColor(0);
50 leg.AddEntry(&graph,"Exp. Points");
51 leg.AddEntry(&f,"Th. Law");
52 leg.DrawClone("Same");
53
54 // Draw an arrow on the canvas
55 TArrow arrow(8,8,6.2,23,0.02,"|>");
56 arrow.SetLineWidth(2);
57 arrow.DrawClone();
58
59 // Add some text to the plot
60 TLatex text(8.2,7.5,"#splitline{Maximum}{Deviation}");
61 text.DrawClone();
62
63 mycanvas->Print("graph_with_law.pdf");
64}
65
66int main(){
67 macro1();
68 }
#define f(i)
Definition RSha256.hxx:104
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
@ kOpenCircle
Definition TAttMarker.h:61
Draw all kinds of Arrows.
Definition TArrow.h:29
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:182
A TGraphErrors is a TGraph with error bars.
To draw Mathematical Formula.
Definition TLatex.h:20
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TText * text
leg
Definition legend1.C:34
void macro1()
Definition macro1.C:13
int main()
Definition macro1.C:66