Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:37

0001 // F. Cossutti
0002 // $Revision://
0003 
0004 // analyzer of a summary information product on filter efficiency for a user specified path
0005 // meant for the generator filter efficiency calculation
0006 
0007 // system include files
0008 #include <memory>
0009 #include <iostream>
0010 
0011 // user include files
0012 
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0015 
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/Run.h"
0018 #include "FWCore/Framework/interface/EventSetup.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/Framework/interface/LuminosityBlock.h"
0021 #include "FWCore/Utilities/interface/EDGetToken.h"
0022 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 
0025 #include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h"
0026 //
0027 // class declaration
0028 //
0029 
0030 namespace gfea {
0031   struct Empty {};
0032 };  // namespace gfea
0033 
0034 class GenFilterEfficiencyAnalyzer final : public edm::global::EDAnalyzer<edm::LuminosityBlockCache<gfea::Empty>> {
0035 public:
0036   explicit GenFilterEfficiencyAnalyzer(const edm::ParameterSet&);
0037   ~GenFilterEfficiencyAnalyzer() final;
0038 
0039 private:
0040   void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const final;
0041   std::shared_ptr<gfea::Empty> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0042                                                           edm::EventSetup const&) const final;
0043   void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const final;
0044   void endJob() final;
0045 
0046   edm::EDGetTokenT<GenFilterInfo> genFilterInfoToken_;
0047   mutable std::mutex mutex_;
0048   CMS_THREAD_GUARD(mutex_) mutable GenFilterInfo totalGenFilterInfo_;
0049 
0050   // ----------member data ---------------------------
0051 };
0052 
0053 GenFilterEfficiencyAnalyzer::GenFilterEfficiencyAnalyzer(const edm::ParameterSet& pset)
0054     : genFilterInfoToken_(consumes<GenFilterInfo, edm::InLumi>(pset.getParameter<edm::InputTag>("genFilterInfoTag"))),
0055       totalGenFilterInfo_(0, 0, 0, 0, 0., 0., 0., 0.) {}
0056 
0057 GenFilterEfficiencyAnalyzer::~GenFilterEfficiencyAnalyzer() {}
0058 
0059 void GenFilterEfficiencyAnalyzer::analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const {}
0060 
0061 std::shared_ptr<gfea::Empty> GenFilterEfficiencyAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi,
0062                                                                                      edm::EventSetup const&) const {
0063   return std::shared_ptr<gfea::Empty>();
0064 }
0065 
0066 // ------------ method called once each job just after ending the event loop  ------------
0067 
0068 void GenFilterEfficiencyAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi,
0069                                                            edm::EventSetup const&) const {
0070   edm::Handle<GenFilterInfo> genFilter;
0071   iLumi.getByToken(genFilterInfoToken_, genFilter);
0072 
0073   std::cout << "Lumi section " << iLumi.id() << std::endl;
0074 
0075   std::cout << "N total = " << genFilter->sumWeights() << " N passed = " << genFilter->sumPassWeights()
0076             << " N failed = " << genFilter->sumFailWeights() << std::endl;
0077   std::cout << "Generator filter efficiency = " << genFilter->filterEfficiency(-1) << " +- "
0078             << genFilter->filterEfficiencyError(-1) << std::endl;
0079   {
0080     std::lock_guard<std::mutex> guard(mutex_);
0081     totalGenFilterInfo_.mergeProduct(*genFilter);
0082   }
0083 }
0084 
0085 void GenFilterEfficiencyAnalyzer::endJob() {
0086   std::cout << "Total events = " << totalGenFilterInfo_.sumWeights()
0087             << " Passed events = " << totalGenFilterInfo_.sumPassWeights() << std::endl;
0088   std::cout << "Filter efficiency = " << totalGenFilterInfo_.filterEfficiency(-1) << " +- "
0089             << totalGenFilterInfo_.filterEfficiencyError(-1) << std::endl;
0090 }
0091 
0092 DEFINE_FWK_MODULE(GenFilterEfficiencyAnalyzer);