Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooFit::Experimental::CodegenContext Class Reference

A class to maintain the context for squashing of RooFit models into code.

Definition at line 46 of file CodegenContext.h.

Classes

class  LoopScope
 A class to manage loop scopes using the RAII technique. More...
 
struct  ScopeRAII
 

Public Member Functions

void addResult (const char *key, std::string const &value)
 Adds (or overwrites) the string representing the result of a node.
 
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.
 
void addToCodeBody (std::string const &in, bool isScopeIndep=false)
 A variation of the previous addToCodeBody that takes in a bool value that determines if input is independent.
 
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.
 
void addVecObs (const char *key, int idx)
 Since the squashed code represents all observables as a single flattened array, it is important to keep track of the start index for a vector valued observable which can later be expanded to access the correct element.
 
std::unique_ptr< LoopScopebeginLoop (RooAbsArg const *in)
 Create a RAII scope for iterating over vector observables.
 
std::string buildArg (RooAbsCollection const &x)
 Function to save a RooListProxy as an array in the squashed code.
 
std::string buildArg (std::span< const double > arr)
 
std::string buildArg (std::span< const int > arr)
 
template<typename... Args_t>
std::string buildCall (std::string const &funcname, Args_t const &...args)
 Build the code to call the function with name funcname, passing some arguments.
 
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::vector< std::string > const & collectedFunctions ()
 
void collectFunction (std::string const &name)
 Register a function that is only know to the interpreter to the context.
 
std::string const & getResult (RooAbsArg const &arg)
 Gets the result for the given node using the node name.
 
template<class T >
std::string const & getResult (RooTemplateProxy< T > const &key)
 
std::string getTmpVarName () const
 Get a unique variable name to be used in the generated code.
 
ScopeRAII OutputScopeRangeComment (RooAbsArg const *arg)
 
std::size_t outputSize (RooFit::Detail::DataKey key) const
 Figure out the output size of a node.
 
auto const & outputSizes () const
 
std::vector< double > const & xlArr ()
 

Private Member Functions

void addResult (TNamed const *key, std::string const &value)
 
std::string buildArg (RooAbsArg const &arg)
 
template<class T >
std::string buildArg (RooTemplateProxy< T > const &arg)
 
std::string buildArg (std::nullptr_t)
 
std::string buildArg (std::string const &x)
 
template<class T , typename std::enable_if< std::is_floating_point< T >{}, bool >::type = true>
std::string buildArg (T x)
 
template<class T , typename std::enable_if< std::is_integral< T >{}, bool >::type = true>
std::string buildArg (T x)
 
std::string buildArgs ()
 
template<class Arg_t >
std::string buildArgs (Arg_t const &arg)
 
template<typename Arg_t , typename... Args_t>
std::string buildArgs (Arg_t const &arg, Args_t const &...args)
 
template<class T >
std::string buildArgSpanImpl (std::span< const T > arr)
 
void endLoop (LoopScope const &scope)
 
bool isScopeIndependent (RooAbsArg const *in) const
 
void popScope ()
 
void pushScope ()
 
template<class T >
std::string typeName () const
 
template<>
std::string typeName () const
 
template<>
std::string typeName () const
 

Private Attributes

std::vector< std::string > _code
 The code layered by lexical scopes used as a stack.
 
std::vector< std::string > _collectedFunctions
 
unsigned _indent = 0
 The indentation level for pretty-printing.
 
std::unordered_map< RooFit::UniqueId< RooAbsCollection >::Value_t, std::string > _listNames
 A map to keep track of list names as assigned by addResult.
 
std::unordered_map< const TNamed *, std::string > _nodeNames
 Map of node names to their result strings.
 
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
 Map of node output sizes.
 
int _tmpVarIdx = 0
 Index to get unique names for temporary variables.
 
std::unordered_map< const TNamed *, int_vecObsIndices
 A map to keep track of the observable indices if they are non scalar.
 
std::vector< double_xlArr
 

#include <RooFit/CodegenContext.h>

Member Function Documentation

◆ addResult() [1/3]

void RooFit::Experimental::CodegenContext::addResult ( const char *  key,
std::string const &  value 
)

Adds (or overwrites) the string representing the result of a node.

Parameters
keyThe name of the node to add the result for.
valueThe new name to assign/overwrite.

Definition at line 41 of file CodegenContext.cxx.

◆ addResult() [2/3]

void RooFit::Experimental::CodegenContext::addResult ( RooAbsArg const *  in,
std::string const &  valueToSave 
)

A function to save an expression that includes/depends on the result of the input node.

Parameters
inThe node on which the valueToSave depends on/belongs to.
valueToSaveThe actual string value to save as a temporary.

Definition at line 204 of file CodegenContext.cxx.

◆ addResult() [3/3]

void RooFit::Experimental::CodegenContext::addResult ( TNamed const *  key,
std::string const &  value 
)
private

Definition at line 48 of file CodegenContext.cxx.

◆ addToCodeBody() [1/2]

void RooFit::Experimental::CodegenContext::addToCodeBody ( RooAbsArg const *  klass,
std::string const &  in 
)

Adds the input string to the squashed code body.

If a class implements a translate function that wants to emit something to the squashed code body, it must call this function with the code it wants to emit. In case of loops, automatically determines if code needs to be stored inside or outside loop scope.

Parameters
klassThe class requesting this addition, usually 'this'.
inString to add to the squashed code.

Definition at line 111 of file CodegenContext.cxx.

◆ addToCodeBody() [2/2]

void RooFit::Experimental::CodegenContext::addToCodeBody ( std::string const &  in,
bool  isScopeIndep = false 
)

A variation of the previous addToCodeBody that takes in a bool value that determines if input is independent.

This overload exists because there might other ways to determine if a value/collection of values is scope independent.

Parameters
inString to add to the squashed code.
isScopeIndepThe value determining if the input is scope dependent.

Definition at line 123 of file CodegenContext.cxx.

◆ addToGlobalScope()

void RooFit::Experimental::CodegenContext::addToGlobalScope ( std::string const &  str)

Adds the given string to the string block that will be emitted at the top of the squashed function.

Useful for variable declarations.

Parameters
strThe string to add to the global scope.

Definition at line 88 of file CodegenContext.cxx.

◆ addVecObs()

void RooFit::Experimental::CodegenContext::addVecObs ( const char *  key,
int  idx 
)

Since the squashed code represents all observables as a single flattened array, it is important to keep track of the start index for a vector valued observable which can later be expanded to access the correct element.

For example, a vector valued variable x with 10 entries will be squashed to obs[start_idx + i].

Parameters
keyThe name of the node representing the vector valued observable.
idxThe start index (or relative position of the observable in the set of all observables).

Definition at line 99 of file CodegenContext.cxx.

◆ beginLoop()

std::unique_ptr< CodegenContext::LoopScope > RooFit::Experimental::CodegenContext::beginLoop ( RooAbsArg const *  in)

Create a RAII scope for iterating over vector observables.

You can't use the result of vector observables outside these loop scopes.

Parameters
inA pointer to the calling class, used to determine the loop dependent variables.

Definition at line 149 of file CodegenContext.cxx.

◆ buildArg() [1/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( RooAbsArg const &  arg)
inlineprivate

Definition at line 166 of file CodegenContext.h.

◆ buildArg() [2/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( RooAbsCollection const &  in)

Function to save a RooListProxy as an array in the squashed code.

Parameters
inThe list to convert to array.
Returns
Name of the array that stores the input list in the squashed code.

Definition at line 229 of file CodegenContext.cxx.

◆ buildArg() [3/9]

template<class T >
std::string RooFit::Experimental::CodegenContext::buildArg ( RooTemplateProxy< T > const &  arg)
inlineprivate

Definition at line 169 of file CodegenContext.h.

◆ buildArg() [4/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( std::nullptr_t  )
inlineprivate

Definition at line 164 of file CodegenContext.h.

◆ buildArg() [5/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( std::span< const double arr)

Definition at line 257 of file CodegenContext.cxx.

◆ buildArg() [6/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( std::span< const int arr)
inline

Definition at line 114 of file CodegenContext.h.

◆ buildArg() [7/9]

std::string RooFit::Experimental::CodegenContext::buildArg ( std::string const &  x)
inlineprivate

Definition at line 162 of file CodegenContext.h.

◆ buildArg() [8/9]

template<class T , typename std::enable_if< std::is_floating_point< T >{}, bool >::type = true>
std::string RooFit::Experimental::CodegenContext::buildArg ( x)
inlineprivate

Definition at line 150 of file CodegenContext.h.

◆ buildArg() [9/9]

template<class T , typename std::enable_if< std::is_integral< T >{}, bool >::type = true>
std::string RooFit::Experimental::CodegenContext::buildArg ( x)
inlineprivate

Definition at line 157 of file CodegenContext.h.

◆ buildArgs() [1/3]

std::string RooFit::Experimental::CodegenContext::buildArgs ( )
inlineprivate

Definition at line 174 of file CodegenContext.h.

◆ buildArgs() [2/3]

template<class Arg_t >
std::string RooFit::Experimental::CodegenContext::buildArgs ( Arg_t const &  arg)
inlineprivate

Definition at line 177 of file CodegenContext.h.

◆ buildArgs() [3/3]

template<typename Arg_t , typename... Args_t>
std::string RooFit::Experimental::CodegenContext::buildArgs ( Arg_t const &  arg,
Args_t const &...  args 
)
inlineprivate

Definition at line 183 of file CodegenContext.h.

◆ buildArgSpanImpl()

template<class T >
std::string RooFit::Experimental::CodegenContext::buildArgSpanImpl ( std::span< const T >  arr)
private

Definition at line 221 of file CodegenContext.h.

◆ buildCall()

template<typename... Args_t>
std::string RooFit::Experimental::CodegenContext::buildCall ( std::string const &  funcname,
Args_t const &...  args 
)
inline

Build the code to call the function with name funcname, passing some arguments.

The arguments can either be doubles or some RooFit arguments whose results will be looked up in the context.

Definition at line 82 of file CodegenContext.h.

◆ buildFunction()

std::string RooFit::Experimental::CodegenContext::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.

Parameters
returnExprThe string representation of what the squashed function should return, usually the head node.
Returns
The name of the declared function.

Definition at line 314 of file CodegenContext.cxx.

◆ collectedFunctions()

std::vector< std::string > const & RooFit::Experimental::CodegenContext::collectedFunctions ( )
inline

Definition at line 119 of file CodegenContext.h.

◆ collectFunction()

void RooFit::Experimental::CodegenContext::collectFunction ( std::string const &  name)

Register a function that is only know to the interpreter to the context.

This is useful to dump the standalone C++ code for the computation graph.

Definition at line 305 of file CodegenContext.cxx.

◆ endLoop()

void RooFit::Experimental::CodegenContext::endLoop ( LoopScope const &  scope)
private

Definition at line 183 of file CodegenContext.cxx.

◆ getResult() [1/2]

std::string const & RooFit::Experimental::CodegenContext::getResult ( RooAbsArg const &  arg)

Gets the result for the given node using the node name.

This node also performs the necessary code generation through recursive calls to 'translate'. A call to this function modifies the already existing code body.

Parameters
keyThe node to get the result string for.
Returns
String representing the result of this node.

Definition at line 58 of file CodegenContext.cxx.

◆ getResult() [2/2]

template<class T >
std::string const & RooFit::Experimental::CodegenContext::getResult ( RooTemplateProxy< T > const &  key)
inline

Definition at line 54 of file CodegenContext.h.

◆ getTmpVarName()

std::string RooFit::Experimental::CodegenContext::getTmpVarName ( ) const

Get a unique variable name to be used in the generated code.

Definition at line 196 of file CodegenContext.cxx.

◆ isScopeIndependent()

bool RooFit::Experimental::CodegenContext::isScopeIndependent ( RooAbsArg const *  in) const
private

Definition at line 298 of file CodegenContext.cxx.

◆ OutputScopeRangeComment()

ScopeRAII RooFit::Experimental::CodegenContext::OutputScopeRangeComment ( RooAbsArg const *  arg)
inline

Definition at line 135 of file CodegenContext.h.

◆ outputSize()

std::size_t RooFit::Experimental::CodegenContext::outputSize ( RooFit::Detail::DataKey  key) const
inline

Figure out the output size of a node.

It is the size of the vector observable that it depends on, or 1 if it doesn't depend on any or is a reducer node.

Parameters
keyThe node to look up the size for.

Definition at line 63 of file CodegenContext.h.

◆ outputSizes()

auto const & RooFit::Experimental::CodegenContext::outputSizes ( ) const
inline

Definition at line 124 of file CodegenContext.h.

◆ popScope()

void RooFit::Experimental::CodegenContext::popScope ( )
private

Definition at line 291 of file CodegenContext.cxx.

◆ pushScope()

void RooFit::Experimental::CodegenContext::pushScope ( )
private

Definition at line 286 of file CodegenContext.cxx.

◆ typeName() [1/3]

template<class T >
std::string RooFit::Experimental::CodegenContext::typeName ( ) const
private

◆ typeName() [2/3]

template<>
std::string RooFit::Experimental::CodegenContext::typeName ( ) const
inlineprivate

Definition at line 210 of file CodegenContext.h.

◆ typeName() [3/3]

template<>
std::string RooFit::Experimental::CodegenContext::typeName ( ) const
inlineprivate

Definition at line 215 of file CodegenContext.h.

◆ xlArr()

std::vector< double > const & RooFit::Experimental::CodegenContext::xlArr ( )
inline

Definition at line 116 of file CodegenContext.h.

Member Data Documentation

◆ _code

std::vector<std::string> RooFit::Experimental::CodegenContext::_code
private

The code layered by lexical scopes used as a stack.

Definition at line 198 of file CodegenContext.h.

◆ _collectedFunctions

std::vector<std::string> RooFit::Experimental::CodegenContext::_collectedFunctions
private

Definition at line 206 of file CodegenContext.h.

◆ _indent

unsigned RooFit::Experimental::CodegenContext::_indent = 0
private

The indentation level for pretty-printing.

Definition at line 200 of file CodegenContext.h.

◆ _listNames

std::unordered_map<RooFit::UniqueId<RooAbsCollection>::Value_t, std::string> RooFit::Experimental::CodegenContext::_listNames
private

A map to keep track of list names as assigned by addResult.

Definition at line 204 of file CodegenContext.h.

◆ _nodeNames

std::unordered_map<const TNamed *, std::string> RooFit::Experimental::CodegenContext::_nodeNames
private

Map of node names to their result strings.

Definition at line 192 of file CodegenContext.h.

◆ _nodeOutputSizes

std::map<RooFit::Detail::DataKey, std::size_t> RooFit::Experimental::CodegenContext::_nodeOutputSizes
private

Map of node output sizes.

Definition at line 196 of file CodegenContext.h.

◆ _tmpVarIdx

int RooFit::Experimental::CodegenContext::_tmpVarIdx = 0
mutableprivate

Index to get unique names for temporary variables.

Definition at line 202 of file CodegenContext.h.

◆ _vecObsIndices

std::unordered_map<const TNamed *, int> RooFit::Experimental::CodegenContext::_vecObsIndices
private

A map to keep track of the observable indices if they are non scalar.

Definition at line 194 of file CodegenContext.h.

◆ _xlArr

std::vector<double> RooFit::Experimental::CodegenContext::_xlArr
private

Definition at line 205 of file CodegenContext.h.

Libraries for RooFit::Experimental::CodegenContext:

The documentation for this class was generated from the following files: