00001 #ifndef IMGALGOS_NDARRIMAGEPRODUCER_H
00002 #define IMGALGOS_NDARRIMAGEPRODUCER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "psana/Module.h"
00021 #include "MsgLogger/MsgLogger.h"
00022
00023
00024
00025
00026 #include "PSCalib/GeometryAccess.h"
00027
00028
00029
00030 #include "ImgAlgos/GlobalMethods.h"
00031
00032
00033
00034
00035
00036 #include "PSEvt/Source.h"
00037
00038
00039
00040
00041
00042
00043 namespace ImgAlgos {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 class NDArrImageProducer : public Module {
00075 public:
00076
00077
00078 NDArrImageProducer (const std::string& name) ;
00079
00080
00081 virtual ~NDArrImageProducer () ;
00082
00083
00084 virtual void beginJob(Event& evt, Env& env);
00085
00086
00087 virtual void beginRun(Event& evt, Env& env);
00088
00089
00090 virtual void beginCalibCycle(Event& evt, Env& env);
00091
00092
00093
00094 virtual void event(Event& evt, Env& env);
00095
00096
00097 virtual void endCalibCycle(Event& evt, Env& env);
00098
00099
00100 virtual void endRun(Event& evt, Env& env);
00101
00102
00103 virtual void endJob(Event& evt, Env& env);
00104
00105
00106 protected:
00107
00108 void printInputParameters();
00109 bool getCalibPars(Event& evt, Env& env);
00110 void cspad_image_init();
00111 void procEvent(Event& evt, Env& env);
00112 void checkTypeImplementation();
00113
00114 private:
00115
00116
00117
00118 std::string m_calibdir;
00119 std::string m_calibgroup;
00120 std::string m_str_src;
00121 Source m_source;
00122 Pds::Src m_src;
00123 std::string m_inkey;
00124 std::string m_outimgkey;
00125 std::string m_outtype;
00126 std::string m_oname;
00127 unsigned m_oindex;
00128 double m_pix_scale_size_um;
00129 int m_x0_off_pix;
00130 int m_y0_off_pix;
00131 int *m_xy0_off_pix;
00132 int m_mode;
00133 bool m_do_tilt;
00134 unsigned m_print_bits;
00135 unsigned m_count_evt;
00136 unsigned m_count_clb;
00137 unsigned m_count_msg;
00138 DATA_TYPE m_dtype;
00139
00140 unsigned m_size;
00141 const unsigned *m_coor_x_ind;
00142 const unsigned *m_coor_y_ind;
00143 unsigned m_x_ind_max;
00144 unsigned m_y_ind_max;
00145
00146 PSCalib::GeometryAccess* m_geometry;
00147
00148 public:
00149
00150
00151
00152 template <typename TINP, typename TOUT>
00153 void save2DArrayInEventForType (Event& evt, ndarray<TINP,2>& img_inp ) {
00154
00155
00156 if (typeid(TOUT) == typeid(TINP)) {
00157 save2DArrayInEvent<TINP>(evt, m_src, m_outimgkey, img_inp);
00158 return;
00159 }
00160
00161
00162 ndarray<TOUT,2> img_out (img_inp.shape());
00163
00164 typename ndarray<TOUT,2>::iterator it_out = img_out.begin();
00165 for (typename ndarray<TINP,2>::iterator it=img_inp.begin(); it!=img_inp.end(); ++it, ++it_out ) {
00166 *it_out = (TOUT)*it;
00167 }
00168
00169 save2DArrayInEvent<TOUT>(evt, m_src, m_outimgkey, img_out);
00170 }
00171
00172
00173
00174 template <typename T, unsigned ND>
00175 void image_fill_and_add_in_event(Event& evt, const ndarray<const T,ND>& data)
00176 {
00177 if (data.size() != m_size) {
00178 stringstream ss; ss << "ndarray for source:" << m_source
00179 << " and key:" << m_inkey
00180 << " has size: " << data.size()
00181 << " different from number of pixels in geometry:" << m_size;
00182 MsgLog(name(), warning, ss.str());
00183 throw std::runtime_error(ss.str());
00184 }
00185
00186 unsigned shape[2] = {m_x_ind_max+1, m_y_ind_max+1};
00187 ndarray<T,2> img_nda(shape);
00188
00189 std::fill_n(img_nda.data(), int(img_nda.size()), T(0));
00190
00191 const T* p_data = data.data();
00192
00193
00194 if (m_mode == 0) {
00195
00196 for (unsigned i=0; i<m_size; ++i)
00197 img_nda[m_coor_x_ind[i]][m_coor_y_ind[i]] = p_data[i];
00198 }
00199
00200 else if (m_mode == 1) {
00201
00202 for (unsigned i=0; i<m_size; ++i) {
00203 T* p_tmp = &img_nda[m_coor_x_ind[i]][m_coor_y_ind[i]];
00204 if ( *p_tmp==0 || p_data[i] > *p_tmp) *p_tmp = p_data[i];
00205 }
00206 }
00207
00208 else if (m_mode == 2) {
00209
00210 for (unsigned i=0; i<m_size; ++i) {
00211
00212
00213 img_nda[m_coor_x_ind[i]][m_coor_y_ind[i]] += p_data[i];
00214 }
00215 }
00216
00217
00218
00219
00220 else {
00221
00222 for (unsigned i=0; i<m_size; ++i)
00223 img_nda[m_coor_x_ind[i]][m_coor_y_ind[i]] = p_data[i];
00224 }
00225
00226 if ( m_dtype == ASINP ) save2DArrayInEvent<T>(evt, m_src, m_outimgkey, img_nda);
00227 else if ( m_dtype == INT16 ) save2DArrayInEventForType<T, int16_t> (evt, img_nda);
00228 else if ( m_dtype == FLOAT ) save2DArrayInEventForType<T, float> (evt, img_nda);
00229 else if ( m_dtype == DOUBLE ) save2DArrayInEventForType<T, double> (evt, img_nda);
00230 else if ( m_dtype == INT ) save2DArrayInEventForType<T, int> (evt, img_nda);
00231 }
00232
00233
00234
00235 template <typename T, unsigned ND>
00236 bool procNDArrForTypeAndND(Event& evt, Env& env) {
00237
00238
00239 shared_ptr< ndarray<const T,ND> > shp_const = evt.get(m_source, m_inkey, &m_src);
00240 if (shp_const.get()) {
00241 if ( ! getCalibPars(evt, env) ) return false;
00242 const ndarray<const T,ND>& nda = *shp_const.get();
00243 image_fill_and_add_in_event <T,ND> (evt, nda);
00244 return true;
00245 }
00246
00247
00248 shared_ptr< ndarray<T,ND> > shp = evt.get(m_source, m_inkey, &m_src);
00249 if (shp.get()) {
00250 if ( ! getCalibPars(evt, env) ) return false;
00251 ndarray<T,ND>& nda = *shp.get();
00252 image_fill_and_add_in_event <T,ND> (evt, nda);
00253 return true;
00254 }
00255
00256 return false;
00257 }
00258
00259
00260
00261 template <typename T>
00262 bool procNDArrForType (Event& evt, Env& env) {
00263
00264 if( m_print_bits & 8 ) MsgLog(name(), warning, "Produce image from ndarray, source:" << m_source
00265 << " key:" << m_inkey << " data type:" << typeid(T).name() );
00266
00267 if ( procNDArrForTypeAndND<T,2>(evt, env) ) return true;
00268 if ( procNDArrForTypeAndND<T,3>(evt, env) ) return true;
00269 if ( procNDArrForTypeAndND<T,4>(evt, env) ) return true;
00270 if ( procNDArrForTypeAndND<T,1>(evt, env) ) return true;
00271
00272 return false;
00273 }
00274
00275
00276
00277
00278 };
00279
00280 }
00281
00282 #endif // IMGALGOS_NDARRIMAGEPRODUCER_H