ImgPixSpectra/src/CSPad2x2PixSpectra.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class CSPad2x2PixSpectra...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "ImgPixSpectra/CSPad2x2PixSpectra.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <fstream>
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "MsgLogger/MsgLogger.h"
00027 // to work with detector data include corresponding 
00028 // header from psddl_psana package
00029 // #include "psddl_psana/acqiris.ddl.h"
00030 
00031 //#include "psddl_psana/cspad.ddl.h"
00032 #include "psddl_psana/cspad2x2.ddl.h"
00033 
00034 #include "PSEvt/EventId.h"
00035 #include "CSPadPixCoords/Image2D.h"
00036 
00037 
00038 //-----------------------------------------------------------------------
00039 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00040 //-----------------------------------------------------------------------
00041 
00042 // This declares this class as psana module
00043 using namespace ImgPixSpectra;
00044 PSANA_MODULE_FACTORY(CSPad2x2PixSpectra)
00045 
00046 //              ----------------------------------------
00047 //              -- Public Function Member Definitions --
00048 //              ----------------------------------------
00049 
00050 namespace ImgPixSpectra {
00051 
00052 //----------------
00053 // Constructors --
00054 //----------------
00055 CSPad2x2PixSpectra::CSPad2x2PixSpectra (const std::string& name)
00056   : Module(name)
00057   , m_src()
00058   , m_key()
00059   , m_amin()
00060   , m_amax()
00061   , m_nbins()
00062   , m_arr_fname()
00063   , m_maxEvents()
00064   , m_filter()
00065   , m_count(0)
00066 {
00067   // get the values from configuration or use defaults
00068   m_src           = configStr("source", "DetInfo(:Cspad2x2)");
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", "cspad2x2_spectral_array.txt");
00074   m_maxEvents     = config   ("events", 1<<31U);
00075   m_filter        = config   ("filter", false);
00076 }
00077 
00078 //--------------
00079 // Destructor --
00080 //--------------
00081 CSPad2x2PixSpectra::~CSPad2x2PixSpectra ()
00082 {
00083 }
00084 
00085 /// Method which is called once at the beginning of the job
00086 void 
00087 CSPad2x2PixSpectra::beginJob(Event& evt, Env& env)
00088 {
00089   this -> printInputPars();
00090   this -> arrayInit();
00091 }
00092 
00093 /// Method which is called at the beginning of the run
00094 void 
00095 CSPad2x2PixSpectra::beginRun(Event& evt, Env& env)
00096 {
00097 }
00098 
00099 /// Method which is called at the beginning of the calibration cycle
00100 void 
00101 CSPad2x2PixSpectra::beginCalibCycle(Event& evt, Env& env)
00102 {
00103 }
00104 
00105 /// Method which is called with event data, this is the only required 
00106 /// method, all other methods are optional
00107 void 
00108 CSPad2x2PixSpectra::event(Event& evt, Env& env)
00109 {
00110   //// example of getting non-detector data from event
00111   //shared_ptr<PSEvt::EventId> eventId = evt.get();
00112   //if (eventId.get()) {
00113   //  // example of producing messages using MgsLog facility
00114   //  MsgLog(name(), info, "event ID: " << *eventId);
00115   //}
00116   
00117   //// tis is how to skip event (all downstream modules will not be called)
00118   //if (m_filter && m_count % 10 == 0) { skip(); return; }
00119   
00120   // this is how to gracefully stop analysis job
00121   if (m_count >= m_maxEvents) { stop(); return; }
00122 
00123   if (   m_count<5 
00124      or (m_count<500 and m_count%100  == 0) 
00125      or                  m_count%1000 == 0  ) WithMsgLog(name(), info, log) { log << "event=" << m_count; }
00126   
00127   shared_ptr<Psana::CsPad2x2::ElementV1> el_cspad2x2 = evt.get(m_src, m_key, &m_actualSrc);
00128   if (el_cspad2x2.get()) {
00129 
00130       const Psana::CsPad2x2::ElementV1& el = *el_cspad2x2;
00131       
00132       //const int16_t* data = el.data(); // depricated
00133       const ndarray<const int16_t,3>& data_nda = el.data();
00134       const int16_t* data = &data_nda[0][0][0];
00135 
00136       const unsigned* dshape = data_nda.shape();
00137       int npix_2x2 = dshape[0] * dshape[1] * dshape[2];  
00138 
00139       if (npix_2x2 != m_npix_2x2) {
00140           MsgLog(name(), error, "Unexpected data shape: " << npix_2x2 << ", expected ElementV1 data size is " << m_npix_2x2);
00141           stop();
00142       }
00143 
00144       //WithMsgLog(name(), info, log) { 
00145       //  log << "CsPad2x2::ElementV1  dshape.size() = " << dshape.size() << "  shape = "; 
00146       //for(uint i=0; i<dshape.size(); i++) log << dshape[i] << ", "; 
00147       //log << "    npix_2x2 = " << npix_2x2 << "\n";
00148       //
00149 
00150       this -> arrayFill (data);
00151   }
00152 
00153   // increment event counter
00154   ++ m_count;
00155 }
00156   
00157 /// Method which is called at the end of the calibration cycle
00158 void 
00159 CSPad2x2PixSpectra::endCalibCycle(Event& evt, Env& env)
00160 {
00161 }
00162 
00163 /// Method which is called at the end of the run
00164 void 
00165 CSPad2x2PixSpectra::endRun(Event& evt, Env& env)
00166 {
00167 }
00168 
00169 /// Method which is called once at the end of the job
00170 void 
00171 CSPad2x2PixSpectra::endJob(Event& evt, Env& env)
00172 {
00173   MsgLog(name(), info, "CSPad2x2PixSpectra::endJob");
00174   this -> saveArrayInFile();
00175   this -> saveShapeInFile();
00176   this -> arrayDelete();
00177 }
00178 
00179 //--------------------
00180 //--------------------
00181 //--------------------
00182 //--------------------
00183 
00184 void 
00185 CSPad2x2PixSpectra::arrayInit()
00186 {
00187   m_factor = double(m_nbins) / (m_amax-m_amin);  // scale factor for histogramm index
00188   m_nbins1 = m_nbins - 1;
00189 
00190   int size = m_npix_2x2 * m_nbins;
00191   m_arr    = new int [size];
00192   for(int i=0; i<size; i++) m_arr[i] = 0;
00193 }
00194 
00195 //--------------------
00196 
00197 void 
00198 CSPad2x2PixSpectra::arrayDelete()
00199 {
00200   delete [] m_arr;
00201 }
00202 
00203 //--------------------
00204 
00205 void
00206 CSPad2x2PixSpectra::arrayFill(const int16_t* data)
00207 {
00208              for (uint32_t i=0; i<m_npix_2x2; i++) {
00209                double amp = (double)data[i];
00210                int iamp = this -> ampToIndex(amp);
00211                //cout << "pix=" << i << " amp=" << amp << endl;  
00212                m_arr[i * m_nbins + iamp] ++; // incriment in spectral array
00213              }
00214 }
00215 
00216 //--------------------
00217 
00218 void 
00219 CSPad2x2PixSpectra::saveArrayInFile()
00220 { 
00221     MsgLog(name(), info, "Save the spectral array in file " << m_arr_fname);
00222     CSPadPixCoords::Image2D<int>* arr = new CSPadPixCoords::Image2D<int>(&m_arr[0], m_npix_2x2, m_nbins); 
00223     arr -> saveImageInFile(m_arr_fname,0);
00224 }
00225 
00226 //--------------------
00227 
00228 void 
00229 CSPad2x2PixSpectra::saveShapeInFile()
00230 { 
00231     m_arr_shape_fname = m_arr_fname + ".sha";
00232     MsgLog(name(), info, "Save the spectral array configuration in file " << m_arr_shape_fname);
00233     std::ofstream file; 
00234     file.open(m_arr_shape_fname.c_str(), std::ios_base::out);
00235     file << "NPIXELS  " << m_npix_2x2        << "\n";
00236     file << "NBINS    " << m_nbins           << "\n";
00237     file << "AMIN     " << m_amin            << "\n";
00238     file << "AMAX     " << m_amax            << "\n";
00239     file << "NEVENTS  " << m_count           << "\n";
00240     file << "ARRFNAME " << m_arr_fname       << "\n";
00241     file.close();
00242 }
00243 
00244 //--------------------
00245 
00246 int  
00247 CSPad2x2PixSpectra::ampToIndex(double amp)
00248 {
00249     int ind = (int) (m_factor*(amp-m_amin));
00250     if( ind < 0       ) return 0;
00251     if( ind > m_nbins1) return m_nbins1;
00252     return ind;
00253 }
00254 
00255 //--------------------
00256 
00257 void 
00258 CSPad2x2PixSpectra::printInputPars()
00259 {
00260   WithMsgLog(name(), info, log) { log 
00261         << "\n    Input parameters:"
00262       //<< "\n    m_src         " << m_src       
00263         << "\n    m_key         " << m_key    
00264         << "\n    m_maxEvents   " << m_maxEvents 
00265         << "\n    m_amin        " << m_amin      
00266         << "\n    m_amax        " << m_amax      
00267         << "\n    m_nbins       " << m_nbins     
00268         << "\n    m_arr_fname   " << m_arr_fname    
00269       //<< "\n    m_filter      " << m_filter
00270         << "\n";
00271       }
00272 }
00273 
00274 //--------------------
00275 
00276 } // namespace ImgPixSpectra
00277 

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7