Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:12:27

0001 //
0002 // Revision 1.19  2010/07/20 02:58:27  wmtan
0003 // Add missing #include files
0004 //
0005 // Revision 1.18  2010/04/02 20:48:12  wittich
0006 // updates to scale entries by received number of FU's
0007 //
0008 // Revision 1.17  2010/03/17 20:56:19  wittich
0009 // Check for good updates based on mergeCount values
0010 // add code for rates normalized per FU
0011 //
0012 // Revision 1.16  2010/03/16 22:19:19  wittich
0013 // updates for per-LS normalization for variable
0014 // number of FU's sending information back to the clients.
0015 //
0016 // Revision 1.15  2010/02/11 23:55:18  wittich
0017 // - adapt to shorter Lumi Section length
0018 // - fix bug in how history of counts was filled
0019 //
0020 // Revision 1.14  2010/02/02 11:44:20  wittich
0021 // more diagnostics for online scalers
0022 //
0023 // Revision 1.13  2009/12/15 20:41:16  wittich
0024 // better hlt scalers client
0025 //
0026 // Revision 1.12  2009/11/22 13:33:05  puigh
0027 // clean beginJob
0028 //
0029 // Revision 1.11  2009/11/07 03:35:05  lorenzo
0030 // fixed binning
0031 //
0032 // Revision 1.10  2009/11/04 06:00:05  lorenzo
0033 // changed folders
0034 //
0035 
0036 ///// THIS ONLY WILL WORK WITH THE DATA FROM THE FU'S
0037 
0038 #include <cassert>
0039 #include <numeric>
0040 
0041 #include "DQM/TrigXMonitorClient/interface/HLTScalersClient.h"
0042 
0043 #include "FWCore/Framework/interface/Event.h"
0044 #include "FWCore/Framework/interface/LuminosityBlock.h"
0045 #include "FWCore/Framework/interface/Run.h"
0046 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0047 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0048 #include "FWCore/ServiceRegistry/interface/Service.h"
0049 #include "FWCore/Utilities/interface/isFinite.h"
0050 
0051 #include "DQMServices/Core/interface/DQMStore.h"
0052 
0053 using edm::LogInfo;
0054 using edm::LogWarning;
0055 
0056 // I am not sure this is right at more than 10%
0057 #define SECS_PER_LUMI_SECTION 23.31
0058 const int kPerHisto = 20;
0059 
0060 /// Constructors
0061 HLTScalersClient::HLTScalersClient(const edm::ParameterSet &ps)
0062     : dbe_(nullptr),
0063       nLumi_(0),
0064       currentRate_(nullptr),
0065       currentLumiBlockNumber_(0),
0066       first_(true),
0067       missingPathNames_(true),
0068       folderName_(ps.getUntrackedParameter<std::string>("dqmFolder", "HLT/HLScalers_EvF")),
0069       kRateIntegWindow_(ps.getUntrackedParameter<unsigned int>("rateIntegWindow", 3)),
0070       processName_(ps.getParameter<std::string>("processName")),
0071       ignores_(),
0072       debug_(ps.getUntrackedParameter<bool>("debugDump", false)),
0073       maxFU_(ps.getUntrackedParameter<unsigned int>("maxFU", false)),
0074       recentOverallCountsPerLS_(kRateIntegWindow_),
0075       recentNormedOverallCountsPerLS_(2) {
0076   LogDebug("HLTScalersClient") << "constructor";
0077   if (debug_) {
0078     textfile_.open("debug.txt");
0079     if (!textfile_) {
0080       std::cout << "constructor: can't open text file" << std::endl;
0081     }
0082   }
0083   // get back-end interface
0084   dbe_ = edm::Service<DQMStore>().operator->();
0085   dbe_->setCurrentFolder(folderName_);
0086 
0087   std::string rawdir(folderName_ + "/raw");
0088   dbe_->setCurrentFolder(rawdir);
0089   hltRate_ = dbe_->book1D("hltRate", "Overall HLT Accept rate vs LS", MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5);
0090   dbe_->setCurrentFolder(folderName_);
0091   hltNormRate_ = dbe_->book1D(
0092       "hltRateNorm", "Overall HLT Accept rate vs LS, scaled", MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5);
0093   hltCount_ = dbe_->book1D("hltCount", "Overall HLT Counts vs LS", MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5);
0094   //   hltCountN_= dbe_->book1D("hltCountN", "Overall HLT Counts per LS vs LS",
0095   //              MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT-0.5);
0096   mergeCount_ =
0097       dbe_->book1D("mergeCount", "Number of merge counts vs LS", MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5);
0098 
0099   updates_ = dbe_->book1D("updates", "Status of Updates", 2, 0, 2);
0100   updates_->setBinLabel(1, "Good Updates");
0101   updates_->setBinLabel(2, "Incomplete Updates");
0102 
0103 }  // end constructor
0104 
0105 /// BeginJob
0106 void HLTScalersClient::beginJob(void) {
0107   LogDebug("HLTScalersClient") << "beingJob";
0108   if (dbe_) {
0109     dbe_->setCurrentFolder(folderName_);
0110   }
0111   first_ = true;
0112   missingPathNames_ = true;
0113 }
0114 
0115 /// beginRun
0116 void HLTScalersClient::beginRun(const edm::Run &run, const edm::EventSetup &c) {
0117   missingPathNames_ = true;
0118   first_ = true;
0119   LogDebug("HLTScalersClient") << "beginRun, run " << run.id();
0120 
0121 }  // beginRun
0122 
0123 /// EndRun
0124 void HLTScalersClient::endRun(const edm::Run &run, const edm::EventSetup &c) {
0125   missingPathNames_ = true;
0126   first_ = true;
0127 }
0128 
0129 /// End LumiBlock
0130 /// DQM Client Diagnostic should be performed here
0131 void HLTScalersClient::endLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &c) {
0132   nLumi_ = lumiSeg.id().luminosityBlock();
0133 
0134   // get raw data
0135   std::string scalHisto = folderName_ + "/raw/hltScalers";
0136   MonitorElement *scalers = dbe_->get(scalHisto);
0137   if (scalers == nullptr) {
0138     LogDebug("HLTScalersClient") << "cannot get hlt scalers histogram, "
0139                                  << "bailing out.";
0140     if (debug_)
0141       std::cout << "No scalers ? Looking for " << scalHisto << std::endl;
0142     return;
0143   }
0144 
0145   int npaths = scalers->getNbinsX();
0146   if (npaths > MAX_PATHS)
0147     npaths = MAX_PATHS;  // HARD CODE FOR NOW
0148   LogDebug("HLTScalersClient") << "I see " << npaths << " paths. ";
0149 
0150   // set the bin labels on the first go-through
0151   // I need to do this here because we don't have the paths yet
0152   // on begin-run. I should do this in a less ugly way (see FV?)
0153   if (first_) {
0154     std::string rawdir(folderName_ + "/raw");
0155 
0156     LogDebug("HLTScalersClient") << "Setting up paths on first endLumiBlock " << npaths;
0157     dbe_->setCurrentFolder(rawdir);
0158     currentRate_ = dbe_->book1D("cur_rate", "current lumi section rate per path", npaths, -0.5, npaths - 0.5);
0159     currentNormRate_ =
0160         dbe_->book1D("cur_rate_norm", "current norm. lumi section rate per path", npaths, -0.5, npaths - 0.5);
0161     recentPathCountsPerLS_.reserve(npaths);
0162     recentNormedPathCountsPerLS_.reserve(npaths);
0163     char rates_subfolder[256];
0164     snprintf(rates_subfolder, 256, "%s/RateHistory", folderName_.c_str());
0165     char counts_subfolder[256];
0166     snprintf(counts_subfolder, 256, "%s/CountHistory", folderName_.c_str());
0167 
0168     hltCurrentRate_.reserve(npaths);
0169     rateHistories_.reserve(npaths);
0170     countHistories_.reserve(npaths);
0171     hltCurrentNormRate_.reserve(npaths);
0172     rateNormHistories_.reserve(npaths);
0173 
0174     dbe_->setCurrentFolder(folderName_);  // these belong in top-level
0175     for (int i = 0; i < npaths; ++i) {
0176       dbe_->setCurrentFolder(std::string(rates_subfolder) + "/raw");
0177 
0178       char name[256];
0179       snprintf(name, 256, "raw_rate_p%03d", i);
0180       //     LogDebug("HLTScalersClient") << "name " << i << " is " << name ;
0181       rateHistories_.push_back(dbe_->book1D(name, name, MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5));
0182       snprintf(name, 256, "norm_rate_p%03d", i);
0183       dbe_->setCurrentFolder(rates_subfolder);
0184       rateNormHistories_.push_back(dbe_->book1D(name, name, MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5));
0185       dbe_->setCurrentFolder(counts_subfolder);
0186       snprintf(name, 256, "counts_p%03d", i);
0187       countHistories_.push_back(dbe_->book1D(name, name, MAX_LUMI_SEG_HLT, -0.5, MAX_LUMI_SEG_HLT - 0.5));
0188       // prefill the data structures
0189       recentPathCountsPerLS_.push_back(CountLSFifo_t(kRateIntegWindow_));
0190       recentNormedPathCountsPerLS_.push_back(CountLSFifo_t(2));
0191     }
0192     dbe_->setCurrentFolder(folderName_);
0193 
0194     // split hlt scalers up into groups of 20
0195     const int maxlen = 64;
0196     char metitle[maxlen];                     // histo name
0197     char mename[maxlen];                      // ME name
0198     int numHistos = int(npaths / kPerHisto);  // this hasta be w/o remainders
0199 
0200     int remainder = npaths % kPerHisto;
0201     if (remainder)
0202       numHistos += 1;
0203 
0204     for (int k = 0; k < numHistos; k++) {
0205       int npath_low = kPerHisto * k;
0206       int npath_high = kPerHisto * (k + 1) - 1;
0207       snprintf(mename, maxlen, "hltScalers_%0d", k);
0208       snprintf(metitle, maxlen, "HLT scalers - Paths %d to %d", npath_low, npath_high);
0209       dbe_->setCurrentFolder(rawdir);
0210       hltCurrentRate_.push_back(dbe_->book1D(mename, metitle, kPerHisto, -0.5 + npath_low, npath_high + 0.5));
0211       dbe_->setCurrentFolder(folderName_);  // these belong in top-level
0212       snprintf(mename, maxlen, "hltScalersNorm_%0d", k);
0213       snprintf(metitle, maxlen, "HLT Rate (scaled) - Paths %d to %d", npath_low, npath_high);
0214       hltCurrentNormRate_.push_back(dbe_->book1D(mename, metitle, kPerHisto, -0.5 + npath_low, npath_high + 0.5));
0215     }
0216 
0217     first_ = false;
0218   }
0219 
0220   if (missingPathNames_) {
0221     // first try the scalers histogram and see if the bin names are set
0222     for (int i = 0; i < npaths; ++i) {
0223       // needs to be i+1 since root bins start at 1
0224       const char *name = scalers->getTH1()->GetXaxis()->GetBinLabel(i + 1);
0225       if (name && (strlen(name) > 0)) {
0226         if (debug_) {
0227           std::cout << "path " << i << " name is " << name << std::endl;
0228         }
0229         int whichHisto = i / kPerHisto;
0230         int whichBin = i % kPerHisto + 1;
0231         char pname[256];
0232         hltCurrentRate_[whichHisto]->setBinLabel(whichBin, name);
0233         hltCurrentNormRate_[whichHisto]->setBinLabel(whichBin, name);
0234         snprintf(pname, 256, "Rate - path %s (Path # %03d)", name, i);
0235         rateHistories_[i]->setTitle(pname);
0236         rateNormHistories_[i]->setTitle(pname);
0237         snprintf(pname, 256, "Counts - path %s (Path # %03d)", name, i);
0238         countHistories_[i]->setTitle(pname);
0239 
0240         currentRate_->setBinLabel(i + 1, name);
0241         currentNormRate_->setBinLabel(i + 1, name);
0242 
0243         missingPathNames_ = false;
0244       }
0245     }
0246   }
0247   // MEGA-HACK
0248   if (missingPathNames_) {
0249     // if that didn't work we load 'em from a text file. damn straight.
0250     int ipath = 1;
0251     std::ifstream names("names.dat");
0252     if (!names) {
0253       if (debug_) {
0254         std::ostringstream msg;
0255         msg << "open of "
0256             << "names.dat";
0257         perror(msg.str().c_str());
0258       }
0259     } else {  // open succeeded
0260       missingPathNames_ = false;
0261       std::string line;
0262       while (!names.eof()) {
0263         getline(names, line);
0264         std::istringstream fnames(line);
0265         std::string label;
0266         int bin;
0267         if (fnames.str().find('#') == 0)  // skip comment lines
0268           continue;
0269         if (fnames >> bin >> label) {
0270           if (debug_) {
0271             std::cout << bin << "--" << label << "(" << ipath << ")" << std::endl;
0272           }
0273           currentRate_->setBinLabel(ipath, label);
0274           currentNormRate_->setBinLabel(ipath, label);
0275           countHistories_[ipath - 1]->setTitle(label);
0276           rateHistories_[ipath - 1]->setTitle(label);
0277           rateNormHistories_[ipath - 1]->setTitle(label);
0278           int whichHisto = (ipath - 1) / kPerHisto;
0279           int whichBin = (ipath - 1) % kPerHisto + 1;
0280           hltCurrentRate_[whichHisto]->setBinLabel(whichBin, label);
0281           hltCurrentNormRate_[whichHisto]->setBinLabel(whichBin, label);
0282           ++ipath;
0283           if (ipath > npaths)
0284             break;
0285         }  //
0286       }    // loop lines
0287     }      // open ok
0288   }
0289 
0290   // END SETUP
0291 
0292   std::string nLumiHisto(folderName_ + "/nLumiBlock");
0293   MonitorElement *nLumi = dbe_->get(nLumiHisto);
0294   if (nLumi == nullptr) {
0295     nLumiHisto = folderName_ + "/raw/nLumiBlock";
0296     nLumi = dbe_->get(nLumiHisto);
0297   }
0298   int testval = (nLumi != nullptr ? nLumi->getIntValue() : -1);
0299   LogDebug("HLTScalersClient") << "Lumi Block from DQM: " << testval << ", local is " << nLumi_;
0300   int nL = (nLumi != nullptr ? nLumi->getIntValue() : nLumi_);
0301   if (nL > MAX_LUMI_SEG_HLT) {
0302     LogDebug("HLTScalersClient") << "Too many Lumi segments, " << nL << " is greater than MAX_LUMI_SEG_HLT,"
0303                                  << " wrapping to " << (nL % MAX_LUMI_SEG_HLT);
0304     // nL = MAX_LUMI_SEG_HLT;
0305     nL = nL % MAX_LUMI_SEG_HLT;
0306   }
0307 
0308   // merging counts
0309   double num_fu = -1.0;
0310   std::string mergeName(folderName_ + "/raw/hltMerge");
0311   MonitorElement *merge = dbe_->get(mergeName);
0312   if (merge != nullptr) {
0313     num_fu = merge->getBinContent(1);
0314     if (debug_) {
0315       std::cout << "Number of received entries: " << num_fu << std::endl;
0316     }
0317     mergeCount_->Fill(nL, num_fu);
0318   }
0319   // end
0320 
0321   // evaluate the data
0322   // loop over current counts
0323 
0324   // this gets filled for all times. Mainly for debugging.
0325   for (int i = 1; i <= npaths; ++i) {  // bins start at 1
0326     double current_count = scalers->getBinContent(i);
0327     // nb that this will now _overwrite_ some settings
0328     countHistories_[i - 1]->setBinContent(nL, current_count);  // good or bad
0329   }
0330 
0331   std::string overallScalerName(folderName_ + "/raw/hltOverallScaler");
0332   MonitorElement *hltScaler = dbe_->get(overallScalerName);
0333   if (hltScaler != nullptr) {
0334     double current_count = hltScaler->getBinContent(1);
0335     hltCount_->setBinContent(nL, current_count);
0336     recentOverallCountsPerLS_.update(CountLS_t(nL, current_count));
0337     std::pair<double, double> sl = getSlope_(recentOverallCountsPerLS_);
0338     double slope = sl.first;
0339     double slope_err = sl.second;
0340     if (slope > 0) {
0341       hltRate_->setBinContent(nL, slope);
0342       if (!edm::isNotFinite(slope_err) && (slope_err >= 0))
0343         hltRate_->setBinError(nL, slope_err);
0344     }
0345   }  // found  histo
0346 
0347   if (num_fu >= 0.95 * maxFU_) {
0348     if (num_fu > maxFU_) {
0349       maxFU_ = num_fu;
0350       if (debug_)
0351         std::cout << "maxFU is now " << maxFU_ << std::endl;
0352     }
0353     // good data
0354     for (int i = 1; i <= npaths; ++i) {  // bins start at 1
0355       double current_count = scalers->getBinContent(i);
0356       // DEBUG
0357       if (!recentPathCountsPerLS_[i - 1].empty() && debug_)
0358         std::cout << i << "\t-> good one: new => cnt, ls = " << current_count << ", " << nL
0359                   << ", old = " << recentPathCountsPerLS_[i - 1].back().second << "\t"
0360                   << recentPathCountsPerLS_[i - 1].back().first << std::endl;
0361       // END DEBUG
0362       recentPathCountsPerLS_[i - 1].update(CountLS_t(nL, current_count));
0363 
0364       // NB: we do not fill a new entry in the rate histo if we can't
0365       // calculate it
0366       std::pair<double, double> sl = getSlope_(recentPathCountsPerLS_[i - 1]);
0367       double slope = sl.first;
0368       double slope_err = sl.second;
0369       // rateHistories_[i-1]->Fill(nL,slope);
0370       if (slope > 0) {
0371         rateHistories_[i - 1]->setBinContent(nL, slope);
0372         // set the current rate(s)
0373         hltCurrentRate_[(i - 1) / kPerHisto]->setBinContent(i % kPerHisto, slope);
0374         currentRate_->setBinContent(i, slope);
0375         if (!edm::isNotFinite(slope_err) && (slope_err >= 0)) {
0376           currentRate_->setBinError(i, slope_err);
0377           hltCurrentRate_[(i - 1) / kPerHisto]->setBinError(i % kPerHisto, slope_err);
0378           rateHistories_[i - 1]->setBinError(nL, slope_err);
0379         }
0380       }
0381       //// HACK - normalized path rates
0382       //// END HACK
0383 
0384     }  // loop over paths
0385 
0386     // ---------------------------- overall rate, absolute counts
0387     std::string overallScalerName(folderName_ + "/raw/hltOverallScaler");
0388     MonitorElement *hltScaler = dbe_->get(overallScalerName);
0389     if (hltScaler != nullptr) {
0390       double current_count = hltScaler->getBinContent(1);
0391       hltCount_->setBinContent(nL, current_count);
0392       recentOverallCountsPerLS_.update(CountLS_t(nL, current_count));
0393       std::pair<double, double> sl = getSlope_(recentOverallCountsPerLS_);
0394       double slope = sl.first;
0395       double slope_err = sl.second;
0396       if (slope >= 0) {
0397         hltRate_->setBinContent(nL, slope);
0398         if (!edm::isNotFinite(slope_err) && (slope_err >= 0))
0399           hltRate_->setBinError(nL, slope_err);
0400       }
0401     }                   // found  histo
0402     updates_->Fill(0);  // good
0403   }                     // check on number of FU's - good data
0404   else {
0405     updates_->Fill(1);  // missing updates
0406   }
0407 
0408   // PW DEBUG
0409   if (debug_) {
0410     textfile_ << nL << "\t" << npaths << "\t";
0411     for (int i = 0; i < npaths; ++i) {
0412       textfile_ << scalers->getBinContent(i) << " ";
0413     }
0414     textfile_ << std::endl;
0415   }
0416   // end DEBUG
0417 
0418 #ifdef LATER
0419   // ------ overall rate normalized - all data
0420   overallScalerName = std::string(folderName_ + "/raw/hltOverallScalerN");
0421   hltScaler = dbe_->get(overallScalerName);
0422   if (hltScaler != 0) {
0423     double cnt = hltScaler->getBinContent(1);
0424     //     hltCountN_->setBinContent(nL,cnt);
0425     if (debug_) {
0426       std::cout << "Overall Norm: new => cnt, ls = " << cnt << ", " << nL << ", num_fu = " << num_fu << std::endl;
0427     }
0428     recentNormedOverallCountsPerLS_.update(CountLS_t(nL, cnt / num_fu));
0429     cnt = recentNormedOverallCountsPerLS_.getCount(nL);  // for dupes/partials
0430     double slope = cnt / num_fu / SECS_PER_LUMI_SECTION;
0431     if (debug_) {
0432       std::cout << "Normalized slope = " << slope << std::endl;
0433     }
0434     if (slope > 0)
0435       hltNormRate_->setBinContent(nL, slope);
0436   }
0437   //
0438   std::string scalHistoNorm = folderName_ + "/raw/hltScalersN";
0439   MonitorElement *scalersN = dbe_->get(scalHistoNorm);
0440   if (scalersN) {
0441     for (int i = 0; i < npaths; ++i) {
0442       double cnt = scalersN->getBinContent(i);
0443       double slope = cnt / num_fu / SECS_PER_LUMI_SECTION;
0444       if (slope > 0) {
0445         rateNormHistories_[i - 1]->setBinContent(nL, slope);
0446         // set the current rate(s)
0447         hltCurrentNormRate_[(i - 1) / kPerHisto]->setBinContent(i % kPerHisto, slope);
0448         currentNormRate_->setBinContent(i, slope);
0449       }
0450     }
0451   }
0452 #else  // NOT LATER
0453   // ------ overall rate normalized - all data
0454   overallScalerName = std::string(folderName_ + "/raw/hltOverallScaler");
0455   hltScaler = dbe_->get(overallScalerName);
0456   if (hltScaler != nullptr) {
0457     double cnt = hltScaler->getBinContent(1);
0458     //     hltCountN_->setBinContent(nL,cnt);
0459     float sf = num_fu / maxFU_;
0460     if (debug_) {
0461       std::cout << "Overall Norm: new => cnt, ls = " << cnt << ", " << nL << ", num_fu = " << num_fu << ", sf = " << sf
0462                 << std::endl;
0463     }
0464     recentNormedOverallCountsPerLS_.update(CountLS_t(nL, cnt / sf));
0465     cnt = recentNormedOverallCountsPerLS_.getCount(nL);  // for dupes/partials
0466     std::pair<double, double> sl = getSlope_(recentNormedOverallCountsPerLS_);
0467     double slope = sl.first;
0468     double slope_err = sl.second;
0469     if (debug_) {
0470       std::cout << "Normalized slope = " << slope << std::endl;
0471     }
0472     if (slope > 0) {
0473       hltNormRate_->setBinContent(nL, slope);
0474       if (cnt > 0)
0475         slope_err = slope * sqrt(2. / num_fu + 2. / cnt);
0476       if (!edm::isNotFinite(slope_err) && (slope_err >= 0))
0477         hltNormRate_->setBinError(nL, slope_err);
0478     }
0479   }
0480   //
0481   std::string scalHistoNorm = folderName_ + "/raw/hltScalers";
0482   MonitorElement *scalersN = dbe_->get(scalHistoNorm);
0483   if (scalersN) {
0484     double sf = num_fu / maxFU_;
0485     for (int i = 1; i <= npaths; ++i) {
0486       double cnt = scalersN->getBinContent(i);
0487       recentNormedPathCountsPerLS_[i - 1].update(CountLS_t(nL, cnt / sf));
0488       std::pair<double, double> sl = getSlope_(recentNormedPathCountsPerLS_[i - 1]);
0489       double slope = sl.first;
0490       double slope_err = sl.second;
0491       if (slope >= 0) {
0492         rateNormHistories_[i - 1]->setBinContent(nL, slope);
0493         // set the current rate(s)
0494         hltCurrentNormRate_[(i - 1) / kPerHisto]->setBinContent(i % kPerHisto, slope);
0495         currentNormRate_->setBinContent(i, slope);
0496         if (slope_err <= 0 && cnt > 0) {
0497           // ignores error on prev point, so scale by sqrt(2)
0498           slope_err = slope * sqrt(2. / num_fu + 2. / cnt);
0499           if (debug_) {
0500             std::cout << "Slope err " << i << " = " << slope_err << std::endl;
0501           }
0502         }
0503         if (!edm::isNotFinite(slope_err) && (slope_err >= 0)) {
0504           rateNormHistories_[i - 1]->setBinError(nL, slope_err);
0505           // set the current rate(s)
0506           hltCurrentNormRate_[(i - 1) / kPerHisto]->setBinError(i % kPerHisto, slope_err);
0507           currentNormRate_->setBinError(i, slope_err);
0508         }
0509       }
0510     }
0511   }
0512 
0513 #endif  // LATER
0514   //
0515 }
0516 
0517 // unused
0518 void HLTScalersClient::analyze(const edm::Event &e, const edm::EventSetup &c) {
0519   // nothing to do here
0520 }
0521 
0522 // this is probably overkill. Do a least-squares fit to get slope
0523 // note that the data is in units of counts, ls number
0524 // but we return a value in Hz...
0525 std::pair<double, double> HLTScalersClient::getSlope_(const HLTScalersClient::CountLSFifo_t &points) {
0526   double slope, sigma_m;
0527   if (points.size() < points.targetSize()) {
0528     return std::pair<double, double>(-1, -1);
0529   }
0530   // just do a delta if we just want two bins
0531   else if (points.size() == 2) {
0532     // just do diff and be done with it
0533     double delta_ls = points.front().first - points.back().first;
0534     double delta_cnt = points.front().second - points.back().second;
0535     slope = delta_cnt / delta_ls;
0536     sigma_m = -1;
0537   } else {  // do a fit
0538     double xy = 0;
0539     double x = 0;
0540     double xsq = 0;
0541     double y = 0;
0542     double n = double(points.size());
0543     for (auto i(points.begin()); i != points.end(); ++i) {
0544       if (debug_)
0545         std::cout << "x = " << i->first << ", y = " << i->second << std::endl;
0546       xy += i->first * i->second;
0547       x += i->first;
0548       xsq += i->first * i->first;
0549       y += i->second;
0550     }
0551     slope = (n * xy - x * y) / (n * xsq - x * x);
0552 
0553     // now get the uncertainty on the slope. Need intercept for this.
0554     double intercept = (xsq * y - xy * x) / (n * xsq - x * x);
0555     double sigma_ysq = 0;
0556     for (auto i(points.begin()); i != points.end(); ++i) {
0557       sigma_ysq += pow((i->second - slope * i->first - intercept), 2.);
0558     }
0559     //     if ( debug_ )
0560     //       std::cout << "chi^2 = " << sigma_ysq << std::endl;
0561     sigma_ysq *= 1. / (n - 2.);
0562 
0563     sigma_m = sqrt(n * sigma_ysq / (n * xsq - x * x));
0564   }
0565 
0566   //  std::cout << "Slope is " << slope << std::endl;
0567   slope /= SECS_PER_LUMI_SECTION;
0568   if (sigma_m > 0)
0569     sigma_m /= SECS_PER_LUMI_SECTION;
0570   //   if ( debug_ ) {
0571   //     std::cout << "Slope = " << slope << " +- " << sigma_m
0572   //          << std::endl;
0573   //   std::cout << "intercept is " << intercept
0574   //        << std::endl;
0575   //  }
0576 
0577   return std::pair<double, double>(slope, sigma_m);
0578 }