Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-04 03:24:30

0001 #include "CondCore/Utilities/interface/PayloadInspectorModule.h"
0002 #include "CondCore/Utilities/interface/PayloadInspector.h"
0003 #include "CondCore/CondDB/interface/Time.h"
0004 #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
0005 
0006 #include <fmt/printf.h>
0007 #include <memory>
0008 #include <sstream>
0009 #include <iostream>
0010 #include "TCanvas.h"
0011 #include "TLatex.h"
0012 #include "TLine.h"
0013 
0014 namespace {
0015 
0016   using namespace cond::payloadInspector;
0017 
0018   /************************************************
0019     Display AlCaRecoTriggerBits mapping
0020   *************************************************/
0021   class AlCaRecoTriggerBits_Display : public PlotImage<AlCaRecoTriggerBits, SINGLE_IOV> {
0022   public:
0023     AlCaRecoTriggerBits_Display() : PlotImage<AlCaRecoTriggerBits, SINGLE_IOV>("Table of AlCaRecoTriggerBits") {}
0024 
0025     bool fill() override {
0026       auto tag = PlotBase::getTag<0>();
0027       auto iov = tag.iovs.front();
0028       std::shared_ptr<AlCaRecoTriggerBits> payload = fetchPayload(std::get<1>(iov));
0029 
0030       std::string IOVsince = std::to_string(std::get<0>(iov));
0031 
0032       // Get map of strings to concatenated list of names of HLT paths:
0033       typedef std::map<std::string, std::string> TriggerMap;
0034       const TriggerMap &triggerMap = payload->m_alcarecoToTrig;
0035 
0036       unsigned int mapsize = triggerMap.size();
0037       float pitch = 1. / (mapsize * 1.1);
0038 
0039       float y, x1, x2;
0040       std::vector<float> y_x1, y_x2, y_line;
0041       std::vector<std::string> s_x1, s_x2, s_x3;
0042 
0043       // starting table at y=1.0 (top of the canvas)
0044       // first column is at 0.02, second column at 0.32 NDC
0045       y = 1.0;
0046       x1 = 0.02;
0047       x2 = x1 + 0.30;
0048 
0049       y -= pitch;
0050       y_x1.push_back(y);
0051       s_x1.push_back("#scale[1.2]{Key}");
0052       y_x2.push_back(y);
0053       s_x2.push_back("#scale[1.2]{tag: " + tag.name + " in IOV: " + IOVsince + "}");
0054 
0055       y -= pitch / 2.;
0056       y_line.push_back(y);
0057 
0058       for (const auto &element : triggerMap) {
0059         y -= pitch;
0060         y_x1.push_back(y);
0061         s_x1.push_back(element.first);
0062 
0063         std::vector<std::string> output;
0064         std::string toAppend = "";
0065         const std::vector<std::string> paths = payload->decompose(element.second);
0066         for (unsigned int iPath = 0; iPath < paths.size(); ++iPath) {
0067           // if the line to be added has less than 60 chars append to current
0068           if ((toAppend + paths[iPath]).length() < 60) {
0069             toAppend += paths[iPath] + ";";
0070           } else {
0071             // else if the line exceeds 60 chars, dump in the vector and resume from scratch
0072             output.push_back(toAppend);
0073             toAppend.clear();
0074             toAppend += paths[iPath] + ";";
0075           }
0076           // if it's the last, dump it
0077           if (iPath == paths.size() - 1)
0078             output.push_back(toAppend);
0079         }
0080 
0081         for (unsigned int br = 0; br < output.size(); br++) {
0082           y_x2.push_back(y);
0083           s_x2.push_back("#color[2]{" + output[br] + "}");
0084           if (br != output.size() - 1)
0085             y -= pitch;
0086         }
0087 
0088         y_line.push_back(y - (pitch / 2.));
0089       }
0090 
0091       TCanvas canvas("AlCaRecoTriggerBits", "AlCaRecoTriggerBits", 2000, std::max(y_x1.size(), y_x2.size()) * 40);
0092       TLatex l;
0093       // Draw the columns titles
0094       l.SetTextAlign(12);
0095 
0096       float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.1);
0097       float factor = newpitch / pitch;
0098       l.SetTextSize(newpitch - 0.002);
0099       canvas.cd();
0100       for (unsigned int i = 0; i < y_x1.size(); i++) {
0101         l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
0102       }
0103 
0104       for (unsigned int i = 0; i < y_x2.size(); i++) {
0105         l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
0106       }
0107 
0108       canvas.cd();
0109       canvas.Update();
0110 
0111       TLine lines[y_line.size()];
0112       unsigned int iL = 0;
0113       for (const auto &line : y_line) {
0114         lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
0115         lines[iL].SetLineWidth(1);
0116         lines[iL].SetLineStyle(9);
0117         lines[iL].SetLineColor(2);
0118         lines[iL].Draw("same");
0119         iL++;
0120       }
0121 
0122       std::string fileName(m_imageFileName);
0123       canvas.SaveAs(fileName.c_str());
0124       return true;
0125     }
0126   };
0127 
0128   /************************************************
0129     Compare AlCaRecoTriggerBits mapping
0130   *************************************************/
0131   template <IOVMultiplicity nIOVs, int ntags>
0132   class AlCaRecoTriggerBits_CompareBase : public PlotImage<AlCaRecoTriggerBits, nIOVs, ntags> {
0133   public:
0134     AlCaRecoTriggerBits_CompareBase()
0135         : PlotImage<AlCaRecoTriggerBits, nIOVs, ntags>("Table of AlCaRecoTriggerBits comparison") {}
0136 
0137     bool fill() override {
0138       // trick to deal with the multi-ioved tag and two tag case at the same time
0139       auto theIOVs = PlotBase::getTag<0>().iovs;
0140       auto f_tagname = PlotBase::getTag<0>().name;
0141       std::string l_tagname = "";
0142       auto firstiov = theIOVs.front();
0143       std::tuple<cond::Time_t, cond::Hash> lastiov;
0144 
0145       // we don't support (yet) comparison with more than 2 tags
0146       assert(this->m_plotAnnotations.ntags < 3);
0147 
0148       if (this->m_plotAnnotations.ntags == 2) {
0149         auto tag2iovs = PlotBase::getTag<1>().iovs;
0150         l_tagname = PlotBase::getTag<1>().name;
0151         lastiov = tag2iovs.front();
0152       } else {
0153         lastiov = theIOVs.back();
0154       }
0155 
0156       std::shared_ptr<AlCaRecoTriggerBits> last_payload = this->fetchPayload(std::get<1>(lastiov));
0157       std::shared_ptr<AlCaRecoTriggerBits> first_payload = this->fetchPayload(std::get<1>(firstiov));
0158 
0159       std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
0160       std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
0161 
0162       // Get map of strings to concatenated list of names of HLT paths:
0163       typedef std::map<std::string, std::string> TriggerMap;
0164       const TriggerMap &first_triggerMap = first_payload->m_alcarecoToTrig;
0165       const TriggerMap &last_triggerMap = last_payload->m_alcarecoToTrig;
0166 
0167       std::vector<std::string> first_keys, not_in_first_keys;
0168       std::vector<std::string> last_keys, not_in_last_keys;
0169 
0170       // fill the vector of first keys
0171       for (const auto &element : first_triggerMap) {
0172         first_keys.push_back(element.first);
0173       }
0174 
0175       // fill the vector of last keys
0176       for (const auto &element : last_triggerMap) {
0177         last_keys.push_back(element.first);
0178       }
0179 
0180       // find the elements not in common
0181       std::set_difference(first_keys.begin(),
0182                           first_keys.end(),
0183                           last_keys.begin(),
0184                           last_keys.end(),
0185                           std::inserter(not_in_last_keys, not_in_last_keys.begin()));
0186 
0187       std::set_difference(last_keys.begin(),
0188                           last_keys.end(),
0189                           first_keys.begin(),
0190                           first_keys.end(),
0191                           std::inserter(not_in_first_keys, not_in_first_keys.begin()));
0192 
0193       float pitch = 0.013;
0194       float y, x1, x2, x3;
0195 
0196       std::vector<float> y_x1, y_x2, y_x3, y_line;
0197       std::vector<std::string> s_x1, s_x2, s_x3;
0198 
0199       y = 1.0;
0200       x1 = 0.02;
0201       x2 = x1 + 0.20;
0202       x3 = x2 + 0.30;
0203 
0204       y -= pitch;
0205       y_x1.push_back(y);
0206       s_x1.push_back("#scale[1.2]{Key}");
0207       y_x2.push_back(y);
0208       s_x2.push_back(fmt::sprintf("#scale[1.2]{%s in IOV: %s}", f_tagname, firstIOVsince));
0209       y_x3.push_back(y);
0210       s_x3.push_back(fmt::sprintf("#scale[1.2]{%s in IOV: %s}", l_tagname, lastIOVsince));
0211       y -= pitch / 3;
0212 
0213       // print the ones missing in the last key
0214       for (const auto &key : not_in_last_keys) {
0215         y -= pitch;
0216         y_x1.push_back(y);
0217         s_x1.push_back(key);
0218 
0219         const std::vector<std::string> missing_in_last_paths = first_payload->decompose(first_triggerMap.at(key));
0220 
0221         std::vector<std::string> output;
0222         std::string toAppend = "";
0223         for (unsigned int iPath = 0; iPath < missing_in_last_paths.size(); ++iPath) {
0224           // if the line to be added has less than 60 chars append to current
0225           if ((toAppend + missing_in_last_paths[iPath]).length() < 60) {
0226             toAppend += missing_in_last_paths[iPath] + ";";
0227           } else {
0228             // else if the line exceeds 60 chars, dump in the vector and resume from scratch
0229             output.push_back(toAppend);
0230             toAppend.clear();
0231             toAppend += missing_in_last_paths[iPath] + ";";
0232           }
0233           // if it's the last, dump it
0234           if (iPath == missing_in_last_paths.size() - 1)
0235             output.push_back(toAppend);
0236         }
0237 
0238         for (unsigned int br = 0; br < output.size(); br++) {
0239           y_x2.push_back(y);
0240           s_x2.push_back("#color[2]{" + output[br] + "}");
0241           if (br != output.size() - 1)
0242             y -= pitch;
0243         }
0244         y_line.push_back(y - 0.008);
0245       }
0246 
0247       // print the ones missing in the first key
0248       for (const auto &key : not_in_first_keys) {
0249         y -= pitch;
0250         y_x1.push_back(y);
0251         s_x1.push_back(key);
0252         const std::vector<std::string> missing_in_first_paths = last_payload->decompose(last_triggerMap.at(key));
0253 
0254         std::vector<std::string> output;
0255         std::string toAppend = "";
0256         for (unsigned int iPath = 0; iPath < missing_in_first_paths.size(); ++iPath) {
0257           // if the line to be added has less than 60 chars append to current
0258           if ((toAppend + missing_in_first_paths[iPath]).length() < 60) {
0259             toAppend += missing_in_first_paths[iPath] + ";";
0260           } else {
0261             // else if the line exceeds 60 chars, dump in the vector and resume from scratch
0262             output.push_back(toAppend);
0263             toAppend.clear();
0264             toAppend += missing_in_first_paths[iPath] + ";";
0265           }
0266           // if it's the last, dump it
0267           if (iPath == missing_in_first_paths.size() - 1)
0268             output.push_back(toAppend);
0269         }
0270 
0271         for (unsigned int br = 0; br < output.size(); br++) {
0272           y_x3.push_back(y);
0273           s_x3.push_back("#color[4]{" + output[br] + "}");
0274           if (br != output.size() - 1)
0275             y -= pitch;
0276         }
0277         y_line.push_back(y - 0.008);
0278       }
0279 
0280       for (const auto &element : first_triggerMap) {
0281         if (last_triggerMap.find(element.first) != last_triggerMap.end()) {
0282           auto lastElement = last_triggerMap.find(element.first);
0283 
0284           std::string output;
0285           const std::vector<std::string> first_paths = first_payload->decompose(element.second);
0286           const std::vector<std::string> last_paths = last_payload->decompose(lastElement->second);
0287 
0288           std::vector<std::string> not_in_first;
0289           std::vector<std::string> not_in_last;
0290 
0291           std::set_difference(first_paths.begin(),
0292                               first_paths.end(),
0293                               last_paths.begin(),
0294                               last_paths.end(),
0295                               std::inserter(not_in_last, not_in_last.begin()));
0296 
0297           std::set_difference(last_paths.begin(),
0298                               last_paths.end(),
0299                               first_paths.begin(),
0300                               first_paths.end(),
0301                               std::inserter(not_in_first, not_in_first.begin()));
0302 
0303           if (!not_in_last.empty() || !not_in_first.empty()) {
0304             y -= pitch;
0305             y_x1.push_back(y);
0306             s_x1.push_back(element.first);
0307 
0308             std::vector<std::string> output;
0309             std::string toAppend = "";
0310             for (unsigned int iPath = 0; iPath < not_in_last.size(); ++iPath) {
0311               // if the line to be added has less than 60 chars append to current
0312               if ((toAppend + not_in_last[iPath]).length() < 60) {
0313                 toAppend += not_in_last[iPath] + ";";
0314               } else {
0315                 // else if the line exceeds 60 chars, dump in the vector and resume from scratch
0316                 output.push_back(toAppend);
0317                 toAppend.clear();
0318                 toAppend += not_in_last[iPath] + ";";
0319               }
0320               // if it's the last and not empty, dump it
0321               if (toAppend.length() > 0 && iPath == not_in_last.size() - 1)
0322                 output.push_back(toAppend);
0323             }
0324 
0325             unsigned int count = output.size();
0326 
0327             for (unsigned int br = 0; br < count; br++) {
0328               y_x2.push_back(y - (br * pitch));
0329               s_x2.push_back("#color[6]{" + output[br] + "}");
0330             }
0331 
0332             // clear vector and string
0333             toAppend.clear();
0334             output.clear();
0335             for (unsigned int jPath = 0; jPath < not_in_first.size(); ++jPath) {
0336               // if the line to be added has less than 60 chars append to current
0337               if ((toAppend + not_in_first[jPath]).length() < 60) {
0338                 toAppend += not_in_first[jPath] + ";";
0339               } else {
0340                 // else if the line exceeds 60 chars, dump in the vector and resume from scratch
0341                 output.push_back(toAppend);
0342                 toAppend.clear();
0343                 toAppend += not_in_first[jPath] + ";";
0344               }
0345               // if it's the last and not empty, dump it
0346               if (toAppend.length() > 0 && jPath == not_in_first.size() - 1)
0347                 output.push_back(toAppend);
0348             }
0349 
0350             unsigned int count1 = output.size();
0351 
0352             for (unsigned int br = 0; br < count1; br++) {
0353               y_x3.push_back(y - (br * pitch));
0354               s_x3.push_back("#color[8]{" + output[br] + "}");
0355             }
0356 
0357             // decrease the y position to the maximum of the two lists
0358             y -= (std::max(count, count1) - 1) * pitch;
0359             //y-=count*pitch;
0360             y_line.push_back(y - 0.008);
0361 
0362           }  // close if there is at least a difference
0363         }    // if there is a common key
0364       }      //loop on the keys
0365 
0366       TCanvas canvas("AlCaRecoTriggerBits", "AlCaRecoTriggerBits", 2500., std::max(y_x1.size(), y_x2.size()) * 40);
0367 
0368       TLatex l;
0369       // Draw the columns titles
0370       l.SetTextAlign(12);
0371 
0372       // rescale the width of the table row to fit into the canvas
0373       float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.65);
0374       float factor = newpitch / pitch;
0375       l.SetTextSize(newpitch - 0.002);
0376       canvas.cd();
0377       for (unsigned int i = 0; i < y_x1.size(); i++) {
0378         l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
0379       }
0380 
0381       for (unsigned int i = 0; i < y_x2.size(); i++) {
0382         l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
0383       }
0384 
0385       for (unsigned int i = 0; i < y_x3.size(); i++) {
0386         l.DrawLatexNDC(x3, 1 - (1 - y_x3[i]) * factor, s_x3[i].c_str());
0387       }
0388 
0389       canvas.cd();
0390       canvas.Update();
0391 
0392       TLine lines[y_line.size()];
0393       unsigned int iL = 0;
0394       for (const auto &line : y_line) {
0395         lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
0396         lines[iL].SetLineWidth(1);
0397         lines[iL].SetLineStyle(9);
0398         lines[iL].SetLineColor(2);
0399         lines[iL].Draw("same");
0400         iL++;
0401       }
0402 
0403       //canvas.SetCanvasSize(2000,(1-y)*1000);
0404       std::string fileName(this->m_imageFileName);
0405       canvas.SaveAs(fileName.c_str());
0406       return true;
0407     }
0408   };
0409 
0410   using AlCaRecoTriggerBits_Compare = AlCaRecoTriggerBits_CompareBase<MULTI_IOV, 1>;
0411   using AlCaRecoTriggerBits_CompareTwoTags = AlCaRecoTriggerBits_CompareBase<SINGLE_IOV, 2>;
0412 
0413 }  // namespace
0414 
0415 PAYLOAD_INSPECTOR_MODULE(AlCaRecoTriggerBits) {
0416   PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_Display);
0417   PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_Compare);
0418   PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_CompareTwoTags);
0419 }