ImgPixSpectra/src/CSPadPixSpectra.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id$
00004 //
00005 // Description:
00006 //      Class CSPadPixSpectra...
00007 //
00008 // Author List:
00009 //      Mikhail S. Dubrovin
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "ImgPixSpectra/CSPadPixSpectra.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 // #include "psddl_psana/cspad.ddl.h" // moved to header
00031 #include "PSEvt/EventId.h"
00032 #include "CSPadPixCoords/Image2D.h"
00033 
00034 //-----------------------------------------------------------------------
00035 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00036 //-----------------------------------------------------------------------
00037 
00038 // This declares this class as psana module
00039 using namespace ImgPixSpectra;
00040 PSANA_MODULE_FACTORY(CSPadPixSpectra)
00041 
00042 //              ----------------------------------------
00043 //              -- Public Function Member Definitions --
00044 //              ----------------------------------------
00045 
00046 namespace ImgPixSpectra {
00047 
00048 //----------------
00049 // Constructors --
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   // get the values from configuration or use defaults
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 // Destructor --
00076 //--------------
00077 CSPadPixSpectra::~CSPadPixSpectra ()
00078 {
00079 }
00080 
00081 /// Method which is called once at the beginning of the job
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 /// Method which is called at the beginning of the run
00092 void 
00093 CSPadPixSpectra::beginRun(Event& evt, Env& env)
00094 {
00095 }
00096 
00097 /// Method which is called at the beginning of the calibration cycle
00098 void 
00099 CSPadPixSpectra::beginCalibCycle(Event& evt, Env& env)
00100 {
00101 }
00102 
00103 /// Method which is called with event data, this is the only required 
00104 /// method, all other methods are optional
00105 void 
00106 CSPadPixSpectra::event(Event& evt, Env& env)
00107 {
00108   // example of getting non-detector data from event
00109   //shared_ptr<PSEvt::EventId> eventId = evt.get();
00110   //if (eventId.get()) {
00111   //  // example of producing messages using MgsLog facility
00112   //  MsgLog(name(), info, "event " << m_count << " ID: " << *eventId);
00113   //}
00114   
00115   // this is how to skip event (all downstream modules will not be called)
00116   //if (m_filter && m_count % 10 == 0) skip();
00117   
00118   // this is how to gracefully stop analysis job
00119   if (m_count >= m_maxEvents) {stop(); return;}
00120 
00121   // getting detector data from event
00122   //shared_ptr<Psana::CsPad::DataV2> data = evt.get(m_src, "", &m_actualSrc); // get m_actualSrc here
00123   shared_ptr<CSPadDataType> data = evt.get(m_src, m_key, &m_actualSrc); // get m_actualSrc here
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   // increment event counter
00134   ++ m_count;
00135 }
00136   
00137 /// Method which is called at the end of the calibration cycle
00138 void 
00139 CSPadPixSpectra::endCalibCycle(Event& evt, Env& env)
00140 {
00141 }
00142 
00143 /// Method which is called at the end of the run
00144 void 
00145 CSPadPixSpectra::endRun(Event& evt, Env& env)
00146 {
00147 }
00148 
00149 /// Method which is called once at the end of the job
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);  // scale factor for histogramm index
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   // m_arr2d = make_ndarray(m_arr, m_sizeOfCSPadArr, m_nbins);
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           //cout << "     q = " << q << " quad =" << quad << endl;
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              //cout  << "  add section " << sect << endl;            
00217  
00218              //for (uint32_t c=0; c<m_ncols2x1; c++) {
00219              //for (uint32_t r=0; r<m_nrows2x1; r++) {
00220              //  double amp = (double)data2x1[c*m_nrows2x1+r];
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] ++; // incriment in spectral array
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       //<< "\n    m_src         " << m_src       
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       //<< "\n    m_filter      " << m_filter
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 } // namespace ImgPixSpectra

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7