Click on this text to display the solution (but try yourself first!)
\n",
37 | " \n",
38 | "\n",
39 | "%jsroot on\n",
40 | "df = ROOT.RDataFrame(\"dataset\", \"../../data/example_file.root\")\n",
41 | "h = df.Filter(\"b <= 0.5\").Define(\"loga\", \"TMath::Log(a)\").Histo1D((\"h\",\"A beautiful histo\", 30, -5, 5), \"loga\")\n",
42 | "c = ROOT.TCanvas()\n",
43 | "h.Draw()\n",
44 | "c.Draw()\n",
45 | "
\n",
46 | " "
47 | ]
48 | }
49 | ],
50 | "metadata": {
51 | "kernelspec": {
52 | "display_name": "Python 3 (ipykernel)",
53 | "language": "python",
54 | "name": "python3"
55 | },
56 | "language_info": {
57 | "codemirror_mode": {
58 | "name": "ipython",
59 | "version": 3
60 | },
61 | "file_extension": ".py",
62 | "mimetype": "text/x-python",
63 | "name": "python",
64 | "nbconvert_exporter": "python",
65 | "pygments_lexer": "ipython3",
66 | "version": "3.9.6"
67 | }
68 | },
69 | "nbformat": 4,
70 | "nbformat_minor": 4
71 | }
72 |
--------------------------------------------------------------------------------
/course/exercises/core/rdataframe-collections.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "41ca9b9e",
6 | "metadata": {},
7 | "source": [
8 | "# Exercise: RDataFrame and collections"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "c5657ad2",
14 | "metadata": {},
15 | "source": [
16 | "The same dataset used in the previous exercise (`TTree` \"dataset\" in file `example_file.root`) contains two vector columns `vec1` and `vec2`.Click on this text to display the solution (but try yourself first!)
\n",
44 | " \n",
45 | "\n",
46 | "%jsroot on\n",
47 | "df = ROOT.RDataFrame(\"dataset\", \"../../data/example_file.root\")\n",
48 | "h = df.Define(\"v1_to_plot\", \"vec1[vec2*vec2 < 0.1]\").Histo1D(\"v1_to_plot\")\n",
49 | "c = ROOT.TCanvas()\n",
50 | "h.Draw()\n",
51 | "c.Draw()\n",
52 | "
\n",
53 | " "
54 | ]
55 | }
56 | ],
57 | "metadata": {
58 | "kernelspec": {
59 | "display_name": "Python 3 (ipykernel)",
60 | "language": "python",
61 | "name": "python3"
62 | },
63 | "language_info": {
64 | "codemirror_mode": {
65 | "name": "ipython",
66 | "version": 3
67 | },
68 | "file_extension": ".py",
69 | "mimetype": "text/x-python",
70 | "name": "python",
71 | "nbconvert_exporter": "python",
72 | "pygments_lexer": "ipython3",
73 | "version": "3.9.6"
74 | }
75 | },
76 | "nbformat": 4,
77 | "nbformat_minor": 5
78 | }
79 |
--------------------------------------------------------------------------------
/course/exercises/extra/00_C++_Interpreter/myMacro.C:
--------------------------------------------------------------------------------
1 |
2 | void myMacro ()
3 | {
4 | // WRITE HERE YOUR OWN CODE
5 | // like std::cout << "Result is: ..." << std::endl;
6 | }
7 |
--------------------------------------------------------------------------------
/course/exercises/extra/00_C++_Interpreter/readme.md:
--------------------------------------------------------------------------------
1 | # C++ Interpreter Learning Module Exercises
2 | Welcome to the exercises of the **C++ Interpreter** learning module!
3 |
4 | ## Start a ROOT session
5 | Carry out these simple tasks:
6 | - From the terminal, fire up ROOT
7 | - Verify it works as a calculator
8 | - List the files in /etc from within the ROOT prompt
9 | - Inspect the help
10 | - Quit
11 |
12 | ## Complete a simple ROOT macro
13 | Consider the file *myMacro.C*. It is a simple ROOT macro. In this exercise, you
14 | will fill in the code you interactively typed into the interpreter to verify the
15 | value of the geometric series.
16 | Run the macro with ROOT in three ways:
17 | - Directly invoking ROOT as interpreter for it
18 | - Executing it from within the prompt
19 | - Loading it from the prompt and then invoking the *myMacro* function
20 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/CreateAHistogram.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Create a Histogram\n",
8 | "Create a histogram, fill it with random numbers, set its colour to blue, draw it.\n",
9 | "Can you:\n",
10 | "- Can you use the native Python random number generator for this?\n",
11 | "- Can you make your plot interactive using JSROOT?\n",
12 | "- Can you document what you did in markdown?"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 1,
18 | "metadata": {},
19 | "outputs": [
20 | {
21 | "name": "stdout",
22 | "output_type": "stream",
23 | "text": [
24 | "Welcome to JupyROOT 6.10/06\n"
25 | ]
26 | }
27 | ],
28 | "source": [
29 | "import ROOT"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {
36 | "collapsed": true
37 | },
38 | "outputs": [],
39 | "source": [
40 | "# Your Code Here"
41 | ]
42 | }
43 | ],
44 | "metadata": {
45 | "kernelspec": {
46 | "display_name": "Python 2",
47 | "language": "python",
48 | "name": "python2"
49 | },
50 | "language_info": {
51 | "codemirror_mode": {
52 | "name": "ipython",
53 | "version": 2
54 | },
55 | "file_extension": ".py",
56 | "mimetype": "text/x-python",
57 | "name": "python",
58 | "nbconvert_exporter": "python",
59 | "pygments_lexer": "ipython2",
60 | "version": "2.7.13"
61 | }
62 | },
63 | "nbformat": 4,
64 | "nbformat_minor": 2
65 | }
66 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/graphDraw.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Interactively Draw a Graph"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import ROOT\n",
17 | "import numpy as np"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {
24 | "collapsed": true
25 | },
26 | "outputs": [],
27 | "source": [
28 | "c = ROOT.TCanvas()"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "The simple graph"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {},
42 | "outputs": [],
43 | "source": [
44 | "x = np.array([1,2,3,4,5], dtype=float)\n",
45 | "y = x*x\n",
46 | "g = ROOT.TGraph(n=x.size, x=x, y=y)\n",
47 | "g.Draw(\"APL\")\n",
48 | "c.Draw()"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "Change marker style, colour as well as line colour and thickness. Make the plot interactive. Re-draw the plot and interact with it!"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": null,
61 | "metadata": {},
62 | "outputs": [],
63 | "source": [
64 | "# Here your code"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "Now we set the title and the grid on the canvas."
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "# Here your code"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "We will now add the symbol of the Calcium isotope"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "# Here your code"
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "Redraw using a Y axis in log scale."
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "# Here your code"
113 | ]
114 | }
115 | ],
116 | "metadata": {
117 | "kernelspec": {
118 | "display_name": "Python 3",
119 | "language": "python",
120 | "name": "python3"
121 | },
122 | "language_info": {
123 | "codemirror_mode": {
124 | "name": "ipython",
125 | "version": 3
126 | },
127 | "file_extension": ".py",
128 | "mimetype": "text/x-python",
129 | "name": "python",
130 | "nbconvert_exporter": "python",
131 | "pygments_lexer": "ipython3",
132 | "version": "3.11.9"
133 | }
134 | },
135 | "nbformat": 4,
136 | "nbformat_minor": 2
137 | }
138 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/readme.md:
--------------------------------------------------------------------------------
1 | # Histograms, Graphs and Functions Exercises
2 |
3 | Welcome to the exercises of the **Histograms, Graphs and Functions** learning module!
4 |
5 | There are multiple ways of running ROOT - for example, you can use notebooks or you can create macros. Here you can find exercises and examples using both notebooks and macros.
6 |
7 | ## Notebooks
8 |
9 | For the beginning you can try with the notebooks:
10 |
11 | - [CreateAHistogram](CreateAHistogram.ipynb)
12 | - [graphDraw](graphDraw.ipynb)
13 |
14 | if you are stuck in these exercises, take a look at the provided solutions in the `solutions` folder. For the `CreateAHistogram.ipynb` exercise, the solution is provided both as a python notebook and the C++ notebook for you to see and compare the differences.
15 |
16 | ## Python macros
17 |
18 | Now instead of using notebooks, try writing your own python macros. These are the two different exercises, in case you're stuck the solutions are provided both as ROOT macros, but also as notebooks.
19 |
20 | ### Build a histogram
21 |
22 | In order to complete this exercise about ROOT histograms in PyROOT, please follow these steps:
23 | - Open the Python interpreter.
24 | - Import the ROOT module.
25 | - Create a histogram with 64 bins and an x-axis ranging from 0 to 16.
26 | - Fill it with random numbers distributed according to a linear function (*pol1*).
27 | - Make the line width a bit larger.
28 | - Draw it.
29 |
30 | You can find the solution [here](solutions/HistogramMacro.py).
31 |
32 | ## Build a graph
33 |
34 | In order to complete this exercise about ROOT graphs in PyROOT, please follow these steps:
35 | - Create a new Python module.
36 | - In the module, create a graph (*TGraph*).
37 | - Set its title to *My graph*, its X axis title to *myX* and Y axis title to *myY*.
38 | - Fill it with three points: (1,0), (2,3), (3,4).
39 | - Set a red full square marker.
40 | - Draw an orange line between points.
41 |
42 | You can find the solution [here](solutions/GraphMacro.py).
43 |
44 | ## Cpp macros
45 |
46 | The next step is writing your own cpp macros. You have 3 different exercises here, again, in case you're stuck, the solutions are provided both as ROOT macros, but also as notebooks.
47 |
48 | ### Build a histogram
49 |
50 | Create a macro which builds and draws a histogram with the following features:
51 | - The number of bins is 50 and the x axis ranges from 0 to 10.
52 | - It is filled with random numbers distributed according to an exponential distribution which has a rate = 0.5.
53 | Suggestion: see the [TRandom](https://root.cern.ch/doc/master/classTRandom.html) for generating random numbers or
54 | [TH1::FillRandom](https://root.cern.ch/doc/master/classTH1.html#random-numbers)
55 | - Its line width is thicker than the default one.
56 |
57 | You can find the solution as C++ macro [SimpleHistogram.C](solutions/SimpleHistogram.C) and [notebook](solutions/SimpleHistogram.ipynb).
58 |
59 | ### Build a graph
60 | Create a macro which builds and draws a graph with the following features:
61 | - The title of the plot is *My graph*.
62 | - The x and y axis have labels `my_{X}` and `my_{Y}` respectively.
63 | - It has three points with the following coordinates (1,0), (2,3), (3,4).
64 | - The marker is a full square. Its colour is red.
65 | - An orange line joins the points.
66 |
67 | You can find the solution as C++ macro [SimpleGraph.C](solutions/SimpleGraph.C) and as [notebook](solutions/SimpleGraph.ipynb).
68 |
69 | ### Build a function
70 | Create a macro that follows these steps:
71 | - Create a function with formula *cos(x)* and draw it.
72 | - Create another *cos(x)*, but scale the argument of the cosine by adding a parameter.
73 | - Set a value for the parameter.
74 | - Change the line color of the second function.
75 | - Draw the second function in the same canvas as the first one.
76 |
77 | You can find the solution as C++ macro [SimpleFunction.C](solutions/SimpleFunction.C) and as [notebook](solutions/SimpleFunction.ipynb).
78 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/solutions/GraphMacro.py:
--------------------------------------------------------------------------------
1 | import ROOT
2 | import numpy as np
3 |
4 | # Create a graph with three points: (1,0), (2,3), (3,4)
5 | x = np.array([1,2,3], dtype=float)
6 | y = np.array([0,3,4], dtype=float)
7 | g = ROOT.TGraph(n=x.size, x=x, y=y)
8 |
9 | # Set its title to My graph, its X axis title to myX and Y axis title to myY
10 | g.SetTitle("My graph;myX;myY")
11 |
12 | # Set a red full square marker
13 | g.SetMarkerStyle(ROOT.kFullSquare)
14 | g.SetMarkerColor(ROOT.kRed)
15 |
16 | # Draw an orange line between points
17 | g.SetLineColor(ROOT.kOrange)
18 |
19 | # Draw the graph
20 | # "A": draw the axis
21 | # "P": draw the points
22 | # "L": draw graph as a simple line
23 | g.Draw("APL")
24 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/solutions/HistogramMacro.py:
--------------------------------------------------------------------------------
1 | # Import the ROOT module
2 | import ROOT
3 |
4 | # Create a histogram with 64 bins and a axis ranging from 0 to 16
5 | h = ROOT.TH1F("myPyHisto", "Productivity;Python Knowledge;Productivity", 64, 0, 16)
6 |
7 | # Fill it with random numbers distributed according to a linear function ("pol1")
8 | h.FillRandom("pol1")
9 |
10 | # Change its line width with a thicker one
11 | h.SetLineWidth(4)
12 |
13 | # Draw it!
14 | h.Draw()
15 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/solutions/SimpleFunction.C:
--------------------------------------------------------------------------------
1 |
2 | void SimpleFunction()
3 | {
4 | // Create a function with formula "cos(x)" and draw it
5 | auto f1 = new TF1("f1", "cos(x)", 0., 10.);
6 | f1->Draw();
7 |
8 | // Create another function based on "cos(x)" with a parameter
9 | auto f2 = new TF1("f2", "cos(x/[0])", 0., 10.);
10 | f2->SetParameter(0, 2);
11 | f2->SetLineColor(kBlue);
12 | f2->Draw("Same");
13 | }
14 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/solutions/SimpleFunction.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Example of a Function \n",
8 | "\n",
9 | "- Create a function with formula *cos(x)* and draw it.\n",
10 | "- Create another *cos(x)*, but scale the argument of the cosine by adding a parameter.\n",
11 | "- Set a value for the parameter.\n",
12 | "- Change the line color of the second function.\n",
13 | "- Draw the second function in the same canvas as the first one."
14 | ]
15 | },
16 | {
17 | "cell_type": "markdown",
18 | "metadata": {},
19 | "source": [
20 | "We now create the TF1 class with formula *cos(x)*"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "auto f1 = new TF1(\"f1\", \"cos(x)\", 0., 10.);"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "We draw now the function."
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "metadata": {},
43 | "outputs": [],
44 | "source": [
45 | "f1->Draw();\n",
46 | "gPad->Draw();"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Create another function based on *cos(x)* with a parameter"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {},
60 | "outputs": [],
61 | "source": [
62 | "auto f2 = new TF1(\"f2\", \"cos(x/[0])\", 0., 10.);"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "f2->SetParameter(0, 2);\n",
72 | "f2->SetLineColor(kBlue);"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "We draw the second function in the same Pad as the first one"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": null,
85 | "metadata": {},
86 | "outputs": [],
87 | "source": [
88 | "f2->Draw(\"SAME\");\n",
89 | "gPad->Draw();"
90 | ]
91 | }
92 | ],
93 | "metadata": {
94 | "kernelspec": {
95 | "display_name": "ROOT C++",
96 | "language": "c++",
97 | "name": "root"
98 | },
99 | "language_info": {
100 | "codemirror_mode": "text/x-c++src",
101 | "file_extension": ".C",
102 | "mimetype": " text/x-c++src",
103 | "name": "c++"
104 | }
105 | },
106 | "nbformat": 4,
107 | "nbformat_minor": 2
108 | }
109 |
--------------------------------------------------------------------------------
/course/exercises/extra/01_Histograms_Graphs_Functions/solutions/SimpleGraph.C:
--------------------------------------------------------------------------------
1 | void SimpleGraph()
2 | {
3 | // Create the graph on the heap (with 'new') so that it survives the end of the macro
4 | auto g = new TGraph();
5 |
6 | // Set the points
7 | std::initializer_list