00001 #ifndef PDSCALIBDATA_GLOBALMETHODS_H
00002 #define PDSCALIBDATA_GLOBALMETHODS_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <string>
00018 #include <fstream>
00019 #include <iomanip>
00020 #include <sstream>
00021 #include <iostream>
00022 #include <stdexcept>
00023 #include <typeinfo>
00024 #include <stdint.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "MsgLogger/MsgLogger.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 namespace pdscalibdata {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
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
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
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
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);
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
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
00125 DATA_TYPE enumDataTypeForString(std::string str_type);
00126
00127
00128
00129 std::string strDataTypeForEnum(DATA_TYPE enum_type);
00130
00131
00132
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
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
00162
00163
00164
00165
00166
00167
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
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
00182 T* it = pars;
00183 size_t count = 0;
00184 while(in and count != size) {
00185 in >> *it++;
00186 ++ count;
00187 }
00188
00189
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
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 }
00214
00215 #endif // PDSCALIBDATA_GLOBALMETHODS_H