Rivet Analyses Reference

EXAMPLE_MINIMIZE

A demo of how to use the built-in function minimizer
Experiment: ()
Status: EXAMPLE
Authors:
  • Andy Buckley
No references listed
Beams: * *
Beam energies: ANY
Run details:
  • All event types will be accepted.

A brief demonstration of using the MendelMin minimizer to wrap a C++ function and minimize its value.

Source code: EXAMPLE_MINIMIZE.cc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// -*- C++ -*-
#include "Rivet/Analysis.hh"
#include "Rivet/Tools/MendelMin.hh"

namespace Rivet {



  double f1(double p0, double p1) {
    const double x = p0 - 0.3;
    const double y = p1 - 0.5;
    return (x*x + 2*y*y) + 0.1*sin(x)*sin(y);
  }

  double f2(const MendelMin::Params& p, const MendelMin::Params&) {
    return f1(p[0], p[1]);
  }

  double f3(const MendelMin::Params& p) {
    return f1(p[0], p[1]);
  }



  /// @brief Demo of using the Rivet function minimizer
  class EXAMPLE_MINIMIZE : public Analysis {
  public:

    /// Constructor
    RIVET_DEFAULT_ANALYSIS_CTOR(EXAMPLE_MINIMIZE);


    /// @name Analysis methods
    //@{

    /// Book histograms and initialise projections before the run
    // void init() {    }


    /// Perform the per-event analysis
    void analyze(const Event& event) {
      // MendelMin mm(f2, 2, {});
      MendelMin mm(f3, 2);
      const double best = mm.evolve(20);

      valarray<double> fittest = mm.fittest();
      // cout << "FOUND ANSWER: " << f2(fittest, {}) << " == " << best << "; ";
      cout << "FOUND ANSWER: " << f3(fittest) << " == " << best << "; ";
      for (double x : fittest)
        cout << x << " ";
      cout << endl;

      valarray<double> right{0.3, 0.5};
      // cout << "RIGHT ANSWER: " << f2(right, {}) << "; ";
      cout << "RIGHT ANSWER: " << f3(right) << "; ";
      for (double x : right)
        cout << x << " ";
      cout << endl;

    }


    /// Normalise histograms etc., after the run
    // void finalize() {    }

    //@}

  };


  RIVET_DECLARE_PLUGIN(EXAMPLE_MINIMIZE);

}