CP3-llbb Framework
JetsProducer.h
1 #ifndef JETS_PRODUCER
2 #define JETS_PRODUCER
3 
4 #include <cp3_llbb/Framework/interface/CandidatesProducer.h>
5 #include <cp3_llbb/Framework/interface/BTaggingScaleFactors.h>
6 
7 #include <DataFormats/PatCandidates/interface/Jet.h>
8 #include "TMVA/Reader.h"
9 
10 
11 class JetsProducer: public CandidatesProducer<pat::Jet>, public BTaggingScaleFactors {
12  public:
13  JetsProducer(const std::string& name, const ROOT::TreeGroup& tree, const edm::ParameterSet& config):
14  CandidatesProducer(name, tree, config), BTaggingScaleFactors(const_cast<ROOT::TreeGroup&>(tree))
15  {
16  BTaggingScaleFactors::create_branches(config);
17 
18  if (config.exists("btags")) {
19  const std::vector<std::string>& btags = config.getUntrackedParameter<std::vector<std::string>>("btags");
20 
21  // Create branches
22  for (const std::string& btag: btags) {
23  std::string branchName{btag};
24  std::replace(std::begin(branchName), std::end(branchName), ':', '_');
25  m_btag_discriminators.emplace(btag, &CandidatesProducer<pat::Jet>::tree[branchName].write<std::vector<float>>());
26  }
27  }
28  if (config.exists("computeRegression")) {
29  computeRegression = config.getUntrackedParameter<bool>("computeRegression");
30  if (computeRegression)
31  {
32  regressionFile = config.getUntrackedParameter<edm::FileInPath>("regressionFile").fullPath();
33  std::cout << " -> storing bjet energy regression, with xml file " << regressionFile << std::endl;
34  bjetRegressionReader.reset(new TMVA::Reader());
35  bjetRegressionReader->AddVariable("Jet_pt", &Jet_pt);
36  bjetRegressionReader->AddVariable("nPVs", &nPVs);
37  bjetRegressionReader->AddVariable("Jet_eta", &Jet_eta);
38  bjetRegressionReader->AddVariable("Jet_mt", &Jet_mt);
39  bjetRegressionReader->AddVariable("Jet_leadTrackPt", &Jet_leadTrackPt);
40  bjetRegressionReader->AddVariable("Jet_leptonPtRel", &Jet_leptonPtRel);
41  bjetRegressionReader->AddVariable("Jet_leptonPt", &Jet_leptonPt);
42  bjetRegressionReader->AddVariable("Jet_leptonDeltaR", &Jet_leptonDeltaR);
43  bjetRegressionReader->AddVariable("Jet_neHEF", &Jet_neHEF);
44  bjetRegressionReader->AddVariable("Jet_neEmEF", &Jet_neEmEF);
45  bjetRegressionReader->AddVariable("Jet_vtxPt", &Jet_vtxPt);
46  bjetRegressionReader->AddVariable("Jet_vtxMass", &Jet_vtxMass);
47  bjetRegressionReader->AddVariable("Jet_vtx3dL", &Jet_vtx3dL);
48  bjetRegressionReader->AddVariable("Jet_vtxNtrk", &Jet_vtxNtrk);
49  bjetRegressionReader->AddVariable("Jet_vtx3deL", &Jet_vtx3deL);
50  bjetRegressionReader->BookMVA("BDT::BDTG", regressionFile.c_str());
51  }
52  } else {
53  computeRegression = false;
54  }
55  }
56 
57  virtual ~JetsProducer() {}
58 
59  virtual void doConsumes(const edm::ParameterSet& config, edm::ConsumesCollector&& collector) override {
60  m_jets_token = collector.consumes<std::vector<pat::Jet>>(config.getUntrackedParameter<edm::InputTag>("jets", edm::InputTag("slimmedJets")));
61  m_vertices_token = collector.consumes<std::vector<reco::Vertex>>(config.getUntrackedParameter<edm::InputTag>("vertices", edm::InputTag("offlineSlimmedPrimaryVertices")));
62  }
63 
64  virtual void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
65 
66 
67  float getBTagDiscriminant(size_t index, const std::string& name) const {
68  return m_btag_discriminators.at(name)->at(index);
69  }
70 
71  float get_scale_factor(Algorithm algo, const std::string& wp, size_t index, Variation variation = Variation::Nominal);
72 
73  private:
74 
75  // Tokens
76  edm::EDGetTokenT<std::vector<pat::Jet>> m_jets_token;
77  edm::EDGetTokenT<std::vector<reco::Vertex>> m_vertices_token;
78 
79  std::map<std::string, std::vector<float>*> m_btag_discriminators;
80  // regression stuff
81  bool computeRegression;
82  std::string regressionFile;
83  std::unique_ptr<TMVA::Reader> bjetRegressionReader;
84  float Jet_pt;
85  float nPVs;
86  float Jet_eta;
87  float Jet_mt;
88  float Jet_leadTrackPt;
89  float Jet_leptonPtRel;
90  float Jet_leptonPt;
91  float Jet_leptonDeltaR;
92  float Jet_neHEF;
93  float Jet_neEmEF;
94  float Jet_vtxPt;
95  float Jet_vtxMass;
96  float Jet_vtx3dL;
97  float Jet_vtxNtrk;
98  float Jet_vtx3deL;
99  public:
100  // Tree members
101  std::vector<float>& area = tree["area"].write<std::vector<float>>();
102  std::vector<int8_t>& partonFlavor = tree["partonFlavor"].write<std::vector<int8_t>>();
103  std::vector<int8_t>& hadronFlavor = tree["hadronFlavor"].write<std::vector<int8_t>>();
104 
105  // Systematics flavor: 1 for heavy jets, 2 for light jets
106  std::vector<int8_t>& systFlavor = tree["systFlavor"].write<std::vector<int8_t>>();
107 
108  std::vector<float>& jecFactor = tree["jecFactor"].write<std::vector<float>>();
109  std::vector<float>& puJetID = tree["puJetID"].write<std::vector<float>>();
110  // Variables needed for 74X b-jet energy regression as of January 26th 2016
111  TRANSIENT_BRANCH(neutralHadronEnergyFraction, std::vector<float>);
112  TRANSIENT_BRANCH(neutralEmEnergyFraction, std::vector<float>);
113  TRANSIENT_BRANCH(vtxMass, std::vector<float>);
114  TRANSIENT_BRANCH(vtx3DVal, std::vector<float>);
115  TRANSIENT_BRANCH(vtx3DSig, std::vector<float>);
116  TRANSIENT_BRANCH(vtxPt, std::vector<float>);
117  TRANSIENT_BRANCH(vtxNtracks, std::vector<float>);
118  TRANSIENT_BRANCH(leptonPtRel, std::vector<float>);
119  TRANSIENT_BRANCH(leptonPt, std::vector<float>);
120  TRANSIENT_BRANCH(leptonDeltaR, std::vector<float>);
121  TRANSIENT_BRANCH(leadTrackPt, std::vector<float>);
122  BRANCH(regPt, std::vector<float>);
123 
124  BRANCH(passLooseID, std::vector<bool>);
125  BRANCH(passTightID, std::vector<bool>);
126  BRANCH(passTightLeptonVetoID, std::vector<bool>);
127 };
128 
129 #endif
Definition: BTaggingScaleFactors.h:36
Definition: CandidatesProducer.h:9
ROOT::TreeGroup tree
Access point to output tree.
Definition: Producer.h:132
Definition: JetsProducer.h:11
virtual void doConsumes(const edm::ParameterSet &config, edm::ConsumesCollector &&collector) override
Hook for the CMSSW consumes interface.
Definition: JetsProducer.h:59
virtual void produce(edm::Event &event, const edm::EventSetup &eventSetup) override
Main method of the producer, called for each event.
Definition: JetsProducer.cc:5