demo.py
Go to the documentation of this file.
1 ####################################################################################
2 # File: demo.py #
3 # Created: April 2021 #
4 # Author: Chris Hilenberg (chilgenb@fnal.gov) #
5 # Description: macro for demonstrating how to use garana #
6 # Usage: in your terminal, enter the command 'python demo.py' #
7 # and that's all there is to it :) #
8 # you can build off the example below to access other trees/variables. #
9 # documentation is a work in progress. for now see documentation in #
10 # accessor base classes (garana/Base/*.h). #
11 ####################################################################################
12 
13 import garanainit # script for loading garana libs
14 import ROOT # loads all ROOT libs
15 
16 garanainit.init() # FIXME (if neccessary) loads the garana libs, may need to update library include paths
17 
18 # central garana tree manager
19 # it links to all of the individual tree accessors given a file
20 # with garana trees produced in the default garana format
21 tm = ROOT.TreeManager("../../test_garana/structuredtree.root") #FIXME update with path to your .root file
22 
23 # get accessor to genTree
24 gen = tm.GetGenTree()
25 
26 print("found GenTree with "+str(gen.NEntries())+" entries")
27 
28 # make a histogram of the true neutrino energy
29 h = ROOT.TH1F("h","my PyROOT histogram;E_{#nu} [GeV]",10,0,10);
30 
31 # loop over events
32 for ientry in range(0,gen.NEntries()):
33 
34  gen.GetEntry(ientry) # fill data members with data for current event
35 
36  # loop over all interactions in this event
37  for igen in range(0,gen.NGen()):
38  h.Fill(gen.NuP(igen).E());
39 
40 # draw our histogram and save it to file
41 c = ROOT.TCanvas()
42 h.Draw()
43 c.SaveAs('testhist_pyroot.png')
44 
45 
46 # ask for user input as a way to keep the program from ending so we can
47 # see the histogram.
48 # python doesn't like empty input strings so I abuse exception handling
49 try:
50  null = input("press <Enter> to close canvas and exit program.")
51 
52 except:
53  null = 'null'
54 
static int input(void)
Definition: code.cpp:15695
def init()
Definition: garanainit.py:12
static QCString str