Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:45

0001 
0002 /*
0003  *  Fits core distribution to single gaussian; iterates once.  
0004  *
0005  *  \author A. Vilela Pereira
0006  */
0007 
0008 #include "CalibMuon/DTCalibration/interface/DTResidualFitter.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 #include "TH1F.h"
0012 #include "TF1.h"
0013 #include "TString.h"
0014 
0015 DTResidualFitter::DTResidualFitter(bool debug) : debug_(debug) {}
0016 
0017 DTResidualFitter::~DTResidualFitter() {}
0018 
0019 DTResidualFitResult DTResidualFitter::fitResiduals(TH1F& histo, int nSigmas) {
0020   TString option("R");
0021   if (!debug_)
0022     option += "Q";
0023 
0024   float under = histo.GetBinContent(0) / histo.GetEntries();
0025   float over = histo.GetBinContent(histo.GetNbinsX() + 1) / histo.GetEntries();
0026   float minFit = histo.GetMean() - histo.GetRMS();
0027   float maxFit = histo.GetMean() + histo.GetRMS();
0028 
0029   if ((under > 0.1) || (over > 0.1))
0030     edm::LogError("DTResidualFitter") << "WARNING in histogram: " << histo.GetName() << "\n"
0031                                       << "             entries: " << histo.GetEntries() << "\n"
0032                                       << "           underflow: " << under * 100. << "% \n"
0033                                       << "            overflow: " << over * 100. << "%";
0034 
0035   TString funcName = TString(histo.GetName()) + "_gaus";
0036   TF1* fitFunc = new TF1(funcName, "gaus", minFit, maxFit);
0037 
0038   histo.Fit(fitFunc, option);
0039 
0040   minFit = fitFunc->GetParameter(1) - nSigmas * fitFunc->GetParameter(2);
0041   maxFit = fitFunc->GetParameter(1) + nSigmas * fitFunc->GetParameter(2);
0042   fitFunc->SetRange(minFit, maxFit);
0043   histo.Fit(fitFunc, option);
0044 
0045   return DTResidualFitResult(
0046       fitFunc->GetParameter(1), fitFunc->GetParError(1), fitFunc->GetParameter(2), fitFunc->GetParError(2));
0047 }