The Gaudi Framework  v30r3 (a5ef0a68)
StoreExplorerAlg.cpp
Go to the documentation of this file.
1 // ====================================================================
2 // StoreExplorerAlg.cpp
3 // --------------------------------------------------------------------
4 //
5 // Author : Markus Frank
6 //
7 // ====================================================================
12 #include "GaudiKernel/IRegistry.h"
14 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/ObjectList.h"
19 #include "GaudiKernel/SmartIF.h"
20 
21 #include <numeric>
22 
33 {
34 
35  Gaudi::Property<bool> m_load{this, "Load", false, "load non existing items"};
36  Gaudi::Property<long> m_print{this, "PrintEvt", 1, "limit printout to first N events"};
37  Gaudi::Property<long> m_printMissing{this, "PrintMissing", 0, "indicate if missing entities should be printed"};
38  Gaudi::Property<double> m_frequency{this, "PrintFreq", 0.0, "printout frequency"};
39  Gaudi::Property<bool> m_exploreRelations{this, "ExploreRelations", false, "if relations should be followed"};
40  Gaudi::Property<std::string> m_dataSvcName{this, "DataSvc", "EventDataSvc", "name of the data provider service"};
41  Gaudi::Property<bool> m_testAccess{this, "TestAccess", false,
42  "test access to objects (DataObject and ContainedObject)"};
43  Gaudi::Property<bool> m_accessForeign{this, "AccessForeign", false, "indicate if foreign files should be opened"};
44 
46  long m_total = 0;
48  long m_frqPrint = 0;
53 
54 public:
57 
58  template <class T>
60  {
61  if ( !p ) return "Access FAILED.";
63  std::begin( *p ), std::end( *p ), std::string{}, [&]( std::string s, typename T::const_reference i ) {
64  return s + std::to_string( p->index( i ) ) + ":" + std::to_string( i->clID() ) + ",";
65  } );
66  return result.substr( 0, result.length() - 2 );
67  }
68 
70  void printObj( IRegistry* pReg, std::vector<bool>& flg )
71  {
72  auto& log = info();
73  for ( size_t j = 1; j < flg.size(); j++ ) {
74  if ( !flg[j - 1] && flg[j] )
75  log << "| ";
76  else if ( flg[j] )
77  log << " ";
78  else
79  log << "| ";
80  }
81  log << "+--> " << pReg->name();
82  if ( pReg->address() ) {
83  log << " [Address: CLID=" << std::showbase << std::hex << pReg->address()->clID()
84  << " Type=" << (void*)pReg->address()->svcType() << "]";
85  } else {
86  log << " [No Address]";
87  }
88  DataObject* p = pReg->object();
89  if ( p ) {
90  try {
91  std::string typ = System::typeinfoName( typeid( *p ) );
92  if ( m_testAccess ) p->clID();
93  log << " " << typ.substr( 0, 32 );
94  } catch ( ... ) {
95  log << "Access test FAILED";
96  }
97  } else {
98  log << " (Unloaded) ";
99  }
100  ObjectContainerBase* base = dynamic_cast<ObjectContainerBase*>( p );
101  if ( base ) {
102  try {
103  int numObj = base->numberOfObjects();
104  const CLID id = p->clID();
105  log << " [" << numObj << "]";
106  if ( m_testAccess ) {
107  CLID idd = id >> 16;
108  switch ( idd ) {
109  case CLID_ObjectList >> 16: /* ObjectList */
111  break;
112  case CLID_ObjectVector >> 16: /* ObjectVector */
114  break;
115  case ( CLID_ObjectVector + 0x00030000 ) >> 16: /* Keyed Map */
117  break;
118  case ( CLID_ObjectVector + 0x00040000 ) >> 16: /* Keyed Hashmap */
120  break;
121  case ( CLID_ObjectVector + 0x00050000 ) >> 16: /* Keyed array */
123  break;
124  }
125  }
126  } catch ( ... ) {
127  log << "Access test FAILED";
128  }
129  }
130  log << endmsg;
131  }
132 
133  void explore( IRegistry* pObj, std::vector<bool>& flg )
134  {
135  printObj( pObj, flg );
136  if ( pObj ) {
137  auto mgr = eventSvc().as<IDataManagerSvc>();
138  if ( mgr ) {
140  StatusCode sc = mgr->objectLeaves( pObj, leaves );
141  const std::string* par0 = nullptr;
142  if ( pObj->address() ) par0 = pObj->address()->par();
143  if ( sc.isSuccess() ) {
144  for ( auto i = leaves.begin(); i != leaves.end(); i++ ) {
145  const std::string& id = ( *i )->identifier();
146  DataObject* p = nullptr;
147  if ( !m_accessForeign && ( *i )->address() ) {
148  if ( par0 ) {
149  const std::string* par1 = ( *i )->address()->par();
150  if ( par1 && par0[0] != par1[0] ) continue;
151  }
152  }
153  if ( m_load ) {
154  sc = eventSvc()->retrieveObject( id, p );
155  } else {
156  sc = eventSvc()->findObject( id, p );
157  }
158  if ( sc.isSuccess() ) {
159  if ( id != "/Event/Rec/Relations" || m_exploreRelations ) {
160  flg.push_back( i + 1 == leaves.end() );
161  explore( *i, flg );
162  flg.pop_back();
163  }
164  } else {
165  flg.push_back( i + 1 == leaves.end() );
166  printObj( *i, flg );
167  flg.pop_back();
168  }
169  }
170  }
171  }
172  }
173  }
174 
177  {
178  m_rootName.clear();
179  m_dataSvc = service( m_dataSvcName, true );
180  if ( !m_dataSvc ) {
181  error() << "Failed to access service \"" << m_dataSvcName << "\"." << endmsg;
182  return StatusCode::FAILURE;
183  }
184  auto mgr = m_dataSvc.as<IDataManagerSvc>();
185  if ( !mgr ) {
186  error() << "Failed to retrieve IDataManagerSvc interface." << endmsg;
187  return StatusCode::FAILURE;
188  }
189  m_rootName = mgr->rootName();
190  return StatusCode::SUCCESS;
191  }
192 
194  StatusCode finalize() override
195  {
196  m_dataSvc.reset();
197  return StatusCode::SUCCESS;
198  }
199 
201  StatusCode execute() override
202  {
204  if ( ( ( m_print > m_total++ ) || ( m_frequency * m_total > m_frqPrint ) ) && root ) {
205  if ( m_frequency * m_total > m_frqPrint ) m_frqPrint++;
206  std::string store_name = "Unknown";
207  IRegistry* pReg = root->registry();
208  if ( pReg ) {
209  auto isvc = SmartIF<IService>{pReg->dataSvc()};
210  if ( isvc ) store_name = isvc->name();
211  }
212  info() << "========= " << m_rootName << "[" << std::showbase << std::hex << (unsigned long)root.ptr() << std::dec
213  << "@" << store_name << "]:" << endmsg;
214  std::vector<bool> flg( 1, true );
215  explore( root->registry(), flg );
216  return StatusCode::SUCCESS;
217  } else if ( root ) {
218  return StatusCode::SUCCESS;
219  }
220  error() << "Cannot retrieve \"/Event\"!" << endmsg;
221  return StatusCode::FAILURE;
222  }
223 };
224 
constexpr static const auto FAILURE
Definition: StatusCode.h:88
StatusCode initialize() override
Initialize.
Gaudi::Property< bool > m_exploreRelations
virtual const CLID & clID() const =0
Retrieve class information from link.
Implementation of property with value of concrete type.
Definition: Property.h:381
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Definition: Algorithm.cpp:823
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:30
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
SmartIF< IDataProviderSvc > m_dataSvc
Reference to data provider service.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
virtual StatusCode findObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Find object identified by its directory entry.
bool isSuccess() const
Definition: StatusCode.h:287
Gaudi::Property< bool > m_accessForeign
T to_string(T...args)
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores...
template class KeyedContainer, KeyedContainer.h
virtual const name_type & name() const =0
Name of the directory (or key)
StatusCode execute() override
Execute procedure.
T showbase(T...args)
T end(T...args)
Gaudi::Property< std::string > m_dataSvcName
virtual const std::string * par() const =0
Retrieve String parameters.
long m_total
Internal counter to trigger printouts.
Gaudi::Property< double > m_frequency
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores...
STL class.
Gaudi::Property< bool > m_testAccess
#define DECLARE_COMPONENT(type)
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:115
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:62
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
std::string m_rootName
Name of the root leaf (obtained at initialize)
T pop_back(T...args)
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
virtual long svcType() const =0
Retrieve service type.
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
T clear(T...args)
void printObj(IRegistry *pReg, std::vector< bool > &flg)
Print datastore leaf.
Gaudi::Property< long > m_print
Algorithm(const std::string &name, ISvcLocator *svcloc, const std::string &version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.cpp:51
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
virtual DataObject * object() const =0
Retrieve object behind the link.
void explore(IRegistry *pObj, std::vector< bool > &flg)
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
T length(T...args)
T begin(T...args)
StatusCode finalize() override
Finalize.
KeyedObjectManager Class to manage keyed objects.
string s
Definition: gaudirun.py:253
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:47
Gaudi::Property< long > m_printMissing
T hex(T...args)
T substr(T...args)
Small algorith, which traverses the data store and prints generic information about all leaves...
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
ObjectContainerBase is the base class for Gaudi container classes.
virtual size_type numberOfObjects() const =0
Number of objects in the container.
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn&#39;t already exist.
Definition: Algorithm.h:231
T accumulate(T...args)
long m_frqPrint
Internal counter to adjust printout frequency.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Gaudi::Property< bool > m_load
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
std::string access(T *p)