Weird axis behavier

Hi!

I see some really strange axis behavior in my macro and don’t know why:

I produce TH1D* in a loop and want to plot all of them into one canvas. Therefore I do:

TCanvas c;
 c.cd();
for(...)
{
  output = ...;
  if (ptbin == 1)
    output->Draw();
  else
    output->Draw("same");
  c.SaveAs("plots/" + name + ".gif");
}

Loop starts at 1, so draw first time without “same” and afterwards with “same”.
The first histogram looks fine, but all TH1D drawn with “same” are screwed up. The axis labels and ranges are wrong. If I remove the “same” options, all histograms are saved properly.

I really don’t know what’s going wrong:(

Thanks,

Tobias

Your little pseudo-code tells not enough to see what is going wrong.
The principle in fine.
Can you send a running small macro reproducing your problem ?

Hi!
Thanks for your offer to help me. I attached the macro.
I can’t give you the input file, but any random TH3D should do it.
The TH1D read from the file is in the same format as the output and loaded to get binning/axis names etc.

Thanks,

Tobias
FitResponsePtEta.C (2.57 KB)

I cannot run your macro … but I saw something a bit illogical:

Your loop is:

  for (int bin = 1; bin < input->GetNbinsY(); bin++)

but the test to detect the first plot is:

  if (ptbin == 1)

Shouldn’t it be “bin” instead of “ptbin” ?

Hi!

the

is outside the loop over “bin” (at least it should be).
“bin” runs over every single bin in the x-axis of the projection and fills the result of the fit to output.

What’s the error message if you try to run the macro?

root [0]
Processing FitResponsePtEta.C…
Error in TFile::TFile: file /atlas_tmp/huelsing/ganga/JetResponse2.root does not exist
Error: illegal pointer to class object ouput2 0x0 1448 FitResponsePtEta.C:15:
*** Interpreter error recovered ***
root [1]

Ok, so my input file is missing.
I’ll try to randomly fill a TH3D at the beginning of the script and send it again.

I hope this should work:

void
FitResponsePtEta()
{
  TH1::AddDirectory(kFALSE);
  TH1::SetDefaultSumw2(true);
  //TFile *f = new TFile("/atlas_tmp/huelsing/ganga/JetResponse2.root");
  //TH3D* input = (TH3D*) f->Get("Kt6TopoEM_response_eta_pt");
	TH3D* input = new TH3D("Kt6TopoEM_response_eta_pt","Kt6TopoEM_response_eta_pt;response;#eta;p_{T}",60,0.5,5.5,100,-4.9,4.9,1000,0,1000);
	input->FillRandom("gaus",10000000);
	TH2D* ouput2 = new TH2F("output2", "output2", 100,-4.9, 4.9, 60, 0.5, 5.5);
  TH1D* output = ouput2->ProjectionX("output");
  TString name;
  TCanvas c;
  c.cd();
  output->GetYaxis()->SetRangeUser(0, 5.5);
  double response;
  TH1D *htemp;
  TH2D *h2;
  TH1D* v_output[34]; // tried to store the histograms in an array and draw them later...
  TF1* f_fit;
  int binpt;
  int hist = 0;
	//=================================
	// run over z-axis of 3D-histogram
	//=================================
  for (int ptbin = 1; ptbin < input->GetNbinsZ(); ptbin = ptbin + 30)
    {
      output->Clear();
			//====Generate name for hists
      name = "fittedresponse";
      binpt = input->GetZaxis()->GetBinLowEdge(ptbin);
      name += binpt;
			//====Produce projections
      for (int bin = 1; bin < input->GetNbinsY(); bin++)
        {
					//Get only one slice in z
          input->GetZaxis()->SetRangeUser(ptbin, ptbin + 30);
					//Projection
          h2 = (TH2D*) input->Project3D("xy");
          htemp = h2->ProjectionY("response", bin, bin + 1, "e");
					//Fit Landau in every Histogram
          if (htemp->GetEntries() != 0)
            {
              htemp->Fit("landau", "Q", "S");
              f_fit = htemp->GetFunction("landau");
              response = f_fit->GetParameter(1);
							//Fill peak position and width as content and error
              output->SetBinContent(bin, response);
              output->SetBinError(bin, f_fit->GetParameter(2));
            }
          else
            {
							//if no fit data exists, set to 0
              output->SetBinContent(bin, 0);
              output->SetBinError(bin, 0);
            }
        }
      //      v_output[hist] = output; //fill array and indrease couter for array
			//       hist++;
      if (ptbin == 1)
        output->Draw();
      else
        output->Draw("");
//      output->Draw("same");
      c.SaveAs("plots/" + name + ".gif");
    }
  c.SaveAs("test.gif");
  //  TCanvas c2;
  //  c2.cd();
  //  v_output[0]->Draw();
  //  for (int i = 1; i < 34; i++)
  //    {
  //    if (v_output[i] != 0)
  //      {
  //        v_output[i]->Draw("SAME *HIST");
  //        cout << "Hist " << i << " filled" << endl;
  //      }
  //    else
  //      cout << "HAAALT" << endl;
  //    c2.SaveAs("plots/CHICHI.gif");
  //    }
  c2.SaveAs("CHICHI.gif");
}

No, it does not:

root [0] 
Processing FitResponsePtEta.C...
Error: Illegal initialization of pointer, wrong type TH2F* FitResponsePtEta.C:10:
*** Interpreter error recovered ***
root [1] 

Sorry.
In line 10:
TH2D* ouput2 = new TH2F(“output2”, “output2”, 100,-4.9, 4.9, 60, 0.5, 5.5);

change to

TH2D* ouput2 = new TH2D(“output2”, “output2”, 100,-4.9, 4.9, 60, 0.5, 5.5);

That works for me.

Thanks,

Tobias

Your macro is running now. It generates many gif files. They all look very similar, but the axes do not look weird for me.

Yes, but if you change Draw() to Draw(“same”), they should all be plotted into one canvas. But at this stage, the axis got screwed up.

  if (ptbin == 1)
    output->Draw();
  else

// output->Draw("");
Change this!
output->Draw(“same”);
c.SaveAs(“plots/” + name + “.gif”);

Thanks for your time!

Tobias

Ok, the plot are differents (we see the error bars and the fir on top) but still fine. No weird axis.

Ok, in my case, the x-range of the first histogram is from -4.9 to 4.9, and all the other are from 0 to 5.5 and labeled with response instead of eta…
Without “same” all histos show the same axis (-4.9 to 4.9;eta).
I should check it on a different root installation…

Thanks,

Tobias

In my case all the plots are the same except the fit one which does not have the fit curve.