The Gaudi Framework  master (37c0b60a)
StoreExplorerAlg.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 // ====================================================================
18 #include <GaudiKernel/Algorithm.h>
22 #include <GaudiKernel/IRegistry.h>
24 #include <GaudiKernel/MsgStream.h>
26 #include <GaudiKernel/ObjectList.h>
29 #include <GaudiKernel/SmartIF.h>
30 
31 #include <numeric>
32 
42 class StoreExplorerAlg : public Algorithm {
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;
62 
63 public:
66 
67  template <class T>
68  std::string access( T* p ) {
69  if ( !p ) return "Access FAILED.";
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 */
116  break;
117  case CLID_ObjectVector >> 16: /* ObjectVector */
119  break;
120  case ( CLID_ObjectVector + 0x00030000 ) >> 16: /* Keyed Map */
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 ) {
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 
178  StatusCode initialize() override {
179  m_rootName.clear();
180  m_dataSvc = service( m_dataSvcName, true );
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 
StoreExplorerAlg::m_rootName
std::string m_rootName
Name of the root leaf (obtained at initialize)
Definition: StoreExplorerAlg.cpp:61
Containers::KeyedObjectManager
KeyedObjectManager Class to manage keyed objects.
Definition: KeyedObjectManager.h:55
std::string
STL class.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
ObjectVector
Definition: ContainedObject.h:22
Gaudi.Configuration.log
log
Definition: Configuration.py:28
IDataManagerSvc
Definition: IDataManagerSvc.h:55
Gaudi::Algorithm::eventSvc
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Definition: Algorithm.cpp:559
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
StoreExplorerAlg::m_print
Gaudi::Property< long > m_print
Definition: StoreExplorerAlg.cpp:45
StoreExplorerAlg
Definition: StoreExplorerAlg.cpp:42
gaudirun.s
string s
Definition: gaudirun.py:346
std::vector< bool >
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
std::string::length
T length(T... args)
IOpaqueAddress::svcType
virtual long svcType() const =0
Retrieve service type.
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
StoreExplorerAlg::m_total
long m_total
Internal counter to trigger printouts.
Definition: StoreExplorerAlg.cpp:55
Algorithm.h
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:42
StoreExplorerAlg::m_load
Gaudi::Property< bool > m_load
Definition: StoreExplorerAlg.cpp:44
IRegistry
Definition: IRegistry.h:32
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
KeyedContainer.h
StoreExplorerAlg::access
std::string access(T *p)
Definition: StoreExplorerAlg.cpp:68
std::showbase
T showbase(T... args)
StoreExplorerAlg::m_accessForeign
Gaudi::Property< bool > m_accessForeign
Definition: StoreExplorerAlg.cpp:52
IDataProviderSvc.h
std::string::clear
T clear(T... args)
std::hex
T hex(T... args)
std::vector::push_back
T push_back(T... args)
StoreExplorerAlg::finalize
StatusCode finalize() override
Finalize.
Definition: StoreExplorerAlg.cpp:195
SmartIF.h
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
StoreExplorerAlg::m_dataSvc
SmartIF< IDataProviderSvc > m_dataSvc
Reference to data provider service.
Definition: StoreExplorerAlg.cpp:59
StatusCode
Definition: StatusCode.h:65
StoreExplorerAlg::printObj
void printObj(IRegistry *pReg, std::vector< bool > &flg)
Print datastore leaf.
Definition: StoreExplorerAlg.cpp:78
ObjectVector.h
ProduceConsume.j
j
Definition: ProduceConsume.py:104
IOpaqueAddress.h
SmartDataPtr.h
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
std::to_string
T to_string(T... args)
KeyedContainer
template class KeyedContainer, KeyedContainer.h
Definition: KeyedContainer.h:74
std::accumulate
T accumulate(T... args)
StoreExplorerAlg::m_exploreRelations
Gaudi::Property< bool > m_exploreRelations
Definition: StoreExplorerAlg.cpp:48
SmartIF< IDataProviderSvc >
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
StoreExplorerAlg::execute
StatusCode execute() override
Execute procedure.
Definition: StoreExplorerAlg.cpp:201
KeyedObject< int >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
ObjectList
Definition: ContainedObject.h:24
std::vector::pop_back
T pop_back(T... args)
IRegistry.h
StoreExplorerAlg::m_dataSvcName
Gaudi::Property< std::string > m_dataSvcName
Definition: StoreExplorerAlg.cpp:49
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
std::string::substr
T substr(T... args)
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
StoreExplorerAlg::m_printMissing
Gaudi::Property< long > m_printMissing
Definition: StoreExplorerAlg.cpp:46
StoreExplorerAlg::explore
void explore(IRegistry *pObj, std::vector< bool > &flg)
Definition: StoreExplorerAlg.cpp:136
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
SmartDataPtr
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
ObjectContainerBase
Definition: ObjectContainerBase.h:29
std::begin
T begin(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Gaudi::Algorithm::service
StatusCode service(std::string_view name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:206
IRegistry::object
virtual DataObject * object() const =0
Retrieve object behind the link.
DataObject
Definition: DataObject.h:36
StoreExplorerAlg::m_frqPrint
long m_frqPrint
Internal counter to adjust printout frequency.
Definition: StoreExplorerAlg.cpp:57
ObjectContainerBase.h
check_ParticleID.base
base
Definition: check_ParticleID.py:24
std::end
T end(T... args)
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
StoreExplorerAlg::m_frequency
Gaudi::Property< double > m_frequency
Definition: StoreExplorerAlg.cpp:47
StoreExplorerAlg::m_testAccess
Gaudi::Property< bool > m_testAccess
Definition: StoreExplorerAlg.cpp:50
ObjectList.h
DataObject::clID
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:66
StoreExplorerAlg::initialize
StatusCode initialize() override
Initialize.
Definition: StoreExplorerAlg.cpp:178
IRegistry::dataSvc
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
Gaudi::Property< bool >
IDataManagerSvc.h
MsgStream.h