18 #include <boost/python.hpp>
22 #include "msdevstudio/MSconfig.h"
63 using std::runtime_error;
67 using namespace boost::python;
77 "A wrapper for the HippoDraw PlotterBase C++ class.\n "
78 " See HippoDraw's QtDisplay documentation for more details.",
79 init < const std::string & >
82 "Display ( string, DataSource, tuple ) -> Display\n"
84 "Constructor for a Display."
85 #else // gcc 2.95.3 crashes on the following...
86 "Display ( string ) -> Display\n"
87 "Display ( string, DataSource, tuple ) -> Display\n"
88 "Display ( string, DataArray, tuple ) - > Display\n"
89 "Display ( string, tuple, tuple ) -> Display\n"
90 "Display ( string, list, tuple ) -> Display\n"
91 "Display ( string, RootNTuple, tuple ) -> Display\n"
92 "Display ( string, RootNTuple, tuple, tuple ) -> Display\n"
94 "This method is used to create a Display object.\n"
95 "The first method is used for creating static version of\n"
96 "Histogram, etc., where the string is the type of DataRep.\n"
97 "The remaining methods are for dynamic versions.\n"
98 "The string argument is the type. The second argument\n"
99 "is the DataSource and the third argument is tuple of\n"
100 "string for the binding to the DataSource.\n"
101 "For the last method, the fourth argument is a tuple\n"
102 "of integers to access a RootNTuple array variable.\n\n"
103 "For the names of the types of DataRep classes available\n"
104 "call DataRep.names()."
108 .def ( init < PyFunctionRep * > () )
110 .def ( init <
const std::string &,
112 const std::vector< std::string > & > () )
114 .def ( init <
const std::string &,
116 const std::vector< std::string > & > () )
118 .def ( init <
const std::string &,
120 const std::vector< std::string > & > () )
122 .def ( init <
const std::string &,
124 const std::vector< std::string > & > () )
127 .def ( init <
const std::string &,
129 const std::vector< std::string > & > () )
131 .def ( init <
const std::string &,
133 const std::vector< std::string > &,
137 .def (
"applyCut", &QtDisplay::applyCut,
138 "applyCut ( Cut ) -> None\n"
140 "Apply a Cut to the Display" )
142 .def (
"applyCuts", &QtDisplay::applyCuts,
143 "applyCuts ( sequence ) -> None\n"
145 "Apply each Cut in the sequence to the Display" )
147 .def (
"createNTuple", &QtDisplay::createNTuple,
148 return_value_policy < manage_new_object > (),
149 "createNTuple ( ) -> NTuple\n"
151 "Returns a NTuple representation of the Display's contents." )
153 .def (
"createDataArray", &QtDisplay::createDataArray,
154 return_value_policy < manage_new_object > (),
155 "createDataArray ( ) -> DataArray\n"
157 "Returns a DataArray representation of the Display's contents\n"
158 "(This method available if configured with numarray)" )
160 .def (
"setNumberOfBins", &QtDisplay::setNumberOfBins,
161 "setNumberOfBins ( string, value ) -> None\n"
163 "Sets the number of bins on named axis, e.g. 'x' to the given \n"
166 .def (
"setBinWidth",
167 (
void (
QtDisplay::*) (
const std::string &,
170 &QtDisplay::setBinWidth,
171 "Set the bin width, explicitly saving the value or not." )
173 .def (
"getBinWidth",
174 (
double (
QtDisplay::*) (
const std::string & ) )
175 &QtDisplay::getBinWidth,
176 "getBinWidth ( string ) -> value\n"
178 "Returns the bin width on axis specified as a string,\n"
181 .def (
"setBinWidth",
182 (
void (
QtDisplay::*) (
const std::string &,
184 &QtDisplay::setBinWidth,
185 "setBinWidth ( string, value ) -> None\n"
187 "Set the bin width to value on axis specified as a string,\n"
190 .def (
"reset", &QtDisplay::reset,
193 "Resets the contents of all bins. Only applicable to static\n"
196 .def (
"setOffset", &QtDisplay::setOffset,
197 "setOffset ( string, value ) -> None\n"
199 "Sets the offset of the bins relative to their current width on\n"
203 (
void (
QtDisplay::*) (
const std::string &,
205 &QtDisplay::setRange )
208 (
void (
QtDisplay::*) (
const std::string &,
209 double,
double,
bool) )
210 &QtDisplay::setRange,
211 "setRange ( string, value, value ) -> None\n"
212 "setRange ( string, value, value, Boolean ) -> None\n"
214 "Set the upper and lower bounds for the specified axis. For the\n"
215 "second form, also save them if the Boolean argument is true." )
218 .def (
"getRange", &QtDisplay::getRange,
219 "getRange ( string ) -> tuple\n"
221 "Returns the tuple representing the range for the axis specified\n"
222 "as a string, e.g., 'x'." )
224 .def (
"saveView", &QtDisplay::saveView,
225 "saveView ( ) -> int\n"
227 "Saves the current set of x and y ranges and "
228 "returns the index of the saved view." )
230 .def (
"setView", &QtDisplay::setView,
231 "setView ( int ) -> None\n"
233 "Set the view by its index." )
235 .def (
"nextView", &QtDisplay::nextView,
236 "nextView ( bool ) -> int\n"
238 "Cycle to the next view in the view buffer. "
239 "Set the argument to True to cycle for wards, "
240 "False to cycle back wards.\n"
241 "Returns the index of the new view." )
243 .def (
"numViews", &QtDisplay::numViews,
244 "numViews ( ) -> int\n"
246 "Return the number of stored views." )
248 .def (
"deleteView", &QtDisplay::deleteView,
249 "deleteView ( int ) -> None\n"
251 "Delete a view by index." )
253 .def (
"currentView", &QtDisplay::currentView,
254 "currentView ( ) -> int\n"
256 "Index of the current view." )
258 .def (
"setTitle", &QtDisplay::setTitle,
259 "setTitle ( string ) -> None\n"
261 "Sets the title of the display." )
263 .def (
"getTitle", &QtDisplay::getTitle,
264 return_value_policy < copy_const_reference > (),
265 "getTitle () -> string\n"
267 "Returns the current title. The title will be the title of\n"
268 "the DataSource unless it has been explicitly changed." )
270 .def (
"setLabel", &QtDisplay::setLabel,
271 "setLabel ( string, string ) -> None\n"
273 "Sets the label for the axis specified by first argument to value\n"
274 "of the second argument." )
276 .def (
"getLabel", &QtDisplay::getLabel,
277 return_value_policy< copy_const_reference > (),
278 "getLabel ( string ) -> string\n"
280 "Returns the label of the axis specified as a string,\n"
283 .def (
"getDataRep", &QtDisplay::getDataRep,
284 return_value_policy < manage_new_object > (),
285 "getDataRep ( ) -> DataRep\n"
287 "Returns a reference to the active DataRep or if all DataRep objects are\n"
288 "active, returns a reference to the first one." )
290 .def (
"getDataReps", &QtDisplay::getDataReps,
291 return_value_policy < copy_const_reference > (),
292 "getDataReps ( ) -> list\n"
294 "Returns a list of DataRep objects contained by the Display.." )
298 &QtDisplay::addDataRep,
299 "addDataRep ( DataRep ) -> Display\n"
300 "addDataRep ( Function ) -> Display\n"
301 "addDataRep ( string, DataSource, tuple ) -> Display\n"
303 "Adds a DataRep to the display sharing the same Y axis range\n"
304 "The first two methods adds existing DataRep or Function to the\n"
305 "Display. The third method creates and adds DataRep to the\n"
306 "Display. Arguments are same as Display constructor." )
309 (
void (
QtDisplay::*) (
const std::string &,
311 const std::vector <std::string > &) )
312 &QtDisplay::addDataRep )
316 &QtDisplay::addDataRep )
318 .def (
"addDataRepStacked",
319 (
void (
QtDisplay::*) (
const std::string &,
321 const std::vector <std::string > &) )
322 &QtDisplay::addDataRepStacked,
323 "addDataRepStacked ( string, DataSource, tuple ) -> Display\n"
325 "Creates and adds a DataRep with independent Y axis ranges.\n"
326 "The arguments are the same as Display constructor." )
329 .def (
"addFunction",
331 &QtDisplay::addFunction,
332 "addFunction ( FunctionBase ) -> None\n"
334 "Adds a FunctionBase object to the display by appropriately\n"
335 "wrapping it with a Function." )
336 #endif // BOOST_DEFECT
338 .def (
"setAutoRanging",
339 (
void (
QtDisplay::*) (
const std::string &,
341 &QtDisplay::setAutoRanging,
342 "setAutoRanging ( string, Boolean ) -> None\n"
344 "Sets auto-ranging on axis specified as a string, e.g. 'x',n"
347 .def (
"setLog", &QtDisplay::setLog,
348 "setLog ( string, Boolean ) -> None\n"
350 "Sets the axis specified by the first argument on log scale." )
352 .def (
"getLog", &QtDisplay::getLog,
353 "getLog ( string ) -> value\n"
355 "Returns True if axis specified as a string, e.g. 'x', is being\n"
356 "displayed on a logarithmic scale." )
358 .def (
"setTransform", &QtDisplay::setTransform,
359 "setTransform ( string ) -> None\n"
361 "Sets the transform object." )
363 .def (
"addValues", &QtDisplay::addValues,
364 "addValue ( tuple ) -> None\n"
366 "For static histograms, adds a value to the accumulation.\n"
367 "For 1D histogram, the tuple should contain one or two values,\n"
368 "the second used as a weight. For 2D histogram, the tuple should\n"
369 "contain two or three elements, the third being the weight.\n"
370 "non static Displays do nothing." )
372 .def (
"setPointRep", &QtDisplay::setPointRep,
373 "setPointRep ( RepBase ) -> None\n"
375 "Sets the point representation to be used." )
377 .def (
"setContourLevels", &QtDisplay::setContourLevels,
378 "setContourLevels ( sequence ) -> None\n"
380 "Sets the contour levels if the Display is using contour point\n"
381 "representation from the values in the sequence." )
383 .def (
"setAspectRatio", &QtDisplay::setAspectRatio,
384 "setAspectRatio ( value ) -> None\n"
386 "Sets the required aspect ratio of the Display to value, the\n"
387 "ratio of the width to the height." )
389 .def (
"numberOfEntries", &QtDisplay::numberOfEntries,
390 "numberOfEntries ( ) -> value\n"
392 "Returns the number of entries in the Display." )
394 .def (
"resize", &QtDisplay::resize,
395 "resize () -> None\n"
397 "Resizes the Display to its saved values." )
399 .def (
"plotterId", &QtDisplay::plotterId,
400 "plotterId () -> value\n"
402 "Returns a unique identifier for the Display." )
404 .def (
"setColorMap", &QtDisplay::setColorMap,
405 "setColorMap ( string ) -> None\n"
407 "Set the value-to-color map to one named by the argument.")
409 .def (
"update", &QtDisplay::update,
410 "update () -> None\n"
412 "Updates the display." )
414 .def (
"addObserver", &QtDisplay::addObserver,
415 "addObserver ( Observer ) -> None\n"
417 "Adds an Observer to the Display object." )
419 .def (
"setAutoTicks", &QtDisplay::setAutoTicks,
420 "setAutoTicks ( Boolean ) -> None\n"
422 "Set the ticks generation to be automatic (the default) or\n"
425 .def (
"setTicks", &QtDisplay::setTicks,
426 "setTicks ( string, sequence, sequence ) -> None\n"
428 "Sets the tick locations and labels. The first argument is the\n"
429 " axis, the second argument is a sequence containing the\n"
430 "locations, and the third argument is a sequence of tick labels." )
432 .def (
"unlock", &QtDisplay::unlock,
433 "unlock () -> None\n"
435 "Unlock the application thread." )
443 using namespace hippodraw;
447 void QtDisplay::createDisplay (
const std::string &
type,
449 const std::vector < std::string > & bindings )
454 m_plotter = controller->
createDisplay ( type, nt, bindings );
465 catch (
const runtime_error & e ) {
472 QtDisplay (
const std::string &
type,
474 const std::vector < std::string > & labels )
478 object obj = seq.attr (
"__len__" ) ();
483 unsigned int size = extract < unsigned int > ( obj );
485 if ( size > labels.size () ) {
486 string what (
"Display: Too few labels" );
487 throw runtime_error ( what );
490 for (
unsigned int i = 0, j = 0; i <
size; i++, j++ ) {
493 while ( labels[j] ==
"nil" ) {
495 if ( ! ( j < labels.size () ) ) {
496 string what (
"Display: Too few non 'nil' labels" );
497 throw runtime_error ( what );
501 ntuple -> addColumn ( labels[j], l );
504 catch (
const runtime_error & e ) {
513 m_plotter = dc -> createDisplay ( type, *ntuple, labels );
525 catch (
const runtime_error & e ) {
533 dsc -> registerNTuple ( ntuple );
539 QtDisplay (
const std::string &
type,
541 const std::vector < std::string > & labels )
545 object obj = seq.attr (
"__len__" ) ();
550 unsigned int size = extract < unsigned int > ( obj );
552 if ( size > labels.size () ) {
553 string what (
"Display: Too few labels" );
554 throw runtime_error ( what );
558 for (
unsigned int i = 0, j = 0; i <
size; i++, j++ ) {
561 while ( labels[j] ==
"nil" ) {
563 if ( ! ( j < labels.size () ) ) {
564 string what (
"Display: Too few non 'nil' labels" );
565 throw runtime_error ( what );
569 ntuple -> addColumn ( labels[j], l );
572 catch (
const runtime_error & e ) {
581 m_plotter = dc -> createDisplay ( type, *ntuple, labels );
593 catch (
const runtime_error & e ) {
601 dsc -> registerNTuple ( ntuple );
606 QtDisplay::QtDisplay ()
617 DataRep * dr = rep -> getRep ();
654 const std::vector< std::string > & bindings )
661 const std::vector< std::string > & bindings )
670 const std::vector < std::string > & bindings )
678 const std::vector < std::string > & variables,
681 object obj = indices.attr (
"__len__" ) ();
682 unsigned int size = extract < unsigned int > ( obj );
684 if ( size != variables.size() ) {
685 const string what (
"Display: bindings and indexes not the same size." );
687 throw runtime_error ( what );
690 vector < vector < int > >
vec ( size );
691 for (
unsigned int i = 0; i <
size; i++ ) {
693 object o = l.attr (
"__len__" ) ();
694 unsigned int len = extract < unsigned int > ( o );
695 for (
unsigned int j = 0; j < len; j++ ) {
696 unsigned int k = extract < unsigned int > ( l[j] );
697 vec[i].push_back ( k );
701 vector < string > bindings ( size );
703 for (
unsigned int i = 0; i <
size; i++ ) {
704 const string & label = variables [ i ];
705 const vector < int > & indexes = vec [ i ];
707 bindings [ i ] = name;
732 const std::vector < std::string > & bindings )
746 const std::vector < std::string > & bindings )
791 dataRep =
reinterpret_cast<DataRep *
>(funcRep);
796 #endif // BOOST_DEFECT
800 setRange (
const std::string & axis,
double low,
double high,
806 if (axis ==
"x" || axis ==
"X")
807 m_ranges[
"x"] = std::make_pair(low, high);
808 if (axis ==
"y" || axis ==
"Y")
809 m_ranges[
"y"] = std::make_pair(low, high);
819 setRange (
const std::string & axis,
double low,
double high )
832 std::vector<double> myRange;
836 myRange.push_back(axisRange.
low());
837 myRange.push_back(axisRange.
high());
851 std::vector<double> range_values;
852 std::vector<double> range =
getRange(
"x");
853 range_values.push_back(range[0]);
854 range_values.push_back(range[1]);
855 m_ranges[
"x"] = std::make_pair(range[0], range[1]);
858 range_values.push_back(range[0]);
859 range_values.push_back(range[1]);
860 m_ranges[
"y"] = std::make_pair(range[0], range[1]);
928 }
catch (
const std::runtime_error & e ) {
942 if ( rep->
name() == std::string(
"Contour") ) {
947 ->setContourValues(
const_cast<std::vector<double>&
>(levels),
959 const std::string & label )
1005 if (axis ==
"x" || axis ==
"X")
1007 if (axis ==
"y" || axis ==
"Y")
1070 bool yes = flag != 0;
1101 if ( index < 0 ) index = 0;
1110 const std::vector<PyDataRep *> &
1118 for (
int i = 0; i < nReps; i++) {
1230 const vector< std::string > & names = factory -> names();
1231 if ( std::find(names.begin(), names.end(), name) != names.end() ) {
1232 BinToColor * rep = factory -> create ( name );
1237 std::ostringstream message;
1238 message <<
"QtDisplay::setColorMap:\n"
1239 <<
"BinToColor rep '" << name <<
"' does not exist.\n"
1240 <<
"Valid rep names are \n\n";
1241 for (
unsigned int i = 0; i < names.size() ; i++) {
1242 message <<
"'" << names[i] <<
"'\n";
1244 throw std::runtime_error(message.str());
1269 const std::vector < double > & values,
1270 const std::vector < std::string > & labels )
1291 #ifdef HAVE_NUMARRAY
1302 runtime_error e (
"HippoDraw was not built with numeric Python support" );
1317 controller -> addCut ( cut_plotter, target_plotter );
1328 vector < PlotterBase * > cut_plotters;
1329 unsigned int size = cuts.size ();
1330 for (
unsigned int i = 0; i <
size; i++ ) {
1333 cut_plotters.push_back ( plotter );
1338 controller -> addCuts ( cut_plotters, target_plotter );