Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created September 3, 2019 01:58
Show Gist options
  • Save marty1885/c0f578f9bd781973b93ccda1d04899a4 to your computer and use it in GitHub Desktop.
Save marty1885/c0f578f9bd781973b93ccda1d04899a4 to your computer and use it in GitHub Desktop.
void to_root_raw()
{
// Create a reader from CSV
csv::CSVReader reader("checkouts-by-title.csv");
// Create a ROOT File. For the first pass, we'll be saving everything as a string
auto f = new TFile("library.root", "recreate");
TTree *t = new TTree("library_raw","raw data from csv file");
auto fields = reader.get_col_names();
auto buf = std::vector<std::string>(fields.size());
// Assign each buffer to a buffer
for(size_t i=0;i<fields.size();i++)
t->Branch(fields[i].c_str(), &buf[i]);
// Copy data from CSV to ROOT
for (auto& row: reader) {
for(size_t i=0;i<fields.size();i++)
buf[i] = row[fields[i]].get();
t->Fill();
}
// Write the data to root
f->Write();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment