StoreExplorerAlg.cpp
Go to the documentation of this file.
1 // ====================================================================
2 // StoreExplorerAlg.cpp
3 // --------------------------------------------------------------------
4 //
5 // Author : Markus Frank
6 //
7 // ====================================================================
16 #include "GaudiKernel/Algorithm.h"
17 #include "GaudiKernel/IRegistry.h"
18 #include "GaudiKernel/MsgStream.h"
19 #include "GaudiKernel/SmartIF.h"
20 
30 class StoreExplorerAlg : public Algorithm {
32  long m_print;
34  double m_frequency;
40  long m_total = 0;
42  long m_frqPrint = 0;
44  bool m_load;
55 public:
56 
59  : Algorithm(name, pSvcLocator)
60  {
61  declareProperty("Load", m_load = false);
62  declareProperty("PrintEvt", m_print = 1);
63  declareProperty("PrintMissing", m_printMissing = 0);
64  declareProperty("PrintFreq", m_frequency = 0.0);
65  declareProperty("ExploreRelations", m_exploreRelations = false);
66  declareProperty("DataSvc", m_dataSvcName="EventDataSvc");
67  declareProperty("TestAccess", m_testAccess = false);
68  declareProperty("AccessForeign", m_accessForeign = false);
69  }
71  ~StoreExplorerAlg() override = default;
72 
73  template <class T>
75  if ( !p ) return "Access FAILED.";
77  [&](std::string s,typename T::const_reference i) {
78  return s+std::to_string(p->index(i)) + ":" + std::to_string(i->clID()) + ",";
79  });
80  return result.substr(0, result.length()-2);
81  }
82 
83 
85  void printObj(IRegistry* pReg, std::vector<bool>& flg) {
86  auto& log = info();
87  for (size_t j = 1; j < flg.size(); j++ ) {
88  if ( !flg[j-1] && flg[j] ) log << "| ";
89  else if ( flg[j] ) log << " ";
90  else log << "| ";
91  }
92  log << "+--> " << pReg->name();
93  if ( pReg->address() ) {
94  log << " [Address: CLID="
95  << std::showbase << std::hex << pReg->address()->clID()
96  << " Type=" << (void*)pReg->address()->svcType() << "]";
97  }
98  else {
99  log << " [No Address]";
100  }
101  DataObject* p = pReg->object();
102  if ( p ) {
103  try {
104  std::string typ = System::typeinfoName(typeid(*p));
105  if ( m_testAccess ) p->clID();
106  log << " " << typ.substr(0,32);
107  }
108  catch (...) {
109  log << "Access test FAILED";
110  }
111  }
112  else {
113  log << " (Unloaded) ";
114  }
115  ObjectContainerBase* base = dynamic_cast<ObjectContainerBase*>(p);
116  if ( base ) {
117  try {
118  int numObj = base->numberOfObjects();
119  const CLID id = p->clID();
120  log << " [" << numObj << "]";
121  if ( m_testAccess ) {
122  CLID idd = id>>16;
123  switch(idd) {
124  case CLID_ObjectList>>16: /* ObjectList */
126  break;
127  case CLID_ObjectVector>>16: /* ObjectVector */
129  break;
130  case (CLID_ObjectVector+0x00030000)>>16: /* Keyed Map */
132  break;
133  case (CLID_ObjectVector+0x00040000)>>16: /* Keyed Hashmap */
135  break;
136  case (CLID_ObjectVector+0x00050000)>>16: /* Keyed array */
138  break;
139  }
140  }
141  }
142  catch (...) {
143  log << "Access test FAILED";
144  }
145  }
146  log << endmsg;
147  }
148 
149  void explore(IRegistry* pObj, std::vector<bool>& flg) {
150  printObj(pObj, flg);
151  if ( pObj ) {
152  auto mgr = eventSvc().as<IDataManagerSvc>();
153  if ( mgr ) {
155  StatusCode sc = mgr->objectLeaves(pObj, leaves);
156  const std::string* par0 = nullptr;
157  if ( pObj->address() ) par0 = pObj->address()->par();
158  if ( sc.isSuccess() ) {
159  for ( auto i=leaves.begin(); i != leaves.end(); i++ ) {
160  const std::string& id = (*i)->identifier();
161  DataObject* p = nullptr;
162  if ( !m_accessForeign && (*i)->address() ) {
163  if ( par0 ) {
164  const std::string* par1 = (*i)->address()->par();
165  if ( par1 && par0[0] != par1[0] ) continue;
166  }
167  }
168  if ( m_load ) {
169  sc = eventSvc()->retrieveObject(id, p);
170  } else {
171  sc = eventSvc()->findObject(id, p);
172  }
173  if ( sc.isSuccess() ) {
174  if ( id != "/Event/Rec/Relations" || m_exploreRelations ) {
175  flg.push_back(i+1 == leaves.end());
176  explore(*i, flg);
177  flg.pop_back();
178  }
179  } else {
180  flg.push_back(i+1 == leaves.end());
181  printObj(*i, flg);
182  flg.pop_back();
183  }
184  }
185  }
186  }
187  }
188  }
189 
191  StatusCode initialize() override {
192  m_rootName.clear();
193  m_dataSvc = service(m_dataSvcName,true);
194  if ( !m_dataSvc ) {
195  error() << "Failed to access service \""
196  << m_dataSvcName << "\"." << endmsg;
197  return StatusCode::FAILURE;
198  }
199  auto mgr = m_dataSvc.as<IDataManagerSvc>();
200  if ( !mgr ) {
201  error() << "Failed to retrieve IDataManagerSvc interface." << endmsg;
202  return StatusCode::FAILURE;
203  }
204  m_rootName = mgr->rootName();
205  return StatusCode::SUCCESS;
206  }
207 
209  StatusCode finalize() override {
210  m_dataSvc.reset();
211  return StatusCode::SUCCESS;
212  }
213 
215  StatusCode execute() override {
217  if ( ((m_print > m_total++) || (m_frequency*m_total > m_frqPrint)) && root ) {
218  if ( m_frequency*m_total > m_frqPrint ) m_frqPrint++;
219  std::string store_name = "Unknown";
220  IRegistry* pReg = root->registry();
221  if ( pReg ) {
222  auto isvc = SmartIF<IService>{pReg->dataSvc()};
223  if ( isvc ) store_name = isvc->name();
224  }
225  info() << "========= " << m_rootName << "["
226  << std::showbase << std::hex << (unsigned long) root.ptr() << std::dec
227  << "@" << store_name << "]:" << endmsg;
228  std::vector<bool> flg(1,true);
229  explore(root->registry(), flg);
230  return StatusCode::SUCCESS;
231  } else if ( root ) {
232  return StatusCode::SUCCESS;
233  }
234  error() << "Cannot retrieve \"/Event\"!" << endmsg;
235  return StatusCode::FAILURE;
236  }
237 };
238 
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode initialize() override
Initialize.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
long m_printMissing
Flag to indicate if missing entities should be printed.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
SmartIF< IDataProviderSvc > m_dataSvc
Reference to data provider service.
bool m_accessForeign
Flag to indicate if foreign files should be opened.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
virtual size_type numberOfObjects() const =0
Number of objects in the container.
T to_string(T...args)
ObjectVector is one of the basic Gaudi container classes capable of being registered in Data Stores...
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Algorithm.h:426
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)
bool m_load
Flag to load non existing items.
T end(T...args)
virtual long svcType() const =0
Retrieve service type.
long m_total
Internal counter to trigger printouts.
double m_frequency
Job option to set the printout frequency.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores...
STL class.
long m_print
Job option to limit printout to first nnn events.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:820
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
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:110
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:68
virtual StatusCode findObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Find object identified by its directory entry.
virtual const CLID & clID() const =0
Retrieve class information from link.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string m_rootName
Name of the root leaf (obtained at initialize)
StoreExplorerAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard algorithm constructor.
T pop_back(T...args)
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
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.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual DataObject * object() const =0
Retrieve object behind the link.
std::string m_dataSvcName
Name of the data provider service.
void explore(IRegistry *pObj, std::vector< bool > &flg)
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:74
T length(T...args)
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
T begin(T...args)
StatusCode finalize() override
Finalize.
bool m_exploreRelations
Flag to check if relations should be followed.
KeyedObjectManager Class to manage keyed objects.
string s
Definition: gaudirun.py:245
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:46
T hex(T...args)
tuple root
Definition: IOTest.py:42
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
T substr(T...args)
~StoreExplorerAlg() override=default
Standard Destructor.
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:88
ObjectContainerBase is the base class for Gaudi container classes.
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:232
T accumulate(T...args)
long m_frqPrint
Internal counter to adjust printout frequency.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
list i
Definition: ana.py:128
bool m_testAccess
Flag to test access to objects (DataObject and ContainedObject)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::string access(T *p)
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.