ConstIntCut.h
Go to the documentation of this file.
1 // \author Darko Veberic
2 // \date 2014
3 
4 #ifndef _cic_ConstIntCut_h_
5 #define _cic_ConstIntCut_h_
6 
7 #include <cic/Event.h>
8 #include <utl/Accumulator.h>
9 #include <utl/Bin.h>
10 #include <vector>
11 #include <algorithm>
12 #include <functional>
13 
14 
15 namespace cic {
16 
17  struct ConstIntCut {
18 
19  typedef std::function<double(double)> Attenuation;
20  typedef std::function<double(double, double)> SFunction;
22 
23  ConstIntCut(const std::vector<Event>& e,
24  const SFunction& sFunc,
25  const double maxSin2Theta,
26  const unsigned int nBinsSin2Theta,
27  const double sRange) :
28  fMaxSin2Theta(maxSin2Theta),
30  utl::EquidistantInNorm<Event>(
31  0,
32  maxSin2Theta,
33  nBinsSin2Theta,
34  [](const Event& e) { return e.fSin2Theta; }
35  )
36  ),
37  fSFunc(sFunc),
38  fNEvents(e.size())
39  {
40  fSin2ThetaBins.Apply(e);
41  for (BinType& b : fSin2ThetaBins.GetAllBins())
42  b.Sort(
43  [&sRange](const Event& a, const Event& b) {
44  return a.fLnS1000 + sRange*a.fLnS1000Err > b.fLnS1000 + sRange*b.fLnS1000Err;
45  }
46  );
47  }
48 
49  unsigned int GetNBins() const { return fSin2ThetaBins.GetNBins(); }
50 
51  private:
52  static
53  void
54  SortByFirst(std::vector<std::pair<double, double>>& pairs)
55  {
56  typedef std::pair<double, double> DD;
57  std::sort(pairs.begin(), pairs.end(),
58  [](const DD& a, const DD& b) { return a.first < b.first; });
59  }
60 
61  void
63  {
64  fNEventsAboveCut = 0;
65  fSin2ThetaMinMaxN =
66  utl::Accumulator::MinMax<double>(std::numeric_limits<double>::infinity(),
67  -std::numeric_limits<double>::infinity());
68  fLnSMinMaxN = fSin2ThetaMinMaxN;
69  }
70 
71  double
73  const double mins2, const double maxs2)
74  {
75  using namespace std;
76 
77  const unsigned int ns2 = 10;
78  const double ds2 = (maxs2 - mins2) / ns2;
79  utl::Accumulator::Min<double> minLnAtt(log(att(mins2)));
80  for (unsigned int i = 1; i <= ns2; ++i)
81  minLnAtt(log(att(mins2 + i*ds2)));
82  const double eps = 0.01;
83  return minLnAtt.GetMin() - eps;
84  }
85 
86  public:
87  std::vector<double>
88  MakePDF(const Attenuation& att, const double lnSCut)
89  {
90  using namespace std;
91 
92  const unsigned int n = fSin2ThetaBins.GetNBins();
93  vector<double> pdf;
94  pdf.reserve(n);
95  ResetCounters();
96  for (unsigned int i = 0; i < n; ++i) {
97  const double minLnAtt =
98  GetMinLnAttenuation(att, fSin2ThetaBins.GetBinLowerEdge(i),
99  fSin2ThetaBins.GetBinUpperEdge(i));
100  const BinType& b = fSin2ThetaBins[i];
101  double sum = 0;
102  for (const Event& e : b.GetCollection()) {
103  const double cut1000 = lnSCut + log(att(e.fSin2Theta));
104  const double w = fSFunc(e.fLnS1000 - cut1000, e.fLnS1000Err);
105  if (!w && !fSFunc(e.fLnS1000 - (lnSCut + minLnAtt), e.fLnS1000Err))
106  break;
107  sum += w;
108  }
109  pdf.push_back(sum);
110  fNEventsAboveCut += sum;
111  fSin2ThetaMinMaxN(sum);
112  }
113  return pdf;
114  }
115 
116  template<class Binning>
117  std::vector<std::vector<double>>
118  MakePDF2D(const Attenuation& att, const Binning& lnS38Binning)
119  {
120  using namespace std;
121  using namespace utl;
122  using namespace utl::Accumulator;
123 
124  Bin<Sum, Binning> binLnS38(lnS38Binning);
125  const unsigned int nlns = binLnS38.GetNBins();
126  const double minLnSCut = binLnS38.GetBinLowerEdge(0);
127  const unsigned int ns2 = fSin2ThetaBins.GetNBins();
128  vector<vector<double>> ret(nlns, vector<double>(ns2));
129  const vector<Collect<Event>>& s2Bins = fSin2ThetaBins.GetAllBins();
130  for (unsigned int is2 = 0; is2 < ns2; ++is2) {
131  const double minLnAtt =
132  GetMinLnAttenuation(att, fSin2ThetaBins.GetBinLowerEdge(is2),
133  fSin2ThetaBins.GetBinUpperEdge(is2));
134  for (const Event& e : s2Bins[is2].GetCollection()) {
135  const double lnS38 = e.fLnS1000 - log(att(e.fSin2Theta));
136  const double err = e.fLnS1000Err;
137  double wup = fSFunc(lnS38 - binLnS38.GetBinUpperEdge(nlns-1), err);
138  for (int ilns = nlns-1; ilns >= 0; --ilns) {
139  const double wdn =
140  fSFunc(lnS38 - binLnS38.GetBinLowerEdge(ilns), err);
141  const double w = wdn - wup;
142  if (w)
143  binLnS38[ilns](w);
144  wup = wdn;
145  }
146  if (!wup && !fSFunc(e.fLnS1000 - (minLnSCut + minLnAtt), err))
147  break;
148  }
149  for (unsigned int ilns = 0; ilns < nlns; ++ilns)
150  ret[ilns][is2] = binLnS38[ilns].GetSum();
151  binLnS38.Clear();
152  }
153 
154  ResetCounters();
155  for (const auto& row : ret) {
156  double rowSum = 0;
157  for (const double n : row) {
158  rowSum += n;
159  fSin2ThetaMinMaxN(n);
160  }
161  fNEventsAboveCut += rowSum;
162  fLnSMinMaxN(rowSum);
163  }
164 
165  return ret;
166  }
167 
168  std::vector<std::pair<double, double>>
169  MakeCDF(const Attenuation& att, const double lnSCut)
170  {
171  using namespace std;
172 
173  const unsigned int n = fSin2ThetaBins.GetNBins();
174  vector<pair<double, double>> wcdf;
175  ResetCounters();
176  for (unsigned int i = 0; i < n; ++i) {
177  const double minLnAtt =
178  GetMinLnAttenuation(att, fSin2ThetaBins.GetBinLowerEdge(i),
179  fSin2ThetaBins.GetBinUpperEdge(i));
180  for (const Event& e : fSin2ThetaBins[i].GetCollection()) {
181  const double s2 = e.fSin2Theta;
182  const double cut1000 = lnSCut + log(att(s2));
183  const double w = fSFunc(e.fLnS1000 - cut1000, e.fLnS1000Err);
184  if (w) {
185  wcdf.push_back(make_pair(s2 / fMaxSin2Theta, w));
186  fNEventsAboveCut += w;
187  } else if (!fSFunc(e.fLnS1000 - (lnSCut + minLnAtt), e.fLnS1000Err))
188  break;
189  }
190  }
191  SortByFirst(wcdf);
192  return wcdf;
193  }
194 
195  template<class Binning>
196  std::vector<std::vector<std::pair<double, double>>>
197  MakeCDF2D(const Attenuation& att, const Binning& lnS38Binning)
198  {
199  using namespace std;
200  using namespace utl;
201  using namespace utl::Accumulator;
202 
203  typedef pair<double, double> DD;
204  Bin<Collect<DD>, Binning> binLnS38(lnS38Binning);
205  const unsigned int nlns = binLnS38.GetNBins();
206  const double minLnSCut = binLnS38.GetBinLowerEdge(0);
207  const unsigned int ns2 = fSin2ThetaBins.GetNBins();
208  const vector<Collect<Event>>& s2Bins = fSin2ThetaBins.GetAllBins();
209  for (unsigned int is2 = 0; is2 < ns2; ++is2) {
210  const double minLnAtt =
211  GetMinLnAttenuation(att, fSin2ThetaBins.GetBinLowerEdge(is2),
212  fSin2ThetaBins.GetBinUpperEdge(is2));
213  for (const Event& e : s2Bins[is2].GetCollection()) {
214  const double s2 = e.fSin2Theta;
215  const double lnS38 = e.fLnS1000 - log(att(s2));
216  const double err = e.fLnS1000Err;
217  double wup = fSFunc(lnS38 - binLnS38.GetBinUpperEdge(nlns-1), err);
218  for (int ilns = nlns-1; ilns >= 0; --ilns) {
219  const double wdn =
220  fSFunc(lnS38 - binLnS38.GetBinLowerEdge(ilns), err);
221  const double w = wdn - wup;
222  if (w)
223  binLnS38[ilns](make_pair(s2 / fMaxSin2Theta, w));
224  wup = wdn;
225  }
226  if (!wup && !fSFunc(e.fLnS1000 - (minLnSCut + minLnAtt), err))
227  break;
228  }
229  }
230 
231  vector<vector<DD>> ret(nlns);
232  for (unsigned int i = 0; i < nlns; ++i) {
233  ret[i] = binLnS38[i].GetCollection();
234  sort(ret[i].begin(), ret[i].end(),
235  [](const DD& a, const DD& b) { return a.first < b.first; });
236  }
237  ResetCounters();
238  for (const auto& row : ret) {
239  double rowSum = 0;
240  for (const DD& xw : row)
241  rowSum += xw.second;
242  fNEventsAboveCut += rowSum;
243  fLnSMinMaxN(rowSum);
244  }
245 
246  return ret;
247  }
248 
249  // Chi^2 measure of uniformity
250  double
251  GetChi2Ndof(const Attenuation& att, const double lnSCut)
252  {
253  const std::vector<double> pdf = MakePDF(att, lnSCut);
254  return utl::UniformChi2Ndof(pdf, fNEventsAboveCut);
255  }
256 
257  double
258  GetPoissonChi2(const Attenuation& att, const double lnSCut)
259  {
260  const std::vector<double> pdf = MakePDF(att, lnSCut);
261  return utl::UniformPoissonChi2(MakePDF(att, lnSCut), fNEventsAboveCut);
262  }
263 
264  // Kolmogorov-Smirnov measure of uniformity
265  double
266  GetKS(const Attenuation& att, const double lnSCut)
267  {
268  const std::vector<std::pair<double, double>> cdf = MakeCDF(att, lnSCut);
269  return utl::UniformKolmogorovSmirnov(cdf, fNEventsAboveCut);
270  }
271 
272  // Anderson-Drling measure of uniformity
273  double
274  GetAndersonDarling(const Attenuation& att, const double lnSCut)
275  {
276  const std::vector<std::pair<double, double>> cdf = MakeCDF(att, lnSCut);
277  return utl::UniformAndersonDarling(cdf, fNEventsAboveCut);
278  }
279 
280  template<class Binning>
281  double
282  GetPoissonChi22D(const Attenuation& att, const Binning& lnSBinning)
283  {
284  return utl::UniformPoissonChi2(MakePDF2D(att, lnSBinning));
285  }
286 
287  template<class Binning>
288  double
289  GetAndersonDarling2D(const Attenuation& att, const Binning& lnSBinning)
290  {
291  return utl::UniformAndersonDarling(MakeCDF2D(att, lnSBinning));
292  }
293 
294  double fMaxSin2Theta = 0;
295  utl::Bin<
296  BinType,
300  unsigned int fNEvents = 0;
301  double fNEventsAboveCut = 0;
303  utl::Accumulator::MinMax<double>(std::numeric_limits<double>::infinity(),
304  -std::numeric_limits<double>::infinity());
306  utl::Accumulator::MinMax<double>(std::numeric_limits<double>::infinity(),
307  -std::numeric_limits<double>::infinity());
308 
309  };
310 
311 }
312 
313 
314 #endif
double fMaxSin2Theta
Definition: ConstIntCut.h:294
double UniformKolmogorovSmirnov(const vector< pair< double, double >> &wcdf, double wtot)
std::vector< std::vector< std::pair< double, double > > > MakeCDF2D(const Attenuation &att, const Binning &lnS38Binning)
Definition: ConstIntCut.h:197
double GetAndersonDarling(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:274
unsigned int GetNBins() const
Definition: ConstIntCut.h:49
static void SortByFirst(std::vector< std::pair< double, double >> &pairs)
Definition: ConstIntCut.h:54
double GetPoissonChi2(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:258
utl::Accumulator::Collect< Event > BinType
Definition: ConstIntCut.h:21
std::function< double(double, double)> SFunction
Definition: ConstIntCut.h:20
double GetMinLnAttenuation(const Attenuation &att, const double mins2, const double maxs2)
Definition: ConstIntCut.h:72
double eps
std::vector< std::pair< double, double > > MakeCDF(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:169
unsigned int fNEvents
Definition: ConstIntCut.h:300
const SFunction & fSFunc
Definition: ConstIntCut.h:299
double UniformPoissonChi2(const std::vector< T > &counts, double nTot=0)
std::vector< std::vector< double > > MakePDF2D(const Attenuation &att, const Binning &lnS38Binning)
Definition: ConstIntCut.h:118
double UniformAndersonDarling(const vector< pair< double, double >> &wcdf, double wtot)
double GetKS(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:266
void ResetCounters()
Definition: ConstIntCut.h:62
ConstIntCut(const std::vector< Event > &e, const SFunction &sFunc, const double maxSin2Theta, const unsigned int nBinsSin2Theta, const double sRange)
Definition: ConstIntCut.h:23
double GetChi2Ndof(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:251
std::function< double(double)> Attenuation
Definition: ConstIntCut.h:19
void Clear()
Definition: Bin.h:152
Definition: Bin.h:76
double GetPoissonChi22D(const Attenuation &att, const Binning &lnSBinning)
Definition: ConstIntCut.h:282
std::vector< BinType > & GetAllBins()
Definition: Bin.h:85
double GetAndersonDarling2D(const Attenuation &att, const Binning &lnSBinning)
Definition: ConstIntCut.h:289
utl::Bin< BinType, utl::EquidistantInNorm< Event > > fSin2ThetaBins
Definition: ConstIntCut.h:298
std::vector< double > MakePDF(const Attenuation &att, const double lnSCut)
Definition: ConstIntCut.h:88
double UniformChi2Ndof(const std::vector< T > &counts, double nTot=0)

, generated on Tue Sep 26 2023.