The Gaudi Framework  master (37c0b60a)
decorators.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
19 """
20 The set of basic decorators for objects from GaudiPartProp package
21 """
22 
23 # =============================================================================
24 __author__ = "Vanya BELYAEV <Ivan.Belyaev@nikhef.nl>"
25 __version__ = ""
26 # =============================================================================
27 
28 import GaudiPython
29 
30 # namespaces shortcuts
31 cpp = GaudiPython.gbl
32 std = GaudiPython.gbl.std
33 Decays = GaudiPython.gbl.Gaudi.Decays
34 Gaudi = GaudiPython.gbl.Gaudi
35 
36 
37 
38 def _get_all_(self, asList=False):
39  """
40  Get all particle properties from the service
41 
42  >>> svc = ... # get the service
43  >>> all = svc.all () # get all properties
44 
45  """
47  if not asList:
48  return _all
49  return _all.toList()
50 
51 
52 
53 Gaudi.Interfaces.IParticlePropertySvc.all = _get_all_
54 
55 
56 
57 def _get_pp_(self, cut, asList=False):
58  """
59  Simple 'get' method for Gaudi::Interfaces::IParticlePropertySvc
60  service to extract the properties which satisfy some criteria
61 
62  >>> svc = ... # get service (Gaudi::Interfaces::IParticlePropertySvc) or vector
63  >>> leptons = svc.get ( lambda s : s.pid().isLepton() ) # get all leptons
64  >>> longlived = svc.get ( lambda s : s.ctau() > 0.001 ) # get longlived
65 
66  """
67  _all = self.all()
68  return _all.get(cut, asList)
69 
70 
71 
72 def _get_ppv_(self, cut, asList=False):
73  """
74  Simple 'get' method for Gaudi::Interfaces::IParticlePropertySvc
75  service to extract the properties which satisfy some criteria
76 
77  >>> svc = ... # get service (Gaudi::Interfaces::IParticlePropertySvc) or vector
78  >>> leptons = svc.get ( lambda s : s.pid().isLepton() ) # get all leptons
79  >>> longlived = svc.get ( lambda s : s.ctau() > 0.001 ) # get longlived
80 
81  """
82  result = []
83  for pp in self:
84  if cut(pp):
85  result.append(pp)
86  if asList:
87  return result
89  vct.fromList(result)
90  return vct
91 
92 
93 # ## decorate service
94 Gaudi.Interfaces.IParticlePropertySvc.get = _get_pp_
95 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.get = _get_ppv_
96 Gaudi.Interfaces.IParticlePropertySvc.__len__ = (
97  Gaudi.Interfaces.IParticlePropertySvc.size
98 )
99 
100 
101 # =============================================================================
102 
103 def _ppv_2_list_(self):
104  """
105  Convert Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties into python list
106 
107  >>> ppv = ... # get the vector
108  >>> lst = ppv.toList () # convert it to the list
109 
110  """
111  result = []
112  size = self.size()
113  index = 0
114  while index < size:
115  pp = self.at(index)
116  result.append(pp)
117  index += 1
118  return result
119 
120 
121 # ==============================================================================
122 
123 def _ppv_from_lst_(self, lst):
124  """
125  Append the iterable sequence 'lst' to the vector of
126  particle properties:
127 
128  >>> Vct = std.vector('const Gaudi::ParticleProperty*')
129  >>> lst = [ pp1 , pp2 , pp3 ]
130  >>> vct = Vct()
131  >>> vct.fromList ( lst )
132 
133  """
134  for item in lst:
135  self.push_back(item)
136 
137 
138 # =============================================================================
139 
140 def _prnt_as_table_(self, *args):
141  """
142  Print vector of particle properties in a form of table
143 
144  >>> print vct.asTable()
145 
146  """
147  return Gaudi.ParticleProperties.printAsTable(self, *args)
148 
149 
150 # ============================================================================
151 # delegate the evaluation of unknown atrributes to Gaudi.ParticleID class
152 def _get_attr_from_PID_(self, attr):
153  """
154  Delegate the evaluation of unknown atrributes to Gaudi.ParticleID class
155 
156  >>> pp = ...
157  >>> print pp.jSpin()
158 
159  """
160  _pid = self.pid()
161  if hasattr(_pid, attr):
162  return getattr(_pid, attr)
163  raise AttributeError("Unknown attribute: %s " % attr)
164 
165 
166 
167 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.toList = _ppv_2_list_
168 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.toLst = _ppv_2_list_
169 
170 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.fromList = _ppv_from_lst_
171 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.fromLst = _ppv_from_lst_
172 
173 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__repr__ = (
174  lambda s: s.toList().__repr__()
175 )
176 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__str__ = _prnt_as_table_
177 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__len__ = (
178  Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.size
179 )
180 
181 
182 Gaudi.ParticleProperty.__str__ = Gaudi.ParticleProperty.toString
183 Gaudi.ParticleProperty.__repr__ = Gaudi.ParticleProperty.toString
184 
185 
186 Gaudi.ParticleProperty.__getattr__ = _get_attr_from_PID_
187 
188 
189 Gaudi.ParticleID.__str__ = Gaudi.ParticleID.toString
190 Gaudi.ParticleID.__repr__ = Gaudi.ParticleID.toString
191 
192 
193 def _pp_cmp_(pp1, pp2):
194  """Comparison for ParticleProperty objects."""
195  if pp1 == pp2:
196  return 0
197  comparator = Gaudi.ParticleProperty.Compare()
198  return -1 if comparator(pp1, pp2) else 1
199 
200 
201 def _pp_lt_(pp1, pp2):
202  """Comparison `<` for ParticleProperty objects."""
203  return _pp_cmp_(pp1, pp2) < 0
204 
205 
206 Gaudi.ParticleProperty.__cmp__ = _pp_cmp_
207 Gaudi.ParticleProperty.__lt__ = _pp_lt_
208 
209 
210 
211 def _abs_1_(self):
212  """
213  Absolute value for the PID
214 
215  >>> p = ...
216  >>> pa = abs ( p )
217 
218  """
219  if 0 <= self.pid():
220  return self
221  return Gaudi.ParticleID(self.abspid())
222 
223 
224 
225 def _abs_2_(self):
226  """
227  Absolute value for the ParticleProperty
228 
229  >>> p = ...
230  >>> pa = abs ( p )
231 
232  """
233 
234  _pid = self.particleID()
235  if self.selfcc() or 0 <= _pid.pid():
236  return self
237 
238  _anti = self.anti()
239  if _anti and 0 <= _anti.particleID().pid():
240  return _anti
241 
242  return self
243 
244 
245 Gaudi.ParticleID.__abs__ = _abs_1_
246 Gaudi.ParticleProperty.__abs__ = _abs_2_
247 
248 
249 Gaudi.ParticleIDs = std.vector(Gaudi.ParticleID)
250 
251 
252 
253 def _get_pid_(self, cut):
254  """
255  Get particleID objects which satisfy some criteria
256 
257  >>> pids = ... # vector of LCHb::ParticleID objects
258  >>> good = pids.get( lambda s : s.isLepton() ) # get leptons
259  >>> scalar = pids.get( lambda s : 1 == s.jSpin() ) # get scalars
260 
261  """
262  size = self.size()
263  index = 0
264  result = []
265  while index < size:
266  pid = self.at(index)
267  if cut(pid):
268  result.append(pid)
269  index += 1
270  return result
271 
272 
273 
274 Gaudi.ParticleIDs.toList = _ppv_2_list_
275 Gaudi.ParticleIDs.toLst = _ppv_2_list_
276 
277 Gaudi.ParticleIDs.fromList = _ppv_from_lst_
278 Gaudi.ParticleIDs.fromLst = _ppv_from_lst_
279 
280 Gaudi.ParticleIDs.__repr__ = lambda s: s.toList().__repr__()
281 Gaudi.ParticleIDs.__str__ = lambda s: s.toList().__str__()
282 
283 Gaudi.ParticleIDs.get = _get_pid_
284 
285 Decays.Decay.Item.__str__ = lambda s: s.name()
286 Decays.Decay.Item.__repr__ = lambda s: s.name()
287 
288 Decays.Decay.__str__ = lambda s: s.toString()
289 Decays.Decay.__repr__ = lambda s: s.toString()
290 
291 _items = std.vector(Decays.Decay.Item)
292 _items.toList = lambda s: [i for i in s]
293 _items.toLst = lambda s: [i for i in s]
294 _items.__str__ = lambda s: s.toList().__str__()
295 _items.__repr__ = lambda s: s.toList().__repr__()
297 _decays.toList = lambda s: [i for i in s]
298 _decays.toLst = lambda s: [i for i in s]
299 _decays.__str__ = lambda s: s.toList().__str__()
300 _decays.__repr__ = lambda s: s.toList().__repr__()
301 
302 Gaudi.Interfaces.IParticlePropertySvc.ParticleIDs = Gaudi.ParticleIDs
303 Gaudi.Interfaces.IParticlePropertySvc.Decays = _decays
304 Gaudi.Interfaces.IParticlePropertySvc.Items = _items
305 Gaudi.Interfaces.IParticlePropertySvc.Decay = Decays.Decay
306 Gaudi.Interfaces.IParticlePropertySvc.Item = Decays.Decay.Item
307 
308 
309 def _validate_(self, svc):
310  """
311  Validate the vector of items/decays
312 
313  >>> vct = ... # get the vector of items/decays
314  >>> svc = ... # get the service
315  >>> vcs.vaildate ( svc ) # validate
316  """
317  for o in self:
318  sc = o.validate(svc)
319  if sc.isFailure():
320  return sc
321  return cpp.StatusCode(cpp.StatusCode.SUCCESS)
322 
323 
324 _decays.validate = _validate_
325 _items.validate = _validate_
326 
327 _old_symbols_ = Decays.Symbols.symbols
328 _old_particles_ = Decays.Symbols.particles
329 
330 
331 def _symbols_(self):
332  """
333  Get all known predefined symbols:
334 
335  >>> syms = ... # get the table of symbols
336  >>> s = syms.symbols() # get the list of symbols
337 
338  """
339  strings = std.vector("std::string")()
340  _old_symbols_(self, strings)
341  res = []
342  for s in strings:
343  res.append(s)
344  return res
345 
346 
347 def _sym_iter_(self):
348  """
349  Iteration over all known symbols
350 
351  >>> syms = ... # get the table of symbols
352  >>> for s in syms :
353  ... print ' help for %s :' % s , syms.symbol ( s )
354  """
355  _list = self.symbols()
356  _i = 0
357  while _i < len(_list):
358  yield _list[_i]
359  _i += 1
360 
361 
362 Decays.Symbols.symbols = _symbols_
363 Decays.Symbols.__iter__ = _sym_iter_
364 
365 
366 Symbols = Decays.Symbols.instance()
GaudiPartProp.decorators._sym_iter_
def _sym_iter_(self)
Definition: decorators.py:347
Gaudi::ParticleProperties::allProperties
Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties allProperties(const Gaudi::Interfaces::IParticlePropertySvc *service)
get all the properties at once
Definition: IParticlePropertySvc.cpp:201
Gaudi::Decays::Decay
Definition: Decay.h:32
std::vector
STL class.
GaudiPartProp.decorators._abs_1_
def _abs_1_(self)
abs for ParticleID
Definition: decorators.py:211
GaudiPartProp.decorators._abs_2_
def _abs_2_(self)
abs for ParticleProperty
Definition: decorators.py:225
GaudiPartProp.decorators.__repr__
__repr__
decorate the vector of properties
Definition: decorators.py:173
GaudiPartProp.decorators._get_ppv_
def _get_ppv_(self, cut, asList=False)
simple "get" method for the service
Definition: decorators.py:72
GaudiPartProp.decorators._prnt_as_table_
def _prnt_as_table_(self, *args)
Print vector of particle properties in a form of table.
Definition: decorators.py:140
GaudiPartProp.decorators._old_symbols_
_old_symbols_
Definition: decorators.py:327
GaudiPartProp.decorators._symbols_
def _symbols_(self)
Definition: decorators.py:331
GaudiPartProp.decorators._get_pid_
def _get_pid_(self, cut)
get particleID objects whcih satisfy some criteria
Definition: decorators.py:253
GaudiPartProp.decorators._pp_lt_
def _pp_lt_(pp1, pp2)
Definition: decorators.py:201
GaudiPartProp.decorators._pp_cmp_
def _pp_cmp_(pp1, pp2)
Definition: decorators.py:193
GaudiPartProp.decorators._get_pp_
def _get_pp_(self, cut, asList=False)
simple "get" method for the service
Definition: decorators.py:57
GaudiPartProp.decorators._get_attr_from_PID_
def _get_attr_from_PID_(self, attr)
Definition: decorators.py:152
GaudiPartProp.decorators._ppv_2_list_
def _ppv_2_list_(self)
Convert Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties into python list.
Definition: decorators.py:103
GaudiPartProp.decorators._get_all_
def _get_all_(self, asList=False)
get all particle properties form the service
Definition: decorators.py:38
GaudiPartProp.decorators._validate_
def _validate_(self, svc)
Definition: decorators.py:309
GaudiPartProp.decorators.__str__
__str__
decorate the printout for Gaudi::ParticleProperty
Definition: decorators.py:176
Gaudi::ParticleID
Definition: ParticleID.h:43
Gaudi::ParticleProperty::Compare
Definition: ParticleProperty.h:44
Gaudi::ParticleProperties::printAsTable
MsgStream & printAsTable(const std::set< Gaudi::ParticleID, C_, A_ > &particles, MsgStream &stream, const Gaudi::Interfaces::IParticlePropertySvc *service=0)
Definition: ParticleProperty.h:394
GaudiPartProp.decorators._ppv_from_lst_
def _ppv_from_lst_(self, lst)
convert python list into Gaudi.IParticlePropertisvc.ParticleProperties
Definition: decorators.py:123