CMS 3D CMS Logo

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(__CLING__))
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 
26 #define SHARED_PTR(T) std::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 
52 
53  name_ = conf.getParameter<std::string>("idName");
54 
55  // now setup the md5 and cute accessor functions
56  constexpr unsigned length = MD5_DIGEST_LENGTH;
57  std::string tracked(conf.trackedPart().dump());
58  memset(id_md5_, 0, length * sizeof(unsigned char));
59  MD5((unsigned char*)tracked.c_str(), tracked.size(), id_md5_);
60  char buf[32];
61  for (unsigned i = 0; i < MD5_DIGEST_LENGTH; ++i) {
62  sprintf(buf, "%02x", id_md5_[i]);
63  md5_string_.append(buf);
64  }
65  initialize(conf);
66  this->retInternal_ = this->getBitTemplate();
67  }
68 
69  bool operator()(const T& ref, pat::strbitset& ret) CINT_GUARD(final) {
70  howfar_ = 0;
71  bitmap_ = 0;
72  values_.clear();
73  bool failed = false;
74  if (!initialized_) {
75  throw cms::Exception("CutNotInitialized") << "VersionedGsfElectronSelector not initialized!" << std::endl;
76  }
77  for (unsigned i = 0; i < cuts_.size(); ++i) {
79  const bool result = (*cuts_[i])(temp);
80  values_.push_back(cuts_[i]->value(temp));
81  if (result || this->ignoreCut(cut_indices_[i])) {
82  this->passCut(ret, cut_indices_[i]);
83  bitmap_ |= 1 << i;
84  if (!failed)
85  ++howfar_;
86  } else {
87  failed = true;
88  }
89  }
90  this->setIgnored(ret);
91  return (bool)ret;
92  }
93 
94  bool operator()(const T& ref, edm::EventBase const& e, pat::strbitset& ret) CINT_GUARD(final) {
95  // setup isolation needs
96  for (size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i) {
97  if (needs_event_content_[i]) {
98  CutApplicatorWithEventContentBase* needsEvent = static_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
99  needsEvent->getEventContent(e);
100  }
101  }
102  return this->operator()(ref, ret);
103  }
104 
105  //repeat the other operator() we left out here
106  //in the base class here so they are exposed to ROOT
107 
108  /* VID BY VALUE */
109  bool operator()(typename T::value_type const& t) {
110  const T temp(&t, 0); // assuming T is edm::Ptr
111  return this->operator()(temp);
112  }
113 
114  bool operator()(typename T::value_type const& t, edm::EventBase const& e) {
115  const T temp(&t, 0);
116  return this->operator()(temp, e);
117  }
118 
119  bool operator()(T const& t) CINT_GUARD(final) {
120  this->retInternal_.set(false);
121  this->operator()(t, this->retInternal_);
122  this->setIgnored(this->retInternal_);
123  return (bool)this->retInternal_;
124  }
125 
126  bool operator()(T const& t, edm::EventBase const& e) CINT_GUARD(final) {
127  this->retInternal_.set(false);
128  this->operator()(t, e, this->retInternal_);
129  this->setIgnored(this->retInternal_);
130  return (bool)this->retInternal_;
131  }
132 
133  const unsigned char* md55Raw() const { return id_md5_; }
134  bool operator==(const VersionedSelector& other) const {
135  constexpr unsigned length = MD5_DIGEST_LENGTH;
136  return (0 == memcmp(id_md5_, other.id_md5_, length * sizeof(unsigned char)));
137  }
138  const std::string& md5String() const { return md5_string_; }
139 
140  const std::string& name() const { return name_; }
141 
142  const unsigned howFarInCutFlow() const { return howfar_; }
143 
144  const unsigned bitMap() const { return bitmap_; }
145 
146  const size_t cutFlowSize() const { return cuts_.size(); }
147 
149 
150  void initialize(const edm::ParameterSet&);
151 
153 
154 private:
155  //here we check that the parameters of the VID cuts are tracked
156  //we allow exactly one parameter to be untracked "isPOGApproved"
157  //as if its tracked, its a pain for the md5Sums
158  //due to the mechanics of PSets, it was demined easier just to
159  //create a new config which doesnt have an untracked isPOGApproved
160  //if isPOGApproved is tracked (if we decide to do that in the future), it keeps it
161  //see https://github.com/cms-sw/cmssw/issues/19799 for the discussion
162  static void validateParamsAreTracked(const edm::ParameterSet& conf) {
163  edm::ParameterSet trackedPart = conf.trackedPart();
164  edm::ParameterSet confWithoutIsPOGApproved;
165  for (auto& paraName : conf.getParameterNames()) {
166  if (paraName != "isPOGApproved")
167  confWithoutIsPOGApproved.copyFrom(conf, paraName);
168  else if (conf.existsAs<bool>(paraName, true))
169  confWithoutIsPOGApproved.copyFrom(conf, paraName); //adding isPOGApproved if its a tracked bool
170  }
171  std::string tracked(conf.trackedPart().dump()), untracked(confWithoutIsPOGApproved.dump());
172  if (tracked != untracked) {
173  throw cms::Exception("InvalidConfiguration") << "VersionedSelector does not allow untracked parameters"
174  << " in the cutflow ParameterSet!";
175  }
176  }
177 
178 protected:
181  std::vector<bool> needs_event_content_;
182  std::vector<typename Selector<T>::index_type> cut_indices_;
183  unsigned howfar_, bitmap_;
184  std::vector<double> values_;
185 
186 private:
187  unsigned char id_md5_[MD5_DIGEST_LENGTH];
189 };
190 
191 template <class T>
193  if (initialized_) {
194  edm::LogWarning("VersionedPatElectronSelector") << "ID was already initialized!";
195  return;
196  }
197  const std::vector<edm::ParameterSet>& cutflow = conf.getParameterSetVector("cutFlow");
198  if (cutflow.empty()) {
199  throw cms::Exception("InvalidCutFlow") << "You have supplied a null/empty cutflow to VersionedIDSelector,"
200  << " please add content to the cuflow and try again.";
201  }
202 
203  // this lets us keep track of cuts without knowing what they are :D
204  std::vector<edm::ParameterSet>::const_iterator cbegin(cutflow.begin()), cend(cutflow.end());
205  std::vector<edm::ParameterSet>::const_iterator icut = cbegin;
206  std::map<std::string, unsigned> cut_counter;
207  std::vector<std::string> ignored_cuts;
208  for (; icut != cend; ++icut) {
209  std::stringstream realname;
210  const std::string& name = icut->getParameter<std::string>("cutName");
211  if (!cut_counter.count(name))
212  cut_counter[name] = 0;
213  realname << name << "_" << cut_counter[name];
214  const bool needsContent = icut->getParameter<bool>("needsAdditionalProducts");
215  const bool ignored = icut->getParameter<bool>("isIgnored");
216  CINT_GUARD(cuts_.emplace_back(CutApplicatorFactory::get()->create(name, *icut)));
217  needs_event_content_.push_back(needsContent);
218  const std::string therealname = realname.str();
219  this->push_back(therealname);
220  this->set(therealname);
221  if (ignored)
222  ignored_cuts.push_back(therealname);
223  cut_counter[name]++;
224  }
225  this->setIgnoredCuts(ignored_cuts);
226 
227  //have to loop again to set cut indices after all are filled
228  icut = cbegin;
229  cut_counter.clear();
230  for (; icut != cend; ++icut) {
231  std::stringstream realname;
232  const std::string& name = cuts_[std::distance(cbegin, icut)]->name();
233  if (!cut_counter.count(name))
234  cut_counter[name] = 0;
235  realname << name << "_" << cut_counter[name];
236  cut_indices_.push_back(typename Selector<T>::index_type(&(this->bits_), realname.str()));
237  cut_counter[name]++;
238  }
239 
240  initialized_ = true;
241 }
242 
243 #ifdef REGULAR_CPLUSPLUS
245 template <class T>
247  std::map<std::string, unsigned> names_to_index;
248  std::map<std::string, unsigned> cut_counter;
249  for (unsigned idx = 0; idx < cuts_.size(); ++idx) {
250  const std::string& name = cuts_[idx]->name();
251  if (!cut_counter.count(name))
252  cut_counter[name] = 0;
253  std::stringstream realname;
254  realname << name << "_" << cut_counter[name];
255  names_to_index.emplace(realname.str(), idx);
256  cut_counter[name]++;
257  }
258  return vid::CutFlowResult(name_, md5_string_, names_to_index, values_, bitmap_);
259 }
260 
262 template <class T>
264  for (size_t i = 0, cutssize = cuts_.size(); i < cutssize; ++i) {
265  if (needs_event_content_[i]) {
266  CutApplicatorWithEventContentBase* needsEvent = dynamic_cast<CutApplicatorWithEventContentBase*>(cuts_[i].get());
267  if (nullptr != needsEvent) {
268  needsEvent->setConsumes(cc);
269  } else {
270  throw cms::Exception("InvalidCutConfiguration") << "Cut: " << ((CutApplicatorBase*)cuts_[i].get())->name()
271  << " configured to consume event products but does not "
272  << " inherit from CutApplicatorWithEventContenBase "
273  << " please correct either your python or C++!";
274  }
275  }
276  }
277 }
278 #endif
279 
280 #endif
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:367
edm::ParameterSet::copyFrom
void copyFrom(ParameterSet const &from, std::string const &name)
Definition: ParameterSet.cc:444
VersionedSelector::operator()
bool operator()(const T &ref, pat::strbitset &ret) final
This provides the interface for base classes to select objects.
Definition: VersionedSelector.h:69
candidate_functions::CandidateCut
Definition: CandidateCut.h:8
Selector::setIgnored
void setIgnored(pat::strbitset &ret)
set ignored bits
Definition: Selector.h:181
Selector
Functor that operates on <T>
Definition: Selector.h:22
mps_fire.i
i
Definition: mps_fire.py:428
funct::false
false
Definition: Factorize.h:29
vid
Definition: VIDCutFlowResult.h:25
VersionedSelector::operator()
bool operator()(typename T::value_type const &t)
Definition: VersionedSelector.h:109
VersionedSelector::operator()
bool operator()(typename T::value_type const &t, edm::EventBase const &e)
Definition: VersionedSelector.h:114
pat::strbitset::set
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:126
Selector::retInternal_
pat::strbitset retInternal_
internal ret if users don't care about return bits
Definition: Selector.h:242
Selector::ignoreCut
bool ignoreCut(std::string const &s) const
ignore the cut at index "s"
Definition: Selector.h:127
VersionedSelector::values_
std::vector< double > values_
Definition: VersionedSelector.h:184
pat::strbitset::index_type
Definition: strbitset.h:25
runEdmFileComparison.failed
failed
Definition: runEdmFileComparison.py:225
VersionedSelector::cuts_
std::vector< std::shared_ptr< candf::CandidateCut > > cuts_
Definition: VersionedSelector.h:180
VersionedSelector::name
const std::string & name() const
Definition: VersionedSelector.h:140
edm::ParameterSet::existsAs
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:171
CINT_GUARD
#define CINT_GUARD(CODE)
Definition: VersionedSelector.h:17
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
VersionedSelector::bitMap
const unsigned bitMap() const
Definition: VersionedSelector.h:144
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
vid::CutFlowResult
Definition: VIDCutFlowResult.h:41
VersionedSelector::needs_event_content_
std::vector< bool > needs_event_content_
Definition: VersionedSelector.h:181
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
VersionedSelector::VersionedSelector
VersionedSelector(const edm::ParameterSet &conf)
Definition: VersionedSelector.h:50
VersionedSelector::name_
std::string name_
Definition: VersionedSelector.h:188
candidate_functions
Definition: CandidateCut.h:7
VersionedSelector::initialized_
bool initialized_
Definition: VersionedSelector.h:179
edm::ParameterSet::dump
std::string dump(unsigned int indent=0) const
Definition: ParameterSet.cc:832
VersionedSelector::md5String
const std::string & md5String() const
Definition: VersionedSelector.h:138
CutApplicatorWithEventContentBase.h
VersionedSelector::VersionedSelector
VersionedSelector()
Definition: VersionedSelector.h:48
VersionedSelector
cut-flow versioning info in the event provenance
Definition: VersionedSelector.h:46
CutApplicatorWithEventContentBase::setConsumes
virtual void setConsumes(edm::ConsumesCollector &)=0
trackingPlots.other
other
Definition: trackingPlots.py:1467
VersionedSelector::operator==
bool operator==(const VersionedSelector &other) const
Definition: VersionedSelector.h:134
CutApplicatorWithEventContentBase::getEventContent
virtual void getEventContent(const edm::EventBase &)=0
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
VersionedSelector::initialize
void initialize(const edm::ParameterSet &)
Definition: VersionedSelector.h:192
Selector::getBitTemplate
pat::strbitset getBitTemplate() const
Get an empty bitset with the proper names.
Definition: Selector.h:168
edm::ParameterSet
Definition: ParameterSet.h:47
VersionedSelector::md5_string_
std::string md5_string_
Definition: VersionedSelector.h:188
CandidateCut.h
Selector::passCut
void passCut(pat::strbitset &ret, std::string const &s)
Passing cuts.
Definition: Selector.h:142
VersionedSelector::md55Raw
const unsigned char * md55Raw() const
Definition: VersionedSelector.h:133
VersionedSelector::cutFlowSize
const size_t cutFlowSize() const
Definition: VersionedSelector.h:146
edm::ParameterSet::getParameterNames
std::vector< std::string > getParameterNames() const
Definition: ParameterSet.cc:663
VersionedSelector::cutFlowResult
vid::CutFlowResult cutFlowResult() const
Definition: VersionedSelector.h:246
VersionedSelector::operator()
bool operator()(const T &ref, edm::EventBase const &e, pat::strbitset &ret) final
This provides an alternative signature that includes extra information.
Definition: VersionedSelector.h:94
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
VersionedSelector::operator()
bool operator()(T const &t, edm::EventBase const &e) final
This provides an alternative signature that includes extra information.
Definition: VersionedSelector.h:126
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
get
#define get
cc
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
SHARED_PTR
#define SHARED_PTR(T)
Definition: VersionedSelector.h:20
edm::Ptr< Candidate >
VersionedSelector::setConsumes
void setConsumes(edm::ConsumesCollector)
Definition: VersionedSelector.h:263
CutApplicatorBase
Definition: CutApplicatorBase.h:45
pat::strbitset
Definition: strbitset.h:23
VIDCutFlowResult.h
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
edm::EventBase
Definition: EventBase.h:46
Exception
Definition: hltDiff.cc:246
VersionedSelector::id_md5_
unsigned char id_md5_[MD5_DIGEST_LENGTH]
Definition: VersionedSelector.h:187
edm::ParameterSet::trackedPart
ParameterSet trackedPart() const
Definition: ParameterSet.cc:686
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Types.untracked
untracked
Definition: Types.py:35
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2160
VersionedSelector::cut_indices_
std::vector< typename Selector< T >::index_type > cut_indices_
Definition: VersionedSelector.h:182
mps_fire.result
result
Definition: mps_fire.py:311
ConsumesCollector.h
Selector.h
ParameterSet.h
CutApplicatorBase.h
HLT_FULL_cff.distance
distance
Definition: HLT_FULL_cff.py:7796
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
VersionedSelector::validateParamsAreTracked
static void validateParamsAreTracked(const edm::ParameterSet &conf)
Definition: VersionedSelector.h:162
VersionedSelector::howfar_
unsigned howfar_
Definition: VersionedSelector.h:183
VersionedSelector::howFarInCutFlow
const unsigned howFarInCutFlow() const
Definition: VersionedSelector.h:142
CutApplicatorWithEventContentBase
Definition: CutApplicatorWithEventContentBase.h:19
VersionedSelector::operator()
bool operator()(T const &t) final
This provides an alternative signature without the second ret.
Definition: VersionedSelector.h:119
VersionedSelector::bitmap_
unsigned bitmap_
Definition: VersionedSelector.h:183
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37