CSPadPixCoords/src/CSPad2x2NDArrReshape.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: CSPad2x2NDArrReshape.cpp 9393 2014-12-11 19:26:20Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class CSPad2x2NDArrReshape...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "CSPadPixCoords/CSPad2x2NDArrReshape.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <time.h>
00022 #include <sstream>   // for stringstream
00023 
00024 //-------------------------------
00025 // Collaborating Class Headers --
00026 //-------------------------------
00027 #include "MsgLogger/MsgLogger.h"
00028 #include "PSEvt/EventId.h"
00029 
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 //#include <boost/lexical_cast.hpp>
00034 
00035 // This declares this class as psana module
00036 using namespace CSPadPixCoords;
00037 PSANA_MODULE_FACTORY(CSPad2x2NDArrReshape)
00038 
00039 using namespace std;
00040 
00041 //              ----------------------------------------
00042 //              -- Public Function Member Definitions --
00043 //              ----------------------------------------
00044 
00045 namespace CSPadPixCoords {
00046 
00047 //----------------
00048 // Constructors --
00049 //----------------
00050 
00051 CSPad2x2NDArrReshape::CSPad2x2NDArrReshape (const std::string& name)
00052   : Module(name)
00053   , m_source()
00054   , m_keys_in()
00055   , m_keys_out()
00056   , m_print_bits()
00057   , m_count_evt(0)
00058   , m_count_clb(0)
00059   , m_count_msg(0)
00060 {
00061   m_source        = configSrc("source",     ":Cspad2x2");
00062   m_keys_in       = configStr("keys_in",    "");
00063   m_keys_out      = configStr("keys_out",   "");
00064   m_print_bits    = config   ("print_bits", 0);
00065 
00066   put_keys_in_vectors();
00067 
00068   //m_config = new CONFIG ( m_source ); 
00069 }
00070 
00071 //--------------
00072 // Destructor --
00073 //--------------
00074 
00075 CSPad2x2NDArrReshape::~CSPad2x2NDArrReshape ()
00076 {
00077 }
00078 
00079 //--------------------
00080 
00081 /// Print input parameters
00082 void 
00083 CSPad2x2NDArrReshape::printInputParameters()
00084 {
00085   WithMsgLog(name(), info, log) {
00086     log << "\nInput parameters:"
00087         << "\nsource        : "     << m_source
00088         << "\nkeys_in       : "     << m_keys_in
00089         << "\nkeys_out      : "     << m_keys_out
00090         << "\nprint_bits    : "     << m_print_bits
00091         << "\n";
00092   }
00093 }
00094 
00095 //--------------------
00096 
00097 /// Method which is called once at the beginning of the job
00098 void 
00099 CSPad2x2NDArrReshape::beginJob(Event& evt, Env& env)
00100 {
00101   if( m_print_bits & 1 ) printInputParameters();
00102   if( m_print_bits & 2 ) print_in_out_keys();
00103   if( m_print_bits & 16) printSizeOfTypes();
00104 }
00105 
00106 //--------------------
00107 
00108 /// Method which is called with event data, this is the only required 
00109 /// method, all other methods are optional
00110 void 
00111 CSPad2x2NDArrReshape::event(Event& evt, Env& env)
00112 {
00113   ++m_count_evt;
00114 
00115   procEvent(evt);
00116 
00117   if( m_count_clb == 0 ) {
00118     if(procCalib(env)) m_count_clb++;
00119   }
00120 }
00121 
00122 //--------------------
00123 
00124 void CSPad2x2NDArrReshape::beginRun(Event& evt, Env& env) 
00125 { 
00126   m_count_clb = 0; 
00127 }
00128 
00129 //--------------------
00130 
00131 void CSPad2x2NDArrReshape::beginCalibCycle(Event& evt, Env& env) {}
00132 void CSPad2x2NDArrReshape::endCalibCycle(Event& evt, Env& env) {}
00133 void CSPad2x2NDArrReshape::endRun(Event& evt, Env& env) {}
00134 void CSPad2x2NDArrReshape::endJob(Event& evt, Env& env) {}
00135 
00136 //--------------------
00137 
00138 void
00139 CSPad2x2NDArrReshape::put_keys_in_vectors()
00140 {
00141   v_keys_in     .reserve(50);
00142   v_keys_out    .reserve(50);
00143 
00144   std::stringstream ss(m_keys_in);
00145   std::string key_in;  
00146   std::string key_out;  
00147 
00148   if(m_keys_in.empty()) {
00149     MsgLog(name(), warning, "The list of input keys in keys_in is empty. " 
00150                          << "At lease one input key needs to be specified.");
00151         throw std::runtime_error("Check CSPad2x2NDArrReshape parameters in the configuration file!");
00152   }
00153 
00154   if(m_keys_out.empty()) {
00155     // Auto-generate output keys
00156 
00157       do { ss >> key_in;
00158         size_t pos = key_in.find(':');
00159         if( pos != std::string::npos ) key_out = std::string(key_in, 0, pos);
00160         else                           key_out = key_in + ":2x185x388";
00161 
00162         //cout  << " key_in: " << std::setw(20) << key_in 
00163         //      << " key_out:" << std::setw(20) << key_out 
00164         //      << '\n'; 
00165 
00166         v_keys_in .push_back(key_in); 
00167         v_keys_out.push_back(key_out); 
00168       
00169       } while( ss.good() ); 
00170 
00171   } else {
00172     // Take output keys from config file
00173 
00174       std::stringstream ss_out(m_keys_out);
00175 
00176       do { ss     >> key_in;  v_keys_in .push_back(key_in);  } while( ss.good() ); 
00177       do { ss_out >> key_out; v_keys_out.push_back(key_out); } while( ss_out.good() ); 
00178 
00179       //cout  << "Input keys  : " << m_keys_in << '\n';  
00180       //cout  << "Output keys : " << m_keys_out << '\n';  
00181       //cout  << "Number of input keys  : " << v_keys_in .size() << '\n';  
00182       //cout  << "Number of output keys : " << v_keys_out.size() << '\n';  
00183 
00184       if(v_keys_in.size() != v_keys_out.size()) {
00185         MsgLog(name(), warning, "The list of output keys: (" << m_keys_out
00186                << ") is not empty, \nbut their number: " << v_keys_out.size() 
00187                <<" does not equal to the number: " << v_keys_in.size() 
00188                <<" of the input keys: (" << m_keys_in << ").");
00189         throw std::runtime_error("Check CSPad2x2NDArrReshape parameters in the configuration file!");
00190       }
00191   }
00192 }
00193 
00194 //--------------------
00195 
00196 void
00197 CSPad2x2NDArrReshape::print_in_out_keys()
00198 {
00199       std::vector<std::string>::iterator it_in  = v_keys_in.begin();
00200       std::vector<std::string>::iterator it_out = v_keys_out.begin();
00201 
00202       std::stringstream ss; ss << "\n  List of input/output keys for [185x388x2] -> [2x185x388] conversion:";
00203 
00204       for ( ; it_in != v_keys_in.end(); ++it_in, ++it_out) 
00205         ss  << "\n    key_in: "  << std::setw(16) << std::left << *it_in 
00206             << "  key_out: "     << std::setw(16) << std::left << *it_out; 
00207       ss  << '\n';
00208 
00209       MsgLog(name(), info, ss.str());
00210 }
00211 
00212 //--------------------
00213 
00214 bool
00215 CSPad2x2NDArrReshape::procEvent(Event& evt)
00216 {  
00217   bool is_found = false;
00218 
00219   for(size_t i=0; i<v_keys_out.size(); ++i) {
00220     if ( procEventForType<int16_t> (evt, i) ) { is_found = true; continue; } 
00221     if ( procEventForType<float>   (evt, i) ) { is_found = true; continue; } 
00222     if ( procEventForType<double>  (evt, i) ) { is_found = true; continue; } 
00223     if ( procEventForType<int>     (evt, i) ) { is_found = true; continue; } 
00224   }
00225 
00226   if(m_print_bits && !is_found && ++m_count_msg < 11) {
00227     MsgLog(name(), info, "ndarray is not available in the eventStore, event:" << m_count_evt 
00228                             << " source:" << m_source << " keys:" << m_keys_in);
00229     if (m_count_msg == 10) MsgLog(name(), warning, "STOP WARNINGS for source:" << m_source << " keys:" << m_keys_in);    
00230   }
00231 
00232   return is_found;
00233 }
00234 
00235 //--------------------
00236 
00237 bool 
00238 CSPad2x2NDArrReshape::procCalib(Env& env)
00239 {  
00240   bool is_found = false;
00241 
00242   for(size_t i=0; i<v_keys_out.size(); ++i) {
00243     if ( procCalibForType<int16_t> (env, i) ) { is_found = true; continue; }
00244     if ( procCalibForType<float>   (env, i) ) { is_found = true; continue; } 
00245     if ( procCalibForType<double>  (env, i) ) { is_found = true; continue; } 
00246     if ( procCalibForType<int>     (env, i) ) { is_found = true; continue; } 
00247   }
00248 
00249   if(m_print_bits && !is_found && ++m_count_msg < 11) {
00250     MsgLog(name(), info, "ndarray is not available in the calibStore, event:" << m_count_evt 
00251                             << " source:" << m_source << " keys:" << m_keys_in);
00252     if (m_count_msg == 10) MsgLog(name(), warning, "STOP WARNINGS for source:" << m_source << " keys:" << m_keys_in);    
00253   }
00254 
00255   return is_found;
00256 }
00257 
00258 //--------------------
00259 
00260 /*
00261 void 
00262 CSPad2x2NDArrReshape::checkTypeImplementation()
00263 {  
00264   if ( m_outtype == "float"   ) { m_dtype = FLOAT;  return; }
00265   if ( m_outtype == "double"  ) { m_dtype = DOUBLE; return; } 
00266   if ( m_outtype == "int"     ) { m_dtype = INT;    return; } 
00267   if ( m_outtype == "int16"   ) { m_dtype = INT16;  return; } 
00268   if ( m_outtype == "int16_t" ) { m_dtype = INT16;  return; } 
00269 
00270   const std::string msg = "The requested data type: " + m_outtype + " is not implemented";
00271   MsgLog(name(), warning, msg );
00272   throw std::runtime_error(msg);
00273 }
00274 */
00275 
00276 //--------------------
00277 
00278 } // namespace CSPadPixCoords

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7