diff --git a/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h b/CondCore/BeamSpotPlugins/interface/BeamSpotPayloadInspectorHelper.h index 8460c7aeb7da1..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) { @@ -57,29 +60,92 @@ 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"; } } + /** + * 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) *************************************************/ @@ -381,7 +447,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: @@ -405,6 +474,162 @@ 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(); + + // for the "text"-filled histogram + auto h2_BSParameters = std::make_unique("Parameters", "", 2, 0.0, 2.0, 8, 0, 8.); + h2_BSParameters->SetStats(false); + 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, diffPars[foo]); /* profiting of the parameters enum indexing */ + h2_BSParameters->SetBinContent(2, yBin, diffErrors[foo]); + yBin--; + } + + // 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()->CenterTitle(); + h2_BSShadow->GetZaxis()->SetTitleOffset(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); + + // 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() - 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() - 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); + canvas.SaveAs(fileName.c_str()); + + return true; + } + + 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; + }; } // 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); diff --git a/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp b/CondCore/BeamSpotPlugins/test/testBeamSpotPayloadInspector.cpp index 4a0741a2a7b3f..7bb6c277123dc 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,14 +35,49 @@ int main(int argc, char** argv) { histoParameters.process(connectionString, PI::mk_input(tag, start, start)); edm::LogPrint("testBeamSpotPayloadInspector") << histoParameters.data() << std::endl; + 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); + end = static_cast(1470910334763033); - edm::LogPrint("testBeamSpotPayloadInspector") << "## Exercising BeamSpotOnline plots " << std::endl; + 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(); } 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