Changeset 15037


Ignore:
Timestamp:
03/12/13 18:24:42 (12 years ago)
Author:
tbretz
Message:
Implemented reading configuration from resources for different run-types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/ratecontrol.cc

    r15035 r15037  
    3434{
    3535private:
     36    struct config
     37    {
     38        uint16_t fCalibrationType;
     39        uint16_t fTargetRate;
     40        uint16_t fMinThreshold;
     41        uint16_t fAverageTime;
     42        uint16_t fRequiredEvents;
     43    };
     44
     45    map<string, config> fRunTypes;
     46
    3647    bool fTriggerOn;
    3748
     
    5061    uint16_t fThresholdMin;
    5162    uint16_t fThresholdReference;
     63
     64    uint16_t fAverageTime;
     65    uint16_t fRequiredEvents;
    5266
    5367    deque<pair<Time,float>> fCurrents;
     
    395409        while (!fCurrents.empty())
    396410        {
    397             if (time-fCurrents.front().first<boost::posix_time::seconds(10))
     411            if (time-fCurrents.front().first<boost::posix_time::seconds(fAverageTime))
    398412                break;
    399413
     
    409423
    410424        // We want at least 8 values for averaging
    411         if (fCurrents.size()<8)
     425        if (fCurrents.size()<fRequiredEvents)
    412426            return GetCurrentState();
    413427
     
    425439        rms = sqrt(rms-avg*avg);
    426440
    427         fThresholdMin = 36.0833*pow(avg, 0.638393)+184.037;
    428         fThresholdReference = fThresholdMin;
     441        fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+184.037), fThresholdReference);
    429442        fThresholds.assign(160, fThresholdMin);
    430443
     
    488501
    489502        return RateControl::State::kSettingGlobalThreshold;
     503    }
     504
     505    int CalibrateRun(const EventImp &evt)
     506    {
     507        const string name = evt.GetText();
     508
     509        const auto it = fRunTypes.find(name);
     510        if (it==fRunTypes.end())
     511        {
     512            Error("CalibrateRun - Run-type '"+name+"' not found.");
     513            return GetCurrentState();
     514        }
     515
     516        const config &conf = it->second;
     517
     518        switch (conf.fCalibrationType)
     519        {
     520        case 0:
     521            Info("No calibration requested.");
     522            return RateControl::State::kGlobalThresholdSet;
     523
     524        case 1:
     525            fThresholdReference = conf.fMinThreshold;
     526            fTargetRate = conf.fTargetRate;
     527            return Calibrate();
     528
     529        case 2:
     530            fThresholdReference = conf.fMinThreshold;
     531            fAverageTime = conf.fAverageTime;
     532            fRequiredEvents = conf.fRequiredEvents;
     533            return CalibrateByCurrent();
     534        }
     535
     536        Error("CalibrateRun - Calibration type "+to_string(conf.fCalibrationType)+" unknown.");
     537        return GetCurrentState();
    490538    }
    491539
     
    623671            ("Set the global threshold from the median current");
    624672
     673        AddEvent("CALIBRATE_RUN", "C")
     674            (bind(&StateMachineRateControl::CalibrateRun, this, placeholders::_1))
     675            ("Start a threshold calibration as defined in the setup for this run-type");
     676
    625677        AddEvent("STOP", RateControl::State::kSettingGlobalThreshold, RateControl::State::kGlobalThresholdSet, RateControl::State::kInProgress)
    626678            (bind(&StateMachineRateControl::StopRC, this))
     
    646698    }
    647699
     700    bool CheckConfig(Configuration &conf, const string &name, const string &sub)
     701    {
     702        if (conf.HasDef(name, sub))
     703            return true;
     704
     705        Error("Neither "+name+"default nor "+name+sub+" found.");
     706        return false;
     707    }
     708
    648709    int EvalOptions(Configuration &conf)
    649710    {
     
    653714        fThresholdMin       = 300;
    654715        fTargetRate         =  75;
     716
     717        fAverageTime        =  10;
     718        fRequiredEvents     =   8;
     719
     720        // ---------- Setup run types ---------
     721        const vector<string> types = conf.Vec<string>("run-type");
     722        if (types.size()==0)
     723            Warn("No run-types defined.");
     724        else
     725            Message("Defining run-types");
     726
     727        for (auto it=types.begin(); it!=types.end(); it++)
     728        {
     729            Message(" -> "+ *it);
     730
     731            if (fRunTypes.count(*it)>0)
     732            {
     733                Error("Run-type "+*it+" defined twice.");
     734                return 1;
     735            }
     736
     737            if (!CheckConfig(conf, "calibration-type.", *it))
     738                return 2;
     739
     740            config c;
     741            c.fCalibrationType = conf.GetDef<uint16_t>("calibration-type.",  *it);
     742
     743            switch (c.fCalibrationType)
     744            {
     745            case 0:
     746                break;
     747
     748                // Calibrate by rate
     749            case 1:
     750                if (!CheckConfig(conf, "target-rate.",   *it) ||
     751                    !CheckConfig(conf, "min-threshold.", *it))
     752                    return 3;
     753                c.fTargetRate   = conf.GetDef<uint16_t>("target-rate.",    *it);
     754                c.fMinThreshold = conf.GetDef<uint16_t>("min-threshold.", *it);
     755                break;
     756
     757                // Calibrate by current
     758            case 2:
     759                if (!CheckConfig(conf, "min-threshold.",   *it) ||
     760                    !CheckConfig(conf, "average-time.",    *it) ||
     761                    !CheckConfig(conf, "required-events.", *it))
     762                    return 4;
     763                c.fMinThreshold    = conf.GetDef<uint16_t>("min-threshold.",   *it);
     764                c.fAverageTime     = conf.GetDef<uint16_t>("average-time.",    *it);
     765                c.fRequiredEvents  = conf.GetDef<uint16_t>("required-events.", *it);
     766                break;
     767            }
     768
     769            fRunTypes[*it] = c;
     770        }
    655771
    656772        return -1;
     
    678794
    679795    conf.AddOptions(control);
     796
     797    po::options_description runtype("Run type configuration");
     798    runtype.add_options()
     799        ("run-type",           vars<string>(),   "Name of run-types (replace the * in the following configuration by the case-sensitive names defined here)")
     800        ("calibration-type.*", vars<uint16_t>(), "Calibration type (0: none, 1: by rate, 2: by current)")
     801        ("target-rate.*",      vars<uint16_t>(), "Target rate for calibration by rate")
     802        ("min-threshold.*",    vars<uint16_t>(), "Minimum threshold which can be applied in a calibration")
     803        ("average-time.*",     vars<uint16_t>(), "Time in seconds to average the currents for a calibration by current.")
     804        ("required-events.*",  vars<uint16_t>(), "Number of required current events to start a calibration by current.");
     805    ;
     806
     807    conf.AddOptions(runtype);
    680808}
    681809
Note: See TracChangeset for help on using the changeset viewer.