Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbBdb2RooNTupleConverter.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdb2RooNTupleConverter.cc,v 1.1 2005/04/23 06:29:43 gapon Exp $
00003 
00004 /// The implementation of the CdbBdb2RooNTupleConverter class.
00005 /**
00006   * @see CdbBdb2RooNTupleConverter
00007   */
00008 #include "BaBar/BaBar.hh"
00009 
00010 #include "CdbRooConversionFwk/CdbBdb2RooNTupleConverter.hh"
00011 
00012 #include "CdbRooTable/CdbRooNTupleBaseR.hh"
00013 #include "CdbRooTable/CdbRooNTupleR.hh"
00014 
00015 #include "CdbBase/CdbTransaction.hh"
00016 
00017 #include "CdbBdb/CdbBdbSchemaUtils.hh"
00018 
00019 #include "CdbBdbShared/CdbBdbShared.hh"
00020 
00021 #include "CdbBdbTable/CdbBdbNTupleBaseP.hh"
00022 #include "CdbBdbTable/CdbBdbNTupleP.hh"
00023 
00024 #include "ErrLogger/ErrLog.hh"
00025 
00026 #include <TClass.h>
00027 
00028 #include <string>
00029 #include <vector>
00030 #include <iostream>
00031 using std::endl;
00032 
00033 template< class P,
00034           class R,
00035           class P_ELEMENT_CONVERSION_RULES,
00036           class R_ELEMENT_CONVERSION_RULES >
00037 CdbBdb2RooNTupleConverter< P,
00038                            R,
00039                            P_ELEMENT_CONVERSION_RULES,
00040                            R_ELEMENT_CONVERSION_RULES >::CdbBdb2RooNTupleConverter( ) :
00041     CdbBdb2RooPayloadConverter<P,R>( )
00042 {
00043     const char* fatalStr = "CdbBdb2RooNTupleConverter::CdbBdb2RooNTupleConverter( ) -- FATAL";
00044 
00045   // Get and verify the 'P' type name
00046 
00047     std::string pTypeName = "";
00048     {
00049       // To get types we need a transaction.
00050 
00051         CdbTransaction readOnlyTransaction( CdbBdbShared::technology( ),
00052                                             CdbBdbShared::implementation( ));
00053 
00054       // ATTENTION: If 'P' is not a valid persistent type known to the current
00055       //            Objectivity/DB schema then the compilations will abort below.
00056 
00057         ooTypeNumber pTypeNumber = ooTypeN( P );
00058 
00059         if( !CdbBdbSchemaUtils::instance( ).typeNumberToClassName( pTypeNumber,
00060                                                                    pTypeName ))
00061             ErrMsg(fatal) << fatalStr << endl
00062                           << "    Failed to translate the Objectivity/DB type number " << pTypeNumber << " of the" << endl
00063                           << "    passed as a template parameter of the class into the corresponding type name." << endmsg;
00064 
00065         if( !CdbBdbSchemaUtils::instance( ).isA( pTypeNumber,
00066                                                  ooTypeN( CdbBdbNTupleP< typename P::ElementType > )))
00067             ErrMsg(fatal) << fatalStr << endl
00068                           << "    The passed as a parameter of the template Objectivity/DB type number " << pTypeNumber << endl
00069                           << "    doesn't correspond to a persistent class deriving from the abtract model" << endl
00070                           << "    specified by the  CdbBdbNTupleP<T> class with type number " << ooTypeN( CdbBdbNTupleP< typename P::ElementType > ) << endmsg;
00071     }
00072 
00073   // Get and verify the "Roo" type name.
00074   //
00075   // ATTENTION: If 'R' is not a valid persistent type known to the current
00076   //            ROOT/CINT dictionary then the compilations will abort below.
00077 
00078     const std::string rTypeName = R::Class( )->GetName( );
00079     {
00080         if( !R::Class( )->InheritsFrom( CdbRooNTupleR< typename R::ElementType >::Class( )))
00081             ErrMsg(fatal) << fatalStr << endl
00082                           << "    The passed as a parameter of the template CINT/ROOT persistent type doesn't" << endl
00083                           << "    derive from \"" << CdbRooNTupleR< typename R::ElementType >::Class( )->ClassName( ) << "\"," << endl
00084                           << "    which is base class of N-Tuples in the \"Roo\" technology." << endmsg;
00085     }
00086 }
00087 
00088 template< class P,
00089           class R,
00090           class P_ELEMENT_CONVERSION_RULES,
00091           class R_ELEMENT_CONVERSION_RULES >
00092 CdbStatus
00093 CdbBdb2RooNTupleConverter< P,
00094                            R,
00095                            P_ELEMENT_CONVERSION_RULES,
00096                            R_ELEMENT_CONVERSION_RULES >::userDefinedConversion( const BdbRef(BdbObject)& theInputObjectRef,
00097                                                                                 R*&                      theOutputObjectPtr ) const
00098 {
00099     const char* errorStr = "<anonymous>::CdbBdb2RooNTupleConverter::userDefinedConversion( )";
00100 
00101   // The following (isValid) test will make sure that a caller passes a non-zero
00102   // object of the proper type. Objectivity/Db will check the type compatibility
00103   // during this assignment.
00104 
00105     BdbHandle( P ) inputObjectH = (const BdbRef( P )&)theInputObjectRef;
00106     if( !inputObjectH.isValid( )) {
00107         ErrMsg(error) << errorStr << endl
00108                       << "    An invalid oject passed to the procedure." << endmsg;
00109         return CdbStatus::Error;
00110     }
00111 
00112   // Get the parameters of the input tuple
00113 
00114     unsigned int nColumns = inputObjectH->columns( );
00115 
00116     std::string name        = inputObjectH->name( );
00117     std::string description = inputObjectH->description( );
00118 
00119     std::vector<std::string> columnNames;
00120     inputObjectH->column_names( columnNames );
00121 
00122     unsigned int nRows = inputObjectH->rows( );
00123 
00124   // Create the output tuple of the final type
00125 
00126     R* outputTuplePtr = new R( nColumns,
00127                                name,
00128                                description ,
00129                                columnNames);
00130 
00131   // Do the actual conversion
00132   //
00133   // ATTENTION: Watch for the conversin of element types.
00134 
00135     for( unsigned int r = 0; r < nRows; ++r ) {
00136 
00137       // Get the input row.
00138 
00139         std::vector< typename P::ElementType > pRow;
00140 
00141         if( CdbStatus::Success != inputObjectH->get_row( pRow,
00142                                                          r )) {
00143             ErrMsg(error) << errorStr << endl
00144                           << "    Failed to get a row number " << r << " from a persistent tuple" << endl
00145                           << "    object " << inputObjectH.sprint( ) << endmsg;
00146 
00147             return CdbStatus::Error;
00148         }
00149         if( pRow.size( ) != nColumns ) {
00150             ErrMsg(error) << errorStr << endl
00151                           << "    The row number " << r << " got from an input persistent tuple object " << inputObjectH.sprint( ) << endl
00152                           << "    has incorrect length " << pRow.size( ) << " instead of expected " << nColumns << "." << endl
00153                           << "    The input object may be corrupted or its class may have been incorrectly" << endl
00154                           << "    implemented." << endmsg;
00155 
00156             return CdbStatus::Error;
00157         }
00158 
00159       // Translate the above obtained row into the one whose elements
00160       // have the output type type.
00161       //
00162       // NOTE: As an intermediate step of this conversion we'll be doing the inderect (through
00163       //       an intermediate "mediator" transient type of elements) translation of elements.
00164       //
00165       //       Any compilation errors at the conversion statement would mean possible
00166       //       incompatibility between element type converters, or wrong types passed
00167       //       as arameters of the current template.
00168 
00169         std::vector< typename R::ElementType > rRow( nColumns );
00170 
00171         for( unsigned int c = 0; c < nColumns; ++c )
00172             rRow[c] = R_ELEMENT_CONVERSION_RULES::to_persistent( P_ELEMENT_CONVERSION_RULES::to_transient( pRow[c] ));
00173 
00174       // Store the converted row into the output tuple
00175 
00176         if( CdbStatus::Success != outputTuplePtr->append_row( rRow )) {
00177 
00178             ErrMsg(error) << errorStr << endl
00179                           << "    Failed to append a row number " << r << " to an persistent tuple object." << endmsg;
00180 
00181             delete outputTuplePtr;
00182             return CdbStatus::Error;
00183         }
00184     }
00185 
00186   // RETURNED OBJECT OWNERHIP NOTE:
00187   //
00188   //    The ownership is returned to a caller along with the object.
00189 
00190     theOutputObjectPtr = outputTuplePtr;
00191 
00192     return CdbStatus::Success;
00193 }
00194 
00195 /////////////////
00196 // End Of File //
00197 /////////////////

Generated on Mon Dec 5 18:21:59 2005 for CDB by doxygen1.3-rc3