// do a lognormal fit to an histogram void lognormal_fit() { // fit to a log normal TFile *ff = new TFile("mass_T4.root"); ff->ls(); // fit to a log normal TF1 * f1 = new TF1("f1","[0]*ROOT::Math::lognormal_pdf(x,[1],[2] ) " ); //TF1 * f1 = new TF1("f1","[0]*ROOT::Math::normal_pdf(x,[1],[2] ) " ); // set initial parameters double p[3]; p[0] = h1->GetEntries()*h1->GetXaxis()->GetBinWidth(1); // area of the histogram // find median of histogram double prob[] = {0.5}; double q[1]; h1->GetQuantiles(1,q,prob); double median = q[0]; // find mode of histogram double mode = h1->GetBinCenter( h1->GetMaximumBin()); std::cout << "histogram mode is " << mode << " median is " << median << std::endl; if (median < 0) { Error("lognormal","not valid histogram median"); return; } // m is log(median) p[1] = std::log(median); // s2 is log(median) - log(mode) p[2] = std::sqrt( std::log(median/mode) ); f1->SetParameters(p); f1->SetParName(0,"A"); f1->SetParName(1,"m"); f1->SetParName(2,"s"); f1->Print(); h1->Draw(); std::cout << f1->GetNpar() << std::endl; h1->Fit(f1,"V"); }