1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-10 19:20:44 +01: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,14 +37,16 @@ int main(void)
MinuitMinimizer minimizer; MinuitMinimizer minimizer;
data.fitAllPoints(); 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) cout << "a= " << p(0) << " b= " << p(1)
<< " chi^2/ndof= " << p.getChi2PerDof() << endl; << " chi^2/ndof= " << p.getChi2PerDof() << endl;
// plot result // plot result
Plot plot; Plot plot;
plot << LogScale(Axis::y) << PlotData(data); plot << LogScale(Axis::y) << PlotData(data);
plot << Color("rgb 'blue'") << PlotFunction(p.getModel(), 0.0, 10.0);
plot.display(); plot.display();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -52,6 +52,36 @@ void PlotObject::pushTmpFile(const std::string &fileName)
tmpFileName_.push(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 ///////////////////////////////////////////////////////////// // PlotObject test /////////////////////////////////////////////////////////////
bool PlotObject::gotTmpFile(void) const bool PlotObject::gotTmpFile(void) const
@ -61,41 +91,42 @@ bool PlotObject::gotTmpFile(void) const
// PlotCommand constructor ///////////////////////////////////////////////////// // PlotCommand constructor /////////////////////////////////////////////////////
PlotCommand::PlotCommand(const string &command) PlotCommand::PlotCommand(const string &command)
: command_(command) {
{} setCommand(command);
}
// PlotData constructor //////////////////////////////////////////////////////// // PlotData constructor ////////////////////////////////////////////////////////
PlotData::PlotData(const XYStatData &data, const int i, const int j) PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
: data_(data)
, i_(i)
, j_(j)
{ {
DMat d(data_.getNData(), 4); DMat d(data.getNData(), 4);
char tmpFileName[MAX_PATH_LENGTH]; string usingCmd, tmpFileName;
int fd;
FILE *tmpFile;
string usingCmd;
d.col(0) = data_.x(i_, _); d.col(0) = data.x(i);
d.col(2) = data_.y(j_, _); d.col(2) = data.y(j);
d.col(1) = data_.xxVar(i_, i_).diagonal().array().sqrt(); d.col(1) = data.xxVar(i, i).diagonal().array().sqrt();
d.col(3) = data_.yyVar(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"; usingCmd = (data.isXExact(i)) ? "u 1:3:4 w yerr" : "u 1:2:3:4 w xyerr";
strcpy(tmpFileName, "./latan_plot_tmp.XXXXXX.dat"); tmpFileName = dumpToTmpFile(d);
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);
pushTmpFile(tmpFileName); 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");
} }
/****************************************************************************** /******************************************************************************
@ -169,8 +200,17 @@ Plot & Plot::operator<<(PlotObject &&command)
commandStr = command.getCommand(); commandStr = command.getCommand();
if (!options_.lineColor.empty()) if (!options_.lineColor.empty())
{ {
commandStr += " lc " + options_.lineColor; commandStr += " lc " + options_.lineColor;
options_.lineColor = ""; options_.lineColor = "";
}
if (options_.title.empty())
{
commandStr += " notitle";
}
else
{
commandStr += " t '" + options_.title + "'";
options_.title = "";
} }
plotCommand_.push_back(commandStr); plotCommand_.push_back(commandStr);

View File

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