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 #ifndef __ROOTCLING__
104  using typename Selector<T>::operator();
105 #endif
106 
107  const unsigned char* md55Raw() const { return id_md5_; }
108  bool operator==(const VersionedSelector& other) const {
109  constexpr unsigned length = MD5_DIGEST_LENGTH;
110  return ( 0 == memcmp(id_md5_,other.id_md5_,length*sizeof(unsigned char)) );
111  }
112  const std::string& md5String() const { return md5_string_; }
113 
114  const std::string& name() const { return name_; }
115 
116  const unsigned howFarInCutFlow() const { return howfar_; }
117 
118  const size_t cutFlowSize() const { return cuts_.size(); }
119 
120  void initialize(const edm::ParameterSet&);
121 
122 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
124 #endif
125 
126  protected:
128  std::vector<boost::shared_ptr<candf::CandidateCut> > cuts_;
129  std::vector<bool> needs_event_content_;
130  std::vector<typename Selector<T>::index_type> cut_indices_;
131  unsigned howfar_;
132 
133  private:
134  unsigned char id_md5_[MD5_DIGEST_LENGTH];
136 };
137 
138 template<class T>
141  if(initialized_) {
142  edm::LogWarning("VersionedPatElectronSelector")
143  << "ID was already initialized!";
144  return;
145  }
146  const std::vector<edm::ParameterSet>& cutflow =
147  conf.getParameterSetVector("cutFlow");
148  if( cutflow.size() == 0 ) {
149  throw cms::Exception("InvalidCutFlow")
150  << "You have supplied a null/empty cutflow to VersionedIDSelector,"
151  << " please add content to the cuflow and try again.";
152  }
153  // this lets us keep track of cuts without knowing what they are :D
154  std::vector<edm::ParameterSet>::const_iterator cbegin(cutflow.begin()),
155  cend(cutflow.end());
156  std::vector<edm::ParameterSet>::const_iterator icut = cbegin;
157  for( ; icut != cend; ++icut ) {
158  const std::string& name = icut->getParameter<std::string>("cutName");
159  const bool needsContent =
160  icut->getParameter<bool>("needsAdditionalProducts");
161  const bool ignored = icut->getParameter<bool>("isIgnored");
163 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
164  CutApplicatorFactory::get()->create(name,*icut);
165 #else
166  nullptr;
167 #endif
168  if( plugin != nullptr ) {
169  cuts_.push_back(boost::shared_ptr<candf::CandidateCut>(plugin));
170  } else {
171  throw cms::Exception("BadPluginName")
172  << "The requested cut: " << name << " is not available!";
173  }
174  needs_event_content_.push_back(needsContent);
175  this->push_back(name);
176  this->set(name);
177  if(ignored) this->ignoreCut(name);
178  }
179  //have to loop again to set cut indices after all are filled
180  icut = cbegin;
181  for( ; icut != cend; ++icut ) {
182  const std::string& name = icut->getParameter<std::string>("cutName");
183  cut_indices_.push_back(typename Selector<T>::index_type(&(this->bits_),name));
184  }
185  initialized_ = true;
186 }
187 
188 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
190 template<class T>
192  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
193  if( needs_event_content_[i] ) {
194  CutApplicatorWithEventContentBase* needsEvent =
195  static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
196  needsEvent->setConsumes(cc);
197  }
198  }
199 }
200 #endif
201 
202 #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