ATLAS Offline Software
Loading...
Searching...
No Matches
RootDatabase.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef ROOTSTORAGESVC_ROOTDBASE_H
5#define ROOTSTORAGESVC_ROOTDBASE_H 1
6
7// Framework include files
9#include "GaudiKernel/SmartIF.h"
12#include "StorageSvc/DbPrint.h"
13
14// ROOT include files
15#include "ROOT/RNTupleReader.hxx"
16
17#include <optional>
18#include <set>
19#include <map>
20#include <mutex>
21#include <unordered_map>
22
23// Forward declarations
24class StatusCode;
25
26class TFile;
27class TTree;
28class TBranch;
29class IFileMgr;
30namespace RootAuxDynIO {
32}
33namespace RootStorageSvc {
35}
37
45class RootDatabase : virtual public ITechDatabase, public APRMessaging
46 {
47 public:
49
50 private:
54 TFile* m_file;
56 long long int m_counters[3];
85
87 std::string m_indexMaster;
90
92 std::map<TBranch*, uint64_t> m_indexSizeMap;
93
95 std::set< std::string > m_indexRebuilt;
96
97 /* --- variables used with TREE_AUTO_FLUSH option for
98 managing combined TTree::Fill for branch containers
99 */
100 // TTree names with requested AUTO_FLUSH value
101 std::map< std::string, int > m_autoFlushTrees;
102
103 // mapping of opened (for update) branch containers, to their TTree
104 typedef std::set< RootTreeContainer* > ContainerSet_t;
105 std::map< TTree*, ContainerSet_t > m_containersInTree;
106
107 std::map< std::string, int > m_customSplitLevel;
108
109 SmartIF<IFileMgr> m_fileMgr;
110
111 // mutex to prevent concurrent read I/O from AuxDynReader
112 std::recursive_mutex m_iomutex;
113
114 std::map<std::string, std::unique_ptr<RootStorageSvc::RNTupleWriterHelper> > m_ntupleWriterMap;
115 std::map<std::string, std::unique_ptr<ROOT::RNTupleReader> > m_ntupleReaderMap;
116
117 using indexLookup_t = std::unordered_map<uint64_t, uint64_t>;
118 std::map<void*, indexLookup_t> m_ntupleIndexMap;
119
120 // RootDatabase currently holds an optional slot-specific active entry token for the main event data container
121 std::optional<SG::SlotSpecificObj<std::unique_ptr<ROOT::RNTupleReader::RActiveEntryToken>>> m_activeEntryTokenPtr;
122
123 public:
126
128 virtual ~RootDatabase();
129
131 TFile* file() { return m_file; }
133 std::string name() const;
134
136 TTree* getTree(const std::string& name);
137
139
143 static bool exists(const std::string& nam);
144
146 virtual long long int size() const;
147
149 void addByteCount(int which, long long int num_bytes);
150
152 long long int byteCount(int which) const;
153
154
156
158
159 uint64_t currentIndexMasterID() const { return m_indexMasterID; }
160
162 uint64_t indexSize(TBranch *branch) { return m_indexSizeMap[branch]; }
163
165 uint64_t indexSizeInc(TBranch *branch) { return ++m_indexSizeMap[branch]; }
166
167
169 bool wasIndexRebuilt(const std::string& treeName) { return m_indexRebuilt.count(treeName)!=0; }
171 void markIndexRebuilt(const std::string& treeName) { m_indexRebuilt.insert(treeName); };
172
174 std::recursive_mutex& ioMutex() { return m_iomutex; }
175
177
181 virtual StatusCode getOption(DbOption& opt);
182
184
188 virtual StatusCode setOption(const DbOption& opt);
189
191 virtual StatusCode setAutoFlush(const DbOption& opt);
192
194
200 virtual StatusCode open(const DbDatabase& dbH,
201 const std::string& nam,
202 Io::IoFlag mode);
203
205
209 virtual StatusCode close(Io::IoFlag mode);
210
212 virtual StatusCode transAct(Transaction::Action action);
213
215 ROOT::RNTupleReader* getNTupleReader(const std::string& ntuple_name);
216
217 // translate index value to row# for a given RNTuple
218 uint64_t indexLookup(ROOT::RNTupleReader *ps, uint64_t idx_val);
219
222 RootStorageSvc::RNTupleWriterHelper* getNTupleWriter(const std::string& ntuple_name, bool create=false);
223
224 protected:
225 // Execute any pending Fills before commit or flush
227
228 // Reduce branches' baskets' size to m_maxBufferSize for a give TTree
230
232
233 StatusCode close();
234
235 void printErrno(const char* nam, int err);
236};
237
238#endif
Maintain a set of objects, one per slot.
APRMessaging(const std::string &name)
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:39
Description: Definition an option to be supplied to database objects.
Definition DbOption.h:22
Description: Interface to the technology specific implementation of a Database object.
virtual StatusCode open(const DbDatabase &dbH, const std::string &nam, Io::IoFlag mode)
Open Database object.
virtual StatusCode close(Io::IoFlag mode)
Close database access.
std::optional< SG::SlotSpecificObj< std::unique_ptr< ROOT::RNTupleReader::RActiveEntryToken > > > m_activeEntryTokenPtr
std::recursive_mutex m_iomutex
int m_defCompression
Default compression level.
int m_minBufferEntries
Minimum buffer entries parameter for Branches.
int m_rntBufferedWriteEnabled
Flag to enable/disable buffered RNTuple writing.
std::map< TBranch *, uint64_t > m_indexSizeMap
Keep index sizes here, because Branches are emptied when fast merged by SharedWriter.
long long int byteCount(int which) const
Do some statistics: retrieve number of bytes read/written/other.
void addByteCount(int which, long long int num_bytes)
Do some statistics: add number of bytes read/written/other.
ROOT::RNTupleReader * getNTupleReader(const std::string &ntuple_name)
return RNTupleReader for a given ntuple_name
uint64_t indexSize(TBranch *branch)
get index size for indexed containers
void increaseBasketsSize(TTree *tree)
std::map< void *, indexLookup_t > m_ntupleIndexMap
void registerBranchContainer(RootTreeContainer *)
StatusCode fillBranchContainerTrees()
int m_rntWriterMetricsEnabled
Flag to enable/disable RNTupleWriter metrics.
std::map< std::string, std::unique_ptr< ROOT::RNTupleReader > > m_ntupleReaderMap
StatusCode close()
int m_defSplitLevel
Default split level.
std::set< std::string > m_indexRebuilt
marks if the index (for index Containers) was rebuilt for given TTree
DbDatabase m_dbH
Parent Database handle.
RootDatabase()
Standard Constructor.
static bool exists(const std::string &nam)
Check for file-existence.
uint64_t currentIndexMasterID() const
TFile * m_file
Reference to the actual implemented file.
uint64_t indexLookup(ROOT::RNTupleReader *ps, uint64_t idx_val)
std::string m_treeNameWithCache
Name of tree with cache.
virtual StatusCode transAct(Transaction::Action action)
Execute Database Transaction action.
std::map< std::string, std::unique_ptr< RootStorageSvc::RNTupleWriterHelper > > m_ntupleWriterMap
int m_branchOffsetTabLen
Offset table length for branches.
RootStorageSvc::RNTupleWriterHelper * getNTupleWriter(const std::string &ntuple_name, bool create=false)
Return RNTupleWriterHelper for a given ntuple_name create a new one if needed when create==true.
std::string m_indexMaster
name of the container with master index ('*' means use the biggest)
TTree * getTree(const std::string &name)
Get TTree by name from the TFile.
long long int m_counters[3]
Counter statistics.
TFile * file()
Access to the actual implemented file.
int m_defCompressionAlg
Default compression algorithm.
int m_rntReaderMetricsEnabled
Flag to enable/disable RNTupleReader metrics.
bool wasIndexRebuilt(const std::string &treeName)
Check if a given TTree had its index rebuilt.
virtual StatusCode setOption(const DbOption &opt)
Set options.
int m_maxBufferSize
Maximum buffer size parameter for Branches.
std::map< std::string, int > m_customSplitLevel
std::map< TTree *, ContainerSet_t > m_containersInTree
virtual StatusCode setAutoFlush(const DbOption &opt)
implementation of TREE_AUTO_FLUSH option - called from setOption()
virtual StatusCode getOption(DbOption &opt)
Access options.
void printErrno(const char *nam, int err)
void reduceBasketsSize(TTree *tree)
std::recursive_mutex & ioMutex()
provide access to the I/O mutex for AuxDynReader and Containers
int m_defBufferSize
Default buffer size parameter for Branches.
uint64_t indexSizeInc(TBranch *branch)
increase index size counter for indexed containers (by 1)
std::map< std::string, int > m_autoFlushTrees
std::set< RootTreeContainer * > ContainerSet_t
virtual ~RootDatabase()
Standard destructor.
void markIndexRebuilt(const std::string &treeName)
Mark that a given TTree had its index rebuilt.
StatusCode markBranchContainerForFill(RootTreeContainer *)
virtual long long int size() const
Access the size of the database: May be undefined for some technologies.
int m_defAutoSave
Default Autosave parameter for trees.
std::unordered_map< uint64_t, uint64_t > indexLookup_t
int m_defWritePolicy
Default policy mode for keyed objects.
std::string name() const
Get the DB name (here it is the TFile name).
uint64_t m_indexMasterID
nextID of the master index
std::string m_rntUnsplitFieldList
Comma separated list of unsplit field patterns for RNTupleWriter.
SmartIF< IFileMgr > m_fileMgr
Description: ROOT specific implementation of Database container.
::StatusCode StatusCode
StatusCode definition for legacy code.
Specialization of RootAuxDynStore for reading Aux Dynamic attributes from RNTuple.
TChain * tree