00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "ImgAlgos/GlobalMethods.h"
00017
00018
00019
00020
00021 #include <boost/lexical_cast.hpp>
00022
00023
00024
00025
00026 #include <boost/shared_ptr.hpp>
00027 #include "PSEvt/EventId.h"
00028 #include "PSTime/Time.h"
00029
00030 #include "pdsdata/xtc/DetInfo.hh"
00031 #include "PSCalib/Exceptions.h"
00032
00033
00034
00035
00036
00037 using namespace std;
00038 using namespace ImgAlgos;
00039
00040
00041
00042
00043
00044 namespace ImgAlgos {
00045
00046
00047
00048
00049 NDArrPars::NDArrPars()
00050 : m_ndim(0)
00051 , m_size(0)
00052 , m_dtype(DOUBLE)
00053 , m_src()
00054 , m_is_set(false)
00055 {
00056
00057 for(unsigned i=0; i<5; ++i) m_shape[i] = 0;
00058 }
00059
00060 NDArrPars::NDArrPars(const unsigned ndim, const unsigned size, const unsigned* shape, const DATA_TYPE dtype, const Pds::Src& src)
00061 {
00062 setPars(ndim, size, shape, dtype, src);
00063 }
00064
00065 void
00066 NDArrPars::setPars(const unsigned ndim, const unsigned size, const unsigned* shape, const DATA_TYPE dtype, const Pds::Src& src)
00067 {
00068 m_ndim = ndim;
00069 m_size = size;
00070 m_dtype = dtype;
00071 m_src = src;
00072 m_is_set = true;
00073 for(unsigned i=0; i<m_ndim; ++i) m_shape[i] = shape[i];
00074 }
00075
00076 void
00077 NDArrPars::print()
00078 {
00079 WithMsgLog("NDArrPars::print():", info, log) {
00080 log << "\n NDArrPars parameters:"
00081 << "\n ndim : " << m_ndim
00082 << "\n size : " << m_size
00083 << "\n dtype : " << m_dtype
00084 << "\n src : " << ImgAlgos::srcToString(m_src)
00085 << "\n is_set : " << m_is_set
00086 << "\n shape : ";
00087 for(unsigned i=0; i<m_ndim; ++i) log << m_shape[i] << " ";
00088 log << "\n";
00089 }
00090 }
00091
00092
00093
00094
00095 std::string
00096 stringFromUint(unsigned number, unsigned width, char fillchar)
00097 {
00098 stringstream ssNum; ssNum << setw(width) << setfill(fillchar) << number;
00099 return ssNum.str();
00100 }
00101
00102
00103
00104 std::string
00105 stringTimeStamp(PSEvt::Event& evt, std::string fmt)
00106 {
00107 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00108 if (eventId.get()) return (eventId->time()).asStringFormat(fmt);
00109 else return std::string("time-stamp-is-unavailable");
00110 }
00111
00112
00113
00114 double
00115 doubleTime(PSEvt::Event& evt)
00116 {
00117 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00118 if (eventId.get()) return double(eventId->time().sec()) + 1e-9*eventId->time().nsec();
00119 else return double(0);
00120 }
00121
00122
00123
00124 std::string
00125 stringRunNumber(PSEvt::Event& evt, unsigned width)
00126 {
00127 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00128 if (eventId.get()) return stringFromUint(eventId->run(), width);
00129 else return std::string("run-is-not-defined");
00130 }
00131
00132
00133
00134
00135 int
00136 getRunNumber(PSEvt::Event& evt)
00137 {
00138 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00139 if (eventId.get()) {
00140 return eventId->run();
00141 } else {
00142 MsgLogRoot(warning, "Cannot determine run number, will use 0.");
00143 return int(0);
00144 }
00145 }
00146
00147
00148
00149 unsigned
00150 fiducials(PSEvt::Event& evt)
00151 {
00152 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00153 if (eventId.get()) return eventId->fiducials();
00154 else return 0;
00155 }
00156
00157
00158
00159 unsigned
00160 eventCounterSinceConfigure(PSEvt::Event& evt)
00161 {
00162 boost::shared_ptr<PSEvt::EventId> eventId = evt.get();
00163 if (eventId.get()) return eventId->vector();
00164 else return 0;
00165 }
00166
00167
00168
00169 void
00170 printSizeOfTypes()
00171 {
00172 std::cout << "Size Of Types:"
00173 << "\nsizeof(bool ) = " << sizeof(bool ) << " with typeid(bool ).name(): " << typeid(bool ).name()
00174 << "\nsizeof(int ) = " << sizeof(int ) << " with typeid(int ).name(): " << typeid(int ).name()
00175 << "\nsizeof(float ) = " << sizeof(float ) << " with typeid(float ).name(): " << typeid(float ).name()
00176 << "\nsizeof(double ) = " << sizeof(double ) << " with typeid(double ).name(): " << typeid(double ).name()
00177 << "\nsizeof(short ) = " << sizeof(short ) << " with typeid(short ).name(): " << typeid(short ).name()
00178 << "\nsizeof(uint8_t ) = " << sizeof(uint8_t ) << " with typeid(uint8_t ).name(): " << typeid(uint8_t ).name()
00179 << "\nsizeof(uint16_t) = " << sizeof(uint16_t) << " with typeid(uint16_t).name(): " << typeid(uint16_t).name()
00180 << "\nsizeof(uint32_t) = " << sizeof(uint32_t) << " with typeid(uint32_t).name(): " << typeid(uint32_t).name()
00181 << "\nsizeof(int16_t ) = " << sizeof(int16_t ) << " with typeid(int16_t ).name(): " << typeid(int16_t ).name()
00182 << "\nsizeof(int32_t ) = " << sizeof(int32_t ) << " with typeid(int32_t ).name(): " << typeid(int32_t ).name()
00183 << "\nsizeof(unsigned short) = " << sizeof(unsigned short) << " with typeid(unsigned short).name(): " << typeid(unsigned short).name()
00184 << "\n\n";
00185 }
00186
00187
00188
00189 bool
00190 defineImageShape(PSEvt::Event& evt, const PSEvt::Source& src, const std::string& key, unsigned* shape)
00191 {
00192 if ( defineImageShapeForType<double> (evt, src, key, shape) ) return true;
00193 if ( defineImageShapeForType<float> (evt, src, key, shape) ) return true;
00194 if ( defineImageShapeForType<int> (evt, src, key, shape) ) return true;
00195 if ( defineImageShapeForType<int32_t> (evt, src, key, shape) ) return true;
00196 if ( defineImageShapeForType<uint32_t>(evt, src, key, shape) ) return true;
00197 if ( defineImageShapeForType<uint16_t>(evt, src, key, shape) ) return true;
00198 if ( defineImageShapeForType<uint8_t> (evt, src, key, shape) ) return true;
00199 if ( defineImageShapeForType<int16_t> (evt, src, key, shape) ) return true;
00200 if ( defineImageShapeForType<short> (evt, src, key, shape) ) return true;
00201
00202
00203 static long counter = 0; counter ++;
00204
00205 if (counter < 11) {
00206 const std::string msg = "Image shape is tested for double, uint16_t, int, float, uint8_t, int16_t, short and is not defined in the event(...)\nfor source:"
00207 + boost::lexical_cast<std::string>(src) + " key:" + key;
00208 MsgLog("GlobalMethods::defineImageShape", warning, msg);
00209 if (counter == 10) MsgLog("GlobalMethods::defineImageShape", warning, "STOP PRINT WARNINGS for source:"
00210 << boost::lexical_cast<std::string>(src) << " key:" << key);
00211
00212 }
00213 return false;
00214 }
00215
00216
00217
00218
00219
00220 bool
00221 defineNDArrPars(PSEvt::Event& evt, const PSEvt::Source& src, const std::string& key, NDArrPars* ndarr_pars, bool print_wng)
00222 {
00223 if ( defineNDArrParsForType<double> (evt, src, key, DOUBLE, ndarr_pars) ) return true;
00224 if ( defineNDArrParsForType<float> (evt, src, key, FLOAT, ndarr_pars) ) return true;
00225 if ( defineNDArrParsForType<int> (evt, src, key, INT, ndarr_pars) ) return true;
00226 if ( defineNDArrParsForType<int32_t> (evt, src, key, INT32, ndarr_pars) ) return true;
00227 if ( defineNDArrParsForType<uint32_t>(evt, src, key, UINT32, ndarr_pars) ) return true;
00228 if ( defineNDArrParsForType<uint16_t>(evt, src, key, UINT16, ndarr_pars) ) return true;
00229 if ( defineNDArrParsForType<uint8_t> (evt, src, key, UINT8, ndarr_pars) ) return true;
00230 if ( defineNDArrParsForType<int16_t> (evt, src, key, INT16, ndarr_pars) ) return true;
00231 if ( defineNDArrParsForType<short> (evt, src, key, SHORT, ndarr_pars) ) return true;
00232 if ( defineNDArrParsForType<unsigned>(evt, src, key, UNSIGNED, ndarr_pars) ) return true;
00233
00234 static long counter = 0; counter ++;
00235
00236 if (print_wng && counter < 11) {
00237 const std::string msg = "ndarray shape is tested for double, uint16_t, int, float, uint8_t, int16_t, short and is not defined in the event(...)\nfor source:"
00238 + boost::lexical_cast<std::string>(src) + " key:" + key;
00239 MsgLog("GlobalMethods::defineNDArrPars", warning, msg);
00240 if (counter == 10)
00241 MsgLog("GlobalMethods::defineNDArrPars", warning, "STOP PRINT WARNINGS for source:"
00242 << boost::lexical_cast<std::string>(src) << " key:" << key);
00243
00244 }
00245
00246 return false;
00247 }
00248
00249
00250
00251 void
00252 saveTextInFile(const std::string& fname, const std::string& text, bool print_msg)
00253 {
00254 std::ofstream out(fname.c_str());
00255 out << text;
00256 out.close();
00257
00258
00259 if( print_msg ) MsgLog("GlobalMethods", info, "Save text in file " << fname.c_str());
00260 }
00261
00262
00263
00264 std::string
00265 stringInstrument(PSEnv::Env& env)
00266 {
00267 return env.instrument();
00268 }
00269
00270
00271
00272 std::string
00273 stringExperiment(PSEnv::Env& env)
00274 {
00275 return env.experiment();
00276 }
00277
00278
00279
00280 unsigned
00281 expNum(PSEnv::Env& env)
00282 {
00283 return env.expNum();
00284 }
00285
00286
00287
00288 std::string
00289 stringExpNum(PSEnv::Env& env, unsigned width)
00290 {
00291 return stringFromUint(env.expNum(), width);
00292 }
00293
00294
00295
00296 bool
00297 file_exists(std::string& fname)
00298 {
00299 std::ifstream f(fname.c_str());
00300 return f;
00301 }
00302
00303
00304
00305 std::string srcToString( const Pds::Src& src )
00306 {
00307 if ( src.level() != Pds::Level::Source ) {
00308 throw PSCalib::NotDetInfoError(ERR_LOC);
00309 }
00310
00311 const Pds::DetInfo& info = static_cast<const Pds::DetInfo&>( src ) ;
00312 std::ostringstream str ;
00313 str << Pds::DetInfo::name(info.detector()) << '.' << info.detId()
00314 << ':' << Pds::DetInfo::name(info.device()) << '.' << info.devId() ;
00315 return str.str() ;
00316 }
00317
00318
00319
00320 DETECTOR_TYPE detectorTypeForStringSource(const std::string& str_src)
00321 {
00322
00323
00324 if ( str_src.find("Cspad2x2") != std::string::npos ) return CSPAD2X2;
00325 else if ( str_src.find("Cspad") != std::string::npos ) return CSPAD;
00326 else if ( str_src.find("pnCCD") != std::string::npos ) return PNCCD;
00327 else if ( str_src.find("Princeton") != std::string::npos ) return PRINCETON;
00328 else if ( str_src.find("Acqiris") != std::string::npos ) return ACQIRIS;
00329 else if ( str_src.find("Tm6740") != std::string::npos ) return TM6740;
00330 else if ( str_src.find("Opal1000") != std::string::npos ) return OPAL1000;
00331 else if ( str_src.find("Opal2000") != std::string::npos ) return OPAL2000;
00332 else if ( str_src.find("Opal4000") != std::string::npos ) return OPAL4000;
00333 else if ( str_src.find("Opal8000") != std::string::npos ) return OPAL8000;
00334 else if ( str_src.find("Andor") != std::string::npos ) return ANDOR;
00335 else if ( str_src.find("OrcaFl40") != std::string::npos ) return ORCAFL40;
00336 else if ( str_src.find("Fccd960") != std::string::npos ) return FCCD960;
00337 else if ( str_src.find("Epix100a") != std::string::npos ) return EPIX100A;
00338 else if ( str_src.find("Epix10k") != std::string::npos ) return EPIX10K;
00339 else if ( str_src.find("Epix") != std::string::npos ) return EPIX;
00340 else if ( str_src.find("Quartz4A150") != std::string::npos ) return QUARTZ4A150;
00341 else if ( str_src.find("Rayonix") != std::string::npos ) return RAYONIX;
00342 else if ( str_src.find("Imp") != std::string::npos ) return IMP;
00343 else if ( str_src.find("Evr") != std::string::npos ) return EVR;
00344 else if ( str_src.find("Fccd") != std::string::npos ) return FCCD;
00345 else if ( str_src.find("Timepix") != std::string::npos ) return TIMEPIX;
00346 else if ( str_src.find("Fli") != std::string::npos ) return FLI;
00347 else if ( str_src.find("Pimax") != std::string::npos ) return PIMAX;
00348 else return OTHER;
00349 }
00350
00351
00352
00353 DETECTOR_TYPE detectorTypeForSource(const PSEvt::Source& src)
00354 {
00355 std::stringstream ss; ss << src;
00356 return detectorTypeForStringSource(ss.str());
00357 }
00358
00359
00360
00361 std::string calibGroupForDetType(const DETECTOR_TYPE det_type)
00362 {
00363 if ( det_type == CSPAD ) return "CsPad::CalibV1";
00364 else if ( det_type == CSPAD2X2 ) return "CsPad2x2::CalibV1";
00365 else if ( det_type == PNCCD ) return "PNCCD::CalibV1";
00366 else if ( det_type == PRINCETON ) return "Princeton::CalibV1";
00367 else if ( det_type == ACQIRIS ) return "Acqiris::CalibV1";
00368 else if ( det_type == TM6740 ) return "Camera::CalibV1";
00369 else if ( det_type == OPAL1000 ) return "Camera::CalibV1";
00370 else if ( det_type == OPAL2000 ) return "Camera::CalibV1";
00371 else if ( det_type == OPAL4000 ) return "Camera::CalibV1";
00372 else if ( det_type == OPAL8000 ) return "Camera::CalibV1";
00373 else if ( det_type == ANDOR ) return "Andor::CalibV1";
00374 else if ( det_type == ORCAFL40 ) return "Camera::CalibV1";
00375 else if ( det_type == FCCD960 ) return "Camera::CalibV1";
00376 else if ( det_type == EPIX ) return "Epix::CalibV1";
00377 else if ( det_type == EPIX100A ) return "Epix100a::CalibV1";
00378 else if ( det_type == EPIX10K ) return "Epix10k::CalibV1";
00379 else if ( det_type == QUARTZ4A150)return "Camera::CalibV1";
00380 else if ( det_type == RAYONIX ) return "Camera::CalibV1";
00381 else if ( det_type == IMP ) return "Imp::CalibV1";
00382 else if ( det_type == EVR ) return "Evr::CalibV1";
00383 else if ( det_type == FCCD ) return "Camera::CalibV1";
00384 else if ( det_type == TIMEPIX ) return "Timepix::CalibV1";
00385 else if ( det_type == FLI ) return "Fli::CalibV1";
00386 else if ( det_type == PIMAX ) return "Pimax::CalibV1";
00387 else return std::string();
00388 }
00389
00390
00391
00392 std::string stringForDetType(const DETECTOR_TYPE det_type)
00393 {
00394 if ( det_type == CSPAD ) return "CSPAD";
00395 else if ( det_type == CSPAD2X2 ) return "CSPAD2x2";
00396 else if ( det_type == PNCCD ) return "PNCCD";
00397 else if ( det_type == PRINCETON ) return "Princeton";
00398 else if ( det_type == ACQIRIS ) return "Acqiris";
00399 else if ( det_type == TM6740 ) return "Camera-TM6740";
00400 else if ( det_type == OPAL1000 ) return "Camera-OPAL1000";
00401 else if ( det_type == OPAL2000 ) return "Camera-OPAL2000";
00402 else if ( det_type == OPAL4000 ) return "Camera-OPAL4000";
00403 else if ( det_type == OPAL8000 ) return "Camera-OPAL5000";
00404 else if ( det_type == ANDOR ) return "Andor";
00405 else if ( det_type == ORCAFL40 ) return "Camera-ORCAFL40";
00406 else if ( det_type == FCCD960 ) return "Camera-FCCD960";
00407 else if ( det_type == EPIX ) return "Epix";
00408 else if ( det_type == EPIX100A ) return "Epix100a";
00409 else if ( det_type == EPIX10K ) return "Epix10k";
00410 else if ( det_type == QUARTZ4A150)return "Quartz4A150";
00411 else if ( det_type == RAYONIX ) return "Rayonix";
00412 else if ( det_type == IMP ) return "Imp";
00413 else if ( det_type == EVR ) return "Evr";
00414 else if ( det_type == FCCD ) return "Fccd";
00415 else if ( det_type == TIMEPIX ) return "Timepix";
00416 else if ( det_type == FLI ) return "Fli";
00417 else if ( det_type == PIMAX ) return "Pimax";
00418 else return std::string();
00419 }
00420
00421
00422
00423 std::string calibGroupForSource(PSEvt::Source& src)
00424 {
00425 DETECTOR_TYPE det_type = detectorTypeForSource(src);
00426 return calibGroupForDetType(det_type);
00427 }
00428
00429
00430
00431 std::string
00432 split_string_left(const std::string& s, size_t& pos, const char& sep)
00433 {
00434 size_t p0 = pos;
00435 size_t p1 = s.find(sep, p0);
00436 size_t nchars = p1-p0;
00437 pos = p1+1;
00438
00439 if (p1 != std::string::npos) return std::string(s,p0,nchars);
00440 else if (p0 < s.size()) return std::string(s,p0);
00441 else return std::string();
00442 }
00443
00444
00445
00446 std::string strDataType(const DATA_TYPE& dtype)
00447 {
00448 if ( dtype == DOUBLE ) return "DOUBLE";
00449 else if ( dtype == FLOAT ) return "FLOAT";
00450 else if ( dtype == INT ) return "INT";
00451 else if ( dtype == INT32 ) return "INT32";
00452 else if ( dtype == UINT32 ) return "UINT32";
00453 else if ( dtype == UINT16 ) return "UINT16";
00454 else if ( dtype == UINT8 ) return "UINT8";
00455 else if ( dtype == INT16 ) return "INT16";
00456 else if ( dtype == SHORT ) return "SHORT";
00457 else if ( dtype == UNSIGNED) return "UNSIGNED";
00458 else return "NONDEFDT";
00459 }
00460
00461
00462
00463
00464
00465 }