Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:42

0001 #ifndef CondCore_L1TPlugins_L1TUtmTriggerMenuPayloadInspectorHelper_H
0002 #define CondCore_L1TPlugins_L1TUtmTriggerMenuPayloadInspectorHelper_H
0003 
0004 #include "TH1.h"
0005 #include "TH2.h"
0006 #include "TStyle.h"
0007 #include "TCanvas.h"
0008 #include "TLatex.h"
0009 #include "TLine.h"
0010 
0011 #include "CondFormats/L1TObjects/interface/L1TUtmTriggerMenu.h"
0012 
0013 namespace L1TUtmTriggerMenuInspectorHelper {
0014 
0015   using l1tUtmAlgoMap = std::map<std::string, L1TUtmAlgorithm>;
0016   using l1tUtmConditionMap = std::map<std::string, L1TUtmCondition>;
0017 
0018   class L1UtmTriggerMenuInfo {
0019   public:
0020     // constructor
0021     L1UtmTriggerMenuInfo(const L1TUtmTriggerMenu* l1utmMenu) {
0022       m_algoMap = l1utmMenu->getAlgorithmMap();
0023       m_condMap = l1utmMenu->getConditionMap();
0024     }
0025 
0026     // destructor
0027     ~L1UtmTriggerMenuInfo() = default;
0028 
0029   public:
0030     //___________________________________________________________________
0031     const std::vector<std::string> listOfAlgos() const {
0032       std::vector<std::string> output;
0033       std::transform(m_algoMap.begin(),
0034                      m_algoMap.end(),
0035                      std::back_inserter(output),
0036                      [](const std::pair<std::string, L1TUtmAlgorithm>& pair) {
0037                        return pair.first;  // Extracting the string key using lambda
0038                      });
0039       return output;
0040     }
0041 
0042     //___________________________________________________________________
0043     const std::vector<std::string> listOfConditions() const {
0044       std::vector<std::string> output;
0045       std::transform(m_condMap.begin(),
0046                      m_condMap.end(),
0047                      std::back_inserter(output),
0048                      [](const std::pair<std::string, L1TUtmCondition>& pair) {
0049                        return pair.first;  // Extracting the string key using lambda
0050                      });
0051       return output;
0052     }
0053 
0054     //___________________________________________________________________
0055     template <typename T>
0056     const std::vector<std::string> listOfCommonKeys(const L1TUtmTriggerMenu* other) const {
0057       const auto& otherMap = getOtherMap<T>(other);
0058       const auto& thisMap = getThisMap<T>();
0059 
0060       std::vector<std::string> commonKeys;
0061 
0062       // Lambda function to find common keys and store them in commonKeys vector
0063       std::for_each(thisMap.begin(), thisMap.end(), [&commonKeys, &otherMap](const std::pair<std::string, T>& pair) {
0064         const std::string& key = pair.first;
0065 
0066         // Check if the key exists in map2
0067         if (otherMap.find(key) != otherMap.end()) {
0068           commonKeys.push_back(key);
0069         }
0070       });
0071       return commonKeys;
0072     }
0073 
0074     //___________________________________________________________________
0075     template <typename T>
0076     const std::vector<std::string> onlyInThis(const L1TUtmTriggerMenu* other) const {
0077       const auto& otherMap = getOtherMap<T>(other);
0078       const auto& thisMap = getThisMap<T>();
0079 
0080       std::vector<std::string> stringsOnlyInFirstMap;
0081 
0082       // Lambda function to extract only the strings present in thisMap but not in otherMap
0083       std::for_each(
0084           thisMap.begin(), thisMap.end(), [&stringsOnlyInFirstMap, &otherMap](const std::pair<std::string, T>& pair) {
0085             const std::string& key = pair.first;
0086             // Check if the key exists in otherMap
0087             if (otherMap.find(key) == otherMap.end()) {
0088               stringsOnlyInFirstMap.push_back(key);  // Add key to the vector
0089             }
0090           });
0091 
0092       return stringsOnlyInFirstMap;
0093     }
0094 
0095     //___________________________________________________________________
0096     template <typename T>
0097     const std::vector<std::string> onlyInOther(const L1TUtmTriggerMenu* other) const {
0098       const auto& otherMap = getOtherMap<T>(other);
0099       const auto& thisMap = getThisMap<T>();
0100 
0101       std::vector<std::string> stringsOnlyInSecondMap;
0102 
0103       // Lambda function capturing 'this' to access the member variable 'thisMap'
0104       std::for_each(
0105           otherMap.begin(), otherMap.end(), [thisMap, &stringsOnlyInSecondMap](const std::pair<std::string, T>& pair) {
0106             const std::string& key = pair.first;
0107 
0108             // Check if the key exists in thisMap
0109             if (thisMap.find(key) == thisMap.end()) {
0110               stringsOnlyInSecondMap.push_back(key);  // Add key to the vector
0111             }
0112           });
0113 
0114       return stringsOnlyInSecondMap;
0115     }
0116 
0117   private:
0118     l1tUtmAlgoMap m_algoMap;
0119     l1tUtmConditionMap m_condMap;
0120 
0121     // Helper function to get otherMap based on T
0122     template <typename T>
0123     decltype(auto) getOtherMap(const L1TUtmTriggerMenu* other) const {
0124       if constexpr (std::is_same<T, L1TUtmCondition>::value) {
0125         return other->getConditionMap();
0126       } else {
0127         return other->getAlgorithmMap();
0128       }
0129     }
0130 
0131     // Helper function to get this Map based on T
0132     template <typename T>
0133     decltype(auto) getThisMap() const {
0134       if constexpr (std::is_same<T, L1TUtmCondition>::value) {
0135         return m_condMap;
0136       } else {
0137         return m_algoMap;
0138       }
0139     }
0140   };
0141 
0142   template <typename T>
0143   class L1TUtmTriggerMenuDisplay {
0144   public:
0145     L1TUtmTriggerMenuDisplay(const L1TUtmTriggerMenu* thisMenu, std::string theTag, std::string theIOV)
0146         : m_info(thisMenu), m_tagName(theTag), m_IOVsinceDisplay(theIOV) {}
0147     ~L1TUtmTriggerMenuDisplay() = default;
0148 
0149     void setImageFileName(const std::string& theFileName) {
0150       m_imageFileName = theFileName;
0151       return;
0152     }
0153 
0154     // Function to set label based on the type T
0155     std::string getLabel() const;
0156 
0157     //___________________________________________________________________
0158     void plotDiffWithOtherMenu(const L1TUtmTriggerMenu* other, std::string theRefTag, std::string theRefIOV) {
0159       const auto& vec_only_in_this = m_info.template onlyInThis<T>(other);
0160       const auto& vec_only_in_other = m_info.template onlyInOther<T>(other);
0161 
0162       // preparations for plotting
0163       // starting table at y=1.0 (top of the canvas)
0164       // first column is at 0.03, second column at 0.22 NDC
0165       unsigned int mapsize = vec_only_in_this.size() + vec_only_in_other.size();
0166       float pitch = 1. / (mapsize * 1.1);
0167       float y, x1, x2;
0168       std::vector<float> y_x1, y_x2, y_line;
0169       std::vector<std::string> s_x1, s_x2, s_x3;
0170       y = 1.0;
0171       x1 = 0.02;
0172       x2 = x1 + 0.37;
0173       y -= pitch;
0174 
0175       // title for plot
0176       y_x1.push_back(y);
0177       s_x1.push_back(getLabel());
0178       y_x2.push_back(y);
0179       s_x2.push_back("#scale[1.1]{Target tag / IOV: #color[2]{" + m_tagName + "} / " + m_IOVsinceDisplay + "}");
0180 
0181       y -= pitch;
0182       y_x1.push_back(y);
0183       s_x1.push_back("");
0184       y_x2.push_back(y);
0185       s_x2.push_back("#scale[1.1]{Refer  tag / IOV: #color[4]{" + theRefTag + "} / " + theRefIOV + "}");
0186 
0187       y -= pitch / 2.;
0188       y_line.push_back(y);
0189 
0190       // First, check if there are records in reference which are not in target
0191       for (const auto& ref : vec_only_in_other) {
0192         y -= pitch;
0193         y_x1.push_back(y);
0194         s_x1.push_back("#scale[0.7]{" + ref + "}");
0195         y_x2.push_back(y);
0196         s_x2.push_back("#color[4]{#bf{Only in reference, not in target.}}");
0197         y_line.push_back(y - (pitch / 2.));
0198       }
0199 
0200       // Second, check if there are records in target which are not in reference
0201       for (const auto& tar : vec_only_in_this) {
0202         y -= pitch;
0203         y_x1.push_back(y);
0204         s_x1.push_back("#scale[0.7]{" + tar + "}");
0205         y_x2.push_back(y);
0206         s_x2.push_back("#color[2]{#bf{Only in target, not in reference.}}");
0207         y_line.push_back(y - (pitch / 2.));
0208       }
0209 
0210       // Finally, print text to TCanvas
0211       TCanvas canvas("L1TUtmMenuData", "L1TUtmMenuData", 2000, std::max(y_x1.size(), y_x2.size()) * 40);
0212       TLatex l;
0213       // Draw the columns titles
0214       l.SetTextAlign(12);
0215 
0216       float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.1);
0217       float factor = newpitch / pitch;
0218       l.SetTextSize(newpitch - 0.002);
0219       canvas.cd();
0220       for (unsigned int i = 0; i < y_x1.size(); i++) {
0221         l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
0222       }
0223 
0224       for (unsigned int i = 0; i < y_x2.size(); i++) {
0225         l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
0226       }
0227 
0228       canvas.cd();
0229       canvas.Update();
0230 
0231       // Draw horizontal lines separating records
0232       TLine lines[y_line.size()];
0233       unsigned int iL = 0;
0234       for (const auto& line : y_line) {
0235         lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
0236         lines[iL].SetLineWidth(1);
0237         lines[iL].SetLineStyle(9);
0238         lines[iL].SetLineColor(2);
0239         lines[iL].Draw("same");
0240         iL++;
0241       }
0242 
0243       std::string fileName("L1UtmMenuData_Compare.png");
0244       if (!m_imageFileName.empty())
0245         fileName = m_imageFileName;
0246       canvas.SaveAs(fileName.c_str());
0247     }
0248 
0249   private:
0250     L1UtmTriggerMenuInfo m_info;    //!< map of the record / metadata associations
0251     std::string m_tagName;          //!< tag name
0252     std::string m_IOVsinceDisplay;  //!< iov since
0253     std::string m_imageFileName;    //!< image file name
0254   };
0255 
0256   // Explicit specialization outside the class
0257   template <>
0258   std::string L1TUtmTriggerMenuDisplay<L1TUtmCondition>::getLabel() const {
0259     return "#scale[1.1]{Condition Name}";
0260   }
0261 
0262   template <>
0263   std::string L1TUtmTriggerMenuDisplay<L1TUtmAlgorithm>::getLabel() const {
0264     return "#scale[1.1]{Algo Name}";
0265   }
0266 }  // namespace L1TUtmTriggerMenuInspectorHelper
0267 
0268 #endif