The Gaudi Framework  master (37c0b60a)
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
30 class TTree;
31 class TClass;
32 class TBranch;
33 
34 class MsgStream;
35 class IRegistry;
36 class DataObject;
37 class IIncidentSvc;
38 
39 /*
40  * Gaudi namespace declaration
41  */
42 namespace Gaudi {
43 
55  private:
59  SmartIF<IIncidentSvc> m_incidentSvc = nullptr;
60 
61  public:
64 
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 
79  Gaudi::Property<bool> root630ForwardCompatibility{
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 
86  RootConnectionSetup() = default;
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:
114  enum class Status : StatusCode::code_t { ROOT_READ_ERROR = 0x2, ROOT_OPEN_ERROR = 0x4 };
115 
128  ContainerSection() : start( -1 ), length( 0 ) {}
130  ContainerSection( int s, int l ) : start( s ), length( l ) {}
132  ContainerSection( const ContainerSection& s ) : start( s.start ), length( s.length ) {}
135  if ( this != &s ) {
136  start = s.start;
137  length = s.length;
138  }
139  return *this;
140  }
142  int start;
144  int length;
145  };
146 
161 
163  MsgStream& msgSvc() const { return m_setup->msgSvc(); }
164  IIncidentSvc* incidentSvc() const { return m_setup->incidentSvc(); }
165 
166  protected:
174  TTree* m_refs = nullptr;
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  };
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  }
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 
Gaudi::RootDataConnection::ContainerSection::ContainerSection
ContainerSection()
Default constructor.
Definition: RootDataConnection.h:128
Gaudi::RootDataConnection::mergeFIDs
const StringVec & mergeFIDs() const
Access merged FIDs.
Definition: RootDataConnection.h:274
Gaudi::RootDataConnection::isConnected
bool isConnected() const override
Check if connected to data source.
Definition: RootDataConnection.h:266
Gaudi::RootDataConnection::ContainerSection::length
int length
The length of the section.
Definition: RootDataConnection.h:144
std::string
STL class.
std::shared_ptr
STL class.
Gaudi::RootDataConnection::m_clients
Clients m_clients
Client list.
Definition: RootDataConnection.h:192
Gaudi::RootDataConnection::Tool::readRefs
virtual StatusCode readRefs()=0
Read references section when opening data file.
Gaudi::RootDataConnection::Tool::dbs
StringVec & dbs() const
Definition: RootDataConnection.h:227
Gaudi::RootConnectionSetup::cacheBranches
StringVec cacheBranches
Vector of strings with branches to be cached for input files.
Definition: RootDataConnection.h:66
Gaudi::RootDataConnection::Sections
std::map< std::string, TTree *, std::less<> > Sections
Definition of tree sections.
Definition: RootDataConnection.h:152
Gaudi::RootDataConnection::Tool::MergeSections
RootDataConnection::MergeSections MergeSections
Definition: RootDataConnection.h:217
Gaudi::IDataConnection
Definition: IIODataManager.h:34
std::pair
gaudirun.s
string s
Definition: gaudirun.py:346
std::vector< std::string >
std::vector::size
T size(T... args)
ClassID.h
Gaudi::RootDataConnection::getLink
const std::string & getLink(int which) const
Access link name from saved index.
Definition: RootDataConnection.h:351
Gaudi::RootDataConnection::read
StatusCode read(void *const, size_t) override
Read root byte buffer from input stream.
Definition: RootDataConnection.h:316
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::RootDataConnection::Tool::conts
StringVec & conts() const
Definition: RootDataConnection.h:228
Gaudi::RootDataConnection::m_file
std::unique_ptr< TFile > m_file
Reference to ROOT file.
Definition: RootDataConnection.h:172
Gaudi::RootDataConnection::isWritable
bool isWritable() const
Is the file writable?
Definition: RootDataConnection.h:268
Gaudi::RootDataConnection::Tool::msgSvc
MsgStream & msgSvc() const
Definition: RootDataConnection.h:231
std::unique_ptr::get
T get(T... args)
Gaudi::RootDataConnection::Tool::ContainerSection
RootDataConnection::ContainerSection ContainerSection
Definition: RootDataConnection.h:219
Gaudi::RootDataConnection::tool
Tool * tool() const
Access tool.
Definition: RootDataConnection.h:270
IIODataManager.h
Gaudi::RootDataConnection::ContainerSection
Definition: RootDataConnection.h:126
Gaudi::RootDataConnection::m_params
ParamMap m_params
Parameter map for file parameters.
Definition: RootDataConnection.h:186
Gaudi::RootDataConnection::Tool::c
RootDataConnection * c
Pointer to containing data connection object.
Definition: RootDataConnection.h:223
gaudirun.c
c
Definition: gaudirun.py:525
Gaudi::RootDataConnection::ContainerSection::operator=
ContainerSection & operator=(const ContainerSection &s)
Assignment operator to copy objects.
Definition: RootDataConnection.h:134
IRegistry
Definition: IRegistry.h:32
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
Gaudi::RootConnectionSetup::msgSvc
MsgStream & msgSvc() const
Retrieve message service.
Definition: RootDataConnection.h:96
Gaudi::RootDataConnection::Tool::name
const std::string & name() const
Definition: RootDataConnection.h:233
IOTest.start
start
Definition: IOTest.py:110
Gaudi::RootDataConnection::m_dbs
StringVec m_dbs
Map containing external database file names (fids)
Definition: RootDataConnection.h:178
Gaudi::RootDataConnection::Tool::incidentSvc
IIncidentSvc * incidentSvc() const
Definition: RootDataConnection.h:232
Gaudi::RootDataConnection::saveRefs
StatusCode saveRefs()
Internal helper to save/update reference tables.
Gaudi::RootDataConnection::Tool::LinkSections
RootDataConnection::LinkSections LinkSections
Definition: RootDataConnection.h:218
Gaudi::RootObjectRefs
Definition: RootRefs.h:68
Gaudi::RootDataConnection
Definition: RootDataConnection.h:112
Gaudi::RootConnectionSetup::StringVec
std::vector< std::string > StringVec
Type definition for string maps.
Definition: RootDataConnection.h:63
IIncidentSvc.h
SmartIF.h
Gaudi::RootDataConnection::ParamMap
std::vector< std::pair< std::string, std::string > > ParamMap
Type definition for the parameter map.
Definition: RootDataConnection.h:150
Gaudi::RootDataConnection::m_linkSects
LinkSections m_linkSects
Database link sections.
Definition: RootDataConnection.h:190
Gaudi::RootDataConnection::ContainerSections
std::vector< ContainerSection > ContainerSections
Definition of container sections to handle merged files.
Definition: RootDataConnection.h:154
StatusCode
Definition: StatusCode.h:65
Gaudi::RootDataConnection::m_mergeFIDs
StringVec m_mergeFIDs
Map containing merge FIDs.
Definition: RootDataConnection.h:184
Gaudi::RootDataConnection::m_setup
std::shared_ptr< RootConnectionSetup > m_setup
Reference to the setup structure.
Definition: RootDataConnection.h:168
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
Gaudi::RootDataConnection::Tool::sections
Sections & sections() const
Definition: RootDataConnection.h:234
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:798
Gaudi::RootDataConnection::m_mergeSects
MergeSections m_mergeSects
Database section map for merged files.
Definition: RootDataConnection.h:188
Gaudi::RootDataConnection::getCont
const std::string & getCont(int which) const
Access container name from saved index.
Definition: RootDataConnection.h:346
Gaudi::RootDataConnection::LinkSections
std::vector< RootRef > LinkSections
Link sections definition.
Definition: RootDataConnection.h:158
Gaudi::RootConnectionSetup::m_msgSvc
std::unique_ptr< MsgStream > m_msgSvc
Reference to message service.
Definition: RootDataConnection.h:57
Gaudi::RootDataConnection::m_statistics
std::unique_ptr< TTreePerfStats > m_statistics
I/O read statistics from TTree.
Definition: RootDataConnection.h:170
Gaudi::RootRef
Definition: RootRefs.h:40
Gaudi::RootDataConnection::Tool::links
StringVec & links() const
Definition: RootDataConnection.h:229
Gaudi::RootDataConnection::ContainerSection::start
int start
The start entry of the section.
Definition: RootDataConnection.h:142
Gaudi::RootDataConnection::write
StatusCode write(const void *, int) override
Write root byte buffer to output stream.
Definition: RootDataConnection.h:318
Gaudi::RootDataConnection::Tool::loadRefs
virtual int loadRefs(std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs &refs)=0
Load references object.
SmartIF< IIncidentSvc >
Gaudi::RootDataConnection::file
TFile * file() const
Direct access to TFile structure.
Definition: RootDataConnection.h:264
std::map
STL class.
Gaudi::RootDataConnection::m_tool
std::unique_ptr< Tool > m_tool
Definition: RootDataConnection.h:253
Gaudi::RootDataConnection::Tool::mergeSections
MergeSections & mergeSections() const
Definition: RootDataConnection.h:236
Gaudi::RootDataConnection::m_conts
StringVec m_conts
Map containing external container names.
Definition: RootDataConnection.h:180
Gaudi::RootDataConnection::Status
Status
Definition: RootDataConnection.h:114
MsgStream
Definition: MsgStream.h:33
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::RootDataConnection::Tool::saveRefs
virtual StatusCode saveRefs()=0
Save references section when closing data file.
Gaudi::RootDataConnection::incidentSvc
IIncidentSvc * incidentSvc() const
Definition: RootDataConnection.h:164
Gaudi::RootDataConnection::Tool::Sections
RootDataConnection::Sections Sections
Definition: RootDataConnection.h:216
Gaudi::RootDataConnection::Tool::params
ParamMap & params() const
Definition: RootDataConnection.h:230
Gaudi::RootDataConnection::Tool::poolRef
virtual RootRef poolRef(size_t) const
Internal overload to facilitate the access to POOL files.
Definition: RootDataConnection.h:243
Gaudi::RootDataConnection::Tool::StringVec
RootDataConnection::StringVec StringVec
Definition: RootDataConnection.h:214
Gaudi::RootDataConnection::seek
long long int seek(long long int, int) override
Seek on the file described by ioDesc. Arguments as in ::seek()
Definition: RootDataConnection.h:320
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:286
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::RootConnectionSetup
Definition: RootDataConnection.h:54
Gaudi::RootDataConnection::Tool::refs
TTree * refs() const
Definition: RootDataConnection.h:226
RootRefs.h
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
Gaudi::RootDataConnection::ContainerSection::ContainerSection
ContainerSection(const ContainerSection &s)
Copy constructor.
Definition: RootDataConnection.h:132
gaudirun.l
dictionary l
Definition: gaudirun.py:583
Gaudi::RootDataConnection::Tool::linkSections
LinkSections & linkSections() const
Definition: RootDataConnection.h:235
Gaudi::RootConnectionSetup::RootConnectionSetup
RootConnectionSetup()=default
Standard constructor.
std::vector::begin
T begin(T... args)
Gaudi::RootDataConnection::Tool::getBranch
virtual TBranch * getBranch(std::string_view section, std::string_view n)=0
Access data branch by name: Get existing branch in read only mode.
IInterface
Definition: IInterface.h:239
Gaudi::RootDataConnection::mergeSections
const MergeSections & mergeSections() const
Access merged data section inventory.
Definition: RootDataConnection.h:272
DataObject
Definition: DataObject.h:36
Gaudi::RootDataConnection::Tool::~Tool
virtual ~Tool()=default
Default destructor.
Gaudi::RootDataConnection::m_links
StringVec m_links
Map containing internal links names.
Definition: RootDataConnection.h:182
Gaudi::RootDataConnection::StringVec
std::vector< std::string > StringVec
Type definition for string maps.
Definition: RootDataConnection.h:148
compareRootHistos.ref
ref
Definition: compareRootHistos.py:27
Gaudi::RootDataConnection::MergeSections
std::map< std::string, ContainerSections, std::less<> > MergeSections
Definition of database section to handle merged files.
Definition: RootDataConnection.h:156
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::RootConnectionSetup::incidentSvc
IIncidentSvc * incidentSvc() const
Retrieve incident service.
Definition: RootDataConnection.h:101
Gaudi::RootConnectionSetup::loadSection
std::string loadSection
RootCnvSvc Property: Root data cache size.
Definition: RootDataConnection.h:70
Gaudi::RootDataConnection::ContainerSection::ContainerSection
ContainerSection(int s, int l)
Initializing constructor.
Definition: RootDataConnection.h:130
Gaudi::RootDataConnection::Tool::ContainerSections
RootDataConnection::ContainerSections ContainerSections
Definition: RootDataConnection.h:220
IIncidentSvc
Definition: IIncidentSvc.h:33
Gaudi::RootDataConnection::m_sections
Sections m_sections
Tree sections in TFile.
Definition: RootDataConnection.h:176
Gaudi::RootDataConnection::getBranch
TBranch * getBranch(std::string_view section, std::string_view branch_name)
Access data branch by name: Get existing branch in read only mode.
Definition: RootDataConnection.h:326
Gaudi::RootDataConnection::Clients
std::set< const IInterface * > Clients
Client set.
Definition: RootDataConnection.h:160
std::unique_ptr< MsgStream >
Gaudi::RootDataConnection::msgSvc
MsgStream & msgSvc() const
Allow access to printer service.
Definition: RootDataConnection.h:163
Gaudi::RootConnectionSetup::vetoBranches
StringVec vetoBranches
Vector of strings with branches to NOT be cached for input files.
Definition: RootDataConnection.h:68
std::set< const IInterface * >
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property< bool >
Gaudi::RootDataConnection::m_empty
std::string m_empty
Buffer for empty string reference.
Definition: RootDataConnection.h:194
Property.h
Gaudi::RootDataConnection::Tool
Definition: RootDataConnection.h:212
Gaudi::RootDataConnection::Tool::ParamMap
RootDataConnection::ParamMap ParamMap
Definition: RootDataConnection.h:215