Project
Loading...
Searching...
No Matches
o2::framework::RootTreeWriter Class Reference

A generic writer interface for ROOT TTree objects. More...

#include <RootTreeWriter.h>

Classes

struct  BranchDef
 BranchDef is used to define the mapping between inputs and branches. More...
 
struct  DefaultKeyExtractor
 

Public Types

enum struct  BranchVerbosity { Quiet , Medium , High }
 
using key_type = std::string
 
using IndexExtractor = std::function< size_t(o2::framework::DataRef const &)>
 
using BranchNameMapper = std::function< std::string(std::string, size_t)>
 
using CustomClose = std::function< void(TFile *file, TTree *tree)>
 

Public Member Functions

 RootTreeWriter ()=delete
 default constructor forbidden
 
 ~RootTreeWriter ()
 a destructor making sure that the Writer is closed (if didn't happen before)
 
template<typename... Args>
 RootTreeWriter (const char *filename, const char *treename, Args &&... args)
 
void init (const char *filename, const char *treename, const char *treetitle=nullptr)
 
void setBranchName (size_t index, const char *branchName)
 
template<typename ContextType >
void operator() (ContextType &&context)
 
void close ()
 
void autoSave ()
 autosave the tree
 
bool isClosed () const
 
size_t getStoreSize () const
 

Static Public Member Functions

template<typename T >
static TBranch * remapBranch (TBranch &branchRef, T **newdata)
 

Detailed Description

A generic writer interface for ROOT TTree objects.

The writer class is configured with the file name, the tree name and a variable list of branch definitions passed to the constructor.

The class is currently fixed to the DPL ProcessingContext and InputRecord for reading the input, but the implementation has been kept open for other interfaces.

Usage:

RootTreeWriter("file_name",
               "tree_name",
               BranchDef<type>{ "key", "branchname" },
               ...// further input and branch config
              ) writer;
writer(processingContext);
Note
See also the MakeRootTreeWriterSpec helper class for easy generation of a processor spec using RootTreeWriter.
Using branch definition BranchDef:
The branch definition describes the mapping of inputs referenced by keys to outputs, i.e. the branches. Each branch definition holds the data type as template parameter, as well as input key definition and a branch name for the output. A variable list of branch definition parameters can be given to the constructor. See BranchDef structure for more details.
Multiple inputs and outputs:
One branch definition can handle multiple branches as output for the same data type and a single input or list of inputs. BranchDef needs to be configured with the number n of output branches, a callback to retrieve an index in the range [0, n-1], and a callback creating the branch name for base name and index. A single input can also be distributed to multiple branches if the callback calculates the index from another piece of information, e.g. from information in the header stack.
Writing binary data:
While the generic writer is primarily intended for ROOT serializable objects, a special case is the writing of binary data when const char* is used as type. Data is written as a std::vector<char>, this ensures separation on event basis as well as having binary data in parallel to ROOT objects in the same file, e.g. a binary data format from the reconstruction in parallel to MC labels.

Definition at line 120 of file RootTreeWriter.h.

Member Typedef Documentation

◆ BranchNameMapper

using o2::framework::RootTreeWriter::BranchNameMapper = std::function<std::string(std::string, size_t)>

Definition at line 129 of file RootTreeWriter.h.

◆ CustomClose

using o2::framework::RootTreeWriter::CustomClose = std::function<void(TFile* file, TTree* tree)>

Definition at line 133 of file RootTreeWriter.h.

◆ IndexExtractor

Definition at line 127 of file RootTreeWriter.h.

◆ key_type

Definition at line 124 of file RootTreeWriter.h.

Member Enumeration Documentation

◆ BranchVerbosity

Enumerator
Quiet 
Medium 
High 

Definition at line 145 of file RootTreeWriter.h.

Constructor & Destructor Documentation

◆ RootTreeWriter() [1/2]

o2::framework::RootTreeWriter::RootTreeWriter ( )
delete

default constructor forbidden

◆ ~RootTreeWriter()

o2::framework::RootTreeWriter::~RootTreeWriter ( )
inline

a destructor making sure that the Writer is closed (if didn't happen before)

Definition at line 281 of file RootTreeWriter.h.

◆ RootTreeWriter() [2/2]

template<typename... Args>
o2::framework::RootTreeWriter::RootTreeWriter ( const char filename,
const char treename,
Args &&...  args 
)
inline

constructor

Parameters
treenamename of file
treenamename of tree to write variable argument list of branch definitions

Definition at line 291 of file RootTreeWriter.h.

Member Function Documentation

◆ autoSave()

void o2::framework::RootTreeWriter::autoSave ( )
inline

autosave the tree

Definition at line 379 of file RootTreeWriter.h.

◆ close()

void o2::framework::RootTreeWriter::close ( )
inline

write the tree and close the file the writer is invalid after calling close

Definition at line 355 of file RootTreeWriter.h.

◆ getStoreSize()

size_t o2::framework::RootTreeWriter::getStoreSize ( ) const
inline

Definition at line 394 of file RootTreeWriter.h.

◆ init()

void o2::framework::RootTreeWriter::init ( const char filename,
const char treename,
const char treetitle = nullptr 
)
inline

Init the output file and tree.

Parameters
filenameoutput file
treenameoutput tree

After setting up the tree, the branches will be created according to the branch definition provided to the constructor.

Definition at line 310 of file RootTreeWriter.h.

◆ isClosed()

bool o2::framework::RootTreeWriter::isClosed ( ) const
inline

Definition at line 389 of file RootTreeWriter.h.

◆ operator()()

template<typename ContextType >
void o2::framework::RootTreeWriter::operator() ( ContextType &&  context)
inline

process functor It expects a context which is used by lambda capture in the snapshot function. Recursively process all inputs and fill branches individually from extracted objects.

Definition at line 343 of file RootTreeWriter.h.

◆ remapBranch()

template<typename T >
static TBranch * o2::framework::RootTreeWriter::remapBranch ( TBranch &  branchRef,
T **  newdata 
)
inlinestatic

A static helper function to change the type of a (yet unused) branch. Removes the old branch from the TTree system and creates a new branch. The function is useful for situations in which we want to transform data after the RootTreeWriter was created and change the type of the branch - such as in user callbacks. The function needs to be used with care. The user should ensure that "branch" is no longer used after a call to this function.

Definition at line 406 of file RootTreeWriter.h.

◆ setBranchName()

void o2::framework::RootTreeWriter::setBranchName ( size_t  index,
const char branchName 
)
inline

Set the branch name for a branch definition from the constructor argument list

Parameters
indexposition in the argument list
branchName(base)branch name

If the branch definition handles multiple output branches, the getName callback of the definition is used to build the names of the output branches

Definition at line 324 of file RootTreeWriter.h.


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