400 likes | 407 Views
Learn about ROOT, an open-source data analysis framework with extensive visualization capabilities, libraries, and user interfaces. Explore its features, installation, application domains, and user statistics.
E N D
Introduction to ROOT (1/2) Jan Fiete Grosse-Oetringhaus, CERN PH/ALICE Summer Student Lectures 2011 July 11th
Content • First lecture • Introduction to the ROOT framework • Library structure • CINT • Macros • Histograms, Graphs, Advanced graphics examples • Input/Output: Files, Trees • Fitting • Second lecture • Practical introduction to the ROOT framework (demo) • Nomenclature • Blue: you type it • Red: you get it Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT in a Nutshell ROOT is a large Object-Oriented data handling and analysis framework Efficient object store scaling from KB’s to PB’s C++ interpreter Extensive 2D+3D scientific data visualization capabilities Extensive set of multi-dimensional histograming, data fitting, modeling and analysis methods Complete set of GUI widgets Classes for threading, shared memory, networking, etc. Parallel version of analysis engine runs on clusters and multi-core Fully cross platform: Unix/Linux, MacOS X and Windows Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT in a Nutshell (2) • The user interacts with ROOT via a graphical user interface, the command line or scripts • The command and scripting language is C++ • Embedded CINT C++ interpreter • Large scripts can be compiled and dynamically loaded And for you? ROOT is usually the interface (and sometimes the barrier) between you and the data Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
The ROOT Libraries Over 2,500 classes 3,000,000 lines of code CORE (8 Mbytes) CINT (2 Mbytes) Most libraries linked on demand via plug-in manager (only a subset shown) 100 shared libs Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT: An Open Source Project The project was started in Jan 1995 First release Nov 1995 The project is developed as a collaboration between: Full time developers: 7 people full time at CERN (PH/SFT) 2 developers at Fermilab/USA Large number of part-time contributors (160 in CREDITS file) A long list of users giving feedback, comments, bug fixes and many small contributions 5,500 users registered to RootTalk forum 10,000 posts per year An Open Source Project, source available under the LGPL license Used by all HEP experiments in the world Used in many other scientific fields and in commercial world Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Some ROOT Statistics • ROOT binaries have been downloaded about 600,000 times since 1997 • The estimated user base is about 20,000 people Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT Application Domains Data Analysis & Visualization General Framework Data Storage: Local, Network Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Three User Interfaces GUI windows, buttons, menus Command line CINT (C++ interpreter) Macros, applications, libraries (C++ compiler and interpreter) We will see that in the demo in the second part of the lecture Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT Download & Installation • http://root.cern.ch • Binaries for common Linux PC flavors, Mac OS, Windows • Preinstalled on AFS (at CERN) • Source files • Installation guide at http://root.cern.ch/drupal/content/installing-root-source • Couple of dependencies, discussed here: http://root.cern.ch/drupal/content/build-prerequisites Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Basic Blocks of ROOT • Command line interpreter CINT • Macros • Histograms and Graphs • Files • Trees Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
CINT in ROOT • CINT is used in ROOT: • As command line interpreter • As script interpreter • To generate class dictionaries • To generate function/method calling stubs • Signals/slots with the GUI • The command line, script and programming language become the same • Large scripts can be compiled for optimal performance Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
First CINT Example $ root root [0] 344+76.8 (const double)4.20800000000000010e+002 root [1] float x=89.7; root [2] float y=567.8; root [3] x+sqrt(y) (double)1.13528550991510710e+002 root [4] float z = x+2*sqrt(y/6); root [5] z (float)1.09155929565429690e+002 root [6] .q $ Display online help with: root [0] .h Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Named Macros It is quite cumbersome to type the same lines again and again Create macros for commonly used code Macro = file that is interpreted by CINT Execute with root [0] .x mymacro.C(10) Orroot [0] .L mymacro.Croot [1] mymacro(10) int mymacro(int value) { int ret = 42; ret += value; return ret; } saved in mymacro.C Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Compile Macros – Libraries "Library": compiled code, shared library CINT can call its functions! Building a library from a macro: ACLiC(Automatic Compiler of Libraries for CINT) Execute it with a “+” root [0] .x mymacro.C(42)+ Or root [0] .L mymacro.C+root [1] mymacro(42) No Makefile needed CINT knows all functions in the library mymacro_C.so/.dll Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Compiled vs. Interpreted • Why compile? • Faster execution, CINT has some limitations… • Why interpret? • Faster Edit → Run → Check result → Edit cycles ("rapid prototyping"). Scripting is sometimes just easier • So when should I start compiling? • For simple things: start with macros • Rule of thumb • Is it a lot of code or running slow? Compile it! • Does it behave weird? Compile it! • Is there an error that you do not find Compile it! Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Unnamed Macros No function, just statements Execute with root [0] .x mymacro.C No functions, thus no arguments Named macro recommended! { float ret = 0.42; return sin(ret); } saved in mymacro.C Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT Types • You can use native C types in your code (as long as you don’t make your data persistent, i.e. write to files) • ROOT redefines all types to achieve platform independency • E.g. the type int has a different number of bits on different systems • int Int_tfloat Float_tdouble Double_tlong Long64_t (not Long_t)etc. • See $ROOTSYS/include/Rtypes.h Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Container for binned data Most of HEP’s distributions Container for distinct points Calculation or fit results Histograms & Graphs Graph Histogram Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Histograms • Histograms are binned data containers • There are 1, 2 and 3-dimensional histograms TH1, TH2, TH3 • The data can be stored with different precision and in different types (byte, short, int, float, double) TH1C, TH1S, TH1I, TH1F, TH1D (same for TH2, TH3) Histogram Example hist = new TH1F("hist", "Vertex distribution;z (cm);Events", 20, -10, 10); hist->Fill(0.05); hist->Fill(-7.4); hist->Fill(0.2); hist->Draw(); NB: All ROOT classes start with T Looking for e.g. a string? Try TString Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Graphs • A graph is a data container filled with distinct points • TGraph: x/y graph without error bars • TGraphErrors: x/y graph with error bars • TGraphAsymmErrors: x/y graph with asymmetric error bars Graph Example graph = new TGraph; graph->SetPoint(graph->GetN(), 1, 2.3); graph->SetPoint(graph->GetN(), 2, 0.8); graph->SetPoint(graph->GetN(), 3, -4); graph->Draw("AP"); graph->SetMarkerStyle(21); graph->GetYaxis()->SetRangeUser(-10, 10); graph->GetXaxis()->SetTitle("Run number"); graph->GetYaxis()->SetTitle("z (cm)"); graph->SetTitle("Average vertex position"); Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Graphs (2) TGraphErrors(n,x,y,ex,ey) $ROOTSYS/tutorials/graphs/gerrors2.C TGraph(n,x,y) TCutG(n,x,y) TMultiGraph TGraphAsymmErrors(n,x,y,exl,exh,eyl,eyh) Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
You can draw with the command line The Draw function adds the object to the list of primitives of the current pad If no pad exists, a pad is automatically created A pad is embedded in a canvas You create one manually with new TCanvas A canvas has one pad by default You can add more Graphics Objects root [ ]TLine line(.1,.9,.6,.6) root [ ]line.Draw() root [ ]TText text(.5,.2,”Hello”) root [ ]text.Draw() Canvas Hello Pad Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
More Graphics Objects TLine TArrow TEllipse TBox TButton TCurvyLine TDiamond TText TPave TMarker TPavesText TCrown TPaveLabel TCurlyArc TPolyLine TLatex Can be accessed with the toolbar View Toolbar (in any canvas) Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Full LateXsupport on screen and postscript $ROOTSYS/tutorials/graphics/latex3.C Formula or diagrams can be edited with the mouse $ROOTSYS/tutorials/graphics/feynman.C TCurlyArc TCurlyLine TWavyLine and other building blocks for Feynmann diagrams A lot more examples come with the ROOT installation Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Graphics Examples TGLParametric TF3 Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Input/Output Net File Object in Memory sockets Web File http Buffer XML File XML RDBMS SQL Streamer File on disk Local The automatically generated ROOT streamer for each class streams all class members, resolves circular dependencies and multiply referenced objects No streamer function needs to be written No need for separation of transient and persistent classes Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Files • TFile is the class to access files on your file system (and elsewhere) • A TFile object may contain directories (TDirectory), like a Unix file system • ROOT files are self describing • Dictionary for persistent classes written to the file • Support for Backward and Forward compatibility • Files created in 2006 must be readable in 2020 Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
File Example voidkeyWrite() { TFile f("file.root", "new"); TH1F h("hist", "test", 100, -3, 3); h.FillRandom("gaus", 1000); h.Write() } This works as well for your own class! voidkeyRead() { TFile f("file.root"); TH1F *h = (TH1F*) f.Get("hist"); h.Draw(); } Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
LHC: How Much Data? High Level-1 Trigger(1 MHz) High No. ChannelsHigh Bandwidth(500 Gbit/s) Level 1 Rate (Hz) 106 LHCB 1 billion people surfing the Web ATLAS CMS 105 HERA-B High Data Archive(5 PetaBytes/year) 10 Gbits/s in Data base KLOE CDF II 104 CDF 103 H1ZEUS ALICE NA49 UA1 STAR How to store large number of events and data volumes efficiently? ROOT Trees 102 104 105 106 107 LEP Event Size (bytes) Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
What is a ROOT Tree? • Trees have been designed to support very large collections of objects. The overhead in memory is in general less than 4 bytes per entry. • Trees allow direct and random access to any entry (sequential access is the most efficient) • Trees are structured into branches and leaves. One can read a subset of all branches • High level functions like TTree::Draw loop on all entries with selection expressions • Trees can be browsed via TBrowser • Trees can be analyzed via TTreeViewer Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Stored Trees vs. Memory One instance in memory Tree On Disk Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
x x x x x x x x x x y y y y y y y y y y z z z z z z z z z z Trees: Split Mode • The tree is partioned in branches • Each class member is a branch (in split mode) • When reading a tree, certain branches can be switched off speed up of analysis when not all data is needed File 1 "Event" Branches Events Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
TTree - Writing • You want to store 1 million objects of type TMyEvent in a tree which is written into a file • Initialization • Fill the tree (1 million times) • TTree::Fill copies content of member as new entry into the tree • Flush the tree to the file, close the file TFile* f = TFile::Open("events.root", "RECREATE");TTree* tree = new TTree("Events","Event Tree");TMyEvent* myEvent = new TMyEvent;TBranch* branch = tree->Branch("myevent", "TMyEvent", &myEvent); myEvent->SetMember(…); tree->Fill(); tree->Write(); f->Close(); Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
TTree - Reading • Open the file, retrieve the tree and connect the branch with a pointer to TMyEvent • Read entries from the tree and use the content of the class TFile *f = TFile::Open("events.root"); TTree *tree = (TTree*)f->Get("Events"); TMyEvent* myEvent = 0; tree->SetBranchAddress("myevent", &myEvent); Int_t nentries = tree->GetEntries(); for (Int_t i=0;i<nentries;i++) { tree->GetEntry(i); cout << myEvent->GetMember() << endl; } A quick way to browse through a tree is to use a TBrowser Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
Fitting • Fitting a histogram or graph • With the GUI • If you just try which functions works well or need a single parameter • Right click on graph or histogram Fit panel • With the command line / macro • If you fit many histograms/graphs or several times hist->Fit("gaus") hist->FindFunction("gaus")->GetParameter(0) Fit parameters printed to the screen Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
ROOT is MORE…. In this talk, I presented the most basic classes typically used during physics analyses ROOT contains many more libraries, e.g. FFT library Oracle, MySQL, etc interfaces XML drivers TMVA (Multi Variate Analysis) GRID, networking and thread classes Interfaces to Castor, Dcache, GFAL, xrootd Interfaces to Pythia, Geant3, Geant4, gdml Matrix packages, Fitting packages, etc Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
One Example: PROOF Parallel ROOT Facility Interactive parallel analysis on a local cluster Parallel processing of (local) data (trivial parallelism) Output handling with direct visualization Not a batch system PROOF itself is not related to Grid Can access Grid files The usage of PROOF is transparent The same code can be run locally and in a PROOF system (certain rules have to be followed) PROOF is part of ROOT Data does not need to be copied Many CPUs available for analysis much faster processing Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
PROOF Schema root root root Client – Local PC Remote PROOF Cluster Result stdout/result root root ana.C node1 Result ana.C Data Data node2 Result Data node3 Result Proof master Proof slave Data node4 Introduction to ROOT - Jan Fiete Grosse-Oetringhaus
More Information... http://root.cern.ch Download Documentation Tutorials Online Help Mailing list Forum The second lecture will show many of the presented features in a demo ROOT tutorial on July 12th and 19th Details have been (will be) announced Credits to Fons Rademakers Introduction to ROOT - Jan Fiete Grosse-Oetringhaus