00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "cspad_mod/DataProxyT.h"
00017
00018
00019
00020
00021
00022
00023
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
00033
00034
00035 namespace {
00036
00037 const char logger[] = "DataProxyT";
00038
00039 }
00040
00041
00042
00043
00044
00045 namespace cspad_mod {
00046
00047
00048
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
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
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
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
00085 m_data.reset(new DataType());
00086
00087
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
00095 int iq = elem.quad();
00096
00097
00098 const ndarray<const int16_t, 3>& data = elem.data();
00099
00100 uint32_t sMask = elem.sectionMask();
00101
00102
00103 const unsigned nSect = data.shape()[0];
00104 const unsigned ssize = Pds::CsPad::ColumnsPerASIC*Pds::CsPad::MaxRowsPerASIC*2;
00105
00106
00107 int16_t* pixelData = new int16_t[nSect*ssize];
00108 float commonMode[nSect];
00109
00110
00111 for ( unsigned is = 0, sect = 0 ; is != Pds::CsPad::ASICsPerQuad/2 ; ++ is ) {
00112
00113 if ( not (sMask & (1 << is))) continue;
00114
00115
00116 const int16_t* sdata = &data[sect][0][0];
00117
00118
00119 int16_t* output = pixelData + sect*ssize;
00120
00121
00122
00123 commonMode[sect] = pdscalibdata::CsPadCommonModeSubV1::UnknownCM;
00124
00125
00126 const uint16_t* pixStatus = 0;
00127 if (pixStatusCalib.get()) {
00128 pixStatus = &pixStatusCalib->status()[iq][is][0][0];
00129 }
00130
00131
00132 const float* peddata = 0;
00133 if (pedestals.get()) {
00134 peddata = &pedestals->pedestals()[iq][is][0][0];
00135 }
00136
00137
00138 const float* gaindata = 0;
00139 if (pixelGain) {
00140 gaindata = &pixelGain->pixelGains()[iq][is][0][0];
00141 }
00142
00143
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
00150 cmode = 0;
00151 } else {
00152
00153 commonMode[sect] = cmode;
00154 }
00155 }
00156
00157
00158
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
00192 template class DataProxyT<cspad_mod::DataV1, cspad_mod::ElementV1>;
00193 template class DataProxyT<cspad_mod::DataV2, cspad_mod::ElementV2>;
00194
00195 }