| # Gaudi Framework, version v20r3
Generated: 24 Nov 2008

# HNTupleCnv.cpp

Go to the documentation of this file.

    00001 // $Header: /local/reps/Gaudi/HbookCnv/src/HNTupleCnv.cpp,v 1.9 2006/03/13 17:17:32 hmd Exp $
    00002 #define HBOOKCNV_HNTUPLECNV_CPP
    00003
    00004
    00005 // Include files
    00006 #include "GaudiKernel/CnvFactory.h"
    00007 #include "GaudiKernel/DataObject.h"
    00008 #include "GaudiKernel/IRegistry.h"
    00009 #include "GaudiKernel/IOpaqueAddress.h"
    00010 #include "GaudiKernel/IDataProviderSvc.h"
    00011 #include "GaudiKernel/INTupleSvc.h"
    00012
    00013 #include "GaudiKernel/MsgStream.h"
    00014 #include "GaudiKernel/NTuple.h"
    00015 #include "NTupleInfo.h"
    00016 #include "HNTupleCnv.h"
    00017 #include "HbookDef.h"
    00018
    00019 //------------------------------------------------------------------------------
    00020 //
    00021 // Implementation of class :  HbookCnv::HNTupleCnv
    00022 //
    00023 // Author :                   Markus Frank
    00024 //
    00025 //------------------------------------------------------------------------------
    00026
    00028 HbookCnv::HNTupleCnv::HNTupleCnv(ISvcLocator* svc, const CLID& clid)
    00029 : HConverter(clid, svc), m_ntupleSvc(0)         {
    00030 }
    00031
    00033 HbookCnv::HNTupleCnv::~HNTupleCnv()             {
    00034 }
    00035
    00037 StatusCode HbookCnv::HNTupleCnv::initialize()   {
    00038   StatusCode status = Converter::initialize();
    00039   if ( status.isSuccess() )   {
    00040     status = dataProvider()->queryInterface(IID_INTupleSvc, (void**)&m_ntupleSvc);
    00041   }
    00042   return status;
    00043 }
    00044
    00046 StatusCode HbookCnv::HNTupleCnv::finalize()     {
    00047   if ( m_ntupleSvc != 0 )     {
    00048     m_ntupleSvc->release();
    00049   }
    00050   m_ntupleSvc = 0;
    00051   return Converter::finalize();
    00052 }
    00053
    00055 StatusCode HbookCnv::HNTupleCnv::updateObj(IOpaqueAddress* pAddress, DataObject* pObject)   {
    00056   StatusCode status = StatusCode::FAILURE;
    00057   try   {
    00058     unsigned long* info = (unsigned long*)pAddress->ipar();
    00059     const std::string& dir = pAddress->par()[0];
    00060     setDiskDirectory(dir);
    00061     setDirectory(dir);
    00062     status = readData(info[0], dynamic_cast<INTuple*>(pObject), info[1]++);
    00063   }
    00064   catch (...)   {
    00065   }
    00066   return status;
    00067 }
    00068
    00070 StatusCode HbookCnv::HNTupleCnv::createObj(IOpaqueAddress* pAddress, DataObject*& refpObject)   {
    00071   long id = pAddress->ipar()[0];
    00072   IRegistry* pReg = pAddress->registry();
    00073   StatusCode status = readObject( pReg->identifier(), id );
    00074   if ( status.isSuccess() )   {
    00075     MsgStream log(msgSvc(), "HbookNTupleCnv");
    00076     INTuple* nt = 0;
    00077     log << MSG::DEBUG;
    00078     if ( log.isActive() ) ::HPRINT (id);
    00079     status = load(id, nt);
    00080     refpObject = dynamic_cast<DataObject*>(nt);
    00081   }
    00082   return status;
    00083 }
    00084
    00086 StatusCode HbookCnv::HNTupleCnv::createRep(DataObject* pObject, IOpaqueAddress*& pAddr)  {
    00087   pAddr = 0;
    00088   try   {
    00089     typedef std::pair<std::string,long> hID;
    00090     typedef std::map< hID, bool > hIDMap;
    00091     static hIDMap usedIDs;
    00092     IRegistry* pReg = pObject->registry();
    00093     INTuple* pTuple = dynamic_cast<INTuple*>(pObject);
    00094     // Get address again....it does not change
    00095     pAddr = pReg->address();
    00096     if ( 0 == pAddr )    {
    00097       char* stop = 0;
    00098       const std::string& regName = pReg->identifier();
    00099       const std::string rzLoc = regName.substr(0, regName.rfind('/'));
    00100       long id = ::strtol(pReg->name().c_str()+1, &stop, 10);
    00101       if ( 0 >= id || usedIDs.find(hID(rzLoc,id)) != usedIDs.end() )
    00102       {
    00103         MsgStream msg(msgSvc(), "HNTupleCnv");
    00104         if ( 0 >= id )
    00105         {
    00106           msg << MSG::WARNING
    00107               << "Ntuple ID " << pReg->identifier()
    00108               << "' is an invalid HBOOK identifier" << endreq;
    00109         }
    00110         else
    00111         {
    00112           // ID is already used
    00113           msg << MSG::WARNING << "Ntuple ID '" << pReg->identifier()
    00114               << "' is already used" << endreq;
    00115         }
    00116         id = 1001;
    00117         while ( usedIDs.find(hID(rzLoc,id)) != usedIDs.end() ) { ++id; }
    00118         const std::string root = pReg->identifier().substr(0,pReg->identifier().rfind('/'));
    00119         msg << MSG::WARNING
    00120             << "  -> Adjusting PERSISTENT ID to '" << root << "/" << id << "'" << endreq;
    00121       }
    00122       usedIDs[hID(rzLoc,id)] = true;
    00123       StatusCode status = createDirectory(rzLoc);
    00124       if ( !status.isSuccess() )   {
    00125         return status;
    00126       }
    00127       setDirectory(rzLoc);
    00128       setDiskDirectory(rzLoc);
    00129       status = book(id, diskDirectory(rzLoc), pTuple);
    00130       if ( !status.isSuccess() )   {
    00131         return status;
    00132       }
    00133       status = createAddress( rzLoc, objType(), id, pAddr );
    00134       if ( !status.isSuccess() )   {
    00135         return status;
    00136       }
    00137     }
    00138     setDirectory(pAddr->par()[0]);
    00139     return writeData(pAddr->ipar()[0], pTuple);
    00140   }
    00141   catch (...)   {
    00142   }
    00143   return StatusCode::FAILURE;
    00144 }
    00145
    00147 StatusCode HbookCnv::HNTupleCnv::updateRep(IOpaqueAddress* pAddress, DataObject* pObject )
    00148 {
    00149   MsgStream log(msgSvc(), "HNTupleCnv");
    00150   if ( 0 != pAddress )    {
    00151     IRegistry* pDir = pAddress->registry();
    00152     if ( 0 != pDir )    {
    00153       const std::string& regName = pDir->identifier();
    00154       const std::string loc = regName.substr(0, regName.rfind('/'));
    00155       long id = pAddress->ipar()[0];
    00156       log << MSG::INFO << "Saving:" << regName << " with ID:" << id << endreq;
    00157       return writeObject( loc, id );
    00158     }
    00159   }
    00160   else {
    00161     log << MSG::WARNING << "empty ntuple: "
    00162         << pObject->registry()->identifier() << endreq;
    00163     return ( createRep(pObject,pAddress) );
    00164   }
    00165   return StatusCode::FAILURE;
    00166 }
    00167
    00168 bool HbookCnv::parseName( std::string full,
    00169                           std::string &blk,
    00170                                             std::string &var)
    00171 {
    00172   int sp;
    00173   if ( (sp=full.find("/")) != -1 ) {
    00174     blk = full.substr(0,sp);
    00175     var = full.substr(sp+1,full.length());
    00176     return true;
    00177   }
    00178   else {
    00179     blk = "AUTO_BLK";
    00180     var = full;
    00181     return false;
    00182   }
    00183 }
    00184
    00185
    00186
    00188 #define INSTANTIATE(TYP)  \
    00189 template INTupleItem* createNTupleItem<TYP>(NTUPLEINFO& tags, long i, INTuple* tuple, TYP minimum, TYP maximum, long& size);
    00190
    00191 namespace HbookCnv  {
    00192   template<class TYP>
    00193   INTupleItem* createNTupleItem (NTUPLEINFO& tags, long i, INTuple* tuple, TYP /* minimum */, TYP /* maximum */ , long& size)   {
    00194     size = 0;
    00195     INTupleItem* col = 0;
    00196
    00197     std::string varname;
    00198     std::string blkname = tags.block[i];
    00199
    00200     if (blkname != "AUTO_BLK") {
    00201       varname = blkname + "/" + tags.name[i];
    00202     } else {
    00203       varname = tags.name[i];
    00204     }
    00205
    00206     //if ( tags.isRangeWithin(i, minimum,maximum))
    00207     {
    00208       long ind = tags.getIndex(tags.index[i]);
    00209       long dim1 = 1, dim2 = 1;
    00210       TYP min, max, null;
    00211       tags.getBounds(i, min, max, null);
    00212       switch( tags.dim[i] )   {
    00213       case 0:
    00214         col = NTuple::_Item<TYP>::create (tuple, varname, typeid(TYP), min, max, null);
    00215         break;
    00216       case 1:
    00217         dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
    00218           col = NTuple::_Array<TYP>::create (tuple, varname,typeid(TYP),tags.index[i],dim1,min,max,null);
    00219         break;
    00220       case 2:
    00221         dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
    00222         dim2 = (ind>=0) ? tags.theDim[i][0] : tags.theDim[i][1];
    00223         col = NTuple::_Matrix<TYP>::create (tuple, varname,typeid(TYP),tags.index[i],dim1,dim2,min,max,null);
    00224         break;
    00225       default:
    00226         break;
    00227       }
    00228       size += dim1 * dim2 * ((sizeof(TYP) < 4) ? 4 : sizeof(TYP));
    00229     }
    00230     return col;
    00231   }
    00232
    00233   INSTANTIATE(float)
    00234   INSTANTIATE(double)
    00235   INSTANTIATE(bool)
    00236   INSTANTIATE(char)
    00237   INSTANTIATE(int)
    00238   INSTANTIATE(short)
    00239   INSTANTIATE(long)
    00240   INSTANTIATE(unsigned char)
    00241   INSTANTIATE(unsigned int)
    00242   INSTANTIATE(unsigned short)
    00243   INSTANTIATE(unsigned long)
    00244     //  INSTANTIATE(IOpaqueAddress*)
    00245
    00246     INTupleItem* createNTupleItem (NTUPLEINFO& tags, long i, INTuple* tuple, IOpaqueAddress* /* minimum */, IOpaqueAddress* /* maximum */, long& size)   {
    00247     size = 0;
    00248     INTupleItem* col = 0;
    00249     long ind = tags.getIndex(tags.index[i]);
    00250     long dim1 = 1, dim2 = 1;
    00251
    00252     std::string varname;
    00253     std::string blkname = tags.block[i];
    00254
    00255     if (blkname != "AUTO_BLK") {
    00256       varname = blkname + "/" + tags.name[i];
    00257     } else {
    00258       varname = tags.name[i];
    00259     }
    00260
    00261     switch( tags.dim[i] )   {
    00262     case 0:
    00263       col = NTuple::_Item<IOpaqueAddress*>::create (tuple, varname,typeid(IOpaqueAddress*), 0,(IOpaqueAddress*)0xffffffff,0);
    00264       break;
    00265     case 1:
    00266       dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
    00267       col = NTuple::_Array<IOpaqueAddress*>::create (tuple, varname,typeid(IOpaqueAddress*),
    00268                                                      tags.index[i], dim1, 0,(IOpaqueAddress*)0xffffffff,0);
    00269       break;
    00270     case 2:
    00271       dim1 = (ind>=0) ? tags.idist(ind) : tags.theDim[i][0];
    00272       dim2 = (ind>=0) ? tags.theDim[i][0] : tags.theDim[i][1];
    00273       col = NTuple::_Matrix<IOpaqueAddress*>::create (tuple, varname,typeid(IOpaqueAddress*),
    00274                                                       tags.index[i],dim1,dim2,0,(IOpaqueAddress*)0xffffffff,0);
    00275       break;
    00276     default:
    00277       break;
    00278     }
    00279     size += dim1 * dim2 * ((sizeof(IOpaqueAddress*) < 4) ? 4 : sizeof(IOpaqueAddress*));
    00280     return col;
    00281   }
    00282 };