Changeset 19804 for trunk/FACT++
- Timestamp:
- 10/27/19 12:51:21 (5 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/fits2sql.cc
r19191 r19804 8 8 #include "Time.h" 9 9 #include "Configuration.h" 10 #include "FileEntry.h" 10 11 11 12 #include "zfits.h" … … 15 16 16 17 // ------------------------------------------------------------------------ 17 18 struct Map : pair<string, string>19 {20 Map() { }21 };22 23 std::istream &operator>>(std::istream &in, Map &m)24 {25 const istreambuf_iterator<char> eos;26 string txt(istreambuf_iterator<char>(in), eos);27 28 const boost::regex expr("((.*)[^\\\\])/(.*)");29 boost::smatch match;30 if (!boost::regex_match(txt, match, expr))31 throw runtime_error("Could not evaluate map argument: "+txt);32 33 m.first = match[1].str();34 m.second = match[3].str();35 36 return in;37 }38 39 18 40 19 void SetupConfiguration(Configuration &conf) … … 48 27 ("extension,e", var<string>(""), "Name of the fits extension (table) to convert") 49 28 ("table", var<string>(""), "Name of the table to use (default is the tree name)") 50 ("map", vars< Map>(),"A regular expression which is applied to the leaf name befoee it is used as SQL column name)")51 ("sql-type", vars< Map>(),"Allows to overwrite the calculated SQL type for a given column e.g. 'sql-column-name/UNSIGNED IN'")29 ("map", vars<Configuration::Map>(),"A regular expression which is applied to the leaf name befoee it is used as SQL column name)") 30 ("sql-type", vars<Configuration::Map>(),"Allows to overwrite the calculated SQL type for a given column e.g. 'sql-column-name/UNSIGNED IN'") 52 31 ("ignore", vars<string>(), "Ignore the given leaf, if the given regular expression matches") 53 32 ("unsigned", vars<string>(), "In fits files per default columns are signed. This interpretss the column as unsigned value. Use the FITS column name.") … … 63 42 ("delete", po_switch(), "Delete all entries first which fit all constant columns defined by --const") 64 43 ("index", po_switch(), "If a table is created, all const columns are used as a single index") 44 ("unique", po_switch(), "If a table is created, all const columns are used as a unqiue index (UNIQUE)") 65 45 ; 66 46 … … 213 193 } 214 194 215 enum BasicType_t216 {217 kNone = 0,218 kConst,219 kVarchar,220 kBool,221 kFloat,222 kDouble,223 kInt8,224 kUInt8,225 kInt16,226 kUInt16,227 kInt32,228 kUInt32,229 kInt64,230 kUInt64,231 // kMJD,232 };233 234 static const map<char, pair<BasicType_t, string>> ConvFits =235 {236 { 'A', { kVarchar, "VARCHAR" } },237 { 'a', { kVarchar, "VARCHAR" } },238 { 'L', { kBool, "BOOLEAN" } },239 { 'l', { kBool, "BOOLEAN" } },240 { 'B', { kInt8, "TINYINT" } },241 { 'b', { kUInt8, "TINYINT UNSIGNED" } },242 { 'I', { kInt16, "SMALLINT" } },243 { 'i', { kUInt16, "SMALLINT UNSIGNED" } },244 { 'J', { kInt32, "INT" } },245 { 'j', { kUInt32, "INT UNSIGNED" } },246 { 'K', { kInt64, "BIGINT" } },247 { 'k', { kUInt64, "BIGINT UNSIGNED" } },248 { 'E', { kFloat, "FLOAT" } },249 { 'e', { kFloat, "FLOAT" } },250 { 'D', { kDouble, "DOUBLE" } },251 { 'd', { kDouble, "DOUBLE" } },252 };253 254 struct Container255 {256 string branch; // fits column name257 string column; // sql column name258 BasicType_t type;259 size_t num;260 // double offset;261 void *ptr;262 263 Container(const string &b, const string &c, const BasicType_t &t, const size_t &n/*, const double &offset=0*/) : branch(b), column(c), type(t), num(n), ptr(0)264 {265 }266 Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0)267 {268 }269 ~Container()270 {271 }272 273 string fmt(const size_t &index) const274 {275 ostringstream str;276 277 switch (type)278 {279 //case kVarchar: str << string(reinterpret_cast<char*>(ptr), num); break;280 case kVarchar: str << string(reinterpret_cast<char*>(ptr), num).c_str(); break;281 case kFloat: str << setprecision(8) << reinterpret_cast<float*>(ptr)[index]; break;282 // case kMJD: str << setprecision(16) << reinterpret_cast<double*>(ptr)[0]+offset; break;283 case kDouble: str << setprecision(16) << reinterpret_cast<double*>(ptr)[index]; break;284 case kBool:285 case kInt8: str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;286 case kUInt8: str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;287 case kInt16: str << reinterpret_cast<int16_t*>(ptr)[index]; break;288 case kUInt16: str << reinterpret_cast<uint16_t*>(ptr)[index]; break;289 case kInt32: str << reinterpret_cast<int32_t*>(ptr)[index]; break;290 case kUInt32: str << reinterpret_cast<uint32_t*>(ptr)[index]; break;291 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break;292 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break;293 case kConst: str << branch; break;294 case kNone:295 break;296 }297 298 return str.str();299 }300 };301 302 195 int main(int argc, const char* argv[]) 303 196 { … … 343 236 const bool print_delete = conf.Get<bool>("print-delete"); 344 237 345 const vector<Map> mymap = conf.Vec<Map>("map"); 346 const vector<Map> sqltypes = conf.Vec<Map>("sql-type"); 238 const auto mymap = conf.Vec<Configuration::Map>("map"); 239 const auto sqltypes = conf.Vec<Configuration::Map>("sql-type"); 240 347 241 const vector<string> _ignore = conf.Vec<string>("ignore"); 348 242 const vector<string> primary = conf.Vec<string>("primary"); … … 353 247 if (first>=max) 354 248 cerr << "WARNING: Resource `first` (" << first << ") exceeds `max` (" << max << ")" << endl; 355 356 249 357 250 // ------------------------------------------------------------------------- … … 415 308 "(\n"; 416 309 417 vector< Container> vec;310 vector<FileEntry::Container> vec; 418 311 419 312 const auto fixed = conf.GetWildcardOptions("const.*"); … … 492 385 tolower(col.type) : toupper(col.type); 493 386 494 auto it = ConvFits.find(tn);495 if (it== ConvFits.end())387 const auto it = FileEntry::LUT.fits(tn); 388 if (it==FileEntry::LUT.cend()) 496 389 { 497 390 if (verbose>2) … … 509 402 cout << " (" << name << ")"; 510 403 511 string sqltype = it->s econd.second;404 string sqltype = it->sql; 512 405 513 406 for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++) … … 537 430 } 538 431 539 const BasicType_t bt =432 const FileEntry::BasicType_t bt = 540 433 /*ic.first=="Time" && col.num==1 && col.unit=="MJD" && it->second.first==kDouble ? 541 kMJD :*/ it->second.first; 542 543 vec.emplace_back(ic->first, name, bt, col.num/*, mjdref*/); 544 vec.back().ptr = f.SetPtrAddress(ic->first); 434 kMJD :*/ it->type; 435 436 vec.emplace_back(ic->first, name, bt, col.num, f.SetPtrAddress(ic->first)); 545 437 } 546 438 … … 714 606 query += ",\n"; 715 607 716 const size_t N = c->type== kVarchar ? 1 : c->num;608 const size_t N = c->type==FileEntry::kVarchar ? 1 : c->num; 717 609 for (size_t i=0; i<N; i++) 718 610 { … … 748 640 query += ",\n"; 749 641 750 const size_t N = c->type== kVarchar ? 1 : c->num;642 const size_t N = c->type==FileEntry::kVarchar ? 1 : c->num; 751 643 for (size_t i=0; i<N; i++) 752 644 { 753 if (c->type== kVarchar)645 if (c->type==FileEntry::kVarchar) 754 646 query += " '"+c->fmt(i)+"'"; 755 647 else -
trunk/FACT++/src/root2csv.cc
r19803 r19804 15 15 #include <TTreeFormulaManager.h> 16 16 17 #include "FileEntry.h" 18 17 19 using namespace std; 18 20 namespace fs = boost::filesystem; 19 21 20 22 // ------------------------------------------------------------------------ 21 22 struct Map : pair<string, string>23 {24 Map() { }25 };26 27 std::istream &operator>>(std::istream &in, Map &m)28 {29 const istreambuf_iterator<char> eos;30 string txt(istreambuf_iterator<char>(in), eos);31 32 const boost::regex expr("((.*)[^\\\\])/(.*)");33 boost::smatch match;34 if (!boost::regex_match(txt, match, expr))35 throw runtime_error("Could not evaluate map argument: "+txt);36 37 m.first = match[1].str();38 m.second = match[3].str();39 40 return in;41 }42 23 43 24 void SetupConfiguration(Configuration &conf) … … 52 33 ("ignore", vars<string>(), "Ignore the given leaf, if the given regular expression matches") 53 34 ("alias.*", var<string>(), "Define an alias") 54 ("auto-alias", vars< Map>(),"Regular expression to define aliases from the branch names automatically")35 ("auto-alias", vars<Configuration::Map>(),"Regular expression to define aliases from the branch names automatically") 55 36 ("header", var<uint16_t>(uint16_t(0)),"Type of header line (0: preceeding #, 1: without preceeding #, 2: none)") 56 37 ("add.*", var<string>(), "Define an additional column") … … 170 151 } 171 152 172 enum BasicType_t173 {174 kNone = 0,175 kConst,176 kFloat,177 kDouble,178 kInt16,179 kUInt16,180 kInt32,181 kUInt32,182 kInt64,183 kUInt64,184 };185 186 static const map<string, pair<BasicType_t, string>> ConvRoot =187 {188 { "Float_t", { kFloat, "FLOAT" } },189 { "Double_t", { kDouble, "DOUBLE" } },190 { "ULong64_t", { kUInt64, "BIGINT UNSIGNED" } },191 { "Long64_t", { kInt64, "BIGINT" } },192 { "UInt_t", { kUInt32, "INT UNSIGNED" } },193 { "Int_t", { kInt32, "INT" } },194 { "UShort_t", { kUInt16, "SMALLINT UNSIGNED" } },195 { "Short_t", { kInt16, "SMALLINT" } },196 };197 198 struct Container199 {200 static map<void*, size_t> counter;201 202 string branch; // branch name203 string column; // column name204 BasicType_t type;205 size_t num;206 void *ptr;207 208 Container(const string &b, const string &c, const BasicType_t &t, const size_t n=1) : branch(b), column(c), type(t), num(n), ptr(0)209 {210 switch (t)211 {212 case kFloat: ptr = new Float_t[n]; break;213 case kDouble: ptr = new Double_t[n]; break;214 case kInt16: ptr = new Short_t[n]; break;215 case kUInt16: ptr = new UShort_t[n]; break;216 case kInt32: ptr = new Int_t[n]; break;217 case kUInt32: ptr = new UInt_t[n]; break;218 case kInt64: ptr = new Long64_t[n]; break;219 case kUInt64: ptr = new ULong64_t[n]; break;220 case kConst:221 case kNone:222 break;223 }224 counter[ptr]++;225 }226 Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0)227 {228 }229 230 Container(const Container &c) : branch(c.branch), column(c.column), type(c.type), num(c.num), ptr(c.ptr)231 {232 counter[ptr]++;233 }234 235 ~Container()236 {237 counter[ptr]--;238 if (counter[ptr]==0)239 ::operator delete[](ptr); // It seems root is deleting it already240 }241 242 string fmt(const size_t &index) const243 {244 ostringstream str;245 246 switch (type)247 {248 case kFloat: str << setprecision(8) << reinterpret_cast<Float_t*>(ptr)[index]; break;249 case kDouble: str << setprecision(16) << reinterpret_cast<Double_t*>(ptr)[index]; break;250 case kInt16: str << reinterpret_cast<Short_t*>(ptr)[index]; break;251 case kUInt16: str << reinterpret_cast<UShort_t*>(ptr)[index]; break;252 case kInt32: str << reinterpret_cast<Int_t*>(ptr)[index]; break;253 case kUInt32: str << reinterpret_cast<UInt_t*>(ptr)[index]; break;254 case kInt64: str << reinterpret_cast<Long64_t*>(ptr)[index]; break;255 case kUInt64: str << reinterpret_cast<ULong64_t*>(ptr)[index]; break;256 case kConst: str << branch; break;257 case kNone:258 break;259 }260 261 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")262 // return "NULL";263 264 return str.str();265 }266 };267 268 map<void*, size_t> Container::counter;269 270 153 void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg) 271 154 { … … 279 162 280 163 // --------------------------- Write Header -------------------------------- 281 void WriteHeader(ostream &out, const vector< Container> &vec, const vector<TTreeFormula*> &form, bool skip, uint16_t header)164 void WriteHeader(ostream &out, const vector<FileEntry::Container> &vec, const vector<TTreeFormula*> &form, bool skip, uint16_t header) 282 165 { 283 166 if (header>1) … … 384 267 const bool print_leaves = conf.Get<bool>("print-leaves"); 385 268 386 const vector<string> _ignore= conf.Vec<string>("ignore");387 const vector<Map> autoalias = conf.Vec<Map>("auto-alias");269 const auto _ignore = conf.Vec<string>("ignore"); 270 const auto autoalias = conf.Vec<Configuration::Map>("auto-alias"); 388 271 389 272 // ------------------------------------------------------------------------- … … 459 342 cout << "\n-------------------------- Evaluating output -----------------------" << endl; 460 343 461 vector< Container> vec;344 vector<FileEntry::Container> vec; 462 345 463 346 /* … … 571 454 const string tn = L->GetTypeName(); 572 455 573 auto it = ConvRoot.find(tn);574 if (it== ConvRoot.end())456 const auto it = FileEntry::LUT.root(tn); 457 if (it==FileEntry::LUT.cend()) 575 458 { 576 459 if (verbose>2) … … 585 468 cout << " (" << name << ")"; 586 469 587 vec.emplace_back(o->GetTitle(), name, it-> second.first, L->GetLenStatic());470 vec.emplace_back(o->GetTitle(), name, it->type, L->GetLenStatic()); 588 471 c.SetBranchAddress(o->GetTitle(), vec.back().ptr); 589 472 } … … 690 573 const string tn = L->GetTypeName(); 691 574 692 auto it = ConvRoot.find(tn);693 if (it== ConvRoot.end())575 const auto it = FileEntry::LUT.root(tn); 576 if (it==FileEntry::LUT.cend()) 694 577 { 695 578 if (verbose>2) … … 704 587 cout << " (" << *l << ")"; 705 588 706 vec.emplace_back(l->c_str(), l->c_str(), it-> second.first, L->GetLenStatic());589 vec.emplace_back(l->c_str(), l->c_str(), it->type, L->GetLenStatic()); 707 590 c.SetBranchAddress(l->c_str(), vec.back().ptr); 708 591 } … … 718 601 c.SetBranchStatus("*", 0); 719 602 for (auto v=vec.cbegin(); v!=vec.cend(); v++) 720 if (v->type!= kConst)603 if (v->type!=FileEntry::kConst) 721 604 c.SetBranchStatus(v->branch.c_str(), 1); 722 605 -
trunk/FACT++/src/root2sql.cc
r19282 r19804 15 15 #include <TError.h> 16 16 17 #include "FileEntry.h" 18 17 19 using namespace std; 18 20 namespace fs = boost::filesystem; … … 20 22 // ------------------------------------------------------------------------ 21 23 24 /* 22 25 struct Map : pair<string, string> 23 26 { … … 40 43 return in; 41 44 } 42 45 */ 43 46 44 47 void SetupConfiguration(Configuration &conf) … … 52 55 ("tree,t", var<string>("Events"), "Name of the root tree to convert") 53 56 ("table", var<string>(""), "Name of the table to use (default is the tree name)") 54 ("map", vars< Map>(),"A regular expression which is applied to the leaf name befoee it is used as SQL column name)")55 ("sql-type", vars< Map>(),"Allows to overwrite the calculated SQL type for a given column e.g. 'sql-column-name/UNSIGNED IN'")57 ("map", vars<Configuration::Map>(),"A regular expression which is applied to the leaf name befoee it is used as SQL column name)") 58 ("sql-type", vars<Configuration::Map>(),"Allows to overwrite the calculated SQL type for a given column e.g. 'sql-column-name/UNSIGNED IN'") 56 59 ("ignore", vars<string>(), "Ignore the given leaf, if the given regular expression matches") 57 60 ("primary", vars<string>(), "List of columns to be used as primary keys during table creation (in connection with --create)") … … 223 226 } 224 227 225 enum BasicType_t226 {227 kNone = 0,228 kConst,229 kFloat,230 kDouble,231 kInt16,232 kUInt16,233 kInt32,234 kUInt32,235 kInt64,236 kUInt64,237 };238 239 static const map<string, pair<BasicType_t, string>> ConvRoot =240 {241 { "Float_t", { kFloat, "FLOAT" } },242 { "Double_t", { kDouble, "DOUBLE" } },243 { "ULong64_t", { kUInt64, "BIGINT UNSIGNED" } },244 { "Long64_t", { kInt64, "BIGINT" } },245 { "UInt_t", { kUInt32, "INT UNSIGNED" } },246 { "Int_t", { kInt32, "INT" } },247 { "UShort_t", { kUInt16, "SMALLINT UNSIGNED" } },248 { "Short_t", { kInt16, "SMALLINT" } },249 };250 251 struct Container252 {253 static map<void*, size_t> counter;254 255 string branch; // branch name256 string column; // column name257 BasicType_t type;258 size_t num;259 void *ptr;260 261 Container(const string &b, const string &c, const BasicType_t &t, const size_t &n) : branch(b), column(c), type(t), num(n), ptr(0)262 {263 switch (t)264 {265 case kFloat: ptr = new Float_t[n]; break;266 case kDouble: ptr = new Double_t[n]; break;267 case kInt16: ptr = new Short_t[n]; break;268 case kUInt16: ptr = new UShort_t[n]; break;269 case kInt32: ptr = new Int_t[n]; break;270 case kUInt32: ptr = new UInt_t[n]; break;271 case kInt64: ptr = new Long64_t[n]; break;272 case kUInt64: ptr = new ULong64_t[n]; break;273 case kConst:274 case kNone:275 break;276 }277 counter[ptr]++;278 }279 Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0)280 {281 }282 283 Container(const Container &c) : branch(c.branch), column(c.column), type(c.type), num(c.num), ptr(c.ptr)284 {285 counter[ptr]++;286 }287 288 ~Container()289 {290 counter[ptr]--;291 if (counter[ptr]==0)292 ::operator delete[](ptr); // It seems root is deleting it already293 }294 295 string fmt(const size_t &index) const296 {297 ostringstream str;298 299 switch (type)300 {301 case kFloat: str << setprecision(8) << reinterpret_cast<Float_t*>(ptr)[index]; break;302 case kDouble: str << setprecision(16) << reinterpret_cast<Double_t*>(ptr)[index]; break;303 case kInt16: str << reinterpret_cast<Short_t*>(ptr)[index]; break;304 case kUInt16: str << reinterpret_cast<UShort_t*>(ptr)[index]; break;305 case kInt32: str << reinterpret_cast<Int_t*>(ptr)[index]; break;306 case kUInt32: str << reinterpret_cast<UInt_t*>(ptr)[index]; break;307 case kInt64: str << reinterpret_cast<Long64_t*>(ptr)[index]; break;308 case kUInt64: str << reinterpret_cast<ULong64_t*>(ptr)[index]; break;309 case kConst: str << branch; break;310 case kNone:311 break;312 }313 314 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")315 // return "NULL";316 317 return str.str();318 }319 };320 321 map<void*, size_t> Container::counter;322 323 228 void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg) 324 229 { … … 380 285 const bool print_delete = conf.Get<bool>("print-delete"); 381 286 382 const vector<Map> mymap = conf.Vec<Map>("map"); 383 const vector<Map> sqltypes = conf.Vec<Map>("sql-type"); 287 const auto mymap = conf.Vec<Configuration::Map>("map"); 288 const auto sqltypes = conf.Vec<Configuration::Map>("sql-type"); 289 384 290 const vector<string> _ignore = conf.Vec<string>("ignore"); 385 291 const vector<string> primary = conf.Vec<string>("primary"); … … 451 357 "(\n"; 452 358 453 vector< Container> vec;359 vector<FileEntry::Container> vec; 454 360 455 361 const auto fixed = conf.GetWildcardOptions("const.*"); … … 537 443 const string tn = L->GetTypeName(); 538 444 539 auto it = ConvRoot.find(tn);540 if (it== ConvRoot.end())445 const auto it = FileEntry::LUT.root(tn); 446 if (it==FileEntry::LUT.cend()) 541 447 { 542 448 if (verbose>2) … … 554 460 cout << " (" << name << ")"; 555 461 556 string sqltype = it->s econd.second;462 string sqltype = it->sql; 557 463 558 464 for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++) … … 574 480 } 575 481 576 vec.emplace_back(o->GetTitle(), name, it-> second.first, L->GetLenStatic());482 vec.emplace_back(o->GetTitle(), name, it->type, L->GetLenStatic()); 577 483 T->SetBranchAddress(o->GetTitle(), vec.back().ptr); 578 484 } … … 593 499 T->SetBranchStatus("*", 0); 594 500 for (auto c=vec.cbegin(); c!=vec.cend(); c++) 595 if (c->type!= kConst)501 if (c->type!=FileEntry::kConst) 596 502 T->SetBranchStatus(c->branch.c_str(), 1); 597 503
Note:
See TracChangeset
for help on using the changeset viewer.