Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:45

0001 // -*- C++ -*-
0002 //
0003 // Package:    CalibTracker/SiStripCommon
0004 // Class:      PrescaleEventFilter
0005 //
0006 /**\class PrescaleEventFilter PrescaleEventFilter.cc CalibTracker/SiStripCommon/plugins/PrescaleEventFilter.cc
0007 
0008  Description: Simple class to prescale events entering the Strip Tracker Calibration Tree
0009 
0010  Implementation:
0011      Largely copied from HLTrigger/HLTcore/plugins/HLTPrescaler.cc, without the need to specify a specific trigger path
0012 */
0013 //
0014 // Original Author:  Marco Musich
0015 //         Created:  Wed, 29 Nov 2017 15:27:07 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
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 }  // namespace prescale
0039 
0040 //
0041 // class declaration
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   // ----------member data ---------------------------
0062 
0063   /// accept one in prescaleFactor_; 0 means never to accept an event
0064   unsigned int prescaleFactor_;
0065 
0066   /// event counter
0067   unsigned int eventCount_;
0068 
0069   /// accept counter
0070   unsigned int acceptCount_;
0071 
0072   /// initial offset
0073   unsigned int offsetCount_;
0074   unsigned int offsetPhase_;
0075 
0076   /// check for (re)initialization of the prescale
0077   bool newLumi_;
0078 
0079   /// "seed" used to initialize the prescale counter
0080   static const unsigned int prescaleSeed_ = 65537;
0081 };
0082 
0083 //
0084 // constructors and destructor
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   //now do what ever initialization is needed
0093 }
0094 
0095 PrescaleEventFilter::~PrescaleEventFilter() {}
0096 
0097 //
0098 // member functions
0099 //
0100 
0101 // ------------ method called on each new Event  ------------
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     // initialize the prescale counter to the first event number multiplied by a big "seed"
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 // ------------ method called once each stream before processing any runs, lumis or events  ------------
0121 void PrescaleEventFilter::beginStream(edm::StreamID) {}
0122 
0123 //_____________________________________________________________________________
0124 void PrescaleEventFilter::endStream() {
0125   //since these are std::atomic, it is safe to increment them
0126   // even if multiple endStreams are being called.
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 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
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 //define this as a plug-in
0150 DEFINE_FWK_MODULE(PrescaleEventFilter);