pdscalibdata/include/GlobalMethods.h

Go to the documentation of this file.
00001 #ifndef PDSCALIBDATA_GLOBALMETHODS_H
00002 #define PDSCALIBDATA_GLOBALMETHODS_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: GlobalMethods.h 9242 2014-11-12 00:18:42Z dubrovin@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class GlobalMethods.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 
00017 #include <string>
00018 #include <fstream>   // ofstream
00019 #include <iomanip>   // for setw, setfill
00020 #include <sstream>   // for stringstream
00021 #include <iostream>
00022 #include <stdexcept>
00023 #include <typeinfo>  // for typeid
00024 #include <stdint.h>    // for uint8_t, uint16_t etc.
00025 //#include <algorithm>
00026 
00027 //----------------------
00028 // Base Class Headers --
00029 //----------------------
00030 
00031 //-------------------------------
00032 // Collaborating Class Headers --
00033 //-------------------------------
00034 
00035 #include "MsgLogger/MsgLogger.h"
00036 
00037 //------------------------------------
00038 // Collaborating Class Declarations --
00039 //------------------------------------
00040 
00041 //              ---------------------
00042 //              -- Class Interface --
00043 //              ---------------------
00044 
00045 namespace pdscalibdata {
00046 
00047 /// @addtogroup pdscalibdata
00048 
00049 /**
00050  *  @ingroup pdscalibdata
00051  *
00052  *  @brief Global methods for pdscalibdata package
00053  *
00054  *  This software was developed for the LCLS project.  If you use all or 
00055  *  part of it, please give an appropriate acknowledgment.
00056  *
00057  *  @version $Id: GlobalMethods.h 9242 2014-11-12 00:18:42Z dubrovin@SLAC.STANFORD.EDU $
00058  *
00059  *  @author Mikhail S. Dubrovin
00060  */
00061 
00062 using namespace std;
00063  
00064 enum DATA_TYPE {FLOAT, DOUBLE, SHORT, UNSIGNED, INT, INT16, INT32, UINT, UINT8, UINT16, UINT32, NONIMPL};
00065 
00066 const static int UnknownCM = -10000; 
00067 
00068 class GlobalMethods  {
00069 public:
00070   GlobalMethods () ;
00071   virtual ~GlobalMethods () ;
00072 
00073 private:
00074   // Copy constructor and assignment are disabled by default
00075   GlobalMethods ( const GlobalMethods& ) ;
00076   GlobalMethods& operator = ( const GlobalMethods& ) ;
00077 };
00078 
00079 //--------------------
00080 
00081   std::string stringFromUint(unsigned number, unsigned width=6, char fillchar='0');
00082 
00083   void printSizeOfTypes();
00084 
00085 //--------------------
00086 
00087   /**
00088    *  Find common mode for an CsPad  section.
00089    *  
00090    *  Function will return UnknownCM value if the calculation 
00091    *  cannot be performed (or need not be performed).
00092    *  
00093    *  @param pars   array[3] of control parameters
00094    *  @param sdata  pixel data
00095    *  @param peddata  pedestal data, can be zero pointer
00096    *  @param pixStatus  pixel status data, can be zero pointer
00097    *  @param ssize  size of all above arrays
00098    *  @param stride increment for pixel indices
00099    */ 
00100   float findCommonMode(const double* pars,
00101                        const int16_t* sdata,
00102                        const float* peddata, 
00103                        const  uint16_t *pixStatus, 
00104                        unsigned ssize,
00105                        int stride = 1); // const;
00106 
00107 //--------------------
00108 
00109   std::string strTimeStamp(const std::string& format=std::string("%Y-%m-%d %H:%M:%S"));
00110 
00111   std::string strEnvVar(const std::string& str=std::string("LOGNAME"));  
00112 
00113 //--------------------
00114 // For type=T returns the string with symbolic data type and its size, i.e. "d of size 8"
00115   template <typename T>
00116   std::string strOfDataTypeAndSize()
00117   {
00118     std::stringstream ss; ss << typeid(T).name() << " of size " << sizeof(T);
00119     return ss.str();
00120   }
00121 
00122 //--------------------
00123 
00124 // Returns enumerated type for string type name
00125   DATA_TYPE enumDataTypeForString(std::string str_type);
00126 
00127 //--------------------
00128 // Returns string name for enumerated data type 
00129   std::string strDataTypeForEnum(DATA_TYPE enum_type);
00130 
00131 //--------------------
00132 // For type=T returns enumerated data type
00133   template <typename T>
00134   DATA_TYPE enumDataType()
00135   {
00136     const char* s = typeid(T).name();
00137     if      (s == typeid(float)   .name()) return FLOAT;
00138     else if (s == typeid(double)  .name()) return DOUBLE; 
00139     else if (s == typeid(short)   .name()) return SHORT; 
00140     else if (s == typeid(unsigned).name()) return UNSIGNED; 
00141     else if (s == typeid(int)     .name()) return INT; 
00142     else if (s == typeid(int16_t) .name()) return INT16; 
00143     else if (s == typeid(int32_t) .name()) return INT32; 
00144     else if (s == typeid(uint8_t) .name()) return UINT8; 
00145     else if (s == typeid(uint16_t).name()) return UINT16; 
00146     else if (s == typeid(uint32_t).name()) return UINT32; 
00147     else return NONIMPL;
00148   }
00149 
00150 //--------------------
00151 // For type=T returns string name for data type
00152   template <typename T>
00153   std::string strDataType()
00154   {
00155     DATA_TYPE enum_type = enumDataType<T>();
00156     return strDataTypeForEnum(enum_type);
00157   }
00158 
00159 //--------------------
00160   /**
00161    * @brief Load parameters from file
00162    * 
00163    * @param[in]  fname - path to the file with parameters
00164    * @param[in]  comment - comment to print with messages 
00165    * @param[in]  size  - size of array with parameters; number of values to load from file
00166    * @param[out] pars  - pointer to array with parameters
00167    * @param[in]  check_bits - +1: check if input does not have enough data, +2: check if file has extra data 
00168    */
00169 
00170   template <typename T>
00171   void load_pars_from_file(const std::string& fname, const std::string& comment, const size_t size, T* pars, const unsigned check_bits=255)
00172   { 
00173     // open file
00174     std::ifstream in(fname.c_str());
00175     if (not in.good()) {
00176       const std::string msg = "Failed to open pedestals file: "+fname;
00177       MsgLogRoot(error, msg);
00178       throw std::runtime_error(msg);
00179     }
00180   
00181     // read all numbers
00182     T* it = pars;
00183     size_t count = 0;
00184     while(in and count != size) {
00185       in >> *it++;
00186       ++ count;
00187     }
00188   
00189     // check that we read whole array
00190     if (check_bits & 1 && count < size) {
00191       const std::string msg = comment+" file does not have enough data: "+fname;
00192       MsgLogRoot(error, msg);
00193       throw std::runtime_error(msg);
00194     }
00195   
00196     // and no data left after we finished reading
00197     float tmp ;
00198     if (check_bits & 2 && in >> tmp ) {
00199       ++ count;
00200       const std::string msg = comment+" file has extra data: "+fname;
00201       MsgLogRoot(error, msg);
00202       MsgLogRoot(error, "read " << count << " numbers, expecting " << size );
00203       throw std::runtime_error(msg);
00204     }
00205   }
00206 
00207 //--------------------
00208 //--------------------
00209 //--------------------
00210 //--------------------
00211 //--------------------
00212 
00213 } // namespace pdscalibdata
00214 
00215 #endif // PDSCALIBDATA_GLOBALMETHODS_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7