How to plot several columns in one in ROOT

Hello there

I desperately need to figure out how to do this.

I have txt file with hundreds (columns) and I need to plot all this one plot but I did fine an appropriate way to manage this.
Moreover, I need the Root to call each column from txt file and multiple by certains factor before plotting.

Thank you in advance

Hello,

Is the file a CSV file?

If that is the case, in order to read the file you could use RDataFrame, with its CSV data source:

https://root.cern.ch/doc/master/df014__CSVDataSource_8C.html

For the plotting part, what plot do you want to generate exactly? @couet can help with that.

Is file.txt.
Yes for plotting part I need to graphic plot, the first column vs rest of the columns using loop in C++ part of ROOT.

Thanks

In what format is file.txt ? Is it a comma-separated value (CSV) file?

No comma separation.

You will need to read the file with C++ streams then:

http://www.cplusplus.com/doc/tutorial/files/

convert the values from strings to whatever type they are (e.g. floats), and then do the plotting from them.

1 Like

Thanks very much
For example I found this script here in the forum.
It does exactly what I want in C++ part, just I want to generalize it in way that does for m number of columns columns.

{
	Int_t N = 100;
	string data_array[N][2];
	ifstream in_file("test.txt", ios::binary);
	
	if(!in_file.is_open()){
		cout << "File not opened..." << endl;

	}
	
	
	for(int i=0; !in_file.eof(); i++){
		in_file >> data_array[i][0];
		in_file >> data_array[i][1];
	
	     }
	
	
	for(int i=0; i<N; i++){
		cout << data_array[i][0] << "  " << data_array[i][1] << '\n';
	

}

I think what you need is to read every line, strip out the different column values, and then iterate on them.

1 Like

You are right ? I am very basic in C++ so I do not to do that indeed.

Sorry!
I do not how iterate each column as 1D array?

Once you have read your file, the simple way will be to plot the arrays you get using TGraph.

Thank a lot Couet
I would be helpful if you demonstrate that for in nxm Matrix so I can figure out how to iterate each 1D array in C++ part ?

It would be simpler if you can provide a small running macro showing what you achieved already.

#include <fstream>
#include <string.h>
#include <vector>
#include <TTree.h>
#include <TH1F.h>
#include <TLegend.h>
#include <TLatex.h>
#include <iomanip>
#include <cstdio>
#include <math.h>
#include <list>
#include <ctime>
#include <TGraph.h>
#include <TBranch.h>
#include <TSystem.h>
using namespace std;
void Compa();

//=============================================================================
void Compa()


{
Int_t N = 7;
Int_t M = 6;
string decay_array[6][7];
string time_step [10038];	
ifstream DECAY("output.txt", ios::binary);
	
    /*if(!DECAY.is_open()){
    cout << "The decay input is not exist " << endl;

        }*/
	
    for(int i=0;  i<M; i++;){	
        DECAY >> decay_array[i][0];
        DECAY >> decay_array[i][1];
        DECAY >> decay_array[i][2];
        DECAY >> decay_array[i][3];
        DECAY >> decay_array[i][4];
        DECAY >> decay_array[i][5];
        DECAY >> decay_array[i][6];
	
	}
	
	
	for(int i=0; i<N; i++){
	    cout << decay_array[i][0] << decay_array[i][1] << decay_array[i][2] << decay_array[i][3] << decay_array[i][4] << decay_array[i][5] << decay_array[i][6] << '\n';	
        }
	 
        TGraph *plot = new TGraph("output.txt");
	plot->Draw("AP");

}

And instead of declaring the value of N each time manually ; I need it as decay_array[i][N] so it
will iterate for all column .
Thank you in advance

You code does not give any thing meaningful. Note there is a C++ error at compilation time. Have you run it ?

yes i got error .

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007fb0c35011ed in std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) () from /usr/lib64/libstdc++.so.6
===========================================================

can you post a sample of this file ?

0.000 1.10091468698e-09 1.10091468698e-09 1.10091468698e-09 1.10091468698e-09 1.10091468698e-09 1.10091468698e-09
1.157 3.34156943562e-06 3.3425750962e-06 3.34558348255e-06 3.34240318841e-06 3.34758620832e-06 3.34541157476e-06
1.157 9.14317374789e-07 9.14592427255e-07 9.15417584654e-07 9.14549450308e-07 9.15967689587e-07 9.15374607706e-07
1.736 9.09486765851e-07 9.09761818317e-07 9.10578380326e-07 9.09710245979e-07 9.11119889869e-07 9.10526807989e-07
2.314 9.04965590936e-07 9.05232048013e-07 9.06048610022e-07 9.05189071065e-07 9.06590119565e-07 9.05997037685e-07
2.893 9.00659300762e-07 9.00925757838e-07 9.01742319848e-07 9.0088278089e-07 9.0228382939e-07 9.0169074751e-07
3.472 8.96524918378e-07 8.96799970844e-07 8.97599342075e-07 8.96748398507e-07 8.98140851617e-07 8.97556365127e-07

dosa.C (587 Bytes)
dosa.txt (796 Bytes)

Thank you very much Couet .