TWiki
>
Main Web
>
WebPreferences
>
RootIRMMTutorial2013
>
RootIRMMTutorial2013StartingROOTExercises
(revision 5) (raw view)
Edit
Attach
PDF
---+ !!Start Using !ROOT %TOC% Welcome to the first hands-on session. We will focus on introductory exercises for getting started working with the !ROOT environment and writing our first !ROOT macro. Two levels of help will be provided: a hint and a full solution. Try not to jump to the solution even if you experience some frustration. The help is organised as follow: %TWISTY{mode="div" showlink="Hint " hidelink="Hide " }% Here the hint is shown. %ENDTWISTY% %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% Here the solution is shown. %ENDTWISTY% Some points linked to the exercises are optional and marked with a %ICON{hand}% icon: they are useful to scrutinise in more detail some aspects. Try to solve them if you have the time. Before starting the exercise 1, follow what is written in the lecture ---++ Start using the !ROOT prompt First of all open a Visual Studio Command window on your computer and type <verbatim> root </verbatim> If this does not work, then it means !ROOT is not properly installed in your system. Stop here and ask for help to fix the installation. If it works then start playing with the prompt. Use as a calculator and type: <verbatim> root [0] 2+2 </verbatim> or whatever you like (see for example the lecture slide) or page 5 of the booklet ("A !ROOT Guide for Beginners"). Note that after having issue a statement from the !ROOT prompt, you can omit the =;= required by C++. This will make !ROOT printing the returned value, if there is one. For example: <verbatim> root [0] TMath::Pi(); </verbatim> will not print anything, while <verbatim> root [0] TMath::Pi() (Double_t)3.14159265358979312e+00 </verbatim> Afterwards start writing your first !ROOT macro. A !ROOT macro is a file containing some C++ code which can be run from the !ROOT prompt. You need to define a function and in the function scope you write the code. For example, you create a file, which you will call =mymacro.C=. Inside the file you define a function =mymacro(int value)= and you write the code of slide 8 of the lecture (or something else, if you prefer). The you can run the macro from the !ROOT prompt by typing: <verbatim> root [0] .x mymacro.C(42) </verbatim> Note that if the function name is different than the macro (file) name, you need two steps to run. First you load the macro: <verbatim> root [0] .L mymacro.C </verbatim> Then you run the function in the macro. Let's assume the function name is =test(int value)=: <verbatim> root [1] test(42) </verbatim> After having followed this introduction, you can try to solve the first exercise. ---++ Exercise 1: Computing PI The aim of this exercise is to write a !ROOT macro which can compute the number PI. You can deduce the number PI from the area of a circle with unitary radius. You can compute the area of the first quadrant of the circle by generating random points (x,y) , which are uniform distributed in the interval [0,1]. The function to generate uniform random number in the interval [0,1] is <verbatim> root [0] gRandom->Rndm() </verbatim> If you throw for example one million random points, which value of PI do you obtain and how is different from the real value of PI, which you can get in ROOT by using =TMath::Pi()=. %TWISTY{mode="div" showlink="Hint " hidelink="Hide " }% To solve the exercise you would need: * make a loop on the n number of points you want to generate * inside the loop you throw two random numbers, uniformly distributed between [0,1], that we can call =x= and =y= * we compute the radius of the point (x,y) : r = x^2 + y^2 * if r <= 1 we accept the point and we increase a counter * if r > 1 we continue * the ratio of the number of accepted points to n, will then be equal to the area of the first quadrant, which is PI/4 This we can do in C++ by making a =for= loop and we call the function to generate the random x and y numbers: <verbatim> for (int i = 0; i < n; ++i) { double x = gRandom->Rndm(); double y = gRandom->Rndm(); // count the number of accepted points (x^2 + y^2 <= 1) } </verbatim> %ENDTWISTY% After having written and run the macro, try to compile it using ACLIC. For compiling the macro you need to add to include the required header files, thus you need to add at the beginning of the code: <verbatim> #include "TRandom.h" // for using gRandom #include "TMath.h" // for using TMath </verbatim> Then you can compile with ACLIC, by adding a =+= at the end <verbatim> root[0] .x computePI.C+ </verbatim> You can note the difference in time by using ACLIC, by generating for example more points (e.g. 10E8). %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% %CODE{"cpp"}% #include "TRandom.h" #include "TStopwatch.h" #include "TMath.h" #include <iostream> void computePI(int n = 1000000) { int nin = 0; TStopwatch w; // for measuring the time for (int i = 0; i < n; ++i) { double x = gRandom->Rndm(); double y = gRandom->Rndm(); if ( (x*x + y*y) < 1.) nin++; } double pi4 = nin/double(n); std::cout << "Computed PI is " << pi4*4 << std::endl; std::cout << "Real PI is " << TMath::Pi() << " difference = " << TMath::Pi()-pi4*4 << std::endl; w.Print(); } %ENDCODE% %ENDTWISTY% %ICON{hand}% Can you measure the time of generating 10E8 points using CINT or ACliC. How much faster is ACliC ? You can use the ROOT class =TStopwatch= for measuring the elapsed time. Add at the begining of the macro (before the loop) <verbatim> TStopwatch w; </verbatim> and after the end of the macro <verbatim> w.Print(); </verbatim> ---++ Exercise 2: Plotting a Function in !ROOT Following slide 13 of the lecture or page 6 of the Introductory Guide, create a TF1 class using the =sin(x)/x= function and draw it. Create then a function with parameters, p0 * sin ( p1 * x) /x and also draw it for different parameter values. You can try to change the parameter values using =TF1::SetParameters= or by using the !ROOT GUI. Try also to change the style of the line and its color. You can either use the !ROOT GUI and/or use the methods of the class TAttLine (see TAttLine [[http://root.cern.ch/root/html/TAttLine.html][reference documentation]]). Since TF1 derives from TAttLine it inherits all its functions. Try for example to set the colour of the parametric function to blue. After having drawn the function, compute for the parameter values (p0=1, p1=2): * function value for x = 1. * function derivative for x = 1 * integral of the function between 0 and 3. %TWISTY{mode="div" showlink="Hint " hidelink="Hide " }% To solve the exercise you need to : * create a TF1 object using a formula expression. In the case of a parametric functions, the two parameters are defined as [0] and [1] * call TF1::Draw() You can also find the available function of the class TF1, by using the Tab key on the !ROOT prompt, for example <verbatim> root [0] TF1 * f1 = .... root [1] f1-><TAB> </verbatim> And you can get the full signature of a method by doing for example: <verbatim> root [1] f1->Derivative(<TAB> </verbatim> %ENDTWISTY% %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% %CODE{"cpp"}% #include "TF1.h" void plotFunction() { TF1 * f1 = new TF1("f1","sin(x)/x",0,10); f1->Draw(); TF1 * fp = new TF1("fp","[0]*sin([1]*x)/x",0,10); fp->SetParameters(1,2); fp->Draw("same"); fp->SetLineColor(kBlue); // to change axis y margins // (or invert the order of plotting the functions) f1->SetMaximum(2); f1->SetMinimum(-2); std::cout << "Value of f(x) at x = 1 is " << fp->Eval(1.) << std::endl; std::cout << "Derivative of f(x) at x = 1 is " << fp->Derivative(1.) << std::endl; std::cout << "Integral of f(x) in [0,3] is " << fp->Integral(0,4) << std::endl; } %ENDCODE% %ENDTWISTY% %ICON{hand}% You can try to use a different function, for example the Gamma distribution, defined in =ROOT::Math::gamma_pdf=. Try to make a plot as the one in Wikipedia, see [[http://en.wikipedia.org/wiki/File:Gamma_distribution_pdf.svg][here]], where the function is plot for different parameter values. See the [[http://project-mathlibs.web.cern.ch/project-mathlibs/sw/html/group__PdfFunc.html][here]] the reference documentation of the gamma distribution in !ROOT. %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% %CODE{"cpp"}% #include "TF1.h" #include "Math/DistFunc.h" void plotGamma() { // Note that parameter [0] is called alpha in definition of gamma_pdf or kappa in Wikipedia // Note that parameter [1] is theta // use range [0,20] as in Wikipedia plot TF1 * f1 = new TF1("f","ROOT::Math::gamma_pdf(x,[0],[1])",0,20); f1->SetLineColor(kRed); f1->SetParameter(0,1); f1->SetParameter(1,2); // use DrawClone because we will plot many different copies of same object but with different // parameter values f1->DrawClone(); // now change parameters and draw at different parameter values f1->SetLineColor(kGreen); f1->SetParameter(0,2); f1->DrawClone("SAME"); f1->SetLineColor(kBlue); f1->SetParameter(0,3); f1->DrawClone("SAME"); f1->SetLineColor(kCyan); f1->SetParameter(0,5); f1->SetParameter(1,1); f1->DrawClone("SAME"); f1->SetLineColor(kOrange); f1->SetParameter(0,9); f1->SetParameter(1,0.5); f1->DrawClone("SAME"); } %ENDCODE% %ENDTWISTY% ---++ Exercise 3: Plotting Measurement Points in !ROOT We will learn in this exercise how to plot a set of points in !ROOT using the TGraph class. Suppose you have this set of points: * x = 1; y = 0.4 * x = 2; y = 0.3 * x = 3; y = 0.5 * x = 4; y = 0.7 * x = 5; y = 1.3 Plot these points using the TGraph class. Use as a marker point a black box. Looking at the possible options for drawing the TGraph in [[http://root.cern.ch/root/html/TGraphPainter.html][TGraphPainter]], plot a line connecting the points. %TWISTY{mode="div" showlink="Hint " hidelink="Hide " }% To solve the exercise you need to : * create an array for the X points: =double x[] = {1,2...= * create an array for the y points * create the TGraph from the X and Y arrays Alternatively you can create first an empty TGraph object and set the point one by one by calling =TGraph::SetPoint= %ENDTWISTY% %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% %CODE{"cpp"}% void plotGraph() { double x[] = {1.,2.,3.,4.,5.}; double y[] = {0.4,0.3,0.5.,0.7,1.3}; TGraph * g = new TGraph(5,x,y); g->Draw("AP"); g->SetMarkerStyle(21); } %ENDCODE% %ENDTWISTY% %ICON{hand}% Make a TGraphError by adding an error of 0.3 in X and 0.2 in Y for every point. ---++ Exercise 4: Making an histogram in !ROOT We will learn in this exercise how to create a one-dimensional histogram in !ROOT, how to fill it with data and how to plot it. Create a one-dimensional histogram with 50 bins between 0 10 and fill it with 10000 gaussian distributed random numbers with mean 5 and sigma 2. Plot the histogram and, looking at the documentation in the [[http://root.cern.ch/root/html/THistPainter.html/#HP07][THistPainter]], show in the statistic box the number of entries, the mean, the RMS, the integral of the histogram, the number of underflows, the number of overflows, the skewness and the kurtosis. %TWISTY{mode="div" showlink="Hint " hidelink="Hide " }% For generating gaussian random numbers use =gRandom->Gaus(mean, sigma)= %ENDTWISTY% %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% %CODE{"cpp"}% #include "TH1.h" #include "TRandom.h" #include "TStyle.h" void plotHistogram() { TH1D * h1 = new TH1D("h1","h1",50,0,10); for (int i = 0; i < 10000; ++i) { double x = gRandom->Gaus(5,2); h1->Fill(x); } h1->Draw(); gStyle->SetOptStat(111111110); } %ENDCODE% %ENDTWISTY% %ICON{hand}% After calling the function =TH1::ResetStats()= , you will see that the statistics (mean, RMS,..) of the histogram is slightly different. Try to understand the reason for this. %TWISTY{mode="div" showlink="Solution " hidelink="Hide " }% The initial statistics is computed using the original (un-binned) data, while after calling ResetStats(), the statistics is computed using the bin contents and centres (binned data). %TWISTY%
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r8
<
r7
<
r6
<
r5
<
r4
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r5 - 2013-02-24
-
LorenzoMoneta
Log In
Main
Home
Index
Search
User Search
Changes
Notifications
RSS Feed
Documentation
Support
Webs
Main
Main Archive
Plugins
Sandbox for tests
Public webs
Public webs
ABATBEA
ACPP
ADCgroup
AEGIS
AfricaMap
AgileInfrastructure
ALICE
AliceEbyE
AliceSPD
AliceSSD
AliceTOF
AliFemto
ALPHA
ArdaGrid
ASACUSA
AthenaFCalTBAna
Atlas
AtlasLBNL
AXIALPET
CAE
CALICE
CDS
CENF
CERNSearch
CLIC
Cloud
CloudServices
CMS
Controls
CTA
CvmFS
DB
DefaultWeb
DESgroup
DPHEP
DM-LHC
DSSGroup
EGEE
EgeePtf
ELFms
EMI
ETICS
FIOgroup
FlukaTeam
Frontier
Gaudi
GeneratorServices
GuidesInfo
HardwareLabs
HCC
HEPIX
ILCBDSColl
ILCTPC
IMWG
Inspire
IPv6
IT
ItCommTeam
ITCoord
ITdeptTechForum
ITDRP
ITGT
ITSDC
LAr
LCG
LCGAAWorkbook
Leade
LHCAccess
LHCAtHome
LHCb
LHCgas
LHCONE
LHCOPN
LinuxSupport
Main
Medipix
Messaging
MPGD
NA49
NA61
NA62
NTOF
Openlab
PDBService
Persistency
PESgroup
Plugins
PSAccess
PSBUpgrade
R2Eproject
RCTF
RD42
RFCond12
RFLowLevel
ROXIE
Sandbox
SocialActivities
SPI
SRMDev
SSM
Student
SuperComputing
Support
SwfCatalogue
TMVA
TOTEM
TWiki
UNOSAT
Virtualization
VOBox
WITCH
XTCA
Welcome Guest
Login
or
Register
Cern Search
TWiki Search
Google Search
Main
All webs
Copyright &© 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use
Discourse
or
Send feedback