Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CodegenContext.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Garima Singh, CERN 2023
5 * Jonas Rembser, CERN 2023
6 *
7 * Copyright (c) 2023, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
15#include <RooAbsArg.h>
16
17#include "RooFitImplHelpers.h"
18
19#include <TInterpreter.h>
20
21#include <algorithm>
22#include <cctype>
23#include <charconv>
24#include <fstream>
25#include <type_traits>
26#include <unordered_map>
27
28namespace {
29
30bool startsWith(std::string_view str, std::string_view prefix)
31{
32 return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
33}
34
35} // namespace
36
37namespace RooFit {
38namespace Experimental {
39
40/// @brief Adds (or overwrites) the string representing the result of a node.
41/// @param key The name of the node to add the result for.
42/// @param value The new name to assign/overwrite.
43void CodegenContext::addResult(const char *key, std::string const &value)
44{
45 const TNamed *namePtr = RooNameReg::known(key);
46 if (namePtr)
47 addResult(namePtr, value);
48}
49
50void CodegenContext::addResult(TNamed const *key, std::string const &value)
51{
52 _nodeNames[key] = value;
53}
54
55/// @brief Gets the result for the given node using the node name. This node also performs the necessary
56/// code generation through recursive calls to 'translate'. A call to this function modifies the already
57/// existing code body.
58/// @param key The node to get the result string for.
59/// @return String representing the result of this node.
60std::string const &CodegenContext::getResult(RooAbsArg const &arg)
61{
62 // If the result has already been recorded, just return the result.
63 // It is usually the responsibility of each translate function to assign
64 // the proper result to its class. Hence, if a result has already been recorded
65 // for a particular node, it means the node has already been 'translate'd and we
66 // dont need to visit it again.
67 auto found = _nodeNames.find(arg.namePtr());
68 if (found != _nodeNames.end())
69 return found->second;
70
71 // The result for vector observables should already be in the map if you
72 // opened the loop scope. This is just to check if we did not request the
73 // result of a vector-valued observable outside of the scope of a loop.
74 auto foundVecObs = _vecObsIndices.find(arg.namePtr());
75 if (foundVecObs != _vecObsIndices.end()) {
76 throw std::runtime_error("You requested the result of a vector observable outside a loop scope for it!");
77 }
78
79 auto RAII(OutputScopeRangeComment(&arg));
80
81 // Now, recursively call translate into the current argument to load the correct result.
82 codegen(const_cast<RooAbsArg &>(arg), *this);
83
84 return _nodeNames.at(arg.namePtr());
85}
86
87/// @brief Adds the given string to the string block that will be emitted at the top of the squashed function. Useful
88/// for variable declarations.
89/// @param str The string to add to the global scope.
90void CodegenContext::addToGlobalScope(std::string const &str)
91{
92 // Introduce proper indentation for multiline strings.
93 _code[0] += str;
94}
95
96/// @brief Since the squashed code represents all observables as a single flattened array, it is important
97/// to keep track of the start index for a vector valued observable which can later be expanded to access the correct
98/// element. For example, a vector valued variable x with 10 entries will be squashed to obs[start_idx + i].
99/// @param key The name of the node representing the vector valued observable.
100/// @param idx The start index (or relative position of the observable in the set of all observables).
101void CodegenContext::addVecObs(const char *key, int idx)
102{
103 const TNamed *namePtr = RooNameReg::known(key);
104 if (namePtr)
105 _vecObsIndices[namePtr] = idx;
106}
107
109{
110 auto it = _vecObsIndices.find(arg.namePtr());
111 if (it != _vecObsIndices.end()) {
112 return it->second;
113 }
114
115 return -1; // Not found
116}
117/// @brief Adds the input string to the squashed code body. If a class implements a translate function that wants to
118/// emit something to the squashed code body, it must call this function with the code it wants to emit. In case of
119/// loops, automatically determines if code needs to be stored inside or outside loop scope.
120/// @param klass The class requesting this addition, usually 'this'.
121/// @param in String to add to the squashed code.
122void CodegenContext::addToCodeBody(RooAbsArg const *klass, std::string const &in)
123{
124 // If we are in a loop and the value is scope independent, save it at the top of the loop.
125 // else, just save it in the current scope.
127}
128
129/// @brief A variation of the previous addToCodeBody that takes in a bool value that determines
130/// if input is independent. This overload exists because there might other ways to determine if
131/// a value/collection of values is scope independent.
132/// @param in String to add to the squashed code.
133/// @param isScopeIndep The value determining if the input is scope dependent.
134void CodegenContext::addToCodeBody(std::string const &in, bool isScopeIndep /* = false */)
135{
137 indented = indented.Strip(TString::kBoth); // trim
138
139 std::string indent_str = "";
140 for (unsigned i = 0; i < _indent; ++i)
141 indent_str += " ";
142 indented = indented.Prepend(indent_str);
143
144 // FIXME: Multiline input.
145 // indent_str += "\n";
146 // indented = indented.ReplaceAll("\n", indent_str);
147
148 // If we are in a loop and the value is scope independent, save it at the top of the loop.
149 // else, just save it in the current scope.
150 if (_code.size() > 2 && isScopeIndep) {
151 _code[_code.size() - 2] += indented;
152 } else {
153 _code.back() += indented;
154 }
155}
156
157/// @brief Create a RAII scope for iterating over vector observables. You can't use the result of vector observables
158/// outside these loop scopes.
159/// @param in A pointer to the calling class, used to determine the loop dependent variables.
160std::unique_ptr<CodegenContext::LoopScope> CodegenContext::beginLoop(RooAbsArg const *in)
161{
162 pushScope();
163 unsigned loopLevel = _code.size() - 2; // subtract global + function scope.
164 std::string idx = "loopIdx" + std::to_string(loopLevel);
165
166 std::vector<TNamed const *> vars;
167 // set the results of the vector observables
168 for (auto const &it : _vecObsIndices) {
169 if (!in->dependsOn(it.first))
170 continue;
171
172 vars.push_back(it.first);
173 _nodeNames[it.first] = "obs[" + std::to_string(it.second) + " + " + idx + "]";
174 }
175
176 // TODO: we are using the size of the first loop variable to the the number
177 // of iterations, but it should be made sure that all loop vars are either
178 // scalar or have the same size.
179 std::size_t numEntries = 1;
180 for (auto &it : vars) {
181 std::size_t n = outputSize(it);
182 if (n > 1 && numEntries > 1 && n != numEntries) {
183 throw std::runtime_error("Trying to loop over variables with different sizes!");
184 }
185 numEntries = std::max(n, numEntries);
186 }
187
188 // Make sure that the name of this variable doesn't clash with other stuff
189 addToCodeBody(in, "for(int " + idx + " = 0; " + idx + " < " + std::to_string(numEntries) + "; " + idx + "++) {\n");
190
191 return std::make_unique<LoopScope>(*this, std::move(vars));
192}
193
195{
196 addToCodeBody("}\n");
197
198 // clear the results of the loop variables if they were vector observables
199 for (auto const &ptr : scope.vars()) {
200 if (_vecObsIndices.find(ptr) != _vecObsIndices.end())
201 _nodeNames.erase(ptr);
202 }
203 popScope();
204}
205
206/// @brief Get a unique variable name to be used in the generated code.
208{
209 return "t" + std::to_string(_tmpVarIdx++);
210}
211
212/// @brief A function to save an expression that includes/depends on the result of the input node.
213/// @param in The node on which the valueToSave depends on/belongs to.
214/// @param valueToSave The actual string value to save as a temporary.
215void CodegenContext::addResult(RooAbsArg const *in, std::string const &valueToSave)
216{
217 // std::string savedName = RooFit::Detail::makeValidVarName(in->GetName());
218 std::string savedName = getTmpVarName();
219
220 // Only save values if they contain operations or they are numerals. Otherwise, we can use them directly.
221
222 // Check if string is numeric.
223 char *end;
224 std::strtod(valueToSave.c_str(), &end);
225 bool isNumeric = (*end == '\0');
226
227 const bool hasOperations = valueToSave.find_first_of(":-+/*") != std::string::npos;
228
229 // If the name is not empty and this value is worth saving, save it to the correct scope.
230 // otherwise, just return the actual value itself
231 if (hasOperations || isNumeric) {
232 std::string outVarDecl = "const double " + savedName + " = " + valueToSave + ";\n";
234 } else {
236 }
237
238 addResult(in->namePtr(), savedName);
239}
240
241/// @brief Function to save a RooListProxy as an array in the squashed code.
242/// @param in The list to convert to array.
243/// @return Name of the array that stores the input list in the squashed code.
244std::string CodegenContext::buildArg(RooAbsCollection const &in, std::string const &arrayType)
245{
246 if (in.empty()) {
247 return "nullptr";
248 }
249
250 auto it = _listNames.find(in.uniqueId().value());
251 if (it != _listNames.end())
252 return it->second;
253
254 std::string savedName = getTmpVarName();
255 bool canSaveOutside = true;
256
257 std::stringstream declStrm;
258 declStrm << arrayType << " " << savedName << "[]{";
259 for (const auto arg : in) {
260 declStrm << getResult(*arg) << ",";
262 }
263 declStrm.seekp(-1, declStrm.cur);
264 declStrm << "};\n";
265
267
268 _listNames.insert({in.uniqueId().value(), savedName});
269 return savedName;
270}
271
272std::string CodegenContext::buildArg(std::span<const double> arr)
273{
274 unsigned int n = arr.size();
275 std::string offset = std::to_string(_xlArr.size());
276 _xlArr.reserve(_xlArr.size() + n);
277 for (unsigned int i = 0; i < n; i++) {
278 _xlArr.push_back(arr[i]);
279 }
280 return "xlArr + " + offset;
281}
282
283CodegenContext::ScopeRAII::ScopeRAII(RooAbsArg const *arg, CodegenContext &ctx) : _ctx(ctx), _arg(arg)
284{
285 std::ostringstream os;
286 Option_t *opts = nullptr;
288 _fn = os.str();
289 const std::string info = "// Begin -- " + _fn;
290 _ctx._indent++;
292}
293
295{
296 const std::string info = "// End -- " + _fn + "\n";
297 _ctx.addToCodeBody(_arg, info);
298 _ctx._indent--;
299}
300
302{
303 _code.push_back("");
304}
305
307{
308 std::string active_scope = _code.back();
309 _code.pop_back();
310 _code.back() += active_scope;
311}
312
314{
315 return !in->isReducerNode() && outputSize(in->namePtr()) == 1;
316}
317
318/// @brief Register a function that is only know to the interpreter to the context.
319/// This is useful to dump the standalone C++ code for the computation graph.
320void CodegenContext::collectFunction(std::string const &name)
321{
322 _collectedFunctions.emplace_back(name);
323}
324
325/// @brief Assemble and return the final code with the return expression and global statements.
326/// @param returnExpr The string representation of what the squashed function should return, usually the head node.
327/// @return The name of the declared function.
328std::string
329CodegenContext::buildFunction(RooAbsArg const &arg, std::map<RooFit::Detail::DataKey, std::size_t> const &outputSizes)
330{
331 CodegenContext ctx;
332 ctx.pushScope(); // push our global scope.
335 // We only want to take over parameters and observables
336 for (auto const &item : _nodeNames) {
337 if (startsWith(item.second, "params[") || startsWith(item.second, "obs[")) {
338 ctx._nodeNames.insert(item);
339 }
340 }
341 ctx._xlArr = _xlArr;
343
344 static int iCodegen = 0;
345 auto funcName = "roo_codegen_" + std::to_string(iCodegen++);
346
347 // Make sure the codegen implementations are known to the interpreter
348 gInterpreter->Declare("#include <RooFit/CodegenImpl.h>\n");
349
350 ctx.pushScope();
351 std::string funcBody = ctx.getResult(arg);
352 ctx.popScope();
353 funcBody = ctx._code[0] + "\n return " + funcBody + ";\n";
354
355 // Declare the function
356 std::stringstream bodyWithSigStrm;
357 bodyWithSigStrm << "double " << funcName << "(double* params, double const* obs, double const* xlArr) {\n"
358 << "constexpr double inf = std::numeric_limits<double>::infinity();\n"
359 << funcBody << "\n}";
360 ctx._collectedFunctions.emplace_back(funcName);
361 if (!gInterpreter->Declare(bodyWithSigStrm.str().c_str())) {
362 std::stringstream errorMsg;
363 std::string debugFileName = "_codegen_" + funcName + ".cxx";
364 errorMsg << "Function " << funcName << " could not be compiled. See above for details. Full code dumped to file "
365 << debugFileName << "for debugging";
366 {
367 std::ofstream outFile;
368 outFile.open(debugFileName.c_str());
369 outFile << bodyWithSigStrm.str();
370 }
371 oocoutE(nullptr, InputArguments) << errorMsg.str() << std::endl;
372 throw std::runtime_error(errorMsg.str().c_str());
373 }
374
375 _xlArr = ctx._xlArr;
377
378 return funcName;
379}
380
381void declareDispatcherCode(std::string const &funcName)
382{
383 std::string dispatcherCode = R"(
384namespace RooFit {
385namespace Experimental {
386
387template <class Arg_t, int P>
388auto FUNC_NAME(Arg_t &arg, CodegenContext &ctx, Prio<P> p)
389{
390 if constexpr (std::is_same<Prio<P>, PrioLowest>::value) {
391 return FUNC_NAME(arg, ctx);
392 } else {
393 return FUNC_NAME(arg, ctx, p.next());
394 }
395}
396
397template <class Arg_t>
398struct Caller_FUNC_NAME {
399
400 static auto call(RooAbsArg &arg, CodegenContext &ctx)
401 {
402 return FUNC_NAME(static_cast<Arg_t &>(arg), ctx, PrioHighest{});
403 }
404};
405
406} // namespace Experimental
407} // namespace RooFit
408 )";
409
411 gInterpreter->Declare(dispatcherCode.c_str());
412}
413
415{
416 static bool codeDeclared = false;
417 if (!codeDeclared) {
418 declareDispatcherCode("codegenImpl");
419 codeDeclared = true;
420 }
421
422 using Func = void (*)(RooAbsArg &, CodegenContext &);
423
424 Func func;
425
426 TClass *tclass = arg.IsA();
427
428 // Cache the overload resolutions
429 static std::unordered_map<TClass *, Func> dispatchMap;
430
431 auto found = dispatchMap.find(tclass);
432
433 if (found != dispatchMap.end()) {
434 func = found->second;
435 } else {
436 // Can probably done with CppInterop in the future to avoid string manipulation.
437 std::stringstream cmd;
438 cmd << "&RooFit::Experimental::Caller_codegenImpl<" << tclass->GetName() << ">::call;";
439 func = reinterpret_cast<Func>(gInterpreter->ProcessLine(cmd.str().c_str()));
440 dispatchMap[tclass] = func;
441 }
442
443 return func(arg, ctx);
444}
445
446} // namespace Experimental
447} // namespace RooFit
bool startsWith(std::string_view str, std::string_view prefix)
#define oocoutE(o, a)
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
#define gInterpreter
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
TClass * IsA() const override
Definition RooAbsArg.h:678
const TNamed * namePtr() const
De-duplicated pointer to this object's name.
Definition RooAbsArg.h:502
Int_t defaultPrintContents(Option_t *opt) const override
Define default contents to print.
Abstract container object that can hold multiple RooAbsArg objects.
A class to manage loop scopes using the RAII technique.
A class to maintain the context for squashing of RooFit models into code.
std::unordered_map< RooFit::UniqueId< RooAbsCollection >::Value_t, std::string > _listNames
A map to keep track of list names as assigned by addResult.
void addToGlobalScope(std::string const &str)
Adds the given string to the string block that will be emitted at the top of the squashed function.
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
std::string getTmpVarName() const
Get a unique variable name to be used in the generated code.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::unique_ptr< LoopScope > beginLoop(RooAbsArg const *in)
Create a RAII scope for iterating over vector observables.
void collectFunction(std::string const &name)
Register a function that is only know to the interpreter to the context.
void addVecObs(const char *key, int idx)
Since the squashed code represents all observables as a single flattened array, it is important to ke...
std::unordered_map< const TNamed *, int > _vecObsIndices
A map to keep track of the observable indices if they are non scalar.
int observableIndexOf(const RooAbsArg &arg) const
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
Map of node output sizes.
std::string buildFunction(RooAbsArg const &arg, std::map< RooFit::Detail::DataKey, std::size_t > const &outputSizes={})
Assemble and return the final code with the return expression and global statements.
std::string buildArg(RooAbsCollection const &x, std::string const &arrayType="double")
Function to save a RooListProxy as an array in the squashed code.
void endLoop(LoopScope const &scope)
std::vector< std::string > _collectedFunctions
bool isScopeIndependent(RooAbsArg const *in) const
std::vector< std::string > _code
The code layered by lexical scopes used as a stack.
unsigned _indent
The indentation level for pretty-printing.
std::unordered_map< const TNamed *, std::string > _nodeNames
Map of node names to their result strings.
std::size_t outputSize(RooFit::Detail::DataKey key) const
Figure out the output size of a node.
ScopeRAII OutputScopeRangeComment(RooAbsArg const *arg)
int _tmpVarIdx
Index to get unique names for temporary variables.
static const TNamed * known(const char *stringPtr)
If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name)...
virtual StyleOption defaultPrintStyle(Option_t *opt) const
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer,...
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:138
@ kBoth
Definition TString.h:284
const Int_t n
Definition legend1.C:16
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)
void declareDispatcherCode(std::string const &funcName)
void codegen(RooAbsArg &arg, CodegenContext &ctx)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:67
@ InputArguments
ScopeRAII(RooAbsArg const *arg, CodegenContext &ctx)