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

CdbBdb2RooPayloadConversionTool.cc

Go to the documentation of this file.
00001 // A tool for populating ROOT I/O based database installations with user defined
00002 // "payload' objects taken from "Bdb" based installations (teh "Shared" technology).
00003 
00004 #include "BaBar/BaBar.hh"
00005 
00006 #include "BdbCond/BdbObject.hh"
00007 
00008 #include "CdbBase/CdbTransaction.hh"
00009 
00010 #include "CdbBdbShared/CdbBdbShared.hh"
00011 
00012 #include "CdbBdbTable/CdbBdbNTupleSimpleImplP_PrimitiveTypes.hh"
00013 
00014 #include "CdbRooReadonly/CdbRooReadonly.hh"
00015 
00016 #include "CdbRooTable/CdbRooNTupleSimpleImplR.hh"
00017 
00018 #include "CdbRooTests/CdbRooTestClassR.hh"
00019 
00020 #include "CdbRooConversionFwk/CdbRooConverterBase.hh"
00021 #include "CdbRooConversionFwk/CdbBdb2RooNTupleConverter.hh"
00022 #include "CdbRooConversionFwk/CdbBdb2RooPayloadConversionFwk.hh"
00023 #include "CdbRooConversionFwk/CdbBdb2RooPayloadMrgConverter.hh"
00024 
00025 #include <stdio.h>  // sscanf()
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 #include <iostream>
00031 using std::cin;
00032 using std::cout;
00033 using std::cerr;
00034 using std::endl;
00035 
00036 #include <sstream>
00037 using std::ostringstream;
00038 
00039 namespace {
00040 
00041   // Parameters of the command. They're passed via command line.
00042   
00043     bool       verboseModeFlag = false;
00044     bool         debugModeFlag = false;
00045     bool doNotCompressModeFlag = false;
00046 
00047   /// Loader for ROOT I/O dictionary of persistent classes
00048   /**
00049     * Default constructors of the 'payload' classes to be loaded from CDB
00050     * needs to be executed at least one to bring in the corresponding dictionary.
00051     * Otherwise the loaded objects will have incomplete shape (they'll be TObject-s only).
00052     */
00053     void loadPayloadClassesDictionary( )
00054     {
00055         CdbRooNTupleSimpleImplR< UInt_t   > test3;
00056         CdbRooNTupleSimpleImplR< Double_t > test4;
00057     }
00058 
00059   // Helpers meant to translate command line parameters
00060 
00061     bool translate_UShort_t( UShort_t&   theValue,
00062                              const char* theString )
00063     {
00064         assert( 0 != theString );
00065 
00066         unsigned value;
00067         if( 1 != sscanf( theString, "%u", &value )) return false;
00068         if( value > 0xFFFF ) return false;
00069 
00070         theValue = (UShort_t)value;
00071 
00072         return true;
00073     }
00074 
00075     bool translate_bool( bool&       theValue,
00076                          const char* theString )
00077     {
00078         assert( 0 != theString );
00079 
00080         unsigned value;
00081         if( 1 != sscanf( theString, "%u", &value )) return false;
00082 
00083         theValue = ( value != 0 );
00084 
00085         return true;
00086      }
00087 
00088   // Report the right syntax of the commandline parameters
00089 
00090     void usage( const std::string& theMessage = "" )
00091     {
00092         if( theMessage != "" ) cerr << theMessage << endl;
00093         cerr << "Usage: testing       <origin_id> <condition_id> <0|1> <partition_id> <cluster_id> <increment_id>" << endl
00094              << "       testing_merge <origin_id> <condition_id> <0|1> <partition_id> <cluster_id> <increment_id>" << endl
00095              << "       conversion    <definition_file>" << endl
00096              << "       [-verbose] [-debug] [-do_not_compress]" << endl;
00097     }
00098 
00099   /// Simple converter to test merging multiple conditions
00100 
00101     class SimpleMergingConverter : public CdbBdb2RooPayloadMrgConverter< CdbBdbNTupleSimpleImplP< ooFloat64 >,
00102                                                                          CdbRooTestClassR > {
00103     public:
00104 
00105       /// Implement a user-defined conversion (merged version)
00106 
00107         virtual CdbStatus userDefinedConversion( const BdbRef(BdbObject)&                theMainInputObjectRef,
00108                                                  const std::vector< BdbRef(BdbObject) >& theExtraObjectsList,
00109                                                  CdbRooTestClassR*&                      theOutputObjectPtr ) const
00110         {
00111             ostringstream description;
00112             description << "Object constructed from: " << theMainInputObjectRef.sprint( );
00113             for( unsigned int i = 0; i < theExtraObjectsList.size( ); ++i ) {
00114                 description << ", " << theExtraObjectsList[i].sprint( );
00115             }
00116             theOutputObjectPtr = new CdbRooTestClassR( description.str( ));
00117             return CdbStatus::Success;
00118         }
00119     };
00120 }
00121 
00122 ////////////////////////////////////
00123 // Start execution of the program //
00124 ////////////////////////////////////
00125 
00126 int
00127 main( int argc, char* argv[] )
00128 {
00129 loadPayloadClassesDictionary();
00130 
00131   // Translate input parameters
00132 
00133     enum CommandType { CMD_TESTING = 0,
00134                        CMD_TESTING_MERGE,
00135                        CMD_CONVERSION
00136     };
00137     CommandType command = CMD_TESTING;
00138 
00139     int numArgs = argc - 1;
00140     int nextArg = 1;
00141 
00142     UShort_t originId            = 0;
00143     UShort_t conditionId         = 0;
00144     bool     isPartitionableFlag = false;
00145     UShort_t partitionId         = 0;
00146     UShort_t clusterId           = 0;
00147     UShort_t incrementId         = 0;
00148 
00149     const char* definitionFile = 0;
00150 
00151     if( 0 == numArgs ) {
00152         usage( );
00153         return 1;
00154     }
00155     if( numArgs > 0 ) {
00156 
00157         const std::string commandName = argv[nextArg];
00158         nextArg++;
00159         numArgs--;
00160 
00161         if( commandName == "testing" ) {
00162 
00163             command = CMD_TESTING;
00164 
00165             if( numArgs >= 6 ) {
00166 
00167                 if( !translate_UShort_t( originId,
00168                                          argv[nextArg++] )) {
00169                     usage( "failed to translate the <origin_id> parameter of the command" );
00170                     return 1;
00171                 }
00172                 numArgs--;
00173 
00174                 if( !translate_UShort_t( conditionId,
00175                                          argv[nextArg++] )) {
00176                     usage( "failed to translate the <condition_id> parameter of the command" );
00177                     return 1;
00178                 }
00179                 numArgs--;
00180 
00181                 if( !translate_bool( isPartitionableFlag,
00182                                      argv[nextArg++] )) {
00183                     usage( "failed to translate the condition type flag (allowed values: 0|1) parameter of the command" );
00184                     return 1;
00185                 }
00186                 numArgs--;
00187 
00188                 if( !translate_UShort_t( partitionId,
00189                                          argv[nextArg++] )) {
00190                     usage( "failed to translate the <partition_id> parameter of the command" );
00191                     return 1;
00192                 }
00193                 numArgs--;
00194 
00195                 if( !translate_UShort_t( clusterId,
00196                                          argv[nextArg++] )) {
00197                     usage( "failed to translate the <cluster_id> parameter of the command" );
00198                     return 1;
00199                 }
00200                 numArgs--;
00201 
00202                 if( !translate_UShort_t( incrementId,
00203                                          argv[nextArg++] )) {
00204                     usage( "failed to translate the <increment_id> parameter of the command" );
00205                     return 1;
00206                 }
00207                 numArgs--;
00208 
00209             } else {
00210                 usage( "illegal number of parameters for the command" );
00211                 return 1;
00212             }
00213 
00214         } else if( commandName == "testing_merge" ) {
00215 
00216             command = CMD_TESTING_MERGE;
00217 
00218             if( numArgs >= 6 ) {
00219 
00220                 if( !translate_UShort_t( originId,
00221                                          argv[nextArg++] )) {
00222                     usage( "failed to translate the <origin_id> parameter of the command" );
00223                     return 1;
00224                 }
00225                 numArgs--;
00226 
00227                 if( !translate_UShort_t( conditionId,
00228                                          argv[nextArg++] )) {
00229                     usage( "failed to translate the <condition_id> parameter of the command" );
00230                     return 1;
00231                 }
00232                 numArgs--;
00233 
00234                 if( !translate_bool( isPartitionableFlag,
00235                                      argv[nextArg++] )) {
00236                     usage( "failed to translate the condition type flag (allowed values: 0|1) parameter of the command" );
00237                     return 1;
00238                 }
00239                 numArgs--;
00240 
00241                 if( !translate_UShort_t( partitionId,
00242                                          argv[nextArg++] )) {
00243                     usage( "failed to translate the <partition_id> parameter of the command" );
00244                     return 1;
00245                 }
00246                 numArgs--;
00247 
00248                 if( !translate_UShort_t( clusterId,
00249                                          argv[nextArg++] )) {
00250                     usage( "failed to translate the <cluster_id> parameter of the command" );
00251                     return 1;
00252                 }
00253                 numArgs--;
00254 
00255                 if( !translate_UShort_t( incrementId,
00256                                          argv[nextArg++] )) {
00257                     usage( "failed to translate the <increment_id> parameter of the command" );
00258                     return 1;
00259                 }
00260                 numArgs--;
00261 
00262             } else {
00263                 usage( "illegal number of parameters for the command" );
00264                 return 1;
00265             }
00266 
00267         } else if( commandName == "conversion" ) {
00268 
00269             command = CMD_CONVERSION;
00270 
00271             if( numArgs >= 1 ) {
00272                 definitionFile = argv[nextArg];
00273                 nextArg++;
00274                 numArgs--;
00275             } else {
00276                 usage( "illegal number of parameters for the command" );
00277                 return 1;
00278             }
00279 
00280         } else {
00281             usage( "unknown command" );
00282             return 1;
00283         }
00284     }
00285 
00286   // Translate optional arguments
00287 
00288     while( numArgs > 0 ) {
00289         if( std::string( "-verbose" ) == argv[nextArg] ) {
00290 
00291             verboseModeFlag = true;
00292 
00293             numArgs --;
00294             nextArg ++;
00295 
00296         } if( std::string( "-debug" ) == argv[nextArg] ) {
00297 
00298             debugModeFlag = true;
00299 
00300             numArgs --;
00301             nextArg ++;
00302 
00303         } if( std::string( "-do_not_compress" ) == argv[nextArg] ) {
00304 
00305             doNotCompressModeFlag = true;
00306 
00307             numArgs --;
00308             nextArg ++;
00309 
00310         } else {
00311             cerr << "unknown command option or parameter: \"" << argv[nextArg] << "\"." << endl;
00312             usage( );
00313             return 1;
00314         }
00315     }
00316     if( numArgs > 0 ) {
00317         usage( "extra parameter which couldn't be recognized by the parser passsed to the procedure" );
00318         return 1;
00319     }
00320 
00321   // Initialize the context of the Objectivity/DB based CDB
00322 
00323     CdbBdbShared::forceLoad( );
00324 
00325     CdbTransaction bdbReadOnlyTransactionStartsHere( CdbBdbShared::technology( ),
00326                                                      CdbBdbShared::implementation( ));
00327 
00328   // Initialize the context of the ROOT I/O based CDB
00329 
00330     CdbRooReadonly::forceLoad( );
00331 
00332     CdbTransaction rooReadOnlyTransactionStartsHere( CdbRooReadonly::technology( ),
00333                                                      CdbRooReadonly::implementation( ));
00334 
00335   // Tell the conversion framework about our preferences regarding
00336   // the "verbose" & "debug" flags.
00337 
00338     CdbBdb2RooPayloadConversionFwk::setVerbose      (         verboseModeFlag );
00339     CdbBdb2RooPayloadConversionFwk::setDebug        (           debugModeFlag );
00340     CdbBdb2RooPayloadConversionFwk::setDoNotCompress(   doNotCompressModeFlag );
00341 
00342   // Instantiate converters in the dictionary. They'll be used by the actual
00343   // conversion sequence to be engaged below.
00344 
00345     CdbStatus result = CdbStatus::Error;
00346     {
00347         CdbCPtr< CdbRooConverterBase > converterPtr( new CdbBdb2RooNTupleConverter< CdbBdbNTupleSimpleImplP< ooFloat64 >,
00348                                                                                                CdbRooNTupleSimpleImplR< Double_t > >( ));
00349 
00350         if( CdbStatus::Success != ( result = CdbBdb2RooPayloadConversionFwk::registerConverter( converterPtr ))) {
00351             cerr << "failed to register the converter for persistent tuples because of: " << result << endl;
00352             return 1;
00353         }
00354         if( verboseModeFlag )
00355             cout << "added converter for \"" << converterPtr->inputClassName( ) << "\" -> \"" << converterPtr->outputClassName( ) << "\"." << endl;
00356     }
00357     {
00358         CdbCPtr< CdbRooConverterBase > converterPtr( new SimpleMergingConverter( ));
00359 
00360         if( CdbStatus::Success != ( result = CdbBdb2RooPayloadConversionFwk::registerConverter( converterPtr ))) {
00361             cerr << "failed to register the converter for persistent tuples because of: " << result << endl;
00362             return 1;
00363         }
00364         if( verboseModeFlag )
00365             cout << "added converter for \"" << converterPtr->inputClassName( ) << "\" -> \"" << converterPtr->outputClassName( ) << "\"." << endl;
00366     }
00367 
00368   // Execite the specified command
00369 
00370     switch( command ) {
00371 
00372     case CMD_TESTING:
00373 
00374         result = CdbBdb2RooPayloadConversionFwk::testConversion( originId,
00375                                                                  conditionId,
00376                                                                  isPartitionableFlag,
00377                                                                  partitionId,
00378                                                                  clusterId,
00379                                                                  incrementId );
00380         return ( CdbStatus::Success == result ? 0 : 1 );
00381 
00382     case CMD_TESTING_MERGE:
00383 
00384         {
00385             cout << "How many other conditions you want to merge in addition to the main one?";
00386 
00387             unsigned int nExtra;
00388             cin >> nExtra;
00389 
00390             if( nExtra < 1 ) {
00391                 cerr << "Wrong number of conditions. Aborting the test." << endl;
00392                 return 1;
00393             }
00394             cout << "Enter the ID-s of these conditions separated by spaces:";
00395 
00396             std::vector<UShort_t> conditionIds( 1, conditionId );
00397 
00398             for( unsigned int i = 0; i < nExtra; ++i ) {
00399                 UShort_t id;
00400                 cin >> id;
00401                 conditionIds.push_back( id );
00402             }
00403 
00404             result = CdbBdb2RooPayloadConversionFwk::testConversion( originId,
00405                                                                      conditionIds,
00406                                                                      isPartitionableFlag,
00407                                                                      partitionId,
00408                                                                      clusterId,
00409                                                                      incrementId );
00410             return ( CdbStatus::Success == result ? 0 : 1 );
00411         }
00412 
00413     case CMD_CONVERSION:
00414 
00415         result = CdbBdb2RooPayloadConversionFwk::doConversion( definitionFile );
00416         return ( CdbStatus::Success == result ? 0 : 1 );
00417     }
00418     return 1;
00419 }
00420 
00421 /////////////////
00422 // End Of File //
00423 /////////////////

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