All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cafana.py
Go to the documentation of this file.
1 if __name__ == '__main__':
2  print("This is the source file that implements the cafana python module.")
3  print("You should 'import cafana' at the top of a .py script")
4  print("and execute it with 'cafe myscript.py'")
5  exit(1)
6 
7 import os
8 
9 if 'SBNANA_INC' in os.environ:
10  inc = os.environ['SBNANA_INC']
11 else:
12  inc = os.environ['MRB_INSTALL']+'/sbnana/'+os.environ['SBNANA_VERSION']+'/include/'
13 
14 if 'SBNANAOBJ_INC' in os.environ:
15  sao_inc = os.environ['SBNANAOBJ_INC']
16 else:
17  sao_inc = os.environ['MRB_INSTALL']+'/sbnanaobj/'+os.environ['SBNANAOBJ_VERSION']+'/include/'
18 
19 os.environ['ROOT_INCLUDE_PATH'] = \
20  ':'.join([inc,
21  inc+'sbnana',
22  inc+'sbnana/CAFAna',
23  sao_inc,
24  os.environ['EIGEN_INC'],
25  os.environ['OSCLIB_INC'],
26  os.environ['SRPROXY_INC']])
27 
28 import ROOT
29 
30 if 'SBNANA_FQ_DIR' in os.environ:
31  ROOT.gApplication.ExecuteFile('${SBNANA_FQ_DIR}/bin/rootlogon.C')
32 else:
33  ROOT.gApplication.ExecuteFile('$MRB_BUILDDIR/sbnana/bin/rootlogon.C')
34 print(' in python')
35 ROOT.gROOT.ForceStyle()
36 
37 print('Load libraries...')
38 for lib in ['Minuit2',
39  'sbnanaobj_StandardRecordProxy',
40  'CAFAnaCore',
41  'SBNAnaVars',
42  'SBNAnaCuts',
43  'CAFAnaSysts',
44  'CAFAnaExtrap',
45  'CAFAnaPrediction',
46  'CAFAnaExperiment',
47  'CAFAnaAnalysis',
48  'SBNAnaVars',
49  'SBNAnaCuts']:
50  print(' ', lib)
51  ROOT.gSystem.Load('lib'+lib+'.so')
52 
53 
54 import cppyy
55 
56 print('Load dictionaries... (please ignore errors about .pcm files)')
57 for d in ['CAFAna', 'SBNAna']:
58  print(' ', d)
59  cppyy.load_reflection_info('lib'+d+'_dict.so')
60 
61 class PyCAFAna:
62  def __init__(self, cppyy):
63  self._cppyy = cppyy
64 
65  def CSliceVar(self, body):
66  '''Construct a new slice Var given the C++ body as a string'''
67  var = 'pyvar_'+self._cppyy.gbl.ana.UniqueName()
68  text = '#include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"\ndouble '+var+'_func(const caf::SRSliceProxy* srp){\nconst caf::SRSliceProxy& sr = *srp;\n'+body+'\n}\nconst ana::Var '+var+'('+var+'_func);'
69  self._cppyy.cppdef(text)
70  return getattr(self._cppyy.gbl, var)
71 
72  def CSpillVar(self, body):
73  '''Construct a new spill Var given the C++ body as a string'''
74  var = 'pyvar_'+self._cppyy.gbl.ana.UniqueName()
75  text = '#include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"\ndouble '+var+'_func(const caf::SRSpillProxy* srp){\nconst caf::SRSpillProxy& sr = *srp;\n'+body+'\n}\nconst ana::Var '+var+'('+var+'_func);'
76  self._cppyy.cppdef(text)
77  return getattr(self._cppyy.gbl, var)
78 
79  def SimpleSliceVar(self, name):
80  '''Equivalent of the SIMPLEVAR() macro'''
81  return self.CSliceVar('return sr.'+name+';')
82 
83  def SimpleSpillVar(self, name):
84  '''Equivalent of the SIMPLEVAR() macro'''
85  return self.CSpillVar('return sr.'+name+';')
86 
87  def CSliceCut(self, body):
88  '''Construct a new Cut given the C++ body as a string'''
89  cut = 'pycut_'+self._cppyy.gbl.ana.UniqueName()
90  text = '#include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"\nbool '+cut+'_func(const caf::SRSliceProxy* srp){\nconst caf::SRSliceProxy& sr = *srp;\n'+body+'\n}\nconst ana::Cut '+cut+'('+cut+'_func);'
91  self._cppyy.cppdef(text)
92  return getattr(self._cppyy.gbl, cut)
93 
94  def CSpillCut(self, body):
95  '''Construct a new Cut given the C++ body as a string'''
96  cut = 'pycut_'+self._cppyy.gbl.ana.UniqueName()
97  text = '#include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"\nbool '+cut+'_func(const caf::SRSpillProxy* srp){\nconst caf::SRSpillProxy& sr = *srp;\n'+body+'\n}\nconst ana::Cut '+cut+'('+cut+'_func);'
98  self._cppyy.cppdef(text)
99  return getattr(self._cppyy.gbl, cut)
100 
101 
102  # If we don't provide it explicitly, assume it's from the ana namespace
103  def __getattr__(self, name):
104  return getattr(self._cppyy.gbl.ana, name)
105 
106 import sys
107 sys.modules['cafana'] = PyCAFAna(cppyy)
def SimpleSliceVar
Definition: cafana.py:79
def __init__
Definition: cafana.py:62
do one_file $F done echo for F in find $TOP name CMakeLists txt print
def CSpillCut
Definition: cafana.py:94
def SimpleSpillVar
Definition: cafana.py:83
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.
def CSliceCut
Definition: cafana.py:87
def CSliceVar
Definition: cafana.py:65
def __getattr__
Definition: cafana.py:103
def CSpillVar
Definition: cafana.py:72