CSPadPixCoords/include/GlobalMethods.h

Go to the documentation of this file.
00001 #ifndef CSPADPIXCOORDS_GLOBALMETHODS_H
00002 #define CSPADPIXCOORDS_GLOBALMETHODS_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: GlobalMethods.h 9169 2014-10-28 00:37:53Z 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 
00023 //----------------------
00024 // Base Class Headers --
00025 //----------------------
00026 
00027 //-------------------------------
00028 // Collaborating Class Headers --
00029 //-------------------------------
00030 
00031 #include "PSEvt/Event.h"
00032 #include "PSEnv/Env.h"
00033 #include "PSEvt/Source.h"
00034 #include "MsgLogger/MsgLogger.h"
00035 #include "CSPadPixCoords/Image2D.h"
00036 
00037 //------------------------------------
00038 // Collaborating Class Declarations --
00039 //------------------------------------
00040 
00041 //              ---------------------
00042 //              -- Class Interface --
00043 //              ---------------------
00044 
00045 namespace CSPadPixCoords {
00046 
00047 /// @addtogroup CSPadPixCoords
00048 
00049 /**
00050  *  @ingroup CSPadPixCoords
00051  *
00052  *  @brief Global methods for CSPadPixCoords 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 9169 2014-10-28 00:37:53Z dubrovin@SLAC.STANFORD.EDU $
00058  *
00059  *  @author Mikhail S. Dubrovin
00060  */
00061 
00062 using namespace std;
00063 
00064 
00065 enum DATA_TYPE {ASDATA, FLOAT, DOUBLE, SHORT, UNSIGNED, INT, INT16, INT32, UINT, UINT8, UINT16, UINT32};
00066 
00067 enum FILE_MODE {BINARY, TEXT, TIFF, PNG, METADTEXT};
00068 
00069 
00070 class GlobalMethods  {
00071 public:
00072   GlobalMethods () ;
00073   virtual ~GlobalMethods () ;
00074 
00075 private:
00076   // Copy constructor and assignment are disabled by default
00077   GlobalMethods ( const GlobalMethods& ) ;
00078   GlobalMethods& operator = ( const GlobalMethods& ) ;
00079 };
00080 
00081 //--------------------
00082 
00083   std::string stringFromUint(unsigned number, unsigned width=6, char fillchar='0');
00084   std::string stringTimeStamp(PSEvt::Event& evt, std::string fmt="%Y%m%d-%H%M%S%f"); //("%Y-%m-%d %H:%M:%S%f%z");
00085   std::string stringRunNumber(PSEvt::Event& evt, unsigned width=4);
00086   int getRunNumber(PSEvt::Event& evt);
00087   double doubleTime(PSEvt::Event& evt);
00088   unsigned fiducials(PSEvt::Event& evt);                  // returns 17-bits (131071) integer value: fiducials clock runs at 360Hz.
00089   unsigned eventCounterSinceConfigure(PSEvt::Event& evt); // returns 15-bits (32767)  integer value: event counter since Configure.
00090   void printSizeOfTypes();
00091   /// Define the shape or throw message that can not do that.
00092   void defineImageShape(PSEvt::Event& evt, const PSEvt::Source& m_str_src, const std::string& m_key, unsigned* shape);
00093   void printTimeStamp(PSEvt::Event& evt, int counter);
00094 
00095 //--------------------
00096 //--------------------
00097 //--------------------
00098 //--------------------
00099 
00100 //--------------------
00101 // For type=T returns the string with symbolic data type and its size, i.e. "d of size 8"
00102   template <typename T>
00103   std::string strOfDataTypeAndSize()
00104   {
00105     std::stringstream ss; ss << typeid(T).name() << " of size " << sizeof(T);
00106     return ss.str();
00107   }
00108 
00109 //--------------------
00110 
00111   template <typename T>
00112     bool isSupportedDataType()
00113     {
00114         std::cout <<  "Input data type: " << strOfDataTypeAndSize<T>() << std::endl;
00115         if ( *typeid(T).name() != 't') {
00116           cout <<  "Sorry, but saving images in PNG works for uint16_t data only..." << endl;
00117           return false;
00118         }
00119         return true;
00120     }
00121 
00122 //--------------------
00123 // Define inage shape in the event for specified type, str_src, and str_key 
00124   template <typename T, unsigned NDIM>
00125   std::string str_shape( const ndarray<const T,NDIM>& nda ) 
00126   {
00127     std::stringstream ss;
00128     const unsigned* shape = nda.shape();
00129     for (unsigned i=0; i<NDIM; ++i)
00130       ss << shape[i] << " ";
00131     return ss.str();
00132   } 
00133 
00134 //--------------------
00135 // Define inage shape in the event for specified type, str_src, and str_key 
00136   template <typename T>
00137   bool defineImageShapeForType(PSEvt::Event& evt, const PSEvt::Source& str_src, const std::string& str_key, unsigned* shape)
00138   {
00139     boost::shared_ptr< ndarray<const T,2> > img = evt.get(str_src, str_key);
00140     if (img.get()) {
00141       for(int i=0;i<2;i++) shape[i]=img->shape()[i];
00142       //shape=img->shape();
00143       return true;
00144     } 
00145     return false;
00146   }
00147 
00148 //--------------------
00149 // Save 2-D array in environment
00150   template <typename T>
00151   void save2DArrayInEnv(PSEnv::Env& env, const Pds::Src& src, const ndarray<const T,2>& ndarr)
00152   {
00153     boost::shared_ptr< ndarray<const T,2> > shp( new ndarray<const T,2>(ndarr) );
00154     env.calibStore().put(shp, src);
00155   }
00156 
00157 //--------------------
00158 // Save 3-D array in environment
00159   template <typename T>
00160   void save3DArrayInEnv(PSEnv::Env& env, const Pds::Src& src, const std::string& key, const ndarray<const T,3>& ndarr)
00161   {
00162     boost::shared_ptr< ndarray<const T,3> > shp( new ndarray<const T,3>(ndarr) );
00163     env.calibStore().put(shp, src, key);
00164   }
00165 
00166 //--------------------
00167 // Save 2-D array in event
00168   template <typename T>
00169   void save2DArrayInEvent(PSEvt::Event& evt, const Pds::Src& src, const std::string& key, const ndarray<const T,2>& ndarr)
00170   {
00171     boost::shared_ptr< ndarray<const T,2> > shp( new ndarray<const T,2>(ndarr) );
00172     evt.put(shp, src, key);
00173   }
00174 
00175 // or its alias
00176 /*
00177   template <typename T>
00178   void save2DArrInEvent(PSEvt::Event& evt, const Pds::Src& src, const std::string& key, const ndarray<const T,2>& ndarr)
00179   {
00180     save2DArrayInEvent<T>(evt, src, key, ndarr);
00181   }
00182 */
00183 //-------------------
00184   /**
00185    * @brief Save 3-D array in event, for src and key.
00186    * 
00187    * @param[in]  evt
00188    * @param[in]  src
00189    * @param[in]  key
00190    * @param[out] ndarr
00191    */
00192 
00193   template <typename T>
00194   void save3DArrInEvent(PSEvt::Event& evt, const Pds::Src& src, const std::string& key, const ndarray<const T,3>& ndarr)
00195   {
00196       boost::shared_ptr< ndarray<const T,3> > shp( new ndarray<const T,3>(ndarr) );
00197       evt.put(shp, src, key);
00198   }
00199 
00200 //--------------------
00201 /// Save ndarray in file
00202   template <typename T>
00203   void save2DArrayInFile(const std::string& fname, const ndarray<const T,2>& ndarr, bool print_msg, FILE_MODE file_type=TEXT)
00204   {  
00205     if (fname.empty()) {
00206       MsgLog("GlobalMethods", warning, "The output file name is empty. 2-d array is not saved.");
00207       return;
00208     }
00209 
00210     if( print_msg ) MsgLog("GlobalMethods", info, "Save 2-d array in file " << fname.c_str() << " file type:" << strOfDataTypeAndSize<T>());
00211 
00212     const unsigned* shape = ndarr.shape();
00213     const unsigned rows = shape[0]; 
00214     const unsigned cols = shape[1];
00215     const T* arr = ndarr.data();
00216 
00217     //======================
00218     if (file_type == TEXT) {
00219         std::ofstream out(fname.c_str()); 
00220         out << std::setprecision(9); // << std::setw(8) << std::setprecision(0) << std::fixed 
00221               for (unsigned r = 0; r != rows; ++r) {
00222                 for (unsigned c = 0; c != cols; ++c) {
00223                   out << arr[r*cols + c] << ' ';
00224                 }
00225                 out << '\n';
00226               }
00227         out.close();
00228         return; 
00229     }
00230 
00231     //======================
00232     if (file_type == BINARY) {
00233         std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary;
00234         std::ofstream out(fname.c_str(), mode);
00235         //out.write(reinterpret_cast<const char*>(arr), rows*cols*sizeof(T));
00236         for (unsigned r = 0; r != rows; ++r) {
00237           const T* data = &arr[r*cols];
00238           out.write(reinterpret_cast<const char*>(data), cols*sizeof(T));
00239         }
00240         out.close();
00241         return; 
00242     }
00243   }
00244 
00245 //--------------------
00246 //--------------------
00247 //--------------------
00248 //--------------------
00249 //--------------------
00250 
00251 } // namespace CSPadPixCoords
00252 
00253 #endif // CSPADPIXCOORDS_GLOBALMETHODS_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7