emcDB_main.C

Go to the documentation of this file.
00001 #include "emcDB_main.h"
00002 #include "emcDB.h"
00003 #include "PHPointerList.h"
00004 #include "PHString.h"
00005 #include "TROOT.h"
00006 #include "emcManageable.h"
00007 #include "emcDataManager.h"
00008 #include "emcRawDataAccessor.h"
00009 #include "PHTimeStamp.h"
00010 #include "emcGains.h"
00011 
00012 #include "convertNormalization.h"
00013 
00014 #include <vector>
00015 #include <string>
00016 
00017 using namespace std;
00018 
00019 //_____________________________________________________________________________
00020 int usage(void) 
00021 {
00022   cout << "emcDB " << endl;
00023   cout << "      --dump [db full name] " << endl
00024        << "        Dump DB on screen. " << endl;
00025   cout << "      --dbms [Objy|Pg(default)]" << endl
00026        << "      Database Management System used (Objy or Postgres)"
00027        << endl;
00028   cout << "      --bankID [int] (use with --dump only) Specify a bankID to look for " << endl;
00029   cout << "      --insertedAfter (with --dump only) \"year,month,....\" " << endl 
00030        << "      --insertedBefore (with --dump only) \"year,month....\" " << endl 
00031        << "        Used to select date interval of bank insertion date." << endl
00032        << endl
00033        << "     --startAfter (with --dump only)" << endl
00034        << "     --startBefore (with --dump only)" << endl
00035        << "     --endAfter (with --dump only)" << endl
00036        << "     --endBefore (with --dump only)" << endl
00037        << "       Like --insertedAfter/Before, but select on Start and End"
00038        << "       Validity dates." << endl;
00039   cout << endl; 
00040   cout << "      --update [directory name] " << endl
00041        << "        Read all calibration data files in the directory " << endl
00042        << "        and write them into DB." << endl;
00043   cout << "      --updatePbGlGains [top] " << endl
00044        << "        Special case of above for PbGlGains, " << endl 
00045        << "        where top is the top directory name," << endl
00046        << "        where to find RunXXXX sub-directories, containing " << endl
00047        << "        normal calibration Gains directory" << endl;
00048 
00049   cout << "      --forceDate \"year,month,day,hour,minute,second\" " << endl
00050        << "        used with --update : specify the start-of-validity " << endl
00051        << "        of all the data to be read from file and written " << endl
00052        << "        into DB." << endl;
00053 
00054   cout << "      --forceEndDate \"year,month,day,hour,minute,second\" " << endl
00055        << "        used with --update : specify the end-of-validity " << endl
00056        << "        of all the data to be read from file and written " << endl
00057        << "        into DB. If not specified, end=infinity" << endl;
00058 
00059   cout << "      --debug" << endl;
00060   cout << "      --batch (non interactive version)" << endl;
00061   cout << endl;
00062   cout << "      --runtimes [minrunnumber] Make a file with relations Run Number<->TimeStamps, starting at minrunnumber" << endl;
00063   cout << endl;
00064   cout << "      --compare [directory] " << endl
00065        << "        (with --dump AND --config) " << endl 
00066        << "        Allow to compare banks from database with banks from " << endl
00067        << "       file(s) to be found under directory." << endl
00068        << "       Several --compare options can be given to compare " << endl
00069        << "       one db bank against several ASCII versions" << endl;
00070   cout << "      --config [gdb.dat] " << endl
00071        << "      Give the name of a gdb.dat configuration file " << endl
00072        << "      To be used to provide FEM Absposition to PinNumber " << endl
00073        << "      relashionship (only needed for --dump with --compare " << endl
00074        << "      for pedestals)" << endl;
00075   cout << "      --convertNormalization data_source " << endl
00076        << "      Convert inical into a format useable by LVL2" << endl;
00077   cout << "      --dbname [name]" << endl
00078        << "      for expert mode : use a different database, e.g. oncal"
00079        << endl;
00080   cout << "      --help "<< endl
00081        << "        This message." << endl;
00082   cout << "      --version " << endl;
00083   cout << "        Version number of this program." << endl;
00084   cout << endl;
00085   cout << "Examples: " << endl;
00086   cout << "  emcDB --dump calib.emc.Gains" << endl;
00087   cout << "  emcDB --update /afs/rhic/phenix/phnxemc/EMC/m_080701/ToF" << endl
00088        << "        --forceDate \"2001,08,01,12,45,00\" " << endl;
00089   cout << endl << endl;
00090   return -1;
00091 }
00092 
00093 //_____________________________________________________________________________
00094 PHTimeStamp* getDate(const string& arg)
00095 {
00096   // Convert a string "year,month,day,hour,min,sec" into a timestamp.
00097   // Brute force method. No checks.
00098 
00099   PHString str(arg.c_str());
00100   PHPointerList<PHString> tok;
00101   
00102   str.split(tok,",");
00103   
00104   int y[6];
00105   size_t j;
00106   
00107   for (j=0;j<tok.length() && j<6;j++) {
00108     y[j] = atoi(tok[j]->getString());
00109   }
00110   
00111   tok.clearAndDestroy();
00112 
00113   return new PHTimeStamp(y[0],y[1],y[2],y[3],y[4],y[5]);
00114 }
00115  
00116 //_____________________________________________________________________________
00117 int test(string config)
00118 {
00119   emcManageable::EStorage data_source = emcManageable::kDB_Objy;
00120 
00121   int status;
00122 
00123   emcRawDataAccessor::GetInstance(status,config.c_str());
00124 
00125   if (!status) {
00126 
00127     emcDataManager* dm = emcDataManager::GetInstance();
00128 
00129     emcGains gains;
00130 
00131     gains.SetSource(data_source);
00132 
00133     emcGains* gaino = 0;
00134 
00135     PHTimeStamp when;
00136     when.setToSystemTime();
00137 
00138     gaino = (emcGains*)(dm->Collect(gains,when));
00139 
00140     if (gaino) {
00141       cout << gaino->GetSize() << " FEMs read" << endl;
00142     }
00143     else {
00144       cerr << "Failed to fetch gain at ts=" << when << endl;
00145     }
00146   }
00147   else {
00148     cerr << "<E> Cannot create RDA" << endl;
00149     return -1;
00150   }
00151   return 0;
00152 }
00153 
00154 
00155 //_____________________________________________________________________________
00156 int main(int argc, char** argv)
00157 {
00158 
00159   //  TROOT root("emcdb","Small EMCAL DB utility program");
00160 
00161   try
00162   {  
00163     bool kDBMS = false; // --dbms option given ?
00164     string dbms = "Pg";
00165     bool kDbname = false; // --dbname option given ?
00166     string dbname = "calibrations";
00167     bool kConfig = false; // --config option given ?
00168     bool kWrite = false; // --write option given ?
00169     string what_to_write = "none";
00170     bool kForceDate = false; // --forceDate option given ?
00171     bool kForceEndDate = false; // --forceEndDate option given ?
00172     bool kDump = false; // --dump option given ?
00173     string what_to_dump;
00174     bool kSimulDBs = false; // --simulDBs
00175     bool kFake = false; // --fake
00176     bool kUpdate = false; // --update
00177     string UpdateDir;
00178     bool kUpdatePbGlGains = false; // --updatePbGlGains
00179     string UpdatePbGlGainsDir;
00180     bool kConvert = false; // --convert
00181     string what_to_convert; 
00182     bool kBatch = false; // --batch
00183     bool kBankID = false; // --bankID
00184     bool kDebug = false; // --debug
00185     bool kRunTimes = false; // --runtimes
00186     int MinRunNumber = 0;
00187     bool kVersion = false; // --version
00188     vector<string> directories; // --compare 
00189     bool kConvertNormalization = false; // --convertNormalization
00190     int ConvertNormalizationSource=0;
00191     bool kSourceDir=false;// --sourceDir
00192     string SourceDir;
00193 
00194     int BankID = -1;
00195 
00196     size_t i;
00197     vector<string> args;
00198 
00199     PHTimeStamp* TforceDate = 0;
00200     PHTimeStamp* TforceEndDate = 0;
00201     PHTimeStamp* TinsertAfter = 0;
00202     PHTimeStamp* TinsertBefore = 0;
00203     PHTimeStamp* TstartAfter = 0;
00204     PHTimeStamp* TstartBefore = 0;
00205     PHTimeStamp* TendAfter = 0;
00206     PHTimeStamp* TendBefore = 0;
00207 
00208     for ( i = 1; i < static_cast<size_t>(argc); i++ ) {
00209       args.push_back(argv[i]);
00210     }
00211 
00212     string config = "FEM.conf";
00213 
00214     i = 0; 
00215     bool error = false;
00216 
00217     if (args.empty()) { return usage(); }
00218 
00219     while ( !error && i < args.size() ) {
00220 
00221       string opt = args[i];
00222       string arg = ".";
00223 
00224       if ( i+1 < args.size() ) { arg = args[i+1]; }
00225 
00226       if ( opt=="--dbname") {
00227         if (arg!=".") {
00228           kDbname=true;
00229           dbname=arg;
00230           i+=2;
00231         }
00232       }
00233       else if (opt=="--write") {
00234         if (arg!=".") {
00235           kWrite=true;
00236           what_to_write=arg;
00237           i+=2;
00238         }
00239       }
00240       else if (opt=="--bankID") {
00241         if (arg!=".") {
00242           kBankID = true;
00243           BankID = atoi(arg.c_str());
00244           i+=2;
00245         }
00246       }
00247       else if ( opt=="--dbms") {
00248         if (arg!=".") {
00249           kDBMS=true;
00250           dbms=arg;
00251           i+=2;
00252         }
00253       }
00254       else if (opt=="--forceDate") {
00255         if (arg!=".") {
00256           kForceDate=true;
00257           TforceDate = getDate(arg);
00258           i+=2; 
00259         }
00260       }      
00261       else if (opt=="--forceEndDate") {
00262         if (arg!=".") {
00263           kForceEndDate=true;
00264           TforceEndDate = getDate(arg);
00265           i+=2; 
00266         }
00267       }
00268       else if (opt=="--compare") {
00269         if (arg!=".") {
00270           directories.push_back(arg);
00271           i+=2;
00272         }
00273       }
00274       else if (opt=="--insertedAfter") {
00275         if (arg!=".") {
00276           TinsertAfter = getDate(arg);
00277           i+=2;
00278         }
00279       }
00280       else if (opt=="--insertedBefore") {
00281         if (arg!=".") {
00282           TinsertBefore = getDate(arg);
00283           i+=2;
00284         }
00285       }
00286       else if (opt=="--startBefore") {
00287         if (arg!=".") {
00288           TstartBefore = getDate(arg);
00289           i+=2;
00290         }
00291       }
00292       else if (opt=="--startAfter") {
00293         if (arg!=".") {
00294           TstartAfter = getDate(arg);
00295           i+=2;
00296         }
00297       }
00298        else if (opt=="--endBefore") {
00299         if (arg!=".") {
00300           TendBefore = getDate(arg);
00301           i+=2;
00302         }
00303       }
00304       else if (opt=="--endAfter") {
00305         if (arg!=".") {
00306           TendAfter = getDate(arg);
00307           i+=2;
00308         }
00309       }
00310       else if (opt=="--dump") {
00311         if (arg!=".") {
00312           kDump=true;
00313           what_to_dump=arg;
00314           i+=2;
00315         }
00316       }
00317       else if (opt=="--config") {
00318         if (arg!=".") {
00319           kConfig=true;
00320           config=arg;
00321           i+=2;
00322         }
00323       }
00324       else if (opt=="--convertNormalization")
00325         {
00326           if (arg!=".") 
00327             {
00328               kConvertNormalization=true;
00329               ConvertNormalizationSource=atoi(arg.c_str());
00330               i+=2;
00331             }
00332         }
00333       else if ( opt=="--sourceDir")
00334         {
00335           if (arg!=".") 
00336             {
00337               kSourceDir=true;
00338               SourceDir=arg;
00339               i+=2;
00340             }
00341         }
00342 
00343       else if (opt=="--help" || opt=="-h" ) {
00344         return usage();
00345       }
00346       else if (opt=="--version") {
00347         kVersion = true;
00348         i++;
00349       }
00350       else if (opt=="--debug") {
00351         kDebug = true;
00352         i++;
00353       }
00354       else if (opt=="--batch") {
00355         kBatch=true;
00356         i++;
00357       }
00358       else if (opt=="--convert") {
00359         kConvert = true;
00360         what_to_convert = arg;
00361         i += 2;
00362       }
00363       else if (opt=="--update") {
00364         kUpdate = true;
00365         UpdateDir = arg; 
00366         i += 2; 
00367       }
00368       else if (opt=="--updatePbGlGains") {
00369         kUpdatePbGlGains = true;
00370         UpdatePbGlGainsDir = arg;
00371         i += 2;
00372       }
00373       else if (opt=="--simulDBs") {
00374         kSimulDBs=true;
00375         i++;
00376       }
00377       else if (opt=="--fake") {
00378         kFake = true;
00379         i++;
00380       }
00381       else if (opt=="--runtimes") {
00382         kRunTimes = true;
00383         ++i;
00384         if (arg.substr(0,2)!="--") 
00385           {
00386             MinRunNumber=atoi(arg.c_str());
00387             ++i;
00388           }
00389       }
00390       else {
00391         cerr << "<E> Unknown option : " << opt << endl;
00392         //error = true; 
00393         i++;
00394       }
00395     } // end loop on arguments
00396 
00397     // Some checks here.
00398     if ( kBankID ) {
00399       if ( !kDump ) {
00400         cerr << "<E> Option --bankID can only be used with --dump" << endl;
00401         return usage();
00402       }
00403       if ( BankID < 0 ) {
00404         cerr << "<E> BankID = " << BankID << " is not valid" << endl;
00405         return usage();
00406       }
00407     }
00408 
00409     if (TforceDate) {
00410       cout << "<I> I will use " << (*TforceDate) 
00411            << " as starting validity period for everything I will write " 
00412            << endl;
00413     } 
00414     if (TforceEndDate) {
00415       cout << "<I> I will use " << (*TforceEndDate) 
00416            << " as end of validity period for everything I will write " 
00417            << endl;
00418     } 
00419 
00420     //
00421     // --------------------------------------------------------------------
00422     //
00423 
00424     if (kVersion) 
00425       {
00426         cout << emcDB::Version() << endl;
00427       }
00428 
00429     emcDB edb(dbms.c_str(),dbname.c_str(),!kBatch,TforceDate,TforceEndDate);
00430     
00431     if (kConvertNormalization)
00432       {
00433         if ( (ConvertNormalizationSource==emcManageable::kDB_Objy ||
00434               ConvertNormalizationSource==emcManageable::kDB_Pg) && 
00435             !TforceDate) 
00436           {
00437             cerr << "<E> --convertNormalization from DB requires the "
00438                  << "--forceDate option !" << endl;
00439             return -2;
00440           }
00441         else if (ConvertNormalizationSource==emcManageable::kFile_ASCII &&
00442                  !kSourceDir) {
00443            cerr << "<E> --convertNormalization from ASCII files requires the "
00444                  << "--sourceDir option !" << endl;
00445             return -3;
00446         }
00447         emcManageable::EStorage source;
00448         emcDataManager* dm = emcDataManager::GetInstance();
00449 
00450         switch (ConvertNormalizationSource)
00451           {
00452           case emcManageable::kDB_Objy:
00453             source=emcManageable::kDB_Objy;
00454             break;
00455           case emcManageable::kDB_Pg:
00456             source=emcManageable::kDB_Pg;
00457             break;
00458           case emcManageable::kFile_ASCII:
00459             source=emcManageable::kFile_ASCII;
00460             dm->SetSourceDir(SourceDir.c_str());
00461             break;
00462           case emcManageable::kDB_Construction:
00463             source=emcManageable::kDB_Construction;
00464             break;
00465           default:
00466             cerr << "<E> Invalid data source : " << ConvertNormalizationSource
00467                  << " Possible values are : "
00468                  << "\n" << emcManageable::kDB_Pg << "=" 
00469                  << emcManageable::GetStorageName(emcManageable::kDB_Pg)
00470                  << "\n" << emcManageable::kDB_Objy << "=" 
00471                  << emcManageable::GetStorageName(emcManageable::kDB_Objy)
00472                  << "\n" << emcManageable::kFile_ASCII << "=" 
00473                  << emcManageable::GetStorageName(emcManageable::kFile_ASCII)
00474                  << "\n" << emcManageable::kDB_Construction << "=" 
00475                  << emcManageable::GetStorageName(emcManageable::kDB_Construction)
00476                  << endl;
00477             return -20;                          
00478           }
00479 
00480         if (kForceDate) 
00481           {
00482             convertNormalization(source,TforceDate);
00483           }
00484         else
00485           {
00486             convertNormalization(source);
00487           }
00488       }
00489 
00490     if (kConfig) {
00491       edb.SetConfigurationFile(config);
00492     }
00493 
00494     edb.SetDirectories(directories);
00495 
00496     if (kDebug) edb.Debug(true);
00497 
00498     if (!TinsertAfter) {
00499       TinsertAfter = new PHTimeStamp(0);
00500     }
00501 
00502     if (!TinsertBefore) {
00503       TinsertBefore = new PHTimeStamp();
00504       TinsertBefore->setToFarFuture();
00505     }
00506 
00507     if (!TstartAfter) {
00508       TstartAfter = new PHTimeStamp(0);
00509     }
00510 
00511     if (!TstartBefore) {
00512       TstartBefore = new PHTimeStamp();
00513       TstartBefore->setToFarFuture();
00514     }
00515 
00516     if (!TendAfter) {
00517       TendAfter = new PHTimeStamp(0);
00518     }
00519 
00520     if (!TendBefore) {
00521       TendBefore = new PHTimeStamp();
00522       TendBefore->setToFarFuture();
00523     }
00524 
00525     edb.SetInsertInterval(*TinsertAfter,*TinsertBefore);
00526     edb.SetStartInterval(*TstartAfter,*TstartBefore);
00527     edb.SetEndInterval(*TendAfter,*TendBefore);
00528 
00529     edb.SetBankID(BankID);
00530 
00531     if (kRunTimes) 
00532       {
00533         edb.MakeRunTimes(MinRunNumber);
00534         exit(1);
00535       }
00536 
00537     if (kDump) {
00538       bool ok = edb.Dump(what_to_dump.c_str());
00539       if (!ok) {
00540         cerr << "<E> --dump failed" << endl;
00541       }
00542     }
00543 
00544     if (kUpdate) {
00545       bool ok = edb.Update(UpdateDir);
00546       if (!ok) {
00547         cerr << "<E> --update failed" << endl;
00548       }
00549     }
00550 
00551     if (kUpdatePbGlGains) {
00552       edb.UpdatePbGlGains(UpdatePbGlGainsDir);
00553     }
00554 
00555     delete TinsertAfter;
00556     delete TinsertBefore;
00557     delete TstartAfter;
00558     delete TstartBefore;
00559     delete TendAfter;
00560     delete TendBefore;
00561     delete TforceDate;
00562     delete TforceEndDate;
00563 
00564   } // end of try...
00565 
00566   catch (const exception &e)
00567   {
00568     cerr << "Got an exception : " << e.what() << endl;
00569   }
00570 
00571   return 0;
00572 }
00573