OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
OpalFilter.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: OpalFilter.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.3.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Filter
10 // The class for the Filter Object.
11 //
12 // $Date: 2008/10/14 22:09:00 $
13 // $Author: C. Kraus $
14 //
15 // ------------------------------------------------------------------------
16 
17 #include "Utilities/OpalFilter.h"
18 
19 #include "Filters/Filters.h"
21 #include "Attributes/Attributes.h"
22 #include "Physics/Physics.h"
24 #include "Utilities/Util.h"
26 
27 #include "Utility/IpplInfo.h"
28 #include <cmath>
29 
30 #define NPOINTS_DEFAULT 129
31 #define NLEFT_DEFAULT 64
32 #define NRIGHT_DEFAULT 64
33 #define POLYORDER_DEFAULT 1
34 
35 extern Inform *gmsg;
36 
37 using namespace Physics;
38 
39 
40 // Class OpalFilter
41 // ------------------------------------------------------------------------
42 
43 // The attributes of class OpalFilter.
44 namespace {
45  enum {
46  // DESCRIPTION OF SINGLE PARTICLE:
47  TYPE, // The type of filter
48  NFREQ, // Number of frequencies in fixedFFTLowPass filter
49  THRESHOLD, // Relative threshold for amplitude of frequency in relativeFFTLowPass
50  NPOINTS, // Number of points in Savitzky-Golay filter
51  NLEFT, // Number of points to the left in S-G filter
52  NRIGHT, // Number of points to the right in S-G filter
53  POLYORDER, // Polynomial order in S-G Filter
54  SIZE
55  };
56 }
57 
59  Definition(SIZE, "FILTER",
60  "The \"FILTER\" statement defines a 1 dimensional filter to be "
61  "applied on histogram."),
62  filter_m(0) {
64  ("TYPE", "Specifies the type of filter: SavitzkyGolay, fixedFFTLowPass, relativeFFTLowPass, Stencil");
65 
67  ("NFREQ", "Number of frequencies to use in fixedFFTLowPass filter", 9.);
68 
69  itsAttr[THRESHOLD] = Attributes::makeReal
70  ("THRESHOLD", "Relative threshold for amplitude of frequencies in relativeFFTLowPass filter", 1.e-6);
71 
72  itsAttr[NPOINTS] = Attributes::makeReal
73  ("NPOINTS", "Number of points in Savitzky-Golay filter", NPOINTS_DEFAULT);
74 
76  ("NLEFT", "Number of points to the left in Savitzky-Golay filter", NLEFT_DEFAULT);
77 
79  ("NRIGHT", "Number of points to the right in Savitzky-Golay filter", NRIGHT_DEFAULT);
80 
81  itsAttr[POLYORDER] = Attributes::makeReal
82  ("POLYORDER", "Polynomial order for local fit-function in Savitzky-Golay filter", POLYORDER_DEFAULT);
83 
85 
86  OpalFilter *defFilter = clone("UNNAMED_FILTER");
87  defFilter->builtin = true;
88 
89  try {
90  defFilter->update();
91  OpalData::getInstance()->define(defFilter);
92  } catch(...) {
93  delete defFilter;
94  }
95 }
96 
97 
98 OpalFilter::OpalFilter(const std::string &name, OpalFilter *parent):
99  Definition(name, parent),
100  filter_m(0)
101 {}
102 
103 
105  if (filter_m)
106  delete filter_m;
107 }
108 
109 
111  // Can replace only by another WAKE.
112  return dynamic_cast<OpalFilter *>(object) != 0;
113 }
114 
115 
116 OpalFilter *OpalFilter::clone(const std::string &name) {
117  return new OpalFilter(name, this);
118 }
119 
120 
122  update();
123 }
124 
125 
126 OpalFilter *OpalFilter::find(const std::string &name) {
127  OpalFilter *filter = dynamic_cast<OpalFilter *>(OpalData::getInstance()->find(name));
128 
129  if (filter == 0) {
130  throw OpalException("OpalFilter::find()", "OpalFilter \"" + name + "\" not found.");
131  }
132  return filter;
133 }
134 
135 
137  // Set default name.
138  if (getOpalName().empty()) setOpalName("UNNAMED_FILTER");
139 }
140 
141 
143  if (filter_m == 0) {
144  *gmsg << "* ************* F I L T E R ************************************************************" << endl;
145  *gmsg << "OpalFilter::initOpalFilterfunction " << endl;
146  *gmsg << "* **********************************************************************************" << endl;
147 
149  if (type == "SAVITZKY-GOLAY") {
150  int num_points = (int)(Attributes::getReal(itsAttr[NPOINTS]));
151  int num_points_left = (int)(Attributes::getReal(itsAttr[NLEFT]));
152  int num_points_right = (int)(Attributes::getReal(itsAttr[NRIGHT]));
153  int polynomial_order = std::abs((int)(Attributes::getReal(itsAttr[POLYORDER])));
154 
155  Inform svg("Savitzky-Golay: ");
156  if (num_points_left < 0) {
157  svg << "Number of points to the left negative; using default (" << NLEFT_DEFAULT << ");" << endl;
158  num_points_left = NLEFT_DEFAULT;
159  }
160  if (num_points_right < 0) {
161  svg << "Number of points to the right negative; using default (" << NRIGHT_DEFAULT << ");" << endl;
162  num_points_right = NRIGHT_DEFAULT;
163  }
164  if (num_points < num_points_left + num_points_right) {
165  svg << "Total number of points small than sum of the ones to the left and to the right plus 1; using default (NLEFT + NRIGHT + 1);" << endl;
166  num_points = num_points_left + num_points_right + 1;
167  }
168  if (polynomial_order > num_points_left + num_points_right) {
169  svg << "Polynomial order bigger than sum of points to the left and to the right; using default (NLEFT + NRIGHT);" << endl;
170  polynomial_order = num_points_left + num_points_right;
171  }
172 
173  filter_m = new SavitzkyGolayFilter(num_points, num_points_left, num_points_right, polynomial_order);
174  } else if (type == "FIXEDFFTLOWPASS") {
176  } else if (type == "RELATIVEFFTLOWPASS") {
178  } else if (type == "STENCIL") {
179  filter_m = new StencilFilter();
180  } else {
181  filter_m = 0;
182  INFOMSG("no filter attached" << endl);
183  }
184  }
185 }
186 
187 void OpalFilter::print(std::ostream &os) const {
188  os << "* ************* F I L T E R ********************************************************\n"
189  << "* FILTER " << getOpalName() << '\n'
190  << "* TYPE " << Attributes::getString(itsAttr[TYPE]) << '\n'
191  << "* NFREQ " << Attributes::getReal(itsAttr[NFREQ]) << '\n'
192  << "* THRESHOLD " << Attributes::getReal(itsAttr[THRESHOLD]) << '\n'
193  << "* NPOINTS " << Attributes::getReal(itsAttr[NPOINTS]) << '\n'
194  << "* NLEFT " << Attributes::getReal(itsAttr[NLEFT]) << '\n'
195  << "* NRIGHT " << Attributes::getReal(itsAttr[NRIGHT]) << '\n'
196  << "* POLYORDER " << Attributes::getReal(itsAttr[POLYORDER]) << '\n'
197  << "* ********************************************************************************** " << std::endl;
198 }
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
constexpr double e
The value of .
Definition: Physics.h:40
void print(std::ostream &os) const
Print the object.
Definition: OpalFilter.cpp:187
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:538
The base class for all OPAL definitions.
Definition: Definition.h:30
void initOpalFilter()
Definition: OpalFilter.cpp:142
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Inform * gmsg
Definition: Main.cpp:21
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
Filter * filter_m
Definition: OpalFilter.h:64
#define NRIGHT_DEFAULT
Definition: OpalFilter.cpp:32
static OpalData * getInstance()
Definition: OpalData.cpp:209
virtual void update()
Update the OpalFilter data.
Definition: OpalFilter.cpp:136
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
OpalFilter()
Exemplar constructor.
Definition: OpalFilter.cpp:58
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
#define INFOMSG(msg)
Definition: IpplInfo.h:397
virtual void execute()
Check the OpalFilter data.
Definition: OpalFilter.cpp:121
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: OpalFilter.cpp:110
#define NPOINTS_DEFAULT
Definition: OpalFilter.cpp:30
virtual ~OpalFilter()
Definition: OpalFilter.cpp:104
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
#define NLEFT_DEFAULT
Definition: OpalFilter.cpp:31
const std::string name
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:305
#define POLYORDER_DEFAULT
Definition: OpalFilter.cpp:33
static OpalFilter * find(const std::string &name)
Find named FILTER.
Definition: OpalFilter.cpp:126
The FILTER definition.
Definition: OpalFilter.h:32
bool builtin
Built-in flag.
Definition: Object.h:231
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
virtual OpalFilter * clone(const std::string &name)
Make clone.
Definition: OpalFilter.cpp:116
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307