18 namespace ba = boost::asio;
19 namespace bs = boost::system;
81 const array<uint32_t,3> v = {{ uint32_t(fThresholdMin), uint32_t(fThresholdMax), uint32_t(fThresholdStep) }};
87 if (
size_t(evt.
GetSize())==size)
94 msg << evt.
GetName() <<
" - Received event has " << evt.
GetSize() <<
" bytes, but expected " << size <<
".";
102 return GetCurrentState();
105 return GetCurrentState();
110 return GetCurrentState();
119 memset(fRateBoard, 0, 40*
sizeof(
double));
120 memset(fRatePatch, 0, 160*
sizeof(
double));
123 return GetCurrentState();
135 for (
int i=0;
i<40;
i++)
137 for (
int i=0;
i<160;
i++)
140 double reference = fRate;
141 if (fReference==kBoard)
142 reference = fRateBoard[fReferenceIdx];
143 if (fReference==kPatch)
144 reference = fRatePatch[fReferenceIdx];
150 if ((reference==0 || sqrt(reference)>fResolution*reference) && fCounter<fCounterMax)
153 out <<
"Triggers so far: " << reference;
155 out <<
" (" << sqrt(reference)/reference <<
")";
158 return GetCurrentState();
164 float data[2+3+1+40+160];
165 memcpy(data, &fStartTime, 8);
166 memcpy(data+2, &th, 4);
168 data[4] = fOnTime/
time;
169 data[5] = fRate/fCounter;
170 for (
int i=0;
i<40;
i++)
171 data[
i+6] = fRateBoard[
i]/fCounter;
172 for (
int i=0;
i<160;
i++)
173 data[
i+46] = fRatePatch[
i]/fCounter;
175 ostringstream sout1, sout2, sout3;
177 sout1 << th <<
" " << data[5];
178 for (
int i=0;
i<200;
i++)
179 sout2 <<
" " << data[
i+6];
180 sout3 <<
" " << data[3] <<
" " << data[4];
187 fDimData.
setQuality(fCommand==
"FTM_CONTROL/SET_THRESHOLD");
188 fDimData.
setData(data,
sizeof(data));
191 fThreshold += fThresholdStep;
193 if (fCounter>=fCounterMax)
195 Message(
"Rate scan stopped due to timeout.");
200 if (fThreshold>fThresholdMax)
202 Message(
"Rate scan finished.");
208 if (fCounter>1 && fThresholdStepDyn>0)
213 fThresholdStep = fCounter*fThresholdStepDyn;
219 const int32_t cmd[2] = { -1, fThreshold };
222 return GetCurrentState();
227 Out() << fDim << endl;
228 Out() << fDimFTM << endl;
230 return GetCurrentState();
239 const string fType = evt.
Ptr<
char>(12);
241 auto it = fTypes.find(fType);
242 if (it==fTypes.end())
244 Info(
"StartRateScan - Type '"+fType+
"' not found... trying 'default'.");
246 it = fTypes.find(
"default");
247 if (it==fTypes.end())
249 Error(
"StartRateScan - Type 'default' not found.");
250 return GetCurrentState();
254 fCounterMax = it->second.fCounterMax;
255 fResolution = it->second.fResolution;
257 fCommand =
"FTM_CONTROL/"+command;
259 const int32_t step = evt.
Get<int32_t>(8);
261 fThresholdMin = evt.
Get<uint32_t>();
262 fThresholdMax = evt.
Get<uint32_t>(4);
263 fThresholdStep = abs(step);
265 fThresholdStepDyn = step<0 ? -step : 0;
272 Message(
"Configuration for ratescan started.");
281 return GetCurrentState();
284 return GetCurrentState();
286 const int32_t
data[2] = { -1, fThresholdMin };
291 fThreshold = fThresholdMin;
313 msg <<
"Rate scan " << now <<
"(" << fStartTime <<
") from " << fThresholdMin <<
" to ";
314 msg << fThresholdMax <<
" in steps of " << fThresholdStep;
315 msg <<
" with a resolution of " << fResolution ;
316 msg <<
" and max-wait " << fCounterMax ;
331 return GetCurrentState();
334 Message(
"Rate scan manually stopped.");
341 fReference = kCamera;
343 return GetCurrentState();
348 if (!CheckEventSize(evt, 4))
349 return kSM_FatalError;
353 Error(
"SetReferenceBoard - Board index out of range [0;39]");
354 return GetCurrentState();
360 return GetCurrentState();
365 if (!CheckEventSize(evt, 4))
366 return kSM_FatalError;
370 Error(
"SetReferencePatch - Patch index out of range [0;159]");
371 return GetCurrentState();
377 return GetCurrentState();
382 if (!CheckEventSize(evt, 4))
383 return kSM_FatalError;
385 fThresholdStep = evt.
Get<uint32_t>();
388 msg <<
"New step size " << fThresholdStep;
393 return GetCurrentState();
398 if (!CheckEventSize(evt, 4))
399 return kSM_FatalError;
401 fThresholdMax = evt.
Get<uint32_t>();
403 return GetCurrentState();
409 return GetCurrentState();
435 return GetCurrentState();
440 fDimFTM(
"FTM_CONTROL"),
441 fDimData(
"RATE_SCAN/DATA",
"X:1;I:1;F:1;F:1;F:1;F:40;F:160",
442 "|Id[s]:Start time used to identify measurement (UnixTime)" 443 "|Threshold[dac]:Threshold in DAC counts" 444 "|ElapsedTime[s]:Real elapsed time" 445 "|RelOnTime[ratio]:Relative on time" 446 "|TriggerRate[Hz]:Camera trigger rate" 447 "|BoardRate[Hz]:Board trigger rates" 448 "|PatchRate[Hz]:Patch trigger rates"),
449 fDimProc(
"RATE_SCAN/PROCESS_DATA",
"I:1;I:1;I:1",
450 "Rate scan process data" 451 "|min[DAC]:Value at which scan was started" 452 "|max[DAC]:Value at which scan will end" 453 "|step[DAC]:Step size for scan"),
454 fAutoPause(false), fThreshold(-1), fReference(kCamera), fReferenceIdx(0)
467 Subscribe(
"FTM_CONTROL/TRIGGER_RATES")
472 "The Dim DNS is not reachable.");
475 "The Dim DNS is reachable, but the required subsystems are not available.");
478 "All needed subsystems are connected to their hardware, no action is performed.");
481 "Waiting for FTM to get 'Configured'.");
484 "Rate scan in progress.");
487 "Rate scan in progress but paused.");
491 (
"Start rate scan for the threshold in the defined range" 492 "|min[int]:Start value in DAC counts" 493 "|max[int]:Limiting value in DAC counts" 494 "|step[int]:Single step in DAC counts" 495 "|type[text]:Ratescan type");
499 (
"Start rate scan for N-out-of-4 in the defined range" 500 "|min[int]:Start value in DAC counts" 501 "|max[int]:Limiting value in DAC counts" 502 "|step[int]:Single step in DAC counts");
506 (
"Change the step size during a ratescan in progress" 507 "|step[int]:Single step in DAC counts");
511 (
"Change the maximum limit during a ratescan in progress" 512 "|max[int]:Limiting value in DAC counts");
516 (
"Stop a ratescan in progress");
520 (
"Use the camera trigger rate as reference for the reolution");
523 (
"Use the given board trigger-rate as reference for the reolution" 524 "|board[idx]:Index of the board (4*crate+board)");
527 (
"Use the given patch trigger-rate as reference for the reolution" 528 "|patch[idx]:Index of the patch (360*crate+36*board+patch)");
532 (
"Enable an automatic pause for the next ratescan, after it got configured.");
536 (
"Pause a ratescan in progress");
539 (
"Resume a paused ratescan");
549 const vector<string> types = conf.
Vec<
string>(
"type");
551 Warn(
"No types defined.");
553 Message(
"Defining types");
555 for (
auto it=types.begin(); it!=types.end(); it++)
557 Message(
" -> "+ *it);
559 if (fTypes.count(*it)>0)
561 Error(
"Type "+*it+
" defined twice.");
566 if (conf.
HasDef(
"max-wait.", *it))
570 Error(
"Neither max-wait.default nor max-wait."+*it+
" found.");
573 if (conf.
HasDef(
"resolution.", *it))
577 Error(
"Neither resolution.default nor resolution."+*it+
" found.");
592 return Main::execute<T, StateMachineRateScan>(conf);
597 po::options_description
type(
"Ratescan type configuration");
599 (
"type", vars<string>(),
"Name of ratescan types (replace the * in the following configuration by the case-sensitive names defined here)")
600 (
"max-wait.*", var<int>(),
"The maximum number of seconds to wait to get the anticipated resolution for a point.")
601 (
"resolution.*", var<double>() ,
"The minimum resolution required for a single data point.")
619 "The ratescan program is a tool for automation of rate scans.\n" 621 "Usage: ratescan [-c type] [OPTIONS]\n" 622 " or: ratescan [OPTIONS]\n";
628 Main::PrintHelp<StateMachineRateScan>();
648 int main(
int argc,
const char* argv[])
661 if (!conf.
Has(
"console"))
666 return RunShell<LocalStream>(conf);
678 if (conf.
Get<
int>(
"console")==0)
679 return RunShell<LocalShell>(conf);
681 return RunShell<LocalConsole>(conf);
void SetupConfiguration(Configuration &conf)
virtual void Subscribe(StateMachineImp &imp)
A general base-class describing events issues in a state machine.
void SetupConfiguration(Configuration &conf)
void setQuality(int quality)
Adds some functionality to boost::posix_time::ptime for our needs.
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
int SetReferenceBoard(const EventImp &evt)
int EvalOptions(Configuration &conf)
const int32_t & state() const
int HandleTriggerRates(const EventImp &evt)
std::vector< T > Vec(const std::string &var)
DimDescribedService fDimData
int Execute()
Is called continously to execute actions in the current state.
map< string, config > fTypes
int SetReferencePatch(const EventImp &evt)
bool Has(const std::string &var)
T GetDef(const std::string &var, const S &val)
void AddOptions(const po::options_description &opt, bool visible=true)
int HandleFtmStateChange()
int ChangeStepSize(const EventImp &evt)
void setData(const void *ptr, size_t sz)
void SendCommandNB(const std::string &command)
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Commandline parsing, resource file parsing and database access.
int ChangeMaximum(const EventImp &evt)
virtual void Subscribe(StateMachineImp &imp)
int StartRateScan(const EventImp &evt, const string &command)
Class for a state machine implementation within a DIM network.
virtual std::string GetName() const
int main(int argc, const char *argv[])
int RunShell(Configuration &conf)
DimDescribedService fDimProc
T Get(size_t offset=0) const
bool HasDef(const std::string &var, const T &val)
bool CheckEventSize(const EventImp &evt, size_t size)
virtual const void * GetData() const
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
void SetCallback(const callback &cb)
const T * Ptr(size_t offset=0) const
DimDescribedState fDimFTM
StateMachineRateScan(ostream &out=cout)
virtual size_t GetSize() const