FACT++  1.0
int StateMachineRateControl::HandleCalibratedCurrents ( const EventImp evt)
inlineprivate

Definition at line 429 of file ratecontrol.cc.

References PixelMapEntry::count(), counter, data, dev, EventImp::Get(), EventImp::GetTime(), PixelMap::hv(), PixelMapEntry::hw(), i, Lid::State::kClosed, RateControl::State::kSettingGlobalThreshold, Drive::State::kTracking, Time::Mjd(), EventImp::Ptr(), Dim::SendCommandNB(), DimService::setQuality(), DimState::state(), time, and DimDescribedService::Update().

Referenced by StateMachineRateControl().

430  {
431  // Check if received event is valid
432  if (!CheckEventSize(evt, (2*416+8)*4))
433  return GetCurrentState();
434 
435  // Record only currents when the drive is tracking to avoid
436  // bias from the movement
438  return GetCurrentState();
439 
440  // Get time and median current (FIXME: check N?)
441  const Time &time = evt.GetTime();
442  const float med = evt.Get<float>(416*4+4+4);
443  const float dev = evt.Get<float>(416*4+4+4+4);
444  const float *cur = evt.Ptr<float>();
445 
446  // Keep all median currents of the past 10 seconds
447  fCurrentsMed.emplace_back(time, med);
448  fCurrentsDev.emplace_back(time, dev);
449  fCurrentsVec.emplace_back(time, vector<float>(cur, cur+320));
450  while (!fCurrentsMed.empty())
451  {
452  if (time-fCurrentsMed.front().first<boost::posix_time::seconds(fAverageTime))
453  break;
454 
455  fCurrentsMed.pop_front();
456  fCurrentsDev.pop_front();
457  fCurrentsVec.pop_front();
458  }
459 
460  // If we are not doing a calibration no further action necessary
461  if (!fCalibrateByCurrent)
462  return GetCurrentState();
463 
464  // We are not setting thresholds at all
466  return GetCurrentState();
467 
468  // Target thresholds have been assigned already
469  if (!fThresholds.empty())
470  return GetCurrentState();
471 
472  // We want at least 8 values for averaging
473  if (fCurrentsMed.size()<fRequiredEvents)
474  return GetCurrentState();
475 
476  // Calculate avera and rms of median
477  double avg = 0;
478  double rms = 0;
479  for (auto it=fCurrentsMed.begin(); it!=fCurrentsMed.end(); it++)
480  {
481  avg += it->second;
482  rms += it->second*it->second;
483  }
484  avg /= fCurrentsMed.size();
485  rms /= fCurrentsMed.size();
486  rms -= avg*avg;
487  rms = rms<0 ? 0 : sqrt(rms);
488 
489  double avg_dev = 0;
490  for (auto it=fCurrentsDev.begin(); it!=fCurrentsDev.end(); it++)
491  avg_dev += it->second;
492  avg_dev /= fCurrentsMed.size();
493 
494  // One could recalculate the median of all pixels including the
495  // correction for the three crazy pixels, but that is three out
496  // of 320. The effect on the median should be negligible anyhow.
497  vector<double> vec(160);
498  for (auto it=fCurrentsVec.begin(); it!=fCurrentsVec.end(); it++)
499  for (int i=0; i<320; i++)
500  {
501  const PixelMapEntry &hv = fMap.hv(i);
502  if (hv)
503  vec[hv.hw()/9] += it->second[i]*hv.count();
504  }
505 
506  //fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+184.037), fThresholdReference);
507  //fThresholdMin = max(uint16_t(42.4*pow(avg, 0.642)+182), fThresholdReference);
508  //fThresholdMin = max(uint16_t(41.6*pow(avg+1, 0.642)+175), fThresholdReference);
509  //fThresholdMin = max(uint16_t(42.3*pow(avg, 0.655)+190), fThresholdReference);
510  //fThresholdMin = max(uint16_t(46.6*pow(avg, 0.627)+187), fThresholdReference);
511  fThresholdMin = max(uint16_t(156.3*pow(avg, 0.3925)+1), fThresholdReference);
512  //fThresholdMin = max(uint16_t(41.6*pow(avg, 0.642)+175), fThresholdReference);
513  fThresholds.assign(160, fThresholdMin);
514 
515  int counter = 1;
516 
517  double avg2 = 0;
518  for (int i=0; i<160; i++)
519  {
520  vec[i] /= fCurrentsVec.size()*9;
521 
522  avg2 += vec[i];
523 
524  if (vec[i]>avg+3.5*avg_dev)
525  {
526  fThresholds[i] = max(uint16_t(40.5*pow(vec[i], 0.642)+164), fThresholdMin);
527 
528  counter++;
529  }
530  }
531  avg2 /= 160;
532 
533 
534  Dim::SendCommandNB("FTM_CONTROL/SET_ALL_THRESHOLDS", fThresholds);
535 
536 
539  fDimThreshold.Update(data);
540 
541  //Info("Sent a total of "+to_string(counter)+" commands for threshold setting");
542 
543  ostringstream out;
544  out << setprecision(3);
545  out << "Measured average current " << avg << "uA +- " << rms << "uA [N=" << fCurrentsMed.size() << "]... minimum threshold set to " << fThresholdMin;
546  Info(out);
547  Info("Set "+to_string(counter)+" individual thresholds.");
548 
549  fTriggerOn = false;
550  fPhysTriggerEnabled = false;
551 
553  }
list< pair< Time, float > > fCurrentsDev
Definition: ratecontrol.cc:74
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
list< pair< Time, float > > fCurrentsMed
Definition: ratecontrol.cc:73
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
const int32_t & state() const
Definition: DimState.h:80
int count() const
Definition: PixelMap.h:41
void Mjd(double mjd)
Definition: Time.cc:145
virtual Time GetTime() const
Definition: EventImp.h:57
DimDescribedState fDimDrive
Definition: ratecontrol.cc:60
list< pair< Time, vector< float > > > fCurrentsVec
Definition: ratecontrol.cc:75
void SendCommandNB(const std::string &command)
Definition: Dim.h:30
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
DimDescribedService fDimThreshold
Definition: ratecontrol.cc:62
float data[4 *1440]
int counter
Definition: db_dim_client.c:19
DimDescribedState fDimLid
Definition: ratecontrol.cc:59
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 dev
Definition: feeserver.c:5
int Info(const std::string &str)
Definition: MessageImp.h:47
int hw() const
Definition: PixelMap.h:39
T Get(size_t offset=0) const
Definition: EventImp.h:66
const T * Ptr(size_t offset=0) const
Definition: EventImp.h:74
vector< uint32_t > fThresholds
Definition: ratecontrol.cc:98
bool CheckEventSize(const EventImp &evt, size_t size)
Definition: ratecontrol.cc:84
const PixelMapEntry & hv(int board, int channel) const
Definition: PixelMap.h:139

+ Here is the call graph for this function:

+ Here is the caller graph for this function: