Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Doing calculations in
C++ and using ROOT.
Derivatives
Steven Kornreich
www.beachlook.com
ROOT: http://root.cern.ch/
Using ROOT
Setting up ROOT in the Rm 106
linux cluster
By default, root is not in your path.
To add, add these lines to your
.bashrc:
export ROOTSYS=/usr/local/root
export PATH=$PATH:$ROOTSYS/bin
2
ROOT commands
Starting root, just type “root”
At the root prompt:
.q = Exit from root
.ls = list the files loaded into root session
.! some-unix-command = execute some-unix-
command in the shell
Most c++ commands can also be
interpreted.
Executing a macro “myMacro.C”:
.x myMacro.C
3
ROOT Classes
Since it is C++, everything is represented by
classes:
Windows (or canvases) : TCanvas
A window where we can draw data, functions, etc.
Functions : TF1, TF2, TF3
Classes to manipulate mathematical functions, such
as sin(x), in order to draw, evaluate, and integrate
them.
Graphs : TGraph
Class used to plot data on a canvas
Histograms: TH1, TH2, TH3
Classes to manipulate histograms. Can draw them
on a canvas, integrate them, obtain means and
RMS values, evaluate bin contents.
4
Derivatives
df f ( x x) f ( x)
lim
dx x 0
x
f ( x x) f ( x)
D (f)
x
x
5
What is the order of the error?
Use Taylor expansion.
6
Example code: derivative operator.
#include <cmath> int main() {
#include <iostream> double del = 1.0e-1;
int choice;
cout << "Choose a function:" << endl;
using namespace std;
cout << "1) cube " << endl;
cout << "2) linear " << endl;
double cube (double aD) { cin >> choice;
return pow(aD,3); switch (choice) {
} case 1:
cout << derivOperator(cube,1.0,del) <<
double linear(double aD) { endl;
return pow(aD,1); break;
} case 2:
cout << derivOperator(linear,1.0,del)
<< endl;
double derivOperator(double f(double), break;
double aX, double aDel) {
default:
return ( f(aX+aDel) - f(aX) )/aDel; cout << "Incorrect input, exiting." <<
} endl;
}
return 0;
}
7
Output:
[mcalderon@born YevickChapter12]$
derivOperatorExample
Choose a function:
1) cube
2) linear
2
1
[mcalderon@born YevickChapter12]$
derivOperatorExample
Choose a function:
1) cube
2) linear
1
3.31
8
Error dependence
Note: the “approximation” is
actually exact for the linear case.
Errorestimation for linear case is
misleading.
When del is small, rounding errors
degrade the accuracy.
doublevariables can do ~14 sig. figs.
what happens if del=1.0e-17?
9
Graphical error analysis
For the finite difference case, we
know dependence through Taylor
series.
Other times, dependence must be
obtained empirically.
Method: determine the variation in the
result when changing the step size and
plot a graph.
10
Plotting with ROOT:
Split into two parts:
Make a program that writes the data into
a text file. Only use c++ standard
libraries.
Read the data from a ROOT session, and
create the necessary TGraph, TH1, etc.
11
Sample code, Part I:
int main() {
#include <cmath> double del = 1.0e-1;
ofstream ofs("outputFile.txt");
#include <iostream>
ofs << "X Y" << endl;
#include <fstream>
float x[10], y[10]; // arrays holding 10
floats
using namespace std; for (int i=0; i<10; ++i) {
y[i] = derivOperator(cube, 1.0, del); //
double cube (double aD) { Derivative for a given value of del
return pow(aD,3); x[i] = del;
} del /=2.0; // decrease the value of del
by factor 2
ofs << x[i] << '\t' << y[i] << endl;
double linear(double aD) { }
return pow(aD,1); ofs.close();
}
return 0;
double derivOperator(double f(double), }
double aX, double aDel) {
return ( f(aX+aDel) - f(aX) )/aDel;
}
12
Reading in ROOT, plotting Graph:
return;
}
13
Using TGraph, TF1 and TCanvas…
14
Graph Draw Options
15
For Homework: Derivatives
Chapter 12, Section 12.7
18
Plotting a user defined function in
ROOT
double mysine(double* x, double* par) {
double Amplitude = par[0];
double wavelength = par[1];
double phase = par[2];
return Amplitude*sin(2*TMath::Pi()/wavelength*x[0]+phase);
}
void plotsine() {
19
Result of plotting TGraph
20
Resources for ROOT
ROOT Web page:
http://root.cern.ch/
User guides
http://root.cern.ch/root/doc/RootDoc.html
21