Attachment 'MyMacro.c'
Download 1 // Builds a graph with errors, displays it and saves it
2 // as image. First, include some header files (within,
3 // CINT these will be ignored).
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
13 void MyMacro(){
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,NULL,y_errs);
25 graph.SetTitle("Measurement XYZ;lenght [cm];Arb.Units");
26
27 // Make the plot looks 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 TCanvas* 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
66 #ifndef __CINT__
67 int main(){
68 MyMacro();
69 }
70 #endif
71
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.