Changeset 15069 for trunk/FACT++
- Timestamp:
- 03/14/13 10:05:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/ratecontrol.cc
r15037 r15069 8 8 #include "Configuration.h" 9 9 #include "Console.h" 10 #include "PixelMap.h" 10 11 11 12 #include "tools.h" … … 45 46 map<string, config> fRunTypes; 46 47 48 PixelMap fMap; 49 47 50 bool fTriggerOn; 48 51 … … 65 68 uint16_t fRequiredEvents; 66 69 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; 68 73 69 74 bool fVerbose; … … 404 409 const Time &time = evt.GetTime(); 405 410 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>(); 406 413 407 414 // 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)) 412 421 break; 413 422 414 fCurrents.pop_front(); 423 fCurrentsMed.pop_front(); 424 fCurrentsDev.pop_front(); 425 fCurrentsVec.pop_front(); 415 426 } 416 427 … … 423 434 424 435 // We want at least 8 values for averaging 425 if (fCurrents .size()<fRequiredEvents)436 if (fCurrentsMed.size()<fRequiredEvents) 426 437 return GetCurrentState(); 427 438 … … 429 440 double avg = 0; 430 441 double rms = 0; 431 for (auto it=fCurrents .begin(); it!=fCurrents.end(); it++)442 for (auto it=fCurrentsMed.begin(); it!=fCurrentsMed.end(); it++) 432 443 { 433 444 avg += it->second; 434 445 rms += it->second*it->second; 435 446 } 436 avg /= fCurrents.size(); 437 rms /= fCurrents.size(); 438 447 avg /= fCurrentsMed.size(); 448 rms /= fCurrentsMed.size(); 439 449 rms = sqrt(rms-avg*avg); 440 450 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); 442 486 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; 443 512 444 513 const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() }; … … 448 517 ostringstream out; 449 518 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; 451 520 Info(out); 452 521 … … 470 539 fTriggerRate = -1; 471 540 fCounter = 0; 541 fBlock.assign(160, false); 472 542 473 543 fCalibrateByCurrent = false; … … 495 565 fCalibrateByCurrent = true; 496 566 fCalibrationTimeStart = Time(); 567 fBlock.assign(160, false); 497 568 498 569 ostringstream out; 499 out << "Rate calibration by current " << fThresholdReference << " with a target rate of " << fTargetRate << " Hz";570 out << "Rate calibration by current " << fThresholdReference << "."; 500 571 Info(out); 501 572 … … 507 578 const string name = evt.GetText(); 508 579 509 constauto it = fRunTypes.find(name);580 auto it = fRunTypes.find(name); 510 581 if (it==fRunTypes.end()) 511 582 { 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 } 514 591 } 515 592 … … 698 775 } 699 776 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) 701 778 { 702 779 if (conf.HasDef(name, sub)) 780 { 781 rc = conf.GetDef<uint16_t>(name, sub); 703 782 return true; 783 } 704 784 705 785 Error("Neither "+name+"default nor "+name+sub+" found."); … … 710 790 { 711 791 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 } 712 798 713 799 fThresholdReference = 300; … … 735 821 } 736 822 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)) 738 829 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 rate749 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 current758 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 830 } 771 831 … … 788 848 po::options_description control("Rate control options"); 789 849 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.") 791 852 //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.") 792 853 // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.") … … 797 858 po::options_description runtype("Run type configuration"); 798 859 runtype.add_options() 799 ("run-type", vars<string>(), 800 ("calibration-type.*", var s<uint16_t>(), "Calibration type (0: none, 1: by rate, 2: by current)")801 ("target-rate.*", var s<uint16_t>(), "Target rate for calibration by rate")802 ("min-threshold.*", var s<uint16_t>(), "Minimum threshold which can be applied in a calibration")803 ("average-time.*", var s<uint16_t>(), "Time in seconds to average the currents for a calibration by current.")804 ("required-events.*", var s<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."); 805 866 ; 806 867
Note:
See TracChangeset
for help on using the changeset viewer.