how to use root -- some features...how to use root-- some features ann-cecilie larsen &...
TRANSCRIPT
How To Use ROOT-- some features
Ann-Cecilie Larsen & Alexander Bürger
15 Januar 2009
OCL group meeting
What is ROOT?
Object-Oriented framework for data analysis data acquisition ...
developed at CERN implemented in C++ C++ interpreter CINT
allows to run C++ code without compilation
Why C++?
helps with object oriented programming (OOP) simplifies maintenance of large projects means
encapsulation (data access control) inheritance (generalization hierarchy) polymorphism (different implementation in inheritance tree)
“based” on C could do OOP in C, BUT
no compiler support, more complicated syntax far more work, more typing, more errors
animal
mammal
dog
fish
...
very general: something“abstract”: does not actually exist
very specific,not “abstract”, can exist
arrow= “is a”
Classes 'in Nature'
class: describes set of properties and 'functions' color of fur, weight, bark(), eat()
object: instance of a class with a specific set of properties white fur, 40kg
Romeo
cow 'instance' withspecific property values
Classes in C++
a class is a set of object properties methods/functions
constructor&destructor variables
objects are instances of their class
// derived class definitionclass Histogram {public: void Fill(float value); int GetNbins(); virtual float GetBinContent(int bin);protected: float* GetBins() const { return bins; }private: int nbins; float* bins;};...// method definitionfloat Histogram::GetBinContent(int bin){ return bins[bin];}...// create an instanceHistogram* h1 = new Histogram(...);cout << h1>GetBinContent(5) << endl;delete h1;
encapsulation
TObject
TNamed
TH1
TVirtualPad
TCanvas
...
...
...
very general: something“abstract”: does not actually exist
something with a namestill “abstract”
histogram with bins, axes, ...not “abstract”, can exist
arrow=“derived from”
Inheritance
a derived class inherits all methods/functions from the base class may override some methods
e.g. TObject::Draw is overridden by TH1::Draw
Derived Classes
a derived class can override (replace)
methods/functions add variables and
methods access proteced and public variables
which version is called? “derived” if virtual “named” otherwise
// derived class definitionclass SafeHistogram : public Histogram {public: float GetBinContent(int bin); // virtual int GetNbins(); // not virtual};...// method definitionfloat SafeHistogram::GetBinContent(int bin){ if( bin>=0 && bin<GetNbins() ) return Histogram::GetBinContent(bin); else return 0;}int SafeHistogram::GetNbins(){ if( GetBins() ) return Histogram::GetNbins(); return 1;}...// create an instanceHistogram* h1 = new SafeHistogram(...);cout << h1>GetBinContent(5) << endl;delete h1;
polymorphism
inheritance
Syntactic Sugar -- “Typing help”
less typing, easier to read, fewer errors
examples array access stream
extraction/insertion name overloading
distinguish functions by signature (name + types)
implicit type conversion
a[i][j] // instead of *(*(a + i) + j)h1>GetNbins() // instead of (*h1).GetNbins()
#include <fstream>
ifstream input_file(“/tmp/plot.dat”);float x, y;input_file >> x >> y;
// definition: istream& istream::operator>>(float& x);
void do_something(int x); // integer argumentvoid do_something(double x); // double argumentvoid do_something(string s); // string
const char* text = “OCL”;do_something(text); // creates temporary // string objectfloat f = 15.1;do_something(f); // conversion may be tricky!
Templates and the STL templates
“generic classes” help avoid preprocessor
macros
STL – standard template library implements often-
needed things examples
sorted collections binary search
documentation e.g.http://www.litho.ucalgary.ca/opt/SPRO/stdlib/
// bad:#define MAX(a,b) ((a>b)?a:b)MAX(“here”,”there”); // compares pointersMAX(f(1),f(2)); // calls f three times
// bettertemplate<typename T>T& max(T& a, T& b){ if( a > b ) return a; else return b;}max(f(1),f(2)); // calls f two times // automatically figures Tmax<double>(10, 15.1); // int and double
template <class T>class vector {public: unsigned size() const { ... }};
#include <map>#include <vector>
std::map<int,std::string> mymap;cout << mymap[5] << endl; // operator[] overload// const T& map<...>::operator[](int) const;
std::vector<int> my_values;my_values.push_back(5);std::cout << my_values.size() << std::endl;
Where to Get Help with ROOT?
Homepage: http://root.cern.ch/ documentation tutorials user forum ...
ROOT User's Guide (PDF from homepage) from people using ROOT
MacOS finder: swshareroottutorials
Installing ROOT on MacOS
easiest with fink shows i if installed
otherwise install withfink install root5in a terminal
Script Example// CINT automatically uses “using namespace std”, some
“#include”s, ...{ gROOT>Reset(); // reset global variables gROOT>SetStyle("Plain"); // no colors/fill areas gStyle>SetOptTitle(0); // no histogram title gStyle>SetOptTitle(1); gStyle.SetOptStat(0); // don't show statistics gStyle.SetFillColor(0); // no fill in the canvas gStyle.SetPadBorderMode(0); // no pad borders
// deleting histogram from last macro run avoid memory leak
m = (TH2F*)gROOT>FindObject("h"); if(m) m>Delete();
// open input files ifstream rho44file("rho44.cnt.web"),
rho46file("rho46.cnt.web"); // create output files ofstream levdens44Ti("rho44Ti_EB"),
levdens46Ti("rho46Ti_EB");
// declare arrays float rho44[44],rhoerr44[44],energy44[44]; float rho46[87],rhoerr46[87],energy46[87]; float energyerr[430] = {0.};
// Read in from input, and write to output file levdens44Ti << "E[MeV]\trho[MeV1]rhoerr[MeV1]" << endl; int i=0; while(rho44file){ float x,y,z; int j; rho44file >> j >> energy44[i] >> rho44[i] >> rhoerr44[i]; levdens44Ti << energy44[i] << '\t' << rho44[i] << '\t' << rhoerr44[i] << endl; i++; }
levdens46Ti << "E[MeV]\trho[MeV1]\trhoerr[MeV1]" << endl; i=0; while(rho46file){ int j; rho46file >> j >> energy46[i] >> rho46[i] >> rhoerr46[i]; levdens46Ti << energy46[i] << '\t' << rho46[i] << '\t' << rhoerr46[i] << endl; i++; }
// close output files levdens44Ti.close(); levdens46Ti.close();
// create a canvas to plot upon TCanvas *c1 = new TCanvas("c1","Level densities,
Ti",600,400);
// create 2D histogram (used here only to get axes) TH2F *h = new TH2F("h","",10,0,12,50,0.3,56e3);
// create graphs TGraphErrors *rho44ti = new TGraphErrors(38,energy44,rho44,energyerr,rhoerr44); TGraphErrors *rho46ti = new TGraphErrors(75,energy46,rho46,energyerr,rhoerr46);
// start plotting c1.SetLogy(); // logscale on y axis c1.SetLeftMargin(0.14); h.GetXaxis().CenterTitle(); h.GetXaxis().SetTitle("Excitation energy E (MeV)"); h.GetYaxis().CenterTitle(); h.GetYaxis().SetTitleOffset(1.4); h.GetYaxis().SetTitle("Level density #rho (E) (MeV^{1})"); h.Draw(); // draw the histogram (here only to get the axes)
Script Example (2) rho44ti>SetMarkerStyle(20); rho44ti>SetMarkerSize(0.8); rho44ti>SetMarkerColor(kBlue); rho44ti>SetLineColor(kBlue); rho44ti>Draw("P"); // draw as points with line rho46ti>SetMarkerStyle(24); rho46ti>SetMarkerSize(0.8); rho46ti>Draw("P");
// create legend to visualize where the data points belong TLegend *leg = new TLegend(0.15,0.60,0.6,0.75); leg.SetBorderSize(0); leg>AddEntry(rho44ti,"Oslo data, ^{44}Ti (norm. E&B)","P"); leg>AddEntry(rho46ti,"Oslo data, ^{46}Ti (norm. E&B)","P"); leg>Draw();
// add some LaTeX text TLatex t; t.SetTextSize(0.05); t.DrawLatex(/*X=*/1.502, /*y=*/1.632e+04, "^{44}Ti and ^{46}Ti level density #rho");
c1>Update(); // actually draw something before “printing” it c1>Print("example.eps"); // “print” as .eps c1>Print("example.pdf"); // “print” as .pdf} // END of script