eic-smear  1.0.3
A collection of ROOT classes for Monte Carlo events and a fast-smearing code simulating detector effects for the Electron-Ion Collider task force
tree.py
Go to the documentation of this file.
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 
tree.load
def load()
Definition: tree.py:16
tree.write
def write(outputname, nevents)
Definition: tree.py:36
tree.build
def build(inputname, outputdir='.', nevents=-1)
Definition: tree.py:24
tree.read
def read(inputname, treename)
Definition: tree.py:61