1.04k likes | 1.07k Views
ROOT. An Object Oriented Data Analysis Framework Day 2. Day 2. Yesterday GUI, Command line Today Command Line (CINT) Scripts (CINT & ACLiC) Exercises Functions and Fitting Trees and Tree Analysis. Command Line. Environment Settings Command types CINT Commands Global Variables
E N D
ROOT An Object Oriented Data Analysis Framework Day 2 ROOT Day 2
Day 2 • Yesterday • GUI, Command line • Today • Command Line (CINT) • Scripts (CINT & ACLiC) • Exercises • Functions and Fitting • Trees and Tree Analysis ROOT Day 2
Command Line • Environment Settings • Command types • CINT Commands • Global Variables • TObject ROOT Day 2
Environment Settings • Environment setting file: • $ROOTSYS/etc/system.rootrc • looks first in current directory for .rootrc • second in $HOME/.rootrc • third in $ROOTSYS/etc/system.rootrc • Show the current settings • root[] gEnv->Print() ROOT Day 2
Environment Settings (cont.) The .rootrc file: • The Macro Path • Unix.*.Root.MacroPath:.:$(HOME)/myRootMacros • Options in .rootrc • Root.ShowPath: false History File • $HOME/.root_hist Automatically Executing Macros • rootlogon.C • rootlogoff.C • rootalias.C ROOT Day 2
Command Line Options • > root -? • Usage: root [-l] [-b] [-n] [-q] [file1.C ... fileN.C] • Options: • -b : run in batch mode without graphics • -n : do not execute logon and logoff macros as specified in .rootrc • -q : exit after processing command line macro files • -l : do not show splash screen ROOT Day 2
Three Types of Commands • CINT commands start with “.” • root[0].? • this command will list all the CINT commands • root[1].X <filename.C> • load <filename.C> and execute function <filename> • root[2].L [filename.C] • load [filename.C] • SHELL commands start with “.!” for example:root[3] .! ls ROOT Day 2
Three Types of Commands 3. C++ syntax (almost) root [0] TBrowser *b = new TBrowser()or root [0] TBrowser *b = new TBrowser(); The optional Semicolon: Leave off the semicolon to see the return value of the command. root [0] 23+5 // show return value (int)28 root [1] 23+5; // no return value root [2] ROOT Day 2
Command Line Help • Use the Tab feature to get help … root [0] b = new TB <TAB> root [1] b = new TBrow<TAB> root [2] b = new TBrowser(<TAB> • Find List of Methods • Find Parameter list ROOT Day 2
Coding Conventions Based on the Taligent coding conventions • Classes begin withT TTree, TBrowser • Non-class types end with_t Int_t • Data members begin with f fTree • Member functions begin with a capital Loop() • Constants begin with k kInitialSize, kRed • Non-local variablesbegin with g gEnv • Static data members begin with fg fgTokenClient ROOT Day 2
CINT Types ROOT Day 2
CINT Extensions to C++ • 1. Declaration can be omitted • f = new TFile("Example.root") • 2. "." notation rather than "->"f.ls() • 3. Search for an object by its name • TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); small->Draw(); Warning: These will not work in compiled code! ROOT Day 2
CINT Commands • [expression] evaluates the expression • root[3] 3*4 • (int)12 • .files show loaded source files • .class [name] show class definition • .g prints all objects in the root session • .ls ls on current directory • .pwd list the current directory, canvas, and style. ROOT Day 2
Demo on CINT Commands .class root [0] .L $ROOTSYS/test/libEvent.so root [1] .class Event .g root [2] .g... 0x104c7560 Event e , size=56 0x0 private: Int_t fNtrack 0x0 private: Int_t fNseg 0x0 private: Int_t fNvertex ... ROOT Day 2
CINT Multi-line Command Start with "{“, for example: root [] { end with '}'> Int_t j = 0; end with '}'> for (Int_t i = 0; i < 3; i++) end with '}'> { end with '}'> j= j + i; end with '}'> cout <<"i = " <<i<<", j = " <<j<<endl; end with '}'> } end with '}'> } i = 0, j = 0 i = 1, j = 1 i = 2, j = 3 ROOT Day 2
Global Variables • gRandom • gRandom->Gaus(1,2) • You can replace the random generator with your own:delete gRandom;gRandom = new TRandom2(0); //seed=0 • gFile • gFile->GetName() • gDirectory • gDirectory->GetName() • gSystem • gSystem->HostName() ROOT Day 2
gROOT • Global ROOT session object • gROOT->GetListOf< list type >(); • gROOT->Macro(); • gROOT->LoadMacro(); • gROOT->Time(); • gROOT->ProcessLine() ROOT Day 2
gROOT->FindObject() TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); • "small" is the Object Name gROOT->FindObject("small") (class TObject*)0x104c7528 • "smallHisto" is the Variable Name gROOT->FindObject("smallHisto") (class TObject*)0x0// null pointer • FindObject needs the Object Name. ROOT Day 2
gROOT->FindObject() (cont.) FindObject returns a pointer to TObject. This generates an error: gROOT->FindObject("small")->GetBinContent(2) This is OK: gROOT->FindObject("small")->ClassName() TH1F* histo=(TH1F*) gROOT->FindObject("small") histo->GetBinContent(2) or ((TH1F*)gROOT->FindObject("small")) ->GetBinContent(2) ROOT Day 2
gROOT->FindObject() cont. Due to CINT magic this is also OK: TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); small->GetBinContent(2); • CINT implicitly executes a FindObject("small") • Casts it to the correct class • Creates a variable called "small" of the correct class Warning: This will not work in compiled code! ROOT Day 2
Demonstration: FindObject FindObject(): root [3] f = new TFile("Example.root") root [4] .ls root [5] gROOT->FindObject("myTree") root [6] myTree ROOT Day 2
TObject: The Mother of all ROOT Objects • Defines protocol and default behavior for all objects in ROOT. • I/O • Drawing/Painting • TObjects can be stored in collection classes. • Introspection, Reflection, Run Time Type Identification ROOT Day 2
TObjectRTTI • RTTI = the ability of a class to reflect upon itself or to "look inside itself" at run time. • TClass implements RTTI • To get the TClass from a TObject descendent:obj->IsA() • TClass can find the: • Methods • Method arguments • Data members • Base classes ROOT Day 2
Summary (Command Line) • Environment Settings • Command types • CINT Commands • Global Variables • TObject ROOT Day 2
Writing Scripts • Named and Un-named Scripts • Debugging • ACLiC ROOT Day 2
Scripts • Un-named Script • Starts with "{" and ends with "}" • All variables are in the global scope • No class definitions • No function declarations • No parameters • Named Script • C++ functions • Scope rules follow standard C++ • Function with the same name as the file is executed with a .x Parameters Class definitions (derived from a compiled class at your own risk) ROOT Day 2
Scripts Examples • Un-named Script: hello.C { cout << "Hello" << endl; } • Named Script: say.C void say(char * what = "Hello") { cout << what << endl; } • Executing the Named Script root [3] .x say.C Hello root [4] .x say.C("Hi there") Hi there ROOT Day 2
Resetting the Environment • gROOT->Reset() • Calls destructors of all objects created on the stack • Objects on Heap are not deleted, but pointer variable is disassociated ROOT Day 2
Debugging: Stepping .s set the step mode to step into function .S set the step mode to go over function or loop .e continue to end of the function .c continue to next breakpoint .c 45 continue to line 45 .p <var> print the value of var ROOT Day 2
Debugging: Breakpoints .trace MyClass prints the executing code to window .deltrace MyClass removes the trace .break MyClass breaks at each method of MyClass .delbreak MyClass removes the break .b 34 sets a break point at line 34 .db 34 removes the break point at line 34 ROOT Day 2
Debugging: Inspecting DrawClass() Graphic list of methods including ancestors Inspect() Draw the current contents of an object Dump() Lists the current contents of an object gDebug = 1 Prints debugging information ROOT Day 2
Tracking Memory Leaks • Counting Objects and Memory use • In the .rootrc or system.rootrc file: Root.MemStat: 1 Root.ObjectStat:1 • Print the Object count and Memory use gObjectTable->Print(); ROOT Day 2
Tracking Memory Leaks • Example output: Before .x FirstContour.C: count on heap size total size heap size Total: 1,079 1,046 3,160 49,99245,824 After: Total: 1,783 1,749 17,920 118,912 114,568 • Put gObjectTable->Print() before and after code segment in your script to find memory leaks. ROOT Day 2
ACLiC: Automatic Compiler of Libraries for CINT • Use an external compiler to create a shared library from a macro. • Use root [0] .L MyMacro.C++ Always recompile root [0] .L MyMacro.C+ Recompile as needed ROOT Day 2
ACLiC Use CompileMacro options are: k : keep the shared library after the session ends f : force recompilation To set the include path: .include "-I$HOME/mypackage/include"; Or gSystem->SetIncludePath (" -I$HOME/mypackage/include ") ROOT Day 2
ACLiC Advantages • Advantages : • syntax checking • about five times faster • full C++ feature set • Disadvantage: • for short scripts compilation overhead can be larger than script execution time ROOT Day 2
ACLiC Demo • .L ScriptCompilerDemo.C++ root [0] gROOT->Time() root [1] .L ACLiCDemo.C++ root [2] .files root [3] Demo() • Compare performance with CINT root [0] gROOT->Time() root [1] .L ACLiCDemo.C root [3] Demo() ROOT Day 2
Summary (Scripts) • Named and Un-named Scripts • Debugging • ACLiC ROOT Day 2
Functions and Fitting • Function Objects (TF1) • Three constructors for TF1 • User Defined Functions • Fitting • Fit() • Fitting with a user defined function • Fitting subranges and combining functions • Demonstration of background and signal function ROOT Day 2
Function Objects (TF1) • Built in function objects • see this link for a full list of built in functions http://root.cern.ch/root/html/TFormula.html#TFormula:TFormula • use the Fit Panel • Creating your own function objects • TF1, TF2, TF3 • Three Signatures for the TF1 constructor ROOT Day 2
TF1 Constructors 1. A C++ like expression using x with a fixed set of operators and functions defined in TFormula TF1 *f1 = new TF1("f1","sin(x)/x",0,10); f1->Draw(); TF1 *f2 = new TF1("f2","f1 * 2",0,10); f2->Draw(“same”) ROOT Day 2
TF1 Constructors (cont.) • 2. Same as the previous TF1 with Parameters • Call the constructor with parameter indices • TF1 *f1 = new TF1 • ("f1","[0]*x*sin( [1]*x)",-3,3);See TFormula for valid expressions • Set the parameters explicitly • f1->SetParameter(0,10);f1->SetParameter(1,5);f1->Draw(); ROOT Day 2
TF1 Constructors (cont.) • 3. Use a defined function • Define a functionDouble_t MyFunction(Double_t *x, Double_t *par){ Float_t xx = x[0]; Double_t val = TMath::Abs(par[0]*sin(par[1]*xx)/xx); return val;} • TF1 constructor TF1 *f1 = new TF1("f1",MyFunction,0,10,2); • NOTE: The 2 is the number of parameters in MyFunction. • Set the parametersf1->SetParameters(0,1); ROOT Day 2
par[2] : Constante par[0] : A par[3] : X0 par[1] : B par[4] : s par[0] : P0 par[4] : Amplitude sinus par[5] : Constante par[8] : Phase sinus par[1] : P1 par[6] : X0 par[2] : P2 par[7] : s par[3] : P3 Combination of Functions • You can mix predefined functions TF1 *f4=new TF1("f4","gaus(2)+expo(0)",0,10,5) • Another example TF1 *f5=new TF1("f5","pol3(0)+[4]*sin(gaus(5)+[8])",0,10,9) ROOT Day 2
Fitting To fit a histogram: TF1 *fn1 = new TF1 • ("f1","[0]*x*sin([1]*x)",-3,3); • f1->SetParameters(10,5);aHistogram->Fit("f1"); //name • aHistogram->Fit(fn1); // pointer ROOT Day 2
Fitting: Example Step 1. Define the function: Double_t MyFunction (Double_t *x, Double_t *par) { Double_t arg= 0; if (par[2]) arg = (x[0] - par[1])/par[2]; Double_t fitval = par[0] * TMath::Exp(-0.5*arg*arg); return fitval; } ROOT Day 2
Fitting (cont.) Step 2. TF1 constructorTF1 *aFunction = new TF1("MyGaus", MyFunction, -5,5,3); Step 3. Set initial value of the parametersaFunction->SetParameters(5000, h->GetMean(), h->GetRMS()); Step 4. Fit and draw the histogramh->Fit("MyGaus"); ROOT Day 2
Fitting Sub-Ranges Example $ROOTSYS/tutorials/multifit.C • Define the range in the TF1 constructor. TF1 *g1 = new TF1("g1", "gaus", 85,95); • By default, TH1::Fit on the defined function range. Use "R" option in the Fit() method.h->Fit("g1", "R"); ROOT Day 2
Fitting Sub-Ranges ROOT Day 2
Fitting Sub-Ranges Define gaussian functions g1 = new TF1("m1","gaus",85,95); g2 = new TF1("m2","gaus",98,108); g3 = new TF1("m3","gaus",110,121); total = new TF1("mstotal","gaus(0)+gaus(3)+gaus(6)",85,125); From TFormula: gaus(0) is a substitute for : [0]*exp(-0.5*((x-[1])/[2])**2) and (0) means start numbering parameters at 0 ROOT Day 2