| # Gaudi Framework, version v21r6 |

Home |  | Generated: 11 Nov 2009

# StoreExplorerAlg.cpp

Go to the documentation of this file.

    00001 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiSvc/src/DataSvc/StoreExplorerAlg.cpp,v 1.8 2007/10/24 19:06:06 marcocle Exp $
    00002 //      ====================================================================
    00003 //  StoreExplorerAlg.cpp
    00004 //      --------------------------------------------------------------------
    00005 //
    00006 //      Author    : Markus Frank
    00007 //
    00008 //      ====================================================================
    00009 #include "GaudiKernel/ObjectContainerBase.h"
    00010 #include "GaudiKernel/ObjectList.h"
    00011 #include "GaudiKernel/ObjectVector.h"
    00012 #include "GaudiKernel/KeyedContainer.h"
    00013 #include "GaudiKernel/IDataProviderSvc.h"
    00014 #include "GaudiKernel/IDataManagerSvc.h"
    00015 #include "GaudiKernel/IOpaqueAddress.h"
    00016 #include "GaudiKernel/SmartDataPtr.h"
    00017 #include "GaudiKernel/AlgFactory.h"
    00018 #include "GaudiKernel/Algorithm.h"
    00019 #include "GaudiKernel/IRegistry.h"
    00020 #include "GaudiKernel/MsgStream.h"
    00021 #include "GaudiKernel/SmartIF.h"
    00022
    00032 class StoreExplorerAlg : public Algorithm {
    00034   long              m_print;
    00036   double            m_frequency;
    00038   bool              m_exploreRelations;
    00040   long              m_printMissing;
    00042   long              m_total;
    00044   long              m_frqPrint;
    00046   bool              m_load;
    00048   bool              m_testAccess;
    00050   bool              m_accessForeign;
    00052   IDataProviderSvc* m_dataSvc;
    00054   std::string       m_dataSvcName;
    00056   std::string       m_rootName;
    00057 public:
    00058
    00060   StoreExplorerAlg(const std::string& name, ISvcLocator* pSvcLocator)
    00061   :     Algorithm(name, pSvcLocator), m_dataSvc(0)
    00062   {
    00063     m_total = m_frqPrint = 0;
    00064     declareProperty("Load",             m_load = false);
    00065     declareProperty("PrintEvt",         m_print = 1);
    00066     declareProperty("PrintMissing",     m_printMissing = 0);
    00067     declareProperty("PrintFreq",        m_frequency = 0.0);
    00068     declareProperty("ExploreRelations", m_exploreRelations = false);
    00069     declareProperty("DataSvc",          m_dataSvcName="EventDataSvc");
    00070     declareProperty("TestAccess",       m_testAccess = false);
    00071     declareProperty("AccessForeign",    m_accessForeign = false);
    00072   }
    00074   virtual ~StoreExplorerAlg()     {
    00075   }
    00076
    00077   template <class T>
    00078   std::string access(T* p)  {
    00079     if ( p )  {
    00080       std::stringstream s;
    00081       for (typename T::const_iterator i = p->begin(); i != p->end(); ++i )  {
    00082         int idx = p->index(*i);
    00083         s << idx << ":" << (*i)->clID() << ",";
    00084       }
    00085       std::string result = s.str();
    00086       return result.substr(0, result.length()-2);
    00087     }
    00088     return "Access FAILED.";
    00089   }
    00090
    00091
    00093   void printObj(IRegistry* pReg, std::vector<bool>& flg)    {
    00094     MsgStream log(msgSvc(), name());
    00095     log << MSG::INFO;
    00096     for (size_t j = 1; j < flg.size(); j++ )     {
    00097       if ( !flg[j-1] && flg[j] ) log << "| ";
    00098       else if ( flg[j] ) log << "  ";
    00099       else          log << "| ";
    00100     }
    00101     log << "+--> " << pReg->name();
    00102     if ( pReg->address() )  {
    00103       log << " [Address: CLID="
    00104           << std::showbase << std::hex << pReg->address()->clID();
    00105       log << " Type=" << (void*)pReg->address()->svcType() << "]";
    00106     }
    00107     else  {
    00108       log << " [No Address]";
    00109     }
    00110     DataObject* p = pReg->object();
    00111     if ( p )   {
    00112       try  {
    00113         std::string typ = System::typeinfoName(typeid(*p));
    00114         if ( m_testAccess )  {
    00115           p->clID();
    00116         }
    00117         log << "  " << typ.substr(0,32);
    00118       }
    00119       catch (...)  {
    00120         log << "Access test FAILED";
    00121       }
    00122     }
    00123     else    {
    00124       log << "  (Unloaded) ";
    00125     }
    00126     ObjectContainerBase* base = dynamic_cast<ObjectContainerBase*>(p);
    00127     if ( base )  {
    00128       try   {
    00129         int numObj = base->numberOfObjects();
    00130         const CLID id = p->clID();
    00131         log << " [" << numObj << "]";
    00132         if ( m_testAccess )  {
    00133           CLID idd = id>>16;
    00134           switch(idd) {
    00135             case CLID_ObjectList>>16:                /* ObjectList    */
    00136               access((ObjectList<ContainedObject>*)base);
    00137               break;
    00138             case CLID_ObjectVector>>16:              /* ObjectVector  */
    00139               access((ObjectVector<ContainedObject>*)base);
    00140               break;
    00141             case (CLID_ObjectVector+0x00030000)>>16: /* Keyed Map     */
    00142               access((KeyedContainer<KeyedObject<int>,Containers::Map>*)base);
    00143               break;
    00144             case (CLID_ObjectVector+0x00040000)>>16: /* Keyed Hashmap */
    00145               access((KeyedContainer<KeyedObject<int>,Containers::HashMap>*)base);
    00146               break;
    00147             case (CLID_ObjectVector+0x00050000)>>16: /* Keyed array   */
    00148               access((KeyedContainer<KeyedObject<int>,Containers::Array>*)base);
    00149               break;
    00150           }
    00151         }
    00152       }
    00153       catch (...)  {
    00154         log << "Access test FAILED";
    00155       }
    00156     }
    00157     log << endmsg;
    00158   }
    00159
    00160   void explore(IRegistry* pObj, std::vector<bool>& flg)    {
    00161     printObj(pObj, flg);
    00162     if ( 0 != pObj )    {
    00163       SmartIF<IDataManagerSvc> mgr(eventSvc());
    00164       if ( mgr )    {
    00165         typedef std::vector<IRegistry*> Leaves;
    00166         Leaves leaves;
    00167         StatusCode sc = mgr->objectLeaves(pObj, leaves);
    00168         const std::string* par0 = 0;
    00169         if ( pObj->address() )  {
    00170           par0 = pObj->address()->par();
    00171         }
    00172         if ( sc.isSuccess() )  {
    00173           for ( Leaves::const_iterator i=leaves.begin(); i != leaves.end(); i++ )   {
    00174             const std::string& id = (*i)->identifier();
    00175             DataObject* p = 0;
    00176             if ( !m_accessForeign && (*i)->address() )  {
    00177               if ( par0 )  {
    00178                 const std::string* par1 = (*i)->address()->par();
    00179                 if ( par1 )  {
    00180                   if ( par0[0] != par1[0] )  {
    00181                     continue;
    00182                   }
    00183                 }
    00184               }
    00185             }
    00186             if ( m_load )  {
    00187               sc = eventSvc()->retrieveObject(id, p);
    00188             }
    00189             else {
    00190               sc = eventSvc()->findObject(id, p);
    00191             }
    00192             if ( sc.isSuccess() )  {
    00193               if ( id != "/Event/Rec/Relations" || m_exploreRelations )   {
    00194                 flg.push_back(i+1 == leaves.end());
    00195                 explore(*i, flg);
    00196                 flg.pop_back();
    00197               }
    00198             }
    00199             else {
    00200               flg.push_back(i+1 == leaves.end());
    00201               printObj(*i, flg);
    00202               flg.pop_back();
    00203             }
    00204           }
    00205         }
    00206       }
    00207     }
    00208   }
    00209
    00211   virtual StatusCode initialize()   {
    00212     MsgStream log(msgSvc(), name());
    00213     m_rootName = "";
    00214     StatusCode sc = service(m_dataSvcName, m_dataSvc, true);
    00215     if ( sc.isSuccess() )  {
    00216       SmartIF<IDataManagerSvc> mgr(m_dataSvc);
    00217       if ( mgr )  {
    00218         m_rootName = mgr->rootName();
    00219         return sc;
    00220       }
    00221       log << MSG::ERROR << "Failed to retrieve IDataManagerSvc interface." << endmsg;
    00222       return StatusCode::FAILURE;
    00223     }
    00224     log << MSG::ERROR << "Failed to access service \""
    00225         << m_dataSvcName << "\"." << endmsg;
    00226     return StatusCode::FAILURE;
    00227   }
    00228
    00230   virtual StatusCode finalize() {
    00231     if ( m_dataSvc ) m_dataSvc->release();
    00232     m_dataSvc = 0;
    00233     return StatusCode::SUCCESS;
    00234   }
    00235
    00237   virtual StatusCode execute()    {
    00238     MsgStream log(msgSvc(), name());
    00239     SmartDataPtr<DataObject>   root(m_dataSvc,m_rootName);
    00240     if ( ((m_print > m_total++) || (m_frequency*m_total > m_frqPrint)) && root )    {
    00241       if ( m_frequency*m_total > m_frqPrint )  {
    00242         m_frqPrint++;
    00243       }
    00244       std::string store_name = "Unknown";
    00245       IRegistry* pReg = root->registry();
    00246       if ( pReg )  {
    00247         SmartIF<IService> isvc(pReg->dataSvc());
    00248         if ( isvc )  {
    00249           store_name = isvc->name();
    00250         }
    00251       }
    00252       log << MSG::INFO << "========= " << m_rootName << "["
    00253           << "0x" << std::hex << (unsigned long) root.ptr() << std::dec
    00254           << "@" << store_name << "]:" << endmsg;
    00255       std::vector<bool> flg;
    00256       flg.push_back(true);
    00257       explore(root->registry(), flg);
    00258       return StatusCode::SUCCESS;
    00259     }
    00260     else if ( root )   {
    00261       return StatusCode::SUCCESS;
    00262     }
    00263     log << MSG::ERROR << "Cannot retrieve \"/Event\"!" << endmsg;
    00264     return StatusCode::FAILURE;
    00265   }
    00266 };
    00267
    00268 DECLARE_ALGORITHM_FACTORY(StoreExplorerAlg)