From 5f716b74e66700425eb3ebe78719eb4347682cd0 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 20 Feb 2022 22:19:33 -0500 Subject: [PATCH] Major overall of internal resource management. Unify maps of histos, cuts, and params to one single static SpectrumManager. Change a bunch of stuff to reflect this --- NavProject/SPSAnalysisStage.cpp | 8 + Navigator/src/Navigator.h | 3 +- Navigator/src/Navigator/Application.cpp | 5 - Navigator/src/Navigator/Application.h | 1 - Navigator/src/Navigator/Cut.cpp | 67 +++++ Navigator/src/Navigator/{CutMap.h => Cut.h} | 40 +-- Navigator/src/Navigator/CutMap.cpp | 133 --------- .../src/Navigator/Editor/EditorLayer.cpp | 15 +- Navigator/src/Navigator/Editor/EditorLayer.h | 4 +- .../src/Navigator/Editor/SpectrumDialog.cpp | 3 +- .../src/Navigator/Editor/SpectrumDialog.h | 5 +- .../src/Navigator/Editor/SpectrumPanel.cpp | 17 +- .../src/Navigator/Editor/SpectrumPanel.h | 5 +- Navigator/src/Navigator/Histogram.cpp | 44 +-- Navigator/src/Navigator/Histogram.h | 6 +- Navigator/src/Navigator/HistogramMap.cpp | 143 --------- Navigator/src/Navigator/HistogramMap.h | 52 ---- Navigator/src/Navigator/Parameter.cpp | 18 ++ Navigator/src/Navigator/Parameter.h | 38 +++ Navigator/src/Navigator/ParameterMap.cpp | 97 ------- Navigator/src/Navigator/ParameterMap.h | 67 ----- .../src/Navigator/Physics/AnalysisStage.h | 2 +- .../Navigator/Physics/PhysicsEventBuilder.h | 6 + .../src/Navigator/Physics/PhysicsLayer.cpp | 11 +- .../src/Navigator/Physics/PhysicsLayer.h | 1 - Navigator/src/Navigator/SpectrumManager.cpp | 273 ++++++++++++++++++ Navigator/src/Navigator/SpectrumManager.h | 71 +++++ .../src/Navigator/SpectrumSerializer.cpp | 21 +- Navigator/src/Navigator/SpectrumSerializer.h | 4 +- 29 files changed, 535 insertions(+), 625 deletions(-) create mode 100644 Navigator/src/Navigator/Cut.cpp rename Navigator/src/Navigator/{CutMap.h => Cut.h} (67%) delete mode 100644 Navigator/src/Navigator/CutMap.cpp delete mode 100644 Navigator/src/Navigator/HistogramMap.cpp delete mode 100644 Navigator/src/Navigator/HistogramMap.h create mode 100644 Navigator/src/Navigator/Parameter.cpp create mode 100644 Navigator/src/Navigator/Parameter.h delete mode 100644 Navigator/src/Navigator/ParameterMap.cpp delete mode 100644 Navigator/src/Navigator/ParameterMap.h create mode 100644 Navigator/src/Navigator/SpectrumManager.cpp create mode 100644 Navigator/src/Navigator/SpectrumManager.h diff --git a/NavProject/SPSAnalysisStage.cpp b/NavProject/SPSAnalysisStage.cpp index 35be431..79f55ba 100644 --- a/NavProject/SPSAnalysisStage.cpp +++ b/NavProject/SPSAnalysisStage.cpp @@ -5,6 +5,14 @@ namespace Navigator { SPSAnalysisStage::SPSAnalysisStage() : AnalysisStage("SPSAnalysis"), delayFLTime("delayFLTime"), delayFRTime("delayFRTime"), delayBLTime("delayBLTime"), delayBRTime("delayBRTime"), x1("x1"), x2("x2"), xavg("xavg") { + SpectrumManager& manager = SpectrumManager::GetInstance(); + manager.BindParameter(delayFLTime); + manager.BindParameter(delayFRTime); + manager.BindParameter(delayBLTime); + manager.BindParameter(delayBRTime); + manager.BindParameter(x1); + manager.BindParameter(x2); + manager.BindParameter(xavg); } SPSAnalysisStage::~SPSAnalysisStage() {} diff --git a/Navigator/src/Navigator.h b/Navigator/src/Navigator.h index fe496f9..8bf776d 100644 --- a/Navigator/src/Navigator.h +++ b/Navigator/src/Navigator.h @@ -21,7 +21,8 @@ #include "Navigator/Application.h" #include "Navigator/Physics/PhysicsLayer.h" #include "Navigator/Physics/AnalysisStage.h" -#include "Navigator/ParameterMap.h" +#include "Navigator/Parameter.h" +#include "Navigator/SpectrumManager.h" #include "Navigator/Layer.h" #include "Navigator/Events/Event.h" #include "Navigator/Renderer/Renderer.h" diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 49f365d..d40b762 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -3,9 +3,6 @@ #include "Renderer/RenderCommand.h" #include "Editor/EditorLayer.h" -//temp -#include "CutMap.h" - namespace Navigator { Application* Application::s_instance = nullptr; @@ -66,8 +63,6 @@ namespace Navigator { { while(m_runFlag) { - HistogramMap::GetInstance().UpdateHistograms(); - RenderCommand::SetClearColor(m_bckgnd_color); RenderCommand::Clear(); diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index d6cfced..fe17d05 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -10,7 +10,6 @@ #include "Navigator/Window.h" #include "Navigator/ImGui/ImGuiLayer.h" #include "Navigator/Physics/PhysicsLayer.h" -#include "Navigator/HistogramMap.h" #include "glm/vec4.hpp" namespace Navigator { diff --git a/Navigator/src/Navigator/Cut.cpp b/Navigator/src/Navigator/Cut.cpp new file mode 100644 index 0000000..a46de75 --- /dev/null +++ b/Navigator/src/Navigator/Cut.cpp @@ -0,0 +1,67 @@ +#include "Cut.h" +#include "implot.h" + +namespace Navigator { + + /*1D Cuts -- Can be made on and applied to either 1D or 2D histograms*/ + Cut1D::Cut1D(const CutParams& params, double min, double max) : + Cut(params), m_minVal(min), m_maxVal(max) + { + } + + Cut1D::~Cut1D() {} + + bool Cut1D::IsInside(double x, double y) const + { + return x >= m_minVal && x <= m_maxVal; + } + + //Only within an ImPlot/ImGui context!!! + void Cut1D::Draw() const + { + double points[2] = { m_minVal, m_maxVal }; + ImPlot::PlotVLines(m_params.name.c_str(), points, 2); + } + + /*2D Cuts -- Can only be made on 2D histogram, but applied to either 1D or 2D histograms*/ + Cut2D::Cut2D(const CutParams& params, const std::vector& xpoints, const std::vector& ypoints) : + Cut(params), m_xpoints(xpoints), m_ypoints(ypoints) + { + } + + Cut2D::~Cut2D() {} + + /* + Even-odd point in polygon algorithm (see Wikipedia) + Walk around the sides of the polygon and check intersection with each of the sides. + Cast a ray from the point to infinity in any direction and check the number of intersections. + If odd number of intersections, point is inside. Even, point is outside. + Edge cases of point is a vertex or on a side considered. + */ + bool Cut2D::IsInside(double x, double y) const + { + bool result = false; + double slope; + for(size_t i=0; i<(m_xpoints.size()-1); i++) + { + if(x == m_xpoints[i+1] && y == m_ypoints[i+1]) + return true; + else if((m_ypoints[i+1] > y) != (m_ypoints[i] > y)) + { + slope = (x - m_xpoints[i+1])*(m_ypoints[i] - m_ypoints[i+1]) - (m_xpoints[i] - m_xpoints[i+1])*(y - m_ypoints[i+1]); + if(slope == 0.0) + return true; + else if ((slope < 0.0) != (m_ypoints[i] < m_ypoints[i+1])) + result = !result; + } + } + return result; + } + + //Only in ImPlot/ImGui context!!!! + void Cut2D::Draw() const + { + ImPlot::PlotLine(m_params.name.c_str(), m_xpoints.data(), m_ypoints.data(), (int)m_xpoints.size()); + } + +} diff --git a/Navigator/src/Navigator/CutMap.h b/Navigator/src/Navigator/Cut.h similarity index 67% rename from Navigator/src/Navigator/CutMap.h rename to Navigator/src/Navigator/Cut.h index dc3cd66..a7b46a1 100644 --- a/Navigator/src/Navigator/CutMap.h +++ b/Navigator/src/Navigator/Cut.h @@ -32,7 +32,7 @@ namespace Navigator { virtual ~Cut() {} - virtual bool IsInside() const = 0; + virtual bool IsInside(double x, double y=0.0) const = 0; virtual void Draw() const = 0; virtual bool Is1D() const = 0; virtual bool Is2D() const = 0; @@ -52,7 +52,7 @@ namespace Navigator { public: Cut1D(const CutParams& params, double min, double max); virtual ~Cut1D(); - virtual bool IsInside() const override; + virtual bool IsInside(double x, double y=0.0) const override; virtual void Draw() const override; virtual bool Is1D() const override { return true; } virtual bool Is2D() const override { return false; } @@ -68,7 +68,7 @@ namespace Navigator { public: Cut2D(const CutParams& params, const std::vector& xpoints, const std::vector& ypoints); virtual ~Cut2D(); - virtual bool IsInside() const override; + virtual bool IsInside(double x, double y) const override; virtual void Draw() const override; virtual bool Is1D() const override { return false; } virtual bool Is2D() const override { return true; } @@ -80,40 +80,6 @@ namespace Navigator { std::vector m_ypoints; const ImVec4 colorVec = {1.0, 0.0, 0.0, 0.5}; }; - - class NAV_API CutMap - { - public: - CutMap(); - ~CutMap(); - - inline static CutMap& GetInstance() { return *s_instance; } - - inline void AddCut(const CutParams& params, double min, double max) - { - m_map[params.name].reset(new Cut1D(params, min, max)); - } - inline void AddCut(const CutParams& params, const std::vector& xpoints, const std::vector& ypoints) - { - m_map[params.name].reset(new Cut2D(params, xpoints, ypoints)); - } - inline void RemoveCut(const std::string& name) - { - m_map.erase(name); - } - - void DrawCut(const std::string& name); - bool IsInsideCut(const std::string& name); - std::vector GetCutXPoints(const std::string& name); - std::vector GetCutYPoints(const std::string& name); - std::vector GetListOfCutParams(); - - private: - std::mutex m_cutMutex; - std::unordered_map> m_map; - - static CutMap* s_instance; - }; } #endif diff --git a/Navigator/src/Navigator/CutMap.cpp b/Navigator/src/Navigator/CutMap.cpp deleted file mode 100644 index c495a6a..0000000 --- a/Navigator/src/Navigator/CutMap.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include "CutMap.h" -#include "ParameterMap.h" -#include "implot.h" - -namespace Navigator { - - /*1D Cuts -- Can be made on and applied to either 1D or 2D histograms*/ - Cut1D::Cut1D(const CutParams& params, double min, double max) : - Cut(params), m_minVal(min), m_maxVal(max) - { - } - - Cut1D::~Cut1D() {} - - bool Cut1D::IsInside() const - { - ParameterMap& parMap = ParameterMap::GetInstance(); - ParameterData param = parMap.GetParameter(m_params.x_par); - if (!param.validFlag) - return false; - return param.value >= m_minVal && param.value <= m_maxVal; - } - - //Only within an ImPlot/ImGui context!!! - void Cut1D::Draw() const - { - double points[2] = { m_minVal, m_maxVal }; - ImPlot::PlotVLines(m_params.name.c_str(), points, 2); - } - - /*2D Cuts -- Can only be made on 2D histogram, but applied to either 1D or 2D histograms*/ - Cut2D::Cut2D(const CutParams& params, const std::vector& xpoints, const std::vector& ypoints) : - Cut(params), m_xpoints(xpoints), m_ypoints(ypoints) - { - } - - Cut2D::~Cut2D() {} - - /* - Even-odd point in polygon algorithm (see Wikipedia) - Walk around the sides of the polygon and check intersection with each of the sides. - Cast a ray from the point to infinity in any direction and check the number of intersections. - If odd number of intersections, point is inside. Even, point is outside. - Edge cases of point is a vertex or on a side considered. - */ - bool Cut2D::IsInside() const - { - ParameterMap& parMap = ParameterMap::GetInstance(); - ParameterData paramx = parMap.GetParameter(m_params.x_par); - ParameterData paramy = parMap.GetParameter(m_params.y_par); - if (!paramx.validFlag || !paramy.validFlag) - return false; - double x = paramx.value; - double y = paramy.value; - bool result = false; - double slope; - for(size_t i=0; i<(m_xpoints.size()-1); i++) - { - if(x == m_xpoints[i+1] && y == m_ypoints[i+1]) - return true; - else if((m_ypoints[i+1] > y) != (m_ypoints[i] > y)) - { - slope = (x - m_xpoints[i+1])*(m_ypoints[i] - m_ypoints[i+1]) - (m_xpoints[i] - m_xpoints[i+1])*(y - m_ypoints[i+1]); - if(slope == 0.0) - return true; - else if ((slope < 0.0) != (m_ypoints[i] < m_ypoints[i+1])) - result = !result; - } - } - return result; - } - - //Only in ImPlot/ImGui context!!!! - void Cut2D::Draw() const - { - ImPlot::PlotLine(m_params.name.c_str(), m_xpoints.data(), m_ypoints.data(), (int)m_xpoints.size()); - } - - /* CutMap */ - CutMap* CutMap::s_instance = new CutMap(); //Guarantee existance for duration of program. - - CutMap::CutMap() {} - - CutMap::~CutMap() {} - - void CutMap::DrawCut(const std::string& name) - { - std::lock_guard guard(m_cutMutex); - auto iter = m_map.find(name); - if(iter != m_map.end()) - iter->second->Draw(); - } - - bool CutMap::IsInsideCut(const std::string& name) - { - std::lock_guard guard(m_cutMutex); - bool result = false; - auto iter = m_map.find(name); - if(iter != m_map.end()) - result = iter->second->IsInside(); - return result; - } - - std::vector CutMap::GetCutXPoints(const std::string& name) - { - std::lock_guard guard(m_cutMutex); - std::vector null_result; - auto iter = m_map.find(name); - if(iter != m_map.end()) - return iter->second->GetXValues(); - return null_result; - } - - std::vector CutMap::GetCutYPoints(const std::string& name) - { - std::lock_guard guard(m_cutMutex); - std::vector null_result; - auto iter = m_map.find(name); - if(iter != m_map.end()) - return iter->second->GetYValues(); - return null_result; - } - - std::vector CutMap::GetListOfCutParams() - { - std::lock_guard guard(m_cutMutex); - std::vector list; - list.reserve(m_map.size()); - for(auto& entry : m_map) - list.push_back(entry.second->GetCutParams()); - return list; - } -} diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index 770e96e..e71f8ac 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -4,6 +4,7 @@ #include "FileDialog.h" #include "Navigator/Application.h" #include "Navigator/SpectrumSerializer.h" +#include "Navigator/SpectrumManager.h" #include "IconsFontAwesome5.h" @@ -34,20 +35,17 @@ namespace Navigator { void EditorLayer::UpdateHistogramList() { - HistogramMap& histoMap = HistogramMap::GetInstance(); - m_histoList = histoMap.GetListOfHistograms(); + m_histoList = SpectrumManager::GetInstance().GetListOfHistograms(); } void EditorLayer::UpdateCutList() { - CutMap& cutMap = CutMap::GetInstance(); - m_cutList = cutMap.GetListOfCutParams(); + m_cutList = SpectrumManager::GetInstance().GetListOfCuts(); } void EditorLayer::UpdateParameterList() { - ParameterMap& parMap = ParameterMap::GetInstance(); - m_paramList = parMap.GetListOfParameters(); + m_paramList = SpectrumManager::GetInstance().GetListOfParameters(); } void EditorLayer::OnImGuiRender() @@ -260,7 +258,7 @@ namespace Navigator { } if (ImGui::Button("Ok")) { - HistogramMap::GetInstance().RemoveHistogram(selectedGram); + SpectrumManager::GetInstance().RemoveHistogram(selectedGram); UpdateHistogramList(); ImGui::CloseCurrentPopup(); } @@ -295,8 +293,7 @@ namespace Navigator { } if (ImGui::Button("Ok")) { - HistogramMap::GetInstance().RemoveCutFromHistograms(selectedCut); - CutMap::GetInstance().RemoveCut(selectedCut); + SpectrumManager::GetInstance().RemoveCut(selectedCut); UpdateHistogramList(); UpdateCutList(); ImGui::CloseCurrentPopup(); diff --git a/Navigator/src/Navigator/Editor/EditorLayer.h b/Navigator/src/Navigator/Editor/EditorLayer.h index 3cda383..6160528 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.h +++ b/Navigator/src/Navigator/Editor/EditorLayer.h @@ -4,8 +4,8 @@ #include "Navigator/Layer.h" #include "Navigator/Events/Event.h" #include "Navigator/Events/PhysicsEvent.h" -#include "Navigator/HistogramMap.h" -#include "Navigator/CutMap.h" +#include "Navigator/Histogram.h" +#include "Navigator/Cut.h" #include "SpectrumPanel.h" #include "FileDialog.h" #include "SpectrumDialog.h" diff --git a/Navigator/src/Navigator/Editor/SpectrumDialog.cpp b/Navigator/src/Navigator/Editor/SpectrumDialog.cpp index 4fdd757..1398221 100644 --- a/Navigator/src/Navigator/Editor/SpectrumDialog.cpp +++ b/Navigator/src/Navigator/Editor/SpectrumDialog.cpp @@ -1,4 +1,5 @@ #include "SpectrumDialog.h" +#include "Navigator/SpectrumManager.h" #include "misc/cpp/imgui_stdlib.h" @@ -115,7 +116,7 @@ namespace Navigator { if (ImGui::Button("Ok")) { - HistogramMap::GetInstance().AddHistogram(m_newParams); + SpectrumManager::GetInstance().AddHistogram(m_newParams); ImGui::CloseCurrentPopup(); result = true; } diff --git a/Navigator/src/Navigator/Editor/SpectrumDialog.h b/Navigator/src/Navigator/Editor/SpectrumDialog.h index a3c29ec..715e45d 100644 --- a/Navigator/src/Navigator/Editor/SpectrumDialog.h +++ b/Navigator/src/Navigator/Editor/SpectrumDialog.h @@ -1,9 +1,8 @@ #ifndef SPECTRUM_DIALOG_H #define SPECTRUM_DIALOG_H -#include "Navigator/HistogramMap.h" -#include "Navigator/ParameterMap.h" -#include "Navigator/CutMap.h" +#include "Navigator/Histogram.h" +#include "Navigator/Cut.h" #include "imgui.h" diff --git a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp index 12d198c..69f02ae 100644 --- a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp +++ b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp @@ -1,4 +1,5 @@ #include "SpectrumPanel.h" +#include "Navigator/SpectrumManager.h" #include "misc/cpp/imgui_stdlib.h" #include "IconsFontAwesome5.h" @@ -38,14 +39,14 @@ namespace Navigator { ImGui::SameLine(); if(ImGui::Button("Clear")) { - HistogramMap::GetInstance().ClearHistogram(m_zoomedGram.name); + SpectrumManager::GetInstance().ClearHistogram(m_zoomedGram.name); } ImGui::SameLine(); RenderRemoveRegionButton(); if (ImPlot::BeginPlot(m_zoomedGram.name.c_str(), ImVec2(-1, -1))) { - HistogramMap::GetInstance().DrawHistogram(m_zoomedGram.name); + SpectrumManager::GetInstance().DrawHistogram(m_zoomedGram.name); if (!m_cutModeFlag && ImPlot::IsPlotHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { NAV_INFO("We lost 'em, de-zoom and enhance!"); @@ -96,7 +97,7 @@ namespace Navigator { if (m_zoomedGram.name == region.histogram_name) { ImPlot::DragRect(int(i), ®ion.region.X.Min, ®ion.region.Y.Min, ®ion.region.X.Max, ®ion.region.Y.Max, ImVec4(1, 0, 1, 1)); - StatResults results = HistogramMap::GetInstance().AnalyzeHistogramRegion(m_zoomedGram.name, region.region); + StatResults results = SpectrumManager::GetInstance().AnalyzeHistogramRegion(m_zoomedGram.name, region.region); ImPlot::PlotText(GenerateStatString(region.name, results, m_zoomedGram.y_par != "None").c_str(), (region.region.X.Max + region.region.X.Min) * 0.5, (region.region.Y.Min + region.region.Y.Max) * 0.5); } @@ -117,13 +118,13 @@ namespace Navigator { if (m_newCutParams.y_par == "None") { std::sort(m_newCutX.begin(), m_newCutX.end()); - CutMap::GetInstance().AddCut(m_newCutParams, m_newCutX[0], m_newCutX[1]); + SpectrumManager::GetInstance().AddCut(m_newCutParams, m_newCutX[0], m_newCutX[1]); } else { - CutMap::GetInstance().AddCut(m_newCutParams, m_newCutX, m_newCutY); + SpectrumManager::GetInstance().AddCut(m_newCutParams, m_newCutX, m_newCutY); } - HistogramMap::GetInstance().AddCutToHistogramDraw(m_newCutParams.name, m_zoomedGram.name); + SpectrumManager::GetInstance().AddCutToHistogramDraw(m_newCutParams.name, m_zoomedGram.name); ImGui::CloseCurrentPopup(); result = true; } @@ -142,7 +143,7 @@ namespace Navigator { ImGui::SameLine(); if(ImGui::Button("Clear All")) { - HistogramMap::GetInstance().ClearHistograms(); + SpectrumManager::GetInstance().ClearHistograms(); } m_totalSlots = m_tableSizes[0] * m_tableSizes[1]; m_selectedGrams.resize(m_totalSlots); @@ -179,7 +180,7 @@ namespace Navigator { { if (ImPlot::BeginPlot(spec.name.c_str())) { - HistogramMap::GetInstance().DrawHistogram(spec.name); + SpectrumManager::GetInstance().DrawHistogram(spec.name); if (ImPlot::IsPlotHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { NAV_INFO("We got'em boys, they're in plot {0}. Zoom and enhance!", i); diff --git a/Navigator/src/Navigator/Editor/SpectrumPanel.h b/Navigator/src/Navigator/Editor/SpectrumPanel.h index 6355470..9f99516 100644 --- a/Navigator/src/Navigator/Editor/SpectrumPanel.h +++ b/Navigator/src/Navigator/Editor/SpectrumPanel.h @@ -1,9 +1,8 @@ #ifndef SPECTRUM_PANEL_H #define SPECTRUM_PANEL_H -#include "Navigator/HistogramMap.h" -#include "Navigator/ParameterMap.h" -#include "Navigator/CutMap.h" +#include "Navigator/Histogram.h" +#include "Navigator/Cut.h" #include "imgui.h" #include "implot.h" diff --git a/Navigator/src/Navigator/Histogram.cpp b/Navigator/src/Navigator/Histogram.cpp index 606d7f9..950ee62 100644 --- a/Navigator/src/Navigator/Histogram.cpp +++ b/Navigator/src/Navigator/Histogram.cpp @@ -1,6 +1,4 @@ #include "Histogram.h" -#include "CutMap.h" -#include "ParameterMap.h" #include "implot.h" /* @@ -45,21 +43,9 @@ namespace Navigator { } //Note: only x is used here, y is simply present to maintain compliance with 2D case and can be ignored - void Histogram1D::FillData() + void Histogram1D::FillData(double x, double y) { - ParameterMap& parMap = ParameterMap::GetInstance(); - ParameterData x = parMap.GetParameter(m_params.x_par); - if(!x.validFlag || x.value < m_params.min_x || x.value >= m_params.max_x || !m_initFlag) - return; - auto& cutmap = CutMap::GetInstance(); - for (auto& cut : m_params.cutsAppliedTo) - { - if (!cutmap.IsInsideCut(cut)) - return; - } - - int bin = int((x.value - m_params.min_x)/(m_binWidth)); - + int bin = int((x - m_params.min_x)/(m_binWidth)); m_binCounts[bin] += 1.0; } @@ -68,9 +54,6 @@ namespace Navigator { { ImPlot::SetupAxes(m_params.x_par.c_str(), "Counts",0, ImPlotAxisFlags_LockMin); ImPlot::PlotBars(m_params.name.c_str(), &m_binCenters.data()[0], &m_binCounts.data()[0], m_params.nbins_x, m_binWidth); - auto& cutmap = CutMap::GetInstance(); - for(auto& cut : m_params.cutsDrawnUpon) - cutmap.DrawCut(cut); } void Histogram1D::ClearData() @@ -143,24 +126,10 @@ namespace Navigator { m_initFlag = true; } - void Histogram2D::FillData() + void Histogram2D::FillData(double x, double y) { - ParameterMap& parMap = ParameterMap::GetInstance(); - CutMap& cutMap = CutMap::GetInstance(); - if (!parMap.IsParameterValid(m_params.x_par) || !parMap.IsParameterValid(m_params.y_par)) - return; - ParameterData x = parMap.GetParameter(m_params.x_par); - ParameterData y = parMap.GetParameter(m_params.y_par); - if(!x.validFlag || !y.validFlag || x.value < m_params.min_x || x.value >= m_params.max_x || y.value < m_params.min_y || y.value >= m_params.max_y || !m_initFlag) - return; - for (auto& cut : m_params.cutsAppliedTo) - { - if (!cutMap.IsInsideCut(cut)) - return; - } - - int bin_x = int((x.value - m_params.min_x)/m_binWidthX); - int bin_y = int((m_params.max_y - y.value)/m_binWidthY); + int bin_x = int((x - m_params.min_x)/m_binWidthX); + int bin_y = int((m_params.max_y - y)/m_binWidthY); int bin = bin_y*m_params.nbins_x + bin_x; m_binCounts[bin] += 1.0; @@ -176,9 +145,6 @@ namespace Navigator { ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, 0.0, m_maxBinContent, NULL, ImPlotPoint(m_params.min_x, m_params.min_y), ImPlotPoint(m_params.max_x, m_params.max_y)); ImPlot::PopColormap(); - auto& cutmap = CutMap::GetInstance(); - for(auto& cut : m_params.cutsDrawnUpon) - cutmap.DrawCut(cut); } void Histogram2D::ClearData() diff --git a/Navigator/src/Navigator/Histogram.h b/Navigator/src/Navigator/Histogram.h index f36b8ec..6d75433 100644 --- a/Navigator/src/Navigator/Histogram.h +++ b/Navigator/src/Navigator/Histogram.h @@ -54,7 +54,7 @@ namespace Navigator { } virtual ~Histogram() {}; - virtual void FillData() { NAV_WARN("Trying to fill a default histogram!"); } + virtual void FillData(double x, double y=0.0) { NAV_WARN("Trying to fill a default histogram!"); } virtual void Draw() {} virtual void ClearData() {} virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) { return StatResults(); } @@ -77,7 +77,7 @@ namespace Navigator { public: Histogram1D(const HistogramParameters& params); virtual ~Histogram1D(); - virtual void FillData() override; + virtual void FillData(double x, double y=0.0) override; virtual void Draw() override; virtual void ClearData() override; virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; @@ -98,7 +98,7 @@ namespace Navigator { public: Histogram2D(const HistogramParameters& params); virtual ~Histogram2D(); - virtual void FillData() override; + virtual void FillData(double x, double y) override; virtual void Draw() override; virtual void ClearData() override; virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; diff --git a/Navigator/src/Navigator/HistogramMap.cpp b/Navigator/src/Navigator/HistogramMap.cpp deleted file mode 100644 index 8447afe..0000000 --- a/Navigator/src/Navigator/HistogramMap.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "HistogramMap.h" -#include "ParameterMap.h" - -#include "implot.h" - -namespace Navigator { - - HistogramMap* HistogramMap::s_instance = new HistogramMap(); - - HistogramMap::HistogramMap() - { - } - - HistogramMap::~HistogramMap() - { - } - - void HistogramMap::AddHistogram(const HistogramParameters& params) - { - std::lock_guard guard(m_histoMutex); - if (params.y_par == "None") - m_map[params.name].reset(new Histogram1D(params)); - else - m_map[params.name].reset(new Histogram2D(params)); - } - - void HistogramMap::RemoveHistogram(const std::string& name) - { - std::lock_guard guard(m_histoMutex); - m_map.erase(name); - } - - void HistogramMap::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname) - { - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(histoname); - if(iter != m_map.end()) - iter->second->AddCutToBeDrawn(cutname); - } - - void HistogramMap::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname) - { - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(histoname); - if(iter != m_map.end()) - iter->second->AddCutToBeApplied(cutname); - } - - void HistogramMap::RemoveCutFromHistograms(const std::string& cutname) - { - std::lock_guard guard(m_histoMutex); - for (auto& gram : m_map) - { - auto& params = gram.second->GetParameters(); - for (size_t i = 0; i < params.cutsDrawnUpon.size(); ++i) - { - if (params.cutsDrawnUpon[i] == cutname) - { - params.cutsDrawnUpon.erase(params.cutsDrawnUpon.begin() + i); - } - } - for (size_t i = 0; i < params.cutsAppliedTo.size(); ++i) - { - if (params.cutsAppliedTo[i] == cutname) - { - params.cutsAppliedTo.erase(params.cutsAppliedTo.begin() + i); - } - } - } - } - - void HistogramMap::UpdateHistograms() - { - std::lock_guard guard(m_histoMutex); - for (auto& pair : m_map) - pair.second->FillData(); - } - - void HistogramMap::ClearHistograms() - { - std::lock_guard guard(m_histoMutex); - for(auto& pair : m_map) - pair.second->ClearData(); - } - - void HistogramMap::ClearHistogram(const std::string& name) - { - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(name); - if(iter != m_map.end()) - iter->second->ClearData(); - } - - const HistogramParameters& HistogramMap::GetHistogramParams(const std::string& name) - { - - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(name); - if (iter != m_map.end()) - return iter->second->GetParameters(); - else - return m_nullResult; - } - - StatResults HistogramMap::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region) - { - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(name); - if (iter != m_map.end()) - return iter->second->AnalyzeRegion(region.X.Min, region.X.Max, region.Y.Min, region.Y.Max); - else - return StatResults(); - } - - std::vector HistogramMap::GetListOfHistograms() - { - std::lock_guard guard(m_histoMutex); - std::vector list; - list.reserve(m_map.size()); - for(auto& gram : m_map) - { - list.push_back(gram.second->GetParameters()); - } - return list; - } - - //Only to be used within ImGui context!! - void Navigator::HistogramMap::DrawHistograms() - { - std::lock_guard guard(m_histoMutex); - for (auto& pair : m_map) - pair.second->Draw(); - } - - //Only to be used within ImGui context!! - void Navigator::HistogramMap::DrawHistogram(const std::string& name) - { - std::lock_guard guard(m_histoMutex); - auto iter = m_map.find(name); - if (iter != m_map.end()) - iter->second->Draw(); - } -} diff --git a/Navigator/src/Navigator/HistogramMap.h b/Navigator/src/Navigator/HistogramMap.h deleted file mode 100644 index 2a56520..0000000 --- a/Navigator/src/Navigator/HistogramMap.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef HISTOGRAM_MAP_H -#define HISTOGRAM_MAP_H - -#include "NavCore.h" -#include "Histogram.h" - -#include - -struct ImPlotRect; - -namespace Navigator { - - class NAV_API HistogramMap - { - public: - HistogramMap(); - ~HistogramMap(); - - void AddHistogram(const HistogramParameters& params); - - void RemoveHistogram(const std::string& name); - - void AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname); - void AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname); - void RemoveCutFromHistograms(const std::string& cutname); - - void UpdateHistograms(); - void ClearHistograms(); - void ClearHistogram(const std::string& name); - - void DrawHistograms(); - void DrawHistogram(const std::string& name); - - const HistogramParameters& GetHistogramParams(const std::string& name); - StatResults AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region); - - std::vector GetListOfHistograms(); - - static HistogramMap& GetInstance() { return *s_instance; } - - private: - std::mutex m_histoMutex; - std::unordered_map> m_map; - - HistogramParameters m_nullResult; - - static HistogramMap* s_instance; - }; - -} - -#endif diff --git a/Navigator/src/Navigator/Parameter.cpp b/Navigator/src/Navigator/Parameter.cpp new file mode 100644 index 0000000..dc67bff --- /dev/null +++ b/Navigator/src/Navigator/Parameter.cpp @@ -0,0 +1,18 @@ +#include "Parameter.h" + +namespace Navigator { + + NavParameter::NavParameter() : + m_name(""), m_pdata(nullptr) + { + } + + NavParameter::NavParameter(const std::string& name) : + m_name(name), m_pdata(nullptr) + { + NAV_INFO("Making a new parameter named {0}...",name); + } + + NavParameter::~NavParameter() {} + +} diff --git a/Navigator/src/Navigator/Parameter.h b/Navigator/src/Navigator/Parameter.h new file mode 100644 index 0000000..c1aede3 --- /dev/null +++ b/Navigator/src/Navigator/Parameter.h @@ -0,0 +1,38 @@ +#ifndef PARAMETER_MAP_H +#define PARAMETER_MAP_H + +#include "NavCore.h" +#include + +namespace Navigator { + + struct NAV_API ParameterData + { + double value=0.0; + bool validFlag=false; + }; + + class NAV_API NavParameter + { + + public: + NavParameter(); + NavParameter(const std::string& name); + ~NavParameter(); + + inline bool IsValid() const { return m_pdata->validFlag; } + inline void Invalidate() { m_pdata->validFlag = false; } + inline void SetValue(double value) { m_pdata->validFlag = true; m_pdata->value = value; } + inline double GetValue() const { return m_pdata->value; } + inline const std::string& GetName() const { return m_name; } + + friend class SpectrumManager; + private: + std::string m_name; + std::shared_ptr m_pdata; + + }; + +} + +#endif diff --git a/Navigator/src/Navigator/ParameterMap.cpp b/Navigator/src/Navigator/ParameterMap.cpp deleted file mode 100644 index 5fee4c7..0000000 --- a/Navigator/src/Navigator/ParameterMap.cpp +++ /dev/null @@ -1,97 +0,0 @@ -// -// ParameterMap.cpp -// Navigator -// -// Created by Gordon McCann on 1/7/22. -// - -#include "ParameterMap.h" - -namespace Navigator { - - NavParameter::NavParameter() : - m_name(""), m_pdata(nullptr) - { - } - - NavParameter::NavParameter(const std::string& name) : - m_name(name) - { - NAV_INFO("Making a new parameter named {0}...",name); - m_pdata = nullptr; - ParameterMap& map = ParameterMap::GetInstance(); - NAV_INFO("Setting the memory..."); - map.SetParameter(name, m_pdata); - } - - NavParameter::~NavParameter() {} - - void NavParameter::SetParameter(const std::string& name) - { - ParameterMap& map = ParameterMap::GetInstance(); - map.SetParameter(name, m_pdata); - } - - ParameterMap* ParameterMap::s_instance = new ParameterMap(); - - ParameterMap::ParameterMap() - { - } - - ParameterMap::~ParameterMap() {} - - void ParameterMap::SetParameter(const std::string& name, std::shared_ptr& param) - { - std::lock_guard guard(m_paramMutex); - auto iter = m_map.find(name); - if(iter == m_map.end()) - { - m_map[name].reset(new ParameterData()); - } - - param = m_map[name]; - } - - ParameterData ParameterMap::GetParameter(const std::string& name) - { - std::lock_guard guard(m_paramMutex); - auto iter = m_map.find(name); - if(iter != m_map.end()) - return *(iter->second); - else - return ParameterData(); - } - - bool ParameterMap::IsParameterValid(const std::string& name) - { - std::lock_guard guard(m_paramMutex); - auto iter = m_map.find(name); - if(iter != m_map.end()) - return iter->second->validFlag; - else - return false; - } - - void ParameterMap::InvalidateParameters() - { - std::lock_guard guard(m_paramMutex); - for(auto& iter : m_map) - { - iter.second->validFlag = false; - iter.second->value = 0.0; - } - } - - std::vector ParameterMap::GetListOfParameters() - { - std::lock_guard guard(m_paramMutex); - std::vector list; - list.reserve(m_map.size()); - for (auto iter : m_map) - { - list.push_back(iter.first); - } - - return list; - } -} diff --git a/Navigator/src/Navigator/ParameterMap.h b/Navigator/src/Navigator/ParameterMap.h deleted file mode 100644 index b0652ed..0000000 --- a/Navigator/src/Navigator/ParameterMap.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef PARAMETER_MAP_H -#define PARAMETER_MAP_H - -#include "NavCore.h" -#include - -namespace Navigator { - - struct NAV_API ParameterData - { - double value=0.0; - bool validFlag=false; - }; - - class NAV_API NavParameter - { - - public: - NavParameter(); - NavParameter(const std::string& name); - ~NavParameter(); - - void SetParameter(const std::string& name); - - inline bool IsValid() const { return m_pdata->validFlag; } - inline void Invalidate() { m_pdata->validFlag = false; } - inline void SetValue(double value) { m_pdata->validFlag = true; m_pdata->value = value; } - inline double GetValue() const { return m_pdata->value; } - inline const std::string& GetName() const { return m_name; } - - private: - std::string m_name; - std::shared_ptr m_pdata; - - }; - - /* - Global parameter accesing, storage - */ - class NAV_API ParameterMap - { - - public: - - using Iter = std::unordered_map>::iterator; - - ParameterMap(); - ~ParameterMap(); - void SetParameter(const std::string& name, std::shared_ptr& param); - ParameterData GetParameter(const std::string& name); - bool IsParameterValid(const std::string& name); - void InvalidateParameters(); - std::vector GetListOfParameters(); - - inline static ParameterMap& GetInstance() { return *s_instance; } - - private: - std::unordered_map> m_map; - static ParameterMap* s_instance; - - std::mutex m_paramMutex; - - }; - -} - -#endif diff --git a/Navigator/src/Navigator/Physics/AnalysisStage.h b/Navigator/src/Navigator/Physics/AnalysisStage.h index 16bdfa7..cef427e 100644 --- a/Navigator/src/Navigator/Physics/AnalysisStage.h +++ b/Navigator/src/Navigator/Physics/AnalysisStage.h @@ -2,7 +2,7 @@ #define ANALYSIS_STAGE_H #include "Navigator/NavCore.h" -#include "Navigator/ParameterMap.h" +#include "Navigator/Parameter.h" #include "NavData.h" namespace Navigator { diff --git a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h index 4f94173..7d6ff95 100644 --- a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h +++ b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h @@ -11,6 +11,12 @@ namespace Navigator { PhysicsEventBuilder(uint64_t windowSize); ~PhysicsEventBuilder(); inline void SetCoincidenceWindow(uint64_t windowSize) { m_coincWindow = windowSize; } + inline void ClearAll() + { + m_event.clear(); + m_readyEvent.clear(); + m_eventStartTime = 0; + } bool AddDatumToEvent(const NavData& datum); const NavEvent& GetReadyEvent() const; diff --git a/Navigator/src/Navigator/Physics/PhysicsLayer.cpp b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp index 50e54b2..e793c0b 100644 --- a/Navigator/src/Navigator/Physics/PhysicsLayer.cpp +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp @@ -1,7 +1,5 @@ #include "PhysicsLayer.h" -#include "Navigator/ParameterMap.h" - -//temp +#include "Navigator/SpectrumManager.h" #include "NavData.h" namespace Navigator { @@ -96,6 +94,7 @@ namespace Navigator { std::lock_guard guard(m_sourceMutex); m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType())); m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow()); + m_eventBuilder.ClearAll(); if (m_source->IsValid()) { NAV_INFO("Attach successful. Enabling data pull..."); @@ -119,7 +118,7 @@ namespace Navigator { void PhysicsLayer::RunSource() { - HistogramMap& histMap = HistogramMap::GetInstance(); + SpectrumManager& manager = SpectrumManager::GetInstance(); NavEvent event; NavData datum; @@ -151,8 +150,8 @@ namespace Navigator { for (auto& stage : m_physStack) stage->AnalyzePhysicsEvent(event); - histMap.UpdateHistograms(); - ParameterMap::GetInstance().InvalidateParameters(); + manager.UpdateHistograms(); + manager.InvalidateParameters(); } } } diff --git a/Navigator/src/Navigator/Physics/PhysicsLayer.h b/Navigator/src/Navigator/Physics/PhysicsLayer.h index 37c753d..21a6c3f 100644 --- a/Navigator/src/Navigator/Physics/PhysicsLayer.h +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.h @@ -2,7 +2,6 @@ #define PHYSICS_LAYER_H #include "Navigator/NavCore.h" -#include "Navigator/HistogramMap.h" #include "Navigator/Layer.h" #include "Navigator/Events/PhysicsEvent.h" #include "AnalysisStack.h" diff --git a/Navigator/src/Navigator/SpectrumManager.cpp b/Navigator/src/Navigator/SpectrumManager.cpp new file mode 100644 index 0000000..97a1790 --- /dev/null +++ b/Navigator/src/Navigator/SpectrumManager.cpp @@ -0,0 +1,273 @@ +#include "SpectrumManager.h" + +#include "implot.h" + +namespace Navigator { + + SpectrumManager* SpectrumManager::s_instance = new SpectrumManager(); + + SpectrumManager::SpectrumManager() + { + } + + SpectrumManager::~SpectrumManager() + { + } + + void SpectrumManager::AddHistogram(const HistogramParameters& params) + { + std::lock_guard guard(m_managerMutex); + if (params.y_par == "None") + m_histoMap[params.name].reset(new Histogram1D(params)); + else + m_histoMap[params.name].reset(new Histogram2D(params)); + } + + void SpectrumManager::RemoveHistogram(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + m_histoMap.erase(name); + } + + void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(histoname); + if (iter != m_histoMap.end()) + iter->second->AddCutToBeDrawn(cutname); + } + + void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(histoname); + if (iter != m_histoMap.end()) + iter->second->AddCutToBeApplied(cutname); + } + + void SpectrumManager::UpdateHistograms() + { + std::lock_guard guard(m_managerMutex); + bool cutFlag; + for (auto& pair : m_histoMap) + { + cutFlag = true; + for (auto& cutname : pair.second->GetParameters().cutsAppliedTo) + { + if (!IsInsideCut(cutname)) + { + cutFlag = false; + break; + } + } + if (!cutFlag) + continue; + + if (pair.second->Is1D()) + { + auto iterX = m_paramMap.find(pair.second->GetXParam()); + if (iterX != m_paramMap.end() && iterX->second->validFlag) + pair.second->FillData(iterX->second->value); + } + else if (pair.second->Is2D()) + { + auto iterX = m_paramMap.find(pair.second->GetXParam()); + auto iterY = m_paramMap.find(pair.second->GetYParam()); + if (iterX != m_paramMap.end() && iterY != m_paramMap.end() && iterX->second->validFlag && iterY->second->validFlag) + pair.second->FillData(iterX->second->value, iterY->second->value); + } + } + } + + void SpectrumManager::ClearHistograms() + { + std::lock_guard guard(m_managerMutex); + for (auto& pair : m_histoMap) + pair.second->ClearData(); + } + + void SpectrumManager::ClearHistogram(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(name); + if (iter != m_histoMap.end()) + iter->second->ClearData(); + } + + void SpectrumManager::DrawHistogram(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(name); + if (iter != m_histoMap.end()) + { + iter->second->Draw(); + for (auto& cutname : iter->second->GetParameters().cutsDrawnUpon) + DrawCut(cutname); + } + } + + const HistogramParameters& SpectrumManager::GetHistogramParams(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(name); + if (iter != m_histoMap.end()) + return iter->second->GetParameters(); + else + return m_nullHistoResult; + } + + StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_histoMap.find(name); + if (iter != m_histoMap.end()) + return iter->second->AnalyzeRegion(region.X.Min, region.X.Max, region.Y.Min, region.Y.Max); + else + return StatResults(); + } + + std::vector SpectrumManager::GetListOfHistograms() + { + std::lock_guard guard(m_managerMutex); + std::vector list; + list.reserve(m_histoMap.size()); + for (auto& gram : m_histoMap) + { + list.push_back(gram.second->GetParameters()); + } + return list; + } + + void SpectrumManager::BindParameter(NavParameter& param) + { + std::lock_guard guard(m_managerMutex); + auto iter = m_paramMap.find(param.GetName()); + if (iter == m_paramMap.end()) + { + m_paramMap[param.GetName()].reset(new ParameterData()); + } + + param.m_pdata = m_paramMap[param.GetName()]; + } + + void SpectrumManager::InvalidateParameters() + { + std::lock_guard guard(m_managerMutex); + for (auto& param : m_paramMap) + { + param.second->validFlag = false; + param.second->value = 0.0; + } + } + + std::vector SpectrumManager::GetListOfParameters() + { + std::lock_guard guard(m_managerMutex); + std::vector list; + list.reserve(m_paramMap.size()); + for (auto iter : m_paramMap) + { + list.push_back(iter.first); + } + + return list; + } + + void SpectrumManager::RemoveCut(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + m_cutMap.erase(name); + RemoveCutFromHistograms(name); + } + + std::vector SpectrumManager::GetCutXPoints(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + std::vector null_result; + auto iter = m_cutMap.find(name); + if (iter != m_cutMap.end()) + return iter->second->GetXValues(); + return null_result; + } + + std::vector SpectrumManager::GetCutYPoints(const std::string& name) + { + std::lock_guard guard(m_managerMutex); + std::vector null_result; + auto iter = m_cutMap.find(name); + if (iter != m_cutMap.end()) + return iter->second->GetYValues(); + return null_result; + } + + std::vector SpectrumManager::GetListOfCuts() + { + std::lock_guard guard(m_managerMutex); + std::vector list; + list.reserve(m_cutMap.size()); + for (auto& entry : m_cutMap) + list.push_back(entry.second->GetCutParams()); + return list; + } + + /* + Private Functions + Can only be called from within the SpectrumManager, therefore the lock should already have been aquired by + whatever parent function calls them + */ + + //private helper function; does not need thread-locked + void SpectrumManager::RemoveCutFromHistograms(const std::string& cutname) + { + for (auto& gram : m_histoMap) + { + auto& params = gram.second->GetParameters(); + for (size_t i = 0; i < params.cutsDrawnUpon.size(); ++i) + { + if (params.cutsDrawnUpon[i] == cutname) + { + params.cutsDrawnUpon.erase(params.cutsDrawnUpon.begin() + i); + } + } + for (size_t i = 0; i < params.cutsAppliedTo.size(); ++i) + { + if (params.cutsAppliedTo[i] == cutname) + { + params.cutsAppliedTo.erase(params.cutsAppliedTo.begin() + i); + } + } + } + } + + void SpectrumManager::DrawCut(const std::string& name) + { + auto iter = m_cutMap.find(name); + if (iter != m_cutMap.end()) + iter->second->Draw(); + } + + bool SpectrumManager::IsInsideCut(const std::string& name) + { + bool result = false; + auto iter = m_cutMap.find(name); + if (iter != m_cutMap.end()) + { + const std::string& xpar = iter->second->GetXParameter(); + const std::string& ypar = iter->second->GetYParameter(); + if (iter->second->Is1D()) + { + auto iterX = m_paramMap.find(xpar); + if (iterX != m_paramMap.end() && iterX->second->validFlag) + result = iter->second->IsInside(iterX->second->value); + } + else if (iter->second->Is2D()) + { + auto iterX = m_paramMap.find(xpar); + auto iterY = m_paramMap.find(ypar); + if (iterX != m_paramMap.end() && iterX->second->validFlag && iterY != m_paramMap.end() && iterY->second->validFlag) + result = iter->second->IsInside(iterX->second->value, iterY->second->value); + } + } + return result; + } +} \ No newline at end of file diff --git a/Navigator/src/Navigator/SpectrumManager.h b/Navigator/src/Navigator/SpectrumManager.h new file mode 100644 index 0000000..da888d8 --- /dev/null +++ b/Navigator/src/Navigator/SpectrumManager.h @@ -0,0 +1,71 @@ +#ifndef SPECTRUM_MANAGER_H +#define SPECTRUM_MANAGER_H + +#include "Histogram.h" +#include "Cut.h" +#include "Parameter.h" + +#include + +struct ImPlotRect; + +namespace Navigator { + + class NAV_API SpectrumManager + { + public: + SpectrumManager(); + ~SpectrumManager(); + + inline static SpectrumManager& GetInstance() { return *s_instance; } + + void AddHistogram(const HistogramParameters& params); + void RemoveHistogram(const std::string& name); + void AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname); + void AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname); + void UpdateHistograms(); + void ClearHistograms(); + void ClearHistogram(const std::string& name); + void DrawHistogram(const std::string& name); + const HistogramParameters& GetHistogramParams(const std::string& name); + StatResults AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region); + std::vector GetListOfHistograms(); + + void BindParameter(NavParameter& param); + void InvalidateParameters(); + std::vector GetListOfParameters(); + + inline void AddCut(const CutParams& params, double min, double max) + { + std::lock_guard guard(m_managerMutex); + m_cutMap[params.name].reset(new Cut1D(params, min, max)); + } + inline void AddCut(const CutParams& params, const std::vector& xpoints, const std::vector& ypoints) + { + std::lock_guard guard(m_managerMutex); + m_cutMap[params.name].reset(new Cut2D(params, xpoints, ypoints)); + } + void RemoveCut(const std::string& name); + std::vector GetCutXPoints(const std::string& name); + std::vector GetCutYPoints(const std::string& name); + std::vector GetListOfCuts(); + + private: + void RemoveCutFromHistograms(const std::string& cutname); + void DrawCut(const std::string& name); + bool IsInsideCut(const std::string& name); + + static SpectrumManager* s_instance; + + std::unordered_map> m_histoMap; + std::unordered_map> m_cutMap; + std::unordered_map> m_paramMap; + + HistogramParameters m_nullHistoResult; + + std::mutex m_managerMutex; + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/SpectrumSerializer.cpp b/Navigator/src/Navigator/SpectrumSerializer.cpp index 42e9fbe..b19419a 100644 --- a/Navigator/src/Navigator/SpectrumSerializer.cpp +++ b/Navigator/src/Navigator/SpectrumSerializer.cpp @@ -1,4 +1,5 @@ #include "SpectrumSerializer.h" +#include "SpectrumManager.h" #include @@ -13,8 +14,7 @@ namespace Navigator { void SpectrumSerializer::SerializeData(const std::vector& histoList, const std::vector& cutList) { - //HistogramMap& histMap = HistogramMap::GetInstance(); - CutMap& cutMap = CutMap::GetInstance(); + SpectrumManager& manager = SpectrumManager::GetInstance(); std::ofstream output(m_filename); if (!output.is_open()) @@ -28,7 +28,7 @@ namespace Navigator { { if (cut.y_par == "None") { - auto xpoints = cutMap.GetCutXPoints(cut.name); + std::vector xpoints = manager.GetCutXPoints(cut.name); output << "\tbegin_cut1D" << std::endl; output << "\t\tname: " << cut.name << std::endl; output << "\t\txparam: " << cut.x_par << std::endl; @@ -38,8 +38,8 @@ namespace Navigator { } else { - auto xpoints = cutMap.GetCutXPoints(cut.name); - auto ypoints = cutMap.GetCutYPoints(cut.name); + std::vector xpoints = manager.GetCutXPoints(cut.name); + std::vector ypoints = manager.GetCutYPoints(cut.name); output << "\tbegin_cut2D" << std::endl; output << "\t\tname: " << cut.name << std::endl; output << "\t\txparam: " << cut.x_par << std::endl; @@ -121,8 +121,7 @@ namespace Navigator { void SpectrumSerializer::DeserializeData() { - HistogramMap& histMap = HistogramMap::GetInstance(); - CutMap& cutMap = CutMap::GetInstance(); + SpectrumManager& manager = SpectrumManager::GetInstance(); std::ifstream input(m_filename); if (!input.is_open()) @@ -156,7 +155,7 @@ namespace Navigator { input >> check >> value_doub; cut_xdata.push_back(value_doub); input >> check; - cutMap.AddCut(cut_data, cut_xdata[0], cut_xdata[1]); + manager.AddCut(cut_data, cut_xdata[0], cut_xdata[1]); } else if (check == "begin_cut2D") { @@ -182,7 +181,7 @@ namespace Navigator { cut_ydata.push_back(std::stod(check)); } input >> check; - cutMap.AddCut(cut_data, cut_xdata, cut_ydata); + manager.AddCut(cut_data, cut_xdata, cut_ydata); } else if (check == "end_cuts") break; @@ -229,7 +228,7 @@ namespace Navigator { } } input >> check; - histMap.AddHistogram(hist_data); + manager.AddHistogram(hist_data); } else if (check == "begin_histogram2D") { @@ -265,7 +264,7 @@ namespace Navigator { } } input >> check; - histMap.AddHistogram(hist_data); + manager.AddHistogram(hist_data); } else if (check == "end_histograms") break; diff --git a/Navigator/src/Navigator/SpectrumSerializer.h b/Navigator/src/Navigator/SpectrumSerializer.h index 7508f69..3f65201 100644 --- a/Navigator/src/Navigator/SpectrumSerializer.h +++ b/Navigator/src/Navigator/SpectrumSerializer.h @@ -1,8 +1,8 @@ #ifndef SPECTRUM_SERIALIZER_H #define SPECTRUM_SERIALIZER_H -#include "HistogramMap.h" -#include "CutMap.h" +#include "Histogram.h" +#include "Cut.h" namespace Navigator {