From 735c990e59d0416bfd56f932f5cbfbadf0f3b028 Mon Sep 17 00:00:00 2001 From: mmusich Date: Thu, 3 Mar 2022 13:45:10 +0100 Subject: [PATCH 1/5] introduce difference plot for BeamSpotObjects parameters --- .../BeamSpotPayloadInspectorHelper.h | 213 ++++++++++++++++++ .../BeamSpotOnline_PayloadInspector.cc | 11 + .../plugins/BeamSpot_PayloadInspector.cc | 9 + 3 files changed, 233 insertions(+) diff --git a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h index 8460c7aeb7da1..f9ef0176d2647 100644 --- a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h +++ b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h @@ -405,6 +405,219 @@ namespace BeamSpotPI { } } }; + + /************************************************ + Display of Beam Spot parameters difference + *************************************************/ + template + class DisplayParametersDiff : public cond::payloadInspector::PlotImage { + public: + DisplayParametersDiff() + : cond::payloadInspector::PlotImage("Display of BeamSpot parameters differences") { + if constexpr (std::is_same_v) { + isOnline_ = true; + } else { + isOnline_ = false; + } + } + + bool fill() override { + // trick to deal with the multi-ioved tag and two tag case at the same time + auto theIOVs = cond::payloadInspector::PlotBase::getTag<0>().iovs; + auto f_tagname = cond::payloadInspector::PlotBase::getTag<0>().name; + std::string l_tagname = ""; + auto firstiov = theIOVs.front(); + std::tuple lastiov; + + // we don't support (yet) comparison with more than 2 tags + assert(this->m_plotAnnotations.ntags < 3); + + if (this->m_plotAnnotations.ntags == 2) { + auto tag2iovs = cond::payloadInspector::PlotBase::getTag<1>().iovs; + l_tagname = cond::payloadInspector::PlotBase::getTag<1>().name; + lastiov = tag2iovs.front(); + } else { + lastiov = theIOVs.back(); + } + + l_payload = this->fetchPayload(std::get<1>(lastiov)); + f_payload = this->fetchPayload(std::get<1>(firstiov)); + + std::string lastIOVsince = std::to_string(std::get<0>(lastiov)); + std::string firstIOVsince = std::to_string(std::get<0>(firstiov)); + + TCanvas canvas("Beam Spot Parameters Difference Summary", "Beam Spot Parameters Difference summary", 1000, 1000); + canvas.cd(1); + + canvas.cd(1)->SetTopMargin(0.08); + canvas.cd(1)->SetBottomMargin(0.06); + canvas.cd(1)->SetLeftMargin(0.14); + canvas.cd(1)->SetRightMargin(0.16); + canvas.cd(1)->Modified(); + canvas.cd(1)->SetGrid(); + + auto h2_BSParameters = std::make_unique("Parameters", "", 2, 0.0, 2.0, 8, 0, 8.); + auto h2_BSShadow = + std::make_unique("Shadow", ";;;#Delta parameter (payload A - payload B)", 2, 0.0, 2.0, 8, 0, 8.); + h2_BSParameters->SetStats(false); + h2_BSShadow->SetStats(false); + + std::function cutFunctor = [this](parameters my_param, bool isError) { + double ret(-999.); + if (!isError) { + switch (my_param) { + case X: + return (f_payload->x() - l_payload->x()); + case Y: + return (f_payload->y() - l_payload->y()); + case Z: + return (f_payload->z() - l_payload->z()); + case sigmaX: + return (f_payload->beamWidthX() - l_payload->beamWidthX()); + case sigmaY: + return (f_payload->beamWidthY() - l_payload->beamWidthY()); + case sigmaZ: + return (f_payload->sigmaZ() - l_payload->sigmaZ()); + case dxdz: + return (f_payload->dxdz() - l_payload->dxdz()); + case dydz: + return (f_payload->dydz() - l_payload->dydz()); + case END_OF_TYPES: + return ret; + default: + return ret; + } + } else { + switch (my_param) { + case X: + return (f_payload->xError() - l_payload->xError()); + case Y: + return (f_payload->yError() - l_payload->yError()); + case Z: + return (f_payload->zError() - l_payload->zError()); + case sigmaX: + return (f_payload->beamWidthXError() - l_payload->beamWidthXError()); + case sigmaY: + return (f_payload->beamWidthYError() - l_payload->beamWidthYError()); + case sigmaZ: + return (f_payload->sigmaZError() - l_payload->sigmaZError()); + case dxdz: + return (f_payload->dxdzError() - l_payload->dxdzError()); + case dydz: + return (f_payload->dydzError() - l_payload->dydzError()); + case END_OF_TYPES: + return ret; + default: + return ret; + } + } + }; + + h2_BSParameters->GetXaxis()->SetBinLabel(1, "Value"); + h2_BSParameters->GetXaxis()->SetBinLabel(2, "Error"); + h2_BSShadow->GetXaxis()->SetBinLabel(1, "Value"); + h2_BSShadow->GetXaxis()->SetBinLabel(2, "Error"); + + unsigned int yBin = 8; + for (int foo = parameters::X; foo <= parameters::dydz; foo++) { + parameters param = static_cast(foo); + std::string theLabel = getStringFromTypeEnum(param); + h2_BSParameters->GetYaxis()->SetBinLabel(yBin, theLabel.c_str()); + h2_BSParameters->SetBinContent(1, yBin, cutFunctor(param, false)); + h2_BSParameters->SetBinContent(2, yBin, cutFunctor(param, true)); + h2_BSShadow->GetYaxis()->SetBinLabel(yBin, theLabel.c_str()); + h2_BSShadow->SetBinContent(1, yBin, cutFunctor(param, false)); + h2_BSShadow->SetBinContent(2, yBin, cutFunctor(param, true)); + yBin--; + } + + h2_BSParameters->GetXaxis()->LabelsOption("h"); + h2_BSParameters->GetYaxis()->SetLabelSize(0.05); + h2_BSParameters->GetXaxis()->SetLabelSize(0.05); + h2_BSShadow->GetXaxis()->LabelsOption("h"); + h2_BSShadow->GetYaxis()->SetLabelSize(0.05); + h2_BSShadow->GetXaxis()->SetLabelSize(0.05); + h2_BSShadow->GetZaxis()->CenterTitle(); + h2_BSShadow->GetZaxis()->SetTitleOffset(1.5); + h2_BSParameters->SetMarkerSize(1.5); + + // this is the fine gradient palette (blue to red) + double max = h2_BSShadow->GetMaximum(); + double min = h2_BSShadow->GetMinimum(); + double val_white = 0.; + double per_white = (max != min) ? ((val_white - min) / (max - min)) : 0.5; + + const int Number = 3; + double Red[Number] = {0., 1., 1.}; + double Green[Number] = {0., 1., 0.}; + double Blue[Number] = {1., 1., 0.}; + double Stops[Number] = {0., per_white, 1.}; + int nb = 256; + h2_BSShadow->SetContour(nb); + TColor::CreateGradientColorTable(Number, Stops, Red, Green, Blue, nb); + + h2_BSShadow->Draw("colz"); + h2_BSParameters->Draw("TEXTsame"); + + auto ltx = TLatex(); + ltx.SetTextFont(62); + ltx.SetTextSize(0.025); + ltx.SetTextAlign(11); + + auto l_runLS = BeamSpotPI::unpack(std::get<0>(lastiov)); + auto f_runLS = BeamSpotPI::unpack(std::get<0>(firstiov)); + + if (this->m_plotAnnotations.ntags == 2) { + ltx.DrawLatexNDC(gPad->GetLeftMargin(), + 1 - gPad->GetTopMargin() + 0.03, + ("#splitline{A = #color[4]{" + f_tagname + "}}{B = #color[4]{" + l_tagname + "}}").c_str()); + } else { + ltx.DrawLatexNDC(gPad->GetLeftMargin(), + 1 - gPad->GetTopMargin() + 0.03, + ("#splitline{#color[4]{" + f_tagname + "}}{A = " + std::to_string(l_runLS.first) + "," + + std::to_string(l_runLS.second) + " B =" + std::to_string(f_runLS.first) + "," + + std::to_string(f_runLS.second) + "}") + .c_str()); + } + + std::string fileName(this->m_imageFileName); + canvas.SaveAs(fileName.c_str()); + + return true; + } + + public: + virtual std::shared_ptr fillTheExtraHistogram() const { return nullptr; } + + protected: + bool isOnline_; + std::shared_ptr f_payload; + std::shared_ptr l_payload; + + /************************************************/ + virtual std::string getStringFromTypeEnum(const parameters& parameter) const { + switch (parameter) { + case X: + return "X [cm]"; + case Y: + return "Y [cm]"; + case Z: + return "Z [cm]"; + case sigmaX: + return "#sigma_{X} [cm]"; + case sigmaY: + return "#sigma_{Y} [cm]"; + case sigmaZ: + return "#sigma_{Z} [cm]"; + case dxdz: + return "#frac{dX}{dZ} [rad]"; + case dydz: + return "#frac{dY}{dZ} [rad]"; + default: + return "should never be here"; + } + } + }; } // namespace BeamSpotPI #endif diff --git a/CondCore/BeamSpotPlugins/plugins/BeamSpotOnline_PayloadInspector.cc b/CondCore/BeamSpotPlugins/plugins/BeamSpotOnline_PayloadInspector.cc index 08cb525b3c72d..c72f2395bd95f 100644 --- a/CondCore/BeamSpotPlugins/plugins/BeamSpotOnline_PayloadInspector.cc +++ b/CondCore/BeamSpotPlugins/plugins/BeamSpotOnline_PayloadInspector.cc @@ -250,11 +250,22 @@ namespace { } }; + /************************************************ + Display of Beam Spot parameters Differences + *************************************************/ + + typedef DisplayParametersDiff + BeamSpotOnlineParametersDiffSingleTag; + typedef DisplayParametersDiff + BeamSpotOnlineParametersDiffTwoTags; + } // namespace PAYLOAD_INSPECTOR_MODULE(BeamSpotOnline) { PAYLOAD_INSPECTOR_CLASS(BeamSpotOnline_xy); PAYLOAD_INSPECTOR_CLASS(BeamSpotOnlineParameters); + PAYLOAD_INSPECTOR_CLASS(BeamSpotOnlineParametersDiffSingleTag); + PAYLOAD_INSPECTOR_CLASS(BeamSpotOnlineParametersDiffTwoTags); PAYLOAD_INSPECTOR_CLASS(BeamSpotOnline_HistoryX); PAYLOAD_INSPECTOR_CLASS(BeamSpotOnline_HistoryY); PAYLOAD_INSPECTOR_CLASS(BeamSpotOnline_HistoryZ); diff --git a/CondCore/BeamSpotPlugins/plugins/BeamSpot_PayloadInspector.cc b/CondCore/BeamSpotPlugins/plugins/BeamSpot_PayloadInspector.cc index 25e90ef8d8bf4..1f65c538ea782 100644 --- a/CondCore/BeamSpotPlugins/plugins/BeamSpot_PayloadInspector.cc +++ b/CondCore/BeamSpotPlugins/plugins/BeamSpot_PayloadInspector.cc @@ -98,6 +98,13 @@ namespace { typedef DisplayParameters BeamSpotParameters; + /************************************************ + Display of Beam Spot parameters Differences + *************************************************/ + + typedef DisplayParametersDiff BeamSpotParametersDiffSingleTag; + typedef DisplayParametersDiff BeamSpotParametersDiffTwoTags; + } // namespace PAYLOAD_INSPECTOR_MODULE(BeamSpot) { @@ -107,6 +114,8 @@ PAYLOAD_INSPECTOR_MODULE(BeamSpot) { PAYLOAD_INSPECTOR_CLASS(BeamSpot_y); PAYLOAD_INSPECTOR_CLASS(BeamSpot_xy); PAYLOAD_INSPECTOR_CLASS(BeamSpotParameters); + PAYLOAD_INSPECTOR_CLASS(BeamSpotParametersDiffSingleTag); + PAYLOAD_INSPECTOR_CLASS(BeamSpotParametersDiffTwoTags); PAYLOAD_INSPECTOR_CLASS(BeamSpot_HistoryX); PAYLOAD_INSPECTOR_CLASS(BeamSpot_HistoryY); PAYLOAD_INSPECTOR_CLASS(BeamSpot_HistoryZ); From 2f850ba5b29176d9c200b76f0163f5e99d35c691 Mon Sep 17 00:00:00 2001 From: mmusich Date: Thu, 3 Mar 2022 20:11:17 +0100 Subject: [PATCH 2/5] add difference plot for BeamSpotObjects parameters to the CondCore/BeamSpotPlugins unit tests --- .../test/testBeamSpotPayloadInspector.cpp | 19 +++++++++--- .../test/testBeamSpotPayloadInspector.sh | 30 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp index 4a0741a2a7b3f..06aa0a6162ab7 100644 --- a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp +++ b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp @@ -25,10 +25,9 @@ int main(int argc, char** argv) { std::string connectionString("frontier://FrontierProd/CMS_CONDITIONS"); // BeamSpot - std::string tag = "BeamSpotObjects_PCL_byLumi_v0_prompt"; cond::Time_t start = static_cast(1406876667347162); - //cond::Time_t end = static_cast(1406876667347162); + cond::Time_t end = static_cast(1488257707672138); edm::LogPrint("testBeamSpotPayloadInspector") << "## Exercising BeamSpot plots " << std::endl; @@ -36,11 +35,23 @@ int main(int argc, char** argv) { histoParameters.process(connectionString, PI::mk_input(tag, start, start)); edm::LogPrint("testBeamSpotPayloadInspector") << histoParameters.data() << std::endl; - tag = "BeamSpotOnlineTestLegacy"; - start = static_cast(1443392479297557); + BeamSpotParametersDiffSingleTag histoParametersDiff; + histoParametersDiff.process(connectionString, PI::mk_input(tag, start, end)); + edm::LogPrint("testBeamSpotPayloadInspector") << histoParametersDiff.data() << std::endl; + + std::string tag1 = "BeamSpotObjects_Realistic25ns_900GeV_2021PilotBeams_v2_mc"; + std::string tag2 = "BeamSpotObjects_Realistic25ns_900GeV_2021PilotBeams_v1_mc"; + start = static_cast(1); + + BeamSpotParametersDiffTwoTags histoParametersDiffTwoTags; + histoParametersDiffTwoTags.process(connectionString, PI::mk_input(tag1, start, start, tag2, start, start)); + edm::LogPrint("testBeamSpotPayloadInspector") << histoParametersDiffTwoTags.data() << std::endl; edm::LogPrint("testBeamSpotPayloadInspector") << "## Exercising BeamSpotOnline plots " << std::endl; + tag = "BeamSpotOnlineTestLegacy"; + start = static_cast(1443392479297557); + BeamSpotOnlineParameters histoOnlineParameters; histoOnlineParameters.process(connectionString, PI::mk_input(tag, start, start)); edm::LogPrint("testBeamSpotPayloadInspector") << histoOnlineParameters.data() << std::endl; diff --git a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.sh b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.sh index a258417bceb9a..21afb329fe516 100755 --- a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.sh +++ b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.sh @@ -19,6 +19,8 @@ if [ -f *.png ]; then rm *.png fi +echo "Tesing the trend plots plots" + for i in "${types[@]}" do for j in "${coordinates[@]}" @@ -33,5 +35,33 @@ do --iovs '{"start_iov": "1406876667346979", "end_iov": "1406876667347162"}' \ --db Prod \ --test; + + mv *.png $W_DIR/results/${i}_${j}.png done done + +echo "Tesing the parameters difference plots" + +getPayloadData.py \ + --plugin pluginBeamSpot_PayloadInspector \ + --plot plot_BeamSpotParametersDiffSingleTag \ + --tag BeamSpotObjects_PCL_byLumi_v0_prompt \ + --time_type Lumi \ + --iovs '{"start_iov": "1406859487477814", "end_iov": "1488257707672138"}' \ + --db Prod \ + --test ; + +mv *.png $W_DIR/results/BeamSpotParametersDiffSingleTag.png + +getPayloadData.py \ + --plugin pluginBeamSpot_PayloadInspector \ + --plot plot_BeamSpotParametersDiffTwoTags \ + --tag BeamSpotObjects_Realistic25ns_900GeV_2021PilotBeams_v2_mc \ + --time_type Run \ + --iovs '{"start_iov": "1", "end_iov": "1"}' \ + --tagtwo BeamSpotObjects_Realistic25ns_900GeV_2021PilotBeams_v1_mc \ + --iovstwo '{"start_iov": "1", "end_iov": "1"}' \ + --db Prod \ + --test ; + +mv *.png $W_DIR/results/BeamSpotParametersDiffTwoTags.png From 4c4251c73a9d59e7e2171cce1fd6fc62df3a8af6 Mon Sep 17 00:00:00 2001 From: mmusich Date: Thu, 3 Mar 2022 22:56:56 +0100 Subject: [PATCH 3/5] miscellaneous improvements to BeamSpotPayloadInspectorHelper --- .../BeamSpotPayloadInspectorHelper.h | 69 ++++++------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h index f9ef0176d2647..7ed388d42f0b7 100644 --- a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h +++ b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h @@ -57,24 +57,25 @@ namespace BeamSpotPI { }; /************************************************/ - inline std::string getStringFromParamEnum(const parameters& parameter) { + inline std::string getStringFromParamEnum(const parameters& parameter, + const bool addUnits = false /*not used by default*/) { switch (parameter) { case X: - return "X"; + return (addUnits ? "X [cm]" : "X"); case Y: - return "Y"; + return (addUnits ? "Y [cm]" : "Y"); case Z: - return "Z"; + return (addUnits ? "Z [cm]" : "Z"); case sigmaX: - return "sigmaX"; + return (addUnits ? "#sigma_{X} [cm]" : "sigmaX"); case sigmaY: - return "sigmaY"; + return (addUnits ? "#sigma_{Y} [cm]" : "sigmaY"); case sigmaZ: - return "sigmaZ"; + return (addUnits ? "#sigma_{Z} [cm]" : "sigmaZ"); case dxdz: - return "dx/dz"; + return (addUnits ? "#frac{dX}{dZ} [rad]" : "dx/dz"); case dydz: - return "dy/dz"; + return (addUnits ? "#frac{dY}{dZ} [rad]" : "dy/dz"); default: return "should never be here"; } @@ -381,7 +382,10 @@ namespace BeamSpotPI { bool isOnline_; std::shared_ptr m_payload; - /************************************************/ + /** + * Can't use BeamSpotPI::getStringFromParamEnum becasue it needs to be overridden + * for the BeamSpotOnlineObjects case. + */ virtual std::string getStringFromTypeEnum(const parameters& parameter) const { switch (parameter) { case X: @@ -457,10 +461,7 @@ namespace BeamSpotPI { canvas.cd(1)->SetGrid(); auto h2_BSParameters = std::make_unique("Parameters", "", 2, 0.0, 2.0, 8, 0, 8.); - auto h2_BSShadow = - std::make_unique("Shadow", ";;;#Delta parameter (payload A - payload B)", 2, 0.0, 2.0, 8, 0, 8.); h2_BSParameters->SetStats(false); - h2_BSShadow->SetStats(false); std::function cutFunctor = [this](parameters my_param, bool isError) { double ret(-999.); @@ -515,31 +516,26 @@ namespace BeamSpotPI { h2_BSParameters->GetXaxis()->SetBinLabel(1, "Value"); h2_BSParameters->GetXaxis()->SetBinLabel(2, "Error"); - h2_BSShadow->GetXaxis()->SetBinLabel(1, "Value"); - h2_BSShadow->GetXaxis()->SetBinLabel(2, "Error"); unsigned int yBin = 8; for (int foo = parameters::X; foo <= parameters::dydz; foo++) { parameters param = static_cast(foo); - std::string theLabel = getStringFromTypeEnum(param); + std::string theLabel = BeamSpotPI::getStringFromParamEnum(param, true /*use units*/); h2_BSParameters->GetYaxis()->SetBinLabel(yBin, theLabel.c_str()); h2_BSParameters->SetBinContent(1, yBin, cutFunctor(param, false)); h2_BSParameters->SetBinContent(2, yBin, cutFunctor(param, true)); - h2_BSShadow->GetYaxis()->SetBinLabel(yBin, theLabel.c_str()); - h2_BSShadow->SetBinContent(1, yBin, cutFunctor(param, false)); - h2_BSShadow->SetBinContent(2, yBin, cutFunctor(param, true)); yBin--; } h2_BSParameters->GetXaxis()->LabelsOption("h"); h2_BSParameters->GetYaxis()->SetLabelSize(0.05); h2_BSParameters->GetXaxis()->SetLabelSize(0.05); - h2_BSShadow->GetXaxis()->LabelsOption("h"); - h2_BSShadow->GetYaxis()->SetLabelSize(0.05); - h2_BSShadow->GetXaxis()->SetLabelSize(0.05); + h2_BSParameters->SetMarkerSize(1.5); + + auto h2_BSShadow = (TH2F*)(h2_BSParameters->Clone("shadow")); + h2_BSShadow->GetZaxis()->SetTitle("#Delta parameter (payload A - payload B)"); h2_BSShadow->GetZaxis()->CenterTitle(); h2_BSShadow->GetZaxis()->SetTitleOffset(1.5); - h2_BSParameters->SetMarkerSize(1.5); // this is the fine gradient palette (blue to red) double max = h2_BSShadow->GetMaximum(); @@ -587,36 +583,15 @@ namespace BeamSpotPI { } public: + /** + * In case an extension to the BeamSpotOnlineObjects case will be needed in future + */ virtual std::shared_ptr fillTheExtraHistogram() const { return nullptr; } protected: bool isOnline_; std::shared_ptr f_payload; std::shared_ptr l_payload; - - /************************************************/ - virtual std::string getStringFromTypeEnum(const parameters& parameter) const { - switch (parameter) { - case X: - return "X [cm]"; - case Y: - return "Y [cm]"; - case Z: - return "Z [cm]"; - case sigmaX: - return "#sigma_{X} [cm]"; - case sigmaY: - return "#sigma_{Y} [cm]"; - case sigmaZ: - return "#sigma_{Z} [cm]"; - case dxdz: - return "#frac{dX}{dZ} [rad]"; - case dydz: - return "#frac{dY}{dZ} [rad]"; - default: - return "should never be here"; - } - } }; } // namespace BeamSpotPI From 333a337408bc4a0f9df049380edd2ee7440c09aa Mon Sep 17 00:00:00 2001 From: mmusich Date: Fri, 4 Mar 2022 15:32:28 +0100 Subject: [PATCH 4/5] improve BeamSpotPayloadInspectorHelper by making use of the helper class BSParamsHelper --- .../BeamSpotPayloadInspectorHelper.h | 181 +++++++++++------- 1 file changed, 109 insertions(+), 72 deletions(-) diff --git a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h index 7ed388d42f0b7..3fa2a88687ada 100644 --- a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h +++ b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h @@ -2,22 +2,25 @@ #define CONDCORE_BEAMSPOTPLUGINS_BEAMSPOTPAYLOADINSPECTORHELPER_H // User includes - #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "CondCore/Utilities/interface/PayloadInspectorModule.h" #include "CondCore/Utilities/interface/PayloadInspector.h" #include "CondCore/CondDB/interface/Time.h" #include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h" -// ROOT includes - +// system includes +#include #include #include + +// ROOT includes #include "TCanvas.h" #include "TStyle.h" #include "TH2F.h" #include "TLatex.h" +//#define MMDEBUG /* to make it verbose */ + namespace BeamSpotPI { inline std::pair unpack(cond::Time_t since) { @@ -81,6 +84,68 @@ namespace BeamSpotPI { } } + /** + * Helper class for operations on the Beam Spot Parameters + * It's a simplified representation of the beamspot + * data used as the underlying type for data transfers and comparisons + */ + template + class BSParamsHelper { + typedef std::array bshelpdata; + + public: + BSParamsHelper(const std::shared_ptr& bs) { + // fill in the central values + m_values[0] = bs->x(), m_values[1] = bs->y(), m_values[2] = bs->z(); + m_values[3] = bs->beamWidthX(), m_values[4] = bs->beamWidthY(), m_values[5] = bs->sigmaZ(); + m_values[6] = bs->dxdz(), m_values[7] = bs->dydz(); + + // fill in the errors + m_errors[0] = bs->xError(), m_errors[1] = bs->yError(), m_errors[2] = bs->zError(); + m_errors[3] = bs->beamWidthXError(), m_errors[4] = bs->beamWidthYError(), m_errors[5] = bs->sigmaZError(); + m_errors[6] = bs->dxdzError(), m_errors[7] = bs->dydzError(); + } + + void printDebug(std::stringstream& ss) { + ss << "Dumping BeamSpot parameters Data:" << std::endl; + for (uint i = parameters::X; i <= parameters::dydz; i++) { + parameters par = static_cast(i); + ss << getStringFromParamEnum(par) << " : " << m_values[i] << std::endl; + ss << getStringFromParamEnum(par) << " error: " << m_errors[i] << std::endl; + ss << std::endl; + } + } + + inline const bshelpdata centralValues() const { return m_values; } + inline const bshelpdata errors() const { return m_errors; } + + // get the difference in values + const bshelpdata diffCentralValues(const BSParamsHelper& bs2, const bool isPull = false) const { + bshelpdata ret; + for (uint i = parameters::X; i <= parameters::dydz; i++) { + ret[i] = this->centralValues()[i] - bs2.centralValues()[i]; + if (isPull) + (this->centralValues()[i] != 0.) ? ret[i] /= this->centralValues()[i] : 0.; + } + return ret; + } + + // get the difference in errors + const bshelpdata diffErrors(const BSParamsHelper& bs2, const bool isPull = false) const { + bshelpdata ret; + for (uint i = parameters::X; i <= parameters::dydz; i++) { + ret[i] = this->errors()[i] - bs2.errors()[i]; + if (isPull) + (this->errors()[i] != 0.) ? ret[i] /= this->errors()[i] : 0.; + } + return ret; + } + + private: + bshelpdata m_values; /* central values */ + bshelpdata m_errors; /* errors */ + }; + /************************************************ template classes (history) *************************************************/ @@ -460,80 +525,48 @@ namespace BeamSpotPI { canvas.cd(1)->Modified(); canvas.cd(1)->SetGrid(); + // for the "text"-filled histogram auto h2_BSParameters = std::make_unique("Parameters", "", 2, 0.0, 2.0, 8, 0, 8.); h2_BSParameters->SetStats(false); - - std::function cutFunctor = [this](parameters my_param, bool isError) { - double ret(-999.); - if (!isError) { - switch (my_param) { - case X: - return (f_payload->x() - l_payload->x()); - case Y: - return (f_payload->y() - l_payload->y()); - case Z: - return (f_payload->z() - l_payload->z()); - case sigmaX: - return (f_payload->beamWidthX() - l_payload->beamWidthX()); - case sigmaY: - return (f_payload->beamWidthY() - l_payload->beamWidthY()); - case sigmaZ: - return (f_payload->sigmaZ() - l_payload->sigmaZ()); - case dxdz: - return (f_payload->dxdz() - l_payload->dxdz()); - case dydz: - return (f_payload->dydz() - l_payload->dydz()); - case END_OF_TYPES: - return ret; - default: - return ret; - } - } else { - switch (my_param) { - case X: - return (f_payload->xError() - l_payload->xError()); - case Y: - return (f_payload->yError() - l_payload->yError()); - case Z: - return (f_payload->zError() - l_payload->zError()); - case sigmaX: - return (f_payload->beamWidthXError() - l_payload->beamWidthXError()); - case sigmaY: - return (f_payload->beamWidthYError() - l_payload->beamWidthYError()); - case sigmaZ: - return (f_payload->sigmaZError() - l_payload->sigmaZError()); - case dxdz: - return (f_payload->dxdzError() - l_payload->dxdzError()); - case dydz: - return (f_payload->dydzError() - l_payload->dydzError()); - case END_OF_TYPES: - return ret; - default: - return ret; - } - } - }; - h2_BSParameters->GetXaxis()->SetBinLabel(1, "Value"); h2_BSParameters->GetXaxis()->SetBinLabel(2, "Error"); + h2_BSParameters->GetXaxis()->LabelsOption("h"); + h2_BSParameters->GetYaxis()->SetLabelSize(0.05); + h2_BSParameters->GetXaxis()->SetLabelSize(0.05); + h2_BSParameters->SetMarkerSize(1.5); + + // prepare the arrays to fill the histogram + BeamSpotPI::BSParamsHelper fBS(f_payload); + BeamSpotPI::BSParamsHelper lBS(l_payload); + +#ifdef MM_DEBUG + std::stringstream ss1, ss2; + edm::LogPrint("") << "**** first payload"; + fBS.printDebug(ss1); + edm::LogPrint("") << ss1.str(); + edm::LogPrint("") << "**** last payload"; + lBS.printDebug(ss2); + edm::LogPrint("") << ss2.str(); +#endif + + const auto diffPars = fBS.diffCentralValues(lBS); + const auto diffErrors = fBS.diffErrors(lBS); + //const auto pullPars = fBS.diffCentralValues(lBS,true /*normalize*/); + //const auto pullErrors = fBS.diffErrors(lBS,true /*normalize*/); unsigned int yBin = 8; for (int foo = parameters::X; foo <= parameters::dydz; foo++) { parameters param = static_cast(foo); std::string theLabel = BeamSpotPI::getStringFromParamEnum(param, true /*use units*/); h2_BSParameters->GetYaxis()->SetBinLabel(yBin, theLabel.c_str()); - h2_BSParameters->SetBinContent(1, yBin, cutFunctor(param, false)); - h2_BSParameters->SetBinContent(2, yBin, cutFunctor(param, true)); + h2_BSParameters->SetBinContent(1, yBin, diffPars[foo]); /* profiting of the parameters enum indexing */ + h2_BSParameters->SetBinContent(2, yBin, diffErrors[foo]); yBin--; } - h2_BSParameters->GetXaxis()->LabelsOption("h"); - h2_BSParameters->GetYaxis()->SetLabelSize(0.05); - h2_BSParameters->GetXaxis()->SetLabelSize(0.05); - h2_BSParameters->SetMarkerSize(1.5); - + // for the "colz"-filled histogram (clonde from the text-based one) auto h2_BSShadow = (TH2F*)(h2_BSParameters->Clone("shadow")); - h2_BSShadow->GetZaxis()->SetTitle("#Delta parameter (payload A - payload B)"); + h2_BSShadow->GetZaxis()->SetTitle("#Delta Parameter(payload A - payload B)"); h2_BSShadow->GetZaxis()->CenterTitle(); h2_BSShadow->GetZaxis()->SetTitleOffset(1.5); @@ -560,20 +593,24 @@ namespace BeamSpotPI { ltx.SetTextSize(0.025); ltx.SetTextAlign(11); + // compute the (run,LS) pairs auto l_runLS = BeamSpotPI::unpack(std::get<0>(lastiov)); + std::string l_runLSs = "(" + std::to_string(l_runLS.first) + "," + std::to_string(l_runLS.second) + ")"; auto f_runLS = BeamSpotPI::unpack(std::get<0>(firstiov)); + std::string f_runLSs = "(" + std::to_string(f_runLS.first) + "," + std::to_string(f_runLS.second) + ")"; if (this->m_plotAnnotations.ntags == 2) { - ltx.DrawLatexNDC(gPad->GetLeftMargin(), - 1 - gPad->GetTopMargin() + 0.03, - ("#splitline{A = #color[4]{" + f_tagname + "}}{B = #color[4]{" + l_tagname + "}}").c_str()); + ltx.DrawLatexNDC( + gPad->GetLeftMargin() - 0.1, + 1 - gPad->GetTopMargin() + 0.015, + (fmt::sprintf( + "#splitline{A = #color[4]{%s}: %s}{B = #color[4]{%s}: %s}", f_tagname, f_runLSs, l_tagname, l_runLSs)) + .c_str()); } else { - ltx.DrawLatexNDC(gPad->GetLeftMargin(), - 1 - gPad->GetTopMargin() + 0.03, - ("#splitline{#color[4]{" + f_tagname + "}}{A = " + std::to_string(l_runLS.first) + "," + - std::to_string(l_runLS.second) + " B =" + std::to_string(f_runLS.first) + "," + - std::to_string(f_runLS.second) + "}") - .c_str()); + ltx.DrawLatexNDC( + gPad->GetLeftMargin() - 0.1, + 1 - gPad->GetTopMargin() + 0.015, + (fmt::sprintf("#splitline{#color[4]{%s}}{A = %s | B = %s}", f_tagname, l_runLSs, f_runLSs)).c_str()); } std::string fileName(this->m_imageFileName); From 8c57f57f4a5273cc7f92986ee40c7c3ddc31b8b4 Mon Sep 17 00:00:00 2001 From: mmusich Date: Fri, 4 Mar 2022 15:38:37 +0100 Subject: [PATCH 5/5] enhance the BeamSpotPayloadInspector unit tests for the BeamSpotOnlineObjects --- .../test/testBeamSpotPayloadInspector.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp index 06aa0a6162ab7..7bb6c277123dc 100644 --- a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp +++ b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp @@ -51,10 +51,33 @@ int main(int argc, char** argv) { tag = "BeamSpotOnlineTestLegacy"; start = static_cast(1443392479297557); + end = static_cast(1470910334763033); + + edm::LogPrint("") << "###########################################################################\n" + << " DISCLAIMER\n" + << " The following unit test is going to print error messages about\n" + << " out-of-range indices for the BeamSpotOnlineParameters.\n" + << " This normal and expected, since the test payload has been written \n" + << " with an obsolete data layout which doesn't contain few additional \n" + << " parameters, see https://github.com/cms-sw/cmssw/pull/35338 for details. \n" + << " This tests the catching of exceptions coming from reading not existing \n" + << " parameters in the Payload inspector.\n"; BeamSpotOnlineParameters histoOnlineParameters; histoOnlineParameters.process(connectionString, PI::mk_input(tag, start, start)); edm::LogPrint("testBeamSpotPayloadInspector") << histoOnlineParameters.data() << std::endl; + BeamSpotOnlineParametersDiffSingleTag histoOnlineParametersDiff; + histoOnlineParametersDiff.process(connectionString, PI::mk_input(tag, start, end)); + edm::LogPrint("testBeamSpotPayloadInspector") << histoOnlineParametersDiff.data() << std::endl; + + tag1 = "BeamSpotOnlineObjects_Ideal_Centered_SLHC_v3_mc"; + tag2 = "BeamSpotOnlineObjects_Realistic25ns_13TeVCollisions_RoundOpticsLowSigmaZ_RunBased_v1_mc"; + start = static_cast(4294967297); /*Run 1 : LS 1 (packed: 4294967297) this needs to be per LS */ + + BeamSpotOnlineParametersDiffTwoTags histoOnlineParametersDiffTwoTags; + histoOnlineParametersDiffTwoTags.process(connectionString, PI::mk_input(tag1, start, start, tag2, start, start)); + edm::LogPrint("testBeamSpotPayloadInspector") << histoOnlineParametersDiffTwoTags.data() << std::endl; + Py_Finalize(); }