diff --git a/Navigator/src/Navigator/Core/Graph.cpp b/Navigator/src/Navigator/Core/Graph.cpp new file mode 100644 index 0000000..c185e49 --- /dev/null +++ b/Navigator/src/Navigator/Core/Graph.cpp @@ -0,0 +1,42 @@ +#include "Graph.h" +#include "implot.h" + +namespace Navigator { + + ScalerGraph::ScalerGraph(const GraphArgs& args) : + m_args(args), m_lastScalerVal(0) + { + m_xPoints.reserve(m_args.maxPoints); + m_xPoints.reserve(m_args.maxPoints); + } + + ScalerGraph::~ScalerGraph() {} + + void ScalerGraph::UpdatePoints(double timeStep, uint64_t scalerVal) + { + double rate = (scalerVal - m_lastScalerVal) / timeStep; + if (m_xPoints.size() < m_args.maxPoints) + { + m_xPoints.push_back(timeStep); + m_yPoints.push_back(rate); + } + else + { + for (size_t i = 0; i < (m_args.maxPoints - 1); i++) + { + m_xPoints[i] = m_xPoints[i + 1]; + m_yPoints[i] = m_yPoints[i + 1]; + } + m_xPoints[m_args.maxPoints - 1] = timeStep; + m_yPoints[m_args.maxPoints - 1] = rate; + } + + m_lastScalerVal = scalerVal; + } + + void ScalerGraph::Draw() + { + ImPlot::SetupAxes("time (s)", "rate (Hz)"); + ImPlot::PlotLine(m_args.name.c_str(), m_xPoints.data(), m_yPoints.data(), m_xPoints.size()); + } +} \ No newline at end of file diff --git a/Navigator/src/Navigator/Core/Graph.h b/Navigator/src/Navigator/Core/Graph.h new file mode 100644 index 0000000..1247c89 --- /dev/null +++ b/Navigator/src/Navigator/Core/Graph.h @@ -0,0 +1,41 @@ +#ifndef GRAPH_H +#define GRAPH_H + +#include + +namespace Navigator { + + struct GraphArgs + { + std::string name; + std::string scalerName; + size_t maxPoints; + }; + + class ScalerGraph + { + public: + ScalerGraph(const GraphArgs& args); + ~ScalerGraph(); + + void UpdatePoints(double timeStep, uint64_t scalerVal); + void Draw(); + inline void Clear() { m_xPoints.clear(); m_yPoints.clear(); } + + inline const std::string& GetName() const { return m_args.name; } + inline const std::string& GetScaler() const { return m_args.scalerName; } + inline const GraphArgs& GetArgs() const { return m_args; } + + private: + + GraphArgs m_args; + + uint64_t m_lastScalerVal; + + std::vector m_yPoints; + std::vector m_xPoints; + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Core/SpectrumManager.cpp b/Navigator/src/Navigator/Core/SpectrumManager.cpp index b3c8106..723f2c7 100644 --- a/Navigator/src/Navigator/Core/SpectrumManager.cpp +++ b/Navigator/src/Navigator/Core/SpectrumManager.cpp @@ -239,6 +239,78 @@ namespace Navigator { /*************Histogram Functions End*************/ + /*************Graph Functions Begin*************/ + + void SpectrumManager::AddGraph(const GraphArgs& args) + { + std::scoped_lock guard(m_managerMutex); + m_graphMap[args.name].reset(new ScalerGraph(args)); + } + + void SpectrumManager::RemoveGraph(const std::string& name) + { + std::scoped_lock guard(m_managerMutex); + m_graphMap.erase(name); + } + + void SpectrumManager::UpdateGraphs(const Timestep& step) + { + std::scoped_lock guard(m_managerMutex); + uint64_t scalerVal; + for (auto& graph : m_graphMap) + { + auto& scalerIter = m_scalerMap.find(graph.second->GetScaler()); + if (scalerIter != m_scalerMap.end()) + { + scalerVal = *(scalerIter->second); + graph.second->UpdatePoints(step, scalerVal); + } + } + } + + void SpectrumManager::ClearGraphs() + { + std::scoped_lock guard(m_managerMutex); + for (auto& graph : m_graphMap) + graph.second->Clear(); + } + + void SpectrumManager::ClearGraph(const std::string& name) + { + std::scoped_lock guard(m_managerMutex); + auto& iter = m_graphMap.find(name); + if (iter != m_graphMap.end()) + iter->second->Clear(); + } + + void SpectrumManager::DrawGraph(const std::string& name) + { + std::scoped_lock guard(m_managerMutex); + auto& iter = m_graphMap.find(name); + if (iter != m_graphMap.end()) + iter->second->Draw(); + } + + const GraphArgs& SpectrumManager::GetGraphArgs(const std::string& name) + { + std::scoped_lock guard(m_managerMutex); + auto& iter = m_graphMap.find(name); + if (iter != m_graphMap.end()) + return iter->second->GetArgs(); + return m_nullGraphResult; + } + + std::vector SpectrumManager::GetListOfGraphs() + { + std::scoped_lock guard(m_managerMutex); + std::vector list; + list.reserve(m_graphMap.size()); + for (auto& graph : m_graphMap) + list.push_back(graph.second->GetArgs()); + return list; + } + + /*************Graph Functions End*************/ /*************Parameter Functions Begin*************/ @@ -366,6 +438,24 @@ namespace Navigator { /*************Cut Functions Begin*************/ + void SpectrumManager::AddCut(const CutArgs& params, double min, double max) + { + std::scoped_lock guard(m_managerMutex); + m_cutMap[params.name].reset(new Cut1D(params, min, max)); + } + + void SpectrumManager::AddCut(const CutArgs& params, const std::vector& xpoints, const std::vector& ypoints) + { + std::scoped_lock guard(m_managerMutex); + m_cutMap[params.name].reset(new Cut2D(params, xpoints, ypoints)); + } + + void SpectrumManager::AddCut(const CutArgs& params, const std::vector& subhistos, double min, double max) + { + std::scoped_lock guard(m_managerMutex); + m_cutMap[params.name].reset(new CutSummary(params, subhistos, min, max)); + } + void SpectrumManager::RemoveCut(const std::string& name) { NAV_PROFILE_FUNCTION(); diff --git a/Navigator/src/Navigator/Core/SpectrumManager.h b/Navigator/src/Navigator/Core/SpectrumManager.h index 1241176..fa2d63d 100644 --- a/Navigator/src/Navigator/Core/SpectrumManager.h +++ b/Navigator/src/Navigator/Core/SpectrumManager.h @@ -14,6 +14,8 @@ #include "Histogram.h" #include "Cut.h" #include "Parameter.h" +#include "Graph.h" +#include "Timestep.h" #include @@ -55,6 +57,17 @@ namespace Navigator { std::vector GetListOfHistograms(); /********************/ + /*ScalerGraph Functions*/ + void AddGraph(const GraphArgs& args); + void RemoveGraph(const std::string& name); + void UpdateGraphs(const Timestep& step); + void ClearGraphs(); + void ClearGraph(const std::string& name); + void DrawGraph(const std::string& name); + const GraphArgs& GetGraphArgs(const std::string& name); + std::vector GetListOfGraphs(); + /********************/ + /*Parameter Functions*/ void BindParameter(Parameter& param); void BindParameter(Parameter& param, int nbins, double maxVal, double minVal); @@ -74,21 +87,9 @@ namespace Navigator { /******************/ /*Cut Functions*/ - inline void AddCut(const CutArgs& params, double min, double max) - { - std::scoped_lock guard(m_managerMutex); - m_cutMap[params.name].reset(new Cut1D(params, min, max)); - } - inline void AddCut(const CutArgs& params, const std::vector& xpoints, const std::vector& ypoints) - { - std::scoped_lock guard(m_managerMutex); - m_cutMap[params.name].reset(new Cut2D(params, xpoints, ypoints)); - } - inline void AddCut(const CutArgs& params, const std::vector& subhistos, double min, double max) - { - std::scoped_lock guard(m_managerMutex); - m_cutMap[params.name].reset(new CutSummary(params, subhistos, min, max)); - } + void AddCut(const CutArgs& params, double min, double max); + void AddCut(const CutArgs& params, const std::vector& xpoints, const std::vector& ypoints); + void AddCut(const CutArgs& params, const std::vector& subhistos, double min, double max); void RemoveCut(const std::string& name); std::vector GetCutXPoints(const std::string& name); std::vector GetCutYPoints(const std::string& name); @@ -112,8 +113,10 @@ namespace Navigator { std::unordered_map> m_paramMap; std::unordered_map>> m_varMap; std::unordered_map>> m_scalerMap; + std::unordered_map> m_graphMap; HistogramArgs m_nullHistoResult; //For handling bad query + GraphArgs m_nullGraphResult; //For handling bad query std::mutex m_managerMutex; //synchronization }; diff --git a/Navigator/src/Navigator/Core/Timestep.h b/Navigator/src/Navigator/Core/Timestep.h index 59e7d27..620e3c5 100644 --- a/Navigator/src/Navigator/Core/Timestep.h +++ b/Navigator/src/Navigator/Core/Timestep.h @@ -13,7 +13,7 @@ namespace Navigator { void SetTime(float time) { m_time = time; } - operator float() { return m_time; } + operator float() const { return m_time; } float GetElapsedSeconds() const { return m_time; } float GetElapsedMilliseconds() const { return m_time * 1000.0f; }