Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-16 05:03:32

0001 // Original Author:  Jan Kašpar
0002 
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/Framework/interface/SourceFactory.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Framework/interface/ESProducer.h"
0008 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0009 
0010 #include "CondFormats/PPSObjects/interface/LHCOpticalFunctionsSetCollection.h"
0011 #include "CondFormats/DataRecord/interface/CTPPSOpticsRcd.h"
0012 
0013 //----------------------------------------------------------------------------------------------------
0014 
0015 /**
0016  * \brief Loads optical functions from ROOT files.
0017  **/
0018 class CTPPSOpticalFunctionsESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0019 public:
0020   CTPPSOpticalFunctionsESSource(const edm::ParameterSet &);
0021   ~CTPPSOpticalFunctionsESSource() override = default;
0022 
0023   std::unique_ptr<LHCOpticalFunctionsSetCollection> produce(const CTPPSOpticsRcd &);
0024   static void fillDescriptions(edm::ConfigurationDescriptions &);
0025 
0026 private:
0027   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
0028                       const edm::IOVSyncValue &,
0029                       edm::ValidityInterval &) override;
0030 
0031   std::string m_label;
0032 
0033   struct FileInfo {
0034     double m_xangle;
0035     std::string m_fileName;
0036   };
0037 
0038   struct RPInfo {
0039     std::string m_dirName;
0040     double m_scoringPlaneZ;
0041   };
0042 
0043   struct Entry {
0044     edm::EventRange m_validityRange;
0045     std::vector<FileInfo> m_fileInfo;
0046     std::unordered_map<unsigned int, RPInfo> m_rpInfo;
0047   };
0048 
0049   std::vector<Entry> m_entries;
0050 
0051   bool m_currentEntryValid;
0052   unsigned int m_currentEntry;
0053 };
0054 
0055 //----------------------------------------------------------------------------------------------------
0056 //----------------------------------------------------------------------------------------------------
0057 
0058 CTPPSOpticalFunctionsESSource::CTPPSOpticalFunctionsESSource(const edm::ParameterSet &conf)
0059     : m_label(conf.getParameter<std::string>("label")), m_currentEntryValid(false), m_currentEntry(0) {
0060   for (const auto &entry_pset : conf.getParameter<std::vector<edm::ParameterSet>>("configuration")) {
0061     edm::EventRange validityRange = entry_pset.getParameter<edm::EventRange>("validityRange");
0062 
0063     std::vector<FileInfo> fileInfo;
0064     for (const auto &pset : entry_pset.getParameter<std::vector<edm::ParameterSet>>("opticalFunctions")) {
0065       fileInfo.emplace_back(pset.getParameter<double>("xangle"),
0066                             pset.getParameter<edm::FileInPath>("fileName").fullPath());
0067     }
0068 
0069     std::unordered_map<unsigned int, RPInfo> rpInfo;
0070     for (const auto &pset : entry_pset.getParameter<std::vector<edm::ParameterSet>>("scoringPlanes")) {
0071       rpInfo.emplace(pset.getParameter<unsigned int>("rpId"),
0072                      RPInfo{pset.getParameter<std::string>("dirName"), pset.getParameter<double>("z")});
0073     }
0074 
0075     m_entries.push_back({validityRange, fileInfo, rpInfo});
0076   }
0077 
0078   setWhatProduced(this, m_label);
0079   findingRecord<CTPPSOpticsRcd>();
0080 }
0081 
0082 //----------------------------------------------------------------------------------------------------
0083 
0084 void CTPPSOpticalFunctionsESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &key,
0085                                                    const edm::IOVSyncValue &iosv,
0086                                                    edm::ValidityInterval &oValidity) {
0087   for (unsigned int idx = 0; idx < m_entries.size(); ++idx) {
0088     const auto &entry = m_entries[idx];
0089 
0090     // is within an entry ?
0091     if (edm::contains(entry.m_validityRange, iosv.eventID())) {
0092       m_currentEntryValid = true;
0093       m_currentEntry = idx;
0094       oValidity = edm::ValidityInterval(edm::IOVSyncValue(entry.m_validityRange.startEventID()),
0095                                         edm::IOVSyncValue(entry.m_validityRange.endEventID()));
0096       return;
0097     }
0098   }
0099 
0100   // not within any entry
0101   m_currentEntryValid = false;
0102   m_currentEntry = 0;
0103 
0104   edm::LogInfo("") << "No configuration entry found for event " << iosv.eventID()
0105                    << ", no optical functions will be available.";
0106 
0107   const edm::EventID start(iosv.eventID().run(), iosv.eventID().luminosityBlock(), iosv.eventID().event());
0108   const edm::EventID end(iosv.eventID().run(), iosv.eventID().luminosityBlock(), iosv.eventID().event());
0109   oValidity = edm::ValidityInterval(edm::IOVSyncValue(start), edm::IOVSyncValue(end));
0110 }
0111 
0112 //----------------------------------------------------------------------------------------------------
0113 
0114 std::unique_ptr<LHCOpticalFunctionsSetCollection> CTPPSOpticalFunctionsESSource::produce(const CTPPSOpticsRcd &) {
0115   // prepare output, empty by default
0116   auto output = std::make_unique<LHCOpticalFunctionsSetCollection>();
0117 
0118   // fill the output
0119   if (m_currentEntryValid) {
0120     const auto &entry = m_entries[m_currentEntry];
0121 
0122     for (const auto &fi : entry.m_fileInfo) {
0123       std::unordered_map<unsigned int, LHCOpticalFunctionsSet> xa_data;
0124 
0125       for (const auto &rpi : entry.m_rpInfo) {
0126         LHCOpticalFunctionsSet fcn(fi.m_fileName, rpi.second.m_dirName, rpi.second.m_scoringPlaneZ);
0127         xa_data.emplace(rpi.first, std::move(fcn));
0128       }
0129 
0130       output->emplace(fi.m_xangle, xa_data);
0131     }
0132   }
0133 
0134   // commit the output
0135   return output;
0136 }
0137 
0138 //----------------------------------------------------------------------------------------------------
0139 
0140 void CTPPSOpticalFunctionsESSource::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0141   edm::ParameterSetDescription desc;
0142 
0143   desc.add<std::string>("label", "")->setComment("label of the optics record");
0144 
0145   edm::ParameterSetDescription config_desc;
0146 
0147   config_desc.add<edm::EventRange>("validityRange", edm::EventRange())->setComment("interval of validity");
0148 
0149   edm::ParameterSetDescription of_desc;
0150   of_desc.add<double>("xangle")->setComment("half crossing angle value in urad");
0151   of_desc.add<edm::FileInPath>("fileName")->setComment("ROOT file with optical functions");
0152   std::vector<edm::ParameterSet> of;
0153   config_desc.addVPSet("opticalFunctions", of_desc, of)
0154       ->setComment("list of optical functions at different crossing angles");
0155 
0156   edm::ParameterSetDescription sp_desc;
0157   sp_desc.add<unsigned int>("rpId")->setComment("associated detector DetId");
0158   sp_desc.add<std::string>("dirName")->setComment("associated path to the optical functions file");
0159   sp_desc.add<double>("z")->setComment("longitudinal position at scoring plane/detector");
0160   std::vector<edm::ParameterSet> sp;
0161   config_desc.addVPSet("scoringPlanes", sp_desc, sp)->setComment("list of sensitive planes/detectors stations");
0162 
0163   std::vector<edm::ParameterSet> config;
0164   desc.addVPSet("configuration", config_desc, sp)->setComment("list of configuration blocks");
0165 
0166   descriptions.add("ctppsOpticalFunctionsESSource", desc);
0167 }
0168 
0169 //----------------------------------------------------------------------------------------------------
0170 
0171 DEFINE_FWK_EVENTSETUP_SOURCE(CTPPSOpticalFunctionsESSource);