Dividing histograms in loop from multiple files

I want to open up 6 root files which contain histograms all with the same naming scheme for each, each file will contain 26199 as a histogram name. I want to divide 26199 from each file by 26199 from all the other 5 files, so there will be 30 divided histograms in total (I think). I’m not sure how best to approach this in a loop. So far I know how to do it for two files (see below). I’m not necessarily looking for a solution but more the logic as I’m not sure how to do all the combinations. This is a very small watered down snippet of what I am trying to do

std::vector<std::string> Ratio_Plot_Names={26199};

 TFile *FileN0A0 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N0A0.root");
 TFile *FileN0A2 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N0A2.root");
 TFile *FileN0A4 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N0A0.root");
 TFile *FileN2A0 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N2A0.root");
 TFile *FileN2A4 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N2A4.root");
 TFile *FileN4A0 = TFile::Open("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/N4A0.root");

 
 for (int Ratio_Bins = 0; Ratio_Bins < 1; Ratio_Bins++){

 //Dividing the same bin from each file (e.g dividing 20199 from N0A0 by 20199 from N0A2)  

 TH1D *h1;
 FileN0A0->GetObject(Ratio_Plot_Names[Ratio_Bins].c_str(), h1 );
 TH1D *h2;
 FileN0A2->GetObject(Ratio_Plot_Names[Ratio_Bins].c_str(), h2 );

 TCanvas *c1 = new TCanvas ("c1", "c1", 700, 500);

  TH1D *h12 = new TH1D(*h2);  //Create a clone of h2 for the correct binning
          h12->Divide(h2 , h1, 1., 1.);  //Empties the h12 canvas, and then does the division of    h2 over h1
  h12->Draw();

        }//Closing loop over the variable names and bins (Ratio_Bins)

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


Have come up with an idea… will post once I’ve completed it

for(int i=0; i<6; i++){

 File[i] = TFile::Open(File_Names[i].c_str() );
 File[i]->Print();

 for (int j=0; j<6; j++){

   File[j] = TFile::Open(File_Names[j].c_str() );
   File[j]->Print();

   for (int Ratio_Bins = 0; Ratio_Bins < 8; Ratio_Bins++){
   
   TH1D *h1;
   File[i]->GetObject(Ratio_Plot_Names[Ratio_Bins].c_str(), h1);
   TH1D *h2;
   File[j]->GetObject(Ratio_Plot_Names[Ratio_Bins].c_str(),h2);

   h1->Print();
   h2->Print();

   TCanvas *SameSliceRatio = new TCanvas (Form ("R_%s_%sby%s", Ratio_Plot_Names[Ratio_Bins].c_str(), Label_Names[i].c_str(), Label_Names[j].c_str()) , Form ("R_%s_%sby%s", Ratio_Plot_Names[Ratio_Bins].c_str(), Label_Names[i].c_str(), Label_Names[j].c_str()), 700, 500);

   gStyle->SetOptStat(1000000);
   gROOT->ForceStyle();

  SameSliceRatio->SetLogy();

 TH1D *h12 = new TH1D(*h2);  //Create a clone of h2 for the correct binning

 SameSliceRatio->SetLogy();
 h12->Divide(h2 , h1, 1., 1.);  //Empties the h12 canvas, and then does the division of h2 over h1
 h12->SetName(Form("R_%s", Ratio_Plot_Names[Ratio_Bins].c_str() ) );
 h12->SetMinimum(0.1);
 h12->GetYaxis()->SetTitleOffset(1.00);
 h12->SetName(Form("R_%s_%sby%s", Ratio_Plot_Names[Ratio_Bins].c_str(), Label_Names[i].c_str(), Label_Names[j].c_str() )); 

 h12->Draw();
 	 
 h12->GetXaxis()->SetTitleSize(0.045);
 h12->GetYaxis()->SetTitleSize(0.045);
 h12->GetYaxis()->SetTitle("Entries");

 SameSliceRatio->Update();

 SameSliceRatio->SaveAs(Form("/home/atlas/amytee/PhD_Work_Analysis_III/MC_V5/Version_2/Ratios_and_Overlays/Plots/R_%s_%sby%s%s",  Ratio_Plot_Names[Ratio_Bins].c_str(), Label_Names[i].c_str(), Label_Names[j].c_str(), ".eps"  ) );

  Save_Plots->cd();
  h12->Write();


  
   }//Closing loop over the same slice ratio plots (Ratio_Bins)
 }  //Closing loop over files (j)

 } //closing loop over files (i)

This is my working solution

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