A Discrete-Event Network Simulator
API
gnuplot-aggregator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mitch Watrous (watrous@u.washington.edu)
19  */
20 
21 #include <iostream>
22 #include <fstream>
23 #include <string>
24 
25 #include "gnuplot-aggregator.h"
26 #include "ns3/abort.h"
27 #include "ns3/log.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("GnuplotAggregator");
32 
33 NS_OBJECT_ENSURE_REGISTERED (GnuplotAggregator);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::GnuplotAggregator")
40  ;
41 
42  return tid;
43 }
44 
45 GnuplotAggregator::GnuplotAggregator (const std::string &outputFileNameWithoutExtension)
46  : m_outputFileNameWithoutExtension (outputFileNameWithoutExtension),
47  m_graphicsFileName (m_outputFileNameWithoutExtension + ".png"),
48  m_title ("Data Values"),
49  m_xLegend ("X Values"),
50  m_yLegend ("Y Values"),
51  m_titleSet (false),
52  m_xAndYLegendsSet (false),
53  m_gnuplot (m_graphicsFileName)
54 {
55  NS_LOG_FUNCTION (this);
56 }
57 
59 {
60  NS_LOG_FUNCTION (this);
61  if (!m_titleSet)
62  {
63  NS_LOG_WARN ("Warning: The plot title was not set for the gnuplot aggregator");
64  }
65  if (!m_xAndYLegendsSet)
66  {
67  NS_LOG_WARN ("Warning: The axis legends were not set for the gnuplot aggregator");
68  }
69 
70  std::string dataFileName = m_outputFileNameWithoutExtension + ".dat";
71  std::string plotFileName = m_outputFileNameWithoutExtension + ".plt";
72  std::string scriptFileName = m_outputFileNameWithoutExtension + ".sh";
73 
74  // Open the gnuplot plot and data files.
75  std::ofstream plotFile;
76  plotFile.open (plotFileName.c_str ());
77  std::ofstream dataFile;
78  dataFile.open (dataFileName.c_str ());
79 
80  // Skip any NaN's that appear in data.
81  m_gnuplot.AppendExtra ("set datafile missing \"-nan\"");
82 
83  // Write the gnuplot plot and data files.
84  m_gnuplot.GenerateOutput (plotFile, dataFile, dataFileName);
85 
86  // Close the gnuplot plot and data files.
87  plotFile.close ();
88  dataFile.close ();
89 
90  // Open the shell script file.
91  std::ofstream scriptFile;
92  scriptFile.open (scriptFileName.c_str ());
93 
94  // Write the shell script file.
95  scriptFile << "#!/bin/sh" << std::endl;
96  scriptFile << std::endl;
97  scriptFile << "gnuplot " << plotFileName << std::endl;
98 
99  // Close the shell script file.
100  scriptFile.close ();
101 }
102 
103 void
104 GnuplotAggregator::Write2d (std::string context, double x, double y)
105 {
106  NS_LOG_FUNCTION (this << context << x << y);
107 
108  if (m_2dDatasetMap.count (context) == 0)
109  {
110  NS_ABORT_MSG ("Dataset " << context << " has not been added");
111  }
112 
113  if (m_enabled)
114  {
115  // Add this 2D data point to its dataset.
116  m_2dDatasetMap[context].Add (x, y);
117  }
118 }
119 
120 void
122  double x,
123  double y,
124  double errorDelta)
125 {
126  NS_LOG_FUNCTION (this << context << x << y << errorDelta);
127 
128  if (m_2dDatasetMap.count (context) == 0)
129  {
130  NS_ABORT_MSG ("Dataset " << context << " has not been added");
131  }
132 
133  if (m_enabled)
134  {
135  // Add this 2D data point with its error bar to its dataset.
136  m_2dDatasetMap[context].Add (x, y, errorDelta);
137  }
138 }
139 
140 void
142  double x,
143  double y,
144  double errorDelta)
145 {
146  NS_LOG_FUNCTION (this << context << x << y << errorDelta);
147 
148  if (m_2dDatasetMap.count (context) == 0)
149  {
150  NS_ABORT_MSG ("Dataset " << context << " has not been added");
151  }
152 
153  if (m_enabled)
154  {
155  // Add this 2D data point with its error bar to its dataset.
156  m_2dDatasetMap[context].Add (x, y, errorDelta);
157  }
158 }
159 
160 void
162  double x,
163  double y,
164  double xErrorDelta,
165  double yErrorDelta)
166 {
167  NS_LOG_FUNCTION (this << context << x << y << xErrorDelta << yErrorDelta);
168 
169  if (m_2dDatasetMap.count (context) == 0)
170  {
171  NS_ABORT_MSG ("Dataset " << context << " has not been added");
172  }
173 
174  if (m_enabled)
175  {
176  // Add this 2D data point with its error bar to its dataset.
177  m_2dDatasetMap[context].Add (x, y, xErrorDelta, yErrorDelta);
178  }
179 }
180 
181 void
182 GnuplotAggregator::SetTerminal (const std::string &terminal)
183 {
184  // Change the extension for the graphics file.
186 
187  // Update the gnuplot, too.
188  m_gnuplot.SetTerminal (terminal);
190 }
191 
192 void
193 GnuplotAggregator::SetTitle (const std::string &title)
194 {
195  NS_LOG_FUNCTION (this << title);
196  m_gnuplot.SetTitle (title);
197  m_titleSet = true;
198 }
199 
200 void
201 GnuplotAggregator::SetLegend (const std::string &xLegend, const std::string &yLegend)
202 {
203  NS_LOG_FUNCTION (this << xLegend << yLegend);
204  m_gnuplot.SetLegend (xLegend, yLegend);
205  m_xAndYLegendsSet = true;
206 }
207 
208 void
209 GnuplotAggregator::SetExtra (const std::string &extra)
210 {
211  NS_LOG_FUNCTION (this << extra);
212  m_gnuplot.SetExtra (extra);
213 }
214 
215 void
216 GnuplotAggregator::AppendExtra (const std::string &extra)
217 {
218  NS_LOG_FUNCTION (this << extra);
219  m_gnuplot.AppendExtra (extra);
220 }
221 
222 void
223 GnuplotAggregator::Add2dDataset (const std::string &dataset, const std::string &title)
224 {
225  NS_LOG_FUNCTION (this << dataset << title);
226 
227  if (m_2dDatasetMap.count (dataset) > 0)
228  {
229  NS_ABORT_MSG ("Dataset " << dataset << " has already been added");
230  }
231 
232  // Add this dataset to the map so that its values can be saved.
233  Gnuplot2dDataset gnuplot2dDataset (title);
234  m_2dDatasetMap[dataset] = gnuplot2dDataset;
235 
236  // Add this dataset to the plot so that its values can be plotted.
238 }
239 
240 void
242 {
243  NS_LOG_FUNCTION (extra);
245 }
246 
247 void
248 GnuplotAggregator::Set2dDatasetExtra (const std::string &dataset, const std::string &extra)
249 {
250  NS_LOG_FUNCTION (this << dataset << extra);
251  if (m_2dDatasetMap.count (dataset) == 0)
252  {
253  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
254  }
255 
256  // Set the extra parameters for the dataset.
257  m_2dDatasetMap[dataset].SetExtra (extra);
258 }
259 
260 void
261 GnuplotAggregator::Write2dDatasetEmptyLine (const std::string &dataset)
262 {
263  NS_LOG_FUNCTION (this << dataset);
264  if (m_2dDatasetMap.count (dataset) == 0)
265  {
266  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
267  }
268 
269  if (m_enabled)
270  {
271  // Add an empty line to the dataset.
272  m_2dDatasetMap[dataset].AddEmptyLine ();
273  }
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION (style);
281 }
282 
283 void
284 GnuplotAggregator::Set2dDatasetStyle (const std::string &dataset, enum Gnuplot2dDataset::Style style)
285 {
286  NS_LOG_FUNCTION (this << dataset << style);
287  if (m_2dDatasetMap.count (dataset) == 0)
288  {
289  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
290  }
291 
292  // Set the style for the dataset.
293  m_2dDatasetMap[dataset].SetStyle (style);
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION (errorBars);
301 }
302 
303 void
304 GnuplotAggregator::Set2dDatasetErrorBars (const std::string &dataset, enum Gnuplot2dDataset::ErrorBars errorBars)
305 {
306  NS_LOG_FUNCTION (this << dataset << errorBars);
307  if (m_2dDatasetMap.count (dataset) == 0)
308  {
309  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
310  }
311 
312  // Set the error bars for the dataset.
313  m_2dDatasetMap[dataset].SetErrorBars (errorBars);
314 }
315 
316 void
318 {
319  NS_LOG_FUNCTION (this << keyLocation);
320  // Set the specifed key location.
321  switch (keyLocation)
322  {
323  case NO_KEY:
324  m_gnuplot.AppendExtra ("set key off");
325  break;
326  case KEY_ABOVE:
327  m_gnuplot.AppendExtra ("set key outside center above");
328  break;
329  case KEY_BELOW:
330  m_gnuplot.AppendExtra ("set key outside center below");
331  break;
332  default:
333  m_gnuplot.AppendExtra ("set key inside");
334  break;
335  }
336 }
337 
338 } // namespace ns3
339 
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:749
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Base class for data collection framework objects.
void Write2dWithXErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x direction.
std::map< std::string, Gnuplot2dDataset > m_2dDatasetMap
Maps context strings to 2D datasets.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
GnuplotAggregator(const std::string &outputFileNameWithoutExtension)
Class to represent a 2D points plot.
Definition: gnuplot.h:113
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
std::string m_graphicsFileName
The graphics file name with its extension.
Gnuplot m_gnuplot
Used to create gnuplot files.
void Write2dWithXYErrorDelta(std::string context, double x, double y, double xErrorDelta, double yErrorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x and y directions.
static void SetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Definition: gnuplot.cc:147
void Add2dDataset(const std::string &dataset, const std::string &title)
Adds a 2D dataset to the plot.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
static void Set2dDatasetDefaultStyle(enum Gnuplot2dDataset::Style style)
Change default style for all newly created objects.
void Write2d(std::string context, double x, double y)
Writes a 2D value to a 2D gnuplot dataset.
void SetTitle(const std::string &title)
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
void Set2dDatasetStyle(const std::string &dataset, enum Gnuplot2dDataset::Style style)
Set the style of plotting to use for this dataset.
void Write2dDatasetEmptyLine(const std::string &dataset)
Add an empty line in the data output sequence.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
void AppendExtra(const std::string &extra)
void Set2dDatasetExtra(const std::string &dataset, const std::string &extra)
Add extra formatting parameters to this dataset.
void SetKeyLocation(enum KeyLocation keyLocation)
Set the location of the key in the plot.
void SetTerminal(const std::string &terminal)
static void Set2dDatasetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:703
bool m_xAndYLegendsSet
Set equal to true after setting the x and y legends.
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
void SetExtra(const std::string &extra)
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
bool m_enabled
Object's activation state.
static TypeId GetTypeId()
Get the type ID.
bool m_titleSet
Set equal to true after setting the title.
KeyLocation
The location of the key in the plot.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
std::string m_outputFileNameWithoutExtension
The output file name without any extension.
static void SetDefaultErrorBars(enum ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition: gnuplot.cc:348
static void SetDefaultStyle(enum Style style)
Change default style for all newly created objects.
Definition: gnuplot.cc:337
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Style
The plotting style to use for this dataset.
Definition: gnuplot.h:119
ErrorBars
Whether errorbars should be used for this dataset.
Definition: gnuplot.h:133
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
static void Set2dDatasetDefaultErrorBars(enum Gnuplot2dDataset::ErrorBars errorBars)
Change default errorbars style for all newly created objects.
void Write2dWithYErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the y direction.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
void Set2dDatasetErrorBars(const std::string &dataset, enum Gnuplot2dDataset::ErrorBars errorBars)
Set the error bars to use for this dataset.
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631