CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Protected Attributes
GenWeightsTableProducer Class Reference
Inheritance diagram for GenWeightsTableProducer:
edm::global::EDProducer< edm::StreamCache< LumiCacheInfoHolder >, edm::RunCache< DynamicWeightChoice >, edm::RunSummaryCache< CounterMap >, edm::EndRunProducer > edm::global::EDProducerBase edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

Public Member Functions

std::unique_ptr< LumiCacheInfoHolder > beginStream (edm::StreamID) const override
 
void fillLHEPdfWeightTablesFromGenInfo (Counter *counter, const DynamicWeightChoiceGenInfo *weightChoice, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
 
void fillLHEWeightTables (Counter *counter, const DynamicWeightChoice *weightChoice, double genWeight, const LHEEventProduct &lheProd, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outRwgt, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
 
void fillOnlyPSWeightTable (Counter *counter, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
 
 GenWeightsTableProducer (edm::ParameterSet const &params)
 
std::shared_ptr< DynamicWeightChoice > globalBeginRun (edm::Run const &iRun, edm::EventSetup const &) const override
 
std::shared_ptr< CounterMap > globalBeginRunSummary (edm::Run const &, edm::EventSetup const &) const override
 
void globalEndRun (edm::Run const &, edm::EventSetup const &) const override
 
void globalEndRunProduce (edm::Run &iRun, edm::EventSetup const &, CounterMap const *runCounterMap) const override
 
void globalEndRunSummary (edm::Run const &, edm::EventSetup const &, CounterMap *runCounterMap) const override
 
void produce (edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
 
void streamBeginLuminosityBlock (edm::StreamID id, edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &eventSetup) const override
 
void streamBeginRun (edm::StreamID id, edm::Run const &, edm::EventSetup const &) const override
 
void streamEndRunSummary (edm::StreamID id, edm::Run const &, edm::EventSetup const &, CounterMap *runCounterMap) const override
 
 ~GenWeightsTableProducer () override
 
- Public Member Functions inherited from edm::global::EDProducer< edm::StreamCache< LumiCacheInfoHolder >, edm::RunCache< DynamicWeightChoice >, edm::RunSummaryCache< CounterMap >, edm::EndRunProducer >
 EDProducer ()=default
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () const final
 
bool wantsGlobalLuminosityBlocks () const final
 
bool wantsGlobalRuns () const final
 
bool wantsStreamLuminosityBlocks () const final
 
bool wantsStreamRuns () const final
 
- Public Member Functions inherited from edm::global::EDProducerBase
 EDProducerBase ()
 
ModuleDescription const & moduleDescription () const
 
 ~EDProducerBase () override
 
- Public Member Functions inherited from edm::ProducerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
std::vector< edm::ProductResolverIndex > const & indiciesForPutProducts (BranchType iBranchType) const
 
 ProducerBase ()
 
std::vector< edm::ProductResolverIndex > const & putTokenIndexToProductResolverIndex () const
 
void registerProducts (ProducerBase *, ProductRegistry *, ModuleDescription const &)
 
std::function< void(BranchDescription const &)> registrationCallback () const
 used by the fwk to register list of products More...
 
void resolvePutIndicies (BranchType iBranchType, ModuleToResolverIndicies const &iIndicies, std::string const &moduleLabel)
 
 ~ProducerBase () noexcept(false) override
 
- Public Member Functions inherited from edm::EDConsumerBase
std::vector< ConsumesInfoconsumesInfo () const
 
void convertCurrentProcessAlias (std::string const &processName)
 Convert "@currentProcess" in InputTag process names to the actual current process name. More...
 
 EDConsumerBase ()
 
 EDConsumerBase (EDConsumerBase const &)=delete
 
 EDConsumerBase (EDConsumerBase &&)=default
 
ESProxyIndex const * esGetTokenIndices (edm::Transition iTrans) const
 
ProductResolverIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
std::vector< ProductResolverIndexAndSkipBit > const & itemsToGetFrom (BranchType iType) const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
void modulesWhoseProductsAreConsumed (std::vector< ModuleDescription const * > &modules, ProductRegistry const &preg, std::map< std::string, ModuleDescription const * > const &labelsToDesc, std::string const &processName) const
 
EDConsumerBase const & operator= (EDConsumerBase const &)=delete
 
EDConsumerBaseoperator= (EDConsumerBase &&)=default
 
bool registeredToConsume (ProductResolverIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
ProductResolverIndexAndSkipBit uncheckedIndexFrom (EDGetToken) const
 
void updateLookup (BranchType iBranchType, ProductResolverIndexHelper const &, bool iPrefetchMayGet)
 
void updateLookup (eventsetup::ESRecordsToProxyIndices const &)
 
virtual ~EDConsumerBase () noexcept(false)
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
- Static Public Member Functions inherited from edm::global::EDProducerBase
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &descriptions)
 

Protected Attributes

std::atomic< bool > debug_
 
std::atomic< bool > debugRun_
 
const edm::EDGetTokenT< GenLumiInfoHeadergenLumiInfoHeadTag_
 
const edm::EDGetTokenT< GenEventInfoProductgenTag_
 
std::atomic< bool > hasIssuedWarning_
 
bool keepAllPSWeights_
 
std::unordered_map< std::string, uint32_t > lhaNameToID_
 
const std::vector< edm::InputTaglheLabel_
 
const std::vector< edm::EDGetTokenT< LHERunInfoProduct > > lheRunTag_
 
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
 
int lheWeightPrecision_
 
unsigned int maxPdfWeights_
 
std::vector< std::string > namedWeightIDs_
 
std::vector< std::string > namedWeightLabels_
 
std::vector< uint32_t > preferredPDFLHAIDs_
 

Additional Inherited Members

- Public Types inherited from edm::global::EDProducerBase
typedef EDProducerBase ModuleType
 
- Public Types inherited from edm::ProducerBase
using ModuleToResolverIndicies = std::unordered_multimap< std::string, std::tuple< edm::TypeID const *, const char *, edm::ProductResolverIndex >>
 
typedef ProductRegistryHelper::TypeLabelList TypeLabelList
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Protected Member Functions inherited from edm::EDConsumerBase
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes ()
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes (ESInputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 

Detailed Description

Definition at line 238 of file GenWeightsTableProducer.cc.

Constructor & Destructor Documentation

GenWeightsTableProducer::GenWeightsTableProducer ( edm::ParameterSet const &  params)
inline

Definition at line 243 of file GenWeightsTableProducer.cc.

References Exception, edm::ParameterSet::getParameter(), nano_cff::lhaid, dataset::name, AlCaHLTBitMon_QueryRunRegistry::string, GlobalPosition_Frontier_DevDB_cff::tag, and edm::vector_transform().

244  : genTag_(consumes<GenEventInfoProduct>(params.getParameter<edm::InputTag>("genEvent"))),
245  lheLabel_(params.getParameter<std::vector<edm::InputTag>>("lheInfo")),
247  [this](const edm::InputTag& tag) { return mayConsume<LHEEventProduct>(tag); })),
249  lheLabel_, [this](const edm::InputTag& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
251  mayConsume<GenLumiInfoHeader, edm::InLumi>(params.getParameter<edm::InputTag>("genLumiInfoHeader"))),
252  namedWeightIDs_(params.getParameter<std::vector<std::string>>("namedWeightIDs")),
253  namedWeightLabels_(params.getParameter<std::vector<std::string>>("namedWeightLabels")),
254  lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")),
255  maxPdfWeights_(params.getParameter<uint32_t>("maxPdfWeights")),
256  keepAllPSWeights_(params.getParameter<bool>("keepAllPSWeights")),
257  debug_(params.getUntrackedParameter<bool>("debug", false)),
258  debugRun_(debug_.load()),
259  hasIssuedWarning_(false) {
260  produces<nanoaod::FlatTable>();
261  produces<std::string>("genModel");
262  produces<nanoaod::FlatTable>("LHEScale");
263  produces<nanoaod::FlatTable>("LHEPdf");
264  produces<nanoaod::FlatTable>("LHEReweighting");
265  produces<nanoaod::FlatTable>("LHENamed");
266  produces<nanoaod::FlatTable>("PS");
267  produces<nanoaod::MergeableCounterTable, edm::Transition::EndRun>();
268  if (namedWeightIDs_.size() != namedWeightLabels_.size()) {
269  throw cms::Exception("Configuration", "Size mismatch between namedWeightIDs & namedWeightLabels");
270  }
271  for (const edm::ParameterSet& pdfps : params.getParameter<std::vector<edm::ParameterSet>>("preferredPDFs")) {
272  const std::string& name = pdfps.getParameter<std::string>("name");
273  uint32_t lhaid = pdfps.getParameter<uint32_t>("lhaid");
274  preferredPDFLHAIDs_.push_back(lhaid);
276  lhaNameToID_[name + ".LHgrid"] = lhaid;
277  }
278  }
const std::vector< edm::EDGetTokenT< LHERunInfoProduct > > lheRunTag_
T getParameter(std::string const &) const
const edm::EDGetTokenT< GenEventInfoProduct > genTag_
std::atomic< bool > hasIssuedWarning_
std::unordered_map< std::string, uint32_t > lhaNameToID_
const std::vector< edm::InputTag > lheLabel_
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
std::vector< std::string > namedWeightLabels_
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
std::vector< uint32_t > preferredPDFLHAIDs_
std::vector< std::string > namedWeightIDs_
const edm::EDGetTokenT< GenLumiInfoHeader > genLumiInfoHeadTag_
GenWeightsTableProducer::~GenWeightsTableProducer ( )
inlineoverride

Definition at line 280 of file GenWeightsTableProducer.cc.

280 {}

Member Function Documentation

std::unique_ptr<LumiCacheInfoHolder> GenWeightsTableProducer::beginStream ( edm::StreamID  ) const
inlineoverride

Definition at line 888 of file GenWeightsTableProducer.cc.

888  {
889  return std::make_unique<LumiCacheInfoHolder>();
890  }
static void GenWeightsTableProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
inlinestatic

Definition at line 1064 of file GenWeightsTableProducer.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::add(), edm::ParameterSetDescription::addOptionalUntracked(), edm::ParameterSetDescription::addVPSet(), edm::ParameterDescriptionNode::setComment(), and AlCaHLTBitMon_QueryRunRegistry::string.

1064  {
1066  desc.add<edm::InputTag>("genEvent", edm::InputTag("generator"))
1067  ->setComment("tag for the GenEventInfoProduct, to get the main weight");
1068  desc.add<edm::InputTag>("genLumiInfoHeader", edm::InputTag("generator"))
1069  ->setComment("tag for the GenLumiInfoProduct, to get the model string");
1070  desc.add<std::vector<edm::InputTag>>("lheInfo", std::vector<edm::InputTag>{{"externalLHEProducer"}, {"source"}})
1071  ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
1072 
1074  prefpdf.add<std::string>("name");
1075  prefpdf.add<uint32_t>("lhaid");
1076  desc.addVPSet("preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())
1077  ->setComment(
1078  "LHA PDF Ids of the preferred PDF sets, in order of preference (the first matching one will be used)");
1079  desc.add<std::vector<std::string>>("namedWeightIDs")->setComment("set of LHA weight IDs for named LHE weights");
1080  desc.add<std::vector<std::string>>("namedWeightLabels")
1081  ->setComment("output names for the namedWeightIDs (in the same order)");
1082  desc.add<int32_t>("lheWeightPrecision")->setComment("Number of bits in the mantissa for LHE weights");
1083  desc.add<uint32_t>("maxPdfWeights")->setComment("Maximum number of PDF weights to save (to crop NN replicas)");
1084  desc.add<bool>("keepAllPSWeights")->setComment("Store all PS weights found");
1085  desc.addOptionalUntracked<bool>("debug")->setComment("dump out all LHE information for one event");
1086  descriptions.add("genWeightsTable", desc);
1087  }
void setComment(std::string const &value)
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
void GenWeightsTableProducer::fillLHEPdfWeightTablesFromGenInfo ( Counter *  counter,
const DynamicWeightChoiceGenInfo *  weightChoice,
double  genWeight,
const GenEventInfoProduct genProd,
std::unique_ptr< nanoaod::FlatTable > &  outScale,
std::unique_ptr< nanoaod::FlatTable > &  outPdf,
std::unique_ptr< nanoaod::FlatTable > &  outNamed,
std::unique_ptr< nanoaod::FlatTable > &  outPS 
) const
inline

Definition at line 447 of file GenWeightsTableProducer.cc.

References nanoaod::FlatTable::FloatColumn, mps_fire::i, AlCaHLTBitMon_QueryRunRegistry::string, HGCalRecHit_cfi::weights, and GenEventInfoProduct::weights().

454  {
455  const std::vector<unsigned int>& scaleWeightIDs = weightChoice->scaleWeightIDs;
456  const std::vector<unsigned int>& pdfWeightIDs = weightChoice->pdfWeightIDs;
457  const std::vector<unsigned int>& psWeightIDs = weightChoice->psWeightIDs;
458 
459  auto weights = genProd.weights();
460  double w0 = (weights.size() > 1) ? weights.at(1) : 1.;
461  double originalXWGTUP = (weights.size() > 1) ? weights.at(1) : 1.;
462 
463  std::vector<double> wScale, wPDF, wPS;
464  for (auto id : scaleWeightIDs)
465  wScale.push_back(weights.at(id) / w0);
466  for (auto id : pdfWeightIDs) {
467  wPDF.push_back(weights.at(id) / w0);
468  }
469  if (!psWeightIDs.empty()) {
470  double psNom =
471  weights.at(psWeightIDs.at(0)); // normalise PS weights by "Baseline", which should be the first entry
472  for (std::size_t i = 1; i < psWeightIDs.size(); i++)
473  wPS.push_back(weights.at(psWeightIDs.at(i)) / psNom);
474  } else
475  wPS.push_back(1.0);
476  std::string psWeightDocStr;
477  if (keepAllPSWeights_) {
478  psWeightDocStr = "All PS weights (w_var / w_nominal)";
479  } else {
480  psWeightDocStr =
481  "PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 "
482  "FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 ";
483  }
484 
485  outScale = std::make_unique<nanoaod::FlatTable>(wScale.size(), "LHEScaleWeight", false);
486  outScale->addColumn<float>(
487  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
488 
489  outPdf = std::make_unique<nanoaod::FlatTable>(wPDF.size(), "LHEPdfWeight", false);
490  outPdf->addColumn<float>(
491  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
492 
493  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
494  outPS->addColumn<float>("",
495  wPS,
496  wPS.size() > 1 ? psWeightDocStr : "dummy PS weight (1.0) ",
499 
500  outNamed = std::make_unique<nanoaod::FlatTable>(1, "LHEWeight", true);
501  outNamed->addColumnValue<float>(
502  "originalXWGTUP", originalXWGTUP, "Nominal event weight in the LHE file", nanoaod::FlatTable::FloatColumn);
503  /*for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
504  outNamed->addColumnValue<float>(namedWeightLabels_[i], wNamed[i], "LHE weight for id "+namedWeightIDs_[i]+", relative to nominal", nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
505  }*/
506 
507  counter->incLHE(genWeight, wScale, wPDF, std::vector<double>(), std::vector<double>(), wPS);
508  }
std::vector< double > & weights()
void GenWeightsTableProducer::fillLHEWeightTables ( Counter *  counter,
const DynamicWeightChoice *  weightChoice,
double  genWeight,
const LHEEventProduct lheProd,
const GenEventInfoProduct genProd,
std::unique_ptr< nanoaod::FlatTable > &  outScale,
std::unique_ptr< nanoaod::FlatTable > &  outPdf,
std::unique_ptr< nanoaod::FlatTable > &  outRwgt,
std::unique_ptr< nanoaod::FlatTable > &  outNamed,
std::unique_ptr< nanoaod::FlatTable > &  outPS 
) const
inline

Definition at line 349 of file GenWeightsTableProducer.cc.

References spr::find(), nanoaod::FlatTable::FloatColumn, mps_fire::i, gen::n, LHEEventProduct::originalXWGTUP(), AlCaHLTBitMon_QueryRunRegistry::string, GenEventInfoProduct::weights(), and LHEEventProduct::weights().

358  {
359  bool lheDebug = debug_.exchange(
360  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
361 
362  const std::vector<std::string>& scaleWeightIDs = weightChoice->scaleWeightIDs;
363  const std::vector<std::string>& pdfWeightIDs = weightChoice->pdfWeightIDs;
364  const std::vector<std::string>& rwgtWeightIDs = weightChoice->rwgtIDs;
365 
366  double w0 = lheProd.originalXWGTUP();
367 
368  std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1),
369  wNamed(namedWeightIDs_.size(), 1);
370  for (auto& weight : lheProd.weights()) {
371  if (lheDebug)
372  printf("Weight %+9.5f rel %+9.5f for id %s\n", weight.wgt, weight.wgt / w0, weight.id.c_str());
373  // now we do it slowly, can be optimized
374  auto mScale = std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(), weight.id);
375  if (mScale != scaleWeightIDs.end())
376  wScale[mScale - scaleWeightIDs.begin()] = weight.wgt / w0;
377 
378  auto mPDF = std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(), weight.id);
379  if (mPDF != pdfWeightIDs.end())
380  wPDF[mPDF - pdfWeightIDs.begin()] = weight.wgt / w0;
381 
382  auto mRwgt = std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(), weight.id);
383  if (mRwgt != rwgtWeightIDs.end())
384  wRwgt[mRwgt - rwgtWeightIDs.begin()] = weight.wgt / w0;
385 
386  auto mNamed = std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(), weight.id);
387  if (mNamed != namedWeightIDs_.end())
388  wNamed[mNamed - namedWeightIDs_.begin()] = weight.wgt / w0;
389  }
390 
391  std::size_t vectorSize =
392  (genProd.weights().size() > 2)
393  ? (keepAllPSWeights_ ? (genProd.weights().size() - 2)
394  : ((genProd.weights().size() == 14 || genProd.weights().size() == 46) ? 4 : 1))
395  : 1;
396  std::vector<double> wPS(vectorSize, 1);
397  std::string psWeightDocStr;
398  if (vectorSize > 1) {
399  double nominal = genProd.weights()[1]; // Called 'Baseline' in GenLumiInfoHeader
400  if (keepAllPSWeights_) {
401  for (std::size_t i = 0; i < vectorSize; i++) {
402  wPS[i] = (genProd.weights()[i + 2]) / nominal;
403  }
404  psWeightDocStr = "All PS weights (w_var / w_nominal)";
405  } else {
406  for (std::size_t i = 6; i < 10; i++) {
407  wPS[i - 6] = (genProd.weights()[i]) / nominal;
408  }
409  psWeightDocStr =
410  "PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 "
411  "FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 ";
412  }
413  } else {
414  psWeightDocStr = "dummy PS weight (1.0) ";
415  }
416  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
417  outPS->addColumn<float>("", wPS, psWeightDocStr, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
418 
419  outScale = std::make_unique<nanoaod::FlatTable>(wScale.size(), "LHEScaleWeight", false);
420  outScale->addColumn<float>(
421  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
422 
423  outPdf = std::make_unique<nanoaod::FlatTable>(wPDF.size(), "LHEPdfWeight", false);
424  outPdf->addColumn<float>(
425  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
426 
427  outRwgt = std::make_unique<nanoaod::FlatTable>(wRwgt.size(), "LHEReweightingWeight", false);
428  outRwgt->addColumn<float>(
429  "", wRwgt, weightChoice->rwgtWeightDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
430 
431  outNamed = std::make_unique<nanoaod::FlatTable>(1, "LHEWeight", true);
432  outNamed->addColumnValue<float>("originalXWGTUP",
433  lheProd.originalXWGTUP(),
434  "Nominal event weight in the LHE file",
436  for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
437  outNamed->addColumnValue<float>(namedWeightLabels_[i],
438  wNamed[i],
439  "LHE weight for id " + namedWeightIDs_[i] + ", relative to nominal",
442  }
443 
444  counter->incLHE(genWeight, wScale, wPDF, wRwgt, wNamed, wPS);
445  }
double originalXWGTUP() const
Definition: weight.py:1
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const std::vector< WGT > & weights() const
std::vector< std::string > namedWeightLabels_
std::vector< double > & weights()
std::vector< std::string > namedWeightIDs_
void GenWeightsTableProducer::fillOnlyPSWeightTable ( Counter *  counter,
double  genWeight,
const GenEventInfoProduct genProd,
std::unique_ptr< nanoaod::FlatTable > &  outPS 
) const
inline

Definition at line 510 of file GenWeightsTableProducer.cc.

References nanoaod::FlatTable::FloatColumn, mps_fire::i, AlCaHLTBitMon_QueryRunRegistry::string, and GenEventInfoProduct::weights().

513  {
514  std::size_t vectorSize =
515  (genProd.weights().size() > 2)
516  ? (keepAllPSWeights_ ? (genProd.weights().size() - 2)
517  : ((genProd.weights().size() == 14 || genProd.weights().size() == 46) ? 4 : 1))
518  : 1;
519  std::vector<double> wPS(vectorSize, 1);
520  std::string psWeightDocStr;
521  if (vectorSize > 1) {
522  double nominal = genProd.weights()[1]; // Called 'Baseline' in GenLumiInfoHeader
523  if (keepAllPSWeights_) {
524  for (std::size_t i = 0; i < vectorSize; i++) {
525  wPS[i] = (genProd.weights()[i + 2]) / nominal;
526  }
527  psWeightDocStr = "All PS weights (w_var / w_nominal)";
528  } else {
529  for (std::size_t i = 6; i < 10; i++) {
530  wPS[i - 6] = (genProd.weights()[i]) / nominal;
531  }
532  psWeightDocStr =
533  "PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 "
534  "FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 ";
535  }
536  } else {
537  psWeightDocStr = "dummy PS weight (1.0) ";
538  }
539  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
540  outPS->addColumn<float>("", wPS, psWeightDocStr, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
541 
542  counter->incGenOnly(genWeight);
543  counter->incPSOnly(genWeight, wPS);
544  }
std::vector< double > & weights()
std::shared_ptr<DynamicWeightChoice> GenWeightsTableProducer::globalBeginRun ( edm::Run const &  iRun,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 547 of file GenWeightsTableProducer.cc.

References popcon2dropbox::copy(), gather_cfg::cout, end, spr::find(), plotBeamSpotDB::first, runEdmFileComparison::found, edm::Run::getByLabel(), LHERunInfoProduct::headers_begin(), LHERunInfoProduct::headers_end(), triggerObjects_cff::id, edm::HandleBase::isValid(), nano_cff::lhaid, nano_cff::lheInfo, groupFilesInBlocks::lines, and AlCaHLTBitMon_QueryRunRegistry::string.

547  {
549 
550  bool lheDebug = debugRun_.exchange(
551  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
552  auto weightChoice = std::make_shared<DynamicWeightChoice>();
553 
554  // getByToken throws since we're not in the endRun (see https://github.com/cms-sw/cmssw/pull/18499)
555  //if (iRun.getByToken(lheRunTag_, lheInfo)) {
556  for (const auto& lheLabel : lheLabel_) {
557  iRun.getByLabel(lheLabel, lheInfo);
558  if (lheInfo.isValid()) {
559  break;
560  }
561  }
562  if (lheInfo.isValid()) {
563  std::vector<ScaleVarWeight> scaleVariationIDs;
564  std::vector<PDFSetWeights> pdfSetWeightIDs;
565  std::vector<std::string> lheReweighingIDs;
566 
567  std::regex weightgroupmg26x("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
568  std::regex weightgroup("<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
569  std::regex weightgroupRwgt("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
570  std::regex endweightgroup("</weightgroup>");
571  std::regex scalewmg26x(
572  "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"("
573  "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
574  std::regex scalew(
575  "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+("
576  "?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
577  std::regex pdfw(
578  "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
579  std::regex pdfwOld("<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
580  std::regex pdfwmg26x(
581  "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF "
582  "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
583  "weight>");
584  std::regex rwgt("<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
585  std::smatch groups;
586  for (auto iter = lheInfo->headers_begin(), end = lheInfo->headers_end(); iter != end; ++iter) {
587  if (iter->tag() != "initrwgt") {
588  if (lheDebug)
589  std::cout << "Skipping LHE header with tag" << iter->tag() << std::endl;
590  continue;
591  }
592  if (lheDebug)
593  std::cout << "Found LHE header with tag" << iter->tag() << std::endl;
594  std::vector<std::string> lines = iter->lines();
595  bool missed_weightgroup =
596  false; //Needed because in some of the samples ( produced with MG26X ) a small part of the header info is ordered incorrectly
597  bool ismg26x = false;
598  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
599  ++iLine) { //First start looping through the lines to see which weightgroup pattern is matched
600  boost::replace_all(lines[iLine], "&lt;", "<");
601  boost::replace_all(lines[iLine], "&gt;", ">");
602  if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
603  ismg26x = true;
604  }
605  }
606  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
607  if (lheDebug)
608  std::cout << lines[iLine];
609  if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
610  std::string groupname = groups.str(2);
611  if (ismg26x)
612  groupname = groups.str(1);
613  if (lheDebug)
614  std::cout << ">>> Looks like the beginning of a weight group for '" << groupname << "'" << std::endl;
615  if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation") {
616  if (lheDebug)
617  std::cout << ">>> Looks like scale variation for theory uncertainties" << std::endl;
618  for (++iLine; iLine < nLines; ++iLine) {
619  if (lheDebug)
620  std::cout << " " << lines[iLine];
621  if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
622  if (lheDebug)
623  std::cout << " >>> Scale weight " << groups[1].str() << " for " << groups[3].str() << " , "
624  << groups[4].str() << " , " << groups[5].str() << std::endl;
625  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
626  } else if (std::regex_search(lines[iLine], endweightgroup)) {
627  if (lheDebug)
628  std::cout << ">>> Looks like the end of a weight group" << std::endl;
629  if (!missed_weightgroup) {
630  break;
631  } else
632  missed_weightgroup = false;
633  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
634  if (lheDebug)
635  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
636  "of the group."
637  << std::endl;
638  if (ismg26x)
639  missed_weightgroup = true;
640  --iLine; // rewind by one, and go back to the outer loop
641  break;
642  }
643  }
644  } else if (groupname == "PDF_variation" || groupname.find("PDF_variation ") == 0) {
645  if (lheDebug)
646  std::cout << ">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
647  for (++iLine; iLine < nLines; ++iLine) {
648  if (lheDebug)
649  std::cout << " " << lines[iLine];
650  if (std::regex_search(lines[iLine], groups, pdfw)) {
651  unsigned int lhaID = std::stoi(groups.str(2));
652  if (lheDebug)
653  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
654  << std::endl;
655  if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
656  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
657  }
658  } else if (std::regex_search(lines[iLine], endweightgroup)) {
659  if (lheDebug)
660  std::cout << ">>> Looks like the end of a weight group" << std::endl;
661  if (!missed_weightgroup) {
662  break;
663  } else
664  missed_weightgroup = false;
665  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
666  if (lheDebug)
667  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
668  "of the group."
669  << std::endl;
670  if (ismg26x)
671  missed_weightgroup = true;
672  --iLine; // rewind by one, and go back to the outer loop
673  break;
674  }
675  }
676  } else if (groupname == "PDF_variation1" || groupname == "PDF_variation2") {
677  if (lheDebug)
678  std::cout << ">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
679  unsigned int lastid = 0;
680  for (++iLine; iLine < nLines; ++iLine) {
681  if (lheDebug)
682  std::cout << " " << lines[iLine];
683  if (std::regex_search(lines[iLine], groups, pdfw)) {
684  unsigned int id = std::stoi(groups.str(1));
685  unsigned int lhaID = std::stoi(groups.str(2));
686  if (lheDebug)
687  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
688  << std::endl;
689  if (id != (lastid + 1) || pdfSetWeightIDs.empty()) {
690  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
691  } else {
692  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
693  }
694  lastid = id;
695  } else if (std::regex_search(lines[iLine], endweightgroup)) {
696  if (lheDebug)
697  std::cout << ">>> Looks like the end of a weight group" << std::endl;
698  if (!missed_weightgroup) {
699  break;
700  } else
701  missed_weightgroup = false;
702  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
703  if (lheDebug)
704  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
705  "of the group."
706  << std::endl;
707  if (ismg26x)
708  missed_weightgroup = true;
709  --iLine; // rewind by one, and go back to the outer loop
710  break;
711  }
712  }
713  } else if (lhaNameToID_.find(groupname) != lhaNameToID_.end()) {
714  if (lheDebug)
715  std::cout << ">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
716  unsigned int firstLhaID = lhaNameToID_.find(groupname)->second;
717  bool first = true;
718  for (++iLine; iLine < nLines; ++iLine) {
719  if (lheDebug)
720  std::cout << " " << lines[iLine];
721  if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
722  unsigned int member = 0;
723  if (ismg26x == 0) {
724  member = std::stoi(groups.str(2));
725  } else {
726  if (!groups.str(4).empty()) {
727  member = std::stoi(groups.str(4));
728  }
729  }
730  unsigned int lhaID = member + firstLhaID;
731  if (lheDebug)
732  std::cout << " >>> PDF weight " << groups.str(1) << " for " << member << " = " << lhaID
733  << std::endl;
734  //if (member == 0) continue; // let's keep also the central value for now
735  if (first) {
736  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
737  first = false;
738  } else {
739  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
740  }
741  } else if (std::regex_search(lines[iLine], endweightgroup)) {
742  if (lheDebug)
743  std::cout << ">>> Looks like the end of a weight group" << std::endl;
744  if (!missed_weightgroup) {
745  break;
746  } else
747  missed_weightgroup = false;
748  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
749  if (lheDebug)
750  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
751  "of the group."
752  << std::endl;
753  if (ismg26x)
754  missed_weightgroup = true;
755  --iLine; // rewind by one, and go back to the outer loop
756  break;
757  }
758  }
759  } else {
760  for (++iLine; iLine < nLines; ++iLine) {
761  if (lheDebug)
762  std::cout << " " << lines[iLine];
763  if (std::regex_search(lines[iLine], groups, endweightgroup)) {
764  if (lheDebug)
765  std::cout << ">>> Looks like the end of a weight group" << std::endl;
766  if (!missed_weightgroup) {
767  break;
768  } else
769  missed_weightgroup = false;
770  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
771  if (lheDebug)
772  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
773  "of the group."
774  << std::endl;
775  if (ismg26x)
776  missed_weightgroup = true;
777  --iLine; // rewind by one, and go back to the outer loop
778  break;
779  }
780  }
781  }
782  } else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
783  std::string groupname = groups.str(1);
784  if (groupname == "mg_reweighting") {
785  if (lheDebug)
786  std::cout << ">>> Looks like a LHE weights for reweighting" << std::endl;
787  for (++iLine; iLine < nLines; ++iLine) {
788  if (lheDebug)
789  std::cout << " " << lines[iLine];
790  if (std::regex_search(lines[iLine], groups, rwgt)) {
791  std::string rwgtID = groups.str(1);
792  if (lheDebug)
793  std::cout << " >>> LHE reweighting weight: " << rwgtID << std::endl;
794  if (std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
795  // we're only interested in the beggining of the block
796  lheReweighingIDs.emplace_back(rwgtID);
797  }
798  } else if (std::regex_search(lines[iLine], endweightgroup)) {
799  if (lheDebug)
800  std::cout << ">>> Looks like the end of a weight group" << std::endl;
801  if (!missed_weightgroup) {
802  break;
803  } else
804  missed_weightgroup = false;
805  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
806  if (lheDebug)
807  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
808  "of the group."
809  << std::endl;
810  if (ismg26x)
811  missed_weightgroup = true;
812  --iLine; // rewind by one, and go back to the outer loop
813  break;
814  }
815  }
816  }
817  }
818  }
819  //std::cout << "============= END [ " << iter->tag() << " ] ============ \n\n" << std::endl;
820 
821  // ----- SCALE VARIATIONS -----
822  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
823  if (lheDebug)
824  std::cout << "Found " << scaleVariationIDs.size() << " scale variations: " << std::endl;
825  std::stringstream scaleDoc;
826  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
827  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
828  const auto& sw = scaleVariationIDs[isw];
829  if (isw)
830  scaleDoc << "; ";
831  scaleDoc << "[" << isw << "] is " << sw.label;
832  weightChoice->scaleWeightIDs.push_back(sw.wid);
833  if (lheDebug)
834  printf(" id %s: scales ren = % .2f fact = % .2f text = %s\n",
835  sw.wid.c_str(),
836  sw.scales.first,
837  sw.scales.second,
838  sw.label.c_str());
839  }
840  if (!scaleVariationIDs.empty())
841  weightChoice->scaleWeightsDoc = scaleDoc.str();
842 
843  // ------ PDF VARIATIONS (take the preferred one) -----
844  if (lheDebug) {
845  std::cout << "Found " << pdfSetWeightIDs.size() << " PDF set errors: " << std::endl;
846  for (const auto& pw : pdfSetWeightIDs)
847  printf("lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
848  pw.lhaIDs.first,
849  pw.lhaIDs.second,
850  pw.wids.size(),
851  pw.wids.front().c_str());
852  }
853 
854  // ------ LHE REWEIGHTING -------
855  if (lheDebug) {
856  std::cout << "Found " << lheReweighingIDs.size() << " reweighting weights" << std::endl;
857  }
858  std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
859 
860  std::stringstream pdfDoc;
861  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
862  bool found = false;
863  for (uint32_t lhaid : preferredPDFLHAIDs_) {
864  for (const auto& pw : pdfSetWeightIDs) {
865  if (pw.lhaIDs.first != lhaid && pw.lhaIDs.first != (lhaid + 1))
866  continue; // sometimes the first weight is not saved if that PDF is the nominal one for the sample
867  if (pw.wids.size() == 1)
868  continue; // only consider error sets
869  pdfDoc << pw.lhaIDs.first << " - " << pw.lhaIDs.second;
870  weightChoice->pdfWeightIDs = pw.wids;
871  if (maxPdfWeights_ < pw.wids.size()) {
872  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
873  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
874  }
875  weightChoice->pdfWeightsDoc = pdfDoc.str();
876  found = true;
877  break;
878  }
879  if (found)
880  break;
881  }
882  }
883  }
884  return weightChoice;
885  }
def copy(args, dbName)
headers_const_iterator headers_end() const
std::unordered_map< std::string, uint32_t > lhaNameToID_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const std::vector< edm::InputTag > lheLabel_
headers_const_iterator headers_begin() const
#define end
Definition: vmac.h:39
bool isValid() const
Definition: HandleBase.h:74
std::vector< uint32_t > preferredPDFLHAIDs_
std::shared_ptr<CounterMap> GenWeightsTableProducer::globalBeginRunSummary ( edm::Run const &  ,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 1000 of file GenWeightsTableProducer.cc.

1000  {
1001  return std::make_shared<CounterMap>();
1002  }
void GenWeightsTableProducer::globalEndRun ( edm::Run const &  ,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 1062 of file GenWeightsTableProducer.cc.

1062 {}
void GenWeightsTableProducer::globalEndRunProduce ( edm::Run iRun,
edm::EventSetup const &  ,
CounterMap const *  runCounterMap 
) const
inlineoverride

Definition at line 1013 of file GenWeightsTableProducer.cc.

References mps_fire::i, eostools::move(), gen::n, MillePedeFileConverter_cfg::out, edm::Run::put(), AlCaHLTBitMon_QueryRunRegistry::string, heppy_batch::val, and x.

1013  {
1014  auto out = std::make_unique<nanoaod::MergeableCounterTable>();
1015 
1016  for (const auto& x : runCounterMap->countermap) {
1017  auto runCounter = &(x.second);
1018  std::string label = (!x.first.empty()) ? (std::string("_") + x.first) : "";
1019  std::string doclabel = (!x.first.empty()) ? (std::string(", for model label ") + x.first) : "";
1020 
1021  out->addInt("genEventCount" + label, "event count" + doclabel, runCounter->num);
1022  out->addFloat("genEventSumw" + label, "sum of gen weights" + doclabel, runCounter->sumw);
1023  out->addFloat("genEventSumw2" + label, "sum of gen (weight^2)" + doclabel, runCounter->sumw2);
1024 
1025  double norm = runCounter->sumw ? 1.0 / runCounter->sumw : 1;
1026  auto sumScales = runCounter->sumScale;
1027  for (auto& val : sumScales)
1028  val *= norm;
1029  out->addVFloatWithNorm("LHEScaleSumw" + label,
1030  "Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw" + doclabel,
1031  sumScales,
1032  runCounter->sumw);
1033  auto sumPDFs = runCounter->sumPDF;
1034  for (auto& val : sumPDFs)
1035  val *= norm;
1036  out->addVFloatWithNorm("LHEPdfSumw" + label,
1037  "Sum of genEventWeight * LHEPdfWeight[i], divided by genEventSumw" + doclabel,
1038  sumPDFs,
1039  runCounter->sumw);
1040  if (!runCounter->sumRwgt.empty()) {
1041  auto sumRwgts = runCounter->sumRwgt;
1042  for (auto& val : sumRwgts)
1043  val *= norm;
1044  out->addVFloatWithNorm("LHEReweightingSumw" + label,
1045  "Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw" + doclabel,
1046  sumRwgts,
1047  runCounter->sumw);
1048  }
1049  if (!runCounter->sumNamed.empty()) { // it could be empty if there's no LHE info in the sample
1050  for (unsigned int i = 0, n = namedWeightLabels_.size(); i < n; ++i) {
1051  out->addFloatWithNorm(
1052  "LHESumw_" + namedWeightLabels_[i] + label,
1053  "Sum of genEventWeight * LHEWeight_" + namedWeightLabels_[i] + ", divided by genEventSumw" + doclabel,
1054  runCounter->sumNamed[i] * norm,
1055  runCounter->sumw);
1056  }
1057  }
1058  }
1059  iRun.put(std::move(out));
1060  }
char const * label
std::vector< std::string > namedWeightLabels_
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
def move(src, dest)
Definition: eostools.py:511
void GenWeightsTableProducer::globalEndRunSummary ( edm::Run const &  ,
edm::EventSetup const &  ,
CounterMap *  runCounterMap 
) const
inlineoverride

Definition at line 1011 of file GenWeightsTableProducer.cc.

1011 {}
void GenWeightsTableProducer::produce ( edm::StreamID  id,
edm::Event iEvent,
const edm::EventSetup iSetup 
) const
inlineoverride

Definition at line 282 of file GenWeightsTableProducer.cc.

References nanoaod::FlatTable::FloatColumn, edm::Event::getByToken(), edm::Event::getRun(), edm::Run::index(), edm::HandleBase::isValid(), nano_cff::lheInfo, eostools::move(), MillePedeFileConverter_cfg::out, edm::Event::put(), AlCaHLTBitMon_QueryRunRegistry::string, GenEventInfoProduct::weight(), and mps_merge::weight.

282  {
283  // get my counter for weights
284  Counter* counter = streamCache(id)->countermap.get();
285 
286  // generator information (always available)
288  iEvent.getByToken(genTag_, genInfo);
289  double weight = genInfo->weight();
290 
291  // table for gen info, always available
292  auto out = std::make_unique<nanoaod::FlatTable>(1, "genWeight", true);
293  out->setDoc("generator weight");
294  out->addColumnValue<float>("", weight, "generator weight", nanoaod::FlatTable::FloatColumn);
295  iEvent.put(std::move(out));
296 
297  std::string model_label = streamCache(id)->countermap.getLabel();
298  auto outM = std::make_unique<std::string>((!model_label.empty()) ? std::string("GenModel_") + model_label : "");
299  iEvent.put(std::move(outM), "genModel");
300  bool getLHEweightsFromGenInfo = !model_label.empty();
301 
302  // tables for LHE weights, may not be filled
303  std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
304  std::unique_ptr<nanoaod::FlatTable> genPSTab;
305 
307  for (const auto& lheTag : lheTag_) {
308  iEvent.getByToken(lheTag, lheInfo);
309  if (lheInfo.isValid()) {
310  break;
311  }
312  }
313  if (lheInfo.isValid()) {
314  if (getLHEweightsFromGenInfo)
315  edm::LogWarning("LHETablesProducer")
316  << "Found both a LHEEventProduct and a GenLumiInfoHeader: will only save weights from LHEEventProduct.\n";
317  // get the dynamic choice of weights
318  const DynamicWeightChoice* weightChoice = runCache(iEvent.getRun().index());
319  // go fill tables
321  counter, weightChoice, weight, *lheInfo, *genInfo, lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab, genPSTab);
322  } else if (getLHEweightsFromGenInfo) {
323  const DynamicWeightChoiceGenInfo* weightChoice = &(streamCache(id)->weightChoice);
325  counter, weightChoice, weight, *genInfo, lheScaleTab, lhePdfTab, lheNamedTab, genPSTab);
326  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
327  //lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
328  //genPSTab = std::make_unique<nanoaod::FlatTable>(1, "PSWeight", true);
329  } else {
330  // Still try to add the PS weights
331  fillOnlyPSWeightTable(counter, weight, *genInfo, genPSTab);
332  // make dummy values
333  lheScaleTab = std::make_unique<nanoaod::FlatTable>(1, "LHEScaleWeights", true);
334  lhePdfTab = std::make_unique<nanoaod::FlatTable>(1, "LHEPdfWeights", true);
335  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
336  lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
337  if (!hasIssuedWarning_.exchange(true)) {
338  edm::LogWarning("LHETablesProducer") << "No LHEEventProduct, so there will be no LHE Tables\n";
339  }
340  }
341 
342  iEvent.put(std::move(lheScaleTab), "LHEScale");
343  iEvent.put(std::move(lhePdfTab), "LHEPdf");
344  iEvent.put(std::move(lheRwgtTab), "LHEReweighting");
345  iEvent.put(std::move(lheNamedTab), "LHENamed");
346  iEvent.put(std::move(genPSTab), "PS");
347  }
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
const edm::EDGetTokenT< GenEventInfoProduct > genTag_
void fillLHEPdfWeightTablesFromGenInfo(Counter *counter, const DynamicWeightChoiceGenInfo *weightChoice, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
Definition: weight.py:1
Run const & getRun() const
Definition: Event.cc:99
double weight() const
std::atomic< bool > hasIssuedWarning_
void fillLHEWeightTables(Counter *counter, const DynamicWeightChoice *weightChoice, double genWeight, const LHEEventProduct &lheProd, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outRwgt, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
std::function< unsigned int(align::ID)> Counter
RunIndex index() const
Definition: Run.cc:21
void fillOnlyPSWeightTable(Counter *counter, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
bool isValid() const
Definition: HandleBase.h:74
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
def move(src, dest)
Definition: eostools.py:511
void GenWeightsTableProducer::streamBeginLuminosityBlock ( edm::StreamID  id,
edm::LuminosityBlock const &  lumiBlock,
edm::EventSetup const &  eventSetup 
) const
inlineoverride

Definition at line 895 of file GenWeightsTableProducer.cc.

References GenLumiInfoHeader::configDescription(), runEdmFileComparison::found, edm::LuminosityBlock::getByToken(), edmIntegrityCheck::group, edm::HandleBase::isValid(), label, nano_cff::lhaid, mps_splice::line, AlCaHLTBitMon_QueryRunRegistry::string, GenLumiInfoHeader::weightNames(), and x.

897  {
898  auto counterMap = &(streamCache(id)->countermap);
899  edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
900  lumiBlock.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
901  if (!genLumiInfoHead.isValid())
902  edm::LogWarning("LHETablesProducer")
903  << "No GenLumiInfoHeader product found, will not fill generator model string.\n";
905  if (genLumiInfoHead.isValid()) {
906  label = genLumiInfoHead->configDescription();
907  boost::replace_all(label, "-", "_");
908  boost::replace_all(label, "/", "_");
909  }
910  counterMap->setLabel(label);
911 
912  if (genLumiInfoHead.isValid()) {
913  auto weightChoice = &(streamCache(id)->weightChoice);
914 
915  std::vector<ScaleVarWeight> scaleVariationIDs;
916  std::vector<PDFSetWeights> pdfSetWeightIDs;
917  weightChoice->psWeightIDs.clear();
918 
919  std::regex scalew("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+)\\,\\s+mur=(\\S+)\\smuf=(\\S+)");
920  std::regex pdfw("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+),\\s+Member\\s+(\\d+)\\s+of\\ssets\\s+(\\w+\\b)");
921  std::smatch groups;
922  auto weightNames = genLumiInfoHead->weightNames();
923  std::unordered_map<std::string, uint32_t> knownPDFSetsFromGenInfo_;
924  unsigned int weightIter = 0;
925  for (const auto& line : weightNames) {
926  if (std::regex_search(line, groups, scalew)) { // scale variation
927  auto id = groups.str(1);
928  auto group = groups.str(2);
929  auto mur = groups.str(3);
930  auto muf = groups.str(4);
931  if (group.find("Central scale variation") != std::string::npos)
932  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
933  } else if (std::regex_search(line, groups, pdfw)) { // PDF variation
934  auto id = groups.str(1);
935  auto group = groups.str(2);
936  auto memberid = groups.str(3);
937  auto pdfset = groups.str(4);
938  if (group.find(pdfset) != std::string::npos) {
939  if (knownPDFSetsFromGenInfo_.find(pdfset) == knownPDFSetsFromGenInfo_.end()) {
940  knownPDFSetsFromGenInfo_[pdfset] = std::atoi(id.c_str());
941  pdfSetWeightIDs.emplace_back(id, std::atoi(id.c_str()));
942  } else
943  pdfSetWeightIDs.back().add(id, std::atoi(id.c_str()));
944  }
945  } else if (line.find("Baseline") != std::string::npos || line.find("isr") != std::string::npos ||
946  line.find("fsr") != std::string::npos) {
947  if (keepAllPSWeights_ || line.find("Baseline") != std::string::npos ||
948  line.find("Def") != std::string::npos) {
949  weightChoice->psWeightIDs.push_back(weightIter); // PS variations
950  }
951  }
952  weightIter++;
953  }
954 
955  weightChoice->scaleWeightIDs.clear();
956  weightChoice->pdfWeightIDs.clear();
957 
958  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
959  std::stringstream scaleDoc;
960  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
961  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
962  const auto& sw = scaleVariationIDs[isw];
963  if (isw)
964  scaleDoc << "; ";
965  scaleDoc << "[" << isw << "] is " << sw.label;
966  weightChoice->scaleWeightIDs.push_back(std::atoi(sw.wid.c_str()));
967  }
968  if (!scaleVariationIDs.empty())
969  weightChoice->scaleWeightsDoc = scaleDoc.str();
970  std::stringstream pdfDoc;
971  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA names ";
972  bool found = false;
973  for (const auto& pw : pdfSetWeightIDs) {
974  if (pw.wids.size() == 1)
975  continue; // only consider error sets
976  for (const auto& wantedpdf : lhaNameToID_) {
977  auto pdfname = wantedpdf.first;
978  if (knownPDFSetsFromGenInfo_.find(pdfname) == knownPDFSetsFromGenInfo_.end())
979  continue;
980  uint32_t lhaid = knownPDFSetsFromGenInfo_.at(pdfname);
981  if (pw.lhaIDs.first != lhaid)
982  continue;
983  pdfDoc << pdfname;
984  for (const auto& x : pw.wids)
985  weightChoice->pdfWeightIDs.push_back(std::atoi(x.c_str()));
986  if (maxPdfWeights_ < pw.wids.size()) {
987  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
988  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
989  }
990  weightChoice->pdfWeightsDoc = pdfDoc.str();
991  found = true;
992  break;
993  }
994  if (found)
995  break;
996  }
997  }
998  }
const std::vector< std::string > & weightNames() const
std::unordered_map< std::string, uint32_t > lhaNameToID_
char const * label
bool isValid() const
Definition: HandleBase.h:74
const std::string & configDescription() const
const edm::EDGetTokenT< GenLumiInfoHeader > genLumiInfoHeadTag_
void GenWeightsTableProducer::streamBeginRun ( edm::StreamID  id,
edm::Run const &  ,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 892 of file GenWeightsTableProducer.cc.

892  {
893  streamCache(id)->clear();
894  }
void GenWeightsTableProducer::streamEndRunSummary ( edm::StreamID  id,
edm::Run const &  ,
edm::EventSetup const &  ,
CounterMap *  runCounterMap 
) const
inlineoverride

Definition at line 1004 of file GenWeightsTableProducer.cc.

1007  {
1008  runCounterMap->merge(streamCache(id)->countermap);
1009  }

Member Data Documentation

std::atomic<bool> GenWeightsTableProducer::debug_
mutableprotected

Definition at line 1104 of file GenWeightsTableProducer.cc.

std::atomic<bool> GenWeightsTableProducer::debugRun_
mutableprotected

Definition at line 1104 of file GenWeightsTableProducer.cc.

const edm::EDGetTokenT<GenLumiInfoHeader> GenWeightsTableProducer::genLumiInfoHeadTag_
protected

Definition at line 1094 of file GenWeightsTableProducer.cc.

const edm::EDGetTokenT<GenEventInfoProduct> GenWeightsTableProducer::genTag_
protected

Definition at line 1090 of file GenWeightsTableProducer.cc.

std::atomic<bool> GenWeightsTableProducer::hasIssuedWarning_
mutableprotected

Definition at line 1104 of file GenWeightsTableProducer.cc.

bool GenWeightsTableProducer::keepAllPSWeights_
protected

Definition at line 1102 of file GenWeightsTableProducer.cc.

std::unordered_map<std::string, uint32_t> GenWeightsTableProducer::lhaNameToID_
protected

Definition at line 1097 of file GenWeightsTableProducer.cc.

const std::vector<edm::InputTag> GenWeightsTableProducer::lheLabel_
protected

Definition at line 1091 of file GenWeightsTableProducer.cc.

const std::vector<edm::EDGetTokenT<LHERunInfoProduct> > GenWeightsTableProducer::lheRunTag_
protected

Definition at line 1093 of file GenWeightsTableProducer.cc.

const std::vector<edm::EDGetTokenT<LHEEventProduct> > GenWeightsTableProducer::lheTag_
protected

Definition at line 1092 of file GenWeightsTableProducer.cc.

int GenWeightsTableProducer::lheWeightPrecision_
protected

Definition at line 1100 of file GenWeightsTableProducer.cc.

unsigned int GenWeightsTableProducer::maxPdfWeights_
protected

Definition at line 1101 of file GenWeightsTableProducer.cc.

std::vector<std::string> GenWeightsTableProducer::namedWeightIDs_
protected

Definition at line 1098 of file GenWeightsTableProducer.cc.

std::vector<std::string> GenWeightsTableProducer::namedWeightLabels_
protected

Definition at line 1099 of file GenWeightsTableProducer.cc.

std::vector<uint32_t> GenWeightsTableProducer::preferredPDFLHAIDs_
protected

Definition at line 1096 of file GenWeightsTableProducer.cc.