PSEnv/src/Env.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: Env.cpp 9877 2015-04-08 21:15:19Z davidsch@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class Env...
00007 //
00008 // Author List:
00009 //      Andrei Salnikov
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "PSEnv/Env.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <dlfcn.h>
00022 #include <boost/make_shared.hpp>
00023 
00024 //-------------------------------
00025 // Collaborating Class Headers --
00026 //-------------------------------
00027 #include "PSEvt/ProxyDict.h"
00028 #include "PSEvt/ProxyDictHist.h"
00029 #include "MsgLogger/MsgLogger.h"
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 
00034 namespace {
00035 
00036   void gsub(std::string& str, const std::string& substr, const std::string& replacement)
00037   {
00038     for (std::string::size_type p = str.find(substr); p != std::string::npos; p = str.find(substr)) {
00039       str.replace(p, substr.size(), replacement);
00040     }
00041   }
00042 
00043 
00044 }
00045 
00046 
00047 //              ----------------------------------------
00048 //              -- Public Function Member Definitions --
00049 //              ----------------------------------------
00050 
00051 namespace PSEnv {
00052 
00053 //----------------
00054 // Constructors --
00055 //----------------
00056 Env::Env (const std::string& jobName,
00057     const boost::shared_ptr<IExpNameProvider>& expNameProvider,
00058     const std::string& calibDir,
00059     const boost::shared_ptr<PSEvt::AliasMap>& aliasMap,
00060     int subproc)
00061   : m_fwkName("psana")
00062   , m_jobName(jobName)
00063   , m_jobNameSub(jobName)
00064   , m_aliasMap(aliasMap)
00065   , m_cfgStore()
00066   , m_calibStore()
00067   , m_epicsStore(boost::make_shared<EpicsStore>())
00068   , m_hmgr()
00069   , m_firstHmgrCall(true)
00070   , m_expNameProvider(expNameProvider)
00071   , m_calibDir(calibDir)
00072   , m_calibDirSetup(false)
00073   , m_subproc(subproc)
00074 {
00075   // instantiate dictionary for config store and store itself
00076   boost::shared_ptr<PSEvt::ProxyDictHist> cfgDict(new PSEvt::ProxyDictHist(m_aliasMap));
00077   m_cfgStore = boost::make_shared<EnvObjectStore>(cfgDict);
00078   
00079   // instantiate dictionary for calib store and store itself
00080   boost::shared_ptr<PSEvt::ProxyDictHist> calibDict(new PSEvt::ProxyDictHist(m_aliasMap));
00081   m_calibStore = boost::make_shared<EnvObjectStore>(calibDict);
00082   
00083   if (subproc >= 0) {
00084     m_jobNameSub += boost::lexical_cast<std::string>(subproc);
00085   }
00086 }
00087 
00088 //--------------
00089 // Destructor --
00090 //--------------
00091 Env::~Env ()
00092 {
00093   // save histograms
00094   if (m_hmgr.get()) m_hmgr->write();
00095 }
00096 
00097 // Returns that name of the calibration directory for current
00098 // instrument/experiment.
00099 const std::string&
00100 Env::calibDir() const
00101 {
00102   if (not m_calibDirSetup) {
00103     ::gsub(m_calibDir, "{exp}", experiment());
00104     ::gsub(m_calibDir, "{instr}", instrument());
00105     m_calibDirSetup = true;
00106   }
00107 
00108   return m_calibDir;
00109 }
00110 
00111 
00112 // Access to histogram manager.
00113 boost::shared_ptr<PSHist::HManager> 
00114 Env::hmgr()
00115 {
00116   typedef PSHist::HManager *(*FnPSHistFromConstCharPtr)(const char *);
00117   if (not m_hmgr.get() and m_firstHmgrCall) {
00118     m_firstHmgrCall = false;
00119     const char * histPackage = "libRootHist.so";  // this could become a psana parameter
00120     const char * histFactoryFunction = "CREATE_PSHIST_HMANAGER_FROM_CONST_CHAR_PTR"; // this fixed,
00121                                 // but we could look for factory functions taking other parameters as well
00122     void* histLibHandle = dlopen(histPackage, RTLD_NOW | RTLD_GLOBAL);
00123     if (histLibHandle) {
00124       FnPSHistFromConstCharPtr histFactorySymbol = (FnPSHistFromConstCharPtr)dlsym(histLibHandle,
00125                                                                                    histFactoryFunction);
00126       if (histFactorySymbol) {
00127         // Instantiate manager
00128         std::string rfname = m_jobName + ".root";
00129         boost::shared_ptr<PSHist::HManager> hmgr(histFactorySymbol(rfname.c_str()));
00130         if (hmgr) {
00131           m_hmgr = hmgr;
00132         } else {
00133           MsgLogRoot(error, "Env::hmgr() factory function to create PSHist::HManager in package "
00134                      << histPackage << " found and called, but it did not return a valid object.");
00135         }
00136       } else {
00137         MsgLogRoot(error, "Env::hmgr() factory function=" << histFactoryFunction
00138                    << " to create PSHist::Hmanager in package "
00139                    << histPackage 
00140                    << " not found.");
00141       } 
00142     } else {
00143       MsgLogRoot(warning, "Env::hmgr() package to create PSHist::Hmanager = " 
00144                  << histPackage 
00145                  << " not found.");
00146     }
00147   }
00148   return m_hmgr;
00149 }
00150 
00151 
00152 } // namespace PSEnv

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7