The Gaudi Framework  v32r2 (46d42edc)
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 from __future__ import print_function
14 # =============================================================================
15 __author__ = "Vanya BELYAEV ibelyaev@physics.syr.edu"
16 # =============================================================================
17 __all__ = (
18  'nTuple', # function to book/retrieve N-tuple
19  'getNTuple', # ditto
20  'getNtuple', # ditto
21  'getntuple', # ditto
22  'gettuple', # ditto
23  'activeTuples', # get the list of all active n-tuples
24  'releaseTuples' # release all actibe N-tuples
25 )
26 # =============================================================================
27 
28 from GaudiPython.Bindings import gbl as cpp
29 from GaudiPython.Bindings import AppMgr
31 
32 _Tool = cpp.ITupleTool
33 _Deco = cpp.GaudiPython.TupleToolDecorator
34 
35 # the list of aquired tools (to be released)
36 _TOOLS_ = []
37 
38 # =============================================================================
39 # Helper private auxillary utility to get Tool Service
40 
41 
42 def _getToolSvc(**kwargs):
43  """ Helper private auxillary utility to get Tool Service """
44  svc = kwargs.get('toolSvc', None)
45  if not svc:
46  svc = kwargs.get('toolsvc', None)
47  if not svc:
48  svc = kwargs.get('service', None)
49  if not svc:
50  svc = kwargs.get('svc', None)
51  else:
52  return svc # RETURN
53  gaudi = kwargs.get('gaudi', None)
54  if not gaudi:
55  gaudi = AppMgr()
56  return gaudi.toolsvc() # RETURN
57 
58 
59 # =============================================================================
60 # Retrive N-Tuple ( book on demand )
61 def _nTuple_(s, *args):
62  """ Retrive N-tuple ( book on demand ) """
63  return _Deco.nTuple(s, *args)
64 
65 
66 # =============================================================================
67 _nTuple_.__doc__ += "\n" + _Deco.nTuple.__doc__
68 _Tool.nTuple = _nTuple_
69 _Tool.ntuple = _nTuple_
70 
71 
72 # =============================================================================
73 # Retrieve (book-on-demand) 'Smart'-N-tuple object.
74 def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1'):
75  """
76  Retrieve 'Smart'-N-tuple object.
77  N-tuple is booked on-demand.
78 
79  Atetntion !!
80  The logical unit LUN must be configured by N-Tuple Service
81 
82  Retrieve (book-n-demand) N-Tuple using
83  the directory name and the title:
84  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
85  'N-tuple title' , ## the title for N-Tuple
86  LUN = 'FILE1' ) ## logical file unit
87 
88  Retrieve (book-n-demand) N-Tuple using
89  the directory name, literal ID and the title:
90  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
91  'Tuple1' , ## the literal ID for N-Tuple
92  'N-tuple title' , ## the title for N-Tuple
93  LUN = 'FILE1' ) ## logical file unit
94 
95  Retrieve (book-n-demand) N-Tuple using
96  the directory name, numerical ID and the title:
97  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
98  124 , ## the numerical ID for N-Tuple
99  'N-tuple title' , ## the title for N-Tuple
100  LUN = 'FILE1' ) ## logical file unit
101 
102 
103  """
104  toolSvc = _getToolSvc()
105 
106  # construct the name of the intermediate TupleTool
107  name = 'Tuple' + LUN + "/"
108  if topdir:
109  name += topdir
110  name += dirpath
111  name += "_%s" % ID
112  if ID2:
113  name += "_%s" % ID2
114  name = name.replace('.', '_')
115  name = name.replace('/', '_')
116  name = name.replace('\\', '_')
117  name = name.replace(' ', '_')
118 
119  # define tool properties
120  t0 = GaudiPython.iAlgTool('ToolSvc.' + name)
121  t0.OutputLevel = 1
122  t0.NTupleLUN = LUN
123  t0.NTupleDir = dirpath
124  t0.PropertiesPrint = False
125  t0.OutputLevel = 4
126  if topdir:
127  t0.NTupleTopDir = topdir
128 
129  # get the tool from Tool service
130  tool = toolSvc.create('TupleTool', name, interface=_Tool)
131 
132  # check the properties and redefine them if needed
133  t1 = GaudiPython.iAlgTool(tool.name(), tool)
134  if t1.NTupleLUN != LUN:
135  t1.NTupleLUN = LUN
136  if t1.NTupleDir != dirpath:
137  t1.NTupleDir = dirpath
138  if topdir and (t1.NTupleTopDir != topdir):
139  t1.NTupleTopDir = topdir
140 
141  _TOOLS_.append(tool)
142  if not ID2:
143  return tool.nTuple(ID) # RETURN
144 
145  return tool.nTuple(ID, ID2) # RETURN
146 
147 
148 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
149  + _Tool.nTuple.__doc__
150 
151 ntuple = nTuple
152 getNTuple = nTuple
153 getNtuple = nTuple
154 getntuple = nTuple
155 getTuple = nTuple
156 gettuple = nTuple
157 
158 # =============================================================================
159 # Return the list of active tools
160 
161 
163  """
164  Return the list of active tools
165  """
166  return _TOOLS_
167 
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 
196 
198  """
199  AtExit function for GaudiPython.TupleUtils module
200  """
201  if activeTuples():
202  print('WARNING: the list of local TupleTools is not empty!')
203  print(
204  'WARNING: please use GaudiPython.TupleUtils.releaseTuples() at the end'
205  )
206 
207 
208 import atexit
209 atexit.register(_TupleUtils_AtExit_)
210 
211 # =============================================================================
212 if "__main__" == __name__:
213  import sys
214  print(__doc__, __all__)
215  for o in __all__:
216  print("\n\n\t", o, "\n")
217  print(sys.modules[__name__].__dict__[o].__doc__)
218 
219 # =============================================================================
220 # The end
221 # =============================================================================
def _nTuple_(s, *args)
Definition: TupleUtils.py:61
def _getToolSvc(**kwargs)
Definition: TupleUtils.py:42
def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1')
Definition: TupleUtils.py:74