pdscalibdata/src/GlobalMethods.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class GlobalMethods...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "pdscalibdata/GlobalMethods.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <cmath> // for sqrt, atan2, etc.
00022 #include <time.h>
00023 #include <stdlib.h>     // getenv
00024 
00025 //-------------------------------
00026 // Collaborating Class Headers --
00027 //-------------------------------
00028 
00029 //-----------------------------------------------------------------------
00030 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00031 //-----------------------------------------------------------------------
00032 
00033 using namespace std;
00034 using namespace pdscalibdata;
00035 
00036 //              ----------------------------------------
00037 //              -- Public Function Member Definitions --
00038 //              ----------------------------------------
00039 
00040 namespace pdscalibdata {
00041 
00042 //----------------
00043 // Constructors --
00044 //----------------
00045 GlobalMethods::GlobalMethods ()
00046 {
00047 }
00048 
00049 //--------------
00050 // Destructor --
00051 //--------------
00052 GlobalMethods::~GlobalMethods ()
00053 {
00054 }
00055 
00056 //--------------------
00057 
00058 std::string 
00059 stringFromUint(unsigned number, unsigned width, char fillchar)
00060 {
00061   stringstream ssNum; ssNum << setw(width) << setfill(fillchar) << number;
00062   return ssNum.str();
00063 }
00064 
00065 //--------------------
00066 
00067 void 
00068 printSizeOfTypes()
00069 {
00070   std::cout << "Size Of Types:" 
00071             << "\nsizeof(bool    ) = " << sizeof(bool    ) << " with typeid(bool    ).name(): " << typeid(bool    ).name() 
00072             << "\nsizeof(uint8_t ) = " << sizeof(uint8_t ) << " with typeid(uint8_t ).name(): " << typeid(uint8_t ).name()  
00073             << "\nsizeof(uint16_t) = " << sizeof(uint16_t) << " with typeid(uint16_t).name(): " << typeid(uint16_t).name()  
00074             << "\nsizeof(uint32_t) = " << sizeof(uint32_t) << " with typeid(uint32_t).name(): " << typeid(uint32_t).name()  
00075             << "\nsizeof(int     ) = " << sizeof(int     ) << " with typeid(int     ).name(): " << typeid(int     ).name()  
00076             << "\nsizeof(int16_t ) = " << sizeof(int16_t ) << " with typeid(int16_t ).name(): " << typeid(int16_t ).name()  
00077             << "\nsizeof(int32_t ) = " << sizeof(int32_t ) << " with typeid(int32_t ).name(): " << typeid(int32_t ).name()  
00078             << "\nsizeof(float   ) = " << sizeof(float   ) << " with typeid(float   ).name(): " << typeid(float   ).name()  
00079             << "\nsizeof(double  ) = " << sizeof(double  ) << " with typeid(double  ).name(): " << typeid(double  ).name()  
00080             << "\n\n";
00081 }
00082 
00083 
00084 //--------------------
00085 
00086 DATA_TYPE 
00087 enumDataTypeForString(std::string str_type)
00088 {
00089     if      (str_type == "float"   ) return FLOAT;   
00090     else if (str_type == "double"  ) return DOUBLE;  
00091     else if (str_type == "short"   ) return SHORT;   
00092     else if (str_type == "unsigned") return UNSIGNED;
00093     else if (str_type == "int"     ) return INT;     
00094     else if (str_type == "int16_t" ) return INT16;   
00095     else if (str_type == "int32_t" ) return INT32;   
00096     else if (str_type == "uint"    ) return UINT;    
00097     else if (str_type == "uint8_t" ) return UINT8;   
00098     else if (str_type == "uint16_t") return UINT16;  
00099     else if (str_type == "uint32_t") return UINT32;  
00100     else return NONIMPL;
00101 }
00102 
00103 //--------------------
00104 
00105 std::string strDataTypeForEnum(DATA_TYPE enum_type)
00106 {
00107     if      (enum_type == FLOAT   ) return std::string("float");  
00108     else if (enum_type == DOUBLE  ) return std::string("double"); 
00109     else if (enum_type == SHORT   ) return std::string("short");  
00110     else if (enum_type == UNSIGNED) return std::string("unsigned");
00111     else if (enum_type == INT     ) return std::string("int");    
00112     else if (enum_type == INT16   ) return std::string("int16_t");
00113     else if (enum_type == INT32   ) return std::string("int32_t");
00114     else if (enum_type == UINT    ) return std::string("unsigned");   
00115     else if (enum_type == UINT8   ) return std::string("uint8_t");
00116     else if (enum_type == UINT16  ) return std::string("uint16_t");
00117     else if (enum_type == UINT32  ) return std::string("uint32_t");
00118     else return std::string("non-implemented");
00119 }
00120 
00121 //--------------------
00122 
00123 float 
00124 findCommonMode(const double* pars,
00125                const int16_t* sdata,
00126                const float* peddata, 
00127                const uint16_t *pixStatus, 
00128                unsigned ssize,
00129                int stride
00130                )
00131 {
00132   // do we even need it
00133   //if (m_mode == None) return float(UnknownCM);
00134 
00135   // for now it does not make sense to calculate common mode
00136   // if pedestals are not known
00137   if (not peddata) return float(UnknownCM);
00138   
00139   // declare array for histogram
00140   const int low = -1000;
00141   const int high = 2000;
00142   const unsigned hsize = high-low;
00143   int hist[hsize];
00144   std::fill_n(hist, hsize, 0);
00145   unsigned long count = 0;
00146   
00147   // fill histogram
00148   for (unsigned c = 0, p = 0; c != ssize; ++ c, p += stride) {
00149     
00150     // ignore channels that re too noisy
00151     if (pixStatus and (pixStatus[p] & 1)) continue;
00152     
00153     // pixel value with pedestal subtracted, rounded to integer
00154     double dval = sdata[p] - peddata[p];
00155     int val = dval < 0 ? int(dval - 0.5) : int(dval + 0.5);
00156     
00157     // histogram bin
00158     unsigned bin = unsigned(val - low);
00159     
00160     // increment bin value if in range
00161     if (bin < hsize) {
00162       ++hist[bin] ;
00163       ++ count;
00164     }
00165       
00166   }
00167 
00168   MsgLog("findCommonMode", debug, "histo filled count = " << count);
00169   
00170   // analyze histogram now, first find peak position
00171   // as the position of the lowest bin with highest count 
00172   // larger than 100 and which has a bin somewhere on 
00173   // right side with count dropping by half
00174   int peakPos = -1;
00175   int peakCount = -1;
00176   int hmRight = hsize;
00177   int thresh = int(pars[2]);
00178   if(thresh<=0) thresh=100;
00179   for (unsigned i = 0; i < hsize; ++ i ) {
00180     if (hist[i] > peakCount and hist[i] > thresh) {
00181       peakPos = i;
00182       peakCount = hist[i];
00183     } else if (peakCount > 0 and hist[i] <= peakCount/2) {
00184       hmRight = i;
00185       break;
00186     }
00187   }
00188 
00189   // did we find anything resembling
00190   if (peakPos < 0) {
00191     MsgLog("findCommonMode", debug, "peakPos = " << peakPos);
00192     return float(UnknownCM);
00193   }
00194 
00195   // find half maximum channel on left side
00196   int hmLeft = -1;
00197   for (int i = peakPos; hmLeft < 0 and i >= 0; -- i) {
00198     if(hist[i] <= peakCount/2) hmLeft = i;
00199   }
00200   MsgLog("findCommonMode", debug, "peakPos = " << peakPos << " peakCount = " << peakCount 
00201       << " hmLeft = " << hmLeft << " hmRight = " << hmRight);
00202   
00203   // full width at half maximum
00204   int fwhm = hmRight - hmLeft;
00205   double sigma = fwhm / 2.36;
00206 
00207   // calculate mean and sigma
00208   double mean = peakPos;
00209   for (int j = 0; j < 2; ++j) {
00210     int s0 = 0;
00211     double s1 = 0;
00212     double s2 = 0;
00213     int d = int(sigma*2+0.5);
00214     for (int i = std::max(0,peakPos-d); i < int(hsize) and i <= peakPos+d; ++ i) {
00215       s0 += hist[i];
00216       s1 += (i-mean)*hist[i];
00217       s2 += (i-mean)*(i-mean)*hist[i];
00218     }
00219     mean = mean + s1/s0;
00220     sigma = std::sqrt(s2/s0 - (s1/s0)*(s1/s0));
00221   }
00222   mean += low;
00223   
00224   MsgLog("findCommonMode", debug, "mean = " << mean << " sigma = " << sigma);
00225 
00226   // limit the values to some reasonable numbers
00227   if (mean > pars[0] or sigma > pars[1]) return float(UnknownCM);
00228   
00229   return mean;
00230 }
00231 
00232 //--------------------
00233 
00234 std::string strTimeStamp(const std::string& format)
00235 {
00236   time_t  time_sec;
00237   time ( &time_sec );
00238   struct tm* timeinfo; timeinfo = localtime ( &time_sec );
00239   char c_time_buf[32]; strftime(c_time_buf, 32, format.c_str(), timeinfo);
00240   return std::string (c_time_buf);
00241 }
00242 
00243 //--------------------
00244 
00245 std::string strEnvVar(const std::string& str)
00246 {
00247   char* var; var = getenv (str.c_str());
00248   if (var!=NULL) return std::string (var);
00249   else           return str + " IS NOT DEFINED!";
00250 }
00251 
00252 
00253 //--------------------
00254 //--------------------
00255 //--------------------
00256 } // namespace pdscalibdata

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7