Running on lxplus7
Dear co-rooters,
I am trying to perform multiple fits on a histogram, in different ranges using the same fitting function.
I would like to store the parameters in each fit as well as to draw the functions.
My code to do the above is below
#include "TCanvas.h"
#include "TStyle.h"
#include "TFile.h"
#include "TH1.h"
void fit(){
// (1) Store the movie in arrays
// The file can be found in https://cernbox.cern.ch/index.php/s/3XnsnbWYg2obDtD
TFile *fin = new TFile("fit.root", "OPEN");
TH1D *h = (TH1D*) fin->Get("histo_movie");
gStyle->SetOptStat(0);
TCanvas *canvas = new TCanvas("canvas", "HPGe", 500, 500);
canvas->Divide(1, 2);
// Pad 1
canvas->cd(1);
h->Draw("histo ][");
h->Fit("pol1", "WCFQ", "HIST ][ SAME", 5.6005e6, 5.6021e6);
h->GetFunction("pol1")->Draw("SAME");
cout << "The [0] parameter is : " << h->GetFunction("pol1")->GetParameter(0) << endl;
h->Fit("pol1", "WCFQ", "HIST ][ SAME", 5.60235e6, 5.60393e6);
h->GetFunction("pol1")->Draw("SAME");
cout << "The [0] parameter is : " << h->GetFunction("pol1")->GetParameter(0) << endl;
//Pad 2
canvas->cd(2);
h->Draw("histo ][");
}
With this code I can get the parameters, however only the second function is drawn.
I tried to use the +
option but then only the first function is drawn and only the parameters of the first fit are kept.
One idea could be to clone the histogram and do the fit on the cloned, but this procedure eventually will involve hundreds of fits on big histograms, so I don’t think that this is an option.
Any idea on how to do it?
Thanks in advance!