Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:53

0001 /****************************************************************************
0002  *
0003  * This is a part of CMS-TOTEM PPS offline software.
0004  * Authors:
0005  *  Jan Kaspar (jan.kaspar@gmail.com)
0006  *  Helena Malbouisson
0007  *  Clemencia Mora Herrera
0008  *  Christopher Misan
0009  ****************************************************************************/
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/SourceFactory.h"
0015 #include "FWCore/Framework/interface/ESProducer.h"
0016 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 
0019 #include "CondFormats/PPSObjects/interface/CTPPSRPAlignmentCorrectionsDataSequence.h"
0020 #include "CondFormats/PPSObjects/interface/CTPPSRPAlignmentCorrectionsMethods.h"
0021 
0022 #include "CondFormats/AlignmentRecord/interface/CTPPSRPAlignmentCorrectionsDataRcd.h"  // this used to be RPMeasuredAlignmentRecord.h
0023 #include "CondFormats/AlignmentRecord/interface/RPRealAlignmentRecord.h"
0024 #include "CondFormats/AlignmentRecord/interface/RPMisalignedAlignmentRecord.h"
0025 
0026 #include "CalibPPS/ESProducers/interface/CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon.h"
0027 
0028 #include <vector>
0029 #include <string>
0030 #include <map>
0031 #include <set>
0032 
0033 using namespace std;
0034 using namespace edm;
0035 
0036 /**
0037  * Loads alignment corrections to EventSetup.
0038  **/
0039 class CTPPSRPAlignmentCorrectionsDataESSourceXML : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0040 public:
0041   CTPPSRPAlignmentCorrectionsDataESSourceXML(const edm::ParameterSet &p);
0042   ~CTPPSRPAlignmentCorrectionsDataESSourceXML() override;
0043 
0044 protected:
0045   std::unique_ptr<CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon> ctppsRPAlignmentCorrectionsDataESSourceXMLCommon;
0046 
0047   std::unique_ptr<CTPPSRPAlignmentCorrectionsData> produceMeasured(const CTPPSRPAlignmentCorrectionsDataRcd &);
0048   std::unique_ptr<CTPPSRPAlignmentCorrectionsData> produceReal(const RPRealAlignmentRecord &);
0049   std::unique_ptr<CTPPSRPAlignmentCorrectionsData> produceMisaligned(const RPMisalignedAlignmentRecord &);
0050 
0051   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
0052                       const edm::IOVSyncValue &,
0053                       edm::ValidityInterval &) override;
0054 };
0055 
0056 //----------------------------------------------------------------------------------------------------
0057 
0058 CTPPSRPAlignmentCorrectionsDataESSourceXML::CTPPSRPAlignmentCorrectionsDataESSourceXML(const edm::ParameterSet &pSet) {
0059   ctppsRPAlignmentCorrectionsDataESSourceXMLCommon =
0060       std::make_unique<CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon>(pSet);
0061   setWhatProduced(this, &CTPPSRPAlignmentCorrectionsDataESSourceXML::produceMeasured);
0062   setWhatProduced(this, &CTPPSRPAlignmentCorrectionsDataESSourceXML::produceReal);
0063   setWhatProduced(this, &CTPPSRPAlignmentCorrectionsDataESSourceXML::produceMisaligned);
0064 
0065   findingRecord<CTPPSRPAlignmentCorrectionsDataRcd>();
0066   findingRecord<RPRealAlignmentRecord>();
0067   findingRecord<RPMisalignedAlignmentRecord>();
0068 }
0069 
0070 //----------------------------------------------------------------------------------------------------
0071 
0072 CTPPSRPAlignmentCorrectionsDataESSourceXML::~CTPPSRPAlignmentCorrectionsDataESSourceXML() {}
0073 
0074 //----------------------------------------------------------------------------------------------------
0075 
0076 std::unique_ptr<CTPPSRPAlignmentCorrectionsData> CTPPSRPAlignmentCorrectionsDataESSourceXML::produceMeasured(
0077     const CTPPSRPAlignmentCorrectionsDataRcd &iRecord) {
0078   return std::make_unique<CTPPSRPAlignmentCorrectionsData>(
0079       ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acMeasured);
0080 }
0081 
0082 //----------------------------------------------------------------------------------------------------
0083 
0084 std::unique_ptr<CTPPSRPAlignmentCorrectionsData> CTPPSRPAlignmentCorrectionsDataESSourceXML::produceReal(
0085     const RPRealAlignmentRecord &iRecord) {
0086   return std::make_unique<CTPPSRPAlignmentCorrectionsData>(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acReal);
0087 }
0088 
0089 //----------------------------------------------------------------------------------------------------
0090 
0091 std::unique_ptr<CTPPSRPAlignmentCorrectionsData> CTPPSRPAlignmentCorrectionsDataESSourceXML::produceMisaligned(
0092     const RPMisalignedAlignmentRecord &iRecord) {
0093   return std::make_unique<CTPPSRPAlignmentCorrectionsData>(
0094       ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acMisaligned);
0095 }
0096 
0097 //----------------------------------------------------------------------------------------------------
0098 
0099 void CTPPSRPAlignmentCorrectionsDataESSourceXML::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &key,
0100                                                                 const IOVSyncValue &iosv,
0101                                                                 ValidityInterval &valInt) {
0102   if (ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->verbosity) {
0103     time_t unixTime = iosv.time().unixTime();
0104     char timeStr[50];
0105     strftime(timeStr, 50, "%F %T", localtime(&unixTime));
0106 
0107     LogInfo("PPS") << ">> CTPPSRPAlignmentCorrectionsDataESSourceXML::setIntervalFor(" << key.name() << ")";
0108 
0109     LogInfo("PPS") << "    event=" << iosv.eventID() << ", UNIX timestamp=" << unixTime << " (" << timeStr << ")";
0110   }
0111 
0112   // // determine what sequence and corrections should be used
0113   CTPPSRPAlignmentCorrectionsDataSequence *p_seq = nullptr;
0114   CTPPSRPAlignmentCorrectionsData *p_corr = nullptr;
0115 
0116   if (strcmp(key.name(), "CTPPSRPAlignmentCorrectionsDataRcd") == 0) {
0117     p_seq = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acsMeasured);
0118     p_corr = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acMeasured);
0119   }
0120 
0121   if (strcmp(key.name(), "RPRealAlignmentRecord") == 0) {
0122     p_seq = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acsReal);
0123     p_corr = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acReal);
0124   }
0125 
0126   if (strcmp(key.name(), "RPMisalignedAlignmentRecord") == 0) {
0127     p_seq = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acsMisaligned);
0128     p_corr = &(ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->acMisaligned);
0129   }
0130 
0131   if (p_corr == nullptr)
0132     throw cms::Exception("CTPPSRPAlignmentCorrectionsDataESSourceXML::setIntervalFor")
0133         << "Unknown record " << key.name();
0134 
0135   // // find the corresponding interval
0136   bool next_exists = false;
0137   const edm::EventID &event_curr = iosv.eventID();
0138   edm::EventID event_next_start(edm::EventID::maxRunNumber(), edm::EventID::maxLuminosityBlockNumber(), 1);
0139 
0140   for (const auto &it : *p_seq) {
0141     const auto &it_event_first = it.first.first().eventID();
0142     const auto &it_event_last = it.first.last().eventID();
0143 
0144     bool it_contained_lo = ((it_event_first.run() < event_curr.run()) ||
0145                             ((it_event_first.run() == event_curr.run()) &&
0146                              (it_event_first.luminosityBlock() <= event_curr.luminosityBlock())));
0147 
0148     bool it_contained_up = ((it_event_last.run() > event_curr.run()) ||
0149                             ((it_event_last.run() == event_curr.run()) &&
0150                              (it_event_last.luminosityBlock() >= event_curr.luminosityBlock())));
0151 
0152     if (it_contained_lo && it_contained_up) {
0153       valInt = it.first;
0154       *p_corr = it.second;
0155 
0156       if (ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->verbosity) {
0157         LogInfo("PPS") << "    setting validity interval ["
0158                        << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(valInt.first()) << ", "
0159                        << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(valInt.last()) << "]";
0160       }
0161 
0162       return;
0163     }
0164 
0165     bool it_in_future = ((it_event_first.run() > event_curr.run()) ||
0166                          ((it_event_first.run() == event_curr.run() &&
0167                            (it_event_first.luminosityBlock() > event_curr.luminosityBlock()))));
0168 
0169     if (it_in_future) {
0170       next_exists = true;
0171       if (event_next_start > it_event_first)
0172         event_next_start = it_event_first;
0173     }
0174   }
0175 
0176   // no interval found, set empty corrections
0177   *p_corr = CTPPSRPAlignmentCorrectionsData();
0178 
0179   if (!next_exists) {
0180     valInt = ValidityInterval(iosv, iosv.endOfTime());
0181   } else {
0182     const EventID &event_last = ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->previousLS(event_next_start);
0183     valInt = ValidityInterval(iosv, IOVSyncValue(event_last));
0184   }
0185 
0186   if (ctppsRPAlignmentCorrectionsDataESSourceXMLCommon->verbosity) {
0187     LogInfo("PPS") << "    setting validity interval ["
0188                    << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(valInt.first()) << ", "
0189                    << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(valInt.last())
0190                    << "] (empty alignment corrections)";
0191   }
0192 }
0193 
0194 DEFINE_FWK_EVENTSETUP_SOURCE(CTPPSRPAlignmentCorrectionsDataESSourceXML);