File indexing completed on 2024-04-06 11:59:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021
0022
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/stream/EDFilter.h"
0025
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/StreamID.h"
0031
0032 namespace prescale {
0033 struct Efficiency {
0034 Efficiency() : eventCount_(0), acceptCount_(0) {}
0035 mutable std::atomic<unsigned int> eventCount_;
0036 mutable std::atomic<unsigned int> acceptCount_;
0037 };
0038 }
0039
0040
0041
0042
0043
0044 class PrescaleEventFilter : public edm::stream::EDFilter<edm::GlobalCache<prescale::Efficiency> > {
0045 public:
0046 explicit PrescaleEventFilter(edm::ParameterSet const& iConfig, const prescale::Efficiency* efficiency);
0047 ~PrescaleEventFilter() override;
0048
0049 static std::unique_ptr<prescale::Efficiency> initializeGlobalCache(edm::ParameterSet const&) {
0050 return std::make_unique<prescale::Efficiency>();
0051 };
0052
0053 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054 static void globalEndJob(const prescale::Efficiency* efficiency);
0055
0056 private:
0057 void beginStream(edm::StreamID) override;
0058 bool filter(edm::Event&, const edm::EventSetup&) override;
0059 void endStream() override;
0060
0061
0062
0063
0064 unsigned int prescaleFactor_;
0065
0066
0067 unsigned int eventCount_;
0068
0069
0070 unsigned int acceptCount_;
0071
0072
0073 unsigned int offsetCount_;
0074 unsigned int offsetPhase_;
0075
0076
0077 bool newLumi_;
0078
0079
0080 static const unsigned int prescaleSeed_ = 65537;
0081 };
0082
0083
0084
0085
0086 PrescaleEventFilter::PrescaleEventFilter(const edm::ParameterSet& iConfig, const prescale::Efficiency* efficiency)
0087 : prescaleFactor_(iConfig.getParameter<unsigned int>("prescale")),
0088 eventCount_(0),
0089 acceptCount_(0),
0090 offsetCount_(0),
0091 offsetPhase_(iConfig.getParameter<unsigned int>("offset")) {
0092
0093 }
0094
0095 PrescaleEventFilter::~PrescaleEventFilter() {}
0096
0097
0098
0099
0100
0101
0102 bool PrescaleEventFilter::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0103 using namespace edm;
0104
0105 bool needsInit(eventCount_ == 0);
0106
0107 if (needsInit && (prescaleFactor_ != 0)) {
0108
0109 offsetCount_ = ((uint64_t)(iEvent.id().event() + offsetPhase_) * prescaleSeed_) % prescaleFactor_;
0110 }
0111
0112 const bool result((prescaleFactor_ == 0) ? false : ((eventCount_ + offsetCount_) % prescaleFactor_ == 0));
0113
0114 ++eventCount_;
0115 if (result)
0116 ++acceptCount_;
0117 return result;
0118 }
0119
0120
0121 void PrescaleEventFilter::beginStream(edm::StreamID) {}
0122
0123
0124 void PrescaleEventFilter::endStream() {
0125
0126
0127 globalCache()->eventCount_ += eventCount_;
0128 globalCache()->acceptCount_ += acceptCount_;
0129 return;
0130 }
0131
0132
0133 void PrescaleEventFilter::globalEndJob(const prescale::Efficiency* efficiency) {
0134 unsigned int accept(efficiency->acceptCount_);
0135 unsigned int event(efficiency->eventCount_);
0136 edm::LogInfo("PrescaleSummary") << accept << "/" << event << " ("
0137 << 100. * accept / static_cast<double>(std::max(1u, event))
0138 << "% of events accepted).";
0139 return;
0140 }
0141
0142
0143 void PrescaleEventFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0144 edm::ParameterSetDescription desc;
0145 desc.add<unsigned int>("prescale", 1);
0146 desc.add<unsigned int>("offset", 0);
0147 descriptions.add("prescaleEvent", desc);
0148 }
0149
0150 DEFINE_FWK_MODULE(PrescaleEventFilter);