The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
StoreExplorerAlg.cpp
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// ====================================================================
12// StoreExplorerAlg.cpp
13// --------------------------------------------------------------------
14//
15// Author : Markus Frank
16//
17// ====================================================================
29#include <GaudiKernel/SmartIF.h>
30
31#include <numeric>
32
43
44 Gaudi::Property<bool> m_load{ this, "Load", false, "load non existing items" };
45 Gaudi::Property<long> m_print{ this, "PrintEvt", 1, "limit printout to first N events" };
46 Gaudi::Property<long> m_printMissing{ this, "PrintMissing", 0, "indicate if missing entities should be printed" };
47 Gaudi::Property<double> m_frequency{ this, "PrintFreq", 0.0, "printout frequency" };
48 Gaudi::Property<bool> m_exploreRelations{ this, "ExploreRelations", false, "if relations should be followed" };
49 Gaudi::Property<std::string> m_dataSvcName{ this, "DataSvc", "EventDataSvc", "name of the data provider service" };
50 Gaudi::Property<bool> m_testAccess{ this, "TestAccess", false,
51 "test access to objects (DataObject and ContainedObject)" };
52 Gaudi::Property<bool> m_accessForeign{ this, "AccessForeign", false, "indicate if foreign files should be opened" };
53
55 long m_total = 0;
57 long m_frqPrint = 0;
61 std::string m_rootName;
62
63public:
66
67 template <class T>
68 std::string access( T* p ) {
69 if ( !p ) return "Access FAILED.";
70 std::string result = std::accumulate(
71 std::begin( *p ), std::end( *p ), std::string{}, [&]( const std::string& s, typename T::const_reference i ) {
72 return s + std::to_string( p->index( i ) ) + ":" + std::to_string( i->clID() ) + ",";
73 } );
74 return result.substr( 0, result.length() - 2 );
75 }
76
78 void printObj( IRegistry* pReg, std::vector<bool>& flg ) {
79 auto& log = info();
80 for ( size_t j = 1; j < flg.size(); j++ ) {
81 if ( !flg[j - 1] && flg[j] )
82 log << "| ";
83 else if ( flg[j] )
84 log << " ";
85 else
86 log << "| ";
87 }
88 log << "+--> " << pReg->name();
89 if ( pReg->address() ) {
90 log << " [Address: CLID=" << std::showbase << std::hex << pReg->address()->clID()
91 << " Type=" << (void*)pReg->address()->svcType() << "]";
92 } else {
93 log << " [No Address]";
94 }
95 DataObject* p = pReg->object();
96 if ( p ) {
97 try {
98 std::string typ = System::typeinfoName( typeid( *p ) );
99 if ( m_testAccess ) p->clID();
100 log << " " << typ.substr( 0, 32 );
101 } catch ( ... ) { log << "Access test FAILED"; }
102 } else {
103 log << " (Unloaded) ";
104 }
105 ObjectContainerBase* base = dynamic_cast<ObjectContainerBase*>( p );
106 if ( base ) {
107 try {
108 int numObj = base->numberOfObjects();
109 const CLID id = p->clID();
110 log << " [" << numObj << "]";
111 if ( m_testAccess ) {
112 CLID idd = id >> 16;
113 switch ( idd ) {
114 case CLID_ObjectList >> 16: /* ObjectList */
115 access( static_cast<ObjectList<ContainedObject>*>( base ) );
116 break;
117 case CLID_ObjectVector >> 16: /* ObjectVector */
118 access( static_cast<ObjectVector<ContainedObject>*>( base ) );
119 break;
120 case ( CLID_ObjectVector + 0x00030000 ) >> 16: /* Keyed Map */
121 access( static_cast<KeyedContainer<KeyedObject<int>, Containers::Map>*>( base ) );
122 break;
123 case ( CLID_ObjectVector + 0x00040000 ) >> 16: /* Keyed Hashmap */
125 break;
126 case ( CLID_ObjectVector + 0x00050000 ) >> 16: /* Keyed array */
128 break;
129 }
130 }
131 } catch ( ... ) { log << "Access test FAILED"; }
132 }
133 log << endmsg;
134 }
135
136 void explore( IRegistry* pObj, std::vector<bool>& flg ) {
137 printObj( pObj, flg );
138 if ( pObj ) {
139 auto mgr = eventSvc().as<IDataManagerSvc>();
140 if ( mgr ) {
141 std::vector<IRegistry*> leaves;
142 StatusCode sc = mgr->objectLeaves( pObj, leaves );
143 const std::string* par0 = nullptr;
144 if ( pObj->address() ) par0 = pObj->address()->par();
145 if ( sc.isSuccess() ) {
146 for ( auto i = leaves.begin(); i != leaves.end(); i++ ) {
147 const std::string& id = ( *i )->identifier();
148 DataObject* p = nullptr;
149 if ( !m_accessForeign && ( *i )->address() ) {
150 if ( par0 ) {
151 const std::string* par1 = ( *i )->address()->par();
152 if ( par1 && par0[0] != par1[0] ) continue;
153 }
154 }
155 if ( m_load ) {
156 sc = eventSvc()->retrieveObject( id, p );
157 } else {
158 sc = eventSvc()->findObject( id, p );
159 }
160 if ( sc.isSuccess() ) {
161 if ( id != "/Event/Rec/Relations" || m_exploreRelations ) {
162 flg.push_back( i + 1 == leaves.end() );
163 explore( *i, flg );
164 flg.pop_back();
165 }
166 } else {
167 flg.push_back( i + 1 == leaves.end() );
168 printObj( *i, flg );
169 flg.pop_back();
170 }
171 }
172 }
173 }
174 }
175 }
176
179 m_rootName.clear();
181 if ( !m_dataSvc ) {
182 error() << "Failed to access service \"" << m_dataSvcName << "\"." << endmsg;
183 return StatusCode::FAILURE;
184 }
185 auto mgr = m_dataSvc.as<IDataManagerSvc>();
186 if ( !mgr ) {
187 error() << "Failed to retrieve IDataManagerSvc interface." << endmsg;
188 return StatusCode::FAILURE;
189 }
190 m_rootName = mgr->rootName();
191 return StatusCode::SUCCESS;
192 }
193
195 StatusCode finalize() override {
196 m_dataSvc.reset();
197 return StatusCode::SUCCESS;
198 }
199
201 StatusCode execute() override {
203 if ( ( ( m_print > m_total++ ) || ( m_frequency * m_total > m_frqPrint ) ) && root ) {
205 std::string store_name = "Unknown";
206 IRegistry* pReg = root->registry();
207 if ( pReg ) {
208 auto isvc = SmartIF<IService>{ pReg->dataSvc() };
209 if ( isvc ) store_name = isvc->name();
210 }
211 info() << "========= " << m_rootName << "[" << std::showbase << std::hex << (unsigned long)root.ptr() << std::dec
212 << "@" << store_name << "]:" << endmsg;
213 std::vector<bool> flg( 1, true );
214 explore( root->registry(), flg );
215 return StatusCode::SUCCESS;
216 } else if ( root ) {
217 return StatusCode::SUCCESS;
218 }
219 error() << "Cannot retrieve \"/Event\"!" << endmsg;
220 return StatusCode::FAILURE;
221 }
222};
223
unsigned int CLID
Class ID definition.
Definition ClassID.h:16
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
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.
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
virtual StatusCode findObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Find object identified by its directory entry.
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 DataObject * object() const =0
Retrieve object behind the link.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
template class KeyedContainer, KeyedContainer.h
ObjectContainerBase is the base class for Gaudi container classes.
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores.
Definition ObjectList.h:42
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores.
A small class used to access easily (and efficiently) data items residing in data stores.
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition SmartIF.h:110
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
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
Small algorith, which traverses the data store and prints generic information about all leaves,...
Gaudi::Property< std::string > m_dataSvcName
StatusCode execute() override
Execute procedure.
SmartIF< IDataProviderSvc > m_dataSvc
Reference to data provider service.
StatusCode initialize() override
Initialize.
Gaudi::Property< bool > m_testAccess
void explore(IRegistry *pObj, std::vector< bool > &flg)
void printObj(IRegistry *pReg, std::vector< bool > &flg)
Print datastore leaf.
Gaudi::Property< long > m_printMissing
Gaudi::Property< bool > m_accessForeign
long m_total
Internal counter to trigger printouts.
std::string access(T *p)
std::string m_rootName
Name of the root leaf (obtained at initialize)
StatusCode finalize() override
Finalize.
Gaudi::Property< long > m_print
long m_frqPrint
Internal counter to adjust printout frequency.
Gaudi::Property< bool > m_exploreRelations
Gaudi::Property< double > m_frequency
Gaudi::Property< bool > m_load
KeyedObjectManager< hashmap > HashMap
Forward declaration of specialized std::hashmap-like object manager.
KeyedObjectManager< map > Map
Forward declaration of specialized std::map-like object manager.
KeyedObjectManager< array > Array
Forward declaration of specialized redirection array object manager.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260