source: trunk/FACT++/src/ratecontrol.cc@ 12450

Last change on this file since 12450 was 12441, checked in by tbretz, 13 years ago
Added a counter to skip the first rate report - for stability reasons.
File size: 19.5 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 "DimServiceInfoList.h"
12//#include "PixelMap.h"
13
14#include "tools.h"
15
16#include "LocalControl.h"
17
18#include "HeadersFTM.h"
19
20namespace ba = boost::asio;
21namespace bs = boost::system;
22namespace dummy = ba::placeholders;
23
24using namespace std;
25
26// ------------------------------------------------------------------------
27
28#include "DimDescriptionService.h"
29
30// ------------------------------------------------------------------------
31
32class StateMachineRateControl : public StateMachineDim, public DimInfoHandler
33{
34private:
35 enum states_t
36 {
37 kStateDimNetworkNA = 1,
38 kStateDisconnected,
39 kStateConnecting,
40 kStateConnected,
41
42 kStateSettingGlobalThreshold,
43 kStateGlobalThresholdSet,
44
45 kStateInProgress,
46 };
47
48 DimServiceInfoList fNetwork;
49
50 pair<Time, int> fStatusDim;
51 pair<Time, int> fStatusFTM;
52
53 DimStampedInfo fDim;
54 DimStampedInfo fFTM;
55 DimStampedInfo fRates;
56 DimStampedInfo fStatic;
57
58// DimDescribedService fDimData;
59// DimDescribedService fDimProc;
60
61 float fTargetRate;
62 float fTriggerRate;
63
64 uint16_t fThresholdMin;
65 uint16_t fThresholdReference;
66
67 bool fTriggerOn;
68 bool fVerbose;
69 bool fEnabled;
70
71 uint64_t fCounter;
72
73 pair<Time, int> GetNewState(DimStampedInfo &info) const
74 {
75 const bool disconnected = info.getSize()==0;
76
77 // Make sure getTimestamp is called _before_ getTimestampMillisecs
78 const int tsec = info.getTimestamp();
79 const int tms = info.getTimestampMillisecs();
80
81 return make_pair(Time(tsec, tms*1000),
82 disconnected ? -2 : info.getQuality());
83 }
84
85 bool CheckEventSize(size_t has, const char *name, size_t size)
86 {
87 if (has==size)
88 return true;
89
90 if (has==0)
91 return false;
92
93 ostringstream msg;
94 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
95 Fatal(msg);
96 return false;
97 }
98
99 vector<uint16_t> fThresholds;
100
101 void PrintThresholds(const FTM::DimStaticData &sdata)
102 {
103 //if (!fVerbose)
104 // return;
105
106 if (fThresholds.size()==0)
107 return;
108
109 Out() << "Min. DAC=" << fThresholdMin << endl;
110
111 int t=0;
112 for (t=0; t<160; t++)
113 if (sdata.fThreshold[t]!=fThresholds[t])
114 break;
115
116 if (t==160)
117 return;
118
119 for (int j=0; j<10; j++)
120 {
121 for (int k=0; k<4; k++)
122 {
123 for (int i=0; i<4; i++)
124 if (fThresholds[i+k*4+j*16]!=fThresholdMin)
125 Out() << setw(3) << fThresholds[i+k*4+j*16] << " ";
126 else
127 Out() << " - ";
128 Out() << " ";
129 }
130 Out() << endl;
131 }
132 Out() << endl;
133 }
134
135 void Step(int idx, float step)
136 {
137 uint16_t diff = fThresholds[idx]+int16_t(truncf(step));
138 if (diff<fThresholdMin)
139 diff=fThresholdMin;
140
141 if (diff==fThresholds[idx])
142 return;
143
144 if (fVerbose)
145 {
146 Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
147 Out() << (step>0 ? " += " : " -= ");
148 Out() << step << " (" << diff << ")" << endl;
149 }
150
151 const uint32_t val[2] = { idx, diff };
152 DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
153 }
154
155 void ProcessPatches(const FTM::DimTriggerRates &sdata)
156 {
157
158 // Caluclate Median and deviation
159 vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
160 vector<float> medp(sdata.fPatchRate, sdata.fPatchRate+160);
161
162 sort(medb.begin(), medb.end());
163 sort(medp.begin(), medp.end());
164
165 vector<float> devb(40);
166 for (int i=0; i<40; i++)
167 devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
168
169 vector<float> devp(160);
170 for (int i=0; i<160; i++)
171 devp[i] = fabs(sdata.fPatchRate[i]-medp[i]);
172
173 sort(devb.begin(), devb.end());
174 sort(devp.begin(), devp.end());
175
176 double mb = (medb[19]+medb[20])/2;
177 double mp = (medp[79]+medp[80])/2;
178
179 double db = devb[27];
180 double dp = devp[109];
181
182 // If any is zero there is something wrong
183 if (mb==0 || mp==0 || db==0 || dp==0)
184 return;
185
186 if (fVerbose)
187 {
188 Out() << "Patch: Median=" << mp << " Dev=" << dp << endl;
189 Out() << "Board: Median=" << mb << " Dev=" << db << endl;
190 }
191
192 for (int i=0; i<40; i++)
193 {
194 int maxi = -1;
195
196 const float dif = fabs(sdata.fBoardRate[i]-mb)/db;
197 if (dif>5)
198 {
199 if (fVerbose)
200 Out() << "B" << i << ": " << dif << endl;
201
202 float max = sdata.fPatchRate[i*4];
203 maxi = 0;
204
205 for (int j=1; j<4; j++)
206 if (sdata.fPatchRate[i*4+j]>max)
207 {
208 max = sdata.fPatchRate[i*4+j];
209 maxi = j;
210 }
211 }
212
213 for (int j=0; j<4; j++)
214 {
215 // For the noise pixel correct down to median+3*deviation
216 if (maxi==j)
217 {
218 // This is the step which has to be performed to go from
219 // a NSB rate of sdata.fPatchRate[i*4+j]
220
221
222 const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+5*dp))/0.039;
223 // * (dif-5)/dif
224 Step(i*4+j, step);
225 continue;
226 }
227
228 // For pixels below the meadian correct also back to median+3*deviation
229 if (sdata.fPatchRate[i*4+j]<mp)
230 {
231 const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3*dp))/0.039;
232 Step(i*4+j, step);
233 continue;
234 }
235
236 const float step = -1.5*(log10(mp+dp)-log10(mp))/0.039;
237 Step(i*4+j, step);
238 }
239 }
240 }
241
242 void ProcessCamera(const FTM::DimTriggerRates &sdata)
243 {
244 if (fCounter++==0)
245 return;
246
247 // Caluclate Median and deviation
248 vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
249
250 sort(medb.begin(), medb.end());
251
252 vector<float> devb(40);
253 for (int i=0; i<40; i++)
254 devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
255
256 sort(devb.begin(), devb.end());
257
258 double mb = (medb[19]+medb[20])/2;
259 double db = devb[27];
260
261 // If any is zero there is something wrong
262 if (mb==0 || db==0)
263 return;
264
265 double avg = 0;
266 int num = 0;
267
268 for (int i=0; i<40; i++)
269 {
270 if ( fabs(sdata.fBoardRate[i]-mb)<3*db)
271 {
272 avg += sdata.fBoardRate[i];
273 num++;
274 }
275 }
276
277 fTriggerRate = avg / num * 40;
278
279 if (fVerbose)
280 {
281 Out() << "Board: Median=" << mb << " Dev=" << db << endl;
282 Out() << "Camera: " << fTriggerRate << " (" << sdata.fTriggerRate << ", n=" << num << ")" << endl;
283 Out() << "Target: " << fTargetRate << endl;
284 }
285
286 // ----------------------
287
288 if (fTriggerRate>0 && fTriggerRate<fTargetRate)
289 {
290 // I am assuming here (and at other places) the the answer from the FTM when setting
291 // the new threshold always arrives faster than the next rate update.
292 fThresholdMin = fThresholds[0];
293 return;
294 }
295
296 // This is a step towards a threshold at which the NSB rate is equal the target rate
297 // +1 to avoid getting a step of 0
298 const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
299
300 const uint16_t diff = fThresholds[0]+int16_t(truncf(step));
301
302 cout << diff << endl;
303
304 if (diff==fThresholds[0])
305 return;
306
307 if (fVerbose)
308 {
309 //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
310 Out() << fThresholds[0];
311 Out() << (step>0 ? " += " : " -= ");
312 Out() << step << " (" << diff << ")" << endl;
313 }
314
315 const uint32_t val[2] = { -1, diff };
316 DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
317 }
318
319 void infoHandler()
320 {
321 DimInfo *curr = getInfo(); // get current DimInfo address
322 if (!curr)
323 return;
324
325 if (curr==&fFTM)
326 {
327 fStatusFTM = GetNewState(fFTM);
328 return;
329 }
330
331 if (curr==&fDim)
332 {
333 fStatusDim = GetNewState(fDim);
334 fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
335 return;
336 }
337
338 static vector<uint8_t> counter(160);
339
340 if (curr==&fStatic)
341 {
342 if (!CheckEventSize(curr->getSize(), "infoHandler[DimStaticData]", sizeof(FTM::DimStaticData)))
343 return;
344
345 const FTM::DimStaticData &sdata = *static_cast<FTM::DimStaticData*>(curr->getData());
346 fTriggerOn = sdata.HasTrigger();
347
348 PrintThresholds(sdata);
349
350 fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
351 return;
352 }
353
354 if (curr==&fRates)
355 {
356 if (fThresholds.size()==0)
357 return;
358
359 if (!fTriggerOn && !fEnabled)
360 return;
361
362 if (!CheckEventSize(curr->getSize(), "infoHandler[DimTriggerRates]", sizeof(FTM::DimTriggerRates)))
363 return;
364
365 const FTM::DimTriggerRates &sdata = *static_cast<FTM::DimTriggerRates*>(curr->getData());
366
367 if (GetCurrentState()==kStateSettingGlobalThreshold)
368 ProcessCamera(sdata);
369
370 if (GetCurrentState()==kStateInProgress)
371 ProcessPatches(sdata);
372 }
373 }
374
375 int Calibrate()
376 {
377 if (!fTriggerOn)
378 return kStateGlobalThresholdSet;
379
380 const int32_t val[2] = { -1, fThresholdReference };
381 Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
382
383 fThresholds.assign(160, fThresholdReference);
384
385 fThresholdMin = fThresholdReference;
386 fTriggerRate = -1;
387 fEnabled = true;
388 fCounter = 0;
389
390 return kStateSettingGlobalThreshold;
391 }
392
393 int StartRC()
394 {
395 fEnabled = true;
396 return GetCurrentState();
397 }
398
399 int StopRC()
400 {
401 fEnabled = false;
402 return GetCurrentState();
403 }
404
405 int SetEnabled(const EventImp &evt)
406 {
407 if (!CheckEventSize(evt.GetSize(), "SetEnabled", 1))
408 return kSM_FatalError;
409
410 fEnabled = evt.GetBool();
411
412 return GetCurrentState();
413 }
414
415 int SetMinThreshold(const EventImp &evt)
416 {
417 if (!CheckEventSize(evt.GetSize(), "SetMinThreshold", 4))
418 return kSM_FatalError;
419
420 // FIXME: Check missing
421
422 fThresholdReference = evt.GetUShort();
423
424 return GetCurrentState();
425 }
426
427 int SetTargetRate(const EventImp &evt)
428 {
429 if (!CheckEventSize(evt.GetSize(), "SetTargetRate", 4))
430 return kSM_FatalError;
431
432 fTargetRate = evt.GetFloat();
433
434 return GetCurrentState();
435 }
436
437 void PrintState(const pair<Time,int> &state, const char *server)
438 {
439 const State rc = fNetwork.GetState(server, state.second);
440
441 Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
442 Out() << kBold << server << ": ";
443 Out() << rc.name << "[" << rc.index << "]";
444 Out() << kReset << " - " << kBlue << rc.comment << endl;
445 }
446
447 int Print()
448 {
449 Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
450 Out() << kBold << "DIM_DNS: ";
451 if (fStatusDim.second==0)
452 Out() << "Offline" << endl;
453 else
454 Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
455
456 PrintState(fStatusFTM, "FTM_CONTROL");
457
458 return GetCurrentState();
459 }
460
461 int SetVerbosity(const EventImp &evt)
462 {
463 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
464 return kSM_FatalError;
465
466 fVerbose = evt.GetBool();
467
468 return GetCurrentState();
469 }
470
471 int Execute()
472 {
473 // Dispatch (execute) at most one handler from the queue. In contrary
474 // to run_one(), it doesn't wait until a handler is available
475 // which can be dispatched, so poll_one() might return with 0
476 // handlers dispatched. The handlers are always dispatched/executed
477 // synchronously, i.e. within the call to poll_one()
478 //poll_one();
479
480 if (fStatusDim.second==0)
481 return kStateDimNetworkNA;
482
483 // All subsystems are not connected
484 if (fStatusFTM.second<FTM::kConnected)
485 return kStateDisconnected;
486
487 if (GetCurrentState()==kStateSettingGlobalThreshold)
488 {
489 if (fTriggerRate<0 || fTriggerRate>fTargetRate)
490 return kStateSettingGlobalThreshold;
491
492 return kStateGlobalThresholdSet;
493 }
494
495 if (GetCurrentState()==kStateGlobalThresholdSet)
496 {
497 if (!fTriggerOn)
498 return kStateGlobalThresholdSet;
499 //return kStateInProgress;
500 }
501
502 // At least one subsystem is not connected
503 // if (fStatusFTM.second>=FTM::kConnected)
504 return fTriggerOn && fEnabled ? kStateInProgress : kStateConnected;
505 }
506
507public:
508 StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
509 fStatusDim(make_pair(Time(), -2)),
510 fStatusFTM(make_pair(Time(), -2)),
511 fDim("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
512 fFTM("FTM_CONTROL/STATE", (void*)NULL, 0, this),
513 fRates("FTM_CONTROL/TRIGGER_RATES", (void*)NULL, 0, this),
514 fStatic("FTM_CONTROL/STATIC_DATA", (void*)NULL, 0, this)/*,
515 fDimData("RATE_SCAN/DATA", "I:1;F:1;F:1;F:1;F:40;F:160", ""),
516 fDimProc("RATE_SCAN/PROCESS_DATA", "I:1;I:1;I:1",
517 "Rate scan process data"
518 "|min[DAC]:Value at which scan was started"
519 "|max[DAC]:Value at which scan will end"
520 "|step[DAC]:Step size for scan")*/,
521 fTriggerOn(false)
522 {
523 // ba::io_service::work is a kind of keep_alive for the loop.
524 // It prevents the io_service to go to stopped state, which
525 // would prevent any consecutive calls to run()
526 // or poll() to do nothing. reset() could also revoke to the
527 // previous state but this might introduce some overhead of
528 // deletion and creation of threads and more.
529
530 // State names
531 AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
532 "The Dim DNS is not reachable.");
533
534 AddStateName(kStateDisconnected, "Disconnected",
535 "The Dim DNS is reachable, but the required subsystems are not available.");
536
537 AddStateName(kStateConnected, "Connected",
538 "All needed subsystems are connected to their hardware, no action is performed.");
539
540 AddStateName(kStateSettingGlobalThreshold, "Calibrating", "");
541 AddStateName(kStateGlobalThresholdSet, "GlobalThresholdSet", "");
542
543 AddStateName(kStateInProgress, "InProgress",
544 "Rate scan in progress.");
545
546 AddEvent("CALIBRATE")
547 (bind(&StateMachineRateControl::Calibrate, this))
548 ("");
549
550 AddEvent("START", "")
551 (bind(&StateMachineRateControl::StartRC, this))
552 ("");
553
554 AddEvent("STOP", "")
555 (bind(&StateMachineRateControl::StopRC, this))
556 ("");
557
558 AddEvent("SET_MIN_THRESHOLD", "I:1")
559 (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
560 ("");
561
562 AddEvent("SET_TARGET_RATE", "F:1")
563 (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
564 ("");
565
566 AddEvent("PRINT")
567 (bind(&StateMachineRateControl::Print, this))
568 ("");
569
570 AddEvent("SET_VERBOSE", "B")
571 (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
572 ("set verbosity state"
573 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
574
575 }
576
577 int EvalOptions(Configuration &conf)
578 {
579 fVerbose = !conf.Get<bool>("quiet");
580
581 fThresholdReference = 300;
582 fTargetRate = 70;
583
584 return -1;
585 }
586};
587
588// ------------------------------------------------------------------------
589
590#include "Main.h"
591
592template<class T>
593int RunShell(Configuration &conf)
594{
595 return Main::execute<T, StateMachineRateControl>(conf);
596}
597
598void SetupConfiguration(Configuration &conf)
599{
600 po::options_description control("Rate scan options");
601 control.add_options()
602 ("quiet,q", po_bool(), "Disable printing more informations during rate control.")
603 //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
604 // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
605 ;
606
607 conf.AddOptions(control);
608}
609
610/*
611 Extract usage clause(s) [if any] for SYNOPSIS.
612 Translators: "Usage" and "or" here are patterns (regular expressions) which
613 are used to match the usage synopsis in program output. An example from cp
614 (GNU coreutils) which contains both strings:
615 Usage: cp [OPTION]... [-T] SOURCE DEST
616 or: cp [OPTION]... SOURCE... DIRECTORY
617 or: cp [OPTION]... -t DIRECTORY SOURCE...
618 */
619void PrintUsage()
620{
621 cout <<
622 "The ratescan program is a tool for automation of rate scans.\n"
623 "\n"
624 "Usage: ratescan [-c type] [OPTIONS]\n"
625 " or: ratescan [OPTIONS]\n";
626 cout << endl;
627}
628
629void PrintHelp()
630{
631 Main::PrintHelp<StateMachineRateControl>();
632
633 /* Additional help text which is printed after the configuration
634 options goes here */
635
636 /*
637 cout << "bla bla bla" << endl << endl;
638 cout << endl;
639 cout << "Environment:" << endl;
640 cout << "environment" << endl;
641 cout << endl;
642 cout << "Examples:" << endl;
643 cout << "test exam" << endl;
644 cout << endl;
645 cout << "Files:" << endl;
646 cout << "files" << endl;
647 cout << endl;
648 */
649}
650
651int main(int argc, const char* argv[])
652{
653 Configuration conf(argv[0]);
654 conf.SetPrintUsage(PrintUsage);
655 Main::SetupConfiguration(conf);
656 SetupConfiguration(conf);
657
658 if (!conf.DoParse(argc, argv, PrintHelp))
659 return -1;
660
661 //try
662 {
663 // No console access at all
664 if (!conf.Has("console"))
665 {
666// if (conf.Get<bool>("no-dim"))
667// return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
668// else
669 return RunShell<LocalStream>(conf);
670 }
671 // Cosole access w/ and w/o Dim
672/* if (conf.Get<bool>("no-dim"))
673 {
674 if (conf.Get<int>("console")==0)
675 return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
676 else
677 return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
678 }
679 else
680*/ {
681 if (conf.Get<int>("console")==0)
682 return RunShell<LocalShell>(conf);
683 else
684 return RunShell<LocalConsole>(conf);
685 }
686 }
687 /*catch (std::exception& e)
688 {
689 cerr << "Exception: " << e.what() << endl;
690 return -1;
691 }*/
692
693 return 0;
694}
Note: See TracBrowser for help on using the repository browser.