The Gaudi Framework  master (37c0b60a)
IParticlePropertySvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
12 #include <Gaudi/ParticleID.h>
13 #include <Gaudi/ParticleProperty.h>
14 #include <algorithm>
15 
22 /* helper utility for mapping of Gaudi::ParticleProperty object into
23  * non-negative integral sequential identifier
24  *
25  * This appears to be useful operation, but since it is
26  * "pure technical" it does not appear as interface method.
27  *
28  * For invalid/missing property and/or service
29  * <c>0</c> is returned. The valid result is always
30  * satisfy the condition: <c> index <= service->size() </c>
31  *
32  * @param property the property to be mapped
33  * @param service the service
34  * @return the sequential non-negative index
35  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
36  * @date 2008-08-03
37  */
38 
41  if ( !property || !service ) { return 0; }
42 
43  auto first = service->begin();
44  auto last = service->end();
45  // start the binary_search
46  static constexpr auto cmp = Gaudi::ParticleProperty::Compare();
47  auto ifind = std::lower_bound( first, last, property, cmp );
48  return last != ifind && !cmp( *ifind, property ) ? ( ifind - first + 1 ) : 0;
49 }
50 
51 /* helper utility for mapping of Gaudi::ParticleID object into
52  * non-negative integral sequential identifier
53  *
54  * This appears to be useful operation, but since it is
55  * "pure technical" it does not appear as interface method.
56  *
57  * For invalid/missing PID and/or service
58  * <c>0</c> is returned. The valid result is always
59  * satisfy the condition: <c> index <= service->size() </c>
60  *
61  * @param pid the object to be mapped
62  * @param service the service
63  * @return the sequential non-negative index
64  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
65  * @date 2008-08-03
66  */
69  if ( !service ) { return 0; }
70  const Gaudi::ParticleProperty* pp = service->find( pid );
71  return pp ? Gaudi::ParticleProperties::index( pp, service ) : 0;
72 }
73 
74 /* the inverse mapping of the integer sequential number onto
75  * Gaudi::ParticleID object
76  *
77  * This appears to be useful operation, but since it is
78  * "pure technical" it does not appear as interface method.
79  *
80  * For invalid/missing PID and/or service
81  * <c>NULL</c> is returned.
82  *
83  * @param pid the object to be mapped
84  * @param service the service
85  * @return the sequential non-negative index
86  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
87  * @date 2008-08-03
88  */
91  if ( 0 == index || !service ) { return nullptr; }
92  // get the iterators from the service
93  auto first = service->begin();
94  auto last = service->end();
95  if ( index > (size_t)std::distance( first, last ) ) { return 0; }
96  std::advance( first, index - 1 );
97  return *first;
98 }
99 
100 /* the inverse mapping of the integer sequential number onto
101  * Gaudi::ParticleID object
102  *
103  * This appears to be useful operation, but since it is
104  * "pure technical" it does not appear as interface method.
105  *
106  * For invalid/missing index and/or service
107  * <c>Gaudi::ParticleID()</c> is returned.
108  *
109  * @param pid the object to be mapped
110  * @param service the service
111  * @return the sequential non-negative index
112  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
113  * @date 2008-08-03
114  */
115 
116 const Gaudi::ParticleID
118  if ( 0 == index || !service ) { return Gaudi::ParticleID(); }
120  return pp ? pp->particleID() : Gaudi::ParticleID();
121 }
122 
123 /* mapping by pythiaID
124  *
125  * @code
126  *
127  * const int pythiaID = ... ;
128  *
129  * const Gaudi::Interfaces::IParticlePropertySvc* svc = ... ;
130  *
131  * const Gaudi::ParticleProeprty* pp = byPythiaID( pythiaID , svc ) ;
132  *
133  * @endcode
134  *
135  * @attention the method is not very efficient and should not be abused
136  * @see Gaudi::ParticleProperties::particle
137  * @param pythia pythia identifier
138  * @param svc pointer to particle property service
139  * @return the particle property for the given pythia ID
140  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
141  * @date 2008-08-03
142  */
145  if ( !svc ) { return nullptr; }
146  // to be efficient
147  // 1) try to use PDG-ID (fast, logarithmic search)
148  const Gaudi::ParticleProperty* pp = svc->find( Gaudi::ParticleID( pythia ) );
149  // 2) check the proper pythia ID
150  if ( pp && pythia == pp->pythiaID() ) { return pp; }
151  // 3) use the regular (linear search)
152  auto begin = svc->begin();
153  auto end = svc->end();
154  auto found =
155  std::find_if( begin, end, [&]( const Gaudi::ParticleProperty* pp ) { return pp->pythiaID() == pythia; } );
156  return found != end ? *found : nullptr;
157 }
158 
159 /* mapping by EvtGen-name
160  *
161  * @code
162  *
163  * const std::string& evtGen = ...
164  *
165  * const Gaudi::Interfaces::IParticlePropertySvc* svc = ... ;
166  *
167  * const Gaudi::ParticleProperty* pp = byEvtGenName ( evtGen , svc ) ;
168  *
169  * @endcode
170  *
171  * @attention the method is not very efficient and should not be abused
172  * @see Gaudi::ParticleProperties::particle
173  * @param evtGen the particle naem in EvtGen-generator
174  * @param svc pointer to particle property service
175  * @return the particle property for the given EvtGen-name
176  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
177  * @date 2008-08-03
178  */
182  if ( !svc ) { return nullptr; }
183  // to be more efficient:
184  // 1) try to use the regular name (fast, logarithmic search)
185  const Gaudi::ParticleProperty* pp = svc->find( evtGen );
186  // 2) check the proper evtgen name
187  if ( pp && evtGen == pp->evtGen() ) { return pp; }
188  // 3) use the regular (linear search)
189  auto begin = svc->begin();
190  auto end = svc->end();
191  auto found = std::find_if( begin, end, [&]( const Gaudi::ParticleProperty* pp ) { return pp->evtGen() == evtGen; } );
192  //
193  return found != end ? *found : nullptr;
194 }
195 
196 /* get all the properties at once
197  * @author Vanya BELYAEV Ivan.Belyaev@nikhef.nl
198  * @date 2008-08-03
199  */
202  return service ? Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties( service->begin(), service->end() )
204 }
Gaudi::Interfaces::IParticlePropertySvc::end
virtual iterator end() const =0
get the end-iterator for the container of particle properties It is assumed that the container is pro...
std::string
STL class.
Gaudi::ParticleProperties::allProperties
Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties allProperties(const Gaudi::Interfaces::IParticlePropertySvc *service)
get all the properties at once
Definition: IParticlePropertySvc.cpp:201
std::vector
STL class.
std::find_if
T find_if(T... args)
Gaudi::Interfaces::IParticlePropertySvc
Definition: IParticlePropertySvc.h:29
std::distance
T distance(T... args)
ParticleID.h
IParticlePropertySvc.h
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Gaudi::ParticleProperties::particle
const Gaudi::ParticleProperty * particle(const size_t index, const Gaudi::Interfaces::IParticlePropertySvc *service)
the inverse mapping of the integer sequential number onto Gaudi::ParticleID object
Definition: IParticlePropertySvc.cpp:90
Gaudi::ParticleProperties::byPythiaID
const Gaudi::ParticleProperty * byPythiaID(const int pythia, const Gaudi::Interfaces::IParticlePropertySvc *svc)
mapping by pythiaID
Definition: IParticlePropertySvc.cpp:144
Gaudi::Interfaces::IParticlePropertySvc::ParticleProperties
std::vector< const Gaudi::ParticleProperty * > ParticleProperties
the actual type of (ordered) container of particle properties
Definition: IParticlePropertySvc.h:34
Gaudi::ParticleProperties::byEvtGenName
const Gaudi::ParticleProperty * byEvtGenName(const std::string &evtGen, const Gaudi::Interfaces::IParticlePropertySvc *svc)
mapping by EvtGen-name
Definition: IParticlePropertySvc.cpp:180
Gaudi::ParticleProperty
Definition: ParticleProperty.h:37
std::advance
T advance(T... args)
std::lower_bound
T lower_bound(T... args)
Gaudi::ParticleProperties::particleID
const Gaudi::ParticleID particleID(const size_t index, const Gaudi::Interfaces::IParticlePropertySvc *service)
the inverse mapping of the integer sequential number onto Gaudi::ParticleID object
Definition: IParticlePropertySvc.cpp:117
Gaudi::Interfaces::IParticlePropertySvc::find
virtual const ParticleProperty * find(const std::string &name) const =0
Retrieve an object by name:
Gaudi::ParticleID
Definition: ParticleID.h:43
Gaudi::ParticleProperty::Compare
Definition: ParticleProperty.h:44
IOTest.end
end
Definition: IOTest.py:125
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:507
Gaudi::Interfaces::IParticlePropertySvc::begin
virtual iterator begin() const =0
get the begin-iterator for the container of particle properties It is assumed that the container is p...
ParticleProperty.h
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39