| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

RootTool.h
Go to the documentation of this file.
1 #include <algorithm>
2 /*
3 * Gaudi namespace declaration
4 */
5 namespace Gaudi
6 {
7
17 class RootTool : virtual public RootDataConnection::Tool
18 {
19 public:
21 RootTool( RootDataConnection* con ) { c = con; }
23 TBranch* getBranch( CSTR section, CSTR branch_name ) override
24 {
25 std::string n = branch_name + ".";
26 for ( int i = 0, m = n.length() - 1; i < m; ++i )
27 if ( !isalnum( n[i] ) ) n[i] = '_';
28 TTree* t = c->getSection( section );
29 TBranch* b = t ? t->GetBranch( n.c_str() ) : 0;
30 if ( !b ) b = t ? t->GetBranch( branch_name.c_str() ) : 0;
31 if ( b ) b->SetAutoDelete( kFALSE );
32 return b;
33 }
35 int loadRefs( CSTR section, CSTR cnt, unsigned long entry, RootObjectRefs& refs ) override
36 {
37 TBranch* b = getBranch( section, cnt + "#R" );
38 RootObjectRefs* prefs = &refs;
39 if ( b ) {
40 b->SetAddress( &prefs );
41 int nb = b->GetEntry( entry );
42 if ( nb >= 1 ) {
43 const MergeSections& ms = c->mergeSections();
44 if ( !ms.empty() ) {
45 MsgStream& msg = msgSvc();
46 msgSvc() << MSG::VERBOSE;
47 pair<const RootRef*, const ContainerSection*> ls = c->getMergeSection( cnt, entry );
48 if ( ls.first ) {
49 if ( ls.first->dbase >= 0 ) {
50 // Now patch the references and links 'en block' to be efficient
51 // First the leafs from the TES
52 if ( msg.isActive() ) {
53 msg << "Refs: LS [" << entry << "] -> " << ls.first->dbase << "," << ls.first->container << ","
54 << ls.first->link << "," << ls.first->entry << endmsg;
55 }
56 for ( size_t j = 0, n = refs.refs.size(); j < n; ++j ) {
57 RootRef& r = refs.refs[j];
58 if ( r.entry >= 0 && r.dbase >= 0 ) {
59 int db = r.dbase + ls.first->dbase;
60 if ( c->getDb( db ) == c->fid() ) {
61 if ( r.dbase ) r.dbase += ls.first->dbase;
62 if ( r.container ) r.container += ls.first->container;
63 if ( r.link ) r.link += ls.first->link;
64 const string& rc = c->getCont( r.container );
65 auto k = ms.find( rc );
66 if ( k != ms.end() ) {
67 const auto& cs = ( *k ).second;
68 r.entry = ( ls.first->entry >= 0 && ls.first->entry < (int)cs.size() )
69 ? cs[ls.first->entry].start + r.entry
70 : -1;
71 if ( msg.isActive() ) {
72 msg << "Add link [" << r.entry << "," << ls.first->entry << "," << ls.first->container << ","
73 << r.container << "," << r.entry << "] to -> " << rc << endmsg;
74 }
75 } else {
76 msg << MSG::WARNING << c->fid() << " [" << c->getDb( db ) << "] Evt:" << entry
77 << " Invalid link to " << rc << endmsg;
78 msg << MSG::VERBOSE;
79 }
80 }
81 }
82 }
84 std::for_each( std::begin( refs.links ), std::end( refs.links ),
85 [&]( int& i ) { i += ls.first->link; } );
86 }
87 return nb;
88 }
89 return -1;
90 }
91 return nb;
92 }
93 }
94 return -1;
95 }
97 void addParam( ParamMap& c, char* p )
98 {
99 char* q = strchr( p, '=' );
100 if ( q ) {
101 *q = 0;
102 c.emplace_back( p, ++q );
103 }
104 }
106 void addEntry( StringVec& c, char* val ) { c.push_back( val ); }
108 template <class C, class F>
109 StatusCode readBranch( TTree* t, const char* nam, C& v, F pmf )
110 {
111 char text[2048];
112 TBranch* b = t->GetBranch( nam );
113 if ( b ) {
114 TLeaf* l = b->GetLeaf( nam );
115 if ( l ) {
116 StatusCode sc = StatusCode::SUCCESS;
117 b->SetAddress( text );
118 msgSvc() << MSG::VERBOSE;
119 for ( Long64_t i = 0, n = b->GetEntries(); i < n; ++i ) {
120 if ( b->GetEntry( i ) > 0 ) {
121 char* p = (char*)l->GetValuePointer();
122 msgSvc() << "Add Value[" << b->GetName() << "]:" << p << endmsg;
123 ( this->*pmf )( v, p );
124 } else {
125 sc = RootDataConnection::ROOT_READ_ERROR;
126 }
127 }
128 return sc;
129 }
130 }
131 msgSvc() << MSG::ERROR << "Failed to read '" << nam << "' table." << endmsg;
132 return RootDataConnection::ROOT_READ_ERROR;
133 }
135 bool get( const string& dsc, pair<string, ContainerSection>& e )
136 {
137 if ( dsc != "[END-OF-SECTION]" ) {
138 size_t id1 = dsc.find( "[CNT=" );
139 size_t id2 = dsc.find( "[START=" );
140 size_t id3 = dsc.find( "[LEN=" );
141 if ( id1 != string::npos && id2 != string::npos && id3 != string::npos ) {
142 string cnt = dsc.substr( id1 + 5, id2 - 1 - 5 );
143 int section_start = std::stoi( dsc.substr( id2 + 7, id3 - id2 - 8 ) );
144 int section_length = std::stoi( dsc.substr( id3 + 5, dsc.find( "]", id3 + 5 ) - id3 - 5 ) );
145 e.first = cnt;
146 e.second = ContainerSection( section_start, section_length );
147 return true;
148 }
149 }
150 e.first.clear();
151 e.second = ContainerSection( -1, -1 );
152 return false;
153 }
155 void analyzeMergeMap( StringVec& tmp )
156 {
157 LinkSections& ls = linkSections();
158 MergeSections& ms = mergeSections();
159 pair<string, ContainerSection> e;
160 MsgStream& msg = msgSvc();
161 RootRef r;
162 int cnt = 0;
163 ls.clear();
164 ms.clear();
165 msg << MSG::VERBOSE;
166 r.dbase = r.container = r.link = r.clid = r.svc = r.entry = 0;
167 for ( const auto& i : tmp ) {
168 if ( get( i, e ) ) {
169 msg << "Added Merge Section:" << e.first << endmsg;
170 ms[e.first].push_back( e.second );
171 if ( e.first == "Links" )
172 r.link = e.second.start;
173 else if ( e.first == "Containers" )
174 r.container = e.second.start;
175 else if ( e.first == "Databases" )
176 r.dbase = e.second.start;
177 else if ( e.first == "Params" )
178 r.svc = e.second.start;
179 } else if ( i == "[END-OF-SECTION]" ) {
180 r.entry = cnt;
181 if ( msg.isActive() ) {
182 msg << "Link Section [" << r.entry << "," << ls.size() << "] -> D:" << r.dbase << " C:" << r.container
183 << " L:" << r.link << " P:" << r.svc << endmsg;
184 }
185 ls.push_back( r );
186 cnt++;
187 }
188 }
189 }
191 StatusCode readRefs() override
192 {
193 TTree* t = (TTree*)c->file()->Get( "Sections" );
194 StatusCode sc( StatusCode::SUCCESS, true );
195 StringVec tmp;
196 if ( t && !( sc = readBranch( t, "Sections", tmp, &RootTool::addEntry ) ).isSuccess() )
197 return sc;
198 else if ( refs() ) {
199 analyzeMergeMap( tmp );
200 if ( !( sc = readBranch( refs(), "Databases", dbs(), &RootTool::addEntry ) ).isSuccess() ) return sc;
201 if ( !( sc = readBranch( refs(), "Containers", conts(), &RootTool::addEntry ) ).isSuccess() ) return sc;
202 if ( !( sc = readBranch( refs(), "Links", links(), &RootTool::addEntry ) ).isSuccess() ) return sc;
203 if ( !( sc = readBranch( refs(), "Params", params(), &RootTool::addParam ) ).isSuccess() ) return sc;
204 return sc;
205 }
206 return StatusCode::FAILURE;
207 }
209 string getEntry( const string& c ) { return c; }
211 string getParam( const pair<string, string>& p ) { return p.first + "=" + p.second; }
213 template <class C, class F>
214 StatusCode saveBranch( const char* nam, C& v, F pmf )
215 {
216 Long64_t i, n;
217 string val, typ = nam;
218 StatusCode sc = StatusCode::SUCCESS;
219 TDirectory::TContext ctxt( c->file() );
220 TBranch* b = refs()->GetBranch( nam );
221 if ( !b ) b = refs()->Branch( nam, 0, ( typ + "/C" ).c_str() );
222 if ( b ) {
223 for ( i = b->GetEntries(), n = Long64_t( v.size() ); i < n; ++i ) {
224 val = ( this->*pmf )( v[size_t( i )] );
225 b->SetAddress( (char*)val.c_str() );
226 msgSvc() << MSG::VERBOSE << "Save Value[" << b->GetName() << "]:" << val << endmsg;
227 if ( b->Fill() < 0 ) sc = StatusCode::FAILURE;
228 }
229 return sc;
230 }
231 return StatusCode::FAILURE;
232 }
234 StatusCode saveRefs() override
235 {
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 }
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode::SUCCESS
Definition: StatusCode.h:31
Gaudi::RootDataConnection::mergeSections
const MergeSections & mergeSections() const
Access merged data section inventory.
Definition: RootDataConnection.h:257
Gaudi::RootTool::readRefs
StatusCode readRefs() override
Read reference tables.
Definition: RootTool.h:191
std::map::empty
T empty(T...args)
Gaudi::IDataConnection::fid
const std::string & fid() const
Access file id.
Definition: IIODataManager.h:55
Gaudi::RootDataConnection::Tool::dbs
StringVec & dbs() const
Definition: RootDataConnection.h:211
StatusCode::FAILURE
Definition: StatusCode.h:31
Gaudi::RootDataConnection::file
TFile * file() const
Direct access to TFile structure.
Definition: RootDataConnection.h:249
Gaudi::RootTool::getBranch
TBranch * getBranch(CSTR section, CSTR branch_name) override
Access data branch by name: Get existing branch in read only mode.
Definition: RootTool.h:23
Gaudi::RootDataConnection::getDb
const std::string & getDb(int which) const
Access database/file name from saved index.
Definition: RootDataConnection.cpp:450
Gaudi::RootDataConnection::getSection
TTree * getSection(const std::string &sect, bool create=false)
Access TTree section from section name. The section is created if required.
Definition: RootDataConnection.cpp:358
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:116
Gaudi::RootRef::container
int container
Definition: RootRefs.h:33
std::map::end
T end(T...args)
Gaudi::RootObjectRefs::refs
std::vector< RootRef > refs
The references corresponding to the next layer of items in the data store.
Definition: RootRefs.h:64
Gaudi::RootRef::entry
int entry
Definition: RootRefs.h:33
MSG::VERBOSE
Definition: IMessageSvc.h:16
Gaudi::RootTool::getEntry
string getEntry(const string &c)
Helper function to convert string vectors to branch entries.
Definition: RootTool.h:209
std::map< std::string, ContainerSections >
Gaudi::RootObjectRefs
Persistent reference object containing all leafs and links corresponding to a Gaudi DataObject...
Definition: RootRefs.h:60
BrunelScenarioForwardScheduler.ms
ms
Definition: BrunelScenarioForwardScheduler.py:130
std::string
STL class.
Gaudi::RootDataConnection::Tool::params
ParamMap & params() const
Definition: RootDataConnection.h:214
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
Gaudi::RootRef::dbase
int dbase
Data members to define object location in the persistent world.
Definition: RootRefs.h:33
Gaudi::RootDataConnection::Tool::mergeSections
MergeSections & mergeSections() const
Definition: RootDataConnection.h:219
Gaudi::RootRef
Persistent reference object.
Definition: RootRefs.h:31
std::vector::push_back
T push_back(T...args)
Gaudi::RootTool::analyzeMergeMap
void analyzeMergeMap(StringVec &tmp)
Build merge sections from the Sections table entries.
Definition: RootTool.h:155
Gaudi::RootTool::loadRefs
int loadRefs(CSTR section, CSTR cnt, unsigned long entry, RootObjectRefs &refs) override
Load references object from file.
Definition: RootTool.h:35
Gaudi::RootDataConnection::ROOT_READ_ERROR
Definition: RootDataConnection.h:97
Gaudi::RootTool
Description:
Definition: RootTool.h:17
Gaudi::RootTool::saveRefs
StatusCode saveRefs() override
Save/update reference tables.
Definition: RootTool.h:234
Gaudi::RootTool::saveBranch
StatusCode saveBranch(const char *nam, C &v, F pmf)
Helper function to save internal tables.
Definition: RootTool.h:214
Gaudi::RootDataConnection::Tool
Helper class to facilitate an abstraction layer for reading POOL style files with this package...
Definition: RootDataConnection.h:195
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:94
IOTest.rc
rc
Definition: IOTest.py:103
Gaudi::RootDataConnection::Tool::links
StringVec & links() const
Definition: RootDataConnection.h:213
Gaudi::RootDataConnection::getMergeSection
std::pair< const RootRef *, const ContainerSection * > getMergeSection(const std::string &container, int entry) const
Access link section for single container and entry.
Definition: RootDataConnection.cpp:578
Gaudi::RootDataConnection::Tool::msgSvc
MsgStream & msgSvc() const
Definition: RootDataConnection.h:215
Gaudi::RootTool::getParam
string getParam(const pair< string, string > &p)
Helper function to convert parameter vectors to branch entries.
Definition: RootTool.h:211
Gaudi::RootDataConnection::getCont
const std::string & getCont(int which) const
Access container name from saved index.
Definition: RootDataConnection.h:330
std::vector::clear
T clear(T...args)
Gaudi::RootRef::clid
int clid
Definition: RootRefs.h:33
gaudirun.l
dictionary l
Definition: gaudirun.py:440
Gaudi::RootObjectRefs::links
std::vector< int > links
The links of the link manager.
Definition: RootRefs.h:62
Gaudi::RootTool::addParam
void addParam(ParamMap &c, char *p)
Helper function to read params table.
Definition: RootTool.h:97
Gaudi::RootTool::addEntry
void addEntry(StringVec &c, char *val)
Helper function to read string tables.
Definition: RootTool.h:106
Gaudi::RootTool::readBranch
StatusCode readBranch(TTree *t, const char *nam, C &v, F pmf)
Helper function to read internal file tables.
Definition: RootTool.h:109
std::map::find
T find(T...args)
std::string::length
T length(T...args)
std::vector< std::pair< std::string, std::string > >
Gaudi::RootDataConnection::Tool::ContainerSection
RootDataConnection::ContainerSection ContainerSection
Definition: RootDataConnection.h:203
Gaudi::RootRef::link
int link
Definition: RootRefs.h:33
Gaudi::RootDataConnection::Tool::conts
StringVec & conts() const
Definition: RootDataConnection.h:212
Gaudi::RootDataConnection::Tool::refs
TTree * refs() const
Definition: RootDataConnection.h:210
std::begin
T begin(T...args)
std::string::c_str
T c_str(T...args)
Gaudi::RootTool::RootTool
RootTool(RootDataConnection *con)
Standard constructor.
Definition: RootTool.h:21
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
MSG::ERROR
Definition: IMessageSvc.h:16
Gaudi::RootDataConnection::Tool::c
RootDataConnection * c
Pointer to containing data connection object.
Definition: RootDataConnection.h:207
Gaudi::RootRef::svc
int svc
Definition: RootRefs.h:33
MSG::WARNING
Definition: IMessageSvc.h:16
std::for_each
T for_each(T...args)
Gaudi::RootDataConnection
Concrete implementation of the IDataConnection interface to access ROOT files.
Definition: RootDataConnection.h:94
std::stoi
T stoi(T...args)
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
std::vector::emplace_back
T emplace_back(T...args)
Gaudi::RootDataConnection::Tool::linkSections
LinkSections & linkSections() const
Definition: RootDataConnection.h:218