PyDataRep.cxx
Go to the documentation of this file.
1 
12 // for dll interface warning
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 // include first to avoid _POSIX_C_SOURCE warning.
18 #include <boost/python.hpp>
19 
20 #include "PyDataRep.h"
21 
22 #include "PyApp.h"
23 #include "PyDataSource.h"
24 #include "QtCut.h"
25 
30 #include "reps/RepBase.h"
31 #include "datareps/DyHistogram.h"
32 #include "datasrcs/NTuple.h"
33 #include "plotters/Cut1DPlotter.h"
34 
35 #include <stdexcept>
36 #include <sstream>
37 
38 using std::runtime_error;
39 using std::string;
40 
41 using namespace boost::python;
42 
43 namespace hippodraw {
44 namespace Python {
45 
46 void
48 {
49  class_ < PyDataRep >
50  ( "DataRep",
51  "A base class for representing data in various ways. For example, a\n"
52  "a histogram is a way to represent a column of data." )
53 
54  .def ( init < const std::string &,
55  const DataSource *,
56  const std::vector< std::string > & >
57  (
58  "DataRep ( string ) -> DataRep\n"
59  "DataRep ( string, DataSource, sequence ) -> DataRep\n"
60  "DataRep ( DataRep ) -> DataRep\n"
61  "\n"
62  "Constructors for DataRep objects. The first form creates a\n"
63  "static version. The second form creates a dynamic version\n"
64  "bound to a DataSource with bindings taken from the strings in\n"
65  "the sequence. The final version makes a copy of existing\n"
66  "DataRep.\n\n"
67  "For the names of the types of DataRep classes available\n"
68  "see names().") )
69 
70  .def ( init < const std::string &,
71  const PyDataSource *,
72  const std::vector< std::string > & > () )
73 
74  .def ( init < PyDataRep * > () )
75 
76  .def ( "names", &PyDataRep::names,
77  return_value_policy < copy_const_reference > (),
78  "names () -> tuple\n"
79  "\n"
80  "Returns a tuple containing the names of the types of DataRep\n"
81  "that are available." )
82 
83  .staticmethod ( "names" )
84 
85  .def ( "setPointRep", &PyDataRep::setPointRep,
86  "setPointRep ( RepBase ) -> None\n"
87  "\n"
88  "Sets the point representation." )
89 
90  .def ( "setAxisBinding", &PyDataRep::setAxisBinding,
91  "setAxisBinding ( string, string ) -> None\n"
92  "\n"
93  "Sets an axis binding. The first argument is the axis and the\n"
94  "second is a label of a column in the DataSource." )
95 
96  .def ( "setAxisBindings", &PyDataRep::setAxisBindings,
97  "setAxisBindings ( string, sequence ) -> None\n"
98  "\n"
99  "Sets all the axes bindings. The first argument is the axis and\n"
100  "the sequence contains labels of columns in the DataSource." )
101 
102  .def ( "setWeight", &PyDataRep::setWeight,
103  "setWeight ( string ) -> None\n"
104  "\n"
105  "Sets the weight option if DataRep is a type of histogram.\n"
106  "The string is a label of a column in the DataSource." )
107 
108  .def ( "name", &PyDataRep::name,
109  return_value_policy < copy_const_reference > (),
110  "name ( ) -> string\n"
111  "\n"
112  "Returns the type of DataRep." )
113 
114  .def ( "getBinWidth", &PyDataRep::getBinWidth,
115  "getBinWidth ( string ) -> value\n"
116  ""
117  "Returns the bin width on specified axis. Only meaningful if\n"
118  "object is histogram type." )
119 
120  .def ( "getMean", &PyDataRep::getMean,
121  "getMean ( string ) -> value\n"
122  "\n"
123  "Returns the mean of the data along specified axis" )
124 
125  .def ( "getRMS", &PyDataRep::getRMS,
126  "getRMS ( string ) -> value\n"
127  "\n"
128  "Returns root mean squared of data along specified axis" )
129 
130  .def ( "numberOfEntries", &PyDataRep::numberOfEntries,
131  "numberOfEntries ( ) -> value\n"
132  "\n"
133  "Returns the number of entries." )
134 
135  .def ( "applyCut", &PyDataRep::applyCut,
136  "applyCut ( Cut ) -> None\n"
137  "\n"
138  "Applies a Cut to limit the rows of the DataSource used by\n"
139  "the Display." )
140 
141  .def ( "applyCuts", &PyDataRep::applyCuts,
142  "applyCuts ( sequence ) -> None\n"
143  "\n"
144  "Applies a sequence of Cut object to limit the rows of\n"
145  "the DataSource used by the Display." )
146 
147  .def ( "colorNames", &PyDataRep::colorNames,
148  return_value_policy < copy_const_reference > (),
149  "colorNames () -> tuple\n"
150  "\n"
151  "Returns a list of available color names." )
152 
153  .def ( "set",
154  ( void ( PyDataRep:: * ) // function pointer cast
155  ( Color::Value ) ) // function signature
156  &PyDataRep::set, // member function
157  "set ( ColorValue ) -> None\n"
158  "set ( Line ) -> None\n"
159  "set ( Symbol ) -> None\n"
160  "\n"
161  "Sets the color, line style or Symbol of the RepBase object.\n"
162  "Use ColorValue.values, Line.values, or Symbol.values to see\n"
163  "available values." )
164 
165  .def ( "set",
166  ( void ( PyDataRep:: * ) // function pointer cast
167  ( Line::Style ) ) // signature
168  &PyDataRep::set ) // member function
169 
170  .def ( "set",
171  ( void ( PyDataRep:: * ) // function pointer cast
172  ( Symbol::Type ) ) // signature
173  &PyDataRep::set ) // member function
174 
175  .def ( "setColor", &PyDataRep::setColor,
176  "setColor ( string ) -> None\n"
177  "\n"
178  "Sets the color of the point representation.\n"
179  "See DataRep.colorNames() for list of available colors." )
180 
181  .def ( "setErrorDisplay", &PyDataRep::setErrorDisplay,
182  "setErrorDisplay ( string, boolean ) -> None\n"
183  "\n"
184  "Turns on or off display of error bars, if available, on\n"
185  "specified axis." )
186 
187  .def ( "createNTuple", &PyDataRep::createNTuple,
188  return_value_policy < manage_new_object > (),
189  "createNTuple () -> NTuple\n"
190  "\n"
191  "Creates and returns an NTuple representation of the object." )
192 
193  .def ( "getNTupleWithCuts", &PyDataRep::getNTupleWithCuts,
194  return_value_policy < manage_new_object > (),
195  "getNTupleWithCuts () -> NTuple\n"
196  "\n"
197  "Creates and returns to NTuple being used, with cuts applied." )
198 
199  .def ( "getColumnWithCuts",
200  ( const std::vector<double> & (PyDataRep:: *)
201  ( const std::string & ) const )
202  &PyDataRep::getColumnWithCuts,
203  return_value_policy < copy_const_reference > (),
204  "getColumnWithCuts ( column ) -> tuple\n"
205  "\n"
206  "Creates and returns the named column, with cuts applied." )
207 
208  .def ( "createNTupleUnderRegion", &PyDataRep::createNTupleUnderRegion,
209  return_value_policy < manage_new_object > (),
210  "createNTupleUnderRegion () -> NTuple\n"
211  "\n"
212  "Creates and returns a NTuple contain only rows that pass the\n"
213  "region cuts." )
214 
215  .def ( "addCut", &PyDataRep::addCut,
216  "addCut ( ) -> None\n"
217  "\n"
218  "Adds a cut to limit the region given to a fitter." )
219 
220  .def ( "setCutRange", &PyDataRep::setCutRange,
221  "setCutRange ( low, high ) -> None\n"
222  "\n"
223  "Sets the low and high end of the region highlight" )
224 
225  .def ( "setSize", & PyDataRep::setSize,
226  "setSize ( value ) -> None\n"
227  "\n"
228  "Sets the size of the point representation." )
229 
230  .def ( "setSymbol", &PyDataRep::setSymbol,
231  "setSymbol ( string, value ) -> None\n"
232  "\n"
233  "Sets the point symbol and size. This method is deprecated, use\n"
234  "DataRep.set(Symbol) and DataRep.setSize() instead." )
235 
236  .def ( "setLineStyle", &PyDataRep::setLineStyle,
237  "setLineStyle ( string ) -> None\n"
238  "\n"
239  "Sets the line style.\n"
240  "This method is deprecated, use DataRep.set(Line) instead." )
241 
242  .def ( "normalizeTo", &PyDataRep::normalizeTo,
243  "normalizeTo ( DataRep ) -> None\n"
244  "\n"
245  "Sets the object to normalize itself to a target one" )
246 
247  .def ( "setBinWidth", &PyDataRep::setBinWidth,
248  "setBinWidth ( string, value ) -> None\n"
249  "\n"
250  "Sets the width of the bins, if data representation is binned.\n" )
251  ;
252 }
253 
254 } // namespace Python
255 } // namespace hippodraw
256 
257 using namespace hippodraw;
258 
259 std::map< std::string, hippodraw::Symbol::Type > PyDataRep::s_symbols;
260 std::map< std::string, hippodraw::Line::Style > PyDataRep::s_lineStyles;
261 bool PyDataRep::s_have_static_members(false);
262 
263 PyDataRep::PyDataRep ( DataRep * rep )
264 {
265  m_datarep = rep;
266  init();
267 }
268 
269 PyDataRep::PyDataRep ( const std::string & type,
270  const DataSource * ntuple,
271  const std::vector< std::string > & bindings )
272 {
273  DataRepController * controller = DataRepController::instance ();
274  m_datarep = controller->createDataRep ( type, ntuple, bindings );
275  init();
276 }
277 
278 PyDataRep::PyDataRep ( const std::string & type,
279  const PyDataSource * nt,
280  const std::vector< std::string > & bindings )
281 {
282  DataRepController * controller = DataRepController::instance ();
283  m_datarep = controller->createDataRep ( type, &(nt->dataSource()),
284  bindings );
285  init();
286 }
287 
288 PyDataRep::PyDataRep ()
289 {
290  m_datarep = 0;
291  init();
292 }
293 
294 PyDataRep::PyDataRep( PyDataRep * pyRep )
295 {
296  m_datarep = pyRep->getDataRep()->clone();
297 }
298 
299 const vector < string > &
300 PyDataRep::
301 names ()
302 {
303  DataRepController * controller = DataRepController::instance ();
304 
305  return controller -> names ();
306 }
307 
308 DataRep * PyDataRep::getDataRep()
309 {
310  return m_datarep;
311 }
312 
318 void PyDataRep::setPointRep ( RepBase * pointRep )
319 {
320  PyApp::lock();
321  m_datarep->setPointRep ( pointRep );
322  PyApp::unlock ();
323 }
324 
325 void PyDataRep::setAxisBinding ( const std::string & axis,
326  const std::string & label )
327 {
328  PyApp::lock();
329  try {
330  m_datarep->setAxisBinding ( axis, label );
331  }
332  catch ( runtime_error & e ) {
333  PyApp::unlock ();
334  throw e;
335  }
336  PyApp::unlock ();
337 }
338 
339 void PyDataRep::setAxisBindings ( const std::vector< std::string > & bindings)
340 {
341  PyApp::lock();
342  try {
343  m_datarep->setAxisBindings ( bindings );
344  }
345  catch ( runtime_error & e ) {
346  PyApp::unlock ();
347  throw e;
348  }
349  PyApp::unlock ();
350 }
351 
352 void PyDataRep::setWeight ( const std::string &label )
353 {
354  PyApp::lock();
355  if ( m_datarep->name() == "Histogram"
356  || m_datarep->name() == "Color Plot"
357  || m_datarep->name() == "Contour Plot") {
358  m_datarep->setAxisBinding( std::string("Weight (optional)"),
359  label );
360  } else {
361  // do nothing
362  }
363  PyApp::unlock ();
364 }
365 
366 const std::string & PyDataRep::name () const
367 {
368  return m_datarep->name();
369 }
370 
371 double PyDataRep::getBinWidth ( const std::string &axis )
372 {
373  PyApp::lock();
374  Axes::Type at = Axes::convert ( axis );
375  ProjectorBase * projector = m_datarep->getProjector ();
376  PyApp::unlock ();
377 
378  return projector->getBinWidth ( at );
379 }
380 
381 double
382 PyDataRep::
383 getMean ( const std::string & axis )
384 {
385  PyApp::lock();
386  double mean = m_datarep -> getMean ( axis );
387  PyApp::unlock ();
388 
389  return mean;
390 }
391 
392 double
393 PyDataRep::
394 getRMS ( const std::string & axis )
395 {
396  PyApp::lock();
397  double rms = m_datarep -> getRMS ( axis );
398  PyApp::unlock ();
399 
400  return rms;
401 }
402 
403 double PyDataRep::numberOfEntries() const
404 {
405  ProjectorBase * projector = m_datarep->getProjector();
406  return projector -> getNumberOfEntries();
407 }
408 
409 void PyDataRep::applyCut ( QtCut * cut )
410 {
411  PyApp::lock();
412  CutController * controller = CutController::instance();
413  CutPlotter * cp = dynamic_cast < CutPlotter * > ( cut -> display() );
414  controller -> linkCutAndRep( cp, m_datarep );
415  PyApp::unlock ();
416 }
417 
418 void PyDataRep::applyCuts ( const std::vector < QtCut * > & cuts )
419 {
420  PyApp::lock();
421  CutController * controller = CutController::instance();
422  unsigned int size = cuts.size();
423  for ( unsigned int i = 0; i < size; i++ ) {
424  CutPlotter * cp = dynamic_cast < CutPlotter * > ( cuts[i] -> display() );
425  controller -> linkCutAndRep( cp, m_datarep );
426  }
427 
428  PyApp::unlock ();
429 }
430 
431 const vector < string > &
432 PyDataRep::
433 colorNames ()
434 {
435  return Color::colorNames ();
436 }
437 
438 void
439 PyDataRep::
440 set ( Color::Value value )
441 {
442  PyApp::lock();
443  const RepBase * rep = m_datarep -> getRepresentation ();
444  if ( rep -> uses ( value ) ) {
445  m_datarep -> set ( value );
446  }
447  else {
448  PyApp::unlock ();
449  const string what ( "This DataRep does not use ColorValue." );
450  throw std::runtime_error ( what );
451  }
452  PyApp::unlock ();
453 }
454 
455 void
456 PyDataRep::
457 setColor ( const std::string & color )
458 {
459  PyApp::lock();
460 
461  if ( Color::isValid ( color ) ) {
462  Color colorObj ( color );
463  m_datarep -> setRepColor ( colorObj );
464  } else {
465  PyApp::unlock ();
466  std::string what ( "DataRep.setColor: color" );
467  what += " `";
468  what += color;
469  what += "' not available.";
470  throw runtime_error( what );
471  }
472  PyApp::unlock ();
473 }
474 
475 void
476 PyDataRep::
477 setErrorDisplay ( const std::string &axis, bool flag )
478 {
479  PyApp::lock();
480 
481  Axes::Type at = Axes::convert ( axis );
482  m_datarep->setErrorDisplay( at, flag );
483 
484  PyApp::unlock ();
485 }
486 
487 const NTuple *
488 PyDataRep::
489 createNTuple () const
490 {
491  PyApp::lock();
492 
493  const NTuple * ntuple = m_datarep -> createNTuple ();
494 
495  PyApp::unlock ();
496 
497  return ntuple;
498 }
499 
500 NTuple * PyDataRep::getNTupleWithCuts () const {
501  PyApp::lock();
502  NTuple * ntuple = m_datarep->getNTupleAfterCuts();
503  PyApp::unlock ();
504 
505  return ntuple;
506 }
507 
508 const std::vector<double> &
509 PyDataRep::
510 getColumnWithCuts( const std::string & column ) {
511  PyApp::unlock ();
512 
513  m_columnData.clear();
514  m_datarep->fillColumnAfterCuts(column, m_columnData);
515 
516  PyApp::unlock ();
517 
518  return m_columnData;
519 }
520 
521 NTuple *
522 PyDataRep::
523 createNTupleUnderRegion () const
524 {
525  PyApp::lock ();
526 
527  NTuple * nt = m_datarep -> createNTupleUnderRegion ();
528 
529  PyApp::unlock ();
530 
531  return nt;
532 }
533 
534 void
535 PyDataRep::
536 addCut ( )
537 {
538  PyApp::lock();
539  m_datarep -> addCut ( );
540  PyApp::unlock ();
541 }
542 
543 void
544 PyDataRep::
545 setCutRange ( double low, double high )
546 {
547  PyApp::lock();
548 
549  const Range range ( low, high );
550  m_datarep -> setCutRangeAt ( range, 0 );
551 
552  PyApp::unlock ();
553 }
554 
555 void PyDataRep::makeSymbolMap() {
556  s_symbols["square"] = hippodraw::Symbol::SQUARE;
557  s_symbols["filled_square"] = hippodraw::Symbol::SOLIDSQUARE;
558  s_symbols["plus"] = hippodraw::Symbol::PLUS;
559  s_symbols["times"] = hippodraw::Symbol::TIMES;
560  s_symbols["triangle"] = hippodraw::Symbol::TRIANGLE;
561  s_symbols["filled_triangle"] = hippodraw::Symbol::FILLED_TRIANGLE;
562  s_symbols["circle"] = hippodraw::Symbol::CIRCLE;
563  s_symbols["filled_circle"] = hippodraw::Symbol::FILLED_CIRCLE;
564 }
565 
566 void
567 PyDataRep::
569 {
570  PyApp::lock();
571  const RepBase * rep = m_datarep -> getRepresentation ();
572  if ( rep -> uses ( type ) ) {
573  m_datarep -> setRepStyle ( type );
574  }
575  else {
576  PyApp::unlock ();
577  const string what ( "This DataRep does not use Symbol type." );
578  throw std::runtime_error ( what );
579  }
580  PyApp::unlock ();
581 }
582 
583 void
584 PyDataRep::
585 setSize ( double size )
586 {
587  PyApp::lock();
588  m_datarep -> setRepSize ( size );
589  PyApp::unlock ();
590 }
591 
592 void PyDataRep::setSymbol( const std::string &symbolName, float size) {
593  PyApp::lock();
594  if ( s_symbols.count( symbolName ) ) {
595  m_datarep -> setRepStyle ( s_symbols[symbolName] );
596  m_datarep -> setRepSize ( size );
597  } else {
598  PyApp::unlock ();
599  std::ostringstream what;
600  what << "PyDataRep::setSymbol: symbol "
601  << symbolName << " is not available.\n"
602  << "Valid symbol names:\n";
603  std::map< std::string, hippodraw::Symbol::Type >
604  ::const_iterator it = s_symbols.begin();
605  for ( ; it != s_symbols.end(); it++) {
606  what << " " << it->first << "\n";
607  }
608  throw runtime_error( what.str() );
609  }
610  PyApp::unlock ();
611 }
612 
613 void PyDataRep::makeLineStyleMap() {
614  s_lineStyles["Solid"] = hippodraw::Line::Solid;
615  s_lineStyles["Dash"] = hippodraw::Line::Dash;
616  s_lineStyles["Dot"] = hippodraw::Line::Dot;
617  s_lineStyles["DashDot"] = hippodraw::Line::DashDot;
618  s_lineStyles["DashDotDot"] = hippodraw::Line::DashDotDot;
619  s_lineStyles["Invisible"] = hippodraw::Line::Invisible;
620 }
621 
622 void
623 PyDataRep::
624 set ( Line::Style style )
625 {
626  PyApp::lock();
627  const RepBase * rep = m_datarep -> getRepresentation ();
628  if ( rep -> uses ( style ) ) {
629  m_datarep -> setRepStyle ( style );
630  }
631  else {
632  PyApp::unlock ();
633  const string what ( "This DataRep does not use Line style." );
634  throw std::runtime_error ( what );
635  }
636 
637  PyApp::unlock ();
638 }
639 
640 void
641 PyDataRep::
642 setLineStyle( const std::string &lineStyleName )
643 {
644  PyApp::lock();
645  if ( s_lineStyles.count( lineStyleName ) ) {
646  m_datarep->setRepStyle( s_lineStyles[lineStyleName] );
647  } else {
648  PyApp::unlock ();
649  std::ostringstream what;
650  what << "PyDataRep::setLineStyle: lineStyle "
651  << lineStyleName << " is not available.\n"
652  << "Valid lineStyle names:\n";
653  std::map< std::string, hippodraw::Line::Style >
654  ::const_iterator it = s_lineStyles.begin();
655  for ( ; it != s_lineStyles.end(); it++) {
656  what << " " << it->first << "\n";
657  }
658  throw runtime_error( what.str() );
659  }
660  PyApp::unlock ();
661 }
662 
663 void PyDataRep::init() {
664  if (!s_have_static_members) {
665  makeSymbolMap();
666  makeLineStyleMap();
667 
668  s_have_static_members = true;
669  }
670 }
671 
672 void
673 PyDataRep::
674 normalizeTo ( const PyDataRep * rep )
675 {
676  PyApp::lock();
677 
678  const DataRep * datarep = rep -> m_datarep;
679  m_datarep -> normalizeTo ( datarep );
680 
681  PyApp::unlock ();
682 }
683 
684 void
685 PyDataRep::
686 setBinWidth ( const std::string & axis, double width )
687 {
688  PyApp::lock();
689 
690  Axes::Type type = Axes::convert ( axis );
691  DisplayController * controller = DisplayController::instance ();
692  controller -> setBinWidth ( m_datarep, type, width );
693  PyApp::unlock ();
694 }

Generated for HippoDraw Class Library by doxygen