The Gaudi Framework  v30r3 (a5ef0a68)
TupleUtils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # =============================================================================
3 # This module contains set of simple and useful utilities to booking and
4 # manipulation with N-Tuples (in the spirit of GaudiTuples<TYPE>)
5 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
6 # @date 2007-08-04
7 # =============================================================================
8 """
9 This module contains set of simple and useful utilities to booking and
10 manipulation with N-Tuples (in the spirit of GaudiTuples<TYPE>)
11 
12 """
13 # =============================================================================
14 __author__ = "Vanya BELYAEV ibelyaev@physics.syr.edu"
15 # =============================================================================
16 __all__ = (
17  'nTuple', # function to book/retrieve N-tuple
18  'getNTuple', # ditto
19  'getNtuple', # ditto
20  'getntuple', # ditto
21  'gettuple', # ditto
22  'activeTuples', # get the list of all active n-tuples
23  'releaseTuples' # release all actibe N-tuples
24 )
25 # =============================================================================
26 
27 from GaudiPython.Bindings import gbl as cpp
28 from GaudiPython.Bindings import AppMgr
30 
31 _Tool = cpp.ITupleTool
32 _Deco = cpp.GaudiPython.TupleToolDecorator
33 
34 # the list of aquired tools (to be released)
35 _TOOLS_ = []
36 
37 # =============================================================================
38 # Helper private auxillary utility to get Tool Service
39 
40 
41 def _getToolSvc(**kwargs):
42  """ Helper private auxillary utility to get Tool Service """
43  svc = kwargs.get('toolSvc', None)
44  if not svc:
45  svc = kwargs.get('toolsvc', None)
46  if not svc:
47  svc = kwargs.get('service', None)
48  if not svc:
49  svc = kwargs.get('svc', None)
50  else:
51  return svc # RETURN
52  gaudi = kwargs.get('gaudi', None)
53  if not gaudi:
54  gaudi = AppMgr()
55  return gaudi.toolsvc() # RETURN
56 
57 
58 # =============================================================================
59 # Retrive N-Tuple ( book on demand )
60 def _nTuple_(s, *args):
61  """ Retrive N-tuple ( book on demand ) """
62  return _Deco.nTuple(s, *args)
63 
64 
65 # =============================================================================
66 _nTuple_. __doc__ += "\n" + _Deco.nTuple . __doc__
67 _Tool.nTuple = _nTuple_
68 _Tool.ntuple = _nTuple_
69 
70 
71 # =============================================================================
72 # Retrieve (book-on-demand) 'Smart'-N-tuple object.
73 def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1'):
74  """
75  Retrieve 'Smart'-N-tuple object.
76  N-tuple is booked on-demand.
77 
78  Atetntion !!
79  The logical unit LUN must be configured by N-Tuple Service
80 
81  Retrieve (book-n-demand) N-Tuple using
82  the directory name and the title:
83  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
84  'N-tuple title' , ## the title for N-Tuple
85  LUN = 'FILE1' ) ## logical file unit
86 
87  Retrieve (book-n-demand) N-Tuple using
88  the directory name, literal ID and the title:
89  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
90  'Tuple1' , ## the literal ID for N-Tuple
91  'N-tuple title' , ## the title for N-Tuple
92  LUN = 'FILE1' ) ## logical file unit
93 
94  Retrieve (book-n-demand) N-Tuple using
95  the directory name, numerical ID and the title:
96  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
97  124 , ## the numerical ID for N-Tuple
98  'N-tuple title' , ## the title for N-Tuple
99  LUN = 'FILE1' ) ## logical file unit
100 
101 
102  """
103  toolSvc = _getToolSvc()
104 
105  # construct the name of the intermediate TupleTool
106  name = 'Tuple' + LUN + "/"
107  if topdir:
108  name += topdir
109  name += dirpath
110  name += "_%s" % ID
111  if ID2:
112  name += "_%s" % ID2
113  name = name.replace('.', '_')
114  name = name.replace('/', '_')
115  name = name.replace('\\', '_')
116  name = name.replace(' ', '_')
117 
118  # define tool properties
119  t0 = GaudiPython.iAlgTool('ToolSvc.' + name)
120  t0.OutputLevel = 1
121  t0.NTupleLUN = LUN
122  t0.NTupleDir = dirpath
123  t0.PropertiesPrint = False
124  t0.OutputLevel = 4
125  if topdir:
126  t0.NTupleTopDir = topdir
127 
128  # get the tool from Tool service
129  tool = toolSvc.create('TupleTool',
130  name,
131  interface=_Tool)
132 
133  # check the properties and redefine them if needed
134  t1 = GaudiPython.iAlgTool(tool.name(), tool)
135  if t1.NTupleLUN != LUN:
136  t1.NTupleLUN = LUN
137  if t1.NTupleDir != dirpath:
138  t1.NTupleDir = dirpath
139  if topdir and (t1.NTupleTopDir != topdir):
140  t1.NTupleTopDir = topdir
141 
142  _TOOLS_.append(tool)
143  if not ID2:
144  return tool.nTuple(ID) # RETURN
145 
146  return tool.nTuple(ID, ID2) # RETURN
147 
148 
149 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
150  + _Tool.nTuple.__doc__
151 
152 ntuple = nTuple
153 getNTuple = nTuple
154 getNtuple = nTuple
155 getntuple = nTuple
156 getTuple = nTuple
157 gettuple = nTuple
158 
159 # =============================================================================
160 # Return the list of active tools
161 
162 
164  """
165  Return the list of active tools
166  """
167  return _TOOLS_
168 
169 # =============================================================================
170 # Release the active tool/tuples
171 
172 
174  """
175  Release the active tool/tuples
176  The method needs to be invoked explicitely at the end of the job
177  """
178  if not _TOOLS_:
179  return
180  from GaudiPython.Bindings import _gaudi
181  if not _gaudi:
182  return
183 
184  toolSvc = _getToolSvc()
185  if toolSvc.isValid():
186  while _TOOLS_:
187  t = _TOOLS_.pop()
188  if not t:
189  continue
190  while 1 < t.refCount():
191  toolSvc._its.releaseTool(t)
192 
193 # =============================================================================
194 
195 
197  """
198  AtExit function for GaudiPython.TupleUtils module
199  """
200  if activeTuples():
201  print 'WARNING: the list of local TupleTools is not empty!'
202  print 'WARNING: please use GaudiPython.TupleUtils.releaseTuples() at the end'
203 
204 
205 import atexit
206 atexit.register(_TupleUtils_AtExit_)
207 
208 # =============================================================================
209 if "__main__" == __name__:
210  import sys
211  print __doc__, __all__
212  for o in __all__:
213  print "\n\n\t", o, "\n"
214  print sys.modules[__name__].__dict__[o].__doc__
215 
216 # =============================================================================
217 # The end
218 # =============================================================================
def _getToolSvc(kwargs)
Definition: TupleUtils.py:41
def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1')
Definition: TupleUtils.py:73
def _nTuple_(s, args)
Definition: TupleUtils.py:60