Home | News | Documentation | Download

Formatting Problems

Hello,
I am making a plot and a residual plot. I am having some formatting problems.

  1. The y-axis label on my runner plot is too high.
  2. I want the runner plot to span the same width that my main plot does, but now it is not so.
  3. I am not sure if the residual plot I made is really plotting data-fit/bin_error.

Any help is greatly appreciated.

dummy1.txt (9.8 KB)
alpha_0_beta_and_C01.txt (785 Bytes)
alpha_0_beta_and_C01.cxx (4.1 KB)


ROOT Version: 6.23/01

Looking at it

void alpha_0_beta_and_C01(){

//   gROOT->SetStyle("Plain");
    gStyle->SetOptFit(1111);

   auto c1= new TCanvas("c","BPRE",10,10,600,600);
   c1->Divide(1,1);
   c1->SetTickx();
   c1->SetTicky();
   c1->SetTitle("");
   c1->SetLineWidth(3);
   c1->SetBottomMargin(0.1);
   c1->SetTopMargin(0.05);
   c1->SetRightMargin(0.01);
   c1->SetFillColor(0);
   c1->cd(0);
   TPad* pad1 = new TPad("pad1","pad1",0,0.3,1,0.97);
   pad1->SetBottomMargin(0);
   pad1->SetLeftMargin(0.13);
   pad1->SetRightMargin(0.04);
   pad1->SetTopMargin(0.02);
   pad1->SetGrid();
   pad1->Draw();
   pad1->cd();

   TH1F* h=pad1->DrawFrame(0,-5,360,200);
   h->GetXaxis()->SetRangeUser(0,360);
   auto hh=new TH1F("Statistics" , "" ,36,0.,360.);
   hh->SetMarkerStyle(8);
   ifstream inp; double x;
   inp.open("dummy1.txt");
   while(!(inp >> x)==0){hh->Fill(x);}
   auto graph = new TGraphErrors("./alpha_0_beta_and_C01.txt","%lg %lg %lg %lg");
   graph->SetTitle("Measurement of Coincidence Counts C01 w.r.t. Angle #beta at #alpha=0#circ + Offset; Angle #beta [degrees]; Coincidence Counts");
   graph->SetMarkerStyle(kCircle);
   graph->SetFillColor(0);
   graph->GetYaxis()->SetTitleOffset(1.3);
   graph->Draw("PEA");

   auto g = new TF1("Sinusoidal Fit","(0.5*[0]*((TMath::Cos([1]*(x-[2]))**2)))",0.,360.);
   g->SetParameters(320.0,0.02,5);
   g->SetLineColor(kRed);
   graph->Fit(g,"M","",0.,360.); // graph.Fit(g,"M","SAMES",0.,10.);
   g->Draw("same");

   // Draw the Legend
   auto leg = new TLegend(0.6,0.71,0.8,0.9,"QUED");
   leg->SetFillColor(0);
   leg->AddEntry(graph,"Measured Points");
   leg->AddEntry(g,"Fit");
   leg->Draw(); // leg.DrawClone("Same");
   gStyle->SetOptFit(1111); // Not working...
   auto text = new TLatex(121.4399,21.4941,"f(x)=0.5 #times p_{0}Cos^2(p_{1}(x-p_{2}))");
   text->SetTextSize(0.03114861);
   text->Draw();

   gPad->Update();
   TPaveStats *ps = (TPaveStats*)graph->GetListOfFunctions()->FindObject("stats");
   ps->SetX1NDC(0.3);
   ps->SetY1NDC(0.7);
   ps->SetX2NDC(0.5);
   ps->SetY2NDC(0.9);

   TH1F *DiffHist = new TH1F(*(hh));
   DiffHist->SetFillColor(2);
   TAxis* ay = DiffHist->GetYaxis();
   TAxis* ax = DiffHist->GetXaxis();
   ay->SetTitle("(Data-Fit)/Error");
   ay->CenterTitle(true);
   ay->SetLabelFont(42);
   ay->SetLabelSize(0.10);
   ay->SetTitleSize(0.3);
   ay->SetTitleOffset(0.33);
   ay->SetNdivisions(505);
   ay->SetLabelSize(0.12);
   ay->SetTitleSize(0.1);

   ax->SetTitle("Angle #beta [degrees]");
   ax->SetRangeUser(0,360);
   ax->SetLabelFont(42);
   ay->SetLabelFont(42);
   ax->SetLabelSize(0.12);
   ax->SetTitleSize(0.11);


   for ( int i = 1; i <= DiffHist->GetNbinsX(); i++ ) {
      DiffHist->SetBinContent(i,(hh->GetBinContent(i) -
      g->Eval(hh->GetBinCenter(i)))/(hh->GetBinError(i)));
   }


   TPad* pad2 = new TPad("pad2","pad2",0,0.01,1,0.3);
   c1->cd(1);
   pad2->SetLeftMargin(0.123);
   pad2->SetRightMargin(0.029);
   pad2->SetTopMargin(0);
   pad2->SetBottomMargin(0.31);

   pad2->Draw();
   pad2->cd();

   gStyle->SetOptStat(0);
   DiffHist->Draw();
}

Thank you couet. Right now it looks nice, but I want it so that the runner plot lines up with the x coordinate of the main plot. Like right now, the top plot goes from x=-40 to x=400, while the bottom one goes from x=0 to x=360, but they span the same width. Is there a way to line up the endpoints of the main plot (x=0 and x=360) with the ends of the runner plot? Like in the image below, except that it is the same width.


void alpha_0_beta_and_C01(){

//   gROOT->SetStyle("Plain");
    gStyle->SetOptFit(1111);

   auto c1= new TCanvas("c","BPRE",10,10,600,600);
   c1->Divide(1,1);
   c1->SetTickx();
   c1->SetTicky();
   c1->SetTitle("");
   c1->SetLineWidth(3);
   c1->SetBottomMargin(0.1);
   c1->SetTopMargin(0.05);
   c1->SetRightMargin(0.01);
   c1->SetFillColor(0);
   c1->cd(0);
   TPad* pad1 = new TPad("pad1","pad1",0,0.3,1,0.97);
   pad1->SetBottomMargin(0);
   pad1->SetLeftMargin(0.13);
   pad1->SetRightMargin(0.04);
   pad1->SetTopMargin(0.02);
   pad1->SetGrid();
   pad1->Draw();
   pad1->cd();

   TH1F* h=pad1->DrawFrame(0,-5,360,200);
   h->GetXaxis()->SetRangeUser(0,360);
   auto hh=new TH1F("Statistics" , "" ,36,0.,360.);
   hh->SetMarkerStyle(8);
   ifstream inp; double x;
   inp.open("dummy1.txt");
   while(!(inp >> x)==0){hh->Fill(x);}
   auto graph = new TGraphErrors("./alpha_0_beta_and_C01.txt","%lg %lg %lg %lg");
   graph->SetTitle("Measurement of Coincidence Counts C01 w.r.t. Angle #beta at #alpha=0#circ + Offset; Angle #beta [degrees]; Coincidence Counts");
   graph->SetMarkerStyle(kCircle);
   graph->SetFillColor(0);
   graph->GetYaxis()->SetTitleOffset(1.3);
   graph->Draw("PEA");

   auto g = new TF1("Sinusoidal Fit","(0.5*[0]*((TMath::Cos([1]*(x-[2]))**2)))",0.,360.);
   g->SetParameters(320.0,0.02,5);
   g->SetLineColor(kRed);
   graph->Fit(g,"M","",0.,360.); // graph.Fit(g,"M","SAMES",0.,10.);
   g->Draw("same");

   // Draw the Legend
   auto leg = new TLegend(0.6,0.71,0.8,0.9,"QUED");
   leg->SetFillColor(0);
   leg->AddEntry(graph,"Measured Points");
   leg->AddEntry(g,"Fit");
   leg->Draw(); // leg.DrawClone("Same");
   gStyle->SetOptFit(1111); // Not working...
   auto text = new TLatex(121.4399,21.4941,"f(x)=0.5 #times p_{0}Cos^2(p_{1}(x-p_{2}))");
   text->SetTextSize(0.03114861);
   text->Draw();

   gPad->Update();
   TPaveStats *ps = (TPaveStats*)graph->GetListOfFunctions()->FindObject("stats");
   ps->SetX1NDC(0.3);
   ps->SetY1NDC(0.7);
   ps->SetX2NDC(0.5);
   ps->SetY2NDC(0.9);
   gPad->Modified();
   gPad->Update();

   TH1F *DiffHist = new TH1F(*(hh));
   DiffHist->SetFillColor(2);
   DiffHist->SetLineColor(0);

   for ( int i = 1; i <= DiffHist->GetNbinsX(); i++ ) {
      DiffHist->SetBinContent(i,(hh->GetBinContent(i) -
      g->Eval(hh->GetBinCenter(i)))/(hh->GetBinError(i)));
   }


   c1->cd(0);
   TPad* pad2 = new TPad("pad2","pad2",0,0.01,1,0.3);
   pad2->SetLeftMargin(0.13);
   pad2->SetRightMargin(0.04);
   pad2->SetTopMargin(0);
   pad2->SetBottomMargin(0.3);

   pad2->Draw();
   pad2->cd();

   TH1F *frame = pad2->DrawFrame(-40.,-2.8,400.,2.8);
   TAxis* ay = frame->GetYaxis();
   TAxis* ax = frame->GetXaxis();
   ay->SetTitle("(Data-Fit)/Error");
   ay->CenterTitle(true);
   ay->SetLabelFont(42);
   ay->SetLabelSize(0.10);
   ay->SetTitleSize(0.3);
   ay->SetTitleOffset(0.33);
   ay->SetNdivisions(505);
   ay->SetLabelSize(0.12);
   ay->SetTitleSize(0.1);

   ax->SetTitle("Angle #beta [degrees]");
   ax->SetLabelFont(42);
   ay->SetLabelFont(42);
   ax->SetLabelSize(0.12);
   ax->SetTitleSize(0.11);

   gStyle->SetOptStat(0);
   DiffHist->Draw("SAME");
}

1 Like

Thank you couet!

Why is it when I change the number of bins, it does not work? In the following line:

   auto hh=new TH1F("Statistics" , "" ,36,0.,360.);

If I change from 36 to 37, it does not work.

@couet
Also, do you know how I can change the precision of the displayed parameters? Like make p0
286 +/- 6 for instance?

Because, in that case, some bin (#37) is -Inf in the loop at the end of your macro.

1 Like

I guess @moneta might know. But seems to me that’s a computed error and you cannot impose it.

@couet : I think @Edward_Finkelstein refers on how to change the precision printing in the static box of the fitted parameters. Do you know if this is possible and how to do it ?
Otherwise one can always change by hand the parameter values and error in the TF1 object by calling TF1::SetParameter and TF1::SetParError

Lorenzo

1 Like

May be https://root.cern/doc/master/classTStyle.html#a90a7b4ccacbcb9e136772a7be297cec4