00001
00002
00003
00004
00005
00006
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
00046
00047 std::string pTypeName = "";
00048 {
00049
00050
00051 CdbTransaction readOnlyTransaction( CdbBdbShared::technology( ),
00052 CdbBdbShared::implementation( ));
00053
00054
00055
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
00074
00075
00076
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
00102
00103
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
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
00125
00126 R* outputTuplePtr = new R( nColumns,
00127 name,
00128 description ,
00129 columnNames);
00130
00131
00132
00133
00134
00135 for( unsigned int r = 0; r < nRows; ++r ) {
00136
00137
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
00160
00161
00162
00163
00164
00165
00166
00167
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
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
00187
00188
00189
00190 theOutputObjectPtr = outputTuplePtr;
00191
00192 return CdbStatus::Success;
00193 }
00194
00195
00196
00197