Scope
Runstates.cpp
1 #include "stdafx.h"
2 #include "parameters/Runstates.h"
3 #include "helpers/ScopeException.h"
4 
5 namespace scope {
6 
7 namespace parameters {
8 
9 // Save some typing here...
10 using namespace boost::property_tree;
11 
12 Stack::Stack()
13  : spacing(1, 0.1, 50, L"Spacing_um")
14  , zdevicetype(ZDeviceHelper::ZStage, L"ZDeviceType")
15  , overalltime(1, 1, 2000, L"OverallTime_s") {
16  spacing.ConnectOther(std::bind(&Stack::UpdatePlanes, this));
17  zdevicetype.ConnectOther(std::bind(&Stack::ResetPlanes, this));
18  for ( auto& s : startat ) {
19  s.position.ConnectOther(std::bind(&Stack::UpdatePlanes, this));
20  s.pockels.ConnectOther(std::bind(&Stack::UpdatePlanes, this));
21  }
22  for ( auto& s : stopat ) {
23  s.position.ConnectOther(std::bind(&Stack::UpdatePlanes, this));
24  s.pockels.ConnectOther(std::bind(&Stack::UpdatePlanes, this));
25  }
26 }
27 
29  // Find max number of planes
30  uint32_t maxplanes = 0;
31  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
32  // +1 since stop plane also counts
33  uint32_t areaplanes = static_cast<uint32_t>(floor(Range(a) / spacing()) + 1);
34  maxplanes = (areaplanes > maxplanes)?areaplanes:maxplanes;
35  }
36 
37  planes___.clear();
38  planes___.resize(maxplanes);
39 
40  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
41  double l = Lambda(a);
42  double c = stopat[a].pockels()*exp(-l*stopat[a].position());
43  // +1 since stop plane also counts
44  uint32_t areaplanes = static_cast<uint32_t>(floor(Range(a) / spacing()) + 1);
45  DBOUT(L"Stack::UpdatePlanes area " << a << L" lambda: " << Lambda(a) << L" c: " << c);
46 
47  uint32_t p = 0;
48  // Planes in the defined range for that area are defined with increasing position and adjusted Pockels power
49  for ( ; p < areaplanes ; p++ ) {
50  planes___[p][a].position = startat[a].position() + p * Increment(a);
51  planes___[p][a].pockels = c * exp(l * planes___[p][a].position());
52  }
53  // If another area has more planes, set this area's planes with the last position and closed Pockels cell
54  for ( ; p < maxplanes ; p++ ) {
55  planes___[p][a].position = planes___[areaplanes-1][a].position();
56  planes___[p][a].pockels = 0;
57  }
58  }
59 }
60 
62  planes___.clear();
63  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
64  // Set to zero and do not call other signal (i.e. UpdatePlanes) right now
65  stopat[a].position.Set(0, true, false);
66  stopat[a].pockels.Set(0, true, false);
67  startat[a].position.Set(0, true, false);
68  startat[a].pockels.Set(0, true, false);
69  }
70  UpdatePlanes();
71 }
72 
73 double Stack::Range(const uint32_t& _area) {
74  return abs(stopat[_area].position() - startat[_area].position());
75 }
76 
77 double Stack::Increment(const uint32_t& _area) {
78  return (stopat[_area].position()>startat[_area].position())?spacing():-1.0*spacing();
79 }
80 
81 double Stack::Lambda(const uint32_t& _area) {
82  if ( (stopat[_area].pockels() == 0) || (startat[_area].pockels() == 0) )
83  return 0.0;
84  if ( startat[_area].position() == stopat[_area].position() )
85  return 0.0;
86  return log(startat[_area].pockels()/stopat[_area].pockels()) / (startat[_area].position()-stopat[_area].position());
87 }
88 
89 void Stack::Load(const wptree& pt) {
90  // Catch possible exceptions from Load, continue unharmed after
91  try {
92  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
93  startat[a].Load(pt.get_child(boost::str(boost::wformat(L"startat%d") % a)));
94  stopat[a].Load(pt.get_child(boost::str(boost::wformat(L"stopat%d") % a)));
95  }
96  }
97  catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
99  zdevicetype.SetFromPropertyTree(pt);
101 }
102 
103 void Stack::Save(wptree& pt) const {
104  std::array<wptree, SCOPE_NAREAS> ptstartat;
105  std::array<wptree, SCOPE_NAREAS> ptstopat;
106  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
107  startat[a].Save(ptstartat[a]);
108  pt.add_child(boost::str(boost::wformat(L"startat%d") % a), ptstartat[a]);
109  stopat[a].Save(ptstopat[a]);
110  pt.add_child(boost::str(boost::wformat(L"stopat%d") % a), ptstopat[a]);
111  }
113  zdevicetype.AddToPropertyTree(pt);
115 }
116 
118  bool enabler = false;
119  // Stack properties are changeable when not scanning or during continuous/live scanning
120  if ( (_runstate.t == RunStateHelper::Mode::Stopped) || (_runstate.t == RunStateHelper::Mode::RunningContinuous) )
121  enabler = true;
122  for ( auto& s : startat )
123  s.SetReadOnlyWhileScanning(_runstate);
124  for ( auto& s : stopat )
125  s.SetReadOnlyWhileScanning(_runstate);
126  spacing.SetRWState(enabler);
127  zdevicetype.SetRWState(enabler);
128 }
129 
130 Timeseries::Timeseries()
131  : triggerchannel(L"/PXI-6259_0/PFI0", L"Triggerchannel")
132  , triggered(false, false, true, L"Triggered")
133  , alltriggered(false, false, true, L"AllTriggered")
134  , repeats(1, 1, 100, L"Repeats")
135  , betweenrepeats(5, 1, 600, L"BetweenRepeats_s")
136  , overalltime(1, 1, 2000, L"OverallTime_s") {
137  std::wostringstream stream;
138  // array members have to have different names for correct loading/saving
139  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
140  stream.str(L"");
141  stream << L"Frames" << a;
142  frames[a] = ScopeNumber<uint32_t>(1, 1, 10000, stream.str());
143  stream.str(L"");
144  stream << L"Totaltimes_s" << a;
145  totaltimes[a] = ScopeNumber<double>(1, 0.5, 10000, stream.str());
146  }
147 }
148 
149 void Timeseries::Load(const wptree& pt) {
150  for ( auto& f : frames )
151  f.SetFromPropertyTree(pt);
152  for ( auto& t : totaltimes )
153  t.SetFromPropertyTree(pt);
160  // Catch possible exceptions from Load, continue unharmed after
161  try {
162  uint32_t p = 0;
163  // Load complete planes until no more found (which causes an exception)
164  while (true) {
165  const wptree planetree = pt.get_child(boost::str(boost::wformat(L"plane%d") % p));
166  // Load single area planes
167  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
168  PlaneProperties plane;
169  plane.Load(planetree.get_child(boost::str(boost::wformat(L"area%d") % a)));
170  }
171  p++;
172  }
173  }
174  catch (...) { // Just catch
175  }
176 }
177 
178 void Timeseries::Save(wptree& pt) const {
179  for ( const auto& f : frames )
180  f.AddToPropertyTree(pt);
181  for ( const auto& t : totaltimes )
182  t.AddToPropertyTree(pt);
183 
190  uint32_t ip = 0;
191  // Go through all complete planes
192  for ( const auto& p : planes ) {
193  wptree planetree;
194  std::array<wptree, SCOPE_NAREAS> areatree;
195  // Go through all single area planes
196  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
197  p[a].Save(areatree[a]);
198  planetree.add_child(boost::str(boost::wformat(L"area%d") % a), areatree[a]);
199  }
200  pt.add_child(boost::str(boost::wformat(L"plane%d") % ip), planetree);
201  ip++;
202  }
203 }
204 
206  const bool enabler = (_runstate.t==RunStateHelper::Mode::Stopped)?true:false;
207  for ( auto& f : frames )
208  f.SetRWState(enabler);
209  for ( auto& t : totaltimes )
210  t.SetRWState(enabler);
211  triggered.SetRWState(enabler);
212  alltriggered.SetRWState(enabler);
213  triggerchannel.SetRWState(enabler);
214  repeats.SetRWState(enabler);
215  betweenrepeats.SetRWState(enabler);
216  overalltime.SetRWState(enabler);
217 }
218 
219 Behavior::Behavior()
220  : mode(BehaviorModeHelper::Gated, L"BehaviorMode")
221  , unlimited_repeats(true, false, true, L"UnlimitedRepeats")
222  , repeats(1, 1, 10000, L"Repeats")
223  , gateline(L"PXI-6229/port0/line0", L"GateLine") {
224 }
225 
226 void Behavior::Load(const wptree& pt) {
227  mode.SetFromPropertyTree(pt);
231  // Catch possible exceptions from Load, continue unharmed after
232  try {
233  uint32_t p = 0;
234  // Load complete planes until no more found (which causes an exception)
235  while (true) {
236  const wptree planetree = pt.get_child(boost::str(boost::wformat(L"plane%d") % p));
237  // Load single area planes
238  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
239  PlaneProperties plane;
240  plane.Load(planetree.get_child(boost::str(boost::wformat(L"area%d") % a)));
241  }
242  p++;
243  }
244  }
245  catch (...) { // Just catch
246  }
247 }
248 
249 void Behavior::Save(wptree& pt) const {
250  mode.AddToPropertyTree(pt);
254  uint32_t ip = 0;
255  // Go through all complete planes
256  for ( const auto& p : planes ) {
257  wptree planetree;
258  std::array<wptree, SCOPE_NAREAS> areatree;
259  // Go through all single area planes
260  for ( uint32_t a = 0 ; a < SCOPE_NAREAS ; a++ ) {
261  p[a].Save(areatree[a]);
262  planetree.add_child(boost::str(boost::wformat(L"area%d") % a), areatree[a]);
263  }
264  pt.add_child(boost::str(boost::wformat(L"plane%d") % ip), planetree);
265  ip++;
266  }
267 }
268 
270  const bool enabler = (_runstate.t==RunStateHelper::Mode::Stopped)?true:false;
271  mode.SetRWState(enabler);
272  unlimited_repeats.SetRWState(enabler);
273  repeats.SetRWState(enabler);
274 }
275 
276 
277 
278 }
279 
280 }
void Load(const wptree &pt) override
load parameters from a boost::property_tree
Definition: Runstates.cpp:226
ScopeNumber< bool > unlimited_repeats
Should acquisition go on until Stop pressed (true) or until repeats done (false)? ...
Definition: Runstates.h:118
ScopeValue< ZDevice > zdevicetype
type of z device to use
Definition: Runstates.h:38
void SetReadOnlyWhileScanning(const RunState &_runstate) override
set values that must not be changed to read-only during scanning.
Definition: Runstates.cpp:117
void AddToPropertyTree(boost::property_tree::wptree &pt) const
Adds the value to a Boost property tree, using its name and value.
Definition: ScopeValue.h:103
ScopeNumber< uint32_t > repeats
How often should the timeseries be repeated.
Definition: Runstates.h:87
Parameters of a single plane for fast plane hopping/switching.
Definition: Plane.h:12
void Load(const wptree &pt) override
load parameters from a boost::property_tree
Definition: Plane.cpp:16
ScopeString triggerchannel
channel name for trigger input
Definition: Runstates.h:78
std::array< ScopeNumber< uint32_t >, SCOPE_NAREAS > frames
number of frames in timeseries for each area
Definition: Runstates.h:72
Base class for all Scope datatypes here, provides a uniform interface (and saves typing...).
ScopeNumber< double > overalltime
time in seconds for all timeseries
Definition: Runstates.h:41
virtual void ResetPlanes()
Resets start/stop and all planes.
Definition: Runstates.cpp:61
std::vector< std::array< PlaneProperties, SCOPE_NAREAS > > planes___
Vector with properties for every plane.
Definition: Runstates.h:35
virtual double Increment(const uint32_t &_area)
distance between planes
Definition: Runstates.cpp:77
ScopeNumber< double > spacing
Spacing between planes in microns.
Definition: Runstates.h:32
This is the include file for standard system include files, or project specific include files that ar...
T::Mode t
the enum from the template class
ScopeNumber< uint32_t > repeats
How many repeats should be acquired if not unlimited_repeats?
Definition: Runstates.h:121
void Save(wptree &pt) const override
save parameters into a boost:property_tree
Definition: Runstates.cpp:178
How does the animal behavior trigger/gate acquisition?
virtual double Range(const uint32_t &_area)
total span of z stack
Definition: Runstates.cpp:73
ScopeNumber< bool > triggered
the current/first repeat is triggered on triggerchannel
Definition: Runstates.h:81
void Save(wptree &pt) const override
save parameters into a boost:property_tree
Definition: Runstates.cpp:103
virtual void UpdatePlanes()
Updates planes with positions and pockels adjustment etc.
Definition: Runstates.cpp:28
void Load(const wptree &pt) override
load parameters from a boost::property_tree
Definition: Runstates.cpp:89
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
void SetReadOnlyWhileScanning(const RunState &_runstate) override
set values that must not be changed to read-only during scanning.
Definition: Runstates.cpp:269
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
ScopeNumber< double > overalltime
time in seconds for all timeseries
Definition: Runstates.h:93
void Save(wptree &pt) const override
save parameters into a boost:property_tree
Definition: Runstates.cpp:249
void SetReadOnlyWhileScanning(const RunState &_runstate) override
set values that must not be changed to read-only during scanning.
Definition: Runstates.cpp:205
std::array< ScopeNumber< double >, SCOPE_NAREAS > totaltimes
total acquisition time for each area
Definition: Runstates.h:75
void SetRWState(const bool &_state)
Sets readonly/read-write state.
std::vector< std::array< PlaneProperties, SCOPE_NAREAS > > planes
vector with properties for all planes, for alternating planes on alternating repeats, one for each area
Definition: Runstates.h:124
std::vector< std::array< PlaneProperties, SCOPE_NAREAS > > planes
vector with properties for all planes, for alternating planes on repeating timeseries, one for each area
Definition: Runstates.h:96
ScopeNumber< double > betweenrepeats
Time in seconds between the beginning of one and beginning of the next timeseries.
Definition: Runstates.h:90
void SetFromPropertyTree(const boost::property_tree::wptree &pt)
Set value from a Boost property, using its name as a key.
Definition: ScopeValue.h:108
virtual double Lambda(const uint32_t &_area)
space constant for exponential pockels interpolation
Definition: Runstates.cpp:81
ScopeNumber< bool > alltriggered
every repeat is triggered
Definition: Runstates.h:84
ScopeString gateline
The DAQmx digital line for gating.
Definition: Runstates.h:127
ScopeValue< BehaviorMode > mode
Which mode to use.
Definition: Runstates.h:115
void Load(const wptree &pt) override
load parameters from a boost::property_tree
Definition: Runstates.cpp:149