Function with .txt file as argument

Hi all,

I am new to ROOT and I’ve a macro that repeats the same thing over and over, so it is probably a good idea to make this part of the code a function, but I don’t know how to do this partially because the function would need to take a data.txt file as an argument.

TGraphErrors *gr1 = new TGraphErrors("~/macros/centralitypions5.txt", "%lg %*s %*s %lg %lg");
gr1->SetLineColor(kRed);
pions->Add(gr1);

I could also download the data as a ROOT file, which has a TDirectoryFile in it, which is something I don’t know how to work with. So in short could someone help me define this piece of code as a function?

Hi,
this is more a C++ question than a ROOT question, but what you want is to pass around the string that contains the filename, so your function should take a string. It should also take whatever pions is as an argument since it uses it:

void MakeGraph(const std::string &filename, WhateverPionsTypeIs *pions) {
  TGraphErrors *gr1 = new TGraphErrors(filename.c_str(), "%lg %*s %*s %lg %lg");
  gr1->SetLineColor(kRed);
  pions->Add(gr1);
}

Hope this helps,
Enrico

Thank you for your help!

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