EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tree.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file tree.py
1 # Created by tpb on 15th Feb 2012
2 
3 """Example routines for creating and reading a ROOT tree.
4 
5 Python is simply a superior way to write many ROOT scripts than
6 using normal CINT macros. It is more stable, and the simpler
7 syntax of Python compared to C++ is better suited to common
8 tree and histogramming operations.
9 This module contains simple routines outlining how to create
10 and access tree files with EIC events via Python.
11 Note you need a version of ROOT compiled against Python and
12 the necessary environment variables set (see the ROOT installation
13 instructions).
14 """
15 
16 def load():
17  """Import the PyROOT module and use ROOT.gSystem.Load
18  to import the eic-smear class library.
19  """
20  import ROOT
21  ROOT.gSystem.Load('libeicsmear') # Or whatever your path is
22 
23 
24 def build(inputname, outputdir = '.', nevents = -1):
25  """Example of creating a tree.
26 
27  Once the eic-smear library, you can just use the
28  BuildTree routine as usual.
29  """
30  import ROOT
31  ROOT.gSystem.Load('libeicsmear') # Or whatever your path is
32 
33  ROOT.BuildTree(inputname, outputdir, nevents)
34 
35 
36 def write(outputname, nevents):
37  """Example of creating a tree.
38 
39  Creating and writing events manually without the use of BuildTree.
40  """
41  import ROOT
42  ROOT.gSystem.Load('libeicsmear') # Or whatever your path is
43 
44  file = ROOT.TFile(outputname, 'recreate')
45  tree = ROOT.TTree('events', 'A ROOT tree of EIC events')
46 
47  # Create a branch buffer of whatever event type you fancy
48  event = ROOT.erhic.EventPythia()
49  tree.Branch('event', event)
50 
51  for i in xrange(0, nevents):
52  event.SetN(i)
53  # ...
54  # Build your event
55  # ...
56  tree.Fill()
57 
58  file.Write()
59 
60 
61 def read(inputname, treename):
62  """Example of reading back a tree.
63  """
64  import ROOT
65  ROOT.gSystem.Load('libeicsmear') # Or whatever your path is
66 
67  file = ROOT.TFile(inputname, 'read')
68 
69  # Note that you can access the tree simply via file.treename
70  file.Get(treename).Draw('GetN()')
71