Hi ROOTers,
So I am trying to fit multiple histos and draw them on the same pad. The fitting itself, together with the colors of the histos etc are done in an external function. I noticed if I do this in an external function, only the last histogram draws itself. Why is this?
An example code is attached: the function fit1d actually sets the cosmetics for the histos and also fits them with some TF1. The main function, FitAHisto, fills the 2 histos and calls the fit1d function. So in the TCanvas cc’s first cell, I try to draw both of these fitted histograms, but as you can see only the last histo draws. Strangely though, if I just draw the histos by themselves in seperate lines, they just draw fine, as you can see in cell 2. And ofcourse if I do this all within the same main function everything is drawn as expected also (last cell, cell 5).
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <TH1.h>
#include <TF1.h>
#include <TCanvas.h>
using namespace std;
void FitAHisto();
TH1F *fit1d(TH1F *hist, int i, char func[256], double lolim, double hilim)
{
hist->SetLineColor(i+1);
hist->SetMarkerColor(i+1);
char fitname[256];
strcpy (fitname,hist->GetName());
strcat (fitname,"Fitted");
TF1 *fitfunc = new TF1 (fitname,func, lolim, hilim);
fitfunc->SetLineColor(i+1);
hist->Fit(fitname,"EMR");
return hist;
}
void FitAHisto(){
TH1F *hh=new TH1F("hh","hh",100,-1.,1.);
TH1F *hh2=new TH1F("hh2","hh2",100,-1.,1.);
hh->FillRandom("gaus",100000);
hh2->FillRandom("gaus",90000);
TCanvas *cc =new TCanvas("cc","cc",500,500);
cc->Divide(2,3);
cc->cd(1);
fit1d(hh, 1, "gaus", -1.,1.)->Draw("same");
fit1d(hh2, 6, "gaus", -1.,1.)->Draw("same");
cc->cd(2);
hh->Draw();
hh2->Draw("same");
cc->cd(3);
fit1d(hh, 1, "gaus", -1.,1.)->Draw();
cc->cd(4);
fit1d(hh2, 6, "gaus", -1.,1.)->Draw("same");
TH1F *hhh=new TH1F("hhh","hhh",100,-1.,1.);
TH1F *hhh2=new TH1F("hhh2","hhh2",100,-1.,1.);
hhh->SetLineColor(2);
hhh2->SetLineColor(7);
hhh->FillRandom("gaus",100000);
hhh2->FillRandom("gaus",90000);
TF1 *fff = new TF1("fff","gaus",-1.,1.);
TF1 *fff2 = new TF1("fff2","gaus",-1.,1.);
fff->SetLineColor(2);
fff2->SetLineColor(7);
hhh->Fit("fff","emr");
hhh2->Fit("fff2","emr");
cc->cd(5);
hhh->Draw();
hhh2->Draw("same");
}
And here’s the canvas produced: cctemp.ps
Thanks in advance!
- Sujeewa
cctemp.ps (46.8 KB)