Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RootObjTree.hxx
Go to the documentation of this file.
1// \file RootObjTree.hxx
2///
3/// Utility functions used by command line tools to parse "path-like" strings like: "foo.root:dir/obj*" into a
4/// tree structure usable to iterate the matched objects.
5///
6/// For example usage, see rootls.cxx
7///
8/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
9/// \date 2025-10-14
10
11#ifndef ROOT_CMDLINE_OBJTREE
12#define ROOT_CMDLINE_OBJTREE
13
14#include <cstdint>
15#include <iostream>
16#include <memory>
17#include <string>
18#include <vector>
19
20#include <TKey.h>
21
22#include <ROOT/RError.hxx>
23
24class TDirectory;
25class TFile;
26
27namespace ROOT::CmdLine {
28
29using NodeIdx_t = std::uint32_t;
30
32 std::string fName;
33 std::int16_t fCycle = 0;
34 std::string fClassName;
35 TKey *fKey = nullptr; // This is non-null for all nodes except the root node (which is the file itself)
36
37 TDirectory *fDir = nullptr; // This is null for all non-directory nodes
38 // NOTE: by construction of the tree, all children of the same node are contiguous.
40 std::uint32_t fNChildren = 0;
41 std::uint32_t fNesting = 0;
43};
44
46{
47 RootObjNode node = {};
48 node.fName = key.GetName();
49 node.fCycle = key.GetCycle();
50 node.fClassName = key.GetClassName();
51 node.fKey = &key;
52 return node;
53}
54
55/// A representation of all objects involved in a rootls-style ROOT file listing. This listing is used in command
56/// line tools like rootls, rootcp etc. and it comes from a shell glob-style syntax like:
57///
58/// file.root:dir/*
59///
60/// In this case, a RootObjTree that represents the above query will contain the TFile relative to `file.root`,
61/// all nodes "visited" by the glob expression in `fNodes` and links to them in `fDirList` and `fLeafList`. This is
62/// basically a filtered view of a ROOT file.
63///
64/// Example: assume you have a ROOT file with the following structure:
65///
66/// file.root
67/// `--- a/
68/// | `--- b
69/// | `--- c
70/// `--- d
71///
72/// Then the RootObjTree representing the query `file.root:*` will contain:
73///
74/// fNodes: [a, b, c, d]
75/// fLeafList: [1, 2, 3]
76/// fDirList: [0]
77///
79 // 0th node is the root node
80 std::vector<RootObjNode> fNodes;
81 // All nodes in fNodes that are dirs
82 std::vector<NodeIdx_t> fDirList;
83 // All nodes in fNodes that are leaves (non-TDirectories)
84 std::vector<NodeIdx_t> fLeafList;
85 // The file must be kept alive in order to access the nodes' keys
86 std::unique_ptr<TFile> fFile;
87};
88
89/// Prints out the structure of the object tree. Helpful for debugging.
90void PrintObjTree(const RootObjTree &tree, std::ostream &out = std::cout);
91
96/// Given a node, returns its full path. If `opt == kIncludeFilename`, the path is prepended by "filename.root:"
97std::string NodeFullPath(const RootObjTree &tree, NodeIdx_t nodeIdx, ENodeFullPathOpt opt);
98
99struct RootSource {
100 std::string fFileName;
102 std::vector<std::string> fErrors;
103};
104
106 /// Recurse into subdirectories when matching objects
107 kRecursive = 1 << 0,
110};
111
112/// Given a file and a "path pattern", returns a RootSource containing the tree of matched objects.
113/// If `pattern` is non-empty, not a single '*' and it doesn't match any object in `fileName`,
114/// an error will be appended to RootSource::fErrors unless `kIgnoreFailedMatches` is part of `flags`.
115///
116/// \param fileName The name of the ROOT file to look into
117/// \param pattern A glob-like pattern (basically a `ls` pattern). May be empty to match anything.
118/// \param flags A bitmask of EGetMatchingPathsFlags
119RootSource GetMatchingPathsInFile(std::string_view fileName, std::string_view pattern, std::uint32_t flags);
120
121/// Given a string like "root://file.root:a/b/c", splits it into { "root://file.root", "a/b/c" }.
122/// \return An error if the file prefix is unknown (e.g. "foo://file.root"), otherwise the result described above.
124
125/// Given a string like "file.root:dir/obj", converts it to a RootSource.
126/// As a result of a successful call, a ROOT file is opened and may be accessed from RootSource::fObjectTree.fFile.
127/// Note that the file will _not_ be registered to the global list and doesn't have to be explicitly closed, being
128/// stored in a unique_ptr.
129///
130/// The string may start with one of the known file protocols: "http", "https", "root", "gs", "s3"
131/// (e.g. "https://file.root").
132///
133/// If the source fails to get created, its fErrors list will be non-empty.
134///
135/// \param flags A bitmask of EGetMatchingPathsFlags
136/// \return The converted source.
137RootSource ParseRootSource(std::string_view sourceRaw, std::uint32_t flags);
138
139/// Given a list of strings like "file.root:dir/obj", converts each string to a RootSource.
140/// The string may start with one of the known file protocols: "http", "https", "root", "gs", "s3"
141/// (e.g. "https://file.root").
142///
143/// If one or more sources fail to get created, each sources's fErrors list will be non-empty.
144///
145/// \param flags A bitmask of EGetMatchingPathsFlags
146/// \return The list of converted sources.
147std::vector<ROOT::CmdLine::RootSource>
148ParseRootSources(const std::vector<std::string> &sourcesRaw, std::uint32_t flags);
149
150} // namespace ROOT::CmdLine
151
152#endif
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
Definition RError.hxx:197
Describe directory structure in memory.
Definition TDirectory.h:45
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:77
Short_t GetCycle() const
Return cycle number associated to this key.
Definition TKey.cxx:611
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
subroutine node(ivo, nuserm, iposp)
Definition g2root.f:833
RootSource ParseRootSource(std::string_view sourceRaw, std::uint32_t flags)
Given a string like "file.root:dir/obj", converts it to a RootSource.
ROOT::RResult< std::pair< std::string_view, std::string_view > > SplitIntoFileNameAndPattern(std::string_view sourceRaw)
Given a string like "root://file.root:a/b/c", splits it into { "root://file.root",...
std::string NodeFullPath(const RootObjTree &tree, NodeIdx_t nodeIdx, ENodeFullPathOpt opt)
Given a node, returns its full path. If opt == kIncludeFilename, the path is prepended by "filename....
void PrintObjTree(const RootObjTree &tree, std::ostream &out=std::cout)
Prints out the structure of the object tree. Helpful for debugging.
@ kRecursive
Recurse into subdirectories when matching objects.
std::uint32_t NodeIdx_t
std::vector< ROOT::CmdLine::RootSource > ParseRootSources(const std::vector< std::string > &sourcesRaw, std::uint32_t flags)
Given a list of strings like "file.root:dir/obj", converts each string to a RootSource.
RootObjNode NodeFromKey(TKey &key)
RootSource GetMatchingPathsInFile(std::string_view fileName, std::string_view pattern, std::uint32_t flags)
Given a file and a "path pattern", returns a RootSource containing the tree of matched objects.
A representation of all objects involved in a rootls-style ROOT file listing.
std::unique_ptr< TFile > fFile
std::vector< NodeIdx_t > fDirList
std::vector< NodeIdx_t > fLeafList
std::vector< RootObjNode > fNodes
std::vector< std::string > fErrors
TTree * tree