LCFIVertex  0.7.2
vertexfuncmaxfinderclassicstepper.cpp
1 #include "../include/vertexfuncmaxfinderclassicstepper.h"
2 #include "../../util/inc/vector3.h"
3 #include "../include/vertexfunction.h"
4 
5 namespace vertex_lcfi { namespace ZVTOP
6 {
7  VertexFuncMaxFinderClassicStepper::VertexFuncMaxFinderClassicStepper()
8  {
9  }
10 
11  Vector3 VertexFuncMaxFinderClassicStepper::findNearestMaximum(const Vector3 & StartPoint, VertexFunction* VertexFunction)
12  {
13  //TODO Exception on null function, sliding scale
14  _Function = VertexFunction;
15  _CurrentPos = StartPoint;
16  _CurrentValue = _Function->valueAt(_CurrentPos);
17  Vector3 start;
18  double step=4.0/1000.0; //Start at 1 Micron (division below) same as fortran NB Scale dependancy
19  do
20  {
21  step /= 2.0;
22  int iterations = 0;
23  do //Loop over directions until we don't move
24  {
25  start=_CurrentPos;
26  _minimiseAlongAxis(Vector3(step,0,0));
27  _minimiseAlongAxis(Vector3(0,step,0));
28  _minimiseAlongAxis(Vector3(0,0,step));
29  iterations++;
30  }while (start != _CurrentPos && iterations < 1000);
31  if (1000 == iterations) std::cerr << "Max Finding: Outer loop too many iterations" << std::endl;
32  }while (step<1.5/1000.0);
33  return _CurrentPos;
34  }
35 
36  void VertexFuncMaxFinderClassicStepper::_minimiseAlongAxis(const Vector3 & Step)
37  {
38  double direction;
39  double oldvalue;
40  Vector3 startpos = _CurrentPos;
41  Vector3 oldpos;
42  double Forward = _Function->valueAt(_CurrentPos+Step);
43  double Backward = _Function->valueAt(_CurrentPos-Step);
44 
45  if (Forward != Backward)
46  {
47  if (Forward > Backward)
48  {
49  direction = 1.0;
50  }
51  else
52  {
53  direction = -1.0;
54  }
55  int iterations = 0;
56  for(;;)
57  {
58  oldvalue = _CurrentValue;
59  oldpos = _CurrentPos;
60  _CurrentPos = _CurrentPos+(Step*direction);
61  _CurrentValue = _Function->valueAt(_CurrentPos);
62  ++iterations;
63  if (_CurrentValue < oldvalue || iterations>1000)
64  break;
65  }
66  if (iterations>1000)
67  {
68  std::cerr << "Max Finding - Too Many Iterations" <<std::endl;
69  _CurrentPos = startpos;
70  }
71  _CurrentValue = oldvalue;
72  _CurrentPos = oldpos;
73  }
74 
75 
76  }
77 
78 
79 
80 }}