DisplayController.cxx
Go to the documentation of this file.
1 
13 // for cfitsio
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #else
17 
18 // for truncation warning in debug mode and version
19 #ifdef _MSC_VER
20 #include "msdevstudio/MSconfig.h"
21 #endif
22 
23 #endif // HAVE_CONFIG_H
24 
25 #include <iostream>
26 #include "DisplayController.h"
27 
28 #include "DataRepController.h"
29 #include "FunctionController.h"
30 
31 #include "axes/AxisModelLog.h"
32 #include "axes/AxisModelLinear.h"
33 
34 #include "binners/BinnerAxis.h"
36 
39 #include "datareps/FunctionRep.h"
40 #include "datareps/LineDataRep.h"
41 #include "datareps/TextDataRep.h"
42 #include "datareps/StripChart.h"
43 
45 #include "datasrcs/NTuple.h"
47 
48 #include "graphics/ViewBase.h"
49 #include "graphics/ViewFactory.h"
50 
53 #include "plotters/TextPlotter.h"
54 #include "plotters/XyPlotter.h"
55 
58 
63 #include "reps/ColumnPointRep.h"
64 #include "reps/LinePointRep.h"
65 #include "reps/PointRepFactory.h"
66 #include "reps/SymbolPointRep.h"
67 #include "reps/TextRepBase.h"
68 #include "reps/TextRepFactory.h"
69 
71 #include "transforms/XYTransform.h"
72 
73 #ifdef HAVE_CFITSIO
74 #include "fits/FitsController.h"
75 #endif
76 
77 #include <algorithm>
78 
79 #include <cassert>
80 
81 using std::list;
82 using std::string;
83 using std::swap;
84 using std::vector;
85 using std::find;
86 
87 using namespace hippodraw;
88 
90 
92 {
93 }
94 
96 {
98  delete controller;
99 }
100 
102 {
103  if ( s_instance == 0 ) {
104  s_instance = new DisplayController ( );
105  }
106  return s_instance;
107 }
108 
109 const vector < string > &
112 {
114 
115 return factory -> names ();
116 }
117 
118 void
120 setValueTransform ( PlotterBase * plotter, const std::string & name )
121 {
123  BinToColor * model = factory -> create ( name );
124 
125  plotter -> setValueRep ( model );
126 }
127 
128 void
130 setValueTransform ( PlotterBase * plotter, int index )
131 {
133  const vector < string > & names = factory -> names ();
134  BinToColor * rep = factory -> create ( names[index] );
135 
136  plotter -> setValueRep ( rep );
137 }
138 
139 void
141 addValueTransform ( const std::string & name,
142  const std::vector < double > & ctrl_points )
143 {
144  BinToColor * model = new BinToUsrDefinedScale ( name.c_str() );
145  model -> setControlPoints ( ctrl_points );
146 
148  factory -> add ( model );
149 }
150 
151 bool
153 removeValueTransform ( const std::string & name )
154 {
156  BinToColor * prototype = factory -> prototype ( name );
157  bool yes = prototype -> isUserDefined ();
158  if ( yes ) {
159  factory -> remove ( name );
160  }
161 
162  return yes;
163 }
164 
165 const vector <double > &
167 getValueCtrlPts ( const PlotterBase * plotter )
168 {
169  BinToColor * r = const_cast< BinToColor *> (plotter->getValueRep());
170  return r -> getControlPoints ();
171 }
172 
173 void
176  const std::vector < double > & sv)
177 {
178  BinToColor * r = const_cast< BinToColor *> (plotter->getValueRep());
179  if ( r != 0 ) {
180  r -> setControlPoints ( sv );
181  DataRep * datarep = plotter -> selectedDataRep ();
182  datarep -> notifyObservers ();
183  }
184 }
185 
186 void
189  const std::vector <double> & sv)
190 {
191  BinToColor * r = const_cast< BinToColor *> (plotter->getValueRep());
192  if ( r != 0 ) {
193  const std::string transformName = r->name();
194  removeValueTransform(transformName);
195  addValueTransform(transformName, sv);
196  }
197 }
198 
199 
200 int
203 {
204  int index = -1;
205  const BinToColor * rep = plotter -> getValueRep (); // return the value to color representation
206  if ( rep != 0 ) { // may not have one.
207  const string & name = rep -> name ();
208  const vector < string > & names = getValueTransformTypes ();
209  unsigned int size = names.size();
210  for ( unsigned int i = 0; i < size; i++ ) {
211  if ( name == names[i] ) {
212  index = i;
213  break;
214  }
215  }
216  }
217 
218  return index;
219 }
220 
221 bool
223 hasControlPoints ( const PlotterBase * plotter ) const
224 {
225  bool yes = false;
226  const BinToColor * rep = plotter -> getValueRep ();
227  if ( rep != 0 ) { // may not have one
228  yes = rep -> hasControlPoints ();
229  }
230 
231  return yes;
232 }
233 
234 bool
236 isUserDefinedValueTransform ( const PlotterBase * plotter ) const
237 {
238  bool yes = false;
239  const BinToColor * rep = plotter -> getValueRep ();
240  if ( rep != 0 ) {
241  yes = rep -> isUserDefined ();
242  }
243 
244  return yes;
245 }
246 
247 const vector < string > &
250 {
252 
253  return factory -> names ();
254 }
255 
256 PlotterBase *
258 createDisplay ( const std::string & name )
259 {
261  DataRep * rep = controller -> createDataRep ( name );
262 
263  return createDisplay ( rep );
264 }
265 
266 PlotterBase *
269 {
270  bool yes = rep -> hasNTupleBindings ();
271  if ( yes ) {
272  string what ( "DisplayController: data rep of type " );
273  what += rep -> name();
274  what +="\nhas NTuple bindings.\n";
275  what += "Can not create without them";
276  throw DataRepException ( what );
277  }
278 
279  PlotterBase * plotter = createPlotter ( rep );
280  plotter -> addDataRep ( rep );
281  plotter -> setAutoRanging ( true );
282 
283  return plotter;
284 }
285 
289 PlotterBase *
291 createDisplay ( const std::string & name,
292  const DataSource & tuple,
293  const std::vector< std::string > & bindings ) const
294 {
295  // Expansion needed by RootNTuple with multi-dimensional array
296  tuple.expandIfNeeded(bindings);
297 
299  DataRep * proto = factory->prototype ( name );
300  assert ( proto != 0 );
301 
302  PlotterBase * plotter = createPlotter ( proto );
303  addDataRep ( plotter, name, &tuple, bindings );
304 #ifdef HAVE_CFITSIO
305  if ( proto -> needsMatrixSet () ) {
306  FitsController * controller = FitsController::instance ();
307  controller -> checkForImage ( plotter, tuple );
308  }
309 #endif
310 
311  plotter -> autoScale ();
312 
313  return plotter;
314 }
315 
316 NTuple *
319 {
320  NTuple * ntuple = 0;
321  ntuple = plotter -> createNTuple ();
322  unsigned int size = ntuple -> rows ();
323 
324  vector < double > values ( size );
325  vector < double > residuals ( size );
326  DataRep * data1 = plotter -> getDataRep(0);
327  DataRep * data2 = plotter -> getDataRep(1);
328  NTuple * ntuple1 = data1 -> createNTuple();
329  NTuple * ntuple2 = data2 -> createNTuple();
330 
331  vector < double > & x1 = ntuple1 -> getColumn ( 0 ); // X coordinate
332  vector < double > & y1 = ntuple1 -> getColumn ( 1 ); // Y coordinate
333  vector < double > & deltaX1 = ntuple1 -> getColumn (2); // Half bin width
334 
335  vector < double > & x2 = ntuple2 -> getColumn ( 0 ); // X coordinate
336  vector < double > & y2 = ntuple2 -> getColumn ( 1 ); // Y coordinate
337  vector < double > & deltaX2 = ntuple2 -> getColumn (2); // Half bin width
338 
339  vector <double> xDiff;
340  vector <double> yDiff;
341  vector <double> deltaXDiff;
342  vector <double> deltaYDiff;
343 
344  unsigned int i = 0;
345  unsigned int j = 0;
346  while( i < x1.size() && j < x2.size() ) { // Compare of two histogram
347  if (x1[i] + deltaX1[i] == x2[j] + deltaX2[j]) {
348  if ( xDiff.empty()) {
349  xDiff.push_back(x1[i]);
350  yDiff.push_back(y2[j] - y1[i]);
351  deltaXDiff.push_back(deltaX1[i]);
352  i++;
353  j++;
354  continue;
355  } else {
356  double tmpX = xDiff.back();
357  double tmpDeltaX = deltaXDiff.back();
358  double xLeft = tmpX + tmpDeltaX;
359  double xRight = x1[i] + deltaX1[i];
360  xDiff.push_back(xLeft+0.5*(xRight - xLeft));
361  yDiff.push_back(y2[j] - y1[i]);
362  deltaXDiff.push_back(0.5*(xRight - xLeft));
363  i++;
364  j++;
365  continue;
366  }
367  }
368  else if (x1[i] + deltaX1[i] < x2[j] + deltaX2[j]) {
369  if ( xDiff.empty()) {
370  xDiff.push_back(x1[i]);
371  yDiff.push_back(y2[j] - y1[i]);
372  deltaXDiff.push_back(deltaX1[i]);
373  i++;
374  continue;
375  } else {
376  double tmpX = xDiff.back();
377  double tmpDeltaX = deltaXDiff.back();
378  double xLeft = tmpX + tmpDeltaX;
379  double xRight = x1[i] + deltaX1[i];
380  xDiff.push_back(xLeft+0.5*(xRight - xLeft));
381  yDiff.push_back(y2[j] - y1[i]);
382  deltaXDiff.push_back(0.5*(xRight - xLeft));
383  i++;
384  continue;
385  }
386  } else {
387  if ( xDiff.empty() ) {
388  xDiff.push_back(x2[j]);
389  yDiff.push_back(y2[j] - y1[i]);
390  deltaXDiff.push_back(deltaX2[j]);
391  j++;
392  continue;
393  } else {
394  double tmpX = xDiff.back();
395  double tmpDeltaX = deltaXDiff.back();
396  double xLeft = tmpX + tmpDeltaX;
397  double xRight = x2[j] + deltaX2[j];
398  xDiff.push_back(xLeft+0.5*(xRight - xLeft));
399  yDiff.push_back(y2[j] - y1[i]);
400  deltaXDiff.push_back(0.5*(xRight - xLeft));
401  j++;
402  continue;
403  }
404  }
405  }
406 
407  NTuple * newNtuple = new NTuple();
408  newNtuple->addColumn ( "X", xDiff );
409  newNtuple->addColumn ( "Residuals", yDiff );
410  newNtuple->addColumn ( "DeltaX", deltaXDiff );
411 
412  return newNtuple;
413 }
414 
415 PlotterBase *
418 {
419  NTuple * ntuple = createNTupleDiff ( plotter );
420  int size = ntuple->rows();
421 
422  ntuple -> setTitle ( plotter -> getTitle () );
423 
424  DataSourceController::instance () -> registerNTuple ( ntuple );
425 
426  vector < string > bindings ( 3 );
427 
428  bindings[0] = ntuple -> getLabelAt ( 0 );
429  bindings[1] = ntuple -> getLabelAt ( 1 );
430  bindings[2] = ntuple -> getLabelAt ( 2 );
431 
433 
434 // Make scaling of x-axis match that of the original plot.
435  PlotterBase * new_plotter = controller -> createDisplay ( "XY Plot",
436  *ntuple,
437  bindings );
438  controller->setLog( new_plotter, "x",
439  controller -> getLog( plotter, "x" ) );
440 
441  return new_plotter;
442 }
443 
444 
446 {
448  PlotterBase * plotter = 0;
449  try {
450  plotter = factory -> create ( "XyPlotter" );
451  if ( rep -> hasAxis ( Axes::Z ) ) {
452  plotter -> setEnableZ ( true );
453  }
454  }
455  catch ( const FactoryException & ) {
456  assert ( false );
457  }
458 return plotter;
459 }
460 
463 DataRep *
466  const std::string & type,
467  const DataSource * tuple,
468  const std::vector < std::string > & bindings ) const
469 {
470  bool yes = isCompatible ( plotter, type );
471  if ( ! yes ) return 0;
472 
474  DataRep * rep = controller->createDataRep ( type, tuple, bindings );
475  addDataRep ( plotter, rep );
476 
477  return rep;
478 }
479 
480 void
482 stackDataRep ( PlotterBase * plotter, DataRep * rep ) const
483 {
484  XyPlotter * xyplotter = dynamic_cast < XyPlotter * > ( plotter );
485  xyplotter -> addDataRepStacked ( rep );
486 }
487 
488 DataRep *
491  const std::string & type,
492  const DataSource * tuple,
493  const std::vector < std::string > & bindings ) const
494 {
495  bool yes = isCompatible ( plotter, type );
496  if ( ! yes ) return 0;
497 
499  DataRep * rep = controller->createDataRep ( type, tuple, bindings );
500 
501  stackDataRep ( plotter, rep );
502 
503  return rep;
504 }
505 
506 LineDataRep *
508 addLineRep ( PlotterBase * plotter, hippodraw::Axes::Type axis, double value )
509 {
510  LineDataRep * rep = new LineDataRep ( axis, value );
511  addDataRep ( plotter, rep );
512 
513  return rep;
514 }
515 
516 LineDataRep *
518 addLineRep ( PlotterBase * plotter, const std::string & axis, double value )
519 {
520  Axes::Type type = Axes::convert ( axis );
521 
522  return addLineRep ( plotter, type, value );
523 }
524 
525 void
527 fixLogIfBinned ( const PlotterBase * plotter, DataRep * rep ) const
528 {
529  setBinner ( plotter, rep, Axes::X );
530  setBinner ( plotter, rep, Axes::Y );
531 }
532 
536 void
538 addDataRep ( PlotterBase * plotter, DataRep * rep ) const
539 {
540  if ( plotter -> getNumDataReps () > 0 ) {
541  fixLogIfBinned ( plotter, rep );
542  }
543 
544  plotter->addDataRep ( rep );
545 
546  if ( !rep->hasZeroRows() ){
547  plotter->checkAxisScaling ( );
548  }
549 
550 }
551 
554 bool
556 isCompatible ( const PlotterBase * plotter,
557  const std::string & datarep ) const
558 {
560  DataRep * proto = factory->prototype ( datarep );
561 
562  bool yes = true;
563  // does it need Z axis display
564  if ( proto -> hasAxis ( Axes::Z ) ) {
565  yes = plotter->hasAxis ( Axes::Z );
566  }
567 
568  return yes;
569 }
570 
571 void
573 removeTextObservers ( const std::vector < const ViewBase * > & views )
574 {
575 #ifdef ITERATOR_MEMBER_DEFECT
576  std::
577 #endif
578  vector < const ViewBase * > ::const_iterator iter = views.begin();
579 
580  while ( iter != views.end() ) {
581 
582  // If its a text view, tell the target DataRep (observable) to remove
583  // this->plotter from its list of observers.
584 
585  const ViewBase * curview = ( * iter++ );
586  PlotterBase * curplotter = curview->getPlotter ();
587 
588  TextPlotter * textPlotter
589  = dynamic_cast < TextPlotter * > ( curplotter );
590 
591  if ( textPlotter ){
592  const DataRep * target = textPlotter->getParentDataRep();
593  if ( target ){
594  DataRep * tar = const_cast < DataRep * > ( target );
595  tar->removeObserver ( textPlotter );
596  }
597  }
598  }
599 }
600 
601 void
603 addTextViewToList ( std::vector < const ViewBase * > & dest,
604  const std::vector < const ViewBase * > & src )
605 {
606  std::vector < const ViewBase * > ::const_iterator first = src.begin();
607  while ( first != src.end() ) {
608  const ViewBase * view = *first++;
609 
610  vector < const ViewBase * > :: iterator i
611  = find ( dest.begin(), dest.end (), view );
612  if ( i != dest.end() ) continue; // already in list
613 
614  PlotterBase * plotter = view->getPlotter ();
615  TextPlotter * text_plotter = dynamic_cast < TextPlotter * > ( plotter );
616  if ( text_plotter == 0 ) continue; // not a text plotter
617 
618  const DataRep * datarep = text_plotter->getParentDataRep ();
619  bool add_to_list = false;
620  for ( i = dest.begin (); i != dest.end(); ++i ) {
621  const ViewBase * view = *i;
622  PlotterBase * plotter = view->getPlotter ();
623  if ( plotter == 0 ) continue; // view has no plotter, can be groupview
624  int num = plotter->getNumDataReps ();
625  for ( int j = 0; j < num; j++ ) {
626  DataRep * rep = plotter->getDataRep ( j );
627  if ( rep == datarep ) {
628  add_to_list = true;
629  break;
630  }
631  }
632  if ( add_to_list ) break;
633  }
634  if ( add_to_list ) dest.push_back ( view );
635  }
636 }
637 
638 bool
641  const std::string & axis )
642 {
643  bool yes = false;
644 
645  int index = plotter->activePlotIndex ( );
646  if ( index < 0 ) {
648  index = controller->getUniqueNonFunctionIndex ( plotter );
649  }
650  if ( index >= 0 ) {
651  ProjectorBase * projector = plotter->activeProjector ();
652  yes = projector->isAxisBinned ( axis );
653  }
654 
655  return yes;
656 }
657 
659  PlotterBase * plotter,
660  const std::string & name,
661  const std::string & text)
662 {
663  int index = activeDataRepIndex ( plotter );
664  DataRep * datarep = plotter->getDataRep ( index );
665  ViewBase * view = createTextView ( v_factory, datarep, name, text );
666  assert ( view != 0 );
667 
668  // Set the parent plotter of the text plotter.
669  view->getPlotter()->setParentPlotter(plotter);
670 
671  return view;
672 }
673 
674 const vector < string > &
676 getTextTypes () const
677 {
678  TextRepFactory * text_factory = TextRepFactory::instance ();
679 
680  return text_factory->names ();
681 }
682 
683 DataRep * DisplayController::createTextDataRep ( const std::string & name,
684  const std::string & text)
685 {
686  TextRepFactory * text_factory = TextRepFactory::instance ();
687  RepBase * textrep = text_factory->create ( name );
688  assert ( textrep != 0 );
689 
690  textrep->setText ( text );
691  DataRep * datarep = new TextDataRep ( textrep );
692 
693  return datarep;
694 }
695 
696 PlotterBase *
698  const std::string & name,
699  const std::string & text)
700 {
701  DataRep * textdatarep = createTextDataRep ( name, text );
702  TextPlotter * plotter = new TextPlotter ( );
703  plotter->addDataRep ( textdatarep );
704  plotter->setParentDataRep ( datarep );
705 
706  return plotter;
707 }
708 
710  DataRep * datarep,
711  const std::string & name,
712  const std::string & text)
713 {
714  PlotterBase * plotter = createTextPlotter ( datarep, name, text );
715 
716  return v_factory->createView ( plotter );
717 }
718 
720 activeDataRepIndex ( const PlotterBase * plotter ) const
721 {
722  int index = plotter->activePlotIndex ();
723  if ( index < 0 ) {
725  index = controller->getUniqueNonFunctionIndex ( plotter );
726  }
727  return index;
728 }
729 
730 DataRep *
732 activeDataRep ( const PlotterBase * plotter ) const
733 {
734  DataRep * rep = 0;
735  int index = activeDataRepIndex ( plotter );
736  if ( index >= 0 ) {
737  rep = plotter -> getDataRep ( index );
738  }
739 
740  return rep;
741 }
742 
743 void
745 setActiveDataRepIndex ( PlotterBase * plotter, int index )
746 {
747  plotter -> setActivePlot ( index, true );
748 }
749 
750 const vector < string > &
752 axisBindings ( const PlotterBase * plotter ) const
753 {
754  // This only make sense for a NTupleProjector, so we downcast
755  NTupleProjector * ntProjector =
756  dynamic_cast <NTupleProjector *> ( plotter->activeProjector() );
757 
758  if ( ntProjector == 0 ) return m_null_vector;
759 
760  return ntProjector->getAxisBindings ();
761 }
762 
763 const vector < string > &
765  int index )
766 {
767  DataRep * rep = plotter->getDataRep ( index );
769 
770  return controller->bindingOptions ( rep );
771 }
772 
773 const vector < string > &
775 bindingOptions ( const std::string & type )
776 {
778  DataRep * rep = factory -> prototype ( type );
780 
781  return controller -> bindingOptions ( rep );
782 }
783 
784 const vector < string > &
786 axisBindings ( const PlotterBase * plotter, int index ) const
787 {
788  assert ( index < plotter->getNumDataReps () );
789 
790  ProjectorBase * pbase = plotter->getProjector ( index );
791  // This only make sense for a NTupleProjector, so we downcast
792  NTupleProjector * ntProjector =
793  dynamic_cast <NTupleProjector *> ( pbase );
794 
795  if ( ntProjector == 0 ) return m_null_vector;
796 
797  return ntProjector->getAxisBindings ();
798 }
799 
800 int
802 getNumberOfEntries ( const PlotterBase * plotter, int index ) const
803 {
804  assert ( index < plotter->getNumDataReps () );
805 
806  ProjectorBase * projector = plotter -> getProjector ( index );
807 
808  return projector -> getNumberOfEntries ();
809 }
810 
811 int
813 getUnderflow ( const PlotterBase * plotter, int index ) const
814 {
815  assert ( index < plotter->getNumDataReps () );
816  ProjectorBase * projector = plotter -> getProjector ( index );
817  return projector -> getUnderflow ();
818 }
819 
820 int
822 getOverflow ( const PlotterBase * plotter, int index ) const
823 {
824  assert ( index < plotter->getNumDataReps () );
825  ProjectorBase * projector = plotter -> getProjector ( index );
826  return projector -> getOverflow ();
827 }
828 
829 double
831 getAverage ( const PlotterBase * plotter,
832  const std::string & axis, int index ) const
833 {
834  Axes::Type a = Axes::convert ( axis );
835 
836  return getAverage ( plotter, a, index );
837 }
838 
839 double
841 getAverage ( const PlotterBase * plotter,
843  int index ) const
844 {
845  assert ( index < plotter -> getNumDataReps () );
846 
847  ProjectorBase * projector = plotter -> getProjector ( index );
848 
849  return projector -> getAverage ( axis );
850 }
851 
852 double
854 getRMS ( const PlotterBase * plotter,
855  const std::string & axis, int index ) const
856 {
857  Axes::Type a = Axes::convert ( axis );
858 
859  return getRMS ( plotter, a, index );
860 }
861 
862 double
864 getRMS ( const PlotterBase * plotter,
866  int index ) const
867 {
868  assert ( index < plotter -> getNumDataReps () );
869 
870  ProjectorBase * projector = plotter -> getProjector ( index );
871 
872  return projector -> getRMS ( axis );
873 }
874 
875 const string & DisplayController::getType ( const PlotterBase * plotter,
876  int index ) const
877 {
878  DataRep * datarep = plotter->getDataRep ( index );
879 
880  return datarep->name ();
881 }
882 
883 ProjectorBase *
885 {
886  DataRep * datarep = plotter->selectedDataRep ();
887 
888  if ( datarep == 0 ) return 0;
889 
890  return datarep->getProjector ();
891 }
892 
898 {
899  ProjectorBase * projbase = getProjector ( plotter );
900  if ( projbase != 0 ) {
901  NTupleProjector * projector
902  = dynamic_cast < NTupleProjector * > ( projbase );
903  return projector;
904  }
905  // Failure of the above is because multiple DataRep objects are
906  // selected. However, if the the additional ones are all
907  // FunctionRep objects, then we want to find the single DataRep
908  // object.
909  int number = plotter->getNumDataReps ();
910  for ( int i = 0; i < number; i++ ) {
911  DataRep * rep = plotter->getDataRep ( i );
912  FunctionRep * frep = dynamic_cast < FunctionRep * > ( rep );
913  if ( frep != 0 ) continue;
914  if ( projbase == 0 ) {
915  projbase = rep->getProjector ();
916  }
917  else {
918  return 0; // multiple non- FunctionRep objects found
919  }
920  }
921  NTupleProjector * projector
922  = dynamic_cast < NTupleProjector * > ( projbase );
923 
924  return projector;
925 }
926 
928  const NTuple * ntuple ) const
929 {
930  NTupleProjector * projector = getBindingProjector ( plotter );
931  if ( projector == 0 ) return;
932 
933  projector->setNTuple ( ntuple );
934  NTuple * nt = const_cast < NTuple * > ( ntuple );
935  nt->addObserver ( projector );
936  DataRep * datarep = plotter->selectedDataRep ();
937 
938  datarep->notifyObservers ();
939 }
940 DataSource *
942 getDataSource ( const PlotterBase * plotter )
943 {
944  DataSource * source = 0;
945  int index = activeDataRepIndex ( plotter );
946  int numDataReps = plotter -> getNumDataReps ();
947  if ( index >= 0 || numDataReps < 2 ) {
948  source = getDataSource ( plotter, 0 );
949  }
950  else {
951  source = getDataSource ( plotter, 0 );
952  for ( int i = 1; i< numDataReps; i++ ) {
953  const DataSource * nt = getDataSource ( plotter, i );
954  if ( source != nt ) {
955  source = 0;
956  break;
957  }
958  }
959  }
960  return source;
961 }
962 
963 DataSource *
965 getDataSource ( const PlotterBase * plotter, int index )
966 {
967  DataSource * source = 0;
968 
969  DataRep * rep = plotter->getDataRep ( index );
970  if ( rep->hasNTupleBindings () ) {
971  ProjectorBase * pbase = rep->getProjector ();
972  NTupleProjector * projector = dynamic_cast < NTupleProjector * > ( pbase );
973  if ( projector != 0 ) {
974  source = projector -> getNTuple ();
975  }
976  }
977 
978  return source;
979 }
980 
981 void
983 setIntervalCount ( const PlotterBase * plotter, unsigned int count )
984 {
985  int i = activeDataRepIndex ( plotter );
986  if ( i < 0 ) return;
987  DataSource * ds = getDataSource ( plotter, i );
988  NTuple * ntuple = dynamic_cast < NTuple * > ( ds );
989 
990  if ( ntuple != 0 ) ntuple->setIntervalCount ( count );
991 }
992 
993 void
995 setIntervalEnabled ( const PlotterBase * plotter, bool yes )
996 {
997  int i = activeDataRepIndex ( plotter );
998  if ( i < 0 ) return;
999  DataSource * ds = getDataSource ( plotter, i );
1000  NTuple * ntuple = dynamic_cast < NTuple * > ( ds );
1001 
1002  if ( ntuple != 0 ) ntuple->setIntervalEnabled ( yes );
1003 }
1004 
1005 void
1007 setAllIntervalEnabled ( const PlotterBase * plotter, bool yes )
1008 {
1009  int number = plotter->getNumDataReps ();
1010  for ( int i = 0; i < number; i++ ) {
1011  DataSource * ds = getDataSource ( plotter, i );
1012  NTuple * ntuple = dynamic_cast < NTuple * > ( ds );
1013  if ( ntuple != 0 ) ntuple->setIntervalEnabled ( yes );
1014  }
1015 }
1016 
1017 bool
1020 {
1021  bool yes = true;
1023 
1024  int number = plotter->getNumDataReps ();
1025  for ( int i = 0; i < number; i++ ) {
1026  const DataSource * source = getDataSource ( plotter, i );
1027  if ( source != 0 ) {
1028  yes = controller -> isFromFile ( source );
1029  }
1030  if ( yes == false ) break;
1031  }
1032 
1033  return yes;
1034 }
1035 
1037  int index )
1038 {
1039  assert ( ! ( index < 0 ) );
1040  DataRep * rep = plotter->getDataRep ( index );
1041 
1042  return rep->hasNTupleBindings ();
1043 }
1044 
1045 const string &
1047 getDataSourceName ( const PlotterBase * plotter,
1048  int index )
1049 {
1050  const DataSource * source = getDataSource ( plotter, index );
1051 
1052  return source -> getName ();
1053 }
1054 
1055 const vector < string > &
1057  int index )
1058 {
1059  DataRep * datarep = plotter->getDataRep ( index );
1060  ProjectorBase * pbase = datarep->getProjector ();
1061  NTupleProjector * projector = dynamic_cast < NTupleProjector * > ( pbase );
1062  if ( projector == 0 ) return m_null_vector;
1063 
1064  const DataSource * tuple = projector->getNTuple ();
1065 
1066  return tuple->getLabels ();
1067 }
1068 
1069 DataRep *
1072 {
1073  DataRep * rep = 0;
1074  int active = plotter -> activePlotIndex ();
1075 
1076  if ( active < 0 ) { // multiple active
1077  int count = 0;
1078  int number = plotter -> getNumDataReps ();
1079  for ( int i = 0; i < number; i++ ) {
1080  DataRep * trep = plotter -> getDataRep ( i );
1081  if ( trep -> hasNTupleBindings () ) {
1082  if ( count == 0 ) {
1083  rep = trep;
1084  count ++;
1085  }
1086  else {
1087  rep = 0;
1088  break;
1089  }
1090  }
1091  }
1092  }
1093  else { // only one active
1094  rep = plotter ->getDataRep ( active );
1095  if ( rep ->hasNTupleBindings () == false ) {
1096  rep = 0;
1097  }
1098  }
1099 
1100  return rep;
1101 }
1102 
1104  const std::string & axis,
1105  const std::string & label )
1106 {
1107  DataRep * rep = plotter -> getTarget ();
1108 
1109  if ( rep != 0 ) {
1110  rep -> setAxisBinding ( axis, label );
1111  Axes::Type a = Axes::X;
1112  if ( axis[0] == 'Y' ) a = Axes::Y;
1113  plotter -> setAutoRanging ( a, true );
1114  plotter -> autoScale ();
1115  }
1116 }
1117 
1120  const std::vector< std::string > & labels ) const
1121 {
1122  NTupleProjector * projector = getBindingProjector ( plotter );
1123  if ( projector == 0 ) return;
1124 
1125  projector->setAxisBindings ( labels );
1126  plotter->setAutoRanging ( true );
1127 
1128 }
1129 
1130 bool
1132 isDataValid ( const PlotterBase * plotter ) const
1133 {
1134  bool valid = true;
1135  const DataRep * rep = plotter -> getTarget ();
1136  if ( rep != 0 ) {
1137  valid = isDataValid ( rep );
1138  }
1139 
1140  return valid;
1141 }
1142 
1143 bool
1145 isDataValid ( const DataRep * rep ) const
1146 {
1147  bool valid = true;
1148  ProjectorBase * base = rep -> getProjector ();
1149  const NTupleProjector * projector
1150  = dynamic_cast < const NTupleProjector * > ( base );
1151  if ( projector != 0 ) {
1152  valid = projector -> isDataValid ();
1153  }
1154 
1155  return valid;
1156 }
1157 
1158 bool
1160 getLog ( const PlotterBase * plotter,
1161  const std::string & axis) const
1162 {
1163  Axes::Type at = Axes::convert ( axis );
1164  return getLog ( plotter, at );
1165 }
1166 
1168 bool
1170 getLog ( const PlotterBase * plotter, hippodraw::Axes::Type axis ) const
1171 {
1172  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
1173 
1174  XYTransform * xy_transform
1175  = dynamic_cast < XYTransform *> ( plotter->getTransform () );
1176 
1177  if ( xy_transform == 0 ) return false;
1178 
1179  TransformBase * transform = 0;
1180 
1181  if ( axis == Axes::X ){
1182  transform = xy_transform->xTransform ();
1183  }
1184  if ( axis == Axes::Y ){
1185  transform = xy_transform->yTransform();
1186  }
1187  if ( axis == Axes::Z ){
1188  transform = xy_transform->zTransform();
1189  }
1190 
1191  assert ( transform );
1192 
1193  const string & name = transform->name ();
1194 
1195  return name == "Log";
1196 }
1197 void
1199 setBinner ( const PlotterBase * plotter,
1200  DataRep * datarep,
1201  hippodraw::Axes::Type axis ) const
1202 {
1203  ProjectorBase * projbase = datarep->getProjector ();
1204 
1205  BinningProjector * projector
1206  = dynamic_cast< BinningProjector * > ( projbase );
1207  if ( projector == 0 ) return;
1208 
1209  if ( axis == Axes::Y &&
1210  projector->isAxisBinned ( "Y" ) == false ) return;
1211 
1212  const Range & range = plotter -> getRange ( axis, false );
1213  BinnerAxis * binner = 0;
1214 
1215  BinnerAxisFactory * binner_factory = BinnerAxisFactory::instance ();
1216  AxisModelBase * model = plotter -> getAxisModel ( axis );
1217 
1218  if ( model->isLog() ) {
1219  binner = binner_factory -> create ( "BinnerLog" );
1220  } else { // not Log
1221  binner = binner_factory -> create ( "BinnerLinear" );
1222  }
1223 
1224  binner->setRange ( range, false );
1225  projector->setBinnerOn ( binner, axis );
1226 }
1227 
1231 void
1233 setBinner ( PlotterBase * plotter,
1234  hippodraw::Axes::Type axis )
1235 {
1236  int number = plotter->getNumDataReps ();
1237  for ( int i = 0; i < number; i++ ) {
1238  DataRep * datarep = plotter->getDataRep ( i );
1239  setBinner ( plotter, datarep, axis );
1240  }
1241 
1242 }
1243 
1244 void
1247  hippodraw::Axes::Type axis,
1248  const std::string & type )
1249 {
1250  AxisModelBase * model = plotter->getAxisModel ( axis );
1251 
1252  if ( model != 0 ) {
1253  AxisModelBase * temp = 0;
1254 
1255  if ( type == "Linear" ) {
1256 
1257  if ( !( model->isLog() ) ) return; // no change needed
1258  temp = new AxisModelLinear ( *model );
1259 
1260  }
1261  else if ( type == "Log" ) {
1262 
1263  if ( model->isLog () ) return; // no change needed
1264  const Range & range = plotter -> getRange ( axis, false );
1265  if ( range.high() < 0. ) {
1266  string what ( "Can not change axis to log scale since current "
1267  "range is negative" );
1268  throw std::runtime_error ( what );
1269  }
1270 
1271  double pos = plotter->getPosRange ( axis );
1272  model->setRangePos ( pos );
1273  temp = new AxisModelLog ( *model );
1274 
1275  }
1276 
1277  assert ( temp );
1278 
1279  swap ( temp, model );
1280 
1281  plotter->setAxisModel ( model, axis );
1282 
1283  setBinner ( plotter, axis );
1284 
1285  // Do not delete until the end so project can use it.
1286  delete temp;
1287  }
1288 }
1289 
1290 void
1292 setLog ( PlotterBase * plotter,
1293  const std::string & axis,
1294  bool flag )
1295 {
1296  Axes::Type at = Axes::convert ( axis );
1297  setLog ( plotter, at, flag );
1298 }
1299 
1301 void
1303 setLog ( PlotterBase * plotter,
1304  hippodraw::Axes::Type axis,
1305  bool flag )
1306 {
1307  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
1308 
1309  TransformBase * current = plotter->getTransform ();
1310 
1311  XYTransform * xy_transform = dynamic_cast < XYTransform *> ( current );
1312 
1313  if ( xy_transform != 0 ) {
1314  setLog ( plotter, axis, flag, xy_transform );
1315  return;
1316  }
1317  assert ( axis == Axes::Z );
1318  BinaryTransform * b_transform
1319  = dynamic_cast < BinaryTransform * > ( current );
1320  string type;
1321  if ( flag ) type = "Log";
1322  else type = "Linear";
1323 
1324  setAxisModel ( plotter, Axes::Z , type );
1325 
1327  TransformBase * transform = factory -> createTransform ( type );
1328 
1329  b_transform -> setZTransform ( transform );
1330  plotter -> setTransform ( b_transform );
1331 }
1332 
1333 void
1335 setLog ( PlotterBase * plotter,
1336  hippodraw::Axes::Type axis,
1337  bool flag,
1338  XYTransform * xy_transform )
1339 {
1340  TransformBase * transform = xy_transform->xTransform ();
1341 
1342  string x_trans = transform->name ();
1343  transform = xy_transform->yTransform ();
1344  string y_trans = transform->name ();
1345  transform = xy_transform->zTransform ();
1346  string z_trans = transform->name ();
1347 
1348  if( flag ){ //if the axis is to be made logarithmic
1349  if( axis == Axes::X ){
1350  x_trans = "Log";
1351  setAxisModel ( plotter, Axes::X, x_trans );
1352  }
1353  if( axis == Axes::Y ){
1354  y_trans = "Log";
1355  setAxisModel ( plotter, Axes::Y, y_trans );
1356  }
1357  if( axis == Axes::Z ){
1358  z_trans = "Log";
1359  setAxisModel ( plotter, Axes::Z, z_trans );
1360  }
1361  }
1362 
1363  if( !flag ){
1364  if( axis == Axes::X ) {
1365  x_trans = "Linear";
1366  setAxisModel ( plotter, Axes::X, x_trans );
1367  }
1368  if( axis == Axes::Y ) {
1369  y_trans = "Linear";
1370  setAxisModel ( plotter, Axes::Y , y_trans );
1371  }
1372  if( axis == Axes::Z ) {
1373  z_trans = "Linear";
1374  setAxisModel ( plotter, Axes::Z , z_trans );
1375  }
1376  }
1377 
1379 
1380 // The following verbosness for VC++ in debug mode.
1381  string t_name = x_trans;
1382  t_name += " ";
1383  t_name += y_trans;
1384  t_name += " ";
1385  t_name += z_trans;
1386  transform = factory->createTransform ( t_name );
1387 
1388  plotter->setTransform ( transform );
1389  delete transform; // The plotter made a copy and deleted the old one.
1390 }
1391 
1392 
1394  const std::string & name )
1395 {
1397  TransformBase * transform = factory->createTransform ( name );
1398 
1399  try {
1400  plotter->setTransform ( transform );
1401  delete transform; // The plotter made a copy.
1402  } catch (PlotterException & eObj) {
1403  delete transform; // not used or needed
1404  throw eObj;
1405  }
1406 }
1407 
1409  const std::string & x,
1410  const std::string & y )
1411 {
1413  const string name = x + " " + y;
1414  TransformBase * transform = factory->createTransform ( name );
1415 
1416  plotter->setTransform ( transform );
1417 
1418  delete transform; // The plotter made a copy.
1419 }
1420 
1422 {
1423  delete plotter;
1424 }
1425 
1426 float DisplayController::pointSize ( const PlotterBase * plotter ) const
1427 {
1428  RepBase * rep = plotter->representation ();
1429 
1430  return rep->size ();
1431 }
1432 
1433 bool
1435 hasSymbolRep ( const PlotterBase * plotter ) const
1436 {
1437  RepBase * rep = plotter -> representation ();
1438  SymbolPointRep * symrep = dynamic_cast < SymbolPointRep * > ( rep );
1439 
1440  return symrep != 0;
1441 }
1442 
1443 bool
1445 hasLineRep ( const PlotterBase * plotter ) const
1446 {
1447  bool yes = false;
1448 
1449  RepBase * rep = plotter -> representation ();
1450  ColumnPointRep * colrep = dynamic_cast < ColumnPointRep * > ( rep );
1451 
1452  if ( colrep != 0 ) {
1453  yes = true;
1454  }
1455  else {
1456  LinePointRep * linerep = dynamic_cast < LinePointRep * > ( rep );
1457  yes = linerep != 0;
1458  }
1459 
1460  return yes;
1461 }
1462 
1463 unsigned int
1465 getRepStyle ( const PlotterBase * plotter ) const
1466 {
1467  RepBase * rep = plotter -> representation ();
1468 
1469  return rep -> getStyle ();
1470 }
1471 
1473 {
1474  DataRep * datarep = plotter->selectedDataRep ();
1475 
1476  datarep->setRepSize ( size );
1477 }
1478 
1479 void
1481 setPointRep ( PlotterBase * plotter, const std::string & point_rep )
1482 {
1483  int index = activeDataRepIndex ( plotter );
1484  if ( index >= 0 ) {
1485  DataRep * datarep = plotter -> getDataRep ( index );
1486  assert ( datarep );
1488  RepBase * rep = factory -> create ( point_rep );
1489  datarep -> setPointRep ( rep );
1490  }
1491 }
1492 
1493 void
1495 setRange ( PlotterBase * plotter,
1496  hippodraw::Axes::Type axis,
1497  const Range & range )
1498 {
1499  plotter->setRange ( axis, range );
1500 
1501  plotter->setAutoRanging ( axis, false );
1502 }
1503 
1504 void
1507  const std::string & axis,
1508  double width )
1509 {
1510  Axes::Type at = Axes::convert ( axis );
1511 
1512  setBinWidth ( plotter, at, width );
1513 }
1514 
1515 void
1517 setBinWidth ( DataRep * datarep,
1518  hippodraw::Axes::Type axis,
1519  double width )
1520 {
1521  const list < Observer * > & obs_list = datarep -> getObservers ();
1522  list < Observer * >::const_iterator first = obs_list.begin();
1523  while ( first != obs_list.end () ) {
1524  Observer * obs = *first++;
1525  PlotterBase * plotter = dynamic_cast < PlotterBase * > ( obs );
1526  if ( plotter != 0 ) {
1527  plotter -> autoScale ();
1528  datarep -> setBinWidth ( axis, width );
1529  plotter -> setAutoRanging ( axis, false );
1530  break;
1531  }
1532  }
1533 }
1534 
1535 void
1538  hippodraw::Axes::Type axis,
1539  double width )
1540 {
1541  plotter -> setBinWidth ( axis, width );
1542  plotter->setAutoRanging ( axis, false );
1543 }
1544 
1549 void
1552  const std::string & axis,
1553  int parm,
1554  bool dragging )
1555 {
1556  bool widths_are_same = false;
1557 
1558  Axes::Type at = Axes::X;
1559  if ( axis == "Y" ) at = Axes::Y;
1560  if ( axis == "Z" ) at = Axes::Z;
1561  plotter->setAutoRanging ( at, false );
1562 
1563  int index = plotter->activePlotIndex ();
1564 
1565  if ( index < 0 ) {
1566  int number = plotter->getNumDataReps ();
1567  for ( int i = 0; i < number; i++ ) {
1568  ProjectorBase * projector = plotter->getProjector ( i );
1569  projector->setOffset ( axis, parm, dragging );
1570  }
1571 
1572  widths_are_same = true;
1573  }
1574  else {
1575  ProjectorBase * projector = plotter->getProjector ( index );
1576  projector->setOffset ( axis, parm, dragging );
1577  }
1578 
1579 }
1580 
1581 void
1584  hippodraw::Axes::Type axis,
1585  double offset )
1586 {
1587  bool widths_are_same = false;
1588 
1589  int index = plotter->activePlotIndex ();
1590 
1591  if ( index < 0 ) {
1592  int number = plotter->getNumDataReps ();
1593  for ( int i = 0; i < number; i++ ) {
1594  ProjectorBase * projector = plotter->getProjector ( i );
1595  projector->setOffset ( axis, offset );
1596  }
1597 
1598  widths_are_same = true;
1599  }
1600  else {
1601  ProjectorBase * projector = plotter->getProjector ( index );
1602  projector->setOffset ( axis, offset );
1603  }
1604 
1605 }
1606 
1607 void
1609 setErrorDisplayed ( const PlotterBase * plotter,
1610  hippodraw::Axes::Type axis,
1611  bool state ) const
1612 {
1613  int index = activeDataRepIndex ( plotter );
1614  if ( index >= 0 ) {
1615  DataRep * rep = plotter -> getDataRep ( index );
1616  rep -> setErrorDisplay ( axis, state );
1617  }
1618 }
1619 
1620 void
1622 createNTuple ( const PlotterBase * plotter )
1623 {
1624  int index = plotter -> activePlotIndex ();
1625 // if ( index < 0 ) return;
1626 
1627  NTuple * ntuple = plotter -> createNTuple ();
1628  DataSourceController::instance () -> registerNTuple ( ntuple );
1629 }
1630 
1631 void
1634  const std::string & x,
1635  const std::string & y)
1636 {
1637  setAxisModelWithoutSetBin ( plotter, Axes::X, x );
1638  setAxisModelWithoutSetBin ( plotter, Axes::Y, y );
1639 }
1640 
1641 
1642 void
1645  hippodraw::Axes::Type axis,
1646  const std::string & type )
1647 {
1648  AxisModelBase * model = plotter->getAxisModel ( axis );
1649 
1650  if ( model != 0 ) {
1651  AxisModelBase * temp = 0;
1652 
1653  if ( type == "Linear" ) {
1654 
1655  if ( !( model->isLog() ) ) return; // no change needed
1656  temp = new AxisModelLinear ( *model );
1657 
1658  }
1659  else if ( type == "Log" ) {
1660 
1661  if ( model->isLog () ) return; // no change needed
1662  const Range & range = plotter -> getRange ( axis, false );
1663  if ( range.high () < 0. ) {
1664  string what ( "Can not change axis to Log scale since current "
1665  "range is negative." );
1666  throw std::runtime_error ( what );
1667  }
1668  double pos = plotter->getPosRange ( axis );
1669  model->setRangePos ( pos );
1670  temp = new AxisModelLog ( *model );
1671 
1672  }
1673 
1674  assert ( temp );
1675 
1676  swap ( temp, model );
1677 
1678  plotter->setAxisModel ( model, axis );
1679 
1680  // Do not delete until the end so project can use it.
1681  delete temp;
1682  }
1683 }

Generated for HippoDraw Class Library by doxygen