cafana.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 if __name__ == '__main__':
4  print("This is the source file that implements the cafana python module.")
5  print("You should 'import cafana' at the top of a .py script")
6  print("and execute it with 'cafe myscript.py'")
7  exit(1)
8 
9 import os
10 
11 import ROOT
12 
13 ROOT.gApplication.ExecuteFile('$SRT_PUBLIC_CONTEXT/Utilities/rootlogon.C')
14 print(' ...python edition')
15 ROOT.gROOT.ForceStyle()
16 
17 libs = ['Minuit2',
18  'StandardRecord',
19  'StandardRecord_dict',
20  'StandardRecordProxy',
21  'NOvARwgt',
22  'StandardRecordNOvARwgt',
23  'CAFAnaCoreExt',
24  'CAFAnaCore',
25  'CAFAnaVars',
26  'CAFAnaCuts']
27 
28 dicts = ['CAFAnaCore', 'CAFAnaVars', 'CAFAnaCuts']
29 
30 
31 if 'PYCAFANA_LOAD_NDANA' in os.environ: dicts += ['NDAna']
32 
33 if 'PYCAFANA_LOAD_3FLAV' in os.environ:
34  libs += ['3FlavorAnaVars', '3FlavorAnaCuts']
35  dicts += ['3FlavorAnaVars', '3FlavorAnaCuts']
36 
37 
38 print('Loading libraries...')
39 for lib in libs:
40  print(' ', lib)
41  ROOT.gSystem.Load('lib'+lib+'.so')
42 
43 import cppyy
44 
45 print('Loading dictionaries...')
46 for d in dicts:
47  print(' ', d)
48  cppyy.load_reflection_info('lib'+d+'_dict.so')
49 
50 
51 # Engineer things so that all the ana namespace contents are directly exposed
52 # as the cafana module - plus a few utility functions of our own.
53 class PyCAFAna:
54  def __init__(self, cppyy):
55  self._cppyy = cppyy
56 
57  # # Embed the osc namespace within cafana so it can be found
58  # self.osc = self._cppyy.gbl.osc
59  # self.caf = self._cppyy.gbl.caf
60 
61  # # Monkey-patch the constructor of Fitter to convert its vector args
62  # orig = self.MinuitFitter.__init__
63  # def monkey_init(fitself, expt, vs, systs = [], prec = self.MinuitFitter.kNormal):
64  # orig(fitself, expt,
65  # self.convert_to_vector(vs, 'const ana::IFitVar*'),
66  # self.convert_to_vector(systs, 'const ana::ISyst*'),
67  # prec)
68  # self.MinuitFitter.__init__ = monkey_init
69 
70 
71  def convert_to_vector(self, elems, elemtype):
72  '''Helper function to convert a python list to a std::vector'''
73  ret = self._cppyy.gbl.std.vector(elemtype)()
74  for e in elems: ret.push_back(e)
75  return ret
76 
77 
78  def CVar(self, body):
79  '''Construct a new Var given the C++ body as a string'''
80  var = 'pyvar_'+self._cppyy.gbl.ana.UniqueName()
81  text = '#include "StandardRecord/Proxy/SRProxy.h"\ndouble '+var+'_func(const caf::SRProxy* srp){\nconst caf::SRProxy& sr = *srp;\n'+body+'\n}\nconst ana::Var '+var+'('+var+'_func);'
82  self._cppyy.cppdef(text)
83  return getattr(self._cppyy.gbl, var)
84 
85 
86  def SimpleVar(self, name):
87  '''Equivalent of the SIMPLEVAR() macro'''
88  return self.CVar('return sr.'+name+';')
89 
90 
91  def CCut(self, body):
92  '''Construct a new Cut given the C++ body as a string'''
93  cut = 'pycut_'+self._cppyy.gbl.ana.UniqueName()
94  text = '#include "StandardRecord/Proxy/SRProxy.h"\nbool '+cut+'_func(const caf::SRProxy* srp){\nconst caf::SRProxy& sr = *srp;\n'+body+'\n}\nconst ana::Cut '+cut+'('+cut+'_func);'
95  self._cppyy.cppdef(text)
96  return getattr(self._cppyy.gbl, cut)
97 
98 
99  # If we don't provide it explicitly, assume it's from the ana namespace
100  def __getattr__(self, name):
101  return getattr(self._cppyy.gbl.ana, name)
102 
103 
104 import sys
105 sys.modules['cafana'] = PyCAFAna(cppyy)
def convert_to_vector(self, elems, elemtype)
Definition: cafana.py:71
def CCut(self, body)
Definition: cafana.py:91
def __init__(self, cppyy)
Definition: cafana.py:54
bool print
def CVar(self, body)
Definition: cafana.py:78
exit(0)
def __getattr__(self, name)
Definition: cafana.py:100
def SimpleVar(self, name)
Definition: cafana.py:86