I cannot store string in tree


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


I am trying to store date and time data in tree.

   TFile f("tree3.root","recreate");
   TTree *t3 = new TTree("t3","Reconst ntuple");

 fstream soc;
 soc.open("Time.txt", ios::in);
 t3->Branch("date_time",date_time,"date_time");

    string time;
    
    
 while(i<=10)
 {
     if(soc.eof()) break;
     
     getline(soc, time);
     cout << time << endl;
     date_time[i] = time;
     t3->Fill();
     l=++l;
     //cout << date_time[i] << endl;
 }

This is what Time.txt file looks like

2023/08/07 17:33:31.649
2023/08/07 17:33:32.192
2023/08/07 17:33:33.604
2023/08/07 17:33:41.852
2023/08/07 17:33:43.374
2023/08/07 17:33:47.833
2023/08/07 17:33:58.806
2023/08/07 17:34:01.200
2023/08/07 17:34:02.940
2023/08/07 17:34:03.810
2023/08/07 17:34:06.637
2023/08/07 17:34:09.569
2023/08/07 17:34:13.587
2023/08/07 17:34:14.896
2023/08/07 17:34:20.247
2023/08/07 17:34:23.637
2023/08/07 17:34:23.963
2023/08/07 17:34:24.618
2023/08/07 17:34:30.838
2023/08/07 17:34:36.295
2023/08/07 17:34:37.607
2023/08/07 17:34:38.915

When I cout time, I get the time information in the txt file, but when I make date_time[i] to transfer it to tree, it does not work. cout of date_time[I] appears blank in terminal.

Hi,

I think an efficient way to achieve this is through RDataFrame:

    std::fstream soc;
    soc.open("Time.txt", ios::in);
    std::string time;
    std::vector<std::string> time_v; 
    time_v.reserve(128);
    while ( std::getline(soc, time) ) time_v.emplace_back(time);

    auto df = ROOT::RDataFrame(time_v.size()).Define("date_time", [&time_v](ULong64_t entry){return time_v[entry];}, {"rdfentry_"});
    df.Snapshot<std::string>("t3","tree3.root",{"date_time"});

Let us know how this go!

Cheers,
Danilo

To read the two columns as strings you can also do:

root [0] auto T = new TTree()
root [1] T->ReadFile("Time.txt","date/C:time/C")
root [2] T->Scan()
************************************
*    Row   * date.date * time.time *
************************************
*        0 * 2023/08/0 * 17:33:31. *
*        1 * 2023/08/0 * 17:33:32. *
*        2 * 2023/08/0 * 17:33:33. *
*        3 * 2023/08/0 * 17:33:41. *
*        4 * 2023/08/0 * 17:33:43. *
*        5 * 2023/08/0 * 17:33:47. *
*        6 * 2023/08/0 * 17:33:58. *
*        7 * 2023/08/0 * 17:34:01. *
*        8 * 2023/08/0 * 17:34:02. *
*        9 * 2023/08/0 * 17:34:03. *
*       10 * 2023/08/0 * 17:34:06. *
*       11 * 2023/08/0 * 17:34:09. *
*       12 * 2023/08/0 * 17:34:13. *
*       13 * 2023/08/0 * 17:34:14. *
*       14 * 2023/08/0 * 17:34:20. *
*       15 * 2023/08/0 * 17:34:23. *
*       16 * 2023/08/0 * 17:34:23. *
*       17 * 2023/08/0 * 17:34:24. *
*       18 * 2023/08/0 * 17:34:30. *
*       19 * 2023/08/0 * 17:34:36. *
*       20 * 2023/08/0 * 17:34:37. *
*       21 * 2023/08/0 * 17:34:38. *
************************************
root [3] 

I am working with the code you wrote. But I couldn’t get it to work.

/Users/kaan/Desktop/Cosmic/bos1.C:34:1: error: non-void function does not return a value [-Werror,-Wreturn-type]
}
^
root.exe(3608,0x112a9fdc0) malloc: *** error for object 0x7fff682df385: pointer being freed was not allocated
root.exe(3608,0x112a9fdc0) malloc: *** set a breakpoint in malloc_error_break to debug

I think root works and creates the tree file, but I see errors in all the files.