CFEL - ASG Software Suite  2.5.0
CASS
alignment.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 Jochen Kuepper
2 // Copyright (C) 2010-2015 Lutz Foucar
3 
4 /**
5  * @file alignment.cpp processors that calculate laser alignment parameters
6  *
7  * @author Lutz Foucar
8  */
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <cmath>
13 #include <list>
14 #include <map>
15 #include <cmath>
16 #include <cstdlib>
17 
18 #include "alignment.h"
19 #include "cass_event.h"
20 #include "result.hpp"
21 #include "convenience_functions.h"
22 #include "cass_settings.h"
23 #include "log.h"
24 
25 
26 using namespace cass;
27 using namespace std;
28 
29 
30 //---processor calculating cos2theta of requested averaged image----------
31 
32 pp200::pp200(const name_t &name)
33  : Processor(name)
34 {
35  loadSettings(0);
36 }
37 
38 void pp200::loadSettings(size_t)
39 {
40  using namespace std;
41  CASSSettings settings;
42  settings.beginGroup("Processor");
44  setupGeneral();
45  _image = setupDependency("ImageName");
46  bool ret = setupCondition();
47  if (!(_image && ret))
48  return;
49  const result_t &hist(_image->result());
50  // Width of image - we assume the images are square
51  _imageWidth = hist.axis(result_t::xAxis).nBins;
52  // center of the image -- this is the center of the angluar distribution of the signal
53  pair<float,float>_userCenter(make_pair(settings.value("ImageXCenter", 500).toFloat(),
54  settings.value("ImageYCenter", 500).toFloat()));
55  // symmetry angle is the angle of in-plane rotation of the image with respect to its vertical axis
56  _symAngle = settings.value("SymmetryAngle", 0).toFloat() * M_PI / 180.;
57  // Set the minimum radius within range - must be within image
58  pair<float,float>_radiusRangeUser(make_pair(min(settings.value("MaxIncludedRadius",10).toFloat(),
59  settings.value("MinIncludedRadius",0).toFloat()),
60  max(settings.value("MaxIncludedRadius",10).toFloat(),
61  settings.value("MinIncludedRadius",0).toFloat())));
62  if (hist.dim() != 2)
63  throw invalid_argument("pp200::loadSettings()'" + name() +
64  "': Error the histogram we depend on '" + _image->name() +
65  "' is not a 2D Histogram.");
66  const result_t::axe_t &xaxis(hist.axis(result_t::xAxis));
67  const result_t::axe_t &yaxis(hist.axis(result_t::yAxis));
68  _center = make_pair(xaxis.bin(_userCenter.first),
69  yaxis.bin(_userCenter.second));
70  const size_t imagewidth (xaxis.nBins);
71  const size_t imageheight (yaxis.nBins);
72  const size_t dist_center_x_right (imagewidth - _center.first);
73  const size_t dist_center_y_top (imageheight - _center.second);
74  const size_t min_dist_x (min(dist_center_x_right, _center.first));
75  const size_t min_dist_y (min(dist_center_y_top, _center.second));
76  const size_t max_allowed_radius (min(min_dist_x, min_dist_y));
77  const size_t user_maxradius_hist_coord (xaxis.bin(_radiusRangeUser.second)- xaxis.bin(0));
78  const size_t maxRadius (min(max_allowed_radius, user_maxradius_hist_coord));
79  const size_t user_minradius_hist_coord (xaxis.bin(_radiusRangeUser.first)- xaxis.bin(0));
80  const size_t minRadius (max(size_t(1) , user_minradius_hist_coord));
81 
82  _radiusRange = make_pair(minRadius , maxRadius);
83  _imageWidth = imagewidth;
84  _nbrRadialPoints = maxRadius - minRadius;
85  _nbrAngularPoints = 360;
86 
88  Log::add(Log::INFO,"Processor '" + name() +
89  "' calculates cos2theta of image from Processor '" + _image->name() +
90  "' Center is x'"+ toString(_center.first) + "' y'" + toString(_center.second) +
91  "' Symmetry angle in radiants is '" + toString(_symAngle) +
92  "' Min radius the user requested is '" + toString(_radiusRangeUser.first) +
93  "' Max radius the user requested is '" + toString(_radiusRangeUser.second) +
94  "' Image width is '" + toString(_imageWidth) +
95  "' This results in Number of radial Points '" + toString(_nbrRadialPoints) +
96  "'. Condition is '" + _condition->name() + "'");
97 }
98 
100 {
101  const result_t &image(_image->result(evt.id()));
102  QReadLocker lock(&image.lock);
103 
104  float nom(0), denom(0), maxval(0);
105  for(size_t jr = 0; jr<_nbrRadialPoints; jr++)
106  {
107  for(size_t jth = 1; jth<_nbrAngularPoints; jth++)
108  {
109  const float radius(_radiusRange.first + jr);
110  const float angle(2.*M_PI * float(jth) / float(_nbrAngularPoints));
111  size_t col(size_t(_center.first + radius*sin(angle + _symAngle)));
112  size_t row(size_t(_center.second + radius*cos(angle + _symAngle)));
113  float val = image[col + row * _imageWidth];
114  denom += val * square(radius);
115  nom += val * square(cos(angle)) * square(radius);
116  maxval = max(val,maxval);
117  }
118  }
119  result.setValue((abs(denom) < 1e-15)?0.5:nom/denom);
120 }
121 
122 
123 
124 
125 
126 
127 
128 // *** processors 201 projects 2d hist to the radius for a selected center ***
129 
131  : Processor(name)
132 {
133  loadSettings(0);
134 }
135 
137 {
138  using namespace std;
139  CASSSettings settings;
140  settings.beginGroup("Processor");
142  pair<float,float> _userCenter(make_pair(settings.value("ImageXCenter", 500).toFloat(),
143  settings.value("ImageYCenter", 500).toFloat()));
144  pair<float,float> _radiusRangeUser(make_pair(settings.value("MinIncludedRadius",10).toFloat(),
145  settings.value("MaxIncludedRadius",0).toFloat()));
146  _nbrAngularPoints = settings.value("NbrAngularPoints",360.).toUInt();
147  setupGeneral();
148  _image = setupDependency("ImageName");
149  bool ret (setupCondition());
150  if (!(ret && _image))
151  return;
152  if (_image->result().dim() != 2)
153  throw invalid_argument("pp201::loadSettings()'" + name() +
154  "': Error the histogram we depend on '" + _image->name() +
155  "' is not a 2D Histogram.");
156  const result_t &hist(_image->result());
157  const result_t::axe_t &xaxis(hist.axis(result_t::xAxis));
158  const result_t::axe_t &yaxis(hist.axis(result_t::yAxis));
159  size_t imagewidth (xaxis.nBins);
160  _center = make_pair(xaxis.bin(_userCenter.first),
161  yaxis.bin(_userCenter.second));
162  _radiusRange = _radiusRangeUser;
163  _radiusRange.second = min(min(_radiusRange.second, _center.first + 0.5f), imagewidth - _center.first - 0.5f);
164  _radiusRange.second = min(min(_radiusRange.second, _center.second + 0.5f), imagewidth - _center.second - 0.5f);
165  _radiusRange.first = max(0.1f, min(_radiusRange.second - 1.0f , _radiusRangeUser.first));
166  // Set number of points on grid
167  _nbrRadialPoints = size_t(floor(_radiusRange.second - _radiusRange.first));
169  (new result_t(result_t::axe_t(_nbrAngularPoints, 0., 360.))));
170  Log::add(Log::INFO,"Processor '" + name() +
171  "' will calculate the angular distribution of '" + _image->name() +
172  "' from radia '" + toString(_radiusRange.first) + "' to '" +
173  toString(_radiusRange.second) + "' with center x:" + toString(_center.first) +
174  " y:" + toString(_center.second) + ". Number of Points on the axis '" +
175  toString(_nbrAngularPoints) + "'. Condition is '" + _condition->name() + "'");
176 }
177 
179 {
180  const result_t &image(_image->result(evt.id()));
181  QReadLocker lock(&image.lock);
182 
183  size_t width(image.axis(result_t::xAxis).nBins);
184 
185  for(size_t jr = 0; jr<_nbrRadialPoints ; jr++)
186  {
187  for(size_t jth = 0; jth<_nbrAngularPoints; jth++)
188  {
189  const float radius(jr+_radiusRange.first);
190  const float angle_deg(jth*360/_nbrAngularPoints);
191  const float angle(angle_deg * M_PI/ 180.f);
192  const float x(_center.first + radius*sin(angle));
193  const float y(_center.second + radius*cos(angle));
194  const size_t x1(static_cast<size_t>(x));
195  const size_t x2(x1 + 1);
196  const size_t y1(static_cast<size_t>(y));
197  const size_t y2(y1 + 1);
198  const float f11 (image[y1 * width + x1]);
199  const float f21 (image[y1 * width + x2]);
200  const float f12 (image[y2 * width + x1]);
201  const float f22 (image[y2 * width + x2]);
202  const float interpolateValue = f11*(x2 - x )*(y2 - y )+
203  f21*(x -x1)*(y2 - y )+
204  f12*(x2 - x )*(y - y1)+
205  f22*(x - x1)*(y - y1);
206  result[jth] += interpolateValue;
207  }
208  }
209 }
210 
211 
212 
213 
214 
215 // *** processor 202 transform 2d kartisian hist to polar coordinates ***
216 
218  : Processor(name)
219 {
220  loadSettings(0);
221 }
222 
224 {
225  using namespace std;
226  CASSSettings settings;
227  settings.beginGroup("Processor");
229  _userCenter = make_pair(settings.value("ImageXCenter", 500).toFloat(),
230  settings.value("ImageYCenter", 500).toFloat());
231  _nbrAngularPoints = settings.value("NbrAngularPoints",360.).toUInt();
232  _nbrRadialPoints = settings.value("NbrRadialPoints",500.).toUInt();
233  setupGeneral();
234  _image = setupDependency("ImageName");
235  bool ret (setupCondition());
236  if (!(ret && _image))
237  return;
238  if (_image->result().dim() != 2)
239  throw invalid_argument("pp202::loadSettings()'" + name() +
240  "': Error the histogram we depend on '" + _image->name() +
241  "' is not a 2D Histogram.");
242  const result_t &hist(_image->result());
243  const result_t::axe_t &xaxis(hist.axis(result_t::xAxis));
244  const result_t::axe_t &yaxis(hist.axis(result_t::yAxis));
245  _center = make_pair(xaxis.bin(_userCenter.first),
246  yaxis.bin(_userCenter.second));
247  const size_t imagewidth (xaxis.nBins);
248  const size_t imageheight (yaxis.nBins);
249  const size_t dist_center_x_right(imagewidth - _center.first);
250  const size_t dist_center_y_top(imageheight - _center.second);
251  const size_t min_dist_x (min(dist_center_x_right, _center.first));
252  const size_t min_dist_y (min(dist_center_y_top, _center.second));
253  _maxRadius = min(min_dist_x, min_dist_y);
254 
257  (new result_t
258  (result_t::axe_t(_nbrAngularPoints, 0., 360.,"#phi"),
260 
261  Log::add(Log::INFO,"Processor '" + name() + "' will transform '" +_image->name() +
262  "' to polar coordinates. Center x'"+ toString(_center.first) +"' y'" +
263  toString(_center.second) + "'. Maximum radius is '" + toString(_maxRadius) +
264  "'. Number of Points on the phi '" + toString(_nbrAngularPoints) +
265  "'. Number of Points on the radius '" + toString(_nbrRadialPoints) +
266  "'. Condition is '" + _condition->name() + "'");
267 }
268 
270 {
271  const result_t &image(_image->result(evt.id()));
272  QReadLocker lock(&image.lock);
273 
274  const size_t width(image.axis(result_t::xAxis).nBins);
275  for(size_t jr = 0; jr<_nbrRadialPoints ; jr++)
276  {
277  for(size_t jth = 0; jth<_nbrAngularPoints; jth++)
278  {
279  const float radius(jr*_maxRadius/_nbrRadialPoints);
280  const float angle_deg(jth*360/_nbrAngularPoints);
281  const float angle(angle_deg * M_PI/ 180.f);
282  const float x(_center.first + radius*sin(angle));
283  const float y(_center.second + radius*cos(angle));
284  const size_t x1(static_cast<size_t>(x));
285  const size_t x2(x1 + 1);
286  const size_t y1(static_cast<size_t>(y));
287  const size_t y2(y1 + 1);
288  const float f11 (image[y1 * width + x1]);
289  const float f21 (image[y1 * width + x2]);
290  const float f12 (image[y2 * width + x1]);
291  const float f22 (image[y2 * width + x2]);
292  const float interpolateValue = f11*(x2 - x )*(y2 - y )+
293  f21*(x -x1)*(y2 - y )+
294  f12*(x2 - x )*(y - y1)+
295  f22*(x - x1)*(y - y1);
296  result[jr*_nbrAngularPoints + jth] += interpolateValue;
297  }
298  }
299 }
300 
CachedList::item_type result_t
define the results
Definition: processor.h:52
Event to store all LCLS Data.
Definition: cass_event.h:32
virtual void createHistList(result_t::shared_pointer result)
create result list.
Definition: processor.cpp:79
std::pair< size_t, size_t > _center
center of the image in histogram coordinates
Definition: alignment.h:188
size_t _nbrRadialPoints
the number of radial, determinded by the _radiusRange
Definition: alignment.h:130
virtual void process(const CASSEvent &, result_t &)
calculate of averaged image
Definition: alignment.cpp:99
const name_t name() const
retrieve the name of this processor
Definition: processor.h:167
file contains declaration of the CASSEvent
shared_pointer _image
pp containing image that we will the angular distribution from
Definition: alignment.h:133
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
virtual void loadSettings(size_t)
load the histogram settings from CASS.ini
Definition: alignment.cpp:136
STL namespace.
an axis of a more than 0 dimensional container
Definition: result.hpp:29
float _symAngle
symmetry angle for calculation
Definition: alignment.h:61
size_t _nbrAngularPoints
the number of angular points that we include in the distribution
Definition: alignment.h:194
processors that calculate laser alignment parameters
result classes
pp201(const name_t &)
Construct processor for Gaussian height of image.
Definition: alignment.cpp:130
float _maxRadius
the maximal radius possible
Definition: alignment.h:191
T square(const T &val)
multiply number by itself
Definition: cass.h:78
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
fromStdString(const std::string &str)
size_t _nbrAngularPoints
the number of angular points that we include in the distribution
Definition: alignment.h:67
std::pair< size_t, size_t > _center
center of the image in histogram coordinates
Definition: alignment.h:55
virtual void process(const CASSEvent &, result_t &)
calculate of averaged image
Definition: alignment.cpp:178
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
virtual void loadSettings(size_t)
load the histogram settings from CASS.ini
Definition: alignment.cpp:38
std::pair< float, float > _userCenter
center of the image in user coordinates
Definition: alignment.h:185
const axis_t & axis() const
read access to the axis
Definition: result.hpp:449
file contains declaration of classes and functions that help other processors to do their job...
size_t _imageWidth
the width of the image
Definition: alignment.h:64
size_t _nbrRadialPoints
the number of radial, determinded by the _radiusRange
Definition: alignment.h:70
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
shared_pointer _image
pp containing image that we will calculate the from
Definition: alignment.h:73
size_t _nbrAngularPoints
the number of angular points that we include in the distribution
Definition: alignment.h:127
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
value(const QString &key, const QVariant &defaultValue=QVariant()
pp202(const name_t &)
Construct processor for Gaussian height of image.
Definition: alignment.cpp:217
virtual void process(const CASSEvent &, result_t &)
calculate of averaged image
Definition: alignment.cpp:269
void setupGeneral()
general setup of the processor
Definition: processor.cpp:85
file contains specialized class that do the settings for cass
shared_pointer _condition
pointer to the processor that will contain the condition
Definition: processor.h:277
size_t _nbrRadialPoints
the number of radial, determinded by the _radiusRange
Definition: alignment.h:197
std::pair< float, float > _radiusRange
the rane of radia used
Definition: alignment.h:124
virtual void loadSettings(size_t)
load the histogram settings from CASS.ini
Definition: alignment.cpp:223
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
std::pair< size_t, size_t > _center
center of the image in histogram coordinates
Definition: alignment.h:121
pp200(const name_t &)
Construct processor for Gaussian height of image.
Definition: alignment.cpp:32
shared_pointer _image
pp containing image that we will the angular distribution from
Definition: alignment.h:200
beginGroup(const QString &prefix)
std::pair< float, float > _radiusRange
the range of radia used
Definition: alignment.h:58
virtual const result_t & result(const CASSEvent::id_t eventid=0)
retrieve a result for a given id.
Definition: processor.cpp:54