CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventHypothesis.h
Go to the documentation of this file.
1 #ifndef DataFormats_EventHypothesis_interface_EventHypothesis_h
2 #define DataFormats_EventHypothesis_interface_EventHypothesis_h
3 
6 #include <boost/regex.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <typeinfo>
9 #include <iostream>
10 #include <type_traits>
11 #include <cxxabi.h>
12 
13 namespace pat {
14  // forward declaration
15  namespace eventhypothesis { template<typename T> class Looper; }
16  // real declarations
17  namespace eventhypothesis {
18  // typedef for the Ref
19  typedef reco::CandidatePtr CandRefType; // Ptr one day in the future
20  // filter
21  struct ParticleFilter {
22  virtual ~ParticleFilter() {}
23  bool operator()(const std::pair<std::string, CandRefType> &p) const { return operator()(p.second, p.first); }
24  virtual bool operator()(const CandRefType &cand, const std::string &role) const = 0;
25  };
26  // smart pointer to the filter
27  typedef boost::shared_ptr<const ParticleFilter> ParticleFilterPtr;
28  }
29  // the class
31  public:
33  typedef std::pair<std::string, CandRefType> value_type;
34  typedef std::vector<value_type> vector_type;
35  typedef vector_type::const_iterator const_iterator;
36  typedef vector_type::const_reverse_iterator const_reverse_iterator;
38 
39  void add(const CandRefType &ref, const std::string &role) ;
40 
41  const_iterator begin() const { return particles_.begin(); }
42  const_iterator end() const { return particles_.end(); }
43  const_reverse_iterator rbegin() const { return particles_.rbegin(); }
44  const_reverse_iterator rend() const { return particles_.rend(); }
45 
46  class ByRole {
47  public:
48  ByRole(const std::string &role) : role_(role) {}
49  bool operator()(const value_type &p) const { return p.first == role_; }
50  private:
52  };
53 
56 
57  const CandRefType & get(const std::string &role, int index=0) const ;
58  const CandRefType & get(const ParticleFilter &filter, int index=0) const ;
59  template<typename T> const T * getAs(const std::string &role, int index=0) const ;
60  template<typename T> const T * getAs(const ParticleFilter &filter, int index=0) const ;
61  const CandRefType & operator[](const std::string &role) const { return get(role,0); }
62  const CandRefType & operator[](const ParticleFilter &filter) const { return get(filter,0); }
63 
65  std::vector<CandRefType> all(const std::string &roleRegexp) const;
67  std::vector<CandRefType> all(const ParticleFilter &filter) const;
68 
69  size_t count() const { return particles_.size(); }
71  size_t count(const std::string &roleRegexp) const;
73  size_t count(const ParticleFilter &role) const;
74 
76  CandLooper loop() const ;
78  CandLooper loop(const std::string &roleRegexp) const;
81  CandLooper loop(const ParticleFilter &filter) const;
85  CandLooper loop(const ParticleFilter *filter) const;
88 
89 
90 
92  template<typename T> eventhypothesis::Looper<T> loopAs(const std::string &roleRegexp) const ;
95  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilter &filter) const;
99  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilter *filter) const;
101  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilterPtr &filter) const;
102  private:
103  template<typename Iterator, typename Predicate>
104  Iterator realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &p, size_t idx) const ;
105  char * getDemangledSymbol(const char* mangledSymbol) const;
106  template<typename T> std::string createExceptionMessage(const CandRefType &ref) const;
107 
108  std::vector<value_type> particles_;
109 
110  } ;
111 
112  namespace eventhypothesis {
113  struct AcceptAllFilter : public ParticleFilter {
115  static const AcceptAllFilter & get() { return s_dummyFilter; }
116  virtual bool operator()(const CandRefType &cand, const std::string &role) const { return true; }
117  private:
119  };
121  public:
122  explicit RoleRegexpFilter(const std::string &roleRegexp) : re_(roleRegexp) {}
123  virtual bool operator()(const CandRefType &cand, const std::string &role) const {
124  return boost::regex_match(role, re_);
125  }
126  private:
127  boost::regex re_;
128  };
129  }
130 
131  template<typename Iterator, typename Predicate>
132  Iterator EventHypothesis::realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &pred, size_t idx) const
133  {
134  Iterator it = realBegin;
135  while (it != realEnd) {
136  if (pred(*it)) {
137  if (idx == 0) return it;
138  idx--;
139  }
140  ++it;
141  }
142  return it;
143  }
144 
145  template<typename T>
148  std::stringstream message;
149  char *currentType = getDemangledSymbol(typeid(std::remove_reference<decltype(ref)>::type::value_type).name());
150  char *targetType = getDemangledSymbol(typeid(T).name());
151  if (currentType != nullptr && targetType != nullptr) {
152  message << "You can't convert a '" << currentType << "' to a '" << targetType << "'" << std::endl;
153  free(currentType);
154  free(targetType);
155  } else {
156  message << "You can't convert a '" << typeid(ref).name() << "' to a '" << typeid(T).name() << "'" << std::endl;
157  message << "Note: you can use 'c++filt -t' command to convert the above in human readable types." << std::endl;
158  }
159  return message.str();
160  }
161 
162  template<typename T>
163  const T *
164  EventHypothesis::getAs(const std::string &role, int index) const
165  {
166  CandRefType ref = get(role, index);
167  const T* ret = dynamic_cast<const T*>(ref.get());
168  if ((ret == 0) && (ref.get() != 0)) throw cms::Exception("Type Checking") << createExceptionMessage<T>(ref);
169  return ret;
170  }
171  template<typename T>
172  const T *
174  {
175  CandRefType ref = get(filter, index);
176  const T* ret = dynamic_cast<const T*>(ref.get());
177  if ((ret == 0) && (ref.get() != 0)) throw cms::Exception("Type Checking") << createExceptionMessage<T>(ref);
178  return ret;
179  }
180  template<typename T>
182  EventHypothesis::loopAs(const std::string &roleRegexp) const
183  {
184  return loopAs<T>(new pat::eventhypothesis::RoleRegexpFilter(roleRegexp));
185  }
186 
187  template<typename T>
190  {
191  return pat::eventhypothesis::Looper<T>(*this, role);
192  }
193 
194  template<typename T>
197  {
198  return pat::eventhypothesis::Looper<T>(*this, role);
199  }
200 
201  template<typename T>
204  {
205  return pat::eventhypothesis::Looper<T>(*this, role);
206  }
207 }
208 
209 #endif
const std::string & role_
CandLooper loop() const
Loops over all particles.
eventhypothesis::ParticleFilter ParticleFilter
std::vector< value_type > particles_
eventhypothesis::Looper< T > loopAs(const std::string &roleRegexp) const
Loops over particles which have certaint roles.
static const AcceptAllFilter s_dummyFilter
std::pair< std::string, CandRefType > value_type
Iterator realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &p, size_t idx) const
RoleRegexpFilter(const std::string &roleRegexp)
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:160
std::string createExceptionMessage(const CandRefType &ref) const
size_t count() const
void add(const CandRefType &ref, const std::string &role)
std::vector< CandRefType > all(const std::string &roleRegexp) const
Return EDM references to all particles which have certaint roles.
const T * getAs(const std::string &role, int index=0) const
boost::shared_ptr< const ParticleFilter > ParticleFilterPtr
const CandRefType & operator[](const std::string &role) const
reco::CandidatePtr CandRefType
char * getDemangledSymbol(const char *mangledSymbol) const
const_iterator end() const
const CandRefType & operator[](const ParticleFilter &filter) const
eventhypothesis::CandRefType CandRefType
bool operator()(const value_type &p) const
eventhypothesis::Looper< reco::Candidate > CandLooper
Container::value_type value_type
std::vector< value_type > vector_type
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
eventhypothesis::ParticleFilterPtr ParticleFilterPtr
vector_type::const_iterator const_iterator
const_reverse_iterator rend() const
ByRole(const std::string &role)
virtual bool operator()(const CandRefType &cand, const std::string &role) const
virtual bool operator()(const CandRefType &cand, const std::string &role) const
long double T
const_iterator begin() const
vector_type::const_reverse_iterator const_reverse_iterator
bool operator()(const std::pair< std::string, CandRefType > &p) const
const_reverse_iterator rbegin() const