CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #include "CondFormats/EcalObjects/interface/EcalTPGCrystalStatus.h"
00002 //#include "CondTools/Ecal/interface/EcalTPGCrystalStatusXMLTranslator.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 EcalTPGCrystalStatus::Items  Items;
00035     typedef EcalTPGCrystalStatus::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(EcalTPGCrystalStatus 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(EcalTPGCrystalStatus const & cont, std::vector<int> const &,  std::vector<float> & result) {
00052       result.resize(1);
00053       result[0] = bad(cont.endcapItems());
00054     }
00055     void extractAll(EcalTPGCrystalStatus 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(EcalTPGCrystalStatus const & cont, std::vector<int> const & which,  std::vector<float> & result) {
00061       // bho...
00062     }
00063 
00064     void extractSingleChannel(EcalTPGCrystalStatus 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(EcalTPGCrystalStatus const & cont, std::vector<int> const & which,  std::vector<float> & result)> CondExtractor;
00072   }  // namespace ecalcond
00073 
00074   template<>
00075   struct ExtractWhat<EcalTPGCrystalStatus> {
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<EcalTPGCrystalStatus>: public  BaseValueExtractor<EcalTPGCrystalStatus> {
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 EcalTPGCrystalStatus 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<EcalTPGCrystalStatus>::dump() const {
00125   //  std::stringstream ss;
00126   //  EcalCondHeader h;
00127   //  ss << EcalTPGCrystalStatusXMLTranslator::dumpXML(h,object());
00128   //  return ss.str();
00129   //}
00130 
00131         class EcalTPGCrystalStatusHelper: public EcalPyWrapperHelper<EcalTPGCrystalStatusCode>{
00132         public:
00133                 //change me
00134                 EcalTPGCrystalStatusHelper():EcalPyWrapperHelper<EcalObject>(1, STATUS, "-Errors total: "){}
00135         protected:
00136 
00137                 //change me
00138                 typedef EcalTPGCrystalStatusCode 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                         //change us
00148                         vValues[0].first = "[0]StatusCode";
00149 
00150                         
00151                         vValues[0].second = .0;
00152         
00153                         //get info:
00154                         unsigned int shift = 0, mask = 1;
00155                         unsigned int statusCode;
00156                         for(std::vector<EcalObject>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems){
00157                                 //change us
00158                                 statusCode =  iItems->getStatusCode();
00159                                 for (shift = 0; shift < total_values; ++shift){
00160                                         mask = 1 << (shift);
00161                                         //std::cout << "; statuscode: " << statusCode;
00162                                         if (statusCode & mask){
00163                                                 vValues[shift].second += 1;
00164                                         }
00165                                 }
00166                         }
00167                         return vValues;
00168                 }
00169         };
00170 
00171    template<>
00172    std::string PayLoadInspector<EcalTPGCrystalStatus>::summary() const {
00173         std::stringstream ss;
00174         EcalTPGCrystalStatusHelper helper;
00175         ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
00176         return ss.str();
00177    }
00178 
00179         template<>
00180         std::string PayLoadInspector<EcalTPGCrystalStatus>::plot(std::string const & filename,
00181                 std::string const &, 
00182                 std::vector<int> const&, 
00183                 std::vector<float> const& ) const {
00184                         gStyle->SetPalette(1);
00185                         const int TOTAL_IMAGES = 1;
00186                         const int TOTAL_PADS = 3;
00187 
00188                         //    TCanvas canvas("CC map","CC map",840,600);
00189                         const float IMG_SIZE = 1.5;
00190                         TCanvas canvas("CC map","CC map",800*IMG_SIZE, 200 * TOTAL_IMAGES*IMG_SIZE);//800, 1200
00191 
00192                         float xmi[3] = {0.0 , 0.22, 0.78};
00193                         float xma[3] = {0.22, 0.78, 1.00};
00194 
00195 
00196                         TPad*** pad = new TPad**[TOTAL_IMAGES];
00197                         for (int gId = 0; gId < TOTAL_IMAGES; gId++) {
00198                                 pad[gId] = new TPad*[TOTAL_PADS];
00199                                 for (int obj = 0; obj < TOTAL_PADS; obj++) {
00200                                         float yma = 1. - 0;//1.- (0.17 * gId);
00201                                         float ymi = yma - 1;//0.15;
00202                                         pad[gId][obj] = new TPad(Form("p_%i_%i", obj, gId),Form("p_%i_%i", obj, gId),
00203                                                 xmi[obj], ymi, xma[obj], yma);
00204                                         pad[gId][obj]->Draw();
00205                                 }
00206                         }
00207 
00208                         const int kGains       = 3;
00209                         const int gainValues[3] = {12, 6, 1};
00210                         const int kSides       = 2;
00211                         const int kBarlRings   = EBDetId::MAX_IETA;
00212                         const int kBarlWedges  = EBDetId::MAX_IPHI;
00213                         const int kEndcWedgesX = EEDetId::IX_MAX;
00214                         const int kEndcWedgesY = EEDetId::IY_MAX;
00215 
00216                         TH2F** barrel_m = new TH2F*[3];
00217                         TH2F** endc_p_m = new TH2F*[3];
00218                         TH2F** endc_m_m = new TH2F*[3];
00219                         //TH2F** barrel_r = new TH2F*[3];
00220                         //TH2F** endc_p_r = new TH2F*[3];
00221                         //TH2F** endc_m_r = new TH2F*[3];
00222                         std::string variableName = "TPGCrystalStatus";
00223                         for (int gainId = 0; gainId < kGains; gainId++) {
00224                                 barrel_m[gainId] = new TH2F(Form("EBm%i",gainId),Form((variableName + " %i EB").c_str(),gainValues[gainId]),360,0,360, 170, -85,85);
00225                                 endc_p_m[gainId] = new TH2F(Form("EE+m%i",gainId),Form((variableName + " %i EE+").c_str(),gainValues[gainId]),100,1,101,100,1,101);
00226                                 endc_m_m[gainId] = new TH2F(Form("EE-m%i",gainId),Form((variableName + " %i EE-").c_str(),gainValues[gainId]),100,1,101,100,1,101);
00227                                 //barrel_r[gainId] = new TH2F(Form("EBr%i",gainId),Form("rms %i EB",gainValues[gainId]),360,0,360, 170, -85,85);
00228                                 //endc_p_r[gainId] = new TH2F(Form("EE+r%i",gainId),Form("rms %i EE+",gainValues[gainId]),100,1,101,100,1,101);
00229                                 //endc_m_r[gainId] = new TH2F(Form("EE-r%i",gainId),Form("rms %i EE-",gainValues[gainId]),100,1,101,100,1,101);
00230                         }
00231 
00232                         for (int sign=0; sign < kSides; sign++) {
00233                                 int thesign = sign==1 ? 1:-1;
00234 
00235                                 for (int ieta=0; ieta<kBarlRings; ieta++) {
00236                                         for (int iphi=0; iphi<kBarlWedges; iphi++) {
00237                                                 EBDetId id((ieta+1)*thesign, iphi+1);
00238                                                 float y = -1 - ieta;
00239                                                 if(sign == 1) y = ieta;
00240                                                 barrel_m[0]->Fill(iphi, y, object()[id.rawId()].getStatusCode());
00241                                                 //barrel_r[0]->Fill(iphi, y, object()[id.rawId()].rms_x12);
00242                                                 //barrel_m[1]->Fill(iphi, y, object()[id.rawId()].mean_x6);
00243                                                 //barrel_r[1]->Fill(iphi, y, object()[id.rawId()].rms_x6);
00244                                                 //barrel_m[2]->Fill(iphi, y, object()[id.rawId()].mean_x1);
00245                                                 //barrel_r[2]->Fill(iphi, y, object()[id.rawId()].rms_x1);
00246                                         }  // iphi
00247                                 }   // ieta
00248 
00249                                 for (int ix=0; ix<kEndcWedgesX; ix++) {
00250                                         for (int iy=0; iy<kEndcWedgesY; iy++) {
00251                                                 if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
00252                                                 EEDetId id(ix+1,iy+1,thesign);
00253                                                 if (thesign==1) {
00254                                                         endc_p_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].getStatusCode());
00255                                                         //endc_p_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
00256                                                         //endc_p_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
00257                                                         //endc_p_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
00258                                                         //endc_p_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
00259                                                         //endc_p_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
00260                                                 }
00261                                                 else{ 
00262                                                         endc_m_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].getStatusCode());
00263                                                         //endc_m_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
00264                                                         //endc_m_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
00265                                                         //endc_m_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
00266                                                         //endc_m_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
00267                                                         //endc_m_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
00268                                                 }
00269                                         }  // iy
00270                                 }   // ix
00271                         }    // side
00272 
00273                         //canvas.cd(1);
00274                         //float bmin[3] ={0.7, 0.5, 0.4};
00275                         //float bmax[3] ={1.7, 1.0, 0.8};
00276                         //float emin[3] ={1.5, 0.8, 0.4};
00277                         //float emax[3] ={2.5, 1.5, 0.8};
00278                         TLine* l = new TLine(0., 0., 0., 0.);
00279                         l->SetLineWidth(1);
00280                         int ixSectorsEE[202] = {
00281                                 62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 
00282                                 41, 41, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 46, 46, 56, 56, 58, 58, 59, 59, 
00283                                 60, 60, 61, 61, 62, 62,  0,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 
00284                                 81, 76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,
00285                                 9,  6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 
00286                                 26, 36, 36, 41, 41, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 93, 93, 96, 
00287                                 96, 98, 98,101,101,  0, 62, 66, 66, 71, 71, 81, 81, 91, 91, 93,  0, 62, 66, 66, 
00288                                 91, 91, 98,  0, 58, 61, 61, 66, 66, 71, 71, 76, 76, 81, 81,  0, 51, 51,  0, 44, 
00289                                 41, 41, 36, 36, 31, 31, 26, 26, 21, 21,  0, 40, 36, 36, 11, 11,  4,  0, 40, 36, 
00290                                 36, 31, 31, 21, 21, 11, 11,  9,  0, 46, 46, 41, 41, 36, 36,  0, 56, 56, 61, 61, 66, 66};
00291 
00292                                 int iySectorsEE[202] = {
00293                                         51, 56, 56, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 61, 61, 60, 60, 59, 59, 58, 
00294                                         58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 41, 41, 42, 42, 43, 
00295                                         43, 44, 44, 46, 46, 51,  0, 51, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 
00296                                         93, 93, 96, 96, 98, 98,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 81, 
00297                                         76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,  9, 
00298                                         6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 26, 
00299                                         36, 36, 41, 41, 51,  0, 46, 46, 41, 41, 36, 36, 31, 31, 26, 26,  0, 51, 51, 56, 
00300                                         56, 61, 61,  0, 61, 61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 62,101,  0, 61, 
00301                                         61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 51, 51, 56, 56, 61, 61,  0, 46, 46, 
00302                                         41, 41, 36, 36, 31, 31, 26, 26,  0, 40, 31, 31, 16, 16,  6,  0, 40, 31, 31, 16, 16,  6};
00303 
00304                                         for (int gId = 0; gId < TOTAL_IMAGES; gId++) {//was 3
00305                                                 pad[gId][0]->cd();
00306                                                 endc_m_m[gId]->SetStats(0);
00307                                                 //endc_m_m[gId]->SetMaximum(225);
00308                                                 //endc_m_m[gId]->SetMinimum(175);
00309                                                 endc_m_m[gId]->Draw("colz");
00310                                                 for ( int i=0; i<201; i=i+1) {
00311                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00312                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00313                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00314                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00315                                                                         l->SetLineWidth(0.2);
00316                                                         }
00317                                                 }
00318 
00319                                                 //pad[gId + 3][0]->cd();
00320                                                 //endc_m_r[gId]->SetStats(0);
00321                                                 //endc_m_r[gId]->SetMaximum(emax[gId]);
00322                                                 //endc_m_r[gId]->SetMinimum(emin[gId]);
00323                                                 //endc_m_r[gId]->Draw("colz");
00324                                                 //for ( int i=0; i<201; i=i+1) {
00325                                                 //      if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00326                                                 //              (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00327                                                 //                      l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00328                                                 //                              ixSectorsEE[i+1], iySectorsEE[i+1]);
00329                                                 //      }
00330                                                 //}
00331 
00332                                                 //canvas.cd(2);
00333                                                 pad[gId][1]->cd();
00334                                                 barrel_m[gId]->SetStats(0);
00335                                                 //barrel_m[gId]->SetMaximum(225);
00336                                                 //barrel_m[gId]->SetMinimum(175);
00337                                                 barrel_m[gId]->Draw("colz");
00338                                                 for(int i = 0; i <17; i++) {
00339                                                         Double_t x = 20.+ (i *20);
00340                                                         l = new TLine(x,-85.,x,86.);
00341                                                         l->Draw();
00342                                                 }
00343                                                 l = new TLine(0.,0.,360.,0.);
00344                                                 l->Draw();
00345 
00346                                                 //pad[gId + 3][1]->cd();
00347                                                 //barrel_r[gId]->SetStats(0);
00348                                                 //barrel_r[gId]->SetMaximum(bmax[gId]);
00349                                                 //barrel_r[gId]->SetMinimum(bmin[gId]);
00350                                                 //barrel_r[gId]->Draw("colz");
00351                                                 //for(int i = 0; i <17; i++) {
00352                                                 //      Double_t x = 20.+ (i *20);
00353                                                 //      l = new TLine(x,-85.,x,86.);
00354                                                 //      l->Draw();
00355                                                 //}
00356                                                 //l = new TLine(0.,0.,360.,0.);
00357                                                 //l->Draw();
00358 
00359                                                 //canvas.cd(3);
00360                                                 pad[gId][2]->cd();
00361                                                 endc_p_m[gId]->SetStats(0);
00362                                                 //endc_p_m[gId]->SetMaximum(225);
00363                                                 //endc_p_m[gId]->SetMinimum(175);
00364                                                 endc_p_m[gId]->Draw("colz");
00365                                                 for ( int i=0; i<201; i=i+1) {
00366                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00367                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00368                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00369                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00370                                                         }
00371                                                 }
00372 
00373                                         //      pad[gId + 3][2]->cd();
00374                                         //      endc_p_r[gId]->SetStats(0);
00375                                         //      endc_p_r[gId]->SetMaximum(emax[gId]);
00376                                         //      endc_p_r[gId]->SetMinimum(emin[gId]);
00377                                         //      endc_p_r[gId]->Draw("colz");
00378                                         //      for ( int i=0; i<201; i=i+1) {
00379                                         //              if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00380                                         //                      (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00381                                         //                              l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00382                                         //                                      ixSectorsEE[i+1], iySectorsEE[i+1]);
00383                                         //              }
00384                                         //      }
00385                                         }
00386 
00387                                         canvas.SaveAs(filename.c_str());
00388                                         return filename;
00389         }  // plot
00390 }
00391 
00392 namespace condPython {
00393   template<>
00394   void defineWhat<EcalTPGCrystalStatus>() {
00395     enum_<cond::ecalcond::How>("How")
00396       .value("singleChannel",cond::ecalcond::singleChannel)
00397       .value("bySuperModule",cond::ecalcond::bySuperModule) 
00398       .value("all",cond::ecalcond::all)
00399       ;
00400 
00401     typedef cond::ExtractWhat<EcalTPGCrystalStatus> What;
00402     class_<What>("What",init<>())
00403       .def("set_how",&What::set_how)
00404       .def("set_which",&What::set_which)
00405       .def("how",&What::how, return_value_policy<copy_const_reference>())
00406       .def("which",&What::which, return_value_policy<copy_const_reference>())
00407       ;
00408   }
00409 }
00410 
00411 PYTHON_WRAPPER(EcalTPGCrystalStatus,EcalTPGCrystalStatus);