FACT++  1.0
int StateMachineRateControl::ProcessCamera ( const FTM::DimTriggerRates sdata)
inlineprivate

Definition at line 259 of file ratecontrol.cc.

References data, FTM::DimTriggerRates::fBoardRate, FTM::DimTriggerRates::fTriggerRate, i, RateControl::State::kGlobalThresholdSet, Time::Mjd(), Dim::SendCommandNB(), DimService::setQuality(), and DimDescribedService::Update().

260  {
261  if (fCounter++==0)
262  return GetCurrentState();
263 
264  // Caluclate Median and deviation
265  vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
266 
267  sort(medb.begin(), medb.end());
268 
269  vector<float> devb(40);
270  for (int i=0; i<40; i++)
271  devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
272 
273  sort(devb.begin(), devb.end());
274 
275  double mb = (medb[19]+medb[20])/2;
276  double db = devb[27];
277 
278  // If any is zero there is something wrong
279  if (mb==0 || db==0)
280  {
281  Warn("The median or the deviation of all board rates is zero... cannot calibrate.");
282  return GetCurrentState();
283  }
284 
285  double avg = 0;
286  int num = 0;
287 
288  for (int i=0; i<40; i++)
289  {
290  if ( fabs(sdata.fBoardRate[i]-mb)<2.5*db)
291  {
292  avg += sdata.fBoardRate[i];
293  num++;
294  }
295  }
296 
297  fTriggerRate = avg/num * 40;
298 
299  if (fVerbose)
300  {
301  Out() << "Board: Median=" << mb << " Dev=" << db << endl;
302  Out() << "Camera: " << fTriggerRate << " (" << sdata.fTriggerRate << ", n=" << num << ")" << endl;
303  Out() << "Target: " << fTargetRate << endl;
304  }
305 
306  if (sdata.fTriggerRate<fTriggerRate)
307  fTriggerRate = sdata.fTriggerRate;
308 
309  // ----------------------
310 
311  /*
312  if (avg>0 && avg<fTargetRate)
313  {
314  // I am assuming here (and at other places) the the answer from the FTM when setting
315  // the new threshold always arrives faster than the next rate update.
316  fThresholdMin = fThresholds[0];
317  Out() << "Setting fThresholdMin to " << fThresholds[0] << endl;
318  }
319  */
320 
322  {
323  fThresholds.assign(160, fThresholdMin);
324 
327  fDimThreshold.Update(data);
328 
329  ostringstream out;
330  out << setprecision(3);
331  out << "Measured rate " << fTriggerRate << "Hz below target rate " << fTargetRate << "... minimum threshold set to " << fThresholdMin;
332  Info(out);
333 
334  fTriggerOn = false;
335  fPhysTriggerEnabled = false;
337  }
338 
339  // This is a step towards a threshold at which the NSB rate is equal the target rate
340  // +1 to avoid getting a step of 0
341  const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
342 
343  const uint16_t diff = fThresholdMin+int16_t(truncf(step));
344  if (diff<=fThresholdMin)
345  {
348  fDimThreshold.Update(data);
349 
350  ostringstream out;
351  out << setprecision(3);
352  out << "Next step would be 0... minimum threshold set to " << fThresholdMin;
353  Info(out);
354 
355  fTriggerOn = false;
356  fPhysTriggerEnabled = false;
358  }
359 
360  if (fVerbose)
361  {
362  //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
363  Out() << fThresholdMin;
364  Out() << (step>0 ? " += " : " -= ");
365  Out() << step << " (" << diff << ")" << endl;
366  }
367 
368  const uint32_t val[2] = { uint32_t(-1), diff };
369  Dim::SendCommandNB("FTM_CONTROL/SET_THRESHOLD", val);
370 
371  fThresholdMin = diff;
372 
373  return GetCurrentState();
374  }
int GetCurrentState() const
return the current state of the machine
int i
Definition: db_dim_client.c:21
void setQuality(int quality)
Definition: discpp.cxx:1256
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
float fBoardRate[40]
Definition: HeadersFTM.h:589
std::ostream & Out() const
Definition: MessageImp.h:73
void Mjd(double mjd)
Definition: Time.cc:145
void SendCommandNB(const std::string &command)
Definition: Dim.h:30
int Warn(const std::string &str)
Definition: MessageImp.h:48
DimDescribedService fDimThreshold
Definition: ratecontrol.cc:62
float data[4 *1440]
int Info(const std::string &str)
Definition: MessageImp.h:47
vector< uint32_t > fThresholds
Definition: ratecontrol.cc:98

+ Here is the call graph for this function: