File indexing completed on 2025-01-18 10:01:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef HEPMC3_FEATURE_H
0012 #define HEPMC3_FEATURE_H
0013
0014 #include <functional>
0015 #include <memory>
0016 #include <limits>
0017 #include "HepMC3/GenParticle.h"
0018 #include "HepMC3/Filter.h"
0019
0020
0021 namespace HepMC3 {
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058 template<typename Feature_type>
0059 class GenericFeature {
0060 public:
0061
0062 using Evaluator_type = std::function<Feature_type(ConstGenParticlePtr)>;
0063
0064 using EvaluatorPtr = std::shared_ptr<Evaluator_type>;
0065
0066
0067 Feature_type operator()(ConstGenParticlePtr input)const {
0068 return (*m_internal)(input);
0069 }
0070
0071
0072
0073 Filter operator > (Feature_type value) const {
0074 EvaluatorPtr functor = m_internal;
0075 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) > value;};
0076 }
0077
0078
0079 Filter operator < (Feature_type value) const {
0080 EvaluatorPtr functor = m_internal;
0081 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) < value;};
0082 }
0083
0084
0085
0086 Filter operator >= (Feature_type value) const {
0087 EvaluatorPtr functor = m_internal;
0088 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) >= value;};
0089 }
0090
0091
0092
0093 Filter operator <= (Feature_type value) const {
0094 EvaluatorPtr functor = m_internal;
0095 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) <= value;};
0096 }
0097
0098
0099
0100 virtual Filter operator == (Feature_type value) const {
0101 EvaluatorPtr functor = m_internal;
0102 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) == value;};
0103 }
0104
0105
0106
0107 virtual Filter operator != (Feature_type value) const {
0108 EvaluatorPtr functor = m_internal;
0109 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) != value;};
0110 }
0111
0112 protected:
0113
0114 GenericFeature(Evaluator_type functor):m_internal(std::make_shared<Evaluator_type>(functor)) {}
0115
0116
0117 GenericFeature(const GenericFeature ©) : m_internal(copy.m_internal) {}
0118
0119
0120 GenericFeature(GenericFeature && ) = default;
0121
0122 GenericFeature& operator=(const GenericFeature&) = default;
0123
0124 GenericFeature& operator=(GenericFeature&&) = default;
0125
0126
0127
0128
0129
0130 EvaluatorPtr m_internal;
0131 };
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 template<typename Feature_type, typename Dummy = void>
0170 class Feature : public GenericFeature<Feature_type> {
0171 public:
0172 using typename GenericFeature<Feature_type>::Evaluator_type;
0173 using typename GenericFeature<Feature_type>::EvaluatorPtr;
0174 using GenericFeature<Feature_type>::m_internal;
0175
0176 using GenericFeature<Feature_type>::operator ();
0177 using GenericFeature<Feature_type>::operator >;
0178 using GenericFeature<Feature_type>::operator >=;
0179 using GenericFeature<Feature_type>::operator <;
0180 using GenericFeature<Feature_type>::operator <=;
0181 using GenericFeature<Feature_type>::operator ==;
0182 using GenericFeature<Feature_type>::operator !=;
0183
0184
0185 Feature(Evaluator_type functor) : GenericFeature<Feature_type>(functor) {}
0186
0187 Feature(const Feature ©) : GenericFeature<Feature_type>(copy) {}
0188
0189 Feature(Feature && ) = default;
0190
0191 Feature& operator=(const Feature&) = default;
0192
0193 Feature& operator=(Feature&&) = default;
0194
0195
0196 Feature<Feature_type> abs() const {
0197 EvaluatorPtr functor = m_internal;
0198 Evaluator_type absfunctor = [functor](ConstGenParticlePtr p)->Feature_type{return ::abs((*functor)(p));};
0199 return Feature<Feature_type>(absfunctor);
0200 }
0201 };
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 template<typename Feature_type>
0219 class Feature<Feature_type, typename std::enable_if<std::is_integral<Feature_type>::value, void>::type> : public GenericFeature<Feature_type> {
0220 public:
0221 using GenericFeature<Feature_type>::operator ();
0222 using GenericFeature<Feature_type>::operator >;
0223 using GenericFeature<Feature_type>::operator >=;
0224 using GenericFeature<Feature_type>::operator <;
0225 using GenericFeature<Feature_type>::operator <=;
0226 using GenericFeature<Feature_type>::operator ==;
0227 using GenericFeature<Feature_type>::operator !=;
0228
0229 using typename GenericFeature<Feature_type>::Evaluator_type;
0230 using typename GenericFeature<Feature_type>::EvaluatorPtr;
0231
0232 using GenericFeature<Feature_type>::m_internal;
0233
0234
0235 Feature(Evaluator_type functor) : GenericFeature<Feature_type>(functor) {}
0236
0237 Feature(const Feature ©) : GenericFeature<Feature_type>(copy) {}
0238
0239 Feature(Feature && ) = default;
0240
0241 Feature& operator=(const Feature&) = default;
0242
0243 Feature& operator=(Feature&&) = default;
0244
0245
0246
0247 Feature<Feature_type> abs() const {
0248 EvaluatorPtr functor = m_internal;
0249 Evaluator_type absfunctor = [functor](ConstGenParticlePtr p)->Feature_type{return ::abs((*functor)(p));};
0250 return Feature<Feature_type>(absfunctor);
0251 }
0252
0253
0254 Filter operator > (double value) const {
0255 EvaluatorPtr functor = m_internal;
0256 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) > value;};
0257 }
0258
0259
0260 Filter operator < (double value) const {
0261 EvaluatorPtr functor = m_internal;
0262 return [value, functor](ConstGenParticlePtr input)->bool{return (*functor)(input) < value;};
0263 }
0264
0265
0266 Filter operator == (double value) const {
0267 EvaluatorPtr functor = m_internal;
0268 return [value, functor](ConstGenParticlePtr input)->bool{
0269 Feature_type local = (*functor)(input);
0270 return std::abs(local - value) < std::numeric_limits<double>::epsilon();
0271 };
0272 }
0273
0274
0275 Filter operator >= (double value) const { return !( (*this) < value );}
0276
0277
0278 Filter operator <= (double value) const { return !( (*this) > value );}
0279
0280
0281 Filter operator != (double value) const {
0282 return !( (*this) == value );
0283 }
0284 };
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 template<typename Feature_type>
0296 class Feature<Feature_type, typename std::enable_if<std::is_floating_point<Feature_type>::value, void>::type> : public GenericFeature<Feature_type> {
0297 public:
0298 using typename GenericFeature<Feature_type>::Evaluator_type;
0299 using typename GenericFeature<Feature_type>::EvaluatorPtr;
0300
0301 using GenericFeature<Feature_type>::operator ();
0302 using GenericFeature<Feature_type>::operator >;
0303 using GenericFeature<Feature_type>::operator >=;
0304 using GenericFeature<Feature_type>::operator <;
0305 using GenericFeature<Feature_type>::operator <=;
0306
0307 using GenericFeature<Feature_type>::m_internal;
0308
0309
0310 Feature(Evaluator_type functor) : GenericFeature<Feature_type>(functor) {}
0311
0312 Feature(const Feature ©) : GenericFeature<Feature_type>(copy) {}
0313
0314
0315 Feature(Feature && ) = default;
0316
0317 Feature& operator=(const Feature&) = default;
0318
0319 Feature& operator=(Feature&&) = default;
0320
0321
0322
0323 Feature<Feature_type> abs() const {
0324 EvaluatorPtr functor = m_internal;
0325 Evaluator_type absfunctor = [functor](ConstGenParticlePtr p)->Feature_type{return std::abs((*functor)(p));};
0326 return Feature<Feature_type>(absfunctor);
0327 }
0328
0329 Filter operator == (Feature_type value) const override {
0330 EvaluatorPtr functor = m_internal;
0331 return [value, functor](ConstGenParticlePtr input)->bool{
0332 Feature_type local = (*functor)(input);
0333 return std::less_equal<Feature_type>{}(fabs(local - value) , std::numeric_limits<Feature_type>::epsilon());
0334 };
0335 }
0336
0337 Filter operator != (Feature_type value) const override {
0338 return !( (*this) == value );
0339 }
0340 };
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351 template<typename Feature_type>
0352 Feature<Feature_type> abs(const Feature<Feature_type> &input) {
0353 return input.abs();
0354 }
0355
0356 }
0357
0358 #endif