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 }

Generated for HippoDraw Class Library by doxygen