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

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