From 021c32da2bb1c08cd9482b8ac1d641a3036d17da Mon Sep 17 00:00:00 2001 From: dastudillo Date: Wed, 27 Nov 2024 16:34:42 +0100 Subject: [PATCH 1/2] Rename and comment graphs tutorials --- documentation/users-guide/Graphs.md | 4 +- graf2d/gpad/src/TButton.cxx | 4 +- graf2d/gpad/src/TControlBar.cxx | 2 +- graf2d/gviz/doc/index.md | 2 +- graf2d/gviz/src/TGraphStruct.cxx | 2 +- hist/hist/src/TGraphSmooth.cxx | 2 +- hist/hist/src/TGraphTime.cxx | 2 +- hist/hist/src/TMultiGraph.cxx | 4 +- hist/hist/src/TScatter.cxx | 2 +- hist/histpainter/doc/index.md | 4 +- hist/histpainter/src/TGraphPainter.cxx | 12 +- test/stressGUI.cxx | 4 +- tutorials/CMakeLists.txt | 10 +- tutorials/demos.C | 4 +- tutorials/graphs/{graph.C => gr001_basic.C} | 0 .../graphs/{gerrors.C => gr002_err_1gr.C} | 14 ++- .../graphs/{gerrors2.C => gr003_err_2gr.C} | 19 ++- .../graphs/{graphApply.C => gr004_err_asym.C} | 0 tutorials/graphs/gr005_apply.C | 56 +++++++++ .../graphs/{scatter.C => gr006_scatter.C} | 9 +- .../{multigraph.C => gr007_multigraph.C} | 0 .../{gmultierrors.C => gr008_multierrors.C} | 0 tutorials/graphs/{bent.C => gr009_bent_err.C} | 9 +- .../graphs/{bent.py => gr009_bent_err.py} | 8 +- .../{approx.C => gr010_approx_smooth.C} | 9 +- ...graph2derrorsfit.C => gr011_2Derrorsfit.C} | 14 ++- .../graphs/{graphpolar.C => gr012_polar.C} | 35 ++++-- tutorials/graphs/gr013_polar2.C | 49 ++++++++ .../graphs/{graphpolar3.C => gr014_polar3.C} | 9 +- .../graphs/{motorcycle.C => gr015_smooth.C} | 8 +- .../graphs/{graphstruct.C => gr016_struct.C} | 2 +- tutorials/graphs/{gtime.C => gr017_time.C} | 17 ++- tutorials/graphs/{gtime2.C => gr018_time2.C} | 9 +- .../{graphShade.C => gr101_shade_area.C} | 0 .../{graphreverse.C => gr102_reverse_axis.C} | 0 tutorials/graphs/{zones.C => gr103_zones.C} | 18 ++- tutorials/graphs/{zones.py => gr103_zones.py} | 15 ++- ...aphpalettecolor.C => gr104_palettecolor.C} | 6 +- ...color.C => gr105_multigraphpalettecolor.C} | 5 +- ...xclusiongraph.C => gr106_exclusiongraph.C} | 13 +- ...lusiongraph2.C => gr107_exclusiongraph2.C} | 15 ++- ...iesFromCSV.C => gr108_timeSeriesFromCSV.C} | 8 +- ...sFromCSV.py => gr108_timeSeriesFromCSV.py} | 4 +- ...SV_TDF.C => gr109_timeSeriesFromCSV_TDF.C} | 8 +- tutorials/graphs/{waves.C => gr201_waves.C} | 41 ++++--- .../{graphtext.C => gr202_textmarkers.C} | 4 +- .../graphs/{hlGraph1.C => gr301_highlight1.C} | 5 +- .../graphs/{hlGraph2.C => gr302_highlight2.C} | 5 +- tutorials/graphs/{zdemo.C => gr303_zdemo.C} | 3 +- tutorials/graphs/graphpolar2.C | 41 ------- tutorials/graphs/index.md | 111 ++++++++++++++++++ tutorials/image/trans_graph.C | 4 +- tutorials/legacy/benchmarks.C | 12 +- 53 files changed, 453 insertions(+), 190 deletions(-) rename tutorials/graphs/{graph.C => gr001_basic.C} (100%) rename tutorials/graphs/{gerrors.C => gr002_err_1gr.C} (51%) rename tutorials/graphs/{gerrors2.C => gr003_err_2gr.C} (55%) rename tutorials/graphs/{graphApply.C => gr004_err_asym.C} (100%) create mode 100644 tutorials/graphs/gr005_apply.C rename tutorials/graphs/{scatter.C => gr006_scatter.C} (75%) rename tutorials/graphs/{multigraph.C => gr007_multigraph.C} (100%) rename tutorials/graphs/{gmultierrors.C => gr008_multierrors.C} (100%) rename tutorials/graphs/{bent.C => gr009_bent_err.C} (71%) rename tutorials/graphs/{bent.py => gr009_bent_err.py} (77%) rename tutorials/graphs/{approx.C => gr010_approx_smooth.C} (89%) rename tutorials/graphs/{graph2derrorsfit.C => gr011_2Derrorsfit.C} (66%) rename tutorials/graphs/{graphpolar.C => gr012_polar.C} (61%) create mode 100644 tutorials/graphs/gr013_polar2.C rename tutorials/graphs/{graphpolar3.C => gr014_polar3.C} (65%) rename tutorials/graphs/{motorcycle.C => gr015_smooth.C} (94%) rename tutorials/graphs/{graphstruct.C => gr016_struct.C} (98%) rename tutorials/graphs/{gtime.C => gr017_time.C} (82%) rename tutorials/graphs/{gtime2.C => gr018_time2.C} (88%) rename tutorials/graphs/{graphShade.C => gr101_shade_area.C} (100%) rename tutorials/graphs/{graphreverse.C => gr102_reverse_axis.C} (100%) rename tutorials/graphs/{zones.C => gr103_zones.C} (50%) rename tutorials/graphs/{zones.py => gr103_zones.py} (50%) rename tutorials/graphs/{graphpalettecolor.C => gr104_palettecolor.C} (90%) rename tutorials/graphs/{multigraphpalettecolor.C => gr105_multigraphpalettecolor.C} (91%) rename tutorials/graphs/{exclusiongraph.C => gr106_exclusiongraph.C} (67%) rename tutorials/graphs/{exclusiongraph2.C => gr107_exclusiongraph2.C} (81%) rename tutorials/graphs/{timeSeriesFromCSV.C => gr108_timeSeriesFromCSV.C} (87%) rename tutorials/graphs/{timeSeriesFromCSV.py => gr108_timeSeriesFromCSV.py} (90%) rename tutorials/graphs/{timeSeriesFromCSV_TDF.C => gr109_timeSeriesFromCSV_TDF.C} (81%) rename tutorials/graphs/{waves.C => gr201_waves.C} (72%) rename tutorials/graphs/{graphtext.C => gr202_textmarkers.C} (97%) rename tutorials/graphs/{hlGraph1.C => gr301_highlight1.C} (89%) rename tutorials/graphs/{hlGraph2.C => gr302_highlight2.C} (94%) rename tutorials/graphs/{zdemo.C => gr303_zdemo.C} (99%) delete mode 100644 tutorials/graphs/graphpolar2.C create mode 100644 tutorials/graphs/index.md diff --git a/documentation/users-guide/Graphs.md b/documentation/users-guide/Graphs.md index 59f7f3ace8ec9..7efb798cc34da 100644 --- a/documentation/users-guide/Graphs.md +++ b/documentation/users-guide/Graphs.md @@ -229,7 +229,7 @@ gStyle->SetEndErrorSize(np); The four parameters of **`TGraphErrors`** are: `X, Y` (as in **`TGraph`**), `X`-errors, and `Y`-errors - the size of the errors in the `x` and `y` direction. Next example is -`$ROOTSYS/tutorials/graphs/gerrors.C.` +`$ROOTSYS/tutorials/graphs/gr002_err_1gr.C.` ``` {.cpp} { @@ -702,7 +702,7 @@ root[] gr5->Draw("ALP") ``` For more graph examples see the scripts: `$ROOTSYS/tutorials` directory -`graph.C`, `gerrors.C`, `zdemo.C`, and `gerrors2.C`. +`gr001_basic.C`, `gr002_err_1gr.C`, `gr303_zdemo.C`, and `gr003_err_2gr.C`. ![A graph with axis titles](pictures/0300005D.png) diff --git a/graf2d/gpad/src/TButton.cxx b/graf2d/gpad/src/TButton.cxx index 8acdb204a3314..2751c076ba7ff 100644 --- a/graf2d/gpad/src/TButton.cxx +++ b/graf2d/gpad/src/TButton.cxx @@ -68,8 +68,8 @@ void but() { but3->Draw(); // Create last button with no name. Instead a graph is draw inside the button -// Clicking on this button will invoke the macro $ROOTSYS/tutorials/graphs/graph.C - button = new TButton("",".x tutorials/graphs/graph.C",0.15,0.15,0.85,0.38); +// Clicking on this button will invoke the macro $ROOTSYS/tutorials/graphs/gr001_basic.C + button = new TButton("",".x tutorials/graphs/gr001_basic.C",0.15,0.15,0.85,0.38); button->SetFillColor(42); button->Draw(); button->SetEditable(kTRUE); diff --git a/graf2d/gpad/src/TControlBar.cxx b/graf2d/gpad/src/TControlBar.cxx index c94f52df6058a..7bfa531f63db0 100644 --- a/graf2d/gpad/src/TControlBar.cxx +++ b/graf2d/gpad/src/TControlBar.cxx @@ -47,7 +47,7 @@ will return the name of the last clicked button. "A Simple Fitting Example"); bar.AddButton("draw2dopt", ".x hist/draw2dopt.C", "Drawing Options for 2D Histograms"); - bar.AddButton("graph", ".x graphs/graph.C", + bar.AddButton("graph", ".x graphs/gr001_basic.C", "Examples of a simple graph"); bar.AddButton("tornado", ".x graphics/tornado.C", "Examples of 3-D PolyMarkers"); diff --git a/graf2d/gviz/doc/index.md b/graf2d/gviz/doc/index.md index 2e7f22a3621ce..2a0ce4298ba36 100644 --- a/graf2d/gviz/doc/index.md +++ b/graf2d/gviz/doc/index.md @@ -2,5 +2,5 @@ \ingroup Graphics2D \brief Interface to the graphing package `graphviz` -- graphstruct.C is an example of the graphviz interface classes usage. +- gr016_struct.C is an example of the graphviz interface classes usage. diff --git a/graf2d/gviz/src/TGraphStruct.cxx b/graf2d/gviz/src/TGraphStruct.cxx index 516acde8deba8..79e38d1ed7203 100644 --- a/graf2d/gviz/src/TGraphStruct.cxx +++ b/graf2d/gviz/src/TGraphStruct.cxx @@ -49,7 +49,7 @@ three classes: a TGraphStruct. Begin_Macro(source) -../../../tutorials/graphs/graphstruct.C +../../../tutorials/graphs/gr016_struct.C End_Macro A graph structure can be dumped into a "dot" file using DumpAsDotFile. diff --git a/hist/hist/src/TGraphSmooth.cxx b/hist/hist/src/TGraphSmooth.cxx index 63a4f6a296d65..828fb1a578189 100644 --- a/hist/hist/src/TGraphSmooth.cxx +++ b/hist/hist/src/TGraphSmooth.cxx @@ -30,7 +30,7 @@ ClassImp(TGraphSmooth); /** \class TGraphSmooth \ingroup Graphs A helper class to smooth TGraph. -see examples in $ROOTSYS/tutorials/graphs/motorcycle.C and approx.C +see examples in gr010_approx_smooth.C and gr015_smooth.C */ TGraphSmooth::TGraphSmooth() diff --git a/hist/hist/src/TGraphTime.cxx b/hist/hist/src/TGraphTime.cxx index e0a84641215f8..6ba3f47ecf0b0 100644 --- a/hist/hist/src/TGraphTime.cxx +++ b/hist/hist/src/TGraphTime.cxx @@ -24,7 +24,7 @@ ClassImp(TGraphTime); TGraphTime is used to draw a set of objects evolving with nsteps in time between tmin and tmax. Each time step has a new list of objects. This list can be identical to the list of objects in the previous steps, but with different attributes. -see example of use in $ROOTSYS/tutorials/graphs/gtime.C +See example of use in gr017_time.C */ //////////////////////////////////////////////////////////////////////////////// diff --git a/hist/hist/src/TMultiGraph.cxx b/hist/hist/src/TMultiGraph.cxx index 94b7d6f2fd541..b287cdc05cc97 100644 --- a/hist/hist/src/TMultiGraph.cxx +++ b/hist/hist/src/TMultiGraph.cxx @@ -224,7 +224,7 @@ Automatic coloring according to the current palette is available as shown in the following example: Begin_Macro(source) -../../../tutorials/graphs/multigraphpalettecolor.C +../../../tutorials/graphs/gr105_multigraphpalettecolor.C End_Macro \anchor MG01f @@ -324,7 +324,7 @@ overlap. The following example shows how to make them all visible. Begin_Macro(source) -../../../tutorials/graphs/multigraph.C +../../../tutorials/graphs/gr007_multigraph.C End_Macro \anchor MG03 diff --git a/hist/hist/src/TScatter.cxx b/hist/hist/src/TScatter.cxx index 6bf8504115729..e599d55cc2fd5 100644 --- a/hist/hist/src/TScatter.cxx +++ b/hist/hist/src/TScatter.cxx @@ -39,7 +39,7 @@ color map and the fourth on the marker size. The following example demonstrates how it works: Begin_Macro(source) -../../../tutorials/graphs/scatter.C +../../../tutorials/graphs/gr006_scatter.C End_Macro ### TScatter's plotting options diff --git a/hist/histpainter/doc/index.md b/hist/histpainter/doc/index.md index f31c0679ec5fc..ec2c4e0677466 100644 --- a/hist/histpainter/doc/index.md +++ b/hist/histpainter/doc/index.md @@ -4,8 +4,8 @@ The histograms and graphs plotting options are described in details in the THistPainter and TGraphPainter classes. Some related tutorials: - - graph.C: Using and drawing a simple TGraph. - - graph2derrorsfit.C: TGraph2D with errors drawing. + - gr001_basic.C: Using and drawing a simple TGraph. + - gr011_2Derrorsfit.C: TGraph2D with errors drawing. - h1draw.C: Drawing Options for 1D Histograms. - hbars.C: Demo of option bar with histograms. - hsimple.C: Simple drawing of a 1D Histograms. diff --git a/hist/histpainter/src/TGraphPainter.cxx b/hist/histpainter/src/TGraphPainter.cxx index 014da7825d411..c9ad10610c32d 100644 --- a/hist/histpainter/src/TGraphPainter.cxx +++ b/hist/histpainter/src/TGraphPainter.cxx @@ -211,7 +211,7 @@ case the line width number is interpreted as: The current fill area attributes are used to draw the hatched zone. Begin_Macro(source) -../../../tutorials/graphs/exclusiongraph.C +../../../tutorials/graphs/gr106_exclusiongraph.C End_Macro \anchor GrP3 @@ -527,11 +527,11 @@ is determined according to the number of objects having palette coloring in the current pad. Begin_Macro(source) -../../../tutorials/graphs/graphpalettecolor.C +../../../tutorials/graphs/gr104_palettecolor.C End_Macro Begin_Macro(source) -../../../tutorials/graphs/multigraphpalettecolor.C +../../../tutorials/graphs/gr105_multigraphpalettecolor.C End_Macro \anchor GrP6 @@ -630,18 +630,18 @@ graphically. Point will be highlighted as "point circle" (presented by marker object). Moreover, any highlight (change of point) emits signal `TCanvas::Highlighted()` which allows the user to react and call their own function. For a better understanding please see also the tutorials -`$ROOTSYS/tutorials/graphs/hlGraph*.C` files. +`$ROOTSYS/tutorials/graphs/gr*_highlight*.C` files. Highlight mode is switched on/off by `TGraph::SetHighlight()` function or interactively from `TGraph` context menu. `TGraph::IsHighlight()` to verify whether the highlight mode enabled or disabled, default it is disabled. ~~~ {.cpp} - root [0] .x $ROOTSYS/tutorials/graphs/gerrors2.C + root [0] .x $ROOTSYS/tutorials/graphs/gr003_err_2gr.C root [1] // try SetHighlight() interactively from TGraph context menu ~~~ -\image html hlgerrors2.gif "Highlight mode for graph" +\image html hl_gr003_err_2gr.gif "Highlight mode for graph" See how it is used highlight mode and user function diff --git a/test/stressGUI.cxx b/test/stressGUI.cxx index b8b16217574df..5d466e1142416 100644 --- a/test/stressGUI.cxx +++ b/test/stressGUI.cxx @@ -2211,8 +2211,8 @@ void testControlBars() bar->AddButton("fit1", ".x fit/fit1.C", "A Simple Fitting Example"); bar->AddButton("multifit", ".x fit/multifit.C", "Fitting in Subranges of Histograms"); bar->AddButton("h1draw", ".x hist/h1draw.C", "Drawing Options for 1D Histograms"); - bar->AddButton("graph", ".x graphs/graph.C", "Example of a Simple Graph"); - bar->AddButton("gerrors", ".x graphs/gerrors.C", "Example of a Graph with Error Bars"); + bar->AddButton("graph", ".x graphs/gr001_graph.C", "Example of a Simple Graph"); + bar->AddButton("gerrors", ".x graphs/gr002_err_1gr.C", "Example of a Graph with Error Bars"); bar->AddButton("tornado", ".x graphics/tornado.C", "Examples of 3-D PolyMarkers"); bar->AddButton("shapes", ".x geom/shapes.C", "The Geometry Shapes"); bar->AddButton("geometry", ".x geom/geometry.C", "Creation of the NA49 Geometry File"); diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index 7b5cfc3a25d57..d91f8ee5db9b7 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -101,7 +101,7 @@ if (NOT dataframe) # RDataFrame list(APPEND dataframe_veto dataframe/*.C dataframe/*.py) # RDataFrame tutorial in graphs - list(APPEND dataframe_veto graphs/timeSeriesFromCSV_TDF.C) + list(APPEND dataframe_veto graphs/gr109_timeSeriesFromCSV_TDF.C) # TMVA tutorials dependent on RDataFrame list(APPEND dataframe_veto tmva/tmva*.C) list(APPEND dataframe_veto tmva/TMVA_SOFIE_RDataFrame*.C) @@ -244,7 +244,7 @@ if(NOT ROOT_opengl_FOUND) endif() if(NOT GRAPHVIZ_FOUND) - set(gviz_veto graphs/graphstruct.C) + set(gviz_veto graphs/gr016_struct.C) endif() if(NOT TBB_FOUND AND NOT builtin_tbb) @@ -434,7 +434,7 @@ if(MSVC AND NOT llvm13_broken_tests) list(APPEND extra_veto dataframe/df007_snapshot.C graphics/earth.C - graphs/motorcycle.C + graphs/gr015_smooth.C v7/ntuple/ntpl001_staff.C) endif() endif() @@ -504,8 +504,8 @@ set(returncode_1 fit/fit2a.C graphics/earth.C graphics/pavetext.C graphics/tmathtext.C graphics/tmathtext2.C - graphs/exclusiongraph.C - graphs/graphstruct.C + graphs/gr106_exclusiongraph.C + graphs/gr016_struct.C hist/ContourList.C hist/hbars.C hist/th2polyBoxes.C diff --git a/tutorials/demos.C b/tutorials/demos.C index aca4d116d53a4..017b18099d9b4 100644 --- a/tutorials/demos.C +++ b/tutorials/demos.C @@ -28,8 +28,8 @@ void demos() { bar->AddButton("fit1", ".x fit/fit1.C", "A Simple Fitting Example"); bar->AddButton("multifit", ".x fit/multifit.C", "Fitting in Subranges of Histograms"); bar->AddButton("h1ReadAndDraw", ".x hist/h1ReadAndDraw.C", "Drawing Options for 1D Histograms"); - bar->AddButton("graph", ".x graphs/graph.C", "Example of a Simple Graph"); - bar->AddButton("gerrors", ".x graphs/gerrors.C", "Example of a Graph with Error Bars"); + bar->AddButton("graph", ".x graphs/gr001_basic.C", "Example of a Simple Graph"); + bar->AddButton("gerrors", ".x graphs/gr002_err_1gr.C", "Example of a Graph with Error Bars"); bar->AddButton("tornado", ".x graphics/tornado.C", "Examples of 3-D PolyMarkers"); bar->AddButton("geometry", ".x geom/rootgeom.C", "Example of TGeoManager drawing"); bar->AddButton("file", ".x io/file.C", "The ROOT File Format"); diff --git a/tutorials/graphs/graph.C b/tutorials/graphs/gr001_basic.C similarity index 100% rename from tutorials/graphs/graph.C rename to tutorials/graphs/gr001_basic.C diff --git a/tutorials/graphs/gerrors.C b/tutorials/graphs/gr002_err_1gr.C similarity index 51% rename from tutorials/graphs/gerrors.C rename to tutorials/graphs/gr002_err_1gr.C index 7a9d80cd7de9f..9dfb0bcb7795c 100644 --- a/tutorials/graphs/gerrors.C +++ b/tutorials/graphs/gr002_err_1gr.C @@ -1,28 +1,36 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -js -/// Draw a graph with error bars +/// Create and draw a graph with error bars. If more graphs are needed, see the +/// [gr03_err2gr.C](https://root.cern/doc/master/gerrors2_8C.html) tutorial +/// +/// See the [TGraphErrors documentation](https://root.cern/doc/master/classTGraphErrors.html) /// /// \macro_image /// \macro_code -/// /// \author Rene Brun -void gerrors() { +void gr002_err_1gr() { TCanvas *c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500); c1->SetGrid(); c1->GetFrame()->SetBorderSize(12); + // We will use the constructor requiring: the number of points, arrays containing the x-and y-axis values, and arrays with the x- andy-axis errors const Int_t n = 10; Float_t x[n] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95}; Float_t y[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1}; Float_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05}; Float_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8}; + + // If all x-axis errors should zero, just provide a single 0 in place of ex TGraphErrors *gr = new TGraphErrors(n,x,y,ex,ey); + gr->SetTitle("TGraphErrors Example"); gr->SetMarkerColor(4); gr->SetMarkerStyle(21); + + // To draw in a new/empty canvas or pad, include the option "A" so that the axes are drawn (leave it out if the graph is to be drawn on top of an existing plot gr->Draw("ALP"); c1->Update(); diff --git a/tutorials/graphs/gerrors2.C b/tutorials/graphs/gr003_err_2gr.C similarity index 55% rename from tutorials/graphs/gerrors2.C rename to tutorials/graphs/gr003_err_2gr.C index 45a725cdefa39..a560cc080f431 100644 --- a/tutorials/graphs/gerrors2.C +++ b/tutorials/graphs/gr003_err_2gr.C @@ -1,15 +1,23 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -js -/// Draw two graphs with error bars +/// Create and draw two graphs with error bars, superposed on the same canvas +/// +/// We first draw an empty frame with the axes, then draw the graphs on top of it +/// Note that the graphs should have the same or very close ranges (in both axis), +/// otherwise they may not be visible in the frame. +/// +/// Alternatively, an automatic axis scaling can be achieved via a +/// [TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html) +/// +/// See the [TGraphErrors documentation](https://root.cern/doc/master/classTGraphErrors.html) /// /// \macro_image /// \macro_code -/// /// \author Rene Brun -void gerrors2() { - TCanvas *c1 = new TCanvas("c1","gerrors2",200,10,700,500); +void gr003_err_2gr() { + TCanvas *c1 = new TCanvas("c1","2 graphs with errors",200,10,700,500); c1->SetGrid(); // draw a frame to define the range @@ -19,14 +27,17 @@ void gerrors2() { c1->GetFrame()->SetBorderSize(12); // create first graph + // We will use the constructor requiring: the number of points, arrays containing the x-and y-axis values, and arrays with the x- andy-axis errors const Int_t n1 = 10; Double_t xval1[] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95}; Double_t yval1[] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1}; Double_t ex1[] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05}; Double_t ey1[] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8}; + // If all x-axis errors should zero, just provide a single 0 in place of ex1 TGraphErrors *gr1 = new TGraphErrors(n1,xval1,yval1,ex1,ey1); gr1->SetMarkerColor(kBlue); gr1->SetMarkerStyle(21); + // Since we already have a frame in the canvas, we draw the graph without the option "A" (which draws axes for this graph) gr1->Draw("LP"); // create second graph diff --git a/tutorials/graphs/graphApply.C b/tutorials/graphs/gr004_err_asym.C similarity index 100% rename from tutorials/graphs/graphApply.C rename to tutorials/graphs/gr004_err_asym.C diff --git a/tutorials/graphs/gr005_apply.C b/tutorials/graphs/gr005_apply.C new file mode 100644 index 0000000000000..3eb6cce7350c0 --- /dev/null +++ b/tutorials/graphs/gr005_apply.C @@ -0,0 +1,56 @@ +/// \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. +/// +/// \macro_image +/// \macro_code +/// +/// \author Miro Helbich + +void graphApply() +{ + 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*"); + + // 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*"); +} diff --git a/tutorials/graphs/scatter.C b/tutorials/graphs/gr006_scatter.C similarity index 75% rename from tutorials/graphs/scatter.C rename to tutorials/graphs/gr006_scatter.C index 068d53911674b..5742a857882e0 100644 --- a/tutorials/graphs/scatter.C +++ b/tutorials/graphs/gr006_scatter.C @@ -1,14 +1,15 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Draw a scatter plot. +/// Draw a scatter plot for 4 variables, mapped to: x, y, marker colour and marker size. +/// +/// TScatter is available since ROOT v.6.30. See the [TScatter documentation](https://root.cern/doc/master/classTScatter.html) /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void scatter() +void gr006_scatter() { auto canvas = new TCanvas(); canvas->SetRightMargin(0.14); @@ -20,7 +21,7 @@ void scatter() double c[n]; double s[n]; - // Define four random data set + // Define four random data sets auto r = new TRandom(); for (int i=0; iRndm(i); diff --git a/tutorials/graphs/multigraph.C b/tutorials/graphs/gr007_multigraph.C similarity index 100% rename from tutorials/graphs/multigraph.C rename to tutorials/graphs/gr007_multigraph.C diff --git a/tutorials/graphs/gmultierrors.C b/tutorials/graphs/gr008_multierrors.C similarity index 100% rename from tutorials/graphs/gmultierrors.C rename to tutorials/graphs/gr008_multierrors.C diff --git a/tutorials/graphs/bent.C b/tutorials/graphs/gr009_bent_err.C similarity index 71% rename from tutorials/graphs/bent.C rename to tutorials/graphs/gr009_bent_err.C index 8415dd5af88cc..5bfe00506786f 100644 --- a/tutorials/graphs/bent.C +++ b/tutorials/graphs/gr009_bent_err.C @@ -1,14 +1,17 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -js -/// Bent error bars +/// Graph with bent error bars. See the [TGraphBentErrors documentation](https://root.cern/doc/master/classTGraphBentErrors.html) +/// +/// exl / exh: low and high (left/right) errors in x; similar for y +/// e*d: delta, in axis units, to be added/subtracted (if >0 or <0) in x or y from +/// the data point's position to use as end point of the corresponding error /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void bent() +void gr009_bent_err() { const Int_t n = 10; Double_t x[n] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95}; diff --git a/tutorials/graphs/bent.py b/tutorials/graphs/gr009_bent_err.py similarity index 77% rename from tutorials/graphs/bent.py rename to tutorials/graphs/gr009_bent_err.py index 63c10a23bfb7b..5ea74fe765e0f 100644 --- a/tutorials/graphs/bent.py +++ b/tutorials/graphs/gr009_bent_err.py @@ -1,7 +1,13 @@ ## \file ## \ingroup tutorial_graphs ## \notebook -js -## Bent error bars. Inspired from work of Olivier Couet. +## Graph with bent error bars. Inspired from work of Olivier Couet. +## +## See the [TGraphBentErrors documentation](https://root.cern/doc/master/classTGraphBentErrors.html) +## +## exl / exh: low and high (left/right) errors in x; similar for y +## e*d: delta, in axis units, to be added/subtracted (if >0 or <0) in x or y from +## the data point's position to use as end point of the corresponding error ## ## \macro_image ## \macro_code diff --git a/tutorials/graphs/approx.C b/tutorials/graphs/gr010_approx_smooth.C similarity index 89% rename from tutorials/graphs/approx.C rename to tutorials/graphs/gr010_approx_smooth.C index baefe571744d9..35f171460037d 100644 --- a/tutorials/graphs/approx.C +++ b/tutorials/graphs/gr010_approx_smooth.C @@ -1,17 +1,18 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -js -/// Macro to test interpolation function Approx +/// Create a TGraphSmooth and show the usage of the interpolation function Approx +/// +/// See the [TGraphSmooth documentation](https://root.cern/doc/master/classTGraphSmooth.html) /// /// \macro_image /// \macro_code -/// /// \author Christian Stratowa, Vienna, Austria. TCanvas *vC1; TGraph *grxy, *grin, *grout; -void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt) +void gr010_approx_smooth(Int_t pad, const char *title, const char *xt, const char *yt) { vC1->cd(pad); TH1F *vFrame = gPad->DrawFrame(0,0,15,150); @@ -30,7 +31,7 @@ void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt) grout->DrawClone("LP"); } -void approx() +void gr10_approx_smooth() { // Test data (square) Int_t n = 11; diff --git a/tutorials/graphs/graph2derrorsfit.C b/tutorials/graphs/gr011_2Derrorsfit.C similarity index 66% rename from tutorials/graphs/graph2derrorsfit.C rename to tutorials/graphs/gr011_2Derrorsfit.C index db83844d54f1b..3d3b68a19568c 100644 --- a/tutorials/graphs/graph2derrorsfit.C +++ b/tutorials/graphs/gr011_2Derrorsfit.C @@ -1,11 +1,10 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Draw and fit a TGraph2DErrors +/// Create, draw and fit a TGraph2DErrors. See the [TGraph2DErrors documentation](https://root.cern/doc/master/classTGraph2DErrors.html) /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet #include @@ -15,7 +14,7 @@ #include #include -void graph2derrorsfit() +void gr011_2Derrorsfit() { TCanvas *c1 = new TCanvas("c1"); @@ -23,12 +22,16 @@ void graph2derrorsfit() Double_t e = 0.3; Int_t nd = 500; + // To generate some random data to put into the graph TRandom r; TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6); f2->SetParameters(1,1); + TGraph2DErrors *dte = new TGraph2DErrors(nd); - // Fill the 2D graph + // 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; iGetRandom2(x,y); @@ -41,7 +44,10 @@ void graph2derrorsfit() 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"); + // To do the fit we use a function, in this example the same f2 from above f2->SetParameters(0.5,1.5); dte->Fit(f2); TF2 *fit2 = (TF2*)dte->FindObject("f2"); diff --git a/tutorials/graphs/graphpolar.C b/tutorials/graphs/gr012_polar.C similarity index 61% rename from tutorials/graphs/graphpolar.C rename to tutorials/graphs/gr012_polar.C index 9472bb013ef3b..dae8ee6c0cf59 100644 --- a/tutorials/graphs/graphpolar.C +++ b/tutorials/graphs/gr012_polar.C @@ -1,21 +1,27 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Create and draw a polar graph. +/// Create and draw a polar graph. See the [TGraphPolar documentation](https://root.cern/doc/master/classTGraphPolar.html) +/// +/// Since TGraphPolar is a TGraphErrors, it is painted with +/// [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) options. +/// +/// With GetPolargram we retrieve the polar axis to format it; see the +/// [TGraphPolargram documentation](https://root.cern/doc/master/classTGraphPolargram.html) /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void graphpolar() +void gr012_polar() { // Illustrates how to use TGraphPolar TCanvas * CPol = new TCanvas("CPol","TGraphPolar Examples",1200,600); CPol->Divide(2,1); - CPol->cd(1); + // Left-side pad. Two graphs without errors + CPol->cd(1); Double_t xmin=0; Double_t xmax=TMath::Pi()*2; @@ -24,13 +30,12 @@ void graphpolar() Double_t xval1[20]; Double_t yval1[20]; + // Graph 1 to be drawn with line and fill TF1 * fplot = new TF1("fplot","cos(2*x)*cos(20*x)",xmin,xmax); - for (Int_t ipt = 0; ipt < 1000; ipt++){ - x[ipt] = ipt*(xmax-xmin)/1000+xmin; + x[ipt] = ipt*(xmax-xmin)/1000 + xmin; y[ipt] = fplot->Eval(x[ipt]); } - TGraphPolar * grP = new TGraphPolar(1000,x,y); grP->SetLineColor(2); grP->SetLineWidth(2); @@ -38,11 +43,11 @@ void graphpolar() grP->SetFillColor(2); grP->Draw("AFL"); + // Graph 2 to be drawn superposed over graph 1, with curve and polymarker for (Int_t ipt = 0; ipt < 20; ipt++){ xval1[ipt] = x[1000/20*ipt]; yval1[ipt] = y[1000/20*ipt]; } - TGraphPolar * grP1 = new TGraphPolar(20,xval1,yval1); grP1->SetMarkerStyle(29); grP1->SetMarkerSize(2); @@ -50,15 +55,17 @@ void graphpolar() grP1->SetLineColor(4); grP1->Draw("CP"); - // Update, otherwise GetPolargram returns 0 + // To format the polar axis, we retrieve the TGraphPolargram. + // First update the canvas, otherwise GetPolargram returns 0 CPol->Update(); if (grP1->GetPolargram()) { grP1->GetPolargram()->SetTextColor(8); grP1->GetPolargram()->SetRangePolar(-TMath::Pi(),TMath::Pi()); grP1->GetPolargram()->SetNdivPolar(703); - grP1->GetPolargram()->SetToRadian(); + grP1->GetPolargram()->SetToRadian(); // tell ROOT that the x and xval1 are in radians } + // Right-side pad. One graph with errors CPol->cd(2); Double_t x2[30]; Double_t y2[30]; @@ -71,6 +78,7 @@ void graphpolar() ey[ipt] = 0.2; } + // Grah to be drawn with polymarker and errors TGraphPolar * grPE = new TGraphPolar(30,x2,y2,ex,ey); grPE->SetMarkerStyle(22); grPE->SetMarkerSize(1.5); @@ -78,12 +86,13 @@ void graphpolar() grPE->SetLineColor(6); grPE->SetLineWidth(2); grPE->Draw("EP"); - // Update, otherwise GetPolargram returns 0 - CPol->Update(); + // To format the polar axis, we retrieve the TGraphPolargram. + // First update the canvas, otherwise GetPolargram returns 0 + CPol->Update(); if (grPE->GetPolargram()) { grPE->GetPolargram()->SetTextSize(0.03); grPE->GetPolargram()->SetTwoPi(); - grPE->GetPolargram()->SetToRadian(); + grPE->GetPolargram()->SetToRadian(); // tell ROOT that the x2 values are in radians } } diff --git a/tutorials/graphs/gr013_polar2.C b/tutorials/graphs/gr013_polar2.C new file mode 100644 index 0000000000000..86d943c55f82b --- /dev/null +++ b/tutorials/graphs/gr013_polar2.C @@ -0,0 +1,49 @@ +/// \file +/// \ingroup tutorial_graphs +/// \notebook +/// Create and draw a polar graph with errors and polar axis in radians (PI fractions). +/// See the [TGraphPolar documentation](https://root.cern/doc/master/classTGraphPolar.html) +/// +/// Since TGraphPolar is a TGraphErrors, it is painted with +/// [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) options. +/// +/// With GetPolargram we retrieve the polar axis to format it; see the +/// [TGraphPolargram documentation](https://root.cern/doc/master/classTGraphPolargram.html) +/// +/// \macro_image +/// \macro_code +/// \author Olivier Couet + +void gr013_polar2() +{ + TCanvas * CPol = new TCanvas("CPol","TGraphPolar Example",500,500); + + Double_t theta[8]; + Double_t radius[8]; + Double_t etheta[8]; + Double_t eradius[8]; + + for (int i=0; i<8; i++) { + theta[i] = (i+1)*(TMath::Pi()/4.); + radius[i] = (i+1)*0.05; + etheta[i] = TMath::Pi()/8.; + eradius[i] = 0.05; + } + + TGraphPolar * grP1 = new TGraphPolar(8, theta, radius, etheta, eradius); + grP1->SetTitle(""); + + grP1->SetMarkerStyle(20); + grP1->SetMarkerSize(2.); + grP1->SetMarkerColor(4); + grP1->SetLineColor(2); + grP1->SetLineWidth(3); + // Draw with polymarker and errors + grP1->Draw("PE"); + + // To format the polar axis, we retrieve the TGraphPolargram. + // First update the canvas, otherwise GetPolargram returns 0 + CPol->Update(); + if (grP1->GetPolargram()) + grP1->GetPolargram()->SetToRadian(); // tell ROOT that the theta values are in radians +} diff --git a/tutorials/graphs/graphpolar3.C b/tutorials/graphs/gr014_polar3.C similarity index 65% rename from tutorials/graphs/graphpolar3.C rename to tutorials/graphs/gr014_polar3.C index 5ecb45cf618a2..3a82319240fab 100644 --- a/tutorials/graphs/graphpolar3.C +++ b/tutorials/graphs/gr014_polar3.C @@ -1,14 +1,17 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Create and draw a polar graph with PI axis using a TF1. +/// Create a polar graph using a TF1 and draw it with PI axis. +/// See the [TGraphPolar documentation](https://root.cern/doc/master/classTGraphPolar.html) +/// +/// Since TGraphPolar is a TGraphErrors, it is painted with +/// [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) options. /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void graphpolar3() +void gr014_polar3() { TCanvas *CPol = new TCanvas("CPol","TGraphPolar Examples",500,500); diff --git a/tutorials/graphs/motorcycle.C b/tutorials/graphs/gr015_smooth.C similarity index 94% rename from tutorials/graphs/motorcycle.C rename to tutorials/graphs/gr015_smooth.C index 294c83d4a01c6..d4c7f2160f3ba 100644 --- a/tutorials/graphs/motorcycle.C +++ b/tutorials/graphs/gr015_smooth.C @@ -1,7 +1,7 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Macro to test scatterplot smoothers: ksmooth, lowess, supsmu +/// Show scatter plot smoothers: ksmooth, lowess, supsmu, /// as described in: /// /// Modern Applied Statistics with S-Plus, 3rd Edition @@ -9,11 +9,11 @@ /// Chapter 9: Smooth Regression, Figure 9.1 /// /// Example is a set of data on 133 observations of acceleration against time -/// for a simulated motorcycle accident, taken from Silverman (1985). +/// for a simulated motorcycle accident, taken from Silverman (1985). The data +/// are read from motorcycle.dat (2 columns of floats) /// /// \macro_image /// \macro_code -/// /// \author Christian Stratowa, Vienna, Austria #include "TString.h" @@ -40,7 +40,7 @@ void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt) grout->DrawClone("LPX"); } -void motorcycle() +void gr015_smooth() { // data taken from R library MASS: mcycle.txt TString dir = gROOT->GetTutorialDir(); diff --git a/tutorials/graphs/graphstruct.C b/tutorials/graphs/gr016_struct.C similarity index 98% rename from tutorials/graphs/graphstruct.C rename to tutorials/graphs/gr016_struct.C index 868d801364afb..e584e41cddd9f 100644 --- a/tutorials/graphs/graphstruct.C +++ b/tutorials/graphs/gr016_struct.C @@ -10,7 +10,7 @@ /// \note For this to work, ROOT has to be compiled with gviz ON /// \author Olivier Couet -TCanvas* graphstruct() +TCanvas* gr016_struct() { #if __has_include("TGraphStruct.h") // handy check on whether gviz was installed TGraphStruct *gs = new TGraphStruct(); diff --git a/tutorials/graphs/gtime.C b/tutorials/graphs/gr017_time.C similarity index 82% rename from tutorials/graphs/gtime.C rename to tutorials/graphs/gr017_time.C index 99eebf72fe99d..51a7ec47ad491 100644 --- a/tutorials/graphs/gtime.C +++ b/tutorials/graphs/gr017_time.C @@ -1,11 +1,10 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Example of TGraphTime. +/// Example of TGraphTime. See the [TGraphTime documentation](https://root.cern/doc/master/classTGraphTime.html) /// /// \macro_image /// \macro_code -/// /// \author Rene Brun #include "TCanvas.h" @@ -16,7 +15,7 @@ #include "TGraphTime.h" #include -void gtime(Int_t nsteps = 500, Int_t np = 100) +void gr017_time(Int_t nsteps = 500, Int_t np = 100) { if (np > 1000) np = 1000; std::vector color(np); @@ -59,16 +58,16 @@ void gtime(Int_t nsteps = 500, Int_t np = 100) g->Draw(); // save object as animated gif - // g->SaveAnimatedGif("gtime.gif"); + // g->SaveAnimatedGif("gr17_time.gif"); - //save object to a file - auto f = TFile::Open("gtime.root","recreate"); + // save object to a file + auto f = TFile::Open("gr17_time.root","recreate"); f->WriteObject(g, "g"); delete f; - //to view this object in another session do - // TFile::Open("gtime.root"); - // g->Draw(); + // to view this object in another session do + // TFile::Open("gr17_time.root"); + // g->Draw(); } diff --git a/tutorials/graphs/gtime2.C b/tutorials/graphs/gr018_time2.C similarity index 88% rename from tutorials/graphs/gtime2.C rename to tutorials/graphs/gr018_time2.C index 021a1fde6fc6c..517ee00263fa6 100644 --- a/tutorials/graphs/gtime2.C +++ b/tutorials/graphs/gr018_time2.C @@ -4,9 +4,10 @@ /// Example of TGraphTime showing how the class could be used to visualize /// a set of particles with their time stamp in a MonteCarlo program. /// +/// See the [TGraphTime documentation](https://root.cern/doc/master/classTGraphTime.html) +/// /// \macro_image /// \macro_code -/// /// \author Rene Brun #include "TRandom3.h" @@ -17,7 +18,7 @@ #include "TGraphTime.h" #include -void gtime2(Int_t nsteps = 200, Int_t np = 5000) +void gr018_time2(Int_t nsteps = 200, Int_t np = 5000) { if (np > 5000) np = 5000; std::vector color(np); @@ -52,8 +53,8 @@ void gtime2(Int_t nsteps = 200, Int_t np = 5000) g->Draw(); - // store object as animated gif - // g->SaveAnimatedGif("gtime2.gif"); + // save object as animated gif + // g->SaveAnimatedGif("gr18_time2.gif"); // start animation, can be stopped with g->Animate(kFALSE); // g->Animate(); diff --git a/tutorials/graphs/graphShade.C b/tutorials/graphs/gr101_shade_area.C similarity index 100% rename from tutorials/graphs/graphShade.C rename to tutorials/graphs/gr101_shade_area.C diff --git a/tutorials/graphs/graphreverse.C b/tutorials/graphs/gr102_reverse_axis.C similarity index 100% rename from tutorials/graphs/graphreverse.C rename to tutorials/graphs/gr102_reverse_axis.C diff --git a/tutorials/graphs/zones.C b/tutorials/graphs/gr103_zones.C similarity index 50% rename from tutorials/graphs/zones.C rename to tutorials/graphs/gr103_zones.C index b7f78e8ca83dd..d5bf51f3d9104 100644 --- a/tutorials/graphs/zones.C +++ b/tutorials/graphs/gr103_zones.C @@ -5,12 +5,26 @@ /// into adjacent subpads + axis labels on the top and right side /// of the pads. /// +/// See the [Divide documentation](https://root.cern/doc/master/classTPad.html#a2714ddd7ba72d5def84edc1fbaea8658) +/// +/// Note that the last 2 arguments in +/// `c1->Divide(2,2,0,0)` +/// define 0 space between the pads. With this, the axis labels where the pads +/// touch may be cut, as in this tutorial. To avoid this, either add some spacing +/// between pads (instead of 0) or change the limits of the plot in the pad (histos +/// in this tutorial). E.g. `h3` could be defined as +/// `TH2F *h3 = new TH2F("h3","test3",10,0,1,22,-1.1,1.1);` +/// but note that this can change the displayed axis labels (requiring SetNdivisions +/// to readjust). +/// +/// SetLabelOffset changes the (perpendicular) distance to the axis. The label +/// position along the axis cannot be changed +/// /// \macro_image /// \macro_code -/// /// \author Rene Brun -void zones() { +void gr103_zones() { TCanvas *c1 = new TCanvas("c1","multipads",900,700); gStyle->SetOptStat(0); c1->Divide(2,2,0,0); diff --git a/tutorials/graphs/zones.py b/tutorials/graphs/gr103_zones.py similarity index 50% rename from tutorials/graphs/zones.py rename to tutorials/graphs/gr103_zones.py index 14af5fb5a3fc3..71d79b9613c60 100644 --- a/tutorials/graphs/zones.py +++ b/tutorials/graphs/gr103_zones.py @@ -5,9 +5,22 @@ ## into adjacent subpads + axis labels on the top and right side ## of the pads. Original tutorial by Rene Brun. ## +## See the [Divide documentation](https://root.cern/doc/master/classTPad.html#a2714ddd7ba72d5def84edc1fbaea8658) +## +## Note that the last 2 arguments in +## c1->Divide(2,2,0,0) +## define 0 space between the pads. With this, the axis labels where the pads +## touch may be cut, as in this tutorial. To avoid this, either add some spacing +## between pads (instead of 0) or change the limits of the plot in the pad (histos +## in this tutorial). E.g. h3 could be defined as +## TH2F *h3 = new TH2F("h3","test3",10,0,1,22,-1.1,1.1); +## but note that this can change the displayed axis labels (requiring SetNdivisions +## to readjust). +## +## SetLabelOffset changes the (perpendicular) distance to the axis. The label +## position along the axis cannot be changed ## \macro_image ## \macro_code -## ## \author Alberto Ferro import ROOT diff --git a/tutorials/graphs/graphpalettecolor.C b/tutorials/graphs/gr104_palettecolor.C similarity index 90% rename from tutorials/graphs/graphpalettecolor.C rename to tutorials/graphs/gr104_palettecolor.C index 4694938585e5a..c52bd92832a1c 100644 --- a/tutorials/graphs/graphpalettecolor.C +++ b/tutorials/graphs/gr104_palettecolor.C @@ -12,13 +12,14 @@ /// and filled area. The graphs are drawn with curves (`C` option) and one can see /// the color of each graph is picked inside the palette `kSolar`. The /// same is visible on filled polygons in the automatically built legend. +/// To have more control on the legend, it can be created manually, see the graph +/// tutorial gr111_legend.C /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void graphpalettecolor () { +void gr104_palettecolor () { gStyle->SetOptTitle(kFALSE); gStyle->SetPalette(kSolar); @@ -42,6 +43,7 @@ void graphpalettecolor () { g4->SetLineWidth(3); g4->SetMarkerColor(kBlue); g5->SetLineWidth(3); g5->SetMarkerStyle(kFullSquare); + // The Draw option "A" (draw axes) is needed (only) for the first drawn graph g1->Draw("CA* PLC PFC"); g2->Draw("PC PLC PFC"); g3->Draw("PC PLC PFC"); diff --git a/tutorials/graphs/multigraphpalettecolor.C b/tutorials/graphs/gr105_multigraphpalettecolor.C similarity index 91% rename from tutorials/graphs/multigraphpalettecolor.C rename to tutorials/graphs/gr105_multigraphpalettecolor.C index 47406a37a9a16..d15791ae904bb 100644 --- a/tutorials/graphs/multigraphpalettecolor.C +++ b/tutorials/graphs/gr105_multigraphpalettecolor.C @@ -4,7 +4,7 @@ /// Palette coloring for multi-graphs is activated thanks to the options `PFC` /// (Palette Fill Color), `PLC` (Palette Line Color) and `AMC` (Palette Marker Color). /// When one of these options is given to `TMultiGraph::Draw` the `TGraph`s in the -/// `TMultiGraph`get their color from the current color palette defined by +/// `TMultiGraph` get their color from the current color palette defined by /// `gStyle->SetPalette(...)`. The color is determined according to the number of /// `TGraph`s. /// @@ -13,10 +13,9 @@ /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void multigraphpalettecolor() +void gr105_multigraphpalettecolor() { auto mg = new TMultiGraph(); diff --git a/tutorials/graphs/exclusiongraph.C b/tutorials/graphs/gr106_exclusiongraph.C similarity index 67% rename from tutorials/graphs/exclusiongraph.C rename to tutorials/graphs/gr106_exclusiongraph.C index 98722e25c6f7e..ae8ef9f929ef8 100644 --- a/tutorials/graphs/exclusiongraph.C +++ b/tutorials/graphs/gr106_exclusiongraph.C @@ -3,13 +3,17 @@ /// \notebook /// Draw three graphs with an exclusion zone. /// +/// The shaded areas are obtained with a fill for the graph and controlled with `SetLineWidth`. +/// `SetLineWidth` for exclusion graphs is explained in the [TGraphPainter documentation](https://root.cern/doc/master/classTGraphPainter.html#GrP2) +/// +/// As the graphs will be superposed on drawing, we add them to a [TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html) and then draw this one. +/// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -TCanvas *exclusiongraph() { - TCanvas *c1 = new TCanvas("c1","Exclusion graphs examples",200,10,600,400); +void gr106_exclusiongraph() { + TCanvas *c1 = new TCanvas("c1","Exclusion graph examples",200,10,600,400); c1->SetGrid(); TMultiGraph *mg = new TMultiGraph(); @@ -26,6 +30,7 @@ TCanvas *exclusiongraph() { yvalues3[i] = 10*sin(xvalues1[i])-2; } + // See explanation for SetLineWidth above TGraph *gr1 = new TGraph(n,xvalues1,yvalues1); gr1->SetLineColor(2); gr1->SetLineWidth(1504); @@ -47,6 +52,4 @@ TCanvas *exclusiongraph() { mg->Add(gr2); mg->Add(gr3); mg->Draw("AC"); - - return c1; } diff --git a/tutorials/graphs/exclusiongraph2.C b/tutorials/graphs/gr107_exclusiongraph2.C similarity index 81% rename from tutorials/graphs/exclusiongraph2.C rename to tutorials/graphs/gr107_exclusiongraph2.C index db9fa47f0104b..796b106cbd161 100644 --- a/tutorials/graphs/exclusiongraph2.C +++ b/tutorials/graphs/gr107_exclusiongraph2.C @@ -1,22 +1,27 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Draw several graphs with an exclusion zones. +/// Draw several graphs with exclusion zones. +/// The shaded areas are obtained with a fill for the graph and controlled with `SetLineWidth`. +/// `SetLineWidth` for exclusion graphs is explained in the [TGraphPainter documentation](https://root.cern/doc/master/classTGraphPainter.html#GrP2) +/// +/// The graphs are drawn superposed, with help of an empty histogram that defines the axes, so we must make sure that the axis limits are adequate for all graphs. Notice that only the first drawn graph needs the "A" (draw axes) option. Alternatively, one could add the graphs to a TMultiGraph, which automatically defines the appropriate limits; see the gr106_exclusiongraph.C tutorial. +/// +/// We add some text to the plot with [TLatex](https://root.cern/doc/master/classTLatex.html). Notice that the TLatex text is drawn on the current pad/canvas and does not need, nor have, a "same" option. /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void exclusiongraph2() +void gr107_exclusiongraph2() { - TCanvas *c = new TCanvas("c", - "Charged Higgs L300 Contour",0,0,700,700); + TCanvas *c = new TCanvas("c","Charged Higgs L300 Contour",0,0,700,700); c->SetTickx(); c->SetTicky(); c->SetGridx(); c->SetGridy(); + // Frame to define the axis limits (50 to 500 and 1 to 50) TH1 *frame = new TH1F("frame","",1000,50,500); frame->SetMinimum(1); frame->SetMaximum(50); diff --git a/tutorials/graphs/timeSeriesFromCSV.C b/tutorials/graphs/gr108_timeSeriesFromCSV.C similarity index 87% rename from tutorials/graphs/timeSeriesFromCSV.C rename to tutorials/graphs/gr108_timeSeriesFromCSV.C index ca81c6b1355d5..3d22c13bbfd59 100644 --- a/tutorials/graphs/timeSeriesFromCSV.C +++ b/tutorials/graphs/gr108_timeSeriesFromCSV.C @@ -7,10 +7,9 @@ /// /// \macro_image /// \macro_code -/// /// \authors Danilo Piparo, Olivier Couet -void timeSeriesFromCSV() +void gr108_timeSeriesFromCSV() { // Open the data file. This csv contains the usage statistics of a CERN IT // service, SWAN, during two weeks. We would like to plot this data with @@ -20,7 +19,8 @@ void timeSeriesFromCSV() dir.ReplaceAll("/./", "/"); FILE *f = fopen(Form("%sSWAN2017.dat", dir.Data()), "r"); - // Create the time graph + // Create the time graph. In this example, we don't specify anything about it, + // and data points will be added with SetPoint (the first point has index 0) auto g = new TGraph(); g->SetTitle("SWAN Users during July 2017;Time;Number of Sessions"); @@ -49,7 +49,7 @@ void timeSeriesFromCSV() g->Draw("al"); g->GetYaxis()->CenterTitle(); - // Make the X axis labelled with time. + // Make the X axis labelled with time auto xaxis = g->GetXaxis(); xaxis->SetTimeDisplay(1); xaxis->CenterTitle(); diff --git a/tutorials/graphs/timeSeriesFromCSV.py b/tutorials/graphs/gr108_timeSeriesFromCSV.py similarity index 90% rename from tutorials/graphs/timeSeriesFromCSV.py rename to tutorials/graphs/gr108_timeSeriesFromCSV.py index 4ae37ac5dbe7c..3663c3a282d7d 100644 --- a/tutorials/graphs/timeSeriesFromCSV.py +++ b/tutorials/graphs/gr108_timeSeriesFromCSV.py @@ -7,7 +7,6 @@ ## ## \macro_image ## \macro_code -## ## \authors Danilo Piparo, Olivier Couet import ROOT @@ -20,7 +19,8 @@ dirName= dirName.replace("/./", "/") inputFileName = "%s/SWAN2017.dat" %dirName -# Create the time graph +# Create the time graph. In this example, we don't specify anything about it, +# and data points will be added with SetPoint (the first point has index 0) g = ROOT.TGraph() g.SetTitle("SWAN Users during July 2017;Time;Number of Sessions") diff --git a/tutorials/graphs/timeSeriesFromCSV_TDF.C b/tutorials/graphs/gr109_timeSeriesFromCSV_TDF.C similarity index 81% rename from tutorials/graphs/timeSeriesFromCSV_TDF.C rename to tutorials/graphs/gr109_timeSeriesFromCSV_TDF.C index 8ff07147c5d51..442848f8c7948 100644 --- a/tutorials/graphs/timeSeriesFromCSV_TDF.C +++ b/tutorials/graphs/gr109_timeSeriesFromCSV_TDF.C @@ -4,11 +4,10 @@ /// This macro illustrates the use of the time axis on a TGraph /// with data read from a text file containing the SWAN usage /// statistics during July 2017. -/// We exploit the TDataFrame for reading from the file +/// We exploit the TDataFrame for reading from the file. See the [RDataFrame documentation](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) and [RDataFrame tutorials](https://root.cern/doc/master/group__tutorial__dataframe.html) /// /// \macro_image /// \macro_code -/// /// \authors Danilo Piparo, Olivier Couet void timeSeriesFromCSV_TDF() @@ -30,7 +29,8 @@ void timeSeriesFromCSV_TDF() auto timeStamps = d.Take("TimeStamp"); auto values = d.Take("Value"); - // Create the time graph + // Create the time graph. In this example, we provide to the TGraph constructor + // the number of (pairs of) points and all the x and y values auto g = new TGraph(values->size(), timeStamps->data(), values->data()); g->SetTitle("SWAN Users during July 2017;Time;Number of Sessions"); @@ -44,7 +44,7 @@ void timeSeriesFromCSV_TDF() g->Draw("al"); g->GetYaxis()->CenterTitle(); - // Make the X axis labelled with time. + // Make the X axis labelled with time auto xaxis = g->GetXaxis(); xaxis->SetTimeDisplay(1); xaxis->CenterTitle(); diff --git a/tutorials/graphs/waves.C b/tutorials/graphs/gr201_waves.C similarity index 72% rename from tutorials/graphs/waves.C rename to tutorials/graphs/gr201_waves.C index 26dfd095bcebc..ef56338ec0976 100644 --- a/tutorials/graphs/waves.C +++ b/tutorials/graphs/gr201_waves.C @@ -1,11 +1,11 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Hint: Spherical waves +/// Draw spherical waves interference. Two closed TGraphs filled with white are used +/// here to draw triangles on top of a 2D function in order to hide parts of it. /// /// \macro_image /// \macro_code -/// /// \author Otto Schaile #include "TROOT.h" @@ -51,18 +51,18 @@ Double_t result( Double_t *x, Double_t *par) //_____________________________________________________________ -void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) +void gr201_waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) { - TCanvas *c1 = new TCanvas("waves", "A double slit experiment", 300, 40, 1004, 759); + TCanvas *c1 = new TCanvas("gr201_waves", "A double slit experiment", 300, 40, 1004, 759); c1->Range(0, -10, 30, 10); c1->SetFillColor(0); - TPad *pad = new TPad("pr","pr", 0.5, 0 , 1., 1); - pad->Range(0, -10, 15, 10); + TPad *pad = new TPad("pr", "pr", 0.5, 0, 1., 1); + pad->Range(0, -10, 15, 10); pad->Draw(); const Int_t colNum = 30; Int_t palette[colNum]; - for (Int_t i=0;icd(); - TF2 *f0 = new TF2("ray_source",interference, 0.02, 15, -8, 8, 4); + // For the incoming waves, on the left side, we use a TF2 and increase the number + // of points used for drawing to 200 (default is 100) for better resolution + TF2 *f0 = new TF2("ray_source", interference, 0.02, 15, -8, 8, 4); f0->SetParameters(amp, lambda, 0, 0); f0->SetNpx(200); f0->SetNpy(200); @@ -81,8 +83,10 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) TLatex title; title.DrawLatex(1.6, 8.5, "A double slit experiment"); + // Draw 2 white triangles to cover parts of f0, simulating a cone + // (change the fill color to see these) TGraph *graph = new TGraph(4); - graph->SetFillColor(0); + graph->SetFillColor(1); graph->SetFillStyle(1001); graph->SetLineWidth(0); graph->SetPoint(0, 0., 0.1); @@ -92,7 +96,7 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) graph->Draw("F"); graph = new TGraph(4); - graph->SetFillColor(0); + graph->SetFillColor(1); graph->SetFillStyle(1001); graph->SetLineWidth(0); graph->SetPoint(0, 0, -0.1); @@ -101,20 +105,22 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) graph->SetPoint(3, 0, -0.1); graph->Draw("F"); - TLine * line = new TLine(15,-10, 15, 0 - 0.5*d -0.2); + // To represent the wall with 2 openings, we draw 3 black vertical lines + TLine * line = new TLine(15,-10, 15, 0 - 0.5*d - 0.2); line->SetLineWidth(10); line->Draw(); - line = new TLine(15, 0 - 0.5*d +0.2 ,15, 0 + 0.5*d -0.2); + line = new TLine(15, 0 - 0.5*d + 0.2, 15, 0 + 0.5*d - 0.2); line->SetLineWidth(10); line->Draw(); - line = new TLine(15,0 + 0.5*d + 0.2,15, 10); + line = new TLine(15, 0 + 0.5*d + 0.2, 15, 10); line->SetLineWidth(10); line->Draw(); - pad ->cd(); + pad->cd(); + // Interference plot, on the centre-right side TF2 *finter = new TF2("interference",interference, 0.01, 14, -10, 10, 4); finter->SetParameters(amp, lambda, d, 1); finter->SetNpx(200); @@ -122,6 +128,7 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) finter->SetContour(colNum-2); finter->Draw("samecol"); + // Some lines TArc arc; arc.SetFillStyle(0); arc.SetLineWidth(2); @@ -133,8 +140,9 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) r += dr; } - pad ->cd(); + pad->cd(); + // Result, on the right edge TF2 *fresult = new TF2("result",result, 14, 15, -10, 10, 4); fresult->SetParameters(amp, lambda, d, 1); fresult->SetNpx(300); @@ -142,7 +150,8 @@ void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10) fresult->SetContour(colNum-2); fresult->Draw("samecol"); - line = new TLine(13.8,-10, 14, 10); + // Vertical white line on the right side + line = new TLine(14,-10, 14, 10); line->SetLineWidth(10); line->SetLineColor(0); line->Draw(); c1->Modified(kTRUE); c1->Update(); diff --git a/tutorials/graphs/graphtext.C b/tutorials/graphs/gr202_textmarkers.C similarity index 97% rename from tutorials/graphs/graphtext.C rename to tutorials/graphs/gr202_textmarkers.C index f0067b15da053..84f6981d7af30 100644 --- a/tutorials/graphs/graphtext.C +++ b/tutorials/graphs/gr202_textmarkers.C @@ -8,10 +8,9 @@ /// /// \macro_image /// \macro_code -/// /// \author Olivier Couet -void graphtext() { +void gr202_textmarkers() { TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500); c->SetGrid(); @@ -27,7 +26,6 @@ void graphtext() { x = i*0.1; y = 10*sin(x+0.2); gr->SetPoint(i,x,y); - } gr->Draw("ALP"); } diff --git a/tutorials/graphs/hlGraph1.C b/tutorials/graphs/gr301_highlight1.C similarity index 89% rename from tutorials/graphs/hlGraph1.C rename to tutorials/graphs/gr301_highlight1.C index f24f9a81e024f..960aeff8a83c2 100644 --- a/tutorials/graphs/hlGraph1.C +++ b/tutorials/graphs/gr301_highlight1.C @@ -1,10 +1,9 @@ /// \file /// \ingroup tutorial_graphs /// -/// This tutorial demonstrates how to use the highlight mode on graph. +/// This tutorial demonstrates how to use the highlight mode on graph, thanks to the TCanvas [HighlightConnect](https://root.cern/doc/master/classTCanvas.html#a462b8dc286a2d29152fefa9b31f89920) method. /// /// \macro_code -/// /// \date March 2018 /// \author Jan Musinsky @@ -27,7 +26,7 @@ void HighlightHisto(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y) } } -void hlGraph1() +void gr301_highlight1() { auto Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 500); Canvas->HighlightConnect("HighlightHisto(TVirtualPad*,TObject*,Int_t,Int_t)"); diff --git a/tutorials/graphs/hlGraph2.C b/tutorials/graphs/gr302_highlight2.C similarity index 94% rename from tutorials/graphs/hlGraph2.C rename to tutorials/graphs/gr302_highlight2.C index 271b56a0cd62a..461110a5115a0 100644 --- a/tutorials/graphs/hlGraph2.C +++ b/tutorials/graphs/gr302_highlight2.C @@ -1,10 +1,9 @@ /// \file /// \ingroup tutorial_graphs /// -/// This tutorial demonstrates how to use the highlight mode on graph. +/// This tutorial demonstrates how to use the highlight mode on graph, thanks to the TCanvas [HighlightConnect](https://root.cern/doc/master/classTCanvas.html#a462b8dc286a2d29152fefa9b31f89920) method. /// /// \macro_code -/// /// \date March 2018 /// \author Jan Musinsky @@ -66,7 +65,7 @@ void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y) savepad->cd(); } -void hlGraph2() +void gr302_highlight2() { auto dir = gROOT->GetTutorialDir(); dir.Append("/hsimple.C"); diff --git a/tutorials/graphs/zdemo.C b/tutorials/graphs/gr303_zdemo.C similarity index 99% rename from tutorials/graphs/zdemo.C rename to tutorials/graphs/gr303_zdemo.C index 53764f7605559..66e0a08cc3bc4 100644 --- a/tutorials/graphs/zdemo.C +++ b/tutorials/graphs/gr303_zdemo.C @@ -15,7 +15,6 @@ /// /// \macro_image /// \macro_code -/// /// \authors Michael Tokarev, Elena Potrebenikova (JINR Dubna) #include "TCanvas.h" @@ -36,7 +35,7 @@ Float_t Z[NMAX], HZ[NMAX], PT[NMAX], INVSIG[NMAX]; void hz_calc(Float_t, Float_t, Float_t, Float_t, Float_t, Float_t); //__________________________________________________________________ -void zdemo() +void gr303_zdemo() { Float_t energ; diff --git a/tutorials/graphs/graphpolar2.C b/tutorials/graphs/graphpolar2.C deleted file mode 100644 index aa848c16721eb..0000000000000 --- a/tutorials/graphs/graphpolar2.C +++ /dev/null @@ -1,41 +0,0 @@ -/// \file -/// \ingroup tutorial_graphs -/// \notebook -/// Create and draw a polar graph with PI axis -/// -/// \macro_image -/// \macro_code -/// -/// \author Olivier Couet - -void graphpolar2() -{ - TCanvas * CPol = new TCanvas("CPol","TGraphPolar Example",500,500); - - Double_t theta[8]; - Double_t radius[8]; - Double_t etheta[8]; - Double_t eradius[8]; - - for (int i=0; i<8; i++) { - theta[i] = (i+1)*(TMath::Pi()/4.); - radius[i] = (i+1)*0.05; - etheta[i] = TMath::Pi()/8.; - eradius[i] = 0.05; - } - - TGraphPolar * grP1 = new TGraphPolar(8, theta, radius, etheta, eradius); - grP1->SetTitle(""); - - grP1->SetMarkerStyle(20); - grP1->SetMarkerSize(2.); - grP1->SetMarkerColor(4); - grP1->SetLineColor(2); - grP1->SetLineWidth(3); - grP1->Draw("PE"); - - CPol->Update(); - - if (grP1->GetPolargram()) - grP1->GetPolargram()->SetToRadian(); -} diff --git a/tutorials/graphs/index.md b/tutorials/graphs/index.md new file mode 100644 index 0000000000000..c82f81fe2d031 --- /dev/null +++ b/tutorials/graphs/index.md @@ -0,0 +1,111 @@ +\addtogroup tutorial_graphs + +@{ +A [TGraph](https://root.cern/doc/master/classTGraph.html) is an object made of two arrays X and Y with npoints each. ROOT can create TGraphs automatically by reading a text file (e.g. CSV), and other methods exist to create them from a function, from another TGraph, a histogram, vectors, arrays, or even by adding pairs of points one by one. + +In addition, ROOT offers multiple variations to basic TGraphs, such as: +- [TGraph2D](https://root.cern/doc/master/classTGraph2D.html) +- [TGraph2DAsymmErrors](https://root.cern/doc/master/classTGraph2DAsymmErrors.html) +- [TGraph2DErrrors](https://root.cern/doc/master/classTGraph2DErrors.html) +- [TGraphAsymmErrors](https://root.cern/doc/master/classTGraphAsymmErrors.html) +- [TGraphBentErrors](https://root.cern/doc/master/classTGraphBentErrors.html) +- [TGraphDelaunay](https://root.cern/doc/master/classTGraphDelaunay.html) +- [TGraphDelaunay2D](https://root.cern/doc/master/classTGraphDelaunay2D.html) +- [TGraphErrors](https://root.cern/doc/master/classTGraphErrors.html) +- [TGraphMultiErrors](https://root.cern/doc/master/classTGraphMultiErrors.html) +- [TGraphPolar](https://root.cern/doc/master/classTGraphPolar.html) +- [TGraphQQ](https://root.cern/doc/master/classTGraphQQ.html) +- [TGraphSmooth](https://root.cern/doc/master/classTGraphSmooth.html) +- [TGraphStruct](https://root.cern/doc/master/classTGraphStruct.html) +- [TGraphTime](https://root.cern/doc/master/classTGraphTime.html) + +TGraphs are painted through the [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) or [TGraph2DPainter](https://root.cern/doc/master/classTGraph2DPainter.html) class. As a general remark, TGraphs are not binned, each point is painted individually, and when the "A" draw option is used, ROOT draws a histogram, which can be retrieved with `graph->GetHistogram()` after drawing it, to display a frame with x and y axes. Note that the bins of this histogram do not correspond to the points of the graph (i.e., generally speaking bin 1 is not the same as the x value of point 1, and so on). + +To draw two or more TGraphs superposed, there are two basic approaches: +1. Draw the first one with the option "A" included (so that the axes are drawn), and successively draw the next one/s (without the "A" option). +2. Create a [TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html) and Add all graphs to it, then draw the `TMultiGraph`. + +In the second approach, `TMultiGraph` automatically sets an appropriate scale for the plot so that all graphs are shown. In the first approach, the scale is determined by the graph that was drawn with "A", which may or may not be large enough for all graphs. + +The graph tutorials below are divided in groups of increasing complexity, starting with examples showing how to create, fill, and draw the different types of graphs. + +## Tutorials sorted after groups +- [Basics: creation and drawing](\ref basics) +- [Formatting: changing/adding elements to the graphs and/or the plots](\ref modfying) +- [Intermediate: more advanced examples](\ref medium) +- [More tutorials](\ref other) + + +[List of all tutorials](\ref alltutorials) +\anchor basics +## Basics + +These examples showscase the creation of different types of graphs and basic ways to plot them. + +| **Tutorial** || **Description** | +|------|--------|-----------------| +| gr001_basic.C | | Create a simple graph from available data or from a file, and draw it. | +| gr002_err_1g.C | | Create and draw a graph with error bars.| +| gr003_err_2gr.C | | Create and draw two graphs with error bars, superposed on the same canvas (not using TMultiGraph). | +| gr004_err_asym.C | | Create and draw a graph with asymmetric x & y errors. | +| gr005_apply.C | | Demonstrate the functionality of the TGraph::Apply() method. | +| gr006_scatter.C | | Scatter plot for 4 variables, mapped to: x, y, marker colour and marker size. | +| gr007_multigraph.C | | Create and draw a TMultiGraph (several graphs superposed). | +| gr008_multierrors.C | | Graph with multiple y errors in each bin. | +| gr009_bent_err.C | gr009_bent_err.py | Graph with bent (non-vertical/non-horizontal) error bars. | +| gr010_approx_smooth.C | | Create a TGraphSmooth and show the usage of the interpolation function Approx. | +| gr011_2Derrorsfit.C | | Create, draw and fit a TGraph2DErrors. | +| gr012_polar.C | | Create and draw a polar graph. | +| gr013_polar2.C | | Polar graph with errors and polar axis in radians (PI fractions). | +| gr014_polar3.C | | Create a polar graph using a TF1 and draw it with PI axis. | +| gr015_smooth.C | | Show scatter plot smoothers: ksmooth, lowess, supsmu | +| gr016_struct.C | | Draw a simple graph structure. | +| gr017_time.C | | Example of TGraphTime. | +| gr018_time2.C | | TGraphTime to visualise a set of particles with their time stamp in a MonteCarlo program. | + + +\anchor modifying +## Formatting + +These examples show + +| **Tutorial** || **Description** | +|------|--------|-----------------| +| gr101_shade_area.C | | Shows how to shade an area between two graphs. | +| gr102_reverse_axis.C | | How to reverse the points of a graph along x. | +| gr103_zones.C | gr103_zones.py | How to divide a canvas into adjacent subpads, with axis labels on the top and right side of the pads. | +| gr104_palettecolor.C | | Automatically set graph colours from a palette. | +| gr105_multigraphpalettecolor.C | | Automatically set multi-graph colours from a palette. | +| gr106_exclusiongraph.C | | Draw three graphs (in a TMultiGraph) with exclusion zones. | +| gr107_exclusiongraph2.C | | Draw graphs (superposed, but no TMultiGraph) with exclusion zones. | +| gr108_timeSeriesFromCSV.C | gr108_timeSeriesFromCSV.py | Use of the time axis on a TGraph + with data read from a text file. | +| gr109_timeSeriesFromCSV_TDF.C | | Use of the time axis on a TGraph with data read from a text file, but using [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html). | + + +\anchor medium +## Intermediate + +These examples show + +| **Tutorial** || **Description** | +|------|--------|-----------------| +| gr201_waves.C | | Draw spherical waves interference, using closed and filled TGraphs to hide other plot elements. | +| gr202_textmarkers.C | | Draw a graph with text attached to each point. Uses a [TExec](https://root.cern/doc/master/classTExec.html) function to attach the text to the points. | +| | | | + + +\anchor other +## More tutorials + +These examples show + +| **Tutorial** || **Description** | +|------|--------|-----------------| +| gr301_highlight1.C | | How to use the interactive highlight mode on graph, thanks to the TCanvas [HighlightConnect](https://root.cern/doc/master/classTCanvas.html#a462b8dc286a2d29152fefa9b31f89920) method. | +| gr302_highlight2.C | | How to use the interactive highlight mode on graph, thanks to the TCanvas [HighlightConnect](https://root.cern/doc/master/classTCanvas.html#a462b8dc286a2d29152fefa9b31f89920) method. | +| gr303_zdemo.C | | Example of graphs in log scales with annotations and other advanced plot formatting. | + + +\anchor alltutorials +@} \ No newline at end of file diff --git a/tutorials/image/trans_graph.C b/tutorials/image/trans_graph.C index 1e1c45e6e7b0d..d60a7d494c07c 100644 --- a/tutorials/image/trans_graph.C +++ b/tutorials/image/trans_graph.C @@ -37,8 +37,8 @@ void trans_graph() // switch to batch mode gROOT->SetBatch(kTRUE); - // execute graph.C macro - gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C"); + // execute gr001_basic.C macro + gROOT->Macro("$ROOTSYS/tutorials/graphs/gr001_basic.C"); // create gVirtualPS object TImageDump dmp("dummy.png"); diff --git a/tutorials/legacy/benchmarks.C b/tutorials/legacy/benchmarks.C index acccf7ec24dd0..6c10e2f857918 100644 --- a/tutorials/legacy/benchmarks.C +++ b/tutorials/legacy/benchmarks.C @@ -44,11 +44,11 @@ void benchmarks() { summary->AddText(" hist/fillrandom.C"); summary->AddText(" fit/fit1.C"); summary->AddText(" hist/h1ReadAndDraw.C"); - summary->AddText(" graphs/graph.C"); - summary->AddText(" graphs/gerrors.C"); + summary->AddText(" graphs/gr001_basic.C"); + summary->AddText(" graphs/gr002_err_1gr.C"); summary->AddText(" graphics/tornado.C"); summary->AddText(" graphs/surfaces.C"); - summary->AddText(" graphs/zdemo.C"); + summary->AddText(" graphs/gr303_zdemo.C"); summary->AddText(" geom/geometry.C"); summary->AddText(" geom/na49view.C"); summary->AddText(" tree/ntuple1.C"); @@ -60,11 +60,11 @@ void benchmarks() { bexec(dir,"hist/fillrandom.C"); bexec(dir,"fit/fit1.C"); bexec(dir,"hist/h1ReadAndDraw.C"); - bexec(dir,"graphs/graph.C"); - bexec(dir,"graphs/gerrors.C"); + bexec(dir,"graphs/gr001_basic.C"); + bexec(dir,"graphs/gr002_err_1gr.C"); bexec(dir,"graphics/tornado.C"); bexec(dir,"graphs/surfaces.C"); - bexec(dir,"graphs/zdemo.C"); + bexec(dir,"graphs/gr303_zdemo.C"); bexec(dir,"geom/geometry.C"); bexec(dir,"geom/na49view.C"); bexec(dir,"tree/ntuple1.C"); From a88adc76a604f1f64c962f8fef1d5f6fa5d8d010 Mon Sep 17 00:00:00 2001 From: emachiamu Date: Wed, 27 Nov 2024 17:12:17 +0100 Subject: [PATCH 2/2] Rename, comment and modernise graph tutorials --- tutorials/graphs/data_basic.txt | 20 +++++ tutorials/graphs/gr001_basic.C | 80 +++++++++++++++----- tutorials/graphs/gr004_err_asym.C | 57 ++++---------- tutorials/graphs/gr005_apply.C | 51 ++++--------- tutorials/graphs/gr007_multigraph.C | 22 +++--- tutorials/graphs/gr008_multierrors.C | 36 +++++---- tutorials/graphs/gr101_shade_area.C | 25 +++--- tutorials/graphs/gr102_reverse_axis.C | 91 ++++------------------ tutorials/graphs/gr110_logscale.C | 43 +++++++++++ tutorials/graphs/gr111_legend.C | 42 +++++++++++ tutorials/graphs/gr112_err_reverse_axis.C | 92 +++++++++++++++++++++++ 11 files changed, 349 insertions(+), 210 deletions(-) create mode 100644 tutorials/graphs/data_basic.txt create mode 100644 tutorials/graphs/gr110_logscale.C create mode 100644 tutorials/graphs/gr111_legend.C create mode 100644 tutorials/graphs/gr112_err_reverse_axis.C diff --git a/tutorials/graphs/data_basic.txt b/tutorials/graphs/data_basic.txt new file mode 100644 index 0000000000000..210b67d98d104 --- /dev/null +++ b/tutorials/graphs/data_basic.txt @@ -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 \ No newline at end of file diff --git a/tutorials/graphs/gr001_basic.C b/tutorials/graphs/gr001_basic.C index f5b47b312bdc7..10d9acc72f833 100644 --- a/tutorials/graphs/gr001_basic.C +++ b/tutorials/graphs/gr001_basic.C @@ -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 //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;iSetLineColor(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"); } + diff --git a/tutorials/graphs/gr004_err_asym.C b/tutorials/graphs/gr004_err_asym.C index 3eb6cce7350c0..a02fa6caf257f 100644 --- a/tutorials/graphs/gr004_err_asym.C +++ b/tutorials/graphs/gr004_err_asym.C @@ -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 } diff --git a/tutorials/graphs/gr005_apply.C b/tutorials/graphs/gr005_apply.C index 3eb6cce7350c0..8c8b6509c255b 100644 --- a/tutorials/graphs/gr005_apply.C +++ b/tutorials/graphs/gr005_apply.C @@ -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). */ } diff --git a/tutorials/graphs/gr007_multigraph.C b/tutorials/graphs/gr007_multigraph.C index 3e7261fa76f90..a313817109f60 100644 --- a/tutorials/graphs/gr007_multigraph.C +++ b/tutorials/graphs/gr007_multigraph.C @@ -1,7 +1,9 @@ /// \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 @@ -9,16 +11,16 @@ /// \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}; @@ -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}; @@ -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"); diff --git a/tutorials/graphs/gr008_multierrors.C b/tutorials/graphs/gr008_multierrors.C index 82e6567d0fa68..1df6092ae01e2 100644 --- a/tutorials/graphs/gr008_multierrors.C +++ b/tutorials/graphs/gr008_multierrors.C @@ -1,14 +1,15 @@ /// \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); @@ -16,25 +17,28 @@ void gmultierrors() { 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(); } diff --git a/tutorials/graphs/gr101_shade_area.C b/tutorials/graphs/gr101_shade_area.C index b51df16b9dc8b..44353796b5e47 100644 --- a/tutorials/graphs/gr101_shade_area.C +++ b/tutorials/graphs/gr101_shade_area.C @@ -1,16 +1,15 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// Show how to shade an area between two graphs +/// Show how to shade an area between two graphs. /// /// \macro_image /// \macro_code /// /// \author Rene Brun -void graphShade() { - TCanvas *c1 = new TCanvas("c1", - "A Simple Graph Example",200,10,700,500); +void gr101_shade_area() { + TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500); c1->SetGrid(); c1->DrawFrame(0,0,2.2,12); @@ -20,21 +19,21 @@ void graphShade() { Int_t i; for (i=0;iSetPoint(i,x[i],ymax[i]); + TGraph *grmin = new TGraph(n,x,ymin); //Bottom graph + TGraph *grmax = new TGraph(n,x,ymax); //Upper graph + TGraph *gr = new TGraph(n,x,y); //Middle graph + TGraph *grshade = new TGraph(2*n); //Create a graph to represent the shaded region between the upper and lower graphs + for (i=0;iSetPoint(i,x[i],ymax[i]); grshade->SetPoint(n+i,x[n-i-1],ymin[n-i-1]); } grshade->SetFillStyle(3013); grshade->SetFillColor(16); - grshade->Draw("f"); + grshade->Draw("f"); //Draw the shaded area with "f" option (filled graph) grmin->Draw("l"); grmax->Draw("l"); gr->SetLineWidth(4); diff --git a/tutorials/graphs/gr102_reverse_axis.C b/tutorials/graphs/gr102_reverse_axis.C index db2a87570fd2d..ed45b20823cff 100644 --- a/tutorials/graphs/gr102_reverse_axis.C +++ b/tutorials/graphs/gr102_reverse_axis.C @@ -1,93 +1,34 @@ /// \file /// \ingroup tutorial_graphs /// \notebook -/// This example test all the various case of reverse graphs -/// combined with logarithmic scale. +/// This example tests the reverse graphs obtained with Draw("a pl rx ry ") on a TGraph, where rx and ry refere to the reversing of x and y axis. /// /// \macro_image /// \macro_code /// /// \author Olivier Couet -void graphreverse() { - auto c = new TCanvas("c","Reversed graphs",0,0,900,900); - c->Divide(3,3); +void gr102_reverse_axis() { + auto c = new TCanvas("c","Reversed graphs",0,0,900,400); + c->Divide(2,1); //Create a canvas with a 2x1 grid layout + + auto gr = new TGraph(); + gr->GetXaxis()->SetNdivisions(514); + gr->GetYaxis()->SetNdivisions(514); + gr->SetMarkerStyle(kCircle); + gr->SetMarkerColor(kBlue); + gr->SetLineColor(kRed); + gr->SetPoint(0,5,5); + gr->SetPoint(1,9,9); + gr->SetPoint(2,14,14); - // TGraphErrors - auto graphe = new TGraphErrors(); - graphe->GetXaxis()->SetNdivisions(514); - graphe->GetYaxis()->SetNdivisions(514); - graphe->SetMarkerStyle(kCircle); - graphe->SetPoint(0,5,5); - graphe->SetPointError(0,1,3); - graphe->SetPoint(1,9,9); - graphe->SetPointError(1,1,3); c->cd(1); gPad->SetGrid(); - graphe->Draw("a pl "); + gr->Draw("a pl "); //Plot with axis ("a") and line+points ("pl") c->cd(2); gPad->SetGrid(); - graphe->Draw("a pl rx ry "); - - c->cd(3); - gPad->SetGrid(); - gPad->SetLogx(); - gPad->SetLogy(); - graphe->GetXaxis()->SetMoreLogLabels(); - graphe->GetYaxis()->SetMoreLogLabels(); - graphe->Draw("a pl rx ry"); - - // TGraphAsymmErrors - auto graphae = new TGraphAsymmErrors(); - graphae->GetXaxis()->SetNdivisions(514); - graphae->GetYaxis()->SetNdivisions(514); - graphae->SetMarkerStyle(kCircle); - graphae->SetPoint(0,5,5); - graphae->SetPointError(0,1,3,3,1); - graphae->SetPoint(1,9,9); - graphae->SetPointError(1,1,3,1,3); - - c->cd(4); - gPad->SetGrid(); - graphae->Draw("a pl "); - - c->cd(5); - gPad->SetGrid(); - graphae->Draw("a pl rx ry "); + gr->Draw("a pl rx ry "); //Plot with axis ("a") and line+points ("pl") with reverse X and Y axes - c->cd(6); - gPad->SetGrid(); - gPad->SetLogx(); - gPad->SetLogy(); - graphae->GetXaxis()->SetMoreLogLabels(); - graphae->GetYaxis()->SetMoreLogLabels(); - graphae->Draw("a pl rx ry"); - - // TGraphBentErrors - auto graphbe = new TGraphBentErrors(); - graphbe->GetXaxis()->SetNdivisions(514); - graphbe->GetYaxis()->SetNdivisions(514); - graphbe->SetMarkerStyle(kCircle); - graphbe->SetPoint(0,5,5); - graphbe->SetPointError(0,1,3,3,1,.5,.2,.5,.2); - graphbe->SetPoint(1,9,9); - graphbe->SetPointError(1,1,3,1,3,-.5,-.2,-.5,-.2); - - c->cd(7); - gPad->SetGrid(); - graphbe->Draw("a pl "); - - c->cd(8); - gPad->SetGrid(); - graphbe->Draw("a pl rx ry "); - - c->cd(9); - gPad->SetGrid(); - gPad->SetLogx(); - gPad->SetLogy(); - graphbe->GetXaxis()->SetMoreLogLabels(); - graphbe->GetYaxis()->SetMoreLogLabels(); - graphbe->Draw("a pl rx ry"); } diff --git a/tutorials/graphs/gr110_logscale.C b/tutorials/graphs/gr110_logscale.C new file mode 100644 index 0000000000000..d33395fd16e4c --- /dev/null +++ b/tutorials/graphs/gr110_logscale.C @@ -0,0 +1,43 @@ +/// \file +/// \ingroup tutorial_graphs +/// \notebook +/// +/// This tutorial demonstrates how to set a logarithmic scale for the axes of a graph using the `SetLogScale()` method. +/// The logarithmic scale can be applied to either the x-axis, the y-axis, or both axes at the same time. +/// When using a logarithmic scale, the data must be positive since the logarithm is undefined for non-positive values and zero. +/// +/// \macro_image +/// \macro_code +/// \date 25/11/2024 +/// \author Emanuele Chiamulera + +void gr110_logscale { + + auto c = new TCanvas("c","Reversed graphs",0,0,900,400); + c->Divide(2,1); + c->cd(1); + + const Int_t n = 6; //Fill the arrays x and y with the data points + Double_t x[n], y[n]; + for (Int_t i=0;iSetLineColor(2); + gr1->SetLineWidth(4); + gr1->SetMarkerColor(4); + gr1->SetMarkerStyle(21); + gr1->SetTitle("Graph without log scale"); + gr1->DrawClone("ACP"); + + //The logarithmic scale setting can also be done in a ROOT interactive session. + c->cd(2); + gPad->SetLogy(); + //gPad->SetLogx(); Uncomment this line if log scale is needed on both axes + gr1->SetTitle("Graph with y log scale"); + gr1->Draw("ACP"); + +} \ No newline at end of file diff --git a/tutorials/graphs/gr111_legend.C b/tutorials/graphs/gr111_legend.C new file mode 100644 index 0000000000000..2c154d8b01656 --- /dev/null +++ b/tutorials/graphs/gr111_legend.C @@ -0,0 +1,42 @@ +/// \file +/// \ingroup tutorial_graphs +/// \notebook +/// +/// Simple graph with legend. For more details on TLegend [see documenation](https://root.cern/doc/master/classTLegend.html) +/// +/// +/// \macro_image +/// \macro_code +/// \date 25/11/2024 +/// \author Emanuele Chiamulera + +void gr111_legend() { + + auto c = new TCanvas("c","", 900, 800); + c->SetGrid(); + + const Int_t n = 5; + Double_t x[n] = {-3.00, -2.68, -2.36, -2.05, -1.73}; + Double_t y1[n] = {-0.98, -0.89, -0.71, -0.46, -0.16}; + Double_t y2[n] = {-2.98, -2.89, -2.71, -2.46, -2.16}; + + + TGraph *gr1 = new TGraph(n, x, y1); //Create the graph + gr1->SetLineColor(2); + gr1->SetLineWidth(4); + gr1->SetMarkerColor(4); + gr1->SetMarkerStyle(21); + gr1->SetTitle("Graph with legend"); + + TLegend *legend = new TLegend(0.1,0.7,0.38,0.9); //Create the TLegend object and define it's position + legend->SetHeader("Legend title", "C"); //"C" Center alignment for the header ("L" Left and "R" Right) + legend->SetFillColor(kWhite); + legend->SetBorderSize(1); + legend->SetTextSize(0.04); + legend->AddEntry(gr1, "Data points", "lp"); // "p" for point marker, "l" for line, "e" for error bars if TGraphError is used. + + gr1->Draw(""); + + // Draw the legend + legend->Draw(); +} diff --git a/tutorials/graphs/gr112_err_reverse_axis.C b/tutorials/graphs/gr112_err_reverse_axis.C new file mode 100644 index 0000000000000..a37fe4fb8788a --- /dev/null +++ b/tutorials/graphs/gr112_err_reverse_axis.C @@ -0,0 +1,92 @@ +/// \file +/// \ingroup tutorial_graphs +/// \notebook +/// This example tests all the various case of reverse graphs obtained with Draw("a pl rx ry "), where rx and ry refere to the reversing of x and y axis. +/// +/// \macro_image +/// \macro_code +/// +/// \author Olivier Couet + +void gr102_err_reverse_axis() { + auto c = new TCanvas("c","Reversed graphs",0,0,900,900); + c->Divide(3,3); + + //TGraphErrors (first row example) + auto graphe = new TGraphErrors(); + graphe->GetXaxis()->SetNdivisions(514); + graphe->GetYaxis()->SetNdivisions(514); + graphe->SetMarkerStyle(kCircle); + graphe->SetPoint(0,5,5); + graphe->SetPointError(0,1,3); + graphe->SetPoint(1,9,9); + graphe->SetPointError(1,1,3); + + c->cd(1); + gPad->SetGrid(); + graphe->Draw("a pl "); //Plot with axis ("a") and line+points ("pl") + + c->cd(2); + gPad->SetGrid(); + graphe->Draw("a pl rx ry "); //Plot with axis ("a") and line+points ("pl") with reverse X and Y axes + + c->cd(3); + gPad->SetGrid(); + gPad->SetLogx(); + gPad->SetLogy(); + graphe->GetXaxis()->SetMoreLogLabels(); + graphe->GetYaxis()->SetMoreLogLabels(); + graphe->Draw("a pl rx ry"); //Plot with reverse axis and log scale + + //TGraphAsymmErrors (second row example) + auto graphae = new TGraphAsymmErrors(); + graphae->GetXaxis()->SetNdivisions(514); + graphae->GetYaxis()->SetNdivisions(514); + graphae->SetMarkerStyle(kCircle); + graphae->SetPoint(0,5,5); + graphae->SetPointError(0,1,3,3,1); + graphae->SetPoint(1,9,9); + graphae->SetPointError(1,1,3,1,3); + + c->cd(4); + gPad->SetGrid(); + graphae->Draw("a pl "); + + c->cd(5); + gPad->SetGrid(); + graphae->Draw("a pl rx ry "); + + c->cd(6); + gPad->SetGrid(); + gPad->SetLogx(); + gPad->SetLogy(); + graphae->GetXaxis()->SetMoreLogLabels(); + graphae->GetYaxis()->SetMoreLogLabels(); + graphae->Draw("a pl rx ry"); + + //TGraphBentErrors (third row example) + auto graphbe = new TGraphBentErrors(); + graphbe->GetXaxis()->SetNdivisions(514); + graphbe->GetYaxis()->SetNdivisions(514); + graphbe->SetMarkerStyle(kCircle); + graphbe->SetPoint(0,5,5); + graphbe->SetPointError(0,1,3,3,1,.5,.2,.5,.2); + graphbe->SetPoint(1,9,9); + graphbe->SetPointError(1,1,3,1,3,-.5,-.2,-.5,-.2); + + c->cd(7); + gPad->SetGrid(); + graphbe->Draw("a pl "); + + c->cd(8); + gPad->SetGrid(); + graphbe->Draw("a pl rx ry "); + + c->cd(9); + gPad->SetGrid(); + gPad->SetLogx(); + gPad->SetLogy(); + graphbe->GetXaxis()->SetMoreLogLabels(); + graphbe->GetYaxis()->SetMoreLogLabels(); + graphbe->Draw("a pl rx ry"); +} \ No newline at end of file