The Gaudi Framework  master (fb0007c6)
Loading...
Searching...
No Matches
decorators.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
19"""
20The set of basic decorators for objects from GaudiPartProp package
21"""
22
23# =============================================================================
24__author__ = "Vanya BELYAEV <Ivan.Belyaev@nikhef.nl>"
25__version__ = ""
26# =============================================================================
27
28import GaudiPython
29
30# namespaces shortcuts
31cpp = GaudiPython.gbl
32std = GaudiPython.gbl.std
33Decays = GaudiPython.gbl.Gaudi.Decays
34Gaudi = GaudiPython.gbl.Gaudi
35
36
37
38def _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
53Gaudi.Interfaces.IParticlePropertySvc.all = _get_all_
54
55
56
57def _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
72def _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
94Gaudi.Interfaces.IParticlePropertySvc.get = _get_pp_
95Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.get = _get_ppv_
96Gaudi.Interfaces.IParticlePropertySvc.__len__ = (
97 Gaudi.Interfaces.IParticlePropertySvc.size
98)
99
100
101# =============================================================================
102
103def _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
123def _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
140def _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
152def _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
167Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.toList = _ppv_2_list_
168Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.toLst = _ppv_2_list_
169
170Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.fromList = _ppv_from_lst_
171Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.fromLst = _ppv_from_lst_
172
173Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__repr__ = (
174 lambda s: s.toList().__repr__()
175)
176Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__str__ = _prnt_as_table_
177Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.__len__ = (
178 Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties.size
179)
180
181
182Gaudi.ParticleProperty.__str__ = Gaudi.ParticleProperty.toString
183Gaudi.ParticleProperty.__repr__ = Gaudi.ParticleProperty.toString
184
185
186Gaudi.ParticleProperty.__getattr__ = _get_attr_from_PID_
187
188
189Gaudi.ParticleID.__str__ = Gaudi.ParticleID.toString
190Gaudi.ParticleID.__repr__ = Gaudi.ParticleID.toString
191
192
193def _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
201def _pp_lt_(pp1, pp2):
202 """Comparison `<` for ParticleProperty objects."""
203 return _pp_cmp_(pp1, pp2) < 0
204
205
206Gaudi.ParticleProperty.__cmp__ = _pp_cmp_
207Gaudi.ParticleProperty.__lt__ = _pp_lt_
208
209
210
211def _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
225def _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
245Gaudi.ParticleID.__abs__ = _abs_1_
246Gaudi.ParticleProperty.__abs__ = _abs_2_
247
248
249Gaudi.ParticleIDs = std.vector(Gaudi.ParticleID)
250
251
252
253def _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
274Gaudi.ParticleIDs.toList = _ppv_2_list_
275Gaudi.ParticleIDs.toLst = _ppv_2_list_
276
277Gaudi.ParticleIDs.fromList = _ppv_from_lst_
278Gaudi.ParticleIDs.fromLst = _ppv_from_lst_
279
280Gaudi.ParticleIDs.__repr__ = lambda s: s.toList().__repr__()
281Gaudi.ParticleIDs.__str__ = lambda s: s.toList().__str__()
282
283Gaudi.ParticleIDs.get = _get_pid_
284
285Decays.Decay.Item.__str__ = lambda s: s.name()
286Decays.Decay.Item.__repr__ = lambda s: s.name()
287
288Decays.Decay.__str__ = lambda s: s.toString()
289Decays.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__()
296_decays = std.vector(Decays.Decay)
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
302Gaudi.Interfaces.IParticlePropertySvc.ParticleIDs = Gaudi.ParticleIDs
303Gaudi.Interfaces.IParticlePropertySvc.Decays = _decays
304Gaudi.Interfaces.IParticlePropertySvc.Items = _items
305Gaudi.Interfaces.IParticlePropertySvc.Decay = Decays.Decay
306Gaudi.Interfaces.IParticlePropertySvc.Item = Decays.Decay.Item
307
308
309def _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
331def _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
347def _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
362Decays.Symbols.symbols = _symbols_
363Decays.Symbols.__iter__ = _sym_iter_
364
365
366Symbols = Decays.Symbols.instance()
the helper representation of the item in the decay chain
Definition Decay.h:39
The simple representation of "simple 1-step" decay (there are no trees!
Definition Decay.h:32
std::vector< const Gaudi::ParticleProperty * > ParticleProperties
the actual type of (ordered) container of particle properties
Holds PDG + LHCb extension particle code, following the PDG particle numbering scheme (pdg....
Definition ParticleID.h:43
Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties allProperties(const Gaudi::Interfaces::IParticlePropertySvc *service)
get all the properties at once
GAUDI_API std::string printAsTable(const std::vector< const Gaudi::ParticleProperty * > &particles, const Gaudi::Interfaces::IParticlePropertySvc *service=0)
print a list of properties in a form of the table
_ppv_2_list_(self)
Convert Gaudi.Interfaces.IParticlePropertySvc.ParticleProperties into python list.
_ppv_from_lst_(self, lst)
convert python list into Gaudi.IParticlePropertisvc.ParticleProperties
_get_ppv_(self, cut, asList=False)
simple "get" method for the service
Definition decorators.py:72
_get_attr_from_PID_(self, attr)
__repr__
decorate the vector of properties
_get_pp_(self, cut, asList=False)
simple "get" method for the service
Definition decorators.py:57
__str__
decorate the printout for Gaudi.ParticleProperty
_get_pid_(self, cut)
get particleID objects whcih satisfy some criteria
_get_all_(self, asList=False)
get all particle properties form the service
Definition decorators.py:38
_abs_2_(self)
abs for ParticleProperty
_prnt_as_table_(self, *args)
Print vector of particle properties in a form of table.
_abs_1_(self)
abs for ParticleID
The comparison criteria for particle properties.