The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
RootDataConnection.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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:
212 class Tool {
213 protected:
221
224
225 public:
226 TTree* refs() const { return c->m_refs; }
227 StringVec& dbs() const { return c->m_dbs; }
228 StringVec& conts() const { return c->m_conts; }
229 StringVec& links() const { return c->m_links; }
230 ParamMap& params() const { return c->m_params; }
231 MsgStream& msgSvc() const { return c->msgSvc(); }
232 IIncidentSvc* incidentSvc() const { return c->incidentSvc(); }
233 const std::string& name() const { return c->m_name; }
234 Sections& sections() const { return c->m_sections; }
235 LinkSections& linkSections() const { return c->m_linkSects; }
236 MergeSections& mergeSections() const { return c->m_mergeSects; }
237
239 virtual ~Tool() = default;
241 virtual TBranch* getBranch( std::string_view section, std::string_view n ) = 0;
243 virtual RootRef poolRef( size_t /* which */ ) const { return RootRef(); }
244
246 virtual StatusCode readRefs() = 0;
248 virtual StatusCode saveRefs() = 0;
250 virtual int loadRefs( std::string_view section, std::string_view cnt, unsigned long entry,
251 RootObjectRefs& refs ) = 0;
252 };
253 std::unique_ptr<Tool> m_tool;
254 friend class Tool;
255
257 Tool* makeTool();
258
259 public:
261 RootDataConnection( const IInterface* own, std::string_view nam, std::shared_ptr<RootConnectionSetup> setup );
262
264 TFile* file() const { return m_file.get(); }
266 bool isConnected() const override { return bool( m_file ); }
268 bool isWritable() const { return m_file && m_file->IsWritable(); }
270 Tool* tool() const { return m_tool.get(); }
272 const MergeSections& mergeSections() const { return m_mergeSects; }
274 const StringVec& mergeFIDs() const { return m_mergeFIDs; }
275
277 void addClient( const IInterface* client );
279 size_t removeClient( const IInterface* client );
281 bool lookupClient( const IInterface* client ) const;
282
284 void badWriteError( std::string_view msg ) const;
285
287 std::pair<const RootRef*, const ContainerSection*> getMergeSection( std::string_view container, int entry ) const;
288
290 void enableStatistics( std::string_view section );
292 void saveStatistics( std::string_view statisticsFile );
293
295 int loadObj( std::string_view section, std::string_view cnt, unsigned long entry, DataObject*& pObj );
296
298 int loadRefs( std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs& refs );
299
301 std::pair<int, unsigned long> saveObj( std::string_view section, std::string_view cnt, TClass* cl, DataObject* pObj,
302 int minBufferSize, int maxBufferSize, int approxEventsPerBasket,
303 int split_lvl, bool fill_missing = false );
305 std::pair<int, unsigned long> save( std::string_view section, std::string_view cnt, TClass* cl, void* pObj,
306 int minBufferSize, int maxBufferSize, int approxEventsPerBasket, int split_lvl,
307 bool fill_missing = false );
308
310 StatusCode connectRead() override;
312 StatusCode connectWrite( IoType typ ) override;
314 StatusCode disconnect() override;
316 StatusCode read( void* const, size_t ) override { return StatusCode::FAILURE; }
318 StatusCode write( const void*, int ) override { return StatusCode::FAILURE; }
320 long long int seek( long long int, int ) override { return -1; }
321
323 TTree* getSection( std::string_view sect, bool create = false );
324
326 TBranch* getBranch( std::string_view section, std::string_view branch_name ) {
327 return m_tool->getBranch( section, branch_name );
328 }
329
330 TBranch* getBranch( std::string_view section, std::string_view branch_name, TClass* cl, void* ptr, int buff_siz,
331 int split_lvl );
332
334 void makeRef( const IRegistry& pA, RootRef& ref );
336 void makeRef( std::string_view name, long clid, int tech, std::string_view db, std::string_view cnt, int entry,
337 RootRef& ref );
338
340 int makeLink( std::string_view p );
341
343 const std::string& getDb( int which ) const;
344
346 const std::string& getCont( int which ) const {
347 return ( which >= 0 ) && ( size_t( which ) < m_conts.size() ) ? *( m_conts.begin() + which ) : empty();
348 }
349
351 const std::string& getLink( int which ) const {
352 return ( which >= 0 ) && ( size_t( which ) < m_links.size() ) ? *( m_links.begin() + which ) : empty();
353 }
354 };
355} // End namespace Gaudi
356
#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.
Helper class to facilitate an abstraction layer for reading POOL style files with this package.
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 RootRef poolRef(size_t) const
Internal overload to facilitate the access to POOL files.
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 to encapsulate POOL compatibiliy.
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