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  .SetGroupName ("Stats")
41  ;
42 
43  return tid;
44 }
45 
46 GnuplotAggregator::GnuplotAggregator (const std::string &outputFileNameWithoutExtension)
47  : m_outputFileNameWithoutExtension (outputFileNameWithoutExtension),
48  m_graphicsFileName (m_outputFileNameWithoutExtension + ".png"),
49  m_title ("Data Values"),
50  m_xLegend ("X Values"),
51  m_yLegend ("Y Values"),
52  m_titleSet (false),
53  m_xAndYLegendsSet (false),
54  m_gnuplot (m_graphicsFileName)
55 {
56  NS_LOG_FUNCTION (this);
57 }
58 
60 {
61  NS_LOG_FUNCTION (this);
62  if (!m_titleSet)
63  {
64  NS_LOG_WARN ("Warning: The plot title was not set for the gnuplot aggregator");
65  }
66  if (!m_xAndYLegendsSet)
67  {
68  NS_LOG_WARN ("Warning: The axis legends were not set for the gnuplot aggregator");
69  }
70 
71  std::string dataFileName = m_outputFileNameWithoutExtension + ".dat";
72  std::string plotFileName = m_outputFileNameWithoutExtension + ".plt";
73  std::string scriptFileName = m_outputFileNameWithoutExtension + ".sh";
74 
75  // Open the gnuplot plot and data files.
76  std::ofstream plotFile;
77  plotFile.open (plotFileName.c_str ());
78  std::ofstream dataFile;
79  dataFile.open (dataFileName.c_str ());
80 
81  // Skip any NaN's that appear in data.
82  m_gnuplot.AppendExtra ("set datafile missing \"-nan\"");
83 
84  // Write the gnuplot plot and data files.
85  m_gnuplot.GenerateOutput (plotFile, dataFile, dataFileName);
86 
87  // Close the gnuplot plot and data files.
88  plotFile.close ();
89  dataFile.close ();
90 
91  // Open the shell script file.
92  std::ofstream scriptFile;
93  scriptFile.open (scriptFileName.c_str ());
94 
95  // Write the shell script file.
96  scriptFile << "#!/bin/sh" << std::endl;
97  scriptFile << std::endl;
98  scriptFile << "gnuplot " << plotFileName << std::endl;
99 
100  // Close the shell script file.
101  scriptFile.close ();
102 }
103 
104 void
105 GnuplotAggregator::Write2d (std::string context, double x, double y)
106 {
107  NS_LOG_FUNCTION (this << context << x << y);
108 
109  if (m_2dDatasetMap.count (context) == 0)
110  {
111  NS_ABORT_MSG ("Dataset " << context << " has not been added");
112  }
113 
114  if (m_enabled)
115  {
116  // Add this 2D data point to its dataset.
117  m_2dDatasetMap[context].Add (x, y);
118  }
119 }
120 
121 void
123  double x,
124  double y,
125  double errorDelta)
126 {
127  NS_LOG_FUNCTION (this << context << x << y << errorDelta);
128 
129  if (m_2dDatasetMap.count (context) == 0)
130  {
131  NS_ABORT_MSG ("Dataset " << context << " has not been added");
132  }
133 
134  if (m_enabled)
135  {
136  // Add this 2D data point with its error bar to its dataset.
137  m_2dDatasetMap[context].Add (x, y, errorDelta);
138  }
139 }
140 
141 void
143  double x,
144  double y,
145  double errorDelta)
146 {
147  NS_LOG_FUNCTION (this << context << x << y << errorDelta);
148 
149  if (m_2dDatasetMap.count (context) == 0)
150  {
151  NS_ABORT_MSG ("Dataset " << context << " has not been added");
152  }
153 
154  if (m_enabled)
155  {
156  // Add this 2D data point with its error bar to its dataset.
157  m_2dDatasetMap[context].Add (x, y, errorDelta);
158  }
159 }
160 
161 void
163  double x,
164  double y,
165  double xErrorDelta,
166  double yErrorDelta)
167 {
168  NS_LOG_FUNCTION (this << context << x << y << xErrorDelta << yErrorDelta);
169 
170  if (m_2dDatasetMap.count (context) == 0)
171  {
172  NS_ABORT_MSG ("Dataset " << context << " has not been added");
173  }
174 
175  if (m_enabled)
176  {
177  // Add this 2D data point with its error bar to its dataset.
178  m_2dDatasetMap[context].Add (x, y, xErrorDelta, yErrorDelta);
179  }
180 }
181 
182 void
183 GnuplotAggregator::SetTerminal (const std::string &terminal)
184 {
185  // Change the extension for the graphics file.
187 
188  // Update the gnuplot, too.
189  m_gnuplot.SetTerminal (terminal);
191 }
192 
193 void
194 GnuplotAggregator::SetTitle (const std::string &title)
195 {
196  NS_LOG_FUNCTION (this << title);
197  m_gnuplot.SetTitle (title);
198  m_titleSet = true;
199 }
200 
201 void
202 GnuplotAggregator::SetLegend (const std::string &xLegend, const std::string &yLegend)
203 {
204  NS_LOG_FUNCTION (this << xLegend << yLegend);
205  m_gnuplot.SetLegend (xLegend, yLegend);
206  m_xAndYLegendsSet = true;
207 }
208 
209 void
210 GnuplotAggregator::SetExtra (const std::string &extra)
211 {
212  NS_LOG_FUNCTION (this << extra);
213  m_gnuplot.SetExtra (extra);
214 }
215 
216 void
217 GnuplotAggregator::AppendExtra (const std::string &extra)
218 {
219  NS_LOG_FUNCTION (this << extra);
220  m_gnuplot.AppendExtra (extra);
221 }
222 
223 void
224 GnuplotAggregator::Add2dDataset (const std::string &dataset, const std::string &title)
225 {
226  NS_LOG_FUNCTION (this << dataset << title);
227 
228  if (m_2dDatasetMap.count (dataset) > 0)
229  {
230  NS_ABORT_MSG ("Dataset " << dataset << " has already been added");
231  }
232 
233  // Add this dataset to the map so that its values can be saved.
234  Gnuplot2dDataset gnuplot2dDataset (title);
235  m_2dDatasetMap[dataset] = gnuplot2dDataset;
236 
237  // Add this dataset to the plot so that its values can be plotted.
239 }
240 
241 void
243 {
244  NS_LOG_FUNCTION (extra);
246 }
247 
248 void
249 GnuplotAggregator::Set2dDatasetExtra (const std::string &dataset, const std::string &extra)
250 {
251  NS_LOG_FUNCTION (this << dataset << extra);
252  if (m_2dDatasetMap.count (dataset) == 0)
253  {
254  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
255  }
256 
257  // Set the extra parameters for the dataset.
258  m_2dDatasetMap[dataset].SetExtra (extra);
259 }
260 
261 void
262 GnuplotAggregator::Write2dDatasetEmptyLine (const std::string &dataset)
263 {
264  NS_LOG_FUNCTION (this << dataset);
265  if (m_2dDatasetMap.count (dataset) == 0)
266  {
267  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
268  }
269 
270  if (m_enabled)
271  {
272  // Add an empty line to the dataset.
273  m_2dDatasetMap[dataset].AddEmptyLine ();
274  }
275 }
276 
277 void
279 {
280  NS_LOG_FUNCTION (style);
282 }
283 
284 void
285 GnuplotAggregator::Set2dDatasetStyle (const std::string &dataset, enum Gnuplot2dDataset::Style style)
286 {
287  NS_LOG_FUNCTION (this << dataset << style);
288  if (m_2dDatasetMap.count (dataset) == 0)
289  {
290  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
291  }
292 
293  // Set the style for the dataset.
294  m_2dDatasetMap[dataset].SetStyle (style);
295 }
296 
297 void
299 {
300  NS_LOG_FUNCTION (errorBars);
302 }
303 
304 void
305 GnuplotAggregator::Set2dDatasetErrorBars (const std::string &dataset, enum Gnuplot2dDataset::ErrorBars errorBars)
306 {
307  NS_LOG_FUNCTION (this << dataset << errorBars);
308  if (m_2dDatasetMap.count (dataset) == 0)
309  {
310  NS_ABORT_MSG ("Dataset " << dataset << " has not been added");
311  }
312 
313  // Set the error bars for the dataset.
314  m_2dDatasetMap[dataset].SetErrorBars (errorBars);
315 }
316 
317 void
319 {
320  NS_LOG_FUNCTION (this << keyLocation);
321  // Set the specified key location.
322  switch (keyLocation)
323  {
324  case NO_KEY:
325  m_gnuplot.AppendExtra ("set key off");
326  break;
327  case KEY_ABOVE:
328  m_gnuplot.AppendExtra ("set key outside center above");
329  break;
330  case KEY_BELOW:
331  m_gnuplot.AppendExtra ("set key outside center below");
332  break;
333  default:
334  m_gnuplot.AppendExtra ("set key inside");
335  break;
336  }
337 }
338 
339 } // namespace ns3
340 
Base class for data collection framework objects.
bool m_enabled
Object's activation state.
Class to represent a 2D points plot.
Definition: gnuplot.h:118
ErrorBars
Whether errorbars should be used for this dataset.
Definition: gnuplot.h:138
static void SetDefaultStyle(enum Style style)
Change default style for all newly created objects.
Definition: gnuplot.cc:341
Style
The plotting style to use for this dataset.
Definition: gnuplot.h:123
static void SetDefaultErrorBars(enum ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition: gnuplot.cc:352
void Write2dDatasetEmptyLine(const std::string &dataset)
Add an empty line in the data output sequence.
GnuplotAggregator(const std::string &outputFileNameWithoutExtension)
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.
static void Set2dDatasetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Gnuplot m_gnuplot
Used to create gnuplot files.
void Write2d(std::string context, double x, double y)
Writes a 2D value to a 2D gnuplot dataset.
std::string m_graphicsFileName
The graphics file name with its extension.
bool m_xAndYLegendsSet
Set equal to true after setting the x and y legends.
void Set2dDatasetExtra(const std::string &dataset, const std::string &extra)
Add extra formatting parameters to this dataset.
void AppendExtra(const std::string &extra)
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.
std::string m_outputFileNameWithoutExtension
The output file name without any extension.
static TypeId GetTypeId()
Get the type ID.
bool m_titleSet
Set equal to true after setting the title.
void Set2dDatasetStyle(const std::string &dataset, enum Gnuplot2dDataset::Style style)
Set the style of plotting to use for this dataset.
void Set2dDatasetErrorBars(const std::string &dataset, enum Gnuplot2dDataset::ErrorBars errorBars)
Set the error bars to use for this dataset.
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.
KeyLocation
The location of the key in the plot.
void SetKeyLocation(enum KeyLocation keyLocation)
Set the location of the key in the plot.
static void Set2dDatasetDefaultErrorBars(enum Gnuplot2dDataset::ErrorBars errorBars)
Change default errorbars style for all newly created objects.
void SetTerminal(const std::string &terminal)
void Add2dDataset(const std::string &dataset, const std::string &title)
Adds a 2D dataset to the plot.
std::map< std::string, Gnuplot2dDataset > m_2dDatasetMap
Maps context strings to 2D datasets.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
void SetTitle(const std::string &title)
static void Set2dDatasetDefaultStyle(enum Gnuplot2dDataset::Style style)
Change default style for all newly created objects.
void SetExtra(const std::string &extra)
static void SetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Definition: gnuplot.cc:147
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:760
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:740
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:728
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:753
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:766
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:747
void SetTitle(const std::string &title)
Definition: gnuplot.cc:734
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:707
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
list x
Random number samples.