Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-31 02:19:53

0001 // * Author: Alberto Zucchetta
0002 // * Mail: a.zucchetta@cern.ch
0003 // * January 16, 2015
0004 
0005 #include <limits>
0006 #include <memory>
0007 
0008 #include <random>
0009 
0010 #include "RecoBTau/JetTagComputer/interface/JetTagComputerRecord.h"
0011 #include "DataFormats/BTauReco/interface/SoftLeptonTagInfo.h"
0012 #include "DataFormats/BTauReco/interface/CandSoftLeptonTagInfo.h"
0013 #include "RecoBTag/SoftLepton/interface/LeptonSelector.h"
0014 #include "RecoBTag/SoftLepton/interface/MuonTagger.h"
0015 
0016 MuonTagger::Tokens::Tokens(const edm::ParameterSet& cfg, edm::ESConsumesCollector&& cc) {
0017   if (cfg.getParameter<bool>("useCondDB")) {
0018     gbrForest_ = cc.consumes(edm::ESInputTag{"", cfg.getParameter<std::string>("gbrForestLabel")});
0019   }
0020 }
0021 
0022 MuonTagger::MuonTagger(const edm::ParameterSet& cfg, Tokens tokens)
0023     : m_selector(cfg),
0024       m_weightFile(cfg.getParameter<edm::FileInPath>("weightFile")),
0025       m_useGBRForest(cfg.getParameter<bool>("useGBRForest")),
0026       m_useAdaBoost(cfg.getParameter<bool>("useAdaBoost")),
0027       m_tokens{tokens} {
0028   uses("smTagInfos");
0029   mvaID = std::make_unique<TMVAEvaluator>();
0030 }
0031 
0032 void MuonTagger::initialize(const JetTagComputerRecord& record) {
0033   // variable names and order need to be the same as in the training
0034   std::vector<std::string> variables(
0035       {"TagInfo1.sip3d", "TagInfo1.sip2d", "TagInfo1.ptRel", "TagInfo1.deltaR", "TagInfo1.ratio"});
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 MuonTagger::discriminator(const TagInfoHelper& tagInfo) const {
0047   float bestTag =
0048       -std::numeric_limits<float>::infinity();  // default value, used if there are no leptons associated to this jet
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       continue;
0059     bool flip(false);
0060     if (m_selector.isNegative()) {
0061       int seed = 1 + round(10000. * properties.deltaR);
0062       random.seed(seed);
0063       float rndm = dist(random);
0064       if (rndm < 0.5)
0065         flip = true;
0066     }
0067     //for negative tagger, flip 50% of the negative signs to positive value
0068     float sip3dsig = flip ? -properties.sip3dsig : properties.sip3dsig;
0069     float sip2dsig = flip ? -properties.sip2dsig : properties.sip2dsig;
0070 
0071     std::map<std::string, float> inputs;
0072     inputs["TagInfo1.sip3d"] = sip3dsig;
0073     inputs["TagInfo1.sip2d"] = sip2dsig;
0074     inputs["TagInfo1.ptRel"] = properties.ptRel;
0075     inputs["TagInfo1.deltaR"] = properties.deltaR;
0076     inputs["TagInfo1.ratio"] = properties.ratio;
0077 
0078     float tag = mvaID->evaluate(inputs);
0079     // Transform output between 0 and 1
0080     tag = (tag + 1.0) / 2.0;
0081     if (tag > bestTag)
0082       bestTag = tag;
0083   }
0084 
0085   return bestTag;
0086 }
0087 
0088 void MuonTagger::fillPSetDescription(edm::ParameterSetDescription& desc) {
0089   btag::LeptonSelector::fillPSetDescription(desc);
0090   desc.add<bool>("useCondDB", false);
0091   desc.add<std::string>("gbrForestLabel", "");
0092   desc.add<edm::FileInPath>("weightFile", edm::FileInPath());
0093   desc.add<bool>("useGBRForest", false);
0094   desc.add<bool>("useAdaBoost", false);
0095 }