CSPadPixCoords/src/CSPadImageGetTest.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class CSPadImageGetTest...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "CSPadPixCoords/CSPadImageGetTest.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 // #include <time.h>
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "MsgLogger/MsgLogger.h"
00027 #include "PSEvt/EventId.h"
00028 #include "CSPadPixCoords/Image2D.h"
00029 
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 //#include <boost/lexical_cast.hpp>
00034 #include <iomanip> // for setw, setfill
00035 #include <sstream> // for streamstring
00036 
00037 // This declares this class as psana module
00038 using namespace CSPadPixCoords;
00039 PSANA_MODULE_FACTORY(CSPadImageGetTest)
00040 
00041 using namespace std;
00042 
00043 //              ----------------------------------------
00044 //              -- Public Function Member Definitions --
00045 //              ----------------------------------------
00046 
00047 namespace CSPadPixCoords {
00048 
00049 //----------------
00050 // Constructors --
00051 //----------------
00052 
00053 CSPadImageGetTest::CSPadImageGetTest (const std::string& name)
00054   : Module(name)
00055   , m_str_src()
00056   , m_key()
00057   , m_eventSave()
00058   , m_saveAll()
00059   , m_fname()
00060   , m_count(0)
00061 {
00062   // get the values from configuration or use defaults
00063   m_str_src       = configSrc("source",   "CxiDs1.0:Cspad.0");
00064   m_key           = configStr("key",      "Image2D");
00065   m_eventSave     = config   ("eventSave", 0);
00066   m_saveAll       = config   ("saveAll",   false);
00067   m_fname         = configStr("fname",    "cspad-image");
00068 }
00069 
00070 
00071 //--------------
00072 // Destructor --
00073 //--------------
00074 
00075 CSPadImageGetTest::~CSPadImageGetTest ()
00076 {
00077 }
00078 
00079 //--------------------
00080 
00081 /// Method which is called once at the beginning of the job
00082 void 
00083 CSPadImageGetTest::beginJob(Event& evt, Env& env)
00084 {
00085 }
00086 
00087 //--------------------
00088 
00089 /// Method which is called at the beginning of the run
00090 void 
00091 CSPadImageGetTest::beginRun(Event& evt, Env& env)
00092 {
00093 }
00094 
00095 //--------------------
00096 
00097 /// Method which is called at the beginning of the calibration cycle
00098 void 
00099 CSPadImageGetTest::beginCalibCycle(Event& evt, Env& env)
00100 {
00101 }
00102 
00103 //--------------------
00104 
00105 /// Method which is called with event data, this is the only required 
00106 /// method, all other methods are optional
00107 void 
00108 CSPadImageGetTest::event(Event& evt, Env& env)
00109 {
00110   // this is how to gracefully stop analysis job
00111   ++m_count;
00112   //if (m_count >= m_maxEvents) stop();
00113   MsgLog(name(), debug, "Event: " << m_count);
00114   if (m_saveAll || m_count == m_eventSave) this -> saveImageInFile(evt);
00115 }
00116 
00117 //--------------------
00118   
00119 /// Method which is called at the end of the calibration cycle
00120 void 
00121 CSPadImageGetTest::endCalibCycle(Event& evt, Env& env)
00122 {
00123 }
00124 
00125 //--------------------
00126 
00127 /// Method which is called at the end of the run
00128 void 
00129 CSPadImageGetTest::endRun(Event& evt, Env& env)
00130 {
00131 }
00132 
00133 //--------------------
00134 
00135 /// Method which is called once at the end of the job
00136 void 
00137 CSPadImageGetTest::endJob(Event& evt, Env& env)
00138 {
00139 }
00140 
00141 //--------------------
00142 //--------------------
00143 //--------------------
00144 //--------------------
00145 
00146 std::string  
00147 CSPadImageGetTest::strTimeStamp(Event& evt)
00148 {
00149   shared_ptr<PSEvt::EventId> eventId = evt.get();
00150   if (eventId.get()) {
00151 
00152     //m_time = eventId->time();
00153     //std::stringstream ss;
00154     //ss << hex << t_msec;
00155     //string hex_msec = ss.str();
00156 
00157     return (eventId->time()).asStringFormat( "%Y-%m-%d-%H%M%S%f"); // "%Y-%m-%d %H:%M:%S%f%z"
00158   }
00159   else
00160     return std::string("time-stamp-is-not-defined");
00161 }
00162 
00163 //--------------------
00164 
00165 std::string  
00166 CSPadImageGetTest::strRunNumber(Event& evt)
00167 {
00168   shared_ptr<PSEvt::EventId> eventId = evt.get();
00169   if (eventId.get()) {
00170     stringstream ssRunNum; ssRunNum << "r" << setw(4) << setfill('0') << eventId->run();
00171     return ssRunNum.str();
00172   }
00173   else
00174     return std::string("run-is-not-defined");
00175 }
00176 
00177 //--------------------
00178 
00179 std::string
00180 CSPadImageGetTest::strEventCounter()
00181 {
00182   stringstream ssEvNum; ssEvNum << setw(6) << setfill('0') << m_count;
00183   return ssEvNum.str();
00184 }
00185 
00186 //--------------------
00187 
00188 void 
00189 CSPadImageGetTest::saveImageInFile(Event& evt)
00190 {
00191   // Define the file name
00192   stringstream ssEvNum; ssEvNum << setw(6) << setfill('0') << m_count;
00193   string fname = m_fname + "-" + strRunNumber(evt) + "-" + strTimeStamp(evt) + ".txt";
00194 
00195   // In case if m_key == "Image2D" 
00196 
00197   shared_ptr< CSPadPixCoords::Image2D<double> > img2d = evt.get(m_str_src, m_key, &m_src);
00198   if (img2d.get()) {
00199     MsgLog(name(), info, "::saveImageInFile(...): Get image as Image2D<double> from event and save it in file");
00200     img2d -> saveImageInFile(fname,0);
00201   } // if (img2d.get())
00202 
00203 
00204   shared_ptr< ndarray<const double,2> > img = evt.get(m_str_src, m_key, &m_src);
00205   if (img.get()) {
00206     MsgLog(name(), info, "::saveImageInFile(...): Get image as ndarray<double,2> from event and save it in file");
00207     CSPadPixCoords::Image2D<double> *img2d = new CSPadPixCoords::Image2D<double>(img->data(),img->shape()[0],img->shape()[1]);
00208     img2d -> saveImageInFile(fname,0);
00209   } // if (img2d.get())
00210 }
00211 
00212 //--------------------
00213 
00214 } // namespace CSPadPixCoords

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7