CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/CalibPlugins/plugins/PedestalPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/Calibration/interface/Pedestals.h"
00002 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00003 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00004 
00005 #include <string>
00006 #include <sstream>
00007 #include <algorithm>
00008 #include <iterator>
00009 #include <boost/ref.hpp>
00010 #include <boost/bind.hpp>
00011 #include <boost/function.hpp>
00012 
00013 namespace {
00014   struct Printer {
00015     void doit(Pedestals::Item const & item) {
00016       ss << item.m_mean << "," << item.m_variance <<"; ";
00017     }
00018     std::stringstream ss;
00019   };
00020 }
00021 
00022 
00023 
00024 namespace cond {
00025 
00026   template<>
00027   struct ExtractWhat<Pedestals> {
00028 
00029     struct DescrQuantity {
00030       std::vector<std::string> me;
00031       DescrQuantity() : me(2) {
00032         me[0]="mean";
00033         me[1]="variance";
00034       }
00035     };
00036 
00037     // example using multiple choice (not static to make the python binding easier
00038     /*static*/ std::vector<std::string> const & descr_quantity() {
00039       static DescrQuantity d;
00040       return d.me;
00041     }
00042 
00043     std::vector<int> m_which;
00044     std::vector<int> const & which() const { return m_which;}
00045     void set_which(std::vector<int> & i) { m_which.swap(i);}
00046 
00047     std::vector<int> m_quantity;
00048     std::vector<int> const & quantity() const { return m_quantity;}
00049     void set_quantity(std::vector<int> & i) { m_quantity.swap(i);}
00050   };
00051 
00052   template<>
00053   class ValueExtractor<Pedestals>: public  BaseValueExtractor<Pedestals> {
00054   public:
00055 
00056     typedef Pedestals Class;
00057     typedef ExtractWhat<Class> What;
00058     static What what() { return What();}
00059 
00060     typedef boost::function<float(Pedestals::Item const &)> Value;
00061     static std::vector<Value> const & allValues() {
00062       static std::vector<Value> v(2);
00063       // shall be done only ones in the usual constructor (later)...
00064       v[0]=boost::bind(&Pedestals::Item::m_mean,_1);
00065       v[1]=boost::bind(&Pedestals::Item::m_variance,_1);
00066       return v;
00067     }
00068 
00069     ValueExtractor(){}
00070     ValueExtractor(What const & what)
00071       : m_which(what.which()), m_what(what.quantity().size())
00072     {
00073       // here one can make stuff really complicated...
00074       std::vector<Value> const & v = allValues();
00075       for (size_t i=0; i< m_what.size(); i++) m_what[i] = v[what.quantity()[i]];
00076     }
00077     void compute(Class const & it){
00078       for (size_t i=0; i<m_which.size(); ++i) {
00079         if (m_which[i]<  int(it.m_pedestals.size()))
00080           for (size_t j=0; j< m_what.size(); ++j) add(m_what[j](it.m_pedestals[m_which[i]]));
00081       }
00082     }
00083   private:
00084     std::vector<int> m_which;
00085     std::vector<Value> m_what;
00086   };
00087 
00088 
00089   template<>
00090   std::string
00091   PayLoadInspector<Pedestals>::dump() const {
00092     Printer p;
00093     std::for_each(object().m_pedestals.begin(),
00094                   object().m_pedestals.end(),boost::bind(&Printer::doit,boost::ref(p),_1));
00095     p.ss << std::endl;
00096     return p.ss.str();
00097   }
00098 
00099   /* use default....
00100     template<>
00101     std::string PayLoadInspector<Pedestals>::summary() const {
00102     std::stringstream ss;
00103     ss << "size="<<object().m_pedestals.size() <<";";
00104     ss << std::endl;
00105     return ss.str();
00106    }
00107   */
00108 }
00109 
00110 namespace condPython {
00111   template<>
00112   void defineWhat<Pedestals>() {
00113     using namespace boost::python;
00114     typedef cond::ExtractWhat<Pedestals> What;
00115     class_<What>("What",init<>())
00116       .def("set_which",&What::set_which)
00117       .def("which",&What::which, return_value_policy<copy_const_reference>())
00118       .def("set_quantity",&What::set_quantity)
00119       .def("quantity",&What::quantity, return_value_policy<copy_const_reference>())
00120       .def("descr_quantity",&What::descr_quantity, return_value_policy<copy_const_reference>())
00121       ;
00122 
00123   }
00124 }
00125 
00126 PYTHON_WRAPPER(Pedestals,Pedestal);