Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /****************************************************************************
0002  * Authors:
0003  *  Jan Kaspar (jan.kaspar@gmail.com)
0004  *  Helena Malbouisson
0005  *  Clemencia Mora Herrera
0006  *  Christopher Misan
0007  ****************************************************************************/
0008 
0009 #include "CalibPPS/ESProducers/interface/CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 
0014 #include "CondFormats/PPSObjects/interface/CTPPSRPAlignmentCorrectionsMethods.h"
0015 
0016 #include <map>
0017 #include <set>
0018 
0019 //----------------------------------------------------------------------------------------------------
0020 
0021 CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon(
0022     const edm::ParameterSet &pSet)
0023     : verbosity(pSet.getUntrackedParameter<unsigned int>("verbosity", 0)) {
0024   std::vector<std::string> measuredFiles;
0025   for (const auto &f : pSet.getParameter<std::vector<std::string> >("MeasuredFiles"))
0026     measuredFiles.push_back(edm::FileInPath(f).fullPath());
0027   PrepareSequence("Measured", acsMeasured, measuredFiles);
0028 
0029   std::vector<std::string> realFiles;
0030   for (const auto &f : pSet.getParameter<std::vector<std::string> >("RealFiles"))
0031     realFiles.push_back(edm::FileInPath(f).fullPath());
0032   PrepareSequence("Real", acsReal, realFiles);
0033 
0034   std::vector<std::string> misalignedFiles;
0035   for (const auto &f : pSet.getParameter<std::vector<std::string> >("MisalignedFiles"))
0036     misalignedFiles.push_back(edm::FileInPath(f).fullPath());
0037   PrepareSequence("Misaligned", acsMisaligned, misalignedFiles);
0038 }
0039 
0040 //----------------------------------------------------------------------------------------------------
0041 
0042 CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::~CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon() {}
0043 
0044 //----------------------------------------------------------------------------------------------------
0045 
0046 CTPPSRPAlignmentCorrectionsDataSequence CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::Merge(
0047     const std::vector<CTPPSRPAlignmentCorrectionsDataSequence> &seqs) const {
0048   // find interval boundaries
0049   std::map<edm::EventID, std::vector<std::pair<bool, const CTPPSRPAlignmentCorrectionsData *> > > bounds;
0050 
0051   for (const auto &seq : seqs) {
0052     for (const auto &p : seq) {
0053       const edm::ValidityInterval &iov = p.first;
0054       const CTPPSRPAlignmentCorrectionsData *corr = &p.second;
0055 
0056       const edm::EventID &event_first = iov.first().eventID();
0057       bounds[event_first].emplace_back(std::pair<bool, const CTPPSRPAlignmentCorrectionsData *>(true, corr));
0058 
0059       const edm::EventID &event_after = nextLS(iov.last().eventID());
0060       bounds[event_after].emplace_back(std::pair<bool, const CTPPSRPAlignmentCorrectionsData *>(false, corr));
0061     }
0062   }
0063 
0064   // build correction sums per interval
0065   std::set<const CTPPSRPAlignmentCorrectionsData *> accumulator;
0066   CTPPSRPAlignmentCorrectionsDataSequence result;
0067   for (std::map<edm::EventID, std::vector<std::pair<bool, const CTPPSRPAlignmentCorrectionsData *> > >::const_iterator
0068            tit = bounds.begin();
0069        tit != bounds.end();
0070        ++tit) {
0071     for (const auto &cit : tit->second) {
0072       bool add = cit.first;
0073       const CTPPSRPAlignmentCorrectionsData *corr = cit.second;
0074 
0075       if (add)
0076         accumulator.insert(corr);
0077       else
0078         accumulator.erase(corr);
0079     }
0080 
0081     auto tit_next = tit;
0082     tit_next++;
0083     if (tit_next == bounds.end())
0084       break;
0085 
0086     const edm::EventID &event_first = tit->first;
0087     const edm::EventID &event_last = previousLS(tit_next->first);
0088 
0089     if (verbosity) {
0090       edm::LogInfo("PPS") << "    first="
0091                           << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(edm::IOVSyncValue(event_first))
0092                           << ", last="
0093                           << CTPPSRPAlignmentCorrectionsMethods::iovValueToString(edm::IOVSyncValue(event_last))
0094                           << ": alignment blocks " << accumulator.size();
0095     }
0096 
0097     CTPPSRPAlignmentCorrectionsData corr_sum;
0098     for (auto sit : accumulator)
0099       corr_sum.addCorrections(*sit);
0100 
0101     result.insert(edm::ValidityInterval(edm::IOVSyncValue(event_first), edm::IOVSyncValue(event_last)), corr_sum);
0102   }
0103 
0104   return result;
0105 }
0106 
0107 //----------------------------------------------------------------------------------------------------
0108 
0109 void CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::PrepareSequence(const std::string &label,
0110                                                                        CTPPSRPAlignmentCorrectionsDataSequence &seq,
0111                                                                        const std::vector<std::string> &files) const {
0112   if (verbosity)
0113     edm::LogInfo("PPS") << "PrepareSequence(" << label << ")";
0114 
0115   std::vector<CTPPSRPAlignmentCorrectionsDataSequence> sequences;
0116   sequences.reserve(files.size());
0117   for (const auto &file : files)
0118     sequences.emplace_back(CTPPSRPAlignmentCorrectionsMethods::loadFromXML(file));
0119 
0120   seq = Merge(sequences);
0121 }
0122 
0123 //----------------------------------------------------------------------------------------------------
0124 
0125 edm::EventID CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::previousLS(const edm::EventID &src) {
0126   if (src.run() == edm::EventID::maxRunNumber() && src.luminosityBlock() == edm::EventID::maxLuminosityBlockNumber())
0127     return src;
0128 
0129   if (src.luminosityBlock() == 0)
0130     return edm::EventID(src.run() - 1, edm::EventID::maxLuminosityBlockNumber(), src.event());
0131 
0132   return edm::EventID(src.run(), src.luminosityBlock() - 1, src.event());
0133 }
0134 
0135 //----------------------------------------------------------------------------------------------------
0136 
0137 edm::EventID CTPPSRPAlignmentCorrectionsDataESSourceXMLCommon::nextLS(const edm::EventID &src) {
0138   if (src.luminosityBlock() == edm::EventID::maxLuminosityBlockNumber()) {
0139     if (src.run() == edm::EventID::maxRunNumber())
0140       return src;
0141 
0142     return edm::EventID(src.run() + 1, 0, src.event());
0143   }
0144 
0145   return edm::EventID(src.run(), src.luminosityBlock() + 1, src.event());
0146 }