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

# HRWNTupleCnv.cpp

Go to the documentation of this file.

    00001 // $Header: /local/reps/Gaudi/HbookCnv/src/HRWNTupleCnv.cpp,v 1.5 2006/01/10 20:11:14 hmd Exp $
    00002 #define HBOOKCNV_HRWNTUPLECNV_CPP
    00003
    00004
    00005 #define ALLOW_ALL_TYPES
    00006
    00007 // Compiler include files
    00008 #include <cstdio>
    00009
    00010 // Include files
    00011 #include "GaudiKernel/xtoa.h"
    00012 #include "GaudiKernel/CnvFactory.h"
    00013 #include "GaudiKernel/GenericAddress.h"
    00014 #include "GaudiKernel/INTupleSvc.h"
    00015
    00016 #include "GaudiKernel/MsgStream.h"
    00017 #include "GaudiKernel/NTuple.h"
    00018
    00019 #include "NTupleInfo.h"
    00020 #include "HRWNTupleCnv.h"
    00021 #include "HbookDef.h"
    00022
    00023 //------------------------------------------------------------------------------
    00024 //
    00025 // Implementation of class :  HbookCnv::HRWNTupleCnv
    00026 //
    00027 // Author :                   Markus Frank
    00028 //
    00029 //------------------------------------------------------------------------------
    00030
    00031
    00032 // Instantiation of a static factory class used by clients to create
    00033 // instances of this service
    00034 DECLARE_NAMESPACE_CONVERTER_FACTORY(HbookCnv,HRWNTupleCnv)
    00035
    00036
    00037 StatusCode HbookCnv::HRWNTupleCnv::book(long idh, const std::string& loc, INTuple* nt)  {
    00038   MsgStream log(msgSvc(), "HbookRWNTupleCnv");
    00039   if ( !::HEXIST(idh) )    {
    00040     const INTuple::ItemContainer& items = nt->items();
    00041     NTUPLEINFO tags;
    00042     float buffer[512];
    00043     memset(&tags,0, sizeof(tags));
    00044     tags.id = idh;
    00045     tags.numVar = items.size();
    00046     strncpy(tags.rzdir, loc.data(), loc.length());
    00047     strncpy(tags.title, nt->title().data(), nt->title().length());
    00048     for (size_t length = items.size(), i = 0; i < length; i++ )    {
    00049       long len = items[i]->name().length();
    00050       long nam_len = (len > 8) ? 8 : len;
    00051       long pad_len=sizeof(tags.name[i])-nam_len-1;
    00052       ::strncpy(tags.name[i], items[i]->name().data(), nam_len);
    00053       ::memset(&tags.name[i][nam_len], ' ', pad_len);
    00054       buffer[i] = float(items[i]->type());
    00055     }
    00056     StatusCode status = ::RWNT_BOOK(tags);
    00057     if ( status.isSuccess() )   {
    00058       log << MSG::INFO << "Booked Row wise HBOOK N tuple with ID:" << idh
    00059           << " \"" << nt->title() << "\" in directory <" << loc << ">" << endreq;
    00060       if ( dataTypePolicy() == USE_DATA_TYPES )   {
    00061         ::HFN(idh, buffer);
    00062       }
    00063       log << MSG::DEBUG;
    00064       if ( log.isActive() ) ::HPRINT( idh );
    00065       return StatusCode::SUCCESS;
    00066     }
    00067     else    {
    00068       log << MSG::ERROR << "HBOOK N tuple " << idh
    00069           << "\"" << nt->title()
    00070           << "\" cannot be booked in directory <" << loc << ">" << endreq;
    00071     }
    00072   }
    00073   log << MSG::ERROR << "HBOOK N tuple " << idh << " \"" << nt->title()
    00074       << "\" already exists." << endreq;
    00075   return StatusCode::FAILURE;
    00076 }
    00077
    00078
    00080 StatusCode HbookCnv::HRWNTupleCnv::writeData(long idh, INTuple* nt)   {
    00081   const INTuple::ItemContainer& items = nt->items();
    00082   INTuple::ItemContainer::const_iterator i;
    00083   float buffer[512], *buff = buffer;
    00084   for (i = items.begin(); i != items.end(); i++)    {
    00085     switch( (*i)->type() )   {
    00086     case DataTypeInfo::BOOL:              // bool
    00087       *buff++ = float( *(bool*)(*i)->buffer() );
    00088       break;
    00089     case DataTypeInfo::CHAR:              // char
    00090       *buff++ = float( *(char*)(*i)->buffer() );
    00091       break;
    00092     case DataTypeInfo::SHORT:             // short
    00093       *buff++ = float( *(short*)(*i)->buffer() );
    00094       break;
    00095     case DataTypeInfo::INT:               // int
    00096       *buff++ = float( *(int*)(*i)->buffer() );
    00097       break;
    00098     case DataTypeInfo::LONG:              // long
    00099       *buff++ = float( *(long*)(*i)->buffer() );
    00100       break;
    00101     case DataTypeInfo::UCHAR:             // unsigned char
    00102       *buff++ = float( *(unsigned char*)(*i)->buffer() );
    00103       break;
    00104     case DataTypeInfo::USHORT:            // unsigned short
    00105       *buff++ = float( *(unsigned short*)(*i)->buffer() );
    00106       break;
    00107     case DataTypeInfo::UINT:              // unsigned int
    00108       *buff++ = float( *(unsigned int*)(*i)->buffer() );
    00109       break;
    00110     case DataTypeInfo::ULONG:             // unsigned long
    00111       *buff++ = float( *(unsigned long*)(*i)->buffer() );
    00112       break;
    00113     case DataTypeInfo::FLOAT:             // float
    00114       *buff++ = *(float*)((*i)->buffer());
    00115       break;
    00116     case DataTypeInfo::DOUBLE:            // double
    00117       *buff++ = float( *(double*)(*i)->buffer() );
    00118       break;
    00119     default:
    00120       *buff++ = 0.0;
    00121       break;
    00122     }
    00123     (*i)->reset();
    00124   }
    00125   ::HFN(idh, buffer);
    00126   return StatusCode::SUCCESS;
    00127 }
    00128
    00129
    00131 StatusCode HbookCnv::HRWNTupleCnv::readData(long id, INTuple* pObject, long ievt)    {
    00132   long istat = 0, numevt = 0, idn = 0;
    00133   float buffer[512], *buff = buffer;
    00134   StatusCode status = StatusCode::SUCCESS;
    00135   INTuple::ItemContainer& items = pObject->items();
    00136   ievt++;
    00137   if ( dataTypePolicy() == USE_DATA_TYPES )   {
    00138     ievt++;
    00139   }
    00140 //  ::HGNF(id, ievt, buffer, istat);
    00141   ::HGN(id, idn, ievt, buffer, istat);
    00142   ::HNOENT( id, numevt);
    00143   if ( istat == 0 && ievt <= numevt )   {
    00144     INTuple::ItemContainer::iterator i;
    00145     for (i = items.begin(); i != items.end(); i++)    {
    00146       try   {
    00147         long item_type = (*i)->type();
    00148         switch( item_type )   {
    00149         case DataTypeInfo::BOOL:              // bool
    00150           (dynamic_cast< NTuple::_Item<bool>* >(*i))->set((*buff++ < 0.000000001) ? false : true);
    00151           break;
    00152         case DataTypeInfo::CHAR:              // char
    00153           (dynamic_cast< NTuple::_Item<char>* >(*i))->set(char(*buff++));
    00154           break;
    00155         case DataTypeInfo::SHORT:             // short
    00156           (dynamic_cast< NTuple::_Item<short>* >(*i))->set(short(*buff++));
    00157           break;
    00158         case DataTypeInfo::INT:               // int
    00159           (dynamic_cast< NTuple::_Item<int>* >(*i))->set(int(*buff++));
    00160           break;
    00161         case DataTypeInfo::LONG:              // long
    00162           (dynamic_cast< NTuple::_Item<long>* >(*i))->set(long(*buff++));
    00163           break;
    00164         case DataTypeInfo::UCHAR:             // unsigned char
    00165           (dynamic_cast< NTuple::_Item<unsigned char>* >(*i))->set((unsigned char)(*buff++));
    00166           break;
    00167         case DataTypeInfo::USHORT:            // unsigned short
    00168           (dynamic_cast< NTuple::_Item<unsigned short>* >(*i))->set((unsigned short)(*buff++));
    00169           break;
    00170         case DataTypeInfo::UINT:              // unsigned int
    00171           (dynamic_cast< NTuple::_Item<unsigned int>* >(*i))->set((unsigned int)(*buff++));
    00172           break;
    00173         case DataTypeInfo::ULONG:             // unsigned long
    00174           (dynamic_cast< NTuple::_Item<unsigned long>* >(*i))->set((unsigned long)(*buff++));
    00175           break;
    00176         case DataTypeInfo::FLOAT:             // float
    00177           (dynamic_cast< NTuple::_Item<float>* >(*i))->set(float(*buff++));
    00178           break;
    00179         case DataTypeInfo::DOUBLE:            // double
    00180           (dynamic_cast< NTuple::_Item<double>* >(*i))->set(double(*buff++));
    00181           break;
    00182         case DataTypeInfo::OBJECT_ADDR:
    00183           (dynamic_cast< NTuple::_Item<IOpaqueAddress*>* >(*i))->set(0);
    00184           break;
    00185         default:
    00186           buff++;
    00187           break;
    00188         }
    00189       }
    00190       catch (...)     {
    00191         status = StatusCode::FAILURE;
    00192       }
    00193     }
    00194     return status;
    00195   }
    00196   return StatusCode::FAILURE;
    00197 }
    00198
    00199
    00200 #define CREATE_RWNTUPLE_COL(i,tuple,TYP) {                            \
    00201 item = createNTupleItem(tags, i, tuple, NTuple::Range<TYP>::min(), NTuple::Range<TYP>::max(), size); }
    00202
    00204 StatusCode HbookCnv::HRWNTupleCnv::load(long idh, INTuple*& refpObject)   {
    00205   MsgStream log(msgSvc(), "HbookNTupleCnv");
    00206   NTUPLEINFO tags;
    00207
    00208   memset(&tags,0,sizeof(tags));
    00209   tags.id = idh;
    00210   StatusCode status = ::RWNT_INFO(tags);
    00211   if ( status.isSuccess() && tags.numEnt > 0 )     {
    00212     NTuple::Tuple* pObj = 0;
    00213     status = m_ntupleSvc->create(CLID_RowWiseTuple, tags.title, pObj);
    00214     INTuple* ntup = dynamic_cast<INTuple*>(pObj);
    00215     if ( status.isSuccess() && 0 != ntup)   {
    00216       long size = 0, istat = 0;
    00217       std::string typ = "NONE";
    00218       float buffer[512];
    00219       std::string call = "HRWNTupleCnv::load";
    00220       ::HGNPAR( idh, call.data(), call.length());
    00221       // Make info compatible with column wise N-tuples
    00222       for ( int i = 0; i < tags.numVar; i++ )   {
    00223         ::strcpy(tags.block[i],"AUTO_BLK");
    00224       }
    00225       if ( dataTypePolicy() == USE_DATA_TYPES )   {
    00226         ::HGNF(idh, 1, buffer, istat);
    00227       }
    00228       else if ( dataTypePolicy() == FORTRAN_STYLE )   {
    00229         for ( int i = 0; i < tags.numVar; i++ )   {
    00230           char c1 = ::toupper(tags.name[i][0]);
    00231           if ( c1 >= 'I' && c1 <= 'N' )
    00232             buffer[i] = DataTypeInfo::INT;
    00233           else
    00234             buffer[i] = DataTypeInfo::FLOAT;
    00235         }
    00236       }
    00237       else if ( dataTypePolicy() == HUNGARIAN_STYLE )   {
    00238         for ( int i = 0; i < tags.numVar; i++ )   {
    00239           char c1 = ::toupper(tags.name[i][0]);
    00240           char c2 = ::toupper(tags.name[i][1]);
    00241           switch(c1)    {
    00242           case 'B':   buffer[i] = DataTypeInfo::BOOL;             break;
    00243           case 'C':   buffer[i] = DataTypeInfo::CHAR;             break;
    00244           case 'S':   buffer[i] = DataTypeInfo::SHORT;            break;
    00245           case 'I':   buffer[i] = DataTypeInfo::INT;              break;
    00246           case 'L':   buffer[i] = DataTypeInfo::LONG;             break;
    00247           case 'F':   buffer[i] = DataTypeInfo::FLOAT;            break;
    00248           case 'D':   buffer[i] = DataTypeInfo::DOUBLE;           break;
    00249           case 'O':   buffer[i] = DataTypeInfo::OBJECT_ADDR;      break;
    00250           case 'U':
    00251             switch(c2)    {
    00252             case 'C':   buffer[i] = DataTypeInfo::UCHAR;          break;
    00253             case 'S':   buffer[i] = DataTypeInfo::USHORT;         break;
    00254             case 'I':   buffer[i] = DataTypeInfo::UINT;           break;
    00255             case 'L':   buffer[i] = DataTypeInfo::ULONG;          break;
    00256             default:
    00257             log << MSG::ERROR << "Cannot Create Item for Var[" << i << "]: "
    00258                               << " Name:" << tags.name[i]
    00259                               << " Unknown data type" << endreq;
    00260             return StatusCode::FAILURE;
    00261             }
    00262             break;
    00263           default:
    00264             log << MSG::ERROR << "Cannot Create Item for Var[" << i << "]: "
    00265                               << " Name:" << tags.name[i]
    00266                               << " Unknown data type" << endreq;
    00267             return StatusCode::FAILURE;
    00268           }
    00269         }
    00270       }
    00271       else    {
    00272         for ( int i = 0; i < tags.numVar; i++ )   {
    00273           buffer[i] = float(DataTypeInfo::FLOAT);
    00274         }
    00275       }
    00276       if ( istat == 0 )   {
    00277         for ( int i = 0; i < tags.numVar; i++ )   {
    00278           INTupleItem* item = 0;
    00279           switch( long(buffer[i]) )   {
    00280             case DataTypeInfo::BOOL:              // bool
    00281               CREATE_RWNTUPLE_COL(i,ntup, bool)
    00282               break;
    00283             case DataTypeInfo::CHAR:              // char
    00284               CREATE_RWNTUPLE_COL(i,ntup, char)
    00285               break;
    00286             case DataTypeInfo::SHORT:             // short
    00287               CREATE_RWNTUPLE_COL(i,ntup, short)
    00288               break;
    00289             case DataTypeInfo::INT:               // int
    00290               CREATE_RWNTUPLE_COL(i,ntup, int)
    00291               break;
    00292             case DataTypeInfo::LONG:              // long
    00293               CREATE_RWNTUPLE_COL(i,ntup, long)
    00294               break;
    00295             case DataTypeInfo::UCHAR:             // unsigned char
    00296               CREATE_RWNTUPLE_COL(i,ntup, unsigned char)
    00297               break;
    00298             case DataTypeInfo::USHORT:            // unsigned short
    00299               CREATE_RWNTUPLE_COL(i,ntup, unsigned short)
    00300               break;
    00301             case DataTypeInfo::UINT:              // unsigned int
    00302               CREATE_RWNTUPLE_COL(i,ntup, unsigned int)
    00303               break;
    00304             case DataTypeInfo::ULONG:             // unsigned long
    00305               CREATE_RWNTUPLE_COL(i,ntup, unsigned long)
    00306               break;
    00307             case DataTypeInfo::FLOAT:             // float
    00308               CREATE_RWNTUPLE_COL(i,ntup, float)
    00309               break;
    00310             case DataTypeInfo::DOUBLE:            // double
    00311               CREATE_RWNTUPLE_COL(i,ntup, double)
    00312               break;
    00313             case DataTypeInfo::OBJECT_ADDR:
    00314               //CREATE_RWNTUPLE_COL(i,ntup, IOpaqueAddress*)
    00315               break;
    00316             default:
    00317               break;
    00318           }
    00319           if ( item )    {
    00320             ntup->items().push_back(item);
    00321             log << MSG::DEBUG << "Create Var[" << i << "]: "
    00322                 << item->typeName() << "[1]"
    00323                 << " Name:" << tags.name[i]
    00324                 << "[" << tags.frange[i][0] << "," << tags.frange[i][1]
    00325                 << "]" << endreq;
    00326           }
    00327           else    {
    00328             log << MSG::ERROR << "Cannot Create Item for Var[" << i << "]: "
    00329                               << " Name:" << tags.name[i] << endreq;
    00330           }
    00331         }
    00332         log << MSG::DEBUG << "Total buffer size of NTuple: " << size << " Bytes." << endreq;
    00333         refpObject = ntup;
    00334         return StatusCode::SUCCESS;
    00335       }
    00336     }
    00337   }
    00338   return StatusCode::FAILURE;
    00339 }