26 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 27 #include "Compression.h" 28 static int s_compressionLevel = ROOT::CompressionSettings( ROOT::kLZMA, 4 );
30 static int s_compressionLevel = 1;
37 using namespace Gaudi;
39 typedef const string&
CSTR;
41 static const string s_empty;
42 static const string s_local =
"<localDB>";
44 #ifdef __POOL_COMPATIBILITY 59 const char*
name()
const override {
return "RootDataConnection"; }
65 switch ( static_cast<RootDataConnection::Status>( code ) ) {
66 case RootDataConnection::Status::ROOT_READ_ERROR:
67 return "ROOT_READ_ERROR";
68 case RootDataConnection::Status::ROOT_OPEN_ERROR:
69 return "ROOT_OPEN_ERROR";
76 static bool match_wild(
const char* str,
const char* pat )
81 static const auto table = init_table();
85 for ( s = str, p = pat; *
s; ++
s, ++p ) {
88 if ( *s ==
'.' )
goto starCheck;
95 }
while ( *pat ==
'*' );
96 if ( !*pat )
return true;
99 if ( table[*s] != table[*p] )
goto starCheck;
103 while ( *p ==
'*' ) ++p;
107 if ( !star )
return false;
118 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 119 int res = 0,
level = ROOT::CompressionSettings( ROOT::kLZMA, 6 );
120 auto idx = compression.find(
':' );
121 if ( idx != string::npos ) {
122 auto alg = compression.substr( 0, idx );
123 ROOT::ECompressionAlgorithm alg_code = ROOT::kUseGlobalSetting;
124 if ( alg.size() == 4 && strncasecmp( alg.data(),
"ZLIB", 4 ) == 0 )
125 alg_code = ROOT::kZLIB;
126 else if ( alg.size() == 4 && strncasecmp( alg.data(),
"LZMA", 4 ) == 0 )
127 alg_code = ROOT::kLZMA;
129 throw runtime_error(
"ERROR: request to set unknown ROOT compression algorithm:" + alg.to_string() );
130 res = ::sscanf( compression.substr( idx + 1 ).to_string().c_str(),
"%d",
133 s_compressionLevel = ROOT::CompressionSettings( alg_code,
level );
136 throw runtime_error(
"ERROR: request to set unknown ROOT compression level:" +
137 compression.substr( idx + 1 ).to_string() );
139 ::
sscanf( compression.to_string().c_str(),
"%d", &
level ) ) {
140 s_compressionLevel =
level;
143 throw runtime_error(
"ERROR: request to set unknown ROOT compression mechanism:" + compression.to_string() );
145 if ( !compression.empty() ) {
166 if ( fname.size() == 36 && fname[8] ==
'-' && fname[13] ==
'-' && fname[18] ==
'-' && fname[23] ==
'-' ) {
204 if ( !statisticsFile.empty() )
m_statistics->SaveAs( statisticsFile.to_string().c_str() );
215 m_statistics.
reset(
new TTreePerfStats( ( section.to_string() +
"_ioperf" ).c_str(), t ) );
229 #ifdef __POOL_COMPATIBILITY 230 else if (
m_file->Get(
"##Links" ) != nullptr )
254 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 255 if ( sc == Status::ROOT_READ_ERROR ) {
268 if ( elem.first ==
"FID" ) {
270 if ( elem.second !=
m_fid ) {
271 msgSvc() << MSG::DEBUG <<
"Check FID param:" << elem.second <<
endmsg;
278 if ( !need_fid && fid !=
m_fid ) {
283 msgSvc() << MSG::DEBUG <<
"Using FID " <<
m_fid <<
" from params table...." <<
endmsg <<
"for PFN:" <<
m_pfn 297 m_refs =
new TTree(
"Refs",
"Root reference data" );
309 m_refs =
new TTree(
"Refs",
"Root reference data" );
324 if ( sc == Status::ROOT_READ_ERROR ) {
325 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 334 TDirectory::TContext ctxt(
m_file.
get() );
335 m_refs =
new TTree(
"Refs",
"Root reference data" );
352 if ( !
m_file->IsZombie() ) {
353 if (
m_file->IsWritable() ) {
355 TDirectory::TContext ctxt(
m_file.
get() );
362 if ( i.second->Write() < 0 )
badWriteError(
"Write section:" + i.first );
363 msgSvc() <<
"Disconnect section " << i.first <<
" " << i.second->GetName() <<
endmsg;
385 TTree* t = ( it !=
m_sections.
end() ? it->second : nullptr );
387 t = (TTree*)
m_file->Get( section.to_string().c_str() );
388 if ( !t && create ) {
389 TDirectory::TContext ctxt(
m_file.
get() );
390 t =
new TTree( section.to_string().c_str(),
"Root data for Gaudi" );
393 int cacheSize =
m_setup->cacheSize;
397 if ( section ==
m_setup->loadSection && cacheSize > -2 ) {
399 int learnEntries =
m_setup->learnEntries;
400 t->SetCacheSize( cacheSize );
401 t->SetCacheLearnEntries( learnEntries );
404 msg <<
"Tree:" << section <<
"Setting up tree cache:" << cacheSize <<
endmsg;
408 msg <<
"Tree:" << section <<
" Setting up tree cache:" << cacheSize <<
" Add all branches." <<
endmsg;
409 msg <<
"Tree:" << section <<
" Learn for " << learnEntries <<
" entries." <<
endmsg;
412 msg <<
"Adding (default) all branches to tree cache." <<
endmsg;
413 t->AddBranchToCache(
"*", kTRUE );
415 if ( cB.
size() == 1 && cB[0] ==
"*" ) {
416 msg <<
"Adding all branches to tree cache according to option \"CacheBranches\"." <<
endmsg;
417 t->AddBranchToCache(
"*", kTRUE );
419 for ( TIter it( t->GetListOfBranches() ); it.Next(); ) {
420 const char*
n = ( (TNamed*)( *it ) )->GetName();
421 bool add =
false, veto =
false;
422 for (
const auto& i : cB ) {
423 if ( !match_wild( n, ( i ).c_str() ) )
continue;
427 for (
auto i = vB.
cbegin(); !add && i != vB.
cend(); ++i ) {
428 if ( !match_wild( n, ( *i ).c_str() ) )
continue;
432 if ( add && !veto ) {
433 msg <<
"Add " << n <<
" to branch cache." <<
endmsg;
434 t->AddBranchToCache( n, kTRUE );
436 msg <<
"Do not cache branch " << n <<
endmsg;
450 int buff_siz,
int split_lvl )
452 string n = branch_name.to_string();
456 TBranch* b = t->GetBranch( n.c_str() );
457 if ( !b && cl &&
m_file->IsWritable() ) {
458 b = t->Branch( n.c_str(), cl->GetName(), (
void*)( ptr ? &ptr :
nullptr ), buff_siz, split_lvl );
460 if ( !b ) b = t->GetBranch( branch_name.to_string().c_str() );
461 if ( b ) b->SetAutoDelete( kFALSE );
477 if ( ( which >= 0 ) && (
size_t( which ) <
m_dbs.
size() ) ) {
491 DataObjectPush push( pObj );
492 return save( section, cnt, cl, pObj, buff_siz, split_lvl, fill );
497 void* pObj,
int buff_siz,
int split_lvl,
bool fill_missing )
500 TBranch* b =
getBranch( section, cnt, cl, pObj ? &pObj :
nullptr, buff_siz, split_lvl );
502 Long64_t
evt = b->GetEntries();
505 if ( fill_missing ) {
506 Long64_t num, nevt = b->GetTree()->GetEntries();
508 b->SetAddress(
nullptr );
514 msgSvc() <<
MSG::DEBUG <<
"Added " << long( nevt - evt ) <<
" / Tree: " << nevt
515 <<
" / Branch: " << b->GetEntries() + 1 <<
" NULL entries to:" << cnt <<
endmsg;
516 evt = b->GetEntries();
519 b->SetAddress( &pObj );
520 return {b->Fill(), evt};
534 TClass* cl = gROOT->GetClass( b->GetClassName(), kTRUE );
539 DataObjectPush push( pObj );
540 b->SetAddress( &pObj );
541 if ( section ==
m_setup->loadSection ) {
542 TTree* t = b->GetTree();
543 if ( Long64_t( entry ) != t->GetReadEntry() ) {
544 t->LoadTree( Long64_t( entry ) );
547 nb = b->GetEntry( entry );
549 if (
msgSvc().isActive() ) {
550 msgSvc() <<
"Load [" << entry <<
"] --> " << section <<
":" << cnt <<
" " << nb <<
" bytes." <<
endmsg;
553 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 559 }
else if ( nb == 0 && pObj->
clID() == CLID_DataObject ) {
560 TFile* f = b->GetFile();
561 int vsn = f->GetVersion();
566 }
else if ( vsn > 1000000 && ( vsn % 1000000 ) < 52400 ) {
587 int nbytes =
m_tool->loadRefs( section, cnt, entry, refs );
588 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 ) 612 for (
auto j = s.
cbegin(); j != s.
cend(); ++j, ++cnt ) {
625 msgSvc() <<
MSG::DEBUG <<
"Return INVALID MergeSection for:" << container <<
" [" << entry <<
"]" <<
endmsg 627 return {
nullptr,
nullptr};
639 boost::string_ref cnt,
int entry,
RootRef&
ref )
641 auto db = ( dbase ==
m_fid ? boost::string_ref{s_local} : dbase );
652 if ( !cnt.empty() ) {
659 if ( !name.empty() ) {
void addClient(const IInterface *client)
Add new client to this data source.
bool lookupClient(const IInterface *client) const
Lookup client for this data source.
constexpr static const auto FAILURE
std::pair< int, unsigned long > saveObj(boost::string_ref section, boost::string_ref cnt, TClass *cl, DataObject *pObj, int buff_siz, int split_lvl, bool fill_missing=false)
Save object of a given class to section and container.
MergeSections m_mergeSects
Database section map for merged files.
Definition of the MsgStream class used to transmit messages.
virtual const CLID & clID() const =0
Retrieve class information from link.
const std::string & fid() const
Access file id.
std::unique_ptr< Tool > m_tool
std::string m_name
Connection name/identifier.
IoType
I/O Connection types.
const std::string & getDb(int which) const
Access database/file name from saved index.
int loadRefs(boost::string_ref section, boost::string_ref cnt, unsigned long entry, RootObjectRefs &refs)
Load references object.
std::string m_fid
File ID of the connection.
The category assigned to a StatusCode.
std::unique_ptr< TTreePerfStats > m_statistics
I/O read statistics from TTree.
int makeLink(boost::string_ref p)
Convert path string to path index.
static int compression()
Access to global compression level.
Clients m_clients
Client list.
int length
The length of the section.
int start
The start entry of the section.
size_t removeClient(const IInterface *client)
Remove client from this data source.
bool isActive() const
Accessor: is MsgStream active.
GAUDI_API void fill(AIDA::IHistogram1D *histo, const double value, const double weight=1.0)
simple function to fill AIDA::IHistogram1D objects
virtual const name_type & name() const =0
Name of the directory (or key)
void setMessageSvc(MsgStream *m)
Set message service reference.
StatusCode connectWrite(IoType typ) override
Open data stream in write mode.
virtual const std::string * par() const =0
Retrieve String parameters.
const std::string & pfn() const
Access physical file name.
Persistent reference object containing all leafs and links corresponding to a Gaudi DataObject...
StringVec m_links
Map containing internal links names.
void saveStatistics(boost::string_ref statisticsFile)
Save TTree access statistics if required.
ParamMap m_params
Parameter map for file parameters.
const long POOL_ROOTKEY_StorageType
StringVec m_mergeFIDs
Map containing merge FIDs.
std::string m_pfn
Physical file name of the connection.
int dbase
Data members to define object location in the persistent world.
Persistent reference object.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
virtual const CLID & clID() const
Retrieve reference to class definition structure.
const std::string & name() const
Connection name.
std::pair< int, unsigned long > save(boost::string_ref section, boost::string_ref cnt, TClass *cl, void *pObj, int buff_siz, int split_lvl, bool fill_missing=false)
Save object of a given class to section and container.
void enableStatistics(boost::string_ref section)
Enable TTreePerStats.
#define STATUSCODE_ENUM_IMPL(...)
Assign a category to the StatusCode enum declared with STATUSCODE_ENUM_DECL( ENUM ) ...
This class is used for returning status codes from appropriate routines.
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition of the basic interface.
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
int loadObj(boost::string_ref section, boost::string_ref cnt, unsigned long entry, DataObject *&pObj)
Load object.
static StatusCode setCompression(boost::string_ref compression)
Set the global compression level.
void badWriteError(boost::string_ref msg) const
Error handler when bad write statements occur.
StringVec m_conts
Map containing external container names.
TTree * getSection(boost::string_ref sect, bool create=false)
Access TTree section from section name. The section is created if required.
const std::string & empty() const
Empty string reference.
Sections m_sections
Tree sections in TFile.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
const long ROOT_StorageType
virtual long svcType() const =0
Retrieve service type.
const long POOL_ROOT_StorageType
Tool * makeTool()
Create file access tool to encapsulate POOL compatibiliy.
constexpr static const auto SUCCESS
TBranch * getBranch(boost::string_ref section, boost::string_ref branch_name)
Access data branch by name: Get existing branch in read only mode.
StatusCode connectRead() override
Open data stream in read mode.
ABC describing basic data connection.
const long POOL_ROOTTREE_StorageType
std::shared_ptr< RootConnectionSetup > m_setup
Reference to the setup structure.
LinkSections m_linkSects
Database link sections.
void resetAge()
Reset age.
const StatusCode & ignore() const
Ignore/check StatusCode.
Base class for all Incidents (computing events).
MsgStream & msgSvc() const
Allow access to printer service.
void makeRef(const IRegistry &pA, RootRef &ref)
Create reference object from registry entry.
AttribStringParser::Iterator begin(const AttribStringParser &parser)
std::unique_ptr< TFile > m_file
Reference to ROOT file.
Opaque address interface definition.
StringVec m_dbs
Map containing external database file names (fids)
StatusCode disconnect() override
Release data stream and release implementation dependent resources.
std::pair< const RootRef *, const ContainerSection * > getMergeSection(boost::string_ref container, int entry) const
Access link section for single container and entry.
void setIncidentSvc(IIncidentSvc *m)
Set incident service reference.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A DataObject is the base class of any identifiable object on any data store.
TTree * m_refs
Pointer to the reference tree.
virtual std::string message(code_t code) const
Description for code within this category.
Helper functions to set/get the application return code.
Internal helper class, which described a TBranch section in a ROOT file.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
The interface implemented by the IncidentSvc service.
RootDataConnection(const IInterface *own, boost::string_ref nam, std::shared_ptr< RootConnectionSetup > setup)
Standard constructor.
unsigned long code_t
type of StatusCode value