Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-13 02:58:34

0001 //********************************************************************************
0002 //
0003 //  Description:
0004 //    Producing and filling resolution histograms
0005 //
0006 //
0007 //  Author: Sam Harper, RAL, April 202
0008 //
0009 //********************************************************************************
0010 
0011 // system include files
0012 #include <memory>
0013 #include <chrono>
0014 #include <ctime>
0015 
0016 // user include files
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/MakerMacros.h"
0019 
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/Utilities/interface/InputTag.h"
0022 
0023 // including GenParticles
0024 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0025 
0026 // icnluding GenMET
0027 #include "DataFormats/METReco/interface/GenMETCollection.h"
0028 #include "DataFormats/METReco/interface/GenMET.h"
0029 
0030 // includes needed for histogram creation
0031 #include "FWCore/ServiceRegistry/interface/Service.h"
0032 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0033 
0034 // FunctionDefs
0035 #include "DQMOffline/Trigger/interface/FunctionDefs.h"
0036 
0037 // includes of histogram collection class
0038 #include "Validation/HLTrigger/interface/HLTGenResHistColl.h"
0039 
0040 // DQMStore
0041 #include "DQMServices/Core/interface/DQMStore.h"
0042 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0043 
0044 // trigger Event
0045 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0046 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0047 
0048 // object that can be a GenJet, GenParticle or energy sum
0049 #include "Validation/HLTrigger/interface/HLTGenValObject.h"
0050 #include "Validation/HLTrigger/interface/HLTGenValObjectMgr.h"
0051 
0052 class HLTGenResSource : public DQMEDAnalyzer {
0053 public:
0054   explicit HLTGenResSource(const edm::ParameterSet&);
0055   ~HLTGenResSource() override = default;
0056   HLTGenResSource(const HLTGenResSource&) = delete;
0057   HLTGenResSource& operator=(const HLTGenResSource&) = delete;
0058 
0059   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0060 
0061 private:
0062   void analyze(const edm::Event&, const edm::EventSetup&) override;
0063   void bookHistograms(DQMStore::IBooker&, edm::Run const& run, edm::EventSetup const& c) override;
0064   void dqmBeginRun(const edm::Run&, const edm::EventSetup&) override;
0065   void initCfgs(const edm::Run&, const edm::EventSetup&);
0066 
0067   // functions to get correct object collection for chosen object type
0068   std::vector<HLTGenValObject> getObjectCollection(const edm::Event&);
0069   std::vector<HLTGenValObject> getGenParticles(const edm::Event&);
0070   reco::GenParticle getLastCopyPreFSR(reco::GenParticle part);
0071   reco::GenParticle getLastCopy(reco::GenParticle part);
0072   bool passGenJetID(const reco::GenJet& jet);
0073   // ----------member data ---------------------------
0074 
0075   // tokens to get collections
0076   HLTGenValObjectMgr genObjMgr_;
0077   const edm::EDGetTokenT<trigger::TriggerEvent> trigEventToken_;
0078 
0079   bool initalised_;
0080   bool booked_;
0081   // config strings/Psets
0082   std::string objType_;
0083   std::string dirName_;
0084   std::vector<edm::ParameterSet> histConfigs_;
0085   std::vector<edm::ParameterSet> histConfigs2D_;
0086   std::vector<edm::ParameterSet> binnings_;
0087   std::string hltProcessName_;
0088   HLTConfigProvider hltConfig_;
0089 
0090   // constructing the info string, which will be written to the output file for display of information in the GUI
0091   // the string will have a JSON formating, thus starting here with the opening bracket, which will be close directly before saving to the root file
0092   std::string infoString_ = "{";
0093 
0094   // histogram collection per path
0095   std::vector<HLTGenResHistColl> collections_;
0096 };
0097 
0098 HLTGenResSource::HLTGenResSource(const edm::ParameterSet& iConfig)
0099     : genObjMgr_(iConfig.getParameter<edm::ParameterSet>("genConfig"), consumesCollector()),
0100 
0101       trigEventToken_(consumes<trigger::TriggerEvent>(iConfig.getParameter<edm::InputTag>("trigEvent"))),
0102       initalised_(false),
0103       booked_(false)
0104 
0105 {
0106   // getting all other configurations
0107   dirName_ = iConfig.getParameter<std::string>("dqmDirName");
0108   hltProcessName_ = iConfig.getParameter<std::string>("hltProcessName");
0109 
0110   auto resCollections = iConfig.getParameter<std::vector<edm::ParameterSet>>("resCollConfigs");
0111   for (const auto& resCollConfig : resCollections) {
0112     collections_.emplace_back(HLTGenResHistColl(resCollConfig, hltProcessName_));
0113   }
0114 }
0115 
0116 void HLTGenResSource::initCfgs(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0117   // writing general information to info JSON
0118   auto t = std::time(nullptr);
0119   auto tm = *std::localtime(&t);
0120 
0121   // date and time of running this
0122   std::ostringstream timeStringStream;
0123   timeStringStream << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
0124   auto timeString = timeStringStream.str();
0125   infoString_ += "\"date & time\":\"" + timeString + "\",";
0126 
0127   // CMSSW version
0128   std::string cmsswVersion = std::getenv("CMSSW_VERSION");
0129   infoString_ += std::string("\"CMSSW release\":\"") + cmsswVersion + "\",";
0130 
0131   // Initialize hltConfig, for cross-checking whether chosen paths exist
0132   bool changedConfig;
0133   if (!hltConfig_.init(iRun, iSetup, hltProcessName_, changedConfig)) {
0134     edm::LogError("HLTGenResSource") << "Initialization of HLTConfigProvider failed!";
0135     return;
0136   }
0137 
0138   // global tag
0139   infoString_ += std::string("\"global tag\":\"") + hltConfig_.globalTag() + "\",";
0140 
0141   // confDB table name
0142   infoString_ += std::string("\"HLT ConfDB table\":\"") + hltConfig_.tableName() + "\"}";
0143 
0144   initalised_ = true;
0145 }
0146 
0147 void HLTGenResSource::dqmBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0148   if (!initalised_)
0149     initCfgs(iRun, iSetup);
0150 }
0151 
0152 // ------------ method called for each event  ------------
0153 void HLTGenResSource::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0154   // creating the collection of HLTGenValObjects
0155 
0156   // init triggerEvent, which is always needed
0157   edm::Handle<trigger::TriggerEvent> triggerEvent;
0158   iEvent.getByToken(trigEventToken_, triggerEvent);
0159 
0160   // loop over all objects and fill hists
0161   for (auto& collection : collections_) {
0162     const std::vector<HLTGenValObject> objects = genObjMgr_.getGenValObjects(iEvent, collection.objType());
0163     for (const auto& object : objects) {
0164       collection.fillHists(object, triggerEvent);
0165     }
0166   }
0167 }
0168 
0169 // ------------ method called once each job just before starting event loop  ------------
0170 void HLTGenResSource::bookHistograms(DQMStore::IBooker& iBooker, const edm::Run& run, const edm::EventSetup& setup) {
0171   if (booked_)
0172     return;
0173   iBooker.setCurrentFolder(dirName_);
0174 
0175   iBooker.bookString("HLTGenResInfo", infoString_);
0176 
0177   for (long unsigned int collNr = 0; collNr < collections_.size(); collNr++) {
0178     collections_[collNr].bookHists(iBooker);
0179   }
0180   booked_ = true;
0181 }
0182 
0183 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0184 void HLTGenResSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0185   edm::ParameterSetDescription desc;
0186 
0187   desc.add<std::string>("dqmDirName", "HLTGenVal");
0188   desc.add<std::string>("hltProcessName", "HLT");
0189   desc.add<edm::InputTag>("trigEvent", edm::InputTag("hltTriggerSummaryAOD"));
0190   desc.add<edm::ParameterSetDescription>("genConfig", HLTGenValObjectMgr::makePSetDescription());
0191   desc.addVPSet("resCollConfigs", HLTGenResHistColl::makePSetDescription(), std::vector<edm::ParameterSet>());
0192 
0193   descriptions.addDefault(desc);
0194 }
0195 
0196 //define this as a framework plug-in
0197 DEFINE_FWK_MODULE(HLTGenResSource);