HepMC3 event record library
Filter.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5 //
6 ///
7 /// @file Filter.h
8 /// @brief Defines Filter operations for combingin Filters
9 ///
10 #ifndef HEPMC3_FILTER_H
11 #define HEPMC3_FILTER_H
12 
13 #include <vector>
14 #include <functional>
15 #include "HepMC3/GenParticle.h"
16 
17 namespace HepMC3 {
18 /// @brief type of Filter
19 using Filter = std::function<bool(ConstGenParticlePtr)>;
20 
21 /// @brief Apply a Filter to a list of GenParticles
22 /// Returns a vector of GenParticles that satisfy the Filter
23 inline std::vector<GenParticlePtr> applyFilter(const Filter &filter, const std::vector<GenParticlePtr> &particles) {
24  std::vector<GenParticlePtr> result;
25  for (GenParticlePtr p: particles) {
26  if (filter(p)) result.push_back(p);
27  }
28  return result;
29 }
30 
31 /// @brief Apply a Filter to a list of ConstGenParticles
32 /// Returns a vector of ConstGenParticles that satisfy the Filter
33 inline std::vector<ConstGenParticlePtr> applyFilter(const Filter &filter, const std::vector<ConstGenParticlePtr> &particles) {
34  std::vector<ConstGenParticlePtr> result;
35  for (ConstGenParticlePtr p: particles) {
36  if (filter(p)) result.push_back(p);
37  }
38  return result;
39 }
40 
41 /// @brief A Filter that will accept all particles
42 /// This might be needed if a signature requires a default Filter
43 inline bool ACCEPT_ALL(ConstGenParticlePtr /*dummy*/) {
44  return true;
45 }
46 
47 /// @brief The logical AND of two Filters is itself a Filter
48 inline Filter operator && (const Filter & lhs, const Filter &rhs) {
49  return [lhs, rhs](ConstGenParticlePtr p)->bool{return lhs(p) && rhs(p); };
50 }
51 
52 /// @brief The logical OR of two Filters is itself a Filter
53 inline Filter operator || (const Filter & lhs, const Filter &rhs) {
54  return [lhs, rhs](ConstGenParticlePtr p)->bool{return lhs(p) || rhs(p); };
55 }
56 
57 /// @brief The negation of a Filter is itself a Filter
58 inline Filter operator !(const Filter &rhs) {
59  return [rhs](ConstGenParticlePtr p)->bool{return !(rhs(p));};
60 }
61 
62 }
63 #endif
Filter operator&&(const Filter &lhs, const Filter &rhs)
The logical AND of two Filters is itself a Filter.
Definition: Filter.h:48
Definition of class GenParticle.
std::vector< GenParticlePtr > applyFilter(const Filter &filter, const std::vector< GenParticlePtr > &particles)
Apply a Filter to a list of GenParticles Returns a vector of GenParticles that satisfy the Filter...
Definition: Filter.h:23
Filter operator||(const Filter &lhs, const Filter &rhs)
The logical OR of two Filters is itself a Filter.
Definition: Filter.h:53
std::function< bool(ConstGenParticlePtr)> Filter
type of Filter
Definition: Filter.h:19
Filter operator!(const Filter &rhs)
The negation of a Filter is itself a Filter.
Definition: Filter.h:58
bool ACCEPT_ALL(ConstGenParticlePtr)
A Filter that will accept all particles This might be needed if a signature requires a default Filter...
Definition: Filter.h:43