Mantid
Loading...
Searching...
No Matches
MuonPeriodInfo.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidAPI/Run.h"
10
11#include <QString>
12
13#include "boost/algorithm/string.hpp"
14
15#include <algorithm>
16#include <bitset>
17
19
20namespace {
21const std::string PERIOD_INFO_NOT_FOUND = "Not found";
22const std::string NOT_DAQ_STRING = "-";
23const std::string DAQ = "1";
24const std::string DWELL = "2";
25const std::string CYCLES_NOT_FOUND = "Number of period cycles not found";
26const std::string DEFAULT_TITLE = "Period Information for Run(s) ";
27const QStringList HEADERS{"Period Count", "Period Name", "Type", "DAQ Number",
28 "Frames", "Total Frames", "Counts", "Tag"};
29const QString HEADER_STYLE = "QHeaderView { font-weight: bold; }";
30} // namespace
31
32using namespace Mantid::Kernel;
33using namespace Mantid::API;
34
41std::string MuonPeriodInfo::readSampleLog(const MatrixWorkspace_sptr &ws, const std::string &logName) {
42 try {
43 return ws->run().getLogData(logName)->value();
44 } catch (const std::runtime_error &) {
45 Logger("MuonPeriodInfo").warning("Workspace does not contain " + logName);
46 return "";
47 }
48}
49
56std::vector<std::string> MuonPeriodInfo::parseSampleLog(const std::string &log, const std::string &delim) {
57 if (log.empty())
58 return std::vector<std::string>();
59 std::vector<std::string> values;
60 boost::split(values, log, boost::is_any_of(delim));
61 return values;
62}
63
70std::vector<std::vector<std::string>> MuonPeriodInfo::makeCorrections(std::vector<std::vector<std::string>> &logs) {
71 // Find max size of logs and assume to be the correct size
72 const auto it = std::max_element(logs.cbegin(), logs.cend(),
73 [](const auto &log1, const auto &log2) { return log1.size() < log2.size(); });
74 size_t maxSize = it == logs.cend() ? 0 : (*it).size();
75 // Check size of each log and make corrections where needed
76 for (size_t i = 0; i < logs.size(); ++i) {
77 if (logs[i].empty()) {
78 logs[i] = std::vector<std::string>(maxSize, PERIOD_INFO_NOT_FOUND);
79 } else {
80 while (logs[i].size() < maxSize) {
81 logs[i].emplace_back(PERIOD_INFO_NOT_FOUND);
82 }
83 }
84 }
85 return logs;
86}
87
92MuonPeriodInfo::MuonPeriodInfo(QWidget *parent) : QWidget(parent), m_numberOfSequences(-1), m_DAQCount(0) {
93 m_uiForm.setupUi(this);
94
95 // Set default label text and default title and set up the table columns
96 m_uiForm.label->setText(QString::fromStdString(CYCLES_NOT_FOUND));
98 setUpTable();
99}
100
106std::vector<std::vector<std::string>> MuonPeriodInfo::getInfo(const MatrixWorkspace_sptr &ws) {
107 auto names = parseSampleLog(readSampleLog(ws, "period_labels"), ";");
108 auto types = parseSampleLog(readSampleLog(ws, "period_type"), ";");
109 auto frames = parseSampleLog(readSampleLog(ws, "frames_period_requested"), ";");
110 auto total_frames = parseSampleLog(readSampleLog(ws, "frames_period_Raw"), ";");
111 auto counts = parseSampleLog(readSampleLog(ws, "total_counts_period"), ";");
112 auto tags = parseSampleLog(readSampleLog(ws, "period_output"), ";");
113 std::vector<std::vector<std::string>> logs = {names, types, frames, total_frames, counts, tags};
114 return makeCorrections(logs);
115}
116
122 if (auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(ws)) {
123 // Read in period sequences
124 const auto numSeq = readSampleLog(matrixWS, "period_sequences");
125 if (!numSeq.empty())
126 setNumberOfSequences(std::stoi(numSeq));
127 else
129
130 // Get remaining logs and add to table
131 auto logs = getInfo(matrixWS);
132 for (size_t i = 0; i < logs[0].size(); ++i) {
133 addPeriodToTable(logs[0][i], logs[1][i], logs[2][i], logs[3][i], logs[4][m_DAQCount], logs[5][i]);
134 }
135 } else {
136 Logger("MuonPeriodInfo").warning("Could not read workspace");
137 }
138}
139
149void MuonPeriodInfo::addPeriodToTable(const std::string &name, const std::string &type, const std::string &frames,
150 const std::string &totalFrames, const std::string &counts,
151 const std::string &tag) {
152 const auto row = m_uiForm.table->rowCount();
153 m_uiForm.table->insertRow(row);
154 m_uiForm.table->setItem(row, 0, createNewItem(std::to_string(row + 1)));
155 m_uiForm.table->setItem(row, 1, createNewItem(name));
156 if (type == DAQ) {
157 ++m_DAQCount;
158 m_uiForm.table->setItem(row, 2, createNewItem("DAQ"));
159 m_uiForm.table->setItem(row, 3, createNewItem(std::to_string(m_DAQCount)));
160 m_uiForm.table->setItem(row, 6, createNewItem(counts));
161 } else if (type == DWELL) {
162 m_uiForm.table->setItem(row, 2, createNewItem("DWELL"));
163 m_uiForm.table->setItem(row, 3, createNewItem(NOT_DAQ_STRING));
164 m_uiForm.table->setItem(row, 6, createNewItem(NOT_DAQ_STRING));
165 }
166 m_uiForm.table->setItem(row, 4, createNewItem(frames));
167 m_uiForm.table->setItem(row, 5, createNewItem(totalFrames));
168 try {
169 m_uiForm.table->setItem(row, 7, createNewItem(std::bitset<4>(std::stoi(tag)).to_string()));
170 } catch (...) {
171 m_uiForm.table->setItem(row, 7, createNewItem(PERIOD_INFO_NOT_FOUND));
172 }
173}
174
179void MuonPeriodInfo::setWidgetTitleRuns(const std::string &title) {
180 if (title.empty())
181 this->setWindowTitle(QString::fromStdString(DEFAULT_TITLE));
182 else
183 this->setWindowTitle(QString::fromStdString(DEFAULT_TITLE + title));
184}
185
186std::string MuonPeriodInfo::getWidgetTitleRuns() const { return this->windowTitle().toStdString(); }
187
194void MuonPeriodInfo::setNumberOfSequences(const int numberOfSequences) {
195 m_numberOfSequences = numberOfSequences;
196 if (numberOfSequences < 0)
197 m_uiForm.label->setText(QString::fromStdString(CYCLES_NOT_FOUND));
198 else
199 m_uiForm.label->setText(
200 QString::fromStdString("Run contains " + std::to_string(numberOfSequences) + " cycles of periods"));
201}
202
204
205std::string MuonPeriodInfo::getNumberOfSequencesString() const { return m_uiForm.label->text().toStdString(); }
206
208
213 m_DAQCount = 0;
216 for (int i = m_uiForm.table->rowCount(); i >= 0; --i)
217 m_uiForm.table->removeRow(i);
218}
219
223bool MuonPeriodInfo::isEmpty() const { return m_uiForm.table->rowCount() <= 0; }
224
225QTableWidget *MuonPeriodInfo::getTable() const { return m_uiForm.table; }
226
231 m_uiForm.table->setColumnCount(HEADERS.size());
232 m_uiForm.table->setHorizontalHeaderLabels(QStringList(HEADERS));
233 m_uiForm.table->horizontalHeader()->setStyleSheet(HEADER_STYLE);
234 m_uiForm.table->verticalHeader()->setVisible(false);
235 auto header = m_uiForm.table->horizontalHeader();
236 for (int i = 0; i < HEADERS.size(); ++i)
237 header->setSectionResizeMode(i, QHeaderView::ResizeToContents);
238}
239
245QTableWidgetItem *MuonPeriodInfo::createNewItem(const std::string &value) const {
246 auto item = new QTableWidgetItem();
247 item->setText(QString::fromStdString(value));
248 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
249 return item;
250}
251
252} // namespace MantidQt::MantidWidgets
double value
The value of the point.
Definition: FitMW.cpp:51
int getNumberOfSequences() const
Get the number of sequences as an int.
std::vector< std::vector< std::string > > getInfo(const Mantid::API::MatrixWorkspace_sptr &ws)
Gets all sample log data related to periods.
static std::vector< std::string > parseSampleLog(const std::string &log, const std::string &delim)
Splits a string separated by a delimeter.
static std::vector< std::vector< std::string > > makeCorrections(std::vector< std::vector< std::string > > &logs)
Unifies the length of all vectors given.
void setNumberOfSequences(const int numberOfSequences)
Set the number of sequences that was gathered.
std::string getNumberOfSequencesString() const
Get the number of sequences as a string.
int getDAQCount() const
Get the number of DAQ periods currently stored in the table.
void clear()
Clear the widget of all information.
void setWidgetTitleRuns(const std::string &title)
Set the title of the widget.
std::string getWidgetTitleRuns() const
Get the title of the widget.
void setUpTable()
Set up properties of the widgets table.
void addPeriodToTable(const std::string &name, const std::string &type, const std::string &frames, const std::string &totalFrames, const std::string &counts, const std::string &tag)
Add a period to the table on the widget.
MuonPeriodInfo(QWidget *parent=nullptr)
Constructor.
void addInfo(const Mantid::API::Workspace_sptr &ws)
Takes the workspace and adds it's period info to the table if any.
bool isEmpty() const
Checks if the table is empty.
QTableWidgetItem * createNewItem(const std::string &value) const
Creates a new table widget item from a string to add to the table.
static std::string readSampleLog(const Mantid::API::MatrixWorkspace_sptr &ws, const std::string &logName)
Reads the data of the sample log from the workspace.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string to_string(const wide_integer< Bits, Signed > &n)