Skip to content

Commit

Permalink
Updates to StFwdTrackMaker to allow running without XML config file (#…
Browse files Browse the repository at this point in the history
…635)

This update makes the StFwdTrackMaker fully configurable from the ROOT
macro without the need for any external XML config file.

Changes:
- FwdTrackerConfig: new functionality to allow loading a config from a
string (instead of a file) and new functions for setting arbitrary
values in the config
- StFwdTrackMaker: many new functions for setting configuration
parameters
- New logic to use a default configuration if set for Data or Mc, else
load from a config file

The default config file is embedded for consistency.
In the future BFC options can be used to toggle parameters if they need
to change from one production to the next.
  • Loading branch information
jdbrice authored Mar 27, 2024
1 parent 2e60a9d commit 2e09826
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 37 deletions.
68 changes: 64 additions & 4 deletions StRoot/StFwdTrackMaker/FwdTrackerConfig.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,48 @@ std::stringstream FwdTrackerConfig::sstr;
// template specializations
////

// Specialization for string to avoid extra conversions
/**
* @brief write a value to path
*
* @tparam template specialization for std::string
* @param path path to write, if it DNE it is created
* @param v value (of type string) to write
*/
template <>
void FwdTrackerConfig::set( std::string path, std::string v ) {

FwdTrackerConfig::canonize( path );
// convrt from string to type T and return
mNodes[ path ] = v;
}

/**
* @brief write a value to path
*
* @tparam template specialization for bool
* @param path path to write, if it DNE it is created
* @param bv boolean to write
*/
template <>
void FwdTrackerConfig::set( std::string path, bool bv ) {

FwdTrackerConfig::canonize( path );
// convrt from string to type T and return
std::string v = "false";
if (bv)
v = "true";
mNodes[ path ] = v;
}

//
/**
* @brief Get a value from the path
*
* @tparam Specialization for string to avoid extra conversions
* @param path path to lookup
* @param dv default value if path DNE
* @return std::string value at path or default
*/
template <>
std::string FwdTrackerConfig::get( std::string path, std::string dv ) const {
// return default value if path DNE
Expand All @@ -20,13 +61,26 @@ std::string FwdTrackerConfig::get( std::string path, std::string dv ) const {
return ( mNodes.at( path ) );
}

// conversion to string is a noop
/**
* @brief conversion to string is a noop
*
* @tparam string specialization
* @param str input
* @return std::string output (unchanged)
*/
template <>
std::string FwdTrackerConfig::convert( std::string str ) const {
return str;
}

// specialization for bool adds recognition of strings "true" and "false" (lower case)
/**
* @brief specialization for bool adds recognition of strings "true" and "false" (lower case)
*
* @tparam bool specialization, fallback to int check
* @param str input string
* @return true for "true"
* @return false for "false"
*/
template <>
bool FwdTrackerConfig::convert( std::string str ) const {

Expand All @@ -39,7 +93,13 @@ bool FwdTrackerConfig::convert( std::string str ) const {
return static_cast<bool>(convert<int>( str ));
}

// get as ROOT TString
/**
* @brief get as ROOT TString
*
* @tparam TString specialization
* @param str input value
* @return TString output as ROOT TString
*/
template <>
TString FwdTrackerConfig::convert(std::string str) const {
TString r(str);
Expand Down
135 changes: 116 additions & 19 deletions StRoot/StFwdTrackMaker/FwdTrackerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ class FwdTrackerConfig {
std::map<std::string, std::string> mNodes;
static std::stringstream sstr; // reused for string to numeric conversion

// assumes bare path and adds [i] until DNE
// reports lowest non-existant index
// starts at 1 since 0 is checked on existance
/**
* @brief get lowest non-existing path index
* assumes bare path and adds [i] until DNE
* starts at 1 since 0 is checked on existance
* @param path base path to check
* @return size_t index, starts at 1
*/
size_t pathCount( const std::string path ){
size_t index = 1;
std::string p = path + TString::Format( "[%zu]", index ).Data();
Expand All @@ -39,6 +43,14 @@ class FwdTrackerConfig {
return index;
}

/**
* @brief Reads an xml document and writes it into map
*
* @param xml xml document to map
* @param node starting node - allows recursive mapping
* @param level the integer index of the level of current parsing
* @param path the path for the current node
*/
void mapFile(TXMLEngine &xml, XMLNodePointer_t node, Int_t level, std::string path = "") {
using namespace std;
// add the path delimeter above top level
Expand Down Expand Up @@ -83,7 +95,11 @@ class FwdTrackerConfig {
} // mapFile
public:

// sanitizes a path to its canonical form
/**
* @brief Returns a path in its cannonical form
*
* @param path Path to cannoize, returned in place by reference
*/
static void canonize( std::string &path ) {
// remove whitespace
path.erase(std::remove_if(path.begin(), path.end(), static_cast<int(*) (int)>(std::isspace)), path.end());
Expand All @@ -98,7 +114,11 @@ class FwdTrackerConfig {
return;
}

// dump config to a basic string representation - mostly for debugging
/**
* @brief dump config to a basic string representation - mostly for debugging
*
* @return std::string
*/
std::string dump() const {
using namespace std;
FwdTrackerConfig::sstr.str("");
Expand All @@ -109,17 +129,29 @@ class FwdTrackerConfig {
return FwdTrackerConfig::sstr.str();
}

// Does a path exist
// Either node or attribute - used to determine if default value is used
/**
* @brief returns whether or not a path exist
* Either node or attribute - used to determine if default value is used
*
* @param path - the path to check
* @return true : path exists
* @return false : path DNE
*/
bool exists( std::string path ) const {
FwdTrackerConfig::canonize( path );
if ( 0 == mNodes.count( path ) )
return false;
return true;
}

// generic conversion to type T from std::string
// override this for special conversions
/**
* @brief Generic conversion of type T from string
* override this for special conversions
*
* @tparam T : Type to convert to and return
* @param s : input string to use for conversion
* @return T converted value of type T
*/
template <typename T>
T convert( std::string s ) const {
T rv;
Expand All @@ -130,10 +162,30 @@ class FwdTrackerConfig {
return rv;
}


/**
* @brief Generic conversion of type T to a string
*
* @tparam T : type to convert
* @param v : value of type T
* @return std::string output string with representation of T
*/
template <typename T>
std::string convertTo( T v ) const {
FwdTrackerConfig::sstr.str("");
FwdTrackerConfig::sstr.clear();
FwdTrackerConfig::sstr << v;
return FwdTrackerConfig::sstr.str();
}


// template function for getting any type that can be converted from string via stringstream
/**
* @brief template function for getting any type that can be converted from string via stringstream
*
* @tparam T type to return
* @param path path to lookup
* @param dv default value to return if the node DNE
* @return T return value of type T
*/
template <typename T>
T get( std::string path, T dv ) const {

Expand All @@ -146,8 +198,28 @@ class FwdTrackerConfig {
return convert<T>( mNodes.at( path ) );
}

/**
* @brief Writes a value of type T to the map
* Uses convertTo<T> to convert type T to a string rep
* @tparam T type of value to write
* @param path path to write to
* @param v value of type T
*/
template <typename T>
void set( std::string path, T v ) {
FwdTrackerConfig::canonize( path );
// convrt from string to type T and return
mNodes[ path ] = convertTo<T>( v );
}


/**
* @brief Get a Vector object from config
*
* @tparam T type of value for the vector object
* @param path path to lookup
* @param dv default value, can use initializer list
* @return std::vector<T> vector of type T returned
*/
template <typename T>
std::vector<T> getVector( std::string path, std::vector<T> dv ) const {
if ( !exists( path ) )
Expand Down Expand Up @@ -176,7 +248,12 @@ class FwdTrackerConfig {
return result;
}

// list the paths of children nodes for a given node
/**
* @brief list the paths of children nodes for a given node
*
* @param path path to search for children
* @return std::vector<std::string> list of full paths to the children nodes
*/
std::vector<std::string> childrenOf( std::string path ) const {
using namespace std;
vector<string> result;
Expand Down Expand Up @@ -204,17 +281,28 @@ class FwdTrackerConfig {
return result;
}

// Constructor is noop, use load(...)
/**
* @brief Constructor is noop, use load(...)
*
*/
FwdTrackerConfig() {}

// constructor that immediately loads an xml file
/**
* @brief Construct a new Fwd Tracker Config object and load a file
*
* @param filename
*/
FwdTrackerConfig(std::string filename) {
load( filename );
}

// Main setup routine.
// Loads the given XML file and maps it
void load( std::string filename ) {
/**
* @brief Main setup routine
* Loads the given XML file (or string) and maps it
* @param filename filename (or xml string) to load. If file the content is loaded as an xml doc
* @param asString false: filename is loaded and contents treated as xml doc, true: treat the string `filename` directly as an xml doc
*/
void load( std::string filename, bool asString = false ) {
using namespace std;

// empty the map of mNodes
Expand All @@ -224,7 +312,12 @@ class FwdTrackerConfig {
TXMLEngine xml;

// Now try to parse xml file
XMLDocPointer_t xmldoc = xml.ParseFile(filename.c_str());
XMLDocPointer_t xmldoc;
if (asString)
xmldoc = xml.ParseString(filename.c_str());
else
xmldoc = xml.ParseFile(filename.c_str());

if (!xmldoc) { // parse failed, TODO inform of error
mErrorParsing = true;
return;
Expand All @@ -249,5 +342,9 @@ template <>
TString FwdTrackerConfig::convert(std::string str) const;
template <>
std::string FwdTrackerConfig::get( std::string path, std::string dv ) const;
template <>
void FwdTrackerConfig::set( std::string path, std::string v );
template <>
void FwdTrackerConfig::set( std::string path, bool bv );

#endif
Loading

0 comments on commit 2e09826

Please sign in to comment.