Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename, comment and modernise graph tutorials #1

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tutorials/graphs/data_basic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-3.000000 -0.989992
-2.684211 -0.897212
-2.368421 -0.715699
-2.052632 -0.463406
-1.736842 -0.165284
-1.421053 0.149185
-1.105263 0.448899
-0.789474 0.704219
-0.473684 0.889894
-0.157895 0.987561
0.157895 0.987561
0.473684 0.889894
0.789474 0.704219
1.105263 0.448899
1.421053 0.149185
1.736842 -0.165284
2.052632 -0.463406
2.368421 -0.715699
2.684211 -0.897212
3.000000 -0.989992
80 changes: 60 additions & 20 deletions tutorials/graphs/gr001_basic.C
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
/// \file
/// \ingroup tutorial_graphs
/// \notebook
/// Draw a simple graph.
///
/// This tutorial demonstrates how to create simple graphs in ROOT. The example is divided into two sections:
/// - The first section plots data generated from arrays.
/// - The second section plots data read from a text file.
///
/// \macro_image
/// \macro_code
///
///
/// \author Rene Brun

void graph() {
TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
#include<fstream> //Include the library for reading the data file

void gr001_basic() {
TCanvas *c1 = new TCanvas("c1","Two simple graphs",200,10,700,500);

c1->Divide(2,1); //Dividing the canvas in subpads for distinguishing the two examples, [See documentation](https://root.cern/doc/master/classTCanvas.html)

c1->SetGrid();
//FIRST EXAMPLE (Available data)
c1->cd(1);

const Int_t n = 20;
const Int_t n = 20; //Fill the arrays x and y with the data points
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
printf(" i %i %f %f \n",i,x[i],y[i]);
}
TGraph *gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("a simple graph");
gr->GetXaxis()->SetTitle("X title");
gr->GetYaxis()->SetTitle("Y title");
gr->Draw("ACP");

// TCanvas::Update() draws the frame, after which one can change it
c1->Update();
c1->GetFrame()->SetBorderSize(12);
c1->Modified();

TGraph *gr1 = new TGraph(n,x,y); //Create a TGraph object, storing the number of data n and the x, y variables

//Set the color, width and style for the markers and line
gr1->SetLineColor(2);
gr1->SetLineWidth(4);
gr1->SetMarkerColor(4);
gr1->SetMarkerStyle(21);
gr1->SetTitle("Graph from available data"); //Choose title for the graph
gr1->GetXaxis()->SetTitle("X title"); //Choose title for the axis
gr1->GetYaxis()->SetTitle("Y title");

//Uncomment the following line to set a custom range for the x-axis (respectivly for the y-axis):
//gr1->GetXaxis()->SetRangeUser(0, 1.8);

gr1->Draw("ACP"); //"A" draw axes, "C" = draw a smooth line through the markers (optional) and "P" = draw markers for data points
//Optional customization can be done on a ROOT interactive session


//SECOND EXAMPLE (Data stored in a text file)
c1->cd(2);

const Int_t m = 20; //Known number of data points in the file
Double_t w[m], z[m];

std::ifstream file("data_basic.txt"); // Open the data file

// Use a for loop to read the data
for (Int_t i = 0; i < m; i++) {
file >> w[i] >> z[i]; //Fill the arrays with the data from the file
}
file.close(); //Close the file after reading


TGraph *gr2 = new TGraph(m, w, z); //Create a TGraph object for the file data
gr2->SetLineColor(4);
gr2->SetLineWidth(2);
gr2->SetMarkerColor(2);
gr2->SetMarkerStyle(20);
gr2->SetTitle("Graph from data file");
gr2->GetXaxis()->SetTitle("W title");
gr2->GetYaxis()->SetTitle("Z title");


gr2->Draw("ACP");
}

57 changes: 16 additions & 41 deletions tutorials/graphs/gr004_err_asym.C
Original file line number Diff line number Diff line change
@@ -1,56 +1,31 @@
/// \file
/// \ingroup tutorial_graphs
/// \notebook
/// A macro to demonstrate the functionality of TGraph::Apply() method.
/// TGraph::Apply applies a function `f` to all the data TGraph points.
/// `f` may be a 1-D function TF1 or 2-d function TF2.
/// The Y values of the graph are replaced by the new values computed using
/// the function.
///
/// This tutorial demonstrates the use of TGraphAsymmErrors to plot a graph with asymmetrical errors on both the x and y axes.
/// The errors for the x values are divided into low (left side of the marker) and high (right side of the marker) errors.
/// Similarly, for the y values, there are low (lower side of the marker) and high (upper side of the marker) errors.
///
/// \macro_image
/// \macro_code
///
///
/// \author Miro Helbich

void graphApply()
{
void gr004_err_asym() {
TCanvas *c2 = new TCanvas("c2","", 700, 500);

c2->SetGrid();
const Int_t npoints=3;
Double_t xaxis[npoints] = {1.,2.,3.};
Double_t yaxis[npoints] = {10.,20.,30.};
Double_t errorx[npoints] = {0.5,0.5,0.5};
Double_t errory[npoints] = {5.,5.,5.};

Double_t exl[npoints] = {0.5,0.5,0.5};
Double_t exh[npoints] = {0.5,0.5,0.5};
Double_t eyl[npoints] = {5.,5.,5.};
Double_t eyh[npoints] = {5.,5.,5.};

TGraph *gr1 = new TGraph(npoints,xaxis,yaxis);
TGraphErrors *gr2 = new TGraphErrors(npoints,xaxis,yaxis,errorx,errory);
TGraphAsymmErrors *gr3 = new TGraphAsymmErrors(npoints,xaxis,yaxis,exl,exh,eyl,eyh);
TF2 *ff = new TF2("ff","-1./y");

TCanvas *c1 = new TCanvas("c1","c1");
c1->Divide(2,3);

// TGraph
c1->cd(1);
gr1->DrawClone("A*");
c1->cd(2);
gr1->Apply(ff);
gr1->Draw("A*");
Double_t exl[npoints] = {0.5,0.2,0.1}; //Lower x errors
Double_t exh[npoints] = {0.5,0.3,0.4}; //Higher x errors
Double_t eyl[npoints] = {3.,5.,4.}; //Lower y errors
Double_t eyh[npoints] = {3.,5.,4.}; //Higher y errors

// TGraphErrors
c1->cd(3);
gr2->DrawClone("A*");
c1->cd(4);
gr2->Apply(ff);
gr2->Draw("A*");
TGraphAsymmErrors *gr = new TGraphAsymmErrors(npoints,xaxis,yaxis,exl,exh,eyl,eyh); //Create the TGraphAsymmErrors object with data and asymmetrical errors

// TGraphAsymmErrors
c1->cd(5);
gr3->DrawClone("A*");
c1->cd(6);
gr3->Apply(ff);
gr3->Draw("A*");
gr->SetTitle("A simple graph with asymmetrical errors");
gr->Draw("A*"); //"A" = draw axes and "*" = draw markers at the points with error bars
}
51 changes: 15 additions & 36 deletions tutorials/graphs/gr005_apply.C
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,34 @@
/// \ingroup tutorial_graphs
/// \notebook
/// A macro to demonstrate the functionality of TGraph::Apply() method.
/// TGraph::Apply applies a function `f` to all the data TGraph points.
/// `f` may be a 1-D function TF1 or 2-d function TF2.
/// The Y values of the graph are replaced by the new values computed using
/// the function.
/// TGraph::Apply applies a function `f` to all the data TGraph points, `f` may be a 1-D function TF1 or 2-d function TF2.
/// The Y values of the graph are replaced by the new values computed using the function.
///
///
/// The Apply() method can be used as well for TGraphErrors and TGraphAsymmErrors.
///
/// \macro_image
/// \macro_code
///
/// \author Miro Helbich

void graphApply()
{
void gr005_apply() {

const Int_t npoints=3;
Double_t xaxis[npoints] = {1.,2.,3.};
Double_t yaxis[npoints] = {10.,20.,30.};
Double_t errorx[npoints] = {0.5,0.5,0.5};
Double_t errory[npoints] = {5.,5.,5.};

Double_t exl[npoints] = {0.5,0.5,0.5};
Double_t exh[npoints] = {0.5,0.5,0.5};
Double_t eyl[npoints] = {5.,5.,5.};
Double_t eyh[npoints] = {5.,5.,5.};

TGraph *gr1 = new TGraph(npoints,xaxis,yaxis);
TGraphErrors *gr2 = new TGraphErrors(npoints,xaxis,yaxis,errorx,errory);
TGraphAsymmErrors *gr3 = new TGraphAsymmErrors(npoints,xaxis,yaxis,exl,exh,eyl,eyh);
TF2 *ff = new TF2("ff","-1./y");
TF2 *ff = new TF2("ff","-1./y"); //Defining the function `f`

TCanvas *c1 = new TCanvas("c1","c1");
c1->Divide(2,3);
TCanvas *c1 = new TCanvas("c1","c1", 0,0,700,500);
c1->Divide(2,1);

// TGraph
c1->cd(1);
gr1->DrawClone("A*");
gr1->DrawClone("A*"); //Using DrawClone to create a copy of the graph in the canvas.
c1->cd(2);
gr1->Apply(ff);
gr1->Draw("A*");

// TGraphErrors
c1->cd(3);
gr2->DrawClone("A*");
c1->cd(4);
gr2->Apply(ff);
gr2->Draw("A*");

// TGraphAsymmErrors
c1->cd(5);
gr3->DrawClone("A*");
c1->cd(6);
gr3->Apply(ff);
gr3->Draw("A*");
gr1->Apply(ff); //Applies the function `f` to all the data TGraph points
gr1->Draw("A*");
/* Without DrawClone, the modifications to gr1 via Apply(ff) are reflected in the original graph
displayed in c1 (the two drawn graphs are not independent). */
}
22 changes: 13 additions & 9 deletions tutorials/graphs/gr007_multigraph.C
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
/// \file
/// \ingroup tutorial_graphs
/// \notebook
/// Create and Draw a TMultiGraph.
/// TMultiGraph is used to combine multiple graphs into one plot.
/// Allowing to overlay different graphs can be useful for comparing different datasets
/// or for plotting multiple related graphs on the same canvas.
///
/// \macro_image
/// \macro_code
///
/// \author Rene Brun


void multigraph()
{
void gr007_multigraph() {
gStyle->SetOptFit();
auto c1 = new TCanvas("c1","multigraph",700,500);
c1->SetGrid();

// draw a frame to define the range
//Initialize a TMultiGraph to hold multiple graphs
//This ensures the entire dataset from all added graphs is visible without manual range adjustments.
auto mg = new TMultiGraph();

// create first graph
//Create first graph
const Int_t n1 = 10;
Double_t px1[] = {-0.1, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
Double_t py1[] = {-1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
Expand All @@ -31,10 +33,11 @@ void multigraph()
gr1->Fit("gaus","q");
auto func1 = (TF1 *) gr1->GetListOfFunctions()->FindObject("gaus");
func1->SetLineColor(kBlue);


//Add the first graph to the multigraph
mg->Add(gr1);

// create second graph
//Create second graph
const Int_t n2 = 10;
Float_t x2[] = {-0.28, 0.005, 0.19, 0.29, 0.45, 0.56,0.65,0.80,0.90,1.01};
Float_t y2[] = {2.1,3.86,7,9,10,10.55,9.64,7.26,5.42,2};
Expand All @@ -47,13 +50,14 @@ void multigraph()
gr2->Fit("pol5","q");
auto func2 = (TF1 *) gr2->GetListOfFunctions()->FindObject("pol5");
func2->SetLineColor(kRed);
func2->SetLineStyle(kDashed);
func2->SetLineStyle(2);

//Add the second graph to the multigraph
mg->Add(gr2);

mg->Draw("ap");

//force drawing of canvas to generate the fit TPaveStats
//Force drawing of canvas to generate the fit TPaveStats
c1->Update();

auto stats1 = (TPaveStats*) gr1->GetListOfFunctions()->FindObject("stats");
Expand Down
36 changes: 20 additions & 16 deletions tutorials/graphs/gr008_multierrors.C
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
/// \file
/// \ingroup tutorial_graphs
/// \notebook -js
/// Draw a graph with multiple y errors
/// Draw a graph with multiple errors. Multi errors can be usefull for distinguishing different type of errors,
/// for example statistical and systematic errors.
///
/// \macro_image
/// \macro_code
///
///
/// \author Simon Spies

void gmultierrors() {
void gr008_multierrors() {
TCanvas *c1 = new TCanvas("c1", "A Simple Graph with multiple y-errors", 200, 10, 700, 500);
c1->SetGrid();
c1->GetFrame()->SetBorderSize(12);

const Int_t np = 5;
Double_t x[np] = {0, 1, 2, 3, 4};
Double_t y[np] = {0, 2, 4, 1, 3};
Double_t exl[np] = {0.3, 0.3, 0.3, 0.3, 0.3};
Double_t exh[np] = {0.3, 0.3, 0.3, 0.3, 0.3};
Double_t eylstat[np] = {1, 0.5, 1, 0.5, 1};
Double_t eyhstat[np] = {0.5, 1, 0.5, 1, 0.5};
Double_t eylsys[np] = {0.5, 0.4, 0.8, 0.3, 1.2};
Double_t eyhsys[np] = {0.6, 0.7, 0.6, 0.4, 0.8};
Double_t exl[np] = {0.3, 0.3, 0.3, 0.3, 0.3}; //Lower x errors
Double_t exh[np] = {0.3, 0.3, 0.3, 0.3, 0.3}; //Higher x errors
Double_t eylstat[np] = {1, 0.5, 1, 0.5, 1}; //Lower y statistical errors
Double_t eyhstat[np] = {0.5, 1, 0.5, 1, 0.5}; //Higher y statistical errors
Double_t eylsys[np] = {0.5, 0.4, 0.8, 0.3, 1.2}; //Lower y systematic errors
Double_t eyhsys[np] = {0.6, 0.7, 0.6, 0.4, 0.8}; //Higher y systematic errors

TGraphMultiErrors* gme = new TGraphMultiErrors("gme", "TGraphMultiErrors Example", np, x, y, exl, exh, eylstat, eyhstat);
gme->AddYError(np, eylsys, eyhsys);
TGraphMultiErrors *gme = new TGraphMultiErrors("gme", "TGraphMultiErrors Example", np, x, y, exl, exh, eylstat, eyhstat); //Create the TGraphMultiErrors object
gme->AddYError(np, eylsys, eyhsys); //Add the systematic y-errors to the graph
gme->SetMarkerStyle(20);
gme->SetLineColor(kRed);
gme->GetAttLine(0)->SetLineColor(kRed);
gme->GetAttLine(1)->SetLineColor(kBlue);
gme->GetAttLine(0)->SetLineColor(kRed); //Color for statistical error bars
gme->GetAttLine(1)->SetLineColor(kBlue); //Color for systematic error bars
gme->GetAttFill(1)->SetFillStyle(0);

// Graph and x erros drawn with "APS"
// Stat Errors drawn with "Z"
// Sys Errors drawn with "5 s=0.5"
//Graph is drawn with the option "APS": "A" draw axes, "P" draw points and "S" draw symmetric horizontal error bars (x-errors)
//Statistical y-errors are drawn with the option "Z" vertical error bars.
//Systematic y-errors are drawn with the option "5 s=0.5":
//"5" draw rectangles to represent the systematic y-error bars.
//"s=0.5" scale the rectangles horizontally (along the x-axis) by a factor of 0.5.
gme->Draw("APS ; Z ; 5 s=0.5");


c1->Update();
}
Loading