How to remove semicolon while reading ".txt" files

ROOT Version: 6.22
Platform: Ubuntu 20.04

Dear experts,

I don’t know how to read the .txt files in ROOT while a semicolon exists at the end of every data.
such as “2.5; 3.6; 4.5;” .
If I use “ifstream” directly, only one data can be input and end up reading immediately.
How can I solve it? Thanks for your help.

Best regard
Jiechen

TTree *t = new TTree("t", "t");
t->ReadFile("data_file.txt", "x/D", ';');

Dear Wile,

Many thanks for your reply, I try your code, seems that it can only read the first column. My “.txt” file includes 42 columns and each data with semicolon, if I want to extract the 31th column, how can I reach it or what if I want to write them to a tree with 42 branches ? Is there an examples?

Thanks again for your help.

Best regard,
Jiechen

t->ReadFile("data_file.txt", "x[42]/D", ';');

Dear Wile,
Thanks for your reply, here is my code as you suggested,

 TTree *t = new TTree("t", "t");
t->ReadFile("ss.txt", "x[42]/d", ';');
t->Draw("x");

The plot shows all 42 columns, how can reach it if I want to extract x[21] and x[31]?

Best regard,
Jiechen

Not sure what you mean, try: t->Draw("x[31]:x[21]");

BTW. I hope you do know that /d” means “a 24 bit truncated floating point (Double32_t).

Dear Wile,
Sorry for confusion, it work when I try your code " t->Draw(“x[31]”) " to plot the data
from 31th column, it’s so convenient. Although it can draw directly, can I add some plot commands. Usually I plot the histogram by using the “TH1F” and add my preferred plot commands, such as “SetMarkerColor,SetLineColor”.
Thanks for your help.

Best regard,
Jiechen

You can use the new RDataFrame or the old way:

TH1F *h31 = new TH1F("MyHisto31", ...);
t->Project("MyHisto31", "x[31]", ...); // or ... t->Draw("x[31] >> MyHisto31", ...)
h31->WhatEverYouWantToDoWithIt(...);

Dear Wile,

Thanks very much, it’s so great, the issue is solved by the old way.
Also I’ll try the ”RDataFrame“.

Best regard,
Jiechen

Dear Wile,

Another problem exists in my work (the same “.txt” file), as you can see the result which is signal versus time (unit is second), it can be achieved by writing the code “t->Draw(“x[36]:x[0]/50”);” [X[0]/50 means 50 bins per second]. In this way, I try the “Project()”, however, it seems not working, how can I project the tree pointer into histogram pointer while plotting signal versus time?
svst
Thanks for your help.

Best regard,
Jiechen

Dear Wile,

I refered the example ”/home/jiangjc/root-6.16.00/tutorials/tree/temperature.C“ and the new code shows below,
TTree *t = new TTree(“t”, “t”);
t->ReadFile(“sss.txt”, “x[42]/d”, ‘;’);
int Time[3]={0,0,0};
t->GetEntry(0);
Time[1]=(int)t->GetLeaf(“x[0]”)->GetValue();
t->GetEntry(t->GetEntries() - 1);
Time[2] = (int)t->GetLeaf(“x[0]”)->GetValue();
Time[2]=Time[2]+1;
Time[0]=Time[2]-Time[1];

TProfile *h=new TProfile(“h”,“h”,Time[0],Time[1],Time[2]);
t->Project(“h”, “x[36]:x[0]”);
h->SetMarkerStyle(8);
h->SetMarkerSize(0.75);
h->Draw();

The problem is that the “x[0]” is a ”branchDescriptor“ rather than a “leafname”,how can I solve it?
Thanks for your help.

Best regard,
Jiechen

In the “${ROOTSYS}/tree/temperature.C” tutorial, all leaves (YEAR/I:MONTH/I:DAY/I:T/F) are ordinary variables (not arrays).

You can use TTree::Project and/or TTree::Draw to fill a TProfile.
Search for “Making a Profile histogram” in the TTree::Draw method description.
Try something like: t->Project("h", "x[36]:x[0]", "", "prof"); // assuming "h" is a TProfile

Dear Wile,

Thanks for your reply, actually, I want to get the values of the first column and the last column for a row, I am not sure weather I can use the function (like GetValue()) to obtain the values.

Best regard,
Jiechen

Try: t->GetLeaf("x")->GetValue(0); // x[0]

Dear Wile,
Geant, it works, thanks very much for your help and have a nice day.

Best regard,
Jiechen

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