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 
37 FunctionRep ( FunctionBase * function,DataRep * rep )
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 
63 FunctionRep ( const FunctionRep & rep )
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
133 setInComposite ( bool yes )
134 {
135  m_in_composite = yes;
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 }
const std::string & functionName() const
Returns the name of the contained function.
const std::vector< std::vector< double > > & covarianceMatrix() const
Returns the covariance matrix of the function to the target DataSource.
hippodraw::FunctionBase class interface
void saveParameters()
Saves the function parameters in a cache so that they can be restored.
void setTupleCut()
Sets up the region over which fitting will be done.
void setRange(const Range &range)
Sets the range of the cut.
Definition: TupleCut.cxx:77
virtual void setFixedFlags(const std::vector< int > &flags)
Set the parameters that are to be held fixed during a fit.
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
bool hasCut() const
Returns true if the receiving object has one or more regional cuts.
Definition: DataRep.cxx:514
const std::string & getFitterName() const
Returns the name of the Fitter object.
bool getIgnoreErrors() const
Get ignore errors.
DataRep * getTarget() const
Returns the DataRep object upon which the function is applied.
void initializeFunction(ProjectorBase *projector)
Gives the function object initial values based on a given data projector.
virtual ~FunctionRep()
The virtual destructor.
Definition: FunctionRep.cxx:70
void willDelete(const Observable *observed)
Implements Observer pattern by removing the DataRep from its target.
virtual void removeFromComposite(FunctionRep *frep)
Removes the function representation from composite if receiving object is a CompositeFunctionRep, otherwise does nothing.
double objectiveValue() const
Returns the objective value of the function to the target DataSource.
void removeCut()
Removes the cut region from the fitting.
virtual void fillFunctionParameters(std::vector< FunctionParameter > &) const
Fills the vector with the parameters and constraints of the function.
bool isAxisBinned(Axes::Type axis) const
Returns true if the specified axis is binned, otherwise returns false.
Definition: DataRep.cxx:371
std::string m_name
The name of the display as it might be displayed in a GUI.
Definition: DataRep.h:76
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 PointRepBase class that draws a representation of a cut range. ...
Definition: CutRangeRep.h:29
void setParameters(const std::vector< double > &params)
Set the parameter values by hand.
bool m_in_composite
A flag which when set true indicates the receiving object is a member of a composite.
Definition: FunctionRep.h:43
bool hasZoomY() const
Returns true.
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
virtual const DataSource * getProjectedValues() const
Returns an DataSource representation of the projected data.
Definition: DataRep.cxx:270
const std::vector< double > & principleErrors() const
Returns the principle diagonal of the covariance matrix.
Namespace for HippoDraw.
Definition: AxesType.cxx:21
void restoreParameters()
Restores the saved parameters.
hippodraw::FunctionRep class interface
const std::vector< double > & principleErrors() const
Returns the principle errors associated with the parameter values.
The base class for data representations.
Definition: DataRep.h:68
Base class for DataSource.
Definition: DataSource.h:55
FunctionBase * function() const
Returns a pointer to the contained function.
ProjectorBase * m_projector
A projector used to transform data to projected values that will be plotted.
Definition: DataRep.h:90
bool isComposite() const
Returns true if represented function is a composite of two or more functions.
hippodraw::TupleCut class interface
void setCutRange(const Range &)
Sets the cut to limit range of fitting.
A class to collect function parameters from a FunctionBase object but also the constraints and errors...
FunctionBase * getFunction() const
Returns the actual function, a FunctionBase derived class.
void removeObserver(Observer *)
Removes an Observer from the Observer list.
Definition: Observable.cxx:66
const std::vector< double > & parameters() const
Returns the current function parameter values.
const std::string & name() const
Returns the name of the display.
Definition: DataRep.cxx:72
void setInComposite(bool yes)
Informs receiving object that it is a member of a composite.
void removeCut()
Remove a region cut on the data representation.
Definition: DataRep.cxx:476
Fitter * getFitter() const
Returns the Fitter object currently in use.
void setParameters(const std::vector< double > &params)
Set the parameter values by hand.
Expresses a range of values.
Definition: Range.h:33
bool fitFunction()
Fits the contained function to the data points from the target projector.
void setIgnoreError(bool ignore)
Set ignore errors.
const std::vector< int > & getFixedFlags() const
Returns a set of flags indicating which function parameters are fixed during a fit.
hippodraw::DataSource class interface.
FunctionParameter class interface.
void addObserver(Observer *)
Adds an Observer to the Observer list.
Definition: Observable.cxx:52
void setTarget(DataRep *rep)
Sets the target for the FunctionRep.
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
int degreesOfFreedom() const
Returns the number of degrees of Freedom of the function fitting to the target DataSource.
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
The base class for fitters.
Definition: Fitter.h:33
virtual ProjectorBase * getProjector() const
Returns the Projector object controlling the data.
Definition: DataRep.cxx:138
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.
bool isInComposite() const
Returns true if receiving object is member of a composite.
void addCut()
Adds a cut to the data representation.
Definition: DataRep.cxx:486
virtual bool hasNTupleBindings() const
Returns false as this class does not have DataSource bindings.
Definition: FunctionRep.cxx:86
const std::vector< std::string > & parmNames() const
Returns the names of the parameters for the function represented by this object.
CutRangeRep * m_cut_rep
The representation of the cut or highlighted range.
Definition: DataRep.h:103
CutRangeRep class interface.
virtual void setDirty(bool yes=true)
Sets the dirty flag.
Definition: DataRep.cxx:133
bool setFitter(Fitter *fitter)
Sets the Fitter to fitter.
void saveParameters()
Makes a copy of the function&#39;s parameter values.
void restoreParameters()
Restores the function&#39;s parameter values from the previously saved values.
hippodraw::FunctionProjector class interface
bool fitFunction()
Fits the function.
void setPrincipleErrors(std::vector< double >::const_iterator begin, std::vector< double >::const_iterator end)
Sets the principle errors.
DataRep * m_target
The target for the function.
Definition: FunctionRep.h:51
FunctionRep(FunctionBase *function, DataRep *rep)
The constructor taking the target DataRep object for the data.
Definition: FunctionRep.cxx:37
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
A projector that plots one function.
const std::vector< TupleCut > & getCuts() const
Returns a reference to the cut list.
Definition: DataRep.cxx:505
A derived class of DataRep which is a base class for displaying a function.
Definition: FunctionRep.h:34
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
void setLabel(const std::string &label)
Sets the label for the cut.
Definition: TupleCut.cxx:117
hippodraw::Fitter class interface

Generated for HippoDraw Class Library by doxygen