CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
reco::ExpressionEvaluator Class Reference

#include <ExpressionEvaluator.h>

Public Member Functions

template<typename EXPR , typename... CArgs>
EXPR * expr () const
 
 ExpressionEvaluator (const char *pkg, const char *iname, const std::string &iexpr)
 
 ~ExpressionEvaluator ()
 

Private Attributes

void * m_expr
 
std::string m_name
 

Detailed Description

Definition at line 8 of file ExpressionEvaluator.h.

Constructor & Destructor Documentation

◆ ExpressionEvaluator()

reco::ExpressionEvaluator::ExpressionEvaluator ( const char *  pkg,
const char *  iname,
const std::string &  iexpr 
)

Definition at line 44 of file ExpressionEvaluator.cc.

45  : m_name("VI_" + generateName()) {
46  std::string pch = pkg;
47  pch += "/src/precompile.h";
48  std::string quote("\"");
49 
50  auto arch = edm::getEnvironmentVariable("SCRAM_ARCH");
51  auto baseDir = edm::getEnvironmentVariable("CMSSW_BASE");
52  auto relDir = edm::getEnvironmentVariable("CMSSW_RELEASE_BASE");
53 
54  std::string sfile = baseDir + "/tmp/" + m_name + ".cc";
55  std::string ofile = baseDir + "/tmp/" + m_name + ".so";
56 
57  std::string incDir = "/include/" + arch + "/";
58  std::string cxxf;
59  {
60  // look in local dir
61  std::string file = baseDir + incDir + pch + ".cxxflags";
62  std::ifstream ss(file.c_str());
63  COUT << "local file: " << file << std::endl;
64  if (ss) {
65  std::getline(ss, cxxf);
66  incDir = baseDir + incDir;
67  } else {
68  // look in release area
69  std::string file = relDir + incDir + pch + ".cxxflags";
70  COUT << "file in release area: " << file << std::endl;
71  std::ifstream ss(file.c_str());
72  if (ss) {
73  std::getline(ss, cxxf);
74  incDir = relDir + incDir;
75  } else {
76  // look in release is a patch area
77  auto paDir = edm::getEnvironmentVariable("CMSSW_FULL_RELEASE_BASE");
78  if (paDir.empty())
79  throw cms::Exception("ExpressionEvaluator", "error in opening patch area for " + baseDir);
80  std::string file = paDir + incDir + pch + ".cxxflags";
81  COUT << "file in base release area: " << file << std::endl;
82  std::ifstream ss(file.c_str());
83  if (!ss)
84  throw cms::Exception(
85  "ExpressionEvaluator",
86  pch + " file not found neither in " + baseDir + " nor in " + relDir + " nor in " + paDir);
87  std::getline(ss, cxxf);
88  incDir = paDir + incDir;
89  }
90  }
91 
92  {
93  std::regex rq("-I[^ ]+");
94  cxxf = std::regex_replace(cxxf, rq, std::string(""));
95  }
96  {
97  std::regex rq("=\"");
98  cxxf = std::regex_replace(cxxf, rq, std::string("='\""));
99  }
100  {
101  std::regex rq("\" ");
102  cxxf = std::regex_replace(cxxf, rq, std::string("\"' "));
103  }
104  COUT << '|' << cxxf << "|\n" << std::endl;
105  }
106 
107  std::string cpp = "c++ -H -Wall -shared -Winvalid-pch ";
108  cpp += cxxf;
109  cpp += " -I" + incDir;
110  cpp += " -o " + ofile + ' ' + sfile + " 2>&1\n";
111 
112  COUT << cpp << std::endl;
113 
114  // prepare the file to compile
115  std::string factory = "factory" + m_name;
116 
117  std::string source = std::string("#include ") + quote + pch + quote + "\n";
118  source += "struct " + m_name + " final : public " + iname + "{\n";
119  source += iexpr;
120  source += "\n};\n";
121 
122  source += "extern " + quote + 'C' + quote + ' ' + std::string(iname) + "* " + factory + "() {\n";
123  source += "static " + m_name + " local;\n";
124  source += "return &local;\n}\n";
125 
126  COUT << source << std::endl;
127 
128  {
129  std::ofstream tmp(sfile.c_str());
130  tmp << source << std::endl;
131  }
132 
133  // compile
134  auto ss = execSysCommand(cpp);
135  COUT << ss << std::endl;
136 
137  void* dl = dlopen(ofile.c_str(), RTLD_LAZY);
138  if (!dl) {
139  remove(m_name, baseDir + "/tmp");
140  throw cms::Exception("ExpressionEvaluator",
141  std::string("compilation/linking failed\n") + cpp + ss + "dlerror " + dlerror());
142  return;
143  }
144 
145  m_expr = dlsym(dl, factory.c_str());
146  remove(m_name, baseDir + "/tmp");
147  }

References ALCARECODTCalibSynchDQM_cff::baseDir, COUT, Exception, reco::exprEvalDetails::execSysCommand(), geometryDiff::file, edm::getEnvironmentVariable(), m_expr, m_name, MatrixUtil::remove(), source, contentValuesCheck::ss, AlCaHLTBitMon_QueryRunRegistry::string, and createJobs::tmp.

◆ ~ExpressionEvaluator()

reco::ExpressionEvaluator::~ExpressionEvaluator ( )

Definition at line 149 of file ExpressionEvaluator.cc.

149 { remove(m_name, edm::getEnvironmentVariable("CMSSW_BASE") + "/tmp"); }

References edm::getEnvironmentVariable(), m_name, and MatrixUtil::remove().

Member Function Documentation

◆ expr()

template<typename EXPR , typename... CArgs>
EXPR* reco::ExpressionEvaluator::expr ( ) const
inline

Definition at line 14 of file ExpressionEvaluator.h.

14  {
15  typedef EXPR* factoryP();
16  return reinterpret_cast<factoryP*>(m_expr)();
17  }

References m_expr.

Referenced by reco::expressionEvaluator(), ExpressionEvaluatorCut::ExpressionEvaluatorCut(), and ExpressionEvaluatorCutWithEventContent::ExpressionEvaluatorCutWithEventContent().

Member Data Documentation

◆ m_expr

void* reco::ExpressionEvaluator::m_expr
private

Definition at line 21 of file ExpressionEvaluator.h.

Referenced by expr(), and ExpressionEvaluator().

◆ m_name

std::string reco::ExpressionEvaluator::m_name
private

Definition at line 20 of file ExpressionEvaluator.h.

Referenced by ExpressionEvaluator(), and ~ExpressionEvaluator().

reco::ExpressionEvaluator::m_expr
void * m_expr
Definition: ExpressionEvaluator.h:21
ALCARECODTCalibSynchDQM_cff.baseDir
baseDir
Definition: ALCARECODTCalibSynchDQM_cff.py:16
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
reco::ExpressionEvaluator::m_name
std::string m_name
Definition: ExpressionEvaluator.h:20
edm::getEnvironmentVariable
std::string getEnvironmentVariable(std::string const &name, std::string const &defaultValue=std::string())
Definition: GetEnvironmentVariable.h:8
reco::exprEvalDetails::execSysCommand
std::string execSysCommand(const std::string &cmdline)
Definition: popenCPP.h:12
source
static const std::string source
Definition: EdmProvDump.cc:47
COUT
#define COUT
Definition: ExpressionEvaluator.cc:18
geometryDiff.file
file
Definition: geometryDiff.py:13
pkg
Definition: pkg.py:1
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Exception
Definition: hltDiff.cc:245
MatrixUtil.remove
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:219
cms::Exception
Definition: Exception.h:70