source: trunk/FACT++/src/ratescan.cc@ 14009

Last change on this file since 14009 was 14009, checked in by tbretz, 12 years ago
Exit with 127 if parsing of command line options failed.
File size: 17.2 KB
Line 
1#include <valarray>
2
3#include "Dim.h"
4#include "Event.h"
5#include "Shell.h"
6#include "StateMachineDim.h"
7#include "Connection.h"
8#include "Configuration.h"
9#include "Console.h"
10#include "Converter.h"
11//#include "PixelMap.h"
12
13#include "tools.h"
14
15#include "LocalControl.h"
16
17#include "HeadersFTM.h"
18#include "HeadersRateScan.h"
19
20namespace ba = boost::asio;
21namespace bs = boost::system;
22
23using namespace std;
24
25// ------------------------------------------------------------------------
26
27#include "DimDescriptionService.h"
28#include "DimState.h"
29
30// ------------------------------------------------------------------------
31
32class StateMachineRateScan : public StateMachineDim
33{
34private:
35 DimVersion fDim;
36 DimState fDimFTM;
37
38 DimDescribedService fDimData;
39 DimDescribedService fDimProc;
40
41 int fCounter;
42 int fCounterMax;
43
44 int fThreshold;
45 int fThresholdMin;
46 int fThresholdMax;
47 int fThresholdStep;
48 int fThresholdStepDyn;
49
50 double fRate;
51 double fRateBoard[40];
52 double fRatePatch[160];
53
54 double fOnTime;
55
56 uint64_t fStartTime;
57
58 float fResolution;
59
60 enum reference_t
61 {
62 kCamera,
63 kBoard,
64 kPatch
65 };
66
67 reference_t fReference;
68 uint16_t fReferenceIdx;
69
70 string fCommand;
71
72 void UpdateProc()
73 {
74 const array<uint32_t,3> v = {{ fThresholdMin, fThresholdMax, fThresholdStep }};
75 fDimProc.Update(v);
76 }
77
78 bool CheckEventSize(const EventImp &evt, size_t size)
79 {
80 if (size_t(evt.GetSize())==size)
81 return true;
82
83 if (evt.GetSize()==0)
84 return false;
85
86 ostringstream msg;
87 msg << evt.GetName() << " - Received event has " << evt.GetSize() << " bytes, but expected " << size << ".";
88 Fatal(msg);
89 return false;
90 }
91
92 int HandleTriggerRates(const EventImp &evt)
93 {
94 if (!CheckEventSize(evt, sizeof(FTM::DimTriggerRates)))
95 return GetCurrentState();
96
97 if (GetCurrentState()!=RateScan::State::kInProgress)
98 return GetCurrentState();
99
100 const FTM::DimTriggerRates &sdata = *static_cast<const FTM::DimTriggerRates*>(evt.GetData());
101
102 if (++fCounter<0)
103 return GetCurrentState();
104
105 if (fCounter==0)
106 {
107 fRate = 0;
108
109 memset(fRateBoard, 0, 40*sizeof(double));
110 memset(fRatePatch, 0, 160*sizeof(double));
111
112 fOnTime = 0;
113 return GetCurrentState();
114 }
115/*
116 if (sdata.fTriggerRate==0)
117 {
118 Message("Rate scan stopped due zero trigger rate.");
119 fThreshold = -1;
120 return;
121 }
122*/
123
124 fRate += sdata.fTriggerRate;
125 for (int i=0; i<40; i++)
126 fRateBoard[i] += sdata.fBoardRate[i];
127 for (int i=0; i<160; i++)
128 fRatePatch[i] += sdata.fPatchRate[i];
129
130 double reference = fRate;
131 if (fReference==kBoard)
132 reference = fRateBoard[fReferenceIdx];
133 if (fReference==kPatch)
134 reference = fRatePatch[fReferenceIdx];
135
136 fOnTime += sdata.fOnTime;
137
138 reference *= sdata.fElapsedTime;
139
140 if ((reference==0 || sqrt(reference)>fResolution*reference) && fCounter<fCounterMax)
141 {
142 ostringstream out;
143 out << "Triggers so far: " << reference;
144 if (reference>0)
145 out << " (" << sqrt(reference)/reference << ")";
146 Info(out);
147
148 return GetCurrentState();
149 }
150
151 const double time = sdata.fElapsedTime*fCounter;
152 const uint32_t th = fThreshold;
153
154 float data[2+3+1+40+160];
155 memcpy(data, &fStartTime, 8);
156 memcpy(data+2, &th, 4);
157 data[3] = time; // total elapsed time
158 data[4] = fOnTime/time; // relative on time
159 data[5] = fRate/fCounter;
160 for (int i=0; i<40; i++)
161 data[i+6] = fRateBoard[i]/fCounter;
162 for (int i=0; i<160; i++)
163 data[i+46] = fRatePatch[i]/fCounter;
164
165 ostringstream sout1, sout2, sout3;
166
167 sout1 << th << " " << data[5];
168 for (int i=0; i<200; i++)
169 sout2 << " " << data[i+6];
170 sout3 << " " << data[3] << " " << data[4];
171
172 Info(sout1.str());
173
174 ofstream fout("ratescan.txt", ios::app);
175 fout << sout1.str() << sout2.str() << sout3.str() << endl;
176
177 fDimData.setQuality(fCommand=="FTM_CONTROL/SET_THRESHOLD");
178 fDimData.setData(data, sizeof(data));
179 fDimData.Update();
180
181 fThreshold += fThresholdStep;
182
183 if (fCounter>=fCounterMax)
184 {
185 Message("Rate scan stopped due to timeout.");
186 return RateScan::State::kConnected;
187 }
188
189 if (fThreshold>fThresholdMax)
190 {
191 Message("Rate scan finished.");
192 return RateScan::State::kConnected;
193 }
194
195 // Does this need to be shifted upwards?
196 if (fCounter>1 && fThresholdStepDyn>0)
197 {
198 //const double scale = fCounter/reference/fResolution/fResolution;
199 //const double step = floor(scale*fThresholdStepDyn);
200
201 fThresholdStep = fCounter*fThresholdStepDyn;
202 }
203
204 //fCounter = -2; // FIXME: In principle one missed report is enough
205 fCounter = -1;
206
207 const int32_t cmd[2] = { -1, fThreshold };
208 DimClient::sendCommandNB(fCommand.c_str(), (void*)cmd, 8);
209
210 return GetCurrentState();
211 }
212
213 int Print() const
214 {
215 Out() << fDim << endl;
216 Out() << fDimFTM << endl;
217
218 return GetCurrentState();
219 }
220
221 int StartRateScan(const EventImp &evt, const string &command)
222 {
223 if (!CheckEventSize(evt, 12))
224 return kSM_FatalError;
225
226 fCommand = "FTM_CONTROL/"+command;
227
228 const int32_t step = evt.Get<int32_t>(8);
229
230 fThresholdMin = evt.Get<uint32_t>();
231 fThresholdMax = evt.Get<uint32_t>(4);
232 fThresholdStep = abs(step);
233
234 fThresholdStepDyn = step<0 ? -step : 0;
235
236 UpdateProc();
237
238 const string data = "\0\0\0\0\0\0\0\0ratescan";
239
240 Dim::SendCommand("FAD_CONTROL/SET_FILE_FORMAT", uint16_t(0));
241 Dim::SendCommand("FTM_CONTROL/CONFIGURE", data);
242
243 Message("Configuration for ratescan started.");
244
245 return RateScan::State::kConfiguring;
246 }
247
248 int HandleFtmStateChange(/*const EventImp &evt*/)
249 {
250 if (GetCurrentState()!=RateScan::State::kConfiguring)
251 return GetCurrentState();
252
253 if (fDimFTM.state()!=FTM::State::kConfigured)
254 return GetCurrentState();
255
256 const int32_t data[2] = { -1, fThresholdMin };
257
258 Dim::SendCommand(fCommand, data);
259
260 fThreshold = fThresholdMin;
261 fCounter = -2;
262
263 const Time now;
264 fStartTime = trunc(now.UnixTime());
265
266 ofstream fout("ratescan.txt", ios::app);
267 fout << "# ----- " << now << " (" << fStartTime << ") -----\n";
268 fout << "# Command: " << fCommand << '\n';
269 fout << "# Reference: ";
270 switch (fReference)
271 {
272 case kCamera: fout << "Camera"; break;
273 case kBoard: fout << "Board #" << fReferenceIdx; break;
274 case kPatch: fout << "Patch #" << fReferenceIdx; break;
275 }
276 fout << '\n';
277 fout << "# -----" << endl;
278
279 ostringstream msg;
280 msg << "Rate scan " << now << "(" << fStartTime << ") from " << fThresholdMin << " to ";
281 msg << fThresholdMax << " in steps of " << fThresholdStep;
282 msg << " started.";
283 Message(msg);
284
285 return RateScan::State::kInProgress;
286 }
287
288 int StopRateScan()
289 {
290 if (GetCurrentState()!=RateScan::State::kConfiguring && GetCurrentState()!=RateScan::State::kInProgress)
291 return GetCurrentState();
292
293 Message("Rate scan manually stopped.");
294
295 return RateScan::State::kConnected;
296 }
297
298 int SetReferenceCamera()
299 {
300 fReference = kCamera;
301
302 return GetCurrentState();
303 }
304
305 int SetReferenceBoard(const EventImp &evt)
306 {
307 if (!CheckEventSize(evt, 4))
308 return kSM_FatalError;
309
310 if (evt.GetUInt()>39)
311 {
312 Error("SetReferenceBoard - Board index out of range [0;39]");
313 return GetCurrentState();
314 }
315
316 fReference = kBoard;
317 fReferenceIdx = evt.GetUInt();
318
319 return GetCurrentState();
320 }
321
322 int SetReferencePatch(const EventImp &evt)
323 {
324 if (!CheckEventSize(evt, 4))
325 return kSM_FatalError;
326
327 if (evt.GetUInt()>159)
328 {
329 Error("SetReferencePatch - Patch index out of range [0;159]");
330 return GetCurrentState();
331 }
332
333 fReference = kPatch;
334 fReferenceIdx = evt.GetUInt();
335
336 return GetCurrentState();
337 }
338
339 int ChangeStepSize(const EventImp &evt)
340 {
341 if (!CheckEventSize(evt, 4))
342 return kSM_FatalError;
343
344 fThresholdStep = evt.Get<uint32_t>();
345
346 ostringstream msg;
347 msg << "New step size " << fThresholdStep;
348 Info(msg);
349
350 UpdateProc();
351
352 return GetCurrentState();
353 }
354
355 int ChangeMaximum(const EventImp &evt)
356 {
357 if (!CheckEventSize(evt, 4))
358 return kSM_FatalError;
359
360 fThresholdMax = evt.Get<uint32_t>();
361
362 return GetCurrentState();
363 }
364
365 int Execute()
366 {
367 if (!fDim.online())
368 return RateScan::State::kDimNetworkNA;
369
370 // All subsystems are not connected
371 if (fDimFTM.state()<FTM::State::kConnected)
372 return RateScan::State::kDisconnected;
373
374 return GetCurrentState();
375 }
376
377public:
378 StateMachineRateScan(ostream &out=cout) : StateMachineDim(out, "RATE_SCAN"),
379 fDimFTM("FTM_CONTROL"),
380 fDimData("RATE_SCAN/DATA", "X:1;I:1;F:1;F:1;F:1;F:40;F:160",
381 "|Id[s]:Start time used to identify measurement (UnixTime)"
382 "|Threshold[dac]:Threshold in DAC counts"
383 "|ElapsedTime[s]:Real elapsed time"
384 "|RelOnTime[ratio]:Relative on time"
385 "|TriggerRate[Hz]:Camera trigger rate"
386 "|BoardRate[Hz]:Board trigger rates"
387 "|PatchRate[Hz]:Patch trigger rates"),
388 fDimProc("RATE_SCAN/PROCESS_DATA", "I:1;I:1;I:1",
389 "Rate scan process data"
390 "|min[DAC]:Value at which scan was started"
391 "|max[DAC]:Value at which scan will end"
392 "|step[DAC]:Step size for scan"),
393 fThreshold(-1), fReference(kCamera), fReferenceIdx(0)
394 {
395 // ba::io_service::work is a kind of keep_alive for the loop.
396 // It prevents the io_service to go to stopped state, which
397 // would prevent any consecutive calls to run()
398 // or poll() to do nothing. reset() could also revoke to the
399 // previous state but this might introduce some overhead of
400 // deletion and creation of threads and more.
401
402 fDim.Subscribe(*this);
403 fDimFTM.Subscribe(*this);
404 fDimFTM.SetCallback(bind(&StateMachineRateScan::HandleFtmStateChange, this));
405
406 Subscribe("FTM_CONTROL/TRIGGER_RATES")
407 (bind(&StateMachineRateScan::HandleTriggerRates, this, placeholders::_1));
408
409 // State names
410 AddStateName(RateScan::State::kDimNetworkNA, "DimNetworkNotAvailable",
411 "The Dim DNS is not reachable.");
412
413 AddStateName(RateScan::State::kDisconnected, "Disconnected",
414 "The Dim DNS is reachable, but the required subsystems are not available.");
415
416 AddStateName(RateScan::State::kConnected, "Connected",
417 "All needed subsystems are connected to their hardware, no action is performed.");
418
419 AddStateName(RateScan::State::kInProgress, "InProgress",
420 "Rate scan in progress.");
421
422 AddEvent("START_THRESHOLD_SCAN", "I:3", RateScan::State::kConnected)
423 (bind(&StateMachineRateScan::StartRateScan, this, placeholders::_1, "SET_THRESHOLD"))
424 ("Start rate scan for the threshold in the defined range"
425 "|min[int]:Start value in DAC counts"
426 "|max[int]:Limiting value in DAC counts"
427 "|step[int]:Single step in DAC counts");
428
429 AddEvent("START_N_OUT_OF_4_SCAN", "I:3", RateScan::State::kConnected)
430 (bind(&StateMachineRateScan::StartRateScan, this, placeholders::_1, "SET_N_OUT_OF_4"))
431 ("Start rate scan for N-out-of-4 in the defined range"
432 "|min[int]:Start value in DAC counts"
433 "|max[int]:Limiting value in DAC counts"
434 "|step[int]:Single step in DAC counts");
435
436 AddEvent("CHANGE_STEP_SIZE", "I:1", RateScan::State::kInProgress)
437 (bind(&StateMachineRateScan::ChangeStepSize, this, placeholders::_1))
438 ("Change the step size during a ratescan in progress"
439 "|step[int]:Single step in DAC counts");
440
441 AddEvent("CHANGE_MAXIMUM", "I:1", RateScan::State::kInProgress)
442 (bind(&StateMachineRateScan::ChangeMaximum, this, placeholders::_1))
443 ("Change the maximum limit during a ratescan in progress"
444 "|max[int]:Limiting value in DAC counts");
445
446 AddEvent("STOP", RateScan::State::kConfiguring, RateScan::State::kInProgress)
447 (bind(&StateMachineRateScan::StopRateScan, this))
448 ("Stop a ratescan in progress");
449
450 AddEvent("SET_REFERENCE_CAMERA", RateScan::State::kDimNetworkNA, RateScan::State::kDisconnected, RateScan::State::kConnected)
451 (bind(&StateMachineRateScan::SetReferenceCamera, this))
452 ("Use the camera trigger rate as reference for the reolution");
453 AddEvent("SET_REFERENCE_BOARD", "I:1", RateScan::State::kDimNetworkNA, RateScan::State::kDisconnected, RateScan::State::kConnected)
454 (bind(&StateMachineRateScan::SetReferenceBoard, this, placeholders::_1))
455 ("Use the given board trigger-rate as reference for the reolution"
456 "|board[idx]:Index of the board (4*crate+board)");
457 AddEvent("SET_REFERENCE_PATCH", "I:1", RateScan::State::kDimNetworkNA, RateScan::State::kDisconnected, RateScan::State::kConnected)
458 (bind(&StateMachineRateScan::SetReferenceBoard, this, placeholders::_1))
459 ("Use the given patch trigger-rate as reference for the reolution"
460 "|patch[idx]:Index of the patch (360*crate+36*board+patch)");
461
462 AddEvent("PRINT")
463 (bind(&StateMachineRateScan::Print, this))
464 ("");
465 }
466
467 int EvalOptions(Configuration &conf)
468 {
469 fCounterMax = conf.Get<uint16_t>("max-wait");
470 fResolution = conf.Get<double>("resolution");
471
472 return -1;
473 }
474};
475
476// ------------------------------------------------------------------------
477
478#include "Main.h"
479
480template<class T>
481int RunShell(Configuration &conf)
482{
483 return Main::execute<T, StateMachineRateScan>(conf);
484}
485
486void SetupConfiguration(Configuration &conf)
487{
488 po::options_description control("Rate scan options");
489 control.add_options()
490 ("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
491 ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
492 ;
493
494 conf.AddOptions(control);
495}
496
497/*
498 Extract usage clause(s) [if any] for SYNOPSIS.
499 Translators: "Usage" and "or" here are patterns (regular expressions) which
500 are used to match the usage synopsis in program output. An example from cp
501 (GNU coreutils) which contains both strings:
502 Usage: cp [OPTION]... [-T] SOURCE DEST
503 or: cp [OPTION]... SOURCE... DIRECTORY
504 or: cp [OPTION]... -t DIRECTORY SOURCE...
505 */
506void PrintUsage()
507{
508 cout <<
509 "The ratescan program is a tool for automation of rate scans.\n"
510 "\n"
511 "Usage: ratescan [-c type] [OPTIONS]\n"
512 " or: ratescan [OPTIONS]\n";
513 cout << endl;
514}
515
516void PrintHelp()
517{
518 Main::PrintHelp<StateMachineRateScan>();
519
520 /* Additional help text which is printed after the configuration
521 options goes here */
522
523 /*
524 cout << "bla bla bla" << endl << endl;
525 cout << endl;
526 cout << "Environment:" << endl;
527 cout << "environment" << endl;
528 cout << endl;
529 cout << "Examples:" << endl;
530 cout << "test exam" << endl;
531 cout << endl;
532 cout << "Files:" << endl;
533 cout << "files" << endl;
534 cout << endl;
535 */
536}
537
538int main(int argc, const char* argv[])
539{
540 Configuration conf(argv[0]);
541 conf.SetPrintUsage(PrintUsage);
542 Main::SetupConfiguration(conf);
543 SetupConfiguration(conf);
544
545 if (!conf.DoParse(argc, argv, PrintHelp))
546 return 127;
547
548 //try
549 {
550 // No console access at all
551 if (!conf.Has("console"))
552 {
553// if (conf.Get<bool>("no-dim"))
554// return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
555// else
556 return RunShell<LocalStream>(conf);
557 }
558 // Cosole access w/ and w/o Dim
559/* if (conf.Get<bool>("no-dim"))
560 {
561 if (conf.Get<int>("console")==0)
562 return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
563 else
564 return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
565 }
566 else
567*/ {
568 if (conf.Get<int>("console")==0)
569 return RunShell<LocalShell>(conf);
570 else
571 return RunShell<LocalConsole>(conf);
572 }
573 }
574 /*catch (std::exception& e)
575 {
576 cerr << "Exception: " << e.what() << endl;
577 return -1;
578 }*/
579
580 return 0;
581}
Note: See TracBrowser for help on using the repository browser.