CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/MagneticField/Layers/src/MagBinFinders.h

Go to the documentation of this file.
00001 
00002 // A set of binfiders adapted from CommonReco/DetLayers.
00003 // FIXME: This file should eventually disappear and binfinders in 
00004 // CommonReco/DetLayers  modified to be general enough!!!
00005 
00006 namespace MagBinFinders {
00007   template <class T> class GeneralBinFinderInR;
00008   template <class T> class GeneralBinFinderInZ;
00009 }
00010 
00011 
00012 //----------------------------------------------------------------------
00013 #ifndef GeneralBinFinderInR_H
00014 #define GeneralBinFinderInR_H
00015 
00023 #include "Utilities/BinningTools/interface/BaseBinFinder.h"
00024 //#include "CommonReco/DetLayers/interface/RBorderFinder.h"
00025 #include <cmath>
00026 #include <vector>
00027 
00028 template <class T>
00029 class MagBinFinders::GeneralBinFinderInR : public BaseBinFinder<T>{
00030 public:
00031   
00032   GeneralBinFinderInR() : theNbins(0) {}
00033 
00034   GeneralBinFinderInR(std::vector<T>& borders) :
00035     theNbins(borders.size()), 
00036     theBorders(borders) {
00037     // FIXME: compute bin positions.
00038 //     for (vector<T>::const_iterator i=theBorders.begin();
00039 //       i<theBorders.end(); ++i) {
00040 //      theBorders.push_back(((*i) + (*(i+1))) / 2.);
00041 
00042 //     cout << "GeneralBinFinderInR_ " << theNbins << " " << theBorders.size() << " " << (int) this << endl;
00043 }
00044   
00045 
00046 //   /// Construct from an already initialized RBorderFinder
00047 //   GeneralBinFinderInR(const RBorderFinder& bf) {
00048 //     theBorders=bf.RBorders();
00049 //     theBins=bf.RBins();
00050 //     theNbins=theBins.size();
00051 //   }
00052 
00053 //  /// Construct from the list of Det*
00054 //   GeneralBinFinderInR(vector<Det*>::const_iterator first,
00055 //                    vector<Det*>::const_iterator last)
00056 //     : theNbins( last-first)
00057 //   {
00058 //     vector<Det*> dets(first,last);
00059 //     RBorderFinder bf(dets);
00060 //     theBorders=bf.phiBorders();
00061 //     theBins=bf.phiBins();
00062 //     theNbins=theBins.size();
00063 //   }
00064 
00065   
00068   virtual int binIndex( T R) const {
00069     int i;
00070     for (i = 0; i<theNbins; ++i) {
00071       if (R < theBorders[i]){ // FIXME: one can be skipped?
00072          break;
00073       }
00074     }
00075     return binIndex(i-1);
00076   }
00077 
00079   virtual int binIndex( int i) const {
00080     return std::min( std::max( i, 0), theNbins-1);
00081   }
00082    
00084   virtual T binPosition( int ind) const {
00085     return theBins[binIndex(ind)];
00086   }
00087 
00088 
00089 private:
00090   int theNbins;
00091   std::vector<T> theBorders;
00092   std::vector<T> theBins;
00093 
00094 };
00095 #endif
00096 //----------------------------------------------------------------------
00097 
00098 
00099 
00100 //----------------------------------------------------------------------
00101 #ifndef GeneralBinFinderInZ_H
00102 #define GeneralBinFinderInZ_H
00103 
00111 #include "Utilities/BinningTools/interface/BaseBinFinder.h"
00112 #include <cmath>
00113 
00114 template <class T>
00115 class MagBinFinders::GeneralBinFinderInZ : public BaseBinFinder<T> {
00116 public:
00117 
00118   GeneralBinFinderInZ() : theNbins(0), theZStep(0), theZOffset(0) {}
00119 
00120   GeneralBinFinderInZ(std::vector<T>& borders) :
00121     theNbins(borders.size()), 
00122     theBorders(++(borders.begin()),borders.end()) { // Skip border of 1. bin
00123     // FIXME:  crashes for borders.size()==1 (trivial case)
00124     // FIXME set theBins!!!
00125     theZOffset = theBorders.front(); 
00126     if (theNbins>2) {
00127       theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
00128     } else {
00129       theZStep = 1.; // Not relevant in this case...
00130     }
00131     
00132 //     cout << "GeneralBinFinderInZ " << theBorders.size()
00133 //       << " " << theZStep << endl;
00134   }
00135   
00136   // FIXME: ??? theNbins e theBorders hanno size theNbins -1.
00137   // theBorders[1] e' l'inizio del secondo bin !!!
00138 
00139 //   GeneralBinFinderInZ(vector<Det*>::const_iterator first,
00140 //                    vector<Det*>::const_iterator last) :
00141 //     theNbins( last-first)
00142 //   {
00143 //     theBins.reserve(theNbins);
00144 //     for (vector<Det*>::const_iterator i=first; i<last-1; ++i) {
00145 //       theBins.push_back((**i).position().z());
00146 //       theBorders.push_back(((**i).position().z() + 
00147 //                          (**(i+1)).position().z()) / 2.);
00148 //     }
00149 
00150 //     theZOffset = theBorders.front(); 
00151 //     theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
00152 //   }
00153 
00155   virtual int binIndex(T z) const {
00156     int bin = binIndex(int((z-theZOffset)/theZStep)+1);
00157     
00158     // check left border
00159     if (bin > 0) {
00160       if (z < theBorders[bin-1]) {
00161         // z is to the left of the left border, the correct bin is left
00162         for (int i=bin-1; ; --i) {
00163           if (i <= 0) return 0;  
00164           if ( z > theBorders[i-1]) return i;
00165         }
00166       }
00167     } 
00168     else return 0;
00169 
00170     // check right border
00171     if (bin < theNbins-1) {
00172       if ( z > theBorders[bin]) {
00173         // z is to the right of the right border, the correct bin is right
00174         for (int i=bin+1; ; ++i) {
00175           if (i >= theNbins-1) return theNbins-1;  
00176           if ( z < theBorders[i]) return i;
00177         }
00178       }
00179     }
00180     else return theNbins-1;
00181 
00182     // if we arrive here it means that the bin is ok 
00183     return bin;
00184   }
00185 
00187   virtual int binIndex( int i) const {
00188     return std::min( std::max( i, 0), theNbins-1);
00189   }
00190    
00192   virtual T binPosition( int ind) const {
00193     return theBins[binIndex(ind)];
00194   }
00195 
00196   static double pi() { return 3.141592653589793238;}
00197   static double twoPi() { return 2.*pi();}
00198 
00199 private:
00200 
00201   int theNbins;
00202   T theZStep;
00203   T theZOffset;
00204   std::vector<T> theBorders;
00205   std::vector<T> theBins;
00206 };
00207 #endif
00208 //----------------------------------------------------------------------