OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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"
25 
26 #include "Utility/IpplInfo.h"
27 #include <cmath>
28 
29 #define NPOINTS_DEFAULT 129
30 #define NLEFT_DEFAULT 64
31 #define NRIGHT_DEFAULT 64
32 #define POLYORDER_DEFAULT 1
33 
34 extern Inform *gmsg;
35 
36 using namespace Physics;
37 
38 
39 // Class OpalFilter
40 // ------------------------------------------------------------------------
41 
42 // The attributes of class OpalFilter.
43 namespace {
44  enum {
45  // DESCRIPTION OF SINGLE PARTICLE:
46  TYPE, // The type of filter
47  NFREQ, // Number of frequencies in fixedFFTLowPass filter
48  THRESHOLD, // Relative threshold for amplitude of frequency in relativeFFTLowPass
49  NPOINTS, // Number of points in Savitzky-Golay filter
50  NLEFT, // Number of points to the left in S-G filter
51  NRIGHT, // Number of points to the right in S-G filter
52  POLYORDER, // Polynomial order in S-G Filter
53  SIZE
54  };
55 }
56 
58  Definition(SIZE, "FILTER",
59  "The \"FILTER\" statement defines a 1 dimensional filter to be "
60  "applied on histogram."),
61  filter_m(0) {
63  ("TYPE", "Specifies the type of filter.",
64  {"SAVITZKY-GOLAY", "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 
148  std::string type = Attributes::getString(itsAttr[TYPE]);
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 }
@ SIZE
Definition: IndexMap.cpp:174
#define NPOINTS_DEFAULT
Definition: OpalFilter.cpp:29
#define NRIGHT_DEFAULT
Definition: OpalFilter.cpp:31
#define NLEFT_DEFAULT
Definition: OpalFilter.cpp:30
#define POLYORDER_DEFAULT
Definition: OpalFilter.cpp:32
Inform * gmsg
Definition: Main.cpp:62
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define INFOMSG(msg)
Definition: IpplInfo.h:348
const std::string name
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Definition: Attributes.cpp:409
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
Definition: Air.h:27
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
The base class for all OPAL definitions.
Definition: Definition.h:30
The base class for all OPAL objects.
Definition: Object.h:48
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:281
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:302
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
bool builtin
Built-in flag.
Definition: Object.h:233
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:565
static OpalData * getInstance()
Definition: OpalData.cpp:195
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:488
The base class for all OPAL exceptions.
Definition: OpalException.h:28
The FILTER definition.
Definition: OpalFilter.h:32
void initOpalFilter()
Definition: OpalFilter.cpp:142
virtual void execute()
Check the OpalFilter data.
Definition: OpalFilter.cpp:121
virtual ~OpalFilter()
Definition: OpalFilter.cpp:104
static OpalFilter * find(const std::string &name)
Find named FILTER.
Definition: OpalFilter.cpp:126
Filter * filter_m
Definition: OpalFilter.h:64
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: OpalFilter.cpp:110
virtual OpalFilter * clone(const std::string &name)
Make clone.
Definition: OpalFilter.cpp:116
virtual void update()
Update the OpalFilter data.
Definition: OpalFilter.cpp:136
OpalFilter()
Exemplar constructor.
Definition: OpalFilter.cpp:57
void print(std::ostream &os) const
Print the object.
Definition: OpalFilter.cpp:187
Definition: Inform.h:42