Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:24

0001 #include "RecoBTag/CTagging/interface/CharmTagger.h"
0002 
0003 #include "DataFormats/BTauReco/interface/CandSoftLeptonTagInfo.h"
0004 #include "DataFormats/BTauReco/interface/CandIPTagInfo.h"
0005 #include "DataFormats/BTauReco/interface/SecondaryVertexTagInfo.h"
0006 #include "FWCore/Utilities/interface/ESInputTag.h"
0007 #include "CondFormats/DataRecord/interface/GBRWrapperRcd.h"
0008 
0009 #include <memory>
0010 #include <algorithm>
0011 #include <iostream>
0012 #include <map>
0013 #include <vector>
0014 
0015 CharmTagger::Tokens::Tokens(const edm::ParameterSet &configuration, edm::ESConsumesCollector &&cc) {
0016   if (configuration.getParameter<bool>("useCondDB")) {
0017     gbrForest_ = cc.consumes(edm::ESInputTag{"",
0018                                              configuration.existsAs<std::string>("gbrForestLabel")
0019                                                  ? configuration.getParameter<std::string>("gbrForestLabel")
0020                                                  : ""});
0021   }
0022 }
0023 
0024 CharmTagger::CharmTagger(const edm::ParameterSet &configuration, Tokens tokens)
0025     : sl_computer_(configuration.getParameter<edm::ParameterSet>("slComputerCfg")),
0026       mva_name_(configuration.getParameter<std::string>("mvaName")),
0027       weight_file_(configuration.getParameter<edm::FileInPath>("weightFile")),
0028       use_GBRForest_(configuration.getParameter<bool>("useGBRForest")),
0029       use_adaBoost_(configuration.getParameter<bool>("useAdaBoost")),
0030       defaultValueNoTracks_(configuration.getParameter<bool>("defaultValueNoTracks")),
0031       tokens_{tokens} {
0032   vpset vars_definition = configuration.getParameter<vpset>("variables");
0033   for (auto &var : vars_definition) {
0034     MVAVar mva_var;
0035     mva_var.name = var.getParameter<std::string>("name");
0036     mva_var.id = reco::getTaggingVariableName(var.getParameter<std::string>("taggingVarName"));
0037     mva_var.has_index = var.existsAs<int>("idx");
0038     mva_var.index = mva_var.has_index ? var.getParameter<int>("idx") : 0;
0039     mva_var.default_value = var.getParameter<double>("default");
0040 
0041     variables_.push_back(mva_var);
0042   }
0043 
0044   uses(0, "pfImpactParameterTagInfos");
0045   uses(1, "pfInclusiveSecondaryVertexFinderCvsLTagInfos");
0046   uses(2, "softPFMuonsTagInfos");
0047   uses(3, "softPFElectronsTagInfos");
0048 }
0049 
0050 void CharmTagger::initialize(const JetTagComputerRecord &record) {
0051   mvaID_ = std::make_unique<TMVAEvaluator>();
0052 
0053   std::vector<std::string> variable_names;
0054   variable_names.reserve(variables_.size());
0055 
0056   for (auto &var : variables_) {
0057     variable_names.push_back(var.name);
0058   }
0059   std::vector<std::string> spectators;
0060 
0061   if (tokens_.gbrForest_.isInitialized()) {
0062     mvaID_->initializeGBRForest(&record.get(tokens_.gbrForest_), variable_names, spectators, use_adaBoost_);
0063   } else {
0064     mvaID_->initialize("Color:Silent:Error",
0065                        mva_name_,
0066                        weight_file_.fullPath(),
0067                        variable_names,
0068                        spectators,
0069                        use_GBRForest_,
0070                        use_adaBoost_);
0071   }
0072 }
0073 
0074 CharmTagger::~CharmTagger() {}
0075 
0076 /// b-tag a jet based on track-to-jet parameters in the extened info collection
0077 float CharmTagger::discriminator(const TagInfoHelper &tagInfo) const {
0078   // default value, used if there are no leptons associated to this jet
0079   const reco::CandIPTagInfo &ip_info = tagInfo.get<reco::CandIPTagInfo>(0);
0080   const reco::CandSecondaryVertexTagInfo &sv_info = tagInfo.get<reco::CandSecondaryVertexTagInfo>(1);
0081   const reco::CandSoftLeptonTagInfo &softmu_info = tagInfo.get<reco::CandSoftLeptonTagInfo>(2);
0082   const reco::CandSoftLeptonTagInfo &softel_info = tagInfo.get<reco::CandSoftLeptonTagInfo>(3);
0083   reco::TaggingVariableList vars = sl_computer_(ip_info, sv_info, softmu_info, softel_info);
0084 
0085   // Loop over input variables
0086   std::map<std::string, float> inputs;
0087   for (auto &mva_var : variables_) {
0088     //vectorial tagging variable
0089     if (mva_var.has_index) {
0090       std::vector<float> vals = vars.getList(mva_var.id, false);
0091       inputs[mva_var.name] = (vals.size() > mva_var.index) ? vals[mva_var.index] : mva_var.default_value;
0092     }
0093     //single value tagging var
0094     else {
0095       inputs[mva_var.name] = vars.get(mva_var.id, mva_var.default_value);
0096     }
0097   }
0098 
0099   //get the MVA output
0100   bool notracks = (vars.get(reco::btau::jetNTracks, 0) == 0);
0101   float tag = mvaID_->evaluate(inputs);
0102   if (defaultValueNoTracks_ && notracks) {
0103     tag = -2;
0104   }  // if no tracks available, put value at -2 (only for Phase I)
0105   return tag;
0106 }