Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-10 05:44:06

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0009 #include "L1Trigger/L1THGCalUtilities/interface/HGCalTriggerNtupleBase.h"
0010 
0011 #include "FWCore/Framework/interface/ESHandle.h"
0012 
0013 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0014 #include "SimGeneral/HepPDTRecord/interface/PDTRecord.h"
0015 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0016 
0017 class HGCalTriggerNtupleManager : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0018 public:
0019   typedef std::unique_ptr<HGCalTriggerNtupleBase> ntuple_ptr;
0020 
0021 public:
0022   explicit HGCalTriggerNtupleManager(const edm::ParameterSet& conf);
0023   ~HGCalTriggerNtupleManager() override = default;
0024   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0025   void endRun(const edm::Run&, const edm::EventSetup&) override {}
0026   void analyze(const edm::Event&, const edm::EventSetup&) override;
0027 
0028 private:
0029   std::vector<ntuple_ptr> hgc_ntuples_;
0030   TTree* tree_;
0031 
0032   HGCalTriggerNtupleEventSetup ntuple_es_;
0033   const edm::ESGetToken<HepPDT::ParticleDataTable, PDTRecord> pdtToken_;
0034   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magfieldToken_;
0035   const edm::ESGetToken<HGCalTriggerGeometryBase, CaloGeometryRecord> triggerGeomToken_;
0036 };
0037 
0038 DEFINE_FWK_MODULE(HGCalTriggerNtupleManager);
0039 
0040 HGCalTriggerNtupleManager::HGCalTriggerNtupleManager(const edm::ParameterSet& conf)
0041     : pdtToken_(esConsumes<HepPDT::ParticleDataTable, PDTRecord, edm::Transition::BeginRun>()),
0042       magfieldToken_(esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>()),
0043       triggerGeomToken_(esConsumes<HGCalTriggerGeometryBase, CaloGeometryRecord, edm::Transition::BeginRun>()) {
0044   usesResource("TFileService");
0045   edm::Service<TFileService> file_service;
0046   tree_ = file_service->make<TTree>("HGCalTriggerNtuple", "HGCalTriggerNtuple");
0047   const std::vector<edm::ParameterSet>& ntuple_cfgs = conf.getParameterSetVector("Ntuples");
0048   for (const auto& ntuple_cfg : ntuple_cfgs) {
0049     const std::string& ntuple_name = ntuple_cfg.getParameter<std::string>("NtupleName");
0050     hgc_ntuples_.emplace_back(HGCalTriggerNtupleFactory::get()->create(ntuple_name, ntuple_cfg));
0051     hgc_ntuples_.back()->initialize(*tree_, ntuple_cfg, consumesCollector());
0052   }
0053 }
0054 
0055 void HGCalTriggerNtupleManager::beginRun(const edm::Run& run, const edm::EventSetup& es) {
0056   ntuple_es_.pdt = es.getHandle(pdtToken_);
0057   ntuple_es_.magfield = es.getHandle(magfieldToken_);
0058   ntuple_es_.geometry = es.getHandle(triggerGeomToken_);
0059 }
0060 
0061 void HGCalTriggerNtupleManager::analyze(const edm::Event& e, const edm::EventSetup& es) {
0062   for (auto& hgc_ntuple : hgc_ntuples_) {
0063     if (hgc_ntuple->accessEventSetup()) {
0064       hgc_ntuple->fill(e, es);
0065     } else {
0066       hgc_ntuple->fill(e, ntuple_es_);
0067     }
0068   }
0069 
0070   tree_->Fill();
0071 }