Changeset 12429 for trunk/FACT++


Ignore:
Timestamp:
11/06/11 18:10:22 (13 years ago)
Author:
tbretz
Message:
Added many more options and the possibility to control the global camera rate.
File:
1 edited

Legend:

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

    r12401 r12429  
    3939        kStateConnecting,
    4040        kStateConnected,
     41
     42        kStateWaitingForReference,
     43        kStateSettingGlobalThreshold,
     44        kStateGlobalThresholdSet,
     45
    4146        kStateInProgress,
    4247    };
     
    5560//    DimDescribedService fDimProc;
    5661
     62    float  fTargetRate;
     63    float  fTriggerRate;
     64
     65    uint16_t fThresholdMin;
     66
    5767    bool fTriggerOn;
     68    bool fVerbose;
     69    bool fEnabled;
    5870
    5971    pair<Time, int> GetNewState(DimStampedInfo &info) const
     
    93105            return;
    94106
     107        Out() << "Min. DAC=" << fThresholdMin << endl;
     108
    95109        int t=0;
    96110        for (t=0; t<160; t++)
     
    106120            {
    107121                for (int i=0; i<4; i++)
    108                     if (fThresholds[i+k*4+j*16]!=300)
     122                    if (fThresholds[i+k*4+j*16]!=fThresholdMin)
    109123                        Out() << setw(3) << fThresholds[i+k*4+j*16] << " ";
    110124                    else
     
    120134    {
    121135        uint16_t diff = fThresholds[idx]+int16_t(truncf(step));
    122         if (diff<300)
    123             diff=300;
     136        if (diff<fThresholdMin)
     137            diff=fThresholdMin;
    124138
    125139        if (diff==fThresholds[idx])
    126140            return;
    127141
    128         if (1/*fVerbose*/)
     142        if (fVerbose)
    129143        {
    130144            Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
     
    137151    }
    138152
     153    void ProcessPatches(const FTM::DimTriggerRates &sdata)
     154    {
     155
     156        // Caluclate Median and deviation
     157        vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
     158        vector<float> medp(sdata.fPatchRate, sdata.fPatchRate+160);
     159
     160        sort(medb.begin(), medb.end());
     161        sort(medp.begin(), medp.end());
     162
     163        vector<float> devb(40);
     164        for (int i=0; i<40; i++)
     165            devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
     166
     167        vector<float> devp(160);
     168        for (int i=0; i<160; i++)
     169            devp[i] = fabs(sdata.fPatchRate[i]-medp[i]);
     170
     171        sort(devb.begin(), devb.end());
     172        sort(devp.begin(), devp.end());
     173
     174        double mb = (medb[19]+medb[20])/2;
     175        double mp = (medp[79]+medp[80])/2;
     176
     177        double db = devb[27];
     178        double dp = devp[109];
     179
     180        // If any is zero there is something wrong
     181        if (mb==0 || mp==0 || db==0 || dp==0)
     182            return;
     183
     184        if (fVerbose)
     185        {
     186            Out() << "Patch: Median=" << mp << " Dev=" << dp << endl;
     187            Out() << "Board: Median=" << mb << " Dev=" << db << endl;
     188        }
     189
     190        for (int i=0; i<40; i++)
     191        {
     192            int maxi = -1;
     193
     194            const float dif = fabs(sdata.fBoardRate[i]-mb)/db;
     195            if (dif>5)
     196            {
     197                if (fVerbose)
     198                    Out() << "B" << i << ": " << dif << endl;
     199
     200                float max = sdata.fPatchRate[i*4];
     201                maxi = 0;
     202
     203                for (int j=1; j<4; j++)
     204                    if (sdata.fPatchRate[i*4+j]>max)
     205                    {
     206                        max = sdata.fPatchRate[i*4+j];
     207                        maxi = j;
     208                    }
     209            }
     210
     211            for (int j=0; j<4; j++)
     212            {
     213                // For the noise pixel correct down to median+3*deviation
     214                if (maxi==j)
     215                {
     216                    const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+5*dp))/0.039;
     217                    //  * (dif-5)/dif
     218                    Step(i*4+j, step);
     219                    continue;
     220                }
     221
     222                // For pixels below the meadian correct also back to median+3*deviation
     223                if (sdata.fPatchRate[i*4+j]<mp)
     224                {
     225                    const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3*dp))/0.039;
     226                    Step(i*4+j, step);
     227                    continue;
     228                }
     229
     230                const float step =  -1.5*(log10(mp+dp)-log10(mp))/0.039;
     231                Step(i*4+j, step);
     232            }
     233        }
     234    }
     235
     236    void ProcessCamera(const FTM::DimTriggerRates &sdata)
     237    {
     238        // Caluclate Median and deviation
     239        vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
     240
     241        sort(medb.begin(), medb.end());
     242
     243        vector<float> devb(40);
     244        for (int i=0; i<40; i++)
     245            devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
     246
     247        sort(devb.begin(), devb.end());
     248
     249        double mb = (medb[19]+medb[20])/2;
     250        double db = devb[27];
     251
     252        // If any is zero there is something wrong
     253        if (mb==0 || db==0)
     254            return;
     255
     256        double avg = 0;
     257        int    num = 0;
     258
     259        for (int i=0; i<40; i++)
     260        {
     261            if ( fabs(sdata.fBoardRate[i]-mb)<5*db)
     262            {
     263                avg += sdata.fBoardRate[i];
     264                num++;
     265            }
     266        }
     267
     268        fTriggerRate = avg / num * 40;
     269
     270        if (fVerbose)
     271        {
     272            Out() << "Board:  Median=" << mb << " Dev=" << db << endl;
     273            Out() << "Camera: " << fTriggerRate << " (" << sdata.fTriggerRate << ", n=" << num << ")" << endl;
     274        }
     275
     276        // ----------------------
     277
     278
     279        if (fTriggerRate<fTargetRate)
     280            return;
     281
     282        // Is this a step to 70Hz?
     283        const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039;
     284
     285        const uint16_t diff = fThresholds[0]+int16_t(truncf(step));
     286
     287        if (diff==fThresholds[0])
     288            return;
     289
     290        if (fVerbose)
     291        {
     292            //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
     293            Out() << fThresholds[0];
     294            Out() << (step>0 ? " += " : " -= ");
     295            Out() << step << " (" << diff << ")" << endl;
     296        }
     297
     298        const uint32_t val[2] = { -1,  diff };
     299        DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
     300    }
     301
    139302    void infoHandler()
    140303    {
     
    187350            const FTM::DimTriggerRates &sdata = *static_cast<FTM::DimTriggerRates*>(curr->getData());
    188351
    189             // Caluclate Median and deviation
    190             vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
    191             vector<float> medp(sdata.fPatchRate, sdata.fPatchRate+160);
    192 
    193             sort(medb.begin(), medb.end());
    194             sort(medp.begin(), medp.end());
    195 
    196             vector<float> devb(40);
    197             for (int i=0; i<40; i++)
    198                 devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
    199 
    200             vector<float> devp(160);
    201             for (int i=0; i<160; i++)
    202                 devp[i] = fabs(sdata.fPatchRate[i]-medp[i]);
    203 
    204             sort(devb.begin(), devb.end());
    205             sort(devp.begin(), devp.end());
    206 
    207             double mb = (medb[19]+medb[20])/2;
    208             double mp = (medp[79]+medp[80])/2;
    209 
    210             double db = devb[27];
    211             double dp = devp[109];
    212 
    213             // If any is zero there is something wrong
    214             if (mb==0 || mp==0 || db==0 || dp==0)
    215                 return;
    216 
    217             if (1/*fVerbose*/)
    218             {
    219                 Out() << "Patch: Median=" << mp << " Dev=" << dp << endl;
    220                 Out() << "Board: Median=" << mb << " Dev=" << db << endl;
    221             }
    222 
    223             for (int i=0; i<40; i++)
    224             {
    225                 int maxi = -1;
    226 
    227                 const float dif = fabs(sdata.fBoardRate[i]-mb)/db;
    228                 if (dif>5)
    229                 {
    230                     if (1/*fVerbose*/)
    231                         Out() << "B" << i << ": " << dif << endl;
    232 
    233                     float max = sdata.fPatchRate[i*4];
    234                     maxi = 0;
    235 
    236                     for (int j=1; j<4; j++)
    237                         if (sdata.fPatchRate[i*4+j]>max)
    238                         {
    239                             max = sdata.fPatchRate[i*4+j];
    240                             maxi = j;
    241                         }
    242                 }
    243 
    244                 for (int j=0; j<4; j++)
    245                 {
    246                     // For the noise pixel correct down to median+3*deviation
    247                     if (maxi==j)
    248                     {
    249                         const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+5*dp))/0.039;
    250                         //  * (dif-5)/dif
    251                         Step(i*4+j, step);
    252                         continue;
    253                     }
    254 
    255                     // For pixels below the meadian correct also back to median+3*deviation
    256                     if (sdata.fPatchRate[i*4+j]<mp)
    257                     {
    258                         const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3*dp))/0.039;
    259                         Step(i*4+j, step);
    260                         continue;
    261                     }
    262 
    263                     const float step =  -1.5*(log10(mp+dp)-log10(mp))/0.039;
    264                     Step(i*4+j, step);
    265                 }
    266             }
    267         }
     352            if (GetCurrentState()==kStateSettingGlobalThreshold)
     353                ProcessCamera(sdata);
     354
     355            if (GetCurrentState()==kStateInProgress)
     356                ProcessPatches(sdata);
     357        }
     358    }
     359
     360    int StartDataTaking()
     361    {
     362        if (!fEnabled)
     363            return kStateGlobalThresholdSet;
     364
     365        fThresholds.resize(0);
     366
     367        const int32_t val[2] = { -1, fThresholdMin };
     368        Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
     369
     370        return kStateWaitingForReference;
     371    }
     372
     373    int SetEnabled(const EventImp &evt)
     374    {
     375        if (!CheckEventSize(evt.GetSize(), "SetEnabled", 1))
     376            return kSM_FatalError;
     377
     378        fEnabled = evt.GetBool();
     379
     380        return GetCurrentState();
     381    }
     382
     383    int SetMinThreshold(const EventImp &evt)
     384    {
     385        if (!CheckEventSize(evt.GetSize(), "SetMinThreshold", 4))
     386            return kSM_FatalError;
     387
     388        // FIXME: Check missing
     389
     390        fThresholdMin = evt.GetUShort();
     391
     392        return GetCurrentState();
     393    }
     394
     395    int SetTargetRate(const EventImp &evt)
     396    {
     397        if (!CheckEventSize(evt.GetSize(), "SetTargetRate", 4))
     398            return kSM_FatalError;
     399
     400        fTargetRate = evt.GetFloat();
     401
     402        return GetCurrentState();
    268403    }
    269404
     
    292427    }
    293428
     429    int SetVerbosity(const EventImp &evt)
     430    {
     431        if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
     432            return kSM_FatalError;
     433
     434        fVerbose = evt.GetBool();
     435
     436        return GetCurrentState();
     437    }
     438
    294439    int Execute()
    295440    {
     
    307452        if (fStatusFTM.second<FTM::kConnected)
    308453            return kStateDisconnected;
     454
     455        if (GetCurrentState()==kStateWaitingForReference)
     456        {
     457            if (fThresholds.size()==0)
     458                return kStateWaitingForReference;
     459
     460            return kStateSettingGlobalThreshold;
     461        }
     462
     463        if (GetCurrentState()==kStateSettingGlobalThreshold)
     464        {
     465            if (fTriggerRate>fTargetRate)
     466                return kStateSettingGlobalThreshold;
     467
     468            return kStateGlobalThresholdSet;
     469        }
     470
     471        if (GetCurrentState()==kStateGlobalThresholdSet)
     472        {
     473            // FIXME: What if it changes?
     474            return kStateGlobalThresholdSet;
     475        }
    309476
    310477        // At least one subsystem is not connected
     
    353520//            ("Stop a ratescan in progress");
    354521
     522        AddEvent("START_DATA_TAKING")
     523            (bind(&StateMachineRateControl::StartDataTaking, this))
     524            ("");
     525
     526        AddEvent("ENABLE_RATE_CONTROL", "B:1")
     527            (bind(&StateMachineRateControl::SetEnabled, this, placeholders::_1))
     528            ("");
     529
     530        AddEvent("SET_MIN_THRESHOLD", "I:1")
     531            (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
     532            ("");
     533
     534        AddEvent("SET_TARGET_RATE", "F:1")
     535            (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
     536            ("");
     537
    355538        AddEvent("PRINT")
    356539            (bind(&StateMachineRateControl::Print, this))
    357540            ("");
     541
     542        AddEvent("SET_VERBOSE", "B")
     543            (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
     544            ("set verbosity state"
     545             "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
     546
    358547    }
    359548
    360549    int EvalOptions(Configuration &conf)
    361550    {
    362         /*
    363         fSecondsMax = conf.Get<uint16_t>("max-wait");
    364         fResolution = conf.Get<double>("resolution");
    365         */
     551        fVerbose = !conf.Get<bool>("quiet");
    366552        return -1;
    367553    }
     
    380566void SetupConfiguration(Configuration &conf)
    381567{
    382     /*
    383568    po::options_description control("Rate scan options");
    384569    control.add_options()
    385         ("max-wait",   var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
    386         ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
     570        ("quiet,q",       po_bool(),  "Disable printing more informations during rate control.")
     571       //("max-wait",   var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
     572       // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
    387573        ;
    388574
    389     conf.AddOptions(control);*/
     575    conf.AddOptions(control);
    390576}
    391577
Note: See TracChangeset for help on using the changeset viewer.