00001 #ifndef CSPADPIXCOORDS_CSPADCONFIGPARS_H 00002 #define CSPADPIXCOORDS_CSPADCONFIGPARS_H 00003 00004 //-------------------------------------------------------------------------- 00005 // File and Version Information: 00006 // $Id: CSPadConfigPars.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $ 00007 // 00008 // Description: 00009 // Class CSPadConfigPars. 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/cspad.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 CSPadConfigPars is a store for CSPAD configuration parameters. 00057 * 00058 * <h1>Interface Description</h1> 00059 * 00060 * @li Include and typedef 00061 * @code 00062 * #include "CSPadPixCoords/CSPadConfigPars.h" 00063 * typedef CSPadPixCoords::CSPadConfigPars 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(CxiDs1.0:Cspad.0)"); 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 -> setCSPadConfigPars (evt, env); 00086 * // or its separate private methods 00087 * bool is_set = config -> setCSPadConfigParsFromEnv (env); 00088 * bool is_set = config -> setCSPadConfigParsFromEvent (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 numQuads = 4; 00094 * uint32_t quadNumber[] = {0, 1, 2, 3}; 00095 * uint32_t roiMask[] = {0377, 0377, 0377, 0377}; 00096 * CONFIG* config = new CSPadConfigPars::CSPadConfigPars ( numQuads, quadNumber, roiMask ); 00097 * @endcode 00098 * 00099 * 00100 * @li Print current configuration parameters 00101 * @code 00102 * config -> printCSPadConfigPars(); 00103 * @endcode 00104 * 00105 * 00106 * @li Get methods for member data \n 00107 * @code 00108 * uint32_t numQuads = config -> numberOfQuadsStored (); 00109 * uint32_t qNum = config -> quadNumber (iq); 00110 * uint32_t n2x1 = config -> numberOf2x1Stored (iq); 00111 * uint32_t quadMask = config -> roiMask (iq); 00112 * @endcode 00113 * 00114 * 00115 * @li Conversion between entire CSPAD pixel array shaped as (32,185,388) and data array shaped as (N,185,388), where N<=32 \n 00116 * Conversion from (32,185,388) to (N,185,388) 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 (N,185,388) to (32,185,388) 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 * @version \$Id: CSPadConfigPars.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $ 00129 * 00130 * @author Mikhail S. Dubrovin 00131 */ 00132 00133 class CSPadConfigPars { 00134 public: 00135 00136 //typedef CSPadPixCoords::PixCoords2x1V2 PC2X1; 00137 //typedef PixCoords2x1V2 PC2X1; 00138 00139 const static unsigned NQuadsMax = 4; 00140 00141 /// @brief Default constructor 00142 CSPadConfigPars () ; 00143 00144 /** 00145 * @brief Constructor using specified source as input parameter 00146 * @param[in] source (def.= "DetInfo(CxiDs1.0:Cspad.0)") 00147 */ 00148 CSPadConfigPars (PSEvt::Source source) ; 00149 00150 /** 00151 * @brief Constructor with explicit defenition of configuration parameters 00152 * @param[in] numQuads (def.= 4) 00153 * @param[in] quadNumber[] (def.= {0,1,2,3}) 00154 * @param[in] roiMask[] (def.= {0377,0377,0377,0377}, or in decimal {255,255,255,255}) 00155 */ 00156 CSPadConfigPars ( uint32_t numQuads, // 4 00157 uint32_t quadNumber[], // {0,1,2,3}, 00158 uint32_t roiMask[] // {0377,0377,0377,0377} // in octal or in decimal: {255,255,255,255} 00159 ) ; 00160 00161 /// @brief Destructor 00162 virtual ~CSPadConfigPars () ; 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 setCSPadConfigPars(PSEvt::Event& evt, PSEnv::Env& env); 00170 00171 /// Sets CSPAD configuration parameters to their default values 00172 void setCSPadConfigParsDefault(); 00173 00174 /// Prints CSPAD configuration parameters 00175 void printCSPadConfigPars(); 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 number of quads stored in data array (def.= 4) 00184 uint32_t numberOfQuadsStored () { return m_numQuads; } 00185 00186 /// Returns the quad number for its index iq in the range [0,m_numQuads] 00187 uint32_t quadNumber (int iq) { return m_quadNumber[iq]; } 00188 00189 /// Returns the number of 2x1s in the quad with index iq (def.= 8) 00190 uint32_t numberOf2x1Stored (int iq) { return m_num2x1Stored[iq]; } 00191 00192 /// Returns the mask for 2x1s in the quad with index iq (def.=0377 for all 8 2x1s) 00193 uint32_t roiMask (int iq) { return m_roiMask[iq]; } 00194 00195 /// Returns the number of 2x1s available in the CSPAD detector (def.= 32) 00196 uint32_t num2x1StoredInData () { return m_num2x1StoredInData; } 00197 00198 /// Returns status: true if configuration parameters are set from env and evt, otherwise false. 00199 bool isSet() { return m_is_set; } 00200 00201 //uint32_t numberOfAsicsStored (int iq) { return m_numAsicsStored[iq]; } 00202 00203 //-------------------- 00204 00205 protected: 00206 00207 /// part of the setCSPadConfigPars(PSEvt::Event& evt, PSEnv::Env& env) 00208 /// @param[in] env pointer to the environment store 00209 bool setCSPadConfigParsFromEnv(PSEnv::Env& env); 00210 00211 /// part of the setCSPadConfigPars(PSEvt::Event& evt, PSEnv::Env& env) 00212 /// @param[in] evt pointer to the event store 00213 bool setCSPadConfigParsFromEvent(PSEvt::Event& evt); 00214 00215 private: 00216 00217 PSEvt::Source m_source; /// member data for data source set from config file 00218 Pds::Src m_src; /// source address as Pds::Src 00219 00220 unsigned m_count_cfg; 00221 unsigned m_count_wornings; 00222 std::string m_config_vers; 00223 std::string m_data_vers; 00224 bool m_is_set_for_evt; 00225 bool m_is_set_for_env; 00226 bool m_is_set; 00227 00228 // Parameters form Psana::CsPad::ConfigV# object 00229 uint32_t m_roiMask [4]; /// mask for turrned on/off (1/0) 2x1s 00230 00231 // Parameters form Psana::CsPad::DataV# and Psana::CsPad::ElementV# object 00232 uint32_t m_numQuads; /// number of quads in data 00233 uint32_t m_quadNumber [4]; /// quad numbers for index in the range [0, m_numQuads] 00234 uint32_t m_num2x1Stored [4]; /// number of 2x1s in quad by index 00235 uint32_t m_num2x1StoredInData; /// number of 2x1s in CSPAD stored in data 00236 00237 //------------------- 00238 /** 00239 * @brief Gets m_src, m_roiMask[q] and m_num2x1Stored[q] from the Psana::CsPad::ConfigV# object. 00240 */ 00241 template <typename T> 00242 bool getQuadConfigParsForType(PSEnv::Env& env) { 00243 00244 boost::shared_ptr<T> config = env.configStore().get(m_source, &m_src); 00245 if (config.get()) { 00246 m_num2x1StoredInData = 0; 00247 for (uint32_t q = 0; q < NQuadsMax; ++ q) { 00248 m_roiMask[q] = config->roiMask(q); 00249 m_num2x1StoredInData += getNum2x1InMask(m_roiMask[q]); 00250 } 00251 ++ m_count_cfg; 00252 return true; 00253 } 00254 return false; 00255 } 00256 00257 //------------------- 00258 /** 00259 * @brief Gets m_numQuads and m_quadNumber[q] and m_num2x1Stored[q] from the Psana::CsPad::DataV# and ElementV# objects. 00260 */ 00261 template <typename TDATA, typename TELEMENT> 00262 bool getCSPadConfigFromDataForType(PSEvt::Event& evt) { 00263 00264 std::string key=""; // FOR RAW CSPAD DATA 00265 00266 boost::shared_ptr<TDATA> data = evt.get(m_source, key, &m_src); 00267 if (data.get()) { 00268 m_numQuads = data->quads_shape()[0]; 00269 m_num2x1StoredInData = 0; 00270 for (uint32_t q = 0; q < m_numQuads; ++ q) { 00271 const TELEMENT& el = data->quads(q); 00272 m_quadNumber[q] = el.quad(); 00273 m_num2x1Stored[q] = el.data().shape()[0]; 00274 m_num2x1StoredInData += m_num2x1Stored[q]; 00275 } 00276 return true; 00277 } 00278 return false; 00279 } 00280 00281 //------------------- 00282 public: 00283 00284 /** 00285 * @brief Convers entire CSPAD pixel array, ndarray<T,3> with shape (32,185,388), 00286 * to the data array, ndarray<T,3> shaped as (N,185,388) 00287 */ 00288 template <typename T> 00289 ndarray<T,3> getCSPadPixNDArrShapedAsData( ndarray<T,3>& arr_det ) { 00290 00291 const unsigned* shape = arr_det.shape(); 00292 unsigned size2x1 = shape[1]*shape[2]; 00293 00294 ndarray<T,3> arr_data = make_ndarray<T>(m_num2x1StoredInData, shape[1], shape[2]); 00295 00296 uint32_t ind2x1_in_data = 0; 00297 for (uint32_t q = 0; q < m_numQuads; ++ q) { 00298 00299 uint32_t qNum = m_quadNumber[q]; 00300 uint32_t mask = m_roiMask[q]; 00301 00302 for(uint32_t sect=0; sect < 8; sect++) { 00303 bool bitIsOn = mask & (1<<sect); 00304 if( !bitIsOn ) continue; 00305 00306 int ind2x1_in_det = qNum*8 + sect; 00307 std::memcpy(&arr_data[ind2x1_in_data][0][0], &arr_det[ind2x1_in_det][0][0], size2x1*sizeof(T)); 00308 ind2x1_in_data ++; 00309 } 00310 } 00311 return arr_data; 00312 } 00313 00314 //------------------- 00315 00316 /** 00317 * @brief Convers entire CSPAD pixel data array, ndarray<T,3> shaped as (N,185,388) 00318 * to the entire CSPAD pixel array, ndarray<T,3> with shape (32,185,388), 00319 */ 00320 template <typename T> 00321 ndarray<T,3> getCSPadPixNDArrFromNDArrShapedAsData( ndarray<T,3>& arr_data, T default_value=0 ) { 00322 00323 const unsigned* shape = arr_data.shape(); 00324 unsigned size2x1 = shape[1]*shape[2]; 00325 //cout << "size2x1 = " << size2x1 << endl; 00326 00327 int numPixTotal = 32*size2x1; 00328 ndarray<T,3> arr_det = make_ndarray<T>(32, shape[1], shape[2]); 00329 std::fill_n(&arr_det[0][0][0], numPixTotal, default_value); 00330 00331 uint32_t ind2x1_in_data = 0; 00332 for (uint32_t q = 0; q < m_numQuads; ++ q) { 00333 00334 uint32_t qNum = m_quadNumber[q]; 00335 uint32_t mask = m_roiMask[q]; 00336 00337 for(uint32_t sect=0; sect < 8; sect++) { 00338 bool bitIsOn = mask & (1<<sect); 00339 if( !bitIsOn ) continue; 00340 00341 int ind2x1_in_det = qNum*8 + sect; 00342 std::memcpy(&arr_det[ind2x1_in_det][0][0], &arr_data[ind2x1_in_data][0][0], size2x1*sizeof(T)); 00343 ind2x1_in_data ++; 00344 } 00345 } 00346 return arr_det; 00347 } 00348 00349 //------------------- 00350 00351 }; 00352 00353 } // namespace CSPadPixCoords 00354 00355 #endif // CSPADPIXCOORDS_CSPADCONFIGPARS_H