Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooFit::Detail::CodeSquashContext Class Reference

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

Definition at line 36 of file CodeSquashContext.h.

Classes

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

Public Member Functions

 CodeSquashContext (std::map< RooFit::Detail::DataKey, std::size_t > const &outputSizes)
 
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::string assembleCode (std::string const &returnExpr)
 Assemble and return the final code with the return expression and global statements.
 
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)
 
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 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 ()
 Get a unique variable name to be used in the generated code.
 
std::size_t outputSize (RooFit::Detail::DataKey key) const
 Figure out the output size of a node.
 

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)
 
void endLoop (LoopScope const &scope)
 
bool isScopeIndependent (RooAbsArg const *in) const
 

Private Attributes

std::string _code
 Stores the squashed code body.
 
std::string _globalScope
 Block of code that is placed before the rest of the function body.
 
int _loopLevel = 0
 The current number of for loops the started.
 
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 _scopePtr = -1
 Keeps track of the position to go back and insert code to.
 
std::string _tempScope
 Stores code that eventually gets injected into main code body.
 
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::unordered_map< RooFit::UniqueId< RooAbsCollection >::Value_t, std::string > listNames
 A map to keep track of list names as assigned by addResult.
 

#include <RooFit/Detail/CodeSquashContext.h>

Constructor & Destructor Documentation

◆ CodeSquashContext()

RooFit::Detail::CodeSquashContext::CodeSquashContext ( std::map< RooFit::Detail::DataKey, std::size_t > const &  outputSizes)
inline

Definition at line 38 of file CodeSquashContext.h.

Member Function Documentation

◆ addResult() [1/3]

void RooFit::Detail::CodeSquashContext::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 28 of file CodeSquashContext.cxx.

◆ addResult() [2/3]

void RooFit::Detail::CodeSquashContext::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 190 of file CodeSquashContext.cxx.

◆ addResult() [3/3]

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

Definition at line 35 of file CodeSquashContext.cxx.

◆ addToCodeBody() [1/2]

void RooFit::Detail::CodeSquashContext::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 103 of file CodeSquashContext.cxx.

◆ addToCodeBody() [2/2]

void RooFit::Detail::CodeSquashContext::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 115 of file CodeSquashContext.cxx.

◆ addToGlobalScope()

void RooFit::Detail::CodeSquashContext::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 73 of file CodeSquashContext.cxx.

◆ addVecObs()

void RooFit::Detail::CodeSquashContext::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 91 of file CodeSquashContext.cxx.

◆ assembleCode()

std::string RooFit::Detail::CodeSquashContext::assembleCode ( std::string const &  returnExpr)

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 final body of the function.

Definition at line 81 of file CodeSquashContext.cxx.

◆ beginLoop()

std::unique_ptr< CodeSquashContext::LoopScope > RooFit::Detail::CodeSquashContext::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 128 of file CodeSquashContext.cxx.

◆ buildArg() [1/8]

std::string RooFit::Detail::CodeSquashContext::buildArg ( RooAbsArg const &  arg)
inlineprivate

Definition at line 133 of file CodeSquashContext.h.

◆ buildArg() [2/8]

std::string RooFit::Detail::CodeSquashContext::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 214 of file CodeSquashContext.cxx.

◆ buildArg() [3/8]

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

Definition at line 136 of file CodeSquashContext.h.

◆ buildArg() [4/8]

std::string RooFit::Detail::CodeSquashContext::buildArg ( std::nullptr_t  )
inlineprivate

Definition at line 131 of file CodeSquashContext.h.

◆ buildArg() [5/8]

std::string RooFit::Detail::CodeSquashContext::buildArg ( std::span< const double arr)

Definition at line 238 of file CodeSquashContext.cxx.

◆ buildArg() [6/8]

std::string RooFit::Detail::CodeSquashContext::buildArg ( std::string const &  x)
inlineprivate

Definition at line 129 of file CodeSquashContext.h.

◆ buildArg() [7/8]

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

Definition at line 117 of file CodeSquashContext.h.

◆ buildArg() [8/8]

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

Definition at line 124 of file CodeSquashContext.h.

◆ buildArgs() [1/3]

std::string RooFit::Detail::CodeSquashContext::buildArgs ( )
inlineprivate

Definition at line 141 of file CodeSquashContext.h.

◆ buildArgs() [2/3]

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

Definition at line 144 of file CodeSquashContext.h.

◆ buildArgs() [3/3]

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

Definition at line 150 of file CodeSquashContext.h.

◆ buildCall()

template<typename... Args_t>
std::string RooFit::Detail::CodeSquashContext::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 77 of file CodeSquashContext.h.

◆ endLoop()

void RooFit::Detail::CodeSquashContext::endLoop ( LoopScope const &  scope)
private

Definition at line 164 of file CodeSquashContext.cxx.

◆ getResult() [1/2]

std::string const & RooFit::Detail::CodeSquashContext::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 45 of file CodeSquashContext.cxx.

◆ getResult() [2/2]

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

Definition at line 48 of file CodeSquashContext.h.

◆ getTmpVarName()

std::string RooFit::Detail::CodeSquashContext::getTmpVarName ( )

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

Definition at line 182 of file CodeSquashContext.cxx.

◆ isScopeIndependent()

bool RooFit::Detail::CodeSquashContext::isScopeIndependent ( RooAbsArg const *  in) const
private

Definition at line 253 of file CodeSquashContext.cxx.

◆ outputSize()

std::size_t RooFit::Detail::CodeSquashContext::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 57 of file CodeSquashContext.h.

Member Data Documentation

◆ _code

std::string RooFit::Detail::CodeSquashContext::_code
private

Stores the squashed code body.

Definition at line 164 of file CodeSquashContext.h.

◆ _globalScope

std::string RooFit::Detail::CodeSquashContext::_globalScope
private

Block of code that is placed before the rest of the function body.

Definition at line 158 of file CodeSquashContext.h.

◆ _loopLevel

int RooFit::Detail::CodeSquashContext::_loopLevel = 0
private

The current number of for loops the started.

Definition at line 166 of file CodeSquashContext.h.

◆ _nodeNames

std::unordered_map<const TNamed *, std::string> RooFit::Detail::CodeSquashContext::_nodeNames
private

Map of node names to their result strings.

Definition at line 156 of file CodeSquashContext.h.

◆ _nodeOutputSizes

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

Map of node output sizes.

Definition at line 162 of file CodeSquashContext.h.

◆ _scopePtr

int RooFit::Detail::CodeSquashContext::_scopePtr = -1
private

Keeps track of the position to go back and insert code to.

Definition at line 170 of file CodeSquashContext.h.

◆ _tempScope

std::string RooFit::Detail::CodeSquashContext::_tempScope
private

Stores code that eventually gets injected into main code body.

Mainly used for placing decls outside of loops.

Definition at line 173 of file CodeSquashContext.h.

◆ _tmpVarIdx

int RooFit::Detail::CodeSquashContext::_tmpVarIdx = 0
private

Index to get unique names for temporary variables.

Definition at line 168 of file CodeSquashContext.h.

◆ _vecObsIndices

std::unordered_map<const TNamed *, int> RooFit::Detail::CodeSquashContext::_vecObsIndices
private

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

Definition at line 160 of file CodeSquashContext.h.

◆ listNames

std::unordered_map<RooFit::UniqueId<RooAbsCollection>::Value_t, std::string> RooFit::Detail::CodeSquashContext::listNames
private

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

Definition at line 175 of file CodeSquashContext.h.

Libraries for RooFit::Detail::CodeSquashContext:

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