FastJet 3.4.1
BackgroundEstimatorBase.cc
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2023, 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 "fastjet/tools/BackgroundEstimatorBase.hh"
33
34using namespace std;
35
36FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
37
38LimitedWarning BackgroundEstimatorBase::_warnings_empty_area;
39
40#ifdef FASTJET_HAVE_THREAD_SAFETY
41BackgroundEstimatorBase::BackgroundEstimatorBase(const BackgroundEstimatorBase &other_bge){
42 _rescaling_class = other_bge._rescaling_class;
43 _cached_estimate = other_bge._cached_estimate;
44 _cache_available = other_bge._cache_available;
45 _writing_to_cache.store(other_bge._writing_to_cache.load());;
46}
47#endif
48
49//----------------------------------------------------------------------
50// given a quantity in a vector (e.g. pt_over_area) and knowledge
51// about the number of empty jets, calculate the median and
52// stand_dev_if_gaussian (roughly from the 16th percentile)
53//
54// If do_fj2_calculation is set to true then this performs FastJet
55// 2.X estimation of the standard deviation, which has a spurious
56// offset in the limit of a small number of jets.
57void BackgroundEstimatorBase::_median_and_stddev(const vector<double> & quantity_vector,
58 double n_empty_jets,
59 double & median,
60 double & stand_dev_if_gaussian,
61 bool do_fj2_calculation) const {
62
63 // this check is redundant (the code below behaves sensibly even
64 // with a zero size), but serves as a reminder of what happens if
65 // the quantity vector is zero-sized
66 if (quantity_vector.size() == 0) {
67 median = 0;
68 stand_dev_if_gaussian = 0;
69 return;
70 }
71
72 vector<double> sorted_quantity_vector = quantity_vector;
73 sort(sorted_quantity_vector.begin(), sorted_quantity_vector.end());
74
75 // empty area can sometimes be negative; with small ranges this can
76 // become pathological, so warn the user
77 int n_jets_used = sorted_quantity_vector.size();
78 if (n_empty_jets < -n_jets_used/4.0) {
79 _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.");
80 }
81
82 // now get the median & error, accounting for empty jets;
83 // define the fractions of distribution at median, median-1sigma
84 double posn[2] = {0.5, (1.0-0.6827)/2.0};
85 double res[2];
86 for (int i = 0; i < 2; i++) {
87 res[i] = _percentile(sorted_quantity_vector, posn[i], n_empty_jets,
88 do_fj2_calculation);
89 }
90
91 median = res[0];
92 stand_dev_if_gaussian = res[0] - res[1];
93}
94
95
96//----------------------------------------------------------------------
97// computes a percentile of a given _sorted_ vector of quantities
98// - sorted_quantities the (sorted) vector contains the data sample
99// - percentile the percentile (defined between 0 and 1) to compute
100// - nempty an additional number of 0's
101// (considered at the beginning of
102// the quantity vector)
103// - do_fj2_calculation carry out the calculation as it
104// was done in fj2 (suffers from "edge effects")
105double BackgroundEstimatorBase::_percentile(const vector<double> & sorted_quantities,
106 const double percentile,
107 const double nempty,
108 const bool do_fj2_calculation
109 ) const {
110 assert(percentile >= 0.0 && percentile <= 1.0);
111
112 int quantities_size = sorted_quantities.size();
113 if (quantities_size == 0) return 0;
114
115 double total_njets = quantities_size + nempty;
116 double percentile_pos;
117 if (do_fj2_calculation) {
118 percentile_pos = (total_njets-1)*percentile - nempty;
119 } else {
120 percentile_pos = (total_njets)*percentile - nempty - 0.5;
121 }
122
123 double result;
124 if (percentile_pos >= 0 && quantities_size > 1) {
125 int int_percentile_pos = int(percentile_pos);
126
127 // avoid potential overflow issues
128 if (int_percentile_pos+1 > quantities_size-1){
129 int_percentile_pos = quantities_size-2;
130 percentile_pos = quantities_size-1;
131 }
132
133 result =
134 sorted_quantities[int_percentile_pos] * (int_percentile_pos+1-percentile_pos)
135 + sorted_quantities[int_percentile_pos+1] * (percentile_pos - int_percentile_pos);
136
137
138 } else if (percentile_pos > -0.5 && quantities_size >= 1
139 && !do_fj2_calculation) {
140 // in the LHS of this "bin", just keep a constant value (we could have
141 // interpolated to zero, but this might misbehave in cases where all jets
142 // are active, because it would go to zero too fast)
143 result = sorted_quantities[0];
144 } else {
145 result = 0.0;
146 }
147 return result;
148
149
150}
151
152void BackgroundEstimatorBase::_lock_if_needed() const{
153#ifdef FASTJET_HAVE_THREAD_SAFETY
154 bool expected;
155 // the following waits until the cache_writing status is "false" and sets it to "true"
156 do {
157 expected = false;
158 } while (!_writing_to_cache.compare_exchange_strong(expected, true,
159 memory_order_seq_cst,
160 memory_order_relaxed));
161#endif // FASTJET_HAVE_THREAD_SAFETY
162}
163
164void BackgroundEstimatorBase::_unlock_if_needed() const{
165#ifdef FASTJET_HAVE_THREAD_SAFETY
166 // release the "write-in-progress" lock
167 _writing_to_cache = false;
168#endif
169}
170
171
172
173FASTJET_END_NAMESPACE // defined in fastjet/internal/base.hh