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 
35 
36 // because we need to be able to validate the ID
37 #include <openssl/md5.h>
38 
39 namespace candf = candidate_functions;
40 
41 namespace vid {
42  class CutFlowResult;
43 }
44 
45 template<class T>
46 class VersionedSelector : public Selector<T> {
47  public:
49 
51  Selector<T>(),
53  constexpr unsigned length = MD5_DIGEST_LENGTH;
54  edm::ParameterSet trackedPart = conf.trackedPart();
55  name_ = conf.getParameter<std::string>("idName");
56  memset(id_md5_,0,length*sizeof(unsigned char));
57  std::string tracked(trackedPart.dump()), untracked(conf.dump());
58  if ( tracked != untracked ) {
59  throw cms::Exception("InvalidConfiguration")
60  << "VersionedSelector does not allow untracked parameters"
61  << " in the cutflow ParameterSet!";
62  }
63  // now setup the md5 and cute accessor functions
64  MD5((unsigned char*)tracked.c_str(), tracked.size(), id_md5_);
65  char buf[32];
66  for( unsigned i=0; i<MD5_DIGEST_LENGTH; ++i ){
67  sprintf(buf, "%02x", id_md5_[i]);
68  md5_string_.append( buf );
69  }
70  initialize(conf);
71  this->retInternal_ = this->getBitTemplate();
72  }
73 
74  virtual bool operator()( const T& ref, pat::strbitset& ret ) CINT_GUARD(override final) {
75  howfar_ = 0;
76  bitmap_ = 0;
77  values_.clear();
78  bool failed = false;
79  if( !initialized_ ) {
80  throw cms::Exception("CutNotInitialized")
81  << "VersionedGsfElectronSelector not initialized!" << std::endl;
82  }
83  for( unsigned i = 0; i < cuts_.size(); ++i ) {
85  const bool result = (*cuts_[i])(temp);
86  values_.push_back(cuts_[i]->value(temp));
87  if( result || this->ignoreCut(cut_indices_[i]) ) {
88  this->passCut(ret,cut_indices_[i]);
89  bitmap_ |= 1<<i;
90  if( !failed ) ++howfar_;
91  } else {
92  failed = true;
93  }
94  }
95  this->setIgnored(ret);
96  return (bool)ret;
97  }
98 
99  virtual bool operator()(const T& ref, edm::EventBase const& e, pat::strbitset& ret) CINT_GUARD(override final) {
100  // setup isolation needs
101  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
102  if( needs_event_content_[i] ) {
103  CutApplicatorWithEventContentBase* needsEvent =
104  static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
105  needsEvent->getEventContent(e);
106  }
107  }
108  return this->operator()(ref, ret);
109  }
110 
111  //repeat the other operator() we left out here
112  //in the base class here so they are exposed to ROOT
113 
114  /* VID BY VALUE */
115  bool operator()( typename T::value_type const & t ) {
116  const T temp(&t,0); // assuming T is edm::Ptr
117  return this->operator()(temp);
118  }
119 
120  bool operator()( typename T::value_type const & t, edm::EventBase const & e) {
121  const T temp(&t,0);
122  return this->operator()(temp,e);
123  }
124 
125  virtual bool operator()( T const & t ) CINT_GUARD(override final) {
126  this->retInternal_.set(false);
127  this->operator()(t, this->retInternal_);
128  this->setIgnored(this->retInternal_);
129  return (bool)this->retInternal_;
130  }
131 
132  virtual bool operator()( T const & t, edm::EventBase const & e) CINT_GUARD(override final) {
133  this->retInternal_.set(false);
134  this->operator()(t, e, this->retInternal_);
135  this->setIgnored(this->retInternal_);
136  return (bool)this->retInternal_;
137  }
138 
139  const unsigned char* md55Raw() const { return id_md5_; }
140  bool operator==(const VersionedSelector& other) const {
141  constexpr unsigned length = MD5_DIGEST_LENGTH;
142  return ( 0 == memcmp(id_md5_,other.id_md5_,length*sizeof(unsigned char)) );
143  }
144  const std::string& md5String() const { return md5_string_; }
145 
146  const std::string& name() const { return name_; }
147 
148  const unsigned howFarInCutFlow() const { return howfar_; }
149 
150  const unsigned bitMap() const { return bitmap_; }
151 
152  const size_t cutFlowSize() const { return cuts_.size(); }
153 
155 
156  void initialize(const edm::ParameterSet&);
157 
159 
160  protected:
162  std::vector<SHARED_PTR(candf::CandidateCut) > cuts_;
163  std::vector<bool> needs_event_content_;
164  std::vector<typename Selector<T>::index_type> cut_indices_;
165  unsigned howfar_, bitmap_;
166  std::vector<double> values_;
167 
168  private:
169  unsigned char id_md5_[MD5_DIGEST_LENGTH];
171 };
172 
173 template<class T>
176  if(initialized_) {
177  edm::LogWarning("VersionedPatElectronSelector")
178  << "ID was already initialized!";
179  return;
180  }
181  const std::vector<edm::ParameterSet>& cutflow =
182  conf.getParameterSetVector("cutFlow");
183  if( cutflow.size() == 0 ) {
184  throw cms::Exception("InvalidCutFlow")
185  << "You have supplied a null/empty cutflow to VersionedIDSelector,"
186  << " please add content to the cuflow and try again.";
187  }
188 
189  // this lets us keep track of cuts without knowing what they are :D
190  std::vector<edm::ParameterSet>::const_iterator cbegin(cutflow.begin()),
191  cend(cutflow.end());
192  std::vector<edm::ParameterSet>::const_iterator icut = cbegin;
193  std::map<std::string,unsigned> cut_counter;
194  for( ; icut != cend; ++icut ) {
195  const std::string& cname = icut->getParameter<std::string>("cutName");
196  const bool needsContent =
197  icut->getParameter<bool>("needsAdditionalProducts");
198  const bool ignored = icut->getParameter<bool>("isIgnored");
199  candf::CandidateCut* plugin = nullptr;
200  CINT_GUARD(plugin = CutApplicatorFactory::get()->create(cname,*icut));
201  if( plugin != nullptr ) {
202  cuts_.push_back(SHARED_PTR(candf::CandidateCut)(plugin));
203  } else {
204  throw cms::Exception("BadPluginName")
205  << "The requested cut: " << cname << " is not available!";
206  }
207  needs_event_content_.push_back(needsContent);
208  const std::string& name = plugin->name();
209  std::stringstream realname;
210  if( !cut_counter.count(name) ) cut_counter[name] = 0;
211  realname << name << "_" << cut_counter[name];
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 = cuts_[std::distance(cbegin,icut)]->name();
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
tuple t
Definition: tree.py:139
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