Reading from a txt input file


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


Hi, I have a source input file with two columns of data separated by white space (X Y). I want to graph all these points. I have a code, i trying to run it with macros but it doesn’t let me have a void function().
Here is my code:

#include <vector>
#include <iostream>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;

//some ROOT includes
#include "TInterpreter.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TH2S.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TVectorD.h"
#include "TGraph.h"

void readFile()
{
    std::vector<Double_t> channel, activity;
    ifstream inputFile;
    inputFile.open("/Desktop/Channel2415.txt");

    int line = 0;
    while (!inputFile.eof())
    {
        inputFile >> channel.push_back(line);
        inputFile >> activity.push_back(line);
        ++line;
    }
    
    auto Graph5 = new TCanvas("Graph","Channel Activity");
    auto channelGraph = new TGraph(8256, channel.data(),activity.data());
    channelGraph -> Draw();
    Graph5 -> Draw();


    inputFile.close();


} ```
    Double_t x, y;
    while (inputFile >> x >> y) { channel.push_back(x); activity.push_back(y); }
    auto channelGraph = new TGraph(channel.size(), channel.data(), activity.data());

Note also that you do not need to read your data file manually. You can simply use: auto channelGraph = new TGraph("/Desktop/Channel2415.txt");

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