ROOT Trips & Tricks. Ole Hansen. Jefferson Lab. Hall A & C Analysis Workshop June 26 27, 2017

Similar documents
ROOT: An object-orientated analysis framework

Introduction to ROOT and application to data analysis at the LHC

ROOT. Introduction. Spring 2010 Lecture 5. S. Lehti and V.Karimäki. COMPUTING METHODS IN HIGH ENERGY PHYSICS (page 1)

ROOT Course. Vincenzo Vitale, Dip. Fisica and INFN Roma 2

Introduction to ROOT. Sebastian Fleischmann. 06th March 2012 Terascale Introductory School PHYSICS AT THE. University of Wuppertal TERA SCALE SCALE

PyROOT: Seamless Melting of C++ and Python. Pere MATO, Danilo PIPARO on behalf of the ROOT Team

ROOT TUTORIAL. Dirk Krücker, Kelly Beernaert, Ilya Bobovnikov.

Getting Started with ROOT

Computational Physics Operating systems

Exercises for Lecture 8

HEP data analysis using ROOT

B. List An Introduction to C++ Page 1

INTRODUCTION TUTORIAL

CS2141 Software Development using C/C++ C++ Basics

An Introduction to Root I/O

ROOT Analysis Framework (I) Introduction. Qipeng Hu March 15 th, 2015

INTRODUCTION TO ROOT & BASIC APPLICATIONS

An Introduction to ROOT

An Introduction to ROOT

Lecture I: Basics REU Root Duke Jen Raaf

A short, pragmatic and INCOMPLETE intro to C++/ROOT programming. Mikołaj Krzewicki Nikhef/UU

Practical Statistics for Particle Physics Analyses: Introduction to Computing Examples

Project 1: Convex hulls and line segment intersection

Introduction to Supercomputing

Introduction to ROOT. M. Eads PHYS 474/790B. Friday, January 17, 14

Lecture 7. Simulations and Event Generators. KVI Root-course, April Gerco Onderwater, KVI p.1/18

CS101 Linux Shell Handout

Setting Up a C++ Analyzer Replay

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

IDL DISCOVER WHAT S IN YOUR DATA

Data Analysis Frameworks: Project 9

Root programming and data analysis

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

HEP data analysis using ROOT

EP578 Computing for Physicists

Lab: Supplying Inputs to Programs

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Data Processing in HEP: ROOT

ROOT for beginners. First Day Discovering the graphical environment

cling Axel Naumann (CERN), Philippe Canal (Fermilab), Paul Russo (Fermilab), Vassil Vassilev (CERN)

P445/515 Data Analysis using PAW

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

PyROOT Automatic Python bindings for ROOT. Enric Tejedor, Stefan Wunsch, Guilherme Amadio for the ROOT team ROOT. Data Analysis Framework

Hall-C Analyzer & Hall-C Replay

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS242 COMPUTER PROGRAMMING

Computational Modelling 102 (Scientific Programming) Tutorials

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Using Fluxes and Geometries

Introduction to Linux

Bitmap Indices for Fast End-User Physics Analysis in ROOT

Weekly Research Report

Preview from Notesale.co.uk Page 2 of 79

Chapter 4. Unix Tutorial. Unix Shell

Python for Astronomers. Week 1- Basic Python

Command Line and Python Introduction. Jennifer Helsby, Eric Potash Computation for Public Policy Lecture 2: January 7, 2016

Project 1: Convex hulls and line segment intersection

Maple Quick Start. Maplesoft, a division of Waterloo Maple Inc.

Object Oriented Design

Independent Representation

Jörg Stelzer. International School of Theory & Analysis in Particle Physics. Michigan State University, East Lansing, USA

Computing and Statistical Data Analysis Lecture 3

Functions. Systems Programming Concepts

Working with Batches of Data

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Lab 1: Introduction to C++

Computer Science II Lecture 2 Strings, Vectors and Recursion

Figure 1. Overview of ENVI viewers and the list of available bands

3 Histograms. The Histogram Classes

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Lab 7: Bayesian analysis of a dice toss problem using C++ instead of python

Using Excel for Graphical Analysis of Data

Exercise 1: Basic Tools

AIDA JAS Massimiliano Turri, SLAC Massimiliano Turri, SLAC INFN-LNF, 7 July 2003

C++ Programming Lecture 1 Software Engineering Group

Multiple variables data sets visualization in ROOT

Expressions, Input, Output and Data Type Conversions

2 nd Week Lecture Notes

Program Organization and Comments

Practical 1P1 Computing Exercise

Scientific Computing

Topic 8: Lazy Evaluation

Study Guide for Test 2

Basics. proper programmers write commands

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

Physics 234: Computational Physics

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

Numerical Algorithms for Physics. Andrea Mignone Physics Department, University of Torino AA

(5) ifit/math: «One Class to do some Math» God damn it! Just compute it! ifit workshop NBI Jan 2012 Math - 1

1. In C++, reserved words are the same as predefined identifiers. a. True

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Intermediate Programming, Spring Misha Kazhdan

What is ROOT? Why do we use it? Answer: ROOT does what physicists do: It makes plots.

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Linux Command Line Primer. By: Scott Marshall

Transcription:

ROOT Trips & Tricks Ole Hansen Jefferson Lab Hall A & C Analysis Workshop June 26 27, 2017 Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 1 / 25

Brief Introduction Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 2 / 25

ROOT in a Nutshell Framework for large scale data handling Provides efficient data storage, access and query (petabytes) advances statistical analysis: histogramming, fitting, minimization, etc. scientific visualization: 2D and 3D graphics geometry toolkit event display parallel query engine (PROOF) interactive C++11 interpreter Open Source. GNU LGPL. 2M lines of code. Supported by 10 staff at CERN and Fermilab Tens of thousands of users in high-energy and nuclear physics, and elsewhere (e.g. finance) This talk covers the current version, ROOT 6 Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 3 / 25

The Interpreter C++11 interpreter: Cling (based on LLVM/Clang) Provides interactive shell Allows rapid prototyping and testing Just-in-time and on-the-fly compilation Same language for scripting and compiled code Python bindings available Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 4 / 25

C++ Object I/O and Persistency ROOT lets you write C++ objects to file Extraordinary: impossible in native C++! Used for storage of hundreds of petabytes at LHC Achieved with serialization of objects based on C++ reflection made possible by class dictionaries generated with Cling Can write single objects, collections (containers), entire object trees One method for all ROOT objects: TObject::Write Evolution from flat n-tuples used in low and medium-energy physics Cornerstone for the storage of experimental data in HEP Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 5 / 25

Large Collection of Additional Libraries Graphics & Visualization 1D, 2D and 3D histograms Very large collection of data plot types Can save graphics in many formats (e.g. PDF, JPEG, PNG) Geometry Toolkit and Event Display Detailed description of detector geometry and materials Used extensively in simulations and event reconstruction 3D visualization of geometry, detector hits and tracks Mathematical Functions TMath: commonly used math functions, constants, statistics ROOT::Math: very large collection of special functions, interface to Gnu Scientific Library (GSL) Minimization Extensive library of fitting and minimization routines Widely used in data analysis and significance testing Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 6 / 25

Using ROOT Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 6 / 25

Before We Get Started Please update the "tutorial" subdirectory in your virtual machine [me@centos7 ~]$ cd ~/tutorial [me@centos7 tutorial]$ git pull Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 7 / 25

Exercise 1: ROOT Command Line Basics Starting up, version check [me@centos7 ~]$ cd ~/tutorial/roottutorial [me@centos7 ~]$ root root [0] groot->getversion() (const char *) "6.08/06" Defining variables root [1] i = 10 (int) 10 root [2] x = 3.14 (double) 3.14000 root [3] e = 2.718 ; root [4] e (double) 2.71800 May omit trailing ; return value printed May omit type specification treated like auto Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 8 / 25

Exercise 2: ROOT Command Line Arithmetic ROOT as a (hyper-charged) pocket calculator root [0] 5+7*(3-2) (int) 12 root [1] sqrt(2) (std::enable_if<true, double>::type) 1.41421 root [2] double angle = 45.; root [3] sin(angle * TMath::DegToRad()) (double) 0.707107 root [4] TMath::Erf(1.) (Double_t) 0.842701 root [15] cout << setprecision(16) << TMath::Pi() << endl; 3.141592653589793 Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 9 / 25

Exercise 3: C++11 in ROOT C++11 in ROOT: a few simple examples // Initialize a std::vector: initializer list analyzer [1] vector<double> dvars {3.45, 1.5, 9.91, 6.28, -2.718} (std::vector<double> &) { 3.45000, 1.50000, 9.91000, 6.28000, -2.71800 } // Much simpler looping over containers (vectors etc.) analyzer [2] for( auto x : dvars ) cout << x << ", "; cout << endl; 3.45, 1.5, 9.91, 6.28, -2.718, // This is old, but still works, now even in ROOT: std::sort analyzer [3] std::sort(dvars.begin(), dvars.end()); analyzer [4] dvars (std::vector<double> &) { -2.71800, 1.50000, 3.45000, 6.28000, 9.91000 } // Define functions on the fly: lambda expressions analyzer [5] std::sort(dvars.begin(), dvars.end(), [](double a, double b) {return b<a;} ); analyzer [6] for( auto x : dvars ) cout << x << ", "; cout << endl; 9.91, 6.28, 3.45, 1.5, -2.718, Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 10 / 25

Exercise 4: Macros/Scripts add42.c int add42(int value) { return value+42; } Running and Loading a Macro/Script // Run plain script root [1].x add42.c(11) (int) 53 // Load, then execute // In this way, you can use multiple functions defined in the script root [2].L add42.c root [3] add42(11) (int) 53 Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 11 / 25

Exercise 4 (cont.): Compiling Macros/Scripts Compiling a macro/script // Compile on the fly - much faster for bigger scripts root [0].L add42.c+ root [1] add42(11) (int) 53 // Reuse compiled script - rebuilt only if changed // Execute directly root [0].x add42.c+(11) (int) 53 // Load, then execute root [0].L add42.c+ root [1] add42(11) (int) 53 // Force recompilation root [2].L add42.c++ Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 12 / 25

Exercise 5: Other Useful Commands Control commands // Quit ROOT ;) root [0].q // Get help on commands root [0].? root [1].help // Run shell command root [2].!pwd root [3].!cat add42.c // Redirect output to a file root [5].> output.txt // overwrites root [6] cout << TMath::C() << endl; root [7].> // stops redirection root [8] cout << 123 << endl; 123 root [9].!cat output.txt 2.99792e+08 root [10].>> output.txt // appends if file exists root [11] (your pick) Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 13 / 25

Histograms Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 13 / 25

Exercise 6: Creating One-Dimensional Histograms Creating & Filling Histograms // Creating // name description #bins min,max root [0] TH1* h1 = new TH1I("h1","Example histogram",120,-3.,3.); // Filling h1->fill(x); // Filling with 1000 random values root [2] for ( int i=0; i<1000; ++i ) { root (cont ed, cancel with.@) [3]double x = grandom->gaus(0,1); root (cont ed, cancel with.@) [4]h1->Fill(x); root (cont ed, cancel with.@) [5]} root [6] h1->getentries() (Double_t) 1000.00 Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 14 / 25

Exercise 7: Plotting Histograms Drawing any histograms root [7] h1->draw(); The box shows Name Number of entries Mean value Std. deviation (RMS) Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 15 / 25

Exercise 8: Plotting Histograms (cont.) Always show over/underflow! root [8] gstyle->setoptstat(111111); Note how the statistics box automatically changes Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 16 / 25

Exercise 9: Histogram Drawing Options Draw error bars on every bin root [9] h1->draw("e"); Draw another histogram without replacing current plot. Can be used to plot one histgram on top of another // Restart ROOT, then root [0].x make_histos.c root [1] h2->setlinecolor(kred); root [2] h2->draw("same"); Display one axis with logarithmic scale root [3] gpad->setlogy(); root [4] gpad->setlogy(false); // y-axis log scale on // off again Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 17 / 25

More About Histograms TH1 is the base class for all histogram classes (even 2D and 3D... ) TH1, TH2 and TH3 are generic. Must use specialized ones (e.g. TH1D) for constructing. TH1C: 1 byte (char) per channel, maximum bin content = 127 TH1S: 2 bytes (short), max. content = 32767 TH1I: 4 bytes (int), max. content = 2147483647 TH1F: 4 bytes (float), max. precision 7 digits TH1D: 8 bytes (double), max. precision 14 digits TH1(2,3)F and TH1(2,3)D are most common, unless you either need integer precision (TH1(2,3)I) or need to save memory (TH1(2,3)(C,S)). Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 18 / 25

Exercise 10: 2D Histograms Creating and Filling a 2D histogram // Create and fill a 2D histogram root [0].!cat make_2d.c #include "TH2F.h" #include "TRandom.h" TH2* h2d; void make_2d() { h2d = new TH2F("h2D","2D Gaussian peak",160,-4.,4.,160,-4.,4.); for( int i=0; i<100000; ++i ) { double x = grandom->gaus(0,1); double y = grandom->gaus(1,2); h2d->fill(x,y); } } root [1].x make_2d.c Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 19 / 25

Exercise 11: Drawing 2D Histograms Drawing 2D histograms // Scatter plot root [2] h2d->draw(); // Color Z root [3] h2d->draw("colz"); // Contour plot root [3] h2d->draw("contz"); // Lego plot (3D) root [4] h2d->draw("lego"); Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 20 / 25

Exercise 12: Projections and Profile Histograms Projections and Profile Histograms // Projection of 2D histogram onto one of its axes // This sums all y-bins with the same x value root [5] TH1* hx = h2d->projectionx(); root [6] hx->draw(); // Profile histogram: Plot the mean and standard deviation // of all y-bins with the same x value root [7].! cat make_prof.c #include "TProfile.h" #include "TRandom.h" TProfile* hp; void make_prof() { hp = new TProfile("hp","Profile of shifted Gaussian",160,-4.,4.); for( int i=0; i<100000; ++i ) { double x = grandom->gaus(0,1); double y = grandom->gaus(1,2); hp->fill(x,y); } } root [8] hp->draw(); Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 21 / 25

Exercise 13: Fitting Histograms Fitting Histograms // Basic root [9] h1->fit("gaus"); root [10] gstyle->setoptfit(1111); // More complex (identical to above, but flexible now) root [11] TF1* f1 = new TF1("f1","[0]*TMath::Gaus(x,[1],[2])"); root [12] f1->setparameters(1,0,1); root [13] h1->fit(f1); // Retrieving fit results root [14] TF1* ff = h1->getfunction("f1"); root [15] ff->getparameter(0) root [16] ff->getparerror(0) // More detailed information about the fit root [17] r = h1->fit(f1,"s"); // Can now retrieve full fit information from r root [18] TMatrixDSym C = r->getcorrelationmatrix(); // operator ->! root [19] C.Print(); Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 22 / 25

Exercise 14: More Fitting Options More Fitting Options // Restrict fit range root [20] h1->fit("gaus","","",-1.5,1.5); // Fit a second function to a different part of the histogram // with the "+" option root [21] h1->fit("expo","+","",2,4); // Verbose more detail than you ever want to know root [22] h1->fit("gaus","v"); // Likelihood fit root [23] h1->fit("gaus","l"); // Pass histogram plotting options as 3rd parameter root [24] c1->clear(); root [25] h1->fit("gaus","l","e"); Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 23 / 25

Files and Trees Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 23 / 25

Exercise 15: Opening an browsing a File ROOT files and the browser // Quit ROOT and start the analyzer so we have all class info [me@centos7 ~]$ analyzer // Open a ROOT file analyzer [0] TFile* file = new TFile("/data/ROOTfiles/gmp_23062_good.root", "READ") // Listing the file contents. KEY : on disk, OBJ : in memory analyzer [1].ls TFile** /data/rootfiles/gmp_23062_good.root TFile* /data/rootfiles/gmp_23062_good.root KEY: THaRun Run_Data;2 gmp run 23062 KEY: TTree T;1 Hall A Analyzer Output DST // Much more user-friendly: the ROOT browser analyzer [2] new TBrowser; Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 24 / 25

Exercise 16: Plotting Tree Variables A very powerful method to analyze our trees is the TTree::Draw method ROOT files and the browser // Basic histogram analyzer [3] T->Draw("R.vdc.u1.wire"); // Applying a selection analyzer [3] T->Draw("R.vdc.u1.wire","R.vdc.u1.wire>100"); // Avoidng automatic binning: defining histograms on the fly analyzer [4] T->Draw("R.vdc.u1.wire>>hw(368,0,368)","R.vdc.u1.wire>100"); // 2D histograms: Inspecting correlations analyzer [5] T->Draw("R.vdc.u1.wire:R.vdc.u2.wire"); // Using histogram drawing options (see earlier) analyzer [6] T->Draw("R.vdc.u1.wire:R.vdc.u2.wire","","CONTZ"); // CAUTION: u1.wire and u2.wire are NOT parallel arrays! analyzer [7] T->Draw("Ndata.R.vdc.u1.wire:Ndata.R.vdc.u2.wire","","LEGO"); analyzer [8] gpad->setlogz() // The 2D plot shows only elements with indices i=j, which may or // may not be meaningful Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 25 / 25

Thanks! Ole Hansen (Jefferson Lab) ROOT Trips & Tricks Analysis Workshop 2017 25 / 25