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