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