Bayeux  3.4.1
Core Foundation library for SuperNEMO
gnuplot_i.h
Go to the documentation of this file.
1 
29 
30 #ifndef GNUPLOT_PIPES_H
31 #define GNUPLOT_PIPES_H
32 
33 // Standard library:
34 #include <iostream>
35 #include <string>
36 #include <vector>
37 #include <fstream>
38 #include <sstream> // for std::ostringstream
39 #include <stdexcept>
40 #include <cstdio>
41 #include <cstdlib> // for getenv ()
42 #include <list> // for std::list
43 
44 
45 #if defined (WIN32) || defined (_WIN32) || defined (__WIN32__) || defined (__TOS_WIN__)
46 //defined for 32 and 64-bit environments
47 #include <io.h> // for _access (), _mktemp ()
48 #define GP_MAX_TMP_FILES 27 // 27 temporary files it's Microsoft restriction
49 #elif defined (unix) || defined (__unix) || defined (__unix__) || defined (__APPLE__)
50 //all UNIX-like OSs (Linux, *BSD, MacOSX, Solaris, ...)
51 #include <unistd.h> // for access (), mkstemp ()
52 #define GP_MAX_TMP_FILES 64
53 #else
54 #error unsupported or unknown operating system
55 #endif
56 
57 #include <datatools/exception.h>
58 
59 //declare classes in global namespace
60 
61 class GnuplotException : public std::runtime_error
62 {
63  public:
64  GnuplotException (const std::string &msg);
65 };
66 
67 
68 class Gnuplot
69 {
70  private:
71 
72  //----------------------------------------------------------------------------------
73  // member data
75  FILE *_gnucmd_;
77  bool _valid_;
79  bool _two_dim_;
81  int _nplots_;
83  std::string _pstyle_;
85  std::string _smooth_;
87  std::vector<std::string> _tmpfile_list_;
88 
90  std::string _terminal_;
91 
92  //----------------------------------------------------------------------------------
93  // static data
95  static int _g_tmpfile_num_;
97  static std::string _g_gnuplot_filename_;
99  static std::string _g_gnuplot_path;
101  static std::string _g_terminal_std_;
102 
103  //----------------------------------------------------------------------------------
104  // member functions (auxiliary functions)
105  // ---------------------------------------------------
111  // ---------------------------------------------------
112  void init ();
113  // ---------------------------------------------------
119  // ---------------------------------------------------
120  std::string create_tmpfile (std::ofstream &tmp);
121 
122  //----------------------------------------------------------------------------------
128  // ---------------------------------------------------------------------------------
129  static bool get_program_path ();
130 
131  // ---------------------------------------------------------------------------------
138  // ---------------------------------------------------------------------------------
139  bool file_available (const std::string &filename);
140 
141  // ---------------------------------------------------------------------------------
148  // ---------------------------------------------------------------------------------
149  static bool file_exists (const std::string &filename, int mode=0);
150 
151  public:
152 
154  static const std::string & terminal_std();
155 
156  // ----------------------------------------------------------------------------
163  // ----------------------------------------------------------------------------
164  static bool set_gnuplot_path (const std::string &path);
165 
166  // ----------------------------------------------------------------------------
173  // ----------------------------------------------------------------------------
174  static void set_terminal_std (const std::string &type);
175 
176  //-----------------------------------------------------------------------------
177  // constructors
178  // ----------------------------------------------------------------------------
179 
180 
182  Gnuplot (const std::string &style = "points");
183 
185  Gnuplot (const std::vector<double> &x,
186  const std::string &title = "",
187  const std::string &style = "points",
188  const std::string &labelx = "x",
189  const std::string &labely = "y");
190 
192  Gnuplot (const std::vector<double> &x,
193  const std::vector<double> &y,
194  const std::string &title = "",
195  const std::string &style = "points",
196  const std::string &labelx = "x",
197  const std::string &labely = "y");
198 
200  Gnuplot (const std::vector<double> &x,
201  const std::vector<double> &y,
202  const std::vector<double> &z,
203  const std::string &title = "",
204  const std::string &style = "points",
205  const std::string &labelx = "x",
206  const std::string &labely = "y",
207  const std::string &labelz = "z");
208 
210  ~Gnuplot ();
211 
213  bool has_terminal() const;
214 
216  void set_terminal(const std::string & terminal_);
217 
219  const std::string & get_terminal() const;
220 
221  //----------------------------------------------------------------------------------
222 
224  Gnuplot& cmd (const std::string &cmdstr);
225  // ---------------------------------------------------------------------------------
232  // ---------------------------------------------------------------------------------
233  inline Gnuplot& operator<< (const std::string &cmdstr){
234  cmd (cmdstr);
235  return (*this);
236  }
237 
238 
239  //----------------------------------------------------------------------------------
240  // show on screen or write to file
241 
244  Gnuplot& showonscreen ();
245 
247  Gnuplot& savetops (const std::string &filename = "gnuplot_output");
248 
249 
250  //----------------------------------------------------------------------------------
251  // set and unset
252 
256  Gnuplot& set_style (const std::string &stylestr = "points");
257 
262  Gnuplot& set_smooth (const std::string &stylestr = "csplines");
263 
264  // ----------------------------------------------------------------------
271  // ----------------------------------------------------------------------
272  inline Gnuplot& unset_smooth (){ _smooth_ = ""; return *this;};
273 
274 
276  Gnuplot& set_pointsize (const double pointsize = 1.0);
277 
279  inline Gnuplot& set_grid () {cmd ("set grid");return *this;};
281  inline Gnuplot& unset_grid (){cmd ("unset grid");return *this;};
282 
283  // -----------------------------------------------
289  // -----------------------------------------------
290  inline Gnuplot& set_multiplot (){cmd ("set multiplot") ;return *this;};
291 
292  // -----------------------------------------------
298  // -----------------------------------------------
299  inline Gnuplot& unset_multiplot (){cmd ("unset multiplot");return *this;};
300 
301 
302 
304  Gnuplot& set_samples (const int samples = 100);
306  Gnuplot& set_isosamples (const int isolines = 10);
307 
308  // --------------------------------------------------------------------------
314  // --------------------------------------------------------------------------
315  Gnuplot& set_hidden3d (){cmd ("set hidden3d");return *this;};
316 
317  // ---------------------------------------------------------------------------
323  // ---------------------------------------------------------------------------
324  inline Gnuplot& unset_hidden3d (){cmd ("unset hidden3d"); return *this;};
325 
328  Gnuplot& set_contour (const std::string &position = "base");
329  // --------------------------------------------------------------------------
335  // ------------------------------------------------------------------
336  inline Gnuplot& unset_contour (){cmd ("unset contour");return *this;};
337 
338  // ------------------------------------------------------------
344  // ------------------------------------------------------------------
345  inline Gnuplot& set_surface (){cmd ("set surface");return *this;};
346 
347  // ----------------------------------------------------------
354  // ------------------------------------------------------------------
355  inline Gnuplot& unset_surface (){cmd ("unset surface"); return *this;}
356 
357 
360  Gnuplot& set_legend (const std::string &position = "default");
361 
362  // ------------------------------------------------------------------
369  // ------------------------------------------------------------------
370  inline Gnuplot& unset_legend (){cmd ("unset key"); return *this;}
371 
372  // -----------------------------------------------------------------------
378  // -----------------------------------------------------------------------
379  inline Gnuplot& set_title (const std::string &title = "")
380  {
381  std::string cmdstr;
382  cmdstr = "set title \"";
383  cmdstr+=title;
384  cmdstr+="\"";
385  *this<<cmdstr;
386  return *this;
387  }
388 
389  //----------------------------------------------------------------------------------
396  // ---------------------------------------------------------------------------------
397  inline Gnuplot& unset_title (){this->set_title ();return *this;}
398 
399 
401  Gnuplot& set_ylabel (const std::string &label = "x");
403  Gnuplot& set_xlabel (const std::string &label = "y");
405  Gnuplot& set_zlabel (const std::string &label = "z");
406 
408  Gnuplot& set_xrange (const double iFrom,
409  const double iTo);
411  Gnuplot& set_yrange (const double iFrom,
412  const double iTo);
414  Gnuplot& set_zrange (const double iFrom,
415  const double iTo);
421  // -----------------------------------------------
422  inline Gnuplot& set_xautoscale (){cmd ("set xrange restore");cmd ("set autoscale x");return *this;};
423 
424  // -----------------------------------------------
430  // -----------------------------------------------
431  inline Gnuplot& set_yautoscale (){cmd ("set yrange restore");cmd ("set autoscale y");return *this;};
432 
433  // -----------------------------------------------
439  // -----------------------------------------------
440  inline Gnuplot& set_zautoscale (){cmd ("set zrange restore");cmd ("set autoscale z");return *this;};
441 
442 
444  Gnuplot& set_xlogscale (const double base = 10);
446  Gnuplot& set_ylogscale (const double base = 10);
448  Gnuplot& set_zlogscale (const double base = 10);
449 
450  // -----------------------------------------------
456  // -----------------------------------------------
457  inline Gnuplot& unset_xlogscale (){cmd ("unset logscale x"); return *this;};
458 
459  // -----------------------------------------------
465  // -----------------------------------------------
466  inline Gnuplot& unset_ylogscale (){cmd ("unset logscale y"); return *this;};
467 
468  // -----------------------------------------------
474  // -----------------------------------------------
475  inline Gnuplot& unset_zlogscale (){cmd ("unset logscale z"); return *this;};
476 
477 
479  Gnuplot& set_cbrange (const double iFrom, const double iTo);
480 
481 
482  //----------------------------------------------------------------------------------
483  // plot
484 
487  Gnuplot& plotfile_x (const std::string &filename,
488  const unsigned int column = 1,
489  const std::string &title = "");
490 
492  //------------------------------------------------------------------------------
493  //
495  //
496  template<typename X>
497  Gnuplot& plot_x(const X& x, const std::string &title = "")
498  {
499  DT_THROW_IF (x.size() == 0,
500  GnuplotException, "std::vector too small");
501  std::ofstream tmp;
502  std::string name = create_tmpfile(tmp);
503  if (name == "")
504  return *this;
505 
506  //
507  // write the data to file
508  //
509  for (unsigned int i = 0; i < x.size(); i++)
510  tmp << x[i] << std::endl;
511 
512  tmp.flush();
513  tmp.close();
514 
515 
516  plotfile_x(name, 1, title);
517 
518  return *this;
519  }
520  /*
521  template<typename X>
522  Gnuplot& plot_x (const X& x, const std::string &title = "");
523  */
524 
527  Gnuplot& plotfile_xy (const std::string &filename,
528  const unsigned int column_x = 1,
529  const unsigned int column_y = 2,
530  const std::string &title = "");
531 
532  Gnuplot& plotfile_xy (const std::string &filename,
533  const unsigned int column_x,
534  const unsigned int column_y,
535  const std::string & title,
536  const std::string & file_options_, // index/every...
537  const std::string & plot_options_); // with
538 
540  /*
541  template<typename X, typename Y>
542  Gnuplot& plot_xy (const X& x, const Y& y, const std::string &title = "");
543  */
544  //------------------------------------------------------------------------------
545  //
547  //
548  template<typename X, typename Y>
549  Gnuplot& plot_xy(const X& x, const Y& y, const std::string &title = "")
550  {
551  DT_THROW_IF (x.size() == 0 || y.size() == 0,
552  GnuplotException, "std::vectors too small");
553  DT_THROW_IF (x.size() != y.size(),
554  GnuplotException, "Length of the std::vectors differs");
555  std::ofstream tmp;
556  std::string name = create_tmpfile(tmp);
557  if (name == "")
558  return *this;
559 
560  //
561  // write the data to file
562  //
563  for (unsigned int i = 0; i < x.size(); i++)
564  tmp << x[i] << " " << y[i] << std::endl;
565 
566  tmp.flush();
567  tmp.close();
568 
569 
570  plotfile_xy(name, 1, 2, title);
571 
572  return *this;
573  }
574 
575 
578  Gnuplot& plotfile_xy_err (const std::string &filename,
579  const unsigned int column_x = 1,
580  const unsigned int column_y = 2,
581  const unsigned int column_dy = 3,
582  const std::string &title = "");
584  /*
585  template<typename X, typename Y, typename E>
586  Gnuplot& plot_xy_err (const X &x, const Y &y, const E &dy,
587  const std::string &title = "");
588  */
593  template<typename X, typename Y, typename E>
594  Gnuplot& plot_xy_err(const X &x,
595  const Y &y,
596  const E &dy,
597  const std::string &title = "")
598  {
599  DT_THROW_IF (x.size() == 0 || y.size() == 0 || dy.size() == 0,
600  GnuplotException, "std::vectors too small");
601  DT_THROW_IF (x.size() != y.size() || y.size() != dy.size(),
602  GnuplotException, "Length of the std::vectors differs");
603  std::ofstream tmp;
604  std::string name = create_tmpfile(tmp);
605  if (name == "")
606  return *this;
607 
608  //
609  // write the data to file
610  //
611  for (unsigned int i = 0; i < x.size(); i++)
612  tmp << x[i] << " " << y[i] << " " << dy[i] << std::endl;
613 
614  tmp.flush();
615  tmp.close();
616 
617 
618  // Do the actual plot
619  plotfile_xy_err(name, 1, 2, 3, title);
620 
621  return *this;
622  }
623 
626  Gnuplot& plotfile_xyz (const std::string &filename,
627  const unsigned int column_x = 1,
628  const unsigned int column_y = 2,
629  const unsigned int column_z = 3,
630  const std::string &title = "");
631 
632  Gnuplot& plotfile_xyzo (const std::string &filename,
633  const unsigned int column_x = 1,
634  const unsigned int column_y = 2,
635  const unsigned int column_z = 3,
636  const unsigned int column_o = 4,
637  const std::string &title = "");
638 
639  Gnuplot& plotfile_xyz_with_colored_wires (const std::string &filename,
640  const std::string &title = "",
641  int color_ = 0);
642 
644  /*
645  template<typename X, typename Y, typename Z>
646  Gnuplot& plot_xyz (const X &x,
647  const Y &y,
648  const Z &z,
649  const std::string &title = "");
650  */
651 
652  //------------------------------------------------------------------------------
653  //
654  // Plots a 3d graph from a list of doubles: x y z
655  //
656  template<typename X, typename Y, typename Z>
657  Gnuplot& plot_xyz(const X &x,
658  const Y &y,
659  const Z &z,
660  const std::string &title = "")
661  {
662  DT_THROW_IF (x.size() == 0 || y.size() == 0 || z.size() == 0,
663  GnuplotException, "std::vectors too small");
664  DT_THROW_IF (x.size() != y.size() || x.size() != z.size(),
665  GnuplotException, "Length of the std::vectors differs");
666  std::ofstream tmp;
667  std::string name = create_tmpfile(tmp);
668  if (name == "")
669  return *this;
670 
671  //
672  // write the data to file
673  //
674  for (unsigned int i = 0; i < x.size(); i++)
675  tmp << x[i] << " " << y[i] << " " << z[i] <<std::endl;
676 
677  tmp.flush();
678  tmp.close();
679 
680 
681  plotfile_xyz(name, 1, 2, 3, title);
682 
683  return *this;
684  }
685 
686 
687 
689  Gnuplot& plot_slope (const double a,
690  const double b,
691  const std::string &title = "");
692 
693 
704  Gnuplot& plot_equation (const std::string &equation,
705  const std::string &title = "");
706 
709  Gnuplot& plot_equation3d (const std::string &equation,
710  const std::string &title = "");
711 
712 
714  Gnuplot& plot_image (const unsigned char *ucPicBuf,
715  const unsigned int iWidth,
716  const unsigned int iHeight,
717  const std::string &title = "");
718 
719 
720  //----------------------------------------------------------------------------------
728  //----------------------------------------------------------------------------------
729  inline Gnuplot& replot (void){if (_nplots_ > 0) cmd ("replot");return *this;};
730 
732  Gnuplot& reset_plot ();
733 
735  Gnuplot& reset_all ();
736 
738  void remove_tmpfiles ();
739 
740  // -------------------------------------------------------------------
747  // -------------------------------------------------------------------
748  inline bool is_valid (){return (_valid_);};
749 
750 };
751 
752 #endif // GNUPLOT_PIPES_H
753 
754 /*
755 ** Local Variables: --
756 ** mode: c++ --
757 ** c-file-style: "gnu" --
758 ** tab-width: 2 --
759 ** End: --
760 */
Gnuplot(const std::string &style="points")
set a style during construction
Gnuplot & savetops(const std::string &filename="gnuplot_output")
saves a gnuplot session to a postscript file, filename without extension
Gnuplot & set_zlabel(const std::string &label="z")
set z axis label
Gnuplot & set_isosamples(const int isolines=10)
set isoline density (grid) for plotting functions as surfaces (for 3d plots)
Gnuplot & set_smooth(const std::string &stylestr="csplines")
Gnuplot & set_grid()
turns grid on/off
Definition: gnuplot_i.h:279
Gnuplot & showonscreen()
GnuplotException(const std::string &msg)
Gnuplot & plotfile_xy_err(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_dy=3, const std::string &title="")
Gnuplot & set_ylabel(const std::string &label="x")
set x axis label
Gnuplot & set_xrange(const double iFrom, const double iTo)
set axis - ranges
Gnuplot & plot_x(const X &x, const std::string &title="")
from std::vector
Definition: gnuplot_i.h:497
Gnuplot & plotfile_xyzo(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_z=3, const unsigned int column_o=4, const std::string &title="")
static bool set_gnuplot_path(const std::string &path)
optional function: set Gnuplot path manual attention: for windows: path with slash '/' not backslash ...
Gnuplot & set_yautoscale()
Definition: gnuplot_i.h:431
Gnuplot & set_multiplot()
Definition: gnuplot_i.h:290
Gnuplot & set_surface()
Definition: gnuplot_i.h:345
Gnuplot & set_hidden3d()
Definition: gnuplot_i.h:315
Gnuplot & unset_contour()
Definition: gnuplot_i.h:336
void remove_tmpfiles()
deletes temporary files
Gnuplot & reset_plot()
resets a gnuplot session (next plot will erase previous ones)
Gnuplot & set_xlabel(const std::string &label="y")
set y axis label
const std::string & get_terminal() const
Return the active terminal type.
Gnuplot & set_ylogscale(const double base=10)
turns on/off log scaling for the specified yaxis (logscale is not set by default)
Gnuplot & set_xautoscale()
Definition: gnuplot_i.h:422
Gnuplot & plotfile_xyz(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_z=3, const std::string &title="")
Definition: gnuplot_i.h:68
Gnuplot & unset_multiplot()
Definition: gnuplot_i.h:299
Gnuplot & set_zautoscale()
Definition: gnuplot_i.h:440
Utility macros for exception handling.
Gnuplot & set_legend(const std::string &position="default")
Gnuplot & set_zlogscale(const double base=10)
turns on/off log scaling for the specified zaxis (logscale is not set by default)
Gnuplot & unset_surface()
Definition: gnuplot_i.h:355
Gnuplot & unset_zlogscale()
Definition: gnuplot_i.h:475
Gnuplot & set_title(const std::string &title="")
sets and clears the title of a gnuplot session
Definition: gnuplot_i.h:379
Gnuplot & set_yrange(const double iFrom, const double iTo)
set y-axis - ranges
Gnuplot & plotfile_xyz_with_colored_wires(const std::string &filename, const std::string &title="", int color_=0)
Gnuplot & plot_equation3d(const std::string &equation, const std::string &title="")
void set_terminal(const std::string &terminal_)
Set the active terminal.
bool has_terminal() const
Check if terminal is set.
Gnuplot & unset_title()
Clears the title of a gnuplot session The title is not set by default.
Definition: gnuplot_i.h:397
Gnuplot & replot(void)
replot repeats the last plot or splot command. this can be useful for viewing a plot with different s...
Definition: gnuplot_i.h:729
Gnuplot & set_cbrange(const double iFrom, const double iTo)
set palette range (autoscale by default)
Gnuplot & unset_hidden3d()
Definition: gnuplot_i.h:324
Gnuplot & plot_xy_err(const X &x, const Y &y, const E &dy, const std::string &title="")
from data
Definition: gnuplot_i.h:594
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
Gnuplot & set_samples(const int samples=100)
set sampling rate of functions, or for interpolating data
static const std::string & terminal_std()
Return the default terminal type on the current system.
Gnuplot & operator<<(const std::string &cmdstr)
Sends a command to an active gnuplot session, identical to cmd () send a command to gnuplot using the...
Definition: gnuplot_i.h:233
Gnuplot & plot_xy(const X &x, const Y &y, const std::string &title="")
from data
Definition: gnuplot_i.h:549
Gnuplot & unset_legend()
Switches legend off attention:legend is set by default.
Definition: gnuplot_i.h:370
Gnuplot & set_style(const std::string &stylestr="points")
Gnuplot & plotfile_xy(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const std::string &title="")
Gnuplot & plot_xyz(const X &x, const Y &y, const Z &z, const std::string &title="")
from std::vector
Definition: gnuplot_i.h:657
static void set_terminal_std(const std::string &type)
Gnuplot & plotfile_x(const std::string &filename, const unsigned int column=1, const std::string &title="")
Gnuplot & unset_smooth()
unset smooth attention: smooth is not set by default
Definition: gnuplot_i.h:272
Gnuplot & set_pointsize(const double pointsize=1.0)
scales the size of the points used in plots
Gnuplot & set_xlogscale(const double base=10)
turns on/off log scaling for the specified xaxis (logscale is not set by default)
Gnuplot & cmd(const std::string &cmdstr)
send a command to gnuplot
Gnuplot & set_zrange(const double iFrom, const double iTo)
set z-axis - ranges
Gnuplot & unset_ylogscale()
Definition: gnuplot_i.h:466
Gnuplot & plot_slope(const double a, const double b, const std::string &title="")
plot an equation of the form: y = ax + b, you supply a and b
Definition: gnuplot_i.h:61
bool is_valid()
Is the gnuplot session valid ??
Definition: gnuplot_i.h:748
Gnuplot & plot_image(const unsigned char *ucPicBuf, const unsigned int iWidth, const unsigned int iHeight, const std::string &title="")
plot image
~Gnuplot()
destructor: needed to delete temporary files
Gnuplot & set_contour(const std::string &position="base")
Gnuplot & unset_grid()
grid is not set by default
Definition: gnuplot_i.h:281
Gnuplot & unset_xlogscale()
Definition: gnuplot_i.h:457
Gnuplot & reset_all()
resets a gnuplot session and sets all variables to default
Gnuplot & plot_equation(const std::string &equation, const std::string &title="")