From ccaae9947237bfe25806385136ab7e320c67a311 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Tue, 18 Jan 2022 22:13:55 -0500 Subject: [PATCH] Switched to single threaded application. Reduce complexity, until proven needed --- Navigator/src/Navigator/Application.cpp | 66 ++------- Navigator/src/Navigator/Application.h | 18 +-- Navigator/src/Navigator/CutMap.cpp | 3 - Navigator/src/Navigator/CutMap.h | 3 - .../src/Navigator/Editor/EditorLayer.cpp | 6 +- Navigator/src/Navigator/Editor/EditorLayer.h | 3 +- .../src/Navigator/Editor/SpectrumPanel.cpp | 9 +- .../src/Navigator/Editor/SpectrumPanel.h | 4 +- Navigator/src/Navigator/HistogramMap.cpp | 9 +- Navigator/src/Navigator/HistogramMap.h | 16 +-- Navigator/src/Navigator/ParameterMap.cpp | 9 +- Navigator/src/Navigator/ParameterMap.h | 6 +- .../Navigator/Physics/PhysicsEventBuilder.cpp | 133 ------------------ .../Navigator/Physics/PhysicsEventBuilder.h | 51 ------- .../src/Navigator/Physics/PhysicsLayer.cpp | 121 ++++++++++++++++ .../src/Navigator/Physics/PhysicsLayer.h | 43 ++++++ 16 files changed, 203 insertions(+), 297 deletions(-) delete mode 100644 Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp delete mode 100644 Navigator/src/Navigator/Physics/PhysicsEventBuilder.h create mode 100644 Navigator/src/Navigator/Physics/PhysicsLayer.cpp create mode 100644 Navigator/src/Navigator/Physics/PhysicsLayer.h diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 87c8a45..39efea1 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -11,45 +11,33 @@ namespace Navigator { Application* Application::s_instance = nullptr; Application::Application() : - m_runFlag(true), m_physThread(nullptr) + m_runFlag(true) { s_instance = this; m_window = std::unique_ptr(Window::Create()); m_window->SetEventCallback(BIND_EVENT_FUNCTION(Application::OnEvent)); - PushLayer(new EditorLayer(&m_histMap)); - - m_histMap.AddHistogram("myHisto", "joseph", 100, 0, 10); - m_histMap.AddHistogram("myHisto2D", "joseph", "joseph", 100, 0, 10, 100, 0, 10); + m_physicsLayer = new PhysicsLayer(); + PushLayer(m_physicsLayer); + PushLayer(new EditorLayer()); + m_imgui_layer = new ImGuiLayer(); + PushOverlay(m_imgui_layer); + HistogramMap& histMap = HistogramMap::GetInstance(); + histMap.AddHistogram("myHisto", "joseph", 100, 0, 10); + histMap.AddHistogram("myHisto2D", "joseph", "joseph", 100, 0, 10, 100, 0, 10); CutMap::GetInstance().AddCut("joe_cut","joseph",0.0, 7.0); CutMap::GetInstance().AddCut("joe2D_cut", "joseph", "joseph", { 1.0, 3.0, 3.0, 1.0, 1.0}, { 1.0, 1.0, 3.0, 3.0, 1.0}); - m_histMap.AddCutToHistogramDraw("joe_cut", "myHisto"); - m_histMap.AddCutToHistogramDraw("joe2D_cut", "myHisto2D"); + histMap.AddCutToHistogramDraw("joe_cut", "myHisto"); + histMap.AddCutToHistogramDraw("joe2D_cut", "myHisto2D"); - m_imgui_layer = new ImGuiLayer(); - PushOverlay(m_imgui_layer); + } Application::~Application() { - NAV_INFO("Detaching PhysicsEventBuilder at shutdown"); - PhysicsEventBuilder::Get().DetachDataSource(); - DestroyPhysThread(); - } - - void Application::DestroyPhysThread() - { - if(m_physThread != nullptr && m_physThread->joinable()) - m_physThread->join(); - - if(m_physThread != nullptr) - { - delete m_physThread; - m_physThread = nullptr; - } } void Application::SetParameterList() @@ -63,8 +51,6 @@ namespace Navigator { { EventDispatcher dispatch(event); dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent)); - dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnPhysicsStartEvent)); - dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnPhysicsStopEvent)); for(auto iter = m_stack.end(); iter != m_stack.begin(); ) { (*(--iter))->OnEvent(event); @@ -80,32 +66,6 @@ namespace Navigator { return true; } - bool Application::OnPhysicsStartEvent(PhysicsStartEvent& event) - { - if(PhysicsEventBuilder::Get().IsRunning()) - { - PhysicsEventBuilder::Get().DetachDataSource(); - NAV_INFO("Stopping the event builder..."); - DestroyPhysThread(); - } - PhysicsEventBuilder::Get().AttachDataSource(event.GetSourceLocation(), event.GetSourceType()); - PhysicsEventBuilder::Get().SetCoincidenceWindow(event.GetCoincidenceWindow()); - if(PhysicsEventBuilder::Get().IsRunning()) - { - NAV_INFO("Starting the event builder..."); - m_physThread = new std::thread(&PhysicsEventBuilder::Run, std::ref(PhysicsEventBuilder::Get())); - } - return true; - } - - bool Application::OnPhysicsStopEvent(PhysicsStopEvent& event) - { - PhysicsEventBuilder::Get().DetachDataSource(); - NAV_INFO("Stopping the event builder..."); - DestroyPhysThread(); - return true; - } - void Application::PushLayer(Layer* layer) { m_stack.PushLayer(layer); @@ -122,7 +82,7 @@ namespace Navigator { { //PhysicsStartEvent junk("/media/gordon/GordonData/gwm17/NavTests/data/", DataSource::SourceType::CompassOffline, 2000000); //OnEvent(junk); - m_histMap.UpdateHistograms(); + HistogramMap::GetInstance().UpdateHistograms(); while(m_runFlag) { diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index b2f511e..32471d7 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -9,9 +9,8 @@ #include "Navigator/Layer.h" #include "Navigator/Window.h" #include "Navigator/ImGui/ImGuiLayer.h" -#include "Navigator/Physics/PhysicsEventBuilder.h" +#include "Navigator/Physics/PhysicsLayer.h" #include "Navigator/HistogramMap.h" -#include namespace Navigator { @@ -25,31 +24,24 @@ namespace Navigator { void OnEvent(Event& event); void PushLayer(Layer* layer); + inline void PushAnalysisStage(AnalysisStage* stage) { m_physicsLayer->PushStage(stage); } void PushOverlay(Layer* layer); void SetParameterList(); - inline void AttachHistogramMap() { PhysicsEventBuilder::Get().AttachHistogramMap(&m_histMap); } - inline const std::vector& GetParameterList() { return m_parameterList; } //Thread-safe way to access a list of the available parameters (i.e. for the editor) + inline const std::vector& GetParameterList() { return m_parameterList; } inline static Application& Get() { return *s_instance; } - inline static void LinkHistogramMap() { s_instance->AttachHistogramMap(); } //IMPORTANT: Only use BEFORE calling Run(). NO guarantee of thread safety. - inline static void LinkParameterList() { s_instance->SetParameterList(); } //IMPORTANT: Only use BEFORE calling Run(). NO guarantee of thread safety. + inline static void LinkParameterList() { s_instance->SetParameterList(); } inline Window& GetWindow() { return *m_window; } private: bool OnWindowCloseEvent(WindowCloseEvent& event); - bool OnPhysicsStartEvent(PhysicsStartEvent& event); - bool OnPhysicsStopEvent(PhysicsStopEvent& event); - void DestroyPhysThread(); - - std::thread* m_physThread; - LayerStack m_stack; - HistogramMap m_histMap; std::unique_ptr m_window; ImGuiLayer* m_imgui_layer; + PhysicsLayer* m_physicsLayer; std::vector m_parameterList; bool m_runFlag; diff --git a/Navigator/src/Navigator/CutMap.cpp b/Navigator/src/Navigator/CutMap.cpp index f89ffe8..c286717 100644 --- a/Navigator/src/Navigator/CutMap.cpp +++ b/Navigator/src/Navigator/CutMap.cpp @@ -73,7 +73,6 @@ namespace Navigator { void CutMap::DrawCut(const std::string& name) { - std::lock_guard lock(m_cutMutex); auto iter = m_map.find(name); if(iter != m_map.end()) iter->second->Draw(); @@ -81,7 +80,6 @@ namespace Navigator { bool CutMap::IsInsideCut(const std::string& name, double xval, double yval) { - std::lock_guard lock(m_cutMutex); bool result = false; auto iter = m_map.find(name); if(iter != m_map.end()) @@ -91,7 +89,6 @@ namespace Navigator { std::vector CutMap::GetListOfCutParams() { - std::lock_guard lock(m_cutMutex); std::vector list; list.reserve(m_map.size()); for(auto& entry : m_map) diff --git a/Navigator/src/Navigator/CutMap.h b/Navigator/src/Navigator/CutMap.h index 692fed5..4f2d7f9 100644 --- a/Navigator/src/Navigator/CutMap.h +++ b/Navigator/src/Navigator/CutMap.h @@ -3,7 +3,6 @@ #include "NavCore.h" #include "imgui.h" -#include namespace Navigator { @@ -82,12 +81,10 @@ namespace Navigator { inline void AddCut(const std::string& name, const std::string& xpar, double min, double max) { - std::lock_guard guard(m_cutMutex); m_map[name].reset(new Cut1D(name, xpar, min, max)); } inline void AddCut(const std::string& name, const std::string& xpar, const std::string& ypar, const std::vector& xpoints, const std::vector& ypoints) { - std::lock_guard guard(m_cutMutex); m_map[name].reset(new Cut2D(name, xpar, ypar, xpoints, ypoints)); } diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index e74f0a8..9a5bfb4 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -6,8 +6,8 @@ namespace Navigator { - EditorLayer::EditorLayer(HistogramMap* hmap) : - Layer("EditorLayer"), m_histMap(hmap), m_spectrumPanel(hmap) + EditorLayer::EditorLayer() : + Layer("EditorLayer"), m_spectrumPanel() { } @@ -33,7 +33,7 @@ namespace Navigator { void EditorLayer::UpdateHistogramLists() { - m_histoList = m_histMap->GetListOfHistogramParams(); + m_histoList = HistogramMap::GetInstance().GetListOfHistogramParams(); m_spectrumPanel.UpdateActiveList(m_histoList); } diff --git a/Navigator/src/Navigator/Editor/EditorLayer.h b/Navigator/src/Navigator/Editor/EditorLayer.h index 7ee8ffc..53559fa 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.h +++ b/Navigator/src/Navigator/Editor/EditorLayer.h @@ -14,7 +14,7 @@ namespace Navigator { class EditorLayer : public Layer { public: - EditorLayer(HistogramMap* hmap); + EditorLayer(); ~EditorLayer(); virtual void OnAttach() override; @@ -28,7 +28,6 @@ namespace Navigator { void UpdateHistogramLists(); void UpdateCutLists(); bool OnPhysicsParamEvent(PhysicsParamEvent& event); - HistogramMap* m_histMap; //Not owned by the EditorLayer!! std::vector m_histoList; std::vector m_cutList; std::vector m_paramList; diff --git a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp index e3b66e9..b4f2f75 100644 --- a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp +++ b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp @@ -3,8 +3,8 @@ namespace Navigator { - SpectrumPanel::SpectrumPanel(HistogramMap* map) : - m_histMap(map), m_zoomedFlag(false), m_zoomedGram(""), m_totalSlots(1) + SpectrumPanel::SpectrumPanel() : + m_zoomedFlag(false), m_zoomedGram(""), m_totalSlots(1) { m_tableSizes[0] = 1; m_tableSizes[1] = 1; } @@ -13,6 +13,7 @@ namespace Navigator { void SpectrumPanel::OnImGuiRender() { + HistogramMap& histMap = HistogramMap::GetInstance(); if (ImGui::Begin("Active View")) { if (m_activeList.size() > 0) @@ -21,7 +22,7 @@ namespace Navigator { { if (ImPlot::BeginPlot(m_zoomedGram.c_str(), ImVec2(-1, -1))) { - m_histMap->DrawHistogram(m_zoomedGram); + histMap.DrawHistogram(m_zoomedGram); if (ImPlot::IsPlotHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { NAV_INFO("We lost 'em, de-zoom and enhance!"); @@ -67,7 +68,7 @@ namespace Navigator { { if (ImPlot::BeginPlot(spec.c_str())) { - m_histMap->DrawHistogram(spec); + histMap.DrawHistogram(spec); 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 0fef182..71214e5 100644 --- a/Navigator/src/Navigator/Editor/SpectrumPanel.h +++ b/Navigator/src/Navigator/Editor/SpectrumPanel.h @@ -9,17 +9,15 @@ namespace Navigator { class NAV_API SpectrumPanel { public: - SpectrumPanel(HistogramMap* map); + SpectrumPanel(); ~SpectrumPanel(); - inline void AttachHistogramMap(HistogramMap* map) { m_histMap = map; } void OnImGuiRender(); inline const std::string& GetZoomedOnHistogram() { return m_zoomedGram; } inline const bool IsZoomed() { return m_zoomedFlag; } inline void UpdateActiveList(const std::vector& list) { m_activeList = list; } private: - HistogramMap* m_histMap; //Not owned by SpectrumPanel std::vector m_activeList; //This is where we get our info from. Reduces thread crossings std::vector m_selectedGrams; bool m_zoomedFlag; diff --git a/Navigator/src/Navigator/HistogramMap.cpp b/Navigator/src/Navigator/HistogramMap.cpp index 416fe30..f07b7c3 100644 --- a/Navigator/src/Navigator/HistogramMap.cpp +++ b/Navigator/src/Navigator/HistogramMap.cpp @@ -2,6 +2,9 @@ #include "ParameterMap.h" namespace Navigator { + + HistogramMap* HistogramMap::s_instance = new HistogramMap(); + HistogramMap::HistogramMap() { } @@ -12,7 +15,6 @@ namespace Navigator { void HistogramMap::AddCutToHistogramDraw(const std::string &cutname, const std::string &histoname) { - std::lock_guard lock(m_histMutex); auto iter = m_map.find(histoname); if(iter != m_map.end()) iter->second->AddCutToBeDrawn(cutname); @@ -20,7 +22,6 @@ namespace Navigator { void HistogramMap::AddCutToHistogramApplied(const std::string &cutname, const std::string &histoname) { - std::lock_guard lock(m_histMutex); auto iter = m_map.find(histoname); if(iter != m_map.end()) iter->second->AddCutToBeApplied(cutname); @@ -28,7 +29,6 @@ namespace Navigator { void HistogramMap::UpdateHistograms() { - std::lock_guard guard(m_histMutex); std::string xpar, ypar; ParameterMap& pmap = ParameterMap::GetInstance(); for (auto& pair : m_map) @@ -68,7 +68,6 @@ namespace Navigator { std::vector HistogramMap::GetListOfHistogramParams() { - std::lock_guard guard(m_histMutex); std::vector params; params.reserve(m_map.size()); for (auto& pair : m_map) @@ -79,7 +78,6 @@ namespace Navigator { //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(); } @@ -87,7 +85,6 @@ namespace Navigator { //Only to be used within ImGui context!! void Navigator::HistogramMap::DrawHistogram(const std::string& name) { - std::lock_guard guard(m_histMutex); 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 index 260f8b6..7dd2ff8 100644 --- a/Navigator/src/Navigator/HistogramMap.h +++ b/Navigator/src/Navigator/HistogramMap.h @@ -3,28 +3,22 @@ #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) { - std::lock_guard guard(m_histMutex); 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) { - std::lock_guard guard(m_histMutex); m_map[name].reset(new Histogram2D(name, paramx, paramy, bins_x, min_x, max_x, bins_y, min_y, max_y)); } @@ -37,13 +31,13 @@ namespace Navigator { std::vector GetListOfHistogramParams(); //thread safe access for GUI to the underlying parameters. Only needs to be called when a gram is added/removed - /*Not in general thread safe*/ - inline Iter begin() { return m_map.begin(); } - inline Iter end() { return m_map.end(); } + static HistogramMap& GetInstance() { return *s_instance; } + private: - std::unordered_map> m_map; - std::mutex m_histMutex; + std::unordered_map> m_map; + + static HistogramMap* s_instance; }; } diff --git a/Navigator/src/Navigator/ParameterMap.cpp b/Navigator/src/Navigator/ParameterMap.cpp index 7748d61..d385975 100644 --- a/Navigator/src/Navigator/ParameterMap.cpp +++ b/Navigator/src/Navigator/ParameterMap.cpp @@ -28,17 +28,10 @@ namespace Navigator { NavParameter::~NavParameter() {} - ParameterMap* ParameterMap::s_instance = nullptr; - - ParameterMap* CreateParameterMap() { return new ParameterMap(); } + ParameterMap* ParameterMap::s_instance = new ParameterMap(); ParameterMap::ParameterMap() { - NAV_INFO("Created ParameterMap"); - if(s_instance != nullptr) - NAV_ERROR("Cannot have multiple instances of ParameterMap!!!"); - - s_instance = this; } ParameterMap::~ParameterMap() {} diff --git a/Navigator/src/Navigator/ParameterMap.h b/Navigator/src/Navigator/ParameterMap.h index cf4baaa..a95eafa 100644 --- a/Navigator/src/Navigator/ParameterMap.h +++ b/Navigator/src/Navigator/ParameterMap.h @@ -2,14 +2,13 @@ #define PARAMETER_MAP_H #include "NavCore.h" -#include namespace Navigator { struct NAV_API ParameterData { - std::atomic value=0.0; - std::atomic validFlag=false; + double value=0.0; + bool validFlag=false; }; /* @@ -64,7 +63,6 @@ namespace Navigator { }; - NAV_API ParameterMap* CreateParameterMap(); } #endif diff --git a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp b/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp deleted file mode 100644 index fefca4c..0000000 --- a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include "PhysicsEventBuilder.h" -#include "Navigator/ParameterMap.h" - -//temp -#include "CompassHit.h" - -namespace Navigator { - - PhysicsEventBuilder* PhysicsEventBuilder::s_instance = nullptr; - - PhysicsEventBuilder::PhysicsEventBuilder() : - m_runFlag(false), m_source(nullptr), m_histMap(nullptr) - { - if(s_instance != nullptr) - { - NAV_ERROR("Trying to create a second PhysicsEventBuilder, this is not allowed!"); - 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() - { - } - - void PhysicsEventBuilder::AttachDataSource(const std::string& loc, DataSource::SourceType type) - { - std::lock_guard lock(m_sourceLock); //Auto free lock at end of scope - NAV_INFO("Attempting to attach phyiscs data source at location {0}", loc); - - m_runFlag = false; - - m_source.reset(CreateDataSource(loc, type)); - if(m_source->IsValid()) - { - NAV_INFO("Attach successful. Enabling data pull..."); - m_runFlag = true; - } - else - { - NAV_ERROR("DataSource attach failed... check for error conditions."); - m_source.reset(nullptr); - } - } - - void PhysicsEventBuilder::DetachDataSource() - { - std::lock_guard lock(m_sourceLock); - NAV_INFO("Detaching physics data source..."); - m_runFlag = false; - m_source.reset(nullptr); - NAV_INFO("Detach successful"); - } - - void PhysicsEventBuilder::PushStage(AnalysisStage* stage) - { - m_physStack.PushStage(stage); - } - - void PhysicsEventBuilder::Run() - { - if(!m_runFlag) - { - 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; - - while(m_runFlag) - { - - //small scope for locking access to the data source - { - std::lock_guard lock(m_sourceLock); - if(m_source == nullptr) // safety for stop occuring while running - { - continue; - } - else if(m_source->IsValid()) - { - hit = m_source->GetData(); - } - - /* - Looks funny, but two conditions lead to !IsValid(). Either source prev. shutdown, - OR we reached end of source, indicated after prev. data grab - */ - if(!m_source->IsValid()) - { - NAV_INFO("End of data source."); - m_runFlag = false; - continue; - } - } - - //NAV_INFO("Doing a physics"); - if(m_rawSort.IsHitInWindow(hit)) - { - //NAV_INFO("Adding hit to event with timestamp {0}", hit.timestamp); - m_rawSort.AddHit(hit); - } - else - { - //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 deleted file mode 100644 index 8f81354..0000000 --- a/Navigator/src/Navigator/Physics/PhysicsEventBuilder.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef PHYSICS_EVENT_BUILDER_H -#define PHYSICS_EVENT_BUILDER_H - -#include "Navigator/NavCore.h" -#include "Navigator/HistogramMap.h" -#include "AnalysisStack.h" -#include "AnalysisStage.h" -#include "PhysicsHitSort.h" -#include "DataSource.h" -#include -#include - -namespace Navigator { - - class NAV_API PhysicsEventBuilder - { - public: - PhysicsEventBuilder(); - virtual ~PhysicsEventBuilder(); - - void Run(); - - void AttachDataSource(const std::string& loc, DataSource::SourceType type); - void DetachDataSource(); - - 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; } - - private: - AnalysisStack m_physStack; - std::atomic m_runFlag; //ensures defined read/write across threads - static PhysicsEventBuilder* s_instance; - PhysicsHitSort m_rawSort; - - std::mutex m_sourceLock; - - std::unique_ptr m_source; - - HistogramMap* m_histMap; //Not owned by PhysicsEventBuilder! - - }; - - NAV_API PhysicsEventBuilder* CreatePhysicsEventBuilder(); - -} - -#endif diff --git a/Navigator/src/Navigator/Physics/PhysicsLayer.cpp b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp new file mode 100644 index 0000000..dff9bf5 --- /dev/null +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.cpp @@ -0,0 +1,121 @@ +#include "PhysicsLayer.h" +#include "Navigator/ParameterMap.h" + +//temp +#include "CompassHit.h" + +namespace Navigator { + + PhysicsLayer::PhysicsLayer() : + m_activeFlag(false), m_source(nullptr) + { + + } + + PhysicsLayer::~PhysicsLayer() + { + } + + void PhysicsLayer::OnAttach() + { + 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")); + } + + void PhysicsLayer::OnDetach() + { + } + + void PhysicsLayer::OnEvent(Event& event) + { + EventDispatcher dispatch(event); + dispatch.Dispatch(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStartEvent)); + dispatch.Dispatch(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStopEvent)); + } + + bool PhysicsLayer::OnPhysicsStartEvent(PhysicsStartEvent& event) + { + m_rawSort.SetCoincidenceWindow(event.GetCoincidenceWindow()); + m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourceType())); + if (m_source->IsValid()) + { + NAV_INFO("Attach successful. Enabling data pull..."); + m_activeFlag = true; + } + else + { + NAV_ERROR("DataSource attach failed... check for error conditions."); + m_source.reset(nullptr); + } + return true; + } + + bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event) + { + NAV_INFO("Detaching physics data source..."); + m_activeFlag = false; + m_source.reset(nullptr); + NAV_INFO("Detach succesful."); + return true; + } + + void PhysicsLayer::PushStage(AnalysisStage* stage) + { + m_physStack.PushStage(stage); + } + + void PhysicsLayer::OnUpdate() + { + HistogramMap& histMap = HistogramMap::GetInstance(); + + if(!m_activeFlag) + { + return; + } + else if (m_source == nullptr || !m_source->IsValid()) + { + NAV_WARN("Internal state of PhysicsEventBuilder not set properly! Either histogram map or data source not initialized!"); + return; + } + + CompassHit hit; + hit = m_source->GetData(); + + /* + Looks funny, but two conditions lead to !IsValid(). Either source prev. shutdown, + OR we reached end of source, indicated after prev. data grab + */ + if(!m_source->IsValid()) + { + NAV_INFO("End of data source."); + m_activeFlag = false; + return; + } + + //NAV_INFO("Doing a physics"); + if(m_rawSort.IsHitInWindow(hit)) + { + //NAV_INFO("Adding hit to event with timestamp {0}", hit.timestamp); + m_rawSort.AddHit(hit); + } + else + { + //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); + 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/PhysicsLayer.h b/Navigator/src/Navigator/Physics/PhysicsLayer.h new file mode 100644 index 0000000..34c104d --- /dev/null +++ b/Navigator/src/Navigator/Physics/PhysicsLayer.h @@ -0,0 +1,43 @@ +#ifndef PHYSICS_EVENT_BUILDER_H +#define PHYSICS_EVENT_BUILDER_H + +#include "Navigator/NavCore.h" +#include "Navigator/HistogramMap.h" +#include "Navigator/Layer.h" +#include "Navigator/Events/PhysicsEvent.h" +#include "AnalysisStack.h" +#include "AnalysisStage.h" +#include "PhysicsHitSort.h" +#include "DataSource.h" + +namespace Navigator { + + class NAV_API PhysicsLayer : public Layer + { + public: + PhysicsLayer(); + virtual ~PhysicsLayer(); + + virtual void OnAttach() override; + virtual void OnUpdate() override; + virtual void OnDetach() override; + virtual void OnImGuiRender() override {}; + virtual void OnEvent(Event& event) override; + + bool OnPhysicsStartEvent(PhysicsStartEvent& event); + bool OnPhysicsStopEvent(PhysicsStopEvent& event); + + void PushStage(AnalysisStage* stage); + + private: + AnalysisStack m_physStack; + bool m_activeFlag; + PhysicsHitSort m_rawSort; + + std::unique_ptr m_source; + + }; + +} + +#endif