DataRepController.cxx
Go to the documentation of this file.
1 
12 // inconsistent dll linkage
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "DataRepController.h"
18 
21 #include "datareps/FunctionRep.h"
22 
23 #include "datasrcs/NTuple.h"
24 #include "datasrcs/TupleCut.h"
25 
26 #include "plotters/PlotterBase.h"
28 
29 #include <stdexcept>
30 #include <cassert>
31 
32 using std::runtime_error;
33 using std::string;
34 using std::vector;
35 
36 using namespace hippodraw;
37 
39 
41 {
42 }
43 
45 {
47  delete factory;
48 }
49 
51 {
52  if ( s_instance == 0 ) {
53  s_instance = new DataRepController ( );
54  }
55  return s_instance;
56 }
57 
58 const vector< string > &
59 DataRepController::bindingOptions ( const std::string & name )
60 {
62  DataRep * datarep = factory->prototype ( name );
63 
64  return bindingOptions ( datarep );
65 }
66 
67 const vector < string > &
69 {
70  // Binding options only make sense for something related to a
71  // NTupleProjector. If the downcast doesn't work, this function
72  // return an empty vector to indicate that.
73  ProjectorBase * pbase = datarep->getProjector ();
74  NTupleProjector * ntProjector = dynamic_cast <NTupleProjector *> ( pbase );
75  if ( ntProjector == 0 ) return m_null_vector;
76 
77  return ntProjector->bindingOptions ();
78 }
79 
81 checkBindings ( const DataSource * ntuple,
82  const std::vector < std::string > & bindings ) const
83 {
84  if ( ntuple == 0 ) return false;
85 
86  bool yes = true;
87  vector < string > ::const_iterator first = bindings.begin ();
88  while ( first != bindings.end() ) {
89  const string & label = *first++;
90  if ( label == "nil" ) continue;
91  ntuple -> throwIfInvalidLabel ( label );
92  }
93 
94  return yes;
95 }
96 
97 const vector < string > &
99 names () const
100 {
102 
103  return factory -> names ();
104 }
105 
106 DataRep *
108 createDataRep ( const std::string & type )
109 {
110  DataRep * rep = 0;
112  try {
113  rep = factory -> create ( type ); // let exception be thrown
114  }
115  catch ( const FactoryException & e ) {
116  const string what = e.what ();
117  throw DataRepException ( what );
118  }
119  ProjectorBase * projector = rep -> getProjector ();
120  projector -> addObserver ( rep );
121 
122  return rep;
123 }
124 
125 DataRep *
127 createDataRep ( const std::string & type,
128  const DataSource * tuple,
129  const std::vector < std::string > & bindings ) const
130 {
131  // Expansion needed by RootNTuple with multi-dimensional array.
132  tuple->expandIfNeeded(bindings);
133 
134  bool ok = checkBindings ( tuple, bindings );
135  if ( !ok ) return 0;
136 
138  DataRep * rep = factory->create ( type ); // let the exception be thrown
139  setAxisBindings ( rep, tuple, bindings );
140 
141  return rep;
142 }
143 
144 void
147  const DataSource * tuple,
148  const std::vector < std::string > & bindings ) const
149 {
150  NTupleProjector * ntProjector =
151  dynamic_cast < NTupleProjector * > ( rep->getProjector() );
152 
153  ntProjector->setNTuple ( tuple );
154  DataSource * nt = const_cast < DataSource * > ( tuple );
155  nt->addObserver ( ntProjector );
156  rep->setAxisBindings ( bindings );
157 
158  const vector < const TupleCut * > & cuts = ntProjector -> getCutList ();
159  unsigned int size = cuts.size ();
160  for ( unsigned int i = 0; i < size; i++ ) {
161  const TupleCut * tc = cuts[i];
162  const string & label = tc -> getLabel ();
163  unsigned int index = ntProjector -> indexOf ( label );
164  TupleCut * cut = const_cast < TupleCut * > ( tc );
165  cut -> setColumn ( index );
166  }
167 
168  ntProjector->addObserver ( rep );
169 }
170 
171 void
173 changeDataSource ( DataRep * rep, const DataSource * tuple )
174 {
175  ProjectorBase * pb = rep -> getProjector ();
176  NTupleProjector * projector = dynamic_cast < NTupleProjector * > ( pb );
177  assert ( projector != 0 );
178 
179  DataSource * source = projector -> getNTuple ();
180  source -> removeObserver ( rep );
181 
182  const vector < string > & bindings = projector -> getAxisBindings ();
183 
184  try {
185  setAxisBindings ( rep, tuple, bindings );
186  }
187  catch ( const runtime_error & e ) {
188  setAxisBindings ( rep, source, bindings );
189  throw e;
190  }
191 }
static DataRepFactory * instance()
Returns the pointer to the singleton instance.
void setAxisBindings(DataRep *rep, const DataSource *ntuple, const std::vector< std::string > &bindings) const
Sets the axis binding of the DataRep object.
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
hippodraw::TupleCut class interface
Type * create(const std::string &name)
Creates a new object from a prototype named name.
Definition: Factory.h:160
virtual void setNTuple(const DataSource *source)
Changes the DataSource used for the projections to source.
static DataRepController * instance()
Returns the pointer to the singleton instance.
const std::vector< std::string > & names() const
Returns the names of the types of DataRep objects available.
An NTupleProjector is a projector that projects data from an DataSource object.
virtual ProjectorBase * getProjector() const
Returns the Projector object controlling the data.
Definition: DataRep.cxx:138
hippodraw::NTupleProjector class interface
hippodraw::PlotterBase class interface.
Base class for DataSource.
Definition: DataSource.h:55
virtual const std::vector< std::string > & bindingOptions() const
Returns a vector of strings describing the binding options.
PyArray_TYPES type(numeric::array arr)
Definition: num_util.cpp:249
~DataRepController()
The destructor.
static DataRepController * s_instance
The pointer to the singleton object.
std::vector< std::string > m_null_vector
A empty vector of strings.
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
An xception class that is thrown when attempting to bind a DataRep to a NTuple with a column name tha...
virtual void expandIfNeeded(const std::vector< std::string > &labels) const
Expand the multidimension column if needed.
Definition: DataSource.cxx:602
intp size(numeric::array arr)
Definition: num_util.cpp:296
DataRep * createDataRep(const std::string &name)
Creates a new DataRep object of class name.
An exception class that is thrown when the factory fails to find the request class by its name...
The base class for data representations.
Definition: DataRep.h:68
void changeDataSource(DataRep *rep, const DataSource *tuple)
Changes the DataSource.
Type * prototype(const std::string &name) const
Returns a pointer to a prototype with name name.
Definition: Factory.h:148
This Singleton class maintains a list of DataRep objects.
bool checkBindings(const DataSource *tuple, const std::vector< std::string > &bindings) const
Returns true if all the the string objects in the vector bindings are valid labels for the columns in...
A singleton class that is the interface between GUI and the DataRep.
hippodraw::NTuple class interface.
const std::vector< std::string > & bindingOptions(const std::string &name)
Returns the binding options, if any, for the named DataRep.
hippodraw::FunctionRep class interface
void addObserver(Observer *)
Adds an Observer to the Observer list.
Definition: Observable.cxx:52
DataRepController class interface.
DataRepController()
A default constructor for avoiding creation except by itself or with derived classes.
virtual void setAxisBindings(const std::vector< std::string > &bindings)
Sets the axis bindings.
Definition: DataRep.cxx:112
char const * what() const
The error message that can be retreived.

Generated for HippoDraw Class Library by doxygen