37 template <
class TauType,
class TauTypeRef,
class ParentClass>
87 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
90 return std::make_unique<TFile>(
inputFileName.fullPath().data());
97 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
98 <<
" Failed to load Object = " <<
objectName.data() <<
" from file = " <<
inputFile.GetName() <<
" !!\n";
100 return std::unique_ptr<const T>{
static_cast<T*
>(
object->Clone())};
103 std::unique_ptr<const TGraph> loadTGraphFromDB(
107 const int& verbosity_ = 0) {
109 std::cout <<
"<loadTGraphFromDB>:" << std::endl;
110 std::cout <<
" graphName = " << graphName << std::endl;
112 return std::make_unique<TGraph>(es.
getData(graphToken));
115 std::unique_ptr<TFormula> loadTFormulaFromDB(
120 const int& verbosity_ = 0) {
122 std::cout <<
"<loadTFormulaFromDB>:" << std::endl;
123 std::cout <<
" formulaName = " << formulaName << std::endl;
125 auto const& formulaPayload = es.
getData(formulaToken_);
127 if (formulaPayload.formulas().size() == 1 && formulaPayload.limits().size() == 1) {
128 return std::make_unique<TFormula>(
newName, formulaPayload.formulas().at(0).data());
130 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadTFormulaFromDB")
131 <<
"Failed to load TFormula = " << formulaName <<
" from Database !!\n";
133 return std::unique_ptr<TFormula>{};
137 template <
class TauType,
class TauTypeRef,
class ParentClass>
141 moduleLabel_(
cfg.getParameter<
std::
string>(
"@module_label")),
142 mvaOutput_normalization_(),
143 isInitialized_(
false) {
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")
172 <<
" Configuration Parameter 'rawValues' containes unknown values. Must be 'discriminator' or 'category'!!\n";
174 n_raws_ = rawValueConfig.size();
176 for (
auto const& mappingEntry :
mapping) {
177 unsigned category = mappingEntry.getParameter<uint32_t>(
"category");
178 std::vector<std::unique_ptr<DiscriminantCutEntry>> cutWPs;
180 bool localWPs =
false;
181 bool wpsAsDouble =
false;
182 if (mappingEntry.exists(
"workingPoints")) {
184 if (mappingEntry.existsAs<VDouble>(
"workingPoints")) {
186 }
else if (mappingEntry.existsAs<VString>(
"workingPoints")) {
190 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
192 }
else if (
cfg.exists(
"workingPoints")) {
194 if (
cfg.existsAs<VDouble>(
"workingPoints")) {
196 }
else if (
cfg.existsAs<VString>(
"workingPoints")) {
200 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
204 <<
" Undefined Configuration Parameter 'workingPoints' !!\n";
209 workingPoints = mappingEntry.getParameter<VDouble>(
"workingPoints");
221 workingPoints = mappingEntry.getParameter<VString>(
"workingPoints");
226 cut->cutName_ = categoryname +
wp;
244 template <
class TauType,
class TauTypeRef,
class ParentClass>
247 template <
class TauType,
class TauTypeRef,
class ParentClass>
251 std::cout <<
" begin! " << moduleLabel_ <<
" " << isInitialized_ << std::endl;
252 if (!isInitialized_) {
256 if (!mvaOutputNormalizationName_.empty()) {
257 if (!loadMVAfromDB_) {
258 inputFile = openInputFile(inputFileName_);
259 mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*
inputFile, mvaOutputNormalizationName_);
261 auto temp = loadTFormulaFromDB(es,
262 mvaOutputNormalizationName_,
264 Form(
"%s_mvaOutput_normalization", moduleLabel_.data()),
269 for (
auto const& cutWPs : cuts_) {
270 for (
auto const&
cut : cutWPs.second) {
271 if (
cut->mode_ == DiscriminantCutEntry::kVariableCut) {
272 if (!loadMVAfromDB_) {
274 inputFile = openInputFile(inputFileName_);
277 std::cout <<
"Loading from file" << inputFileName_ << std::endl;
278 cut->cutFunction_ = loadObjectFromFile<TGraph>(*
inputFile,
cut->cutName_);
281 std::cout <<
"Loading from DB" << std::endl;
282 cut->cutFunction_ = loadTGraphFromDB(es,
cut->cutName_,
cut->cutToken_, verbosity_);
287 isInitialized_ =
true;
290 evt.
getByToken(toMultiplex_token, toMultiplexHandle_);
293 template <
class TauType,
class TauTypeRef,
class ParentClass>
295 const TauTypeRef&
tau)
const {
297 std::cout <<
"<TauDiscriminantCutMultiplexerT::discriminate>:" << std::endl;
298 std::cout <<
" moduleLabel = " << moduleLabel_ << std::endl;
302 result.rawValues.resize(n_raws_);
303 double disc_result = (*toMultiplexHandle_)[
tau].rawValues.at(0);
305 std::cout <<
"disc_result = " << disc_result << std::endl;
307 if (raw_discriminator_idx_ >= 0)
308 result.rawValues[raw_discriminator_idx_] = disc_result;
309 if (mvaOutput_normalization_) {
310 disc_result = mvaOutput_normalization_->Eval(disc_result);
312 std::cout <<
"disc_result (normalized) = " << disc_result << std::endl;
315 double key_result = 0.0;
317 key_result = (*toMultiplexHandle_)[
tau].rawValues.at(1);
318 if (raw_category_idx_ >= 0)
319 result.rawValues[raw_category_idx_] = key_result;
321 typename DiscriminantCutMap::const_iterator cutWPsIter = cuts_.find(std::round(key_result));
324 if (cutWPsIter == cuts_.end()) {
328 for (
auto const& cutIter : cutWPsIter->second) {
329 bool passesCuts =
false;
330 if (cutIter->mode_ == DiscriminantCutEntry::kFixedCut) {
331 passesCuts = (disc_result > cutIter->cutValue_);
333 std::cout <<
"cutValue (fixed) = " << cutIter->cutValue_ <<
" --> passesCuts = " << passesCuts << std::endl;
335 }
else if (cutIter->mode_ == DiscriminantCutEntry::kVariableCut) {
336 double cutVariable = (*cutIter->cutVariable_)(*
tau);
338 cutIter->cutFunction_->GetPoint(0,
xMin,
dummy);
339 cutIter->cutFunction_->GetPoint(cutIter->cutFunction_->GetN() - 1,
xMax,
dummy);
345 double cutValue = cutIter->cutFunction_->Eval(cutVariable);
346 passesCuts = (disc_result > cutValue);
348 std::cout <<
"cutValue (@" << cutVariable <<
") = " << cutValue <<
" --> passesCuts = " << passesCuts
352 result.workingPoints.push_back(passesCuts);
358 template <
class TauType>
363 <<
"Unsupported TauType used. You must use either PFTau or PATTau.";
368 return "recoTauDiscriminantCutMultiplexerDefault";
372 return "patTauDiscriminantCutMultiplexerDefault";
375 template <
class TauType,
class TauTypeRef,
class ParentClass>
381 desc.add<
int>(
"verbosity", 0);
388 desc_mapping.
add<
unsigned int>(
"category", 0);
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);
399 std::vector<std::string> defaultRaws{
"discriminator"};
400 desc.add<std::vector<std::string>>(
"rawValues", defaultRaws);
401 std::vector<double> defaultWP{0.0};
405 desc.add<
bool>(
"loadMVAfromDB",
true);
406 ParentClass::fillProducerDescriptions(
desc);
408 descriptions.
add(getDefaultConfigString<TauType>(),
desc);
edm::EDGetTokenT< reco::TauDiscriminatorContainer > toMultiplex_token
edm::ESGetToken< PhysicsTFormulaPayload, PhysicsTFormulaPayloadRcd > formulaToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
std::map< int, std::vector< std::unique_ptr< DiscriminantCutEntry > > > DiscriminantCutMap
TauDiscriminantCutMultiplexerT(const edm::ParameterSet &pset)
TauDiscriminantCutMultiplexerT< pat::Tau, pat::TauRef, PATTauDiscriminationContainerProducerBase > PATTauDiscriminantCutMultiplexer
std::string getDefaultConfigString()
edm::InputTag toMultiplex_
edm::Handle< reco::TauDiscriminatorContainer > toMultiplexHandle_
TauDiscriminantCutMultiplexerT< reco::PFTau, reco::PFTauRef, PFTauDiscriminationContainerProducerBase > RecoTauDiscriminantCutMultiplexer
void addParameter(std::string const &name, T const &value)
#define DEFINE_FWK_MODULE(type)
reco::SingleTauDiscriminatorContainer discriminate(const TauTypeRef &) const override
bool getData(T &iHolder) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup) override
std::unique_ptr< const TFormula > mvaOutput_normalization_
int raw_discriminator_idx_
edm::ESGetToken< PhysicsTGraphPayload, PhysicsTGraphPayloadRcd > cutToken_
~TauDiscriminantCutMultiplexerT() override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::unique_ptr< StringObjectFunction< TauType > > cutVariable_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string mvaOutputNormalizationName_
std::unique_ptr< const TGraph > cutFunction_
edm::FileInPath inputFileName_