ATLAS Offline Software
RootDatabase.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //====================================================================
6 //
7 // Package : RootStorageSvc (The POOL project)
8 //
9 // Author : M.Frank
10 //====================================================================
11 #ifndef POOL_ROOTSTORAGESVC_ROOTDBASE_H
12 #define POOL_ROOTSTORAGESVC_ROOTDBASE_H 1
13 
14 // Framework include files
15 #include "GaudiKernel/SmartIF.h"
16 #include "StorageSvc/IDbDatabase.h"
17 #include "StorageSvc/DbDatabase.h"
18 #include "POOLCore/DbPrint.h"
19 
20 #include <set>
21 #include <map>
22 #include <mutex>
23 #include <unordered_map>
24 
25 // Forward declarations
26 namespace ROOT { class RNTupleReader; }
27 
28 class TFile;
29 class TTree;
30 class TBranch;
31 class IFileMgr;
32 namespace RootAuxDynIO {
33  class IRootAuxDynReader;
34 }
35 namespace RootStorageSvc {
36  class RNTupleWriterHelper;
37 }
38 
39 /*
40  * POOL namespace declaration
41  */
42 namespace pool {
43  class RootTreeContainer;
44 
54  class RootDatabase : public IDbDatabase, public APRMessaging
55  {
56  public:
57  enum { READ_COUNTER = 0, WRITE_COUNTER = 1, OTHER_COUNTER = 2 };
58 
59  private:
63  TFile* m_file;
65  std::string m_version;
67  long long int m_counters[3];
87  std::string m_treeNameWithCache;
96 
98  std::string m_indexMaster;
101 
103  std::map<TBranch*, uint64_t> m_indexSizeMap;
104 
106  std::set< std::string > m_indexRebuilt;
107 
108  /* --- variables used with TREE_AUTO_FLUSH option for
109  managing combined TTree::Fill for branch containers
110  */
111  // TTree names with requested AUTO_FLUSH value
112  std::map< std::string, int > m_autoFlushTrees;
113 
114  // mapping of opened (for update) branch containers, to their TTree
115  typedef std::set< RootTreeContainer* > ContainerSet_t;
116  std::map< TTree*, ContainerSet_t > m_containersInTree;
117 
118  std::map< std::string, int > m_customSplitLevel;
119 
120  SmartIF<IFileMgr> m_fileMgr;
121 
122  // mutex to prevent concurrent read I/O from AuxDynReader
123  std::recursive_mutex m_iomutex;
124 
125  std::map<std::string, std::unique_ptr<RootStorageSvc::RNTupleWriterHelper> > m_ntupleWriterMap;
126  std::map<std::string, std::unique_ptr<ROOT::RNTupleReader> > m_ntupleReaderMap;
127 
128  using indexLookup_t = std::unordered_map<uint64_t, uint64_t>;
129  std::map<void*, indexLookup_t> m_ntupleIndexMap;
130 
131  public:
134 
136  virtual ~RootDatabase();
137 
139  TFile* file() { return m_file; }
141  std::string name() const;
142 
144  TTree* getTree(const std::string& name);
145 
147  const std::string& fmtVersion() const { return m_version; }
148 
150 
154  static bool exists(const std::string& nam);
155 
157  virtual long long int size() const;
158 
160  void addByteCount(int which, long long int num_bytes);
161 
163  long long int byteCount(int which) const;
164 
165 
167 
169 
171 
174 
177 
178 
180  bool wasIndexRebuilt(const std::string& treeName) { return m_indexRebuilt.count(treeName)!=0; }
182  void markIndexRebuilt(const std::string& treeName) { m_indexRebuilt.insert(treeName); };
183 
185  std::recursive_mutex& ioMutex() { return m_iomutex; }
186 
188 
193 
195 
199  virtual DbStatus setOption(const DbOption& opt);
200 
203 
205 
212  virtual DbStatus open(const DbDomain& domH,
213  const std::string& nam,
215 
217 
222 
224 
230 
232 
237 
240 
242  ROOT::RNTupleReader* getNTupleReader(const std::string& ntuple_name);
243 
244  // translate index value to row# for a given RNTuple
245  uint64_t indexLookup(ROOT::RNTupleReader *ps, uint64_t idx_val);
246 
249  RootStorageSvc::RNTupleWriterHelper* getNTupleWriter(const std::string& ntuple_name, bool create=false);
250 
251  protected:
252  // Execute any pending Fills before commit or flush
254 
255  // Reduce branches' baskets' size to m_maxBufferSize for a give TTree
256  void reduceBasketsSize(TTree* tree);
257 
258  void increaseBasketsSize(TTree* tree);
259 
261 
262  void printErrno(const char* nam, int err);
263  };
264 
265 } // End namespace pool
266 #endif /* POOL_ROOTSTORAGESVC_ROOTDBASE_H */
pool::RootDatabase::RootDatabase
RootDatabase()
Standard Constructor.
pool::RootDatabase::increaseBasketsSize
void increaseBasketsSize(TTree *tree)
pool::DbAccessMode
int DbAccessMode
Definition: Database/APR/StorageSvc/StorageSvc/pool.h:34
pool::RootDatabase::setOption
virtual DbStatus setOption(const DbOption &opt)
Set options.
pool::RootDatabase::transAct
virtual DbStatus transAct(Transaction::Action action)
Execute Database Transaction action.
pool::RootDatabase::close
virtual DbStatus close(DbAccessMode mode)
Close database access.
pool::RootDatabase::m_customSplitLevel
std::map< std::string, int > m_customSplitLevel
Definition: RootDatabase.h:118
pool::RootDatabase::m_ntupleReaderMap
std::map< std::string, std::unique_ptr< ROOT::RNTupleReader > > m_ntupleReaderMap
Definition: RootDatabase.h:126
pool::DbStatus
Definition: DbStatus.h:64
pool::RootDatabase::currentIndexMasterID
uint64_t currentIndexMasterID() const
Definition: RootDatabase.h:170
pool::RootDatabase::m_branchOffsetTabLen
int m_branchOffsetTabLen
Offset table length for branches.
Definition: RootDatabase.h:85
make_coralServer_rep.opt
opt
Definition: make_coralServer_rep.py:19
IDbDatabase.h
pool::RootDatabase::m_defTreeCacheLearnEvents
int m_defTreeCacheLearnEvents
Default tree cache learn events.
Definition: RootDatabase.h:89
pool::RootDatabase::m_maxBufferSize
int m_maxBufferSize
Maximum buffer size parameter for Branches.
Definition: RootDatabase.h:79
pool::RootDatabase::indexLookup
uint64_t indexLookup(ROOT::RNTupleReader *ps, uint64_t idx_val)
pool::RootDatabase::m_indexRebuilt
std::set< std::string > m_indexRebuilt
marks if the index (for index Containers) was rebuilt for given TTree
Definition: RootDatabase.h:106
pool::RootDatabase::size
virtual long long int size() const
Access the size of the database: May be undefined for some technologies.
pool::RootDatabase::m_defSplitLevel
int m_defSplitLevel
Default split level.
Definition: RootDatabase.h:73
pool::RootDatabase::m_indexMasterID
uint64_t m_indexMasterID
nextID of the master index
Definition: RootDatabase.h:100
pool::RootDatabase::m_indexSizeMap
std::map< TBranch *, uint64_t > m_indexSizeMap
Keep index sizes here, because Branches are emptied when fast merged by SharedWriter.
Definition: RootDatabase.h:103
pool::RootDatabase::m_ntupleWriterMap
std::map< std::string, std::unique_ptr< RootStorageSvc::RNTupleWriterHelper > > m_ntupleWriterMap
Definition: RootDatabase.h:125
pool::RootDatabase::fillBranchContainerTrees
DbStatus fillBranchContainerTrees()
pool::RootDatabase::m_fileMgr
SmartIF< IFileMgr > m_fileMgr
Definition: RootDatabase.h:120
tree
TChain * tree
Definition: tile_monitor.h:30
pool
pool namespace
Definition: libname.h:15
pool::RootDatabase::m_containersInTree
std::map< TTree *, ContainerSet_t > m_containersInTree
Definition: RootDatabase.h:116
pool::RootDatabase::m_dbH
DbDatabase m_dbH
Parent Database handle.
Definition: RootDatabase.h:61
pool::RootDatabase::m_defCompressionAlg
int m_defCompressionAlg
Default compression algorithm.
Definition: RootDatabase.h:71
pool::RootDatabase::name
std::string name() const
Get the DB name (here it is the TFile name)
pool::RootDatabase::m_rntReaderMetricsEnabled
int m_rntReaderMetricsEnabled
Flag to enable/disable RNTupleReader metrics.
Definition: RootDatabase.h:93
pool::APRMessaging
AthMessaging wrapper to set the output level in APR components.
Definition: DbPrint.h:35
pool::RootDatabase::m_indexMaster
std::string m_indexMaster
name of the container with master index ('*' means use the biggest)
Definition: RootDatabase.h:98
pool::RootDatabase::close
DbStatus close()
pool::RootDatabase::WRITE_COUNTER
@ WRITE_COUNTER
Definition: RootDatabase.h:57
pool::RootDatabase::m_defWritePolicy
int m_defWritePolicy
Default policy mode for keyed objects.
Definition: RootDatabase.h:83
DbDatabase.h
pool::RootDatabase::m_ntupleIndexMap
std::map< void *, indexLookup_t > m_ntupleIndexMap
Definition: RootDatabase.h:129
pool::RootDatabase::wasIndexRebuilt
bool wasIndexRebuilt(const std::string &treeName)
Check if a given TTree had its index rebuilt.
Definition: RootDatabase.h:180
pool::RootDatabase::printErrno
void printErrno(const char *nam, int err)
pool::RootDatabase::m_counters
long long int m_counters[3]
Counter statistics.
Definition: RootDatabase.h:67
pool::RootDatabase::m_rntWriterMetricsEnabled
int m_rntWriterMetricsEnabled
Flag to enable/disable RNTupleWriter metrics.
Definition: RootDatabase.h:95
DbPrint.h
pool::IDbDatabase
Definition: IDbDatabase.h:41
pool::RootDatabase::ioMutex
std::recursive_mutex & ioMutex()
provide access to the I/O mutex for AuxDynReader and Containers
Definition: RootDatabase.h:185
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:183
python.SystemOfUnits.ps
float ps
Definition: SystemOfUnits.py:150
pool::DbOption
Definition: DbOption.h:36
pool::RootDatabase::registerBranchContainer
void registerBranchContainer(RootTreeContainer *)
pool::RootDatabase::getNTupleWriter
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.
pool::RootDatabase::byteCount
long long int byteCount(int which) const
Do some statistics: retrieve number of bytes read/written/other.
pool::RootDatabase::m_defBufferSize
int m_defBufferSize
Default buffer size parameter for Branches.
Definition: RootDatabase.h:77
pool::RootDatabase::m_iomutex
std::recursive_mutex m_iomutex
Definition: RootDatabase.h:123
pool::RootDatabase::file
TFile * file()
Access to the actual implemented file.
Definition: RootDatabase.h:139
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:19
pool::RootDatabase::READ_COUNTER
@ READ_COUNTER
Definition: RootDatabase.h:57
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
Preparation.mode
mode
Definition: Preparation.py:107
pool::RootDatabase::reopen
virtual DbStatus reopen(DbAccessMode mode)
Re-open database with changing access permissions.
python.Utils.unixtools.which
def which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition: unixtools.py:39
pool::RootTreeContainer
Definition: RootTreeContainer.h:60
pool::RootDatabase::addByteCount
void addByteCount(int which, long long int num_bytes)
Do some statistics: add number of bytes read/written/other.
pool::RootDatabase::indexSize
uint64_t indexSize(TBranch *branch)
get index size for indexed containers
Definition: RootDatabase.h:173
pool::RootDatabase::~RootDatabase
virtual ~RootDatabase()
Standard destructor.
pool::RootDatabase::indexLookup_t
std::unordered_map< uint64_t, uint64_t > indexLookup_t
Definition: RootDatabase.h:128
pool::Transaction::Action
Action
Definition: Transaction.h:34
pool::RootDatabase::OTHER_COUNTER
@ OTHER_COUNTER
Definition: RootDatabase.h:57
pool::RootDatabase::m_minBufferEntries
int m_minBufferEntries
Minimum buffer entries parameter for Branches.
Definition: RootDatabase.h:81
pool::RootDatabase::markBranchContainerForFill
DbStatus markBranchContainerForFill(RootTreeContainer *)
RootAuxDynIO
Specialization of RootAuxDynStore for reading Aux Dynamic attributes from RNTuple.
Definition: RNTupleContainer.h:28
pool::RootDatabase::reduceBasketsSize
void reduceBasketsSize(TTree *tree)
pool::RootDatabase::open
virtual DbStatus open(const DbDomain &domH, const std::string &nam, DbAccessMode mode)
Open Database object.
pool::DbDomain
Definition: DbDomain.h:47
pool::RootDatabase::getTree
TTree * getTree(const std::string &name)
Get TTree by name from the TFile.
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
pool::RootDatabase::m_rntBufferedWriteEnabled
int m_rntBufferedWriteEnabled
Flag to enable/disable buffered RNTuple writing.
Definition: RootDatabase.h:91
pool::RootDatabase::m_file
TFile * m_file
Reference to the actual implemented file.
Definition: RootDatabase.h:63
pool::RootDatabase::onOpen
virtual DbStatus onOpen(DbDatabase &dbH, DbAccessMode mode)
Callback after successful open of a database object.
RootStorageSvc::RNTupleWriterHelper
Definition: RNTupleWriterHelper.h:20
pool::RootDatabase::fmtVersion
const std::string & fmtVersion() const
Access to the version string.
Definition: RootDatabase.h:147
pool::RootDatabase
Definition: RootDatabase.h:55
pool::RootDatabase::m_defAutoSave
int m_defAutoSave
Default Autosave parameter for trees.
Definition: RootDatabase.h:75
python.CaloScaleNoiseConfig.action
action
Definition: CaloScaleNoiseConfig.py:77
pool::RootDatabase::m_autoFlushTrees
std::map< std::string, int > m_autoFlushTrees
Definition: RootDatabase.h:112
pool::RootDatabase::getOption
virtual DbStatus getOption(DbOption &opt)
Access options.
pool::RootDatabase::m_defCompression
int m_defCompression
Default compression level.
Definition: RootDatabase.h:69
pool::RootDatabase::markIndexRebuilt
void markIndexRebuilt(const std::string &treeName)
Mark that a given TTree had its index rebuilt.
Definition: RootDatabase.h:182
pool::RootDatabase::m_version
std::string m_version
Persistency format version.
Definition: RootDatabase.h:65
pool::RootDatabase::getNTupleReader
ROOT::RNTupleReader * getNTupleReader(const std::string &ntuple_name)
return RNTupleReader for a given ntuple_name
pool::RootDatabase::exists
static bool exists(const std::string &nam)
Check for file-existence.
pool::DbDatabase
Definition: DbDatabase.h:53
pool::RootDatabase::setAutoFlush
virtual DbStatus setAutoFlush(const DbOption &opt)
implementation of TREE_AUTO_FLUSH option - called from setOption()
pool::RootDatabase::indexSizeInc
uint64_t indexSizeInc(TBranch *branch)
increase index size counter for indexed containers (by 1)
Definition: RootDatabase.h:176
pool::RootDatabase::ContainerSet_t
std::set< RootTreeContainer * > ContainerSet_t
Definition: RootDatabase.h:115
RootStorageSvc
Definition: RNTupleContainer.h:29
ROOT
Selection rules: declare transient members.
Definition: DataVector.h:581
pool::RootDatabase::m_treeNameWithCache
std::string m_treeNameWithCache
Name of tree with cache.
Definition: RootDatabase.h:87