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  std::vector<std::string> ignored_cuts;
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) ignored_cuts.push_back(therealname);
216  cut_counter[name]++;
217  }
218  this->setIgnoredCuts(ignored_cuts);
219 
220  //have to loop again to set cut indices after all are filled
221  icut = cbegin;
222  cut_counter.clear();
223  for( ; icut != cend; ++icut ) {
224  std::stringstream realname;
225  const std::string& name = cuts_[std::distance(cbegin,icut)]->name();
226  if( !cut_counter.count(name) ) cut_counter[name] = 0;
227  realname << name << "_" << cut_counter[name];
228  cut_indices_.push_back(typename Selector<T>::index_type(&(this->bits_),realname.str()));
229  cut_counter[name]++;
230  }
231 
232  initialized_ = true;
233 }
234 
235 
236 
237 #ifdef REGULAR_CPLUSPLUS
239 template<class T>
241  std::map<std::string,unsigned> names_to_index;
242  std::map<std::string,unsigned> cut_counter;
243  for( unsigned idx = 0; idx < cuts_.size(); ++idx ) {
244  const std::string& name = cuts_[idx]->name();
245  if( !cut_counter.count(name) ) cut_counter[name] = 0;
246  std::stringstream realname;
247  realname << name << "_" << cut_counter[name];
248  names_to_index.emplace(realname.str(),idx);
249  cut_counter[name]++;
250  }
251  return vid::CutFlowResult(name_,md5_string_,names_to_index,values_,bitmap_);
252 }
253 
255 template<class T>
257  for( size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i ) {
258  if( needs_event_content_[i] ) {
259  CutApplicatorWithEventContentBase* needsEvent =
260  dynamic_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
261  if( nullptr != needsEvent ) {
262  needsEvent->setConsumes(cc);
263  } else {
264  throw cms::Exception("InvalidCutConfiguration")
265  << "Cut: " << ((CutApplicatorBase*)cuts_[i].get())->name()
266  << " configured to consume event products but does not "
267  << " inherit from CutApplicatorWithEventContenBase "
268  << " please correct either your python or C++!";
269  }
270  }
271  }
272 }
273 #endif
274 
275 #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
tuple ret
prodAgent to be discontinued
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
tuple result
Definition: mps_fire.py:83
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
const unsigned bitMap() const
Functor that operates on &lt;T&gt;
Definition: Selector.h:24
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