Skip to content

Commit acb0ab0

Browse files
committed
TkAl PV validation tools, use least squares fit in case the input histogram is weighted
1 parent 86e2e0d commit acb0ab0

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Alignment/OfflineValidation/macros/FitPVResiduals.C

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,11 +3083,26 @@ std::pair<params::measurement, params::measurement> fitResiduals(TH1 *hist, bool
30833083
sigma = func.GetParameter(2);
30843084

30853085
if (!singleTime) {
3086-
// second fit: three sigma of first fit around mean of first fit
3086+
// Check if histogram is weighted
3087+
double sumWeights = hist->GetSumOfWeights();
3088+
double effectiveEntries = hist->GetEffectiveEntries();
3089+
bool isWeighted = !(sumWeights == effectiveEntries);
3090+
3091+
if (isWeighted && isDebugMode) {
3092+
std::cout << "A weighted input histogram has been provided, will use least squares fit instead of likelihood!"
3093+
<< " Sum of weights: " << sumWeights << " effective entries: " << hist->GetEffectiveEntries()
3094+
<< std::endl;
3095+
}
3096+
// If histogram is weighted, exclude the "L" option (Likelihood fit)
3097+
std::string fitOptions = isWeighted ? "Q0R" : "Q0LR";
3098+
3099+
// second fit: two sigma of first fit around mean of first fit
30873100
func.SetRange(std::max(mean - 2 * sigma, minHist), std::min(mean + 2 * sigma, maxHist));
3101+
3102+
// Perform fit with the appropriate options
30883103
// I: integral gives more correct results if binning is too wide
30893104
// L: Likelihood can treat empty bins correctly (if hist not weighted...)
3090-
if (0 == hist->Fit(&func, "Q0LR")) {
3105+
if (0 == hist->Fit(&func, fitOptions.c_str())) {
30913106
if (hist->GetFunction(func.GetName())) { // Take care that it is later on drawn:
30923107
hist->GetFunction(func.GetName())->ResetBit(TF1::kNotDraw);
30933108
}

Alignment/OfflineValidation/macros/FitPVResolution.C

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ int VTXBINS = 60;
4040
to be used to plot
4141
*/
4242

43+
bool debugMode = false;
44+
4345
class PVResolutionVariables {
4446
public:
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

Comments
 (0)