CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CondCore/EcalPlugins/plugins/EcalFloatCondObjectContainerPyWrapper.cc

Go to the documentation of this file.
00001 
00002 
00003 #include "CondFormats/EcalObjects/interface/EcalCondObjectContainer.h"
00004 #include "TH2F.h"
00005 #include "TCanvas.h"
00006 #include "TStyle.h"
00007 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00008 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00009 #include "CondTools/Ecal/interface/EcalFloatCondObjectContainerXMLTranslator.h"
00010 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00011 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00012 
00013 #include <string>
00014 
00015 #include <fstream>
00016 #include <sstream>
00017 #include <algorithm>
00018 #include <numeric>
00019 #include <iterator>
00020 #include <boost/ref.hpp>
00021 #include <boost/bind.hpp>
00022 #include <boost/function.hpp>
00023 #include <boost/iterator/transform_iterator.hpp>
00024 #include <algorithm>
00025 
00026 namespace {
00027   struct Printer {
00028     Printer() : i(0){}
00029     void reset() { i=0;}
00030     void doB(float const & item) {
00031       ss << i <<":"<< item << "\n";
00032       i++;
00033     }
00034     void doE(float const & item) {
00035       ss << i <<":"<< item << "\n";
00036       i++;
00037     }
00038     int i;
00039     std::stringstream ss;
00040   };
00041 }
00042 
00043 
00044 namespace cond {
00045 
00046   // migrate to a common trait (when fully understood)
00047   namespace ecalcond {
00048 
00049     typedef EcalFloatCondObjectContainer Container;
00050     typedef Container::value_type  value_type;
00051 
00052     enum How { singleChannel, bySuperModule, barrel, endcap, all};
00053 
00054 
00055     void extractBarrel(Container const & cont, std::vector<int> const &,  std::vector<float> & result) {
00056       result.resize(cont.barrelItems().size());
00057       std::copy(cont.barrelItems().begin(),cont.barrelItems().end(),result.begin());
00058     }
00059 
00060     void extractEndcap(Container const & cont, std::vector<int> const &,  std::vector<float> & result) {
00061       result.resize(cont.endcapItems().size());
00062       std::copy(cont.endcapItems().begin(),cont.endcapItems().end(),result.begin());
00063     }
00064 
00065      void extractAll(Container const & cont, std::vector<int> const &,  std::vector<float> & result) {
00066 
00067        // EcalCondObjectContainer does not have a real iterator ... 
00068       result.resize(cont.barrelItems().size()+ cont.endcapItems().size());
00069       
00070       std::copy(cont.barrelItems().begin(),cont.barrelItems().end(),result.begin());
00071       std::copy(cont.endcapItems().begin(),cont.endcapItems().end(),result.end());
00072     }
00073     
00074     void extractSuperModules(Container const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00075       // bho...
00076     }
00077 
00078     void extractSingleChannel(Container const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00079       result.reserve(which.size());
00080       for (unsigned int i=0; i<which.size();i++) {
00081           result.push_back(cont[which[i]]);
00082       }
00083     }
00084 
00085     typedef boost::function<void(Container const & cont, std::vector<int> const & which,  std::vector<float> & result)> CondExtractor;
00086 
00087   } // ecalcond
00088 
00089   template<>
00090   struct ExtractWhat<ecalcond::Container> {
00091     
00092     ecalcond::How m_how;
00093     std::vector<int> m_which;
00094     
00095     ecalcond::How const & how() const { return m_how;}
00096     std::vector<int> const & which() const { return m_which;}
00097     
00098     void set_how(ecalcond::How i) {m_how=i;}
00099     void set_which(std::vector<int> & i) { m_which.swap(i);}
00100   };
00101   
00102 
00103 
00104 
00105   template<>
00106   class ValueExtractor<ecalcond::Container>: public  BaseValueExtractor<ecalcond::Container> {
00107   public:
00108     
00109     static ecalcond::CondExtractor & extractor(ecalcond::How how) {
00110       static  ecalcond::CondExtractor fun[5] = { 
00111         ecalcond::CondExtractor(ecalcond::extractSingleChannel),
00112         ecalcond::CondExtractor(ecalcond::extractSuperModules),
00113         ecalcond::CondExtractor(ecalcond::extractBarrel),
00114         ecalcond::CondExtractor(ecalcond::extractEndcap),
00115         ecalcond::CondExtractor(ecalcond::extractAll)
00116       };
00117       return fun[how];
00118     }
00119     
00120     
00121     typedef ecalcond::Container Class;
00122     typedef ExtractWhat<Class> What;
00123     static What what() { return What();}
00124     
00125     ValueExtractor(){}
00126     ValueExtractor(What const & what)
00127       : m_what(what)
00128     {
00129       // here one can make stuff really complicated... 
00130       // ask to make average on selected channels...
00131     }
00132     
00133     void compute(Class const & it){
00134       std::vector<float> res;
00135       extractor(m_what.how())(it,m_what.which(),res);
00136       swap(res);
00137     }
00138     
00139   private:
00140     What  m_what;
00141     
00142   };
00143   
00144   
00145   template<>
00146   std::string
00147   PayLoadInspector<EcalFloatCondObjectContainer>::dump() const {
00148 
00149     std::stringstream ss;
00150     EcalCondHeader header;
00151     ss<<EcalFloatCondObjectContainerXMLTranslator::dumpXML(header,object());
00152     return ss.str();
00153 
00154  //    Printer p;
00155 //     std::for_each(object().barrelItems().begin(),object().barrelItems().end(),boost::bind(&Printer::doB,boost::ref(p),_1));
00156 //     p.ss <<"\n";
00157 //     p.reset();
00158 //     std::for_each(object().endcapItems().begin(),object().endcapItems().end(),boost::bind(&Printer::doE,boost::ref(p),_1));
00159 //     p.ss << std::endl;
00160 //     return p.ss.str();
00161   }
00162   
00163   template<>
00164   std::string PayLoadInspector<EcalFloatCondObjectContainer>::summary() const {
00165 
00166     std::stringstream ss;
00167 
00168     const int kSides       = 2;
00169     const int kBarlRings   = EBDetId::MAX_IETA;
00170     const int kBarlWedges  = EBDetId::MAX_IPHI;
00171     const int kEndcWedgesX = EEDetId::IX_MAX;
00172     const int kEndcWedgesY = EEDetId::IY_MAX;
00173 
00175 
00176     float mean_x_EB=0;
00177     float mean_xx_EB=0;
00178     int num_x_EB=0;
00179 
00180     float mean_x_EE=0;
00181     float mean_xx_EE=0;
00182     int num_x_EE=0;
00183 
00184 
00185     for (int sign=0; sign<kSides; sign++) {
00186 
00187        int thesign = sign==1 ? 1:-1;
00188 
00189        for (int ieta=0; ieta<kBarlRings; ieta++) {
00190          for (int iphi=0; iphi<kBarlWedges; iphi++) {
00191            EBDetId id((ieta+1)*thesign, iphi+1);
00192             float x= object()[id.rawId()];
00193             num_x_EB++;
00194             mean_x_EB=mean_x_EB+x;
00195             mean_xx_EB=mean_xx_EB+x*x;
00196          }
00197        }
00198 
00199        for (int ix=0; ix<kEndcWedgesX; ix++) {
00200          for (int iy=0; iy<kEndcWedgesY; iy++) {
00201            if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
00202            EEDetId id(ix+1,iy+1,thesign);
00203             float x=object()[id.rawId()];
00204             num_x_EE++;
00205             mean_x_EE=mean_x_EE+x;
00206             mean_xx_EE=mean_xx_EE+x*x;
00207 
00208          }//iy
00209        }//ix
00210 
00211 
00212     }
00213 
00214     mean_x_EB=mean_x_EB/num_x_EB;
00215     mean_x_EE=mean_x_EE/num_x_EE;
00216     mean_xx_EB=mean_xx_EB/num_x_EB;
00217     mean_xx_EE=mean_xx_EE/num_x_EE;
00218     float rms_EB=(mean_xx_EB-mean_x_EB*mean_x_EB);
00219     float rms_EE=(mean_xx_EE-mean_x_EE*mean_x_EE);
00220 
00221     ss << "ECAL BARREL Mean: "<< mean_x_EB <<" RMS: "<<  rms_EB << " Nchan: "<< num_x_EB<< std::endl
00222        << "ECAL Endcap Mean: "<< mean_x_EE <<" RMS: "<<  rms_EE << " Nchan: "<< num_x_EE<< std::endl ;
00223 
00224 
00225     return ss.str();
00226   }
00227   
00228 
00229 
00230   template<>
00231   std::string PayLoadInspector<EcalFloatCondObjectContainer>::plot(std::string const & filename,
00232                                                                    std::string const &, 
00233                                                                    std::vector<int> const&, 
00234                                                                    std::vector<float> const& ) const {
00235 
00236     gStyle->SetPalette(1);
00237     TCanvas canvas("CC map","CC map",840,280);
00238     //canvas.Divide(3,1);
00239  
00240     TPad pad1("p1","p1", 0.0, 0.0, 0.2, 1.0);
00241     TPad pad2("p2","p2", 0.2, 0.0, 0.8, 1.0);
00242     TPad pad3("p3","p3", 0.8, 0.0, 1.0, 1.0);
00243     pad1.Draw();
00244     pad2.Draw();
00245     pad3.Draw();
00246 
00247     TH2F barrelmap("EB","EB",360,1,360, 171, -85,86);
00248     TH2F endcmap_p("EE+","EE+",100,1,101,100,1,101);
00249     TH2F endcmap_m("EE-","EE-",100,1,101,100,1,101);
00250 
00251     const int kSides       = 2;
00252     const int kBarlRings   = EBDetId::MAX_IETA;
00253     const int kBarlWedges  = EBDetId::MAX_IPHI;
00254     const int kEndcWedgesX = EEDetId::IX_MAX;
00255     const int kEndcWedgesY = EEDetId::IY_MAX;
00256 
00258     for (int sign=0; sign<kSides; sign++) {
00259 
00260        int thesign = sign==1 ? 1:-1;
00261 
00262        for (int ieta=0; ieta<kBarlRings; ieta++) {
00263          for (int iphi=0; iphi<kBarlWedges; iphi++) {
00264            EBDetId id((ieta+1)*thesign, iphi+1);
00265            barrelmap.Fill(iphi+1,ieta*thesign + thesign, object()[id.rawId()]);
00266          }
00267        }
00268 
00269        for (int ix=0; ix<kEndcWedgesX; ix++) {
00270          for (int iy=0; iy<kEndcWedgesY; iy++) {
00271            if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
00272            EEDetId id(ix+1,iy+1,thesign);
00273     
00274            if (thesign==1) {
00275              endcmap_p.Fill(ix+1,iy+1,object()[id.rawId()]);
00276            }
00277            else{ 
00278              endcmap_m.Fill(ix+1,iy+1,object()[id.rawId()]);
00279         
00280            }
00281          }//iy
00282        }//ix
00283 
00284 
00285 
00286     }
00287 
00288     
00289     //canvas.cd(1);
00290     pad1.cd();
00291     endcmap_m.SetStats(0);
00292     endcmap_m.Draw("colz");
00293     //canvas.cd(2);
00294     pad2.cd();
00295     barrelmap.SetStats(0);
00296     barrelmap.Draw("colz");
00297     //canvas.cd(3);
00298     pad3.cd();
00299     endcmap_p.SetStats(0);
00300     endcmap_p.Draw("colz");
00301 
00302     canvas.SaveAs(filename.c_str());
00303     return filename;
00304   }
00305   
00306   
00307 }
00308 
00309 namespace condPython {
00310   template<>
00311   void defineWhat<cond::ecalcond::Container>() {
00312     using namespace boost::python;
00313     enum_<cond::ecalcond::How>("How")
00314       .value("singleChannel",cond::ecalcond::singleChannel)
00315       .value("bySuperModule",cond::ecalcond::bySuperModule) 
00316       .value("barrel",cond::ecalcond::barrel)
00317       .value("endcap",cond::ecalcond::endcap)
00318       .value("all",cond::ecalcond::all)
00319       ;
00320     
00321     typedef cond::ExtractWhat<cond::ecalcond::Container> What;
00322     class_<What>("What",init<>())
00323       .def("set_how",&What::set_how)
00324       .def("set_which",&What::set_which)
00325       .def("how",&What::how, return_value_policy<copy_const_reference>())
00326       .def("which",&What::which, return_value_policy<copy_const_reference>())
00327       ;
00328   }
00329 }
00330 
00331 
00332 
00333 
00334 PYTHON_WRAPPER(EcalFloatCondObjectContainer,EcalFloatCondObjectContainer);