FunctionProjector.cxx
Go to the documentation of this file.
1 
12 // For truncation warning
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "FunctionProjector.h"
18 
19 #include "ProjectorHelper.h"
20 
21 #include "axes/AxisModelBase.h"
22 #include "datasrcs/NTuple.h"
23 #include "functions/FunctionBase.h"
24 #include "minimizers/Fitter.h"
25 
26 #include <cmath>
27 
28 #include <cassert>
29 
30 using std::copy;
31 using std::list;
32 using std::string;
33 using std::vector;
34 
35 using namespace hippodraw;
36 
37 string FunctionProjector::s_x_label ("X");
38 
41  ProjectorBase * targetProjector )
42  : ProjectorBase (),
43  m_target ( targetProjector ),
44  m_fitter ( 0 )
45 {
46  m_function = function;
47  assert ( m_function != 0 );
48  m_z_label = function -> name ();
49 }
50 
53 FunctionProjector ( const FunctionProjector & projector )
54  : ProjectorBase ( projector )
55 {
56 }
57 
59 {
60  delete m_function;
61  delete m_fitter;
62 }
63 
66 {
67  return 0;
68 }
69 
71 {
72  return m_function;
73 }
74 
76 {
77  return m_save_parms.empty() == false;
78 }
79 
81 {
82  assert ( m_function != 0 );
83 
85  m_principleErrors.resize ( m_save_parms.size(), 0. );
86 }
87 
89 {
90  assert ( m_function != 0 );
91  assert ( ! m_save_parms.empty () );
92 
94 
95  setDirty ( true );
96 }
97 
98 void
100 setFitter ( Fitter * fitter )
101 {
102  m_fitter = fitter;
103 }
104 
105 Fitter *
107 getFitter ( ) const
108 {
109  return m_fitter;
110 }
111 
112 void
115 {
116  assert ( m_function != 0 );
117 
118  m_target = projector;
119  if ( m_target != 0 ) {
120  projector->prepareValues();
121  ProjectorHelper helper ( projector -> getProjectedValues () );
122  m_function->initialParameters ( & helper );
123  }
124 
125  saveParameters ();
126 }
127 
128 bool
131 {
132  assert ( m_function != 0 && m_target != 0 );
133  assert ( m_fitter != 0 );
134  m_target -> prepareValues ();
135 
136  if ( hasSavedParameters() == false ) {
137  saveParameters ();
138  }
139  // Calculate the best fit
140  bool ok = m_fitter->calcBestFit ();
141 
142  setDirty ( true );
143 
144  return ok;
145 }
146 
147 const string & FunctionProjector::getTitle() const
148 {
149  return m_function->name ();
150 }
151 
152 void
155 {
156  setDirty ( true );
157 }
158 
159 const std::string & FunctionProjector::getXLabel () const
160 {
161  return s_x_label;
162 }
163 
164 const string & FunctionProjector::getYLabel ( bool ) const
165 {
166  return m_function->name ();
167 }
168 
171 {
172  return Range ( 0, 0 );
173 }
174 
175 int
178 {
179  // A call to this function for a FunctionProjector is meaningless.
180  // Thus it returns meaningless negative number
181  return -1;
182 }
183 
184 int
186 getUnderflow () const
187 {
188  return -1;
189 }
190 
191 int
193 getOverflow () const
194 {
195  return -1;
196 }
197 
198 Range
201 {
202  AxisModelBase * model = 0;
203  switch ( axis ) {
204  case Axes::X :
205  model = m_x_axis;
206  break;
207  case Axes::Y :
208  model = m_y_axis;
209  break;
210  case Axes::Z :
211  model = m_z_axis;
212  break;
213  default:
214  assert ( false );
215  }
216 
217  return model -> getRange ( false );
218 }
219 
221 {
222  return m_fitter->objectiveValue ();
223 }
224 
225 void
228 {
229  int num_parms = m_save_parms.size ();
230  m_principleErrors.clear();
231  m_principleErrors.resize( num_parms, 0.0 );
232  if ( m_fitter != 0 ) {
233  m_fitter -> calcCovariance ( m_covariance );
234  int n = m_covariance.size();
235  if ( n != 0 ) {
236  const vector < int > & fixed = m_fitter -> getFixedFlags ();
237  int ic = 0;
238  for( int i = 0; i < num_parms; i++ ) {
239  if ( fixed [ i ] == 0 ) {
240  m_principleErrors[i] = sqrt( m_covariance[ic][ic] );
241  ic++;
242  }
243  else {
244  m_principleErrors[i] = 0.0;
245  }
246  }
247  }
248  }
249 }
250 
251 const vector < double > &
254 {
255  return m_principleErrors;
256 }
257 
258 void
260 setPrincipleErrors ( std::vector < double > :: const_iterator begin,
261  std::vector < double > :: const_iterator end )
262 {
263  unsigned int size = std::distance ( begin, end );
264  assert ( size == m_principleErrors.size () );
265 
266  copy ( begin, end, m_principleErrors.begin() );
267 }
268 
270 {
271  return m_fitter->calcDegreesOfFreedom ();
272 }
273 
274 double
277 {
278  return 0.0;
279 }
280 
281 const string & FunctionProjector::getZLabel() const
282 {
283  return m_z_label;
284 }
285 
287 {
288 }
289 
290 bool
292 isEmpty () const
293 {
294  return m_function == 0;
295 }
296 
297 void FunctionProjector::setParameters ( const std::vector<double> & params )
298 {
299  assert ( m_function != 0 );
300 
301  std::vector<double> myParams = m_function->getParameters();
302 
303  if ( myParams.size() == params.size() ) {
304  m_function->setParameters ( params );
305  setDirty ( true );
306  }
307 }
308 
309 const vector < vector < double > > &
311 covariance ( ) const
312 {
313  if ( m_covariance.empty () ) {
314  m_fitter -> calcCovariance ( m_covariance );
315  }
316 
317  return m_covariance;
318 }
319 
320 DataSource *
322 createNTuple () const
323 {
324  // does nothing
325  return 0;
326 }
327 
328 void
331 {
332  // does nothing.
333 }
334 
335 void
338 {
339  m_fitter -> setFitCut ( cut );
340 }
341 
342 void
344 setFitRange ( bool yes )
345 {
346  m_fitter -> setFitRange ( yes );
347 }
virtual bool calcBestFit()=0
Calculates the best fit.
unsigned int i
void initializeFunction(ProjectorBase *projector)
Gives the function object initial values based on a given data projector.
int degreesOfFreedom() const
Returns the number of degrees of freedom in the fit.
AxisModelBase * m_x_axis
The AxisModel along the X axis.
Definition: ProjectorBase.h:88
virtual void setRange(hippodraw::Axes::Type, bool)
Sets the range of the selected axis.
virtual const std::string & getXLabel() const
Finds the X axis label of the plot.
ProjectorBase * m_target
The ProjectorBase object containing the data points for the function and fitter.
std::vector< double > m_principleErrors
The principle errors of the errors associated with the parameters of the function.
virtual void prepareValues()
Informs the projector to prepare its projected values for plotting.
virtual const Range & getRange(Axes::Type) const
Returns the Range along the specified axis.
void setFitter(Fitter *fitter)
Sets the Fitter to be used.
virtual void addPointReps()
Function to add the acceptable point reps.
virtual DataSource * createNTuple() const
Returns a null pointer.
bool hasSavedParameters() const
Returns true if the function&#39;s parameters have been saved.
virtual void fillProjectedValues(DataSource *ntuple, bool in_range=false) const
Does nothing.
virtual Range valueRange() const
virtual bool isEmpty() const
Implements ProjectorBase.
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
void restoreParameters()
Restores the function&#39;s parameter values from the previously saved values.
virtual void setDirty(bool value=true)
Sets the dirty flag to value.
return yes
Definition: CanvasView.cxx:883
const DataSource * getProjectedValues() const
Returns DataSource representation of projected values.
AxisModelBase * m_z_axis
The AxisModel along the Z axis.
Definition: ProjectorBase.h:96
The base class for fitters.
Definition: Fitter.h:33
hippodraw::NTuple class interface.
const std::vector< double > & principleErrors() const
Returns the principle diagonal of the covariance matrix.
virtual Range dataRangeOn(hippodraw::Axes::Type) const
Returns the range of the raw data.
virtual void setFitRange(bool yes=true)
Sets use of a fitting range on or off.
bool ok
Definition: CanvasView.cxx:163
int getOverflow() const
Returns the number of overflow.
std::vector< double > m_save_parms
A vector used to save parameters so that they can be restored.
int getNumberOfEntries() const
A call to this member function is meaningless.
const std::string & getZLabel() const
Is meaningless for this projector.
void setParameters(const std::vector< double > &params)
Set the parameter values by hand.
std::vector< std::vector< double > > m_covariance
The error covariance matrix of the errors associated with the parameters of the function.
double objectiveValue() const
Returns the objective value (chi2 or likelihood) between the function and the data.
intp size(numeric::array arr)
Definition: num_util.cpp:296
FunctionProjector(FunctionBase *function, ProjectorBase *targetProjector)
This constructor takes a FunctionBase object and its target data projector.
static std::string s_x_label
An X label that might be used.
The AxisModelBase class maintains the Range and scaling of an axis.
Definition: AxisModelBase.h:33
bool fitFunction()
Fits the function.
hippodraw::Fitter class interface
virtual int calcDegreesOfFreedom() const
Returns the number of degrees of freedom in the fit.
Definition: Fitter.cxx:229
virtual void setFitCut(TupleCut *cut)
Sets the cut to limit range of fitting.
const std::vector< std::vector< double > > & covariance() const
Returns the covariance matrix.
virtual ~FunctionProjector()
The virtual destructor.
virtual double getPosOn(hippodraw::Axes::Type) const
Returns the minimum positive value.
virtual void initialParameters(const FunctionHelper *helper)
Sets the FunctionHelper so that the function can calculate a reasonable set of initial parameter valu...
std::string m_z_label
Dummy member so getZLabel member can return reference.
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
virtual const std::vector< double > & getParameters() const
Returns the values of the parameters as a vector.
hippodraw::AxisModelBase class interface
hippodraw::FunctionProjector class interface
Fitter * m_fitter
A fitter object for the function.
FunctionBase * function() const
Returns a pointer to the contained function.
A concreate implementation of the FunctionHelper class.
virtual double objectiveValue() const
Calculates the value of the objective function at the current set of parameters.
Definition: Fitter.cxx:222
virtual void setParameters(const std::vector< double > &incr)
Sets the parameter values.
Expresses a range of values.
Definition: Range.h:33
hippodraw::FunctionBase class interface
Fitter * getFitter() const
Returns the Fitter that is in use.
AxisModelBase * m_y_axis
The AxisModel along the Y axis.
Definition: ProjectorBase.h:92
void saveParameters()
Makes a copy of the function&#39;s parameter values.
virtual const std::string & getTitle() const
Finds the title of the plot.
void setPrincipleErrors(std::vector< double >::const_iterator first, std::vector< double >::const_iterator last)
Sets the principle errors.
ProjectorHelper class interface.
int getUnderflow() const
Returns the number of underflow.
void calcPrincipleErrors() const
Calculates the principle errors from the fit.
virtual ProjectorBase * clone()
virtual const std::string & getYLabel(bool density=false) const
Finds the Y axis label of the plot.
Type
Axes constants.
Definition: AxesType.h:31
const std::string & name() const
Returns the name of the function.
A projector that plots one function.
FunctionBase * m_function
The list of function objects to be projected.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen