CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
GSUtilities Class Reference

#include <GSUtilities.h>

Public Member Functions

double cdf (const double &) const
 value of integral(pdf) More...
 
double combinedMean () const
 mean value of combined state More...
 
double dpdf1 (const double &) const
 first derivative of pdf More...
 
double dpdf2 (const double &) const
 second derivative of pdf More...
 
double errorCombinedMean () const
 
float errorHighestWeight () const
 
float errorMode ()
 
float getMax (float)
 
float getMin (float)
 
 GSUtilities (const unsigned nComp, const float *weights, const float *parameters, const float *errors)
 constructor from arrays of weights, parameters and standard deviations More...
 
float maxWeight () const
 
float mode () const
 mode More...
 
double pdf (const double &) const
 value of the pdf More...
 
float quantile (const float) const
 
 ~GSUtilities ()
 

Private Member Functions

double findMode (const double) const
 
double gauss (const double &, const double &, const double &) const
 value of gaussian distribution More...
 
double gaussInt (const double &, const double &, const double &) const
 integrated value of gaussian distribution More...
 

Private Attributes

float * theErrors
 
unsigned theNComp
 
float * theParameters
 
float * theWeights
 

Detailed Description

Some utilities for analysing 1D Gaussian mixtures. Copied from ORCA's EgammaGSUtilities.

Definition at line 8 of file GSUtilities.h.

Constructor & Destructor Documentation

◆ GSUtilities()

GSUtilities::GSUtilities ( const unsigned  nComp,
const float *  weights,
const float *  parameters,
const float *  errors 
)
inline

constructor from arrays of weights, parameters and standard deviations

Definition at line 11 of file GSUtilities.h.

References mps_fire::i, theErrors, theNComp, theParameters, theWeights, and HLT_2022v12_cff::weights.

12  : theNComp(nComp), theWeights(nullptr), theParameters(nullptr), theErrors(nullptr) {
13  if (theNComp) {
14  theWeights = new float[theNComp];
15  theParameters = new float[theNComp];
16  theErrors = new float[theNComp];
17  }
18  const float* wPtr1(weights);
19  const float* pPtr1(parameters);
20  const float* ePtr1(errors);
21  float* wPtr2(theWeights);
22  float* pPtr2(theParameters);
23  float* ePtr2(theErrors);
24  for (unsigned i = 0; i < theNComp; i++) {
25  *(wPtr2++) = weights ? *(wPtr1++) : 1.;
26  *(pPtr2++) = *(pPtr1++);
27  *(ePtr2++) = *(ePtr1++);
28  }
29  }
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76
Definition: errors.py:1

◆ ~GSUtilities()

GSUtilities::~GSUtilities ( )
inline

Definition at line 30 of file GSUtilities.h.

References theErrors, theParameters, and theWeights.

30  {
31  delete[] theWeights;
32  delete[] theParameters;
33  delete[] theErrors;
34  }
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

Member Function Documentation

◆ cdf()

double GSUtilities::cdf ( const double &  x) const

value of integral(pdf)

Definition at line 147 of file GSUtilities.cc.

References gaussInt(), mps_fire::i, mps_fire::result, theErrors, theNComp, theParameters, theWeights, and x.

Referenced by errorMode(), getMax(), getMin(), and quantile().

147  {
148  double result(0.);
149  for (unsigned i = 0; i < theNComp; i++)
151  return result;
152 }
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76
double gaussInt(const double &, const double &, const double &) const
integrated value of gaussian distribution
Definition: GSUtilities.cc:183

◆ combinedMean()

double GSUtilities::combinedMean ( ) const

mean value of combined state

Definition at line 187 of file GSUtilities.cc.

References mps_fire::i, theNComp, theParameters, and theWeights.

187  {
188  double s0(0.);
189  double s1(0.);
190  for (unsigned i = 0; i < theNComp; i++) {
191  s0 += theWeights[i];
192  s1 += theWeights[i] * theParameters[i];
193  }
194  return s1 / s0;
195 }
unsigned theNComp
Definition: GSUtilities.h:74
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

◆ dpdf1()

double GSUtilities::dpdf1 ( const double &  x) const

first derivative of pdf

Definition at line 154 of file GSUtilities.cc.

References PVValHelper::dx, gauss(), mps_fire::i, mps_fire::result, theErrors, theNComp, theParameters, theWeights, and x.

Referenced by findMode().

154  {
155  double result(0.);
156  for (unsigned i = 0; i < theNComp; i++) {
157  double dx = (x - theParameters[i]) / theErrors[i];
159  }
160  return result;
161 }
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:172
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

◆ dpdf2()

double GSUtilities::dpdf2 ( const double &  x) const

second derivative of pdf

Definition at line 163 of file GSUtilities.cc.

References PVValHelper::dx, gauss(), mps_fire::i, mps_fire::result, theErrors, theNComp, theParameters, theWeights, and x.

Referenced by findMode().

163  {
164  double result(0.);
165  for (unsigned i = 0; i < theNComp; i++) {
166  double dx = (x - theParameters[i]) / theErrors[i];
167  result += theWeights[i] / theErrors[i] / theErrors[i] * (dx * dx - 1) * gauss(x, theParameters[i], theErrors[i]);
168  }
169  return result;
170 }
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:172
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

◆ errorCombinedMean()

double GSUtilities::errorCombinedMean ( ) const

Definition at line 197 of file GSUtilities.cc.

References mps_fire::i, mathSSE::sqrt(), theNComp, and theWeights.

197  {
198  double s0(0.);
199  for (unsigned i = 0; i < theNComp; i++) {
200  s0 += theWeights[i];
201  }
202  return 1. / (sqrt(s0));
203 }
unsigned theNComp
Definition: GSUtilities.h:74
T sqrt(T t)
Definition: SSEVec.h:19
float * theWeights
Definition: GSUtilities.h:75

◆ errorHighestWeight()

float GSUtilities::errorHighestWeight ( ) const

Definition at line 71 of file GSUtilities.cc.

References mps_fire::i, theErrors, theNComp, and theWeights.

71  {
72  int iwMax(-1);
73  float wMax(0.);
74  for (unsigned i = 0; i < theNComp; i++) {
75  if (theWeights[i] > wMax) {
76  iwMax = i;
77  wMax = theWeights[i];
78  }
79  }
80  return theErrors[iwMax];
81 }
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75

◆ errorMode()

float GSUtilities::errorMode ( )

Definition at line 219 of file GSUtilities.cc.

References cdf(), PVValHelper::dx, getMax(), getMin(), Exhume::I, SiStripPI::max, SiStripPI::min, mod(), mode(), seedmultiplicitymonitor_newtracking_cfi::nBins, pdf(), testProducerWithPsetDescEmpty_cfi::x1, and testProducerWithPsetDescEmpty_cfi::x2.

219  {
220  float mod = mode();
221  float min = getMin(mod);
222  float max = getMax(mod);
223  int nBins = 1000;
224  float dx = (max - min) / (float)nBins;
225 
226  float x1 = mod, x2 = mod, x1f = mod, x2f = mod;
227  float I = 0;
228  int cnt = 0;
229  while (I < .68) {
230  x1 -= dx;
231  x2 += dx;
232  if (pdf(x1) >= pdf(x2)) {
233  x1f = x1;
234  x2 -= dx;
235  } else {
236  x2f = x2;
237  x1 += dx;
238  }
239  I = cdf(x2f) - cdf(x1f);
240  cnt++;
241  // for crazy pdf's return a crazy value
242  if (cnt > 2500)
243  return 100000.;
244  }
245  return (x2f - x1f) / 2;
246 }
float getMax(float)
Definition: GSUtilities.cc:262
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:147
const std::complex< double > I
Definition: I.h:8
float mode() const
mode
Definition: GSUtilities.cc:83
float getMin(float)
Definition: GSUtilities.cc:248
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:140
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4

◆ findMode()

double GSUtilities::findMode ( const double  xStart) const
private

mean value of combined state double combinedMean() const; mode from starting value

Definition at line 113 of file GSUtilities.cc.

References dpdf1(), dpdf2(), MillePedeFileConverter_cfg::e, pdf(), x, testProducerWithPsetDescEmpty_cfi::y1, and testProducerWithPsetDescEmpty_cfi::y2.

Referenced by mode().

113  {
114  //
115  // try with Newton
116  //
117  double y1(0.);
118  double x(xStart);
119  double y2(pdf(xStart));
120  double yd(dpdf1(xStart));
121  int nLoop(0);
122  if ((y1 + y2) < 10 * DBL_MIN)
123  return xStart;
124  while (nLoop++ < 20 && fabs(y2 - y1) / (y2 + y1) > 1.e-6) {
125  // std::cout << "dy = " << y2-y1 << std::endl;
126  double yd2 = dpdf2(x);
127  if (fabs(yd2) < 10 * DBL_MIN)
128  return xStart;
129  x -= yd / dpdf2(x);
130  yd = dpdf1(x);
131  y1 = y2;
132  y2 = pdf(x);
133  // std::cout << "New x / yd = " << x << " / " << yd << std::endl;
134  }
135  if (nLoop >= 20)
136  return xStart;
137  return x;
138 }
double dpdf1(const double &) const
first derivative of pdf
Definition: GSUtilities.cc:154
double dpdf2(const double &) const
second derivative of pdf
Definition: GSUtilities.cc:163
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:140

◆ gauss()

double GSUtilities::gauss ( const double &  x,
const double &  mean,
const double &  sigma 
) const
private

value of gaussian distribution

Definition at line 172 of file GSUtilities.cc.

References ztail::d, JetChargeProducer_cfi::exp, SiStripPI::mean, Pi, mps_fire::result, mathSSE::sqrt(), and x.

Referenced by dpdf1(), dpdf2(), and pdf().

172  {
173  const double fNorm(1. / sqrt(2 * TMath::Pi()));
174  double result(0.);
175 
176  double d((x - mean) / sigma);
177  if (fabs(d) < 20.)
178  result = exp(-d * d / 2.);
179  result *= fNorm / sigma;
180  return result;
181 }
const double Pi
T sqrt(T t)
Definition: SSEVec.h:19
d
Definition: ztail.py:151

◆ gaussInt()

double GSUtilities::gaussInt ( const double &  x,
const double &  mean,
const double &  sigma 
) const
private

integrated value of gaussian distribution

Definition at line 183 of file GSUtilities.cc.

References SiStripPI::mean, and x.

Referenced by cdf().

183  {
184  return TMath::Freq((x - mean) / sigma);
185 }

◆ getMax()

float GSUtilities::getMax ( float  x)

Definition at line 262 of file GSUtilities.cc.

References cdf(), PVValHelper::dx, and x.

Referenced by errorMode().

262  {
263  int cnt = 0;
264  float dx;
265  if (fabs(x) < 2)
266  dx = .5;
267  else
268  dx = fabs(x) / 10;
269  while (cdf(x) < .9 && cnt < 1000) {
270  x += dx;
271  cnt++;
272  }
273  return x;
274 }
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:147

◆ getMin()

float GSUtilities::getMin ( float  x)

Definition at line 248 of file GSUtilities.cc.

References cdf(), PVValHelper::dx, and x.

Referenced by errorMode().

248  {
249  int cnt = 0;
250  float dx;
251  if (fabs(x) < 2)
252  dx = .5;
253  else
254  dx = fabs(x) / 10;
255  while (cdf(x) > .1 && cnt < 1000) {
256  x -= dx;
257  cnt++;
258  }
259  return x;
260 }
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:147

◆ maxWeight()

float GSUtilities::maxWeight ( ) const

Definition at line 205 of file GSUtilities.cc.

References mps_fire::i, theNComp, and theWeights.

205  {
206  // Look for the highest weight component
207  //
208  //int iwMax(-1);
209  float wMax(0.);
210  for (unsigned i = 0; i < theNComp; i++) {
211  if (theWeights[i] > wMax) {
212  //iwMax = i;
213  wMax = theWeights[i];
214  }
215  }
216  return wMax;
217 }
unsigned theNComp
Definition: GSUtilities.h:74
float * theWeights
Definition: GSUtilities.h:75

◆ mode()

float GSUtilities::mode ( void  ) const

mode

Definition at line 83 of file GSUtilities.cc.

References findMode(), mps_fire::i, pdf(), theNComp, theParameters, and x.

Referenced by errorMode().

83  {
84  //
85  // start values = means of components
86  //
87  typedef std::multimap<double, double, std::greater<double> > StartMap;
88  StartMap xStart;
89  for (unsigned i = 0; i < theNComp; i++) {
90  xStart.insert(std::pair<double, float>(pdf(theParameters[i]), theParameters[i]));
91  }
92  //
93  // now try with each start value
94  //
95  typedef std::multimap<double, double, std::greater<double> > ResultMap;
96  ResultMap xFound;
97  for (StartMap::const_iterator i = xStart.begin(); i != xStart.end(); i++) {
98  double x = findMode((*i).second);
99  xFound.insert(std::pair<double, double>(pdf(x), x));
100  // std::cout << "Started at " << (*i).second
101  // << " , found " << x << std::endl;
102  }
103  //
104  // results
105  //
106  // for ( ResultMap::const_iterator i=xFound.begin();
107  // i!=xFound.end(); i++ ) {
108  // std::cout << "pdf at " << (*i).second << " = " << (*i).first << std::endl;
109  // }
110  return xFound.begin()->second;
111 }
double findMode(const double) const
Definition: GSUtilities.cc:113
unsigned theNComp
Definition: GSUtilities.h:74
float * theParameters
Definition: GSUtilities.h:76
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:140

◆ pdf()

double GSUtilities::pdf ( const double &  x) const

value of the pdf

Definition at line 140 of file GSUtilities.cc.

References gauss(), mps_fire::i, mps_fire::result, theErrors, theNComp, theParameters, theWeights, and x.

Referenced by errorMode(), findMode(), and mode().

140  {
141  double result(0.);
142  for (unsigned i = 0; i < theNComp; i++)
144  return result;
145 }
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:172
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

◆ quantile()

float GSUtilities::quantile ( const float  q) const

normalised integral from -inf to x (taking into account under- & overflows)

Definition at line 11 of file GSUtilities.cc.

References cdf(), PVValHelper::dx, MillePedeFileConverter_cfg::e, mps_fire::i, submitPVResolutionJobs::q, theErrors, theNComp, theParameters, theWeights, x, testProducerWithPsetDescEmpty_cfi::x1, testProducerWithPsetDescEmpty_cfi::x2, y, testProducerWithPsetDescEmpty_cfi::y1, and testProducerWithPsetDescEmpty_cfi::y2.

11  {
12  float qq = q;
13  if (q > 1)
14  qq = 1;
15  else if (q < 0)
16  qq = 0;
17  //
18  // mean and sigma of highest weight component
19  //
20  int iwMax(-1);
21  float wMax(0.);
22  for (unsigned i = 0; i < theNComp; i++) {
23  if (theWeights[i] > wMax) {
24  iwMax = i;
25  wMax = theWeights[i];
26  }
27  }
28  //
29  // Find start values: begin with mean and
30  // go towards f(x)=0 until two points on
31  // either side are found (assumes monotonously
32  // increasing function)
33  //
34  double x1(theParameters[iwMax]);
35  double y1(cdf(x1) - qq);
36  double dx = y1 > 0. ? -theErrors[iwMax] : theErrors[iwMax];
37  double x2(x1 + dx);
38  double y2(cdf(x2) - qq);
39  int cnt = 0;
40  while (y1 * y2 > 0.) {
41  x1 = x2;
42  y1 = y2;
43  x2 += dx;
44  y2 = cdf(x2) - qq;
45  cnt++;
46  }
47  // std::cout << "(" << x1 << "," << y1 << ") / ("
48  // << x2 << "," << y2 << ")" << std::endl;
49  //
50  // now use bisection to find final value
51  //
52  double x(0.);
53  while (true) {
54  // use linear interpolation
55  x = -(x2 * y1 - x1 * y2) / (y2 - y1);
56  double y = cdf(x) - qq;
57  // std::cout << x << " " << y << std::endl;
58  if (fabs(y) < 1.e-6)
59  break;
60  if (y * y1 > 0.) {
61  y1 = y;
62  x1 = x;
63  } else {
64  y2 = y;
65  x2 = x;
66  }
67  }
68  return x;
69 }
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:147
unsigned theNComp
Definition: GSUtilities.h:74
float * theErrors
Definition: GSUtilities.h:77
float * theWeights
Definition: GSUtilities.h:75
float * theParameters
Definition: GSUtilities.h:76

Member Data Documentation

◆ theErrors

float* GSUtilities::theErrors
private

Definition at line 77 of file GSUtilities.h.

Referenced by cdf(), dpdf1(), dpdf2(), errorHighestWeight(), GSUtilities(), pdf(), quantile(), and ~GSUtilities().

◆ theNComp

unsigned GSUtilities::theNComp
private

◆ theParameters

float* GSUtilities::theParameters
private

Definition at line 76 of file GSUtilities.h.

Referenced by cdf(), combinedMean(), dpdf1(), dpdf2(), GSUtilities(), mode(), pdf(), quantile(), and ~GSUtilities().

◆ theWeights

float* GSUtilities::theWeights
private