CFEL - ASG Software Suite  2.5.0
CASS
rate_plotter.cpp
Go to the documentation of this file.
1 //Copyright (C) 2010,2013,2016,2017 Lutz Foucar
2 
3 /**
4  * @file rate_plotter.cpp file contains declaration of class to plot the rate
5  * calculated by ratemeters
6  *
7  * @author Lutz Foucar
8  */
9 
10 #define __STDC_FORMAT_MACROS
11 
12 #include <QtCore/QDateTime>
13 
14 #include <iostream>
15 #include <sstream>
16 #include <iomanip>
17 #include <stdio.h>
18 #include <inttypes.h>
19 
20 #include "rate_plotter.h"
21 #include "ratemeter.h"
22 #include "input_base.h"
23 #include "log.h"
24 #include "cass_settings.h"
25 #include "result.hpp"
26 #include "processor_manager.h"
27 
28 using namespace std;
29 using namespace cass;
30 
31 RatePlotter::RatePlotter(Ratemeter &inputrate,
32  Ratemeter &inputload,
33  Ratemeter &analyzerate,
34  QObject *parent)
35  : QThread(parent),
36  _inputrate(inputrate),
37  _inputload(inputload),
38  _analyzerate(analyzerate)
39 {
40  CASSSettings s;
41  stringstream output;
42  output << "ProcessingInfo: ";
43  s.beginGroup("ProcessingInfo");
44  _showInfo = s.value("ShowInfo",true).toBool();
45  output << "ShowInfo '"<< std::boolalpha << _showInfo << "', ";
46  _showTime = s.value("ShowTime",false).toBool();
47  output << "ShowTime '"<< std::boolalpha << _showTime << "', ";
48  _timeformat = s.value("TimeFormatString","dd-MMM-yy_HH:mm:ss ").toString().toStdString();
49  output << "Timeformat-string '" << _timeformat <<"', ";
50  _filename = s.value("Output","").toString().toStdString();
51  output << "Output to '" <<(_filename == "" ? "COUT":_filename) <<"', ";
52  _interval = s.value("UpdateInterval",1).toInt();
53  output << "UpdateInterval '" << _interval << "', ";
54  _showInputRate = s.value("ShowInputRate",true).toBool();
55  output << "ShowInputRate '"<< std::boolalpha << _showInputRate << "', ";
56  _showInputLoad= s.value("ShowInputLoad",true).toBool();
57  output << "ShowInputLoad '"<< std::boolalpha << _showInputLoad << "', ";
58  _showAnalysisRate = s.value("ShowAnalysisRate",true).toBool();
59  output << "ShowAnalysisRate '"<< std::boolalpha << _showAnalysisRate << "', ";
60  _showProgress = s.value("ShowProgress",true).toBool();
61  output << "ShowProcessRatio '"<< std::boolalpha << _showProgress << "', ";
62  _showNProcessedEvents = s.value("ShowNbrProcessedEvents",false).toBool();
63  output << "ShowNbrProcessEvents '"<< std::boolalpha << _showNProcessedEvents << "', ";
64  _showNSkippedEvents = s.value("ShowNbrSkippedEvents",false).toBool();
65  output << "ShowNbrSkippedEvents '"<< std::boolalpha << _showNSkippedEvents << "', ";
66  _newLine = s.value("NewLine",false).toBool();
67  output << "NewLine '"<< std::boolalpha << _newLine << "', ";
68  output << "ValueProcessors: ";
69  int size = s.beginReadArray("ValueProcessors");
70  for (int i = 0; i < size; ++i)
71  {
72  s.setArrayIndex(i);
73  ProcProperties proc;
74  proc.name = s.value("Name","Unknown").toString().toStdString();
75  proc.fieldWidth = s.value("FieldWidth",10).toInt();
76  proc.precision = s.value("Precision",7).toInt();
77  proc.unit = s.value("Unit","").toString().toStdString();
78  if (proc.name != "Unknown")
79  {
80  _procs.push_back(proc);
81  output << "Name '" << proc.name << "', "
82  << "FieldWidth '" << proc.fieldWidth << "', "
83  << "Precision '" << proc.precision << "', "
84  << "Unit '" << proc.unit << "', ";
85  }
86  }
87  Log::add(Log::INFO,output.str());
88 }
89 
91 {
92  if(isRunning())
93  terminate();
94  wait();
95  std::streambuf * buf;
96  std::ofstream of;
97  if(_filename!="")
98  {
99  of.open(_filename.c_str());
100  buf = of.rdbuf();
101  }
102  else
103  {
104  buf = cout.rdbuf();
105  }
106  std::ostream out(buf);
107  out << endl << "Quit" << endl;
108 }
109 
111 {
112  if (!_showInfo)
113  return;
114 
115  while(true)
116  {
117  sleep(_interval);
118  stringstream output;
119  if (!_newLine)
120  output <<"\r";
121  if (_showTime)
122  output << QDateTime::currentDateTime().toString(QString::fromStdString(_timeformat)).toStdString();
123  if (_showInputRate)
124  {
125  output << "Input: " << std::setw(5) << std::fixed << std::setprecision(1)
126  << _inputrate.calculateRate() << "Hz";
127  }
128  if (_showInputLoad)
129  {
130  double load(_inputload.calculateRate());
131  string size("_B/s");
132  if (load > 999.9)
133  {
134  load /= 1024.;
135  size = "KB/s";
136  }
137  if (load > 999.9)
138  {
139  load /= 1024.;
140  size = "MB/s";
141  }
142  if (load > 999.9)
143  {
144  load /= 1024.;
145  size = "GB/s";
146  }
147  if (load > 999.9)
148  {
149  load /= 1024.;
150  size = "TB/s";
151  }
152  output << " | Load: "
153  << std::setw(5) << std::fixed << std::setprecision(1)
154  << load << size;
155  }
156  if (_showAnalysisRate)
157  {
158  output << " | Analyze: "
159  << std::setw(5) << std::fixed << std::setprecision(1)
160  << _analyzerate.calculateRate() << "Hz";
161  }
162  if (_showProgress)
163  {
164  output << " | Processed: "
165  << std::setw(5) << std::fixed << std::setprecision(1)
166  << InputBase::reference().progress()*100. << "%";
167  }
169  {
170  output << " | Events: "
171  << std::setw(7)
172  << InputBase::reference().eventcounter();
173  }
175  {
176  output << " | Skipped Events: "
177  << std::setw(7)
178  << InputBase::reference().skippedeventcounter();
179  }
180  for (proclist_t::const_iterator it(_procs.begin()); it !=_procs.end(); ++it)
181  {
182  try
183  {
184  QWriteLocker pplock(&ProcessorManager::instance()->lock);
186  (ProcessorManager::reference().getProcessor(it->name).resultCopy(0));
187  if (result->dim() != 0)
188  {
189  continue;
190  }
191  else
192  {
193  output << " | " << result->name() << ": "
194  << std::setw(it->fieldWidth)
195  << std::fixed << std::setprecision(it->precision)
196  << result->getValue() << it->unit;
197  }
198 
199  }
200  catch(const InvalidResultError& error)
201  {
202  Log::add(Log::ERROR,string("ProcessingInfo: ") + error.what());
203  }
204  catch(const InvalidProcessorError& error)
205  {
206  Log::add(Log::ERROR,string("ProcessingInfo: ") + error.what());
207  }
208  }
209  if (_newLine)
210  output <<"\n";
211 
212 // char tmp[256];
213 // snprintf(tmp, 255, "\rInput: %5.1fHz (%5.1f%cB/s) | Analyze: %5.1fHz | Processed: %5.1f%% | Events: %" PRIu64 "",
214 //
215 // _inputrate.calculateRate(),load,shortsize,
216 // _analyzerate.calculateRate(),
217 // InputBase::reference().processed()*100.,
218 // InputBase::reference().eventcounter());
219 
220  // taken from http://stackoverflow.com/questions/366955/obtain-a-stdostream-either-from-stdcout-or-stdofstreamfile
221  std::streambuf * buf;
222  std::ofstream of;
223  if(_filename!="")
224  {
225  of.open(_filename.c_str());
226  buf = of.rdbuf();
227  }
228  else
229  {
230  buf = cout.rdbuf();
231  }
232  std::ostream out(buf);
233 
234  out << output.str() << flush;
235  }
236 }
std::string _filename
the filename to which the status will be written
Definition: rate_plotter.h:126
setArrayIndex(int i)
bool _newLine
flag to tell whether the updated info should be put into a new line
Definition: rate_plotter.h:147
bool _showAnalysisRate
flag to tell whether the analysis rate should be reported
Definition: rate_plotter.h:135
find nbr of bragg peaks by taking background with pixels that are free of
class calculating a rate in Hz.
Definition: ratemeter.h:28
sleep(unsigned long secs)
file contains declaration of class calculating a rate
bool _showNProcessedEvents
flag to tell whether to report on how many events have been processed
Definition: rate_plotter.h:141
Settings for CASS.
Definition: cass_settings.h:30
Ratemeter & _analyzerate
reference to the workers (analysis) Ratemeter
Definition: rate_plotter.h:117
std::tr1::shared_ptr< self_type > shared_pointer
a shared pointer of this class
Definition: result.hpp:323
int fieldWidth
how much space should be used for the output of the variable
Definition: rate_plotter.h:162
STL namespace.
void run()
the plotting loop
proclist_t _procs
list of value like processors that should be reported on
Definition: rate_plotter.h:175
things written only at end of run H5Dump ProcessorSummary size
virtual ~RatePlotter()
destructor
result classes
std::string name
the name of the processor
Definition: rate_plotter.h:159
std::string _timeformat
string to tell how to format the time output
Definition: rate_plotter.h:153
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
bool _showTime
flag to tell whether to display the time when the output was written
Definition: rate_plotter.h:150
fromStdString(const std::string &str)
bool _showNSkippedEvents
flag to tell whether to report on how many events have been processed
Definition: rate_plotter.h:144
beginReadArray(const QString &prefix)
Ratemeter & _inputload
reference to the input Ratemeter
Definition: rate_plotter.h:114
static shared_pointer instance()
return the already created instance of this
file contains declaration of class to plot the rate calculated by ratemeters
int _interval
the interval in which the rate is plottet in s
Definition: rate_plotter.h:123
int precision
how much digits after the decimal point should be used
Definition: rate_plotter.h:165
contains the manager for the processors
double calculateRate()
retrieve the rate
Definition: ratemeter.cpp:26
Exception thrown when accessing invalid processor.
std::string unit
the unit that should be displayed after the value
Definition: rate_plotter.h:168
value(const QString &key, const QVariant &defaultValue=QVariant()
contains the base class for all input modules
wait(unsigned long time=ULONG_MAX)
Ratemeter & _inputrate
reference to the input Ratemeter
Definition: rate_plotter.h:111
define the processor properties
Definition: rate_plotter.h:156
file contains specialized class that do the settings for cass
static shared_pointer::element_type & reference()
return a reference to this instance
bool _showProgress
flag to tell whether the how much is processed ratio should be reported
Definition: rate_plotter.h:138
Exception thrown when accessing invalid histogram.
bool _showInputLoad
flag to tell whether the input load should be reported
Definition: rate_plotter.h:132
static shared_pointer::element_type & reference()
get reference to the singelton instance
Definition: input_base.cpp:28
bool _showInputRate
flag to tell whether the input rate should be reported
Definition: rate_plotter.h:129
contains a logger for cass
beginGroup(const QString &prefix)
bool _showInfo
flag to tell whether to show the info at all
Definition: rate_plotter.h:120