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