Dosa
June 1, 2020, 8:22pm
1
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.
Dosa
June 2, 2020, 2:38pm
3
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?
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
Dosa
June 2, 2020, 3:53pm
7
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
Dosa
June 2, 2020, 4:18pm
9
You are right ? I am very basic in C++ so I do not to do that indeed.
Dosa
June 2, 2020, 5:26pm
10
Sorry!
I do not how iterate each column as 1D array?
couet
June 3, 2020, 6:54am
11
Once you have read your file, the simple way will be to plot the arrays you get using TGraph.
Dosa
June 3, 2020, 7:15am
12
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 ?
couet
June 3, 2020, 7:44am
13
It would be simpler if you can provide a small running macro showing what you achieved already.
Dosa
June 3, 2020, 8:10am
14
#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
couet
June 3, 2020, 8:27am
15
You code does not give any thing meaningful. Note there is a C++ error at compilation time. Have you run it ?
Dosa
June 3, 2020, 8:36am
16
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
===========================================================
couet
June 3, 2020, 8:39am
17
can you post a sample of this file ?
couet
June 3, 2020, 9:02am
19
dosa.C (587 Bytes)
dosa.txt (796 Bytes)
Dosa
June 3, 2020, 9:15am
20
Thank you very much Couet .