Looping over all histograms in a ROOT file using pyROOT
from ROOT import TFile tfile = TFile.Open("histos.root") tfile.cd() for h in tfile.GetListOfKeys(): h = h.ReadObj() print h.ClassName(), h.GetName()
Looping over all histograms in a ROOT file using pyROOT
from ROOT import TFile tfile = TFile.Open("histos.root") tfile.cd() for h in tfile.GetListOfKeys(): h = h.ReadObj() print h.ClassName(), h.GetName()
Here’s a code snippet to loop over all histograms (and get their names) in pyROOT:
from ROOT import TFile fname = 'histos.root' hnames = [] tfile = TFile.Open(fname) for key in tfile.GetListOfKeys(): h = key.ReadObj() if h.ClassName() == 'TH1F' or h.ClassName() == 'TH2F': hnames.append(h.GetName())
To build ROOT 6.06/00 (released on 2015-12-09) on Linux Mint 17.3 “Rosa”, follow these instructions:
sudo apt-get install git dpkg-dev make g++ gcc binutils libx11-dev libxpm-dev \ libxft-dev libxext-dev cmake libpng12-dev libjpeg-dev libgif-dev libxml2-dev python-dev wget https://root.cern.ch/download/root_v6.06.00.source.tar.gz tar -xzf root_v6.06.00.source.tar.gz mkdir root-build cd root-build cmake ../root-6.06.00/ # Edit CMakeCache.txt to enable roofit, minuit2, fortran, xrootd cmake --build . cmake -DCMAKE_INSTALL_PREFIX=/tmp/root -P cmake_install.cmake
(based on https://root.cern.ch/building-root.)
The instructions should also work for Ubuntu 14.04 “Trusty Tahr” and other distributions derived from it.
pyROOT is a python binding of ROOT, which allows you to do a lot of pythonic things. The example below shows how to make a TGraph in pyROOT:
from ROOT import TGraph, gPad from array import array from math import exp def func(x): y = 100*exp(-x) return y # Method 1 if True: n = 100 gr1 = TGraph(n) for i in range(n): x = 0.01*i gr1.SetPoint(i, x, func(x)) gr1.SetMarkerStyle(20) gr1.SetMarkerSize(1) gr1.Draw("AP") # Method 2 if False: n = 100 xarr = [] yarr = [] for i in range(n): x = 0.01*i y = func(x) xarr.append(x) yarr.append(y) gr2 = TGraph(len(xarr), array('d', xarr), array('d', yarr)) gr2.SetMarkerStyle(20) gr2.SetMarkerSize(1) gr2.Draw("AP")
RooFit tutorial by Wouter Verkerke at School of Statistics (SOS ’08):
https://indico.in2p3.fr/contributionDisplay.py?contribId=15&confId=750