1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-23 02:38:52 -05:00

Fixed a small bug in the RemoveSpectrum line. Added exporting for histograms to .csv files.

This commit is contained in:
Gordon McCann 2022-02-26 23:38:54 -05:00
parent dca824dc2e
commit b805cf9292
5 changed files with 99 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#include "EditorLayer.h" #include "EditorLayer.h"
#include "imgui.h" #include "imgui.h"
#include "misc/cpp/imgui_stdlib.h"
#include "implot.h" #include "implot.h"
#include "FileDialog.h" #include "FileDialog.h"
#include "Navigator/Application.h" #include "Navigator/Application.h"
@ -11,7 +12,7 @@
namespace Navigator { namespace Navigator {
EditorLayer::EditorLayer() : EditorLayer::EditorLayer() :
Layer("EditorLayer"), m_removeHistogram(false), m_removeCut(false) Layer("EditorLayer"), m_removeHistogram(false), m_removeCut(false), m_exportHistogram(false)
{ {
} }
@ -152,6 +153,14 @@ namespace Navigator {
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Export"))
{
if (ImGui::MenuItem(ICON_FA_SAVE "\tAs .csv"))
{
m_exportHistogram = true;
}
ImGui::EndMenu();
}
ImGui::EndMenuBar(); ImGui::EndMenuBar();
} }
@ -181,6 +190,8 @@ namespace Navigator {
RemoveCutDialog(); RemoveCutDialog();
ExportHistogramDialog();
if(m_spectrumPanel.OnImGuiRender(m_histoList, m_cutList, m_paramList)) if(m_spectrumPanel.OnImGuiRender(m_histoList, m_cutList, m_paramList))
{ {
UpdateCutList(); UpdateCutList();
@ -254,7 +265,7 @@ namespace Navigator {
if (ImGui::Selectable(gram.name.c_str(), gram.name == selectedGram, ImGuiSelectableFlags_DontClosePopups)) if (ImGui::Selectable(gram.name.c_str(), gram.name == selectedGram, ImGuiSelectableFlags_DontClosePopups))
selectedGram = gram.name; selectedGram = gram.name;
} }
ImGui::EndPopup(); ImGui::EndCombo();
} }
if (ImGui::Button("Ok")) if (ImGui::Button("Ok"))
{ {
@ -306,4 +317,69 @@ namespace Navigator {
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }
void EditorLayer::ExportHistogramDialog()
{
static std::string filename = "";
static HistogramParameters selectedGram = HistogramParameters();
if(m_exportHistogram)
{
filename = "";
selectedGram = HistogramParameters();
m_exportHistogram = false;
ImGui::OpenPopup("Export Histogram");
}
if(ImGui::BeginPopupModal("Export Histogram"))
{
if(ImGui::BeginCombo("Histogram", selectedGram.name.c_str()))
{
for (auto& gram : m_histoList)
{
if (ImGui::Selectable(gram.name.c_str(), gram.name == selectedGram.name, ImGuiSelectableFlags_DontClosePopups))
selectedGram = gram;
}
ImGui::EndCombo();
}
ImGui::InputText("File", &filename);
ImGui::SameLine();
if(ImGui::Button("Open"))
{
m_fileDialog.SetSaveFileDialog(true);
}
std::string result = m_fileDialog.ImGuiRenderSaveFile(".csv");
if(!result.empty())
filename = result;
if(ImGui::Button("Ok"))
{
ExportHistogram(selectedGram, filename);
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if(ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
}
void EditorLayer::ExportHistogram(HistogramParameters selectedGram, const std::string& filename)
{
std::ofstream output(filename);
if(!output.is_open())
{
NAV_ERROR("Unable to create export file {0}. Check pathing.", filename);
return;
}
std::vector<double> data = SpectrumManager::GetInstance().GetBinData(selectedGram.name);
output<<"Histogram Name,"<<selectedGram.name<<std::endl;
output<<"Min X,"<<selectedGram.min_x<<std::endl<<"Max X,"<<selectedGram.max_x<<std::endl;
if(selectedGram.y_par != "None")
output<<"Min Y,"<<selectedGram.min_y<<std::endl<<"Max Y,"<<selectedGram.max_y<<std::endl;
output<<"Nbins,"<<data.size()<<std::endl;
output<<"Bin,Counts"<<std::endl;
for(size_t i=0; i<data.size(); i++)
output<<i<<","<<data[i]<<std::endl;
output.close();
}
} }

View File

@ -33,9 +33,11 @@ namespace Navigator {
private: private:
void RemoveCutDialog(); void RemoveCutDialog();
void RemoveHistogramDialog(); void RemoveHistogramDialog();
void ExportHistogramDialog();
void UpdateHistogramList(); void UpdateHistogramList();
void UpdateCutList(); void UpdateCutList();
void UpdateParameterList(); //Currently not really used, only once. Params all made at construction time of PhysicsLayer void UpdateParameterList(); //Currently not really used, only once. Params all made at construction time of PhysicsLayer
void ExportHistogram(HistogramParameters selectedGram, const std::string& filename);
EventCallbackFunc m_callbackFunc; EventCallbackFunc m_callbackFunc;
@ -58,6 +60,7 @@ namespace Navigator {
bool m_removeHistogram; bool m_removeHistogram;
bool m_removeCut; bool m_removeCut;
bool m_exportHistogram;
}; };
} }

View File

@ -61,6 +61,7 @@ namespace Navigator {
inline virtual bool Is1D() const { return false; } inline virtual bool Is1D() const { return false; }
inline virtual bool Is2D() const { return false; } inline virtual bool Is2D() const { return false; }
inline virtual float* GetColorScaleRange() { return nullptr; } inline virtual float* GetColorScaleRange() { return nullptr; }
inline virtual std::vector<double> GetBinData() { return std::vector<double>(); }
inline HistogramParameters& GetParameters() { return m_params; } inline HistogramParameters& GetParameters() { return m_params; }
inline const std::string& GetXParam() const { return m_params.x_par; }; inline const std::string& GetXParam() const { return m_params.x_par; };
inline const std::string& GetYParam() const { return m_params.y_par; }; inline const std::string& GetYParam() const { return m_params.y_par; };
@ -84,6 +85,7 @@ namespace Navigator {
virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override;
inline virtual bool Is1D() const override { return true; } inline virtual bool Is1D() const override { return true; }
inline virtual bool Is2D() const override { return false; } inline virtual bool Is2D() const override { return false; }
inline virtual std::vector<double> GetBinData() override { return m_binCounts; }
private: private:
void InitBins(); void InitBins();
@ -105,6 +107,7 @@ namespace Navigator {
virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override; virtual StatResults AnalyzeRegion(double x_min, double x_max, double y_min = 0.0, double y_max = 0.0) override;
inline virtual bool Is1D() const override { return false; } inline virtual bool Is1D() const override { return false; }
inline virtual bool Is2D() const override { return true; } inline virtual bool Is2D() const override { return true; }
inline virtual std::vector<double> GetBinData() override { return m_binCounts; }
inline virtual float* GetColorScaleRange() override { return m_colorScaleRange; } inline virtual float* GetColorScaleRange() override { return m_colorScaleRange; }

View File

@ -128,6 +128,18 @@ namespace Navigator {
return nullptr; return nullptr;
} }
std::vector<double> SpectrumManager::GetBinData(const std::string& name)
{
std::lock_guard<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end())
{
return iter->second->GetBinData();
}
return std::vector<double>();
}
StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region) StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::lock_guard<std::mutex> guard(m_managerMutex);

View File

@ -36,6 +36,7 @@ namespace Navigator {
void DrawHistogram(const std::string& name); void DrawHistogram(const std::string& name);
const HistogramParameters& GetHistogramParams(const std::string& name); const HistogramParameters& GetHistogramParams(const std::string& name);
float* GetColorScaleRange(const std::string& name); float* GetColorScaleRange(const std::string& name);
std::vector<double> GetBinData(const std::string& name);
StatResults AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region); StatResults AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region);
std::vector<HistogramParameters> GetListOfHistograms(); std::vector<HistogramParameters> GetListOfHistograms();