Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:48

0001 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0002 #include "DataFormats/OnlineMetaData/interface/OnlineLuminosityRecord.h"
0003 #include "DataFormats/Scalers/interface/LumiScalers.h"
0004 #include "FWCore/Framework/interface/ConsumesCollector.h"
0005 #include "FWCore/Framework/interface/stream/EDProducer.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0010 
0011 class TkInstLumiTableProducer : public edm::stream::EDProducer<> {
0012 public:
0013   explicit TkInstLumiTableProducer(const edm::ParameterSet& params)
0014       : m_name(params.getParameter<std::string>("name")),
0015         m_doc(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
0016         m_extension(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
0017         m_scalerToken(consumes<LumiScalersCollection>(params.getParameter<edm::InputTag>("lumiScalers"))),
0018         m_metaDataToken(consumes<OnlineLuminosityRecord>(params.getParameter<edm::InputTag>("metadata"))) {
0019     produces<nanoaod::FlatTable>();
0020   }
0021 
0022   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0023     edm::ParameterSetDescription desc;
0024     desc.add<std::string>("name", "");
0025     desc.add<std::string>("doc", "");
0026     desc.add<bool>("extension", false);
0027     desc.add<edm::InputTag>("lumiScalers", edm::InputTag("scalersRawToDigi"));
0028     desc.add<edm::InputTag>("metadata", edm::InputTag("onlineMetaDataDigis"));
0029     descriptions.add("tkInstLumiTable", desc);
0030   }
0031 
0032   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0033 
0034 private:
0035   const std::string m_name;
0036   const std::string m_doc;
0037   bool m_extension;
0038 
0039   const edm::EDGetTokenT<LumiScalersCollection> m_scalerToken;
0040   const edm::EDGetTokenT<OnlineLuminosityRecord> m_metaDataToken;
0041 };
0042 
0043 void TkInstLumiTableProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0044   auto out = std::make_unique<nanoaod::FlatTable>(1, m_name, true, m_extension);
0045 
0046   out->addColumnValue<int>("bx", iEvent.bunchCrossing(), "Bunch-crossing ID");
0047 
0048   float instLumi{0.}, pu{0.};
0049   edm::Handle<LumiScalersCollection> lumiScalers = iEvent.getHandle(m_scalerToken);
0050   edm::Handle<OnlineLuminosityRecord> metaData = iEvent.getHandle(m_metaDataToken);
0051 
0052   if (lumiScalers.isValid() && !lumiScalers->empty()) {
0053     if (lumiScalers->begin() != lumiScalers->end()) {
0054       instLumi = lumiScalers->begin()->instantLumi();
0055       pu = lumiScalers->begin()->pileup();
0056     }
0057   } else if (metaData.isValid()) {
0058     instLumi = metaData->instLumi();
0059     pu = metaData->avgPileUp();
0060   } else {
0061     edm::LogInfo("TkInstLumiTableProducer")
0062         << "Luminosity related collections not found in the event; will write dummy values";
0063   }
0064 
0065   out->addColumnValue<float>("instLumi", instLumi, "Instantaneous luminosity");
0066   out->addColumnValue<float>("PU", pu, "Pileup");
0067 
0068   iEvent.put(std::move(out));
0069 }
0070 
0071 #include "FWCore/PluginManager/interface/ModuleDef.h"
0072 #include "FWCore/Framework/interface/MakerMacros.h"
0073 DEFINE_FWK_MODULE(TkInstLumiTableProducer);