EnergySpectrumAnalyer/src/ParticleInjectTimeView/ParticleInjectTimeAnalysis.cpp

171 lines
4.7 KiB
C++
Raw Normal View History

#include "ParticleInjectTimeAnalysis.h"
#include "ui_ParticleInjectTimeAnalysis.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QwtPlotCurve>
#include <QwtPlotMarker>
#include <QwtPlotZoneItem>
#include <QwtPlotCanvas>
#include <QwtLegend>
#include <QwtText>
#include <cmath>
#include <QtMath>
#include "CustomQwtPlot.h"
#include <QDebug>
#include <GlobalDefine.h>
ParticleInjectTimeAnalysis::ParticleInjectTimeAnalysis(QWidget *parent) :
MeasureAnalysisView(parent),
ui(new Ui::ParticleInjectTimeAnalysis)
{
ui->setupUi(this);
InitUi();
}
ParticleInjectTimeAnalysis::~ParticleInjectTimeAnalysis()
{
delete ui;
}
void ParticleInjectTimeAnalysis::InitViewWorkspace(const QString &project_name)
{
}
void ParticleInjectTimeAnalysis::SetAnalyzeDataFilename(const QMap<QString, QVariant> &data_files_set)
{
if(!data_files_set.isEmpty())
{
m_AllData = getParticleInjectTimeData(data_files_set.first().toString());
setData(m_AllData);
}
}
void ParticleInjectTimeAnalysis::setData(QVector<ParticleInjectTime> data)
{
int energyStart = ui->label_energyStart->text().toInt();
int energyEnd = ui->label_energyEnd->text().toInt();
QVector<double> x;
QVector<double> y;
double minValue = 0;
double maxValue = 0;
for(auto info : data)
{
if(info.dEnergy <= energyStart || info.dEnergy >= energyEnd)
{
continue;
}
x.append(info.index);
y.append(info.dTime);
minValue = qMin(minValue, info.dTime);
maxValue = qMax(maxValue, info.dTime);
}
// 创建曲线并设置数据
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setSamples(x, y);
// 将曲线添加到 CustomQwtPlot 中(会自动分配颜色)
plot->AddCurve(curve);
// 设置坐标轴范围和标题
// plot->setAxisScale(QwtPlot::xBottom, 0, x.last());
// plot->setAxisScale(QwtPlot::yLeft,minValue , maxValue);
plot->setAxisTitle(QwtPlot::xBottom, "粒子序号");
plot->setAxisTitle(QwtPlot::yLeft, "粒子时间");
LOG_INFO(QStringLiteral(u"%1数据读取完毕.").arg(this->GetViewName()));
// 刷新绘图
plot->replot();
}
QVector<ParticleInjectTime> ParticleInjectTimeAnalysis::getParticleInjectTimeData(QString path)
{
QVector<ParticleInjectTime> records;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "无法打开文件:" << file.errorString();
return records;
}
QTextStream stream(&file);
stream.setCodec("UTF-8");
int lineNumber = 0;
while (!stream.atEnd())
{
QString line = stream.readLine().trimmed();
lineNumber++;
// 跳过空行
if (line.isEmpty())
continue;
// 按逗号分割
QStringList fields = line.split(',');
// 检查字段数量是否正确应该为4
if (fields.size() != 4) {
qWarning() << "" << lineNumber << "字段数量不正确,跳过:" << line;
continue;
}
//获取板卡号通道号
int bd = fields[0].toInt();
int ch = fields[1].toInt();
int detector = bd + ch * 8;
if(detector >= 32)
{
continue;
}
bool ok;
ParticleInjectTime rec;
rec.index = lineNumber;
rec.dEnergy = fields[2].toDouble(&ok);
if (!ok) { qWarning() << "道址格式错误,行" << rec.dEnergy << lineNumber; continue; }
rec.dTime = fields[3].toLongLong(&ok);
if (!ok) { qWarning() << "时间计数格式错误,行" << fields[3] << rec.dTime <<lineNumber; continue; }
records.append(rec);
}
file.close();
return records;
}
void ParticleInjectTimeAnalysis::InitUi()
{
plot = new CustomQwtPlot();
plot->SetXaxisDragScale(true);
setupPlot();
ui->verticalLayout_2->addWidget(plot);
}
void ParticleInjectTimeAnalysis::setupPlot()
{
plot->setCanvasBackground(Qt::white);
QwtPlotCanvas* canvas = qobject_cast<QwtPlotCanvas*>(plot->canvas());
canvas->setFrameStyle(QFrame::NoFrame);
plot->setAxisTitle(QwtPlot::xBottom, QString(QStringLiteral(u"道址")));
plot->setAxisTitle(QwtPlot::yLeft, QString(QStringLiteral(u"计数")));
// set axis auto scale
plot->setAxisAutoScale(QwtPlot::xBottom, true);
plot->setAxisAutoScale(QwtPlot::yLeft, true);
// 启用网格线
plot->enableAxis(QwtPlot::xBottom);
plot->enableAxis(QwtPlot::yLeft);
// 设置QWT图例
QwtLegend* legend = new QwtLegend();
legend->setDefaultItemMode(QwtLegendData::ReadOnly);
plot->insertLegend(legend, QwtPlot::RightLegend);
plot->SetXaxisDragScale(true);
}