How To Use ROOT - some features - Ann-Cecilie Larsen & Alexander Bürger 15 Januar 2009 OCL group meeting

Page created by Michele Wright
 
CONTINUE READING
How To Use ROOT - some features - Ann-Cecilie Larsen & Alexander Bürger 15 Januar 2009 OCL group meeting
How To Use ROOT
  -- some features

Ann-Cecilie Larsen & Alexander Bürger
           15 Januar 2009
         OCL group meeting
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
How To Use ROOT - some features - Ann-Cecilie Larsen & Alexander Bürger 15 Januar 2009 OCL group meeting
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
Classes 'in Nature'
                                             very general: something
                             animal
arrow= “is a”                                “abstract”: does not actually exist

                                             very specific,
                fish            mammal
                                             not “abstract”, can exist

                       ...
                              cow     dog                      'instance' with
                                                               specific
                                                               property values
                                            Romeo
   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
Classes in C++
                                       // derived class definition
   a class is a set of                class Histogram {
                                       public:

    object properties                      void Fill(float value);
                                           int   GetNbins();
                                           virtual float GetBinContent(int bin);
        methods/functions             protected:
                                           float* GetBins() const { return bins; }
                                       private:
             constructor&destructor       int    nbins;            encapsulation
                                           float* bins;
        variables                     };
                                       ...
                                       // method definition
   objects are instances              float Histogram::GetBinContent(int bin)
                                       {
    of their class                     }
                                           return bins[bin];

                                       ...
                                       // create an instance
                                       Histogram* h1 = new Histogram(...);
                                       cout GetBinContent(5)
Inheritance
    arrow=“derived from”

                                            very general: something
                           TObject
                                            “abstract”: does not actually exist

TVirtualPad                                 something with a name
                           TNamed
                                            still “abstract”

 TCanvas         ...
                                            histogram with bins, axes, ...
                            TH1
                                            not “abstract”, can exist
     ...
                 ...
    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
                                                                 inheritance
   a derived class can         // derived class definition
                                class SafeHistogram : public Histogram {
                                public:
        override (replace)         float GetBinContent(int bin); // virtual
                                    int GetNbins(); // not virtual
         methods/functions      };
                                ...
                                // method definition
                                                             polymorphism
        add variables and      float SafeHistogram::GetBinContent(int bin)
                                {
         methods                    if( bin>=0 && bin
Syntactic Sugar -- “Typing help”
                                        a[i][j]        // instead of *(*(a + i) + j)
   less typing, easier to              h1­>GetNbins() // instead of (*h1).GetNbins()

    read, fewer errors                  #include 

                                        ifstream input_file(“/tmp/plot.dat”);
                                        float x, y;
   examples                            input_file >> x >> y;

                                        // definition: istream& istream::operator>>(float& x);

       array access                    void do_something(int x);    // integer argument
                                        void do_something(double x); // double argument
                                        void do_something(string s); // string
       stream                          const char* text = “OCL”;
        extraction/insertion            do_something(text); // creates temporary
                                                            // string object
                                        float f = 15.1;
       name overloading                do_something(f); // conversion may be tricky!

            distinguish functions by
             signature (name + types)
       implicit type conversion
Templates and the STL
   templates                                           // bad:
                                                        #define MAX(a,b) ((a>b)?a:b)
                                                        MAX(“here”,”there”); // compares pointers
                                                        MAX(f(1),f(2));      // calls f three times
       “generic classes”                               // better
                                                        template
       help avoid preprocessor                         T& max(T& a, T& b)
                                                        {
        macros                                              if( a > b )
                                                                return a;
                                                            else
   STL – standard                                      }
                                                                return b;

    template library
                                                        max(f(1),f(2));      // calls f two times
                                                                             // automatically figures T
                                                        max(10, 15.1); // int and double

       implements often-                               template 
                                                        class vector {

        needed things                                   public:
                                                            unsigned size() const { ... }
                                                        };
       examples                                        #include 
                                                        #include 
              sorted collections                       std::map mymap;
                                                        cout
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: swshareroottutorials
Installing ROOT on MacOS

   easiest with fink
       shows i if installed
       otherwise install with
        fink install root5
        in a terminal
Starting and Stopping

   start: “root”
    in a terminal
   stop: “.q”
Running Scripts
   .x mymacro.C              .x mymacro.C+ or
       interpreted mode       .x mymacro.C++
                                  compiled mode
Script Example
// CINT automatically uses “using namespace std”, some
    “#include”s, ...                                         levdens46Ti SetStyle("Plain"); // no colors/fill areas            int j;
 gStyle­>SetOptTitle(0);    // no histogram title             rho46file >> j >> energy46[i] >> rho46[i] >> rhoerr46[i];
 gStyle­>SetOptTitle(1);                                      levdens46Ti
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
You can also read