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

Last change on this file since 12464 was 12457, checked in by tbretz, 13 years ago
A few small fixes, a more strict condition to find outliers, changed default rate to 75Hz
File size: 19.8 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)<2.5*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 if (sdata.fTriggerRate<0)
287 fTriggerRate = sdata.fTriggerRate;
288
289 // ----------------------
290
291 /*
292 if (avg>0 && avg<fTargetRate)
293 {
294 // I am assuming here (and at other places) the the answer from the FTM when setting
295 // the new threshold always arrives faster than the next rate update.
296 fThresholdMin = fThresholds[0];
297 Out() << "Setting fThresholdMin to " << fThresholds[0] << endl;
298 }
299 */
300
301 if (fTriggerRate>0 && fTriggerRate<fTargetRate)
302 {
303 fThresholds.assign(160, fThresholdMin);
304 return;
305 }
306
307 // This is a step towards a threshold at which the NSB rate is equal the target rate
308 // +1 to avoid getting a step of 0
309 const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
310
311 const uint16_t diff = fThresholdMin+int16_t(truncf(step));
312 if (diff<=fThresholdMin)
313 return;
314
315 if (fVerbose)
316 {
317 //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
318 Out() << fThresholdMin;
319 Out() << (step>0 ? " += " : " -= ");
320 Out() << step << " (" << diff << ")" << endl;
321 }
322
323 const uint32_t val[2] = { -1, diff };
324 DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
325
326 fThresholdMin = diff;
327 }
328
329 void infoHandler()
330 {
331 DimInfo *curr = getInfo(); // get current DimInfo address
332 if (!curr)
333 return;
334
335 if (curr==&fFTM)
336 {
337 fStatusFTM = GetNewState(fFTM);
338 return;
339 }
340
341 if (curr==&fDim)
342 {
343 fStatusDim = GetNewState(fDim);
344 fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
345 return;
346 }
347
348 static vector<uint8_t> counter(160);
349
350 if (curr==&fStatic)
351 {
352 if (!CheckEventSize(curr->getSize(), "infoHandler[DimStaticData]", sizeof(FTM::DimStaticData)))
353 return;
354
355 const FTM::DimStaticData &sdata = *static_cast<FTM::DimStaticData*>(curr->getData());
356 fTriggerOn = sdata.HasTrigger();
357
358 PrintThresholds(sdata);
359
360 fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
361 return;
362 }
363
364 if (curr==&fRates)
365 {
366 if (fThresholds.size()==0)
367 return;
368
369 if (!fTriggerOn && !fEnabled)
370 return;
371
372 if (!CheckEventSize(curr->getSize(), "infoHandler[DimTriggerRates]", sizeof(FTM::DimTriggerRates)))
373 return;
374
375 const FTM::DimTriggerRates &sdata = *static_cast<FTM::DimTriggerRates*>(curr->getData());
376
377 if (GetCurrentState()==kStateSettingGlobalThreshold)
378 ProcessCamera(sdata);
379
380 if (GetCurrentState()==kStateInProgress)
381 ProcessPatches(sdata);
382 }
383 }
384
385 int Calibrate()
386 {
387 if (!fTriggerOn)
388 return kStateGlobalThresholdSet;
389
390 const int32_t val[2] = { -1, fThresholdReference };
391 Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
392
393 fThresholds.assign(160, fThresholdReference);
394
395 fThresholdMin = fThresholdReference;
396 fTriggerRate = -1;
397 fEnabled = true;
398 fCounter = 0;
399
400 return kStateSettingGlobalThreshold;
401 }
402
403 int StartRC()
404 {
405 fEnabled = true;
406 return GetCurrentState();
407 }
408
409 int StopRC()
410 {
411 fEnabled = false;
412 return GetCurrentState();
413 }
414
415 int SetEnabled(const EventImp &evt)
416 {
417 if (!CheckEventSize(evt.GetSize(), "SetEnabled", 1))
418 return kSM_FatalError;
419
420 fEnabled = evt.GetBool();
421
422 return GetCurrentState();
423 }
424
425 int SetMinThreshold(const EventImp &evt)
426 {
427 if (!CheckEventSize(evt.GetSize(), "SetMinThreshold", 4))
428 return kSM_FatalError;
429
430 // FIXME: Check missing
431
432 fThresholdReference = evt.GetUShort();
433
434 return GetCurrentState();
435 }
436
437 int SetTargetRate(const EventImp &evt)
438 {
439 if (!CheckEventSize(evt.GetSize(), "SetTargetRate", 4))
440 return kSM_FatalError;
441
442 fTargetRate = evt.GetFloat();
443
444 return GetCurrentState();
445 }
446
447 void PrintState(const pair<Time,int> &state, const char *server)
448 {
449 const State rc = fNetwork.GetState(server, state.second);
450
451 Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
452 Out() << kBold << server << ": ";
453 Out() << rc.name << "[" << rc.index << "]";
454 Out() << kReset << " - " << kBlue << rc.comment << endl;
455 }
456
457 int Print()
458 {
459 Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
460 Out() << kBold << "DIM_DNS: ";
461 if (fStatusDim.second==0)
462 Out() << "Offline" << endl;
463 else
464 Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
465
466 PrintState(fStatusFTM, "FTM_CONTROL");
467
468 return GetCurrentState();
469 }
470
471 int SetVerbosity(const EventImp &evt)
472 {
473 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
474 return kSM_FatalError;
475
476 fVerbose = evt.GetBool();
477
478 return GetCurrentState();
479 }
480
481 int Execute()
482 {
483 // Dispatch (execute) at most one handler from the queue. In contrary
484 // to run_one(), it doesn't wait until a handler is available
485 // which can be dispatched, so poll_one() might return with 0
486 // handlers dispatched. The handlers are always dispatched/executed
487 // synchronously, i.e. within the call to poll_one()
488 //poll_one();
489
490 if (fStatusDim.second==0)
491 return kStateDimNetworkNA;
492
493 // All subsystems are not connected
494 if (fStatusFTM.second<FTM::kConnected)
495 return kStateDisconnected;
496
497 if (GetCurrentState()==kStateSettingGlobalThreshold)
498 {
499 if (fTriggerRate<0 || fTriggerRate>fTargetRate)
500 return kStateSettingGlobalThreshold;
501
502 return kStateGlobalThresholdSet;
503 }
504
505 if (GetCurrentState()==kStateGlobalThresholdSet)
506 {
507 if (!fTriggerOn)
508 return kStateGlobalThresholdSet;
509 //return kStateInProgress;
510 }
511
512 // At least one subsystem is not connected
513 // if (fStatusFTM.second>=FTM::kConnected)
514 return fTriggerOn && fEnabled ? kStateInProgress : kStateConnected;
515 }
516
517public:
518 StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
519 fStatusDim(make_pair(Time(), -2)),
520 fStatusFTM(make_pair(Time(), -2)),
521 fDim("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
522 fFTM("FTM_CONTROL/STATE", (void*)NULL, 0, this),
523 fRates("FTM_CONTROL/TRIGGER_RATES", (void*)NULL, 0, this),
524 fStatic("FTM_CONTROL/STATIC_DATA", (void*)NULL, 0, this)/*,
525 fDimData("RATE_SCAN/DATA", "I:1;F:1;F:1;F:1;F:40;F:160", ""),
526 fDimProc("RATE_SCAN/PROCESS_DATA", "I:1;I:1;I:1",
527 "Rate scan process data"
528 "|min[DAC]:Value at which scan was started"
529 "|max[DAC]:Value at which scan will end"
530 "|step[DAC]:Step size for scan")*/,
531 fTriggerOn(false)
532 {
533 // ba::io_service::work is a kind of keep_alive for the loop.
534 // It prevents the io_service to go to stopped state, which
535 // would prevent any consecutive calls to run()
536 // or poll() to do nothing. reset() could also revoke to the
537 // previous state but this might introduce some overhead of
538 // deletion and creation of threads and more.
539
540 // State names
541 AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
542 "The Dim DNS is not reachable.");
543
544 AddStateName(kStateDisconnected, "Disconnected",
545 "The Dim DNS is reachable, but the required subsystems are not available.");
546
547 AddStateName(kStateConnected, "Connected",
548 "All needed subsystems are connected to their hardware, no action is performed.");
549
550 AddStateName(kStateSettingGlobalThreshold, "Calibrating", "");
551 AddStateName(kStateGlobalThresholdSet, "GlobalThresholdSet", "");
552
553 AddStateName(kStateInProgress, "InProgress",
554 "Rate scan in progress.");
555
556 AddEvent("CALIBRATE")
557 (bind(&StateMachineRateControl::Calibrate, this))
558 ("");
559
560 AddEvent("START", "")
561 (bind(&StateMachineRateControl::StartRC, this))
562 ("");
563
564 AddEvent("STOP", "")
565 (bind(&StateMachineRateControl::StopRC, this))
566 ("");
567
568 AddEvent("SET_MIN_THRESHOLD", "I:1")
569 (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
570 ("");
571
572 AddEvent("SET_TARGET_RATE", "F:1")
573 (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
574 ("");
575
576 AddEvent("PRINT")
577 (bind(&StateMachineRateControl::Print, this))
578 ("");
579
580 AddEvent("SET_VERBOSE", "B")
581 (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
582 ("set verbosity state"
583 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
584
585 }
586
587 int EvalOptions(Configuration &conf)
588 {
589 fVerbose = !conf.Get<bool>("quiet");
590
591 fThresholdReference = 300;
592 fTargetRate = 75;
593
594 return -1;
595 }
596};
597
598// ------------------------------------------------------------------------
599
600#include "Main.h"
601
602template<class T>
603int RunShell(Configuration &conf)
604{
605 return Main::execute<T, StateMachineRateControl>(conf);
606}
607
608void SetupConfiguration(Configuration &conf)
609{
610 po::options_description control("Rate scan options");
611 control.add_options()
612 ("quiet,q", po_bool(), "Disable printing more informations during rate control.")
613 //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
614 // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
615 ;
616
617 conf.AddOptions(control);
618}
619
620/*
621 Extract usage clause(s) [if any] for SYNOPSIS.
622 Translators: "Usage" and "or" here are patterns (regular expressions) which
623 are used to match the usage synopsis in program output. An example from cp
624 (GNU coreutils) which contains both strings:
625 Usage: cp [OPTION]... [-T] SOURCE DEST
626 or: cp [OPTION]... SOURCE... DIRECTORY
627 or: cp [OPTION]... -t DIRECTORY SOURCE...
628 */
629void PrintUsage()
630{
631 cout <<
632 "The ratescan program is a tool for automation of rate scans.\n"
633 "\n"
634 "Usage: ratescan [-c type] [OPTIONS]\n"
635 " or: ratescan [OPTIONS]\n";
636 cout << endl;
637}
638
639void PrintHelp()
640{
641 Main::PrintHelp<StateMachineRateControl>();
642
643 /* Additional help text which is printed after the configuration
644 options goes here */
645
646 /*
647 cout << "bla bla bla" << endl << endl;
648 cout << endl;
649 cout << "Environment:" << endl;
650 cout << "environment" << endl;
651 cout << endl;
652 cout << "Examples:" << endl;
653 cout << "test exam" << endl;
654 cout << endl;
655 cout << "Files:" << endl;
656 cout << "files" << endl;
657 cout << endl;
658 */
659}
660
661int main(int argc, const char* argv[])
662{
663 Configuration conf(argv[0]);
664 conf.SetPrintUsage(PrintUsage);
665 Main::SetupConfiguration(conf);
666 SetupConfiguration(conf);
667
668 if (!conf.DoParse(argc, argv, PrintHelp))
669 return -1;
670
671 //try
672 {
673 // No console access at all
674 if (!conf.Has("console"))
675 {
676// if (conf.Get<bool>("no-dim"))
677// return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
678// else
679 return RunShell<LocalStream>(conf);
680 }
681 // Cosole access w/ and w/o Dim
682/* if (conf.Get<bool>("no-dim"))
683 {
684 if (conf.Get<int>("console")==0)
685 return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
686 else
687 return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
688 }
689 else
690*/ {
691 if (conf.Get<int>("console")==0)
692 return RunShell<LocalShell>(conf);
693 else
694 return RunShell<LocalConsole>(conf);
695 }
696 }
697 /*catch (std::exception& e)
698 {
699 cerr << "Exception: " << e.what() << endl;
700 return -1;
701 }*/
702
703 return 0;
704}
Note: See TracBrowser for help on using the repository browser.