CSPadPixCoords/src/PixCoordsTest.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class PixCoordsTest...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "CSPadPixCoords/PixCoordsTest.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <time.h>
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "MsgLogger/MsgLogger.h"
00027 // to work with detector data include corresponding 
00028 // header from psddl_psana package
00029 #include "psddl_psana/acqiris.ddl.h"
00030 #include "PSEvt/EventId.h"
00031 
00032 #include "CSPadPixCoords/Image2D.h"
00033 
00034 //-----------------------------------------------------------------------
00035 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00036 //-----------------------------------------------------------------------
00037 #include <boost/lexical_cast.hpp>
00038 
00039 // This declares this class as psana module
00040 using namespace CSPadPixCoords;
00041 PSANA_MODULE_FACTORY(PixCoordsTest)
00042 
00043 using namespace std;
00044 
00045 //              ----------------------------------------
00046 //              -- Public Function Member Definitions --
00047 //              ----------------------------------------
00048 
00049 namespace CSPadPixCoords {
00050 
00051 //----------------
00052 // Constructors --
00053 //----------------
00054 
00055 PixCoordsTest::PixCoordsTest (const std::string& name)
00056   : Module(name)
00057   , m_calibDir()
00058   , m_typeGroupName()
00059   , m_source()
00060   , m_src()
00061   , m_maxEvents()
00062   , m_filter()
00063   , m_count(0)
00064 {
00065   // get the values from configuration or use defaults
00066   m_calibDir      = configStr("calibDir",      "/reg/d/psdm/CXI/cxi35711/calib");
00067   m_typeGroupName = configStr("typeGroupName", "CsPad::CalibV1");
00068   m_source        = configStr("source",        "CxiDs1.0:Cspad.0");
00069   m_runNumber     = config   ("runNumber",     32U);
00070   m_maxEvents     = config   ("events",        32U);
00071   m_filter        = config   ("filter",        false);
00072   m_src           = m_source;
00073 }
00074 
00075 
00076 //--------------
00077 // Destructor --
00078 //--------------
00079 
00080 PixCoordsTest::~PixCoordsTest ()
00081 {
00082 }
00083 
00084 //--------------------
00085 
00086 /// Method which is called once at the beginning of the job
00087 void 
00088 PixCoordsTest::beginJob(Event& evt, Env& env)
00089 {
00090 }
00091 
00092 //--------------------
00093 
00094 /// Method which is called at the beginning of the run
00095 void 
00096 PixCoordsTest::beginRun(Event& evt, Env& env)
00097 {
00098   cout << "ImageCSPad::beginRun " << endl;
00099 
00100   //m_cspad_calibpar = new PSCalib::CSPadCalibPars(); // get default calib pars from my local directory.
00101   m_cspad_calibpar   = new PSCalib::CSPadCalibPars(m_calibDir, m_typeGroupName, m_source, m_runNumber);
00102   m_pix_coords_2x1   = new CSPadPixCoords::PixCoords2x1   ();
00103   m_pix_coords_quad  = new CSPadPixCoords::PixCoordsQuad  ( m_pix_coords_2x1,  m_cspad_calibpar );
00104   m_pix_coords_cspad = new CSPadPixCoords::PixCoordsCSPad ( m_pix_coords_quad, m_cspad_calibpar );
00105 
00106   m_cspad_calibpar -> printCalibPars();
00107   m_pix_coords_2x1 -> print_member_data();
00108 
00109   this -> getQuadConfigPars(env);
00110 }
00111 
00112 //--------------------
00113 
00114 void 
00115 PixCoordsTest::getQuadConfigPars(Env& env)
00116 {
00117   shared_ptr<Psana::CsPad::ConfigV3> config = env.configStore().get(m_src);
00118   if (config.get()) {
00119       for (uint32_t q = 0; q < config->numQuads(); ++ q) {
00120         m_roiMask[q]         = config->roiMask(q);
00121         m_numAsicsStored[q]  = config->numAsicsStored(q);
00122       }
00123   }
00124 
00125   m_n2x1         = Psana::CsPad::SectorsPerQuad;     // 8
00126   m_ncols2x1     = Psana::CsPad::ColumnsPerASIC;     // 185
00127   m_nrows2x1     = Psana::CsPad::MaxRowsPerASIC * 2; // 388
00128   m_sizeOf2x1Arr = m_nrows2x1 * m_ncols2x1;          // 185*388;
00129 
00130   XCOOR = CSPadPixCoords::PixCoords2x1::X;
00131   YCOOR = CSPadPixCoords::PixCoords2x1::Y;
00132   ZCOOR = CSPadPixCoords::PixCoords2x1::Z;
00133 }
00134 
00135 //--------------------
00136 
00137 /// Method which is called at the beginning of the calibration cycle
00138 void 
00139 PixCoordsTest::beginCalibCycle(Event& evt, Env& env)
00140 {
00141 }
00142 
00143 //--------------------
00144 
00145 /// Method which is called with event data, this is the only required 
00146 /// method, all other methods are optional
00147 void 
00148 PixCoordsTest::event(Event& evt, Env& env)
00149 {
00150   // this is how to gracefully stop analysis job
00151   ++m_count;
00152   if (m_count >= m_maxEvents) stop();
00153   cout << "m_count=" << m_count << endl;
00154 
00155   shared_ptr<Psana::CsPad::DataV2> data2 = evt.get(m_src);
00156   if (data2.get()) {
00157 
00158         this -> test_cspad_init ();
00159 
00160     int nQuads = data2->quads_shape()[0];
00161     cout << "nQuads = " << nQuads << endl;
00162     for (int q = 0; q < nQuads; ++ q) {
00163         const Psana::CsPad::ElementV2& el = data2->quads(q);
00164 
00165         int quad                           = el.quad() ;
00166         const ndarray<const int16_t,3>& data_nda = el.data();
00167         const int16_t* data = data_nda.data();
00168 
00169         CSPadPixCoords::QuadParameters *quadpars = new CSPadPixCoords::QuadParameters(quad, NX_QUAD, NY_QUAD, m_numAsicsStored[q], m_roiMask[q]);
00170 
00171         this -> test_2x1  (data, quadpars, m_cspad_calibpar);
00172         this -> test_quad (data, quadpars, m_cspad_calibpar);
00173 
00174              struct timespec start, stop;
00175              int status = clock_gettime( CLOCK_REALTIME, &start ); // Get LOCAL time
00176         this -> test_cspad (data, quadpars, m_cspad_calibpar);
00177                  status = clock_gettime( CLOCK_REALTIME, &stop ); // Get LOCAL time
00178         cout << "Time to fill quad is " 
00179              << stop.tv_sec - start.tv_sec + 1e-9*(stop.tv_nsec - start.tv_nsec) 
00180              << " sec" << endl;
00181     }
00182 
00183         this -> test_cspad_save ();
00184 
00185   } // if (data2.get())
00186 }
00187 
00188 //--------------------
00189   
00190 /// Method which is called at the end of the calibration cycle
00191 void 
00192 PixCoordsTest::endCalibCycle(Event& evt, Env& env)
00193 {
00194 }
00195 
00196 //--------------------
00197 
00198 /// Method which is called at the end of the run
00199 void 
00200 PixCoordsTest::endRun(Event& evt, Env& env)
00201 {
00202 }
00203 
00204 //--------------------
00205 
00206 /// Method which is called once at the end of the job
00207 void 
00208 PixCoordsTest::endJob(Event& evt, Env& env)
00209 {
00210 }
00211 
00212 //--------------------
00213 
00214 void
00215 PixCoordsTest::test_2x1(const int16_t* data, CSPadPixCoords::QuadParameters* quadpars, PSCalib::CSPadCalibPars *cspad_calibpar)
00216 {
00217         int              quad           = quadpars -> getQuadNumber();
00218         uint32_t         roiMask        = quadpars -> getRoiMask();
00219 
00220         //cout  << "PixCoordsTest::test_2x1:  quadNumber=" << quad << endl;
00221 
00222   // SELECT QUADRANT NUMBER FOR TEST HERE !!!
00223   if(quad != 2) return;
00224  
00225   // SELECT 2x1 ORIENTATION FOR TEST HERE !!!
00226   CSPadPixCoords::PixCoords2x1::ORIENTATION rot=CSPadPixCoords::PixCoords2x1::R000;
00227 
00228   // SELECT 2x1 SECTION NUMBER FOR TEST HERE !!!
00229   uint32_t sect=1;
00230 
00231         bool bitIsOn = roiMask & (1<<sect);
00232         if( !bitIsOn ) return;
00233  
00234         // Initialization
00235         for (unsigned ix=0; ix<NX_2x1; ix++){
00236         for (unsigned iy=0; iy<NY_2x1; iy++){
00237           m_arr_2x1_image[ix][iy] = 0;
00238         }
00239         }
00240 
00241         double  mrgx=20.1;
00242         double  mrgy=20.1;
00243 
00244         const int16_t *data2x1 = &data[sect * m_sizeOf2x1Arr];
00245  
00246         for (uint32_t c=0; c<m_ncols2x1; c++) {
00247         for (uint32_t r=0; r<m_nrows2x1; r++) {
00248 
00249            double xcoor = m_pix_coords_2x1 -> getPixCoorRotN90_pix (rot, XCOOR, r, c);
00250            double ycoor = m_pix_coords_2x1 -> getPixCoorRotN90_pix (rot, YCOOR, r, c);
00251 
00252            int ix = (int) (mrgx + xcoor);
00253            int iy = (int) (mrgy + ycoor);
00254 
00255            if(ix <  0)      continue;
00256            if(iy <  0)      continue;
00257            if(ix >= NX_2x1) continue;
00258            if(iy >= NY_2x1) continue;
00259 
00260            m_arr_2x1_image[ix][iy] = (double)data2x1[c*m_nrows2x1+r];
00261 
00262         }
00263         }
00264 
00265     CSPadPixCoords::Image2D<double> *img2d = new CSPadPixCoords::Image2D<double>(&m_arr_2x1_image[0][0],NX_2x1,NY_2x1);
00266     img2d -> saveImageInFile("test_2x1.txt",0);
00267 }
00268 
00269 //--------------------
00270 
00271 void
00272 PixCoordsTest::test_quad(const int16_t* data, CSPadPixCoords::QuadParameters* quadpars, PSCalib::CSPadCalibPars *cspad_calibpar)
00273 {
00274         int              quad           = quadpars -> getQuadNumber();
00275         uint32_t         roiMask        = quadpars -> getRoiMask();
00276  
00277         cout  << "PixCoordsTest::test_quad:  quadNumber=" << quad << endl;
00278 
00279         // Initialization
00280         for (unsigned ix=0; ix<NX_QUAD; ix++){
00281         for (unsigned iy=0; iy<NY_QUAD; iy++){
00282           m_arr_quad_image[ix][iy] = 0;
00283         }
00284         }
00285 
00286         for(uint32_t sect=0; sect < m_n2x1; sect++)
00287         {
00288              bool bitIsOn = roiMask & (1<<sect);
00289              if( !bitIsOn ) continue;
00290  
00291              const int16_t *data2x1 = &data[sect * m_sizeOf2x1Arr];
00292 
00293              for (uint32_t c=0; c<m_ncols2x1; c++) {
00294              for (uint32_t r=0; r<m_nrows2x1; r++) {
00295 
00296                int ix = (int) m_pix_coords_quad -> getPixCoorRot000_pix (XCOOR, quad, sect, r, c);
00297                int iy = (int) m_pix_coords_quad -> getPixCoorRot000_pix (YCOOR, quad, sect, r, c);
00298 
00299                if(ix <  0)       continue;
00300                if(iy <  0)       continue;
00301                if(ix >= NX_QUAD) continue;
00302                if(iy >= NY_QUAD) continue;
00303 
00304                m_arr_quad_image[ix][iy] = (double)data2x1[c*m_nrows2x1+r];
00305 
00306              }
00307              }
00308         }
00309 
00310         string fname = "test_q";
00311                fname += boost::lexical_cast<string>( quad );
00312                fname += ".txt";
00313  
00314   CSPadPixCoords::Image2D<double> *img2d = new CSPadPixCoords::Image2D<double>(&m_arr_quad_image[0][0],NX_QUAD,NY_QUAD);
00315   img2d -> saveImageInFile(fname,0);
00316 }
00317 
00318 //--------------------
00319 
00320 void
00321 PixCoordsTest::test_cspad_init()
00322 {
00323   // Initialization
00324   for (unsigned ix=0; ix<NX_CSPAD; ix++){
00325   for (unsigned iy=0; iy<NY_CSPAD; iy++){
00326     m_arr_cspad_image[ix][iy] = 0;
00327   }
00328   }
00329   m_cspad_ind = 0;
00330   m_coor_x_pix = m_pix_coords_cspad -> getPixCoorArrX_pix();
00331   m_coor_y_pix = m_pix_coords_cspad -> getPixCoorArrY_pix();
00332   m_coor_x_int = m_pix_coords_cspad -> getPixCoorArrX_int();
00333   m_coor_y_int = m_pix_coords_cspad -> getPixCoorArrY_int();
00334 }
00335 
00336 //--------------------
00337 
00338 void
00339 PixCoordsTest::test_cspad(const int16_t* data, CSPadPixCoords::QuadParameters* quadpars, PSCalib::CSPadCalibPars *cspad_calibpar)
00340 {
00341       //int              quad           = quadpars -> getQuadNumber();
00342         uint32_t         roiMask        = quadpars -> getRoiMask();
00343 
00344         for(uint32_t sect=0; sect < m_n2x1; sect++)
00345         {
00346              bool bitIsOn = roiMask & (1<<sect);
00347              if( !bitIsOn ) { m_cspad_ind += m_sizeOf2x1Arr; continue; }
00348  
00349              const int16_t *data2x1 = &data[sect * m_sizeOf2x1Arr];
00350 
00351              //cout  << "  add section " << sect << endl;            
00352  
00353              for (uint32_t c=0; c<m_ncols2x1; c++) {
00354              for (uint32_t r=0; r<m_nrows2x1; r++) {
00355 
00356                // This access takes 14ms/quad
00357                //int ix = (int) m_pix_coords_cspad -> getPixCoor_pix (XCOOR, quad, sect, r, c);
00358                //int iy = (int) m_pix_coords_cspad -> getPixCoor_pix (YCOOR, quad, sect, r, c);
00359 
00360                // This access takes 8ms/quad
00361                //int ix = (int) m_coor_x_pix [m_cspad_ind];
00362                //int iy = (int) m_coor_y_pix [m_cspad_ind];
00363 
00364                // This access takes 6ms/quad
00365                int ix = m_coor_x_int [m_cspad_ind];
00366                int iy = m_coor_y_int [m_cspad_ind];
00367                m_cspad_ind++;
00368 
00369                if(ix <  0)        continue;
00370                if(iy <  0)        continue;
00371                if(ix >= NX_CSPAD) continue;
00372                if(iy >= NY_CSPAD) continue;
00373 
00374                m_arr_cspad_image[ix][iy] = (double)data2x1[c*m_nrows2x1+r];
00375              }
00376              }
00377         }
00378 }
00379 
00380 //--------------------
00381 
00382 void
00383 PixCoordsTest::test_cspad_save()
00384 {
00385   CSPadPixCoords::Image2D<double> *img2d = new CSPadPixCoords::Image2D<double>(&m_arr_cspad_image[0][0],NX_CSPAD,NY_CSPAD);
00386   img2d -> saveImageInFile("test_cspad.txt",0);
00387 }
00388 
00389 //--------------------
00390 
00391 } // namespace CSPadPixCoords

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7