#include #include #include #include #include #include #include #include #include #include "../DB/DBQueryCreator.h" #include "../DB/OracleDB.h" #include "../DB/FERODBFieldPositions.h" #include "../Config/TpcConfig.h" #include "../CentralHeaders/BitConstants.h" #include "../Utilities/Stopwatch.h" #include "../Utilities/ProgressBar.h" struct arguments { bool pdbUserSet, pdbPasswordSet, pdbConnectIdSet; bool pUpdateFromFileFPED, pUpdateFromFileZSTHR, pUpdateFromFileBCTHR, pUpdateFromFileZSBCTHR; bool pUpdateFromFileFec, pUpdateFromFileAltros, pUpdateFromFileChannels, pUpdateFromFileTail; bool pSelectTableTpc, pSelectSingleField; bool pFile, pUpdateBlob, pSelectBlob, pFactorValueSet, pMinValueSet, pMaxValueSet, pTest, pConnected; bool pSelectTableAltro, pSelectTableFec, pSelectTableRcu; int32_t pMinValue, pMaxValue, pUpdateValue; vector pSelectAltroList, pSelectFecList, pSelectBranchList, pSelectRcuList, pSelectSideList; vector pSelectSectorList, pSelectTagList; string pSelectTableBlob, pBlobFileName, pSelectField, pUpdateString, pdbUser, pdbPassword, pdbConnectId; vector pFillComment, pFileNameList; float pFactorValue, pFpedAdd; }; struct arguments arguments; #include "DBTool.h" #include "Misc.h" #include "Connect.h" #include "Blob.h" #include "UpdateFromFile.h" using namespace std; using namespace oracle::occi; const char *argp_program_version = "DBUpdateFromFile.app"; const char *argp_program_bug_address = ""; static const char doc[]= "Program to modify content of the TPC FERO Configuration Data Base.\ \v==============================================================================\ \nExample: DBUpdateFromFile.app --fecfile=broken_fecs.dat -t2,4,6,7,8,9,12 -n0\ \n DBUpdateFromFile.app --bcthrfile=noise.dat -t15 -x3 -k3 -l10\ \n=============================================================================="; #define OPT_FPED_ADD 0 #define OPT_TEST 1 #define OPT_SEL_BLOB 2 #define OPT_UP_BLOB 3 #define OPT_UP_FPEDFIL 4 #define OPT_UP_ZSTHRFI 5 #define OPT_UP_BCTHRFI 6 #define OPT_UP_THRFILE 7 #define OPT_UP_ALTROFI 8 #define OPT_UP_CHANFIL 9 #define OPT_UP_FECFILE 10 #define OPT_UP_TAILFIL 11 #define OPT_UP_BLOBFIL 12 // .. up to 31 static const struct argp_option options[]={ {"user", 'u', "USER", 0, "Data base user (default tpcfero)." , 0}, {"connect_id", 'z', "CONNECT_ID", 0, "Oracle DB connect identifier (default alice_fero)." , 0}, {"password", 'P', "PASSWORD", 0, "Password for the data base (mandatory)." , 0}, {"newvalue", 'n', "NEWVALUE", 0, "New value for update." , 1}, {"tag", 't', "TAG1[,TAG2...]", 0, "Specify configuration ID (Tag)." , 1}, {"id", 0, 0, OPTION_ALIAS }, {"test", OPT_TEST, 0, 0, "Test only. No DB activity. SQL Commands are written to files." , 2}, {"column", 'C', "COLUMNNAME", 0, "Specify column name for select or update." , 2}, {"col", 0, "COLUMNNAME", OPTION_ALIAS }, {"field", 0, "COLUMNNAME", OPTION_ALIAS }, {"selectblob", OPT_SEL_BLOB, 0, 0, "Select a blob. Specify also outputfile, tablename and column name." , 3}, {"updateblob", OPT_UP_BLOB, 0, 0, "Update a blob. Specify also inputfile, tablename and column name." , 3}, {"fpedfile", OPT_UP_FPEDFIL, "FNAME1[,FNAME2...]", 0, "Update FPED in DB from files. Specify also tag." , 4}, {"zsthrfile", OPT_UP_ZSTHRFI, "FNAME1[,FNAME2...]", 0, "Update ZSTHR in DB from files. Specify also tag." , 4}, {"bcthrfile", OPT_UP_BCTHRFI, "FNAME1[,FNAME2...]", 0, "Update BCTHR in DB from files. Specify also tag." , 4}, {"thrfile", OPT_UP_THRFILE, "FNAME1[,FNAME2...]", 0, "Update ZSTHR and BCTHR from files (same value). Specify also tag." , 4}, {"altrofile", OPT_UP_ALTROFI, "FNAME1[,FNAME2...]", 0, "Update ON_ALTRO in DB from files. Specify also tag." , 4}, {"channelfile", OPT_UP_CHANFIL, "FNAME1[,FNAME2...]", 0, "Update ON_CHANNELxy in DB from files. Specify also tag." , 4}, {"fecfile", OPT_UP_FECFILE, "FNAME1[,FNAME2...]", 0, "Update ON_FEC in DB from files. Specify also tag." , 4}, {"tailfile", OPT_UP_TAILFIL, "FNAME1[,FNAME2...]", 0, "Update tail parameters in DB from files." , 4}, {"blobfile", OPT_UP_BLOBFIL, "FNAME", 0, "Update a BLOB in DB from file." , 4}, {"add", OPT_FPED_ADD, "VALUE", 0, "Add an offset to the FPED values from file (when using fpedfile option).", 5}, {"factor", 'x', "VALUE", 0, "Multiplication factor for BCTHR/ZSTHR (thr=factor*noise (default=3.0)." , 5}, {"min", 'k', "VALUE", 0, "Minimum threshold for BCTHR/ZSTHR (default=3)." , 5}, {"max", 'l', "VALUE", 0, "Maximum threshold for BCTHR/ZSTHR (default=10)." , 5}, { 0 } }; static error_t parse_opt(int32_t key, char *arg, struct argp_state *state) { struct arguments *arguments = (struct arguments*)state->input; char *arg_copy; char *token; switch (key) { case 'u': arguments->pdbUserSet = true; arguments->pdbUser = arg; break; case 'z': arguments->pdbConnectIdSet = true; arguments->pdbConnectId = arg; break; case 'P': arguments->pdbPasswordSet = true; arguments->pdbPassword = arg; break; case 'C': arguments->pSelectSingleField = true; arguments->pSelectField = arg; break; case 'n': arguments->pUpdateValue = atoi(arg); break; case 'T': arguments->pSelectTableBlob = arg; break; case 't': arguments->pSelectTagList.clear(); arg_copy=strdup(arg); token=strtok(arg_copy,","); do { int32_t tag = atoi(token); if ( tag < 1 ) { cout << "*Configuration ID (tag) must be > 0!" << endl; } else { arguments->pSelectTagList.push_back(tag); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_TEST: arguments->pTest = true; cout << "*Test mode. No DB activity. Output written to local file." << endl; break; case OPT_SEL_BLOB: arguments->pSelectBlob = true; break; case OPT_UP_BLOB: arguments->pUpdateBlob = true; break; case OPT_UP_FPEDFIL: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileFPED = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_ZSTHRFI: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileZSTHR = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_BCTHRFI: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileBCTHR = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_THRFILE: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileZSBCTHR = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_ALTROFI: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileAltros = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_CHANFIL: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileChannels = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_FECFILE: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileFec = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_TAILFIL: arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); arguments->pUpdateFromFileTail = true; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_UP_BLOBFIL: arguments->pBlobFileName = arg; break; case 'x': arguments->pFactorValue = atof(arg); arguments->pFactorValueSet = true; break; case 'k': arguments->pMinValue = atoi(arg); arguments->pMinValueSet = true; break; case 'l': arguments->pMaxValue = atoi(arg); arguments->pMaxValueSet = true; break; case OPT_FPED_ADD: arguments->pFpedAdd = atof(arg); break; default: return ARGP_ERR_UNKNOWN; } return 0; } static struct argp argp={options,parse_opt,0,doc}; // ============================================================================= // ============================================================================= int32_t check_args(int32_t argc, char** argv ) { // // Check all arguments given // TpcConfig tpcconf; // Default arguments arguments.pdbUserSet = false; arguments.pdbPasswordSet = false; arguments.pdbConnectIdSet = false; arguments.pTest = false; arguments.pConnected = false; arguments.pUpdateFromFileFPED = false; arguments.pUpdateFromFileZSTHR = false; arguments.pUpdateFromFileBCTHR = false; arguments.pUpdateFromFileZSBCTHR = false; arguments.pUpdateFromFileFec = false; arguments.pUpdateFromFileAltros = false; arguments.pUpdateFromFileTail = false; arguments.pUpdateFromFileChannels = false; arguments.pFile = false; arguments.pUpdateBlob = false; arguments.pSelectBlob = false; arguments.pFactorValue = 3.0; // 3 sigma arguments.pMinValue = 3; arguments.pMaxValue = 10; arguments.pFactorValueSet = false; arguments.pMinValueSet = false; arguments.pMaxValueSet = false; arguments.pSelectTableAltro = false; arguments.pSelectTableFec = false; arguments.pSelectTableRcu = false; arguments.pSelectTableTpc = false; arguments.pSelectSingleField = false; arguments.pSelectField = ""; arguments.pUpdateString = ""; arguments.pUpdateValue = -1; arguments.pFpedAdd = 0.; argp_parse(&argp, argc, argv, 0, 0, &arguments); // // Check Commandline Parameters // if ( arguments.pdbUserSet == false ) { arguments.pdbUser = "tpcfero"; arguments.pdbUserSet = true; if (arguments.pTest == false) cout << "*No db Username set. Using default: " << arguments.pdbUser << endl; } if ( (arguments.pdbPasswordSet == false) && (arguments.pTest == false) ) { cout << "*No db Password set. Specify using \"-dbp\" option!" << endl; return 0; } if ( arguments.pdbConnectIdSet == false ) { arguments.pdbConnectId = "alice_fero"; arguments.pdbConnectIdSet = true; if (arguments.pTest == false) cout << "*No db Connect-String set. Using default: " << arguments.pdbConnectId << endl; } if ( (arguments.pUpdateBlob == true) || (arguments.pSelectBlob == true) ) { if ( arguments.pSelectTableBlob.size() < 1 ) { cout << "*No table specified!" << endl; return 0; } if ( arguments.pSelectSingleField == false ) { cout << "*No column specified!" << endl; return 0; } } if ( (arguments.pUpdateBlob == true) && (arguments.pBlobFileName.size() < 1) ) { cout << "*No input file specified! Use -blobfile ." << endl; return 0; } if ( (arguments.pSelectBlob == true) && (arguments.pBlobFileName.size() < 1) ) { cout << "*No output file specified! Use -blobfile ." << endl; return 0; } if ( arguments.pFile == true ) { if ( arguments.pSelectTagList.size() == 0 ) { cout << "*No Tag (Configuration ID) specified!" << endl; return 0; } if ( arguments.pFileNameList.size() == 0 ) { cout << "*No input file specified!" << endl; return 0; } if ( ( arguments.pUpdateFromFileChannels || arguments.pUpdateFromFileAltros ) && (arguments.pUpdateValue < 0) ) { cout << "*No new value specified for update!" << endl; return 0; } if ( arguments.pUpdateFromFileFec ) { if ( arguments.pSelectSingleField == false ) { cout << "*No column specified!" << endl; return 0; } if ( ( (arguments.pSelectField == "ON_FEC") || (arguments.pSelectField == "CSR0") ) && (arguments.pUpdateValue < 0) ) { cout << "*No new value specified for update!" << endl; return 0; } } if ( arguments.pUpdateFromFileZSTHR || arguments.pUpdateFromFileBCTHR || arguments.pUpdateFromFileZSBCTHR ) { if (!arguments.pFactorValueSet) cout << "*No factor set. Using default: " << arguments.pFactorValue << "!" << endl; if (!arguments.pMinValueSet) cout << "*No minimum threshold set. Using default: " << arguments.pMinValue << endl; if (!arguments.pMaxValueSet) cout << "*No maximum threshold set. Using default: " << arguments.pMaxValue << endl; } } return 1; } // ============================================================================= // ============================================================================= int main(int argc, char** argv ) { if (!check_args(argc, argv)) { return 1; } // // Connect // if ( !arguments.pTest && !connect() ) { cerr << "*Error: Could not connect to DB!" << endl; return 0; } // // Do it // if ( arguments.pSelectBlob && !selectBlob() ) { cerr << "*Error: Could not select (read) Blob!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pUpdateBlob && !updateBlob() ) { cerr << "*Error: Could not update Blob!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pFile ) { if ( arguments.pUpdateFromFileFec && !updatefecfromfile() ) { cerr << "*Error: Could not update FEC table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileAltros && !updateonaltrofromfile() ) { cerr << "*Error: Could not update ON_ALTRO in ALTRO table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileChannels && !updateonchannelfromfile() ) { cerr << "*Error: Could not update ON_CHANNEL in ALTRO table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileFPED && !updatealtrofromfile() ) { cerr << "*Error: Could not update FPED values in altro table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileZSTHR && !updatealtrofromfile() ) { cerr << "*Error: Could not update ZSTHR values in altro table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileBCTHR && !updatealtrofromfile() ) { cerr << "*Error: Could not update BCTHR values in altro table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileZSBCTHR && !updatealtrofromfile() ) { cerr << "*Error: Could not update ZSTHR and BCTHR values in altro table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } else if ( arguments.pUpdateFromFileTail && !updatesectortailparametersfromfile() ) { cerr << "*Error: Could not update tail filter values in altro table from file!" << endl; if (!arguments.pTest) disconnect(); return 0; } } // // Disconnect // if ( !arguments.pTest && !disconnect() ) { cerr << "*Error: Could not disconnect from DB!" << endl; return 0; } if (sw != NULL) { delete sw; sw = 0; } if (dbq != NULL) { delete dbq; dbq = 0; } if (db != NULL) { delete db; db = 0; } return 1; } // EOF