CSPadPixCoords/src/CSPadImageProducer.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class CSPadImageProducer...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 
00017 #include "CSPadPixCoords/CSPadImageProducer.h"
00018 
00019 //-----------------
00020 // C/C++ Headers --
00021 //-----------------
00022 #include <time.h>
00023 
00024 //-------------------------------
00025 // Collaborating Class Headers --
00026 //-------------------------------
00027 #include "PSEvt/EventId.h"
00028 #include "PSCalib/CalibFileFinder.h"
00029 #include "PSCalib/GeometryAccess.h"
00030 #include "PSCalib/SegGeometryCspad2x1V1.h"
00031 //#include "PSCalib/PixCoords2x1V2.h"
00032 
00033 //-----------------------------------------------------------------------
00034 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00035 //-----------------------------------------------------------------------
00036 #include <boost/lexical_cast.hpp>
00037 
00038 // This declares this class as psana module
00039 using namespace CSPadPixCoords;
00040 PSANA_MODULE_FACTORY(CSPadImageProducer)
00041 
00042 using namespace std;
00043 
00044 //              ----------------------------------------
00045 //              -- Public Function Member Definitions --
00046 //              ----------------------------------------
00047 
00048 namespace CSPadPixCoords {
00049 
00050 
00051 //----------------
00052 // Constructors --
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   // get the values from configuration or use defaults
00067   m_calibDir      = configStr("calibDir",      ""); // if not provided default from env will be used
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 //checkTypeImplementation();
00081 }
00082 
00083 
00084 //--------------
00085 // Destructor --
00086 //--------------
00087 
00088 CSPadImageProducer::~CSPadImageProducer ()
00089 {
00090 }
00091 
00092 //--------------------
00093 
00094 /// Print input parameters
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 /// Method which is called once at the beginning of the job
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 /// Method which is called at the beginning of the run
00127 void 
00128 CSPadImageProducer::beginRun(Event& evt, Env& env)
00129 {
00130   m_count_cfg = 0; 
00131 }
00132 
00133 //--------------------
00134 
00135 /// Method which is called at the beginning of the calibration cycle
00136 void 
00137 CSPadImageProducer::beginCalibCycle(Event& evt, Env& env)
00138 {
00139 }
00140 
00141 //--------------------
00142 
00143 /// Method which is called with event data, this is the only required 
00144 /// method, all other methods are optional
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);     // get m_src, incriments m_count_cfg
00154 
00155       if ( m_count_cfg==0 ) return; // skip event processing if configuration is missing
00156 
00157       getCalibPars(evt, env); // use m_src
00158       cspadImgActivePixelMask(env);
00159   }
00160 
00161 
00162   struct timespec start, stop;
00163   int status = clock_gettime( CLOCK_REALTIME, &start ); // Get LOCAL time
00164 
00165   procEvent(evt, env);
00166 
00167   if( m_print_bits & 4 ) {
00168     status = clock_gettime( CLOCK_REALTIME, &stop ); // Get LOCAL time
00169     //cout << "  Time to produce cspad image is " 
00170     //     << stop.tv_sec - start.tv_sec + 1e-9*(stop.tv_nsec - start.tv_nsec) 
00171     //     << " sec" << endl;
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 /// Method which is called at the end of the calibration cycle
00181 void 
00182 CSPadImageProducer::endCalibCycle(Event& evt, Env& env)
00183 {
00184 }
00185 
00186 //--------------------
00187 
00188 /// Method which is called at the end of the run
00189 void 
00190 CSPadImageProducer::endRun(Event& evt, Env& env)
00191 {
00192 }
00193 
00194 //--------------------
00195 
00196 /// Method which is called once at the end of the job
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   // find minimal X,Y coordinates
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   // apply coordinate offset and evaluate indexes
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   //return false;
00262 }
00263 
00264 //--------------------
00265 
00266 /// Method which is called at the beginning of the run
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   //m_pix_coords_2x1  -> print_member_data();
00290   //m_pix_coords_quad -> print_member_data(); 
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   // Check if the requested src and key are consistent with Psana::CsPad::DataV1, or V2
00317   if ( procCSPadDataForType  <Psana::CsPad::DataV1, Psana::CsPad::ElementV1> (evt) ) return;
00318   if ( procCSPadDataForType  <Psana::CsPad::DataV2, Psana::CsPad::ElementV2> (evt) ) return;
00319 
00320   // Check if the requested src and key are consistent with ndarray<T,3> of shape [N][185][388]
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         //std::fill_n(img_nda.data(), int(IMG_SIZE), mask_cspad_t(0));    
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 } // namespace CSPadPixCoords

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7