1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 08:55:37 +00:00

Plot: object to plot a DoubleFunction

This commit is contained in:
Antonin Portelli 2014-03-03 18:33:53 +00:00
parent 2b678ac72a
commit af246dc5d3
3 changed files with 92 additions and 42 deletions

View File

@ -37,7 +37,8 @@ int main(void)
MinuitMinimizer minimizer;
data.fitAllPoints();
p = data.fit(model, minimizer, init, true, Minimizer::Verbosity::Debug);
p = data.fit(model, minimizer, init, true, Minimizer::Verbosity::Normal);
cout << "a= " << p(0) << " b= " << p(1)
<< " chi^2/ndof= " << p.getChi2PerDof() << endl;
@ -45,6 +46,7 @@ int main(void)
Plot plot;
plot << LogScale(Axis::y) << PlotData(data);
plot << Color("rgb 'blue'") << PlotFunction(p.getModel(), 0.0, 10.0);
plot.display();
return EXIT_SUCCESS;

View File

@ -52,6 +52,36 @@ void PlotObject::pushTmpFile(const std::string &fileName)
tmpFileName_.push(fileName);
}
// PlotObject dump a matrix to a temporary file ////////////////////////////////
string PlotObject::dumpToTmpFile(const DMat &m)
{
char tmpFileName[MAX_PATH_LENGTH];
int fd;
FILE *tmpFile;
for (Index j = 0; j < m.cols(); ++j)
{
}
strcpy(tmpFileName, "./latan_plot_tmp.XXXXXX.dat");
fd = mkstemps(tmpFileName, 4);
if (fd == -1)
{
LATAN_ERROR(System, "impossible to create a temporary file from template "
+ strFrom(tmpFileName));
}
tmpFile = fdopen(fd, "w");
for (Index i = 0; i < m.rows(); ++i)
{
for (Index j = 0; j < m.cols(); ++j)
{
fprintf(tmpFile, "%e ", m(i, j));
}
fprintf(tmpFile, "\n");
}
fclose(tmpFile);
return string(tmpFileName);
}
// PlotObject test /////////////////////////////////////////////////////////////
bool PlotObject::gotTmpFile(void) const
@ -61,41 +91,42 @@ bool PlotObject::gotTmpFile(void) const
// PlotCommand constructor /////////////////////////////////////////////////////
PlotCommand::PlotCommand(const string &command)
: command_(command)
{}
{
setCommand(command);
}
// PlotData constructor ////////////////////////////////////////////////////////
PlotData::PlotData(const XYStatData &data, const int i, const int j)
: data_(data)
, i_(i)
, j_(j)
PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
{
DMat d(data_.getNData(), 4);
char tmpFileName[MAX_PATH_LENGTH];
int fd;
FILE *tmpFile;
string usingCmd;
DMat d(data.getNData(), 4);
string usingCmd, tmpFileName;
d.col(0) = data_.x(i_, _);
d.col(2) = data_.y(j_, _);
d.col(1) = data_.xxVar(i_, i_).diagonal().array().sqrt();
d.col(3) = data_.yyVar(i_, i_).diagonal().array().sqrt();
usingCmd = (data_.isXExact(i_)) ? "u 1:3:4 w yerr" : "u 1:2:3:4 w xyerr";
strcpy(tmpFileName, "./latan_plot_tmp.XXXXXX.dat");
fd = mkstemps(tmpFileName, 4);
if (fd == -1)
{
LATAN_ERROR(System, "impossible to create a temporary file from template "
+ strFrom(tmpFileName));
}
tmpFile = fdopen(fd, "w");
for (Index r = 0; r < data_.getNData(); ++r)
{
fprintf(tmpFile, "%e %e %e %e\n", d(r, 0), d(r, 1), d(r, 2), d(r, 3));
}
fclose(tmpFile);
d.col(0) = data.x(i);
d.col(2) = data.y(j);
d.col(1) = data.xxVar(i, i).diagonal().array().sqrt();
d.col(3) = data.yyVar(i, i).diagonal().array().sqrt();
usingCmd = (data.isXExact(i)) ? "u 1:3:4 w yerr" : "u 1:2:3:4 w xyerr";
tmpFileName = dumpToTmpFile(d);
pushTmpFile(tmpFileName);
setCommand("'" + string(tmpFileName) + "' " + usingCmd);
setCommand("'" + tmpFileName + "' " + usingCmd);
}
// PlotFunction constructor ////////////////////////////////////////////////////
PlotFunction::PlotFunction(const DoubleFunction &function, const double xMin,
const double xMax, const unsigned int nSample)
{
DMat d(nSample, 2);
string tmpFileName;
double dx = (xMax - xMin)/static_cast<double>(nSample - 1);
for (Index i = 0; i < nSample; ++i)
{
d(i, 0) = xMin + i*dx;
d(i, 1) = function(d(i, 0));
}
tmpFileName = dumpToTmpFile(d);
pushTmpFile(tmpFileName);
setCommand("'" + tmpFileName + "' u 1:2 w lines");
}
/******************************************************************************
@ -172,6 +203,15 @@ Plot & Plot::operator<<(PlotObject &&command)
commandStr += " lc " + options_.lineColor;
options_.lineColor = "";
}
if (options_.title.empty())
{
commandStr += " notitle";
}
else
{
commandStr += " t '" + options_.title + "'";
options_.title = "";
}
plotCommand_.push_back(commandStr);
return *this;

View File

@ -21,6 +21,7 @@
#define Latan_Plot_hpp_
#include <latan/Global.hpp>
#include <latan/Mat.hpp>
#include <latan/XYStatData.hpp>
#include <sstream>
#include <stack>
@ -54,6 +55,8 @@ protected:
// access
void pushTmpFile(const std::string &fileName);
void setCommand(const std::string &command);
// dump a matrix to a temporary file
std::string dumpToTmpFile(const DMat &m);
private:
// plot command
std::string command_;
@ -64,24 +67,29 @@ private:
class PlotCommand: public PlotObject
{
public:
// constructors
// constructor
explicit PlotCommand(const std::string &command);
// destructor
virtual ~PlotCommand(void) = default;
private:
std::string command_;
};
class PlotData: public PlotObject
{
public:
// constructors
explicit PlotData(const XYStatData &data, const int i = 0, const int j = 0);
// constructor
PlotData(const XYStatData &data, const Index i = 0, const Index j = 0);
// destructor
virtual ~PlotData(void) = default;
private:
const XYStatData &data_;
const int i_, j_;
};
class PlotFunction: public PlotObject
{
public:
// constructor
PlotFunction(const DoubleFunction &function, const double xMin,
const double xMax, const unsigned int nSample = 1000);
// destructor
virtual ~PlotFunction(void) = default;
};
/******************************************************************************