The Gaudi Framework  master (37c0b60a)
RootTool.h
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 #include <algorithm>
12 /*
13  * Gaudi namespace declaration
14  */
15 namespace Gaudi {
16 
26  class RootTool : virtual public RootDataConnection::Tool {
27  public:
29  RootTool( RootDataConnection* con ) { c = con; }
31  TBranch* getBranch( std::string_view section, std::string_view branch_name ) override {
32  std::string n{ branch_name };
33  n += '.';
34  for ( int i = 0, m = n.length() - 1; i < m; ++i )
35  if ( !isalnum( n[i] ) ) n[i] = '_';
36  TTree* t = c->getSection( section );
37  TBranch* b = t ? t->GetBranch( n.c_str() ) : nullptr;
38  if ( !b ) b = t ? t->GetBranch( std::string{ branch_name }.c_str() ) : nullptr;
39  if ( b ) b->SetAutoDelete( kFALSE );
40  return b;
41  }
43  int loadRefs( std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs& refs ) override {
44  TBranch* b = getBranch( section, std::string{ cnt } + "#R" );
45  RootObjectRefs* prefs = &refs;
46  if ( b ) {
47  b->SetAddress( &prefs );
48  int nb = b->GetEntry( entry );
49  if ( nb >= 1 ) {
50  const MergeSections& ms = c->mergeSections();
51  if ( !ms.empty() ) {
52  MsgStream& msg = msgSvc();
53  msgSvc() << MSG::VERBOSE;
54  pair<const RootRef*, const ContainerSection*> ls = c->getMergeSection( cnt, entry );
55  if ( ls.first ) {
56  if ( ls.first->dbase >= 0 ) {
57  // Now patch the references and links 'en block' to be efficient
58  // First the leafs from the TES
59  if ( msg.isActive() ) {
60  msg << "Refs: LS [" << entry << "] -> " << ls.first->dbase << "," << ls.first->container << ","
61  << ls.first->link << "," << ls.first->entry << endmsg;
62  }
63  for ( size_t j = 0, n = refs.refs.size(); j < n; ++j ) {
64  RootRef& r = refs.refs[j];
65  if ( r.entry >= 0 && r.dbase >= 0 ) {
66  int db = r.dbase + ls.first->dbase;
67  if ( c->getDb( db ) == c->fid() ) {
68  if ( r.dbase ) r.dbase += ls.first->dbase;
69  if ( r.container ) r.container += ls.first->container;
70  if ( r.link ) r.link += ls.first->link;
71  const string& rc = c->getCont( r.container );
72  auto k = ms.find( rc );
73  if ( k != ms.end() ) {
74  const auto& cs = ( *k ).second;
75  r.entry = ( ls.first->entry >= 0 && ls.first->entry < (int)cs.size() )
76  ? cs[ls.first->entry].start + r.entry
77  : -1;
78  if ( msg.isActive() ) {
79  msg << "Add link [" << r.entry << "," << ls.first->entry << "," << ls.first->container << ","
80  << r.container << "," << r.entry << "] to -> " << rc << endmsg;
81  }
82  } else {
83  msg << MSG::WARNING << c->fid() << " [" << c->getDb( db ) << "] Evt:" << entry
84  << " Invalid link to " << rc << endmsg;
85  msg << MSG::VERBOSE;
86  }
87  }
88  }
89  }
91  std::for_each( std::begin( refs.links ), std::end( refs.links ),
92  [&]( int& i ) { i += ls.first->link; } );
93  }
94  return nb;
95  }
96  return -1;
97  }
98  return nb;
99  }
100  }
101  return -1;
102  }
104  void addParam( ParamMap& c, char* p ) {
105  char* q = strchr( p, '=' );
106  if ( q ) {
107  *q = 0;
108  c.emplace_back( p, ++q );
109  }
110  }
112  void addEntry( StringVec& c, char* val ) { c.push_back( val ); }
114  template <class C, class F>
115  StatusCode readBranch( TTree* t, const char* nam, C& v, F pmf ) {
116  char text[2048];
117  TBranch* b = t->GetBranch( nam );
118  if ( b ) {
119  TLeaf* l = b->GetLeaf( nam );
120  if ( l ) {
122  b->SetAddress( text );
123  msgSvc() << MSG::VERBOSE;
124  for ( Long64_t i = 0, n = b->GetEntries(); i < n; ++i ) {
125  if ( b->GetEntry( i ) > 0 ) {
126  char* p = (char*)l->GetValuePointer();
127  msgSvc() << "Add Value[" << b->GetName() << "]:" << p << endmsg;
128  ( this->*pmf )( v, p );
129  } else {
131  }
132  }
133  return sc;
134  }
135  }
136  msgSvc() << MSG::ERROR << "Failed to read '" << nam << "' table." << endmsg;
138  }
140  bool get( const string& dsc, pair<string, ContainerSection>& e ) {
141  if ( dsc != "[END-OF-SECTION]" ) {
142  size_t id1 = dsc.find( "[CNT=" );
143  size_t id2 = dsc.find( "[START=" );
144  size_t id3 = dsc.find( "[LEN=" );
145  if ( id1 != string::npos && id2 != string::npos && id3 != string::npos ) {
146  string cnt = dsc.substr( id1 + 5, id2 - 1 - 5 );
147  int section_start = std::stoi( dsc.substr( id2 + 7, id3 - id2 - 8 ) );
148  int section_length = std::stoi( dsc.substr( id3 + 5, dsc.find( "]", id3 + 5 ) - id3 - 5 ) );
149  e.first = cnt;
150  e.second = ContainerSection( section_start, section_length );
151  return true;
152  }
153  }
154  e.first.clear();
155  e.second = ContainerSection( -1, -1 );
156  return false;
157  }
159  void analyzeMergeMap( StringVec& tmp ) {
160  LinkSections& ls = linkSections();
162  pair<string, ContainerSection> e;
163  MsgStream& msg = msgSvc();
164  RootRef r;
165  int cnt = 0;
166  ls.clear();
167  ms.clear();
168  msg << MSG::VERBOSE;
169  r.dbase = r.container = r.link = r.clid = r.svc = r.entry = 0;
170  for ( const auto& i : tmp ) {
171  if ( get( i, e ) ) {
172  msg << "Added Merge Section:" << e.first << endmsg;
173  ms[e.first].push_back( e.second );
174  if ( e.first == "Links" )
175  r.link = e.second.start;
176  else if ( e.first == "Containers" )
177  r.container = e.second.start;
178  else if ( e.first == "Databases" )
179  r.dbase = e.second.start;
180  else if ( e.first == "Params" )
181  r.svc = e.second.start;
182  } else if ( i == "[END-OF-SECTION]" ) {
183  r.entry = cnt;
184  if ( msg.isActive() ) {
185  msg << "Link Section [" << r.entry << "," << ls.size() << "] -> D:" << r.dbase << " C:" << r.container
186  << " L:" << r.link << " P:" << r.svc << endmsg;
187  }
188  ls.push_back( r );
189  cnt++;
190  }
191  }
192  }
194  StatusCode readRefs() override {
195  TTree* t = (TTree*)c->file()->Get( "Sections" );
197  StringVec tmp;
198  if ( t && !( sc = readBranch( t, "Sections", tmp, &RootTool::addEntry ) ).isSuccess() )
199  return sc;
200  else if ( refs() ) {
201  analyzeMergeMap( tmp );
202  if ( !( sc = readBranch( refs(), "Databases", dbs(), &RootTool::addEntry ) ).isSuccess() ) return sc;
203  if ( !( sc = readBranch( refs(), "Containers", conts(), &RootTool::addEntry ) ).isSuccess() ) return sc;
204  if ( !( sc = readBranch( refs(), "Links", links(), &RootTool::addEntry ) ).isSuccess() ) return sc;
205  if ( !( sc = readBranch( refs(), "Params", params(), &RootTool::addParam ) ).isSuccess() ) return sc;
206  return sc;
207  }
208  return StatusCode::FAILURE;
209  }
211  string getEntry( const string& c ) { return c; }
213  string getParam( const pair<string, string>& p ) { return p.first + "=" + p.second; }
215  template <class C, class F>
216  StatusCode saveBranch( const char* nam, C& v, F pmf ) {
217  Long64_t i, n;
218  string val, typ = nam;
220  TDirectory::TContext ctxt( c->file() );
221  TBranch* b = refs()->GetBranch( nam );
222  if ( !b ) b = refs()->Branch( nam, 0, ( typ + "/C" ).c_str() );
223  if ( b ) {
224  for ( i = b->GetEntries(), n = Long64_t( v.size() ); i < n; ++i ) {
225  val = ( this->*pmf )( v[size_t( i )] );
226  b->SetAddress( (char*)val.c_str() );
227  msgSvc() << MSG::VERBOSE << "Save Value[" << b->GetName() << "]:" << val << endmsg;
228  if ( b->Fill() < 0 ) sc = StatusCode::FAILURE;
229  }
230  return sc;
231  }
232  return StatusCode::FAILURE;
233  }
235  StatusCode saveRefs() override {
236  if ( refs() ) {
237  if ( !saveBranch( "Databases", dbs(), &RootTool::getEntry ).isSuccess() ) return StatusCode::FAILURE;
238  if ( !saveBranch( "Containers", conts(), &RootTool::getEntry ).isSuccess() ) return StatusCode::FAILURE;
239  if ( !saveBranch( "Links", links(), &RootTool::getEntry ).isSuccess() ) return StatusCode::FAILURE;
240  if ( !saveBranch( "Params", params(), &RootTool::getParam ).isSuccess() ) return StatusCode::FAILURE;
241  return StatusCode::SUCCESS;
242  }
243  return StatusCode::FAILURE;
244  }
245  };
246 } // namespace Gaudi
Gaudi::RootTool::get
bool get(const string &dsc, pair< string, ContainerSection > &e)
Analyze the Sections table entries.
Definition: RootTool.h:140
std::for_each
T for_each(T... args)
Gaudi::RootTool::readBranch
StatusCode readBranch(TTree *t, const char *nam, C &v, F pmf)
Helper function to read internal file tables.
Definition: RootTool.h:115
std::string
STL class.
Gaudi::RootTool
Definition: RootTool.h:26
Gaudi::RootDataConnection::Tool::dbs
StringVec & dbs() const
Definition: RootDataConnection.h:227
Gaudi::RootRef::clid
int clid
Definition: RootRefs.h:42
Gaudi::RootTool::addEntry
void addEntry(StringVec &c, char *val)
Helper function to read string tables.
Definition: RootTool.h:112
std::vector< std::pair< std::string, std::string > >
std::vector::size
T size(T... args)
Gaudi::RootRef::dbase
int dbase
Data members to define object location in the persistent world.
Definition: RootRefs.h:42
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::RootDataConnection::Tool::conts
StringVec & conts() const
Definition: RootDataConnection.h:228
Gaudi::RootDataConnection::Tool::msgSvc
MsgStream & msgSvc() const
Definition: RootDataConnection.h:231
Gaudi::RootDataConnection::Tool::ContainerSection
RootDataConnection::ContainerSection ContainerSection
Definition: RootDataConnection.h:219
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
Gaudi::RootTool::RootTool
RootTool(RootDataConnection *con)
Standard constructor.
Definition: RootTool.h:29
Gaudi::RootDataConnection::Tool::c
RootDataConnection * c
Pointer to containing data connection object.
Definition: RootDataConnection.h:223
std::vector::clear
T clear(T... args)
Gaudi::RootObjectRefs
Definition: RootRefs.h:68
Gaudi::RootDataConnection
Definition: RootDataConnection.h:112
Gaudi::Units::ms
constexpr double ms
Definition: SystemOfUnits.h:154
Gaudi::RootTool::saveBranch
StatusCode saveBranch(const char *nam, C &v, F pmf)
Helper function to save internal tables.
Definition: RootTool.h:216
Gaudi::RootTool::readRefs
StatusCode readRefs() override
Read reference tables.
Definition: RootTool.h:194
Gaudi::RootTool::loadRefs
int loadRefs(std::string_view section, std::string_view cnt, unsigned long entry, RootObjectRefs &refs) override
Load references object from file.
Definition: RootTool.h:43
std::vector::push_back
T push_back(T... args)
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::RootRef::link
int link
Definition: RootRefs.h:42
std::stoi
T stoi(T... args)
Gaudi::RootTool::saveRefs
StatusCode saveRefs() override
Save/update reference tables.
Definition: RootTool.h:235
Gaudi::RootDataConnection::getDb
const std::string & getDb(int which) const
Access database/file name from saved index.
Definition: RootDataConnection.cpp:486
Gaudi::RootRef::svc
int svc
Definition: RootRefs.h:42
StatusCode
Definition: StatusCode.h:65
Gaudi::RootTool::getEntry
string getEntry(const string &c)
Helper function to convert string vectors to branch entries.
Definition: RootTool.h:211
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::RootDataConnection::Status::ROOT_READ_ERROR
@ ROOT_READ_ERROR
std::string::c_str
T c_str(T... args)
Gaudi::RootDataConnection::getCont
const std::string & getCont(int which) const
Access container name from saved index.
Definition: RootDataConnection.h:346
Gaudi::RootRef::container
int container
Definition: RootRefs.h:42
Gaudi::RootRef
Definition: RootRefs.h:40
Gaudi::RootDataConnection::Tool::links
StringVec & links() const
Definition: RootDataConnection.h:229
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
Gaudi::RootDataConnection::file
TFile * file() const
Direct access to TFile structure.
Definition: RootDataConnection.h:264
std::map< std::string, ContainerSections, std::less<> >
Gaudi::RootDataConnection::Tool::mergeSections
MergeSections & mergeSections() const
Definition: RootDataConnection.h:236
MsgStream
Definition: MsgStream.h:33
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::RootRef::entry
int entry
Definition: RootRefs.h:42
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::RootDataConnection::Tool::params
ParamMap & params() const
Definition: RootDataConnection.h:230
Gaudi::RootTool::getParam
string getParam(const pair< string, string > &p)
Helper function to convert parameter vectors to branch entries.
Definition: RootTool.h:213
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Gaudi::RootDataConnection::Tool::refs
TTree * refs() const
Definition: RootDataConnection.h:226
gaudirun.l
dictionary l
Definition: gaudirun.py:583
Gaudi::RootDataConnection::Tool::linkSections
LinkSections & linkSections() const
Definition: RootDataConnection.h:235
std::begin
T begin(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:387
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
Gaudi::RootDataConnection::mergeSections
const MergeSections & mergeSections() const
Access merged data section inventory.
Definition: RootDataConnection.h:272
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:620
Gaudi::IDataConnection::fid
const std::string & fid() const
Access file id.
Definition: IIODataManager.h:63
Properties.v
v
Definition: Properties.py:122
Gaudi::RootTool::addParam
void addParam(ParamMap &c, char *p)
Helper function to read params table.
Definition: RootTool.h:104
std::end
T end(T... args)
Gaudi::RootTool::getBranch
TBranch * getBranch(std::string_view section, std::string_view branch_name) override
Access data branch by name: Get existing branch in read only mode.
Definition: RootTool.h:31
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::RootTool::analyzeMergeMap
void analyzeMergeMap(StringVec &tmp)
Build merge sections from the Sections table entries.
Definition: RootTool.h:159
IOTest.rc
rc
Definition: IOTest.py:114
Gaudi::RootDataConnection::Tool
Definition: RootDataConnection.h:212