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

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