Superimposing histograms in loop

Hey,

I am trying to open two root files, extract certain histograms from them, and then superimpose them. However, I seem to be only opening one of the files.

void Ratio_And_Overlaying_Plots() { //OPENING BRACE

  //NUMBER OF ROOT FILES
  const int numHistograms = 4;

  //DECLARE POINTERS
  TFile* s[numHistograms];
  TCanvas *b[numHistograms];

  std::vector<std::string> Histogram_Names_Mu0Mu0Y0 = {"mc20099",  //variable 20 is lambda
					       "mc26099",  //variable 26 is CosThetaCS
					       "mc27099",  //Variable 27 is PhiCS
					       "mc28099"  //Variable 28 is (q_T)^2
};   

   std::vector<std::string> Histogram_Names_Mu4Mu4Y5 = {"mc20099",
					       "mc26099",
					       "mc27099",
					       "mc28099"
  };

  std::vector<std::string> Root_File_Names = {"mc_Plots_Cos_CS_Theta_Mu0Mu0Y0.root",
				      "mc_Plots_Cos_CS_Theta_Mu4Mu4Y5.root"
  };

    for (Int_t i = 0; i <numHistograms; i++){

      s[i] = TFile::Open(Root_File_Names[0].c_str(),Root_File_Names[1].c_str()); // Open root files    
  
      TH1D *h1;
      s[i]->GetObject(Histogram_Names_Mu0Mu0Y0[i].c_str(), h1);   //Get the ith histogram from the file
      TH1D *h2;
      s[i]->GetObject(Histogram_Names_Mu4Mu4Y5[i].c_str(), h2);

     b[i] = new TCanvas(TString::Format("b%i", i), TString::Format("Plot Canvas %i", i), 700, 500);  //Declaring TCanvas on which to superimpose the two histograms.


 
            TH1D *h12 = new TH1D(*h1);  //setting h12 to h1 
      h12->SetName(Form("h_%i",i));
        h12->Draw();
      h12->SetLineColor(kRed);
      h2->Draw("same");
      h2->SetLineColor(kBlue);
       b[i]->Update();
  	 
     
      	} //closing i loop


 } //CLOSING BRACE  

Please provide the following information:


ROOT Version rootVer=6.10/04
Platform, compiler gcc gcc620_x86_64_slc6


  TFile *f1 = TFile::Open(Root_File_Names[0].c_str());
  TFile *f2 = TFile::Open(Root_File_Names[1].c_str());
  for (Int_t i = 0; i < numHistograms; i++) {
    TH1D *h1; f1->GetObject(Histogram_Names_Mu0Mu0Y0[i].c_str(), h1);
    TH1D *h2; f2->GetObject(Histogram_Names_Mu4Mu4Y5[i].c_str(), h2);

Hey,

Thanks for this. Is there anyway to extrapolate this to say 100 root files? Or would it have to all be done like your example?

  const char *File_Names[] = { "first.root",
                               "second.root",
                               // ...
                               "last.root" };
  const int numFiles = sizeof(File_Names) / sizeof(char*);
  TFile *f[numFiles];
  for (int i = 0; i < numFiles; i++) f[i] = TFile::Open(File_Names[i]);

BTW. You can, of course, easily modify it to use std::vector<std::string> File_Names; (or std::vector<const char*> File_Names;) and std::vector<TFile*> f;.

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