HDTree  0.5.2
HDTree C++ API
transform.cxx File Reference

Example of transforming an HDTree by adding more branches. More...

#include "hdtree/Tree.h"
#include "examples.h"
Include dependency graph for transform.cxx:

Functions

int main (int argc, char **argv)
 

Detailed Description

Example of transforming an HDTree by adding more branches.

This example determines whether a tree should be copied into a new file or simply transformed in its current file by what arguments are provided to the program. We assume the input tree was generated by the hdtree-eg-save example program defined in save.cxx (i.e. we look for specific branches).

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

parse command line for arguments

Wrap an existing on-disk HDTree

Here is where we make the decision on whether to copy a tree into a new file or not. We choose to copy the tree into a new file if a destination file and tree are provided on the command line. We use the slightly-ugly ternary operator in order to avoid unnecessary copying from an if-else tree.

We are going to calculate the average of the random numbers within each tree entry, so we create a new branch to store that result as well as retrieve the branch with the numbers we will use.

Actual update and filling of the HDTree.

We use a tree helper that will make sure we go through each entry in the tree, calling the hdtree::Tree::load at the beginning and hdtree::Tree::save at the end of each run in the loop. This code is essentially equivalent to

for (std::size_t i{0}; i < tree.entries(); ++i) {
tree.load();
// the code inside the lambda function below
if (rand_nums->size() > 0) {
*avg = (std::reduce(rand_nums->begin(), rand_nums->end()))/rand_nums->size();
} else {
*avg = -1;
}
//
tree.save();
}

Just using this example to show off some potentially-helpful features - if lambda functions are causing you difficulty, feel free to avoid them. Just make sure to remember to call the load and save functions!

The final flushing of the data to disk as well as handle cleanup procedures will all be handled automatically by deconstruction.