Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PLOTTER_H_
00039 #define PLOTTER_H_
00040 #include <stdio.h>
00041 #include <vector>
00042 #include <iostream>
00043 #include <fstream>
00044 #include <algorithm>
00045 #include "Stats.h"
00046 #include <ext/hash_map>
00047 #include <iostream>
00048 #include <vector>
00049 #include <string>
00050 #include <map>
00051 #include <pthread.h>
00052 #include <semaphore.h>
00053 #include <assert.h>
00054
00055 using namespace std;
00056 template <class Q, class T>
00057 class Plotter {
00058 std::vector<Q * > v;
00059 std::vector<T> v1,v2;
00060 std::vector<std::string> names;
00061 std::vector<int> quotient_to_ms;
00062 public:
00063 void write_to_file(int id) {
00064 std::ofstream fdata;
00065 std::string sname = names.at(id);
00066 sname += ".data";
00067 for (int i = 0; i < sname.length(); i++) {
00068 if (sname[i] == ' ' || sname[i] == ':' || sname[i] == ' '
00069 || sname[i] == '#') {
00070 sname[i] = '_';
00071 }
00072 }
00073 std::cout << "Writing " << sname.c_str() << std::endl;
00074 fdata.open(sname.c_str());
00075 Q * elem = v.at(id);
00076
00077 for (int j = 0; j < elem->getCount(); j++) {
00078 fdata << elem->get(j) << "\n";
00079 }
00080 fdata.close();
00081 }
00082
00083 void writeAll(std::string name) {
00084
00085
00086 return;
00087 int res = system("mkdir data");
00088 if (res!=0){
00089 fprintf(stderr,"Error creating 'data' directory\n");
00090 }
00091 int pos = name.find_last_of('/');
00092 if (pos > 0){
00093 name = name.substr(pos);
00094 pos = name.find_last_of('.');
00095 name = name.substr(0,pos);
00096 }
00097 name += "_";
00098 for (int i = 0; i< v.size(); i++){
00099 std::ofstream fdata;
00100 std::string sname = "data/" + name;
00101 sname += names.at(i);
00102 sname += ".data";
00103 for (int j=0; j< sname.length();j++){
00104 if (sname[j] == ' ' || sname[j] == ':' || sname[j] == ' ' || sname[j] == '#' ){
00105 sname[j] = '_';
00106 }
00107 }
00108
00109 fdata.open(sname.c_str());
00110
00111 Q * elem = v.at(i);
00112
00113 for (int j = 0; j < elem->getCount(); j++) {
00114 fdata << elem->get(j) << "\n";
00115 }
00116 fdata.close();
00117 }
00118
00119 }
00120
00121 Plotter(){
00122
00123 }
00124
00125 int subscribe(Q * elem, std::string s= "***", int q_t_ms = 1){
00126 v.push_back(elem);
00127 names.push_back(s);
00128 quotient_to_ms.push_back(q_t_ms);
00129 return (v.size()-1);
00130 }
00131
00132 void plot(int idx){
00133 if (idx < v.size()){
00134 Q * elem = v.at(idx);
00135 std::ofstream fdata;
00136 fdata.open (".dataplot");
00137 for (int i = 0 ; i< elem->getCount();i++){
00138 fdata << elem->getPos(i) << " " << (elem->get(i)/quotient_to_ms.at(idx)) << std::endl;
00139 }
00140 fdata.close();
00141 FILE * f = popen("gnuplot","w");
00142 string s = "set xlabel 'Sample'\n";
00143 fprintf(f,"%s",s.c_str());
00144 s = "set ylabel '"+ elem->getName()+" (ms)'\n";
00145 fprintf(f,"%s",s.c_str());
00146
00147 s = "plot '.dataplot' title '" + elem->getName() + "'\n";
00148 fprintf(f,"%s",s.c_str());
00149 fprintf(stderr,"*** %s",s.c_str());
00150 fflush(f);
00151 }
00152 }
00153
00154 void plotHist(int idx, int nbins, double zoom){
00155 if (idx < v.size()) {
00156 Q * elem = v.at(idx);
00157 T mean=0;
00158 for (int i = 0; i < elem->getCount(); i++) {
00159 v1.push_back(elem->get(i));
00160 mean+=elem->get(i);
00161 }
00162 mean = mean / v1.size();
00163 std::sort(v1.begin(), v1.end());
00164
00165 v1.resize(v1.size()*zoom);
00166
00167 double min = v1.at(0);
00168 double max = v1.at(v1.size() - 1);
00169 if (nbins == 0){
00170 nbins = (max/mean) /1.5;
00171 }
00172 v2.resize(nbins + 1);
00173
00174 double bin = (max - min) / (double) nbins;
00175 for (int i = 0; i < v1.size(); i++) {
00176 int sbin = (int) (( (double) v1.at(i) - min) / bin);
00177 v2.at(sbin)++;
00178 }
00179 std::ofstream fdata;
00180 fdata.open (".dataplot");
00181 for (int i = 0; i < v2.size(); i++) {
00182 fdata << double((min+i*bin)/1000.0) << " " << v2.at(i) << std::endl;
00183 }
00184 fdata.close();
00185 FILE * f = popen("gnuplot","w");
00186 string s = "set xlabel 'Time (ms)'\n";
00187 fprintf(f,"%s",s.c_str());
00188 s = "set ylabel 'Occurrences'\n";
00189 fprintf(f,"%s",s.c_str());
00190 s = "plot '.dataplot' w boxes title '" + elem->getName() + "'\n";
00191 fprintf(f,"%s",s.c_str());
00192 fflush(f);
00193 }
00194 }
00195 void clear(){
00196 v.clear();
00197 }
00198 bool hasThis(int i){
00199 bool ret = (i < v.size() && i >= 0);
00200 return ret;
00201 }
00202 virtual ~Plotter(){
00203
00204 }
00205 };
00206
00207 #endif