00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "ImgAlgos/NDArrImageProducer.h"
00017
00018
00019
00020
00021 #include <time.h>
00022 #include <sstream>
00023
00024
00025
00026
00027
00028
00029 #include "PSCalib/CalibFileFinder.h"
00030
00031
00032
00033
00034
00035
00036
00037 #include <boost/lexical_cast.hpp>
00038
00039
00040 using namespace ImgAlgos;
00041 using namespace std;
00042
00043 PSANA_MODULE_FACTORY(NDArrImageProducer)
00044
00045
00046
00047
00048
00049 namespace ImgAlgos {
00050
00051
00052
00053
00054
00055 NDArrImageProducer::NDArrImageProducer (const std::string& name)
00056 : Module(name)
00057 , m_calibdir()
00058 , m_calibgroup()
00059 , m_source()
00060 , m_inkey()
00061 , m_outimgkey()
00062 , m_outtype()
00063 , m_oname()
00064 , m_oindex()
00065 , m_pix_scale_size_um()
00066 , m_x0_off_pix()
00067 , m_y0_off_pix()
00068 , m_xy0_off_pix(0)
00069 , m_mode()
00070 , m_do_tilt()
00071 , m_print_bits()
00072 , m_count_evt(0)
00073 , m_count_msg(0)
00074 {
00075
00076 m_calibdir = configStr("calibdir", "");
00077 m_calibgroup = configStr("calibgroup", "");
00078 m_source = configSrc("source", "");
00079 m_inkey = configStr("key_in", "");
00080 m_outimgkey = configStr("key_out", "image");
00081 m_outtype = configStr("type_out", "asinp");
00082 m_oname = configStr("oname", "");
00083 m_oindex = config ("oindex", 0);
00084 m_pix_scale_size_um= config ("pix_scale_size_um", 0);
00085 m_x0_off_pix = config ("x0_off_pix", 0);
00086 m_y0_off_pix = config ("y0_off_pix", 0);
00087 m_mode = config ("mode", 0);
00088 m_do_tilt = config ("do_tilt", true);
00089 m_print_bits = config ("print_bits", 0);
00090
00091
00092 stringstream ss; ss << m_source;
00093 m_str_src = ss.str();
00094
00095 if(m_str_src.empty()) {
00096 const std::string msg = "NDArrImageProducer:: Input source IS NOT specified! Please define the \"source\" parameter.";
00097
00098 throw std::runtime_error(msg);
00099 }
00100
00101 if (m_x0_off_pix or m_y0_off_pix) {
00102 m_xy0_off_pix = new int[2];
00103 m_xy0_off_pix[0] = m_x0_off_pix;
00104 m_xy0_off_pix[1] = m_y0_off_pix;
00105 }
00106
00107 checkTypeImplementation();
00108 }
00109
00110
00111
00112
00113
00114 NDArrImageProducer::~NDArrImageProducer ()
00115 {
00116 }
00117
00118
00119
00120
00121 void
00122 NDArrImageProducer::printInputParameters()
00123 {
00124 WithMsgLog(name(), info, log) {
00125 log << "\n Input parameters:"
00126 << "\n calibdir : " << m_calibdir
00127 << "\n calibgroup : " << m_calibgroup
00128 << "\n source : " << m_source
00129 << "\n inkey : " << m_inkey
00130 << "\n outimgkey : " << m_outimgkey
00131 << "\n outtype : " << m_outtype
00132 << "\n oname : " << m_oname
00133 << "\n oindex : " << m_oindex
00134 << "\n pix_scale_size_um : " << m_pix_scale_size_um
00135 << "\n x0_off_pix : " << m_x0_off_pix
00136 << "\n y0_off_pix : " << m_y0_off_pix
00137 << "\n mode : " << m_mode
00138 << "\n do_tilt : " << m_do_tilt
00139 << "\n print_bits : " << m_print_bits
00140 << "\n";
00141 }
00142 }
00143
00144
00145
00146 void
00147 NDArrImageProducer::beginJob(Event& evt, Env& env)
00148 {
00149 if( m_print_bits & 1 ) printInputParameters();
00150 if( m_print_bits & 16) printSizeOfTypes();
00151 }
00152
00153
00154
00155 void
00156 NDArrImageProducer::beginRun(Event& evt, Env& env)
00157 {
00158 m_count_clb = 0;
00159 }
00160
00161
00162
00163 void
00164 NDArrImageProducer::beginCalibCycle(Event& evt, Env& env)
00165 {
00166 }
00167
00168
00169
00170
00171 void
00172 NDArrImageProducer::event(Event& evt, Env& env)
00173 {
00174 ++m_count_evt;
00175 if( m_print_bits & 4 ) MsgLog(name(), info, stringTimeStamp(evt) << " evt:" << m_count_evt);
00176
00177
00178
00179 procEvent(evt, env);
00180 }
00181
00182
00183
00184 void
00185 NDArrImageProducer::endCalibCycle(Event& evt, Env& env)
00186 {
00187 }
00188
00189
00190
00191 void
00192 NDArrImageProducer::endRun(Event& evt, Env& env)
00193 {
00194 }
00195
00196
00197
00198 void
00199 NDArrImageProducer::endJob(Event& evt, Env& env)
00200 {
00201 }
00202
00203
00204
00205
00206
00207
00208 bool
00209 NDArrImageProducer::getCalibPars(Event& evt, Env& env)
00210 {
00211 if(m_count_clb) return true;
00212
00213 std::string calib_dir = (m_calibdir.empty()) ? env.calibDir() : m_calibdir;
00214 std::string group = (m_calibgroup.empty()) ? calibGroupForSource(m_source) : m_calibgroup;
00215 unsigned prbits = (m_print_bits & 64) ? 0177777 : 0;
00216 int runnum = getRunNumber(evt);
00217
00218 PSCalib::CalibFileFinder calibfinder(calib_dir, group, prbits);
00219 std::string fname = calibfinder.findCalibFile(m_src, "geometry", runnum);
00220
00221 if( fname.empty() ) {
00222 if( m_print_bits & 2 ) MsgLog(name(), warning, "NOT FOUND \"geometry\" file for calibdir: " << calib_dir
00223 << " group:" << group
00224 << " src:" << m_source
00225 << " run:" << runnum);
00226 return false;
00227 }
00228
00229
00230 if( m_print_bits & 2 ) MsgLog(name(), info, "Use \"geometry\" constants for run: " << runnum
00231 << " from file:\n" << fname);
00232 m_geometry = new PSCalib::GeometryAccess(fname, prbits);
00233
00234 m_geometry->get_pixel_coord_indexes(m_coor_x_ind, m_coor_y_ind, m_size,
00235 m_oname, m_oindex, m_pix_scale_size_um, m_xy0_off_pix, m_do_tilt);
00236
00237 if( m_print_bits & 32 ) m_geometry->print_pixel_coords();
00238
00239 m_x_ind_max = m_coor_x_ind[0];
00240 m_y_ind_max = m_coor_y_ind[0];
00241 for (unsigned i=1; i<m_size; ++i) {
00242 if (m_coor_x_ind[i] > m_x_ind_max) m_x_ind_max = m_coor_x_ind[i];
00243 if (m_coor_y_ind[i] > m_y_ind_max) m_y_ind_max = m_coor_y_ind[i];
00244 }
00245
00246 m_count_clb++;
00247 return true;
00248 }
00249
00250
00251
00252 void
00253 NDArrImageProducer::procEvent(Event& evt, Env& env)
00254 {
00255 if ( procNDArrForType <int16_t> (evt, env) ) return;
00256 if ( procNDArrForType <uint16_t> (evt, env) ) return;
00257 if ( procNDArrForType <float> (evt, env) ) return;
00258 if ( procNDArrForType <double> (evt, env) ) return;
00259 if ( procNDArrForType <int> (evt, env) ) return;
00260
00261 m_count_msg++;
00262 if (m_count_msg < 11 && m_print_bits) {
00263 MsgLog(name(), warning, "procEvent(...): Input data or geometry file is not available in event:"
00264 << m_count_evt << " for source:"
00265 << m_source << " key:" << m_inkey);
00266 if (m_count_msg == 10) MsgLog(name(), warning, "STOP PRINT WARNINGS for source:"
00267 << m_source << " key:" << m_inkey);
00268 }
00269 }
00270
00271
00272
00273 void
00274 NDArrImageProducer::checkTypeImplementation()
00275 {
00276 if ( m_outtype == "asinp" ) { m_dtype = ASINP; return; }
00277 if ( m_outtype == "int16" ) { m_dtype = INT16; return; }
00278 if ( m_outtype == "float" ) { m_dtype = FLOAT; return; }
00279 if ( m_outtype == "double" ) { m_dtype = DOUBLE; return; }
00280 if ( m_outtype == "int" ) { m_dtype = INT; return; }
00281
00282 const std::string msg = "The requested data type: " + m_outtype + " is not implemented";
00283 MsgLog(name(), warning, msg );
00284 throw std::runtime_error(msg);
00285 }
00286
00287
00288
00289 }