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 
14 #if ( !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) ) || defined(__ROOTCLING__)
15 
16 #define REGULAR_CPLUSPLUS 1
17 #define CINT_GUARD(CODE) CODE
19 #include <memory>
20 #define SHARED_PTR(T) std::shared_ptr<T>
21 
22 #else
23 
24 #define CINT_GUARD(CODE)
25 #include <boost/shared_ptr.hpp>
26 #define SHARED_PTR(T) boost::shared_ptr<T>
27 
28 #endif
29 
36 
37 // because we need to be able to validate the ID
38 #include <openssl/md5.h>
39 
40 namespace candf = candidate_functions;
41 
42 namespace vid {
43  class CutFlowResult;
44 }
45 
46 template<class T>
47 class VersionedSelector : public Selector<T> {
48  public:
50 
52  Selector<T>(),
54  constexpr unsigned length = MD5_DIGEST_LENGTH;
55  edm::ParameterSet trackedPart = conf.trackedPart();
56  name_ = conf.getParameter<std::string>("idName");
57  memset(id_md5_,0,length*sizeof(unsigned char));
58  std::string tracked(trackedPart.dump()), untracked(conf.dump());
59  if ( tracked != untracked ) {
60  throw cms::Exception("InvalidConfiguration")
61  << "VersionedSelector does not allow untracked parameters"
62  << " in the cutflow ParameterSet!";
63  }
64  // now setup the md5 and cute accessor functions
65  MD5((unsigned char*)tracked.c_str(), tracked.size(), id_md5_);
66  char buf[32];
67  for( unsigned i=0; i<MD5_DIGEST_LENGTH; ++i ){
68  sprintf(buf, "%02x", id_md5_[i]);
69  md5_string_.append( buf );
70  }
71  initialize(conf);
72  this->retInternal_ = this->getBitTemplate();
73  }
74 
75  virtual bool operator()( const T& ref, pat::strbitset& ret ) CINT_GUARD(override final) {
76  howfar_ = 0;
77  bitmap_ = 0;
78  values_.clear();
79  bool failed = false;
80  if( !initialized_ ) {
81  throw cms::Exception("CutNotInitialized")
82  << "VersionedGsfElectronSelector not initialized!" << std::endl;
83  }
84  for( unsigned i = 0; i < cuts_.size(); ++i ) {
86  const bool result = (*cuts_[i])(temp);
87  values_.push_back(cuts_[i]->value(temp));
88  if( result || this->ignoreCut(cut_indices_[i]) ) {
89  this->passCut(ret,cut_indices_[i]);
90  bitmap_ |= 1<<i;
91  if( !failed ) ++howfar_;
92  } else {
93  failed = true;
94  }
95  }
96  this->setIgnored(ret);
97  return (bool)ret;
98  }
99 
100  virtual bool operator()(const T& ref, edm::EventBase const& e, pat::strbitset& ret) CINT_GUARD(override final) {
101  // setup isolation needs
102  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
103  if( needs_event_content_[i] ) {
104  CutApplicatorWithEventContentBase* needsEvent =
105  static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
106  needsEvent->getEventContent(e);
107  }
108  }
109  return this->operator()(ref, ret);
110  }
111 
112  //repeat the other operator() we left out here
113  //in the base class here so they are exposed to ROOT
114 
115  /* VID BY VALUE */
116  bool operator()( typename T::value_type const & t ) {
117  const T temp(edm::ProductID(),&t,0); // assuming T is edm::Ptr
118  return this->operator()(temp);
119  }
120 
121  bool operator()( typename T::value_type const & t, edm::EventBase const & e) {
122  const T temp(edm::ProductID(),&t,0);
123  return this->operator()(temp,e);
124  }
125 
126  virtual bool operator()( T const & t ) CINT_GUARD(override final) {
127  this->retInternal_.set(false);
128  this->operator()(t, this->retInternal_);
129  this->setIgnored(this->retInternal_);
130  return (bool)this->retInternal_;
131  }
132 
133  virtual bool operator()( T const & t, edm::EventBase const & e) CINT_GUARD(override final) {
134  this->retInternal_.set(false);
135  this->operator()(t, e, this->retInternal_);
136  this->setIgnored(this->retInternal_);
137  return (bool)this->retInternal_;
138  }
139 
140  const unsigned char* md55Raw() const { return id_md5_; }
141  bool operator==(const VersionedSelector& other) const {
142  constexpr unsigned length = MD5_DIGEST_LENGTH;
143  return ( 0 == memcmp(id_md5_,other.id_md5_,length*sizeof(unsigned char)) );
144  }
145  const std::string& md5String() const { return md5_string_; }
146 
147  const std::string& name() const { return name_; }
148 
149  const unsigned howFarInCutFlow() const { return howfar_; }
150 
151  const unsigned bitMap() const { return bitmap_; }
152 
153  const size_t cutFlowSize() const { return cuts_.size(); }
154 
156 
157  void initialize(const edm::ParameterSet&);
158 
160 
161  protected:
163  std::vector<SHARED_PTR(candf::CandidateCut) > cuts_;
164  std::vector<bool> needs_event_content_;
165  std::vector<typename Selector<T>::index_type> cut_indices_;
166  unsigned howfar_, bitmap_;
167  std::vector<double> values_;
168 
169  private:
170  unsigned char id_md5_[MD5_DIGEST_LENGTH];
172 };
173 
174 template<class T>
177  if(initialized_) {
178  edm::LogWarning("VersionedPatElectronSelector")
179  << "ID was already initialized!";
180  return;
181  }
182  const std::vector<edm::ParameterSet>& cutflow =
183  conf.getParameterSetVector("cutFlow");
184  if( cutflow.size() == 0 ) {
185  throw cms::Exception("InvalidCutFlow")
186  << "You have supplied a null/empty cutflow to VersionedIDSelector,"
187  << " please add content to the cuflow and try again.";
188  }
189 
190  // this lets us keep track of cuts without knowing what they are :D
191  std::vector<edm::ParameterSet>::const_iterator cbegin(cutflow.begin()),
192  cend(cutflow.end());
193  std::vector<edm::ParameterSet>::const_iterator icut = cbegin;
194  std::map<std::string,unsigned> cut_counter;
195  for( ; icut != cend; ++icut ) {
196  std::stringstream realname;
197  const std::string& name = icut->getParameter<std::string>("cutName");
198  if( !cut_counter.count(name) ) cut_counter[name] = 0;
199  realname << name << "_" << cut_counter[name];
200  const bool needsContent =
201  icut->getParameter<bool>("needsAdditionalProducts");
202  const bool ignored = icut->getParameter<bool>("isIgnored");
203  candf::CandidateCut* plugin = nullptr;
204  CINT_GUARD(plugin = CutApplicatorFactory::get()->create(name,*icut));
205  if( plugin != nullptr ) {
206  cuts_.push_back(SHARED_PTR(candf::CandidateCut)(plugin));
207  } else {
208  throw cms::Exception("BadPluginName")
209  << "The requested cut: " << name << " is not available!";
210  }
211  needs_event_content_.push_back(needsContent);
212  const std::string therealname = realname.str();
213  this->push_back(therealname);
214  this->set(therealname);
215  if(ignored) this->ignoreCut(therealname);
216  cut_counter[name]++;
217  }
218 
219  //have to loop again to set cut indices after all are filled
220  icut = cbegin;
221  cut_counter.clear();
222  for( ; icut != cend; ++icut ) {
223  std::stringstream realname;
224  const std::string& name = icut->getParameter<std::string>("cutName");
225  if( !cut_counter.count(name) ) cut_counter[name] = 0;
226  realname << name << "_" << cut_counter[name];
227  cut_indices_.push_back(typename Selector<T>::index_type(&(this->bits_),realname.str()));
228  cut_counter[name]++;
229  }
230 
231  initialized_ = true;
232 }
233 
234 
235 
236 #ifdef REGULAR_CPLUSPLUS
238 template<class T>
240  std::map<std::string,unsigned> names_to_index;
241  std::map<std::string,unsigned> cut_counter;
242  for( unsigned idx = 0; idx < cuts_.size(); ++idx ) {
243  const std::string& name = cuts_[idx]->name();
244  if( !cut_counter.count(name) ) cut_counter[name] = 0;
245  std::stringstream realname;
246  realname << name << "_" << cut_counter[name];
247  names_to_index.emplace(realname.str(),idx);
248  cut_counter[name]++;
249  }
250  return vid::CutFlowResult(name_,md5_string_,names_to_index,values_,bitmap_);
251 }
252 
254 template<class T>
256  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
257  if( needs_event_content_[i] ) {
258  CutApplicatorWithEventContentBase* needsEvent =
259  dynamic_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
260  if( nullptr != needsEvent ) {
261  needsEvent->setConsumes(cc);
262  } else {
263  throw cms::Exception("InvalidCutConfiguration")
264  << "Cut: " << ((CutApplicatorBase*)cuts_[i].get())->name()
265  << " configured to consume event products but does not "
266  << " inherit from CutApplicatorWithEventContenBase "
267  << " please correct either your python or C++!";
268  }
269  }
270  }
271 }
272 #endif
273 
274 #endif
virtual bool operator()(const T &ref, pat::strbitset &ret) overridefinal
This provides the interface for base classes to select objects.
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()(typename T::value_type const &t)
bool operator()(typename T::value_type const &t, edm::EventBase const &e)
auto_ptr< JetDefinition::Plugin > plugin
std::vector< double > values_
vid::CutFlowResult cutFlowResult() const
std::string dump(unsigned int indent=0) const
virtual bool operator()(T const &t) overridefinal
This provides an alternative signature without the second ret.
const size_t cutFlowSize() const
#define SHARED_PTR(T)
void setIgnored(pat::strbitset &ret)
set ignored bits
Definition: Selector.h:224
std::vector< std::shared_ptr< candf::CandidateCut > > cuts_
virtual bool operator()(const T &ref, edm::EventBase const &e, pat::strbitset &ret) overridefinal
This provides an alternative signature that includes extra information.
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
virtual bool operator()(T const &t, edm::EventBase const &e) overridefinal
This provides an alternative signature that includes extra information.
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
const unsigned bitMap() const
Container::value_type value_type
Functor that operates on &lt;T&gt;
Definition: Selector.h:24
tuple conf
Definition: dbtoconf.py:185
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:144
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)
unsigned char id_md5_[MD5_DIGEST_LENGTH]
#define CINT_GUARD(CODE)
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
SurfaceDeformation * create(int type, const std::vector< double > &params)
virtual void setConsumes(edm::ConsumesCollector &)=0
T get(const Candidate &c)
Definition: component.h:55