Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:35

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       const double &xangle = pset.getParameter<double>("xangle");
0066       const std::string &fileName = pset.getParameter<edm::FileInPath>("fileName").fullPath();
0067       fileInfo.push_back({xangle, fileName});
0068     }
0069 
0070     std::unordered_map<unsigned int, RPInfo> rpInfo;
0071     for (const auto &pset : entry_pset.getParameter<std::vector<edm::ParameterSet>>("scoringPlanes")) {
0072       const unsigned int rpId = pset.getParameter<unsigned int>("rpId");
0073       const std::string dirName = pset.getParameter<std::string>("dirName");
0074       const double z = pset.getParameter<double>("z");
0075       const RPInfo entry = {dirName, z};
0076       rpInfo.emplace(rpId, entry);
0077     }
0078 
0079     m_entries.push_back({validityRange, fileInfo, rpInfo});
0080   }
0081 
0082   setWhatProduced(this, m_label);
0083   findingRecord<CTPPSOpticsRcd>();
0084 }
0085 
0086 //----------------------------------------------------------------------------------------------------
0087 
0088 void CTPPSOpticalFunctionsESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &key,
0089                                                    const edm::IOVSyncValue &iosv,
0090                                                    edm::ValidityInterval &oValidity) {
0091   for (unsigned int idx = 0; idx < m_entries.size(); ++idx) {
0092     const auto &entry = m_entries[idx];
0093 
0094     // is within an entry ?
0095     if (edm::contains(entry.m_validityRange, iosv.eventID())) {
0096       m_currentEntryValid = true;
0097       m_currentEntry = idx;
0098       oValidity = edm::ValidityInterval(edm::IOVSyncValue(entry.m_validityRange.startEventID()),
0099                                         edm::IOVSyncValue(entry.m_validityRange.endEventID()));
0100       return;
0101     }
0102   }
0103 
0104   // not within any entry
0105   m_currentEntryValid = false;
0106   m_currentEntry = 0;
0107 
0108   edm::LogInfo("") << "No configuration entry found for event " << iosv.eventID()
0109                    << ", no optical functions will be available.";
0110 
0111   const edm::EventID start(iosv.eventID().run(), iosv.eventID().luminosityBlock(), iosv.eventID().event());
0112   const edm::EventID end(iosv.eventID().run(), iosv.eventID().luminosityBlock(), iosv.eventID().event());
0113   oValidity = edm::ValidityInterval(edm::IOVSyncValue(start), edm::IOVSyncValue(end));
0114 }
0115 
0116 //----------------------------------------------------------------------------------------------------
0117 
0118 std::unique_ptr<LHCOpticalFunctionsSetCollection> CTPPSOpticalFunctionsESSource::produce(const CTPPSOpticsRcd &) {
0119   // prepare output, empty by default
0120   auto output = std::make_unique<LHCOpticalFunctionsSetCollection>();
0121 
0122   // fill the output
0123   if (m_currentEntryValid) {
0124     const auto &entry = m_entries[m_currentEntry];
0125 
0126     for (const auto &fi : entry.m_fileInfo) {
0127       std::unordered_map<unsigned int, LHCOpticalFunctionsSet> xa_data;
0128 
0129       for (const auto &rpi : entry.m_rpInfo) {
0130         LHCOpticalFunctionsSet fcn(fi.m_fileName, rpi.second.m_dirName, rpi.second.m_scoringPlaneZ);
0131         xa_data.emplace(rpi.first, std::move(fcn));
0132       }
0133 
0134       output->emplace(fi.m_xangle, xa_data);
0135     }
0136   }
0137 
0138   // commit the output
0139   return output;
0140 }
0141 
0142 //----------------------------------------------------------------------------------------------------
0143 
0144 void CTPPSOpticalFunctionsESSource::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0145   edm::ParameterSetDescription desc;
0146 
0147   desc.add<std::string>("label", "")->setComment("label of the optics record");
0148 
0149   edm::ParameterSetDescription config_desc;
0150 
0151   config_desc.add<edm::EventRange>("validityRange", edm::EventRange())->setComment("interval of validity");
0152 
0153   edm::ParameterSetDescription of_desc;
0154   of_desc.add<double>("xangle")->setComment("half crossing angle value in urad");
0155   of_desc.add<edm::FileInPath>("fileName")->setComment("ROOT file with optical functions");
0156   std::vector<edm::ParameterSet> of;
0157   config_desc.addVPSet("opticalFunctions", of_desc, of)
0158       ->setComment("list of optical functions at different crossing angles");
0159 
0160   edm::ParameterSetDescription sp_desc;
0161   sp_desc.add<unsigned int>("rpId")->setComment("associated detector DetId");
0162   sp_desc.add<std::string>("dirName")->setComment("associated path to the optical functions file");
0163   sp_desc.add<double>("z")->setComment("longitudinal position at scoring plane/detector");
0164   std::vector<edm::ParameterSet> sp;
0165   config_desc.addVPSet("scoringPlanes", sp_desc, sp)->setComment("list of sensitive planes/detectors stations");
0166 
0167   std::vector<edm::ParameterSet> config;
0168   desc.addVPSet("configuration", config_desc, sp)->setComment("list of configuration blocks");
0169 
0170   descriptions.add("ctppsOpticalFunctionsESSource", desc);
0171 }
0172 
0173 //----------------------------------------------------------------------------------------------------
0174 
0175 DEFINE_FWK_EVENTSETUP_SOURCE(CTPPSOpticalFunctionsESSource);