00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "CSPadPixCoords/CSPadImageProducer.h"
00018
00019
00020
00021
00022 #include <time.h>
00023
00024
00025
00026
00027 #include "PSEvt/EventId.h"
00028 #include "PSCalib/CalibFileFinder.h"
00029 #include "PSCalib/GeometryAccess.h"
00030 #include "PSCalib/SegGeometryCspad2x1V1.h"
00031
00032
00033
00034
00035
00036 #include <boost/lexical_cast.hpp>
00037
00038
00039 using namespace CSPadPixCoords;
00040 PSANA_MODULE_FACTORY(CSPadImageProducer)
00041
00042 using namespace std;
00043
00044
00045
00046
00047
00048 namespace CSPadPixCoords {
00049
00050
00051
00052
00053
00054
00055 CSPadImageProducer::CSPadImageProducer (const std::string& name)
00056 : Module(name)
00057 , m_calibDir()
00058 , m_typeGroupName()
00059 , m_str_src()
00060 , m_inkey()
00061 , m_imgkey()
00062 , m_tiltIsApplied()
00063 , m_print_bits()
00064 , m_count(0)
00065 {
00066
00067 m_calibDir = configStr("calibDir", "");
00068 m_typeGroupName = configStr("typeGroupName", "CsPad::CalibV1");
00069 m_str_src = configStr("source", "CxiDs1.0:Cspad.0");
00070 m_inkey = configStr("key", "");
00071 m_imgkey = configStr("imgkey", "image");
00072 m_fname_pixmap = configStr("fname_pixmap", "");
00073 m_fname_pixnum = configStr("fname_pixnum", "");
00074 m_tiltIsApplied = config ("tiltIsApplied", true);
00075 m_print_bits = config ("print_bits", 0);
00076
00077 m_source = Source(m_str_src);
00078 m_count_msg= 0;
00079
00080
00081 }
00082
00083
00084
00085
00086
00087
00088 CSPadImageProducer::~CSPadImageProducer ()
00089 {
00090 }
00091
00092
00093
00094
00095 void
00096 CSPadImageProducer::printInputParameters()
00097 {
00098 WithMsgLog(name(), info, log) {
00099 log << "\nInput parameters:"
00100 << "\ncalibDir : " << m_calibDir
00101 << "\ntypeGroupName : " << m_typeGroupName
00102 << "\nstr_src : " << m_str_src
00103 << "\nsource : " << m_source
00104 << "\nkey : " << m_inkey
00105 << "\nimgkey : " << m_imgkey
00106 << "\nfname_pixmap : " << m_fname_pixmap
00107 << "\nfname_pixnum : " << m_fname_pixnum
00108 << "\ntiltIsApplied : " << m_tiltIsApplied
00109 << "\nprint_bits : " << m_print_bits
00110 << "\n";
00111 }
00112 }
00113
00114
00115
00116
00117 void
00118 CSPadImageProducer::beginJob(Event& evt, Env& env)
00119 {
00120 if( m_print_bits & 1 ) printInputParameters();
00121 if( m_print_bits & 16) printSizeOfTypes();
00122 }
00123
00124
00125
00126
00127 void
00128 CSPadImageProducer::beginRun(Event& evt, Env& env)
00129 {
00130 m_count_cfg = 0;
00131 }
00132
00133
00134
00135
00136 void
00137 CSPadImageProducer::beginCalibCycle(Event& evt, Env& env)
00138 {
00139 }
00140
00141
00142
00143
00144
00145 void
00146 CSPadImageProducer::event(Event& evt, Env& env)
00147 {
00148 ++m_count;
00149 if( m_print_bits & 4 ) printTimeStamp(evt, m_count);
00150
00151
00152 if ( m_count_cfg==0 ) {
00153 getConfigPars(env);
00154
00155 if ( m_count_cfg==0 ) return;
00156
00157 getCalibPars(evt, env);
00158 cspadImgActivePixelMask(env);
00159 }
00160
00161
00162 struct timespec start, stop;
00163 int status = clock_gettime( CLOCK_REALTIME, &start );
00164
00165 procEvent(evt, env);
00166
00167 if( m_print_bits & 4 ) {
00168 status = clock_gettime( CLOCK_REALTIME, &stop );
00169
00170
00171
00172 MsgLog(name(), info, "Time to produce cspad image is "
00173 << stop.tv_sec - start.tv_sec + 1e-9*(stop.tv_nsec - start.tv_nsec)
00174 << " sec");
00175 }
00176 }
00177
00178
00179
00180
00181 void
00182 CSPadImageProducer::endCalibCycle(Event& evt, Env& env)
00183 {
00184 }
00185
00186
00187
00188
00189 void
00190 CSPadImageProducer::endRun(Event& evt, Env& env)
00191 {
00192 }
00193
00194
00195
00196
00197 void
00198 CSPadImageProducer::endJob(Event& evt, Env& env)
00199 {
00200 }
00201
00202
00203
00204 void
00205 CSPadImageProducer::getConfigPars(Env& env)
00206 {
00207 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV2>(env) ) return;
00208 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV3>(env) ) return;
00209 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV4>(env) ) return;
00210 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV5>(env) ) return;
00211
00212 m_count_msg++;
00213 if (m_count_msg < 11 && m_print_bits) {
00214 MsgLog(name(), warning, "CsPad::ConfigV2-V5 is not available in this run, event:" << m_count << " for source:" << m_str_src);
00215 if (m_count_msg == 10) MsgLog(name(), warning, "STOP PRINTING WARNINGS for source:" << m_str_src);
00216 }
00217 }
00218
00219
00220
00221 bool
00222 CSPadImageProducer::getGeometryPars(const std::string& calib_dir, const int runnum, const unsigned prbits)
00223 {
00224 PSCalib::CalibFileFinder calibfinder(calib_dir, m_typeGroupName, prbits);
00225 const std::string calibtype("geometry");
00226 std::string fname = calibfinder.findCalibFile(m_src, calibtype, runnum);
00227
00228 if( fname.empty() ) return false;
00229 if( m_print_bits & 2 ) MsgLog(name(), info, "Use \"geometry\" constants for run: " << runnum << " from file:\n" << fname);
00230
00231 PSCalib::GeometryAccess geometry(fname, prbits);
00232 if( m_print_bits & 2 ) geometry.print_pixel_coords();
00233
00234 const double* X;
00235 const double* Y;
00236 const double* Z;
00237 unsigned size;
00238 geometry.get_pixel_coords(X,Y,Z,size);
00239
00240 const double um_to_pix = PSCalib::SegGeometryCspad2x1V1::UM_TO_PIX;
00241 const double pix_size_um = PSCalib::SegGeometryCspad2x1V1::PIX_SCALE_SIZE;
00242
00243
00244 double Xmin = 1e6;
00245 double Ymin = 1e6;
00246 for(unsigned i=0; i<size; ++i) {
00247 if(X[i]<Xmin) Xmin=X[i];
00248 if(Y[i]<Ymin) Ymin=Y[i];
00249 }
00250
00251
00252 m_coor_x_int = new uint32_t[size];
00253 m_coor_y_int = new uint32_t[size];
00254
00255 for(unsigned i=0; i<size; ++i) {
00256 m_coor_x_int[i] = uint32_t( (X[i]-Xmin+0.5*pix_size_um)*um_to_pix );
00257 m_coor_y_int[i] = uint32_t( (Y[i]-Ymin+0.5*pix_size_um)*um_to_pix );
00258 }
00259
00260 return true;
00261
00262 }
00263
00264
00265
00266
00267 void
00268 CSPadImageProducer::getCalibPars(Event& evt, Env& env)
00269 {
00270 std::string calib_dir = (m_calibDir == "") ? env.calibDir() : m_calibDir;
00271 int runnum = getRunNumber(evt);
00272 unsigned prbits = (m_print_bits & 64) ? 0377 : 0;
00273
00274
00275 if( getGeometryPars(calib_dir, runnum, prbits) ) return;
00276
00277
00278 m_cspad_calibpar = new PSCalib::CSPadCalibPars(calib_dir, m_typeGroupName, m_src, runnum, prbits);
00279 m_pix_coords_2x1 = new CSPadPixCoords::PixCoords2x1 ();
00280 m_pix_coords_quad = new CSPadPixCoords::PixCoordsQuad ( m_pix_coords_2x1, m_cspad_calibpar, m_tiltIsApplied );
00281 m_pix_coords_cspad = new CSPadPixCoords::PixCoordsCSPad ( m_pix_coords_quad, m_cspad_calibpar, m_tiltIsApplied );
00282
00283 m_coor_x_pix = m_pix_coords_cspad -> getPixCoorArrX_pix();
00284 m_coor_y_pix = m_pix_coords_cspad -> getPixCoorArrY_pix();
00285 m_coor_x_int = m_pix_coords_cspad -> getPixCoorArrX_int();
00286 m_coor_y_int = m_pix_coords_cspad -> getPixCoorArrY_int();
00287
00288 if( m_print_bits & 2 ) m_cspad_calibpar -> printCalibPars();
00289
00290
00291 }
00292
00293
00294
00295 void
00296 CSPadImageProducer::getCSPadConfigFromData(Event& evt)
00297 {
00298 if ( getCSPadConfigFromDataForType <Psana::CsPad::DataV1, Psana::CsPad::ElementV1> (evt) ) return;
00299 if ( getCSPadConfigFromDataForType <Psana::CsPad::DataV2, Psana::CsPad::ElementV2> (evt) ) return;
00300
00301 m_count_msg++;
00302 if (m_count_msg < 11 && m_print_bits) {
00303 MsgLog(name(), warning, "getCSPadConfigFromData(...): Psana::CsPad::DataV#,ElementV# for #=1,2 is not available in event:"
00304 << m_count << " for source:" << m_str_src);
00305 if (m_count_msg == 10) MsgLog(name(), warning, "STOP PRINTING WARNINGS for source:" << m_str_src);
00306 }
00307 }
00308
00309
00310
00311 void
00312 CSPadImageProducer::procEvent(Event& evt, Env& env)
00313 {
00314 getCSPadConfigFromData(evt);
00315
00316
00317 if ( procCSPadDataForType <Psana::CsPad::DataV1, Psana::CsPad::ElementV1> (evt) ) return;
00318 if ( procCSPadDataForType <Psana::CsPad::DataV2, Psana::CsPad::ElementV2> (evt) ) return;
00319
00320
00321 if ( procCSPadNDArrForType <float> (evt) ) return;
00322 if ( procCSPadNDArrForType <double> (evt) ) return;
00323 if ( procCSPadNDArrForType <int> (evt) ) return;
00324 if ( procCSPadNDArrForType <int16_t> (evt) ) return;
00325 if ( procCSPadNDArrForType <uint16_t> (evt) ) return;
00326
00327 m_count_msg++;
00328 if (m_count_msg < 11 && m_print_bits) {
00329 MsgLog(name(), warning, "procEvent(...): Psana::CsPad::DataV# / ElementV# for #=[2-5] is not available in event:"
00330 << m_count << " for source:" << m_str_src << " key:" << m_inkey);
00331 if (m_count_msg == 10) MsgLog(name(), warning, "STOP PRINTING WARNINGS for source:" << m_str_src << " key:" << m_inkey);
00332 }
00333 }
00334
00335
00336
00337 void
00338 CSPadImageProducer::checkTypeImplementation()
00339 {
00340 if ( m_outtype == "float" ) { m_dtype = FLOAT; return; }
00341 if ( m_outtype == "double" ) { m_dtype = DOUBLE; return; }
00342 if ( m_outtype == "int" ) { m_dtype = INT; return; }
00343 if ( m_outtype == "int16" ) { m_dtype = INT16; return; }
00344 if ( m_outtype == "int16_t" ) { m_dtype = INT16; return; }
00345
00346 const std::string msg = "The requested data type: " + m_outtype + " is not implemented";
00347 MsgLog(name(), warning, msg );
00348 throw std::runtime_error(msg);
00349 }
00350
00351
00352 void
00353 CSPadImageProducer::cspadImgActivePixelMask(Env& env)
00354 {
00355 const unsigned shape[] = {NY_CSPAD,NX_CSPAD};
00356 ndarray<pixmap_cspad_t,2> pixmap_2da(shape);
00357 ndarray<pixnum_cspad_t,2> pixnum_2da(shape);
00358 std::fill(pixmap_2da.begin(), pixmap_2da.end(), pixmap_cspad_t(0));
00359 std::fill(pixnum_2da.begin(), pixnum_2da.end(), pixnum_cspad_t(-1));
00360
00361
00362 int ix=0; int iy=0;
00363 for(uint32_t pix=0; pix<ARR_SIZE; pix++)
00364 {
00365 ix = m_coor_x_int[pix];
00366 iy = m_coor_y_int[pix];
00367 pixmap_2da[ix][iy] = 1;
00368 pixnum_2da[ix][iy] = pix;
00369 }
00370
00371 save2DArrayInEnv<pixmap_cspad_t>(env, m_src, pixmap_2da);
00372 save2DArrayInEnv<pixnum_cspad_t>(env, m_src, pixnum_2da);
00373
00374 if( ! m_fname_pixmap.empty() ) {
00375 const std::string msg = "Save active pixel map in file: " + m_fname_pixmap;
00376 MsgLog(name(), info, msg );
00377 save2DArrayInFile<pixmap_cspad_t>(m_fname_pixmap, pixmap_2da, m_print_bits&32);
00378 }
00379
00380 if( ! m_fname_pixnum.empty() ) {
00381 const std::string msg = "Save enumerated pixel map in file: " + m_fname_pixnum;
00382 MsgLog(name(), info, msg );
00383 save2DArrayInFile<pixnum_cspad_t>(m_fname_pixnum, pixnum_2da, m_print_bits&32);
00384 }
00385 }
00386
00387
00388
00389 }