StoreExplorerAlg.cpp
Go to the documentation of this file.
1 // ====================================================================
2 // StoreExplorerAlg.cpp
3 // --------------------------------------------------------------------
4 //
5 // Author : Markus Frank
6 //
7 // ====================================================================
8 #include "GaudiKernel/ObjectContainerBase.h"
9 #include "GaudiKernel/ObjectList.h"
10 #include "GaudiKernel/ObjectVector.h"
11 #include "GaudiKernel/KeyedContainer.h"
12 #include "GaudiKernel/IDataProviderSvc.h"
13 #include "GaudiKernel/IDataManagerSvc.h"
14 #include "GaudiKernel/IOpaqueAddress.h"
15 #include "GaudiKernel/SmartDataPtr.h"
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;
52  std::string m_dataSvcName;
54  std::string m_rootName;
55 public:
56 
58  StoreExplorerAlg(const std::string& name, ISvcLocator* pSvcLocator)
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>
74  std::string access(T* p) {
75  if ( !p ) return "Access FAILED.";
76  std::string result = std::accumulate(std::begin(*p),std::end(*p),std::string{},
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  MsgStream log(msgSvc(), name());
87  log << MSG::INFO;
88  for (size_t j = 1; j < flg.size(); j++ ) {
89  if ( !flg[j-1] && flg[j] ) log << "| ";
90  else if ( flg[j] ) log << " ";
91  else log << "| ";
92  }
93  log << "+--> " << pReg->name();
94  if ( pReg->address() ) {
95  log << " [Address: CLID="
96  << std::showbase << std::hex << pReg->address()->clID()
97  << " Type=" << (void*)pReg->address()->svcType() << "]";
98  }
99  else {
100  log << " [No Address]";
101  }
102  DataObject* p = pReg->object();
103  if ( p ) {
104  try {
105  std::string typ = System::typeinfoName(typeid(*p));
106  if ( m_testAccess ) p->clID();
107  log << " " << typ.substr(0,32);
108  }
109  catch (...) {
110  log << "Access test FAILED";
111  }
112  }
113  else {
114  log << " (Unloaded) ";
115  }
116  ObjectContainerBase* base = dynamic_cast<ObjectContainerBase*>(p);
117  if ( base ) {
118  try {
119  int numObj = base->numberOfObjects();
120  const CLID id = p->clID();
121  log << " [" << numObj << "]";
122  if ( m_testAccess ) {
123  CLID idd = id>>16;
124  switch(idd) {
125  case CLID_ObjectList>>16: /* ObjectList */
127  break;
128  case CLID_ObjectVector>>16: /* ObjectVector */
130  break;
131  case (CLID_ObjectVector+0x00030000)>>16: /* Keyed Map */
133  break;
134  case (CLID_ObjectVector+0x00040000)>>16: /* Keyed Hashmap */
136  break;
137  case (CLID_ObjectVector+0x00050000)>>16: /* Keyed array */
139  break;
140  }
141  }
142  }
143  catch (...) {
144  log << "Access test FAILED";
145  }
146  }
147  log << endmsg;
148  }
149 
150  void explore(IRegistry* pObj, std::vector<bool>& flg) {
151  printObj(pObj, flg);
152  if ( pObj ) {
153  auto mgr = eventSvc().as<IDataManagerSvc>();
154  if ( mgr ) {
155  std::vector<IRegistry*> leaves;
156  StatusCode sc = mgr->objectLeaves(pObj, leaves);
157  const std::string* par0 = nullptr;
158  if ( pObj->address() ) par0 = pObj->address()->par();
159  if ( sc.isSuccess() ) {
160  for ( auto i=leaves.begin(); i != leaves.end(); i++ ) {
161  const std::string& id = (*i)->identifier();
162  DataObject* p = nullptr;
163  if ( !m_accessForeign && (*i)->address() ) {
164  if ( par0 ) {
165  const std::string* par1 = (*i)->address()->par();
166  if ( par1 && par0[0] != par1[0] ) continue;
167  }
168  }
169  if ( m_load ) {
170  sc = eventSvc()->retrieveObject(id, p);
171  } else {
172  sc = eventSvc()->findObject(id, p);
173  }
174  if ( sc.isSuccess() ) {
175  if ( id != "/Event/Rec/Relations" || m_exploreRelations ) {
176  flg.push_back(i+1 == leaves.end());
177  explore(*i, flg);
178  flg.pop_back();
179  }
180  } else {
181  flg.push_back(i+1 == leaves.end());
182  printObj(*i, flg);
183  flg.pop_back();
184  }
185  }
186  }
187  }
188  }
189  }
190 
192  StatusCode initialize() override {
193  MsgStream log(msgSvc(), name());
194  m_rootName.clear();
195  m_dataSvc = service(m_dataSvcName,true);
196  if ( !m_dataSvc ) {
197  log << MSG::ERROR << "Failed to access service \""
198  << m_dataSvcName << "\"." << endmsg;
199  return StatusCode::FAILURE;
200  }
201  auto mgr = m_dataSvc.as<IDataManagerSvc>();
202  if ( !mgr ) {
203  log << MSG::ERROR << "Failed to retrieve IDataManagerSvc interface." << endmsg;
204  return StatusCode::FAILURE;
205  }
206  m_rootName = mgr->rootName();
207  return StatusCode::SUCCESS;
208  }
209 
211  StatusCode finalize() override {
212  m_dataSvc.reset();
213  return StatusCode::SUCCESS;
214  }
215 
217  StatusCode execute() override {
218  MsgStream log(msgSvc(), name());
220  if ( ((m_print > m_total++) || (m_frequency*m_total > m_frqPrint)) && root ) {
221  if ( m_frequency*m_total > m_frqPrint ) m_frqPrint++;
222  std::string store_name = "Unknown";
223  IRegistry* pReg = root->registry();
224  if ( pReg ) {
225  auto isvc = SmartIF<IService>{pReg->dataSvc()};
226  if ( isvc ) store_name = isvc->name();
227  }
228  log << MSG::INFO << "========= " << m_rootName << "["
229  << "0x" << std::hex << (unsigned long) root.ptr() << std::dec
230  << "@" << store_name << "]:" << endmsg;
231  std::vector<bool> flg(1,true);
232  explore(root->registry(), flg);
233  return StatusCode::SUCCESS;
234  } else if ( root ) {
235  return StatusCode::SUCCESS;
236  }
237  log << MSG::ERROR << "Cannot retrieve \"/Event\"!" << endmsg;
238  return StatusCode::FAILURE;
239  }
240 };
241 
virtual const std::string * par() const =0
Retrieve String parameters.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Initialize.
string to_string(const T &value)
Definition: mergesort.cpp:40
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
long m_printMissing
Flag to indicate if missing entities should be printed.
SmartIF< IDataProviderSvc > m_dataSvc
Reference to data provider service.
bool m_accessForeign
Flag to indicate if foreign files should be opened.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
virtual size_type numberOfObjects() const =0
Number of objects in the container.
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:435
template class KeyedContainer, KeyedContainer.h
virtual const name_type & name() const =0
Name of the directory (or key)
StatusCode execute() override
Execute procedure.
bool m_load
Flag to load non existing items.
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.
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores...
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:919
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
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.
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::string m_rootName
Name of the root leaf (obtained at initialize)
StoreExplorerAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard algorithm constructor.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
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:77
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
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
tuple root
Definition: IOTest.py:42
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
~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:231
long m_frqPrint
Internal counter to adjust printout frequency.
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
list i
Definition: ana.py:128
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: Algorithm.cpp:1001
bool m_testAccess
Flag to test access to objects (DataObject and ContainedObject)
std::string access(T *p)
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.