CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondCore/EcalPlugins/plugins/EcalDQMTowerStatusPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/EcalObjects/interface/EcalDQMTowerStatus.h"
00002 //#include "CondTools/Ecal/interface/EcalDQMTowerStatusXMLTranslator.h"
00003 #include "CondTools/Ecal/interface/EcalCondHeader.h"
00004 #include "TROOT.h"
00005 #include "TH2F.h"
00006 #include "TCanvas.h"
00007 #include "TStyle.h"
00008 #include "TColor.h"
00009 #include "TLine.h"
00010 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00011 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00012 
00013 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00014 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00015 
00016 #include <string>
00017 #include <sstream>
00018 #include <algorithm>
00019 #include <numeric>
00020 #include <iterator>
00021 #include <boost/ref.hpp>
00022 #include <boost/bind.hpp>
00023 #include <boost/function.hpp>
00024 #include <boost/iterator/transform_iterator.hpp>
00025 
00026 #include <fstream>
00027 
00028 #include "CondCore/EcalPlugins/plugins/EcalPyWrapperFunctions.h"
00029 
00030 namespace cond {
00031 
00032   namespace ecalcond {
00033 
00034     typedef EcalDQMTowerStatus::Items  Items;
00035     typedef EcalDQMTowerStatus::value_type  value_type;
00036 
00037     enum How { singleChannel, bySuperModule, all};
00038 
00039     int bad(Items const & cont) {
00040       return  std::count_if(cont.begin(),cont.end(),
00041                             boost::bind(std::greater<int>(),
00042                                         boost::bind(&value_type::getStatusCode,_1),0)
00043                             );
00044     }
00045 
00046     void extractBarrel(EcalDQMTowerStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00047       result.resize(1);
00048       result[0] =  bad(cont.barrelItems());
00049     }
00050     
00051     void extractEndcap(EcalDQMTowerStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00052       result.resize(1);
00053       result[0] = bad(cont.endcapItems());
00054     }
00055     void extractAll(EcalDQMTowerStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00056       result.resize(1);
00057       result[0] = bad(cont.barrelItems())+bad(cont.endcapItems());
00058     }
00059 
00060     void extractSuperModules(EcalDQMTowerStatus const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00061       // bho...
00062     }
00063 
00064     void extractSingleChannel(EcalDQMTowerStatus const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00065       result.reserve(which.size());
00066       for (unsigned int i=0; i<which.size();i++) {
00067         result.push_back(cont[which[i]].getStatusCode());
00068       }
00069     }
00070 
00071         typedef boost::function<void(EcalDQMTowerStatus const & cont, std::vector<int> const & which,  std::vector<float> & result)> CondExtractor;
00072   }  // namespace ecalcond
00073 
00074   template<>
00075   struct ExtractWhat<EcalDQMTowerStatus> {
00076 
00077     ecalcond::How m_how;
00078     std::vector<int> m_which;
00079 
00080     ecalcond::How const & how() const { return m_how;}
00081     std::vector<int> const & which() const { return m_which;}
00082  
00083     void set_how(ecalcond::How i) {m_how=i;}
00084     void set_which(std::vector<int> & i) { m_which.swap(i);}
00085   };
00086 
00087 
00088   template<>
00089   class ValueExtractor<EcalDQMTowerStatus>: public  BaseValueExtractor<EcalDQMTowerStatus> {
00090   public:
00091 
00092     static ecalcond::CondExtractor & extractor(ecalcond::How how) {
00093       static  ecalcond::CondExtractor fun[3] = { 
00094         ecalcond::CondExtractor(ecalcond::extractSingleChannel),
00095         ecalcond::CondExtractor(ecalcond::extractSuperModules),
00096         ecalcond::CondExtractor(ecalcond::extractAll)
00097       };
00098       return fun[how];
00099     }
00100 
00101     typedef EcalDQMTowerStatus Class;
00102     typedef ExtractWhat<Class> What;
00103     static What what() { return What();}
00104 
00105     ValueExtractor(){}
00106     ValueExtractor(What const & what)
00107       : m_what(what)
00108     {
00109       // here one can make stuff really complicated... 
00110     }
00111 
00112     void compute(Class const & it){
00113       std::vector<float> res;
00114       extractor(m_what.how())(it,m_what.which(),res);
00115       swap(res);
00116     }
00117 
00118   private:
00119     What  m_what;  
00120   };
00121 
00122 
00123   //template<>
00124   //std::string PayLoadInspector<EcalDQMTowerStatus>::dump() const {
00125   //  std::stringstream ss;
00126   //  EcalCondHeader h;
00127   //  ss << EcalDQMTowerStatusXMLTranslator::dumpXML(h,object());
00128   //  return ss.str();
00129   //}
00130 
00131         class EcalDQMTowerStatusHelper: public EcalPyWrapperHelper<EcalDQMStatusCode>{
00132         public:
00133                 //change me
00134                 EcalDQMTowerStatusHelper():EcalPyWrapperHelper<EcalObject>(30, STATUS, "-Errors total: "){}
00135         protected:
00136 
00137                 //change me
00138                 typedef EcalDQMStatusCode EcalObject;
00139 
00140                 type_vValues getValues( const std::vector<EcalObject> & vItems)
00141                 {
00142                         //change me
00143                         //unsigned int totalValues = 2; 
00144 
00145                         type_vValues vValues(total_values);
00146 
00147                         std::stringstream ss;
00148 
00149                         std::string valueNames[] = {
00150                                 "CH_ID_ERROR                          ",
00151                                 "CH_GAIN_ZERO_ERROR                   ",
00152                                 "CH_GAIN_SWITCH_ERROR                 ",
00153                                 "TT_ID_ERROR                          ",
00154                                 "TT_SIZE_ERROR                        ",
00155                                 "PEDESTAL_LOW_GAIN_MEAN_ERROR         ",
00156                                 "PEDESTAL_MIDDLE_GAIN_MEAN_ERROR      ",
00157                                 "PEDESTAL_HIGH_GAIN_MEAN_ERROR        ",
00158                                 "PEDESTAL_LOW_GAIN_RMS_ERROR          ",
00159                                 "PEDESTAL_MIDDLE_GAIN_RMS_ERROR       ",
00160                                 "PEDESTAL_HIGH_GAIN_RMS_ERROR         ",
00161                                 "PEDESTAL_ONLINE_HIGH_GAIN_MEAN_ERROR ",
00162                                 "PEDESTAL_ONLINE_HIGH_GAIN_RMS_ERROR  ",
00163                                 "TESTPULSE_LOW_GAIN_MEAN_ERROR        ",
00164                                 "TESTPULSE_MIDDLE_GAIN_MEAN_ERROR     ",
00165                                 "TESTPULSE_HIGH_GAIN_MEAN_ERROR       ",
00166                                 "TESTPULSE_LOW_GAIN_RMS_ERROR         ",
00167                                 "TESTPULSE_MIDDLE_GAIN_RMS_ERROR      ",
00168                                 "TESTPULSE_HIGH_GAIN_RMS_ERROR        ",
00169                                 "LASER_MEAN_ERROR                     ",
00170                                 "LASER_RMS_ERROR                      ",
00171                                 "LASER_TIMING_MEAN_ERROR              ",
00172                                 "LASER_TIMING_RMS_ERROR               ",
00173                                 "LED_MEAN_ERROR                       ",
00174                                 "LED_RMS_ERROR                        ",
00175                                 "LED_TIMING_MEAN_ERROR                ",
00176                                 "LED_TIMING_RMS_ERROR                 ",
00177                                 "STATUS_FLAG_ERROR                    ",
00178                                 "PHYSICS_BAD_CHANNEL_WARNING          ",
00179                                 "PHYSICS_BAD_CHANNEL_ERROR            "
00180                         };
00181                         for (unsigned int i = 0; i < total_values; ++i){
00182                                 ss.str(""); ss << "[" << i << "]" << valueNames[i];
00183                                 vValues[i].first = ss.str();
00184                                 vValues[i].second = .0;
00185                         }
00186                         
00187                         //get info:
00188                         unsigned int shift = 0, mask = 1;
00189                         unsigned int statusCode;
00190                         for(std::vector<EcalObject>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems){
00191                                 //change us
00192                                 statusCode = iItems->getStatusCode();
00193                                 for (shift = 0; shift < total_values; ++shift){
00194                                         mask = 1 << (shift);
00195                                         //std::cout << "; statuscode: " << statusCode;
00196                                         if (statusCode & mask){
00197                                                 vValues[shift].second += 1;
00198                                         }
00199                                 }
00200                         }
00201                         return vValues;
00202                 }
00203         };
00204 
00205    template<>
00206    std::string PayLoadInspector<EcalDQMTowerStatus>::summary() const {
00207         std::stringstream ss;
00208         EcalDQMTowerStatusHelper helper;
00209         ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
00210         return ss.str();
00211    }
00212 
00213    // return the real name of the file including extension...
00214   template<>
00215   std::string PayLoadInspector<EcalDQMTowerStatus>::plot(std::string const & filename,
00216                                                          std::string const &, 
00217                                                          std::vector<int> const&, 
00218                                                          std::vector<float> const& ) const {
00219     //    std::string fname = filename + ".txt";
00220     //    EcalDAQTowerStatusXMLTranslator::plot(fname, object());
00221     //    return fname;
00222     TCanvas canvas("CC map","CC map",800,800);
00223     TPad* padb = new TPad("padb","padb", 0., 0.55, 1., 1.);
00224     padb->Draw();
00225     TPad* padem = new TPad("padem","padem", 0., 0., 0.45, 0.45);
00226     padem->Draw();
00227     TPad* padep = new TPad("padep","padep", 0.55, 0., 1., 0.45);
00228     padep->Draw();
00229 
00230     TH2F* barrel = new TH2F("EB","EB Tower Status", 72, 0, 72, 34, -17, 17);
00231     TH2F* endc_p = new TH2F("EE+","EE+ Tower Status",22, 0, 22, 22, 0, 22);
00232     TH2F* endc_m = new TH2F("EE-","EE- Tower Status",22, 0, 22, 22, 0, 22);
00233     for(uint cellid = 0;
00234         cellid < EcalTrigTowerDetId::kEBTotalTowers;
00235         ++cellid) {
00236       EcalTrigTowerDetId rawid = EcalTrigTowerDetId::detIdFromDenseIndex(cellid);
00237       if (object().find(rawid) == object().end()) continue;
00238       int ieta = rawid.ieta();
00239       if(ieta > 0) ieta--;   // 1 to 17
00240       int iphi = rawid.iphi() - 1;  // 0 to 71
00241       barrel->Fill(iphi, ieta, object()[rawid].getStatusCode());
00242     }
00243     for(uint cellid = 0;
00244         cellid < EcalTrigTowerDetId::kEETotalTowers;
00245         ++cellid) {
00246       if(EcalScDetId::validHashIndex(cellid)) {
00247         EcalScDetId rawid = EcalScDetId::unhashIndex(cellid); 
00248         int ix = rawid.ix();  // 1 to 20
00249         int iy = rawid.iy();  // 1 to 20
00250         int side = rawid.zside();
00251         if(side == -1)
00252           endc_m->Fill(ix, iy, object()[rawid].getStatusCode());
00253         else
00254           endc_p->Fill(ix, iy, object()[rawid].getStatusCode());
00255       }
00256     }
00257     TLine* l = new TLine(0., 0., 0., 0.);
00258     l->SetLineWidth(1);
00259     padb->cd();
00260     barrel->SetStats(0);
00261     //    barrel->SetMaximum(14);
00262     //    barrel->SetMinimum(0);
00263     //    barrel->Draw("colz");
00264     barrel->Draw("col");
00265     for(int i = 0; i <17; i++) {
00266       Double_t x = 4.+ (i * 4);
00267       l = new TLine(x, -17., x, 17.);
00268       l->Draw();
00269     }
00270     l = new TLine(0., 0., 72., 0.);
00271     l->Draw();
00272 
00273     int ixSectorsEE[136] = {
00274        8,14,14,17,17,18,18,19,19,20,20,21,21,20,20,19,19,18,18,17,
00275       17,14,14, 8, 8, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 4,
00276        4, 5, 5, 8, 8, 8, 9, 9,10,10,12,12,13,13,12,12,10,10, 9, 9,
00277       10,10, 0,11,11, 0,10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 0,12,13,
00278       13,14,14,15,15,16,16,17,17, 0, 9, 8, 8, 3, 3, 1, 0,13,14,14,
00279       19,19,21, 0, 9, 8, 8, 7, 7, 5, 5, 3, 3, 2, 0,13,14,14,15,15,
00280       17,17,19,19,20, 0,14,14,13,13,12,12,0};
00281     int iySectorsEE[136] = {
00282        1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 8, 8,14,14,17,17,18,18,19,19,
00283       20,20,21,21,20,20,19,19,18,18,17,17,14,14, 8, 8, 5, 5, 4, 4,
00284        3, 3, 2, 2, 1, 4, 4, 7, 7, 9, 9,10,10,12,12,13,13,12,12,10,
00285       10, 9, 0,13,21, 0,13,13,14,14,15,15,16,16,18,18,19, 0,13,13,
00286       14,14,15,15,16,16,18,18,19, 0,11,11,12,12,13,13, 0,11,11,12,
00287       12,13,13, 0,10,10, 9, 9, 8, 8, 7, 7, 6, 6, 0,10,10, 9, 9, 8,
00288        8, 7, 7, 6, 6, 0, 2, 4, 4, 7, 7, 9, 0} ;
00289     padem->cd();
00290     endc_m->SetStats(0);
00291     endc_m->Draw("col");
00292     for ( int i = 0; i < 136; i=i+1) {
00293       if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00294            (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00295         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00296                     ixSectorsEE[i+1], iySectorsEE[i+1]);
00297       }
00298     }
00299 
00300     padep->cd();
00301     endc_p->SetStats(0);
00302     endc_p->Draw("col");
00303     for ( int i = 0; i < 136; i=i+1) {
00304       if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00305            (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00306         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00307                     ixSectorsEE[i+1], iySectorsEE[i+1]);
00308       }
00309     }
00310 
00311     canvas.SaveAs(filename.c_str());
00312     return filename;
00313   }
00314 }
00315 
00316 
00317 namespace condPython {
00318   template<>
00319   void defineWhat<EcalDQMTowerStatus>() {
00320     enum_<cond::ecalcond::How>("How")
00321       .value("singleChannel",cond::ecalcond::singleChannel)
00322       .value("bySuperModule",cond::ecalcond::bySuperModule) 
00323       .value("all",cond::ecalcond::all)
00324       ;
00325 
00326     typedef cond::ExtractWhat<EcalDQMTowerStatus> What;
00327     class_<What>("What",init<>())
00328       .def("set_how",&What::set_how)
00329       .def("set_which",&What::set_which)
00330       .def("how",&What::how, return_value_policy<copy_const_reference>())
00331       .def("which",&What::which, return_value_policy<copy_const_reference>())
00332       ;
00333   }
00334 }
00335 
00336 PYTHON_WRAPPER(EcalDQMTowerStatus,EcalDQMTowerStatus);