CSPadPixCoords/include/CSPad2x2ConfigPars.h

Go to the documentation of this file.
00001 #ifndef CSPADPIXCOORDS_CSPAD2X2CONFIGPARS_H
00002 #define CSPADPIXCOORDS_CSPAD2X2CONFIGPARS_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: CSPad2x2ConfigPars.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class CSPad2x2ConfigPars.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 
00017 //----------------------
00018 // Base Class Headers --
00019 //----------------------
00020 //#include "psana/Module.h"
00021 #include "MsgLogger/MsgLogger.h"
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 
00027 //#include "CSPadPixCoords/Image2D.h"
00028 //#include "CSPadPixCoords/GlobalMethods.h"
00029 
00030 //------------------------------------
00031 // Collaborating Class Declarations --
00032 //------------------------------------
00033 #include <boost/shared_ptr.hpp>
00034 
00035 #include "PSEvt/Source.h"
00036 #include "PSEnv/Env.h"
00037 #include "PSEvt/Event.h"
00038 #include "psddl_psana/cspad2x2.ddl.h"
00039 
00040 //#include "CSPadPixCoords/PixCoords2x1V2.h"
00041 #include "ndarray/ndarray.h"
00042 
00043 //              ---------------------
00044 //              -- Class Interface --
00045 //              ---------------------
00046 
00047 using namespace std;
00048 
00049 namespace CSPadPixCoords {
00050 
00051 /// @addtogroup CSPadPixCoords
00052 
00053 /**
00054  *  @ingroup CSPadPixCoords
00055  *
00056  *  @brief CSPad2x2ConfigPars is a store for CSPAD2x2 configuration parameters.
00057  *
00058  *  <h1>Interface Description</h1>
00059  *   
00060  *  @li  Include and typedef
00061  *  @code
00062  *  #include "CSPadPixCoords/CSPad2x2ConfigPars.h"
00063  *  typedef CSPadPixCoords::CSPad2x2ConfigPars CONFIG;
00064  *  @endcode
00065  *  
00066  *  @li  Instatiation\n
00067  *  Default constructor; may be used if all 32 2x1 are in working condition and are presented in data
00068  *  @code
00069  *      CONFIG* config = new CONFIG (); 
00070  *  @endcode
00071  *  \n
00072  *  Constructor from specified data source, defined in psana. Prefered use case of this class objects.
00073  *  @code
00074  *      CONFIG* config = new CONFIG ("DetInfo(MecTargetChamber.0:Cspad2x2.3)");
00075  *  @endcode
00076  *  In this case configuration parameters need to be defined in psana module overloding methods like
00077  *  @code
00078  *      virtual void beginRun(PSEvt::Event& evt, PSEnv::Env& env); 
00079  *      virtual void beginCalibCycle(PSEvt::Event& evt, PSEnv::Env& env); 
00080  *      // or
00081  *      virtual void event(PSEvt::Event& evt, PSEnv::Env& env); 
00082  *  @endcode
00083  *  from the PSEvt::Event and PSEnv::Env variables using method
00084  *  @code
00085  *      bool is_set = config -> setCSPad2x2ConfigPars (evt, env); 
00086  *      // or its separate private methods
00087  *      bool is_set = config -> setCSPad2x2ConfigParsFromEnv (env); 
00088  *      bool is_set = config -> setCSPad2x2ConfigParsFromEvent (evt); 
00089  *  @endcode
00090  *  \n
00091  *  Constructor from explicitly defined configuration parameters. It is not recommended to use. Can be used for stable non-complete configuration of the detector or for test purpose.
00092  *  @code
00093  *      uint32_t roiMask = 0;
00094  *      CONFIG* config = new CSPad2x2ConfigPars::CSPad2x2ConfigPars ( roiMask );
00095  *  @endcode
00096  *  
00097  *  
00098  * @li  Print current configuration parameters
00099  *  @code
00100  *      config -> printCSPad2x2ConfigPars();
00101  *  @endcode
00102  * 
00103  *  
00104  * @li  Get methods for member data  \n
00105  *  @code
00106  *      uint32_t n2x1     = config -> num2x1Stored();
00107  *      uint32_t roiMask  = config -> roiMask();
00108  *      float common_mode = config -> commonMode();
00109  *  @endcode
00110  *
00111  */  
00112 
00113 
00114 /*   
00115  * @li  Conversion between entire CSPAD2x2 pixel array shaped as (185,388,2) and data array shaped as (185,388,N), where N<=2 \n
00116  * Conversion from (185,388,2) to (185,388,N)
00117  * @code
00118  *     ndarray<double,3> nda_det  = make_ndarray (p_pix_arr_det, N2X1_IN_DET, ROWS2X1, COLS2X1);
00119  *     ndarray<double,3> nda_data = cspad_configpars -> getCSPadPixNDArrShapedAsData<double> ( nda_det );
00120  * @endcode
00121  *  \n 
00122  * Conversion from (185,388,N) to (185,388,2)
00123  * @code
00124  *     ndarray<double,3> nda_data = make_ndarray (p_pix_arr_data, N2X1_IN_DATA, ROWS2X1, COLS2X1); 
00125  *     ndarray<double,3> nda_det  = cspad_configpars -> getCSPadPixNDArrFromNDArrShapedAsData<double> ( nda_data ); 
00126  * @endcode
00127  */
00128 
00129   
00130 /** 
00131  *  @version \$Id: CSPad2x2ConfigPars.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $
00132  *
00133  *  @author Mikhail S. Dubrovin
00134  */
00135 
00136 class CSPad2x2ConfigPars {
00137 public:
00138 
00139   const static uint32_t N2x1 = 2; 
00140 
00141   //typedef CSPadPixCoords::PixCoords2x1V2 PC2X1;
00142   //typedef PixCoords2x1V2 PC2X1;
00143 
00144   //const static unsigned NQuadsMax = 4;
00145 
00146   /// @brief Default constructor
00147   CSPad2x2ConfigPars () ;
00148 
00149  /**
00150    * @brief Constructor using specified source as input parameter
00151    *  @param[in] source         (def.= "DetInfo(CxiDs1.0:Cspad.0)")                
00152    */
00153   CSPad2x2ConfigPars (PSEvt::Source source) ;
00154 
00155  /**
00156    *  @brief Constructor with explicit defenition of configuration parameters
00157    *  @param[in] roiMask        (def.= 03, or in decimal 3)
00158    */
00159   CSPad2x2ConfigPars ( uint32_t roiMask ) ;
00160 
00161   /// @brief Destructor
00162   virtual ~CSPad2x2ConfigPars () ;
00163 
00164  /**
00165    *  @brief Sets CSPAD configuration parameters
00166    *  @param[in] evt pointer to the event store
00167    *  @param[in] env pointer to the environment store 
00168    */
00169   bool setCSPad2x2ConfigPars(PSEvt::Event& evt, PSEnv::Env& env);
00170 
00171   /// Sets CSPAD configuration parameters to their default values
00172   void setCSPad2x2ConfigParsDefault();
00173 
00174   /// Prints CSPAD configuration parameters
00175   void printCSPad2x2ConfigPars();
00176 
00177   /// Returns the string with package name for logger. 
00178   std::string name() { return "pkg: CSPadPixCoords"; }  
00179 
00180   /// Returns the number of turned on (1) bits (2x1s) in the binary mask (def.= 8)
00181   uint32_t getNum2x1InMask(uint32_t mask);
00182 
00183   /// Returns the mask for 2x1s in the quad with index iq (def.=03 for 2 2x1s)
00184   uint32_t roiMask() { return m_roiMask; }
00185 
00186   /// Returns the number of 2x1s available in the CSPAD detector (def.= 32)
00187   uint32_t num2x1Stored() { return m_num2x1Stored; }
00188 
00189   /// Returns status: true if configuration parameters are set from env and evt, otherwise false.
00190   bool isSet() { return m_is_set; }   
00191 
00192   /// Returns common mode for 2x1s sections from evt
00193   float commonMode(int sec) { return m_common_mode[sec]; }
00194 
00195 //--------------------
00196  
00197 protected:
00198 
00199   /// part of the setCSPad2x2ConfigPars(PSEvt::Event& evt, PSEnv::Env& env)
00200   /// @param[in] env pointer to the environment store 
00201   bool setCSPad2x2ConfigParsFromEnv(PSEnv::Env& env);
00202 
00203   /// part of the setCSPad2x2ConfigPars(PSEvt::Event& evt, PSEnv::Env& env)
00204   /// @param[in] evt pointer to the event store
00205   bool setCSPad2x2ConfigParsFromEvent(PSEvt::Event& evt);
00206 
00207 private:
00208 
00209   PSEvt::Source m_source;        /// member data for data source set from config file
00210   Pds::Src      m_src;           /// source address as Pds::Src 
00211   unsigned      m_count_cfg;
00212   std::string   m_config_vers; 
00213   std::string   m_data_vers; 
00214   bool          m_is_set_for_evt;
00215   bool          m_is_set_for_env;
00216   bool          m_is_set;
00217  
00218   // Parameters form Psana::CsPad::ConfigV# object
00219   uint32_t m_roiMask; /// mask for turrned on/off (1/0) 2x1s
00220   uint32_t m_num2x1Stored; /// number of 2x1s in CSPAD2x2 stored in data
00221   uint32_t m_numAsicsStored; /// number of ASICs in CSPAD2x2 stored 
00222 
00223   // Parameters form Psana::CsPad::DataV# and Psana::CsPad::ElementV# object
00224   float m_common_mode[2]; /// common mode for 2 sensors
00225  
00226 //-------------------
00227   /**
00228    * @brief Gets m_roiMask and m_numAsicsStored from the Psana::CsPad2x2::ConfigV# object.
00229    * 
00230    */
00231   template <typename T>
00232   bool getConfigParsForType(PSEnv::Env& env) {
00233 
00234       boost::shared_ptr<T> config = env.configStore().get(m_source, &m_src);
00235       if (config) {
00236           m_roiMask        = config->roiMask();
00237           m_numAsicsStored = config->numAsicsStored();
00238           m_num2x1Stored   = getNum2x1InMask(m_roiMask);
00239           ++ m_count_cfg;
00240           return true;
00241       }
00242       return false;
00243   }
00244 
00245 //-------------------
00246   /**
00247    * @brief Gets extra info (if necessary) from the Psana::CsPad2x2::DataV# and ElementV# object.
00248    */
00249   template <typename TELEMENT>
00250   bool getCSPadConfigFromDataForType(PSEvt::Event& evt) {
00251 
00252     std::string key=""; // FOR RAW CSPAD DATA
00253     boost::shared_ptr<TELEMENT> elem = evt.get(m_source, key, &m_src);
00254     if (elem) {
00255       const ndarray<const int16_t, 3>& data = elem->data();
00256       for (unsigned i = 0; i != data.shape()[2]; ++ i) {
00257          m_common_mode[i] = elem->common_mode(i);
00258       }
00259       return true;
00260     }
00261     return false;
00262   }
00263 
00264 //-------------------
00265 
00266 public:
00267 
00268 //  /**
00269 //   * @brief Convers entire CSPAD pixel array, ndarray<T,3> with shape (32,185,388),
00270 //   * to the data array, ndarray<T,3> shaped as (N,185,388)
00271 //   */
00272 //  template <typename T>
00273 //      ndarray<T,3> getCSPadPixNDArrShapedAsData( ndarray<T,3>& arr_det ) { 
00274 //      
00275 //      const unsigned* shape = arr_det.shape();
00276 //      unsigned size2x1 = shape[1]*shape[2];
00277 //      //cout << "size2x1 = " << size2x1 << endl;
00278 //      //const unsigned shape[] = {32, PC2X1:ROWS2X1, PC2X1:COLS2X1}; // (32,185,388)
00279 //      //unsigned int shape_data[] = {m_num2x1StoredInData, shape[1], shape[2]}; // (N,185,388)
00280 //
00281 //      ndarray<T,3> arr_data = make_ndarray<T>(m_num2x1StoredInData, shape[1], shape[2]);
00282 //
00283 //      uint32_t ind2x1_in_data = 0;
00284 //      for (uint32_t q = 0; q < m_numQuads; ++ q) {
00285 //      
00286 //          uint32_t qNum = m_quadNumber[q]; 
00287 //          uint32_t mask = m_roiMask[q];
00288 //      
00289 //          for(uint32_t sect=0; sect < 8; sect++) {
00290 //              bool bitIsOn = mask & (1<<sect);
00291 //              if( !bitIsOn ) continue; 
00292 //      
00293 //              int ind2x1_in_det = qNum*8 + sect;             
00294 //                    std::memcpy(&arr_data[ind2x1_in_data][0][0], &arr_det[ind2x1_in_det][0][0], size2x1*sizeof(double));      
00295 //              ind2x1_in_data ++;
00296 //                }
00297 //      }        
00298 //      return arr_data;
00299 //  }
00300 //
00301 ////-------------------
00302 //
00303 //  /**
00304 //   * @brief Convers entire CSPAD pixel data array, ndarray<T,3> shaped as (N,185,388)
00305 //   * to the entire CSPAD pixel array, ndarray<T,3> with shape (32,185,388),
00306 //   */
00307 //  template <typename T>
00308 //    ndarray<T,3> getCSPadPixNDArrFromNDArrShapedAsData( ndarray<T,3>& arr_data, T default_value=0 ) { 
00309 //
00310 //      const unsigned* shape = arr_data.shape();
00311 //      unsigned size2x1 = shape[1]*shape[2];
00312 //      //cout << "size2x1 = " << size2x1 << endl;
00313 //
00314 //      int numPixTotal = 32*size2x1;
00315 //      ndarray<T,3> arr_det = make_ndarray<T>(32, shape[1], shape[2]);
00316 //      std::fill_n(&arr_det[0][0][0], numPixTotal, double(default_value));
00317 //
00318 //      uint32_t ind2x1_in_data = 0;
00319 //      for (uint32_t q = 0; q < m_numQuads; ++ q) {
00320 //
00321 //          uint32_t qNum = m_quadNumber[q]; 
00322 //          uint32_t mask = m_roiMask[q];
00323 //
00324 //          for(uint32_t sect=0; sect < 8; sect++) {
00325 //              bool bitIsOn = mask & (1<<sect);
00326 //              if( !bitIsOn ) continue; 
00327 //
00328 //              int ind2x1_in_det = qNum*8 + sect;             
00329 //                    std::memcpy(&arr_det[ind2x1_in_det][0][0], &arr_data[ind2x1_in_data][0][0], size2x1*sizeof(double));
00330 //              ind2x1_in_data ++;
00331 ////      }
00332 //      }        
00333 //      return arr_det;
00334 //  }
00335 //
00336 ////-------------------
00337 
00338 };
00339 
00340 } // namespace CSPadPixCoords
00341 
00342 #endif // CSPADPIXCOORDS_CSPAD2X2CONFIGPARS_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7