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