CSPadPixCoords/include/CSPad2x2ImageProducer.h

Go to the documentation of this file.
00001 #ifndef CSPADPIXCOORDS_CSPAD2X2IMAGEPRODUCER_H
00002 #define CSPADPIXCOORDS_CSPAD2X2IMAGEPRODUCER_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: CSPad2x2ImageProducer.h 9051 2014-10-09 21:31:20Z dubrovin@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class CSPad2x2ImageProducer.
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 #include "PSCalib/CSPad2x2CalibPars.h"
00027 #include "CSPadPixCoords/PixCoordsCSPad2x2V2.h"
00028 #include "CSPadPixCoords/GlobalMethods.h"
00029 
00030 //------------------------------------
00031 // Collaborating Class Declarations --
00032 //------------------------------------
00033 
00034 #include "PSEvt/Source.h"
00035 
00036 
00037 //              ---------------------
00038 //              -- Class Interface --
00039 //              ---------------------
00040 
00041 namespace CSPadPixCoords {
00042 
00043 /// @addtogroup CSPadPixCoords
00044 
00045 /**
00046  *  @ingroup CSPadPixCoords
00047  *
00048  *  @brief CSPad2x2ImageProducer produces the CSPad2x2 image for each event and add it to the event in psana framework.
00049  *
00050  *  CSPad2x2ImageProducer works in psana framework. It does a few operation as follows:
00051  *  1) get the pixel coordinates from PixCoords2x1 and PixCoordsCSPad2x2 classes,
00052  *  2) get data from the event,
00053  *  3) produce the Image2D object with CSPad image for each event,
00054  *  4) add the Image2D object in the event for further modules.
00055  *
00056  *  The CSPad2x2 image array currently is shaped as [400][400] pixels.
00057  *
00058  *  This class should not be used directly in the code of users modules. 
00059  *  Instead, it should be added as a module in the psana.cfg file with appropriate parameters.
00060  *  Then, the produced Image2D object can be extracted from event and used in other modules.
00061  *
00062  *  This software was developed for the LCLS project.  If you use all or 
00063  *  part of it, please give an appropriate acknowledgment.
00064  *
00065  *  @see PixCoords2x1, PixCoordsQuad, PixCoordsCSPad, CSPadImageGetTest
00066  *
00067  *  @version \$Id: CSPad2x2ImageProducer.h 9051 2014-10-09 21:31:20Z dubrovin@SLAC.STANFORD.EDU $
00068  *
00069  *  @author Mikhail S. Dubrovin
00070  */
00071 
00072 class CSPad2x2ImageProducer : public Module {
00073 public:
00074 
00075   typedef CSPadPixCoords::PixCoordsCSPad2x2V2 PC2X2;
00076 
00077   const static int NX_CSPAD2X2=400; 
00078   const static int NY_CSPAD2X2=400;
00079 
00080   // Default constructor
00081   CSPad2x2ImageProducer (const std::string& name) ;
00082 
00083   // Destructor
00084   virtual ~CSPad2x2ImageProducer () ;
00085 
00086   /// Method which is called once at the beginning of the job
00087   virtual void beginJob(Event& evt, Env& env);
00088   
00089   /// Method which is called at the beginning of the run
00090   virtual void beginRun(Event& evt, Env& env);
00091   
00092   /// Method which is called at the beginning of the calibration cycle
00093   virtual void beginCalibCycle(Event& evt, Env& env);
00094   
00095   /// Method which is called with event data, this is the only required 
00096   /// method, all other methods are optional
00097   virtual void event(Event& evt, Env& env);
00098   
00099   /// Method which is called at the end of the calibration cycle
00100   virtual void endCalibCycle(Event& evt, Env& env);
00101 
00102   /// Method which is called at the end of the run
00103   virtual void endRun(Event& evt, Env& env);
00104 
00105   /// Method which is called once at the end of the job
00106   virtual void endJob(Event& evt, Env& env);
00107 
00108 
00109 protected:
00110 
00111   void printInputParameters();
00112   void getConfigPars(Env& env);
00113   bool getGeometryPars(const std::string& calib_dir, const int runnum, const unsigned prbits);
00114   void getCalibPars(Event& evt, Env& env);
00115   void cspad_image_init();
00116   void processEvent(Event& evt, Env& env);
00117 
00118   //void cspad_image_fill(const int16_t* data, CSPadPixCoords::QuadParameters* quadpars, PSCalib::CSPadCalibPars *cspad_calibpar);
00119   //void cspad_image_fill(const ndarray<const int16_t,3>& data);
00120   void cspad_image_add_in_event(Event& evt);
00121   void checkTypeImplementation();
00122 
00123 private:
00124 
00125   // Data members, this is for example purposes only
00126 
00127   std::string m_calibDir;       // i.e. ./calib
00128   std::string m_typeGroupName;  // i.e. CsPad2x2::CalibV1
00129   //std::string m_str_src;        // i.e. MecTargetChamber.0:Cspad2x2.1
00130   Source      m_source;         // i.e. Detinfo(MecTargetChamber.0:Cspad2x2.1)
00131   Pds::Src    m_src;
00132   std::string m_inkey; 
00133   std::string m_outimgkey;      // i.e. "CSPad:Image"
00134   std::string m_outtype;
00135   bool        m_tiltIsApplied;
00136   bool        m_useWidePixCenter; 
00137   unsigned    m_print_bits;
00138   unsigned    m_count;
00139   unsigned    m_count_cfg;
00140   long        m_count_msg;        // number of messages counter
00141   DATA_TYPE   m_dtype;
00142 
00143   uint32_t m_roiMask;
00144   uint32_t m_numAsicsStored;
00145 
00146   PSCalib::CSPad2x2CalibPars  *m_cspad2x2_calibpars;
00147   PC2X2                       *m_pix_coords_cspad2x2;
00148 
00149   //uint32_t   m_cspad_ind;
00150   //double    *m_coor_x_pix;
00151   //double    *m_coor_y_pix;
00152   uint32_t  *m_coor_x_int;
00153   uint32_t  *m_coor_y_int;
00154 
00155   float      m_common_mode[2];
00156 
00157   double m_arr_cspad2x2_image[NX_CSPAD2X2][NY_CSPAD2X2];
00158 
00159 
00160 public:
00161 
00162   /**
00163    * @brief Get configuration info from Env, return true if configuration is found, othervise false.
00164    * 
00165    */
00166 
00167 //--------------------
00168 
00169   template <typename T>
00170   bool getConfigParsForType(Env& env)
00171   {
00172       shared_ptr<T> config = env.configStore().get(m_source, &m_src);
00173       if (config) {
00174         m_roiMask        = config->roiMask();
00175         m_numAsicsStored = config->numAsicsStored();
00176         ++ m_count_cfg;
00177         WithMsgLog(name(), info, str) {
00178           str << "CsPad2x2::ConfigV"    << m_count_cfg << ":";
00179           str << " roiMask = "          << config->roiMask();
00180           str << " m_numAsicsStored = " << config->numAsicsStored();
00181          }  
00182         return true;
00183       }
00184       return false;
00185   }
00186 //--------------------
00187 
00188   template <typename TOUT>
00189   void save2DArrayInEventForType (Event& evt) {
00190 
00191       const unsigned shape[] = {NX_CSPAD2X2, NY_CSPAD2X2};
00192       ndarray<double,2> img_nda (&m_arr_cspad2x2_image[0][0],shape);
00193       
00194       if (typeid(TOUT) == typeid(double)) { // typeid(double).name()
00195         save2DArrayInEvent<double>(evt, m_src, m_outimgkey, img_nda);
00196         return;
00197       }
00198       
00199       ndarray<TOUT,2> img_out (shape);
00200       
00201       //Copy array with type changing
00202       typename ndarray<TOUT,2>::iterator it_out = img_out.begin(); 
00203       for ( ndarray<const double,2>::iterator it=img_nda.begin(); it!=img_nda.end(); ++it, ++it_out ) {
00204         *it_out = (TOUT)*it;
00205       }
00206 
00207       save2DArrayInEvent<TOUT>(evt, m_src, m_outimgkey, img_out);
00208   }
00209 
00210 //--------------------
00211 
00212   template <typename T>
00213   void cspad_image_fill(const ndarray<const T,3>& data)
00214   {
00215     std::fill_n(&m_arr_cspad2x2_image[0][0], int(NX_CSPAD2X2*NY_CSPAD2X2), double(0));
00216   
00217     for(unsigned sect=0; sect < PC2X2::N2X1_IN_DET; ++sect) {
00218       if ( !(m_roiMask & (1<<sect)) ) continue;
00219    
00220         for (unsigned r=0; r<PC2X2::ROWS2X1; ++r) {
00221         for (unsigned c=0; c<PC2X2::COLS2X1; ++c) {
00222   
00223           //int ix = int (m_pix_coords_cspad2x2 -> getPixCoor_um (PC2X2::AXIS_X, sect, r, c) * PC2X2::UM_TO_PIX);
00224           //int iy = int (m_pix_coords_cspad2x2 -> getPixCoor_um (PC2X2::AXIS_Y, sect, r, c) * PC2X2::UM_TO_PIX);
00225 
00226           unsigned ind = r*PC2X2::COLS2X1*PC2X2::N2X1_IN_DET + c*PC2X2::N2X1_IN_DET + sect;
00227           int ix = m_coor_x_int[ind];
00228           int iy = m_coor_y_int[ind];
00229   
00230           if(ix <  0)           continue;
00231           if(iy <  0)           continue;
00232           if(ix >= NX_CSPAD2X2) continue;
00233           if(iy >= NY_CSPAD2X2) continue;
00234 
00235           m_arr_cspad2x2_image[ix][iy] += (double)data[r][c][sect]; 
00236         }
00237         }
00238     }
00239   }
00240 
00241 //--------------------
00242 
00243   template <typename TELEMENT>
00244   bool procCSPad2x2DataForType (Event& evt) {
00245 
00246     shared_ptr<TELEMENT> elem1 = evt.get(m_source, m_inkey, &m_src); // get m_src here
00247 
00248     if (elem1) {
00249 
00250       for (unsigned i=0; i<PC2X2::N2X1_IN_DET; i++) m_common_mode[i] = elem1->common_mode(i);
00251 
00252       const ndarray<const int16_t, 3>& data_nda = elem1->data();
00253       //const int16_t* data = &data_nda[0][0][0];
00254 
00255       cspad_image_fill <int16_t> (data_nda);
00256       cspad_image_add_in_event(evt);
00257 
00258       return true; 
00259     } // if (elem1)
00260     return false;
00261   }
00262 
00263 //--------------------
00264 
00265   template <typename T>
00266     void procCSPad2x2NDArrForTypeAndNDArr(Event& evt, const ndarray<const T,3>& inp_nda) {
00267        cspad_image_fill <T> (inp_nda);
00268        cspad_image_add_in_event(evt);
00269   }
00270   
00271 //--------------------
00272 
00273   template <typename T>
00274   bool procCSPad2x2NDArrForType (Event& evt) {
00275  
00276     if( m_print_bits & 8 ) MsgLog(name(), warning, "Produce image from CSPAD array, source:" << m_source 
00277                                   << " key:" << m_inkey << " data type:" << typeid(T).name() );
00278     
00279     shared_ptr< ndarray<const T,3> > shp_const = evt.get(m_source, m_inkey, &m_src); // get m_src here
00280     if (shp_const.get()) { procCSPad2x2NDArrForTypeAndNDArr<T>(evt, *shp_const.get()); return true; }
00281     
00282     shared_ptr< ndarray<T,3> > shp = evt.get(m_source, m_inkey, &m_src); // get m_src here
00283     if (shp.get()) { procCSPad2x2NDArrForTypeAndNDArr<T>(evt, *shp.get()); return true; }
00284     
00285     return false;
00286   }
00287   
00288 //--------------------
00289 
00290 
00291 }; // class CSPad2x2ImageProducer
00292 
00293 } // namespace CSPadPixCoords
00294 
00295 #endif // CSPADPIXCOORDS_CSPAD2X2IMAGEPRODUCER_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7