ComPWA
Common Partial-Wave-Analysis Framework
Integration.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 #include "Integration.hpp"
6 
7 #include "Core/Logging.hpp"
8 #include "Data/DataSet.hpp"
9 
10 #include <algorithm>
11 #include <cmath>
12 #include <vector>
13 
14 #include "ThirdParty/parallelstl/include/pstl/algorithm"
15 #include "ThirdParty/parallelstl/include/pstl/execution"
16 
17 namespace ComPWA {
18 namespace Tools {
19 
21  double sum;
22  double correction;
23  operator double() const { return sum; }
24 };
25 
29 KahanSummation KahanSum(KahanSummation accumulation, double value) {
30  KahanSummation result;
31  double y = value - accumulation.correction;
32  double t = accumulation.sum + y;
33  result.correction = (t - accumulation.sum) - y;
34  result.sum = t;
35  return result;
36 }
37 
38 std::pair<double, double>
40  const ComPWA::Data::DataSet &phspsample, double phspVolume) {
41  auto WeightSum =
42  std::accumulate(phspsample.Weights.begin(), phspsample.Weights.end(),
43  KahanSummation{0., 0.}, KahanSum);
44 
45  std::vector<double> Intensities = intensity.evaluate(phspsample.Data);
46  std::transform(
47  pstl::execution::par_unseq, Intensities.begin(), Intensities.end(),
48  phspsample.Weights.begin(), Intensities.begin(),
49  [](double intensity, double weight) { return intensity * weight; });
50 
51  auto IntensitySum = std::accumulate(Intensities.begin(), Intensities.end(),
52  KahanSummation{0., 0.}, KahanSum);
53  double AvgInt = IntensitySum / WeightSum;
54  double Integral = AvgInt * phspVolume;
55 
56  // We reuse the intensities vector to store residuals of intensities
57  std::transform(pstl::execution::par_unseq, Intensities.begin(),
58  Intensities.end(), Intensities.begin(),
59  [&AvgInt](double intensity) {
60  return (intensity - AvgInt) * (intensity - AvgInt);
61  });
62  auto IntensityResidualsSum = std::accumulate(
63  Intensities.begin(), Intensities.end(), KahanSummation{0., 0.}, KahanSum);
64  double AvgIntResSq = IntensityResidualsSum / (WeightSum - 1);
65  double IntegralErrorSq = AvgIntResSq * phspVolume * phspVolume / WeightSum;
66 
67  return std::make_pair(Integral, std::sqrt(IntegralErrorSq));
68 }
69 
70 double integrate(ComPWA::Intensity &intensity,
71  const ComPWA::Data::DataSet &phspsample, double phspVolume) {
72  return integrateWithError(intensity, phspsample, phspVolume).first;
73 }
74 
75 double maximum(ComPWA::Intensity &intensity,
76  const ComPWA::Data::DataSet &sample) {
77  if (!sample.Weights.size()) {
78  LOG(DEBUG) << "Tools::Maximum(): Maximum can not be determined since "
79  "sample is empty.";
80  return 1.0;
81  }
82 
83  std::vector<double> Intensities = intensity.evaluate(sample.Data);
84 
85  std::transform(Intensities.begin(), Intensities.end(), sample.Weights.begin(),
86  Intensities.begin(), [](double Intensity, double Weight) {
87  return Intensity * Weight;
88  });
89  // determine maximum
90  double max(*std::max_element(Intensities.begin(), Intensities.end()));
91  LOG(INFO) << "Tools::Maximum(): found maximum value of " << max;
92  return max;
93 }
94 
95 } // namespace Tools
96 } // namespace ComPWA
ComPWA::DataMap Data
Definition: DataSet.hpp:18
std::vector< double > Weights
Definition: DataSet.hpp:19
std::pair< double, double > integrateWithError(ComPWA::Intensity &intensity, const ComPWA::Data::DataSet &phspsample, double phspVolume)
Calculate integral and its error.
Definition: Integration.cpp:39
virtual OutputType evaluate(const InputTypes &... args) noexcept=0
KahanSummation KahanSum(KahanSummation accumulation, double value)
KahanSummation keeps track of lost bits and reduced the uncertainty in the summation of many large/sm...
Definition: Integration.cpp:29
Function< std::vector< double >, DataMap > Intensity
An Intensity is just a Function that takes a list of data vectors and returns a list of intensities (...
Definition: Function.hpp:40
double maximum(ComPWA::Intensity &intensity, const ComPWA::Data::DataSet &sample)
Definition: Integration.cpp:75
double integrate(ComPWA::Intensity &intensity, const ComPWA::Data::DataSet &phspsample, double phspVolume)
Definition: Integration.cpp:70
Interface template for a general Function of the form OutputType Function(InputTypes) The concept clo...
Definition: Function.hpp:24