Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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{
0019         "", cfg.existsAs<std::string>("gbrForestLabel") ? cfg.getParameter<std::string>("gbrForestLabel") : ""});
0020   }
0021 }
0022 
0023 MuonTagger::MuonTagger(const edm::ParameterSet& cfg, Tokens tokens)
0024     : m_selector(cfg),
0025       m_weightFile(cfg.existsAs<edm::FileInPath>("weightFile") ? cfg.getParameter<edm::FileInPath>("weightFile")
0026                                                                : edm::FileInPath()),
0027       m_useGBRForest(cfg.existsAs<bool>("useGBRForest") ? cfg.getParameter<bool>("useGBRForest") : false),
0028       m_useAdaBoost(cfg.existsAs<bool>("useAdaBoost") ? cfg.getParameter<bool>("useAdaBoost") : false),
0029       m_tokens{tokens} {
0030   uses("smTagInfos");
0031   mvaID = std::make_unique<TMVAEvaluator>();
0032 }
0033 
0034 void MuonTagger::initialize(const JetTagComputerRecord& record) {
0035   // variable names and order need to be the same as in the training
0036   std::vector<std::string> variables(
0037       {"TagInfo1.sip3d", "TagInfo1.sip2d", "TagInfo1.ptRel", "TagInfo1.deltaR", "TagInfo1.ratio"});
0038   std::vector<std::string> spectators;
0039 
0040   if (m_tokens.gbrForest_.isInitialized()) {
0041     mvaID->initializeGBRForest(&record.get(m_tokens.gbrForest_), variables, spectators, m_useAdaBoost);
0042   } else
0043     mvaID->initialize(
0044         "Color:Silent:Error", "BDT", m_weightFile.fullPath(), variables, spectators, m_useGBRForest, m_useAdaBoost);
0045 }
0046 
0047 // b-tag a jet based on track-to-jet parameters in the extened info collection
0048 float MuonTagger::discriminator(const TagInfoHelper& tagInfo) const {
0049   float bestTag =
0050       -std::numeric_limits<float>::infinity();  // default value, used if there are no leptons associated to this jet
0051   const reco::CandSoftLeptonTagInfo& info = tagInfo.get<reco::CandSoftLeptonTagInfo>();
0052 
0053   std::mt19937_64 random;
0054   std::uniform_real_distribution<float> dist(0.f, 1.f);
0055 
0056   // If there are multiple leptons, look for the highest tag result
0057   for (unsigned int i = 0; i < info.leptons(); i++) {
0058     const reco::SoftLeptonProperties& properties = info.properties(i);
0059     if (!m_selector(properties))
0060       continue;
0061     bool flip(false);
0062     if (m_selector.isNegative()) {
0063       int seed = 1 + round(10000. * properties.deltaR);
0064       random.seed(seed);
0065       float rndm = dist(random);
0066       if (rndm < 0.5)
0067         flip = true;
0068     }
0069     //for negative tagger, flip 50% of the negative signs to positive value
0070     float sip3dsig = flip ? -properties.sip3dsig : properties.sip3dsig;
0071     float sip2dsig = flip ? -properties.sip2dsig : properties.sip2dsig;
0072 
0073     std::map<std::string, float> inputs;
0074     inputs["TagInfo1.sip3d"] = sip3dsig;
0075     inputs["TagInfo1.sip2d"] = sip2dsig;
0076     inputs["TagInfo1.ptRel"] = properties.ptRel;
0077     inputs["TagInfo1.deltaR"] = properties.deltaR;
0078     inputs["TagInfo1.ratio"] = properties.ratio;
0079 
0080     float tag = mvaID->evaluate(inputs);
0081     // Transform output between 0 and 1
0082     tag = (tag + 1.0) / 2.0;
0083     if (tag > bestTag)
0084       bestTag = tag;
0085   }
0086 
0087   return bestTag;
0088 }