CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonTagger.cc
Go to the documentation of this file.
1 // * Author: Alberto Zucchetta
2 // * Mail: a.zucchetta@cern.ch
3 // * January 16, 2015
4 
5 #include <limits>
6 #include <random>
7 
16 
17 
19  m_selector(cfg),
20  m_useCondDB(cfg.getParameter<bool>("useCondDB")),
21  m_gbrForestLabel(cfg.existsAs<std::string>("gbrForestLabel") ? cfg.getParameter<std::string>("gbrForestLabel") : ""),
22  m_weightFile(cfg.existsAs<edm::FileInPath>("weightFile") ? cfg.getParameter<edm::FileInPath>("weightFile") : edm::FileInPath()),
23  m_useGBRForest(cfg.existsAs<bool>("useGBRForest") ? cfg.getParameter<bool>("useGBRForest") : false),
24  m_useAdaBoost(cfg.existsAs<bool>("useAdaBoost") ? cfg.getParameter<bool>("useAdaBoost") : false)
25 {
26  uses("smTagInfos");
27  mvaID.reset(new TMVAEvaluator());
28 }
29 
31 {
32  // variable names and order need to be the same as in the training
33  std::vector<std::string> variables({"TagInfo1.sip3d", "TagInfo1.sip2d", "TagInfo1.ptRel", "TagInfo1.deltaR", "TagInfo1.ratio"});
34  std::vector<std::string> spectators;
35 
36  if (m_useCondDB)
37  {
38  const GBRWrapperRcd & gbrWrapperRecord = record.getRecord<GBRWrapperRcd>();
39 
40  edm::ESHandle<GBRForest> gbrForestHandle;
41  gbrWrapperRecord.get(m_gbrForestLabel.c_str(), gbrForestHandle);
42 
43  mvaID->initializeGBRForest(gbrForestHandle.product(), variables, spectators, m_useAdaBoost);
44  }
45  else
46  mvaID->initialize("Color:Silent:Error", "BDT", m_weightFile.fullPath(), variables, spectators, m_useGBRForest, m_useAdaBoost);
47 }
48 
49 // b-tag a jet based on track-to-jet parameters in the extened info collection
51 
52  float bestTag = - std::numeric_limits<float>::infinity(); // default value, used if there are no leptons associated to this jet
54 
55  std::mt19937_64 random;
56  std::uniform_real_distribution<float> dist(0.f,1.f);
57 
58  // If there are multiple leptons, look for the highest tag result
59  for (unsigned int i=0; i<info.leptons(); i++) {
60  const reco::SoftLeptonProperties& properties = info.properties(i);
61  if(!m_selector(properties)) continue;
62  bool flip(false);
63  if(m_selector.isNegative()) {
64  int seed=1+round(10000.*properties.deltaR);
65  random.seed(seed);
66  float rndm = dist(random);
67  if(rndm<0.5) flip=true;
68  }
69  //for negative tagger, flip 50% of the negative signs to positive value
70  float sip3dsig = flip ? -properties.sip3dsig : properties.sip3dsig;
71  float sip2dsig = flip ? -properties.sip2dsig : properties.sip2dsig;
72 
73  std::map<std::string,float> inputs;
74  inputs["TagInfo1.sip3d"] = sip3dsig;
75  inputs["TagInfo1.sip2d"] = sip2dsig;
76  inputs["TagInfo1.ptRel"] = properties.ptRel;
77  inputs["TagInfo1.deltaR"] = properties.deltaR;
78  inputs["TagInfo1.ratio"] = properties.ratio;
79 
80  float tag = mvaID->evaluate(inputs);
81  // Transform output between 0 and 1
82  tag = (tag+1.0)/2.0;
83  if(tag>bestTag) bestTag = tag;
84  }
85 
86  return bestTag;
87 }
88 
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
const bool m_useGBRForest
Definition: MuonTagger.h:27
const T & get(unsigned int index=0) const
tuple cfg
Definition: looper.py:293
JetCorrectorParameters::Record record
Definition: classes.h:7
const std::string m_gbrForestLabel
Definition: MuonTagger.h:25
const bool m_useAdaBoost
Definition: MuonTagger.h:28
TRandom random
Definition: MVATrainer.cc:138
const SoftLeptonProperties & properties(size_t i) const
void initialize(const JetTagComputerRecord &) override
Definition: MuonTagger.cc:30
btag::LeptonSelector m_selector
Definition: MuonTagger.h:23
void get(HolderT &iHolder) const
const double infinity
void uses(unsigned int id, const std::string &label)
double f[11][100]
MuonTagger(const edm::ParameterSet &)
Definition: MuonTagger.cc:18
const edm::FileInPath m_weightFile
Definition: MuonTagger.h:26
bool isNegative() const
const bool m_useCondDB
Definition: MuonTagger.h:24
std::unique_ptr< TMVAEvaluator > mvaID
Definition: MuonTagger.h:30
virtual float discriminator(const TagInfoHelper &tagInfo) const override
Definition: MuonTagger.cc:50
volatile std::atomic< bool > shutdown_flag false
std::string fullPath() const
Definition: FileInPath.cc:165