Reading a set of value from a single line text file and use it as a constant

Hi I am trying to read a file that contain a single line, something like this


where it is corresponds to year,month,day,hour,minute,second. So the part of my code that tries to read this, I seem to stuck on figuring how to assign it individual variable.

TString dir = gSystem->UnixPathName(__FILE__);

   	TTree *T = new TTree("ntuple","date from date.txt");
   	Long64_t date = T->ReadFile(Form("date.txt",dir.Data()),"year:month:day:hour:minute:second");

Any help is appreciated.


I don’t understand where exactly your problem lies. Is it reading the file? Is it importing it into a TTree?

First of all, when you just have a single line, why use a TTree at all?

If the single line is just an example and you have many lines in your real data file, then your code almost works: you need to specify a delimiter for your input file like this:

auto entriesRead = T->ReadFile("date.txt", "year:month:day:hour:minute:second", ',');

Note also that the result is the number of lines imported, i.e. 1 if you are importing a single line.
You can then run T->Scan(“year:month:day:hour:minute:second”); to check if the result looks ok.

If you are just reading ONE line, do not use a TTree at all, instread consider using C++ streams like this:

#include <fstream>
#include <iostream>

template <char C> std::istream &expectChar(std::istream &in) {
    char c;
    if (in >> c && c != C)
    return in;

int main() {
    int year, month, day, hour, minute;
    float second;
    std::ifstream is("d.txt");
    if (is >> year >> expectChar<','>
        >> month >> expectChar<','>
        >> day >> expectChar<','>
        >> hour >> expectChar<','>
        >> minute >> expectChar<','>
        >> second)
        std::cout << "Success!\n"
                  << "Year = " << year << '\n'
                  << "Month = " << month << '\n'
                  << "Day = " << day << '\n'
                  << "h = " << hour << '\n'
                  << "m = " << minute << '\n'
                  << "s = " << second << '\n';

    } else {
        std::cout << "Error reading date\n";

Sorry if I worded my problem poorly.

That single line file contain the information(date and time), to be precise it is the start time for set of data of a root file. The “Time” of each entry of another root file however are integers of miliseconds and it is not so useful.

I thinking of reading the the single lined file that contained the date and time information that I produced from its raw data(it is in MEM, where the root file were produced from). Using the information from the single lined file I’m planning to use it to either add a new branch to the root file(so just the same time constant for all entries) or add it to the “Time” in milliseconds(in TtimeStamp) by changing the value of the branch.

A rough idea of what the root file , with first lines indicating the branch name

1, 123, 123, 123, 123, 123, 123, 123, 123
2, 123, 123, 123, 123, 123, 123, 123, 123
3, 123, 123, 123, 123, 123, 123, 123, 123

By using the start time information of that single lined .txt, I can use it like a constant that add thoughout the whole branch of “Time”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.