CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CalibCalorimetry/HcalAlgos/interface/genlkupmap.h

Go to the documentation of this file.
00001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00002 #include "CalibCalorimetry/HcalAlgos/interface/HcalShapeIntegrator.h"
00003 #include <algorithm> // for "max","min"
00004 #include <math.h>
00005 #include <iostream>
00006 #include <boost/scoped_ptr.hpp>
00007 
00008 // Function generates a lookup map for a passed-in function (via templated object algoObject,
00009 // which must contain method "calcpair" that spits out (x,y) pair from a type float seed.
00010 // Each map y-value is separated from the previous value by a programmable fractional error
00011 // relative to the previous value.
00012 //
00013 // Currently this function coded for only monotonically increasing or
00014 // decreasing functions...
00015 
00016 template <class T_algo>
00017 void genlkupmap(double smin,
00018                 double smax,
00019                 double max_fracerror,
00020                 double min_sstep,
00021                 T_algo& algoObject,
00022                 std::map<double,double>& m_ylookup)
00023 {
00024   std::pair<double,double> thisxy, lastxy, laststoredxy;
00025   std::pair<double,double> minxy = algoObject.calcpair(smin);
00026   std::pair<double,double> maxxy = algoObject.calcpair(smax);
00027   
00028   double slope = maxxy.second - minxy.second;
00029   slope = (slope >= 0.0) ? 1.0 : -1.0;
00030 
00031   double sstep = min_sstep;
00032 
00033   for (double s=smin; lastxy.first<smax; s += sstep) {
00034 
00035     thisxy = algoObject.calcpair(s);
00036 
00037     double fracerror  = slope*(thisxy.second - laststoredxy.second)/ thisxy.second;
00038     double fracchange = slope*(thisxy.second - lastxy.second)/ thisxy.second;
00039 
00040     bool store_cur_pair  = false;
00041     bool store_prev_pair = false;
00042 
00043 #if 0
00044     char str[80];
00045     sprintf(str, "%7.1f %7.1f (%8.3f %8.4f) %8.5f %8.5f",
00046             s, sstep, thisxy.first, thisxy.second, fracerror, fracchange);
00047     cout << str;
00048 #endif
00049 
00050     if (s == smin) {
00051       store_cur_pair = true;
00052     }
00053     else if ((fracerror > 2*max_fracerror) ||
00054              (thisxy.first > smax) ) {
00055       if (sstep > min_sstep) {
00056         // possibly overstepped the next entry, back up and reduce the step size
00057         s -= sstep;
00058         sstep = std::max(0.5*sstep, min_sstep);
00059 #if 0
00060         cout << endl;
00061 #endif
00062         continue;
00063       }
00064       else if (lastxy.second == laststoredxy.second) {
00065         store_cur_pair = true;
00066 
00067         // current step size is too big for the max allowed fractional error,
00068         // store current value and issue a warning.
00069         //
00070         //      edm::LogWarning("HcalPulseContainmentCorrection::genlkupmap") << " fractional error max exceeded";
00071       }
00072       else {
00073         store_prev_pair = true;
00074 
00075         // re-evaluate current yval with prev yval.
00076         fracerror = slope*(thisxy.second - lastxy.second)/ thisxy.second;
00077 
00078         if (fracerror > 2*max_fracerror) {
00079           store_cur_pair = true;
00080 
00081           // current step size is too big for the max allowed fractional error,
00082           // store current value and issue a warning.
00083           //
00084           //      edm::LogWarning("HcalPulseContainmentCorrection::genlkupmap") << " fractional error max exceeded";
00085         }
00086       }
00087     }
00088     else if ((fracerror  < 1.9*max_fracerror) &&
00089              (fracchange < 0.1*max_fracerror) &&
00090              (thisxy.first < 0.99 * smax) ) {
00091       // adapt step size to reduce iterations
00092       sstep *= 2.0;
00093     }
00094 
00095     if (thisxy.first > smax)
00096       store_cur_pair = true;
00097       
00098     if (store_prev_pair) {
00099       m_ylookup[lastxy.first] = lastxy.second;
00100       laststoredxy            = lastxy;
00101     }
00102     if (store_cur_pair) {
00103       m_ylookup[thisxy.first] = thisxy.second;
00104       laststoredxy            = thisxy;
00105     }
00106 
00107     lastxy = thisxy;
00108     
00109 #if 0
00110     sprintf(str, " %c %c",
00111             store_cur_pair ? 'C' : ' ',
00112             store_prev_pair ? 'P' : ' ');
00113     cout << str << endl;
00114 #endif
00115 
00116   }
00117 
00118 }
00119 
00120 //======================================================================
00121 // Fixed Phase mode amplitude correction factor generation routines:
00122 
00123 template <class S> 
00124 class RecoFCcorFactorAlgo {
00125 public:
00126   RecoFCcorFactorAlgo(int    num_samples,
00127                       double fixedphase_ns);
00128   std::pair<double,double> calcpair(double);
00129 private:
00130   double fixedphasens_;
00131   double integrationwindowns_;
00132   double time0shiftns_;
00133   S shape_;
00134   const boost::scoped_ptr<HcalShapeIntegrator> integrator_;
00135 };
00136