One histogram, several fit function, optfit info

_ROOT Version: 6.26.04

Hello,

Here is the function:

int root_histogramfitstore()
{
	TH1I *h, *h_copy;
	TF1 *f[2];
	TCanvas *canva, *c1, *c2;
	TPaveStats *stats;
	double gaus_pars[] = {1000, 0, 1};
	double expo_pars[] = {0, 1};

	h = new TH1I("hist", "hist", 100, -10, 10);
	h->FillRandom("gaus", 1000);
	h->FillRandom("expo", 1000);

	canva = new TCanvas("canva", "canva");
	h_copy = (TH1I *)h->DrawCopy("hist");
	h_copy->SetName("hist_maincopy");
	canva->Update();
	stats = (TPaveStats *)h_copy->FindObject("stats");
	stats->SetOptStat(1000011);
	gPad->Modified();
	gPad->Update();
	{
		TObject *obj;
		TIter next(canva->GetListOfPrimitives());
		printf("list of primitives\n");
		while ((obj = next())) {
			printf("primitive name = %s\n", obj->GetName());
		}
	}

	f[0] = new TF1("gaus", "gaus", -4, 4);
	f[0]->SetParameters(gaus_pars);
	h->Fit(f[0], "ER0", "");
	{
		TObject *obj;
		TIter next(h->GetListOfFunctions());
		printf("list of functions 1\n");
		while ((obj = next())) {
			printf("function name = %s\n", obj->GetName());
		}
	}
	f[1] = new TF1("expo", "expo", 4, 10);
	f[1]->SetParameters(expo_pars);
	h->Fit(f[1], "ER0+", "");
	{
		TObject *obj;
		TIter next(h->GetListOfFunctions());
		printf("list of functions 2\n");
		while ((obj = next())) {
			printf("function name = %s\n", obj->GetName());
		}
	}
	/* SECOND PART */
	c1 = new TCanvas("c1", "c1");
	h_copy = (TH1I *)h->DrawCopy("hist");
	h_copy->SetName("hist_copy_1");
	{
		TObject *obj;
		TIter next(h_copy->GetListOfFunctions());
		printf("list of functions 3\n");
		while ((obj = next())) {
			printf("function name = %s\n", obj->GetName());
		}
	}
	f[0]->DrawCopy("same");
	h_copy->GetXaxis()->SetRangeUser(-5, 5);
	c1->Update();
	stats = (TPaveStats *)h_copy->FindObject("stats");
	stats->SetOptStat(1000000001);
	stats->SetOptFit(1112);
	stats->SetX1NDC(0.7);
	stats->SetY1NDC(0.6);
	gPad->Modified();
	gPad->Update();
	
	c2 = new TCanvas("c2", "c2");
	h_copy = (TH1I *)h->DrawCopy("hist");
	h_copy->SetName("hist_copy_2");
	delete h_copy->GetListOfFunctions()->FindObject("gaus");
	{
		TObject *obj;
		TIter next(h_copy->GetListOfFunctions());
		printf("list of functions 4\n");
		while ((obj = next())) {
			printf("function name = %s\n", obj->GetName());
		}
	}
	f[1]->DrawCopy("same");
	h_copy->GetXaxis()->SetRangeUser(3, 10);
	c2->Update();
	stats = (TPaveStats *)h_copy->FindObject("stats");
	stats->SetOptStat(1000000001);
	stats->SetOptFit(1112);
	stats->SetX1NDC(0.7);
	stats->SetY1NDC(0.6);
	gPad->Modified();
	gPad->Update();
	return (0);
}

Question about second part:
As you can see, i create a copy and rename it to hist_copy_1. I will draw this copy in first canva c1. This copy has two functions from previous fit. There are fit parameters of first gaus function in stats box. Second copy hist_copy_2 which i draw on c2, also has two functions. I don’t understand how i can manage which fit parameters will be drawn in stats box, because now both stats box have the same info about gaus parameters.

Best regards,
Nazar

This code:

   c2 = new TCanvas("c2", "c2");
   h_copy = (TH1I *)h->DrawCopy("hist");
   h_copy->SetName("hist_copy_2");
   delete h_copy->GetListOfFunctions()->FindObject("gaus");

produces a crash on my machine. Is it the same for you ?

yes. delete produce a crash.

Ok, may be fix that first.

I found a solution.
I need to remove all functions from copied histogram, except the one that should be in stats box. Deleting function from the list is very VERY BAD decision. Deleting destroy internal structure of the list. So, first you have remove the function using GetListOfFunctions()->Remove() and then delete it. If you want to delete several functions you can use the cycle. For example, in my case I have this one:

	TObjLink *lnk = h_copy->GetListOfFunctions()->FirstLink();
	while (lnk) {
		if (0 != strcmp(lnk->GetObject()->GetName(), aFunc->GetName()))
			delete h_copy->GetListOfFunctions()->Remove(lnk->GetObject());
		lnk = lnk->Next();
	}

It will remove and delete all functions except aFunc.

Thanks and best,
Nazar

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