Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-12 09:07:19

0001 /*!
0002   \file DropBoxMetadata_PayloadInspector
0003   \Payload Inspector Plugin for DropBoxMetadata
0004   \author M. Musich
0005   \version $Revision: 1.0 $
0006   \date $Date: 2018/03/18 10:01:00 $
0007 */
0008 
0009 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0010 #include "CondCore/Utilities/interface/PayloadInspector.h"
0011 #include "CondCore/CondDB/interface/Time.h"
0012 
0013 // the data format of the condition to be inspected
0014 #include "CondFormats/Common/interface/DropBoxMetadata.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 // helper classes
0018 #include "CondCore/PhysicsToolsPlugins/interface/DropBoxMetaDataPayloadInspectorHelper.h"
0019 
0020 // system includes
0021 #include <memory>
0022 #include <sstream>
0023 #include <iostream>
0024 #include <boost/algorithm/string/replace.hpp>
0025 
0026 // include ROOT
0027 #include "TProfile.h"
0028 #include "TH2F.h"
0029 #include "TLegend.h"
0030 #include "TCanvas.h"
0031 #include "TLine.h"
0032 #include "TStyle.h"
0033 #include "TLatex.h"
0034 #include "TPave.h"
0035 #include "TPaveStats.h"
0036 #include "TPaletteAxis.h"
0037 
0038 namespace {
0039 
0040   using namespace cond::payloadInspector;
0041 
0042   /************************************************
0043      DropBoxMetadata Payload Inspector of 1 IOV 
0044   *************************************************/
0045   class DropBoxMetadataTest : public Histogram1D<DropBoxMetadata, SINGLE_IOV> {
0046   public:
0047     DropBoxMetadataTest()
0048         : Histogram1D<DropBoxMetadata, SINGLE_IOV>("Test DropBoxMetadata", "Test DropBoxMetadata", 1, 0.0, 1.0) {}
0049 
0050     bool fill() override {
0051       auto tag = PlotBase::getTag<0>();
0052       for (auto const& iov : tag.iovs) {
0053         std::shared_ptr<DropBoxMetadata> payload = Base::fetchPayload(std::get<1>(iov));
0054         if (payload.get()) {
0055           std::vector<std::string> records = payload->getAllRecords();
0056           for (const auto& record : records) {
0057             edm::LogPrint("DropBoxMetadata_PayloadInspector") << "record: " << record << std::endl;
0058             const auto& parameters = payload->getRecordParameters(record);
0059             const auto& recordParams = parameters.getParameterMap();
0060             for (const auto& [key, val] : recordParams) {
0061               if (val.find("&quot;") != std::string::npos) {
0062                 const auto& replaced = replaceAll(val, std::string("&quot;"), std::string("'"));
0063                 edm::LogPrint("DropBoxMetadata_PayloadInspector") << key << " : " << replaced << std::endl;
0064               } else {
0065                 edm::LogPrint("DropBoxMetadata_PayloadInspector") << key << " : " << val << std::endl;
0066               }
0067             }
0068           }
0069         }
0070       }
0071       return true;
0072     }
0073 
0074   private:
0075     std::string replaceAll(std::string str, const std::string& from, const std::string& to) {
0076       size_t start_pos = 0;
0077       while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
0078         str.replace(start_pos, from.length(), to);
0079         start_pos += to.length();  // Handles case where 'to' is a substring of 'from'
0080       }
0081       return str;
0082     }
0083   };
0084 
0085   /************************************************
0086      DropBoxMetadata Payload Inspector of 1 IOV 
0087   *************************************************/
0088   class DropBoxMetadata_Display : public PlotImage<DropBoxMetadata, SINGLE_IOV> {
0089   public:
0090     DropBoxMetadata_Display() : PlotImage<DropBoxMetadata, SINGLE_IOV>("DropBoxMetadata Display of contents") {}
0091 
0092     bool fill() override {
0093       auto tag = PlotBase::getTag<0>();
0094       auto iov = tag.iovs.front();
0095       std::shared_ptr<DropBoxMetadata> payload = fetchPayload(std::get<1>(iov));
0096 
0097       std::vector<std::string> records = payload->getAllRecords();
0098 
0099       DBoxMetadataHelper::recordMap theRecordMap;
0100       for (const auto& record : records) {
0101         edm::LogPrint("DropBoxMetadata_PayloadInspector") << "record: " << record << std::endl;
0102         const auto& parameters = payload->getRecordParameters(record);
0103         theRecordMap.insert(std::make_pair(record, DBoxMetadataHelper::RecordMetaDataInfo(parameters)));
0104       }
0105 
0106       DBoxMetadataHelper::DBMetaDataTableDisplay theDisplay(theRecordMap);
0107       theDisplay.printMetaDatas();
0108 
0109       DBoxMetadataHelper::DBMetaDataPlotDisplay thePlot(theRecordMap, tag.name, std::to_string(std::get<0>(iov)));
0110       thePlot.setImageFileName(this->m_imageFileName);
0111       thePlot.plotMetaDatas();
0112 
0113       return true;
0114     }
0115   };
0116 
0117   /************************************************
0118      DropBoxMetadata Payload Comparator of 2 IOVs 
0119    *************************************************/
0120   template <IOVMultiplicity nIOVs, int ntags>
0121   class DropBoxMetadata_CompareBase : public PlotImage<DropBoxMetadata, nIOVs, ntags> {
0122   public:
0123     DropBoxMetadata_CompareBase()
0124         : PlotImage<DropBoxMetadata, nIOVs, ntags>("DropBoxMetadata comparison of contents") {}
0125 
0126     bool fill() override {
0127       // trick to deal with the multi-ioved tag and two tag case at the same time
0128       auto theIOVs = PlotBase::getTag<0>().iovs;
0129       auto f_tagname = PlotBase::getTag<0>().name;
0130       std::string l_tagname = "";
0131       auto firstiov = theIOVs.front();
0132       std::tuple<cond::Time_t, cond::Hash> lastiov;
0133 
0134       // we don't support (yet) comparison with more than 2 tags
0135       assert(this->m_plotAnnotations.ntags < 3);
0136 
0137       if (this->m_plotAnnotations.ntags == 2) {
0138         auto tag2iovs = PlotBase::getTag<1>().iovs;
0139         l_tagname = PlotBase::getTag<1>().name;
0140         lastiov = tag2iovs.front();
0141       } else {
0142         lastiov = theIOVs.back();
0143       }
0144 
0145       std::shared_ptr<DropBoxMetadata> last_payload = this->fetchPayload(std::get<1>(lastiov));
0146       std::shared_ptr<DropBoxMetadata> first_payload = this->fetchPayload(std::get<1>(firstiov));
0147 
0148       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
0149       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
0150 
0151       // first payload
0152       std::vector<std::string> f_records = first_payload->getAllRecords();
0153       DBoxMetadataHelper::recordMap f_theRecordMap;
0154       for (const auto& record : f_records) {
0155         //edm::LogPrint("DropBoxMetadata_PayloadInspector") << "record: " << record << std::endl;
0156         const auto& parameters = first_payload->getRecordParameters(record);
0157         f_theRecordMap.insert(std::make_pair(record, DBoxMetadataHelper::RecordMetaDataInfo(parameters)));
0158       }
0159 
0160       DBoxMetadataHelper::DBMetaDataTableDisplay f_theDisplay(f_theRecordMap);
0161       //f_theDisplay.printMetaDatas();
0162 
0163       // last payload
0164       std::vector<std::string> l_records = last_payload->getAllRecords();
0165       DBoxMetadataHelper::recordMap l_theRecordMap;
0166       for (const auto& record : l_records) {
0167         //edm::LogPrint("DropBoxMetadata_PayloadInspector") << "record: " << record << std::endl;
0168         const auto& parameters = last_payload->getRecordParameters(record);
0169         l_theRecordMap.insert(std::make_pair(record, DBoxMetadataHelper::RecordMetaDataInfo(parameters)));
0170       }
0171 
0172       DBoxMetadataHelper::DBMetaDataTableDisplay l_theDisplay(l_theRecordMap);
0173       //l_theDisplay.printMetaDatas();
0174 
0175       l_theDisplay.printDiffWithMetadata(f_theRecordMap);
0176 
0177       // In case of only one tag, use f_tagname for both target and reference
0178       std::string tmpTagName = l_tagname;
0179       if (tmpTagName.empty())
0180         tmpTagName = f_tagname;
0181       DBoxMetadataHelper::DBMetaDataPlotDisplay thePlot(l_theRecordMap, tmpTagName, lastIOVsince);
0182       thePlot.setImageFileName(this->m_imageFileName);
0183       thePlot.plotDiffWithMetadata(f_theRecordMap, f_tagname, firstIOVsince);
0184 
0185       return true;
0186     }
0187   };
0188 
0189   using DropBoxMetadata_Compare = DropBoxMetadata_CompareBase<MULTI_IOV, 1>;
0190   using DropBoxMetadata_CompareTwoTags = DropBoxMetadata_CompareBase<SINGLE_IOV, 2>;
0191 
0192 }  // namespace
0193 
0194 PAYLOAD_INSPECTOR_MODULE(DropBoxMetadata) {
0195   PAYLOAD_INSPECTOR_CLASS(DropBoxMetadataTest);
0196   PAYLOAD_INSPECTOR_CLASS(DropBoxMetadata_Display);
0197   PAYLOAD_INSPECTOR_CLASS(DropBoxMetadata_Compare);
0198   PAYLOAD_INSPECTOR_CLASS(DropBoxMetadata_CompareTwoTags);
0199 }