cspad_mod/src/CsPadFilter.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: CsPadFilter.cpp 2904 2012-02-08 22:19:48Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class CsPadFilter...
00007 //
00008 // Author List:
00009 //      Andy Salnikov
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "cspad_mod/CsPadFilter.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <numeric>
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "cspad_mod/CalibDataProxy.h"
00027 #include "MsgLogger/MsgLogger.h"
00028 #include "pdscalibdata/CsPadFilterV1.h"
00029 #include "PSCalib/CalibFileFinder.h"
00030 #include "psddl_psana/cspad.ddl.h"
00031 #include "psddl_psana/cspad2x2.ddl.h"
00032 #include "PSEvt/EventId.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 cspad_mod;
00040 PSANA_MODULE_FACTORY(CsPadFilter)
00041 
00042 //              ----------------------------------------
00043 //              -- Public Function Member Definitions --
00044 //              ----------------------------------------
00045 
00046 namespace cspad_mod {
00047 
00048 //----------------
00049 // Constructors --
00050 //----------------
00051 CsPadFilter::CsPadFilter (const std::string& name)
00052   : Module(name)
00053   , m_src()
00054   , m_key()
00055   , m_skipIfNoData()
00056   , m_mode()
00057   , m_param()
00058 {
00059   m_src = configStr("source", "DetInfo(:Cspad)");
00060   m_key = configStr("inputKey", "");
00061   m_skipIfNoData = config("skipIfNoData", true);
00062   m_mode = config("mode", -1);
00063   m_param = configList("parameters", std::list<double>());
00064 }
00065 
00066 //--------------
00067 // Destructor --
00068 //--------------
00069 CsPadFilter::~CsPadFilter ()
00070 {
00071 }
00072 
00073 /// Method which is called once at the beginning of the job
00074 void 
00075 CsPadFilter::beginJob(Event& evt, Env& env)
00076 {
00077 }
00078 
00079 /// Method which is called at the beginning of the run
00080 void 
00081 CsPadFilter::beginRun(Event& evt, Env& env)
00082 {
00083   if (m_mode<= 0) {
00084     
00085     // use calibration file
00086     
00087     // get run number
00088     shared_ptr<EventId> eventId = evt.get();
00089     int run = 0;
00090     if (eventId.get()) {
00091       run = eventId->run();
00092       MsgLog(name(), trace, name() << ": Using run number " << run);
00093     } else {
00094       MsgLog(name(), warning, name() << ": Cannot determine run number, will use 0.");
00095     }
00096   
00097     // add proxy to calib store
00098     boost::shared_ptr< PSEvt::Proxy<pdscalibdata::CsPadFilterV1> > proxy(
00099         new CalibDataProxy<pdscalibdata::CsPadFilterV1>(env.calibDir(), "filter", run));
00100     env.calibStore().putProxy(proxy, PSEvt::EventKey::anySource());
00101 
00102   } else {
00103     
00104     // copy parameters to array
00105     const int paramSize = pdscalibdata::CsPadFilterV1::DataSize;
00106     double param[paramSize];    
00107     std::fill_n(param, paramSize, 0.0);
00108     
00109     std::list<double>::const_iterator it = m_param.begin();
00110     for (int i = 0; i != paramSize and it != m_param.end(); ++ i, ++ it) {
00111       param[i] = *it;
00112     }
00113     if (it != m_param.end()) {
00114       MsgLog(name(), warning, "Too many values specified in parameters, ignoring extra values.");
00115     }
00116 
00117     // make filter object
00118     pdscalibdata::CsPadFilterV1::FilterMode mode = pdscalibdata::CsPadFilterV1::FilterMode(m_mode);
00119     boost::shared_ptr<pdscalibdata::CsPadFilterV1> filter(new pdscalibdata::CsPadFilterV1(mode, param));
00120     
00121     // store it
00122     env.calibStore().put(filter, PSEvt::EventKey::anySource());
00123   }
00124 }
00125 
00126 /// Method which is called at the beginning of the calibration cycle
00127 void 
00128 CsPadFilter::beginCalibCycle(Event& evt, Env& env)
00129 {
00130 }
00131 
00132 /// Method which is called with event data, this is the only required 
00133 /// method, all other methods are optional
00134 void 
00135 CsPadFilter::event(Event& evt, Env& env)
00136 {
00137 
00138   Pds::Src actualSrc;
00139 
00140   shared_ptr<Psana::CsPad::DataV1> data1 = evt.get(m_src, m_key, &actualSrc);
00141   if (data1.get()) {
00142 
00143     // get calibration object
00144     boost::shared_ptr<pdscalibdata::CsPadFilterV1> filter = env.calibStore().get(actualSrc);
00145     if (filter.get()) {
00146 
00147       // loop over all quads
00148       int nQuads = data1->quads_shape()[0];
00149       for (int q = 0; q < nQuads; ++ q) {
00150 
00151         const Psana::CsPad::ElementV1& el = data1->quads(q);
00152 
00153         // call filter
00154         bool stat = filter->filter(el.data());
00155         if (stat) {
00156           // at least some data is good, do not skip and stop here
00157           MsgLog(name(), debug, name() << ": Good data found in CsPad::DataV1 quadrant=" << el.quad());
00158           return;
00159         }
00160 
00161       }
00162 
00163       // no good data found, skip it
00164       MsgLog(name(), debug, name() << ": No good data found in CsPad::DataV1");
00165       skip();
00166       return;
00167 
00168     }
00169   }
00170 
00171 
00172   shared_ptr<Psana::CsPad::DataV2> data2 = evt.get(m_src, m_key, &actualSrc);
00173   if (data2.get()) {
00174 
00175     // get calibration object
00176     boost::shared_ptr<pdscalibdata::CsPadFilterV1> filter = env.calibStore().get(actualSrc);
00177     if (filter.get()) {
00178 
00179       // loop over all quads
00180       int nQuads = data2->quads_shape()[0];
00181       for (int q = 0; q < nQuads; ++ q) {
00182 
00183         const Psana::CsPad::ElementV2& el = data2->quads(q);
00184 
00185         // call filter
00186         bool stat = filter->filter(el.data());
00187         if (stat) {
00188           // at least some data is good, do not skip and stop here
00189           MsgLog(name(), debug, name() << ": Good data found in CsPad::DataV2 quadrant=" << el.quad());
00190           return;
00191         }
00192 
00193       }
00194 
00195       // no good data found, skip it
00196       MsgLog(name(), debug, name() << ": No good data were found in CsPad::DataV2");
00197       skip();
00198       return;
00199 
00200     }
00201   }
00202 
00203 
00204   shared_ptr<Psana::CsPad2x2::ElementV1> mini1 = evt.get(m_src, m_key, &actualSrc);
00205   if (mini1.get()) {
00206 
00207     // get calibration object
00208     boost::shared_ptr<pdscalibdata::CsPadFilterV1> filter = env.calibStore().get(actualSrc);
00209     if (filter.get()) {
00210 
00211       const Psana::CsPad2x2::ElementV1& el = *mini1;
00212 
00213       // call filter
00214       bool stat = filter->filter(el.data());
00215       if (stat) {
00216         // at least some data is good, do not skip and stop here
00217         MsgLog(name(), debug, name() << ": Good data found in CsPad2x2::ElementV1 quadrant=" << el.quad());
00218         return;
00219       }
00220 
00221       // no good data found, skip it
00222       MsgLog(name(), debug, name() << ": No good data were found in CsPad2x2::ElementV1");
00223       skip();
00224       return;
00225 
00226     }
00227   }
00228 
00229   // no data found
00230   MsgLog(name(), debug, name() << ": No cspad data was found in event");
00231   if (m_skipIfNoData) {
00232     skip();
00233   }
00234 
00235 }
00236   
00237 /// Method which is called at the end of the calibration cycle
00238 void 
00239 CsPadFilter::endCalibCycle(Event& evt, Env& env)
00240 {
00241 }
00242 
00243 /// Method which is called at the end of the run
00244 void 
00245 CsPadFilter::endRun(Event& evt, Env& env)
00246 {
00247 }
00248 
00249 /// Method which is called once at the end of the job
00250 void 
00251 CsPadFilter::endJob(Event& evt, Env& env)
00252 {
00253 }
00254 
00255 } // namespace cspad_mod

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7