17 #include <boost/foreach.hpp>
36 template <
class TauType,
class TauTypeRef,
class ParentClass>
84 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
87 return std::unique_ptr<TFile>{
new TFile(
inputFileName.fullPath().data())};
94 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadObjectFromFile")
95 <<
" Failed to load Object = " <<
objectName.data() <<
" from file = " <<
inputFile.GetName() <<
" !!\n";
97 return std::unique_ptr<const T>{static_cast<T*>(
object->Clone())};
100 std::unique_ptr<const TGraph> loadTGraphFromDB(
const edm::EventSetup& es,
102 const int& verbosity_ = 0) {
104 std::cout <<
"<loadTGraphFromDB>:" << std::endl;
105 std::cout <<
" graphName = " << graphName << std::endl;
109 return std::unique_ptr<const TGraph>{
new TGraph(*graphPayload.
product())};
112 std::unique_ptr<TFormula> loadTFormulaFromDB(
const edm::EventSetup& es,
115 const int& verbosity_ = 0) {
117 std::cout <<
"<loadTFormulaFromDB>:" << std::endl;
118 std::cout <<
" formulaName = " << formulaName << std::endl;
123 if (formulaPayload->
formulas().size() == 1 && formulaPayload->
limits().size() == 1) {
124 return std::unique_ptr<TFormula>{
new TFormula(
newName, formulaPayload->
formulas().at(0).data())};
126 throw cms::Exception(
"TauDiscriminantCutMultiplexerT::loadTFormulaFromDB")
127 <<
"Failed to load TFormula = " << formulaName <<
" from Database !!\n";
129 return std::unique_ptr<TFormula>{};
133 template <
class TauType,
class TauTypeRef,
class ParentClass>
137 moduleLabel_(
cfg.getParameter<
std::
string>(
"@module_label")),
138 mvaOutput_normalization_(),
139 isInitialized_(
false) {
154 typedef std::vector<edm::ParameterSet>
VPSet;
155 typedef std::vector<std::string> VString;
156 typedef std::vector<double> VDouble;
157 VString rawValueConfig =
cfg.getParameter<VString>(
"rawValues");
158 for (
uint i = 0;
i < rawValueConfig.size();
i++) {
159 if (rawValueConfig[
i] ==
"discriminator")
161 else if (rawValueConfig[
i] ==
"category")
165 <<
" Configuration Parameter 'rawValues' containes unknown values. Must be 'discriminator' or 'category'!!\n";
167 n_raws_ = rawValueConfig.size();
169 for (
auto const& mappingEntry :
mapping) {
170 unsigned category = mappingEntry.getParameter<uint32_t>(
"category");
171 std::vector<std::unique_ptr<DiscriminantCutEntry>> cutWPs;
173 bool localWPs =
false;
174 bool wpsAsDouble =
false;
175 if (mappingEntry.exists(
"workingPoints")) {
177 if (mappingEntry.existsAs<VDouble>(
"workingPoints")) {
179 }
else if (mappingEntry.existsAs<VString>(
"workingPoints")) {
183 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
185 }
else if (
cfg.exists(
"workingPoints")) {
187 if (
cfg.existsAs<VDouble>(
"workingPoints")) {
189 }
else if (
cfg.existsAs<VString>(
"workingPoints")) {
193 <<
" Configuration Parameter 'workingPoints' must be filled with cms.string or cms.double!!\n";
197 <<
" Undefined Configuration Parameter 'workingPoints' !!\n";
202 workingPoints = mappingEntry.getParameter<VDouble>(
"workingPoints");
214 workingPoints = mappingEntry.getParameter<VString>(
"workingPoints");
219 cut->cutName_ = categoryname +
wp;
234 template <
class TauType,
class TauTypeRef,
class ParentClass>
237 template <
class TauType,
class TauTypeRef,
class ParentClass>
241 std::cout <<
" begin! " << moduleLabel_ <<
" " << isInitialized_ << std::endl;
242 if (!isInitialized_) {
246 if (!mvaOutputNormalizationName_.empty()) {
247 if (!loadMVAfromDB_) {
248 inputFile = openInputFile(inputFileName_);
249 mvaOutput_normalization_ = loadObjectFromFile<TFormula>(*
inputFile, mvaOutputNormalizationName_);
251 auto temp = loadTFormulaFromDB(
252 es, mvaOutputNormalizationName_, Form(
"%s_mvaOutput_normalization", moduleLabel_.data()), verbosity_);
256 for (
auto const& cutWPs : cuts_) {
257 for (
auto const&
cut : cutWPs.second) {
258 if (
cut->mode_ == DiscriminantCutEntry::kVariableCut) {
259 if (!loadMVAfromDB_) {
261 inputFile = openInputFile(inputFileName_);
264 std::cout <<
"Loading from file" << inputFileName_ << std::endl;
265 cut->cutFunction_ = loadObjectFromFile<TGraph>(*
inputFile,
cut->cutName_);
268 std::cout <<
"Loading from DB" << std::endl;
269 cut->cutFunction_ = loadTGraphFromDB(es,
cut->cutName_, verbosity_);
274 isInitialized_ =
true;
277 evt.
getByToken(toMultiplex_token, toMultiplexHandle_);
280 template <
class TauType,
class TauTypeRef,
class ParentClass>
282 const TauTypeRef&
tau)
const {
284 std::cout <<
"<TauDiscriminantCutMultiplexerT::discriminate>:" << std::endl;
285 std::cout <<
" moduleLabel = " << moduleLabel_ << std::endl;
289 result.rawValues.resize(n_raws_);
290 double disc_result = (*toMultiplexHandle_)[
tau].rawValues.at(0);
292 std::cout <<
"disc_result = " << disc_result << std::endl;
294 if (raw_discriminator_idx_ >= 0)
295 result.rawValues[raw_discriminator_idx_] = disc_result;
296 if (mvaOutput_normalization_) {
297 disc_result = mvaOutput_normalization_->Eval(disc_result);
299 std::cout <<
"disc_result (normalized) = " << disc_result << std::endl;
302 double key_result = 0.0;
304 key_result = (*toMultiplexHandle_)[
tau].rawValues.at(1);
305 if (raw_category_idx_ >= 0)
306 result.rawValues[raw_category_idx_] = key_result;
308 typename DiscriminantCutMap::const_iterator cutWPsIter = cuts_.find(std::round(key_result));
311 if (cutWPsIter == cuts_.end()) {
315 for (
auto const& cutIter : cutWPsIter->second) {
316 bool passesCuts =
false;
317 if (cutIter->mode_ == DiscriminantCutEntry::kFixedCut) {
318 passesCuts = (disc_result > cutIter->cutValue_);
320 std::cout <<
"cutValue (fixed) = " << cutIter->cutValue_ <<
" --> passesCuts = " << passesCuts << std::endl;
322 }
else if (cutIter->mode_ == DiscriminantCutEntry::kVariableCut) {
323 double cutVariable = (*cutIter->cutVariable_)(*
tau);
325 cutIter->cutFunction_->GetPoint(0,
xMin,
dummy);
326 cutIter->cutFunction_->GetPoint(cutIter->cutFunction_->GetN() - 1,
xMax,
dummy);
332 double cutValue = cutIter->cutFunction_->Eval(cutVariable);
333 passesCuts = (disc_result > cutValue);
335 std::cout <<
"cutValue (@" << cutVariable <<
") = " << cutValue <<
" --> passesCuts = " << passesCuts
339 result.workingPoints.push_back(passesCuts);
345 template <
class TauType>
350 <<
"Unsupported TauType used. You must use either PFTau or PATTau.";
355 return "recoTauDiscriminantCutMultiplexerDefault";
359 return "patTauDiscriminantCutMultiplexerDefault";
362 template <
class TauType,
class TauTypeRef,
class ParentClass>
368 desc.
add<
int>(
"verbosity", 0);
375 desc_mapping.
add<
unsigned int>(
"category", 0);
380 ->setComment(
"the parameter is required when \"workingPoints\" are string");
381 std::vector<edm::ParameterSet> vpsd_mapping;
382 vpsd_mapping.push_back(pset_mapping);
383 desc.
addVPSet(
"mapping", desc_mapping, vpsd_mapping);
386 std::vector<std::string> defaultRaws{
"discriminator"};
387 desc.
add<std::vector<std::string>>(
"rawValues", defaultRaws);
388 std::vector<double> defaultWP{0.0};
392 desc.
add<
bool>(
"loadMVAfromDB",
true);
393 ParentClass::fillProducerDescriptions(desc);
395 descriptions.
add(getDefaultConfigString<TauType>(), desc);