FunctionRep.cxx
Go to the documentation of this file.
1 
12 // for truncation warning in debug mode
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "FunctionRep.h"
18 
19 #include "FunctionParameter.h"
20 
21 #include "datasrcs/DataSource.h"
22 #include "datasrcs/TupleCut.h"
23 
24 #include "functions/FunctionBase.h"
25 #include "minimizers/Fitter.h"
27 #include "reps/CutRangeRep.h"
28 
29 #include <cassert>
30 
31 using std::string;
32 using std::vector;
33 
34 using namespace hippodraw;
35 
38  : m_in_composite ( false )
39 {
40  m_name = "Function";
41  m_target = rep;
42 
43  ProjectorBase * target = 0;
44 
45  if ( m_target != 0 ) {
46  m_target -> addObserver ( this );
47 
48  const DataSource * ntuple = rep -> getProjectedValues ();
49  DataSource * nt = const_cast < DataSource * > ( ntuple );
50  nt -> addObserver ( this );
51  target = rep -> getProjector ();
52  }
53 
54  m_projector = new FunctionProjector ( function, target );
55 
56  // The following is needed to ensure FunctionProjector::principleErrors()
57  // knows the correct number of parameters for this function.
58  dynamic_cast<FunctionProjector *>(m_projector)->saveParameters();
59 
60 }
61 
64  : DataRep ( rep )
65 {
66  m_cut_rep = rep.m_cut_rep -> clone ();
67 }
68 
71 {
72  if ( m_target != 0 ) {
73  m_target -> removeObserver ( this );
74 
75  const DataSource * target = m_target -> getProjectedValues ();
76  DataSource * nt = const_cast < DataSource * > ( target );
77  nt -> removeObserver ( this );
78  }
79 }
80 
82 {
83  return new FunctionRep ( *this );
84 }
85 
87 {
88  return false;
89 }
90 
92 {
93  m_target = rep;
94  ProjectorBase * projector = rep -> getProjector ();
95 
96  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
97 
98  fp->initializeFunction ( projector );
99 }
100 
102 {
103  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
104 
105  return fp->function ();
106 }
107 
109 {
110  return m_target;
111 }
112 
114 {
115  m_target = target;
116 }
117 
119 {
120  FunctionBase * function = getFunction();
121  return function->isComposite ();
122 }
123 
124 bool
127 {
128  return m_in_composite;
129 }
130 
131 void
134 {
136 }
137 
138 bool
140 setFitter ( Fitter * fitter )
141 {
142  if ( fitter -> needsIntegrated () == true ) {
143  bool yes = m_target -> isAxisBinned ( Axes::X );
144  if ( ! yes ) return false;
145  }
146 
147  FunctionBase * function = getFunction ();
148  fitter -> setFunction ( function );
149 
150  const DataSource * target = m_target -> getProjectedValues ();
151  fitter -> setDataSource ( target );
152 
153  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
154  assert ( fp );
155 
156  fp -> setFitter ( fitter );
157 
158  return true;
159 }
160 
161 Fitter *
163 getFitter ( ) const
164 {
165  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
166  assert ( fp );
167 
168  return fp -> getFitter ();
169 }
170 
171 const string &
174 {
175  Fitter * fitter = getFitter ();
176 
177  return fitter -> name ();
178 }
179 
181 {
182 
183  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
184  assert ( fp );
185 
186  bool ok = fp->fitFunction ( );
187 
188  fp -> calcPrincipleErrors ();
189  notifyObservers ();
190 
191  return ok;
192 }
193 
194 double
197 {
198  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
199 
200  return fp -> objectiveValue ();
201 }
202 
203 const vector < vector < double > > &
206 {
207  FunctionProjector * projector
208  = dynamic_cast < FunctionProjector * > ( m_projector );
209 
210  return projector -> covariance ();
211 }
212 
213 int
216 {
217  FunctionProjector * projector
218  = dynamic_cast < FunctionProjector * > ( m_projector );
219 
220  return projector -> degreesOfFreedom ();
221 }
222 
223 const vector< double > &
226 {
227  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
228  assert ( fp );
229 
230  return fp->principleErrors();
231 }
232 
234 {
235  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
236  fp->saveParameters ();
237 }
238 
239 void
241 setPrincipleErrors ( std::vector < double >::const_iterator begin,
242  std::vector < double >::const_iterator end )
243 {
244  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
245 
246  fp -> setPrincipleErrors ( begin, end );
247 }
248 
250 {
251  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
252  fp->restoreParameters ();
253  notifyObservers ();
254 }
255 
256 void FunctionRep::setParameters ( const std::vector<double> & params )
257 {
258  FunctionProjector * fp = dynamic_cast < FunctionProjector * > ( m_projector);
259  fp->setParameters (params);
260  notifyObservers ();
261 }
262 
263 void
265 setFixedFlags ( const std::vector < int > & flags )
266 {
267  Fitter * fitter = getFitter ();
268  fitter -> setFixedFlags ( flags );
269 }
270 
271 const vector < string > & FunctionRep::parmNames () const
272 {
273  FunctionBase * function = getFunction ();
274 
275  return function->parmNames ();
276 }
277 
278 const vector < double > & FunctionRep::parameters () const
279 {
280  FunctionBase * function = getFunction ();
281 
282  return function->getParameters ();
283 }
284 
285 const vector < int > &
287 getFixedFlags ( ) const
288 {
289  const Fitter * fitter = getFitter ();
290 
291  return fitter -> getFixedFlags ();
292 }
293 
294 void
296 setIgnoreError(bool ignore)
297 {
298 
299  Fitter * fitter = getFitter ();
300  if ( fitter != 0 ) {
301  fitter -> setUseErrors ( ! ignore );
302  }
303 }
304 
305 void
307 willDelete ( const Observable * observed )
308 {
309  const DataRep * rep = dynamic_cast < const DataRep * > ( observed );
310  if ( rep != 0 ) {
311  m_target = 0;
312  const DataSource * projvalues = rep -> getProjectedValues ();
313  DataSource * source = const_cast < DataSource * > ( projvalues );
314  source -> removeObserver ( this );
315  }
316 }
317 
318 bool
321 {
322  bool yes = false;
323  Fitter * fitter = getFitter ();
324  if (fitter !=0) {
325  yes = ! fitter -> getUseErrors ();
326  }
327 
328  return yes;
329 }
330 
331 bool
333 isTargetable () const
334 {
335  return false;
336 }
337 
338 void
340 setCutRange ( const Range & range )
341 {
342  if ( m_cut_rep == 0 ) {
343  m_cut_rep = new CutRangeRep ();
344  TupleCut cut;
345  m_cut_rep -> addCut ( cut );
346  }
347 
348  const vector < TupleCut > & cut_list = m_cut_rep -> getCuts ();
349  const TupleCut & c = cut_list.front(); // to be index later
350  TupleCut & cut = const_cast < TupleCut &> ( c );
351 
352  const ProjectorBase * target = m_target -> getProjector ();
353  const string & label = target -> getXLabel ();
354  cut.setLabel ( label );
355  cut.setRange ( range );
356 
357  FunctionProjector * projector
358  = dynamic_cast < FunctionProjector * > ( m_projector );
359  assert ( projector != 0 );
360 
361  projector -> setFitCut ( & cut );
362  projector -> setFitRange ( true );
363 }
364 
367 void
369 setCutRange ( bool yes )
370 {
371  FunctionProjector * projector
372  = dynamic_cast < FunctionProjector * > ( m_projector );
373  assert ( projector != 0 );
374 
375  projector -> setFitRange ( yes );
376 }
377 
378 void
381 {
382  bool yes = m_target -> hasCut ();
383  if ( yes ) {
384  const vector < TupleCut > & cut_list = m_target -> getCuts ();
385 
386  const TupleCut & temp = cut_list.front ();
387  TupleCut & cut = const_cast < TupleCut & > ( temp );
388 
389  FunctionProjector * projector
390  = dynamic_cast < FunctionProjector * > ( m_projector );
391  assert ( projector != 0 );
392 
393  projector -> setFitCut ( & cut );
394  projector -> setFitRange ( true );
395  }
396 }
397 
398 void
401 {
402  FunctionProjector * projector
403  = dynamic_cast < FunctionProjector * > ( m_projector );
404  projector -> setFitCut ( 0 );
405  projector -> setFitRange ( false );
406 
407  DataRep::removeCut (); // If this object owns it
408  m_target -> removeCut (); // if target owns it.
409  m_target -> setDirty ( true );
410 }
411 
412 bool
414 hasZoomY () const
415 {
416  return true;
417 }
418 
419 void
421 fillFunctionParameters( std::vector<FunctionParameter> & function_parms) const
422 {
423  function_parms.clear ();
424  const vector < string > & parm_names = parmNames ();
425  const vector < double > & values = parameters ();
426  const vector < double > & errors = principleErrors ();
427  const vector < int > & flags = getFixedFlags ();
428 
429  unsigned int count = parm_names.size ();
430  for ( unsigned int i = 0; i < count; i++ ) {
431  FunctionParameter fp ( parm_names[i],
432  values[i],
433  errors[i],
434  flags[i] != 0,
435  0., // lower limit
436  0., // upper limit
437  false, // has lower
438  false ); // has upper
439  function_parms.push_back ( fp );
440  }
441 }
442 
443 void
446 {
447  // does nothing
448 }
449 
450 void
453 {
454  // does nothing
455 }
456 
457 const string &
459 functionName () const
460 {
461  FunctionBase * function = getFunction ();
462 
463  return function -> name ();
464 }
bool getIgnoreErrors() const
Get ignore errors.
void setParameters(const std::vector< double > &params)
Set the parameter values by hand.
const std::vector< std::vector< double > > & covarianceMatrix() const
Returns the covariance matrix of the function to the target DataSource.
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
const std::vector< TupleCut > & getCuts() const
Returns a reference to the cut list.
Definition: DataRep.cxx:505
unsigned int i
std::string m_name
The name of the display as it might be displayed in a GUI.
Definition: DataRep.h:76
void initializeFunction(ProjectorBase *projector)
Gives the function object initial values based on a given data projector.
virtual void setFixedFlags(const std::vector< int > &flags)
Set the parameters that are to be held fixed during a fit.
FunctionBase * getFunction() const
Returns the actual function, a FunctionBase derived class.
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
void setCutRange(const Range &)
Sets the cut to limit range of fitting.
const std::vector< int > & getFixedFlags() const
Returns a set of flags indicating which function parameters are fixed during a fit.
bool m_in_composite
A flag which when set true indicates the receiving object is a member of a composite.
Definition: FunctionRep.h:43
A derived class of PointRepBase class that draws a representation of a cut range. ...
Definition: CutRangeRep.h:29
virtual bool hasNTupleBindings() const
Returns false as this class does not have DataSource bindings.
Definition: FunctionRep.cxx:86
const std::vector< double > & parameters() const
Returns the current function parameter values.
void restoreParameters()
Restores the saved parameters.
const std::string & name() const
Returns the name of the display.
Definition: DataRep.cxx:72
bool isComposite() const
Returns true if represented function is a composite of two or more functions.
bool setFitter(Fitter *fitter)
Sets the Fitter to fitter.
void setRange(const Range &range)
Sets the range of the cut.
Definition: TupleCut.cxx:77
FunctionParameter class interface.
const std::string & getFitterName() const
Returns the name of the Fitter object.
void initializeWith(DataRep *)
Sets the target and Initializes the function with the projected values of the DataRep.
Definition: FunctionRep.cxx:91
A derived class of DataRep which is a base class for displaying a function.
Definition: FunctionRep.h:34
bool fitFunction()
Fits the contained function to the data points from the target projector.
A class to collect function parameters from a FunctionBase object but also the constraints and errors...
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
bool isInComposite() const
Returns true if receiving object is member of a composite.
double objectiveValue() const
Returns the objective value of the function to the target DataSource.
virtual void removeFromComposite(FunctionRep *frep)
Removes the function representation from composite if receiving object is a CompositeFunctionRep, otherwise does nothing.
return rep
Definition: Inspector.cxx:3843
void restoreParameters()
Restores the function&#39;s parameter values from the previously saved values.
virtual DataRep * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
Definition: FunctionRep.cxx:81
return yes
Definition: CanvasView.cxx:883
DataRep * getTarget() const
Returns the DataRep object upon which the function is applied.
The base class for fitters.
Definition: Fitter.h:33
const std::vector< double > & principleErrors() const
Returns the principle diagonal of the covariance matrix.
hippodraw::FunctionRep class interface
hippodraw::DataSource class interface.
const std::vector< double > & principleErrors() const
Returns the principle errors associated with the parameter values.
const std::vector< std::string > & parmNames() const
Returns the names of the parameters for the function represented by this object.
virtual const DataSource * getProjectedValues() const
Returns an DataSource representation of the projected data.
Definition: DataRep.cxx:270
void removeObserver(Observer *)
Removes an Observer from the Observer list.
Definition: Observable.cxx:66
bool ok
Definition: CanvasView.cxx:163
The base class for data representations.
Definition: DataRep.h:68
void setTupleCut()
Sets up the region over which fitting will be done.
virtual void addToComposite(FunctionRep *frep)
Adds the function representation to composite if receiving object is a CompositeFunctionRep, otherwise does nothing.
virtual bool isTargetable() const
Returns false.
void saveParameters()
Saves the function parameters in a cache so that they can be restored.
void setParameters(const std::vector< double > &params)
Set the parameter values by hand.
CutRangeRep * m_cut_rep
The representation of the cut or highlighted range.
Definition: DataRep.h:103
virtual void fillFunctionParameters(std::vector< FunctionParameter > &) const
Fills the vector with the parameters and constraints of the function.
bool hasCut() const
Returns true if the receiving object has one or more regional cuts.
Definition: DataRep.cxx:514
bool fitFunction()
Fits the function.
void setInComposite(bool yes)
Informs receiving object that it is a member of a composite.
const std::string & functionName() const
Returns the name of the contained function.
hippodraw::Fitter class interface
Fitter * getFitter() const
Returns the Fitter object currently in use.
virtual ~FunctionRep()
The virtual destructor.
Definition: FunctionRep.cxx:70
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
ProjectorBase * m_projector
A projector used to transform data to projected values that will be plotted.
Definition: DataRep.h:90
bool hasZoomY() const
Returns true.
hippodraw::TupleCut class interface
hippodraw::FunctionProjector class interface
FunctionBase * function() const
Returns a pointer to the contained function.
void willDelete(const Observable *observed)
Implements Observer pattern by removing the DataRep from its target.
void removeCut()
Remove a region cut on the data representation.
Definition: DataRep.cxx:476
void setPrincipleErrors(std::vector< double >::const_iterator begin, std::vector< double >::const_iterator end)
Sets the principle errors.
void setIgnoreError(bool ignore)
Set ignore errors.
int degreesOfFreedom() const
Returns the number of degrees of Freedom of the function fitting to the target DataSource.
DataRep * m_target
The target for the function.
Definition: FunctionRep.h:51
void addCut()
Adds a cut to the data representation.
Definition: DataRep.cxx:486
virtual void setDirty(bool yes=true)
Sets the dirty flag.
Definition: DataRep.cxx:133
Expresses a range of values.
Definition: Range.h:33
hippodraw::FunctionBase class interface
void removeCut()
Removes the cut region from the fitting.
void saveParameters()
Makes a copy of the function&#39;s parameter values.
bool isAxisBinned(Axes::Type axis) const
Returns true if the specified axis is binned, otherwise returns false.
Definition: DataRep.cxx:371
void addObserver(Observer *)
Adds an Observer to the Observer list.
Definition: Observable.cxx:52
void setLabel(const std::string &label)
Sets the label for the cut.
Definition: TupleCut.cxx:117
virtual ProjectorBase * getProjector() const
Returns the Projector object controlling the data.
Definition: DataRep.cxx:138
FunctionRep(FunctionBase *function, DataRep *rep)
The constructor taking the target DataRep object for the data.
Definition: FunctionRep.cxx:37
void setTarget(DataRep *rep)
Sets the target for the FunctionRep.
CutRangeRep class interface.
A projector that plots one function.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen