FastJet 3.5.0
Loading...
Searching...
No Matches
BackgroundEstimatorBase.cc
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2025, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
5//
6//----------------------------------------------------------------------
7// This file is part of FastJet.
8//
9// FastJet is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// The algorithms that underlie FastJet have required considerable
15// development. They are described in the original FastJet paper,
16// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
17// FastJet as part of work towards a scientific publication, please
18// quote the version you use and include a citation to the manual and
19// optionally also to hep-ph/0512210.
20//
21// FastJet is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
28//----------------------------------------------------------------------
29//FJENDHEADER
30
31
32#include <algorithm>
33#include "fastjet/tools/BackgroundEstimatorBase.hh"
34
35using namespace std;
36
37FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
38
39LimitedWarning BackgroundEstimatorBase::_warnings_empty_area;
40
41#ifdef FASTJET_HAVE_THREAD_SAFETY
42BackgroundEstimatorBase::BackgroundEstimatorBase(const BackgroundEstimatorBase &other_bge){
43 _rescaling_class = other_bge._rescaling_class;
44 _cached_estimate = other_bge._cached_estimate;
45 _cache_available = other_bge._cache_available;
46 _writing_to_cache.store(other_bge._writing_to_cache.load());;
47}
48#endif
49
50//----------------------------------------------------------------------
51// given a quantity in a vector (e.g. pt_over_area) and knowledge
52// about the number of empty jets, calculate the median and
53// stand_dev_if_gaussian (roughly from the 16th percentile)
54//
55// If do_fj2_calculation is set to true then this performs FastJet
56// 2.X estimation of the standard deviation, which has a spurious
57// offset in the limit of a small number of jets.
58void BackgroundEstimatorBase::_median_and_stddev(const vector<double> & quantity_vector,
59 double n_empty_jets,
60 double & median,
61 double & stand_dev_if_gaussian,
62 bool do_fj2_calculation) const {
63
64 // this check is redundant (the code below behaves sensibly even
65 // with a zero size), but serves as a reminder of what happens if
66 // the quantity vector is zero-sized
67 if (quantity_vector.size() == 0) {
68 median = 0;
69 stand_dev_if_gaussian = 0;
70 return;
71 }
72
73 vector<double> sorted_quantity_vector = quantity_vector;
74 sort(sorted_quantity_vector.begin(), sorted_quantity_vector.end());
75
76 // empty area can sometimes be negative; with small ranges this can
77 // become pathological, so warn the user
78 int n_jets_used = sorted_quantity_vector.size();
79 if (n_empty_jets < -n_jets_used/4.0) {
80 _warnings_empty_area.warn("BackgroundEstimatorBase::_median_and_stddev(...): the estimated empty area is suspiciously large and negative and may lead to an over-estimation of rho. This may be due to (i) a rare statistical fluctuation or (ii) too small a range used to estimate the background properties.");
81 }
82
83 // now get the median & error, accounting for empty jets;
84 // define the fractions of distribution at median, median-1sigma
85 double posn[2] = {0.5, (1.0-0.6827)/2.0};
86 double res[2];
87 for (int i = 0; i < 2; i++) {
88 res[i] = _percentile(sorted_quantity_vector, posn[i], n_empty_jets,
89 do_fj2_calculation);
90 }
91
92 median = res[0];
93 stand_dev_if_gaussian = res[0] - res[1];
94}
95
96
97//----------------------------------------------------------------------
98// computes a percentile of a given _sorted_ vector of quantities
99// - sorted_quantities the (sorted) vector contains the data sample
100// - percentile the percentile (defined between 0 and 1) to compute
101// - nempty an additional number of 0's
102// (considered at the beginning of
103// the quantity vector)
104// - do_fj2_calculation carry out the calculation as it
105// was done in fj2 (suffers from "edge effects")
106double BackgroundEstimatorBase::_percentile(const vector<double> & sorted_quantities,
107 const double percentile,
108 const double nempty,
109 const bool do_fj2_calculation
110 ) const {
111 assert(percentile >= 0.0 && percentile <= 1.0);
112
113 int quantities_size = sorted_quantities.size();
114 if (quantities_size == 0) return 0;
115
116 double total_njets = quantities_size + nempty;
117 double percentile_pos;
118 if (do_fj2_calculation) {
119 percentile_pos = (total_njets-1)*percentile - nempty;
120 } else {
121 percentile_pos = (total_njets)*percentile - nempty - 0.5;
122 }
123
124 double result;
125 if (percentile_pos >= 0 && quantities_size > 1) {
126 int int_percentile_pos = int(percentile_pos);
127
128 // avoid potential overflow issues
129 if (int_percentile_pos+1 > quantities_size-1){
130 int_percentile_pos = quantities_size-2;
131 percentile_pos = quantities_size-1;
132 }
133
134 result =
135 sorted_quantities[int_percentile_pos] * (int_percentile_pos+1-percentile_pos)
136 + sorted_quantities[int_percentile_pos+1] * (percentile_pos - int_percentile_pos);
137
138
139 } else if (percentile_pos > -0.5 && quantities_size >= 1
140 && !do_fj2_calculation) {
141 // in the LHS of this "bin", just keep a constant value (we could have
142 // interpolated to zero, but this might misbehave in cases where all jets
143 // are active, because it would go to zero too fast)
144 result = sorted_quantities[0];
145 } else {
146 result = 0.0;
147 }
148 return result;
149
150
151}
152
153void BackgroundEstimatorBase::_lock_if_needed() const{
154#ifdef FASTJET_HAVE_THREAD_SAFETY
155 bool expected;
156 // the following waits until the cache_writing status is "false" and sets it to "true"
157 do {
158 expected = false;
159 } while (!_writing_to_cache.compare_exchange_strong(expected, true,
160 memory_order_seq_cst,
161 memory_order_relaxed));
162#endif // FASTJET_HAVE_THREAD_SAFETY
163}
164
165void BackgroundEstimatorBase::_unlock_if_needed() const{
166#ifdef FASTJET_HAVE_THREAD_SAFETY
167 // release the "write-in-progress" lock
168 _writing_to_cache = false;
169#endif
170}
171
172
173
174FASTJET_END_NAMESPACE // defined in fastjet/internal/base.hh