Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-04 04:34:52

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       // Calculate the total number of entries
0163       unsigned int mapsize = vec_only_in_this.size() + vec_only_in_other.size();
0164 
0165       // Dynamically calculate the pitch based on the number of entries
0166       float canvasHeight = std::max(800.0f, mapsize * 30.0f);  // Adjust canvas height based on the number of entries
0167       float pitch = 1.0 / (mapsize + 3.0);  // Pitch for spacing between lines (extra space for headers)
0168 
0169       float y = 1.0;
0170       float x1 = 0.02, x2 = x1 + 0.37;
0171       std::vector<float> y_x1, y_x2, y_line;
0172       std::vector<std::string> s_x1, s_x2;
0173 
0174       // Title for plot
0175       y -= pitch;
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.0;
0188       y_line.push_back(y);
0189 
0190       // Records only in reference (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.0));
0198       }
0199 
0200       // Records only in target (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.0));
0208       }
0209 
0210       // Adjust canvas size dynamically
0211       TCanvas canvas("L1TUtmMenuData", "L1TUtmMenuData", 2000, static_cast<int>(canvasHeight));
0212       TLatex l;
0213       l.SetTextAlign(12);
0214 
0215       // Set the text size dynamically based on pitch
0216       float textSize = std::clamp(pitch, 0.015f, 0.035f);
0217       l.SetTextSize(textSize);
0218 
0219       canvas.cd();
0220       for (unsigned int i = 0; i < y_x1.size(); i++) {
0221         l.DrawLatexNDC(x1, y_x1[i], s_x1[i].c_str());
0222       }
0223       for (unsigned int i = 0; i < y_x2.size(); i++) {
0224         l.DrawLatexNDC(x2, y_x2[i], s_x2[i].c_str());
0225       }
0226 
0227       // Draw horizontal lines separating records
0228       TLine lines[y_line.size()];
0229       for (unsigned int i = 0; i < y_line.size(); i++) {
0230         lines[i] = TLine(gPad->GetUxmin(), y_line[i], gPad->GetUxmax(), y_line[i]);
0231         lines[i].SetLineWidth(1);
0232         lines[i].SetLineStyle(9);
0233         lines[i].SetLineColor(2);
0234         lines[i].Draw("same");
0235       }
0236 
0237       // Save the canvas as an image
0238       std::string fileName = "L1UtmMenuData_Compare.png";
0239       if (!m_imageFileName.empty()) {
0240         fileName = m_imageFileName;
0241       }
0242       canvas.SaveAs(fileName.c_str());
0243     }
0244 
0245   private:
0246     L1UtmTriggerMenuInfo m_info;    //!< map of the record / metadata associations
0247     std::string m_tagName;          //!< tag name
0248     std::string m_IOVsinceDisplay;  //!< iov since
0249     std::string m_imageFileName;    //!< image file name
0250   };
0251 
0252   // Explicit specialization outside the class
0253   template <>
0254   std::string L1TUtmTriggerMenuDisplay<L1TUtmCondition>::getLabel() const {
0255     return "#scale[1.1]{Condition Name}";
0256   }
0257 
0258   template <>
0259   std::string L1TUtmTriggerMenuDisplay<L1TUtmAlgorithm>::getLabel() const {
0260     return "#scale[1.1]{Algo Name}";
0261   }
0262 }  // namespace L1TUtmTriggerMenuInspectorHelper
0263 
0264 #endif