Histograms coming out blank

Hey,

In the following bits of code, I open up 8 root files, select two histograms from each root file, divide them and then fit the resulting histogram. At the moment only on the 1st and 4th iteration of the loops is a histogram not coming out blank. On all the other loop iterations the canvas has the fit statistics on the canvas and all the labels, but there is no data and no fit.

    Double_t fitf(Double_t *x, Double_t *par)
    {    
        Double_t fitval = par[0] + par[1]*TMath::Cos(2.0*x[0]) + par[2]*TMath::Cos(4.0*x[0]);
        return fitval;
    }

     void Fitting_Code() { //OPENING BRACE

       //declare a new TFile in which to save histograms
       TFile *file = new TFile ("Division_Plots.root", "RECREATE");
        file->cd();

        const int numfiles = 8;
       TFile* s[numfiles];

       std::vector<std::string> histogram_names = {"one", "Two","Three", "Four", "five", "six", "seven", "eight"/*, "nine", "Ten", "eleven", "Twelve", "Thirten", "Fourteen", "fiften", "sixten"*/};

      TCanvas *b[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
   
	  TH1D *h1;
	  s[i]->GetObject(Hist_List[0].c_str(), h1);
	  TH1D *h2;
	  s[i]->GetObject(Hist_List[1].c_str(), h2);

	  TH1D *h12 = new TH1D(*h1);

	  h12->Divide(h2,h1,1,1);
	  
	  b[i] = new TCanvas(Form("b%i",i),Form("Plot Canvas %i",i), 700, 500);
	  
	  h12->Draw();
	 
	  h12->GetYaxis()->SetRangeUser(-1.0,3.0);
	  gStyle ->SetOptStat(1111111);
	  gStyle->SetOptFit();
  
	  TF1 * f1 = new TF1("f1",fitf,-3.14159,3.14159,3);
	  f1->SetParameters(0.25,0.25,0.);
	  
	  h12->Fit(f1,"","",-3.14159,3.14159);
	  h12->Fit(f1,"","",-3.14159,3.14159);

	  h12->Draw();	 	    

	  b[i]->SaveAs(Form("c%s.png",histogram_names[i].c_str()));
	
	  file->cd();
	   b[i]->Write();
	 
      
      } //closing i loop

  file->cd();

  file->Write();

} //CLOSING BRACE

Try to replace:

TH1D *h12 = new TH1D(*h1);
h12->Divide(h2,h1,1,1);

with:

TH1D *h12 = new TH1D(*h1);
h12->Divide(h2);

or:

TH1D *h12 = new TH1D(*h2);
h12->Divide(h1);

Remove both h12->Draw(); calls and the second call to h12->Fit(...);.

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Hey,

I’ve made the changes you have recommended, but I am getting the following errors:

././Fitting_Code.C:133:18: error: allocation of incomplete type ‘TF1’
TF1 *f1 = new TF1(“f1”,fitf,-3.14159,3.14159,3);
^~~
/cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.10.04-x86_64-slc6-gcc62-opt/include/RooGlobalFunc.h:49:7: note: forward declaration of ‘TF1’
class TF1 ;
^
In file included from input_line_11:9:
././Fitting_Code.C:134:6: error: member access into incomplete type ‘TF1’
f1->SetParameters(0.25,0.25,0.);

Also, you probably need to use:

	  b[i] = new TCanvas(TString::Format("b%i", i), TString::Format("Plot Canvas %i", i), 700, 500);

Try:

#include "TMath.h"
#include "TString.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TFile.h"
#include "TH1.h"
#include "TF1.h"

Double_t fitf(Double_t *x, Double_t *par) {
  Double_t fitval = par[0] + par[1]*TMath::Cos(2.0*x[0]) + par[2]*TMath::Cos(4.0*x[0]);
  return fitval;
}

void Fitting_Code() { //OPENING BRACE
  gStyle->SetOptStat(1111111);
  gStyle->SetOptFit();
  
  //declare a new TFile in which to save histograms
  TFile *file = new TFile ("Division_Plots.root", "RECREATE");
  if ((!file) || file->IsZombie()) { delete file; return; } // just a precaution
  
  const char *histogram_names[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen"};
  
  const char *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"};
  
  Int_t numfiles = TMath::Min( (sizeof(root_File_Names) / sizeof(char*)),
                               (sizeof(histogram_names) / sizeof(char*)) );
  
  const char *Hist_List[] = {"Data_29599", "Data_29199"};
  
  for (Int_t i = 0; i < numfiles; i++) {
    TFile *f = TFile::Open(root_File_Names[i]);  //Loops through the file names in root_File_Names
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    
    TH1D *h1; f->GetObject(Hist_List[0], h1);
    TH1D *h2; f->GetObject(Hist_List[1], h2);
    if ((!h1) || (!h2)) { delete f; continue; } // just a precaution
    
#if 0 /* 0 or 1 */
    TH1D *h12 = new TH1D(*h1);
    h12->Divide(h2);
#else /* 0 or 1 */
    TH1D *h12 = new TH1D(*h2);
    h12->Divide(h1);
#endif /* 0 or 1 */
    
    h12->GetYaxis()->SetRangeUser(-1.0, 3.0);
    
    TCanvas *c = new TCanvas(TString::Format("b%i", i),
                             TString::Format("Plot Canvas %i", i),
                             700, 500);
    
    TF1 *f1 = new TF1("f1", fitf, -3.14159, 3.14159, 3);
    f1->SetParameters(0.25, 0.25, 0.);
    
    h12->Fit(f1, "", "", -3.14159, 3.14159);
    
    c->SaveAs(TString::Format("c%s.png", histogram_names[i]));
    
    file->cd();
    c->Write();
    
    delete c;
    delete f;
    delete f1;
  } //closing i loop
  
  delete file;
} //CLOSING BRACE

Hey,

Thank you for your efforts in writing this bit of code. However, I am still getting the exact same problem, in that only on the 1st (i=0) and 4th (i=3) iteration of the loops are canva’s being produced that I require. The plots are still coming out blank :-(.

Amy

Maybe your original histograms are “blank”.

Hey,

I’ve checked the two relevent histograms in all 8 files and they are completley fine and not blank…

I’m afraid we need at et least one good file and a bad one for inspection (attach them here).

Previously I checked to see if the h12’s were being created fine (after the division) and they were when I saved them to a root file. But yes, below is a good and bad histogram

I meant the original “.root” files so that one can try to run the macro.

Anyhow, from your macro, remove the line h12->GetYaxis()->SetRangeUser(-1.0, 3.0); as the histogram in the second canvas seems to have many more events (compare the “Integral”).

I have managed to fix the problem, it was the SetRangeUser causing problems, thank you!

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