test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VersionedSelector.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_SelectorUtils_VersionedSelector_h
2 #define PhysicsTools_SelectorUtils_VersionedSelector_h
3 
21 
22 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
24 #endif
25 
26 // because we need to be able to validate the ID
27 #include <openssl/md5.h>
28 
29 #include <boost/shared_ptr.hpp>
30 
31 namespace candf = candidate_functions;
32 
33 template<class T>
34 class VersionedSelector : public Selector<T> {
35  public:
37 
39  Selector<T>(),
41  constexpr unsigned length = MD5_DIGEST_LENGTH;
42  edm::ParameterSet trackedPart = conf.trackedPart();
43  name_ = conf.getParameter<std::string>("idName");
44  memset(id_md5_,0,length*sizeof(unsigned char));
45  std::string tracked(trackedPart.dump()), untracked(conf.dump());
46  if ( tracked != untracked ) {
47  throw cms::Exception("InvalidConfiguration")
48  << "VersionedSelector does not allow untracked parameters"
49  << " in the configuration ParameterSet!";
50  }
51  // now setup the md5 and cute accessor functions
52  MD5((unsigned char*)tracked.c_str(), tracked.size(), id_md5_);
53  char buf[32];
54  for( unsigned i=0; i<MD5_DIGEST_LENGTH; ++i ){
55  sprintf(buf, "%02x", id_md5_[i]);
56  md5_string_.append( buf );
57  }
58  initialize(conf);
59  this->retInternal_ = this->getBitTemplate();
60  }
61 
62  bool operator()( const T& ref, pat::strbitset& ret )
63 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
64  override final
65 #endif
66  {
67  howfar_ = 0;
68  bool failed = false;
69  if( !initialized_ ) {
70  throw cms::Exception("CutNotInitialized")
71  << "VersionedGsfElectronSelector not initialized!" << std::endl;
72  }
73  for( unsigned i = 0; i < cuts_.size(); ++i ) {
75  const bool result = (*cuts_[i])(temp);
76  if( result || this->ignoreCut(cut_indices_[i]) ) {
77  this->passCut(ret,cut_indices_[i]);
78  if( !failed ) ++howfar_;
79  } else {
80  failed = true;
81  }
82  }
83  this->setIgnored(ret);
84  return (bool)ret;
85  }
86 
87  bool operator()(const T& ref, edm::EventBase const& e, pat::strbitset& ret)
88 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
89  override final
90 #endif
91  {
92  // setup isolation needs
93  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
94  if( needs_event_content_[i] ) {
96  static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
97  needsEvent->getEventContent(e);
98  }
99  }
100  return this->operator()(ref, ret);
101  }
102 
103  using typename Selector<T>::operator();
104 
105  const unsigned char* md55Raw() const { return id_md5_; }
106  bool operator==(const VersionedSelector& other) const {
107  constexpr unsigned length = MD5_DIGEST_LENGTH;
108  return ( 0 == memcmp(id_md5_,other.id_md5_,length*sizeof(unsigned char)) );
109  }
110  const std::string& md5String() const { return md5_string_; }
111 
112  const std::string& name() const { return name_; }
113 
114  const unsigned howFarInCutFlow() const { return howfar_; }
115 
116  const size_t cutFlowSize() const { return cuts_.size(); }
117 
118  void initialize(const edm::ParameterSet&);
119 
120 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
122 #endif
123 
124  protected:
126  std::vector<boost::shared_ptr<candf::CandidateCut> > cuts_;
127  std::vector<bool> needs_event_content_;
128  std::vector<typename Selector<T>::index_type> cut_indices_;
129  unsigned howfar_;
130 
131  private:
132  unsigned char id_md5_[MD5_DIGEST_LENGTH];
134 };
135 
136 template<class T>
139  if(initialized_) {
140  edm::LogWarning("VersionedPatElectronSelector")
141  << "ID was already initialized!";
142  return;
143  }
144  const std::vector<edm::ParameterSet>& cutflow =
145  conf.getParameterSetVector("cutFlow");
146  if( cutflow.size() == 0 ) {
147  throw cms::Exception("InvalidCutFlow")
148  << "You have supplied a null/empty cutflow to VersionedIDSelector,"
149  << " please add content to the cuflow and try again.";
150  }
151  // this lets us keep track of cuts without knowing what they are :D
152  std::vector<edm::ParameterSet>::const_iterator cbegin(cutflow.begin()),
153  cend(cutflow.end());
154  std::vector<edm::ParameterSet>::const_iterator icut = cbegin;
155  for( ; icut != cend; ++icut ) {
156  const std::string& name = icut->getParameter<std::string>("cutName");
157  const bool needsContent =
158  icut->getParameter<bool>("needsAdditionalProducts");
159  const bool ignored = icut->getParameter<bool>("isIgnored");
161 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
162  CutApplicatorFactory::get()->create(name,*icut);
163 #else
164  nullptr;
165 #endif
166  if( plugin != nullptr ) {
167  cuts_.push_back(boost::shared_ptr<candf::CandidateCut>(plugin));
168  } else {
169  throw cms::Exception("BadPluginName")
170  << "The requested cut: " << name << " is not available!";
171  }
172  needs_event_content_.push_back(needsContent);
173  this->push_back(name);
174  this->set(name);
175  if(ignored) this->ignoreCut(name);
176  }
177  //have to loop again to set cut indices after all are filled
178  icut = cbegin;
179  for( ; icut != cend; ++icut ) {
180  const std::string& name = icut->getParameter<std::string>("cutName");
181  cut_indices_.push_back(typename Selector<T>::index_type(&(this->bits_),name));
182  }
183  initialized_ = true;
184 }
185 
186 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
188 template<class T>
190  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
191  if( needs_event_content_[i] ) {
192  CutApplicatorWithEventContentBase* needsEvent =
193  static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
194  needsEvent->setConsumes(cc);
195  }
196  }
197 }
198 #endif
199 
200 #endif
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
VParameterSet const & getParameterSetVector(std::string const &name) const
const unsigned char * md55Raw() const
bool operator()(const T &ref, pat::strbitset &ret) overridefinal
This provides the interface for base classes to select objects.
auto_ptr< JetDefinition::Plugin > plugin
std::string dump(unsigned int indent=0) const
bool operator()(const T &ref, edm::EventBase const &e, pat::strbitset &ret) overridefinal
This provides an alternative signature that includes extra information.
const size_t cutFlowSize() const
void setIgnored(pat::strbitset &ret)
set ignored bits
Definition: Selector.h:224
pat::strbitset retInternal_
internal ret if users don&#39;t care about return bits
Definition: Selector.h:287
VersionedSelector(const edm::ParameterSet &conf)
#define constexpr
ParameterSet trackedPart() const
std::vector< bool > needs_event_content_
const std::string & name() const
void passCut(pat::strbitset &ret, std::string const &s)
Passing cuts.
Definition: Selector.h:176
void initialize(const edm::ParameterSet &)
virtual void getEventContent(const edm::EventBase &)=0
bool ignoreCut(std::string const &s) const
ignore the cut at index &quot;s&quot;
Definition: Selector.h:159
tuple result
Definition: query.py:137
Functor that operates on &lt;T&gt;
Definition: Selector.h:24
tuple conf
Definition: dbtoconf.py:185
const unsigned howFarInCutFlow() const
const std::string & md5String() const
pat::strbitset getBitTemplate() const
Get an empty bitset with the proper names.
Definition: Selector.h:212
void setConsumes(edm::ConsumesCollector)
std::vector< boost::shared_ptr< candf::CandidateCut > > cuts_
unsigned char id_md5_[MD5_DIGEST_LENGTH]
volatile std::atomic< bool > shutdown_flag false
long double T
std::vector< typename Selector< T >::index_type > cut_indices_
cut-flow versioning info in the event provenance
tuple untracked
Definition: Types.py:27
bool operator==(const VersionedSelector &other) const
virtual void setConsumes(edm::ConsumesCollector &)=0
T get(const Candidate &c)
Definition: component.h:55