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 }

Generated for HippoDraw Class Library by doxygen