00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "DataRepController.h"
00018
00019 #include "datareps/DataRepException.h"
00020 #include "datareps/DataRepFactory.h"
00021 #include "datareps/FunctionRep.h"
00022
00023 #include "datasrcs/NTuple.h"
00024 #include "datasrcs/TupleCut.h"
00025
00026 #include "plotters/PlotterBase.h"
00027 #include "projectors/NTupleProjector.h"
00028
00029 #include <stdexcept>
00030 #include <cassert>
00031
00032 using std::runtime_error;
00033 using std::string;
00034 using std::vector;
00035
00036 using namespace hippodraw;
00037
00038 DataRepController * DataRepController::s_instance = 0;
00039
00040 DataRepController::DataRepController ( )
00041 {
00042 }
00043
00044 DataRepController::~DataRepController ( )
00045 {
00046 DataRepFactory * factory = DataRepFactory::instance ();
00047 delete factory;
00048 }
00049
00050 DataRepController * DataRepController::instance ( )
00051 {
00052 if ( s_instance == 0 ) {
00053 s_instance = new DataRepController ( );
00054 }
00055 return s_instance;
00056 }
00057
00058 const vector< string > &
00059 DataRepController::bindingOptions ( const std::string & name )
00060 {
00061 DataRepFactory * factory = DataRepFactory::instance ();
00062 DataRep * datarep = factory->prototype ( name );
00063
00064 return bindingOptions ( datarep );
00065 }
00066
00067 const vector < string > &
00068 DataRepController::bindingOptions ( DataRep * datarep )
00069 {
00070
00071
00072
00073 ProjectorBase * pbase = datarep->getProjector ();
00074 NTupleProjector * ntProjector = dynamic_cast <NTupleProjector *> ( pbase );
00075 if ( ntProjector == 0 ) return m_null_vector;
00076
00077 return ntProjector->bindingOptions ();
00078 }
00079
00080 bool DataRepController::
00081 checkBindings ( const DataSource * ntuple,
00082 const std::vector < std::string > & bindings ) const
00083 {
00084 if ( ntuple == 0 ) return false;
00085
00086 bool yes = true;
00087 vector < string > ::const_iterator first = bindings.begin ();
00088 while ( first != bindings.end() ) {
00089 const string & label = *first++;
00090 if ( label == "nil" ) continue;
00091 ntuple -> throwIfInvalidLabel ( label );
00092 }
00093
00094 return yes;
00095 }
00096
00097 const vector < string > &
00098 DataRepController::
00099 names () const
00100 {
00101 DataRepFactory * factory = DataRepFactory::instance ();
00102
00103 return factory -> names ();
00104 }
00105
00106 DataRep *
00107 DataRepController::
00108 createDataRep ( const std::string & type )
00109 {
00110 DataRep * rep = 0;
00111 DataRepFactory * factory = DataRepFactory::instance ();
00112 try {
00113 rep = factory -> create ( type );
00114 }
00115 catch ( const FactoryException & e ) {
00116 const string what = e.what ();
00117 throw DataRepException ( what );
00118 }
00119 ProjectorBase * projector = rep -> getProjector ();
00120 projector -> addObserver ( rep );
00121
00122 return rep;
00123 }
00124
00125 DataRep *
00126 DataRepController::
00127 createDataRep ( const std::string & type,
00128 const DataSource * tuple,
00129 const std::vector < std::string > & bindings ) const
00130 {
00131
00132 tuple->expandIfNeeded(bindings);
00133
00134 bool ok = checkBindings ( tuple, bindings );
00135 if ( !ok ) return 0;
00136
00137 DataRepFactory * factory = DataRepFactory::instance ();
00138 DataRep * rep = factory->create ( type );
00139 setAxisBindings ( rep, tuple, bindings );
00140
00141 return rep;
00142 }
00143
00144 void
00145 DataRepController::
00146 setAxisBindings ( DataRep * rep,
00147 const DataSource * tuple,
00148 const std::vector < std::string > & bindings ) const
00149 {
00150 NTupleProjector * ntProjector =
00151 dynamic_cast < NTupleProjector * > ( rep->getProjector() );
00152
00153 ntProjector->setNTuple ( tuple );
00154 DataSource * nt = const_cast < DataSource * > ( tuple );
00155 nt->addObserver ( ntProjector );
00156 rep->setAxisBindings ( bindings );
00157
00158 const vector < const TupleCut * > & cuts = ntProjector -> getCutList ();
00159 unsigned int size = cuts.size ();
00160 for ( unsigned int i = 0; i < size; i++ ) {
00161 const TupleCut * tc = cuts[i];
00162 const string & label = tc -> getLabel ();
00163 unsigned int index = ntProjector -> indexOf ( label );
00164 TupleCut * cut = const_cast < TupleCut * > ( tc );
00165 cut -> setColumn ( index );
00166 }
00167
00168 ntProjector->addObserver ( rep );
00169 }
00170
00171 void
00172 DataRepController::
00173 changeDataSource ( DataRep * rep, const DataSource * tuple )
00174 {
00175 ProjectorBase * pb = rep -> getProjector ();
00176 NTupleProjector * projector = dynamic_cast < NTupleProjector * > ( pb );
00177 assert ( projector != 0 );
00178
00179 DataSource * source = projector -> getNTuple ();
00180 source -> removeObserver ( rep );
00181
00182 const vector < string > & bindings = projector -> getAxisBindings ();
00183
00184 try {
00185 setAxisBindings ( rep, tuple, bindings );
00186 }
00187 catch ( const runtime_error & e ) {
00188 setAxisBindings ( rep, source, bindings );
00189 throw e;
00190 }
00191 }