From 13a7805cde6c15f5491ee4eec0ff419a2e502e7e Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 9 Jan 2022 14:06:10 -0500 Subject: [PATCH] Added HistogramMap class. Fixed ParameterMap usage, now isolated to the evb. --- NavProject/main.cpp | 2 + Navigator/src/Navigator/Application.cpp | 12 +--- Navigator/src/Navigator/Application.h | 5 ++ Navigator/src/Navigator/Histogram.h | 10 +++- Navigator/src/Navigator/HistogramMap.cpp | 57 +++++++++++++++++++ Navigator/src/Navigator/HistogramMap.h | 40 +++++++++++++ Navigator/src/Navigator/ParameterMap.cpp | 9 +++ Navigator/src/Navigator/ParameterMap.h | 2 +- .../src/Navigator/Physics/AnalysisStage.h | 3 +- .../Navigator/Physics/PhysicsEventBuilder.cpp | 25 +++++++- .../Navigator/Physics/PhysicsEventBuilder.h | 4 ++ 11 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 Navigator/src/Navigator/HistogramMap.cpp create mode 100644 Navigator/src/Navigator/HistogramMap.h diff --git a/NavProject/main.cpp b/NavProject/main.cpp index 1b15d09..b91d4a4 100644 --- a/NavProject/main.cpp +++ b/NavProject/main.cpp @@ -10,7 +10,9 @@ int main(int argc, const char** argv) auto app = Navigator::CreateApplication(); auto evb = Navigator::CreatePhysicsEventBuilder(); + Navigator::Application::LinkHistogramMap(); app->Run(); delete app; + delete evb; } \ No newline at end of file diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 45912fa..e32ffea 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -1,5 +1,4 @@ #include "Application.h" -#include "ParameterMap.h" #include "Renderer/Renderer.h" #include "Renderer/RenderCommand.h" @@ -15,16 +14,10 @@ namespace Navigator { m_window = std::unique_ptr(Window::Create()); m_window->SetEventCallback(BIND_EVENT_FUNCTION(Application::OnEvent)); - /*order is important, must be pMap then evb*/ - CreateParameterMap(); - - NavParameter par("joseph","mama"); - par.SetValue(8); - NAV_INFO("Does the par exist? {0}", ParameterMap::GetInstance().IsParameterValid("joseph")); - NAV_INFO("What is its value? {0}", ParameterMap::GetInstance().GetParameterValue("joseph")); - PushLayer(new Layer()); + m_histMap.AddHistogram("myHisto", "joseph", 100, 0, 10); + m_imgui_layer = new ImGuiLayer(); PushOverlay(m_imgui_layer); } @@ -111,6 +104,7 @@ namespace Navigator { { //PhysicsStartEvent junk("/media/gordon/GordonData/gwm17/NavTests/data/", DataSource::SourceType::CompassOffline, 2000000); //OnEvent(junk); + m_histMap.UpdateHistograms(); while(m_runFlag) { diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index 6807464..1032c86 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -10,6 +10,7 @@ #include "Navigator/Window.h" #include "Navigator/ImGui/ImGuiLayer.h" #include "Navigator/Physics/PhysicsEventBuilder.h" +#include "Navigator/HistogramMap.h" #include namespace Navigator { @@ -26,7 +27,10 @@ namespace Navigator { void PushLayer(Layer* layer); void PushOverlay(Layer* layer); + inline void AttachHistogramMap() { PhysicsEventBuilder::Get().AttachHistogramMap(&m_histMap); } + inline static Application& Get() { return *s_instance; } + inline static void LinkHistogramMap() { s_instance->AttachHistogramMap(); } inline Window& GetWindow() { return *m_window; } @@ -40,6 +44,7 @@ namespace Navigator { std::thread* m_physThread; LayerStack m_stack; + HistogramMap m_histMap; std::unique_ptr m_window; ImGuiLayer* m_imgui_layer; bool m_runFlag; diff --git a/Navigator/src/Navigator/Histogram.h b/Navigator/src/Navigator/Histogram.h index 8e55d93..e4b9f09 100644 --- a/Navigator/src/Navigator/Histogram.h +++ b/Navigator/src/Navigator/Histogram.h @@ -8,15 +8,19 @@ namespace Navigator { class NAV_API Histogram { public: + Histogram() : + m_name("None"), m_xParam("None"), m_yParam("None"), m_initFlag(false) + { + } Histogram(const std::string& name, const std::string& param_x, const std::string& param_y="None") : m_name(name), m_xParam(param_x), m_yParam(param_y), m_initFlag(false) { } virtual ~Histogram() {}; - virtual void FillData(double x, double y=0) = 0; - virtual void Draw() = 0; - virtual void ClearData() = 0; + virtual void FillData(double x, double y = 0) { NAV_WARN("Trying to fill a default histogram!"); } + virtual void Draw() {} + virtual void ClearData() {} inline const std::string& GetXParam() const { return m_xParam; }; inline const std::string& GetYParam() const { return m_yParam; }; inline const std::string& GetName() const { return m_name; } diff --git a/Navigator/src/Navigator/HistogramMap.cpp b/Navigator/src/Navigator/HistogramMap.cpp new file mode 100644 index 0000000..a88475a --- /dev/null +++ b/Navigator/src/Navigator/HistogramMap.cpp @@ -0,0 +1,57 @@ +#include "HistogramMap.h" +#include "ParameterMap.h" + +namespace Navigator { + Navigator::HistogramMap::HistogramMap() + { + } + + Navigator::HistogramMap::~HistogramMap() + { + } + + void Navigator::HistogramMap::UpdateHistograms() + { + std::lock_guard guard(m_histMutex); + std::string xpar, ypar; + ParameterMap& pmap = ParameterMap::GetInstance(); + for (auto& pair : m_map) + { + xpar = pair.second->GetXParam(); + ypar = pair.second->GetYParam(); + + if (ypar == "None") + { + auto iter = pmap.find(xpar); + if (iter == pmap.end() || !iter->second->validFlag) + { + continue; + } + else + { + pair.second->FillData(iter->second->value); + } + } + else + { + auto iterx = pmap.find(xpar); + auto itery = pmap.find(ypar); + if (iterx == pmap.end() || itery == pmap.end() || !iterx->second->validFlag || !itery->second->validFlag) + continue; + else + { + pair.second->FillData(iterx->second->value, itery->second->value); + } + + } + } + } + + //Only to be used within ImGui context!! + void Navigator::HistogramMap::DrawHistograms() + { + std::lock_guard guard(m_histMutex); + for (auto& pair : m_map) + pair.second->Draw(); + } +} \ No newline at end of file diff --git a/Navigator/src/Navigator/HistogramMap.h b/Navigator/src/Navigator/HistogramMap.h new file mode 100644 index 0000000..fa283fe --- /dev/null +++ b/Navigator/src/Navigator/HistogramMap.h @@ -0,0 +1,40 @@ +#ifndef HISTOGRAM_MAP_H +#define HISTOGRAM_MAP_H + +#include "NavCore.h" +#include "Histogram.h" +#include + +namespace Navigator { + + class NAV_API HistogramMap + { + public: + + using Iter = std::unordered_map>::iterator; + + HistogramMap(); + ~HistogramMap(); + + inline void AddHistogram(const std::string& name, const std::string& param, int bins, double min, double max) { m_map[name].reset(new Histogram1D(name, param, bins, min, max)); } + inline void AddHistogram(const std::string& name, const std::string& paramx, const std::string& paramy, int bins_x, double min_x, double max_x, + int bins_y, double min_y, double max_y) + { + m_map[name].reset(new Histogram2D(name, paramx, paramy, bins_x, min_x, max_x, bins_y, min_y, max_y)); + } + + void UpdateHistograms(); + + void DrawHistograms(); + + inline Iter begin() { return m_map.begin(); } + inline Iter end() { return m_map.end(); } + + private: + std::unordered_map> m_map; + std::mutex m_histMutex; + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/ParameterMap.cpp b/Navigator/src/Navigator/ParameterMap.cpp index 6fc5a3d..9c64aa4 100644 --- a/Navigator/src/Navigator/ParameterMap.cpp +++ b/Navigator/src/Navigator/ParameterMap.cpp @@ -60,4 +60,13 @@ namespace Navigator { else return false; } + + void ParameterMap::InvalidateParameters() + { + for(auto& iter : m_map) + { + iter.second->validFlag = false; + iter.second->value = 0.0; + } + } } diff --git a/Navigator/src/Navigator/ParameterMap.h b/Navigator/src/Navigator/ParameterMap.h index c31c437..a7ca367 100644 --- a/Navigator/src/Navigator/ParameterMap.h +++ b/Navigator/src/Navigator/ParameterMap.h @@ -50,7 +50,7 @@ namespace Navigator { inline void SetParameter(const std::string& name, std::shared_ptr& param) { param = m_map[name]; } double GetParameterValue(const std::string& name); bool IsParameterValid(const std::string& name); - void ResetParameters(); + void InvalidateParameters(); inline Iter end() { return m_map.end(); } inline Iter begin() { return m_map.begin(); } inline Iter find(const std::string& name) { return m_map.find(name); } diff --git a/Navigator/src/Navigator/Physics/AnalysisStage.h b/Navigator/src/Navigator/Physics/AnalysisStage.h index 7c626cd..3021609 100644 --- a/Navigator/src/Navigator/Physics/AnalysisStage.h +++ b/Navigator/src/Navigator/Physics/AnalysisStage.h @@ -2,6 +2,7 @@ #define ANALYSIS_STAGE_H #include "Navigator/NavCore.h" +#include "Navigator/ParameterMap.h" #include "CompassHit.h" namespace Navigator { @@ -16,8 +17,6 @@ namespace Navigator { virtual void AnalyzeRawPhysicsEvent(const RawPhysicsEvent& event) {}; - void AttachParameterMap() {}; - inline std::string GetName() { return m_name; } private: std::string m_name; diff --git a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp index 9b828fb..fefca4c 100644 --- a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp +++ b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp @@ -1,16 +1,15 @@ #include "PhysicsEventBuilder.h" +#include "Navigator/ParameterMap.h" //temp #include "CompassHit.h" -//GWM Jan. 3 2021 -- Make DataSource to unique ptr - namespace Navigator { PhysicsEventBuilder* PhysicsEventBuilder::s_instance = nullptr; PhysicsEventBuilder::PhysicsEventBuilder() : - m_runFlag(false), m_source(nullptr) + m_runFlag(false), m_source(nullptr), m_histMap(nullptr) { if(s_instance != nullptr) { @@ -18,6 +17,13 @@ namespace Navigator { return; } s_instance = this; + + CreateParameterMap(); + + NavParameter par("joseph", "mama"); + par.SetValue(8); + NAV_INFO("Does the par exist? {0}", ParameterMap::GetInstance().IsParameterValid("joseph")); + NAV_INFO("What is its value? {0}", ParameterMap::GetInstance().GetParameterValue("joseph")); } PhysicsEventBuilder::~PhysicsEventBuilder() @@ -64,6 +70,11 @@ namespace Navigator { { NAV_WARN("Trying to Run PhysicsEventBuilder without a Data Source, killing thread!"); } + else if (m_histMap == nullptr || m_source == nullptr) + { + NAV_WARN("Internal state of PhysicsEventBuilder not set properly! Either histogram map or data source not initialized!"); + return; + } CompassHit hit; @@ -105,7 +116,15 @@ namespace Navigator { //NAV_INFO("Obtaining built event..."); auto event = m_rawSort.GetRawPhysicsEvent(); //NAV_INFO("Built event size: {0}", event.size()); + for (auto& stage : m_physStack) + stage->AnalyzeRawPhysicsEvent(event); + m_histMap->UpdateHistograms(); + + //Cleanup to be ready for next event + ParameterMap::GetInstance().InvalidateParameters(); m_rawSort.ClearRawPhysicsEvent(); + //Need to add hit in hand, start new event + m_rawSort.AddHit(hit); } } diff --git a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h index d15f426..8f81354 100644 --- a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h +++ b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h @@ -2,6 +2,7 @@ #define PHYSICS_EVENT_BUILDER_H #include "Navigator/NavCore.h" +#include "Navigator/HistogramMap.h" #include "AnalysisStack.h" #include "AnalysisStage.h" #include "PhysicsHitSort.h" @@ -25,6 +26,7 @@ namespace Navigator { void SetCoincidenceWindow(uint64_t window) { m_rawSort.SetCoincidenceWindow(window); } void PushStage(AnalysisStage* stage); bool IsRunning() { return m_runFlag; } + inline void AttachHistogramMap(HistogramMap* map) { m_histMap = map; } static PhysicsEventBuilder& Get() { return *s_instance; } @@ -38,6 +40,8 @@ namespace Navigator { std::unique_ptr m_source; + HistogramMap* m_histMap; //Not owned by PhysicsEventBuilder! + }; NAV_API PhysicsEventBuilder* CreatePhysicsEventBuilder();