Changeset 19068
- Timestamp:
- 07/21/18 14:57:22 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/calcsource.cc
r19053 r19068 49 49 control.add_options() 50 50 ("uri,u", var<string>()->required(), "Database link as in\n\tuser:password@server[:port]/database.") 51 ("ra", var<double>()/*5.575539)*/, "")52 ("dec", var<double>()/*22.014500)*/, "")53 ("focal-dist", var<double>(4889.), "")54 51 //("source-key", var<uint32_t>(5), "") 55 52 //("source-name", var<string>(""), "") 56 ("file", var<uint32_t>(171011115)->required(), "") 57 ("drop", po_switch(), "Drop the table (implied create)") 58 ("list-files", po_switch(), "") 59 ("tree,t", var<string>("Events"), "Name of the root tree to convert") 60 ("table", var<string>("Events"), "") 61 ("update", var<string>(""), "") 62 ("create", po_switch(), "") 53 ("file", var<uint32_t>(0), "FileId (YYMMDDXXX), defined the events to be processed (if omitted (=0), a list of possible numbers is printed)") 54 ("table.events", var<string>("Events"), "Name of the table where the events are stored") 55 ("table.runinfo", var<string>("RunInfo"), "Name of the table where the run-info data is stored") 56 ("table.source", var<string>("Source"), "Name of the table where the sources are stored") 57 ("table.position", var<string>("Position"), "Name of the table where the calculated posiiton will be stored") 58 ("force", po_switch(), "Force processing even if there is no database connection") 59 ("drop", po_switch(), "Drop the table (implies create)") 60 ("create", po_switch(), "Create the table if it does not yet exist") 61 ("update", po_switch(), "Update the table.position instead of inserting it (disables drop, create and delete)") 62 ("no-insert", po_switch(), "Does not insert or update any data to any table") 63 ("dry-run", po_switch(), "Skip any query which changes the databse (might result in consecutive failures)") 64 ("ra", var<double>(), "Right ascension of the source (use together with --dec)") 65 ("dec", var<double>(), "Declination of the source (use together with --ra)") 66 ("focal-dist", var<double>(4889.), "Focal distance of the camera in millimeter") 63 67 ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)") 64 ("no-insert", po_switch(), "Does not insert any data to the table") 65 //("print-insert", po_switch(), "Print the INSERT query (note that it contains all data)") 66 //("print-create", po_switch(), "Print the CREATE query") 68 ("print-meta", po_switch(), "Print meta-queries (DROP, CREATE, DELETE, SELECT)") 69 ("print-insert", po_switch(), "Print the INSERT/UPDATE queries") 67 70 ; 68 71 … … 192 195 193 196 // ----------------------------- Evaluate options -------------------------- 197 if (conf.Has("ra")^conf.Has("dec")) 198 throw runtime_error("--ra and --dec can only be used together"); 199 194 200 const bool has_radec = conf.Has("ra") && conf.Has("dec"); 195 201 196 const string uri = conf.Get<string>("uri"); 197 const uint32_t file = conf.Get<uint32_t>("file"); 198 const string tree = conf.Get<string>("tree"); 199 const string table = conf.Get<string>("table"); 202 double source_ra = conf.Has("ra") ? conf.Get<double>("ra") : 0; 203 double source_dec = conf.Has("dec") ? conf.Get<double>("dec") : 0; 204 200 205 //string source_name = conf.Get<string>("source-name"); 201 206 //uint32_t source_key = conf.Has("source-key") ? conf.Get<uint32_t>("source-key") : 0; 202 double source_ra = conf.Has("ra") ? conf.Get<double>("ra") : 0; 203 double source_dec = conf.Has("dec") ? conf.Get<double>("dec") : 0; 204 double focal_dist = conf.Get<double>("focal-dist"); 205 //const bool print_create = conf.Get<bool>("print-create"); 206 //const bool print_insert = conf.Get<bool>("print-insert"); 207 const bool drop = conf.Get<bool>("drop"); 208 const bool create = conf.Get<bool>("create") || drop; 209 const string update = conf.Get<string>("update"); 210 const bool list_files = conf.Get<bool>("list-files"); 211 const uint16_t verbose = conf.Get<uint16_t>("verbose"); 207 208 const string uri = conf.Get<string>("uri"); 209 const string tab_events = conf.Get<string>("table.events"); 210 const string tab_runinfo = conf.Get<string>("table.runinfo"); 211 const string tab_source = conf.Get<string>("table.source"); 212 const string tab_position = conf.Get<string>("table.position"); 213 214 const uint32_t file = conf.Get<uint32_t>("file"); 215 216 const double focal_dist = conf.Get<double>("focal-dist"); 217 218 const bool print_meta = conf.Get<bool>("print-meta"); 219 const bool print_insert = conf.Get<bool>("print-insert"); 220 221 const bool force = conf.Get<bool>("force"); 222 const bool drop = conf.Get<bool>("drop"); 223 const bool create = conf.Get<bool>("create") || drop; 224 const bool update = conf.Get<bool>("update"); 225 const bool noinsert = conf.Get<bool>("no-insert"); 226 const bool dry_run = conf.Get<bool>("dry-run"); 227 const uint16_t verbose = conf.Get<uint16_t>("verbose"); 212 228 213 229 // ------------------------------------------------------------------------- 214 215 if (list_files) 230 // Checking for database connection 231 232 Database connection(uri); // Keep alive while fetching rows 233 234 try 235 { 236 if (!force) 237 connection.connected(); 238 } 239 catch (const exception &e) 240 { 241 cerr << "SQL connection failed: " << e.what() << endl; 242 return 1; 243 } 244 245 // ------------------------------------------------------------------------- 246 // list file-ids in the events table 247 248 if (file==0) 216 249 { 217 250 const string query = 218 "SELECT FileId FROM Events GROUP BY FileId"; 219 220 cout << query << endl; 251 "SELECT FileId FROM `"+tab_events+"` GROUP BY FileId"; 252 253 if (print_meta) 254 cout << query << endl; 221 255 222 256 const mysqlpp::StoreQueryResult res = 223 Database(uri).query(query).store();257 connection.query(query).store(); 224 258 225 259 for (size_t i=0; i<res.num_rows(); i++) … … 230 264 } 231 265 266 // ------------------------------------------------------------------------- 267 // retrieve source from the database 268 232 269 if (verbose>0) 270 { 233 271 cout << "\n------------------------- Evaluating source ------------------------" << endl; 234 235 cout << "Requesting coordinates from RunInfo/Source table for 20" << file/1000 << "/" << file%1000 << endl;272 cout << "Requesting coordinates from " << tab_runinfo << "/" << tab_source << " table for 20" << file/1000 << "/" << file%1000 << endl; 273 } 236 274 237 275 if (!has_radec) 238 276 { 239 277 const string query = 240 "SELECT Source.fRightAscension, Source.fDeclination, Source.fSourceName"241 " FROM RunInfo"242 " LEFT JOIN Source"278 "SELECT "+tab_source+".fRightAscension, "+tab_source+".fDeclination, "+tab_source+".fSourceName" 279 " FROM `"+tab_runinfo+"`"+ 280 " LEFT JOIN `"+tab_source+"`"+ 243 281 " USING (fSourceKey)" 244 282 " WHERE fNight=20"+to_string(file/1000)+ 245 283 " AND fRunID="+to_string(file%1000); 246 284 247 cout << query << endl; 285 if (print_meta) 286 cout << query << endl; 287 288 try 289 { 290 const mysqlpp::StoreQueryResult res = 291 connection.query(query).store(); 292 293 if (res.num_rows()!=1) 294 { 295 cerr << "No coordinates from " << tab_runinfo << " for " << file << endl; 296 return 2; 297 } 298 299 source_ra = res[0][0]; 300 source_dec = res[0][1]; 301 302 if (verbose>0) 303 cout << "Using coordinates " << source_ra << "h / " << source_dec << " deg for '" << res[0][2] << "'" << endl; 304 } 305 catch (const exception &e) 306 { 307 cerr << query << "\n"; 308 cerr << "SQL query failed:\n" << e.what() << endl; 309 return 3; 310 } 311 } 312 else 313 if (verbose>0) 314 cout << "Using coordinates " << source_ra << "h / " << source_dec << " deg from resources." << endl; 315 316 /* 317 if (!source_name.empty()) 318 { 319 cout << "Requesting coordinates for '" << source_name << "'" << endl; 248 320 249 321 const mysqlpp::StoreQueryResult res = 250 Database(uri).query(query).store();322 connection.query("SELECT `Ra`, `Dec` WHERE fSourceName='"+source_name+"'").store(); 251 323 252 324 if (res.num_rows()!=1) 253 325 { 254 cerr << "No coordinates from RunInfo for " << file<< endl;326 cerr << "No " << (res.num_rows()>1?"unique ":"") << "coordinates found for '" << source_name << "'" << endl; 255 327 return 1; 256 328 } … … 258 330 source_ra = res[0][0]; 259 331 source_dec = res[0][1]; 260 261 cout << "Using coordinates " << source_ra << "h / " << source_dec << " deg for " << res[0][2] << endl;262 }263 else264 cout << "Using coordinates " << source_ra << "h / " << source_dec << " deg from resources." << endl;265 266 /*267 if (!source_name.empty())268 {269 cout << "Requesting coordinates for '" << source_name << "'" << endl;270 271 const mysqlpp::StoreQueryResult res =272 Database(uri).query("SELECT `Ra`, `Dec` WHERE fSourceName='"+source_name+"'").store();273 274 if (res.num_rows()!=1)275 {276 cerr << "No " << (res.num_rows()>1?"unique ":"") << "coordinates found for '" << source_name << "'" << endl;277 return 1;278 }279 280 source_ra = res[0][0];281 source_dec = res[0][1];282 332 } 283 333 */ 284 334 335 // ------------------------------------------------------------------------- 336 // create INSERT/UPDATE query (calculate positions) 337 285 338 if (verbose>0) 286 cout << "\n-------------------------- Evaluating file ------------------------" << endl;287 288 289 Database connection(uri); // Keep alive while fetching rows339 { 340 cout << "\n-------------------------- Evaluating file ------------------------"; 341 cout << "\nRequesting data from table `" << tab_events << "`" << endl; 342 } 290 343 291 344 const string query = 292 345 "SELECT `Ra`, `Dec`, MJD, MilliSec, NanoSec, Zd, Az, EvtNumber" 293 " FROM `"+tab le+"`"346 " FROM `"+tab_events+"`" 294 347 " WHERE FileId="+to_string(file); 295 348 296 cout << query << endl; 349 if (print_meta) 350 cout << query << endl; 297 351 298 352 const mysqlpp::UseQueryResult res1 = … … 314 368 ins << setprecision(16); 315 369 316 ostringstream upd; 317 upd << setprecision(16); 370 vector<string> upd; 318 371 319 372 size_t count = 0; … … 468 521 */ 469 522 470 if ( 1/*insert*/)523 if (!update) 471 524 ins << "( " << file << ", " << event << ", " << v.X() << ", " << v.Y() << " ),\n"; 472 473 if (!update.empty()) 474 upd << "UPDATE " << update << " SET X=" << v.X() << ", Y=" << v.Y() << " WHERE FileId=" << file << " AND EvtNumber=" << event <<";\n"; 475 }; 525 else 526 { 527 ostringstream out; 528 out << setprecision(16); 529 out << "UPDATE `" << tab_position << "` SET X=" << v.X() << ", Y=" << v.Y() << " WHERE FileId=" << file << " AND EvtNumber=" << event; 530 upd.emplace_back(out.str()); 531 } 532 } 476 533 477 534 if (connection.errnum()) 478 535 { 479 536 cerr << "SQL error fetching row: " << connection.error() << endl; 480 return 7; 481 } 482 483 484 if (drop) 485 { 486 cout << "Drop table Position." << endl; 487 const mysqlpp::SimpleResult res2 = 488 Database(uri).query("DROP TABLE Position").execute(); 489 490 //cout << res.rows() << " rows affected." << res.info() << endl; 491 } 492 493 494 if (create) 495 { 496 cout << "Create table Position." << endl; 497 const mysqlpp::SimpleResult res3 = 498 Database(uri).query("CREATE TABLE IF NOT EXISTS Position\n" 499 "(\n" 500 " FileId INT UNSIGNED NOT NULL,\n" 501 " EvtNumber INT UNSIGNED NOT NULL,\n" 502 " X FLOAT NOT NULL,\n" 503 " Y FLOAT NOT NULL,\n" 504 " PRIMARY KEY (FileId, EvtNumber)\n" 505 ")\n" 506 "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n" 507 "ENGINE=MyISAM\n" 508 "COMMENT='created by root2db'\n").execute(); 509 510 //cout << res.rows() << " rows affected." << res.info() << endl; 511 512 } 513 else 514 { 515 // FIXME: Only if entries exist? 516 cout << "Delete old entries from Position." << endl; 517 const mysqlpp::SimpleResult res3 = 518 Database(uri).query("DELETE FROM Position WHERE FileId="+to_string(file)).execute(); 519 520 cout << res3.rows() << " rows affected." << endl; 521 } 522 523 if (1/*insert*/) 524 { 525 cout << "Insert data into table Position." << endl; 526 const string query3 = 527 "INSERT `Position` (FileId, EvtNumber, X, Y) VALUES\n"+ 537 return 4; 538 } 539 540 if (verbose>0) 541 cout << "Processed " << count << " events.\n" << endl; 542 543 // ------------------------------------------------------------------------- 544 // drop table if requested 545 546 if (drop && !update) 547 { 548 try 549 { 550 if (verbose>0) 551 cout << "Dropping table `" << tab_position << "`" << endl; 552 553 if (!dry_run) 554 connection.query("DROP TABLE `"+tab_position+"`").execute(); 555 556 if (verbose>0) 557 cout << "Table `" << tab_position << "` dropped.\n" << endl; 558 } 559 catch (const exception &e) 560 { 561 cerr << "DROP TABLE `" << tab_position << "`\n\n"; 562 cerr << "SQL query failed:\n" << e.what() << endl; 563 return 5; 564 } 565 } 566 567 // ------------------------------------------------------------------------- 568 // crate table if requested 569 570 if (create && !update) 571 { 572 if (verbose>0) 573 cout << "Creating table `" << tab_position << "`" << endl; 574 575 const string query2 = 576 "CREATE TABLE IF NOT EXISTS `"+tab_position+"`\n" 577 "(\n" 578 " FileId INT UNSIGNED NOT NULL,\n" 579 " EvtNumber INT UNSIGNED NOT NULL,\n" 580 " X FLOAT NOT NULL,\n" 581 " Y FLOAT NOT NULL,\n" 582 " PRIMARY KEY (FileId, EvtNumber)\n" 583 ")\n" 584 "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n" 585 "ENGINE=MyISAM\n" 586 "COMMENT='created by "+conf.GetName()+"'\n"; 587 588 try 589 { 590 if (!dry_run) 591 connection.query(query2).execute(); 592 } 593 catch (const exception &e) 594 { 595 cerr << query2 << "\n\n"; 596 cerr << "SQL query failed:\n" << e.what() << endl; 597 return 6; 598 } 599 600 if (print_meta) 601 cout << query2 << endl; 602 603 if (verbose>0) 604 cout << "Table `" << tab_position << "` created.\n" << endl; 605 } 606 607 // ------------------------------------------------------------------------- 608 // delete old entries from table 609 610 if (!drop && !update) 611 { 612 if (verbose>0) 613 cout << "Deleting old entries from table `" << tab_position << "`" << endl; 614 615 const string query2 = 616 "DELETE FROM `"+tab_position+"` WHERE FileId="+to_string(file); 617 618 try 619 { 620 if (!dry_run) 621 { 622 const mysqlpp::SimpleResult res = 623 connection.query(query2).execute(); 624 625 if (verbose>0) 626 cout << res.rows() << " row(s) affected.\n" << endl; 627 } 628 } 629 catch (const exception &e) 630 { 631 cerr << query2 << "\n\n"; 632 cerr << "SQL query failed:\n" << e.what() << endl; 633 return 7; 634 } 635 636 if (print_meta) 637 cout << query2 << endl; 638 } 639 640 // ------------------------------------------------------------------------- 641 // insert data into table 642 643 if (!update) 644 { 645 if (verbose>0) 646 cout << "Inserting data into table " << tab_position << "." << endl; 647 648 const string query2 = 649 "INSERT `"+tab_position+"` (FileId, EvtNumber, X, Y) VALUES\n"+ 528 650 ins.str().substr(0, ins.str().size()-2)+ 529 651 "\n"; 530 652 531 if (0/*print_insert*/) 532 cout << query3 << endl; 533 534 const mysqlpp::SimpleResult res3 = 535 Database(uri).query(query3).execute(); 536 537 cout << res3.info() << endl; 538 } 539 540 if (!update.empty()) 541 { 542 cout << upd.str() << endl; 543 const mysqlpp::StoreQueryResult res3 = 544 connection.query(upd.str()).store(); 545 546 /* 547 mysqlpp::Connection con(db, server, user, pass); 548 // Set option to allow multiple queries to be issued. 549 mysqlpp::MultiStatementsOption* opt = new mysqlpp::MultiStatementsOption(true); 550 con.set_option(opt); 551 // Build either single queries or multiple queries strung together. 552 mysqlpp::Query query = con.query(); 553 .... 554 // Issue query 555 query.store(); 556 // Do NOT delete 'opt'; it will be destroyed by the 'con' object when it 557 // falls out of scope. 558 */ 559 } 560 561 //cout << ins.str().substr(0, ins.str().size()-2) << endl; 562 //cout << count << endl; 653 try 654 { 655 if (!noinsert) 656 { 657 const mysqlpp::SimpleResult res = 658 connection.query(query2).execute(); 659 660 if (verbose>0) 661 cout << res.rows() << " row(s) affected.\n" << endl; 662 } 663 } 664 catch (const exception &e) 665 { 666 cerr << query2 << "\n\n"; 667 cerr << "SQL query failed:\n" << e.what() << endl; 668 return 8; 669 } 670 671 if (print_insert) 672 cout << query2 << endl; 673 } 674 675 676 677 // ------------------------------------------------------------------------- 678 // update data in table 679 680 if (update) 681 { 682 if (verbose>0) 683 cout << "Updating data in table " << tab_position << "." << endl; 684 685 size_t cnt = 0; 686 for (const auto &str : upd) 687 { 688 try 689 { 690 if (!noinsert && !dry_run) 691 { 692 const mysqlpp::SimpleResult res = 693 connection.query(str).execute(); 694 695 cnt += res.rows(); 696 } 697 } 698 catch (const exception &e) 699 { 700 cerr << str << "\n\n"; 701 cerr << "SQL query failed:\n" << e.what() << endl; 702 return 9; 703 } 704 } 705 706 if (verbose>0) 707 cout << cnt << " row(s) out of " << upd.size() << " affected.\n" << endl; 708 709 if (print_insert) 710 { 711 for (const auto &str : upd) 712 cout << str << '\n'; 713 cout << endl; 714 } 715 } 716 717 if (verbose>0) 718 cout << "Total execution time: " << Time().UnixTime()-start.UnixTime() << "s\n" << endl; 563 719 564 720 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.