ROOT - Download as PowerPoint by Ib7358QG

VIEWS: 16 PAGES: 68

									                              ROOT
          An object oriented HEP analysis
                    framework.
                       Day 2
                 http://www-pat.fnal.gov/root/

              The ROOT system website is at: http://root.cern.ch/



November 11                   ROOT Day 2, Suzanne Panacek           1
 Day 2
        Last time
            GUI, Command line
        Today
          Command Line (CINT)
          Scripts (CINT & ACLiC)

          Exercises

          Functions and Fitting

          TreeViewer



Nov-11               ROOT Day 2, Suzanne Panacek   v.7.0, slide# 2
Command line
    Environment Settings
    Command types
    CINT Commands
    Global Variables
    TObject




    November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 3
         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

   Find the current settings
        root[] gEnv->Print()



        November 11         ROOT Day 2, Suzanne Panacek   v.7.0, slide# 4
  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

 November 11           ROOT Day 2, Suzanne Panacek   v.7.0, slide# 5
    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




   November 11            ROOT Day 2, Suzanne Panacek        v.7.0, slide# 6
 Three Types of Commands
  1.     CINT commands start with “.”
              root[0].?
                this command will list all the CINT commands
              root[1].X [filename]
                load [filename] and execute function
                [filename]
              root[2].L [filename]
                load [filename]

  2.     SHELL commands start with “.!” for
         example:
              root[3] .! ls

November 11           ROOT Day 2, Suzanne Panacek   v.7.0, slide# 7
  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]




 November 11            ROOT Day 2, Suzanne Panacek          v.7.0, slide# 8
 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


November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 9
       Coding Conventions

Based on Taligent
   Classes          begin with T         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
   Static variables begin with g         gEnv
   Static data members
                     begin with fg        fgTokenClient

      November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 10
         CINT Types
                                                      Size
C++ type              Size (bytes) ROOT types                  FORTRAN analog
                                                     (bytes)

(unsigned)char            1         (U)Char_t           1      CHARACTER*1

(unsigned)short
                          2         (U)Short_t          2      INTEGER*2
(int)

(unsigned)int           2 or 4      (U)Int_t            4      INTEGER*4

(unsigned)long
                        4 or 8      (U)Long_t           8      INTEGER*8
(int)

float                     4         Float_t             4      REAL*4

double                 8 (=4)       Double_t            8      REAL*8

                        16 (=
long double                                                    REAL*16
                       double)




        November 11              ROOT Day 2, Suzanne Panacek          v.7.0, slide# 11
 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!



November 11         ROOT Day 2, Suzanne Panacek   v.7.0, slide# 12
     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.

    November 11       ROOT Day 2, Suzanne Panacek   v.7.0, slide# 13
 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
                ...


November 11           ROOT Day 2, Suzanne Panacek   v.7.0, slide# 14
    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


   November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 15
    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()



November 11             ROOT Day 2, Suzanne Panacek          v.7.0, slide# 16
gROOT
   global ROOT session object

   gROOT->GetListOf< list type >();

   gROOT->LoadMacro();

   gROOT->Time();

   gROOT->ProcessLine()



     November 11     ROOT Day 2, Suzanne Panacek   v.7.0, slide# 17
    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.
     November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 18
gROOT->FindObject() ++
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)


   November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 19
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!


  November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 20
  Demonstration: FindObject

FindObject():
   root        [3]   f = TFile("Example.root")
   root        [4]   .ls
   root        [5]   gROOT->FindObject("myTree")
   root        [6]   myTree




 November 11            ROOT Day 2, Suzanne Panacek   v.7.0, slide# 21
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, Runt Time Type

       Identification


    November 11      ROOT Day 2, Suzanne Panacek   v.7.0, slide# 22
 TObject RTTI
     RTTI = the ability of a class to reflect upon
      itself or to "look inside itself" at run time.
     TClass implement RTTI
     To get the TClass from a TObject descendent:
         obj->Class()
     TClass can find the:
          Methods
          Data members
          Base classes


November 11         ROOT Day 2, Suzanne Panacek   v.7.0, slide# 23
 Summary (Command Line)
     Environment Settings
     Command types
     CINT Commands
     Global Variables
     TObject




November 11     ROOT Day 2, Suzanne Panacek   v.7.0, slide# 24
Writing Scripts
    Named and Un-named Scripts
    Debugging
    ACLiC




    November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 25
 Scripts
Un-named Script
   Start with "{" and end 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)



November 11            ROOT Day 2, Suzanne Panacek     v.7.0, slide# 26
     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

    November 11       ROOT Day 2, Suzanne Panacek   v.7.0, slide# 27
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




     November 11          ROOT Day 2, Suzanne Panacek   v.7.0, slide# 28
     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

 November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 29
  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

 November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 30
 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

November 11     ROOT Day 2, Suzanne Panacek   v.7.0, slide# 31
 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();



November 11          ROOT Day 2, Suzanne Panacek   v.7.0, slide# 32
 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,992       45,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.

November 11          ROOT Day 2, Suzanne Panacek            v.7.0, slide# 33
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


November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 34
 ACLiC Use
  Restriction: can not use path name with .L
  .L ../root_base/MyMacro.C+
  Instead do:
  gSystem->cd("../directory");
  gSystem->CompileMacro("MyMacro.C")
  Options are:
      k : keep the shared library after the session
      end.
      f : force recompilation.
  To set the Include path:
       .include "-I$HOME/mypackage/include";
November 11      ROOT Day 2, Suzanne Panacek   v.7.0, slide# 35
     ACLiC Advantages

        Advantages :
           syntax checking
           about five times faster
           full C++ feature set


        Disadvantage:
          On KCC, you can load each C++ shared
           library only once


    November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 36
 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()


November 11         ROOT Day 2, Suzanne Panacek   v.7.0, slide# 37
 Summary (Scripts)
     Named and Un-named Scripts
     Debugging
     ACLiC




November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 38
Getting started with the Exercises
   Go to:
    http://patwww.fnal.gov/root/class/Setup.htm for
    setup instructions using Reflection and ssh
    on fcdfsgi2 and d0mino and minos1.

   Find the exercises on line at:
http://patwww.fnal.gov/root/class/exercises.htm



     November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 39
 Exercise Overview
     Session A
          Use ROOT command line
          Write a named and un-named script
          Use the GUI to create objects and change their attributes
          Save your canvas to a PostScript file
          Fit a graph
     Session B
          Fit and rotate a histogram
          Use the Object Browser and the Tree Viewer
          Make a profile and contour graphs
          Build an event list from a cut
          Fill a histograms with random numbers
          Use ACLiC


November 11               ROOT Day 2, Suzanne Panacek          v.7.0, slide# 40
 Exercise Session C
     Session C:
          Study an example analysis from DESY
          Learn about the TTree::MakeSelector method to
           automatically create a class that loops over each entry in a
           tree.
          Save and retrieve a canvas to and from a ROOT file
          Compute the integral of a histogram
          Compute the integral of a function within a range




November 11             ROOT Day 2, Suzanne Panacek        v.7.0, slide# 41
 Solutions to the Exercises
  http://patwww.fnal.gov/root/class/solutions.htm




November 11       ROOT Day 2, Suzanne Panacek   v.7.0, slide# 42
 ROOT contacts at Fermi
       Philippe Canal , x2545
          pcanal@fnal.gov

       Suzanne Panacek, x8334
         spanacek@fnal.gov

       Jeff Kallenbach, x2210
           jeffk@fnal.gov


November 11       ROOT Day 2, Suzanne Panacek   v.7.0, slide# 43
     Mailing Lists
 the ROOT mailing list:
roottalk@root.cern.ch
 archives:

http://root.cern.ch/root/roottalk/AboutRootTalk.html
 Fermilab mailing list:

about-root@fnal.gov
 archives:

http://listserv.fnal.gov/archives/about-root.html


    November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 44
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


November 11          ROOT Day 2, Suzanne Panacek   v.7.0, slide# 45
        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

     November 11         ROOT Day 2, Suzanne Panacek    v.7.0, slide# 46
    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);


   November 11      ROOT Day 2, Suzanne Panacek   v.7.0, slide# 47
     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();


    November 11       ROOT Day 2, Suzanne Panacek   v.7.0, slide# 48
TF1 Constructors (cont.)
3. Use a defined function
  Define a function
  Double_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 parameters
  f1->SetParameters(2,1);

    November 11           ROOT Day 2, Suzanne Panacek      v.7.0, slide# 49
 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




   November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 50
 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;
}



   November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 51
    Fitting (cont.)

Step 2. TF1 constructor
 TF1 *aFunction =
          new TF1("MyGaus", MyFunction, -5,5,3);

Step 3. Set initial value of the parameters
 aFunction->SetParameters(5000,
          h->GetMean(),
          h->GetRMS());

Step 4. Fit and draw the histogram
 h->Fit("MyGaus");



   November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 52
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 histogram
    range. Use "R" option in the Fit() method.
    h->Fit("g1", "R");




    November 11         ROOT Day 2, Suzanne Panacek   v.7.0, slide# 53
 Fitting Subranges




November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 54
     Fitting Subranges++
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

    November 11        ROOT Day 2, Suzanne Panacek   v.7.0, slide# 55
 Fitting Sub-Ranges++
  Fit each range and get the parameter for "total"
         h->Fit(g1,"R");
         h->Fit(g2,"R+");
         h->Fit(g3,"R+");
         g1->GetParameters(&par[0]);
         g2->GetParameters(&par[3]);
         g3->GetParameters(&par[6]);
         total->SetParameters(par);
         h->Fit(total,"R+")



November 11      ROOT Day 2, Suzanne Panacek   v.7.0, slide# 56
      Signal and Background Demo
y(E) = a1 + a2E + a3E2 + AP ( / 2 )/( (E-)2 + (/2)2)
       background                       lorenzianPeak
       par[0] = a1                      par[0] = AP
       par[1] = a2                      par[1] = 
       par[2] = a3                      par[2] = 

fitFunction = background (x, par ) + lorenzianPeak (x, &par[3])
       par[0] = a1
       par[1] = a2
       par[2] = a3
       par[3] = Ap
       par[4] = 
       par[5] = 
    November 11          ROOT Day 2, Suzanne Panacek    v.7.0, slide# 57
        Fitting Demo
Look at
 FittingDemo.C
       Unnamed Macro
   fitf.C
       Named Macro
Run FittingDemo.C
More info on fitting:
http://root.cern.ch/root/html/examples/fit1.C.html
http://root.cern.ch/root/html/examples/myfit.C.html
http://root.cern.ch/root/html/examples/backsig.C.html
     November 11          ROOT Day 2, Suzanne Panacek   v.7.0, slide# 58
Summary: Fitting and Functions
          Functions Objects (TF1)
                  Three constructors for TF1
                  User Defined Functions
          Fitting
                  Fit()
                  Fitting with a user defined function
                  Fitting sub ranges and combining
                   functions
                  Demonstration of background and signal
                   function

 November 11               ROOT Day 2, Suzanne Panacek   v.7.0, slide# 59
 The Tree Viewer Overview
     Drawing Histograms
     Using Cuts
     Lego Plots
     Creating new Variables
     Saving the Canvas




November 11     ROOT Day 2, Suzanne Panacek   v.7.0, slide# 60
 The Tree Viewer
                                  root[] myTree->StartViewer()
                                  Contents:
                                           XYZ
                                           Cut
                                           Scan
                                           Expressions
                                           Variables (leaves)
                                           Draw option
                                           Recording commands
                                           Histogram
                                           Range Selector
                                           IList/OList




November 11   ROOT Day 2, Suzanne Panacek           v.7.0, slide# 61
 Drawing Two Histograms
                                  Double click on one
                                   variable
                                  Select Superimpose
                                   option
                                  Double click on another
                                   variable




November 11   ROOT Day 2, Suzanne Panacek       v.7.0, slide# 62
 Adding Weight

                        Add a Weight: xmain < 0




November 11   ROOT Day 2, Suzanne Panacek    v.7.0, slide# 63
 2D Lego Plots
                             Draw xmain vs. xs1
                             Rotating the lego
                              plot




November 11   ROOT Day 2, Suzanne Panacek   v.7.0, slide# 64
  Creating a variable
1. Create New Expression

                                  2. Set the name and expression
                                  3. Double click to draw it




 November 11        ROOT Day 2, Suzanne Panacek      v.7.0, slide# 65
     More Features
Record:
  1) Command is recorded in history file ($HOME/root_hist)
  2) Is printed on the command line
Scan:
  1) Drag and drop variables to scan box
  2) Redirect output to a file
Saving and recovering a Session:
  1) Save the current cuts, expressions into treeviewer.C
  2) Read session back




    November 11          ROOT Day 2, Suzanne Panacek    v.7.0, slide# 66
 Saving the Canvas
                 The save options:
                     Postscript
                     gif
                     ROOT file
                     Macro
                 Save as a ROOT file:
                     Open the saved ROOT file (c1.root)
                      and draw the canvas
                         root[2] > c1.Draw();




November 11             ROOT Day 2, Suzanne Panacek   v.7.0, slide# 67
 Summary (TreeViewer)
     Drawing a Two Histogram
     Adding Weight
     Lego Plots
     Creating a variable
     More Commands
     Saving a Canvas



November 11    ROOT Day 2, Suzanne Panacek   v.7.0, slide# 68

								
To top