The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
RootDataConnection.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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
20 #include "GaudiKernel/DataObject.h"
23 #include "GaudiKernel/IRegistry.h"
24 #include "GaudiKernel/Incident.h"
26 #include "GaudiKernel/MsgStream.h"
27 #include "GaudiKernel/strcasecmp.h"
28 #include "RootUtils.h"
29 // ROOT include files
30 #include "TBranch.h"
31 #include "TClass.h"
32 #include "TFile.h"
33 #include "TLeaf.h"
34 #include "TMemFile.h"
35 #include "TROOT.h"
36 #include "TTree.h"
37 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
38 # include "Compression.h"
39 static int s_compressionLevel = ROOT::CompressionSettings( ROOT::kLZMA, 4 );
40 #else
41 static int s_compressionLevel = 1;
42 #endif
43 
44 // C/C++ include files
45 #include <limits>
46 #include <numeric>
47 #include <stdexcept>
48 
49 using namespace Gaudi;
50 using namespace std;
51 typedef const string& CSTR;
52 
53 static const string s_empty;
54 static const string s_local = "<localDB>";
55 
56 #ifdef __POOL_COMPATIBILITY
57 # include "PoolTool.h"
58 #endif
59 #include "RootTool.h"
60 
61 namespace {
62  std::array<char, 256> init_table() {
64  std::iota( std::begin( table ), std::end( table ), 0 );
65  return table;
66  }
67 
68  struct RootDataConnectionCategory : StatusCode::Category {
69  const char* name() const override { return "RootDataConnection"; }
70 
71  bool isRecoverable( StatusCode::code_t ) const override { return false; }
72 
73  std::string message( StatusCode::code_t code ) const override {
74  switch ( static_cast<RootDataConnection::Status>( code ) ) {
76  return "ROOT_READ_ERROR";
78  return "ROOT_OPEN_ERROR";
79  default:
80  return StatusCode::default_category().message( code );
81  }
82  }
83  };
84 
85  static bool match_wild( const char* str, const char* pat ) {
86  //
87  // Credits: Code from Alessandro Felice Cantatore.
88  //
89  static const auto table = init_table();
90  const char * s, *p;
91  bool star = false;
92  loopStart:
93  for ( s = str, p = pat; *s; ++s, ++p ) {
94  switch ( *p ) {
95  case '?':
96  if ( *s == '.' ) goto starCheck;
97  break;
98  case '*':
99  star = true;
100  str = s, pat = p;
101  do { ++pat; } while ( *pat == '*' );
102  if ( !*pat ) return true;
103  goto loopStart;
104  default:
105  if ( table[*s] != table[*p] ) goto starCheck;
106  break;
107  } /* endswitch */
108  } /* endfor */
109  while ( *p == '*' ) ++p;
110  return ( !*p );
111 
112  starCheck:
113  if ( !star ) return false;
114  str++;
115  goto loopStart;
116  }
117 } // namespace
118 
119 STATUSCODE_ENUM_IMPL( Gaudi::RootDataConnection::Status, RootDataConnectionCategory )
120 
121 StatusCode RootConnectionSetup::setCompression( std::string_view compression ) {
123 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
124  int res = 0, level = ROOT::CompressionSettings( ROOT::kLZMA, 6 );
125  auto idx = compression.find( ':' );
126  if ( idx != string::npos ) {
127  auto alg = compression.substr( 0, idx );
128  ROOT::ECompressionAlgorithm alg_code = ROOT::kUseGlobalSetting;
129  if ( alg.size() == 4 && strncasecmp( alg.data(), "ZLIB", 4 ) == 0 )
130  alg_code = ROOT::kZLIB;
131  else if ( alg.size() == 4 && strncasecmp( alg.data(), "LZMA", 4 ) == 0 )
132  alg_code = ROOT::kLZMA;
133  else if ( alg.size() == 3 && strncasecmp( alg.data(), "LZ4", 3 ) == 0 )
134  alg_code = ROOT::kLZ4;
135 # if ROOT_VERSION_CODE >= ROOT_VERSION( 6, 20, 0 )
136  else if ( alg.size() == 4 && strncasecmp( alg.data(), "ZSTD", 4 ) == 0 )
137  alg_code = ROOT::kZSTD;
138 # endif
139  else
140  throw runtime_error( "ERROR: request to set unknown ROOT compression algorithm:" + std::string{ alg } );
141  res = ::sscanf( std::string{ compression.substr( idx + 1 ) }.c_str(), "%d",
142  &level ); // TODO: use C++17 std::from_chars instead...
143  if ( res == 1 ) {
144  s_compressionLevel = ROOT::CompressionSettings( alg_code, level );
145  return StatusCode::SUCCESS;
146  }
147  throw runtime_error( "ERROR: request to set unknown ROOT compression level:" +
148  std::string{ compression.substr( idx + 1 ) } );
149  } else if ( 1 == ::sscanf( std::string{ compression }.c_str(), "%d", &level ) ) { // TODO: use C++17 std::from_chars
150  // instead
151  s_compressionLevel = level;
152  return StatusCode::SUCCESS;
153  }
154  throw runtime_error( "ERROR: request to set unknown ROOT compression mechanism:" + std::string{ compression } );
155 #else
156  if ( !compression.empty() ) {}
157  return StatusCode::SUCCESS;
158 #endif
159 }
160 
162 int RootConnectionSetup::compression() { return s_compressionLevel; }
163 
165 void RootConnectionSetup::setMessageSvc( MsgStream* m ) { m_msgSvc.reset( m ); }
166 
168 void RootConnectionSetup::setIncidentSvc( IIncidentSvc* s ) { m_incidentSvc.reset( s ); }
169 
171 RootDataConnection::RootDataConnection( const IInterface* owner, std::string_view fname,
173  : IDataConnection( owner, std::string{ fname } )
174  , m_setup( std::move( setup ) ) { // 01234567890123456789012345678901234567890
175  // Check if FID: A82A3BD8-7ECB-DC11-8DC0-000423D950B0
176  if ( fname.size() == 36 && fname[8] == '-' && fname[13] == '-' && fname[18] == '-' && fname[23] == '-' ) {
177  m_name = "FID:";
178  m_name.append( fname.data(), fname.size() );
179  }
180  m_age = 0;
181  m_file.reset();
182  addClient( owner );
183 }
184 
186 void RootDataConnection::addClient( const IInterface* client ) { m_clients.insert( client ); }
187 
190  auto i = m_clients.find( client );
191  if ( i != m_clients.end() ) m_clients.erase( i );
192  return m_clients.size();
193 }
194 
196 bool RootDataConnection::lookupClient( const IInterface* client ) const {
197  auto i = m_clients.find( client );
198  return i != m_clients.end();
199 }
200 
202 void RootDataConnection::badWriteError( std::string_view msg ) const {
203  msgSvc() << MSG::ERROR << "File:" << fid() << "Failed action:" << msg << endmsg;
204 }
205 
207 void RootDataConnection::saveStatistics( std::string_view statisticsFile ) {
208  if ( m_statistics ) {
209  m_statistics->Print();
210  if ( !statisticsFile.empty() ) m_statistics->SaveAs( std::string{ statisticsFile }.c_str() );
212  }
213 }
214 
216 void RootDataConnection::enableStatistics( std::string_view section ) {
217  if ( m_statistics ) {
218  TTree* t = getSection( section, false );
219  if ( t ) {
220  m_statistics.reset( new TTreePerfStats( ( std::string{ section } + "_ioperf" ).c_str(), t ) );
221  return;
222  }
223  msgSvc() << MSG::WARNING << "Failed to enable perfstats for tree:" << section << endmsg;
224  return;
225  }
226  msgSvc() << MSG::INFO << "Perfstats are ALREADY ENABLED." << endmsg;
227 }
228 
231  if ( !m_refs ) m_refs = (TTree*)m_file->Get( "Refs" );
232  if ( m_refs ) m_tool.reset( new RootTool( this ) );
233 #ifdef __POOL_COMPATIBILITY
234  else if ( m_file->Get( "##Links" ) != nullptr )
235  m_tool.reset( new PoolTool( this ) );
236 #endif
237  else
238  m_tool.reset();
239  return m_tool.get();
240 }
241 
244  m_file.reset( TFile::Open( m_pfn.c_str() ) );
245  if ( !m_file || m_file->IsZombie() ) {
246  m_file.reset();
247  return StatusCode::FAILURE;
248  }
250  msgSvc() << MSG::DEBUG << "Opened file " << m_pfn << " in mode READ. [" << m_fid << "]" << endmsg << MSG::DEBUG;
251  if ( msgSvc().isActive() ) m_file->ls();
252  msgSvc() << MSG::VERBOSE;
253  if ( msgSvc().isActive() ) m_file->Print();
254  if ( makeTool() ) {
255  sc = m_tool->readRefs();
256  sc.ignore();
257 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
258  if ( sc == Status::ROOT_READ_ERROR ) {
259  IIncidentSvc* inc = m_setup->incidentSvc();
260  if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
261  }
262 #endif
263  }
264  if ( !sc.isSuccess() ) return sc;
265  bool need_fid = m_fid == m_pfn;
266  string fid = m_fid;
267  m_mergeFIDs.clear();
268  for ( auto& elem : m_params ) {
269  if ( elem.first == "FID" ) {
270  m_mergeFIDs.push_back( elem.second );
271  if ( elem.second != m_fid ) {
272  msgSvc() << MSG::DEBUG << "Check FID param:" << elem.second << endmsg;
273  // if ( m_fid == m_pfn ) {
274  m_fid = elem.second;
275  //}
276  }
277  }
278  }
279  if ( !need_fid && fid != m_fid ) {
280  msgSvc() << MSG::ERROR << "FID mismatch:" << fid << "(Catalog) != " << m_fid << "(file)" << endmsg
281  << "for PFN:" << m_pfn << endmsg;
282  return StatusCode::FAILURE;
283  }
284  msgSvc() << MSG::DEBUG << "Using FID " << m_fid << " from params table...." << endmsg << "for PFN:" << m_pfn
285  << endmsg;
286  return sc;
287 }
288 
291  int compress = RootConnectionSetup::compression();
292  msgSvc() << MSG::DEBUG;
293  std::string spec = m_pfn;
294  if ( m_setup->produceReproducibleFiles ) spec += "?reproducible"; // https://root.cern.ch/doc/master/classTFile.html
295  switch ( typ ) {
296  case CREATE:
297  resetAge();
298  m_file.reset( TFile::Open( spec.c_str(), "CREATE", "Root event data", compress ) );
299  m_refs = new TTree( "Refs", "Root reference data" );
300  msgSvc() << "Opened file " << m_pfn << " in mode CREATE. [" << m_fid << "]" << endmsg;
301  m_params.emplace_back( "PFN", m_pfn );
302  if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
303  makeTool();
304  break;
305  case RECREATE:
306  resetAge();
307  m_file.reset( TFile::Open( spec.c_str(), "RECREATE", "Root event data", compress ) );
308  msgSvc() << "Opened file " << m_pfn << " in mode RECREATE. [" << m_fid << "]" << endmsg;
309  m_refs = new TTree( "Refs", "Root reference data" );
310  m_params.emplace_back( "PFN", m_pfn );
311  if ( m_fid != m_pfn ) { m_params.emplace_back( "FID", m_fid ); }
312  makeTool();
313  break;
314  case UPDATE:
315  resetAge();
316  m_file.reset( TFile::Open( spec.c_str(), "UPDATE", "Root event data", compress ) );
317  msgSvc() << "Opened file " << m_pfn << " in mode UPDATE. [" << m_fid << "]" << endmsg;
318  if ( m_file && !m_file->IsZombie() ) {
319  if ( makeTool() ) {
320  StatusCode sc = m_tool->readRefs();
321  sc.ignore();
322  if ( sc == Status::ROOT_READ_ERROR ) {
323 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
324  IIncidentSvc* inc = m_setup->incidentSvc();
325  if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
326 #endif
327  }
328  return sc;
329  }
330  TDirectory::TContext ctxt( m_file.get() );
331  m_refs = new TTree( "Refs", "Root reference data" );
332  makeTool();
333  return StatusCode::SUCCESS;
334  }
335  break;
336  default:
337  m_refs = nullptr;
338  m_file.reset();
339  return StatusCode::FAILURE;
340  }
342 }
343 
346  if ( m_file ) {
347  if ( !m_file->IsZombie() ) {
348  if ( m_file->IsWritable() ) {
349  msgSvc() << MSG::DEBUG;
350  TDirectory::TContext ctxt( m_file.get() );
351  if ( m_refs ) {
352  if ( !m_tool->saveRefs().isSuccess() ) badWriteError( "Saving References" );
353  if ( m_refs->Write() < 0 ) badWriteError( "Write Reference branch" );
354  }
355  for ( auto& i : m_sections ) {
356  if ( i.second ) {
357  if ( i.second->Write() < 0 ) badWriteError( "Write section:" + i.first );
358  msgSvc() << "Disconnect section " << i.first << " " << i.second->GetName() << endmsg;
359  }
360  }
361  m_sections.clear();
362  }
363  msgSvc() << MSG::DEBUG;
364  if ( msgSvc().isActive() ) m_file->ls();
365  msgSvc() << MSG::VERBOSE;
366  if ( msgSvc().isActive() ) m_file->Print();
367  m_file->Close();
368  }
369  msgSvc() << MSG::DEBUG << "Disconnected file " << m_pfn << " " << m_file->GetName() << endmsg;
370  m_file.reset();
371  m_tool.reset();
372  }
373  return StatusCode::SUCCESS;
374 }
375 
377 TTree* RootDataConnection::getSection( std::string_view section, bool create ) {
378  auto it = m_sections.find( section );
379  TTree* t = ( it != m_sections.end() ? it->second : nullptr );
380  if ( !t ) {
381  t = (TTree*)m_file->Get( std::string{ section }.c_str() );
382  if ( !t && create ) {
383  TDirectory::TContext ctxt( m_file.get() );
384  t = new TTree( std::string{ section }.c_str(), "Root data for Gaudi" );
385  }
386  if ( t ) {
387  int cacheSize = m_setup->cacheSize;
388  if ( create ) {
389  // t->SetAutoFlush(100);
390  }
391  if ( section == m_setup->loadSection && cacheSize > -2 ) {
392  MsgStream& msg = msgSvc();
393  int learnEntries = m_setup->learnEntries;
394  t->SetCacheSize( cacheSize );
395  t->SetCacheLearnEntries( learnEntries );
396  msg << MSG::DEBUG;
397  if ( create ) {
398  msg << "Tree:" << section << "Setting up tree cache:" << cacheSize << endmsg;
399  } else {
400  const StringVec& vB = m_setup->vetoBranches;
401  const StringVec& cB = m_setup->cacheBranches;
402  msg << "Tree:" << section << " Setting up tree cache:" << cacheSize << " Add all branches." << endmsg;
403  msg << "Tree:" << section << " Learn for " << learnEntries << " entries." << endmsg;
404 
405  if ( cB.empty() && vB.empty() ) {
406  msg << "Adding (default) all branches to tree cache." << endmsg;
407  t->AddBranchToCache( "*", kTRUE );
408  }
409  if ( cB.size() == 1 && cB[0] == "*" ) {
410  msg << "Adding all branches to tree cache according to option \"CacheBranches\"." << endmsg;
411  t->AddBranchToCache( "*", kTRUE );
412  } else {
413  for ( TIter it( t->GetListOfBranches() ); it.Next(); ) {
414  const char* n = ( (TNamed*)( *it ) )->GetName();
415  bool add = false, veto = false;
416  for ( const auto& i : cB ) {
417  if ( !match_wild( n, ( i ).c_str() ) ) continue;
418  add = true;
419  break;
420  }
421  for ( auto i = vB.cbegin(); !add && i != vB.cend(); ++i ) {
422  if ( !match_wild( n, ( *i ).c_str() ) ) continue;
423  veto = true;
424  break;
425  }
426  if ( add && !veto ) {
427  msg << "Add " << n << " to branch cache." << endmsg;
428  t->AddBranchToCache( n, kTRUE );
429  } else {
430  msg << "Do not cache branch " << n << endmsg;
431  }
432  }
433  }
434  }
435  }
436  m_sections[std::string{ section }] = t;
437  }
438  }
439  return t;
440 }
441 
443 TBranch* RootDataConnection::getBranch( std::string_view section, std::string_view branch_name, TClass* cl, void* ptr,
444  int buff_siz, int split_lvl ) {
445  string n = std::string{ branch_name };
447  begin( n ), end( n ), []( const char c ) { return !isalnum( c ); }, '_' );
448  n += ".";
449  TTree* t = getSection( section, true );
450  TBranch* b = t->GetBranch( n.c_str() );
451  if ( !b && cl && m_file->IsWritable() ) {
452  b = t->Branch( n.c_str(), cl->GetName(), (void*)( ptr ? &ptr : nullptr ), buff_siz, split_lvl );
453  }
454  if ( !b ) b = t->GetBranch( std::string{ branch_name }.c_str() );
455  if ( b ) b->SetAutoDelete( kFALSE );
456  return b;
457 }
458 
460 int RootDataConnection::makeLink( std::string_view p ) {
461  auto ip = std::find( std::begin( m_links ), std::end( m_links ), p );
462  if ( ip != std::end( m_links ) ) return std::distance( std::begin( m_links ), ip );
463  m_links.push_back( std::string{ p } );
464  return m_links.size() - 1;
465 }
466 
469  if ( ( which >= 0 ) && ( size_t( which ) < m_dbs.size() ) ) {
470  if ( *( m_dbs.begin() + which ) == s_local ) return m_fid;
471  return *( m_dbs.begin() + which );
472  }
473  return s_empty;
474 }
475 
477 CSTR RootDataConnection::empty() const { return s_empty; }
478 
480 pair<int, unsigned long> RootDataConnection::saveObj( std::string_view section, std::string_view cnt, TClass* cl,
481  DataObject* pObj, int minBufferSize, int maxBufferSize,
482  int approxEventsPerBasket, int split_lvl, bool fill ) {
483  DataObjectPush push( pObj );
484  return save( section, cnt, cl, pObj, minBufferSize, maxBufferSize, approxEventsPerBasket, split_lvl, fill );
485 }
486 
488 pair<int, unsigned long> RootDataConnection::save( std::string_view section, std::string_view cnt, TClass* cl,
489  void* pObj, int minBufferSize, int maxBufferSize,
490  int approxEventsPerBasket, int split_lvl, bool fill_missing ) {
491  split_lvl = 0;
492  TBranch* b = getBranch( section, cnt, cl, pObj ? &pObj : nullptr, minBufferSize, split_lvl );
493  if ( b ) {
494  Long64_t evt = b->GetEntries();
495  // msgSvc() << MSG::DEBUG << cnt.c_str() << " Obj:" << (void*)pObj
496  // << " Split:" << split_lvl << " Buffer size:" << minBufferSize << endl;
497  bool set_buffer_size = ( evt == 0 );
498  if ( fill_missing ) {
499  Long64_t num, nevt = b->GetTree()->GetEntries();
500  if ( nevt > evt ) {
501  set_buffer_size = true;
502  b->SetAddress( nullptr );
503  num = nevt - evt;
504  while ( num > 0 ) {
505  b->Fill();
506  --num;
507  }
508  msgSvc() << MSG::DEBUG << "Added " << long( nevt - evt ) << " / Tree: " << nevt
509  << " / Branch: " << b->GetEntries() + 1 << " NULL entries to:" << cnt << endmsg;
510  evt = b->GetEntries();
511  }
512  }
513  if ( set_buffer_size ) {
514  auto dummy_file = make_unique<TMemFile>( "dummy.root", "CREATE" );
515  auto dummy_tree = make_unique<TTree>( "DummyTree", "DummyTree", split_lvl, dummy_file->GetDirectory( "/" ) );
516  TBranch* dummy_branch = dummy_tree->Branch( "DummyBranch", cl->GetName(), &pObj, minBufferSize, split_lvl );
517  Int_t nWritten = dummy_branch->Fill();
518  if ( nWritten < 0 ) return { nWritten, evt };
519  Int_t newBasketSize = nWritten * approxEventsPerBasket;
520  // Ensure that newBasketSize doesn't wrap around
521  if ( std::numeric_limits<Int_t>::max() / approxEventsPerBasket < nWritten ) {
522  newBasketSize = std::numeric_limits<Int_t>::max();
523  }
524  b->SetBasketSize( std::min( maxBufferSize, std::max( minBufferSize, newBasketSize ) ) );
525  msgSvc() << MSG::DEBUG << "Setting basket size to " << newBasketSize << " for " << cnt << endmsg;
526  }
527  b->SetAddress( &pObj );
528  return { b->Fill(), evt };
529  }
530  if ( pObj ) { msgSvc() << MSG::ERROR << "Failed to access branch " << m_name << "/" << cnt << endmsg; }
531  return { -1, ~0 };
532 }
533 
535 int RootDataConnection::loadObj( std::string_view section, std::string_view cnt, unsigned long entry,
536  DataObject*& pObj ) {
537  TBranch* b = getBranch( section, cnt );
538  if ( b ) {
539  TClass* cl = gROOT->GetClass( b->GetClassName(), kTRUE );
540  if ( cl ) {
541  int nb = -1;
542  pObj = (DataObject*)cl->New();
543  {
544  DataObjectPush push( pObj );
545  b->SetAddress( &pObj );
546  if ( section == m_setup->loadSection ) {
547  TTree* t = b->GetTree();
548  if ( Long64_t( entry ) != t->GetReadEntry() ) { t->LoadTree( Long64_t( entry ) ); }
549  }
550  nb = b->GetEntry( entry );
551  msgSvc() << MSG::VERBOSE;
552  if ( msgSvc().isActive() ) {
553  msgSvc() << "Load [" << entry << "] --> " << section << ":" << cnt << " " << nb << " bytes." << endmsg;
554  }
555  if ( nb < 0 ) { // This is definitely an error...ROOT says if reads fail, -1 is issued.
556 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
557  IIncidentSvc* inc = m_setup->incidentSvc();
558  if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
559 #endif
560  } else if ( nb == 0 && pObj->clID() == CLID_DataObject ) {
561  TFile* f = b->GetFile();
562  int vsn = f->GetVersion();
563  if ( vsn < 52400 ) {
564  // For Gaudi v21r5 (ROOT 5.24.00b) DataObject::m_version was not written!
565  // Still this call be well be successful.
566  nb = 1;
567  } else if ( vsn > 1000000 && ( vsn % 1000000 ) < 52400 ) {
568  // dto. Some POOL files have for unknown reasons a version
569  // not according to ROOT standards. Hack this explicitly.
570  nb = 1;
571  }
572  }
573  if ( nb < 0 ) {
574  delete pObj;
575  pObj = nullptr;
576  }
577  }
578  return nb;
579  }
580  }
581  return -1;
582 }
583 
585 int RootDataConnection::loadRefs( std::string_view section, std::string_view cnt, unsigned long entry,
586  RootObjectRefs& refs ) {
587  int nbytes = m_tool->loadRefs( section, cnt, entry, refs );
588 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 33, 0 )
589  if ( nbytes < 0 ) {
590  // This is definitely an error:
591  // -- Either branch not preesent at all or
592  // -- ROOT I/O error, which issues -1
593  IIncidentSvc* inc = m_setup->incidentSvc();
594  if ( inc ) { inc->fireIncident( Incident( pfn(), IncidentType::CorruptedInputFile ) ); }
595  }
596 #endif
597  return nbytes;
598 }
599 
602 RootDataConnection::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 
634 void 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 }
IOTest.evt
evt
Definition: IOTest.py:105
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::IDataConnection::name
const std::string & name() const
Connection name.
Definition: IIODataManager.h:59
GaudiHive.precedence.message
message
Definition: precedence.py:19
Gaudi::RootDataConnection::removeClient
size_t removeClient(const IInterface *client)
Remove client from this data source.
Definition: RootDataConnection.cpp:189
std::string
STL class.
std::shared_ptr
STL class.
Gaudi::RootDataConnection::m_clients
Clients m_clients
Client list.
Definition: RootDataConnection.h:186
Gaudi::RootDataConnection::enableStatistics
void enableStatistics(std::string_view section)
Enable TTreePerStats.
Definition: RootDataConnection.cpp:216
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
Gaudi::RootConnectionSetup::setMessageSvc
void setMessageSvc(MsgStream *m)
Set message service reference.
Definition: RootDataConnection.cpp:165
StatusCode::default_category
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
Definition: StatusCode.h:310
Gaudi::RootDataConnection::disconnect
StatusCode disconnect() override
Release data stream and release implementation dependent resources.
Definition: RootDataConnection.cpp:345
Gaudi::RootTool
Definition: RootTool.h:26
std::move
T move(T... args)
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
Gaudi::RootDataConnection::badWriteError
void badWriteError(std::string_view msg) const
Error handler when bad write statements occur.
Definition: RootDataConnection.cpp:202
Gaudi::IDataConnection
Definition: IIODataManager.h:34
std::pair
Properties.long
long
(c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations # # This software i...
Definition: Properties.py:15
gaudirun.s
string s
Definition: gaudirun.py:348
IOpaqueAddress
Definition: IOpaqueAddress.h:33
std::vector< std::string >
std::set::find
T find(T... args)
strcasecmp.h
std::set::size
T size(T... args)
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
Gaudi::IDataConnection::RECREATE
@ RECREATE
Definition: IIODataManager.h:49
Gaudi::IDataConnection::resetAge
void resetAge()
Reset age.
Definition: IIODataManager.h:71
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::RootDataConnection::connectRead
StatusCode connectRead() override
Open data stream in read mode.
Definition: RootDataConnection.cpp:243
Gaudi::RootDataConnection::m_file
std::unique_ptr< TFile > m_file
Reference to ROOT file.
Definition: RootDataConnection.h:166
std::unique_ptr::get
T get(T... args)
std::distance
T distance(T... args)
Gaudi::RootDataConnection::saveObj
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.
Definition: RootDataConnection.cpp:480
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
Gaudi::RootDataConnection::ContainerSection
Definition: RootDataConnection.h:121
Gaudi::RootDataConnection::m_params
ParamMap m_params
Parameter map for file parameters.
Definition: RootDataConnection.h:180
std::isalnum
T isalnum(T... args)
RootTool.h
std::sscanf
T sscanf(T... args)
gaudirun.c
c
Definition: gaudirun.py:527
IRegistry
Definition: IRegistry.h:32
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
RootDataConnection.h
std::unique_ptr::reset
T reset(T... args)
IIncidentSvc::fireIncident
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
Gaudi::RootDataConnection::m_dbs
StringVec m_dbs
Map containing external database file names (fids)
Definition: RootDataConnection.h:172
std::vector::clear
T clear(T... args)
Gaudi::RootObjectRefs
Definition: RootRefs.h:68
std::replace_if
T replace_if(T... args)
basic.alg
alg
Definition: basic.py:15
IIncidentSvc.h
std::vector::push_back
T push_back(T... args)
bug_34121.t
t
Definition: bug_34121.py:30
Gaudi::RootDataConnection::m_linkSects
LinkSections m_linkSects
Database link sections.
Definition: RootDataConnection.h:184
Gaudi::IDataConnection::CREATE
@ CREATE
Definition: IIODataManager.h:49
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
Gaudi::RootDataConnection::getDb
const std::string & getDb(int which) const
Access database/file name from saved index.
Definition: RootDataConnection.cpp:468
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
StatusCode::Category
Definition: StatusCode.h:78
Gaudi::RootDataConnection::m_mergeFIDs
StringVec m_mergeFIDs
Map containing merge FIDs.
Definition: RootDataConnection.h:178
Gaudi::RootDataConnection::m_setup
std::shared_ptr< RootConnectionSetup > m_setup
Reference to the setup structure.
Definition: RootDataConnection.h:162
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
ProduceConsume.j
j
Definition: ProduceConsume.py:101
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::RootDataConnection::Status::ROOT_READ_ERROR
@ ROOT_READ_ERROR
IOpaqueAddress.h
POOL_ROOT_StorageType
const long POOL_ROOT_StorageType
Definition: ClassID.h:78
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:746
Gaudi::RootDataConnection::Status::ROOT_OPEN_ERROR
@ ROOT_OPEN_ERROR
Gaudi::RootDataConnection::m_mergeSects
MergeSections m_mergeSects
Database section map for merged files.
Definition: RootDataConnection.h:182
std::string::c_str
T c_str(T... args)
POOL_ROOTKEY_StorageType
const long POOL_ROOTKEY_StorageType
Definition: ClassID.h:79
Gaudi::RootConnectionSetup::setIncidentSvc
void setIncidentSvc(IIncidentSvc *m)
Set incident service reference.
Definition: RootDataConnection.cpp:168
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
Gaudi::IDataConnection::UPDATE
@ UPDATE
Definition: IIODataManager.h:49
std::array
STL class.
Gaudi::RootDataConnection::m_statistics
std::unique_ptr< TTreePerfStats > m_statistics
I/O read statistics from TTree.
Definition: RootDataConnection.h:164
Gaudi::RootRef
Definition: RootRefs.h:40
Gaudi::IDataConnection::m_name
std::string m_name
Connection name/identifier.
Definition: IIODataManager.h:37
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:92
Gaudi::RootConnectionSetup::setCompression
static StatusCode setCompression(std::string_view compression)
Set the global compression level.
Definition: RootDataConnection.cpp:122
std::set::erase
T erase(T... args)
Gaudi::IDataConnection::IoType
IoType
I/O Connection types.
Definition: IIODataManager.h:49
std::runtime_error
STL class.
Gaudi::RootDataConnection::lookupClient
bool lookupClient(const IInterface *client) const
Lookup client for this data source.
Definition: RootDataConnection.cpp:196
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Gaudi::RootDataConnection::makeRef
void makeRef(const IRegistry &pA, RootRef &ref)
Create reference object from registry entry.
Definition: RootDataConnection.cpp:628
ROOT_StorageType
const long ROOT_StorageType
Definition: ClassID.h:62
Gaudi::RootDataConnection::m_tool
std::unique_ptr< Tool > m_tool
Definition: RootDataConnection.h:246
Gaudi::RootDataConnection::m_conts
StringVec m_conts
Map containing external container names.
Definition: RootDataConnection.h:174
Gaudi::IDataConnection::m_fid
std::string m_fid
File ID of the connection.
Definition: IIODataManager.h:39
gaudirun.level
level
Definition: gaudirun.py:366
IRegistry.h
Gaudi::RootDataConnection::Status
Status
Definition: RootDataConnection.h:109
MsgStream
Definition: MsgStream.h:34
Gaudi::PoolTool
Definition: PoolTool.h:27
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
Gaudi::RootDataConnection::makeLink
int makeLink(std::string_view p)
Convert path string to path index.
Definition: RootDataConnection.cpp:460
std::min
T min(T... args)
std::string::substr
T substr(T... args)
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
std::vector::emplace_back
T emplace_back(T... args)
Gaudi::RootDataConnection::saveStatistics
void saveStatistics(std::string_view statisticsFile)
Save TTree access statistics if required.
Definition: RootDataConnection.cpp:207
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataObject.h
Gaudi::RootDataConnection::loadRefs
int loadRefs(std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs &refs)
Load references object.
Definition: RootDataConnection.cpp:585
POOL_ROOTTREE_StorageType
const long POOL_ROOTTREE_StorageType
Definition: ClassID.h:80
Gaudi::RootDataConnection::save
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.
Definition: RootDataConnection.cpp:488
std::begin
T begin(T... args)
STATUSCODE_ENUM_IMPL
#define STATUSCODE_ENUM_IMPL(...)
Assign a category to the StatusCode enum declared with STATUSCODE_ENUM_DECL( ENUM )
Definition: StatusCode.h:295
std
STL namespace.
std::set::insert
T insert(T... args)
Gaudi::RootDataConnection::getSection
TTree * getSection(std::string_view sect, bool create=false)
Access TTree section from section name. The section is created if required.
Definition: RootDataConnection.cpp:377
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
IInterface
Definition: IInterface.h:237
Gaudi::RootDataConnection::RootDataConnection
RootDataConnection(const IInterface *own, std::string_view nam, std::shared_ptr< RootConnectionSetup > setup)
Standard constructor.
Definition: RootDataConnection.cpp:171
std::iota
T iota(T... args)
DataObject
Definition: DataObject.h:40
Gaudi::RootDataConnection::getMergeSection
std::pair< const RootRef *, const ContainerSection * > getMergeSection(std::string_view container, int entry) const
Access link section for single container and entry.
Definition: RootDataConnection.cpp:602
Gaudi::IDataConnection::fid
const std::string & fid() const
Access file id.
Definition: IIODataManager.h:63
RootUtils.h
std::vector::empty
T empty(T... args)
Gaudi::RootDataConnection::m_links
StringVec m_links
Map containing internal links names.
Definition: RootDataConnection.h:176
Gaudi::Utils::Histos::fill
GAUDI_API void fill(AIDA::IHistogram1D *histo, const double value, const double weight=1.0)
simple function to fill AIDA::IHistogram1D objects
Definition: Fill.cpp:45
Gaudi::RootDataConnection::loadObj
int loadObj(std::string_view section, std::string_view cnt, unsigned long entry, DataObject *&pObj)
Load object.
Definition: RootDataConnection.cpp:535
std::end
T end(T... args)
IOTest.end
end
Definition: IOTest.py:123
Gaudi::IDataConnection::m_pfn
std::string m_pfn
Physical file name of the connection.
Definition: IIODataManager.h:41
Gaudi::IDataConnection::pfn
const std::string & pfn() const
Access physical file name.
Definition: IIODataManager.h:65
compareRootHistos.ref
ref
Definition: compareRootHistos.py:28
Gaudi::RootDataConnection::addClient
void addClient(const IInterface *client)
Add new client to this data source.
Definition: RootDataConnection.cpp:186
Gaudi::RootDataConnection::makeTool
Tool * makeTool()
Create file access tool to encapsulate POOL compatibiliy.
Definition: RootDataConnection.cpp:230
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::RootDataConnection::empty
const std::string & empty() const
Empty string reference.
Definition: RootDataConnection.cpp:477
std::numeric_limits::max
T max(T... args)
PoolTool.h
Incident.h
IIncidentSvc
Definition: IIncidentSvc.h:33
Gaudi::RootDataConnection::m_sections
Sections m_sections
Tree sections in TFile.
Definition: RootDataConnection.h:170
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:319
Incident
Definition: Incident.h:27
DataObject::clID
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:66
Gaudi::RootConnectionSetup::compression
static int compression()
Access to global compression level.
Definition: RootDataConnection.cpp:162
Gaudi::RootDataConnection::msgSvc
MsgStream & msgSvc() const
Allow access to printer service.
Definition: RootDataConnection.h:158
std::numeric_limits
Gaudi::RootDataConnection::connectWrite
StatusCode connectWrite(IoType typ) override
Open data stream in write mode.
Definition: RootDataConnection.cpp:290
Gaudi::RootDataConnection::m_refs
TTree * m_refs
Pointer to the reference tree.
Definition: RootDataConnection.h:168
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:49
MsgStream.h
Gaudi::RootDataConnection::Tool
Definition: RootDataConnection.h:206
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86
CSTR
const string & CSTR
Definition: RootDataConnection.cpp:51