FastJet  3.4.0
BackgroundEstimatorBase.cc
1 //FJSTARTHEADER
2 // $Id$
3 //
4 // Copyright (c) 2005-2021, 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 
34 using namespace std;
35 
36 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
37 
38 LimitedWarning BackgroundEstimatorBase::_warnings_empty_area;
39 
40 #ifdef FASTJET_HAVE_THREAD_SAFETY
41 BackgroundEstimatorBase::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.
57 void 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  // now get the median & error, accounting for empty jets;
82  // define the fractions of distribution at median, median-1sigma
83  double posn[2] = {0.5, (1.0-0.6827)/2.0};
84  double res[2];
85  for (int i = 0; i < 2; i++) {
86  res[i] = _percentile(sorted_quantity_vector, posn[i], n_empty_jets,
87  do_fj2_calculation);
88  }
89 
90  median = res[0];
91  stand_dev_if_gaussian = res[0] - res[1];
92 }
93 
94 
95 //----------------------------------------------------------------------
96 // computes a percentile of a given _sorted_ vector of quantities
97 // - sorted_quantities the (sorted) vector contains the data sample
98 // - percentile the percentile (defined between 0 and 1) to compute
99 // - nempty an additional number of 0's
100 // (considered at the beginning of
101 // the quantity vector)
102 // - do_fj2_calculation carry out the calculation as it
103 // was done in fj2 (suffers from "edge effects")
104 double BackgroundEstimatorBase::_percentile(const vector<double> & sorted_quantities,
105  const double percentile,
106  const double nempty,
107  const bool do_fj2_calculation
108  ) const {
109  assert(percentile >= 0.0 && percentile <= 1.0);
110 
111  int quantities_size = sorted_quantities.size();
112  if (quantities_size == 0) return 0;
113 
114  double total_njets = quantities_size + nempty;
115  double percentile_pos;
116  if (do_fj2_calculation) {
117  percentile_pos = (total_njets-1)*percentile - nempty;
118  } else {
119  percentile_pos = (total_njets)*percentile - nempty - 0.5;
120  }
121 
122  double result;
123  if (percentile_pos >= 0 && quantities_size > 1) {
124  int int_percentile_pos = int(percentile_pos);
125 
126  // avoid potential overflow issues
127  if (int_percentile_pos+1 > quantities_size-1){
128  int_percentile_pos = quantities_size-2;
129  percentile_pos = quantities_size-1;
130  }
131 
132  result =
133  sorted_quantities[int_percentile_pos] * (int_percentile_pos+1-percentile_pos)
134  + sorted_quantities[int_percentile_pos+1] * (percentile_pos - int_percentile_pos);
135 
136 
137  } else if (percentile_pos > -0.5 && quantities_size >= 1
138  && !do_fj2_calculation) {
139  // in the LHS of this "bin", just keep a constant value (we could have
140  // interpolated to zero, but this might misbehave in cases where all jets
141  // are active, because it would go to zero too fast)
142  result = sorted_quantities[0];
143  } else {
144  result = 0.0;
145  }
146  return result;
147 
148 
149 }
150 
151 void BackgroundEstimatorBase::_lock_if_needed() const{
152 #ifdef FASTJET_HAVE_THREAD_SAFETY
153  bool expected;
154  // the following waits until the cache_writing status is "false" and sets it to "true"
155  do {
156  expected = false;
157  } while (!_writing_to_cache.compare_exchange_strong(expected, true,
158  memory_order_seq_cst,
159  memory_order_relaxed));
160 #endif // FASTJET_HAVE_THREAD_SAFETY
161 }
162 
163 void BackgroundEstimatorBase::_unlock_if_needed() const{
164 #ifdef FASTJET_HAVE_THREAD_SAFETY
165  // release the "write-in-progress" lock
166  _writing_to_cache = false;
167 #endif
168 }
169 
170 
171 
172 FASTJET_END_NAMESPACE // defined in fastjet/internal/base.hh