00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "ImgPixSpectra/CSPadPixSpectra.h"
00017
00018
00019
00020
00021 #include <fstream>
00022
00023
00024
00025
00026 #include "MsgLogger/MsgLogger.h"
00027
00028
00029
00030
00031 #include "PSEvt/EventId.h"
00032 #include "CSPadPixCoords/Image2D.h"
00033
00034
00035
00036
00037
00038
00039 using namespace ImgPixSpectra;
00040 PSANA_MODULE_FACTORY(CSPadPixSpectra)
00041
00042
00043
00044
00045
00046 namespace ImgPixSpectra {
00047
00048
00049
00050
00051 CSPadPixSpectra::CSPadPixSpectra (const std::string& name)
00052 : Module(name)
00053 , m_src()
00054 , m_key()
00055 , m_maxEvents()
00056 , m_amin()
00057 , m_amax()
00058 , m_nbins()
00059 , m_arr_fname()
00060 , m_filter()
00061 , m_count(0)
00062 {
00063
00064 m_src = configStr("source", "CxiDs1.0:Cspad.0");
00065 m_key = configStr("inputKey", "");
00066 m_maxEvents = config ("events", 1<<31U);
00067 m_amin = config ("amin", 0.);
00068 m_amax = config ("amax", 1000.);
00069 m_nbins = config ("nbins", 100);
00070 m_arr_fname = configStr("arr_fname", "cspad_spectral_array.txt");
00071 m_filter = config ("filter", false);
00072 }
00073
00074
00075
00076
00077 CSPadPixSpectra::~CSPadPixSpectra ()
00078 {
00079 }
00080
00081
00082 void
00083 CSPadPixSpectra::beginJob(Event& evt, Env& env)
00084 {
00085 this -> printInputPars();
00086 this -> getQuadConfigPars(env);
00087 this -> printQuadConfigPars();
00088 this -> arrayInit();
00089 }
00090
00091
00092 void
00093 CSPadPixSpectra::beginRun(Event& evt, Env& env)
00094 {
00095 }
00096
00097
00098 void
00099 CSPadPixSpectra::beginCalibCycle(Event& evt, Env& env)
00100 {
00101 }
00102
00103
00104
00105 void
00106 CSPadPixSpectra::event(Event& evt, Env& env)
00107 {
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 if (m_count >= m_maxEvents) {stop(); return;}
00120
00121
00122
00123 shared_ptr<CSPadDataType> data = evt.get(m_src, m_key, &m_actualSrc);
00124
00125 if ( m_count<5
00126 or (m_count<500 and m_count%100 == 0)
00127 or m_count%1000 == 0 ) WithMsgLog(name(), info, log) { log << "event=" << m_count; }
00128
00129 if (data.get()) {
00130 this -> loopOverQuads(data);
00131 }
00132
00133
00134 ++ m_count;
00135 }
00136
00137
00138 void
00139 CSPadPixSpectra::endCalibCycle(Event& evt, Env& env)
00140 {
00141 }
00142
00143
00144 void
00145 CSPadPixSpectra::endRun(Event& evt, Env& env)
00146 {
00147 }
00148
00149
00150 void
00151 CSPadPixSpectra::endJob(Event& evt, Env& env)
00152 {
00153 this -> saveArrayInFile();
00154 this -> saveShapeInFile();
00155 this -> arrayDelete();
00156 }
00157
00158
00159
00160
00161
00162
00163 void
00164 CSPadPixSpectra::arrayInit()
00165 {
00166 m_factor = double(m_nbins) / (m_amax-m_amin);
00167 m_nbins1 = m_nbins - 1;
00168
00169 int size = m_sizeOfCSPadArr * m_nbins;
00170 m_arr = new int [size];
00171 for(int i=0; i<size; i++) m_arr[i] = 0;
00172
00173
00174 }
00175
00176
00177
00178 void
00179 CSPadPixSpectra::arrayDelete()
00180 {
00181 delete [] m_arr;
00182 }
00183
00184
00185
00186 void
00187 CSPadPixSpectra::loopOverQuads(shared_ptr<CSPadDataType> data)
00188 {
00189 m_pixel_ind = 0;
00190 int nQuads = data -> quads_shape()[0];
00191
00192 for (int q = 0; q < nQuads; ++ q) {
00193 const Psana::CsPad::ElementV2& el = data->quads(q);
00194
00195 int quad = el.quad() ;
00196 const ndarray<const int16_t,3>& data_nda = el.data();
00197 const int16_t* data = &data_nda[0][0][0];
00198
00199
00200 this -> arrayFill (quad, data, m_roiMask[q]);
00201 }
00202 }
00203
00204
00205
00206 void
00207 CSPadPixSpectra::arrayFill(int quad, const int16_t* data, uint32_t roiMask)
00208 {
00209 for(uint32_t sect=0; sect < m_n2x1; sect++)
00210 {
00211 bool bitIsOn = roiMask & (1<<sect);
00212 if( !bitIsOn ) { m_pixel_ind += m_sizeOf2x1Arr; continue; }
00213
00214 const int16_t *data2x1 = &data[sect * m_sizeOf2x1Arr];
00215
00216
00217
00218
00219
00220
00221
00222 for (uint32_t i=0; i<m_sizeOf2x1Arr; i++) {
00223 double amp = (double)data2x1[i];
00224 int iamp = this -> ampToIndex(amp);
00225
00226 int ipix = m_pixel_ind ++;
00227 m_arr[ipix * m_nbins + iamp] ++;
00228 }
00229 }
00230 }
00231
00232
00233
00234 int
00235 CSPadPixSpectra::ampToIndex(double amp)
00236 {
00237 int ind = (int) (m_factor*(amp-m_amin));
00238 if( ind < 0 ) return 0;
00239 if( ind > m_nbins1) return m_nbins1;
00240 return ind;
00241 }
00242
00243
00244
00245 void
00246 CSPadPixSpectra::getQuadConfigPars(Env& env)
00247 {
00248 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV2>(env) ) return;
00249 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV3>(env) ) return;
00250 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV4>(env) ) return;
00251 if ( getQuadConfigParsForType<Psana::CsPad::ConfigV5>(env) ) return;
00252
00253 MsgLog(name(), warning, "CsPad::ConfigV2 - V5 is not available in this run.");
00254 }
00255
00256
00257
00258 void
00259 CSPadPixSpectra::printInputPars()
00260 {
00261 WithMsgLog(name(), info, log) { log
00262 << "\n Input parameters:"
00263
00264 << "\n m_key " << m_key
00265 << "\n m_maxEvents " << m_maxEvents
00266 << "\n m_amin " << m_amin
00267 << "\n m_amax " << m_amax
00268 << "\n m_nbins " << m_nbins
00269 << "\n m_arr_fname " << m_arr_fname
00270
00271 << "\n";
00272 }
00273 }
00274
00275
00276
00277 void
00278 CSPadPixSpectra::printQuadConfigPars()
00279 {
00280 WithMsgLog(name(), info, log) { log
00281 << "\n CSPad configuration parameters:"
00282 << "\n m_nquads = " << m_nquads
00283 << "\n m_n2x1 in quad = " << m_n2x1
00284 << "\n m_ncols2x1 = " << m_ncols2x1
00285 << "\n m_nrows2x1 = " << m_nrows2x1
00286 << "\n m_sizeOf2x1Arr = " << m_sizeOf2x1Arr
00287 << "\n m_sizeOfQuadArr = " << m_sizeOfQuadArr
00288 << "\n m_sizeOfCSPadArr = " << m_sizeOfCSPadArr;
00289
00290 for (uint32_t q = 0; q < m_nquads; ++ q) { log
00291 << "\nq = " << q
00292 << " m_roiMask[q] = " << m_roiMask[q]
00293 << " m_numAsicsStored[q] = " << m_numAsicsStored[q];
00294 }
00295 log << "\n";
00296 }
00297 }
00298
00299
00300
00301 void
00302 CSPadPixSpectra::saveArrayInFile()
00303 {
00304 MsgLog(name(), info, "Save the spectral array in file " << m_arr_fname);
00305 CSPadPixCoords::Image2D<int>* arr = new CSPadPixCoords::Image2D<int>(&m_arr[0], m_sizeOfCSPadArr, m_nbins);
00306 arr -> saveImageInFile(m_arr_fname,0);
00307 }
00308
00309
00310
00311 void
00312 CSPadPixSpectra::saveShapeInFile()
00313 {
00314 m_arr_shape_fname = m_arr_fname + ".sha";
00315 MsgLog(name(), info, "Save the spectral array configuration in file " << m_arr_shape_fname);
00316 std::ofstream file;
00317 file.open(m_arr_shape_fname.c_str(), std::ios_base::out);
00318 file << "NPIXELS " << m_sizeOfCSPadArr << "\n";
00319 file << "NBINS " << m_nbins << "\n";
00320 file << "AMIN " << m_amin << "\n";
00321 file << "AMAX " << m_amax << "\n";
00322 file << "NEVENTS " << m_count << "\n";
00323 file << "ARRFNAME " << m_arr_fname << "\n";
00324 file.close();
00325 }
00326
00327
00328
00329
00330 }