Fitting a Histogram

Hi all,

I apologize for the extremely basic question. I am new to writing code in general and even more inexperienced with ROOT.

With that said, my code fits a landau curve to data from a file titled 1.root. I would like ROOT to fit the curve on a user inputted range (xMin and xMax).
I also would like the program to save the histogram as a jpg or png file.

I have tried h1->Fit(“landau”); and c1->Print(“Fit.png”, “png”); with no luck

My code is as follows:

analysis1(int noBins=0, int xMin=0, int xMax=0) {
           
    TCanvas *c1 = new TCanvas("c1", "canvas", 600, 50, 700, 700 );
	c1 -> SetFillColor(0);
	TH1I *h1 = new TH1I( "h1", "Cosmic Spectrum", noBins , 0, 3000 );

	UInt_t pulse_height;
	float thresh_voltage;
	int num_events, i;
	
	
	TFile *file = new TFile( "1.root" );       
	TTree *tree = (TTree*)file->Get("CalorTree");  
	
	tree->SetBranchAddress("pulse_height", &pulse_height); //Get the correct branch
	
	num_events = (int)tree->GetEntries();
	
	for( i = 0; i < num_events; i++ ) {
		tree->GetEntry(i);
		if (pulse_height > 0){
		h1->Fill(pulse_height);  //fill the histograms with data from the pulse_height branch
		}
	}
	
	h1->Draw();
	h1->Fit("landau");         //fit a landau curve to our data set
	h1->GetXaxis()->SetTitle("ADC Counts");
	h1->GetXaxis()->CenterTitle();
	h1->GetYaxis()->SetTitle("No. Of Events");             
	h1->GetYaxis()->CenterTitle();
	gStyle->SetOptFit(111);   //set parameters box

	c1->Modified();
	c1->Update();
		
}

Thanks for your time

Hi,

if you want to fit only between xmin and xmax do:

h1->Fit("landau","",xmin,xmax);

Best Regards,
Lorenzo

Thank you!!! I see my error now. Also, would you mind telling me why

c1->Print(“Fit.png”, “png”);

does not save my histogram.

Hi,

this command should work, if not please attach a script reproducing the problem.
Lorenzo