All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 def _getToolSvc( **kwargs ) :
40  """ Helper private auxillary utility to get Tool Service """
41  svc = kwargs.get ( 'toolSvc' , None )
42  if not svc : svc = kwargs.get ( 'toolsvc' , None )
43  if not svc : svc = kwargs.get ( 'service' , None )
44  if not svc : svc = kwargs.get ( 'svc' , None )
45  else : return svc ## RETURN
46  gaudi = kwargs.get ( 'gaudi' , None )
47  if not gaudi : gaudi = AppMgr()
48  return gaudi.toolsvc() ## RETURN
49 
50 
51 # =============================================================================
52 ## Retrive N-Tuple ( book on demand )
53 def _nTuple_ ( s , *args ) :
54  """ Retrive N-tuple ( book on demand ) """
55  print 'ARGS:' , args
56  return _Deco.nTuple ( s , *args)
57 
58 # =============================================================================
59 _nTuple_. __doc__ += "\n" + _Deco.nTuple . __doc__
60 _Tool.nTuple = _nTuple_
61 _Tool.ntuple = _nTuple_
62 
63 
64 # =============================================================================
65 ## Retrieve (book-on-demand) 'Smart'-N-tuple object.
66 def nTuple ( dirpath , ID , ID2 = None , topdir = None , LUN = 'FILE1' ) :
67  """
68  Retrieve 'Smart'-N-tuple object.
69  N-tuple is booked on-demand.
70 
71  Atetntion !!
72  The logical unit LUN must be configured by N-Tuple Service
73 
74  Retrieve (book-n-demand) N-Tuple using
75  the directory name and the title:
76  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
77  'N-tuple title' , ## the title for N-Tuple
78  LUN = 'FILE1' ) ## logical file unit
79 
80  Retrieve (book-n-demand) N-Tuple using
81  the directory name, literal ID and the title:
82  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
83  'Tuple1' , ## the literal ID for N-Tuple
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, numerical ID and the title:
89  >>> t = nTuple ( 'the/path/to/directory' , ## the path to the directory
90  124 , ## the numerical ID for N-Tuple
91  'N-tuple title' , ## the title for N-Tuple
92  LUN = 'FILE1' ) ## logical file unit
93 
94 
95  """
96  toolSvc = _getToolSvc ()
97 
98  # construct the name of the intermediate TupleTool
99  name = 'Tuple'+LUN+"/"
100  if topdir : name += topdir
101  name += dirpath
102  name += "_%s"%ID
103  if ID2 : name += "_%s"%ID2
104  name=name.replace ( '.' , '_' )
105  name=name.replace ( '/' , '_' )
106  name=name.replace ( '\\' , '_' )
107  name=name.replace ( ' ' , '_' )
108 
109  ## define tool properties
110  t0 = GaudiPython.iAlgTool( 'ToolSvc.'+name )
111  t0.OutputLevel = 1
112  t0.NTupleLUN = LUN
113  t0.NTupleDir = dirpath
114  t0.PropertiesPrint = False
115  t0.OutputLevel = 4
116  if topdir : t0.NTupleTopDir = topdir
117 
118  ## get the tool from Tool service
119  tool = toolSvc.create ( 'TupleTool' ,
120  name ,
121  interface = _Tool )
122 
123  ## check the properties and redefine them if needed
124  t1 = GaudiPython.iAlgTool ( tool.name() , tool )
125  if t1.NTupleLUN != LUN : t1.NTupleLUN = LUN
126  if t1.NTupleDir != dirpath : t1.NTupleDir = dirpath
127  if topdir and ( t1.NTupleTopDir != topdir ) :
128  t1.NTupleTopDir = topdir
129 
130  _TOOLS_.append ( tool )
131  if not ID2 : return tool.nTuple ( ID ) ## RETURN
132 
133  return tool.nTuple ( ID , ID2 ) ## RETURN
134 
135 
136 nTuple . __doc__ += "\n\t help(ITupleTool.nTuple) : \n" \
137  + _Tool.nTuple.__doc__
138 
139 ntuple = nTuple
140 getNTuple = nTuple
141 getNtuple = nTuple
142 getntuple = nTuple
143 getTuple = nTuple
144 gettuple = nTuple
145 
146 # =============================================================================
147 ## Return the list of active tools
148 def activeTuples () :
149  """
150  Return the list of active tools
151  """
152  return _TOOLS_
153 
154 # =============================================================================
155 ## Release the active tool/tuples
157  """
158  Release the active tool/tuples
159  The method needs to be invoked explicitely at the end of the job
160  """
161  if not _TOOLS_ : return
162  print ' %s/%s: release all pending ITupleTools: %s' % ( __file__ ,
163  __name__ ,
164  len(_TOOLS_) )
165  from GaudiPython.Bindings import _gaudi
166  if not _gaudi : return
167 
168  toolSvc = _getToolSvc()
169  if toolSvc.isValid() :
170  while _TOOLS_ :
171  t = _TOOLS_.pop()
172  if not t : continue
173  while 1 < t.refCount() :
174  toolSvc._its.releaseTool( t )
175 
176 # =============================================================================
178  """
179  AtExit function for GaudiPython.TupleUtils module
180  """
181  if activeTuples() :
182  print 'WARNING: the list of local TupleTools is not empty!'
183  print 'WARNING: please use GaudiPython.TupleUtils.releaseTuples() at the end'
184 
185 import atexit
186 atexit.register ( _TupleUtils_AtExit_ )
187 
188 # =============================================================================
189 if "__main__" == __name__ :
190  import sys
191  print __doc__ , __all__
192  for o in __all__ :
193  print "\n\n\t",o,"\n"
194  print sys.modules[__name__].__dict__[o].__doc__
195 
196 # =============================================================================
197 # The end
198 # =============================================================================
def activeTuples
Return the list of active tools.
Definition: TupleUtils.py:148
def releaseTuples
Release the active tool/tuples.
Definition: TupleUtils.py:156
def nTuple
Retrieve (book-on-demand) 'Smart'-N-tuple object.
Definition: TupleUtils.py:66
def _getToolSvc
Helper private auxillary utility to get Tool Service.
Definition: TupleUtils.py:39
def _nTuple_
Retrive N-Tuple ( book on demand )
Definition: TupleUtils.py:53