CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
BTagSFProducer Class Reference
Inheritance diagram for BTagSFProducer:
edm::stream::EDProducer<>

Public Member Functions

 BTagSFProducer (const edm::ParameterSet &iConfig)
 
 ~BTagSFProducer () override
 
- Public Member Functions inherited from edm::stream::EDProducer<>
 EDProducer ()=default
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () const final
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Member Functions

void produce (edm::Event &, const edm::EventSetup &) override
 

Private Attributes

const StringCutObjectSelector< pat::Jetcut_
 
std::vector< std::string > discNames_
 
std::vector< std::string > discShortNames_
 
std::vector< std::vector< std::string > > inBranchNames
 
std::vector< std::string > measurementTypesB_
 
std::vector< std::string > measurementTypesC_
 
std::vector< std::string > measurementTypesUDSG_
 
unsigned int nDiscs
 
BTagEntry::OperatingPoint op
 
std::vector< std::string > operatingPoints_
 
std::vector< BTagCalibrationReaderreaders
 
edm::EDGetTokenT< std::vector< pat::Jet > > src_
 
std::vector< std::string > sysTypes_
 
std::vector< std::string > weightFiles_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
typedef CacheContexts< T... > CacheTypes
 
typedef CacheTypes::GlobalCache GlobalCache
 
typedef AbilityChecker< T... > HasAbility
 
typedef CacheTypes::LuminosityBlockCache LuminosityBlockCache
 
typedef LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCacheLuminosityBlockContext
 
typedef CacheTypes::LuminosityBlockSummaryCache LuminosityBlockSummaryCache
 
typedef CacheTypes::RunCache RunCache
 
typedef RunContextT< RunCache, GlobalCacheRunContext
 
typedef CacheTypes::RunSummaryCache RunSummaryCache
 

Detailed Description

Definition at line 37 of file BTagSFProducer.cc.

Constructor & Destructor Documentation

BTagSFProducer::BTagSFProducer ( const edm::ParameterSet iConfig)
inline

Definition at line 39 of file BTagSFProducer.cc.

References MicroEventContent_cff::branch, calib, discNames_, discShortNames_, BTagEntry::FLAV_B, BTagEntry::FLAV_C, BTagEntry::FLAV_UDSG, edm::FileInPath::fullPath(), edm::ParameterSet::getUntrackedParameter(), inBranchNames, BTagCalibrationReader::load(), measurementTypesB_, measurementTypesC_, measurementTypesUDSG_, nDiscs, op, BTagEntry::OP_LOOSE, BTagEntry::OP_MEDIUM, BTagEntry::OP_RESHAPING, BTagEntry::OP_TIGHT, operatingPoints_, matplotRender::reader, readers, AlCaHLTBitMon_QueryRunRegistry::string, sysTypes_, and weightFiles_.

39  :
40  src_(consumes<std::vector<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
41  cut_(iConfig.getParameter<std::string>("cut")),
42  discNames_(iConfig.getParameter<std::vector<std::string>>("discNames")),
43  discShortNames_(iConfig.getParameter<std::vector<std::string>>("discShortNames")),
44  weightFiles_(iConfig.getParameter<std::vector<std::string>>("weightFiles")),
45  operatingPoints_(iConfig.getParameter<std::vector<std::string>>("operatingPoints")),
46  measurementTypesB_(iConfig.getParameter<std::vector<std::string>>("measurementTypesB")),
47  measurementTypesC_(iConfig.getParameter<std::vector<std::string>>("measurementTypesC")),
48  measurementTypesUDSG_(iConfig.getParameter<std::vector<std::string>>("measurementTypesUDSG")),
49  sysTypes_(iConfig.getParameter<std::vector<std::string>>("sysTypes"))
50  {
51  produces<nanoaod::FlatTable>();
52 
53  nDiscs=discNames_.size();
54  assert(discShortNames_.size()==nDiscs && weightFiles_.size()==nDiscs && operatingPoints_.size()==nDiscs && measurementTypesB_.size()==nDiscs && measurementTypesC_.size()==nDiscs && measurementTypesUDSG_.size()==nDiscs && sysTypes_.size()==nDiscs);
55 
56  bool validate = iConfig.getUntrackedParameter<bool>("validate");
57  for (unsigned int iDisc = 0; iDisc < nDiscs; ++iDisc) {
58 
59  if (weightFiles_[iDisc]!="unavailable") {
60  // setup calibration
62  edm::FileInPath fip(weightFiles_[iDisc]);
63  calib=BTagCalibration(discShortNames_[iDisc],fip.fullPath(), validate);
64 
65  // determine op
66  std::string opname;
67  if (operatingPoints_[iDisc] == "0" || operatingPoints_[iDisc] == "loose") {
69  opname="loose";
70  }
71  else if (operatingPoints_[iDisc] == "1" || operatingPoints_[iDisc] == "medium") {
73  opname="medium";
74  }
75  else if (operatingPoints_[iDisc] == "2" || operatingPoints_[iDisc] == "tight") {
77  opname="tight";
78  }
79  else if (operatingPoints_[iDisc] == "3" || operatingPoints_[iDisc] == "reshaping") {
81  opname="discriminator reshaping";
82  }
83 
84  // setup reader
86  reader=BTagCalibrationReader(op, sysTypes_[iDisc]);
87  reader.load(calib, BTagEntry::FLAV_B, measurementTypesB_[iDisc]);
88  reader.load(calib, BTagEntry::FLAV_C, measurementTypesC_[iDisc]);
89  reader.load(calib, BTagEntry::FLAV_UDSG, measurementTypesUDSG_[iDisc]);
90 
91  //calibs.push_back(calib);
92  readers.push_back(reader);
93 
94  // report
95  edm::LogInfo("BTagSFProducer") << "Loaded "+discShortNames_[iDisc]+" SFs from weight file "+weightFiles_[iDisc]+" with\noperating point: "+opname+",\nmeasurement type: B="+measurementTypesB_[iDisc]+", C="+measurementTypesC_[iDisc]+", UDSG="+measurementTypesUDSG_[iDisc]+",\nsystematic type: "+sysTypes_[iDisc]+".\n" << std::endl;
96 
97  // find if multiple MiniAOD branches need to be summed up (e.g., DeepCSV b+bb) and separate them using '+' delimiter from config
98  std::stringstream dName(discNames_[iDisc]);
100  std::vector<std::string> branches;
101  while (std::getline(dName, branch, '+')) {
102  branches.push_back(branch);
103  }
104  inBranchNames.push_back(branches);
105  }
106  else {
107  //BTagCalibration calib;
109  //calibs.push_back(calib); //dummy, so that index of vectors still match
110  readers.push_back(reader); //dummy, so that index of vectors still match
111  std::vector<std::string> branches;
112  branches.push_back("");
113  inBranchNames.push_back(branches);
114 
115  // report
116  edm::LogWarning("BTagSFProducer") << "Skipped loading BTagCalibration for "+discShortNames_[iDisc]+" as it was marked as unavailable in the configuration file. Event weights will not be stored.\n" << std::endl;
117  }
118  }
119  }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
BTagEntry::OperatingPoint op
const StringCutObjectSelector< pat::Jet > cut_
edm::EDGetTokenT< std::vector< pat::Jet > > src_
unsigned int nDiscs
std::vector< std::string > measurementTypesUDSG_
std::vector< std::string > measurementTypesB_
std::vector< std::string > weightFiles_
std::vector< std::string > measurementTypesC_
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
std::vector< std::string > sysTypes_
std::vector< BTagCalibrationReader > readers
std::vector< std::vector< std::string > > inBranchNames
std::vector< std::string > discNames_
std::vector< std::string > operatingPoints_
void load(const BTagCalibration &c, BTagEntry::JetFlavor jf, const std::string &measurementType="comb")
std::vector< std::string > discShortNames_
BTagSFProducer::~BTagSFProducer ( )
inlineoverride

Definition at line 121 of file BTagSFProducer.cc.

121 {};

Member Function Documentation

static void BTagSFProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
inlinestatic

Definition at line 123 of file BTagSFProducer.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::add(), edm::ParameterSetDescription::addUntracked(), produce(), and AlCaHLTBitMon_QueryRunRegistry::string.

123  {
125 
126  desc.add<edm::InputTag>("src")->setComment("input AK4 jet collection");
127  desc.add<std::string>("cut")->setComment("minimum pT and maximum eta cuts for jets");
128  desc.add<std::vector<std::string>>("discNames")->setComment("name of b-tag discriminator branch in MiniAOD");
129  desc.add<std::vector<std::string>>("discShortNames")->setComment("common name of discriminator");
130  desc.add<std::vector<std::string>>("weightFiles")->setComment("path to the .csv file containing the SFs");
131  desc.add<std::vector<std::string>>("operatingPoints")->setComment("loose = 0, medium = 1, tight = 2, disriminator reshaping = 3");
132  desc.add<std::vector<std::string>>("measurementTypesB")->setComment("e.g. \"ttbar\", \"comb\", \"incl\", \"iterativefit\" for b jets");
133  desc.add<std::vector<std::string>>("measurementTypesC")->setComment("e.g. \"ttbar\", \"comb\", \"incl\", \"iterativefit\" for c jets");
134  desc.add<std::vector<std::string>>("measurementTypesUDSG")->setComment("e.g. \"ttbar\", \"comb\", \"incl\", \"iterativefit\" for light jets");
135  desc.add<std::vector<std::string>>("sysTypes")->setComment("\"up\", \"central\", \"down\", but arbitrary strings possible, like \"up_generator\" or \"up_jec\"");
136  desc.addUntracked<bool>("validate", false)->setComment("validate the function expressions in the weightFiles");
137 
138  descriptions.add("BTagWeightTable", desc);
139  }
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void BTagSFProducer::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 166 of file BTagSFProducer.cc.

References cut_, DEFINE_FWK_MODULE, discShortNames_, PVValHelper::eta, BTagEntry::FLAV_B, BTagEntry::FLAV_C, BTagEntry::FLAV_UDSG, GenHFHadronMatcher_cff::flavour, nanoaod::FlatTable::FloatColumn, edm::Event::getByToken(), inBranchNames, metsig::jet, fwrapper::jets, eostools::move(), nDiscs, op, BTagEntry::OP_RESHAPING, MillePedeFileConverter_cfg::out, EnergyCorrector::pt, edm::Event::put(), readers, src_, sysTypes_, and weightFiles_.

Referenced by fillDescriptions().

167 {
168  using namespace edm;
169  using namespace std;
170 
172  iEvent.getByToken(src_, jets);
173 
174  double pt;
175  double eta;
176  int flavour;
177  double bdisc;
178  double SF;
179 
180  double EventWt;
181 
182  auto out = std::make_unique<nanoaod::FlatTable>(1, "btagWeight", true);
183  out->setDoc("b-tagging event weights");
184 
185  for (unsigned int iDisc = 0; iDisc < nDiscs; ++iDisc) { // loop over b-tagging algorithms
186 
187  if (weightFiles_[iDisc]!="unavailable") {
188  EventWt=1.;
189  for (const pat::Jet & jet : *jets) { // loop over jets and accumulate product of SF for each jet
190  pt=jet.pt();
191  eta=jet.eta();
192  bdisc=0.;
193 
195  for (string inBranch : inBranchNames[iDisc]) { //sum up the discriminator values if multiple, e.g. DeepCSV b+bb
196  bdisc+=jet.bDiscriminator(inBranch);
197  }
198  }
199 
200  flavour=jet.hadronFlavour();
201 
202  if (cut_(jet)) { //multiply SF of only the jets that pass the cut
203  if (fabs(flavour) == 5) { // b jets
204  SF = readers[iDisc].eval_auto_bounds(sysTypes_[iDisc],BTagEntry::FLAV_B,eta,pt,bdisc);
205  }
206  else if (fabs(flavour) == 4) { // c jets
207  SF = readers[iDisc].eval_auto_bounds(sysTypes_[iDisc],BTagEntry::FLAV_C,eta,pt,bdisc);
208  }
209  else { // others
210  SF = readers[iDisc].eval_auto_bounds(sysTypes_[iDisc],BTagEntry::FLAV_UDSG,eta,pt,bdisc);
211  }
212  }
213  else {
214  SF=1.;
215  }
216 
217 
218 
219  if (SF==0.) { // default value of SF is set to 1 in case BTagCalibration returns 0
220  //no need to log this as could be pretty common, leaving the cout commented in case this is needed by the author for simple debugging
221  //cout << discShortNames_[iDisc]+" SF not found for jet with pT="+to_string(pt)+", eta="+to_string(eta)+", discValue="+to_string(bdisc)+", flavour="+to_string(flavour) +". Setting SF to 1." << endl;
222  SF=1.;
223  }
224 
225  EventWt *= SF;
226  }
227 
228  out->addColumnValue<float>(discShortNames_[iDisc], EventWt, "b-tag event weight for "+discShortNames_[iDisc], nanoaod::FlatTable::FloatColumn);
229  }
230  }
231 
232  iEvent.put(move(out));
233 }
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
BTagEntry::OperatingPoint op
const StringCutObjectSelector< pat::Jet > cut_
edm::EDGetTokenT< std::vector< pat::Jet > > src_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
unsigned int nDiscs
std::vector< std::string > weightFiles_
std::vector< std::string > sysTypes_
vector< PseudoJet > jets
std::vector< BTagCalibrationReader > readers
std::vector< std::vector< std::string > > inBranchNames
Analysis-level calorimeter jet class.
Definition: Jet.h:80
HLT enums.
std::vector< std::string > discShortNames_
def move(src, dest)
Definition: eostools.py:511

Member Data Documentation

const StringCutObjectSelector<pat::Jet> BTagSFProducer::cut_
private

Definition at line 145 of file BTagSFProducer.cc.

Referenced by produce().

std::vector<std::string> BTagSFProducer::discNames_
private

Definition at line 147 of file BTagSFProducer.cc.

Referenced by BTagSFProducer().

std::vector<std::string> BTagSFProducer::discShortNames_
private

Definition at line 148 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

std::vector<std::vector<std::string> > BTagSFProducer::inBranchNames
private

Definition at line 157 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

std::vector<std::string> BTagSFProducer::measurementTypesB_
private

Definition at line 151 of file BTagSFProducer.cc.

Referenced by BTagSFProducer().

std::vector<std::string> BTagSFProducer::measurementTypesC_
private

Definition at line 152 of file BTagSFProducer.cc.

Referenced by BTagSFProducer().

std::vector<std::string> BTagSFProducer::measurementTypesUDSG_
private

Definition at line 153 of file BTagSFProducer.cc.

Referenced by BTagSFProducer().

unsigned int BTagSFProducer::nDiscs
private

Definition at line 160 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

BTagEntry::OperatingPoint BTagSFProducer::op
private

Definition at line 156 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

std::vector<std::string> BTagSFProducer::operatingPoints_
private

Definition at line 150 of file BTagSFProducer.cc.

Referenced by BTagSFProducer().

std::vector<BTagCalibrationReader> BTagSFProducer::readers
private

Definition at line 159 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

edm::EDGetTokenT<std::vector<pat::Jet> > BTagSFProducer::src_
private

Definition at line 144 of file BTagSFProducer.cc.

Referenced by produce().

std::vector<std::string> BTagSFProducer::sysTypes_
private

Definition at line 154 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().

std::vector<std::string> BTagSFProducer::weightFiles_
private

Definition at line 149 of file BTagSFProducer.cc.

Referenced by BTagSFProducer(), and produce().