Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PyDataRep.cxx

Go to the documentation of this file.
00001 
00012 // for dll interface warning
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016 
00017 #include "PyDataRep.h"
00018 #include "PyDataSource.h"
00019 #include "QtCut.h"
00020 
00021 #include "controllers/DisplayController.h"
00022 #include "controllers/DataRepController.h"
00023 #include "controllers/CutController.h"
00024 #include "projectors/ProjectorBase.h"
00025 #include "reps/RepBase.h"
00026 #include "datasrcs/DataSourceException.h"
00027 #include "datareps/DyHistogram.h"
00028 #include "datasrcs/NTuple.h"
00029 #include "plotters/Cut1DPlotter.h"
00030 
00031 // with Python 2.3, include before qapplication.h to avoid conflict
00032 // with symbol `slots'
00033 #include <boost/python.hpp>
00034 
00035 #include <qapplication.h>
00036 
00037 #include <sstream>
00038 
00039 using namespace hippodraw;
00040 
00041 using std::string;
00042 
00043 using namespace boost::python;
00044 
00045 namespace hippodraw {
00046 namespace Python {
00047 
00048 void
00049 export_DataRep()
00050 {
00051   class_ < PyDataRep > ( "DataRep" )
00052 
00053      .def ( init < const std::string &,
00054             const DataSource *,
00055             const std::vector< std::string > & > () )
00056 
00057      .def ( init < const std::string &,
00058             const PyDataSource *,
00059             const std::vector< std::string > & > () )
00060 
00061      .def ( init < PyDataRep * > () )
00062 
00063      .def ( "setPointRep", &PyDataRep::setPointRep,
00064             "Sets the point representation." )
00065 
00066      .def ( "setAxisBinding", &PyDataRep::setAxisBinding,
00067             "Sets an axis binding." )
00068 
00069      .def ( "setAxisBindings", &PyDataRep::setAxisBindings,
00070             "Sets all the axes bindings." )
00071 
00072      .def ( "setWeight", &PyDataRep::setWeight,
00073             "Sets the weight option if DataRep is a type of histogram." )
00074 
00075      .def ( "name", &PyDataRep::name,
00076             return_value_policy < copy_const_reference > (),
00077             "Returns the name of type of DataRep." )
00078 
00079      .def ( "getBinWidth", &PyDataRep::getBinWidth,
00080             "Returns the bin width if DataRep is histogramming type." )
00081 
00082      .def ( "getMean", &PyDataRep::getMean,
00083            args ( "axis" ),
00084            "Returns the mean of the data along axis"  )
00085 
00086      .def ( "getRMS", &PyDataRep::getRMS,
00087            args( "axis" ),
00088            "Returns root mean squared of data along axis" )
00089 
00090      .def ( "numberOfEntries", &PyDataRep::numberOfEntries,
00091             "Returns the number of entries." )
00092 
00093      .def ( "applyCut", &PyDataRep::applyCut,
00094             "Applies a Cut." )
00095 
00096      .def ( "setColor", &PyDataRep::setColor,
00097             "Sets the color." )
00098 
00099      .def ( "setErrorDisplay", &PyDataRep::setErrorDisplay,
00100             "Turns on or off display of error bars." )
00101 
00102      .def ( "createNTuple", &PyDataRep::createNTuple,
00103             return_value_policy < manage_new_object > (),
00104             "Returns an NTuple representation of the DataRep." )
00105 
00106      .def ( "getNTupleWithCuts", &PyDataRep::getNTupleWithCuts,
00107             return_value_policy < manage_new_object > (),
00108             "Returns to NTuple being used, with cuts applied." )
00109 
00110      .def ( "setSymbol", &PyDataRep::setSymbol,
00111             "Sets the point symbol and size." )
00112 
00113      .def ( "setLineStyle", &PyDataRep::setLineStyle,
00114             "Sets the line style." )
00115 
00116     .def ( "normalizeTo", &PyDataRep::normalizeTo,
00117            "Sets the data rep to normalize itself to a target one" )
00118 
00119     .def ( "setBinWidth", &PyDataRep::setBinWidth,
00120            "Sets the width of the bins, if data representation is binned.\n"
00121            "Arguments:\n"
00122            "\t axis (string)\n"
00123            "\t width" )
00124 
00125     ;
00126 }
00127 
00128 } // namespace Python
00129 } // namespace hippodraw
00130 
00131 std::map< std::string, std::vector<int> > PyDataRep::s_colorMap;
00132 std::map< std::string, hippodraw::Symbol::Type > PyDataRep::s_symbols;
00133 std::map< std::string, hippodraw::Line::Style > PyDataRep::s_lineStyles;
00134 bool PyDataRep::s_have_static_members(false);
00135 
00136 PyDataRep::PyDataRep ( DataRep * rep ) 
00137 {
00138    m_datarep = rep;
00139    init();
00140 }
00141 
00142 PyDataRep::PyDataRep ( const std::string & type,
00143                        const DataSource * ntuple,
00144                        const std::vector< std::string > & bindings )
00145 {
00146    DataRepController * controller = DataRepController::instance ();
00147    m_datarep = controller->createDataRep ( type, ntuple, bindings );
00148    init();
00149 }
00150 
00151 PyDataRep::PyDataRep ( const std::string & type,
00152                        const PyDataSource * nt,
00153                        const std::vector< std::string > & bindings )
00154 {
00155    DataRepController * controller = DataRepController::instance ();
00156    m_datarep = controller->createDataRep ( type, &(nt->dataSource()),
00157                                            bindings );
00158    init();
00159 }
00160 
00161 PyDataRep::PyDataRep ()
00162 {
00163   m_datarep = 0;
00164   init();
00165 }
00166 
00167 PyDataRep::PyDataRep( PyDataRep * pyRep )
00168 {
00169    m_datarep = pyRep->getDataRep()->clone();
00170 }   
00171 
00172 DataRep * PyDataRep::getDataRep()
00173 {
00174   return m_datarep;
00175 }
00176 
00177 void PyDataRep::setPointRep ( RepBase * pointRep )
00178 {
00179    if (qApp) qApp->lock();
00180    m_datarep->setRepresentation( pointRep );
00181    if (qApp) qApp->unlock();
00182 }
00183 
00184 void PyDataRep::setAxisBinding ( const std::string & axis,
00185                                  const std::string & label )
00186 {
00187    if (qApp) qApp->lock();
00188    try {
00189      m_datarep->setAxisBinding ( axis, label );
00190    }
00191    catch ( DataSourceException & e ) {
00192      if (qApp) qApp->unlock();
00193      throw e;
00194    }
00195    if (qApp) qApp->unlock();
00196 }
00197 
00198 void PyDataRep::setAxisBindings ( const std::vector< std::string > & bindings)
00199 {
00200    if (qApp) qApp->lock();
00201    try {
00202      m_datarep->setAxisBindings ( bindings );
00203    }
00204    catch ( DataSourceException & e ) {
00205      if (qApp) qApp->unlock();
00206      throw e;
00207    }
00208    if (qApp) qApp->unlock();
00209 }
00210 
00211 void PyDataRep::setWeight ( const std::string &label )
00212 {
00213    if (qApp) qApp->lock();
00214    if ( m_datarep->name() == "Histogram" 
00215         || m_datarep->name() == "Color Plot"
00216         || m_datarep->name() == "Contour Plot") {
00217       m_datarep->setAxisBinding( std::string("Weight (optional)"),
00218                                  label );
00219    } else {
00220       // do nothing
00221    }
00222    if (qApp) qApp->unlock();
00223 }
00224 
00225 const std::string & PyDataRep::name () const 
00226 {
00227    return m_datarep->name();
00228 }
00229 
00230 double PyDataRep::getBinWidth ( const std::string &axis )
00231 {
00232    if (qApp) qApp->lock();
00233    Axes::Type at = Axes::convert ( axis );
00234    ProjectorBase * projector = m_datarep->getProjector ();
00235    if (qApp) qApp->unlock();
00236    return projector->getBinWidth ( at );
00237 }
00238 
00239 double
00240 PyDataRep::
00241 getMean ( const std::string & axis )
00242 {
00243   if (qApp) qApp->lock();
00244   double mean = m_datarep -> getMean ( axis );
00245   if (qApp) qApp->unlock();
00246 
00247   return mean;
00248 }
00249 
00250 double
00251 PyDataRep::
00252 getRMS ( const std::string & axis )
00253 {
00254   if (qApp) qApp->lock();
00255   double rms = m_datarep -> getRMS ( axis );
00256   if (qApp) qApp->unlock();
00257 
00258   return rms;
00259 }
00260 
00261 double PyDataRep::numberOfEntries() const 
00262 {
00263    ProjectorBase * projector = m_datarep->getProjector();
00264    return projector -> getNumberOfEntries();
00265 }
00266 
00267 void PyDataRep::applyCut ( QtCut * cut )
00268 {
00269    if (qApp) qApp->lock();
00270    CutController * controller = CutController::instance();
00271    controller -> linkCutAndRep( cut -> display(), m_datarep );
00272    if (qApp) qApp->unlock();
00273 }
00274 
00275 void PyDataRep::setColor ( const std::string & color )
00276 {
00277    if (qApp) qApp->lock();
00278    if ( s_colorMap.count( color ) ) {
00279       Color colorObj( s_colorMap[color][0], 
00280                       s_colorMap[color][1], 
00281                       s_colorMap[color][2] );
00282       m_datarep -> setRepColor ( colorObj );
00283    } else {
00284       if (qApp) qApp->unlock();
00285       const std::string 
00286          what ( "PyDataRep::setColor: color not available.");
00287       throw DataSourceException( what );
00288    }
00289    if (qApp) qApp->unlock();
00290 }
00291 
00292 void PyDataRep::setErrorDisplay( const std::string &axis, bool flag )
00293 {
00294    if (qApp) qApp->lock();
00295 
00296    Axes::Type at = Axes::convert ( axis );   
00297    m_datarep->setErrorDisplay( at, flag );
00298 
00299    if (qApp) qApp->unlock();
00300 }
00301 
00302 const NTuple * 
00303 PyDataRep::
00304 createNTuple () const
00305 {
00306    if (qApp) qApp->lock();
00307    const NTuple * ntuple = m_datarep -> createNTuple ();
00308    if (qApp) qApp->unlock();
00309 
00310    return ntuple;
00311 }
00312 
00313 NTuple * PyDataRep::getNTupleWithCuts () const {
00314    if (qApp) qApp->lock();
00315    NTuple * ntuple = m_datarep->getNTupleAfterCuts();
00316    if (qApp) qApp->unlock();
00317 
00318    return ntuple;
00319 }
00320 
00324 void PyDataRep::makeColorMap() {
00325 
00326    int black[] = {0, 0, 0};
00327    s_colorMap["black"] = std::vector<int>(black, black+3);
00328 
00329    int red[] = {255, 0, 0};
00330    s_colorMap["red"] = std::vector<int>(red, red+3);
00331 
00332    int green[] = {0, 255, 0};
00333    s_colorMap["green"] = std::vector<int>(green, green+3);
00334 
00335    int blue[] = {0, 0, 255};
00336    s_colorMap["blue"] = std::vector<int>(blue, blue+3);
00337 
00338    int yellow[] = {255, 255, 0};
00339    s_colorMap["yellow"] = std::vector<int>(yellow, yellow+3);
00340    
00341    int cyan[] = {0, 255, 255};
00342    s_colorMap["cyan"] = std::vector<int>(cyan, cyan+3);
00343    
00344    int magenta[] = {255, 0, 255};
00345    s_colorMap["magenta"] = std::vector<int>(magenta, magenta+3);
00346 }
00347 
00348 void PyDataRep::makeSymbolMap() {
00349    s_symbols["square"] = hippodraw::Symbol::SQUARE;
00350    s_symbols["filled_square"] = hippodraw::Symbol::SOLIDSQUARE;
00351    s_symbols["plus"] = hippodraw::Symbol::PLUS;
00352    s_symbols["times"] = hippodraw::Symbol::TIMES;
00353    s_symbols["triangle"] = hippodraw::Symbol::TRIANGLE;
00354    s_symbols["filled_triangle"] = hippodraw::Symbol::FILLED_TRIANGLE;
00355    s_symbols["circle"] = hippodraw::Symbol::CIRCLE;
00356    s_symbols["filled_circle"] = hippodraw::Symbol::FILLED_CIRCLE;
00357 }
00358 
00360 void PyDataRep::setSymbol( const std::string &symbolName, float size) {
00361    if (qApp) qApp->lock();
00362    if ( s_symbols.count( symbolName ) ) {
00363       m_datarep -> setRepStyle ( s_symbols[symbolName] );
00364       m_datarep -> setRepSize ( size );
00365    } else {
00366       if (qApp) qApp->unlock();
00367       std::ostringstream what;
00368       what << "PyDataRep::setSymbol: symbol " 
00369            << symbolName << " is not available.\n"
00370            << "Valid symbol names:\n";
00371       std::map< std::string, hippodraw::Symbol::Type >
00372          ::const_iterator it = s_symbols.begin();
00373       for ( ; it != s_symbols.end(); it++) {
00374          what << "  " << it->first << "\n";
00375       }
00376       throw DataSourceException( what.str() );
00377    }
00378    if (qApp) qApp->unlock();
00379 }
00380 
00381 void PyDataRep::makeLineStyleMap() {
00382    s_lineStyles["Solid"] = hippodraw::Line::Solid;
00383    s_lineStyles["Dash"] = hippodraw::Line::Dash;
00384    s_lineStyles["Dot"] = hippodraw::Line::Dot;
00385    s_lineStyles["DashDot"] = hippodraw::Line::DashDot;
00386    s_lineStyles["DashDotDot"] = hippodraw::Line::DashDotDot;
00387    s_lineStyles["Invisible"] = hippodraw::Line::Invisible;
00388 }
00389 
00390 void PyDataRep::setLineStyle( const std::string &lineStyleName ) {
00391    if (qApp) qApp->lock();
00392    if ( s_lineStyles.count( lineStyleName ) ) {
00393       m_datarep->setRepStyle( s_lineStyles[lineStyleName] );
00394    } else {
00395       if (qApp) qApp->unlock();
00396       std::ostringstream what;
00397       what << "PyDataRep::setLineStyle: lineStyle " 
00398            << lineStyleName << " is not available.\n"
00399            << "Valid lineStyle names:\n";
00400       std::map< std::string, hippodraw::Line::Style >
00401          ::const_iterator it = s_lineStyles.begin();
00402       for ( ; it != s_lineStyles.end(); it++) {
00403          what << "  " << it->first << "\n";
00404       }
00405       throw DataSourceException( what.str() );
00406    }
00407    if (qApp) qApp->unlock();
00408 }
00409 
00410 void PyDataRep::init() {
00411    if (!s_have_static_members) {
00412       makeColorMap();
00413       makeSymbolMap();
00414       makeLineStyleMap();
00415 
00416       s_have_static_members = true;
00417    }
00418 }
00419 
00420 void
00421 PyDataRep::
00422 normalizeTo ( const PyDataRep * rep )
00423 {
00424   if (qApp) qApp->lock();
00425   const DataRep * datarep = rep -> m_datarep;
00426   m_datarep -> normalizeTo ( datarep );
00427   if (qApp) qApp->unlock();
00428 }
00429 
00430 void
00431 PyDataRep::
00432 setBinWidth ( const std::string & axis, double width )
00433 {
00434   if (qApp) qApp->lock();
00435 
00436   Axes::Type type = Axes::convert ( axis );
00437   DisplayController * controller = DisplayController::instance ();
00438   controller -> setBinWidth ( m_datarep, type, width );
00439   if (qApp) qApp->unlock();
00440 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3