37 template <
class TauType,
class TauTypeRef,
class ParentClass>
85 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
88 return std::make_unique<TFile>(
inputFileName.fullPath().data());
95 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
96 <<
" Failed to load Object = " <<
objectName.data() <<
" from file = " <<
inputFile.GetName() <<
" !!\n";
98 return std::unique_ptr<const T>{static_cast<T*>(
object->Clone())};
101 std::unique_ptr<const TGraph> loadTGraphFromDB(
const edm::EventSetup& es,
103 const int& verbosity_ = 0) {
105 std::cout <<
"<loadTGraphFromDB>:" << std::endl;
106 std::cout <<
" graphName = " << graphName << std::endl;
110 return std::unique_ptr<const TGraph>{
new TGraph(*graphPayload.
product())};
113 std::unique_ptr<TFormula> loadTFormulaFromDB(
const edm::EventSetup& es,
116 const int& verbosity_ = 0) {
118 std::cout <<
"<loadTFormulaFromDB>:" << std::endl;
119 std::cout <<
" formulaName = " << formulaName << std::endl;
124 if (formulaPayload->
formulas().size() == 1 && formulaPayload->
limits().size() == 1) {
125 return std::make_unique<TFormula>(
newName, formulaPayload->
formulas().at(0).data());
127 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadTFormulaFromDB")
128 <<
"Failed to load TFormula = " << formulaName <<
" from Database !!\n";
130 return std::unique_ptr<TFormula>{};
134 template <
class TauType,
class TauTypeRef,
class ParentClass>
138 moduleLabel_(
cfg.getParameter<
std::
string>(
"@module_label")),
139 mvaOutput_normalization_(),
140 isInitialized_(
false) {
155 typedef std::vector<edm::ParameterSet>
VPSet;
156 typedef std::vector<std::string> VString;
157 typedef std::vector<double> VDouble;
158 VString rawValueConfig =
cfg.getParameter<VString>(
"rawValues");
159 for (
uint i = 0;
i < rawValueConfig.size();
i++) {
160 if (rawValueConfig[
i] ==
"discriminator")
162 else if (rawValueConfig[
i] ==
"category")
166 <<
" Configuration Parameter 'rawValues' containes unknown values. Must be 'discriminator' or 'category'!!\n";
168 n_raws_ = rawValueConfig.size();
170 for (
auto const& mappingEntry :
mapping) {
171 unsigned category = mappingEntry.getParameter<uint32_t>(
"category");
172 std::vector<std::unique_ptr<DiscriminantCutEntry>> cutWPs;
174 bool localWPs =
false;
175 bool wpsAsDouble =
false;
176 if (mappingEntry.exists(
"workingPoints")) {
178 if (mappingEntry.existsAs<VDouble>(
"workingPoints")) {
180 }
else if (mappingEntry.existsAs<VString>(
"workingPoints")) {
184 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
186 }
else if (
cfg.exists(
"workingPoints")) {
188 if (
cfg.existsAs<VDouble>(
"workingPoints")) {
190 }
else if (
cfg.existsAs<VString>(
"workingPoints")) {
194 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
198 <<
" Undefined Configuration Parameter 'workingPoints' !!\n";
203 workingPoints = mappingEntry.getParameter<VDouble>(
"workingPoints");
215 workingPoints = mappingEntry.getParameter<VString>(
"workingPoints");
220 cut->cutName_ = categoryname +
wp;
235 template <
class TauType,
class TauTypeRef,
class ParentClass>
238 template <
class TauType,
class TauTypeRef,
class ParentClass>
242 std::cout <<
" begin! " << moduleLabel_ <<
" " << isInitialized_ << std::endl;
243 if (!isInitialized_) {
247 if (!mvaOutputNormalizationName_.empty()) {
248 if (!loadMVAfromDB_) {
249 inputFile = openInputFile(inputFileName_);
250 mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*
inputFile, mvaOutputNormalizationName_);
252 auto temp = loadTFormulaFromDB(
253 es, mvaOutputNormalizationName_, Form(
"%s_mvaOutput_normalization", moduleLabel_.data()), verbosity_);
257 for (
auto const& cutWPs : cuts_) {
258 for (
auto const&
cut : cutWPs.second) {
259 if (
cut->mode_ == DiscriminantCutEntry::kVariableCut) {
260 if (!loadMVAfromDB_) {
262 inputFile = openInputFile(inputFileName_);
265 std::cout <<
"Loading from file" << inputFileName_ << std::endl;
266 cut->cutFunction_ = loadObjectFromFile<TGraph>(*
inputFile,
cut->cutName_);
269 std::cout <<
"Loading from DB" << std::endl;
270 cut->cutFunction_ = loadTGraphFromDB(es,
cut->cutName_, verbosity_);
275 isInitialized_ =
true;
278 evt.
getByToken(toMultiplex_token, toMultiplexHandle_);
281 template <
class TauType,
class TauTypeRef,
class ParentClass>
283 const TauTypeRef&
tau)
const {
285 std::cout <<
"<TauDiscriminantCutMultiplexerT::discriminate>:" << std::endl;
286 std::cout <<
" moduleLabel = " << moduleLabel_ << std::endl;
290 result.rawValues.resize(n_raws_);
291 double disc_result = (*toMultiplexHandle_)[
tau].rawValues.at(0);
293 std::cout <<
"disc_result = " << disc_result << std::endl;
295 if (raw_discriminator_idx_ >= 0)
296 result.rawValues[raw_discriminator_idx_] = disc_result;
297 if (mvaOutput_normalization_) {
298 disc_result = mvaOutput_normalization_->Eval(disc_result);
300 std::cout <<
"disc_result (normalized) = " << disc_result << std::endl;
303 double key_result = 0.0;
305 key_result = (*toMultiplexHandle_)[
tau].rawValues.at(1);
306 if (raw_category_idx_ >= 0)
307 result.rawValues[raw_category_idx_] = key_result;
309 typename DiscriminantCutMap::const_iterator cutWPsIter = cuts_.find(std::round(key_result));
312 if (cutWPsIter == cuts_.end()) {
316 for (
auto const& cutIter : cutWPsIter->second) {
317 bool passesCuts =
false;
318 if (cutIter->mode_ == DiscriminantCutEntry::kFixedCut) {
319 passesCuts = (disc_result > cutIter->cutValue_);
321 std::cout <<
"cutValue (fixed) = " << cutIter->cutValue_ <<
" --> passesCuts = " << passesCuts << std::endl;
323 }
else if (cutIter->mode_ == DiscriminantCutEntry::kVariableCut) {
324 double cutVariable = (*cutIter->cutVariable_)(*
tau);
326 cutIter->cutFunction_->GetPoint(0,
xMin,
dummy);
327 cutIter->cutFunction_->GetPoint(cutIter->cutFunction_->GetN() - 1,
xMax,
dummy);
333 double cutValue = cutIter->cutFunction_->Eval(cutVariable);
334 passesCuts = (disc_result > cutValue);
336 std::cout <<
"cutValue (@" << cutVariable <<
") = " << cutValue <<
" --> passesCuts = " << passesCuts
340 result.workingPoints.push_back(passesCuts);
346 template <
class TauType>
351 <<
"Unsupported TauType used. You must use either PFTau or PATTau.";
356 return "recoTauDiscriminantCutMultiplexerDefault";
360 return "patTauDiscriminantCutMultiplexerDefault";
363 template <
class TauType,
class TauTypeRef,
class ParentClass>
369 desc.add<
int>(
"verbosity", 0);
376 desc_mapping.
add<
unsigned int>(
"category", 0);
381 ->setComment(
"the parameter is required when \"workingPoints\" are string");
382 std::vector<edm::ParameterSet> vpsd_mapping;
383 vpsd_mapping.push_back(pset_mapping);
384 desc.addVPSet(
"mapping", desc_mapping, vpsd_mapping);
387 std::vector<std::string> defaultRaws{
"discriminator"};
388 desc.add<std::vector<std::string>>(
"rawValues", defaultRaws);
389 std::vector<double> defaultWP{0.0};
393 desc.add<
bool>(
"loadMVAfromDB",
true);
394 ParentClass::fillProducerDescriptions(
desc);
396 descriptions.
add(getDefaultConfigString<TauType>(),
desc);