00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "ImgPixSpectra/CameraPixSpectra.h"
00017
00018
00019
00020
00021 #include <fstream>
00022
00023
00024
00025
00026 #include "MsgLogger/MsgLogger.h"
00027
00028
00029
00030
00031
00032 #include "PSEvt/EventId.h"
00033 #include "CSPadPixCoords/Image2D.h"
00034 #include "psddl_psana/opal1k.ddl.h"
00035 #include "psddl_psana/princeton.ddl.h"
00036
00037
00038
00039
00040
00041
00042 using namespace ImgPixSpectra;
00043 PSANA_MODULE_FACTORY(CameraPixSpectra)
00044
00045
00046
00047
00048
00049 namespace ImgPixSpectra {
00050
00051
00052
00053
00054 CameraPixSpectra::CameraPixSpectra (const std::string& name)
00055 : Module(name)
00056 , m_src()
00057 , m_key()
00058 , m_amin()
00059 , m_amax()
00060 , m_nbins()
00061 , m_arr_fname()
00062 , m_maxEvents()
00063 , m_filter()
00064 , m_count(0)
00065 {
00066
00067
00068 m_src = configStr("source", "DetInfo(SxrBeamline.0:Opal1000.1)");
00069 m_key = configStr("inputKey", "");
00070 m_amin = config ("amin", 0.);
00071 m_amax = config ("amax", 1000.);
00072 m_nbins = config ("nbins", 100);
00073 m_arr_fname = configStr("arr_fname", "camera_spectral_array.txt");
00074 m_maxEvents = config ("events", 1<<31U);
00075 m_filter = config ("filter", false);
00076 }
00077
00078
00079
00080
00081 CameraPixSpectra::~CameraPixSpectra ()
00082 {
00083 }
00084
00085
00086 void
00087 CameraPixSpectra::beginJob(Event& evt, Env& env)
00088 {
00089 m_offset = 0;
00090 m_width = 0;
00091 m_height = 0;
00092 m_numPixels = 0;
00093
00094 shared_ptr<Psana::Opal1k::ConfigV1> config = env.configStore().get(m_src);
00095 if (config.get()) {
00096 m_offset = (int) config ->output_offset();
00097 }
00098
00099 shared_ptr<Psana::Princeton::ConfigV1> config1 = env.configStore().get(m_src);
00100 if (config1.get()) {
00101 m_width = (int) config1->width();
00102 m_height = (int) config1->height();
00103 m_numPixels = (int) config1->numPixels();
00104 }
00105
00106 shared_ptr<Psana::Princeton::ConfigV2> config2 = env.configStore().get(m_src);
00107 if (config2.get()) {
00108 m_width = (int) config2->width();
00109 m_height = (int) config2->height();
00110 m_numPixels = (int) config2->numPixels();
00111 }
00112
00113 WithMsgLog(name(), info, str) {
00114 str << "\n offset = " << m_offset;
00115 str << "\n width = " << m_width;
00116 str << "\n height = " << m_height;
00117 str << "\n numPixels = " << m_numPixels;
00118 }
00119
00120 this -> printInputPars();
00121 if( m_numPixels != 0 ) this -> arrayInit();
00122 }
00123
00124
00125 void
00126 CameraPixSpectra::beginRun(Event& evt, Env& env)
00127 {
00128 }
00129
00130
00131 void
00132 CameraPixSpectra::beginCalibCycle(Event& evt, Env& env)
00133 {
00134
00135 shared_ptr<Psana::Opal1k::ConfigV1> config = env.configStore().get(m_src);
00136 if (config.get()) {
00137 WithMsgLog(name(), info, str) {
00138 str << "Psana::Opal1k::ConfigV1:";
00139 str << "\n black_level = " << config->black_level();
00140 str << "\n gain_percent = " << config->gain_percent();
00141 str << "\n output_resolution = " << config->output_resolution();
00142 str << "\n vertical_binning = " << config->vertical_binning();
00143 str << "\n output_mirroring = " << config->output_mirroring();
00144 str << "\n vertical_remapping = " << int(config->vertical_remapping());
00145 str << "\n output_offset = " << config->output_offset();
00146 str << "\n output_resolution_bits = " << config->output_resolution_bits();
00147 str << "\n defect_pixel_correction_enabled = " << int(config->defect_pixel_correction_enabled());
00148 str << "\n output_lookup_table_enabled = " << int(config->output_lookup_table_enabled());
00149 }
00150 }
00151
00152 shared_ptr<Psana::Princeton::ConfigV1> config1 = env.configStore().get(m_src);
00153 if (config1.get()) {
00154 WithMsgLog(name(), info, str) {
00155 str << "Princeton::ConfigV1:";
00156 str << "\n width = " << config1->width();
00157 str << "\n height = " << config1->height();
00158 str << "\n orgX = " << config1->orgX();
00159 str << "\n orgY = " << config1->orgY();
00160 str << "\n binX = " << config1->binX();
00161 str << "\n binY = " << config1->binY();
00162 str << "\n exposureTime = " << config1->exposureTime();
00163 str << "\n coolingTemp = " << config1->coolingTemp();
00164 str << "\n readoutSpeedIndex = " << config1->readoutSpeedIndex();
00165 str << "\n readoutEventCode = " << config1->readoutEventCode();
00166 str << "\n delayMode = " << config1->delayMode();
00167 str << "\n frameSize = " << config1->frameSize();
00168 str << "\n numPixels = " << config1->numPixels();
00169
00170 }
00171 }
00172
00173 shared_ptr<Psana::Princeton::ConfigV2> config2 = env.configStore().get(m_src);
00174 if (config2.get()) {
00175 WithMsgLog(name(), info, str) {
00176 str << "Princeton::ConfigV2:";
00177 str << "\n width = " << config2->width();
00178 str << "\n height = " << config2->height();
00179 str << "\n orgX = " << config2->orgX();
00180 str << "\n orgY = " << config2->orgY();
00181 str << "\n binX = " << config2->binX();
00182 str << "\n binY = " << config2->binY();
00183 str << "\n exposureTime = " << config2->exposureTime();
00184 str << "\n coolingTemp = " << config2->coolingTemp();
00185 str << "\n gainIndex = " << config2->gainIndex();
00186 str << "\n readoutSpeedIndex = " << config2->readoutSpeedIndex();
00187 str << "\n readoutEventCode = " << config2->readoutEventCode();
00188 str << "\n delayMode = " << config2->delayMode();
00189 str << "\n frameSize = " << config2->frameSize();
00190 str << "\n numPixels = " << config2->numPixels();
00191 }
00192 }
00193 }
00194
00195
00196
00197 void
00198 CameraPixSpectra::event(Event& evt, Env& env)
00199 {
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 if (m_count >= m_maxEvents) { stop(); return; }
00212
00213 if ( m_count<5
00214 or (m_count<500 and m_count%100 == 0)
00215 or m_count%1000 == 0 ) WithMsgLog(name(), info, log) { log << "event=" << m_count; }
00216
00217
00218
00219 shared_ptr<Psana::Camera::FrameV1> frmData = evt.get(m_src);
00220 if (frmData.get()) {
00221
00222 if (m_numPixels == 0) {
00223 m_offset = (int) frmData->offset();
00224 m_width = (int) frmData->width();
00225 m_height = (int) frmData->height();
00226 m_numPixels = m_width * m_height;
00227 this -> arrayInit();
00228 }
00229
00230 const ndarray<const uint8_t,2>& data8 = frmData->data8();
00231 if (not data8.empty()) {
00232 const uint8_t *p_data = &data8[0][0];
00233 this -> arrayFill8 (p_data);
00234 }
00235
00236 const ndarray<const uint16_t,2>& data16 = frmData->data16();
00237 if (not data16.empty()){
00238 const uint16_t *p_data = &data16[0][0];
00239 this -> arrayFill (p_data);
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 }
00265
00266
00267 shared_ptr<Psana::Princeton::FrameV1> frame = evt.get(m_src);
00268 if (frame.get()) {
00269
00270 const ndarray<const uint16_t,2>& data = frame->data();
00271 const uint16_t *p_data = &data[0][0];
00272 this -> arrayFill (p_data);
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 }
00289
00290
00291 ++ m_count;
00292 }
00293
00294
00295
00296 void
00297 CameraPixSpectra::endCalibCycle(Event& evt, Env& env)
00298 {
00299 }
00300
00301
00302 void
00303 CameraPixSpectra::endRun(Event& evt, Env& env)
00304 {
00305 }
00306
00307
00308 void
00309 CameraPixSpectra::endJob(Event& evt, Env& env)
00310 {
00311 MsgLog(name(), info, "CameraPixSpectra::endJob");
00312 this -> saveArrayInFile();
00313 this -> saveShapeInFile();
00314 this -> arrayDelete();
00315 }
00316
00317
00318
00319
00320
00321
00322 void
00323 CameraPixSpectra::arrayInit()
00324 {
00325 MsgLog(name(), info, "CameraPixSpectra::arrayInit()");
00326
00327 m_factor = double(m_nbins) / (m_amax-m_amin);
00328 m_nbins1 = m_nbins - 1;
00329
00330 int size = m_numPixels * m_nbins;
00331 m_arr = new int [size];
00332 for(int i=0; i<size; i++) m_arr[i] = 0;
00333 }
00334
00335
00336
00337 void
00338 CameraPixSpectra::arrayDelete()
00339 {
00340 delete [] m_arr;
00341 }
00342
00343
00344
00345 void
00346 CameraPixSpectra::arrayFill(const uint16_t* data)
00347 {
00348 for (int i=0; i<m_numPixels; i++) {
00349 double amp = (double)data[i];
00350 int iamp = this -> ampToIndex(amp);
00351
00352 m_arr[i * m_nbins + iamp] ++;
00353 }
00354 }
00355
00356
00357
00358 void
00359 CameraPixSpectra::arrayFill8(const uint8_t* data)
00360 {
00361 for (int i=0; i<m_numPixels; i++) {
00362 double amp = (double)data[i];
00363 int iamp = this -> ampToIndex(amp);
00364
00365 m_arr[i * m_nbins + iamp] ++;
00366 }
00367 }
00368
00369
00370
00371 void
00372 CameraPixSpectra::saveArrayInFile()
00373 {
00374 MsgLog(name(), info, "Save the spectral array in file " << m_arr_fname);
00375 CSPadPixCoords::Image2D<int>* arr = new CSPadPixCoords::Image2D<int>(&m_arr[0], m_numPixels, m_nbins);
00376 arr -> saveImageInFile(m_arr_fname,0);
00377 }
00378
00379
00380
00381 void
00382 CameraPixSpectra::saveShapeInFile()
00383 {
00384 m_arr_shape_fname = m_arr_fname + ".sha";
00385 MsgLog(name(), info, "Save the spectral array configuration in file " << m_arr_shape_fname);
00386 std::ofstream file;
00387 file.open(m_arr_shape_fname.c_str(), std::ios_base::out);
00388 file << "NPIXELS " << m_numPixels << "\n";
00389 file << "NBINS " << m_nbins << "\n";
00390 file << "AMIN " << m_amin << "\n";
00391 file << "AMAX " << m_amax << "\n";
00392 file << "NEVENTS " << m_count << "\n";
00393 file << "ARRFNAME " << m_arr_fname << "\n";
00394 file.close();
00395 }
00396
00397
00398
00399 int
00400 CameraPixSpectra::ampToIndex(double amp)
00401 {
00402 int ind = (int) (m_factor*(amp-m_amin));
00403 if( ind < 0 ) return 0;
00404 if( ind > m_nbins1) return m_nbins1;
00405 return ind;
00406 }
00407
00408
00409
00410 void
00411 CameraPixSpectra::printInputPars()
00412 {
00413 WithMsgLog(name(), info, log) { log
00414 << "\n Input parameters:"
00415
00416 << "\n m_key " << m_key
00417 << "\n m_maxEvents " << m_maxEvents
00418 << "\n m_amin " << m_amin
00419 << "\n m_amax " << m_amax
00420 << "\n m_nbins " << m_nbins
00421 << "\n m_arr_fname " << m_arr_fname
00422
00423 << "\n";
00424 }
00425 }
00426
00427
00428
00429 }
00430