From b217ff045240cfd84fe7183f7c7f6abdbde456a3 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Fri, 27 Mar 2020 14:34:05 -0500 Subject: [PATCH 1/2] Removed the use of module header files Only the .cc files need the module declaration. --- .../interface/GenFilterEfficiencyAnalyzer.h | 45 ------- .../Core/interface/GenXSecAnalyzer.h | 117 ------------------ .../Core/interface/GeneratorSmearedProducer.h | 27 ---- .../plugins/GenFilterEfficiencyAnalyzer.cc | 45 ++++++- .../Core/plugins/GenXSecAnalyzer.cc | 115 ++++++++++++++++- .../Core/plugins/GeneratorSmearedProducer.cc | 27 +++- GeneratorInterface/Core/plugins/module.cc | 10 -- 7 files changed, 184 insertions(+), 202 deletions(-) delete mode 100644 GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h delete mode 100644 GeneratorInterface/Core/interface/GenXSecAnalyzer.h delete mode 100644 GeneratorInterface/Core/interface/GeneratorSmearedProducer.h delete mode 100644 GeneratorInterface/Core/plugins/module.cc diff --git a/GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h b/GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h deleted file mode 100644 index d2286b5a61efe..0000000000000 --- a/GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef GENFILTEREFFICIENCYANALYZER_H -#define GENFILTEREFFICIENCYANALYZER_H - -// F. Cossutti -// $Revision:// - -// analyzer of a summary information product on filter efficiency for a user specified path -// meant for the generator filter efficiency calculation - -// system include files -#include - -// user include files -#include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/EDAnalyzer.h" - -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/Run.h" -#include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/Framework/interface/LuminosityBlock.h" -#include "FWCore/Utilities/interface/EDGetToken.h" - -#include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" -// -// class declaration -// - -class GenFilterEfficiencyAnalyzer : public edm::EDAnalyzer { -public: - explicit GenFilterEfficiencyAnalyzer(const edm::ParameterSet&); - ~GenFilterEfficiencyAnalyzer() override; - -private: - void analyze(const edm::Event&, const edm::EventSetup&) override; - void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override; - void endJob() override; - - edm::EDGetTokenT genFilterInfoToken_; - GenFilterInfo totalGenFilterInfo_; - - // ----------member data --------------------------- -}; - -#endif diff --git a/GeneratorInterface/Core/interface/GenXSecAnalyzer.h b/GeneratorInterface/Core/interface/GenXSecAnalyzer.h deleted file mode 100644 index 23152dd5ffc39..0000000000000 --- a/GeneratorInterface/Core/interface/GenXSecAnalyzer.h +++ /dev/null @@ -1,117 +0,0 @@ - -#ifndef GENXSECANALYZER_H -#define GENXSECANALYZER_H - -// $Revision:// - -// analyzer of a summary information product on filter efficiency for a user specified path -// meant for the generator filter efficiency calculation - -// system include files -#include -#include -#include - -// user include files -#include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/one/EDAnalyzer.h" - -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/Run.h" -#include "FWCore/Framework/interface/EventSetup.h" -#include "FWCore/Framework/interface/LuminosityBlock.h" -#include "FWCore/Utilities/interface/InputTag.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h" -#include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" -#include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h" - -// -// class declaration -// - -class GenXSecAnalyzer : public edm::one::EDAnalyzer { -public: - explicit GenXSecAnalyzer(const edm::ParameterSet &); - ~GenXSecAnalyzer() override; - const double final_xsec_value() const { return xsec_.value(); } - const double final_xsec_error() const { return xsec_.error(); } - -private: - void beginJob() override; - void beginRun(edm::Run const &, edm::EventSetup const &) override; - void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; - void analyze(const edm::Event &, const edm::EventSetup &) override; - void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; - void endRun(edm::Run const &, edm::EventSetup const &) override; - void endJob() override; - // computation of cross section after matching and before HepcFilter and GenFilter - GenLumiInfoProduct::XSec compute(const GenLumiInfoProduct &); - // combination of cross section from different MCs after matching (could be either before or after HepcFilter and GenFilter) - void combine(GenLumiInfoProduct::XSec &, double &, const GenLumiInfoProduct::XSec &, const double &); - void combine(double &, double &, double &, const double &, const double &, const double &); - - edm::EDGetTokenT genFilterInfoToken_; - edm::EDGetTokenT hepMCFilterInfoToken_; - edm::EDGetTokenT genLumiInfoToken_; - edm::EDGetTokenT lheRunInfoToken_; - - // ----------member data -------------------------- - - int nMCs_; - - int hepidwtup_; - - // for weight before GenFilter and HepMCFilter and before matching - double totalWeightPre_; - double thisRunWeightPre_; - - // for weight after GenFilter and HepMCFilter and after matching - double totalWeight_; - double thisRunWeight_; - - // combined cross sections before HepMCFilter and GenFilter - GenLumiInfoProduct::XSec xsecPreFilter_; - - // final combined cross sections - GenLumiInfoProduct::XSec xsec_; - - // GenLumiInfo before HepMCFilter and GenFilter, this is used - // for computation - GenLumiInfoProduct product_; - - // statistics from additional generator filter, for computation - // reset for each run - GenFilterInfo filterOnlyEffRun_; - - // statistics from HepMC filter, for computation - GenFilterInfo hepMCFilterEffRun_; - - // statistics from additional generator filter, for print-out only - GenFilterInfo filterOnlyEffStat_; - - // statistics from HepMC filter, for print-out only - GenFilterInfo hepMCFilterEffStat_; - - // the vector/map size is the number of LHE processes + 1 - // needed only for printouts, not used for computation - // only printed out when combining the same physics process - // uncertainty-averaged cross sections before matching - std::vector xsecBeforeMatching_; - // uncertainty-averaged cross sections after matching - std::vector xsecAfterMatching_; - // statistics from jet matching - std::map jetMatchEffStat_; - - // the following vectors all have the same size - // LHE or Pythia/Herwig cross section of previous luminosity block - // vector size = number of processes, used for computation - std::map previousLumiBlockLHEXSec_; - - // LHE or Pythia/Herwig combined cross section of current luminosity block - // updated for each luminosity block, initialized in every run - // used for computation - std::map currentLumiBlockLHEXSec_; -}; - -#endif diff --git a/GeneratorInterface/Core/interface/GeneratorSmearedProducer.h b/GeneratorInterface/Core/interface/GeneratorSmearedProducer.h deleted file mode 100644 index 1a20092153e42..0000000000000 --- a/GeneratorInterface/Core/interface/GeneratorSmearedProducer.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef GeneratorInterface_Core_GeneratorSmearedProducer_h -#define GeneratorInterface_Core_GeneratorSmearedProducer_h - -#include "FWCore/Framework/interface/global/EDProducer.h" -#include "FWCore/Utilities/interface/EDGetToken.h" - -namespace edm { - class ParameterSet; - class ConfigurationDescriptions; - class Event; - class EventSetup; - class HepMCProduct; -} // namespace edm - -class GeneratorSmearedProducer : public edm::global::EDProducer<> { -public: - explicit GeneratorSmearedProducer(edm::ParameterSet const& p); - - void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override; - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - -private: - const edm::EDGetTokenT newToken_; - const edm::EDGetTokenT oldToken_; -}; - -#endif diff --git a/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc b/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc index ef17089fe19c6..c52d93160a124 100644 --- a/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc @@ -1,6 +1,47 @@ -#include "GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h" +// F. Cossutti +// $Revision:// + +// analyzer of a summary information product on filter efficiency for a user specified path +// meant for the generator filter efficiency calculation + +// system include files +#include #include +// user include files + +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/EDAnalyzer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/Run.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Framework/interface/LuminosityBlock.h" +#include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" +// +// class declaration +// + +class GenFilterEfficiencyAnalyzer : public edm::EDAnalyzer { +public: + explicit GenFilterEfficiencyAnalyzer(const edm::ParameterSet&); + ~GenFilterEfficiencyAnalyzer() override; + +private: + void analyze(const edm::Event&, const edm::EventSetup&) override; + void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override; + void endJob() override; + + edm::EDGetTokenT genFilterInfoToken_; + GenFilterInfo totalGenFilterInfo_; + + // ----------member data --------------------------- +}; + GenFilterEfficiencyAnalyzer::GenFilterEfficiencyAnalyzer(const edm::ParameterSet& pset) : genFilterInfoToken_(consumes(pset.getParameter("genFilterInfoTag"))), totalGenFilterInfo_(0, 0, 0, 0, 0., 0., 0., 0.) {} @@ -30,3 +71,5 @@ void GenFilterEfficiencyAnalyzer::endJob() { std::cout << "Filter efficiency = " << totalGenFilterInfo_.filterEfficiency(-1) << " +- " << totalGenFilterInfo_.filterEfficiencyError(-1) << std::endl; } + +DEFINE_FWK_MODULE(GenFilterEfficiencyAnalyzer); diff --git a/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc b/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc index c830f4ec786c8..b0f1b817209a8 100644 --- a/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc @@ -1,9 +1,120 @@ -#include "GeneratorInterface/Core/interface/GenXSecAnalyzer.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "TMath.h" #include #include +// analyzer of a summary information product on filter efficiency for a user specified path +// meant for the generator filter efficiency calculation + +// system include files +#include +#include +#include + +// user include files +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/one/EDAnalyzer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/Run.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/Framework/interface/LuminosityBlock.h" +#include "FWCore/Utilities/interface/InputTag.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h" +#include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" +#include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h" + +#include "FWCore/Framework/interface/MakerMacros.h" + +// +// class declaration +// + +class GenXSecAnalyzer : public edm::one::EDAnalyzer { +public: + explicit GenXSecAnalyzer(const edm::ParameterSet &); + ~GenXSecAnalyzer() override; + const double final_xsec_value() const { return xsec_.value(); } + const double final_xsec_error() const { return xsec_.error(); } + +private: + void beginJob() override; + void beginRun(edm::Run const &, edm::EventSetup const &) override; + void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; + void analyze(const edm::Event &, const edm::EventSetup &) override; + void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; + void endRun(edm::Run const &, edm::EventSetup const &) override; + void endJob() override; + // computation of cross section after matching and before HepcFilter and GenFilter + GenLumiInfoProduct::XSec compute(const GenLumiInfoProduct &); + // combination of cross section from different MCs after matching (could be either before or after HepcFilter and GenFilter) + void combine(GenLumiInfoProduct::XSec &, double &, const GenLumiInfoProduct::XSec &, const double &); + void combine(double &, double &, double &, const double &, const double &, const double &); + + edm::EDGetTokenT genFilterInfoToken_; + edm::EDGetTokenT hepMCFilterInfoToken_; + edm::EDGetTokenT genLumiInfoToken_; + edm::EDGetTokenT lheRunInfoToken_; + + // ----------member data -------------------------- + + int nMCs_; + + int hepidwtup_; + + // for weight before GenFilter and HepMCFilter and before matching + double totalWeightPre_; + double thisRunWeightPre_; + + // for weight after GenFilter and HepMCFilter and after matching + double totalWeight_; + double thisRunWeight_; + + // combined cross sections before HepMCFilter and GenFilter + GenLumiInfoProduct::XSec xsecPreFilter_; + + // final combined cross sections + GenLumiInfoProduct::XSec xsec_; + + // GenLumiInfo before HepMCFilter and GenFilter, this is used + // for computation + GenLumiInfoProduct product_; + + // statistics from additional generator filter, for computation + // reset for each run + GenFilterInfo filterOnlyEffRun_; + + // statistics from HepMC filter, for computation + GenFilterInfo hepMCFilterEffRun_; + + // statistics from additional generator filter, for print-out only + GenFilterInfo filterOnlyEffStat_; + + // statistics from HepMC filter, for print-out only + GenFilterInfo hepMCFilterEffStat_; + + // the vector/map size is the number of LHE processes + 1 + // needed only for printouts, not used for computation + // only printed out when combining the same physics process + // uncertainty-averaged cross sections before matching + std::vector xsecBeforeMatching_; + // uncertainty-averaged cross sections after matching + std::vector xsecAfterMatching_; + // statistics from jet matching + std::map jetMatchEffStat_; + + // the following vectors all have the same size + // LHE or Pythia/Herwig cross section of previous luminosity block + // vector size = number of processes, used for computation + std::map previousLumiBlockLHEXSec_; + + // LHE or Pythia/Herwig combined cross section of current luminosity block + // updated for each luminosity block, initialized in every run + // used for computation + std::map currentLumiBlockLHEXSec_; +}; + GenXSecAnalyzer::GenXSecAnalyzer(const edm::ParameterSet& iConfig) : nMCs_(0), hepidwtup_(-9999), @@ -591,3 +702,5 @@ void GenXSecAnalyzer::endJob() { edm::LogPrint("GenXSecAnalyzer") << "After filter: final equivalent lumi for 1M events (1/fb) = " << std::scientific << std::setprecision(3) << lumi_1M_evts << " +- " << lumi_1M_evts_unc; } + +DEFINE_FWK_MODULE(GenXSecAnalyzer); diff --git a/GeneratorInterface/Core/plugins/GeneratorSmearedProducer.cc b/GeneratorInterface/Core/plugins/GeneratorSmearedProducer.cc index b4f05164777bb..4a19f88e44d6e 100644 --- a/GeneratorInterface/Core/plugins/GeneratorSmearedProducer.cc +++ b/GeneratorInterface/Core/plugins/GeneratorSmearedProducer.cc @@ -1,4 +1,3 @@ -#include "GeneratorInterface/Core/interface/GeneratorSmearedProducer.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Utilities/interface/InputTag.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" @@ -6,8 +5,32 @@ #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h" +#include "FWCore/Framework/interface/global/EDProducer.h" +#include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/Framework/interface/MakerMacros.h" + #include +namespace edm { + class ParameterSet; + class ConfigurationDescriptions; + class Event; + class EventSetup; + class HepMCProduct; +} // namespace edm + +class GeneratorSmearedProducer : public edm::global::EDProducer<> { +public: + explicit GeneratorSmearedProducer(edm::ParameterSet const& p); + + void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override; + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + +private: + const edm::EDGetTokenT newToken_; + const edm::EDGetTokenT oldToken_; +}; + GeneratorSmearedProducer::GeneratorSmearedProducer(edm::ParameterSet const& ps) : newToken_(consumes(ps.getUntrackedParameter("currentTag"))), oldToken_(consumes(ps.getUntrackedParameter("previousTag"))) { @@ -34,3 +57,5 @@ void GeneratorSmearedProducer::fillDescriptions(edm::ConfigurationDescriptions& desc.addUntracked("previousTag", edm::InputTag("generator")); descriptions.add("generatorSmeared", desc); } + +DEFINE_FWK_MODULE(GeneratorSmearedProducer); diff --git a/GeneratorInterface/Core/plugins/module.cc b/GeneratorInterface/Core/plugins/module.cc deleted file mode 100644 index 41762e46c0a0f..0000000000000 --- a/GeneratorInterface/Core/plugins/module.cc +++ /dev/null @@ -1,10 +0,0 @@ -#include "FWCore/Framework/interface/MakerMacros.h" - -#include "GeneratorInterface/Core/interface/GenFilterEfficiencyAnalyzer.h" -DEFINE_FWK_MODULE(GenFilterEfficiencyAnalyzer); - -#include "GeneratorInterface/Core/interface/GenXSecAnalyzer.h" -DEFINE_FWK_MODULE(GenXSecAnalyzer); - -#include "GeneratorInterface/Core/interface/GeneratorSmearedProducer.h" -DEFINE_FWK_MODULE(GeneratorSmearedProducer); From 6f9e7ea0f74964b5bcedda5be6b7e4a5ad73ca40 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Fri, 27 Mar 2020 16:14:19 -0500 Subject: [PATCH 2/2] Move EDAnalyzers to be global modules --- .../plugins/GenFilterEfficiencyAnalyzer.cc | 37 ++- .../Core/plugins/GenXSecAnalyzer.cc | 266 +++++++++--------- 2 files changed, 162 insertions(+), 141 deletions(-) diff --git a/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc b/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc index c52d93160a124..ec7dfeb4f8116 100644 --- a/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenFilterEfficiencyAnalyzer.cc @@ -11,7 +11,7 @@ // user include files #include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/EDAnalyzer.h" +#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/Run.h" @@ -19,6 +19,7 @@ #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/Framework/interface/LuminosityBlock.h" #include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/Utilities/interface/thread_safety_macros.h" #include "FWCore/Framework/interface/MakerMacros.h" #include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" @@ -26,18 +27,25 @@ // class declaration // -class GenFilterEfficiencyAnalyzer : public edm::EDAnalyzer { +namespace gfea { + struct Empty {}; +}; // namespace gfea + +class GenFilterEfficiencyAnalyzer : public edm::global::EDAnalyzer> { public: explicit GenFilterEfficiencyAnalyzer(const edm::ParameterSet&); - ~GenFilterEfficiencyAnalyzer() override; + ~GenFilterEfficiencyAnalyzer() final; private: - void analyze(const edm::Event&, const edm::EventSetup&) override; - void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override; - void endJob() override; + void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const final; + std::shared_ptr globalBeginLuminosityBlock(edm::LuminosityBlock const&, + edm::EventSetup const&) const final; + void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const final; + void endJob() final; edm::EDGetTokenT genFilterInfoToken_; - GenFilterInfo totalGenFilterInfo_; + mutable std::mutex mutex_; + CMS_THREAD_GUARD(mutex_) mutable GenFilterInfo totalGenFilterInfo_; // ----------member data --------------------------- }; @@ -48,11 +56,17 @@ GenFilterEfficiencyAnalyzer::GenFilterEfficiencyAnalyzer(const edm::ParameterSet GenFilterEfficiencyAnalyzer::~GenFilterEfficiencyAnalyzer() {} -void GenFilterEfficiencyAnalyzer::analyze(const edm::Event&, const edm::EventSetup&) {} +void GenFilterEfficiencyAnalyzer::analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const {} + +std::shared_ptr GenFilterEfficiencyAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi, + edm::EventSetup const&) const { + return std::shared_ptr(); +} // ------------ method called once each job just after ending the event loop ------------ -void GenFilterEfficiencyAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) { +void GenFilterEfficiencyAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi, + edm::EventSetup const&) const { edm::Handle genFilter; iLumi.getByToken(genFilterInfoToken_, genFilter); @@ -62,7 +76,10 @@ void GenFilterEfficiencyAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& << " N failed = " << genFilter->sumFailWeights() << std::endl; std::cout << "Generator filter efficiency = " << genFilter->filterEfficiency(-1) << " +- " << genFilter->filterEfficiencyError(-1) << std::endl; - totalGenFilterInfo_.mergeProduct(*genFilter); + { + std::lock_guard guard(mutex_); + totalGenFilterInfo_.mergeProduct(*genFilter); + } } void GenFilterEfficiencyAnalyzer::endJob() { diff --git a/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc b/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc index b0f1b817209a8..0cc592e73c5d5 100644 --- a/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenXSecAnalyzer.cc @@ -13,13 +13,14 @@ // user include files #include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/one/EDAnalyzer.h" +#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/Run.h" #include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/Framework/interface/LuminosityBlock.h" #include "FWCore/Utilities/interface/InputTag.h" +#include "FWCore/Utilities/interface/thread_safety_macros.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h" #include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h" @@ -30,27 +31,62 @@ // // class declaration // - -class GenXSecAnalyzer : public edm::one::EDAnalyzer { +namespace gxsec { + struct LumiCache {}; + struct RunCache { + RunCache() + : product_(-9999), + filterOnlyEffRun_(0, 0, 0, 0, 0., 0., 0., 0.), + hepMCFilterEffRun_(0, 0, 0, 0, 0., 0., 0., 0.) {} + // for weight before GenFilter and HepMCFilter and before matching + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable double thisRunWeightPre_ = 0; + + // for weight after GenFilter and HepMCFilter and after matching + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable double thisRunWeight_ = 0; + + // GenLumiInfo before HepMCFilter and GenFilter, this is used + // for computation + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable GenLumiInfoProduct product_; + + // statistics from additional generator filter, for computation + // reset for each run + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable GenFilterInfo filterOnlyEffRun_; + + // statistics from HepMC filter, for computation + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable GenFilterInfo hepMCFilterEffRun_; + + // the following vectors all have the same size + // LHE or Pythia/Herwig cross section of previous luminosity block + // vector size = number of processes, used for computation + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable std::map previousLumiBlockLHEXSec_; + + // LHE or Pythia/Herwig combined cross section of current luminosity block + // updated for each luminosity block, initialized in every run + // used for computation + CMS_THREAD_GUARD(GenXSecAnalyzer::mutex_) mutable std::map currentLumiBlockLHEXSec_; + }; +} // namespace gxsec + +class GenXSecAnalyzer + : public edm::global::EDAnalyzer, edm::LuminosityBlockCache> { public: explicit GenXSecAnalyzer(const edm::ParameterSet &); ~GenXSecAnalyzer() override; - const double final_xsec_value() const { return xsec_.value(); } - const double final_xsec_error() const { return xsec_.error(); } private: - void beginJob() override; - void beginRun(edm::Run const &, edm::EventSetup const &) override; - void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; - void analyze(const edm::Event &, const edm::EventSetup &) override; - void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override; - void endRun(edm::Run const &, edm::EventSetup const &) override; - void endJob() override; + void beginJob() final; + std::shared_ptr globalBeginRun(edm::Run const &, edm::EventSetup const &) const final; + std::shared_ptr globalBeginLuminosityBlock(edm::LuminosityBlock const &, + edm::EventSetup const &) const final; + void analyze(edm::StreamID, const edm::Event &, const edm::EventSetup &) const final; + void globalEndLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const final; + void globalEndRun(edm::Run const &, edm::EventSetup const &) const final; + void endJob() final; // computation of cross section after matching and before HepcFilter and GenFilter - GenLumiInfoProduct::XSec compute(const GenLumiInfoProduct &); + GenLumiInfoProduct::XSec compute(const GenLumiInfoProduct &) const; // combination of cross section from different MCs after matching (could be either before or after HepcFilter and GenFilter) - void combine(GenLumiInfoProduct::XSec &, double &, const GenLumiInfoProduct::XSec &, const double &); - void combine(double &, double &, double &, const double &, const double &, const double &); + void combine(GenLumiInfoProduct::XSec &, double &, const GenLumiInfoProduct::XSec &, const double &) const; + void combine(double &, double &, double &, const double &, const double &, const double &) const; edm::EDGetTokenT genFilterInfoToken_; edm::EDGetTokenT hepMCFilterInfoToken_; @@ -59,82 +95,50 @@ class GenXSecAnalyzer : public edm::one::EDAnalyzer nMCs_; + + mutable std::atomic hepidwtup_; - int hepidwtup_; + mutable std::mutex mutex_; // for weight before GenFilter and HepMCFilter and before matching - double totalWeightPre_; - double thisRunWeightPre_; + CMS_THREAD_GUARD(mutex_) mutable double totalWeightPre_; // for weight after GenFilter and HepMCFilter and after matching - double totalWeight_; - double thisRunWeight_; + CMS_THREAD_GUARD(mutex_) mutable double totalWeight_; // combined cross sections before HepMCFilter and GenFilter - GenLumiInfoProduct::XSec xsecPreFilter_; + CMS_THREAD_GUARD(mutex_) mutable GenLumiInfoProduct::XSec xsecPreFilter_; // final combined cross sections - GenLumiInfoProduct::XSec xsec_; - - // GenLumiInfo before HepMCFilter and GenFilter, this is used - // for computation - GenLumiInfoProduct product_; - - // statistics from additional generator filter, for computation - // reset for each run - GenFilterInfo filterOnlyEffRun_; - - // statistics from HepMC filter, for computation - GenFilterInfo hepMCFilterEffRun_; + CMS_THREAD_GUARD(mutex_) mutable GenLumiInfoProduct::XSec xsec_; // statistics from additional generator filter, for print-out only - GenFilterInfo filterOnlyEffStat_; + CMS_THREAD_GUARD(mutex_) mutable GenFilterInfo filterOnlyEffStat_; // statistics from HepMC filter, for print-out only - GenFilterInfo hepMCFilterEffStat_; + CMS_THREAD_GUARD(mutex_) mutable GenFilterInfo hepMCFilterEffStat_; // the vector/map size is the number of LHE processes + 1 // needed only for printouts, not used for computation // only printed out when combining the same physics process // uncertainty-averaged cross sections before matching - std::vector xsecBeforeMatching_; + CMS_THREAD_GUARD(mutex_) mutable std::vector xsecBeforeMatching_; // uncertainty-averaged cross sections after matching - std::vector xsecAfterMatching_; + CMS_THREAD_GUARD(mutex_) mutable std::vector xsecAfterMatching_; // statistics from jet matching - std::map jetMatchEffStat_; - - // the following vectors all have the same size - // LHE or Pythia/Herwig cross section of previous luminosity block - // vector size = number of processes, used for computation - std::map previousLumiBlockLHEXSec_; - - // LHE or Pythia/Herwig combined cross section of current luminosity block - // updated for each luminosity block, initialized in every run - // used for computation - std::map currentLumiBlockLHEXSec_; + CMS_THREAD_GUARD(mutex_) mutable std::map jetMatchEffStat_; }; -GenXSecAnalyzer::GenXSecAnalyzer(const edm::ParameterSet& iConfig) +GenXSecAnalyzer::GenXSecAnalyzer(const edm::ParameterSet &iConfig) : nMCs_(0), hepidwtup_(-9999), totalWeightPre_(0), - thisRunWeightPre_(0), totalWeight_(0), - thisRunWeight_(0), xsecPreFilter_(-1, -1), xsec_(-1, -1), - product_(GenLumiInfoProduct(-9999)), - filterOnlyEffRun_(0, 0, 0, 0, 0., 0., 0., 0.), - hepMCFilterEffRun_(0, 0, 0, 0, 0., 0., 0., 0.), filterOnlyEffStat_(0, 0, 0, 0, 0., 0., 0., 0.), hepMCFilterEffStat_(0, 0, 0, 0, 0., 0., 0., 0.) { - xsecBeforeMatching_.clear(); - xsecAfterMatching_.clear(); - jetMatchEffStat_.clear(); - previousLumiBlockLHEXSec_.clear(); - currentLumiBlockLHEXSec_.clear(); - genFilterInfoToken_ = consumes(edm::InputTag("genFilterEfficiencyProducer", "")); hepMCFilterInfoToken_ = consumes(edm::InputTag("generator", "")); genLumiInfoToken_ = consumes(edm::InputTag("generator", "")); @@ -143,48 +147,37 @@ GenXSecAnalyzer::GenXSecAnalyzer(const edm::ParameterSet& iConfig) GenXSecAnalyzer::~GenXSecAnalyzer() {} -void GenXSecAnalyzer::beginJob() { - xsecBeforeMatching_.clear(); - xsecAfterMatching_.clear(); - jetMatchEffStat_.clear(); - previousLumiBlockLHEXSec_.clear(); - currentLumiBlockLHEXSec_.clear(); -} +void GenXSecAnalyzer::beginJob() {} -void GenXSecAnalyzer::beginRun(edm::Run const& iRun, edm::EventSetup const&) { +std::shared_ptr GenXSecAnalyzer::globalBeginRun(edm::Run const &iRun, edm::EventSetup const &) const { // initialization for every different physics MC nMCs_++; - thisRunWeightPre_ = 0; - thisRunWeight_ = 0; - - product_ = GenLumiInfoProduct(-9999); - - filterOnlyEffRun_ = GenFilterInfo(0, 0, 0, 0, 0., 0., 0., 0.); - hepMCFilterEffRun_ = GenFilterInfo(0, 0, 0, 0, 0., 0., 0., 0.); - - xsecBeforeMatching_.clear(); - xsecAfterMatching_.clear(); - jetMatchEffStat_.clear(); - previousLumiBlockLHEXSec_.clear(); - currentLumiBlockLHEXSec_.clear(); - - return; + { + std::lock_guard l{mutex_}; + xsecBeforeMatching_.clear(); + xsecAfterMatching_.clear(); + jetMatchEffStat_.clear(); + } + return std::make_shared(); } -void GenXSecAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) {} +std::shared_ptr GenXSecAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const &iLumi, + edm::EventSetup const &) const { + return std::shared_ptr(); +} -void GenXSecAnalyzer::analyze(const edm::Event&, const edm::EventSetup&) {} +void GenXSecAnalyzer::analyze(edm::StreamID, const edm::Event &, const edm::EventSetup &) const {} -void GenXSecAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) { +void GenXSecAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const &iLumi, edm::EventSetup const &) const { edm::Handle genLumiInfo; iLumi.getByToken(genLumiInfoToken_, genLumiInfo); if (!genLumiInfo.isValid()) return; hepidwtup_ = genLumiInfo->getHEPIDWTUP(); - std::vector theProcesses = genLumiInfo->getProcessInfos(); + std::vector const &theProcesses = genLumiInfo->getProcessInfos(); unsigned int theProcesses_size = theProcesses.size(); @@ -205,30 +198,37 @@ void GenXSecAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm: } } - product_.mergeProduct(*genLumiInfo); - + auto runC = runCache(iLumi.getRun().index()); + { + std::lock_guard g{mutex_}; + runC->product_.mergeProduct(*genLumiInfo); + } edm::Handle genFilter; iLumi.getByToken(genFilterInfoToken_, genFilter); + if (genFilter.isValid()) { + std::lock_guard g{mutex_}; filterOnlyEffStat_.mergeProduct(*genFilter); - filterOnlyEffRun_.mergeProduct(*genFilter); - thisRunWeight_ += genFilter->sumPassWeights(); + runC->filterOnlyEffRun_.mergeProduct(*genFilter); + runC->thisRunWeight_ += genFilter->sumPassWeights(); } edm::Handle hepMCFilter; iLumi.getByToken(hepMCFilterInfoToken_, hepMCFilter); if (hepMCFilter.isValid()) { + std::lock_guard g{mutex_}; hepMCFilterEffStat_.mergeProduct(*hepMCFilter); - hepMCFilterEffRun_.mergeProduct(*hepMCFilter); + runC->hepMCFilterEffRun_.mergeProduct(*hepMCFilter); } + std::lock_guard g{mutex_}; // doing generic summing for jet matching statistics // and computation of combined LHE information for (unsigned int ip = 0; ip < theProcesses_size; ip++) { int id = theProcesses[ip].process(); - GenFilterInfo& x = jetMatchEffStat_[id]; - GenLumiInfoProduct::XSec& y = currentLumiBlockLHEXSec_[id]; + GenFilterInfo &x = jetMatchEffStat_[id]; + GenLumiInfoProduct::XSec &y = runC->currentLumiBlockLHEXSec_[id]; GenLumiInfoProduct::FinalStat temp_killed = theProcesses[ip].killed(); GenLumiInfoProduct::FinalStat temp_selected = theProcesses[ip].selected(); double passw = temp_killed.sum(); @@ -250,71 +250,75 @@ void GenXSecAnalyzer::endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm: double currentError = theProcesses[ip].lheXSec().error(); // this process ID has occurred before + auto &thisRunWeightPre = runC->thisRunWeightPre_; if (y.value() > 0) { x.mergeProduct(tempInfo); - double previousValue = previousLumiBlockLHEXSec_[id].value(); + double previousValue = runC->previousLumiBlockLHEXSec_[id].value(); if (currentValue != previousValue) // transition of cross section { double xsec = y.value(); double err = y.error(); - combine(xsec, err, thisRunWeightPre_, currentValue, currentError, totalw); + combine(xsec, err, thisRunWeightPre, currentValue, currentError, totalw); y = GenLumiInfoProduct::XSec(xsec, err); } else // LHE cross section is the same as previous lumiblock - thisRunWeightPre_ += totalw; + thisRunWeightPre += totalw; } // this process ID has never occurred before else { x = tempInfo; y = theProcesses[ip].lheXSec(); - thisRunWeightPre_ += totalw; + thisRunWeightPre += totalw; } - previousLumiBlockLHEXSec_[id] = theProcesses[ip].lheXSec(); + runC->previousLumiBlockLHEXSec_[id] = theProcesses[ip].lheXSec(); } // end return; } -void GenXSecAnalyzer::endRun(edm::Run const& iRun, edm::EventSetup const&) { +void GenXSecAnalyzer::globalEndRun(edm::Run const &iRun, edm::EventSetup const &) const { //xsection before matching edm::Handle run; if (iRun.getByToken(lheRunInfoToken_, run)) { - const lhef::HEPRUP thisHeprup_ = run->heprup(); + const lhef::HEPRUP thisHeprup = run->heprup(); - for (unsigned int iSize = 0; iSize < thisHeprup_.XSECUP.size(); iSize++) { - std::cout << std::setw(14) << std::fixed << thisHeprup_.XSECUP[iSize] << std::setw(14) << std::fixed - << thisHeprup_.XERRUP[iSize] << std::setw(14) << std::fixed << thisHeprup_.XMAXUP[iSize] - << std::setw(14) << std::fixed << thisHeprup_.LPRUP[iSize] << std::endl; + for (unsigned int iSize = 0; iSize < thisHeprup.XSECUP.size(); iSize++) { + std::cout << std::setw(14) << std::fixed << thisHeprup.XSECUP[iSize] << std::setw(14) << std::fixed + << thisHeprup.XERRUP[iSize] << std::setw(14) << std::fixed << thisHeprup.XMAXUP[iSize] << std::setw(14) + << std::fixed << thisHeprup.LPRUP[iSize] << std::endl; } std::cout << " " << std::endl; } + auto runC = runCache(iRun.index()); + std::lock_guard l{mutex_}; + // compute cross section for this run first // set the correct combined LHE+filter cross sections unsigned int i = 0; std::vector newInfos; - for (std::map::const_iterator iter = currentLumiBlockLHEXSec_.begin(); - iter != currentLumiBlockLHEXSec_.end(); + for (std::map::const_iterator iter = runC->currentLumiBlockLHEXSec_.begin(); + iter != runC->currentLumiBlockLHEXSec_.end(); ++iter, i++) { - GenLumiInfoProduct::ProcessInfo temp = product_.getProcessInfos()[i]; + GenLumiInfoProduct::ProcessInfo temp = runC->product_.getProcessInfos()[i]; temp.setLheXSec(iter->second.value(), iter->second.error()); newInfos.push_back(temp); } - product_.setProcessInfo(newInfos); + runC->product_.setProcessInfo(newInfos); - const GenLumiInfoProduct::XSec thisRunXSecPre = compute(product_); + const GenLumiInfoProduct::XSec thisRunXSecPre = compute(runC->product_); // xsection after matching before filters - combine(xsecPreFilter_, totalWeightPre_, thisRunXSecPre, thisRunWeightPre_); + combine(xsecPreFilter_, totalWeightPre_, thisRunXSecPre, runC->thisRunWeightPre_); double thisHepFilterEff = 1; double thisHepFilterErr = 0; - if (hepMCFilterEffRun_.sumWeights2() > 0) { - thisHepFilterEff = hepMCFilterEffRun_.filterEfficiency(hepidwtup_); - thisHepFilterErr = hepMCFilterEffRun_.filterEfficiencyError(hepidwtup_); + if (runC->hepMCFilterEffRun_.sumWeights2() > 0) { + thisHepFilterEff = runC->hepMCFilterEffRun_.filterEfficiency(hepidwtup_); + thisHepFilterErr = runC->hepMCFilterEffRun_.filterEfficiencyError(hepidwtup_); if (thisHepFilterEff < 0) { thisHepFilterEff = 1; thisHepFilterErr = 0; @@ -324,9 +328,9 @@ void GenXSecAnalyzer::endRun(edm::Run const& iRun, edm::EventSetup const&) { double thisGenFilterEff = 1; double thisGenFilterErr = 0; - if (filterOnlyEffRun_.sumWeights2() > 0) { - thisGenFilterEff = filterOnlyEffRun_.filterEfficiency(hepidwtup_); - thisGenFilterErr = filterOnlyEffRun_.filterEfficiencyError(hepidwtup_); + if (runC->filterOnlyEffRun_.sumWeights2() > 0) { + thisGenFilterEff = runC->filterOnlyEffRun_.filterEfficiency(hepidwtup_); + thisGenFilterErr = runC->filterOnlyEffRun_.filterEfficiencyError(hepidwtup_); if (thisGenFilterEff < 0) { thisGenFilterEff = 1; thisGenFilterErr = 0; @@ -339,15 +343,15 @@ void GenXSecAnalyzer::endRun(edm::Run const& iRun, edm::EventSetup const&) { pow(thisHepFilterErr / thisHepFilterEff, 2) + pow(thisGenFilterErr / thisGenFilterEff, 2)) : 0; const GenLumiInfoProduct::XSec thisRunXSec = GenLumiInfoProduct::XSec(thisXsec, thisErr); - combine(xsec_, totalWeight_, thisRunXSec, thisRunWeight_); + combine(xsec_, totalWeight_, thisRunXSec, runC->thisRunWeight_); } -void GenXSecAnalyzer::combine(double& finalValue, - double& finalError, - double& finalWeight, - const double& currentValue, - const double& currentError, - const double& currentWeight) { +void GenXSecAnalyzer::combine(double &finalValue, + double &finalError, + double &finalWeight, + const double ¤tValue, + const double ¤tError, + const double ¤tWeight) const { if (finalValue <= 0) { finalValue = currentValue; finalError = currentError; @@ -364,10 +368,10 @@ void GenXSecAnalyzer::combine(double& finalValue, return; } -void GenXSecAnalyzer::combine(GenLumiInfoProduct::XSec& finalXSec, - double& totalw, - const GenLumiInfoProduct::XSec& thisRunXSec, - const double& thisw) { +void GenXSecAnalyzer::combine(GenLumiInfoProduct::XSec &finalXSec, + double &totalw, + const GenLumiInfoProduct::XSec &thisRunXSec, + const double &thisw) const { double value = finalXSec.value(); double error = finalXSec.error(); double thisValue = thisRunXSec.value(); @@ -377,7 +381,7 @@ void GenXSecAnalyzer::combine(GenLumiInfoProduct::XSec& finalXSec, return; } -GenLumiInfoProduct::XSec GenXSecAnalyzer::compute(const GenLumiInfoProduct& iLumiInfo) { +GenLumiInfoProduct::XSec GenXSecAnalyzer::compute(const GenLumiInfoProduct &iLumiInfo) const { // sum of cross sections and errors over different processes double sigSelSum = 0.0; double err2SelSum = 0.0; @@ -531,7 +535,7 @@ void GenXSecAnalyzer::endJob() { const unsigned sizeOfInfos = jetMatchEffStat_.size(); const unsigned last = sizeOfInfos - 1; - std::string* title = new std::string[sizeOfInfos]; + std::string *title = new std::string[sizeOfInfos]; unsigned int i = 0; double jetmatch_eff = 0; double jetmatch_err = 0;