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 9 of file ExpressionEvaluator.h.

Constructor & Destructor Documentation

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

Definition at line 54 of file ExpressionEvaluator.cc.

References COUT, Exception, reco::exprEvalDetails::execSysCommand(), FrontierConditions_GlobalTag_cff::file, edm::getEnvironmentVariable(), m_expr, m_name, checklumidiff::ofile, source, AlCaHLTBitMon_QueryRunRegistry::string, and tmp.

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

Definition at line 155 of file ExpressionEvaluator.cc.

References m_name.

155  {
156  remove(m_name);
157 }

Member Function Documentation

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

Definition at line 15 of file ExpressionEvaluator.h.

References m_expr.

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

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

Member Data Documentation

void* reco::ExpressionEvaluator::m_expr
private

Definition at line 23 of file ExpressionEvaluator.h.

Referenced by expr(), and ExpressionEvaluator().

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

Definition at line 22 of file ExpressionEvaluator.h.

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