Plotting ASCII files in ROOT

ε祈祈猫儿з 提交于 2019-12-18 09:39:24

问题


I am trying to write a small macro that reads data from an ASCII file that has 4 columns. But I want to graph only the second the third columns as (x, y).


回答1:


The constructor of TGraph can directly take a CSV file, see the documentation.

TGraph g("data.csv", "%*lg %lg %lg %*lg", ",");

The first argument is the filename, and the second argument a format string. Skipped columns are denoted with a *; to skip the last column you could actually just omit it from the format string,

%*lg %lg %lg

The third argument is the column separator which might be , for your flavor of CSV.




回答2:


follow this example: https://root.cern.ch/root/html534/tutorials/tree/basic.C.html and instead of filling a histogram make a graph.




回答3:


You can use the "CSV Contructor" as shown above or simply generate an empty TGraph (or TGraphErrors), loop over your CSV file and add points. Example with some constant error bars given in the first line/header:

    ifstream infile("input.csv");

    TGraphErrors *g1 = new TGraphErrors();
    g1->SetName("name_for_graph");
    g1->SetTitle("Title for your Graph;x values [x unit];y values [y unit]");

    Int_t pt=0, nv=0;
    Double_t e_vc=0.;
    Double_t vs=0., vc=0.;

    infile >> e_vs >> e_vc;

    while (1) {
            if(!infile.good()) break;

            infile >> vs >> vc;

            g1->SetPoint(pt, vs, vc);
            g1->SetPointError(pt, e_vs, e_vc);
            pt++;
    }
    infile.close();

...

    g1->Draw("APX");


来源:https://stackoverflow.com/questions/30475239/plotting-ascii-files-in-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!