CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Attributes
TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass > Class Template Reference
Inheritance diagram for TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >:

Classes

struct  DiscriminantCutEntry
 

Public Member Functions

void beginEvent (const edm::Event &event, const edm::EventSetup &eventSetup) override
 
reco::SingleTauDiscriminatorContainer discriminate (const TauTypeRef &) const override
 
 TauDiscriminantCutMultiplexerT (const edm::ParameterSet &pset)
 
 ~TauDiscriminantCutMultiplexerT () override
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Types

typedef std::map< int, std::vector< std::unique_ptr< DiscriminantCutEntry > > > DiscriminantCutMap
 

Private Attributes

DiscriminantCutMap cuts_
 
edm::ESGetToken< PhysicsTFormulaPayload, PhysicsTFormulaPayloadRcdformulaToken_
 
edm::FileInPath inputFileName_
 
bool isInitialized_
 
bool loadMVAfromDB_
 
std::string moduleLabel_
 
std::unique_ptr< const TFormula > mvaOutput_normalization_
 
std::string mvaOutputNormalizationName_
 
uint n_raws_
 
int raw_category_idx_ = -1
 
int raw_discriminator_idx_ = -1
 
edm::InputTag toMultiplex_
 
edm::EDGetTokenT< reco::TauDiscriminatorContainertoMultiplex_token
 
edm::Handle< reco::TauDiscriminatorContainertoMultiplexHandle_
 
int verbosity_
 

Detailed Description

template<class TauType, class TauTypeRef, class ParentClass>
class TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >

Definition at line 38 of file TauDiscriminantCutMultiplexer.cc.

Member Typedef Documentation

◆ DiscriminantCutMap

template<class TauType , class TauTypeRef , class ParentClass >
typedef std::map<int, std::vector<std::unique_ptr<DiscriminantCutEntry> > > TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::DiscriminantCutMap
private

Definition at line 65 of file TauDiscriminantCutMultiplexer.cc.

Constructor & Destructor Documentation

◆ TauDiscriminantCutMultiplexerT()

template<class TauType , class TauTypeRef , class ParentClass >
TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::TauDiscriminantCutMultiplexerT ( const edm::ParameterSet pset)
explicit

Definition at line 138 of file TauDiscriminantCutMultiplexer.cc.

References taus_updatedMVAIds_cff::category, looper::cfg, gather_cfg::cout, PA_MinBiasSkim_cff::cut, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::cuts_, deDxTools::esConsumes(), Exception, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::formulaToken_, mps_fire::i, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::inputFileName_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::DiscriminantCutEntry::kFixedCut, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::DiscriminantCutEntry::kVariableCut, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::loadMVAfromDB_, taus_updatedMVAIds_cff::mapping, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::moduleLabel_, eostools::move(), TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::mvaOutputNormalizationName_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::n_raws_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::raw_category_idx_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::raw_discriminator_idx_, AlCaHLTBitMon_QueryRunRegistry::string, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::toMultiplex_, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::toMultiplex_token, parallelization::uint, TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::verbosity_, HistogramManager_cfi::VPSet(), PDWG_TauSkim_cff::workingPoints, and pfClustersFromHGC3DClusters_cfi::wp.

140  : ParentClass(cfg),
141  moduleLabel_(cfg.getParameter<std::string>("@module_label")),
143  isInitialized_(false) {
144  toMultiplex_ = cfg.getParameter<edm::InputTag>("toMultiplex");
145  toMultiplex_token = this->template consumes<reco::TauDiscriminatorContainer>(toMultiplex_);
146 
147  verbosity_ = cfg.getParameter<int>("verbosity");
148 
149  mvaOutputNormalizationName_ = cfg.getParameter<std::string>("mvaOutput_normalization");
150 
151  loadMVAfromDB_ = cfg.getParameter<bool>("loadMVAfromDB");
152  if (!loadMVAfromDB_) {
153  inputFileName_ = cfg.getParameter<edm::FileInPath>("inputFileName");
154  } else if (not mvaOutputNormalizationName_.empty()) {
156  }
157  if (verbosity_)
158  std::cout << moduleLabel_ << " loadMVA = " << loadMVAfromDB_ << std::endl;
159 
160  // Setup our cut map, first raw values then working points
161  typedef std::vector<edm::ParameterSet> VPSet;
162  typedef std::vector<std::string> VString;
163  typedef std::vector<double> VDouble;
164  VString rawValueConfig = cfg.getParameter<VString>("rawValues");
165  for (uint i = 0; i < rawValueConfig.size(); i++) {
166  if (rawValueConfig[i] == "discriminator")
168  else if (rawValueConfig[i] == "category")
170  else
171  throw cms::Exception("TauDiscriminantCutMultiplexerT")
172  << " Configuration Parameter 'rawValues' containes unknown values. Must be 'discriminator' or 'category'!!\n";
173  }
174  n_raws_ = rawValueConfig.size();
175  VPSet mapping = cfg.getParameter<VPSet>("mapping");
176  for (auto const& mappingEntry : mapping) {
177  unsigned category = mappingEntry.getParameter<uint32_t>("category");
178  std::vector<std::unique_ptr<DiscriminantCutEntry>> cutWPs;
179  std::string categoryname = mappingEntry.getParameter<std::string>("cut");
180  bool localWPs = false;
181  bool wpsAsDouble = false;
182  if (mappingEntry.exists("workingPoints")) {
183  localWPs = true;
184  if (mappingEntry.existsAs<VDouble>("workingPoints")) {
185  wpsAsDouble = true;
186  } else if (mappingEntry.existsAs<VString>("workingPoints")) {
187  wpsAsDouble = false;
188  } else {
189  throw cms::Exception("TauDiscriminantCutMultiplexerT")
190  << " Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
191  }
192  } else if (cfg.exists("workingPoints")) {
193  localWPs = false;
194  if (cfg.existsAs<VDouble>("workingPoints")) {
195  wpsAsDouble = true;
196  } else if (cfg.existsAs<VString>("workingPoints")) {
197  wpsAsDouble = false;
198  } else {
199  throw cms::Exception("TauDiscriminantCutMultiplexerT")
200  << " Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
201  }
202  } else {
203  throw cms::Exception("TauDiscriminantCutMultiplexerT")
204  << " Undefined Configuration Parameter 'workingPoints' !!\n";
205  }
206  if (wpsAsDouble) {
207  VDouble workingPoints;
208  if (localWPs)
209  workingPoints = mappingEntry.getParameter<VDouble>("workingPoints");
210  else
211  workingPoints = cfg.getParameter<VDouble>("workingPoints");
212  for (auto const& wp : workingPoints) {
213  std::unique_ptr<DiscriminantCutEntry> cut{new DiscriminantCutEntry()};
214  cut->cutValue_ = wp;
216  cutWPs.push_back(std::move(cut));
217  }
218  } else {
219  VString workingPoints;
220  if (localWPs)
221  workingPoints = mappingEntry.getParameter<VString>("workingPoints");
222  else
223  workingPoints = cfg.getParameter<VString>("workingPoints");
224  for (auto const& wp : workingPoints) {
225  std::unique_ptr<DiscriminantCutEntry> cut{new DiscriminantCutEntry()};
226  cut->cutName_ = categoryname + wp;
227  if (loadMVAfromDB_) {
228  cut->cutToken_ = this->esConsumes(edm::ESInputTag{"", cut->cutName_});
229  }
230  std::string cutVariable_string = mappingEntry.getParameter<std::string>("variable");
231  cut->cutVariable_.reset(new StringObjectFunction<TauType>(cutVariable_string));
233  cutWPs.push_back(std::move(cut));
234  }
235  }
236  cuts_[category] = std::move(cutWPs);
237  }
238 
239  verbosity_ = cfg.getParameter<int>("verbosity");
240  if (verbosity_)
241  std::cout << "constructed " << moduleLabel_ << std::endl;
242 }
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::EDGetTokenT< reco::TauDiscriminatorContainer > toMultiplex_token
edm::ESGetToken< PhysicsTFormulaPayload, PhysicsTFormulaPayloadRcd > formulaToken_
std::unique_ptr< const TFormula > mvaOutput_normalization_
def move(src, dest)
Definition: eostools.py:511

◆ ~TauDiscriminantCutMultiplexerT()

template<class TauType , class TauTypeRef , class ParentClass >
TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::~TauDiscriminantCutMultiplexerT ( )
override

Definition at line 245 of file TauDiscriminantCutMultiplexer.cc.

245 {}

Member Function Documentation

◆ beginEvent()

template<class TauType , class TauTypeRef , class ParentClass >
void TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::beginEvent ( const edm::Event event,
const edm::EventSetup eventSetup 
)
override

Definition at line 248 of file TauDiscriminantCutMultiplexer.cc.

References gather_cfg::cout, PA_MinBiasSkim_cff::cut, edm::Event::getByToken(), dtResolutionTest_cfi::inputFile, eostools::move(), and groupFilesInBlocks::temp.

249  {
250  if (verbosity_)
251  std::cout << " begin! " << moduleLabel_ << " " << isInitialized_ << std::endl;
252  if (!isInitialized_) {
253  //Only open the file once and we can close it when this routine is done
254  // since all objects gotten from the file will have been copied
255  std::unique_ptr<TFile> inputFile;
256  if (!mvaOutputNormalizationName_.empty()) {
257  if (!loadMVAfromDB_) {
258  inputFile = openInputFile(inputFileName_);
259  mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*inputFile, mvaOutputNormalizationName_);
260  } else {
261  auto temp = loadTFormulaFromDB(es,
264  Form("%s_mvaOutput_normalization", moduleLabel_.data()),
265  verbosity_);
267  }
268  }
269  for (auto const& cutWPs : cuts_) {
270  for (auto const& cut : cutWPs.second) {
271  if (cut->mode_ == DiscriminantCutEntry::kVariableCut) {
272  if (!loadMVAfromDB_) {
273  if (not inputFile) {
274  inputFile = openInputFile(inputFileName_);
275  }
276  if (verbosity_)
277  std::cout << "Loading from file" << inputFileName_ << std::endl;
278  cut->cutFunction_ = loadObjectFromFile<TGraph>(*inputFile, cut->cutName_);
279  } else {
280  if (verbosity_)
281  std::cout << "Loading from DB" << std::endl;
282  cut->cutFunction_ = loadTGraphFromDB(es, cut->cutName_, cut->cutToken_, verbosity_);
283  }
284  }
285  }
286  }
287  isInitialized_ = true;
288  }
289 
290  evt.getByToken(toMultiplex_token, toMultiplexHandle_);
291 }
edm::EDGetTokenT< reco::TauDiscriminatorContainer > toMultiplex_token
edm::ESGetToken< PhysicsTFormulaPayload, PhysicsTFormulaPayloadRcd > formulaToken_
edm::Handle< reco::TauDiscriminatorContainer > toMultiplexHandle_
std::unique_ptr< const TFormula > mvaOutput_normalization_
def move(src, dest)
Definition: eostools.py:511

◆ discriminate()

template<class TauType , class TauTypeRef , class ParentClass >
reco::SingleTauDiscriminatorContainer TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::discriminate ( const TauTypeRef &  tau) const
override

Definition at line 294 of file TauDiscriminantCutMultiplexer.cc.

References gather_cfg::cout, geometryDiff::epsilon, PDWG_TauSkim_cff::rawValues, mps_fire::result, metsig::tau, multiplicitycorr_cfi::xMax, and photonAnalyzer_cfi::xMin.

295  {
296  if (verbosity_) {
297  std::cout << "<TauDiscriminantCutMultiplexerT::discriminate>:" << std::endl;
298  std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
299  }
300 
302  result.rawValues.resize(n_raws_);
303  double disc_result = (*toMultiplexHandle_)[tau].rawValues.at(0);
304  if (verbosity_) {
305  std::cout << "disc_result = " << disc_result << std::endl;
306  }
307  if (raw_discriminator_idx_ >= 0)
308  result.rawValues[raw_discriminator_idx_] = disc_result;
310  disc_result = mvaOutput_normalization_->Eval(disc_result);
311  if (verbosity_) {
312  std::cout << "disc_result (normalized) = " << disc_result << std::endl;
313  }
314  }
315  double key_result = 0.0;
316  if ((*toMultiplexHandle_)[tau].rawValues.size() == 2) {
317  key_result = (*toMultiplexHandle_)[tau].rawValues.at(1);
318  if (raw_category_idx_ >= 0)
319  result.rawValues[raw_category_idx_] = key_result;
320  }
321  typename DiscriminantCutMap::const_iterator cutWPsIter = cuts_.find(std::round(key_result));
322 
323  // Return null if it doesn't exist
324  if (cutWPsIter == cuts_.end()) {
325  return result;
326  }
327  // See if the discriminator passes our cuts
328  for (auto const& cutIter : cutWPsIter->second) {
329  bool passesCuts = false;
330  if (cutIter->mode_ == DiscriminantCutEntry::kFixedCut) {
331  passesCuts = (disc_result > cutIter->cutValue_);
332  if (verbosity_) {
333  std::cout << "cutValue (fixed) = " << cutIter->cutValue_ << " --> passesCuts = " << passesCuts << std::endl;
334  }
335  } else if (cutIter->mode_ == DiscriminantCutEntry::kVariableCut) {
336  double cutVariable = (*cutIter->cutVariable_)(*tau);
337  double xMin, xMax, dummy;
338  cutIter->cutFunction_->GetPoint(0, xMin, dummy);
339  cutIter->cutFunction_->GetPoint(cutIter->cutFunction_->GetN() - 1, xMax, dummy);
340  const double epsilon = 1.e-3;
341  if (cutVariable < (xMin + epsilon))
342  cutVariable = xMin + epsilon;
343  else if (cutVariable > (xMax - epsilon))
344  cutVariable = xMax - epsilon;
345  double cutValue = cutIter->cutFunction_->Eval(cutVariable);
346  passesCuts = (disc_result > cutValue);
347  if (verbosity_) {
348  std::cout << "cutValue (@" << cutVariable << ") = " << cutValue << " --> passesCuts = " << passesCuts
349  << std::endl;
350  }
351  }
352  result.workingPoints.push_back(passesCuts);
353  }
354  return result;
355 }
edm::Handle< reco::TauDiscriminatorContainer > toMultiplexHandle_
std::unique_ptr< const TFormula > mvaOutput_normalization_

◆ fillDescriptions()

template<class TauType , class TauTypeRef , class ParentClass >
void TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 376 of file TauDiscriminantCutMultiplexer.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::add(), edm::ParameterSet::addParameter(), submitPVResolutionJobs::desc, HLT_2022v14_cff::InputTag, and AlCaHLTBitMon_QueryRunRegistry::string.

377  {
378  // recoTauDiscriminantCutMultiplexer
380  desc.add<edm::InputTag>("toMultiplex", edm::InputTag("fixme"));
381  desc.add<int>("verbosity", 0);
382 
383  {
384  edm::ParameterSet pset_mapping;
385  pset_mapping.addParameter<unsigned int>("category", 0);
386  pset_mapping.addParameter<std::string>("cut", "fixme");
387  edm::ParameterSetDescription desc_mapping;
388  desc_mapping.add<unsigned int>("category", 0);
389  desc_mapping.add<std::string>("cut");
390  // it seems the parameter string "variable" exists only when workingPoints are string
391  // see hpsPFTauDiscriminationByVLooseIsolationMVArun2v1DBdR03oldDMwLT in RecoTauTag/Configuration/python/HPSPFTaus_cff.py
392  desc_mapping.addOptional<std::string>("variable")
393  ->setComment("the parameter is required when \"workingPoints\" are string");
394  std::vector<edm::ParameterSet> vpsd_mapping;
395  vpsd_mapping.push_back(pset_mapping);
396  desc.addVPSet("mapping", desc_mapping, vpsd_mapping);
397  }
398 
399  std::vector<std::string> defaultRaws{"discriminator"};
400  desc.add<std::vector<std::string>>("rawValues", defaultRaws);
401  std::vector<double> defaultWP{0.0};
402  desc.addNode(edm::ParameterDescription<std::vector<double>>("workingPoints", defaultWP, true) xor
403  edm::ParameterDescription<std::vector<std::string>>("workingPoints", true));
404  desc.add<edm::FileInPath>("inputFileName", edm::FileInPath("RecoTauTag/RecoTau/data/emptyMVAinputFile"));
405  desc.add<bool>("loadMVAfromDB", true);
406  ParentClass::fillProducerDescriptions(desc); // inherited from the base
407  desc.add<std::string>("mvaOutput_normalization", "");
408  descriptions.add(getDefaultConfigString<TauType>(), desc);
409 }
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:135
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)

Member Data Documentation

◆ cuts_

template<class TauType , class TauTypeRef , class ParentClass >
DiscriminantCutMap TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::cuts_
private

◆ formulaToken_

template<class TauType , class TauTypeRef , class ParentClass >
edm::ESGetToken<PhysicsTFormulaPayload, PhysicsTFormulaPayloadRcd> TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::formulaToken_
private

◆ inputFileName_

template<class TauType , class TauTypeRef , class ParentClass >
edm::FileInPath TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::inputFileName_
private

◆ isInitialized_

template<class TauType , class TauTypeRef , class ParentClass >
bool TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::isInitialized_
private

Definition at line 75 of file TauDiscriminantCutMultiplexer.cc.

◆ loadMVAfromDB_

template<class TauType , class TauTypeRef , class ParentClass >
bool TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::loadMVAfromDB_
private

◆ moduleLabel_

template<class TauType , class TauTypeRef , class ParentClass >
std::string TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::moduleLabel_
private

◆ mvaOutput_normalization_

template<class TauType , class TauTypeRef , class ParentClass >
std::unique_ptr<const TFormula> TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::mvaOutput_normalization_
private

Definition at line 73 of file TauDiscriminantCutMultiplexer.cc.

◆ mvaOutputNormalizationName_

template<class TauType , class TauTypeRef , class ParentClass >
std::string TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::mvaOutputNormalizationName_
private

◆ n_raws_

template<class TauType , class TauTypeRef , class ParentClass >
uint TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::n_raws_
private

◆ raw_category_idx_

template<class TauType , class TauTypeRef , class ParentClass >
int TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::raw_category_idx_ = -1
private

◆ raw_discriminator_idx_

template<class TauType , class TauTypeRef , class ParentClass >
int TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::raw_discriminator_idx_ = -1
private

◆ toMultiplex_

template<class TauType , class TauTypeRef , class ParentClass >
edm::InputTag TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::toMultiplex_
private

◆ toMultiplex_token

template<class TauType , class TauTypeRef , class ParentClass >
edm::EDGetTokenT<reco::TauDiscriminatorContainer> TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::toMultiplex_token
private

◆ toMultiplexHandle_

template<class TauType , class TauTypeRef , class ParentClass >
edm::Handle<reco::TauDiscriminatorContainer> TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::toMultiplexHandle_
private

Definition at line 78 of file TauDiscriminantCutMultiplexer.cc.

◆ verbosity_

template<class TauType , class TauTypeRef , class ParentClass >
int TauDiscriminantCutMultiplexerT< TauType, TauTypeRef, ParentClass >::verbosity_
private