Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:25

0001 
0002 // A set of binfiders adapted from CommonReco/DetLayers.
0003 // FIXME: This file should eventually disappear and binfinders in
0004 // CommonReco/DetLayers  modified to be general enough!!!
0005 
0006 namespace MagBinFinders {
0007   template <class T>
0008   class GeneralBinFinderInR;
0009   template <class T>
0010   class GeneralBinFinderInZ;
0011 }  // namespace MagBinFinders
0012 
0013 //----------------------------------------------------------------------
0014 #ifndef GeneralBinFinderInR_H
0015 #define GeneralBinFinderInR_H
0016 /** \class MagBinFinders::GeneralBinFinderInR
0017  *  A R binfinder for a non-periodic group of detectors.
0018  *
0019  *  \author N. Amapane - INFN Torino
0020  */
0021 
0022 #include "Utilities/BinningTools/interface/BaseBinFinder.h"
0023 //#include "CommonReco/DetLayers/interface/RBorderFinder.h"
0024 #include <cmath>
0025 #include <vector>
0026 
0027 template <class T>
0028 class MagBinFinders::GeneralBinFinderInR : public BaseBinFinder<T> {
0029 public:
0030   GeneralBinFinderInR() : theNbins(0) {}
0031 
0032   GeneralBinFinderInR(std::vector<T>& borders) : theNbins(borders.size()), theBorders(borders) {
0033     // FIXME: compute bin positions.
0034     //     for (vector<T>::const_iterator i=theBorders.begin();
0035     //   i<theBorders.end(); ++i) {
0036     //      theBorders.push_back(((*i) + (*(i+1))) / 2.);
0037 
0038     //     cout << "GeneralBinFinderInR_ " << theNbins << " " << theBorders.size() << " " << (int) this << endl;
0039   }
0040 
0041   //   /// Construct from an already initialized RBorderFinder
0042   //   GeneralBinFinderInR(const RBorderFinder& bf) {
0043   //     theBorders=bf.RBorders();
0044   //     theBins=bf.RBins();
0045   //     theNbins=theBins.size();
0046   //   }
0047 
0048   //  /// Construct from the list of Det*
0049   //   GeneralBinFinderInR(vector<Det*>::const_iterator first,
0050   //              vector<Det*>::const_iterator last)
0051   //     : theNbins( last-first)
0052   //   {
0053   //     vector<Det*> dets(first,last);
0054   //     RBorderFinder bf(dets);
0055   //     theBorders=bf.phiBorders();
0056   //     theBins=bf.phiBins();
0057   //     theNbins=theBins.size();
0058   //   }
0059 
0060   /// Returns an index in the valid range for the bin that contains
0061   /// AND is closest to R
0062   int binIndex(T R) const override {
0063     int i;
0064     for (i = 0; i < theNbins; ++i) {
0065       if (R < theBorders[i]) {  // FIXME: one can be skipped?
0066         break;
0067       }
0068     }
0069     return binIndex(i - 1);
0070   }
0071 
0072   /// Returns an index in the valid range
0073   int binIndex(int i) const override { return std::min(std::max(i, 0), theNbins - 1); }
0074 
0075   /// The middle of the bin
0076   T binPosition(int ind) const override { return theBins[binIndex(ind)]; }
0077 
0078 private:
0079   int theNbins;
0080   std::vector<T> theBorders;
0081   std::vector<T> theBins;
0082 };
0083 #endif
0084 //----------------------------------------------------------------------
0085 
0086 //----------------------------------------------------------------------
0087 #ifndef GeneralBinFinderInZ_H
0088 #define GeneralBinFinderInZ_H
0089 
0090 /** \class MagBinFinders::GeneralBinFinderInZ
0091  * A Z bin finder for a non-periodic group of detectors.
0092  * Search is done starting from an initial equal-size-bin guess. Therefore
0093  * it becomes less efficient for binnings with very different bin size. 
0094  * It is not particularily suited for very few bins...
0095  */
0096 
0097 #include "Utilities/BinningTools/interface/BaseBinFinder.h"
0098 #include <cmath>
0099 
0100 template <class T>
0101 class MagBinFinders::GeneralBinFinderInZ : public BaseBinFinder<T> {
0102 public:
0103   GeneralBinFinderInZ() : theNbins(0), theZStep(0), theZOffset(0) {}
0104 
0105   GeneralBinFinderInZ(std::vector<T>& borders)
0106       : theNbins(borders.size()), theBorders(++(borders.begin()), borders.end()) {  // Skip border of 1. bin
0107     // FIXME:  crashes for borders.size()==1 (trivial case)
0108     // FIXME set theBins!!!
0109     theZOffset = theBorders.front();
0110     if (theNbins > 2) {
0111       theZStep = (theBorders.back() - theBorders.front()) / (theNbins - 2);
0112     } else {
0113       theZStep = 1.;  // Not relevant in this case...
0114     }
0115 
0116     //     cout << "GeneralBinFinderInZ " << theBorders.size()
0117     //   << " " << theZStep << endl;
0118   }
0119 
0120   // FIXME: ??? theNbins e theBorders hanno size theNbins -1.
0121   // theBorders[1] e' l'inizio del secondo bin !!!
0122 
0123   //   GeneralBinFinderInZ(vector<Det*>::const_iterator first,
0124   //              vector<Det*>::const_iterator last) :
0125   //     theNbins( last-first)
0126   //   {
0127   //     theBins.reserve(theNbins);
0128   //     for (vector<Det*>::const_iterator i=first; i<last-1; ++i) {
0129   //       theBins.push_back((**i).position().z());
0130   //       theBorders.push_back(((**i).position().z() +
0131   //                (**(i+1)).position().z()) / 2.);
0132   //     }
0133 
0134   //     theZOffset = theBorders.front();
0135   //     theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
0136   //   }
0137 
0138   /// returns an index in the valid range for the bin closest to Z
0139   int binIndex(T z) const override {
0140     int bin = binIndex(int((z - theZOffset) / theZStep) + 1);
0141 
0142     // check left border
0143     if (bin > 0) {
0144       if (z < theBorders[bin - 1]) {
0145         // z is to the left of the left border, the correct bin is left
0146         for (int i = bin - 1;; --i) {
0147           if (i <= 0)
0148             return 0;
0149           if (z > theBorders[i - 1])
0150             return i;
0151         }
0152       }
0153     } else
0154       return 0;
0155 
0156     // check right border
0157     if (bin < theNbins - 1) {
0158       if (z > theBorders[bin]) {
0159         // z is to the right of the right border, the correct bin is right
0160         for (int i = bin + 1;; ++i) {
0161           if (i >= theNbins - 1)
0162             return theNbins - 1;
0163           if (z < theBorders[i])
0164             return i;
0165         }
0166       }
0167     } else
0168       return theNbins - 1;
0169 
0170     // if we arrive here it means that the bin is ok
0171     return bin;
0172   }
0173 
0174   /// returns an index in the valid range
0175   int binIndex(int i) const override { return std::min(std::max(i, 0), theNbins - 1); }
0176 
0177   /// the middle of the bin.
0178   T binPosition(int ind) const override { return theBins[binIndex(ind)]; }
0179 
0180   static double pi() { return 3.141592653589793238; }
0181   static double twoPi() { return 2. * pi(); }
0182 
0183 private:
0184   int theNbins;
0185   T theZStep;
0186   T theZOffset;
0187   std::vector<T> theBorders;
0188   std::vector<T> theBins;
0189 };
0190 #endif
0191 //----------------------------------------------------------------------