Skip to content

Instantly share code, notes, and snippets.

@alexpearce
Created January 19, 2015 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpearce/4b45f553ecae48015913 to your computer and use it in GitHub Desktop.
Save alexpearce/4b45f553ecae48015913 to your computer and use it in GitHub Desktop.
TTreeFormula example
import ROOT
# Replace with location to your favourite ntuple
f = ROOT.TFile((
'/Users/apearce/Physics/Data/CharmProduction2010/Reprocessed'
'/DVntuple.2010.12.MagUp.root'
), 'read')
t = f.Get('TupleD0Tohh/DecayTree')
h1 = ROOT.TH1F('h1', 'h1', 170, 1780, 1950)
h2 = ROOT.TH1F('h2', 'h2', 170, 1780, 1950)
# Select 40 MeV around the nominal D0 mass
formula = ROOT.TTreeFormula('mass_cut', 'abs(D0_M - 1864.84) < 40', t)
formula.GetNdata()
for i in range(t.GetEntries()):
t.GetEntry(i)
# This will generate an apparently harmless warning
# https://root.cern.ch/phpBB3/viewtopic.php?f=14&t=14213
if formula.EvalInstance():
h1.Fill(t.D0_M)
else:
h2.Fill(t.D0_M)
c = ROOT.TCanvas('c', 'c', 800, 400)
c.Divide(2, 1)
c.cd(1)
h1.Draw()
c.cd(2)
h2.Draw()
c.SaveAs('test_ttreeformula.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment