Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:41

0001 #ifndef MU_END_ANALOG_SIGNAL
0002 #define MU_END_ANALOG_SIGNAL
0003 
0004 /** \class CSCAnalogSignal
0005  *  Simple histogram meant to represent the analog
0006  *  signal on a detector element.
0007  *
0008  * \author Rick Wilkinson
0009  *
0010  * Last mod: <BR>
0011  * 30-Jun-00 ptc Add further traps in getBinValue() and setBinValue(). <BR>
0012  * 06-Jul-00 ptc In fact the getBinValue trap was an important bug-fix:
0013  * it trapped on > size() of stl std::vector but should have trapped >= size().
0014  * It occasionally does reach size(). <BR>
0015  * <p>
0016  * Mods (performace improvements) by Vin  31/07/2000<br>
0017  *   Critical methods (getBinValue, get Value +=) inlined<br>
0018  *   bin-size stored and used as his inverse
0019  *   (encapulation helped in not changing interface, named changed to use
0020  * compiler to catch its occurrencies)<br> swap input std::vector (be careful if
0021  * const..)<br> do proper interpolation (not just /2)<br>
0022  *
0023  */
0024 
0025 #include <cassert>
0026 #include <iosfwd>
0027 #include <vector>
0028 
0029 // TODO remove
0030 #include <iostream>
0031 
0032 class CSCAnalogSignal {
0033 public:
0034   inline CSCAnalogSignal() : theElement(0), invBinSize(0.), theBinValues(0), theTotal(0), theTimeOffset(0.) {}
0035 
0036   inline CSCAnalogSignal(
0037       int element, float binSize, std::vector<float> &binValues, float total = 0., float timeOffset = 0.)
0038       : theElement(element), invBinSize(1. / binSize), theBinValues(), theTotal(total), theTimeOffset(timeOffset) {
0039     theBinValues.swap(binValues);
0040   }
0041 
0042   /// constructor from time and amp shape
0043   //  CSCAnalogSignal(int element, const CSCAnalogSignal& shape, float time,
0044   //  float total);
0045 
0046   inline int getElement() const { return theElement; };
0047   inline void setElement(int element) { theElement = element; };
0048   inline float getBinValue(int i) const {
0049     return (i >= static_cast<int>(theBinValues.size()) || i < 0) ? 0. : theBinValues[i];
0050   }
0051 
0052   inline float getValue(float t) const {
0053     // interpolate between bins, if necessary
0054     float retval = 0.;
0055     float f = (t - theTimeOffset) * invBinSize + 0.000000001;
0056     if (f >= 0.) {
0057       int i = static_cast<int>(f);
0058       f -= static_cast<float>(i);
0059       retval = (1. - f) * getBinValue(i) + f * getBinValue(i + 1);
0060     }
0061     return retval;
0062   }
0063 
0064   //  inline void  setBinValue(int i, float value) {
0065   //    if( i >= 0 && i < theBinValues.size() )
0066   //      theBinValues[i] = value;
0067   //  }
0068 
0069   inline int getSize() const { return theBinValues.size(); };
0070   inline float getBinSize() const { return 1. / invBinSize; };
0071   inline float getTotal() const { return theTotal; };
0072   inline float getTimeOffset() const { return theTimeOffset; };
0073   inline void setTimeOffset(float offset) { theTimeOffset = offset; };
0074 
0075   inline void superimpose(const CSCAnalogSignal &signal2) {
0076     size_t n = theBinValues.size();
0077     for (size_t i = 0; i < n; ++i) {
0078       float t = i / invBinSize + theTimeOffset;
0079       theBinValues[i] += signal2.getValue(t);
0080     }
0081     theTotal += signal2.theTotal;
0082   }
0083 
0084   inline void operator+=(float offset) {
0085     for (int i = 0; i < getSize(); ++i) {
0086       theBinValues[i] += offset;
0087     }
0088   }
0089 
0090   inline void operator*=(float scaleFactor) {
0091     for (int i = 0; i < getSize(); ++i) {
0092       theBinValues[i] *= scaleFactor;
0093     }
0094     theTotal *= scaleFactor;
0095   }
0096 
0097   friend std::ostream &operator<<(std::ostream &, const CSCAnalogSignal &);
0098 
0099   float &operator[](int i) {
0100     assert(i >= 0 && i < getSize());
0101     return theBinValues[i];
0102   }
0103 
0104   const float &operator[](int i) const {
0105     assert(i >= 0 && i < getSize());
0106     return theBinValues[i];
0107   }
0108 
0109   /// the time when the signal peaks
0110   float peakTime() const;
0111   unsigned size() const { return theBinValues.size(); }
0112 
0113 private:
0114   int theElement;
0115   float invBinSize;
0116   std::vector<float> theBinValues;
0117   float theTotal;
0118   float theTimeOffset;
0119 };
0120 
0121 #endif