Changeset 15069 for trunk/FACT++


Ignore:
Timestamp:
03/14/13 10:05:21 (12 years ago)
Author:
tbretz
Message:
Added reading of pixel map and setting of initial thresholds for hot patches; implemented reading the configuration from the resources.
File:
1 edited

Legend:

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

    r15037 r15069  
    88#include "Configuration.h"
    99#include "Console.h"
     10#include "PixelMap.h"
    1011
    1112#include "tools.h"
     
    4546    map<string, config> fRunTypes;
    4647
     48    PixelMap fMap;
     49
    4750    bool fTriggerOn;
    4851
     
    6568    uint16_t fRequiredEvents;
    6669
    67     deque<pair<Time,float>> fCurrents;
     70    deque<pair<Time,float>> fCurrentsMed;
     71    deque<pair<Time,float>> fCurrentsDev;
     72    deque<pair<Time,vector<float>>> fCurrentsVec;
    6873
    6974    bool fVerbose;
     
    404409        const Time &time = evt.GetTime();
    405410        const float med  = evt.Get<float>(416*4+4+4);
     411        const float dev  = evt.Get<float>(416*4+4+4+4);
     412        const float *cur = evt.Ptr<float>();
    406413
    407414        // Keep all median currents of the past 10 seconds
    408         fCurrents.push_back(make_pair(time, med));
    409         while (!fCurrents.empty())
    410         {
    411             if (time-fCurrents.front().first<boost::posix_time::seconds(fAverageTime))
     415        fCurrentsMed.push_back(make_pair(time, med));
     416        fCurrentsDev.push_back(make_pair(time, dev));
     417        fCurrentsVec.push_back(make_pair(time, vector<float>(cur, cur+320)));
     418        while (!fCurrentsMed.empty())
     419        {
     420            if (time-fCurrentsMed.front().first<boost::posix_time::seconds(fAverageTime))
    412421                break;
    413422
    414             fCurrents.pop_front();
     423            fCurrentsMed.pop_front();
     424            fCurrentsDev.pop_front();
     425            fCurrentsVec.pop_front();
    415426        }
    416427
     
    423434
    424435        // We want at least 8 values for averaging
    425         if (fCurrents.size()<fRequiredEvents)
     436        if (fCurrentsMed.size()<fRequiredEvents)
    426437            return GetCurrentState();
    427438
     
    429440        double avg = 0;
    430441        double rms = 0;
    431         for (auto it=fCurrents.begin(); it!=fCurrents.end(); it++)
     442        for (auto it=fCurrentsMed.begin(); it!=fCurrentsMed.end(); it++)
    432443        {
    433444            avg += it->second;
    434445            rms += it->second*it->second;
    435446        }
    436         avg /= fCurrents.size();
    437         rms /= fCurrents.size();
    438 
     447        avg /= fCurrentsMed.size();
     448        rms /= fCurrentsMed.size();
    439449        rms = sqrt(rms-avg*avg);
    440450
    441         fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+184.037), fThresholdReference);
     451        double avg_dev = 0;
     452        for (auto it=fCurrentsDev.begin(); it!=fCurrentsDev.end(); it++)
     453            avg_dev += it->second;
     454        avg_dev /= fCurrentsMed.size();
     455
     456        // One could recalculate the median of all pixels incluing the
     457        // correction for the three crazy pixels, but that is three out
     458        // of 320. The effect on the median should be negligible anyhow.
     459        vector<double> vec(160);
     460        for (auto it=fCurrentsVec.begin(); it!=fCurrentsVec.end(); it++)
     461            for (int i=0; i<320; i++)
     462            {
     463                const PixelMapEntry &hv = fMap.hv(i);
     464                if (!hv)
     465                    continue;
     466
     467                // The current is proportional to the rate. To calculate
     468                // a measure for the rate, the average current per pixel
     469                // is caluclated for the trigger patch.
     470                int weight = hv.group() ? 5 : 4;
     471
     472                // Use only the current in the pixels with the correct
     473                // resistor as a reference, ignore the crazy ones.
     474                // Effects of these should be corrected by the
     475                // rate control later, not the initial setup.
     476                if (i==66)           
     477                    weight = 4./(3+10);
     478                if (i==191 || i==193)
     479                    weight = 5./(4+10);
     480
     481                vec[hv.hw()/9] += it->second[i] * weight;
     482            }
     483
     484        //fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+184.037), fThresholdReference);
     485        fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+210), fThresholdReference);
    442486        fThresholds.assign(160, fThresholdMin);
     487
     488        const int32_t val[2] = { -1, fThresholdMin };
     489        Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
     490
     491        double avg2 = 0;
     492        for (int i=0; i<160; i++)
     493        {
     494            vec[i] /= fCurrentsVec.size()*9;
     495            avg2 += vec[i];
     496
     497            if (vec[i]-avg>6*avg_dev)
     498            {
     499                fThresholds[i] = max(uint16_t(36.0833*pow(vec[i], 0.638393)+185), fThresholdReference);
     500
     501                cout << "i=" << i << " " << avg << " " << avg_dev << " " << vec[i] << " " << fThresholds[i] << endl;
     502
     503                const int32_t dat[2] = { i, fThresholds[i] };
     504                Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", dat);
     505
     506                fBlock[i/4] = true;
     507            }
     508        }
     509
     510        avg2 /= 160;
     511        cout << "AVG2="<<avg2 << endl;
    443512
    444513        const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
     
    448517        ostringstream out;
    449518        out << setprecision(3);
    450         out << "Measured average current " << avg << "uA +- " << rms << "uA [N=" << fCurrents.size() << "]... mininum threshold set to " << fThresholdMin;
     519        out << "Measured average current " << avg << "uA +- " << rms << "uA [N=" << fCurrentsMed.size() << "]... mininum threshold set to " << fThresholdMin;
    451520        Info(out);
    452521
     
    470539        fTriggerRate  = -1;
    471540        fCounter      = 0;
     541        fBlock.assign(160, false);
    472542
    473543        fCalibrateByCurrent = false;
     
    495565        fCalibrateByCurrent = true;
    496566        fCalibrationTimeStart = Time();
     567        fBlock.assign(160, false);
    497568
    498569        ostringstream out;
    499         out << "Rate calibration by current " << fThresholdReference << " with a target rate of " << fTargetRate << " Hz";
     570        out << "Rate calibration by current " << fThresholdReference << ".";
    500571        Info(out);
    501572
     
    507578        const string name = evt.GetText();
    508579
    509         const auto it = fRunTypes.find(name);
     580        auto it = fRunTypes.find(name);
    510581        if (it==fRunTypes.end())
    511582        {
    512             Error("CalibrateRun - Run-type '"+name+"' not found.");
    513             return GetCurrentState();
     583            Info("CalibrateRun - Run-type '"+name+"' not found... trying 'default'.");
     584
     585            it = fRunTypes.find("default");
     586            if (it==fRunTypes.end())
     587            {
     588                Error("CalibrateRun - Run-type 'default' not found.");
     589                return GetCurrentState();
     590            }
    514591        }
    515592
     
    698775    }
    699776
    700     bool CheckConfig(Configuration &conf, const string &name, const string &sub)
     777    bool GetConfig(Configuration &conf, const string &name, const string &sub, uint16_t &rc)
    701778    {
    702779        if (conf.HasDef(name, sub))
     780        {
     781            rc = conf.GetDef<uint16_t>(name, sub);
    703782            return true;
     783        }
    704784
    705785        Error("Neither "+name+"default nor "+name+sub+" found.");
     
    710790    {
    711791        fVerbose = !conf.Get<bool>("quiet");
     792
     793        if (!fMap.Read(conf.Get<string>("pixel-map-file")))
     794        {
     795            Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
     796            return 1;
     797        }
    712798
    713799        fThresholdReference = 300;
     
    735821            }
    736822
    737             if (!CheckConfig(conf, "calibration-type.", *it))
     823            config &c = fRunTypes[*it];
     824            if (!GetConfig(conf, "calibration-type.", *it, c.fCalibrationType) ||
     825                !GetConfig(conf, "target-rate.",      *it, c.fTargetRate)      ||
     826                !GetConfig(conf, "min-threshold.",    *it, c.fMinThreshold)    ||
     827                !GetConfig(conf, "average-time.",     *it, c.fAverageTime)     ||
     828                !GetConfig(conf, "required-events.",  *it, c.fRequiredEvents))
    738829                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;
    770830        }
    771831
     
    788848    po::options_description control("Rate control options");
    789849    control.add_options()
    790         ("quiet,q",       po_bool(),  "Disable printing more informations during rate control.")
     850        ("quiet,q", po_bool(),  "Disable printing more informations during rate control.")
     851        ("pixel-map-file", var<string>("FACTmapV5a.txt"), "Pixel mapping file. Used here to get the default reference voltage.")
    791852       //("max-wait",   var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
    792853       // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
     
    797858    po::options_description runtype("Run type configuration");
    798859    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.");
     860        ("run-type",           vars<string>(),  "Name of run-types (replace the * in the following configuration by the case-sensitive names defined here)")
     861        ("calibration-type.*", var<uint16_t>(), "Calibration type (0: none, 1: by rate, 2: by current)")
     862        ("target-rate.*",      var<uint16_t>(), "Target rate for calibration by rate")
     863        ("min-threshold.*",    var<uint16_t>(), "Minimum threshold which can be applied in a calibration")
     864        ("average-time.*",     var<uint16_t>(), "Time in seconds to average the currents for a calibration by current.")
     865        ("required-events.*",  var<uint16_t>(), "Number of required current events to start a calibration by current.");
    805866    ;
    806867
Note: See TracChangeset for help on using the changeset viewer.