ImgAlgos/src/NDArrCalib.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class NDArrCalib...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "ImgAlgos/NDArrCalib.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <sstream> // for stringstream
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "PSEvt/EventId.h"
00027 #include "PSCalib/CalibParsStore.h"
00028 #include "pdscalibdata/GlobalMethods.h" // for load_pars_from_file(...)
00029 
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 
00034 // This declares this class as psana module
00035 using namespace ImgAlgos;
00036 PSANA_MODULE_FACTORY(NDArrCalib)
00037 
00038 using namespace std;
00039 
00040 //              ----------------------------------------
00041 //              -- Public Function Member Definitions --
00042 //              ----------------------------------------
00043 
00044 namespace ImgAlgos {
00045 
00046 //----------------
00047 // Constructors --
00048 //----------------
00049 NDArrCalib::NDArrCalib (const std::string& name)
00050   : Module(name)
00051   , m_str_src()
00052   , m_key_in()
00053   , m_key_out() 
00054   , m_do_peds()
00055   , m_do_cmod()
00056   , m_do_stat()
00057   , m_do_mask()
00058   , m_do_bkgd()
00059   , m_do_gain()
00060   , m_do_nrms()
00061   , m_do_thre() 
00062   , m_fname_mask()
00063   , m_fname_bkgd()
00064   , m_mask_val()
00065   , m_low_nrms()
00066   , m_low_thre()
00067   , m_low_val()
00068   , m_ind_min()
00069   , m_ind_max()
00070   , m_ind_inc()
00071   , m_outtype()
00072   , m_print_bits()
00073   , m_count_event(0)
00074   , m_count_get(0)
00075   , m_count_msg(0)
00076   , m_is_done_once(false) 
00077 {
00078   // get the values from configuration or use defaults
00079   m_str_src           = configSrc("source",   "DetInfo(:Camera)");
00080   m_key_in            = configStr("key_in",                   "");
00081   m_key_out           = configStr("key_out",        "calibrated");
00082   m_do_peds           = config   ("do_peds",              false );
00083   m_do_cmod           = config   ("do_cmod",              false );
00084   m_do_stat           = config   ("do_stat",              false );
00085   m_do_mask           = config   ("do_mask",              false );
00086   m_do_bkgd           = config   ("do_bkgd",              false );
00087   m_do_gain           = config   ("do_gain",              false );
00088   m_do_nrms           = config   ("do_nrms",              false );
00089   m_do_thre           = config   ("do_thre",              false );
00090   m_fname_mask        = configStr("fname_mask",               "");
00091   m_fname_bkgd        = configStr("fname_bkgd",               "");
00092   m_mask_val          = config   ("masked_value",             0.);
00093   m_low_nrms          = config   ("threshold_nrms",           3.);
00094   m_low_thre          = config   ("threshold",                0.);
00095   m_low_val           = config   ("below_thre_value",         0.);
00096   m_ind_min           = config   ("bkgd_ind_min",             0 );
00097   m_ind_max           = config   ("bkgd_ind_max",           100 );
00098   m_ind_inc           = config   ("bkgd_ind_inc",             2 );
00099   m_outtype           = configStr("outtype",            "double");
00100   m_print_bits        = config   ("print_bits",               0 );
00101 
00102   m_ndarr_pars = new NDArrPars();
00103 
00104   checkOutTypeImplementation();
00105   findDetectorType();
00106 }
00107 
00108 //--------------------
00109 
00110 void 
00111 NDArrCalib::printInputParameters()
00112 {
00113   WithMsgLog(name(), info, log) {
00114     log << "\n Input parameters  :"
00115         << "\n source            : " << m_str_src
00116         << "\n m_key_in          : " << m_key_in      
00117         << "\n m_key_out         : " << m_key_out
00118         << "\n m_do_peds         : " << m_do_peds
00119         << "\n m_do_cmod         : " << m_do_cmod
00120         << "\n m_do_stat         : " << m_do_stat
00121         << "\n m_do_mask         : " << m_do_mask     
00122         << "\n m_do_bkgd         : " << m_do_bkgd     
00123         << "\n m_do_gain         : " << m_do_gain     
00124         << "\n m_do_nrms         : " << m_do_nrms     
00125         << "\n m_do_thre         : " << m_do_thre     
00126         << "\n m_fname_mask      : " << m_fname_mask     
00127         << "\n m_fname_bkgd      : " << m_fname_bkgd     
00128         << "\n m_mask_val        : " << m_mask_val   
00129         << "\n m_low_nrms        : " << m_low_nrms   
00130         << "\n m_low_thre        : " << m_low_thre   
00131         << "\n m_low_val         : " << m_low_val   
00132         << "\n m_ind_min         : " << m_ind_min    
00133         << "\n m_ind_max         : " << m_ind_max    
00134         << "\n m_ind_inc         : " << m_ind_inc
00135         << "\n m_print_bits      : " << m_print_bits
00136         << "\n m_dettype         : " << m_dettype
00137         << "\n Proc data type    : " << typeid(data_proc_t).name() << " of size " << sizeof(data_proc_t)
00138         << "\n outtype           : " << m_outtype
00139         << "\n";     
00140 
00141     printSizeOfTypes();
00142   }
00143 }
00144 
00145 //--------------------
00146 //--------------------
00147 
00148 void 
00149 NDArrCalib::findDetectorType()
00150 { 
00151   /* THIS STUFF IS MOVED TO GlobalMethods  
00152   std::stringstream ss; ss << m_str_src; std::string str_src = ss.str();
00153   if      ( str_src.find("Cspad.")     != std::string::npos ) m_dettype = CSPAD; // from GlobalMethods.h
00154   else if ( str_src.find("Cspad2x2.")  != std::string::npos ) m_dettype = CSPAD2X2;
00155   else if ( str_src.find("pnCCD.")     != std::string::npos ) m_dettype = PNCCD;
00156   else if ( str_src.find("Princeton.") != std::string::npos ) m_dettype = PRINCETON;
00157   else if ( str_src.find("Acqiris.")   != std::string::npos ) m_dettype = ACQIRIS;
00158   else if ( str_src.find("Tm6740.")    != std::string::npos ) m_dettype = TM6740;
00159   else if ( str_src.find("Opal1000.")  != std::string::npos ) m_dettype = OPAL1000;
00160   else if ( str_src.find("Opal2000.")  != std::string::npos ) m_dettype = OPAL2000;
00161   else if ( str_src.find("Opal4000.")  != std::string::npos ) m_dettype = OPAL4000;
00162   else if ( str_src.find("Opal8000.")  != std::string::npos ) m_dettype = OPAL8000;
00163   else if ( str_src.find("Epix100a")   != std::string::npos ) m_dettype = EPIX100A;
00164   else if ( str_src.find("Epix10k.")   != std::string::npos ) m_dettype = EPIX10K;
00165   else if ( str_src.find("Epix.")      != std::string::npos ) m_dettype = EPIX;
00166   else if ( str_src.find("Fccd960.")   != std::string::npos ) m_dettype = FCCD960;
00167   else                                                        m_dettype = OTHER;
00168   */
00169 
00170   m_dettype = detectorTypeForSource(m_str_src);
00171 }
00172 
00173 //--------------------
00174 
00175 //--------------
00176 // Destructor --
00177 //--------------
00178 NDArrCalib::~NDArrCalib ()
00179 {
00180 }
00181 
00182 /// Method which is called once at the beginning of the job
00183 void 
00184 NDArrCalib::beginJob(Event& evt, Env& env)
00185 {
00186   if( m_print_bits & 1 ) printInputParameters();
00187 }
00188 
00189 /// Method which is called at the beginning of the run
00190 void 
00191 NDArrCalib::beginRun(Event& evt, Env& env)
00192 {
00193   m_count_get = 0; // In order to load calibration pars etc for each run
00194 }
00195 
00196 /// Method which is called at the beginning of the calibration cycle
00197 void 
00198 NDArrCalib::beginCalibCycle(Event& evt, Env& env)
00199 {
00200 }
00201 
00202 /// Method which is called with event data, this is the only required 
00203 /// method, all other methods are optional
00204 void 
00205 NDArrCalib::event(Event& evt, Env& env)
00206 {
00207   ++ m_count_event;
00208   if( m_print_bits & 16 ) printEventRecord(evt);
00209   procEvent(evt, env);
00210 }
00211   
00212 /// Method which is called at the end of the calibration cycle
00213 void 
00214 NDArrCalib::endCalibCycle(Event& evt, Env& env)
00215 {
00216 }
00217 
00218 /// Method which is called at the end of the run
00219 void 
00220 NDArrCalib::endRun(Event& evt, Env& env)
00221 {
00222 }
00223 
00224 /// Method which is called once at the end of the job
00225 void 
00226 NDArrCalib::endJob(Event& evt, Env& env)
00227 {
00228 }
00229 
00230 //--------------------
00231 
00232 /*
00233 void 
00234 NDArrCalib::getConfigPars(Env& env)
00235 {
00236   //if ( getConfigParsForType <Psana::CsPad2x2::ConfigV1> (env) ) return;
00237   if ( getConfigParsForType <Psana::PNCCD::ConfigV1> (env) ) return;
00238   if ( getConfigParsForType <Psana::PNCCD::ConfigV2> (env) ) return;
00239   MsgLog(name(), error, "No configuration objects found, terminating.");
00240   terminate();
00241 }
00242 */
00243 
00244 //--------------------
00245 
00246 void 
00247 NDArrCalib::getCalibPars(Event& evt, Env& env)
00248 {
00249   //std::string calib_dir = (m_calibDir == "") ? env.calibDir() : m_calibDir;
00250   std::string calib_dir = env.calibDir();
00251 
00252   if( m_print_bits & 2 ) MsgLog(name(), info, "Calibration directory: " << calib_dir);
00253   std::string m_group = std::string(); // for ex: "PNCCD::CalibV1";
00254   unsigned prbits = (m_print_bits & 2) ? 255 : 0;
00255 
00256   m_calibpars = PSCalib::CalibParsStore::Create(calib_dir, m_group, m_src, getRunNumber(evt), prbits);
00257 
00258   m_peds_data = (m_do_peds) ? m_calibpars->pedestals()    : 0;
00259   m_gain_data = (m_do_gain) ? m_calibpars->pixel_gain()   : 0;
00260   m_cmod_data = (m_do_cmod) ? m_calibpars->common_mode()  : 0;
00261   m_rms_data  = (m_do_nrms) ? m_calibpars->pixel_rms()    : 0;
00262   m_stat_data = (m_do_stat || m_do_cmod) ? m_calibpars->pixel_status() : 0;
00263 
00264   if( m_print_bits & 2 ) m_calibpars->printCalibPars();
00265 
00266   if ( ! m_is_done_once ) {
00267          m_is_done_once = true;
00268 
00269       m_bkgd_data = new PSCalib::CalibPars::pixel_bkgd_t[m_size];
00270       m_mask_data = new PSCalib::CalibPars::pixel_mask_t[m_size];
00271       m_nrms_data = new data_proc_t[m_size];
00272 
00273       if( m_do_mask && !m_fname_mask.empty())
00274         pdscalibdata::load_pars_from_file<PSCalib::CalibPars::pixel_mask_t> (m_fname_mask, "Mask", m_size, m_mask_data);
00275       else std::fill_n(m_mask_data, int(m_size), PSCalib::CalibPars::pixel_mask_t(1));    
00276       
00277       if( m_do_bkgd && !m_fname_bkgd.empty())
00278         pdscalibdata::load_pars_from_file<PSCalib::CalibPars::pixel_bkgd_t> (m_fname_bkgd, "Background", m_size, m_bkgd_data);
00279       else std::fill_n(m_bkgd_data, int(m_size), PSCalib::CalibPars::pixel_bkgd_t(0));
00280 
00281       if( m_print_bits & 2 ) printMaskAndBkgd();
00282   }
00283 
00284   if( m_do_nrms ) { for(unsigned i=0; i<m_size; i++) m_nrms_data[i] = m_low_nrms * m_rms_data[i]; }
00285 
00286   if( m_do_cmod && m_print_bits & 4 ) printCommonModePars();
00287 }
00288 
00289 //--------------------
00290 
00291 void
00292 NDArrCalib::initAtFirstGetNdarray(Event& evt, Env& env)
00293 {
00294   if ( !defineNDArrPars(evt, m_str_src, m_key_in, m_ndarr_pars, bool(m_print_bits)) ) return;
00295 
00296   // Do initialization for known ndarray type and NDims
00297   if( m_print_bits & 8 ) m_ndarr_pars -> print();
00298   m_size  = m_ndarr_pars->size();
00299   m_dtype = m_ndarr_pars->dtype();
00300   m_ndim  = m_ndarr_pars->ndim();
00301   m_src   = m_ndarr_pars->src();
00302 
00303   //p_cdata = new data_proc_t[m_size];
00304 
00305   getCalibPars(evt, env);
00306 }
00307  
00308 //--------------------
00309 
00310 void 
00311 NDArrCalib::procEvent(Event& evt, Env& env)
00312 {
00313   if ( ! m_count_get ) initAtFirstGetNdarray(evt, env);
00314   if ( ! m_ndarr_pars -> is_set() ) return;
00315 
00316   if      (m_dtype == UINT16   && procEventForType<uint16_t, data_proc_t> (evt) ) return;
00317   else if (m_dtype == INT      && procEventForType<int,      data_proc_t> (evt) ) return;
00318   else if (m_dtype == INT16    && procEventForType<int16_t,  data_proc_t> (evt) ) return;
00319   else if (m_dtype == FLOAT    && procEventForType<float,    data_proc_t> (evt) ) return;
00320   else if (m_dtype == UINT8    && procEventForType<uint8_t,  data_proc_t> (evt) ) return;
00321   else if (m_dtype == DOUBLE   && procEventForType<double,   data_proc_t> (evt) ) return;
00322 
00323   if (++m_count_msg < 11 && m_print_bits) {
00324     MsgLog(name(), warning, "Image is not available in the event:" << m_count_event 
00325                             << " for source:" << m_str_src << " key:" << m_key_in);
00326     if (m_count_msg == 10) MsgLog(name(), warning, "STOP WARNINGS for source:" << m_str_src << " key:" << m_key_in);    
00327   }
00328 }
00329 
00330 //--------------------
00331  
00332 void 
00333 NDArrCalib::defImgIndexesForBkgdNorm()
00334 {
00335   v_inds.clear(); 
00336   for(unsigned i = m_ind_min; i < m_ind_max; i+=m_ind_inc) 
00337       v_inds.push_back(i);
00338 }
00339 
00340 //--------------------
00341 
00342 void 
00343 NDArrCalib::printEventRecord(Event& evt)
00344 {
00345   MsgLog( name(), info,  "Run="    << stringRunNumber(evt) 
00346                      << " Evt="    << stringFromUint(m_count_event) 
00347                      << " get="    << stringFromUint(m_count_get) 
00348                      << " Time="   << stringTimeStamp(evt) 
00349   );
00350 }
00351 
00352 //--------------------
00353 
00354 void 
00355 NDArrCalib::printCommonModePars()
00356 {
00357      std::stringstream ss; ss << "Common mode parameters: "; 
00358      for (int i=0; i<16;  ++i) ss << " " << m_cmod_data[i];
00359      MsgLog(name(), info, ss.str());
00360 }
00361 
00362 //--------------------
00363 
00364 void
00365 NDArrCalib::printMaskAndBkgd()
00366 {
00367   std::stringstream ss; 
00368 
00369   if( m_do_mask && !m_fname_mask.empty()) {
00370     ss << "\n  Content of the file: " << m_fname_mask << ':';
00371     for (int i=0; i<min(20,int(m_size)); ++i) ss << " " << m_mask_data[i];
00372   }
00373   else ss << "\n  File for mask: \"" << m_fname_mask << "\" is not requested in configuration parameters, m_do_mask=" << m_do_mask;
00374 
00375   if( m_do_bkgd && !m_fname_bkgd.empty()) {
00376     ss << "\n  Content of the file: " << m_fname_bkgd << ':'; 
00377     for (int i=0; i<min(10,int(m_size)); ++i) ss << " " << m_bkgd_data[i];
00378   }
00379   else ss << "\n  File for bkgd: \"" << m_fname_bkgd << "\" is not requested in configuration parameters, m_do_bkgd=" << m_do_bkgd;
00380 
00381   MsgLog(name(), info, ss.str());
00382 }
00383 
00384 //--------------------
00385 
00386 void 
00387 NDArrCalib::checkOutTypeImplementation()
00388 {  
00389   m_ptype = dataType<data_proc_t>();
00390 
00391   if (m_outtype == "double") { m_otype = DOUBLE; return; } 
00392   if (m_outtype == "float" ) { m_otype = FLOAT;  return; }
00393   if (m_outtype == "int"   ) { m_otype = INT;    return; } 
00394   if (m_outtype == "int16" ) { m_otype = INT16;  return; } 
00395 
00396   const std::string msg = "The requested output type: " + m_outtype + " is not implemented";
00397   MsgLog(name(), warning, msg );
00398   throw std::runtime_error(msg);
00399 }
00400 
00401 
00402 //--------------------
00403 } // namespace ImgAlgos
00404 //--------------------

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7