Looping over multiple root files and extracting particular histograms

Hey,

I have 8 root files containing ~200+ histograms. All the histograms in the 8 different root files have the same naming structure. So I want to extract histograms titled 27599 and 27199 from each root file in turn, divde them and then fit them. I created a small c_str file which is simply a list of the root files with their path directories separated by commas. However, I keep getting the error message saying the file does not exist for each root file.

Below is a short snippet of the code:

void Fitting_Code() { //OPENING BRACE

   int numfiles = 8;

  TFile* s[numfiles];
  for (Int_t i = 0; i <numfiles; i++)
{
    s[i] = TFile::Open("root_File_Names.c_str()", "read");
    
       TH1D *h1 = (TH1D*)s[i]->Get("Data_27199");
        TH1D *h2 = (TH1D*)s[i]->Get("Data_27599");
        TH1D *h12=new TH1D(*h1);
        h12->Divide(h1,h2,1.,1.);
     
       //Perform fitting of the newly created histogram here. 

   
      }

}

This will not work. You need to transform root_File_Names into something like a std::vector<std::string> (one string per file name) and then use:

    s[i] = TFile::Open(root_File_Names[i].c_str());

Hey,

I’m not sure how to do that…

Amy

Try something like:

std::vector<std::string> root_File_Names = {"first_file", "second_file", "third_file", ...};

I’m still getting the same error about the files not existing.

EDIT: Nvm, had a typo and now fixed.

Thank you so much!

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