Add filename from list

ROOT Version: 6.26.14
Platform: Ubuntu

Hi All

I have several root files where (apart from some TTrees), some TH2 objects are stored. I want to add all those TH2 objects without _hadd_ing all the root files as that will make the output file large.

I have attached a small macro representing my issue. When I take filename from a list (reco.txt), it gives me error:

Error in TFile::TFile: file name is not specified
Error in TH2F::Add: Attempt to add a non-existing histogram

But when I write exact location in TFile as TFile* f1 = new TFile("some/location/file.root");
it works. Can someone please explain the issue?

Attached are the list file, macro, and the root file which were able to produce the said outputs.
addhisto.C (483 Bytes)
trialreco.root (17.1 KB)
reco.txt (51 Bytes)

Your macro is trying to open an undefined file name see:

void addhisto(){
  TH2* hist = new TH2F("herr_dipAngle_stt","",200,-0.05,0.05,10,0,10);
  std::ifstream listroot("reco.txt");
  string filename;

  if(listroot.is_open()){
    while(listroot){
      std::getline(listroot,filename);
      printf("[%s]\n",filename.c_str());
      TFile* f1 = new TFile(filename.c_str());
    }
  }
Processing addhisto.C...
[trialreco.root]
[]
Error in <TFile::TFile>: file name is not specified
root [1] 
1 Like

Check for the empty line:

      std::getline(listroot,filename);
      if (filename == "")
         break;
1 Like

I checked reco.txt but I don’t see any extra blank line there which should give me this error. So, is there a reason (code or the editor) that within the while loop an extra line is seen?

I tried by replacing while with a for loop with the number of iterations = number of lines in the file. But that is like a forced measure even though a simple measure.

Sorry, my bad. I tried your code without an empty line at the end and it works just fine.
I also tried this way:

  if (listroot.is_open()) {
    while (std::getline(listroot,filename)) {

And it works too

1 Like

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