cspad_mod/src/DataProxyT.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: DataProxyT.cpp 5253 2013-01-30 17:56:28Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class DataProxyT...
00007 //
00008 // Author List:
00009 //      Andy Salnikov
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "cspad_mod/DataProxyT.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 
00022 //-------------------------------
00023 // Collaborating Class Headers --
00024 //-------------------------------
00025 #include "MsgLogger/MsgLogger.h"
00026 #include "pdscalibdata/CsPadCommonModeSubV1.h"
00027 #include "pdscalibdata/CsPadPedestalsV1.h"
00028 #include "pdscalibdata/CsPadPixelGainV1.h"
00029 #include "pdscalibdata/CsPadPixelStatusV1.h"
00030 
00031 //-----------------------------------------------------------------------
00032 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00033 //-----------------------------------------------------------------------
00034 
00035 namespace {
00036 
00037   const char logger[] = "DataProxyT";
00038 
00039 }
00040 
00041 //              ----------------------------------------
00042 //              -- Public Function Member Definitions --
00043 //              ----------------------------------------
00044 
00045 namespace cspad_mod {
00046 
00047 //----------------
00048 // Constructors --
00049 //----------------
00050 template <typename DataType, typename ElemType>
00051 DataProxyT<DataType, ElemType>::DataProxyT (const PSEvt::EventKey& key, PSEnv::EnvObjectStore& calibStore)
00052   : PSEvt::Proxy<DataIfaceType>()
00053   , m_key(key)
00054   , m_calibStore(calibStore)
00055   , m_data()
00056 {
00057 }
00058 
00059 //--------------
00060 // Destructor --
00061 //--------------
00062 template <typename DataType, typename ElemType>
00063 DataProxyT<DataType, ElemType>::~DataProxyT ()
00064 {
00065 }
00066 
00067 template <typename DataType, typename ElemType>
00068 boost::shared_ptr<typename DataType::IfaceType>
00069 DataProxyT<DataType, ElemType>::getTypedImpl(PSEvt::ProxyDictI* dict, const Pds::Src& source, const std::string& key)
00070 {
00071   if (m_data.get()) return m_data;
00072 
00073   // get the original object
00074   boost::shared_ptr<void> vptr = dict->get(m_key.typeinfo(), PSEvt::Source(m_key.src()), m_key.key(), 0);
00075   if (not vptr.get()) return m_data;
00076   boost::shared_ptr<DataIfaceType> obj = boost::static_pointer_cast<DataIfaceType>(vptr);
00077 
00078   // get calibration data
00079   boost::shared_ptr<pdscalibdata::CsPadPedestalsV1> pedestals = m_calibStore.get(m_key.src());
00080   boost::shared_ptr<pdscalibdata::CsPadPixelGainV1> pixelGain = m_calibStore.get(m_key.src());
00081   boost::shared_ptr<pdscalibdata::CsPadPixelStatusV1> pixStatusCalib = m_calibStore.get(m_key.src());
00082   boost::shared_ptr<pdscalibdata::CsPadCommonModeSubV1> cModeCalib = m_calibStore.get(m_key.src());
00083 
00084   // make new instance
00085   m_data.reset(new DataType());
00086 
00087   // number of elements
00088   const int nElem = obj->quads_shape()[0];
00089 
00090   for (int iElem = 0; iElem != nElem; ++ iElem) {
00091 
00092     const ElemIfaceType& elem = obj->quads(iElem);
00093 
00094     // quadrant number
00095     int iq = elem.quad();
00096 
00097     // data array
00098     const ndarray<const int16_t, 3>& data = elem.data();
00099 
00100     uint32_t sMask = elem.sectionMask();
00101 
00102     // get few constants
00103     const unsigned nSect = data.shape()[0];
00104     const unsigned ssize = Pds::CsPad::ColumnsPerASIC*Pds::CsPad::MaxRowsPerASIC*2;
00105 
00106     // make data arrays
00107     int16_t* pixelData = new int16_t[nSect*ssize];
00108     float commonMode[nSect];
00109 
00110     // loop over sections
00111     for ( unsigned is = 0, sect = 0 ; is != Pds::CsPad::ASICsPerQuad/2 ; ++ is ) {
00112 
00113       if ( not (sMask & (1 << is))) continue;
00114 
00115       // start of pixel data
00116       const int16_t* sdata = &data[sect][0][0];
00117 
00118       // output pixel data
00119       int16_t* output = pixelData + sect*ssize;
00120 
00121       // default common mode value for this section,
00122       // large negative number means unknown
00123       commonMode[sect] = pdscalibdata::CsPadCommonModeSubV1::UnknownCM;
00124 
00125       // status codes for pixels
00126       const uint16_t* pixStatus = 0;
00127       if (pixStatusCalib.get()) {
00128         pixStatus = &pixStatusCalib->status()[iq][is][0][0];
00129       }
00130 
00131       // this sector's pedestal data
00132       const float* peddata = 0;
00133       if (pedestals.get()) {
00134         peddata = &pedestals->pedestals()[iq][is][0][0];
00135       }
00136 
00137       // this sector's pixel gain data
00138       const float* gaindata = 0;
00139       if (pixelGain) {
00140         gaindata = &pixelGain->pixelGains()[iq][is][0][0];
00141       }
00142 
00143       // calculate common mode if requested
00144       float cmode = 0;
00145       if (cModeCalib.get()) {
00146         MsgLog(logger, debug, "calculating common mode for s=" << sect);
00147         cmode = cModeCalib->findCommonMode(sdata, peddata, pixStatus, ssize);
00148         if (cmode == pdscalibdata::CsPadCommonModeSubV1::UnknownCM) {
00149           // reset subtracted value
00150           cmode = 0;
00151         } else {
00152           // remember it
00153           commonMode[sect] = cmode;
00154         }
00155       }
00156 
00157       // subtract pedestals and common mode, apply pixel gain, and round to
00158       // nearest int; pixel gain is _inverse_ gain so we multiply it
00159       if (peddata and gaindata) {
00160         for (unsigned i = 0; i != ssize; ++ i) {
00161           double val = (sdata[i] - peddata[i] - cmode) * gaindata[i];
00162           output[i] = val < 0 ? int(val - 0.5) : int(val + 0.5);
00163         }
00164       } else if (peddata) {
00165         for (unsigned i = 0; i != ssize; ++ i) {
00166           double val = sdata[i] - peddata[i] - cmode;
00167           output[i] = val < 0 ? int(val - 0.5) : int(val + 0.5);
00168         }
00169       } else if (gaindata) {
00170         for (unsigned i = 0; i != ssize; ++ i) {
00171           double val = (sdata[i] - cmode) * gaindata[i];
00172           output[i] = val < 0 ? int(val - 0.5) : int(val + 0.5);
00173         }
00174       } else {
00175         for (unsigned i = 0; i != ssize; ++ i) {
00176           double val = sdata[i] - cmode;
00177           output[i] = val < 0 ? int(val - 0.5) : int(val + 0.5);
00178         }
00179       }
00180 
00181       ++ sect;
00182 
00183     }
00184 
00185     m_data->append(new ElemType(elem, pixelData, commonMode));
00186   }
00187 
00188   return m_data;
00189 }
00190 
00191 // explicit instatiation
00192 template class DataProxyT<cspad_mod::DataV1, cspad_mod::ElementV1>;
00193 template class DataProxyT<cspad_mod::DataV2, cspad_mod::ElementV2>;
00194 
00195 } // namespace cspad_mod

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7