The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
RootDataConnection.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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//====================================================================
12// RootDataConnection.cpp
13//--------------------------------------------------------------------
14//
15// Author : M.Frank
16//====================================================================
17
18// Framework include files
19#include "RootUtils.h"
29// ROOT include files
30#include <Compression.h>
31#include <TBranch.h>
32#include <TClass.h>
33#include <TFile.h>
34#include <TLeaf.h>
35#include <TMemFile.h>
36#include <TROOT.h>
37#include <TTree.h>
38
39static int s_compressionLevel = ROOT::CompressionSettings( ROOT::RCompressionSetting::EAlgorithm::kLZMA, 4 );
40
41#define ROOT_HAS_630_FWD_COMPAT ROOT_VERSION_CODE > ROOT_VERSION( 6, 30, 4 )
42
43// C/C++ include files
44#include <format>
45#include <limits>
46#include <numeric>
47#include <stdexcept>
48#include <strings.h>
49
50using namespace Gaudi;
51using namespace std;
52typedef const string& CSTR;
53
54static const string s_empty;
55static const string s_local = "<localDB>";
56
57#ifdef __POOL_COMPATIBILITY
58# include "PoolTool.h"
59#endif
60#include "RootTool.h"
61
62namespace {
63 std::array<char, 256> init_table() {
64 std::array<char, 256> table;
65 std::iota( std::begin( table ), std::end( table ), 0 );
66 return table; // cppcheck-suppress uninitvar; false positive
67 }
68
69 struct RootDataConnectionCategory : StatusCode::Category {
70 const char* name() const override { return "RootDataConnection"; }
71
72 bool isRecoverable( StatusCode::code_t ) const override { return false; }
73
74 std::string message( StatusCode::code_t code ) const override {
75 switch ( static_cast<RootDataConnection::Status>( code ) ) {
77 return "ROOT_READ_ERROR";
79 return "ROOT_OPEN_ERROR";
80 default:
82 }
83 }
84 };
85
86 static bool match_wild( const char* str, const char* pat ) {
87 //
88 // Credits: Code from Alessandro Felice Cantatore.
89 //
90 static const auto table = init_table();
91 const char * s, *p;
92 bool star = false;
93 loopStart:
94 for ( s = str, p = pat; *s; ++s, ++p ) {
95 switch ( *p ) {
96 case '?':
97 if ( *s == '.' ) goto starCheck;
98 break;
99 case '*':
100 star = true;
101 str = s, pat = p;
102 do { ++pat; } while ( *pat == '*' );
103 if ( !*pat ) return true;
104 goto loopStart;
105 default:
106 if ( table[*s] != table[*p] ) goto starCheck;
107 break;
108 } /* endswitch */
109 } /* endfor */
110 while ( *p == '*' ) ++p;
111 return ( !*p );
112
113 starCheck:
114 if ( !star ) return false;
115 str++;
116 goto loopStart;
117 }
118} // namespace
119
120STATUSCODE_ENUM_IMPL( Gaudi::RootDataConnection::Status, RootDataConnectionCategory )
121
122
124 int res = 0, level = ROOT::CompressionSettings( ROOT::RCompressionSetting::EAlgorithm::kLZMA, 6 );
125 auto idx = compression.find( ':' );
126 if ( idx != string::npos ) {
127 auto alg = compression.substr( 0, idx );
128 ROOT::RCompressionSetting::EAlgorithm::EValues alg_code = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal;
129 if ( alg.size() == 4 && strncasecmp( alg.data(), "ZLIB", 4 ) == 0 )
130 alg_code = ROOT::RCompressionSetting::EAlgorithm::kZLIB;
131 else if ( alg.size() == 4 && strncasecmp( alg.data(), "LZMA", 4 ) == 0 )
132 alg_code = ROOT::RCompressionSetting::EAlgorithm::kLZMA;
133 else if ( alg.size() == 3 && strncasecmp( alg.data(), "LZ4", 3 ) == 0 )
134 alg_code = ROOT::RCompressionSetting::EAlgorithm::kLZ4;
135 else if ( alg.size() == 4 && strncasecmp( alg.data(), "ZSTD", 4 ) == 0 )
136 alg_code = ROOT::RCompressionSetting::EAlgorithm::kZSTD;
137 else
138 throw runtime_error( "ERROR: request to set unknown ROOT compression algorithm:" + std::string{ alg } );
139 res = ::sscanf( std::string{ compression.substr( idx + 1 ) }.c_str(), "%d",
140 &level ); // TODO: use C++17 std::from_chars instead...
141 if ( res == 1 ) {
142 s_compressionLevel = ROOT::CompressionSettings( alg_code, level );
143 return StatusCode::SUCCESS;
144 }
145 throw runtime_error( "ERROR: request to set unknown ROOT compression level:" +
146 std::string{ compression.substr( idx + 1 ) } );
147 } else if ( 1 == ::sscanf( std::string{ compression }.c_str(), "%d", &level ) ) { // TODO: use C++17 std::from_chars
148 // instead
149 s_compressionLevel = level;
150 return StatusCode::SUCCESS;
151 }
152 throw runtime_error( "ERROR: request to set unknown ROOT compression mechanism:" + std::string{ compression } );
153}
154
156int RootConnectionSetup::compression() { return s_compressionLevel; }
157
160
163
166 std::shared_ptr<RootConnectionSetup> setup )
167 : IDataConnection( owner, std::string{ fname } ), m_setup( std::move( setup ) ) {
168 // 01234567890123456789012345678901234567890
169 // Check if FID: A82A3BD8-7ECB-DC11-8DC0-000423D950B0
170 if ( fname.size() == 36 && fname[8] == '-' && fname[13] == '-' && fname[18] == '-' && fname[23] == '-' ) {
171 m_name = "FID:";
172 m_name.append( fname.data(), fname.size() );
173 }
174 m_age = 0;
175 m_file.reset();
176 addClient( owner );
177}
178
180void RootDataConnection::addClient( const IInterface* client ) { m_clients.insert( client ); }
181
184 auto i = m_clients.find( client );
185 if ( i != m_clients.end() ) m_clients.erase( i );
186 return m_clients.size();
187}
188
190bool RootDataConnection::lookupClient( const IInterface* client ) const {
191 auto i = m_clients.find( client );
192 return i != m_clients.end();
193}
194
196void RootDataConnection::badWriteError( std::string_view msg ) const {
197 msgSvc() << MSG::ERROR << "File:" << fid() << "Failed action:" << msg << endmsg;
198}
199
201void RootDataConnection::saveStatistics( std::string_view statisticsFile ) {
202 if ( m_statistics ) {
203 m_statistics->Print();
204 if ( !statisticsFile.empty() ) m_statistics->SaveAs( std::string{ statisticsFile }.c_str() );
205 m_statistics.reset();
206 }
207}
208
210void RootDataConnection::enableStatistics( std::string_view section ) {
211 if ( m_statistics ) {
212 TTree* t = getSection( section, false );
213 if ( t ) {
214 m_statistics.reset( new TTreePerfStats( ( std::string{ section } + "_ioperf" ).c_str(), t ) );
215 return;
216 }
217 msgSvc() << MSG::WARNING << "Failed to enable perfstats for tree:" << section << endmsg;
218 return;
219 }
220 msgSvc() << MSG::INFO << "Perfstats are ALREADY ENABLED." << endmsg;
221}
222
225 if ( !m_refs ) m_refs = (TTree*)m_file->Get( "Refs" );
226 if ( m_refs ) m_tool.reset( new RootTool( this ) );
227#ifdef __POOL_COMPATIBILITY
228 else if ( m_file->Get( "##Links" ) != nullptr )
229 m_tool.reset( new PoolTool( this ) );
230#endif
231 else
232 m_tool.reset();
233 return m_tool.get();
234}
235
238 m_file.reset( TFile::Open( m_pfn.c_str() ) );
239 if ( !m_file || m_file->IsZombie() ) {
240 m_file.reset();
241 return StatusCode::FAILURE;
242 }
244 msgSvc() << MSG::DEBUG << "Opened file " << m_pfn << " in mode READ. [" << m_fid << "]" << endmsg << MSG::DEBUG;
245 if ( msgSvc().isActive() ) m_file->ls();
246 msgSvc() << MSG::VERBOSE;
247 if ( msgSvc().isActive() ) m_file->Print();
248 if ( makeTool() ) {
249 sc = m_tool->readRefs();
250 sc.ignore();
251 if ( sc == Status::ROOT_READ_ERROR ) {
252 IIncidentSvc* inc = m_setup->incidentSvc();
253 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
254 }
255 }
256 if ( !sc.isSuccess() ) return sc;
257 bool need_fid = m_fid == m_pfn;
258 string fid = m_fid;
259 m_mergeFIDs.clear();
260 for ( auto& elem : m_params ) {
261 if ( elem.first == "FID" ) {
262 m_mergeFIDs.push_back( elem.second );
263 if ( elem.second != m_fid ) {
264 msgSvc() << MSG::DEBUG << "Check FID param:" << elem.second << endmsg;
265 // if ( m_fid == m_pfn ) {
266 m_fid = elem.second;
267 //}
268 }
269 }
270 }
271 if ( !need_fid && fid != m_fid ) {
272 msgSvc() << MSG::ERROR << "FID mismatch:" << fid << "(Catalog) != " << m_fid << "(file)" << endmsg
273 << "for PFN:" << m_pfn << endmsg;
274 return StatusCode::FAILURE;
275 }
276 msgSvc() << MSG::DEBUG << "Using FID " << m_fid << " from params table...." << endmsg << "for PFN:" << m_pfn
277 << endmsg;
278 return sc;
279}
280
283 int compress = RootConnectionSetup::compression();
284 msgSvc() << MSG::DEBUG;
285 std::string spec = m_pfn;
286 if ( m_setup->produceReproducibleFiles ) spec += "?reproducible"; // https://root.cern.ch/doc/master/classTFile.html
287 switch ( typ ) {
288 case CREATE:
289 resetAge();
290 m_file.reset( TFile::Open( spec.c_str(), "CREATE", "Root event data", compress ) );
291#if ROOT_HAS_630_FWD_COMPAT
292 if ( m_file && m_setup->root630ForwardCompatibility ) m_file->SetBit( TFile::k630forwardCompatibility );
293#endif
294 m_refs = new TTree( "Refs", "Root reference data" );
295 msgSvc() << "Opened file " << m_pfn << " in mode CREATE. [" << m_fid << "]" << endmsg;
296 m_params.emplace_back( "PFN", m_pfn );
297 if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
298 makeTool();
299 break;
300 case RECREATE:
301 resetAge();
302 m_file.reset( TFile::Open( spec.c_str(), "RECREATE", "Root event data", compress ) );
303#if ROOT_HAS_630_FWD_COMPAT
304 if ( m_file && m_setup->root630ForwardCompatibility ) m_file->SetBit( TFile::k630forwardCompatibility );
305#endif
306 msgSvc() << "Opened file " << m_pfn << " in mode RECREATE. [" << m_fid << "]" << endmsg;
307 m_refs = new TTree( "Refs", "Root reference data" );
308 m_params.emplace_back( "PFN", m_pfn );
309 if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
310 makeTool();
311 break;
312 case UPDATE:
313 resetAge();
314 m_file.reset( TFile::Open( spec.c_str(), "UPDATE", "Root event data", compress ) );
315 msgSvc() << "Opened file " << m_pfn << " in mode UPDATE. [" << m_fid << "]" << endmsg;
316 if ( m_file && !m_file->IsZombie() ) {
317 if ( makeTool() ) {
318 StatusCode sc = m_tool->readRefs();
319 sc.ignore();
320 if ( sc == Status::ROOT_READ_ERROR ) {
321 IIncidentSvc* inc = m_setup->incidentSvc();
322 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
323 }
324 return sc;
325 }
326 TDirectory::TContext ctxt( m_file.get() );
327 m_refs = new TTree( "Refs", "Root reference data" );
328 makeTool();
329 return StatusCode::SUCCESS;
330 }
331 break;
332 default:
333 m_refs = nullptr;
334 m_file.reset();
335 return StatusCode::FAILURE;
336 }
338}
339
342 if ( m_file ) {
343 if ( !m_file->IsZombie() ) {
344 if ( m_file->IsWritable() ) {
345 msgSvc() << MSG::DEBUG;
346 TDirectory::TContext ctxt( m_file.get() );
347 if ( m_refs ) {
348 if ( !m_tool->saveRefs().isSuccess() ) badWriteError( "Saving References" );
349 if ( m_refs->Write() < 0 ) badWriteError( "Write Reference branch" );
350 }
351 for ( auto& i : m_sections ) {
352 if ( i.second ) {
353 if ( i.second->Write() < 0 ) badWriteError( "Write section:" + i.first );
354 msgSvc() << "Disconnect section " << i.first << " " << i.second->GetName() << endmsg;
355 }
356 }
357 m_sections.clear();
358 }
359 msgSvc() << MSG::DEBUG;
360 if ( msgSvc().isActive() ) m_file->ls();
361 msgSvc() << MSG::VERBOSE;
362 if ( msgSvc().isActive() ) m_file->Print();
363 m_file->Close();
364 }
365 msgSvc() << MSG::DEBUG << "Disconnected file " << m_pfn << " " << m_file->GetName() << endmsg;
366 m_file.reset();
367 m_tool.reset();
368 }
369 return StatusCode::SUCCESS;
370}
371
373TTree* RootDataConnection::getSection( std::string_view section, bool create ) {
374 auto it = m_sections.find( section );
375 TTree* t = ( it != m_sections.end() ? it->second : nullptr );
376 if ( !t ) {
377 t = (TTree*)m_file->Get( std::string{ section }.c_str() );
378 if ( !t && create ) {
379 TDirectory::TContext ctxt( m_file.get() );
380 t = new TTree( std::string{ section }.c_str(), "Root data for Gaudi" );
381 }
382 if ( t ) {
383 int cacheSize = m_setup->cacheSize;
384 if ( create ) {
385 // t->SetAutoFlush(100);
386 }
387 if ( section == m_setup->loadSection && cacheSize > -2 ) {
388 MsgStream& msg = msgSvc();
389 int learnEntries = m_setup->learnEntries;
390 t->SetCacheSize( cacheSize );
391 t->SetCacheLearnEntries( learnEntries );
392 msg << MSG::DEBUG;
393 if ( create ) {
394 msg << "Tree:" << section << "Setting up tree cache:" << cacheSize << endmsg;
395 } else {
396 const StringVec& vB = m_setup->vetoBranches;
397 const StringVec& cB = m_setup->cacheBranches;
398 msg << "Tree:" << section << " Setting up tree cache:" << cacheSize << " Add all branches." << endmsg;
399 msg << "Tree:" << section << " Learn for " << learnEntries << " entries." << endmsg;
400
401 if ( cB.empty() && vB.empty() ) {
402 msg << "Adding (default) all branches to tree cache." << endmsg;
403 t->AddBranchToCache( "*", kTRUE );
404 }
405 if ( cB.size() == 1 && cB[0] == "*" ) {
406 msg << "Adding all branches to tree cache according to option \"CacheBranches\"." << endmsg;
407 t->AddBranchToCache( "*", kTRUE );
408 } else {
409 for ( TIter it( t->GetListOfBranches() ); it.Next(); ) {
410 const char* n = ( (TNamed*)( *it ) )->GetName();
411 bool add = false, veto = false;
412 for ( const auto& i : cB ) {
413 if ( !match_wild( n, ( i ).c_str() ) ) continue;
414 add = true;
415 break;
416 }
417 for ( auto i = vB.cbegin(); !add && i != vB.cend(); ++i ) {
418 if ( !match_wild( n, ( *i ).c_str() ) ) continue;
419 veto = true;
420 break;
421 }
422 if ( add && !veto ) {
423 msg << "Add " << n << " to branch cache." << endmsg;
424 t->AddBranchToCache( n, kTRUE );
425 } else {
426 msg << "Do not cache branch " << n << endmsg;
427 }
428 }
429 }
430 }
431 }
432 m_sections[std::string{ section }] = t;
433 } else {
434 // in some rare cases we do have the entry we expect, but we cannot read it
435 // https://gitlab.cern.ch/gaudi/Gaudi/-/issues/301
436 auto key = m_file->GetKey( std::string{ section }.c_str() );
437 if ( key ) {
438 incidentSvc()->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) );
439 msgSvc() << MSG::ERROR << std::format( "failed to get TTree '{}' in {}", section, pfn() ) << endmsg;
440 }
441 }
442 }
443 return t;
444}
445
447TBranch* RootDataConnection::getBranch( std::string_view section, std::string_view branch_name, TClass* cl, void* ptr,
448 int buff_siz, int split_lvl ) {
449 string n = std::string{ branch_name };
450 std::replace_if(
451 begin( n ), end( n ), []( const char c ) { return !isalnum( c ); }, '_' );
452 n += ".";
453 TTree* t = getSection( section, true );
454 TBranch* b = t->GetBranch( n.c_str() );
455 if ( !b && cl && m_file->IsWritable() ) {
456 b = t->Branch( n.c_str(), cl->GetName(), (void*)( ptr ? &ptr : nullptr ), buff_siz, split_lvl );
457 }
458 if ( !b ) b = t->GetBranch( std::string{ branch_name }.c_str() );
459 if ( b ) b->SetAutoDelete( kFALSE );
460 return b;
461}
462
464int RootDataConnection::makeLink( std::string_view p ) {
465 auto ip = std::find( std::begin( m_links ), std::end( m_links ), p );
466 if ( ip != std::end( m_links ) ) return std::distance( std::begin( m_links ), ip );
467 m_links.push_back( std::string{ p } );
468 return m_links.size() - 1;
469}
470
472CSTR RootDataConnection::getDb( int which ) const {
473 if ( ( which >= 0 ) && ( size_t( which ) < m_dbs.size() ) ) {
474 if ( *( m_dbs.begin() + which ) == s_local ) return m_fid;
475 return *( m_dbs.begin() + which );
476 }
477 return s_empty;
478}
479
481CSTR RootDataConnection::empty() const { return s_empty; }
482
484pair<int, unsigned long> RootDataConnection::saveObj( std::string_view section, std::string_view cnt, TClass* cl,
485 DataObject* pObj, int minBufferSize, int maxBufferSize,
486 int approxEventsPerBasket, int split_lvl, bool fill ) {
487 DataObjectPush push( pObj );
488 return save( section, cnt, cl, pObj, minBufferSize, maxBufferSize, approxEventsPerBasket, split_lvl, fill );
489}
490
492pair<int, unsigned long> RootDataConnection::save( std::string_view section, std::string_view cnt, TClass* cl,
493 void* pObj, int minBufferSize, int maxBufferSize,
494 int approxEventsPerBasket, int split_lvl, bool fill_missing ) {
495 split_lvl = 0;
496 TBranch* b = getBranch( section, cnt, cl, pObj ? &pObj : nullptr, minBufferSize, split_lvl );
497 if ( b ) {
498 Long64_t evt = b->GetEntries();
499 // msgSvc() << MSG::DEBUG << cnt.c_str() << " Obj:" << (void*)pObj
500 // << " Split:" << split_lvl << " Buffer size:" << minBufferSize << endl;
501 bool set_buffer_size = ( evt == 0 );
502 if ( fill_missing ) {
503 Long64_t num, nevt = b->GetTree()->GetEntries();
504 if ( nevt > evt ) {
505 set_buffer_size = true;
506 b->SetAddress( nullptr );
507 num = nevt - evt;
508 while ( num > 0 ) {
509 b->Fill();
510 --num;
511 }
512 msgSvc() << MSG::DEBUG << "Added " << long( nevt - evt ) << " / Tree: " << nevt
513 << " / Branch: " << b->GetEntries() + 1 << " NULL entries to:" << cnt << endmsg;
514 evt = b->GetEntries();
515 }
516 }
517 if ( set_buffer_size ) {
518 auto dummy_file = make_unique<TMemFile>( "dummy.root", "CREATE" );
519 auto dummy_tree = make_unique<TTree>( "DummyTree", "DummyTree", split_lvl, dummy_file->GetDirectory( "/" ) );
520 TBranch* dummy_branch = dummy_tree->Branch( "DummyBranch", cl->GetName(), &pObj, minBufferSize, split_lvl );
521 Int_t nWritten = dummy_branch->Fill();
522 if ( nWritten < 0 ) return { nWritten, evt };
523 Int_t newBasketSize = nWritten * approxEventsPerBasket;
524 // Ensure that newBasketSize doesn't wrap around
525 if ( std::numeric_limits<Int_t>::max() / approxEventsPerBasket < nWritten ) {
526 newBasketSize = std::numeric_limits<Int_t>::max();
527 }
528 b->SetBasketSize( std::min( maxBufferSize, std::max( minBufferSize, newBasketSize ) ) );
529 msgSvc() << MSG::DEBUG << "Setting basket size to " << newBasketSize << " for " << cnt << endmsg;
530 }
531 b->SetAddress( &pObj );
532 return { b->Fill(), evt };
533 }
534 if ( pObj ) { msgSvc() << MSG::ERROR << "Failed to access branch " << m_name << "/" << cnt << endmsg; }
535 return { -1, ~0 };
536}
537
539int RootDataConnection::loadObj( std::string_view section, std::string_view cnt, unsigned long entry,
540 DataObject*& pObj ) {
541 TBranch* b = getBranch( section, cnt );
542 if ( b ) {
543 TClass* cl = gROOT->GetClass( b->GetClassName(), kTRUE );
544 if ( cl ) {
545 int nb = -1;
546 pObj = (DataObject*)cl->New();
547 {
548 DataObjectPush push( pObj );
549 b->SetAddress( &pObj );
550 if ( section == m_setup->loadSection ) {
551 TTree* t = b->GetTree();
552 if ( Long64_t( entry ) != t->GetReadEntry() ) { t->LoadTree( Long64_t( entry ) ); }
553 }
554 nb = b->GetEntry( entry );
555 msgSvc() << MSG::VERBOSE;
556 if ( msgSvc().isActive() ) {
557 msgSvc() << "Load [" << entry << "] --> " << section << ":" << cnt << " " << nb << " bytes." << endmsg;
558 }
559 if ( nb < 0 ) { // This is definitely an error...ROOT says if reads fail, -1 is issued.
560 IIncidentSvc* inc = m_setup->incidentSvc();
561 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
562 } else if ( nb == 0 && pObj->clID() == CLID_DataObject ) {
563 TFile* f = b->GetFile();
564 int vsn = f->GetVersion();
565 if ( vsn < 52400 ) {
566 // For Gaudi v21r5 (ROOT 5.24.00b) DataObject::m_version was not written!
567 // Still this call be well be successful.
568 nb = 1;
569 } else if ( vsn > 1000000 && ( vsn % 1000000 ) < 52400 ) {
570 // dto. Some POOL files have for unknown reasons a version
571 // not according to ROOT standards. Hack this explicitly.
572 nb = 1;
573 }
574 }
575 if ( nb < 0 ) {
576 delete pObj;
577 pObj = nullptr;
578 }
579 }
580 return nb;
581 }
582 }
583 return -1;
584}
585
587int RootDataConnection::loadRefs( std::string_view section, std::string_view cnt, unsigned long entry,
588 RootObjectRefs& refs ) {
589 int nbytes = m_tool->loadRefs( section, cnt, entry, refs );
590 if ( nbytes < 0 ) {
591 // This is definitely an error:
592 // -- Either branch not present at all or
593 // -- ROOT I/O error, which issues -1
594 IIncidentSvc* inc = m_setup->incidentSvc();
595 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
596 }
597 return nbytes;
598}
599
601pair<const RootRef*, const RootDataConnection::ContainerSection*>
602RootDataConnection::getMergeSection( std::string_view container, int entry ) const {
603 // size_t idx = cont.find('/',1);
604 // string container = cont[0]=='/' ? cont.substr(1,idx==string::npos?idx:idx-1) : cont;
605 auto i = m_mergeSects.find( container );
606 if ( i != m_mergeSects.end() ) {
607 size_t cnt = 0;
608 const ContainerSections& s = ( *i ).second;
609 for ( auto j = s.cbegin(); j != s.cend(); ++j, ++cnt ) {
610 const ContainerSection& c = *j;
611 if ( entry >= c.start && entry < ( c.start + c.length ) ) {
612 if ( m_linkSects.size() > cnt ) {
613 if ( msgSvc().isActive() ) {
614 msgSvc() << MSG::VERBOSE << "MergeSection for:" << container << " [" << entry << "]" << endmsg
615 << "FID:" << m_fid << " -> PFN:" << m_pfn << endmsg;
616 }
617 return { &( m_linkSects[cnt] ), &c };
618 }
619 }
620 }
621 }
622 msgSvc() << MSG::DEBUG << "Return INVALID MergeSection for:" << container << " [" << entry << "]" << endmsg
623 << "FID:" << m_fid << " -> PFN:" << m_pfn << endmsg;
624 return { nullptr, nullptr };
625}
626
629 IOpaqueAddress* pA = pR.address();
630 makeRef( pR.name(), pA->clID(), pA->svcType(), pA->par()[0], pA->par()[1], -1, ref );
631}
632
634void RootDataConnection::makeRef( std::string_view name, long clid, int tech, std::string_view dbase,
635 std::string_view cnt, int entry, RootRef& ref ) {
636 auto db = ( dbase == m_fid ? std::string_view{ s_local } : dbase );
637 ref.entry = entry;
638
639 int cdb = -1;
640 if ( !db.empty() ) {
641 auto idb = std::find_if( m_dbs.begin(), m_dbs.end(), [&]( const std::string& i ) { return i == db; } );
642 cdb = std::distance( m_dbs.begin(), idb );
643 if ( idb == m_dbs.end() ) m_dbs.push_back( std::string{ db } );
644 }
645
646 int ccnt = -1;
647 if ( !cnt.empty() ) {
648 auto icnt = std::find_if( m_conts.begin(), m_conts.end(), [&]( const std::string& i ) { return i == cnt; } );
649 ccnt = std::distance( m_conts.begin(), icnt );
650 if ( icnt == m_conts.end() ) m_conts.push_back( std::string{ cnt } );
651 }
652
653 int clnk = -1;
654 if ( !name.empty() ) {
655 auto ilnk = std::find_if( m_links.begin(), m_links.end(), [&]( const std::string& i ) { return i == name; } );
656 clnk = std::distance( m_links.begin(), ilnk );
657 if ( ilnk == m_links.end() ) m_links.push_back( std::string{ name } );
658 }
659
660 ref.dbase = cdb;
661 ref.container = ccnt;
662 ref.link = clnk;
663 ref.clid = clid;
664 ref.svc = tech;
665 if ( ref.svc == POOL_ROOT_StorageType || ref.svc == POOL_ROOTKEY_StorageType ||
666 ref.svc == POOL_ROOTTREE_StorageType ) {
667 ref.svc = ROOT_StorageType;
668 }
669}
const long POOL_ROOTKEY_StorageType
Definition ClassID.h:77
const long POOL_ROOT_StorageType
Definition ClassID.h:76
const long POOL_ROOTTREE_StorageType
Definition ClassID.h:78
const long ROOT_StorageType
Definition ClassID.h:60
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
const std::string CSTR
#define STATUSCODE_ENUM_IMPL(...)
Assign a category to the StatusCode enum declared with STATUSCODE_ENUM_DECL( ENUM )
Definition StatusCode.h:295
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
virtual const CLID & clID() const
Retrieve reference to class definition structure.
int m_age
Age counter.
std::string m_fid
File ID of the connection.
const IInterface * owner() const
Owner instance.
void resetAge()
Reset age.
std::string m_pfn
Physical file name of the connection.
const std::string & fid() const
Access file id.
std::string m_name
Connection name/identifier.
const std::string & name() const
Connection name.
IDataConnection(const IInterface *own, std::string nam)
Standard constructor.
const std::string & pfn() const
Access physical file name.
IoType
I/O Connection types.
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.
void setIncidentSvc(IIncidentSvc *m)
Set incident service reference.
static StatusCode setCompression(std::string_view compression)
Set the global compression level.
std::unique_ptr< MsgStream > m_msgSvc
Reference to message service.
Helper class to facilitate an abstraction layer for reading POOL style files with this package.
Sections m_sections
Tree sections in TFile.
LinkSections m_linkSects
Database link sections.
void addClient(const IInterface *client)
Add new client to this data source.
Tool * makeTool()
Create file access tool to encapsulate POOL compatibiliy.
StringVec m_links
Map containing internal links names.
const std::string & empty() const
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.
std::unique_ptr< TTreePerfStats > m_statistics
I/O read statistics from TTree.
std::unique_ptr< Tool > m_tool
Clients m_clients
Client list.
ParamMap m_params
Parameter map for file parameters.
TTree * getSection(std::string_view sect, bool create=false)
Access TTree section from section name. The section is created if required.
std::unique_ptr< TFile > m_file
Reference to ROOT file.
std::pair< int, unsigned long > save(std::string_view section, std::string_view cnt, TClass *cl, void *pObj, int minBufferSize, int maxBufferSize, int approxEventsPerBasket, 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.
StatusCode disconnect() override
Release data stream and release implementation dependent resources.
StatusCode connectRead() override
Open data stream in read mode.
int makeLink(std::string_view p)
Convert path string to path index.
StringVec m_conts
Map containing external container names.
bool lookupClient(const IInterface *client) const
Lookup client for this data source.
StatusCode connectWrite(IoType typ) override
Open data stream in write mode.
void saveStatistics(std::string_view statisticsFile)
Save TTree access statistics if required.
TBranch * getBranch(std::string_view section, std::string_view branch_name)
Access data branch by name: Get existing branch in read only mode.
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.
void makeRef(const IRegistry &pA, RootRef &ref)
Create reference object from registry entry.
const std::string & getDb(int which) const
Access database/file name from saved index.
void enableStatistics(std::string_view section)
Enable TTreePerStats.
std::pair< int, unsigned long > saveObj(std::string_view section, std::string_view cnt, TClass *cl, DataObject *pObj, int minBufferSize, int maxBufferSize, int approxEventsPerBasket, int split_lvl, bool fill_missing=false)
Save object of a given class to section and container.
int loadRefs(std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs &refs)
Load references object.
std::pair< const RootRef *, const ContainerSection * > getMergeSection(std::string_view container, int entry) const
Access link section for single container and entry.
void badWriteError(std::string_view msg) const
Error handler when bad write statements occur.
int loadObj(std::string_view section, std::string_view cnt, unsigned long entry, DataObject *&pObj)
Load object.
IIncidentSvc * incidentSvc() const
TTree * m_refs
Pointer to the reference tree.
size_t removeClient(const IInterface *client)
Remove client from this data source.
Description:
Definition RootTool.h:28
The interface implemented by the IncidentSvc service.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
Definition of the basic interface.
Definition IInterface.h:225
Opaque address interface definition.
virtual long svcType() const =0
Retrieve service type.
virtual const CLID & clID() const =0
Retrieve class information from link.
virtual const std::string * par() const =0
Retrieve String parameters.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
virtual const name_type & name() const =0
Name of the directory (or key)
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
Base class for all Incidents (computing events).
Definition Incident.h:24
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
Definition StatusCode.h:310
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
unsigned long code_t
type of StatusCode value
Definition StatusCode.h:66
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
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
@ WARNING
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22
STL namespace.
Internal helper class, which described a TBranch section in a ROOT file.
Persistent reference object containing all leafs and links corresponding to a Gaudi DataObject.
Definition extractEvt.C:81
Persistent reference object.
Definition extractEvt.C:44
The category assigned to a StatusCode.
virtual std::string message(code_t code) const
Description for code within this category.
Definition StatusCode.h:85