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