CFEL - ASG Software Suite  2.5.0
CASS
rankfilter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 Stephan Kassemeyer
2 
3 /** @file rankfilter.cpp contains definition of processors that will
4  * operate on histograms of other processors,
5  * calculating statistical rank filters like median filter.
6  *
7  * @author Stephan Kassemeyer
8  */
9 
10 #include <iterator>
11 #include <algorithm>
12 #include <numeric>
13 
14 #include <QtCore/QString>
15 
16 #include "rankfilter.h"
17 
18 #include "cass_settings.h"
19 #include "log.h"
20 
21 using namespace std;
22 using namespace std::tr1;
23 using namespace cass;
24 
25 
26 // ********** processor 301: median of last values ************
27 
28 pp301::pp301(const name_t &name)
29  : AccumulatingProcessor(name)
30 {
31  loadSettings(0);
32 }
33 
34 void pp301::loadSettings(size_t)
35 {
36  CASSSettings s;
37  s.beginGroup("Processor");
39 
40  // size of dataset for median
41  _medianSize = s.value("MedianSize", 100).toInt();
42 
43  setupGeneral();
44 
45  // Get the input
46  _one = setupDependency("InputName");
47  bool ret (setupCondition());
48  if (!(_one && ret))
49  return;
50 
52 
53  Log::add(Log::INFO,"Processor '" + name() +
54  "' computes median with a size of '" + toString(_medianSize) +
55  "' of pp '"+ _one->name() +
56  "'. Condition is '" + _condition->name() + "'");
57 }
58 
59 void pp301::process(const CASSEvent& evt, result_t &result)
60 {
61  const result_t &one(_one->result(evt.id()));
62  QReadLocker lock(&one.lock);
63  QMutexLocker mLock(&_mutex);
64 
65  const float value(accumulate(one.begin(), one.end(), 0.f));
66 
67  if (_medianStorage.empty())
68  _medianStorage.resize(_medianSize,value);
69  else
70  _medianStorage.push_back(value);
71 
72  vector<float> lastData(_medianStorage.begin(),_medianStorage.end());
73  nth_element(lastData.begin(), lastData.begin() + _medianSize/2,
74  lastData.end());
75  result.setValue(lastData[_medianSize/2]);
76  _medianStorage.pop_front();
77 }
78 
79 
80 
81 
82 
83 
84 
85 
86 // ****** processor 302: Binary data from file into 2DHistogram *******
87 
88 pp302::pp302(const name_t &name)
89  : Processor(name)
90 {
91  loadSettings(0);
92 }
93 
94 void pp302::loadSettings(size_t)
95 {
96  CASSSettings s;
97 
98  s.beginGroup("Processor");
100 
101  setupGeneral();
102 
103  string filename(s.value("BinaryFile", "").toString().toStdString());
104  size_t sizeX(s.value("SizeX",0).toInt());
105  size_t sizeY(s.value("SizeY",0).toInt());
106 
107  _result = result_t::shared_pointer(new result_t(sizeX,sizeY));
108 
109  // load binary file into _result:
110  std::ifstream in(filename.c_str(), std::ios::binary|std::ios::ate);
111  if (in.is_open())
112  {
113  const size_t size_raw = in.tellg();
114  const size_t size = size_raw / sizeof(float);
115  Log::add(Log::DEBUG4,"pp302:loadSettings(): binary size: " + toString(size)+
116  " " + toString(sizeX*sizeY) );
117  if (size == sizeX*sizeY)
118  {
119  in.seekg(0,std::ios::beg); // go to beginning
120  in.read(reinterpret_cast<char*>(&(_result->front())), size_raw );
121  }
122  else
123  throw invalid_argument("pp302:loadSettings(): binary file Histogram2D: wrong size '"+
124  filename +"'");
125  }
126  else
127  throw invalid_argument("pp302:loadSettings(): binary file Histogram2D: cannot open file '" +
128  filename +"'");
129 
130  Log::add(Log::INFO,"Processor '" + name() +
131  "' loads 2DHistogram from binary File '" + filename +
132  "' which is is of size '" + toString(sizeX) +
133  "x" + toString(sizeY));
134 }
file contains processors that will operate on histograms of other processors, calculating statistical...
CachedList::item_type result_t
define the results
Definition: processor.h:52
virtual void loadSettings(size_t)
load the settings of this pp
Definition: rankfilter.cpp:94
Event to store all LCLS Data.
Definition: cass_event.h:32
const_iterator end() const
retrieve iterator to the end of storage
Definition: result.hpp:632
unsigned int _medianSize
last N items to be used for median calculation
Definition: rankfilter.h:59
const name_t name() const
retrieve the name of this processor
Definition: processor.h:167
Settings for CASS.
Definition: cass_settings.h:30
std::tr1::shared_ptr< self_type > shared_pointer
a shared pointer of this class
Definition: result.hpp:323
QMutex _mutex
mutex to lock the median storage
Definition: rankfilter.h:65
shared_pointer _one
pp containing first histogram
Definition: rankfilter.h:56
STL namespace.
things written only at end of run H5Dump ProcessorSummary size
std::deque< float > _medianStorage
storage of last values for median calculation
Definition: rankfilter.h:62
pp302(const name_t &)
constructor
Definition: rankfilter.cpp:88
result_t::shared_pointer _result
the constant result
Definition: rankfilter.h:134
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
void setValue(const_reference value)
assign the result container to a value
Definition: result.hpp:543
const_iterator begin() const
retrieve a iterator for read access to beginning
Definition: result.hpp:608
fromStdString(const std::string &str)
base class for processors.
Definition: processor.h:39
shared_pointer setupDependency(const std::string &depVarName, const name_t &name="")
setup the dependecy.
Definition: processor.cpp:114
QReadWriteLock lock
lock for locking operations on the data of the container
Definition: result.hpp:954
id_t & id()
setters
Definition: cass_event.h:64
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
value(const QString &key, const QVariant &defaultValue=QVariant()
virtual void createHistList(result_t::shared_pointer result)
create the list of results
Definition: processor.h:348
void setupGeneral()
general setup of the processor
Definition: processor.cpp:85
file contains specialized class that do the settings for cass
an accumulating processor
Definition: processor.h:289
shared_pointer _condition
pointer to the processor that will contain the condition
Definition: processor.h:277
virtual void loadSettings(size_t)
load the settings of this pp
Definition: rankfilter.cpp:34
bool setupCondition(bool defaultConditionType=true)
setup the condition.
Definition: processor.cpp:94
std::string name_t
define the name type
Definition: processor.h:46
contains a logger for cass
check if there is some light in the chamber based upon the GMD value
virtual void process(const CASSEvent &, result_t &)
process event
Definition: rankfilter.cpp:59
beginGroup(const QString &prefix)