CSPadPixCoords/include/CSPadNDArrProducer.h

Go to the documentation of this file.
00001 #ifndef CSPADPIXCOORDS_CSPADNDARRPRODUCER_H
00002 #define CSPADPIXCOORDS_CSPADNDARRPRODUCER_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: CSPadNDArrProducer.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class CSPadNDArrProducer.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 
00017 //----------------------
00018 // Base Class Headers --
00019 //----------------------
00020 #include "psana/Module.h"
00021 
00022 //-------------------------------
00023 // Collaborating Class Headers --
00024 //-------------------------------
00025 #include "CSPadPixCoords/GlobalMethods.h"
00026 #include "CSPadPixCoords/CSPadConfigPars.h"
00027 
00028 //------------------------------------
00029 // Collaborating Class Declarations --
00030 //------------------------------------
00031 #include "PSEvt/Source.h"
00032 #include "psddl_psana/cspad.ddl.h"
00033 
00034 
00035 //              ---------------------
00036 //              -- Class Interface --
00037 //              ---------------------
00038 
00039 using namespace std;
00040 
00041 namespace CSPadPixCoords {
00042 
00043 /// @addtogroup CSPadPixCoords
00044 
00045 /**
00046  *  @ingroup CSPadPixCoords
00047  *
00048  *  @brief CSPadNDArrProducer produces the CSPad data ndarray<T,3> array for each event and add it to the event in psana framework.
00049  *
00050  *  1) get cspad configuration and data from the event,
00051  *  2) produce the CSPad data ndarray<T,3> array,
00052  *  3) save array in the event for further modules.
00053  *
00054  *  This class should not be used directly in the code of users modules. 
00055  *  Instead, it should be added as a module in the psana.cfg file with appropriate parameters.
00056  *  Then, the produced Image2D object can be extracted from event and used in other modules.
00057  *
00058  *  This software was developed for the LCLS project.  If you use all or 
00059  *  part of it, please give an appropriate acknowledgment.
00060  *
00061  *  @see PixCoords2x1, PixCoordsQuad, PixCoordsCSPad, CSPadImageGetTest
00062  *
00063  *  @version \$Id: CSPadNDArrProducer.h 8040 2014-04-19 01:00:36Z dubrovin@SLAC.STANFORD.EDU $
00064  *
00065  *  @author Mikhail S. Dubrovin
00066  */
00067 
00068 class CSPadNDArrProducer : public Module {
00069 public:
00070 
00071   typedef CSPadPixCoords::CSPadConfigPars CONFIG;
00072 
00073   const static uint32_t NQuadsMax    = 4;
00074   const static uint32_t N2x1         = 8;
00075   const static uint32_t NRows2x1     = 185;
00076   const static uint32_t NCols2x1     = 388;
00077   const static uint32_t SizeOf2x1Arr = NRows2x1 * NCols2x1;
00078 
00079   //enum { NQuadsMax    = Psana::CsPad::MaxQuadsPerSensor  };  // 4
00080   //enum { N2x1         = Psana::CsPad::SectorsPerQuad     };  // 8
00081   //enum { NCols2x1     = Psana::CsPad::ColumnsPerASIC     };  // 185
00082   //enum { NRows2x1     = Psana::CsPad::MaxRowsPerASIC * 2 };  // 388
00083   //enum { SizeOf2x1Arr = NRows2x1 * NCols2x1              };  // 185*388;
00084 
00085   // Default constructor
00086   CSPadNDArrProducer (const std::string& name) ;
00087 
00088   // Destructor
00089   virtual ~CSPadNDArrProducer () ;
00090 
00091   /// Method which is called once at the beginning of the job
00092   virtual void beginJob(Event& evt, Env& env);
00093   
00094   /// Method which is called at the beginning of the run
00095   virtual void beginRun(Event& evt, Env& env);
00096   
00097   /// Method which is called at the beginning of the calibration cycle
00098   virtual void beginCalibCycle(Event& evt, Env& env);
00099   
00100   /// Method which is called with event data, this is the only required 
00101   /// method, all other methods are optional
00102   virtual void event(Event& evt, Env& env);
00103   
00104   /// Method which is called at the end of the calibration cycle
00105   virtual void endCalibCycle(Event& evt, Env& env);
00106 
00107   /// Method which is called at the end of the run
00108   virtual void endRun(Event& evt, Env& env);
00109 
00110   /// Method which is called once at the end of the job
00111   virtual void endJob(Event& evt, Env& env);
00112 
00113 
00114 protected:
00115 
00116   void printInputParameters();
00117   void procEvent(Event& evt, Env& env);
00118   void checkTypeImplementation();
00119 
00120 private:
00121 
00122   // Data members, this is for example purposes only
00123 
00124   Pds::Src    m_src;
00125   Source      m_source;         // Data source set from config file
00126   std::string m_inkey; 
00127   std::string m_outkey;
00128   std::string m_outtype;
00129   bool        m_is_fullsize;
00130   bool        m_is_2darray;
00131   unsigned    m_print_bits;
00132   long        m_count;
00133   DATA_TYPE   m_dtype;
00134   CONFIG*     m_config;
00135 
00136 //-------------------
00137   /**
00138    * @brief For requested m_source and m_inkey process Psana::CsPad::DataV1, or V2
00139    * Returns false if data is missing.
00140    */
00141 
00142   template <typename TDATA, typename TELEMENT, typename TOUT>
00143   bool procCSPadDataForType (Event& evt) {
00144 
00145       typedef int16_t data_cspad_t;
00146 
00147       shared_ptr<TDATA> shp = evt.get(m_source, m_inkey, &m_src); // get m_src here
00148       
00149       if (shp.get()) {
00150 
00151         // Create and initialize the array of the same shape as data, but for all 2x1...
00152         const unsigned shape[] = {m_config->num2x1StoredInData(), NRows2x1, NCols2x1};
00153         ndarray<TOUT,3> out_ndarr(shape);
00154         std::fill(out_ndarr.begin(), out_ndarr.end(), TOUT(0));    
00155 
00156         typename ndarray<TOUT,3>::iterator it_out = out_ndarr.begin(); 
00157         //TOUT* it_out = out_ndarr.data();
00158 
00159         uint32_t numQuads = shp->quads_shape()[0];
00160 
00161         for (uint32_t q = 0; q < numQuads; ++ q) {
00162             const TELEMENT& el = shp->quads(q);      
00163             const ndarray<const data_cspad_t,3>& quad_ndarr = el.data();
00164 
00165             // pixel-by-pixel copy of quad data ndarray to output ndarray with type conversion:
00166             for ( ndarray<const data_cspad_t,3>::iterator it=quad_ndarr.begin(); it!=quad_ndarr.end(); ++it, ++it_out) {
00167               *it_out = (TOUT)*it;
00168             }
00169         }
00170 
00171         if (m_is_fullsize) {
00172              ndarray<TOUT,3> nda_det = m_config->getCSPadPixNDArrFromNDArrShapedAsData<TOUT>(out_ndarr);
00173 
00174              if (m_is_2darray) {
00175                //ndarray<TOUT,2> arr2d = make_ndarray(nda_det.data(), NQuadsMax*N2x1*NRows2x1, NCols2x1);
00176                ndarray<TOUT,2> arr2d = make_ndarray<TOUT>(NQuadsMax*N2x1*NRows2x1, NCols2x1);
00177                std::memcpy(arr2d.begin(), nda_det.begin(), sizeof(TOUT)*32*SizeOf2x1Arr);              
00178                save2DArrayInEvent<TOUT>(evt, m_src, m_outkey, arr2d);
00179              }
00180              else save3DArrInEvent<TOUT>(evt, m_src, m_outkey, nda_det);
00181         }
00182         else save3DArrInEvent<TOUT>(evt, m_src, m_outkey, out_ndarr);
00183 
00184         return true;
00185       } // if (shp.get())
00186       return false;
00187   }
00188 
00189 //--------------------
00190   /**
00191    * @brief Process event for requested output type TOUT
00192    * Returns false if data is missing.
00193    */
00194 
00195   template <typename TOUT>
00196   bool procEventForOutputType (Event& evt) {
00197 
00198     // proc event for available types Psana::CsPad::DataV1, or V2
00199     if ( procCSPadDataForType  <Psana::CsPad::DataV1, Psana::CsPad::ElementV1, TOUT> (evt) ) return true;
00200     if ( procCSPadDataForType  <Psana::CsPad::DataV2, Psana::CsPad::ElementV2, TOUT> (evt) ) return true;
00201     return false;
00202   }
00203 
00204 //-------------------
00205 
00206 };
00207 
00208 } // namespace CSPadPixCoords
00209 
00210 #endif // CSPADPIXCOORDS_CSPADNDARRPRODUCER_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7