Extracting histograms in loop

Hey,

I have 8 different root files and from each root file I want to extract two histograms of the same name (data_29599 and data_2199). My printout statements indicate that the histograms are not found.

 void Fitting_Code() { //OPENING BRACE

  int numfiles = 8;
  int histograms= 2;  
 TFile* s[numfiles];

 std::vector<std::string> root_File_Names = {"Plots_Cos_CS_Theta_Squared_MC.root",
				      "Plots_Cos_CS_Theta_Squared_MC_Low_Q.root",
				      "Plots_Cos_CS_Theta_Squared_MC_High_Q.root",
				      "Plots_Cos_CS_Theta_Squared_MC_Low_qT.root",
				      "Plots_Cos_CS_Theta_Squared_MC_High_qT.root",
				      "Plots_Cos_CS_Theta_Squared_MC_Low_Photon_Pt.root",
				      "Plots_Cos_CS_Theta_Squared_MC_Mid_Photon_Pt.root",
				      "Plots_Cos_CS_Theta_Squared_MC_High_Photon_Pt.root"
  };  //The file containing all the root files needs to be transformed into a string, one string per file and be named the same as the file name

  std::vector<std::string> Hist_List = {"Data_29599", "Data_29199"};

  for (Int_t i = 0; i <numfiles; i++)
  {
      s[i] = TFile::Open(root_File_Names[i].c_str());  //Loops through the file names in the .c_str file

     std::cout << root_File_Names[i].c_str()  << std::endl;

      for (Int_t j=0; j<histograms; j++)
    {

      std::cout << Hist_List[j].c_str()  << std::endl;

      TH1D *h1 = (TH1D*)s[i]->Get("Data_29599");
       if(h1==0){

        std::cout << "h1 found" << std::endl;
     }
     else { std::cout <<"h1 not found" << std::endl;
    }
  }
  	
  }

} //CLOSING BRACE

Try to execute:

rootls -l Plots_Cos_CS_Theta_Squared_MC*.root | grep Data_29[15]99

My terminal gave the following output:

TH1D Mar 24 20:42 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 20:42 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:40 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:40 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:23 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:23 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:34 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:34 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:37 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:37 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:12 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:12 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:31 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:31 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:39 Data_29199 “Data_MuMuY_CS_Phi_Replacement_Plot”
TH1D Mar 24 11:39 Data_29599 “Data_MuMuY_CS_Phi_Replacement_Plot”

       if(h1!=0){ // histogram found

Ahhhh… I get this error message:

error: no member named ‘Get’ in ‘TH1D’
TH1D *h1 = (TH1D*)h[j]->Get(“Data_29599”);

Am I supposed to therefore access it another way?

TH1D *h1; s[i]->GetObject(Hist_List[0].c_str(), h1);
TH1D *h2; s[i]->GetObject(Hist_List[1].c_str(), h2);

Hey,

Thank you for this. I wouldn’t have realised I needed GetObject (from my googlings I’ve yet to see this used in this way), so thank you. I will have a read up on GetObject now for future reference.

Amy

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