| The Gaudi Framework v32r1 (f65d50dc)

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
29 import GaudiPython.GaudiAlgs
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', name, interface=_Tool)
130
131 # check the properties and redefine them if needed
132 t1 = GaudiPython.iAlgTool(tool.name(), tool)
133 if t1.NTupleLUN != LUN:
134 t1.NTupleLUN = LUN
135 if t1.NTupleDir != dirpath:
136 t1.NTupleDir = dirpath
137 if topdir and (t1.NTupleTopDir != topdir):
138 t1.NTupleTopDir = topdir
139
140 _TOOLS_.append(tool)
141 if not ID2:
142 return tool.nTuple(ID) # RETURN
143
144 return tool.nTuple(ID, ID2) # RETURN
145
146
147 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
148 + _Tool.nTuple.__doc__
149
150 ntuple = nTuple
151 getNTuple = nTuple
152 getNtuple = nTuple
153 getntuple = nTuple
154 getTuple = nTuple
155 gettuple = nTuple
156
157 # =============================================================================
158 # Return the list of active tools
159
160
161 def activeTuples():
162 """
163 Return the list of active tools
164 """
165 return _TOOLS_
166
167
168 # =============================================================================
169 # Release the active tool/tuples
170
171
172 def releaseTuples():
173 """
174 Release the active tool/tuples
175 The method needs to be invoked explicitely at the end of the job
176 """
177 if not _TOOLS_:
178 return
179 from GaudiPython.Bindings import _gaudi
180 if not _gaudi:
181 return
182
183 toolSvc = _getToolSvc()
184 if toolSvc.isValid():
185 while _TOOLS_:
186 t = _TOOLS_.pop()
187 if not t:
188 continue
189 while 1 < t.refCount():
190 toolSvc._its.releaseTool(t)
191
192
193 # =============================================================================
194
195
196 def _TupleUtils_AtExit_():
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 # =============================================================================
GaudiPython.TupleUtils.releaseTuples
def releaseTuples()
Definition: TupleUtils.py:172
GaudiPython.TupleUtils._nTuple_
def _nTuple_(s, *args)
Definition: TupleUtils.py:60
GaudiPython.TupleUtils._getToolSvc
def _getToolSvc(**kwargs)
Definition: TupleUtils.py:41
GaudiPython.GaudiAlgs
Definition: GaudiAlgs.py:1
GaudiPython.TupleUtils.activeTuples
def activeTuples()
Definition: TupleUtils.py:161
GaudiPython.Bindings
Definition: Bindings.py:1
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:913
GaudiPython.TupleUtils.nTuple
def nTuple(dirpath, ID, ID2=None, topdir=None, LUN='FILE1')
Definition: TupleUtils.py:73
GaudiPython.TupleUtils._TupleUtils_AtExit_
def _TupleUtils_AtExit_()
Definition: TupleUtils.py:196
GaudiPython.Bindings.iAlgTool
Definition: Bindings.py:479