CMS 3D CMS Logo

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

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