Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:27

0001 #include "CondFormats/PhysicsToolsObjects/interface/PerformancePayloadFromBinnedTFormula.h"
0002 
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0005 
0006 const int PerformancePayloadFromBinnedTFormula::InvalidPos = -1;
0007 
0008 #include <iostream>
0009 using namespace std;
0010 
0011 void PerformancePayloadFromBinnedTFormula::initialize() {
0012   for (unsigned int t = 0; t < pls.size(); ++t) {
0013     std::vector<std::shared_ptr<TFormula> > temp;
0014     for (unsigned int i = 0; i < (pls[t].formulas()).size(); ++i) {
0015       const auto formulaUniqueName = edm::createGlobalIdentifier();
0016       PhysicsTFormulaPayload tmp = pls[t];
0017       std::shared_ptr<TFormula> tt(new TFormula(formulaUniqueName.c_str(), tmp.formulas()[i].c_str()));
0018       tt->Compile();
0019       temp.push_back(tt);
0020     }
0021     compiledFormulas_.push_back(temp);
0022   }
0023 }
0024 
0025 const std::shared_ptr<TFormula>& PerformancePayloadFromBinnedTFormula::getFormula(PerformanceResult::ResultType r,
0026                                                                                   const BinningPointByMap& p) const {
0027   //
0028   // chooses the correct rectangular region
0029   //
0030   if (!isInPayload(r, p)) {
0031     throw cms::Exception("MalformedPerfPayload") << "Requested performance data not available!";
0032   }
0033   unsigned int region;
0034   bool ok = isOk(p, region);
0035   if (ok == false) {
0036     throw cms::Exception("MalformedPerfPayload") << "Requested variable does not match internal structure!";
0037   }
0038 
0039   return compiledFormulas_[region][resultPos(r)];
0040 }
0041 
0042 float PerformancePayloadFromBinnedTFormula::getResult(PerformanceResult::ResultType r,
0043                                                       const BinningPointByMap& _p) const {
0044   BinningPointByMap p = _p;
0045   //
0046   // which formula to use?
0047   //
0048   if (!isInPayload(r, p))
0049     return PerformancePayload::InvalidResult;
0050 
0051   // nice, what to do here???
0052   //  TFormula * formula = compiledFormulas_[resultPos(r)];
0053   //
0054 
0055   const std::shared_ptr<TFormula>& formula = getFormula(r, p);
0056 
0057   // prepare the vector to pass, order counts!!!
0058   //
0059   std::vector<BinningVariables::BinningVariablesType> t = myBinning();
0060 
0061   // sorry, TFormulas just work up to dimension==4
0062   Double_t values[4];
0063   int i = 0;
0064   for (std::vector<BinningVariables::BinningVariablesType>::const_iterator it = t.begin(); it != t.end(); ++it, ++i) {
0065     values[i] = p.value(*it);
0066   }
0067   //
0068   // i need a non const version
0069   // Note, in current implementation of TFormula EvalPar should be
0070   // thread safe as it does nothing more than call a function
0071   // through a function pointer which is stateless. In spite of the
0072   // fact that it is not const.
0073   return formula->EvalPar(values);
0074 }
0075 
0076 bool PerformancePayloadFromBinnedTFormula::isOk(const BinningPointByMap& _p, unsigned int& whichone) const {
0077   BinningPointByMap p = _p;
0078   //
0079   // change: look on whether a single rectangularr region matches
0080   //
0081   for (unsigned int ti = 0; ti < pls.size(); ++ti) {
0082     bool result = true;
0083     std::vector<BinningVariables::BinningVariablesType> t = myBinning();
0084     for (std::vector<BinningVariables::BinningVariablesType>::const_iterator it = t.begin(); it != t.end(); ++it) {
0085       //
0086       // now looking into a single payload
0087       //
0088       if (!p.isKeyAvailable(*it))
0089         return false;
0090       float v = p.value(*it);
0091       int pos = limitPos(*it);
0092       std::pair<float, float> limits = (pls[ti].limits())[pos];
0093       if (v < limits.first || v > limits.second)
0094         result = false;
0095     }
0096     if (result == true) {
0097       whichone = ti;
0098       return true;
0099     }
0100   }
0101   whichone = 9999;
0102   return false;
0103 }
0104 
0105 bool PerformancePayloadFromBinnedTFormula::isInPayload(PerformanceResult::ResultType res,
0106                                                        const BinningPointByMap& point) const {
0107   // first, let's see if it is available at all
0108   if (resultPos(res) == PerformancePayloadFromBinnedTFormula::InvalidPos)
0109     return false;
0110   unsigned int whocares;
0111   if (!isOk(point, whocares))
0112     return false;
0113   return true;
0114 }
0115 
0116 void PerformancePayloadFromBinnedTFormula::printFormula(PerformanceResult::ResultType res,
0117                                                         const BinningPointByMap& point) const {
0118   //
0119   // which formula to use?
0120   //
0121   if (resultPos(res) == PerformancePayloadFromBinnedTFormula::InvalidPos) {
0122     cout << "Warning: result not available!" << endl;
0123   }
0124 
0125   // nice, what to do here???
0126   const std::shared_ptr<TFormula>& formula = getFormula(res, point);
0127   unsigned int whichone;
0128   isOk(point, whichone);
0129   cout << "-- Formula: " << formula->GetExpFormula("p") << endl;
0130   // prepare the vector to pass, order counts!!!
0131   //
0132   std::vector<BinningVariables::BinningVariablesType> t = myBinning();
0133 
0134   for (std::vector<BinningVariables::BinningVariablesType>::const_iterator it = t.begin(); it != t.end(); ++it) {
0135     int pos = limitPos(*it);
0136     std::pair<float, float> limits = (pls[whichone].limits())[pos];
0137     cout << "      Variable: " << *it << " with limits: "
0138          << "from: " << limits.first << " to: " << limits.second << endl;
0139   }
0140 }
0141 
0142 #include "FWCore/Utilities/interface/typelookup.h"
0143 TYPELOOKUP_DATA_REG(PerformancePayloadFromBinnedTFormula);