Skip to content

Commit

Permalink
copy files from daniel's pr
Browse files Browse the repository at this point in the history
  • Loading branch information
martamaja10 committed Jan 10, 2025
1 parent 7793a83 commit 7f4f9d2
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 425 deletions.
89 changes: 64 additions & 25 deletions tutorials/visualisation/graphs/gr001_basic.C
Original file line number Diff line number Diff line change
@@ -1,38 +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]);
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 *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
}
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();
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");
}

16 changes: 3 additions & 13 deletions tutorials/visualisation/graphs/gr006_scatter.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,20 @@ void gr006_scatter()
double c[n];
double s[n];

<<<<<<<< HEAD:tutorials/visualisation/graphs/scatter.C
// Define four random data set
auto r = new TRandom();
for (int i = 0; i < n; i++) {
x[i] = 100 * r->Rndm(i);
y[i] = 200 * r->Rndm(i);
c[i] = 300 * r->Rndm(i);
s[i] = 400 * r->Rndm(i);
========
// Define four random data sets
auto r = new TRandom();
for (int i=0; i<n; i++) {
x[i] = 100*r->Rndm(i);
y[i] = 200*r->Rndm(i);
c[i] = 300*r->Rndm(i);
s[i] = 400*r->Rndm(i);
>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr006_scatter.C
}

auto scatter = new TScatter(n, x, y, c, s);
scatter->SetMarkerStyle(20);
scatter->SetTitle("Scatter plot title;X title;Y title;Z title");
scatter->GetXaxis()->SetRangeUser(20., 90.);
scatter->GetYaxis()->SetRangeUser(55., 90.);
scatter->GetZaxis()->SetRangeUser(10., 200.);
scatter->GetXaxis()->SetRangeUser(20.,90.);
scatter->GetYaxis()->SetRangeUser(55.,90.);
scatter->GetZaxis()->SetRangeUser(10.,200.);
scatter->Draw("A");
}
21 changes: 10 additions & 11 deletions tutorials/visualisation/graphs/gr008_multierrors.C
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ void gr008_multierrors() {
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 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}; //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); //Color for statistical error bars
Expand Down
32 changes: 11 additions & 21 deletions tutorials/visualisation/graphs/gr011_2Derrorsfit.C
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,33 @@ void gr011_2Derrorsfit()

// To generate some random data to put into the graph
TRandom r;
<<<<<<<< HEAD:tutorials/visualisation/graphs/graph2derrorsfit.C
TF2 *f2 = new TF2("f2", "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200", -6, 6, -6, 6);
f2->SetParameters(1, 1);
========
TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
f2->SetParameters(1,1);

>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr011_2Derrorsfit.C
TGraph2DErrors *dte = new TGraph2DErrors(nd);

// Fill the 2D graph. It was created only specifying the number of points, so all
// elements are empty. We now "fill" the values and errors with SetPoint and SetPointError.
// Note that the first point has index zero
Double_t zmax = 0;
for (Int_t i = 0; i < nd; i++) {
f2->GetRandom2(x, y);
rnd = r.Uniform(-e, e); // Generate a random number in [-e,e]
z = f2->Eval(x, y) * (1 + rnd);
if (z > zmax)
zmax = z;
dte->SetPoint(i, x, y, z);
ex = 0.05 * r.Rndm();
ey = 0.05 * r.Rndm();
ez = TMath::Abs(z * rnd);
dte->SetPointError(i, ex, ey, ez);
for (Int_t i=0; i<nd; i++) {
f2->GetRandom2(x,y);
rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
z = f2->Eval(x,y)*(1+rnd);
if (z>zmax) zmax = z;
dte->SetPoint(i,x,y,z);
ex = 0.05*r.Rndm();
ey = 0.05*r.Rndm();
ez = TMath::Abs(z*rnd);
dte->SetPointError(i,ex,ey,ez);
}
// If the fit is not needed, just draw dte here and skip the lines below
// dte->Draw("A p0");

<<<<<<<< HEAD:tutorials/visualisation/graphs/graph2derrorsfit.C
f2->SetParameters(0.5, 1.5);
========
// To do the fit we use a function, in this example the same f2 from above
f2->SetParameters(0.5,1.5);
>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr011_2Derrorsfit.C
dte->Fit(f2);
TF2 *fit2 = (TF2 *)dte->FindObject("f2");
TF2 *fit2 = (TF2*)dte->FindObject("f2");
fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
fit2->SetMaximum(zmax);
gStyle->SetHistTopMargin(0);
Expand Down
12 changes: 6 additions & 6 deletions tutorials/visualisation/graphs/gr014_polar3.C
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

void gr014_polar3()
{
TCanvas *CPol = new TCanvas("CPol", "TGraphPolar Examples", 500, 500);
TCanvas *CPol = new TCanvas("CPol","TGraphPolar Examples",500,500);

Double_t rmin = 0;
Double_t rmax = TMath::Pi() * 2;
Double_t rmax = TMath::Pi()*2;
Double_t r[1000];
Double_t theta[1000];

TF1 fp1("fplot", "cos(x)", rmin, rmax);
TF1 *fp1 = new TF1("fplot","cos(x)",rmin,rmax);
for (Int_t ipt = 0; ipt < 1000; ipt++) {
r[ipt] = ipt * (rmax - rmin) / 1000 + rmin;
theta[ipt] = fp1.Eval(r[ipt]);
r[ipt] = ipt*(rmax-rmin)/1000+rmin;
theta[ipt] = fp1->Eval(r[ipt]);
}

auto grP1 = new TGraphPolar(1000, r, theta);
TGraphPolar *grP1 = new TGraphPolar(1000, r, theta);
grP1->SetTitle("");
grP1->SetLineColor(2);
grP1->Draw("AOL");
Expand Down
15 changes: 4 additions & 11 deletions tutorials/visualisation/graphs/gr017_time.C
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

void gr017_time(Int_t nsteps = 500, Int_t np = 100)
{
if (np > 1000)
np = 1000;
if (np > 1000) np = 1000;
std::vector<Int_t> color(np);
std::vector<Double_t> rr(np), phi(np), dr(np), size(np);
TRandom3 r;
Expand Down Expand Up @@ -62,20 +61,14 @@ void gr017_time(Int_t nsteps = 500, Int_t np = 100)
// g->SaveAnimatedGif("gr17_time.gif");

// save object to a file
<<<<<<<< HEAD:tutorials/visualisation/graphs/gtime.C
auto f = TFile::Open("gtime.root", "recreate");
========
auto f = TFile::Open("gr17_time.root","recreate");
>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr017_time.C
f->WriteObject(g, "g");
delete f;

// to view this object in another session do
<<<<<<<< HEAD:tutorials/visualisation/graphs/gtime.C
// TFile::Open("gtime.root");
// g->Draw();
========
// TFile::Open("gr17_time.root");
// g->Draw();
>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr017_time.C
}



29 changes: 14 additions & 15 deletions tutorials/visualisation/graphs/gr018_time2.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,35 @@

void gr018_time2(Int_t nsteps = 200, Int_t np = 5000)
{
if (np > 5000)
np = 5000;
if (np > 5000) np = 5000;
std::vector<Int_t> color(np);
std::vector<Double_t> cosphi(np), sinphi(np), speed(np);
TRandom3 r;
Double_t xmin = 0, xmax = 10, ymin = -10, ymax = 10;
TGraphTime *g = new TGraphTime(nsteps, xmin, ymin, xmax, ymax);
TGraphTime *g = new TGraphTime(nsteps,xmin,ymin,xmax,ymax);
g->SetTitle("TGraphTime demo 2;X;Y");
Double_t fact = xmax / Double_t(nsteps);
Double_t fact = xmax/Double_t(nsteps);
for (Int_t i = 0; i < np; i++) { // calculate some object parameters
speed[i] = r.Uniform(0.5, 1);
Double_t phi = r.Gaus(0, TMath::Pi() / 6.);
cosphi[i] = fact * speed[i] * TMath::Cos(phi);
sinphi[i] = fact * speed[i] * TMath::Sin(phi);
Double_t rc = r.Rndm();
color[i] = kRed;
if (rc > 0.3)
color[i] = kBlue;
if (rc > 0.7)
color[i] = kYellow;
if (rc > 0.3) color[i] = kBlue;
if (rc > 0.7) color[i] = kYellow;
}
for (Int_t s = 0; s < nsteps; s++) { // fill the TGraphTime step by step
for (Int_t i = 0; i < np; i++) {
Double_t xx = s * cosphi[i];
if (xx < xmin)
continue;
Double_t yy = s * sinphi[i];
TMarker *m = new TMarker(xx, yy, 25);
Double_t xx = s*cosphi[i];
if (xx < xmin) continue;
Double_t yy = s*sinphi[i];
TMarker *m = new TMarker(xx,yy,25);
m->SetMarkerColor(color[i]);
m->SetMarkerSize(1.5 - s / (speed[i] * nsteps));
m->SetMarkerSize(1.5 -s/(speed[i]*nsteps));
g->Add(m, s);
}
g->Add(new TPaveLabel(.70, .92, .98, .99, TString::Format("shower at %5.3f nsec", 3. * s / nsteps), "brNDC"), s);
g->Add(new TPaveLabel(.70,.92,.98,.99,TString::Format("shower at %5.3f nsec",3.*s/nsteps),"brNDC"),s);
}

g->Draw();
Expand All @@ -63,3 +59,6 @@ void gr018_time2(Int_t nsteps = 200, Int_t np = 5000)
// start animation, can be stopped with g->Animate(kFALSE);
// g->Animate();
}



2 changes: 2 additions & 0 deletions tutorials/visualisation/graphs/gr102_reverse_axis.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ void gr102_reverse_axis() {
gr->SetPoint(1,9,9);
gr->SetPoint(2,14,14);


c->cd(1);
gPad->SetGrid();
gr->Draw("a pl "); //Plot with axis ("a") and line+points ("pl")

c->cd(2);
gPad->SetGrid();
gr->Draw("a pl rx ry "); //Plot with axis ("a") and line+points ("pl") with reverse X and Y axes

}
16 changes: 5 additions & 11 deletions tutorials/visualisation/graphs/gr103_zones.C
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@
/// \macro_code
/// \author Rene Brun

<<<<<<<< HEAD:tutorials/visualisation/graphs/zones.C
void zones()
{
TCanvas *c1 = new TCanvas("c1", "multipads", 900, 700);
========
void gr103_zones() {
TCanvas *c1 = new TCanvas("c1","multipads",900,700);
>>>>>>>> 021c32da2b (Rename and comment graphs tutorials):tutorials/visualisation/graphs/gr103_zones.C
gStyle->SetOptStat(0);
c1->Divide(2, 2, 0, 0);
TH2F *h1 = new TH2F("h1", "test1", 10, 0, 1, 20, 0, 20);
TH2F *h2 = new TH2F("h2", "test2", 10, 0, 1, 20, 0, 100);
TH2F *h3 = new TH2F("h3", "test3", 10, 0, 1, 20, -1, 1);
TH2F *h4 = new TH2F("h4", "test4", 10, 0, 1, 20, 0, 1000);
c1->Divide(2,2,0,0);
TH2F *h1 = new TH2F("h1","test1",10,0,1,20,0,20);
TH2F *h2 = new TH2F("h2","test2",10,0,1,20,0,100);
TH2F *h3 = new TH2F("h3","test3",10,0,1,20,-1,1);
TH2F *h4 = new TH2F("h4","test4",10,0,1,20,0,1000);

c1->cd(1);
gPad->SetTickx(2);
Expand Down
Loading

0 comments on commit 7f4f9d2

Please sign in to comment.