The Gaudi Framework  master (e98cfcff)
Loading...
Searching...
No Matches
RootDataConnection.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
13// Framework include files
14#include <Gaudi/Property.h>
15#include <GaudiKernel/ClassID.h>
17#include <GaudiKernel/SmartIF.h>
19#include <map>
20#include <set>
21#include <string>
22#include <string_view>
23#include <vector>
24
25#include <RootCnv/RootRefs.h>
26#include <TFile.h>
27#include <TTreePerfStats.h>
28
29// Forward declarations
30class TTree;
31class TClass;
32class TBranch;
33
34class MsgStream;
35class IRegistry;
36class DataObject;
37class IIncidentSvc;
38
39/*
40 * Gaudi namespace declaration
41 */
42namespace Gaudi {
43
55 private:
57 std::unique_ptr<MsgStream> m_msgSvc;
60
61 public:
63 typedef std::vector<std::string> StringVec;
64
70 std::string loadSection;
72 int cacheSize{ 0 };
74 int learnEntries{ 0 };
75
76 Gaudi::Property<bool> produceReproducibleFiles{ "ProduceReproducibleFiles", true,
77 "configure output files to be more reproducible" };
78
80 "ROOT630ForwardCompatibility", false,
81 "When opening a file for CREATE or RECREATE, enable ROOT 6.30 forward compatibility "
82 "(i.e. write a file that is readable with ROOT 6.24). Requires ROOT >= 6.30.06, see "
83 "<https://github.com/root-project/root/issues/14793>." };
84
87
89 static StatusCode setCompression( std::string_view compression );
91 static int compression();
92
94 void setMessageSvc( MsgStream* m );
96 MsgStream& msgSvc() const { return *m_msgSvc; }
97
99 void setIncidentSvc( IIncidentSvc* m );
101 IIncidentSvc* incidentSvc() const { return m_incidentSvc.get(); }
102 };
103
113 public:
115
128 ContainerSection() : start( -1 ), length( 0 ) {}
130 ContainerSection( int s, int l ) : start( s ), length( l ) {}
135 if ( this != &s ) {
136 start = s.start;
137 length = s.length;
138 }
139 return *this;
140 }
141
142 int start;
145 };
146
148 typedef std::vector<std::string> StringVec;
150 typedef std::vector<std::pair<std::string, std::string>> ParamMap;
152 typedef std::map<std::string, TTree*, std::less<>> Sections;
154 typedef std::vector<ContainerSection> ContainerSections;
156 typedef std::map<std::string, ContainerSections, std::less<>> MergeSections;
158 typedef std::vector<RootRef> LinkSections;
160 typedef std::set<const IInterface*> Clients;
161
163 MsgStream& msgSvc() const { return m_setup->msgSvc(); }
164 IIncidentSvc* incidentSvc() const { return m_setup->incidentSvc(); }
165
166 protected:
168 std::shared_ptr<RootConnectionSetup> m_setup;
170 std::unique_ptr<TTreePerfStats> m_statistics;
172 std::unique_ptr<TFile> m_file;
174 TTree* m_refs = nullptr;
194 std::string m_empty;
195
197 const std::string& empty() const;
198
201
202 public:
210 class Tool {
211 protected:
219
222
223 public:
224 TTree* refs() const { return c->m_refs; }
225 StringVec& dbs() const { return c->m_dbs; }
226 StringVec& conts() const { return c->m_conts; }
227 StringVec& links() const { return c->m_links; }
228 ParamMap& params() const { return c->m_params; }
229 MsgStream& msgSvc() const { return c->msgSvc(); }
230 IIncidentSvc* incidentSvc() const { return c->incidentSvc(); }
231 const std::string& name() const { return c->m_name; }
232 Sections& sections() const { return c->m_sections; }
233 LinkSections& linkSections() const { return c->m_linkSects; }
234 MergeSections& mergeSections() const { return c->m_mergeSects; }
235
237 virtual ~Tool() = default;
239 virtual TBranch* getBranch( std::string_view section, std::string_view n ) = 0;
240
242 virtual StatusCode readRefs() = 0;
244 virtual StatusCode saveRefs() = 0;
246 virtual int loadRefs( std::string_view section, std::string_view cnt, unsigned long entry,
247 RootObjectRefs& refs ) = 0;
248 };
249 std::unique_ptr<Tool> m_tool;
250 friend class Tool;
251
253 Tool* makeTool();
254
255 public:
257 RootDataConnection( const IInterface* own, std::string_view nam, std::shared_ptr<RootConnectionSetup> setup );
258
260 TFile* file() const { return m_file.get(); }
262 bool isConnected() const override { return bool( m_file ); }
264 bool isWritable() const { return m_file && m_file->IsWritable(); }
266 Tool* tool() const { return m_tool.get(); }
268 const MergeSections& mergeSections() const { return m_mergeSects; }
270 const StringVec& mergeFIDs() const { return m_mergeFIDs; }
271
273 void addClient( const IInterface* client );
275 size_t removeClient( const IInterface* client );
277 bool lookupClient( const IInterface* client ) const;
278
280 void badWriteError( std::string_view msg ) const;
281
283 std::pair<const RootRef*, const ContainerSection*> getMergeSection( std::string_view container, int entry ) const;
284
286 void enableStatistics( std::string_view section );
288 void saveStatistics( std::string_view statisticsFile );
289
291 int loadObj( std::string_view section, std::string_view cnt, unsigned long entry, DataObject*& pObj );
292
294 int loadRefs( std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs& refs );
295
297 std::pair<int, unsigned long> saveObj( std::string_view section, std::string_view cnt, TClass* cl, DataObject* pObj,
298 int minBufferSize, int maxBufferSize, int approxEventsPerBasket,
299 int split_lvl, bool fill_missing = false );
301 std::pair<int, unsigned long> save( std::string_view section, std::string_view cnt, TClass* cl, void* pObj,
302 int minBufferSize, int maxBufferSize, int approxEventsPerBasket, int split_lvl,
303 bool fill_missing = false );
304
306 StatusCode connectRead() override;
308 StatusCode connectWrite( IoType typ ) override;
310 StatusCode disconnect() override;
312 StatusCode read( void* const, size_t ) override { return StatusCode::FAILURE; }
314 StatusCode write( const void*, int ) override { return StatusCode::FAILURE; }
316 long long int seek( long long int, int ) override { return -1; }
317
319 TTree* getSection( std::string_view sect, bool create = false );
320
322 TBranch* getBranch( std::string_view section, std::string_view branch_name ) {
323 return m_tool->getBranch( section, branch_name );
324 }
325
326 TBranch* getBranch( std::string_view section, std::string_view branch_name, TClass* cl, void* ptr, int buff_siz,
327 int split_lvl );
328
330 void makeRef( const IRegistry& pA, RootRef& ref );
332 void makeRef( std::string_view name, long clid, int tech, std::string_view db, std::string_view cnt, int entry,
333 RootRef& ref );
334
336 int makeLink( std::string_view p );
337
339 const std::string& getDb( int which ) const;
340
342 const std::string& getCont( int which ) const {
343 return ( which >= 0 ) && ( size_t( which ) < m_conts.size() ) ? *( m_conts.begin() + which ) : empty();
344 }
345
347 const std::string& getLink( int which ) const {
348 return ( which >= 0 ) && ( size_t( which ) < m_links.size() ) ? *( m_links.begin() + which ) : empty();
349 }
350 };
351} // End namespace Gaudi
352
#define GAUDI_API
Definition Kernel.h:49
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition StatusCode.h:286
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
ABC describing basic data connection.
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
StringVec cacheBranches
Vector of strings with branches to be cached for input files.
RootConnectionSetup()=default
Standard constructor.
SmartIF< IIncidentSvc > m_incidentSvc
Reference to incident service.
void setMessageSvc(MsgStream *m)
Set message service reference.
static int compression()
Access to global compression level.
StringVec vetoBranches
Vector of strings with branches to NOT be cached for input files.
MsgStream & msgSvc() const
Retrieve message service.
Gaudi::Property< bool > produceReproducibleFiles
std::string loadSection
RootCnvSvc Property: Root data cache size.
int learnEntries
RootCnvSvc Property: ROOT cache learn entries.
int cacheSize
RootCnvSvc Property: Root data cache size.
static StatusCode setCompression(std::string_view compression)
Set the global compression level.
std::unique_ptr< MsgStream > m_msgSvc
Reference to message service.
Gaudi::Property< bool > root630ForwardCompatibility
std::vector< std::string > StringVec
Type definition for string maps.
IIncidentSvc * incidentSvc() const
Retrieve incident service.
abstraction layer probably not needed anymore as the only implementation if RootTool
RootDataConnection::Sections Sections
RootDataConnection::ContainerSection ContainerSection
RootDataConnection * c
Pointer to containing data connection object.
RootDataConnection::ParamMap ParamMap
LinkSections & linkSections() const
virtual StatusCode saveRefs()=0
Save references section when closing data file.
virtual int loadRefs(std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs &refs)=0
Load references object.
RootDataConnection::ContainerSections ContainerSections
virtual TBranch * getBranch(std::string_view section, std::string_view n)=0
Access data branch by name: Get existing branch in read only mode.
const std::string & name() const
virtual ~Tool()=default
Default destructor.
MergeSections & mergeSections() const
RootDataConnection::LinkSections LinkSections
RootDataConnection::MergeSections MergeSections
RootDataConnection::StringVec StringVec
virtual StatusCode readRefs()=0
Read references section when opening data file.
Sections m_sections
Tree sections in TFile.
LinkSections m_linkSects
Database link sections.
std::map< std::string, TTree *, std::less<> > Sections
Definition of tree sections.
StatusCode read(void *const, size_t) override
Read root byte buffer from input stream.
Tool * makeTool()
Create file access tool.
long long int seek(long long int, int) override
Seek on the file described by ioDesc. Arguments as in seek()
StringVec m_links
Map containing internal links names.
const std::string & empty() const
Empty string reference.
const std::string & getLink(int which) const
Access link name from saved index.
std::string m_empty
Buffer for empty string reference.
MsgStream & msgSvc() const
Allow access to printer service.
std::vector< std::string > StringVec
Type definition for string maps.
std::vector< ContainerSection > ContainerSections
Definition of container sections to handle merged files.
RootDataConnection(const IInterface *own, std::string_view nam, std::shared_ptr< RootConnectionSetup > setup)
Standard constructor.
Tool * tool() const
Access tool.
StatusCode saveRefs()
Internal helper to save/update reference tables.
std::vector< RootRef > LinkSections
Link sections definition.
std::unique_ptr< TTreePerfStats > m_statistics
I/O read statistics from TTree.
std::unique_ptr< Tool > m_tool
Clients m_clients
Client list.
bool isConnected() const override
Check if connected to data source.
ParamMap m_params
Parameter map for file parameters.
std::unique_ptr< TFile > m_file
Reference to ROOT file.
TFile * file() const
Direct access to TFile structure.
MergeSections m_mergeSects
Database section map for merged files.
bool isWritable() const
Is the file writable?
std::map< std::string, ContainerSections, std::less<> > MergeSections
Definition of database section to handle merged files.
std::set< const IInterface * > Clients
Client set.
StringVec m_conts
Map containing external container names.
TBranch * getBranch(std::string_view section, std::string_view branch_name)
Access data branch by name: Get existing branch in read only mode.
StatusCode write(const void *, int) override
Write root byte buffer to output stream.
StringVec m_mergeFIDs
Map containing merge FIDs.
StringVec m_dbs
Map containing external database file names (fids)
std::shared_ptr< RootConnectionSetup > m_setup
Reference to the setup structure.
const MergeSections & mergeSections() const
Access merged data section inventory.
const StringVec & mergeFIDs() const
Access merged FIDs.
IIncidentSvc * incidentSvc() const
std::vector< std::pair< std::string, std::string > > ParamMap
Type definition for the parameter map.
TTree * m_refs
Pointer to the reference tree.
const std::string & getCont(int which) const
Access container name from saved index.
The interface implemented by the IncidentSvc service.
Definition of the basic interface.
Definition IInterface.h:225
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
unsigned long code_t
type of StatusCode value
Definition StatusCode.h:66
constexpr static const auto FAILURE
Definition StatusCode.h:100
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
Internal helper class, which described a TBranch section in a ROOT file.
ContainerSection(const ContainerSection &s)
Copy constructor.
ContainerSection(int s, int l)
Initializing constructor.
ContainerSection & operator=(const ContainerSection &s)
Assignment operator to copy objects.
int start
The start entry of the section.
Persistent reference object containing all leafs and links corresponding to a Gaudi DataObject.
Definition extractEvt.C:81
Persistent reference object.
Definition extractEvt.C:44