Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:49

0001 #include <limits>
0002 #include <memory>
0003 
0004 #include <random>
0005 
0006 #include "CondFormats/DataRecord/interface/BTauGenericMVAJetTagComputerRcd.h"
0007 #include "CondFormats/DataRecord/interface/GBRWrapperRcd.h"
0008 #include "RecoBTau/JetTagComputer/interface/JetTagComputerRecord.h"
0009 #include "DataFormats/BTauReco/interface/SoftLeptonTagInfo.h"
0010 #include "RecoBTag/SoftLepton/interface/LeptonSelector.h"
0011 #include "RecoBTag/SoftLepton/interface/ElectronTagger.h"
0012 #include "DataFormats/BTauReco/interface/CandSoftLeptonTagInfo.h"
0013 #include <iostream>
0014 
0015 ElectronTagger::Tokens::Tokens(const edm::ParameterSet& cfg, edm::ESConsumesCollector&& cc) {
0016   if (cfg.getParameter<bool>("useCondDB")) {
0017     gbrForest_ = cc.consumes(edm::ESInputTag{
0018         "", cfg.existsAs<std::string>("gbrForestLabel") ? cfg.getParameter<std::string>("gbrForestLabel") : ""});
0019   }
0020 }
0021 
0022 ElectronTagger::ElectronTagger(const edm::ParameterSet& cfg, Tokens tokens)
0023     : m_selector(cfg),
0024       m_weightFile(cfg.existsAs<edm::FileInPath>("weightFile") ? cfg.getParameter<edm::FileInPath>("weightFile")
0025                                                                : edm::FileInPath()),
0026       m_useGBRForest(cfg.existsAs<bool>("useGBRForest") ? cfg.getParameter<bool>("useGBRForest") : false),
0027       m_useAdaBoost(cfg.existsAs<bool>("useAdaBoost") ? cfg.getParameter<bool>("useAdaBoost") : false),
0028       m_tokens{tokens} {
0029   uses("seTagInfos");
0030   mvaID = std::make_unique<TMVAEvaluator>();
0031 }
0032 
0033 void ElectronTagger::initialize(const JetTagComputerRecord& record) {
0034   // variable names and order need to be the same as in the training
0035   std::vector<std::string> variables({"sip3d", "sip2d", "ptRel", "deltaR", "ratio", "mva_e_pi"});
0036   std::vector<std::string> spectators;
0037 
0038   if (m_tokens.gbrForest_.isInitialized()) {
0039     mvaID->initializeGBRForest(&record.get(m_tokens.gbrForest_), variables, spectators, m_useAdaBoost);
0040   } else
0041     mvaID->initialize(
0042         "Color:Silent:Error", "BDT", m_weightFile.fullPath(), variables, spectators, m_useGBRForest, m_useAdaBoost);
0043 }
0044 
0045 /// b-tag a jet based on track-to-jet parameters in the extened info collection
0046 float ElectronTagger::discriminator(const TagInfoHelper& tagInfo) const {
0047   // default value, used if there are no leptons associated to this jet
0048   float bestTag = -std::numeric_limits<float>::infinity();
0049   const reco::CandSoftLeptonTagInfo& info = tagInfo.get<reco::CandSoftLeptonTagInfo>();
0050 
0051   std::mt19937_64 random;
0052   std::uniform_real_distribution<float> dist(0.f, 1.f);
0053 
0054   // if there are multiple leptons, look for the highest tag result
0055   for (unsigned int i = 0; i < info.leptons(); i++) {
0056     const reco::SoftLeptonProperties& properties = info.properties(i);
0057     if (m_selector(properties)) {
0058       int theSeed = 1 + round(10000.0 * std::abs(properties.deltaR));
0059       random.seed(theSeed);
0060       float rndm = dist(random);
0061       //for negative tagger, flip 50% of the negative signs to positive value
0062       float sip3dsig = (m_selector.isNegative() && rndm < 0.5) ? -properties.sip3dsig : properties.sip3dsig;
0063       float sip2dsig = (m_selector.isNegative() && rndm < 0.5) ? -properties.sip2dsig : properties.sip2dsig;
0064 
0065       std::map<std::string, float> inputs;
0066       inputs["sip3d"] = sip3dsig;
0067       inputs["sip2d"] = sip2dsig;
0068       inputs["ptRel"] = properties.ptRel;
0069       inputs["deltaR"] = properties.deltaR;
0070       inputs["ratio"] = properties.ratio;
0071       inputs["mva_e_pi"] = properties.elec_mva;
0072 
0073       float tag = mvaID->evaluate(inputs);
0074       // Transform output between 0 and 1
0075       tag = (tag + 1.0) / 2.0;
0076       if (tag > bestTag)
0077         bestTag = tag;
0078     }
0079   }
0080   return bestTag;
0081 }