From df5bfe0f9fce3f422f6f5982ce7d4c0ac43c0338 Mon Sep 17 00:00:00 2001
From: Tobias Triffterer <tobias@ep1.ruhr-uni-bochum.de>
Date: Wed, 11 Aug 2021 00:47:18 +0200
Subject: [PATCH] Method to Save ROOT Files

This simply writes the histogram to a .root file.

It has still to be verified that this works within the Flatpak sandbox.
---
 src/clientgui.cpp | 22 +++++++++++++++++++++-
 src/clientgui.h   |  2 ++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/clientgui.cpp b/src/clientgui.cpp
index 5588f7c..f95b17a 100644
--- a/src/clientgui.cpp
+++ b/src/clientgui.cpp
@@ -25,10 +25,12 @@
  **/
 
 #include <QApplication>
+#include <QFileDialog>
 #include <QFontMetrics>
 #include <QMessageBox>
 #include <QVBoxLayout>
 
+#include <TFile.h>
 #include <TSystem.h>
 
 #include "clientgui.h"
@@ -62,11 +64,12 @@ ClientGui::ClientGui(const QString& name, const QString& token, std::unique_ptr<
     //connect(_ui.cmdTargetGold, &QPushButton::clicked, std::bind(&ClientGui::setBeamholeState, this, ExperimentState::BeamHoleState::GoldFoil));
     connect(_ui.cmdStartAdc, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::startAdc));
     connect(_ui.cmdStopAdc, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::stopAdc));
+    connect(_ui.cmdSaveData, &QPushButton::clicked, this, &ClientGui::saveRootFile);
     connect(_ui.cmdClearHisto, &QPushButton::clicked, this, &ClientGui::clearHistogram);
     connect(_ui.cmdAdcThresholdUp, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::adcThresholdUp));
     connect(_ui.cmdAdcThresholdDown, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::adcThresholdDown));
 
-    connect(_ui.cmdTargetGold, &QPushButton::clicked, [this] () -> void {QMessageBox::information(this, tr("Experiment Part not available"), tr("This part of the experiment is currently left out, because it has also been left out in the real experiment in the recent years.\nThis measurement only confirms the calibraton of the setup and takes a long time due to the low activity of the real-world ²⁴¹Am source."), QMessageBox::Ok);});
+    connect(_ui.cmdTargetGold, &QPushButton::clicked, [this]() -> void {QMessageBox::information(this, tr("Experiment Part not available"), tr("This part of the experiment is currently left out, because it has also been left out in the real experiment in the recent years.\nThis measurement only confirms the calibraton of the setup and takes a long time due to the low activity of the real-world ²⁴¹Am source."), QMessageBox::Ok);});
 
     gStyle->SetCanvasPreferGL(true);
 
@@ -268,6 +271,23 @@ void ClientGui::clearHistogram(const bool fromServer)
         sendSimpleActiontoServer(Protocol::Action::clearHistogram);
 }
 
+void Fp311Online::ClientGui::saveRootFile()
+{
+    const QString filename = QFileDialog::getSaveFileName(
+                                 this,
+                                 tr("Save Content of Histogram to ROOT File"),
+                                 QString(),
+                                 tr("ROOT files (*.root)")
+                             );
+    if (filename.isEmpty())
+        return;
+
+    TFile savefile(filename.toUtf8().constData(), "RECREATE");
+    savefile.cd();
+    _histo->Write("ADC");
+    savefile.Close("R");
+}
+
 void ClientGui::showEvent(QShowEvent* event)
 {
     QMainWindow::showEvent(event);
diff --git a/src/clientgui.h b/src/clientgui.h
index 411b64d..d98dbbf 100644
--- a/src/clientgui.h
+++ b/src/clientgui.h
@@ -87,6 +87,8 @@ private:
     void fillEventsIntoHistogram(const QString& adcchannels);
     void clearHistogram(const bool fromServer = false);
 
+    void saveRootFile();
+
     void showEvent(QShowEvent* event) override;
 
     const CliLogger::ComponentLogger logError = CliLogger::createComponentLogger(QStringLiteral("ClientGui"), LogLevel::Error);
-- 
GitLab