CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/EcalPlugins/plugins/EcalDQMChannelStatusPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/EcalObjects/interface/EcalDQMChannelStatus.h"
00002 //#include "CondTools/Ecal/interface/EcalPedestalsXMLTranslator.h"
00003 #include "CondTools/Ecal/interface/EcalCondHeader.h"
00004 #include "TH2F.h"
00005 #include "TCanvas.h"
00006 #include "TStyle.h"
00007 #include "TLine.h"
00008 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00009 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00010 
00011 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00012 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00013 
00014 #include <string>
00015 #include <sstream>
00016 #include <algorithm>
00017 #include <numeric>
00018 #include <iterator>
00019 #include <boost/ref.hpp>
00020 #include <boost/bind.hpp>
00021 #include <boost/function.hpp>
00022 #include <boost/iterator/transform_iterator.hpp>
00023 #include <fstream>
00024 
00025 #include "CondCore/EcalPlugins/plugins/EcalPyWrapperFunctions.h"
00026 namespace cond {
00027 
00028         namespace ecalcond {
00029 
00030                 typedef EcalDQMChannelStatus::Items  Items;
00031                 typedef EcalDQMChannelStatus::value_type  value_type;
00032 
00033                 enum How { singleChannel, bySuperModule, all};
00034 
00035                 int bad(Items const & cont) {
00036                         return  std::count_if(cont.begin(),cont.end(),
00037                                 boost::bind(std::greater<int>(),
00038                                 boost::bind(&value_type::getStatusCode,_1),0)
00039                                 );
00040                 }
00041 
00042                 void extractBarrel(EcalDQMChannelStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00043                         result.resize(1);
00044                         result[0] =  bad(cont.barrelItems());
00045                 }
00046 
00047                 void extractEndcap(EcalDQMChannelStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00048                         result.resize(1);
00049                         result[0] = bad(cont.endcapItems());
00050                 }
00051                 void extractAll(EcalDQMChannelStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00052                         result.resize(1);
00053                         result[0] = bad(cont.barrelItems())+bad(cont.endcapItems());
00054                 }
00055 
00056                 void extractSuperModules(EcalDQMChannelStatus const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00057                         // bho...
00058                 }
00059 
00060                 void extractSingleChannel(EcalDQMChannelStatus const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00061                         result.reserve(which.size());
00062                         for (unsigned int i=0; i<which.size();i++) {
00063                                 result.push_back(cont[which[i]].getStatusCode());
00064                         }
00065                 }
00066 
00067                 typedef boost::function<void(EcalDQMChannelStatus const & cont, std::vector<int> const & which,  std::vector<float> & result)> CondExtractor;
00068         }  // namespace ecalcond
00069 
00070         template<>
00071         struct ExtractWhat<EcalDQMChannelStatus> {
00072 
00073                 ecalcond::How m_how;
00074                 std::vector<int> m_which;
00075 
00076                 ecalcond::How const & how() const { return m_how;}
00077                 std::vector<int> const & which() const { return m_which;}
00078 
00079                 void set_how(ecalcond::How i) {m_how=i;}
00080                 void set_which(std::vector<int> & i) { m_which.swap(i);}
00081         };
00082 
00083 
00084         template<>
00085         class ValueExtractor<EcalDQMChannelStatus>: public  BaseValueExtractor<EcalDQMChannelStatus> {
00086         public:
00087 
00088                 static ecalcond::CondExtractor & extractor(ecalcond::How how) {
00089                         static  ecalcond::CondExtractor fun[3] = { 
00090                                 ecalcond::CondExtractor(ecalcond::extractSingleChannel),
00091                                 ecalcond::CondExtractor(ecalcond::extractSuperModules),
00092                                 ecalcond::CondExtractor(ecalcond::extractAll)
00093                         };
00094                         return fun[how];
00095                 }
00096 
00097                 typedef EcalDQMChannelStatus Class;
00098                 typedef ExtractWhat<Class> What;
00099                 static What what() { return What();}
00100 
00101                 ValueExtractor(){}
00102                 ValueExtractor(What const & what)
00103                         : m_what(what)
00104                 {
00105                         // here one can make stuff really complicated... 
00106                 }
00107 
00108                 void compute(Class const & it){
00109                         std::vector<float> res;
00110                         extractor(m_what.how())(it,m_what.which(),res);
00111                         swap(res);
00112                 }
00113 
00114         private:
00115                 What  m_what;  
00116         };
00117 
00118 
00119         //template<>
00120         //std::string PayLoadInspector<EcalDQMChannelStatus>::dump() const {
00121         //  std::stringstream ss;
00122         //  EcalCondHeader h;
00123         //  ss << EcalDQMChannelStatusXMLTranslator::dumpXML(h,object());
00124         //  return ss.str();
00125         //}
00126 
00127         class EcalDQMChannelStatusHelper: public EcalPyWrapperHelper<EcalDQMStatusCode>{
00128         public:
00129                 //change me
00130                 EcalDQMChannelStatusHelper():EcalPyWrapperHelper<EcalObject>(30, STATUS, "-Errors total: "){}
00131         protected:
00132 
00133                 //change me
00134                 typedef EcalDQMStatusCode EcalObject;
00135 
00136                 type_vValues getValues( const std::vector<EcalObject> & vItems)
00137                 {
00138                         //change me
00139                         //unsigned int totalValues = 2; 
00140 
00141                         type_vValues vValues(total_values);
00142 
00143                         std::stringstream ss;
00144 
00145                         std::string valueNames[] = {
00146                                 "CH_ID_ERROR                          ",
00147                                 "CH_GAIN_ZERO_ERROR                   ",
00148                                 "CH_GAIN_SWITCH_ERROR                 ",
00149                                 "TT_ID_ERROR                          ",
00150                                 "TT_SIZE_ERROR                        ",
00151                                 "PEDESTAL_LOW_GAIN_MEAN_ERROR         ",
00152                                 "PEDESTAL_MIDDLE_GAIN_MEAN_ERROR      ",
00153                                 "PEDESTAL_HIGH_GAIN_MEAN_ERROR        ",
00154                                 "PEDESTAL_LOW_GAIN_RMS_ERROR          ",
00155                                 "PEDESTAL_MIDDLE_GAIN_RMS_ERROR       ",
00156                                 "PEDESTAL_HIGH_GAIN_RMS_ERROR         ",
00157                                 "PEDESTAL_ONLINE_HIGH_GAIN_MEAN_ERROR ",
00158                                 "PEDESTAL_ONLINE_HIGH_GAIN_RMS_ERROR  ",
00159                                 "TESTPULSE_LOW_GAIN_MEAN_ERROR        ",
00160                                 "TESTPULSE_MIDDLE_GAIN_MEAN_ERROR     ",
00161                                 "TESTPULSE_HIGH_GAIN_MEAN_ERROR       ",
00162                                 "TESTPULSE_LOW_GAIN_RMS_ERROR         ",
00163                                 "TESTPULSE_MIDDLE_GAIN_RMS_ERROR      ",
00164                                 "TESTPULSE_HIGH_GAIN_RMS_ERROR        ",
00165                                 "LASER_MEAN_ERROR                     ",
00166                                 "LASER_RMS_ERROR                      ",
00167                                 "LASER_TIMING_MEAN_ERROR              ",
00168                                 "LASER_TIMING_RMS_ERROR               ",
00169                                 "LED_MEAN_ERROR                       ",
00170                                 "LED_RMS_ERROR                        ",
00171                                 "LED_TIMING_MEAN_ERROR                ",
00172                                 "LED_TIMING_RMS_ERROR                 ",
00173                                 "STATUS_FLAG_ERROR                    ",
00174                                 "PHYSICS_BAD_CHANNEL_WARNING          ",
00175                                 "PHYSICS_BAD_CHANNEL_ERROR            "
00176                         };
00177                         for (unsigned int i = 0; i < total_values; ++i){
00178                                 ss.str(""); ss << "[" << i << "]" << valueNames[i];
00179                                 vValues[i].first = ss.str();
00180                                 vValues[i].second = .0;
00181                         }
00182 
00183                         //get info:
00184                         unsigned int shift = 0, mask = 1;
00185                         unsigned int statusCode;
00186                         for(std::vector<EcalObject>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems){
00187                                 //change us
00188                                 statusCode = iItems->getStatusCode();
00189                                 for (shift = 0; shift < total_values; ++shift){
00190                                         mask = 1 << (shift);
00191                                         //std::cout << "; statuscode: " << statusCode;
00192                                         if (statusCode & mask){
00193                                                 vValues[shift].second += 1;
00194                                         }
00195                                 }
00196                         }
00197                         return vValues;
00198                 }
00199         };
00200 
00201         template<>
00202         std::string PayLoadInspector<EcalDQMChannelStatus>::summary() const {
00203                 std::stringstream ss;
00204                 EcalDQMChannelStatusHelper helper;
00205                 ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
00206                 return ss.str();
00207         }
00208 
00209 
00210         template<>
00211         std::string PayLoadInspector<EcalDQMChannelStatus>::plot(std::string const & filename,
00212                 std::string const &, 
00213                 std::vector<int> const&, 
00214                 std::vector<float> const& ) const {
00215                         gStyle->SetPalette(1);
00216                         const int TOTAL_IMAGES = 1;
00217                         const int TOTAL_PADS = 3;
00218 
00219                         const float IMG_SIZE = 1.5;
00220                         TCanvas canvas("CC map","CC map",800*IMG_SIZE, 200 * TOTAL_IMAGES*IMG_SIZE);//800, 1200
00221 
00222                         float xmi[3] = {0.0 , 0.22, 0.78};
00223                         float xma[3] = {0.22, 0.78, 1.00};
00224 
00225 
00226                         TPad*** pad = new TPad**[TOTAL_IMAGES];
00227                         for (int gId = 0; gId < TOTAL_IMAGES; gId++) {
00228                                 pad[gId] = new TPad*[TOTAL_PADS];
00229                                 for (int obj = 0; obj < TOTAL_PADS; obj++) {
00230                                         float yma = 1. - 0;//1.- (0.17 * gId);
00231                                         float ymi = yma - 1;//0.15;
00232                                         pad[gId][obj] = new TPad(Form("p_%i_%i", obj, gId),Form("p_%i_%i", obj, gId),
00233                                                 xmi[obj], ymi, xma[obj], yma);
00234                                         pad[gId][obj]->Draw();
00235                                 }
00236                         }
00237 
00238                         const int kGains       = 3;
00239                         const int gainValues[3] = {12, 6, 1};
00240                         const int kSides       = 2;
00241                         const int kBarlRings   = EBDetId::MAX_IETA;
00242                         const int kBarlWedges  = EBDetId::MAX_IPHI;
00243                         const int kEndcWedgesX = EEDetId::IX_MAX;
00244                         const int kEndcWedgesY = EEDetId::IY_MAX;
00245 
00246                         TH2F** barrel_m = new TH2F*[3];
00247                         TH2F** endc_p_m = new TH2F*[3];
00248                         TH2F** endc_m_m = new TH2F*[3];
00249                         std::string variableName = "DQMChannelStatus";
00250                         for (int gainId = 0; gainId < kGains; gainId++) {
00251                                 barrel_m[gainId] = new TH2F(Form((variableName + " EBm%i").c_str(),gainId), Form((variableName + " EB").c_str(),gainValues[gainId]),360,0,360, 170, -85,85);
00252                                 endc_p_m[gainId] = new TH2F(Form((variableName + " EE+m%i").c_str(),gainId), Form((variableName + " EE+").c_str(),gainValues[gainId]),100,1,101,100,1,101);
00253                                 endc_m_m[gainId] = new TH2F(Form((variableName + " EE-m%i").c_str(),gainId), Form((variableName + " EE-").c_str(),gainValues[gainId]),100,1,101,100,1,101);
00254                         }
00255 
00256                         for (int sign=0; sign < kSides; sign++) {
00257                                 int thesign = sign==1 ? 1:-1;
00258 
00259                                 for (int ieta=0; ieta<kBarlRings; ieta++) {
00260                                         for (int iphi=0; iphi<kBarlWedges; iphi++) {
00261                                                 EBDetId id((ieta+1)*thesign, iphi+1);
00262                                                 float y = -1 - ieta;
00263                                                 if(sign == 1) y = ieta;
00264                                                 barrel_m[0]->Fill(iphi, y, object()[id.rawId()].getStatusCode());
00265                                         }  // iphi
00266                                 }   // ieta
00267 
00268                                 for (int ix=0; ix<kEndcWedgesX; ix++) {
00269                                         for (int iy=0; iy<kEndcWedgesY; iy++) {
00270                                                 if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
00271                                                 EEDetId id(ix+1,iy+1,thesign);
00272                                                 if (thesign==1) {
00273                                                         endc_p_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].getStatusCode());
00274                                                 }
00275                                                 else{ 
00276                                                         endc_m_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].getStatusCode());
00277                                                 }
00278                                         }  // iy
00279                                 }   // ix
00280                         }    // side
00281 
00282                         //canvas.cd(1);
00283                         TLine* l = new TLine(0., 0., 0., 0.);
00284                         l->SetLineWidth(1);
00285                         int ixSectorsEE[202] = {
00286                                 62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 
00287                                 41, 41, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 46, 46, 56, 56, 58, 58, 59, 59, 
00288                                 60, 60, 61, 61, 62, 62,  0,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 
00289                                 81, 76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,
00290                                 9,  6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 
00291                                 26, 36, 36, 41, 41, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 93, 93, 96, 
00292                                 96, 98, 98,101,101,  0, 62, 66, 66, 71, 71, 81, 81, 91, 91, 93,  0, 62, 66, 66, 
00293                                 91, 91, 98,  0, 58, 61, 61, 66, 66, 71, 71, 76, 76, 81, 81,  0, 51, 51,  0, 44, 
00294                                 41, 41, 36, 36, 31, 31, 26, 26, 21, 21,  0, 40, 36, 36, 11, 11,  4,  0, 40, 36, 
00295                                 36, 31, 31, 21, 21, 11, 11,  9,  0, 46, 46, 41, 41, 36, 36,  0, 56, 56, 61, 61, 66, 66};
00296 
00297                                 int iySectorsEE[202] = {
00298                                         51, 56, 56, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 61, 61, 60, 60, 59, 59, 58, 
00299                                         58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 41, 41, 42, 42, 43, 
00300                                         43, 44, 44, 46, 46, 51,  0, 51, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 
00301                                         93, 93, 96, 96, 98, 98,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 81, 
00302                                         76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,  9, 
00303                                         6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 26, 
00304                                         36, 36, 41, 41, 51,  0, 46, 46, 41, 41, 36, 36, 31, 31, 26, 26,  0, 51, 51, 56, 
00305                                         56, 61, 61,  0, 61, 61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 62,101,  0, 61, 
00306                                         61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 51, 51, 56, 56, 61, 61,  0, 46, 46, 
00307                                         41, 41, 36, 36, 31, 31, 26, 26,  0, 40, 31, 31, 16, 16,  6,  0, 40, 31, 31, 16, 16,  6};
00308 
00309                                         for (int gId = 0; gId < TOTAL_IMAGES; gId++) {//was 3
00310                                                 pad[gId][0]->cd();
00311                                                 endc_m_m[gId]->SetStats(0);
00312                                                 //endc_m_m[gId]->SetMaximum(225);
00313                                                 //endc_m_m[gId]->SetMinimum(175);
00314                                                 endc_m_m[gId]->Draw("colz");
00315                                                 for ( int i=0; i<201; i=i+1) {
00316                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00317                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00318                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00319                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00320                                                                         l->SetLineWidth(0.2);
00321                                                         }
00322                                                 }
00323 
00324                                                 //canvas.cd(2);
00325                                                 pad[gId][1]->cd();
00326                                                 barrel_m[gId]->SetStats(0);
00327                                                 //barrel_m[gId]->SetMaximum(225);
00328                                                 //barrel_m[gId]->SetMinimum(175);
00329                                                 barrel_m[gId]->Draw("colz");
00330                                                 for(int i = 0; i <17; i++) {
00331                                                         Double_t x = 20.+ (i *20);
00332                                                         l = new TLine(x,-85.,x,86.);
00333                                                         l->Draw();
00334                                                 }
00335                                                 l = new TLine(0.,0.,360.,0.);
00336                                                 l->Draw();
00337 
00338                                                 //canvas.cd(3);
00339                                                 pad[gId][2]->cd();
00340                                                 endc_p_m[gId]->SetStats(0);
00341                                                 //endc_p_m[gId]->SetMaximum(225);
00342                                                 //endc_p_m[gId]->SetMinimum(175);
00343                                                 endc_p_m[gId]->Draw("colz");
00344                                                 for ( int i=0; i<201; i=i+1) {
00345                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00346                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00347                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00348                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00349                                                         }
00350                                                 }
00351                                         }
00352 
00353                                         canvas.SaveAs(filename.c_str());
00354                                         return filename;
00355         }  // plot
00356 
00357 
00358 }
00359 
00360 
00361 namespace condPython {
00362         template<>
00363         void defineWhat<EcalDQMChannelStatus>() {
00364                 enum_<cond::ecalcond::How>("How")
00365                         .value("singleChannel",cond::ecalcond::singleChannel)
00366                         .value("bySuperModule",cond::ecalcond::bySuperModule) 
00367                         .value("all",cond::ecalcond::all)
00368                         ;
00369 
00370                 typedef cond::ExtractWhat<EcalDQMChannelStatus> What;
00371                 class_<What>("What",init<>())
00372                         .def("set_how",&What::set_how)
00373                         .def("set_which",&What::set_which)
00374                         .def("how",&What::how, return_value_policy<copy_const_reference>())
00375                         .def("which",&What::which, return_value_policy<copy_const_reference>())
00376                         ;
00377         }
00378 }
00379 
00380 PYTHON_WRAPPER(EcalDQMChannelStatus,EcalDQMChannelStatus);