00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "CSPadPixCoords/CSPad2x2ImageProducer.h"
00017
00018
00019
00020
00021 #include <time.h>
00022 #include <sstream>
00023
00024
00025
00026
00027 #include "psddl_psana/cspad2x2.ddl.h"
00028 #include "PSEvt/EventId.h"
00029 #include "PSCalib/CalibFileFinder.h"
00030 #include "PSCalib/GeometryAccess.h"
00031 #include "PSCalib/SegGeometryCspad2x1V1.h"
00032
00033 #include "CSPadPixCoords/Image2D.h"
00034
00035
00036
00037
00038 #include <boost/lexical_cast.hpp>
00039
00040
00041 using namespace CSPadPixCoords;
00042 using namespace std;
00043
00044 PSANA_MODULE_FACTORY(CSPad2x2ImageProducer)
00045
00046
00047
00048
00049
00050 namespace CSPadPixCoords {
00051
00052
00053
00054
00055
00056 CSPad2x2ImageProducer::CSPad2x2ImageProducer (const std::string& name)
00057 : Module(name)
00058 , m_calibDir()
00059 , m_typeGroupName()
00060 , m_source()
00061 , m_inkey()
00062 , m_outimgkey()
00063 , m_outtype()
00064 , m_tiltIsApplied()
00065 , m_useWidePixCenter()
00066 , m_print_bits()
00067 , m_count(0)
00068 , m_count_msg(0)
00069 {
00070
00071 m_calibDir = configStr("calibDir", "");
00072 m_typeGroupName = configStr("typeGroupName", "CsPad2x2::CalibV1");
00073 m_source = configSrc("source", "DetInfo(:Cspad2x2.1)");
00074 m_inkey = configStr("inkey", "");
00075 m_outimgkey = configStr("outimgkey", "image");
00076 m_outtype = configStr("outtype", "asdata");
00077 m_tiltIsApplied = config ("tiltIsApplied", true);
00078 m_useWidePixCenter = config ("useWidePixCenter",false);
00079 m_print_bits = config ("print_bits", 0);
00080
00081
00082
00083
00084 checkTypeImplementation();
00085 }
00086
00087
00088
00089
00090
00091 CSPad2x2ImageProducer::~CSPad2x2ImageProducer ()
00092 {
00093 }
00094
00095
00096
00097
00098 void
00099 CSPad2x2ImageProducer::printInputParameters()
00100 {
00101 WithMsgLog(name(), info, log) {
00102 log << "\n Input parameters:"
00103 << "\n calibDir : " << m_calibDir
00104 << "\n typeGroupName : " << m_typeGroupName
00105 << "\n source : " << m_source
00106 << "\n inkey : " << m_inkey
00107 << "\n outimgkey : " << m_outimgkey
00108 << "\n outtype : " << m_outtype
00109 << "\n tiltIsApplied : " << m_tiltIsApplied
00110 << "\n useWidePixCenter : " << m_useWidePixCenter
00111 << "\n print_bits : " << m_print_bits
00112 << "\n";
00113 }
00114
00115 MsgLog(name(), debug,
00116 "\n NX_CSPAD2X2 : " << NX_CSPAD2X2
00117 << "\n NY_CSPAD2X2 : " << NY_CSPAD2X2
00118 << "\n"
00119 );
00120 }
00121
00122
00123
00124 void
00125 CSPad2x2ImageProducer::beginJob(Event& evt, Env& env)
00126 {
00127 if( m_print_bits & 1 ) printInputParameters();
00128 if( m_print_bits & 16) printSizeOfTypes();
00129 }
00130
00131
00132
00133 void
00134 CSPad2x2ImageProducer::beginRun(Event& evt, Env& env)
00135 {
00136 m_count_cfg = 0;
00137 }
00138
00139
00140
00141 void
00142 CSPad2x2ImageProducer::beginCalibCycle(Event& evt, Env& env)
00143 {
00144 }
00145
00146
00147
00148
00149 void
00150 CSPad2x2ImageProducer::event(Event& evt, Env& env)
00151 {
00152 ++m_count;
00153 if( m_print_bits & 4 ) printTimeStamp(evt, m_count);
00154
00155 if ( m_count_cfg==0 ) {
00156 getConfigPars(env);
00157 if ( m_count_cfg==0 ) return;
00158 getCalibPars(evt, env);
00159 }
00160
00161 processEvent(evt, env);
00162 }
00163
00164
00165
00166 void
00167 CSPad2x2ImageProducer::endCalibCycle(Event& evt, Env& env)
00168 {
00169 }
00170
00171
00172
00173 void
00174 CSPad2x2ImageProducer::endRun(Event& evt, Env& env)
00175 {
00176 }
00177
00178
00179
00180 void
00181 CSPad2x2ImageProducer::endJob(Event& evt, Env& env)
00182 {
00183 }
00184
00185
00186
00187
00188
00189
00190 void
00191 CSPad2x2ImageProducer::getConfigPars(Env& env)
00192 {
00193 if ( getConfigParsForType <Psana::CsPad2x2::ConfigV1> (env) ) return;
00194 if ( getConfigParsForType <Psana::CsPad2x2::ConfigV2> (env) ) return;
00195
00196 m_count_msg++;
00197 if (m_count_msg < 11 && m_print_bits) {
00198 MsgLog(name(), warning, "No CsPad2x2 configuration objects found. event:"
00199 << m_count << " for source:" << m_source);
00200 if (m_count_msg ==10) MsgLog(name(), warning, "STOP PRINTING WARNINGS for source:" << m_source);
00201
00202 }
00203 }
00204
00205
00206
00207
00208 bool
00209 CSPad2x2ImageProducer::getGeometryPars(const std::string& calib_dir, const int runnum, const unsigned prbits)
00210 {
00211 PSCalib::CalibFileFinder calibfinder(calib_dir, m_typeGroupName, prbits);
00212 std::string fname = calibfinder.findCalibFile(m_src, "geometry", runnum);
00213
00214 if( fname.empty() ) return false;
00215 if( m_print_bits & 2 ) MsgLog(name(), info, "Use \"geometry\" constants for run: " << runnum << " from file:\n" << fname);
00216
00217 PSCalib::GeometryAccess geometry(fname, prbits);
00218 if( m_print_bits & 2 ) geometry.print_pixel_coords();
00219
00220 const unsigned * iX;
00221 const unsigned * iY;
00222 unsigned size;
00223 geometry.get_pixel_coord_indexes(iX, iY, size);
00224
00225 m_coor_x_int = new uint32_t[size];
00226 m_coor_y_int = new uint32_t[size];
00227
00228 for(unsigned i=0; i<size; ++i) {
00229 m_coor_x_int[i] = iX[i];
00230 m_coor_y_int[i] = iY[i];
00231 }
00232
00233 return true;
00234
00235 }
00236
00237
00238
00239 void
00240 CSPad2x2ImageProducer::getCalibPars(Event& evt, Env& env)
00241 {
00242 std::string calib_dir = (m_calibDir == "") ? env.calibDir() : m_calibDir;
00243 int runnum = getRunNumber(evt);
00244 unsigned prbits = (m_print_bits & 64) ? 0377 : 0;
00245
00246
00247 if( getGeometryPars(calib_dir, runnum, prbits) ) return;
00248
00249 m_cspad2x2_calibpars = new PSCalib::CSPad2x2CalibPars(calib_dir, m_typeGroupName, m_src, getRunNumber(evt));
00250 m_pix_coords_cspad2x2 = new PC2X2 (m_cspad2x2_calibpars, m_tiltIsApplied, m_useWidePixCenter);
00251
00252
00253 unsigned size = PC2X2::ROWS2X1 * PC2X2::COLS2X1 * PC2X2::N2X1_IN_DET;
00254 m_coor_x_int = new uint32_t[size];
00255 m_coor_y_int = new uint32_t[size];
00256
00257 unsigned ind=0;
00258 for (unsigned r=0; r<PC2X2::ROWS2X1; r++){
00259 for (unsigned c=0; c<PC2X2::COLS2X1; c++){
00260 for (unsigned s=0; s<PC2X2::N2X1_IN_DET; s++){
00261
00262 m_coor_x_int[ind] = int (m_pix_coords_cspad2x2 -> getPixCoor_pix(PC2X2::AXIS_X, s, r, c) + 0.1);
00263 m_coor_y_int[ind] = int (m_pix_coords_cspad2x2 -> getPixCoor_pix(PC2X2::AXIS_Y, s, r, c) + 0.1);
00264 ind++;
00265 }
00266 }
00267 }
00268
00269 if( m_print_bits & 2 ) {
00270 m_cspad2x2_calibpars -> printInputPars();
00271 m_cspad2x2_calibpars -> printCalibPars();
00272
00273
00274 }
00275 }
00276
00277
00278
00279 void
00280 CSPad2x2ImageProducer::processEvent(Event& evt, Env& env)
00281 {
00282
00283 if ( procCSPad2x2DataForType <Psana::CsPad2x2::ElementV1> (evt) ) return;
00284
00285
00286 if ( procCSPad2x2NDArrForType <float> (evt) ) return;
00287 if ( procCSPad2x2NDArrForType <double> (evt) ) return;
00288 if ( procCSPad2x2NDArrForType <int> (evt) ) return;
00289 if ( procCSPad2x2NDArrForType <int16_t> (evt) ) return;
00290 if ( procCSPad2x2NDArrForType <uint16_t> (evt) ) return;
00291
00292 m_count_msg++;
00293 if (m_count_msg < 11 && m_print_bits) {
00294 MsgLog(name(), warning, "processEvent(...): cspad2x2 data or ndarr is not available in event:" << m_count
00295 << " for source:" << m_source << " key:" << m_inkey);
00296 if (m_count_msg ==10) MsgLog(name(), warning, "STOP PRINTING WARNINGS for source:"
00297 << m_source << " key:" << m_inkey);
00298 }
00299 }
00300
00301
00302
00303 void
00304 CSPad2x2ImageProducer::cspad_image_add_in_event(Event& evt)
00305 {
00306
00307 if ( m_dtype == ASDATA ) save2DArrayInEventForType<int16_t> (evt);
00308 else if ( m_dtype == INT16 ) save2DArrayInEventForType<int16_t> (evt);
00309 else if ( m_dtype == FLOAT ) save2DArrayInEventForType<float> (evt);
00310 else if ( m_dtype == DOUBLE ) save2DArrayInEventForType<double> (evt);
00311 else if ( m_dtype == INT ) save2DArrayInEventForType<int> (evt);
00312 }
00313
00314
00315
00316 void
00317 CSPad2x2ImageProducer::checkTypeImplementation()
00318 {
00319 if ( m_outtype == "asdata" ) { m_dtype = ASDATA; return; }
00320 if ( m_outtype == "int16" ) { m_dtype = INT16; return; }
00321 if ( m_outtype == "float" ) { m_dtype = FLOAT; return; }
00322 if ( m_outtype == "double" ) { m_dtype = DOUBLE; return; }
00323 if ( m_outtype == "int" ) { m_dtype = INT; return; }
00324
00325 const std::string msg = "The requested data type: " + m_outtype + " is not implemented";
00326 MsgLog(name(), warning, msg );
00327 throw std::runtime_error(msg);
00328 }
00329
00330
00331
00332 }