@@ -40,6 +40,8 @@ int VTXBINS = 60;
4040 to be used to plot
4141*/
4242
43+ bool debugMode = false;
44+
4345class PVResolutionVariables {
4446public :
4547 PVResolutionVariables (TString fileName , TString baseDir , TString legName = "" , int color = 1 , int style = 1 );
@@ -1061,11 +1063,26 @@ statmode::fitParams fitResolutions(TH1* hist, bool singleTime)
10611063 sigma = func .GetParameter (2 );
10621064
10631065 if (!singleTime ) {
1066+ // Check if histogram is weighted
1067+ double sumWeights = hist -> GetSumOfWeights ();
1068+ double effectiveEntries = hist -> GetEffectiveEntries ();
1069+ bool isWeighted = !(sumWeights == effectiveEntries );
1070+
1071+ if (isWeighted && debugMode ) {
1072+ std ::cout << "A weighted input histogram has been provided, will use least squares fit instead of likelihood!"
1073+ << " Sum of weights: " << sumWeights << " effective entries: " << hist -> GetEffectiveEntries ()
1074+ << std ::endl ;
1075+ }
1076+ // If histogram is weighted, exclude the "L" option (Likelihood fit)
1077+ std ::string fitOptions = isWeighted ? "Q0R" : "Q0LR" ;
1078+
10641079 // second fit: three sigma of first fit around mean of first fit
10651080 func .SetRange (std ::max (mean - 3 * sigma , minHist ), std ::min (mean + 3 * sigma , maxHist ));
1081+
1082+ // Perform fit with the appropriate options
10661083 // I: integral gives more correct results if binning is too wide
10671084 // L: Likelihood can treat empty bins correctly (if hist not weighted...)
1068- if (0 == hist -> Fit (& func , "Q0LR" )) {
1085+ if (0 == hist -> Fit (& func , fitOptions . c_str () )) {
10691086 if (hist -> GetFunction (func .GetName ())) { // Take care that it is later on drawn:
10701087 hist -> GetFunction (func .GetName ())-> ResetBit (TF1 ::kNotDraw );
10711088 }
0 commit comments