The Gaudi Framework  master (e98cfcff)
Loading...
Searching...
No Matches
RootDataConnection.cpp
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//====================================================================
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#include "RootTool.h"
58
59namespace {
60 std::array<char, 256> init_table() {
61 std::array<char, 256> table;
62 std::iota( std::begin( table ), std::end( table ), 0 );
63 return table; // cppcheck-suppress uninitvar; false positive
64 }
65
66 struct RootDataConnectionCategory : StatusCode::Category {
67 const char* name() const override { return "RootDataConnection"; }
68
69 bool isRecoverable( StatusCode::code_t ) const override { return false; }
70
71 std::string message( StatusCode::code_t code ) const override {
72 switch ( static_cast<RootDataConnection::Status>( code ) ) {
74 return "ROOT_READ_ERROR";
76 return "ROOT_OPEN_ERROR";
77 default:
79 }
80 }
81 };
82
83 static bool match_wild( const char* str, const char* pat ) {
84 //
85 // Credits: Code from Alessandro Felice Cantatore.
86 //
87 static const auto table = init_table();
88 const char * s, *p;
89 bool star = false;
90 loopStart:
91 for ( s = str, p = pat; *s; ++s, ++p ) {
92 switch ( *p ) {
93 case '?':
94 if ( *s == '.' ) goto starCheck;
95 break;
96 case '*':
97 star = true;
98 str = s, pat = p;
99 do { ++pat; } while ( *pat == '*' );
100 if ( !*pat ) return true;
101 goto loopStart;
102 default:
103 if ( table[*s] != table[*p] ) goto starCheck;
104 break;
105 } /* endswitch */
106 } /* endfor */
107 while ( *p == '*' ) ++p;
108 return ( !*p );
109
110 starCheck:
111 if ( !star ) return false;
112 str++;
113 goto loopStart;
114 }
115} // namespace
116
117STATUSCODE_ENUM_IMPL( Gaudi::RootDataConnection::Status, RootDataConnectionCategory )
118
119
121 int res = 0, level = ROOT::CompressionSettings( ROOT::RCompressionSetting::EAlgorithm::kLZMA, 6 );
122 auto idx = compression.find( ':' );
123 if ( idx != string::npos ) {
124 auto alg = compression.substr( 0, idx );
125 ROOT::RCompressionSetting::EAlgorithm::EValues alg_code = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal;
126 if ( alg.size() == 4 && strncasecmp( alg.data(), "ZLIB", 4 ) == 0 )
127 alg_code = ROOT::RCompressionSetting::EAlgorithm::kZLIB;
128 else if ( alg.size() == 4 && strncasecmp( alg.data(), "LZMA", 4 ) == 0 )
129 alg_code = ROOT::RCompressionSetting::EAlgorithm::kLZMA;
130 else if ( alg.size() == 3 && strncasecmp( alg.data(), "LZ4", 3 ) == 0 )
131 alg_code = ROOT::RCompressionSetting::EAlgorithm::kLZ4;
132 else if ( alg.size() == 4 && strncasecmp( alg.data(), "ZSTD", 4 ) == 0 )
133 alg_code = ROOT::RCompressionSetting::EAlgorithm::kZSTD;
134 else
135 throw runtime_error( "ERROR: request to set unknown ROOT compression algorithm:" + std::string{ alg } );
136 res = ::sscanf( std::string{ compression.substr( idx + 1 ) }.c_str(), "%d",
137 &level ); // TODO: use C++17 std::from_chars instead...
138 if ( res == 1 ) {
139 s_compressionLevel = ROOT::CompressionSettings( alg_code, level );
140 return StatusCode::SUCCESS;
141 }
142 throw runtime_error( "ERROR: request to set unknown ROOT compression level:" +
143 std::string{ compression.substr( idx + 1 ) } );
144 } else if ( 1 == ::sscanf( std::string{ compression }.c_str(), "%d", &level ) ) { // TODO: use C++17 std::from_chars
145 // instead
146 s_compressionLevel = level;
147 return StatusCode::SUCCESS;
148 }
149 throw runtime_error( "ERROR: request to set unknown ROOT compression mechanism:" + std::string{ compression } );
150}
151
153int RootConnectionSetup::compression() { return s_compressionLevel; }
154
157
160
163 std::shared_ptr<RootConnectionSetup> setup )
164 : IDataConnection( owner, std::string{ fname } ), m_setup( std::move( setup ) ) {
165 // 01234567890123456789012345678901234567890
166 // Check if FID: A82A3BD8-7ECB-DC11-8DC0-000423D950B0
167 if ( fname.size() == 36 && fname[8] == '-' && fname[13] == '-' && fname[18] == '-' && fname[23] == '-' ) {
168 m_name = "FID:";
169 m_name.append( fname.data(), fname.size() );
170 }
171 m_age = 0;
172 m_file.reset();
173 addClient( owner );
174}
175
177void RootDataConnection::addClient( const IInterface* client ) { m_clients.insert( client ); }
178
181 auto i = m_clients.find( client );
182 if ( i != m_clients.end() ) m_clients.erase( i );
183 return m_clients.size();
184}
185
187bool RootDataConnection::lookupClient( const IInterface* client ) const {
188 auto i = m_clients.find( client );
189 return i != m_clients.end();
190}
191
193void RootDataConnection::badWriteError( std::string_view msg ) const {
194 msgSvc() << MSG::ERROR << "File:" << fid() << "Failed action:" << msg << endmsg;
195}
196
198void RootDataConnection::saveStatistics( std::string_view statisticsFile ) {
199 if ( m_statistics ) {
200 m_statistics->Print();
201 if ( !statisticsFile.empty() ) m_statistics->SaveAs( std::string{ statisticsFile }.c_str() );
202 m_statistics.reset();
203 }
204}
205
207void RootDataConnection::enableStatistics( std::string_view section ) {
208 if ( m_statistics ) {
209 TTree* t = getSection( section, false );
210 if ( t ) {
211 m_statistics.reset( new TTreePerfStats( ( std::string{ section } + "_ioperf" ).c_str(), t ) );
212 return;
213 }
214 msgSvc() << MSG::WARNING << "Failed to enable perfstats for tree:" << section << endmsg;
215 return;
216 }
217 msgSvc() << MSG::INFO << "Perfstats are ALREADY ENABLED." << endmsg;
218}
219
221 if ( !m_refs ) m_refs = (TTree*)m_file->Get( "Refs" );
222 if ( m_refs )
223 m_tool.reset( new RootTool( this ) );
224 else
225 m_tool.reset();
226 return m_tool.get();
227}
228
231 m_file.reset( TFile::Open( m_pfn.c_str() ) );
232 if ( !m_file || m_file->IsZombie() ) {
233 m_file.reset();
234 return StatusCode::FAILURE;
235 }
237 msgSvc() << MSG::DEBUG << "Opened file " << m_pfn << " in mode READ. [" << m_fid << "]" << endmsg << MSG::DEBUG;
238 if ( msgSvc().isActive() ) m_file->ls();
239 msgSvc() << MSG::VERBOSE;
240 if ( msgSvc().isActive() ) m_file->Print();
241 if ( makeTool() ) {
242 sc = m_tool->readRefs();
243 sc.ignore();
244 if ( sc == Status::ROOT_READ_ERROR ) {
245 IIncidentSvc* inc = m_setup->incidentSvc();
246 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
247 }
248 }
249 if ( !sc.isSuccess() ) return sc;
250 bool need_fid = m_fid == m_pfn;
251 string fid = m_fid;
252 m_mergeFIDs.clear();
253 for ( auto& elem : m_params ) {
254 if ( elem.first == "FID" ) {
255 m_mergeFIDs.push_back( elem.second );
256 if ( elem.second != m_fid ) {
257 msgSvc() << MSG::DEBUG << "Check FID param:" << elem.second << endmsg;
258 // if ( m_fid == m_pfn ) {
259 m_fid = elem.second;
260 //}
261 }
262 }
263 }
264 // Compare uuids in a case insensitive way
265 if ( !need_fid &&
266 !std::ranges::equal( fid, m_fid, []( char a, char b ) { return std::tolower( a ) == std::tolower( b ); } ) ) {
267 msgSvc() << MSG::ERROR << "FID mismatch:" << fid << "(Catalog) != " << m_fid << "(file)" << endmsg
268 << "for PFN:" << m_pfn << endmsg;
269 return StatusCode::FAILURE;
270 }
271 msgSvc() << MSG::DEBUG << "Using FID " << m_fid << " from params table...." << endmsg << "for PFN:" << m_pfn
272 << endmsg;
273 return sc;
274}
275
278 int compress = RootConnectionSetup::compression();
279 msgSvc() << MSG::DEBUG;
280 std::string spec = m_pfn;
281 if ( m_setup->produceReproducibleFiles ) spec += "?reproducible"; // https://root.cern.ch/doc/master/classTFile.html
282 switch ( typ ) {
283 case CREATE:
284 resetAge();
285 m_file.reset( TFile::Open( spec.c_str(), "CREATE", "Root event data", compress ) );
286#if ROOT_HAS_630_FWD_COMPAT
287 if ( m_file && m_setup->root630ForwardCompatibility ) m_file->SetBit( TFile::k630forwardCompatibility );
288#endif
289 m_refs = new TTree( "Refs", "Root reference data" );
290 msgSvc() << "Opened file " << m_pfn << " in mode CREATE. [" << m_fid << "]" << endmsg;
291 m_params.emplace_back( "PFN", m_pfn );
292 if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
293 makeTool();
294 break;
295 case RECREATE:
296 resetAge();
297 m_file.reset( TFile::Open( spec.c_str(), "RECREATE", "Root event data", compress ) );
298#if ROOT_HAS_630_FWD_COMPAT
299 if ( m_file && m_setup->root630ForwardCompatibility ) m_file->SetBit( TFile::k630forwardCompatibility );
300#endif
301 msgSvc() << "Opened file " << m_pfn << " in mode RECREATE. [" << m_fid << "]" << endmsg;
302 m_refs = new TTree( "Refs", "Root reference data" );
303 m_params.emplace_back( "PFN", m_pfn );
304 if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
305 makeTool();
306 break;
307 case UPDATE:
308 resetAge();
309 m_file.reset( TFile::Open( spec.c_str(), "UPDATE", "Root event data", compress ) );
310 msgSvc() << "Opened file " << m_pfn << " in mode UPDATE. [" << m_fid << "]" << endmsg;
311 if ( m_file && !m_file->IsZombie() ) {
312 if ( makeTool() ) {
313 StatusCode sc = m_tool->readRefs();
314 sc.ignore();
315 if ( sc == Status::ROOT_READ_ERROR ) {
316 IIncidentSvc* inc = m_setup->incidentSvc();
317 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
318 }
319 return sc;
320 }
321 TDirectory::TContext ctxt( m_file.get() );
322 m_refs = new TTree( "Refs", "Root reference data" );
323 makeTool();
324 return StatusCode::SUCCESS;
325 }
326 break;
327 default:
328 m_refs = nullptr;
329 m_file.reset();
330 return StatusCode::FAILURE;
331 }
333}
334
337 if ( m_file ) {
338 if ( !m_file->IsZombie() ) {
339 if ( m_file->IsWritable() ) {
340 msgSvc() << MSG::DEBUG;
341 TDirectory::TContext ctxt( m_file.get() );
342 if ( m_refs ) {
343 if ( !m_tool->saveRefs().isSuccess() ) badWriteError( "Saving References" );
344 if ( m_refs->Write() < 0 ) badWriteError( "Write Reference branch" );
345 }
346 for ( auto& i : m_sections ) {
347 if ( i.second ) {
348 if ( i.second->Write() < 0 ) badWriteError( "Write section:" + i.first );
349 msgSvc() << "Disconnect section " << i.first << " " << i.second->GetName() << endmsg;
350 }
351 }
352 m_sections.clear();
353 }
354 msgSvc() << MSG::DEBUG;
355 if ( msgSvc().isActive() ) m_file->ls();
356 msgSvc() << MSG::VERBOSE;
357 if ( msgSvc().isActive() ) m_file->Print();
358 m_file->Close();
359 }
360 msgSvc() << MSG::DEBUG << "Disconnected file " << m_pfn << " " << m_file->GetName() << endmsg;
361 m_file.reset();
362 m_tool.reset();
363 }
364 return StatusCode::SUCCESS;
365}
366
368TTree* RootDataConnection::getSection( std::string_view section, bool create ) {
369 auto it = m_sections.find( section );
370 TTree* t = ( it != m_sections.end() ? it->second : nullptr );
371 if ( !t ) {
372 t = (TTree*)m_file->Get( std::string{ section }.c_str() );
373 if ( !t && create ) {
374 TDirectory::TContext ctxt( m_file.get() );
375 t = new TTree( std::string{ section }.c_str(), "Root data for Gaudi" );
376 }
377 if ( t ) {
378 int cacheSize = m_setup->cacheSize;
379 if ( create ) {
380 // t->SetAutoFlush(100);
381 }
382 if ( section == m_setup->loadSection && cacheSize > -2 ) {
383 MsgStream& msg = msgSvc();
384 int learnEntries = m_setup->learnEntries;
385 t->SetCacheSize( cacheSize );
386 t->SetCacheLearnEntries( learnEntries );
387 msg << MSG::DEBUG;
388 if ( create ) {
389 msg << "Tree:" << section << "Setting up tree cache:" << cacheSize << endmsg;
390 } else {
391 const StringVec& vB = m_setup->vetoBranches;
392 const StringVec& cB = m_setup->cacheBranches;
393 msg << "Tree:" << section << " Setting up tree cache:" << cacheSize << " Add all branches." << endmsg;
394 msg << "Tree:" << section << " Learn for " << learnEntries << " entries." << endmsg;
395
396 if ( cB.empty() && vB.empty() ) {
397 msg << "Adding (default) all branches to tree cache." << endmsg;
398 t->AddBranchToCache( "*", kTRUE );
399 }
400 if ( cB.size() == 1 && cB[0] == "*" ) {
401 msg << "Adding all branches to tree cache according to option \"CacheBranches\"." << endmsg;
402 t->AddBranchToCache( "*", kTRUE );
403 } else {
404 for ( TIter it( t->GetListOfBranches() ); it.Next(); ) {
405 const char* n = ( (TNamed*)( *it ) )->GetName();
406 bool add = false, veto = false;
407 for ( const auto& i : cB ) {
408 if ( !match_wild( n, ( i ).c_str() ) ) continue;
409 add = true;
410 break;
411 }
412 for ( auto i = vB.cbegin(); !add && i != vB.cend(); ++i ) {
413 if ( !match_wild( n, ( *i ).c_str() ) ) continue;
414 veto = true;
415 break;
416 }
417 if ( add && !veto ) {
418 msg << "Add " << n << " to branch cache." << endmsg;
419 t->AddBranchToCache( n, kTRUE );
420 } else {
421 msg << "Do not cache branch " << n << endmsg;
422 }
423 }
424 }
425 }
426 }
427 m_sections[std::string{ section }] = t;
428 } else {
429 // in some rare cases we do have the entry we expect, but we cannot read it
430 // https://gitlab.cern.ch/gaudi/Gaudi/-/issues/301
431 auto key = m_file->GetKey( std::string{ section }.c_str() );
432 if ( key ) {
433 incidentSvc()->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) );
434 msgSvc() << MSG::ERROR << std::format( "failed to get TTree '{}' in {}", section, pfn() ) << endmsg;
435 }
436 }
437 }
438 return t;
439}
440
442TBranch* RootDataConnection::getBranch( std::string_view section, std::string_view branch_name, TClass* cl, void* ptr,
443 int buff_siz, int split_lvl ) {
444 string n = std::string{ branch_name };
445 std::replace_if(
446 begin( n ), end( n ), []( const char c ) { return !isalnum( c ); }, '_' );
447 n += ".";
448 TTree* t = getSection( section, true );
449 TBranch* b = t->GetBranch( n.c_str() );
450 if ( !b && cl && m_file->IsWritable() ) {
451 b = t->Branch( n.c_str(), cl->GetName(), (void*)( ptr ? &ptr : nullptr ), buff_siz, split_lvl );
452 }
453 if ( !b ) b = t->GetBranch( std::string{ branch_name }.c_str() );
454 if ( b ) b->SetAutoDelete( kFALSE );
455 return b;
456}
457
459int RootDataConnection::makeLink( std::string_view p ) {
460 auto ip = std::find( std::begin( m_links ), std::end( m_links ), p );
461 if ( ip != std::end( m_links ) ) return std::distance( std::begin( m_links ), ip );
462 m_links.push_back( std::string{ p } );
463 return m_links.size() - 1;
464}
465
467CSTR RootDataConnection::getDb( int which ) const {
468 if ( ( which >= 0 ) && ( size_t( which ) < m_dbs.size() ) ) {
469 if ( *( m_dbs.begin() + which ) == s_local ) return m_fid;
470 return *( m_dbs.begin() + which );
471 }
472 return s_empty;
473}
474
476CSTR RootDataConnection::empty() const { return s_empty; }
477
479pair<int, unsigned long> RootDataConnection::saveObj( std::string_view section, std::string_view cnt, TClass* cl,
480 DataObject* pObj, int minBufferSize, int maxBufferSize,
481 int approxEventsPerBasket, int split_lvl, bool fill ) {
482 DataObjectPush push( pObj );
483 return save( section, cnt, cl, pObj, minBufferSize, maxBufferSize, approxEventsPerBasket, split_lvl, fill );
484}
485
487pair<int, unsigned long> RootDataConnection::save( std::string_view section, std::string_view cnt, TClass* cl,
488 void* pObj, int minBufferSize, int maxBufferSize,
489 int approxEventsPerBasket, int split_lvl, bool fill_missing ) {
490 split_lvl = 0;
491 TBranch* b = getBranch( section, cnt, cl, pObj ? &pObj : nullptr, minBufferSize, split_lvl );
492 if ( b ) {
493 Long64_t evt = b->GetEntries();
494 // msgSvc() << MSG::DEBUG << cnt.c_str() << " Obj:" << (void*)pObj
495 // << " Split:" << split_lvl << " Buffer size:" << minBufferSize << endl;
496 bool set_buffer_size = ( evt == 0 );
497 if ( fill_missing ) {
498 Long64_t num, nevt = b->GetTree()->GetEntries();
499 if ( nevt > evt ) {
500 set_buffer_size = true;
501 b->SetAddress( nullptr );
502 num = nevt - evt;
503 while ( num > 0 ) {
504 b->Fill();
505 --num;
506 }
507 msgSvc() << MSG::DEBUG << "Added " << long( nevt - evt ) << " / Tree: " << nevt
508 << " / Branch: " << b->GetEntries() + 1 << " NULL entries to:" << cnt << endmsg;
509 evt = b->GetEntries();
510 }
511 }
512 if ( set_buffer_size ) {
513 auto dummy_file = make_unique<TMemFile>( "dummy.root", "CREATE" );
514 auto dummy_tree = make_unique<TTree>( "DummyTree", "DummyTree", split_lvl, dummy_file->GetDirectory( "/" ) );
515 TBranch* dummy_branch = dummy_tree->Branch( "DummyBranch", cl->GetName(), &pObj, minBufferSize, split_lvl );
516 Int_t nWritten = dummy_branch->Fill();
517 if ( nWritten < 0 ) return { nWritten, evt };
518 Int_t newBasketSize = nWritten * approxEventsPerBasket;
519 // Ensure that newBasketSize doesn't wrap around
520 if ( std::numeric_limits<Int_t>::max() / approxEventsPerBasket < nWritten ) {
521 newBasketSize = std::numeric_limits<Int_t>::max();
522 }
523 b->SetBasketSize( std::min( maxBufferSize, std::max( minBufferSize, newBasketSize ) ) );
524 msgSvc() << MSG::DEBUG << "Setting basket size to " << newBasketSize << " for " << cnt << endmsg;
525 }
526 b->SetAddress( &pObj );
527 return { b->Fill(), evt };
528 }
529 if ( pObj ) { msgSvc() << MSG::ERROR << "Failed to access branch " << m_name << "/" << cnt << endmsg; }
530 return { -1, ~0 };
531}
532
534int RootDataConnection::loadObj( std::string_view section, std::string_view cnt, unsigned long entry,
535 DataObject*& pObj ) {
536 TBranch* b = getBranch( section, cnt );
537 if ( b ) {
538 TClass* cl = gROOT->GetClass( b->GetClassName(), kTRUE );
539 if ( cl ) {
540 int nb = -1;
541 pObj = (DataObject*)cl->New();
542 {
543 DataObjectPush push( pObj );
544 b->SetAddress( &pObj );
545 if ( section == m_setup->loadSection ) {
546 TTree* t = b->GetTree();
547 if ( Long64_t( entry ) != t->GetReadEntry() ) { t->LoadTree( Long64_t( entry ) ); }
548 }
549 nb = b->GetEntry( entry );
550 msgSvc() << MSG::VERBOSE;
551 if ( msgSvc().isActive() ) {
552 msgSvc() << "Load [" << entry << "] --> " << section << ":" << cnt << " " << nb << " bytes." << endmsg;
553 }
554 if ( nb < 0 ) { // This is definitely an error...ROOT says if reads fail, -1 is issued.
555 IIncidentSvc* inc = m_setup->incidentSvc();
556 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
557 } else if ( nb == 0 && pObj->clID() == CLID_DataObject ) {
558 TFile* f = b->GetFile();
559 int vsn = f->GetVersion();
560 if ( vsn < 52400 ) {
561 // For Gaudi v21r5 (ROOT 5.24.00b) DataObject::m_version was not written!
562 // Still this call be well be successful.
563 nb = 1;
564 } else if ( vsn > 1000000 && ( vsn % 1000000 ) < 52400 ) {
565 // dto. Some POOL files have for unknown reasons a version
566 // not according to ROOT standards. Hack this explicitly.
567 nb = 1;
568 }
569 }
570 if ( nb < 0 ) {
571 delete pObj;
572 pObj = nullptr;
573 }
574 }
575 return nb;
576 }
577 }
578 return -1;
579}
580
582int RootDataConnection::loadRefs( std::string_view section, std::string_view cnt, unsigned long entry,
583 RootObjectRefs& refs ) {
584 int nbytes = m_tool->loadRefs( section, cnt, entry, refs );
585 if ( nbytes < 0 ) {
586 // This is definitely an error:
587 // -- Either branch not present at all or
588 // -- ROOT I/O error, which issues -1
589 IIncidentSvc* inc = m_setup->incidentSvc();
590 if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
591 }
592 return nbytes;
593}
594
596pair<const RootRef*, const RootDataConnection::ContainerSection*>
597RootDataConnection::getMergeSection( std::string_view container, int entry ) const {
598 // size_t idx = cont.find('/',1);
599 // string container = cont[0]=='/' ? cont.substr(1,idx==string::npos?idx:idx-1) : cont;
600 auto i = m_mergeSects.find( container );
601 if ( i != m_mergeSects.end() ) {
602 size_t cnt = 0;
603 const ContainerSections& s = ( *i ).second;
604 for ( auto j = s.cbegin(); j != s.cend(); ++j, ++cnt ) {
605 const ContainerSection& c = *j;
606 if ( entry >= c.start && entry < ( c.start + c.length ) ) {
607 if ( m_linkSects.size() > cnt ) {
608 if ( msgSvc().isActive() ) {
609 msgSvc() << MSG::VERBOSE << "MergeSection for:" << container << " [" << entry << "]" << endmsg
610 << "FID:" << m_fid << " -> PFN:" << m_pfn << endmsg;
611 }
612 return { &( m_linkSects[cnt] ), &c };
613 }
614 }
615 }
616 }
617 msgSvc() << MSG::DEBUG << "Return INVALID MergeSection for:" << container << " [" << entry << "]" << endmsg
618 << "FID:" << m_fid << " -> PFN:" << m_pfn << endmsg;
619 return { nullptr, nullptr };
620}
621
624 IOpaqueAddress* pA = pR.address();
625 makeRef( pR.name(), pA->clID(), pA->svcType(), pA->par()[0], pA->par()[1], -1, ref );
626}
627
629void RootDataConnection::makeRef( std::string_view name, long clid, int tech, std::string_view dbase,
630 std::string_view cnt, int entry, RootRef& ref ) {
631 auto db = ( dbase == m_fid ? std::string_view{ s_local } : dbase );
632 ref.entry = entry;
633
634 int cdb = -1;
635 if ( !db.empty() ) {
636 auto idb = std::find_if( m_dbs.begin(), m_dbs.end(), [&]( const std::string& i ) { return i == db; } );
637 cdb = std::distance( m_dbs.begin(), idb );
638 if ( idb == m_dbs.end() ) m_dbs.push_back( std::string{ db } );
639 }
640
641 int ccnt = -1;
642 if ( !cnt.empty() ) {
643 auto icnt = std::find_if( m_conts.begin(), m_conts.end(), [&]( const std::string& i ) { return i == cnt; } );
644 ccnt = std::distance( m_conts.begin(), icnt );
645 if ( icnt == m_conts.end() ) m_conts.push_back( std::string{ cnt } );
646 }
647
648 int clnk = -1;
649 if ( !name.empty() ) {
650 auto ilnk = std::find_if( m_links.begin(), m_links.end(), [&]( const std::string& i ) { return i == name; } );
651 clnk = std::distance( m_links.begin(), ilnk );
652 if ( ilnk == m_links.end() ) m_links.push_back( std::string{ name } );
653 }
654
655 ref.dbase = cdb;
656 ref.container = ccnt;
657 ref.link = clnk;
658 ref.clid = clid;
659 ref.svc = tech;
660}
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.
abstraction layer probably not needed anymore as the only implementation if RootTool
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.
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