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, const DynamicWeightChoiceGenInfo *genWeightChoice, 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, const DynamicWeightChoiceGenInfo *genWeightChoice, 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 setPSWeightInfo (const std::vector< double > &genWeights, const DynamicWeightChoiceGenInfo *genWeightChoice, std::vector< double > &wPS, std::string &psWeightDocStr) const
 
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_
 
std::atomic< bool > psWeightWarning_
 

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 247 of file GenWeightsTableProducer.cc.

Constructor & Destructor Documentation

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

Definition at line 252 of file GenWeightsTableProducer.cc.

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

253  : genTag_(consumes<GenEventInfoProduct>(params.getParameter<edm::InputTag>("genEvent"))),
254  lheLabel_(params.getParameter<std::vector<edm::InputTag>>("lheInfo")),
256  [this](const edm::InputTag& tag) { return mayConsume<LHEEventProduct>(tag); })),
258  lheLabel_, [this](const edm::InputTag& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
260  mayConsume<GenLumiInfoHeader, edm::InLumi>(params.getParameter<edm::InputTag>("genLumiInfoHeader"))),
261  namedWeightIDs_(params.getParameter<std::vector<std::string>>("namedWeightIDs")),
262  namedWeightLabels_(params.getParameter<std::vector<std::string>>("namedWeightLabels")),
263  lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")),
264  maxPdfWeights_(params.getParameter<uint32_t>("maxPdfWeights")),
265  keepAllPSWeights_(params.getParameter<bool>("keepAllPSWeights")),
266  debug_(params.getUntrackedParameter<bool>("debug", false)),
267  debugRun_(debug_.load()),
268  hasIssuedWarning_(false),
269  psWeightWarning_(false) {
270  produces<nanoaod::FlatTable>();
271  produces<std::string>("genModel");
272  produces<nanoaod::FlatTable>("LHEScale");
273  produces<nanoaod::FlatTable>("LHEPdf");
274  produces<nanoaod::FlatTable>("LHEReweighting");
275  produces<nanoaod::FlatTable>("LHENamed");
276  produces<nanoaod::FlatTable>("PS");
277  produces<nanoaod::MergeableCounterTable, edm::Transition::EndRun>();
278  if (namedWeightIDs_.size() != namedWeightLabels_.size()) {
279  throw cms::Exception("Configuration", "Size mismatch between namedWeightIDs & namedWeightLabels");
280  }
281  for (const edm::ParameterSet& pdfps : params.getParameter<std::vector<edm::ParameterSet>>("preferredPDFs")) {
282  const std::string& name = pdfps.getParameter<std::string>("name");
283  uint32_t lhaid = pdfps.getParameter<uint32_t>("lhaid");
284  preferredPDFLHAIDs_.push_back(lhaid);
286  lhaNameToID_[name + ".LHgrid"] = lhaid;
287  }
288  }
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_
std::atomic< bool > psWeightWarning_
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 290 of file GenWeightsTableProducer.cc.

290 {}

Member Function Documentation

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

Definition at line 948 of file GenWeightsTableProducer.cc.

948  {
949  return std::make_unique<LumiCacheInfoHolder>();
950  }
static void GenWeightsTableProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
inlinestatic

Definition at line 1146 of file GenWeightsTableProducer.cc.

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

1146  {
1148  desc.add<edm::InputTag>("genEvent", edm::InputTag("generator"))
1149  ->setComment("tag for the GenEventInfoProduct, to get the main weight");
1150  desc.add<edm::InputTag>("genLumiInfoHeader", edm::InputTag("generator"))
1151  ->setComment("tag for the GenLumiInfoProduct, to get the model string");
1152  desc.add<std::vector<edm::InputTag>>("lheInfo", std::vector<edm::InputTag>{{"externalLHEProducer"}, {"source"}})
1153  ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
1154 
1156  prefpdf.add<std::string>("name");
1157  prefpdf.add<uint32_t>("lhaid");
1158  desc.addVPSet("preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())
1159  ->setComment(
1160  "LHA PDF Ids of the preferred PDF sets, in order of preference (the first matching one will be used)");
1161  desc.add<std::vector<std::string>>("namedWeightIDs")->setComment("set of LHA weight IDs for named LHE weights");
1162  desc.add<std::vector<std::string>>("namedWeightLabels")
1163  ->setComment("output names for the namedWeightIDs (in the same order)");
1164  desc.add<int32_t>("lheWeightPrecision")->setComment("Number of bits in the mantissa for LHE weights");
1165  desc.add<uint32_t>("maxPdfWeights")->setComment("Maximum number of PDF weights to save (to crop NN replicas)");
1166  desc.add<bool>("keepAllPSWeights")->setComment("Store all PS weights found");
1167  desc.addOptionalUntracked<bool>("debug")->setComment("dump out all LHE information for one event");
1168  descriptions.add("genWeightsTable", desc);
1169  }
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, 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 
458  auto weights = genProd.weights();
459  double w0 = (weights.size() > 1) ? weights.at(1) : 1.;
460  double originalXWGTUP = (weights.size() > 1) ? weights.at(1) : 1.;
461 
462  std::vector<double> wScale, wPDF, wPS;
463  for (auto id : scaleWeightIDs)
464  wScale.push_back(weights.at(id) / w0);
465  for (auto id : pdfWeightIDs) {
466  wPDF.push_back(weights.at(id) / w0);
467  }
468 
469  std::string psWeightsDocStr;
470  setPSWeightInfo(genProd.weights(), weightChoice, wPS, psWeightsDocStr);
471 
472  outScale = std::make_unique<nanoaod::FlatTable>(wScale.size(), "LHEScaleWeight", false);
473  outScale->addColumn<float>(
474  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
475 
476  outPdf = std::make_unique<nanoaod::FlatTable>(wPDF.size(), "LHEPdfWeight", false);
477  outPdf->addColumn<float>(
478  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
479 
480  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
481  outPS->addColumn<float>("", wPS, psWeightsDocStr, nanoaod::FlatTable::FloatColumn,
483 
484  outNamed = std::make_unique<nanoaod::FlatTable>(1, "LHEWeight", true);
485  outNamed->addColumnValue<float>(
486  "originalXWGTUP", originalXWGTUP, "Nominal event weight in the LHE file", nanoaod::FlatTable::FloatColumn);
487  /*for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
488  outNamed->addColumnValue<float>(namedWeightLabels_[i], wNamed[i], "LHE weight for id "+namedWeightIDs_[i]+", relative to nominal", nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
489  }*/
490 
491  counter->incLHE(genWeight, wScale, wPDF, std::vector<double>(), std::vector<double>(), wPS);
492  }
void setPSWeightInfo(const std::vector< double > &genWeights, const DynamicWeightChoiceGenInfo *genWeightChoice, std::vector< double > &wPS, std::string &psWeightDocStr) const
std::vector< double > & weights()
void GenWeightsTableProducer::fillLHEWeightTables ( Counter *  counter,
const DynamicWeightChoice *  weightChoice,
const DynamicWeightChoiceGenInfo *  genWeightChoice,
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 369 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().

379  {
380  bool lheDebug = debug_.exchange(
381  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
382 
383  const std::vector<std::string>& scaleWeightIDs = weightChoice->scaleWeightIDs;
384  const std::vector<std::string>& pdfWeightIDs = weightChoice->pdfWeightIDs;
385  const std::vector<std::string>& rwgtWeightIDs = weightChoice->rwgtIDs;
386 
387  double w0 = lheProd.originalXWGTUP();
388 
389  std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1),
390  wNamed(namedWeightIDs_.size(), 1);
391  for (auto& weight : lheProd.weights()) {
392  if (lheDebug)
393  printf("Weight %+9.5f rel %+9.5f for id %s\n", weight.wgt, weight.wgt / w0, weight.id.c_str());
394  // now we do it slowly, can be optimized
395  auto mScale = std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(), weight.id);
396  if (mScale != scaleWeightIDs.end())
397  wScale[mScale - scaleWeightIDs.begin()] = weight.wgt / w0;
398 
399  auto mPDF = std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(), weight.id);
400  if (mPDF != pdfWeightIDs.end())
401  wPDF[mPDF - pdfWeightIDs.begin()] = weight.wgt / w0;
402 
403  auto mRwgt = std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(), weight.id);
404  if (mRwgt != rwgtWeightIDs.end())
405  wRwgt[mRwgt - rwgtWeightIDs.begin()] = weight.wgt / w0;
406 
407  auto mNamed = std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(), weight.id);
408  if (mNamed != namedWeightIDs_.end())
409  wNamed[mNamed - namedWeightIDs_.begin()] = weight.wgt / w0;
410  }
411 
412  std::vector<double> wPS;
413  std::string psWeightDocStr;
414  setPSWeightInfo(genProd.weights(), genWeightChoice, wPS, psWeightDocStr);
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
void setPSWeightInfo(const std::vector< double > &genWeights, const DynamicWeightChoiceGenInfo *genWeightChoice, std::vector< double > &wPS, std::string &psWeightDocStr) 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,
const DynamicWeightChoiceGenInfo *  genWeightChoice,
double  genWeight,
const GenEventInfoProduct genProd,
std::unique_ptr< nanoaod::FlatTable > &  outPS 
) const
inline

Definition at line 494 of file GenWeightsTableProducer.cc.

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

498  {
499  std::vector<double> wPS;
500  std::string psWeightDocStr;
501  setPSWeightInfo(genProd.weights(), genWeightChoice, wPS, psWeightDocStr);
502  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
503  outPS->addColumn<float>("", wPS, psWeightDocStr, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
504 
505  counter->incGenOnly(genWeight);
506  counter->incPSOnly(genWeight, wPS);
507  }
void setPSWeightInfo(const std::vector< double > &genWeights, const DynamicWeightChoiceGenInfo *genWeightChoice, std::vector< double > &wPS, std::string &psWeightDocStr) const
std::vector< double > & weights()
std::shared_ptr<DynamicWeightChoice> GenWeightsTableProducer::globalBeginRun ( edm::Run const &  iRun,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 555 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(), genWeightsTable_cfi::lhaid, genWeightsTable_cfi::lheInfo, groupFilesInBlocks::lines, and AlCaHLTBitMon_QueryRunRegistry::string.

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

1082  {
1083  return std::make_shared<CounterMap>();
1084  }
void GenWeightsTableProducer::globalEndRun ( edm::Run const &  ,
edm::EventSetup const &   
) const
inlineoverride

Definition at line 1144 of file GenWeightsTableProducer.cc.

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

Definition at line 1095 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.

1095  {
1096  auto out = std::make_unique<nanoaod::MergeableCounterTable>();
1097 
1098  for (const auto& x : runCounterMap->countermap) {
1099  auto runCounter = &(x.second);
1100  std::string label = (!x.first.empty()) ? (std::string("_") + x.first) : "";
1101  std::string doclabel = (!x.first.empty()) ? (std::string(", for model label ") + x.first) : "";
1102 
1103  out->addInt("genEventCount" + label, "event count" + doclabel, runCounter->num);
1104  out->addFloat("genEventSumw" + label, "sum of gen weights" + doclabel, runCounter->sumw);
1105  out->addFloat("genEventSumw2" + label, "sum of gen (weight^2)" + doclabel, runCounter->sumw2);
1106 
1107  double norm = runCounter->sumw ? 1.0 / runCounter->sumw : 1;
1108  auto sumScales = runCounter->sumScale;
1109  for (auto& val : sumScales)
1110  val *= norm;
1111  out->addVFloatWithNorm("LHEScaleSumw" + label,
1112  "Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw" + doclabel,
1113  sumScales,
1114  runCounter->sumw);
1115  auto sumPDFs = runCounter->sumPDF;
1116  for (auto& val : sumPDFs)
1117  val *= norm;
1118  out->addVFloatWithNorm("LHEPdfSumw" + label,
1119  "Sum of genEventWeight * LHEPdfWeight[i], divided by genEventSumw" + doclabel,
1120  sumPDFs,
1121  runCounter->sumw);
1122  if (!runCounter->sumRwgt.empty()) {
1123  auto sumRwgts = runCounter->sumRwgt;
1124  for (auto& val : sumRwgts)
1125  val *= norm;
1126  out->addVFloatWithNorm("LHEReweightingSumw" + label,
1127  "Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw" + doclabel,
1128  sumRwgts,
1129  runCounter->sumw);
1130  }
1131  if (!runCounter->sumNamed.empty()) { // it could be empty if there's no LHE info in the sample
1132  for (unsigned int i = 0, n = namedWeightLabels_.size(); i < n; ++i) {
1133  out->addFloatWithNorm(
1134  "LHESumw_" + namedWeightLabels_[i] + label,
1135  "Sum of genEventWeight * LHEWeight_" + namedWeightLabels_[i] + ", divided by genEventSumw" + doclabel,
1136  runCounter->sumNamed[i] * norm,
1137  runCounter->sumw);
1138  }
1139  }
1140  }
1141  iRun.put(std::move(out));
1142  }
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 1093 of file GenWeightsTableProducer.cc.

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

Definition at line 292 of file GenWeightsTableProducer.cc.

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

292  {
293  // get my counter for weights
294  Counter* counter = streamCache(id)->countermap.get();
295 
296  // generator information (always available)
298  iEvent.getByToken(genTag_, genInfo);
299  double weight = genInfo->weight();
300 
301  // table for gen info, always available
302  auto out = std::make_unique<nanoaod::FlatTable>(1, "genWeight", true);
303  out->setDoc("generator weight");
304  out->addColumnValue<float>("", weight, "generator weight", nanoaod::FlatTable::FloatColumn);
305  iEvent.put(std::move(out));
306 
307  std::string model_label = streamCache(id)->countermap.getLabel();
308  auto outM = std::make_unique<std::string>((!model_label.empty()) ? std::string("GenModel_") + model_label : "");
309  iEvent.put(std::move(outM), "genModel");
310  bool getLHEweightsFromGenInfo = !model_label.empty();
311 
312  // tables for LHE weights, may not be filled
313  std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
314  std::unique_ptr<nanoaod::FlatTable> genPSTab;
315 
317  for (const auto& lheTag : lheTag_) {
318  iEvent.getByToken(lheTag, lheInfo);
319  if (lheInfo.isValid()) {
320  break;
321  }
322  }
323 
324  const auto genWeightChoice = &(streamCache(id)->weightChoice);
325  if (lheInfo.isValid()) {
326  if (getLHEweightsFromGenInfo && !hasIssuedWarning_.exchange(true))
327  edm::LogWarning("LHETablesProducer")
328  << "Found both a LHEEventProduct and a GenLumiInfoHeader: will only save weights from LHEEventProduct.\n";
329  // get the dynamic choice of weights
330  const DynamicWeightChoice* weightChoice = runCache(iEvent.getRun().index());
331  // go fill tables
332  fillLHEWeightTables(counter,
333  weightChoice,
334  genWeightChoice,
335  weight,
336  *lheInfo,
337  *genInfo,
338  lheScaleTab,
339  lhePdfTab,
340  lheRwgtTab,
341  lheNamedTab,
342  genPSTab);
343  } else if (getLHEweightsFromGenInfo) {
345  counter, genWeightChoice, weight, *genInfo, lheScaleTab, lhePdfTab, lheNamedTab, genPSTab);
346  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
347  //lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
348  //genPSTab = std::make_unique<nanoaod::FlatTable>(1, "PSWeight", true);
349  } else {
350  // Still try to add the PS weights
351  fillOnlyPSWeightTable(counter, genWeightChoice, weight, *genInfo, genPSTab);
352  // make dummy values
353  lheScaleTab = std::make_unique<nanoaod::FlatTable>(1, "LHEScaleWeights", true);
354  lhePdfTab = std::make_unique<nanoaod::FlatTable>(1, "LHEPdfWeights", true);
355  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
356  lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
357  if (!hasIssuedWarning_.exchange(true)) {
358  edm::LogWarning("LHETablesProducer") << "No LHEEventProduct, so there will be no LHE Tables\n";
359  }
360  }
361 
362  iEvent.put(std::move(lheScaleTab), "LHEScale");
363  iEvent.put(std::move(lhePdfTab), "LHEPdf");
364  iEvent.put(std::move(lheRwgtTab), "LHEReweighting");
365  iEvent.put(std::move(lheNamedTab), "LHENamed");
366  iEvent.put(std::move(genPSTab), "PS");
367  }
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_
std::function< unsigned int(align::ID)> Counter
void fillLHEWeightTables(Counter *counter, const DynamicWeightChoice *weightChoice, const DynamicWeightChoiceGenInfo *genWeightChoice, 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
RunIndex index() const
Definition: Run.cc:21
bool isValid() const
Definition: HandleBase.h:74
void fillOnlyPSWeightTable(Counter *counter, const DynamicWeightChoiceGenInfo *genWeightChoice, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
def move(src, dest)
Definition: eostools.py:511
void GenWeightsTableProducer::setPSWeightInfo ( const std::vector< double > &  genWeights,
const DynamicWeightChoiceGenInfo *  genWeightChoice,
std::vector< double > &  wPS,
std::string &  psWeightDocStr 
) const
inline

Definition at line 509 of file GenWeightsTableProducer.cc.

References mps_fire::i.

512  {
513  wPS.clear();
514  // isRegularPSSet = keeping all weights and the weights are a usual size, ie
515  // all weights are PS weights (don't use header incase missing names)
516  bool isRegularPSSet = keepAllPSWeights_ && (genWeights.size() == 14 || genWeights.size() == 46);
517  if (!genWeightChoice->psWeightIDs.empty() && !isRegularPSSet) {
518  psWeightDocStr = genWeightChoice->psWeightsDoc;
519  double psNom = genWeights.at(genWeightChoice->psBaselineID);
520  for (auto wgtidx : genWeightChoice->psWeightIDs) {
521  wPS.push_back(genWeights.at(wgtidx) / psNom);
522  }
523  } else {
524  int vectorSize =
525  keepAllPSWeights_ ? (genWeights.size() - 2) : ((genWeights.size() == 14 || genWeights.size() == 46) ? 4 : 1);
526 
527  if (vectorSize > 1) {
528  double nominal = genWeights.at(1); // Called 'Baseline' in GenLumiInfoHeader
529  if (keepAllPSWeights_) {
530  for (int i = 0; i < vectorSize; i++) {
531  wPS.push_back(genWeights.at(i + 2) / nominal);
532  }
533  psWeightDocStr = "All PS weights (w_var / w_nominal)";
534  } else {
535  if (!psWeightWarning_.exchange(true))
536  edm::LogWarning("LHETablesProducer")
537  << "GenLumiInfoHeader not found: Central PartonShower weights will fill with the 6-10th entries \n"
538  << " This may incorrect for some mcs (madgraph 2.6.1 with its `isr:murfact=0.5` have a differnt "
539  "order )";
540  for (std::size_t i = 6; i < 10; i++) {
541  wPS.push_back(genWeights.at(i) / nominal);
542  }
543  psWeightDocStr =
544  "PS weights (w_var / w_nominal); [0] is ISR=2 FSR=1; [1] is ISR=1 FSR=2"
545  "[2] is ISR=0.5 FSR=1; [3] is ISR=1 FSR=0.5;";
546  }
547  } else {
548  wPS.push_back(1.0);
549  psWeightDocStr = "dummy PS weight (1.0) ";
550  }
551  }
552  }
std::atomic< bool > psWeightWarning_
void GenWeightsTableProducer::streamBeginLuminosityBlock ( edm::StreamID  id,
edm::LuminosityBlock const &  lumiBlock,
edm::EventSetup const &  eventSetup 
) const
inlineoverride

Definition at line 955 of file GenWeightsTableProducer.cc.

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

957  {
958  auto counterMap = &(streamCache(id)->countermap);
959  edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
960  lumiBlock.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
961  if (!genLumiInfoHead.isValid())
962  edm::LogWarning("LHETablesProducer")
963  << "No GenLumiInfoHeader product found, will not fill generator model string.\n";
964 
966  if (genLumiInfoHead.isValid()) {
967  label = genLumiInfoHead->configDescription();
968  boost::replace_all(label, "-", "_");
969  boost::replace_all(label, "/", "_");
970  }
971  counterMap->setLabel(label);
972 
973  if (genLumiInfoHead.isValid()) {
974  auto weightChoice = &(streamCache(id)->weightChoice);
975 
976  std::vector<ScaleVarWeight> scaleVariationIDs;
977  std::vector<PDFSetWeights> pdfSetWeightIDs;
978  weightChoice->psWeightIDs.clear();
979 
980  std::regex scalew("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+)\\,\\s+mur=(\\S+)\\smuf=(\\S+)");
981  std::regex pdfw("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+),\\s+Member\\s+(\\d+)\\s+of\\ssets\\s+(\\w+\\b)");
982  std::regex mainPSw("sr(Def|:murfac=)(Hi|Lo|_dn|_up|0.5|2.0)");
983  std::smatch groups;
984  auto weightNames = genLumiInfoHead->weightNames();
985  std::unordered_map<std::string, uint32_t> knownPDFSetsFromGenInfo_;
986  unsigned int weightIter = 0;
987  for (const auto& line : weightNames) {
988  if (std::regex_search(line, groups, scalew)) { // scale variation
989  auto id = groups.str(1);
990  auto group = groups.str(2);
991  auto mur = groups.str(3);
992  auto muf = groups.str(4);
993  if (group.find("Central scale variation") != std::string::npos)
994  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
995  } else if (std::regex_search(line, groups, pdfw)) { // PDF variation
996  auto id = groups.str(1);
997  auto group = groups.str(2);
998  auto memberid = groups.str(3);
999  auto pdfset = groups.str(4);
1000  if (group.find(pdfset) != std::string::npos) {
1001  if (knownPDFSetsFromGenInfo_.find(pdfset) == knownPDFSetsFromGenInfo_.end()) {
1002  knownPDFSetsFromGenInfo_[pdfset] = std::atoi(id.c_str());
1003  pdfSetWeightIDs.emplace_back(id, std::atoi(id.c_str()));
1004  } else
1005  pdfSetWeightIDs.back().add(id, std::atoi(id.c_str()));
1006  }
1007  } else if (line == "Baseline") {
1008  weightChoice->psBaselineID = weightIter;
1009  } else if (line.find("isr") != std::string::npos || line.find("fsr") != std::string::npos) {
1010  weightChoice->matchPS_alt = line.find("sr:") != std::string::npos; // (f/i)sr: for new weights
1011  if (keepAllPSWeights_) {
1012  weightChoice->psWeightIDs.push_back(weightIter); // PS variations
1013  } else if (std::regex_search(line, groups, mainPSw)) {
1014  if (weightChoice->psWeightIDs.empty())
1015  weightChoice->psWeightIDs = std::vector<unsigned int>(4, -1);
1016  int psIdx = (line.find("fsr") != std::string::npos) ? 1 : 0;
1017  psIdx += (groups.str(2) == "Hi" || groups.str(2) == "_up" || groups.str(2) == "2.0") ? 0 : 2;
1018  weightChoice->psWeightIDs[psIdx] = weightIter;
1019  }
1020  }
1021  weightIter++;
1022  }
1023  if (keepAllPSWeights_) {
1024  weightChoice->psWeightsDoc = "All PS weights (w_var / w_nominal)";
1025  } else if (weightChoice->psWeightIDs.size() == 4) {
1026  weightChoice->psWeightsDoc =
1027  "PS weights (w_var / w_nominal); [0] is ISR=2 FSR=1; [1] is ISR=1 FSR=2"
1028  "[2] is ISR=0.5 FSR=1; [3] is ISR=1 FSR=0.5;";
1029  for (int i = 0; i < 4; i++) {
1030  if (static_cast<int>(weightChoice->psWeightIDs[i]) == -1)
1031  weightChoice->setMissingWeight(i);
1032  }
1033  } else {
1034  weightChoice->psWeightsDoc = "dummy PS weight (1.0) ";
1035  }
1036 
1037  weightChoice->scaleWeightIDs.clear();
1038  weightChoice->pdfWeightIDs.clear();
1039 
1040  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
1041  std::stringstream scaleDoc;
1042  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
1043  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
1044  const auto& sw = scaleVariationIDs[isw];
1045  if (isw)
1046  scaleDoc << "; ";
1047  scaleDoc << "[" << isw << "] is " << sw.label;
1048  weightChoice->scaleWeightIDs.push_back(std::atoi(sw.wid.c_str()));
1049  }
1050  if (!scaleVariationIDs.empty())
1051  weightChoice->scaleWeightsDoc = scaleDoc.str();
1052  std::stringstream pdfDoc;
1053  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA names ";
1054  bool found = false;
1055  for (const auto& pw : pdfSetWeightIDs) {
1056  if (pw.wids.size() == 1)
1057  continue; // only consider error sets
1058  for (const auto& wantedpdf : lhaNameToID_) {
1059  auto pdfname = wantedpdf.first;
1060  if (knownPDFSetsFromGenInfo_.find(pdfname) == knownPDFSetsFromGenInfo_.end())
1061  continue;
1062  uint32_t lhaid = knownPDFSetsFromGenInfo_.at(pdfname);
1063  if (pw.lhaIDs.first != lhaid)
1064  continue;
1065  pdfDoc << pdfname;
1066  for (const auto& x : pw.wids)
1067  weightChoice->pdfWeightIDs.push_back(std::atoi(x.c_str()));
1068  if (maxPdfWeights_ < pw.wids.size()) {
1069  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
1070  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
1071  }
1072  weightChoice->pdfWeightsDoc = pdfDoc.str();
1073  found = true;
1074  break;
1075  }
1076  if (found)
1077  break;
1078  }
1079  }
1080  }
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 952 of file GenWeightsTableProducer.cc.

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

Definition at line 1086 of file GenWeightsTableProducer.cc.

1089  {
1090  runCounterMap->merge(streamCache(id)->countermap);
1091  }

Member Data Documentation

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

Definition at line 1186 of file GenWeightsTableProducer.cc.

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

Definition at line 1186 of file GenWeightsTableProducer.cc.

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

Definition at line 1176 of file GenWeightsTableProducer.cc.

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

Definition at line 1172 of file GenWeightsTableProducer.cc.

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

Definition at line 1186 of file GenWeightsTableProducer.cc.

bool GenWeightsTableProducer::keepAllPSWeights_
protected

Definition at line 1184 of file GenWeightsTableProducer.cc.

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

Definition at line 1179 of file GenWeightsTableProducer.cc.

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

Definition at line 1173 of file GenWeightsTableProducer.cc.

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

Definition at line 1175 of file GenWeightsTableProducer.cc.

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

Definition at line 1174 of file GenWeightsTableProducer.cc.

int GenWeightsTableProducer::lheWeightPrecision_
protected

Definition at line 1182 of file GenWeightsTableProducer.cc.

unsigned int GenWeightsTableProducer::maxPdfWeights_
protected

Definition at line 1183 of file GenWeightsTableProducer.cc.

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

Definition at line 1180 of file GenWeightsTableProducer.cc.

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

Definition at line 1181 of file GenWeightsTableProducer.cc.

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

Definition at line 1178 of file GenWeightsTableProducer.cc.

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

Definition at line 1186 of file GenWeightsTableProducer.cc.