#include #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 pFillFee, pUpdate, pUpdateAll, pUpdateACQ_START, pUpdateACQ_START2; bool pTest, pConnected, pInfo, pInfoID, pDrop, pDelete, pCreate, pFill; bool pSelect, pSelectTableAltro, pSelectTableFec, pSelectTableRcu; bool pSelectTableTpc, pSelectSingleField, pDumpDisabled; int32_t pInfoSelectedID, pDropCounter, pDeleteCounter, pCreateCounter, pdbSystem, pdbConnectPort, pUpdateValue; uint32_t pGeomMinTB, pGeomMaxTB; float pGeomAngle, pGeomOffset, pFactorValue; string pSelectField, pUpdateString, pdbUser, pdbPassword, pdbConnectId; set pDropList, pDeleteList, pCreateList; vector pFillComment, pFileNameList; vector pSelectAltroList, pSelectFecList, pSelectBranchList, pSelectRcuList; vector pSelectSectorList, pSelectSideList, pSelectTagList; }; struct arguments arguments; #include "DBTool.h" #include "Misc.h" #include "Info.h" #include "Connect.h" #include "Drop.h" #include "Delete.h" #include "Create.h" #include "Fill.h" #include "FillBind.h" #include "Update.h" #include "UpdateBind.h" #include "Select.h" #define PI 3.14159265 using namespace std; using namespace oracle::occi; const char *argp_program_version = "DBTool.app"; const char *argp_program_bug_address = ""; static const char doc[]= "Program to modify content of the TPC FERO Configuration Data Base.\ \v==============================================================================\ \nExample: DBTool.app -ualice_dcs_tpc -Palice-dcs-tpc -zdevdb10 -j\ \n DBTool.app --select --table=RCU --tag=5 --side=1 --sector=9 --rcu=2\ \n DBTool.app --update -TFEC -t1 -s0 -r12 -p5 -b1 -f0,1,2 -CON_FEC -n1\ \n=============================================================================="; #define OPT_UP_GEOM 1 #define OPT_DEL 2 #define OPT_DEL_ALL 3 #define OPT_DROP 4 #define OPT_DROP_ALL 5 #define OPT_TEST 6 #define OPT_FILL 7 #define OPT_CREATE 8 #define OPT_CREATE_ALL 9 #define OPT_NEWSTRING 10 #define OPT_ANGLE 11 #define OPT_ETA 12 #define OPT_OFFSET 13 #define OPT_MINTB 14 #define OPT_MAXTB 15 // possible 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}, {"info", 'i', "TAG", 0, "Show info on selected configurations specified by tags." , 1}, {"infoall", 'j', 0, 0, "Show info on all configurations in the DB." , 1}, {"availabletags", 'A', 0, 0, "Show list of possible tags (these are maybe not yet stored in DB)." , 1}, {"fill", OPT_FILL, 0, 0, "Fills a standard configuration into the detector. Specify also tag." , 1}, {"icl", 'Z', 0, 0, "Fill the FEE_A/C and FEESERVICES_A/C tables (for InterComLayer)." , 1}, {"dump", 'D', 0, 0, "Dump all disabled equipment. Specify list of configurations (tags)" , 1}, {"select", 'S', 0, 0, "Get data for specified target. Specify also table and target." , 1}, {"update", 'U', 0, 0, "Update a field. Specify also table, field (column name) and target." , 1}, {"updateall", 'V', 0, 0, "Update all values of a column and table for a given list of tags." , 1}, {"updategeom", OPT_UP_GEOM, 0, 0, "Update ACQ_START using angle or eta and offset for all altros of list of tags.", 1}, {"updatestart", 'y', 0, 0, "Update ACQ_START using desyncronising for all altros of list of tags." , 1}, {"create", OPT_CREATE, "TABLENAME", 0, "Create table(s) (ALTRO,FEC,RCU,SECTOR,TPC, ...)." , 1}, {"createall", OPT_CREATE_ALL, 0, 0, "Create all tables except FEE and FEESERVICES." , 1}, {"delete", OPT_DEL, "TABLENAME", 0, "Delete table(s) (ALTRO,FEC,RCU,SECTOR,TPC, ...). CAREFUL!" , 1}, {"deleteall", OPT_DEL_ALL, 0, 0, "Delete all tables. CAREFUL!" , 1}, {"drop", OPT_DROP, "TABLENAME", 0, "Drop table(s) (ALTRO,FEC,RCU,SECTOR,TPC, ...). CAREFUL!" , 1}, {"dropall", OPT_DROP_ALL, 0, 0, "Drop all tables. CAREFUL!" , 1}, {"newvalue", 'n', "NEWVALUE", 0, "New value for update." , 2}, {"newstring", OPT_NEWSTRING, "NEWSTRING", 0, "New string for update of Comment in TPC table." , 2}, {"table", 'T', "TABLENAME", 0, "Specify table (ALTRO, FEC, RCU or TPC) for select or update." , 2}, {"column", 'C', "COLUMNNAME", 0, "Specify column name for select or update." , 2}, {"col", 0, "COLUMNNAME", OPTION_ALIAS }, {"field", 0, "COLUMNNAME", OPTION_ALIAS }, {"tag", 't', "TAG1[,TAG2...]", 0, "Specify configuration ID (Tag)." , 2}, {"id", 0, 0, OPTION_ALIAS }, {"side", 's', "SIDE1[,SIDE2]", 0, "Specify side of TPC (0,1). More values separated by comma." , 2}, {"sector", 'r', "SEC1[,SEC2...]", 0, "Specify sector of TPC (0...17). More values separated by comma." , 2}, {"partition", 'p', "RCU1[,RCU2...]", 0, "Specify partition (0...5). More values separated by comma." , 2}, {"rcu", 0, 0, OPTION_ALIAS }, {"branch", 'b', "BR1[,BR2]", 0, "Specify read out branch (0,1). More values separated by comma." , 2}, {"fec", 'f', "FEC1[,FEC2...]", 0, "Specify fec (0...12). More values separated by comma." , 2}, {"altro", 'a', "AL1[,AL2...]", 0, "Specify altro (0...7). More values separated by comma." , 2}, {"test", OPT_TEST, 0, 0, "Test only. No DB activity. SQL Commands are written to files." , 3}, {"angle", OPT_ANGLE, "VALUE", 0, "Specify theta angle [deg] for cone cut (Default=~45deg, eta=~0.9)." , 4}, {"eta", OPT_ETA, "VALUE", 0, "Specify eta range for cone cut (Default=~0.9, Theta angle=~45deg)." , 4}, {"offset", OPT_OFFSET, "VALUE", 0, "Specify offset [cm] (horizontal dist from IP, default=~2.5cm) ." , 4}, {"mintb", OPT_MINTB, "VALUE", 0, "Specify the minimum time bin (Default=30)." , 4}, {"maxtb", OPT_MAXTB, "VALUE", 0, "Specify the maximum time bin (Default=980)." , 4}, { 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 'i': arguments->pInfo = true; arguments->pInfoID = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pInfoSelectedID = atoi(arg); } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'j': arguments->pInfo = true; break; case 'A': gettags(); break; case OPT_FILL: arguments->pFill = true; break; case 'Z': arguments->pFillFee = true; break; case 'D': arguments->pDumpDisabled = true; break; case 'S': arguments->pSelect = true; break; case 'U': arguments->pUpdate = true; break; case 'V': arguments->pUpdateAll = true; break; case OPT_UP_GEOM: arguments->pUpdateACQ_START = true; break; case 'y': arguments->pUpdateACQ_START2 = true; break; case OPT_CREATE: arguments->pCreate = true; arguments->pCreateList.clear(); arguments->pCreateCounter = 0; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pCreateList.insert(token); arguments->pCreateCounter++; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_CREATE_ALL: arguments->pCreate = true; arguments->pCreateList.clear(); arguments->pCreateList.insert("ALL"); arguments->pCreateCounter = ciTables; break; case OPT_DEL: arguments->pDelete = true; arguments->pDeleteList.clear(); arguments->pDeleteCounter = 0; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pDeleteList.insert(token); arguments->pDeleteCounter++; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_DEL_ALL: arguments->pDelete = true; arguments->pDeleteList.clear(); arguments->pDeleteList.insert("ALL"); arguments->pDeleteCounter = ciTables; break; case OPT_DROP: arguments->pDrop = true; arguments->pDropList.clear(); arguments->pDropCounter = 0; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pDropList.insert(token); arguments->pDropCounter++; } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_DROP_ALL: arguments->pDrop = true; arguments->pDropList.clear(); arguments->pDropList.insert("ALL"); arguments->pDropCounter = ciTables; break; case 'n': arguments->pUpdateValue = atoi(arg); break; case OPT_NEWSTRING: arguments->pUpdateString = arg; break; case 'T': if ( strcmp(arg,"ALTRO" ) == 0) arguments->pSelectTableAltro = true; else if ( strcmp(arg,"RCU" ) == 0) arguments->pSelectTableRcu = true; else if ( strcmp(arg,"FEC" ) == 0) arguments->pSelectTableFec = true; else if ( strcmp(arg,"TPC" ) == 0) arguments->pSelectTableTpc = true; break; case 'C': arguments->pSelectSingleField = true; arguments->pSelectField = 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 's': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t side = atoi(token); if ( side > 1 ) { cout << "*Strange side: " << side << endl; } else { arguments->pSelectSideList.push_back(side); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'r': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t sector = atoi(token); if ( sector > 17 ) { cout << "*Strange sector: " << sector << endl; } else { arguments->pSelectSectorList.push_back(sector); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'p': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t rcu = atoi(token); if ( rcu > 5 ) { cout << "*Strange rcu: " << rcu << endl; } else { arguments->pSelectRcuList.push_back(rcu); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'b': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t branch = atoi(token); if ( branch > 1 ) { cout << "*Strange branch: " << branch << endl; } else { arguments->pSelectBranchList.push_back(branch); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'f': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t fec = atoi(token); if ( fec > 12 ) { cout << "*Strange fec: " << fec << endl; } else { arguments->pSelectFecList.push_back(fec); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'a': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t altro = atoi(token); if ( altro > 7 ) { cout << "*Strange altro: " << altro << endl; } else { arguments->pSelectAltroList.push_back(altro); } } 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_ANGLE: arguments->pGeomAngle = atof(arg); break; case OPT_ETA: arguments->pGeomAngle = 2*atan(exp(-atof(arg)))*180/PI; break; case OPT_OFFSET: arguments->pGeomOffset = atoi(arg); break; case OPT_MINTB: arguments->pGeomMinTB = atoi(arg); break; case OPT_MAXTB: arguments->pGeomMaxTB = atoi(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.pInfo = false; arguments.pInfoID = false; arguments.pInfoSelectedID = -1; arguments.pDrop = false; arguments.pDropCounter = 0; arguments.pDelete = false; arguments.pDeleteCounter = 0; arguments.pCreate = false; arguments.pCreateCounter = 0; arguments.pFill = false; arguments.pFillFee = false; arguments.pUpdate = false; arguments.pUpdateAll = false; arguments.pUpdateACQ_START = false; arguments.pUpdateACQ_START2 = false; arguments.pGeomAngle = 0.; arguments.pGeomOffset = 0.; arguments.pGeomMinTB = 32; arguments.pGeomMaxTB = 955; arguments.pDumpDisabled = false; arguments.pSelect = false; arguments.pSelectTableAltro = false; arguments.pSelectTableFec = false; arguments.pSelectTableRcu = false; arguments.pSelectTableTpc = false; arguments.pSelectSingleField = false; arguments.pSelectField = ""; arguments.pUpdateString = ""; arguments.pUpdateValue = -1; 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.pInfoID == true ) { if ( arguments.pInfoSelectedID < 0 ) { if (arguments.pTest == false) cout << "*No valid ID specified:" << arguments.pInfoSelectedID << endl; return 0; } } if ( arguments.pFill == true ) { if ( arguments.pSelectTagList.size() == 0 ) { cout << "*No Tag (Configuration ID) specified! Use option \"--tag\"." << endl; return 0; } } if ( ( arguments.pSelect == true) || ( arguments.pUpdate == true) ) { if ( ( arguments.pSelectTableTpc == false) && ( arguments.pSelectTableAltro == false) && ( arguments.pSelectTableFec == false) && ( arguments.pSelectTableRcu == false) ) { cout << "*No table specified! Specify (-table) TPC, RCU, FEC, ALTRO." << endl; return 0; } } if ( ( (arguments.pUpdateACQ_START == true) || (arguments.pUpdateACQ_START2 == true) ) && (arguments.pSelectTagList.size() == 0) ) { cout << "*No Tag (Configuration ID) specified! Use option \"--tag\"." << endl; return 0; } if ( (arguments.pUpdateACQ_START2 == true) && (arguments.pUpdateValue < 0) ) { cout << "*No (minimum) new value specified! Use option \"--newvalue\"." << endl; return 0; } if ( arguments.pUpdate == true ) { if ( arguments.pSelectSingleField == false ) { cout << "*No column specified!" << endl; return 0; } if ( (arguments.pUpdateValue < 0) && ( arguments.pUpdateString.length() < 1) ) { cout << "*No new value or string specified for update!" << endl; return 0; } } if ( arguments.pUpdateAll == true ) { if ( (arguments.pSelectTableAltro == false) && ( arguments.pSelectTableFec == false) && ( arguments.pSelectTableRcu == false) ) { cout << "*No table specified! Specify (-table) RCU, FEC or ALTRO." << endl; return 0; } if ( arguments.pSelectTagList.size() == 0 ) { cout << "*No Tag (Configuration ID) specified!" << endl; return 0; } if ( arguments.pSelectSingleField == false ) { cout << "*No column specified!" << endl; return 0; } if ( (arguments.pUpdateValue < 0) && ( arguments.pUpdateString.length() < 1) ) { cout << "*No new value specified for update!" << endl; return 0; } } if ( (arguments.pDumpDisabled == true) && (arguments.pSelectTagList.size() == 0) ) { cout << "*No Tag (Configuration ID) specified!" << endl; return 0; } if ( ( arguments.pSelect == true) || ( arguments.pUpdate == true) ) { if ( arguments.pSelectTagList.size() == 0 ) { cout << "*No Tag (Configuration ID) specified!" << endl; return 0; } if ( ( arguments.pSelectTableRcu == true) || ( arguments.pSelectTableFec == true) || ( arguments.pSelectTableAltro == true ) ) { if ( ( arguments.pSelectSideList.size() == 0) || ( arguments.pSelectSectorList.size() == 0) || ( arguments.pSelectRcuList.size() == 0) ) { cout << "*No side, sector or partition (RCU) specified!" << endl; return 0; } } if ( ( arguments.pSelectTableFec == true ) || ( arguments.pSelectTableAltro == true ) ) { if ( arguments.pSelectBranchList.size() == 0 ) { cout << "*No FEC branch specified!" << endl; return 0; } if ( arguments.pSelectFecList.size() == 0 ) { cout << "*No FEC number specified!" << endl; return 0; } } if ( (arguments.pSelectTableAltro == true ) ) { if ( arguments.pSelectAltroList.size() == 0 ) { cout << "*No ALTRO specified!" << 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; } } */ 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.pInfo && !getinfo()) { cerr << "*Error: Could not get info! Maybe TPC table does not exist?" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pDrop && !droptables()) { cerr << "*Error: Could not drop the table(s)!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pDelete && !deletetables()) { cerr << "*Error: Could not delete the table(s)!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pCreate && !createtables()) { cerr << "*Error: Could not create the table(s)!" << endl; if (!arguments.pTest) disconnect(); return 0; } //if ( arguments.pFill && !filltables()) { if ( arguments.pFill && !filltables2()) { cerr << "*Error: Could not fill the table(s)!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pFillFee && !fillicl()) { cerr << "*Error: Could not fill the FEE_A/C and FEESERVICES_A/C tables!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pDumpDisabled && !dumpdisabled() ) { cerr << "*Error: Could not get nor dump data!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelect ) { if ( arguments.pSelectTableTpc && !selecttpc() ) { cerr << "*Error: Could not get tpc data!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelectTableRcu && !selectrcu() ) { cerr << "*Error: Could not get rcu data!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelectTableFec && !selectfec() ) { cerr << "*Error: Could not get fec data!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelectTableAltro && !selectaltro() ) { cerr << "*Error: Could not get altro data!" << endl; if (!arguments.pTest) disconnect(); return 0; } } if ( arguments.pUpdate ) { if ( arguments.pSelectTableTpc && !updatetpc() ) { cerr << "*Error: Could not update tpc table!" << endl; if (!arguments.pTest) disconnect(); return 0; } //if ( arguments.pSelectTableRcu && !updatercubind() ) { if ( arguments.pSelectTableRcu && !updatercu() ) { cerr << "*Error: Could not update rcu table!" << endl; if (!arguments.pTest) disconnect(); return 0; } //if ( arguments.pSelectTableFec && !updatefecbind() ) { if ( arguments.pSelectTableFec && !updatefec() ) { cerr << "*Error: Could not update fec table!" << endl; if (!arguments.pTest) disconnect(); return 0; } //if ( arguments.pSelectTableAltro && !updatealtrobind() ) { if ( arguments.pSelectTableAltro && !updatealtro() ) { cerr << "*Error: Could not update altro table!" << endl; if (!arguments.pTest) disconnect(); return 0; } } if ( arguments.pUpdateAll ) { if ( arguments.pSelectTableRcu && !updateallrcu() ) { cerr << "*Error: Could not update rcu table!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelectTableFec && !updateallfec() ) { cerr << "*Error: Could not update fec table!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pSelectTableAltro && !updateallaltro() ) { cerr << "*Error: Could not update altro table!" << endl; if (!arguments.pTest) disconnect(); return 0; } } if ( arguments.pUpdateACQ_START && !updatealtroACQ_START() ) { cerr << "*Error: Could not update ACQ_START!" << endl; if (!arguments.pTest) disconnect(); return 0; } if ( arguments.pUpdateACQ_START2 && !updatealtroACQ_START2() ) { cerr << "*Error: Could not update ACQ_START!" << 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