CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CondCore/EcalPlugins/plugins/EcalPedestalsPyWrapper.cc

Go to the documentation of this file.
00001 #include "CondFormats/EcalObjects/interface/EcalPedestals.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 
00027 namespace {
00028         struct Printer {
00029                 void doit(EcalPedestal const & item) {
00030                         for (int i=1; i<4; i++)
00031                                 ss << item.mean(i) << "," << item.rms(i) <<";";
00032                         ss << " ";
00033                 }
00034                 std::stringstream ss;
00035         };
00036 }
00037 
00038 namespace cond {
00039 
00040         namespace ecalped {
00041                 enum Quantity { mean_x12=1, mean_x6=2, mean_x3=3 };
00042                 enum How { singleChannel, bySuperModule, all};
00043 
00044                 float average(EcalPedestals const & peds, Quantity q) {
00045                         return std::accumulate(
00046                                 boost::make_transform_iterator(peds.barrelItems().begin(),bind(&EcalPedestal::mean,_1,q)),
00047                                 boost::make_transform_iterator(peds.barrelItems().end(),bind(&EcalPedestal::mean,_1,q)),
00048                                 0.)/float(peds.barrelItems().size());
00049                 }
00050 
00051                 void extractAverage(EcalPedestals const & peds, Quantity q, std::vector<int> const &,  std::vector<float> & result) {
00052                         result.resize(1);
00053                         result[0] = average(peds,q);
00054                 }
00055 
00056                 void extractSuperModules(EcalPedestals const & peds, Quantity q, std::vector<int> const & which,  std::vector<float> & result) {
00057                         // bho...
00058                 }
00059 
00060                 void extractSingleChannel(EcalPedestals const & peds, Quantity q, std::vector<int> const & which,  std::vector<float> & result) {
00061                         for (unsigned int i=0; i<which.size();i++) {
00062                                 // absolutely arbitraty
00063                                 if ((unsigned int) (which[i])<  peds.barrelItems().size())
00064                                         result.push_back( peds.barrelItems()[which[i]].mean(q));
00065                         }
00066                 }
00067 
00068                 typedef boost::function<void(EcalPedestals const & peds, Quantity q, std::vector<int> const & which,  std::vector<float> & result)> PedExtractor;
00069         }
00070 
00071         template<>
00072         struct ExtractWhat<EcalPedestals> {
00073 
00074                 ecalped::Quantity m_quantity;
00075                 ecalped::How m_how;
00076                 std::vector<int> m_which;
00077 
00078                 ecalped::Quantity const & quantity() const { return m_quantity;}
00079                 ecalped::How const & how() const { return m_how;}
00080                 std::vector<int> const & which() const { return m_which;}
00081 
00082 
00083                 void set_quantity( ecalped::Quantity i) { m_quantity=i;}
00084                 void set_how(ecalped::How i) {m_how=i;}
00085                 void set_which(std::vector<int> & i) { m_which.swap(i);}
00086         };
00087 
00088 
00089         template<>
00090         class ValueExtractor<EcalPedestals>: public  BaseValueExtractor<EcalPedestals> {
00091         public:
00092 
00093                 static ecalped::PedExtractor & extractor(ecalped::How how) {
00094                         static  ecalped::PedExtractor fun[3] = { 
00095                                 ecalped::PedExtractor(ecalped::extractSingleChannel),
00096                                 ecalped::PedExtractor(ecalped::extractSuperModules),
00097                                 ecalped::PedExtractor(ecalped::extractAverage)
00098                         };
00099                         return fun[how];
00100                 }
00101 
00102                 typedef EcalPedestals Class;
00103                 typedef ExtractWhat<Class> What;
00104                 static What what() { return What();}
00105 
00106                 ValueExtractor(){}
00107                 ValueExtractor(What const & what)
00108                         : m_what(what)
00109                 {
00110                         // here one can make stuff really complicated... (select mean rms, 12,6,1)
00111                         // ask to make average on selected channels...
00112                 }
00113 
00114                 void compute(Class const & it){
00115                         std::vector<float> res;
00116                         extractor(m_what.how())(it,m_what.quantity(),m_what.which(),res);
00117                         swap(res);
00118                 }
00119 
00120         private:
00121                 What  m_what;
00122 
00123         };
00124 
00125 
00126         template<>
00127         std::string
00128                 PayLoadInspector<EcalPedestals>::dump() const {
00129                         std::stringstream ss;
00130                         EcalCondHeader header;
00131                         ss<<EcalPedestalsXMLTranslator::dumpXML(header,object());
00132                         return ss.str();
00133         }  // dump
00134 
00135 
00136         class EcalPedestalsHelper: public EcalPyWrapperHelper<EcalPedestal>{
00137         public:
00138                 EcalPedestalsHelper():EcalPyWrapperHelper<EcalObject>(6){}
00139         protected:
00140                 typedef EcalPedestal EcalObject;
00141                 type_vValues getValues( const std::vector<EcalPedestal> & vItems)
00142                 {
00143                         //unsigned int totalValues = 6; 
00144                         type_vValues vValues(total_values);
00145 
00146                         vValues[0].first = "mean_x12";
00147                         vValues[1].first = "rms_x12";
00148                         vValues[2].first = "mean_x6";
00149                         vValues[3].first = "rms_x6";
00150                         vValues[4].first = "mean_x1";
00151                         vValues[5].first = "rms_x1";
00152                         
00153                         vValues[0].second = .0;
00154                         vValues[1].second = .0;
00155                         vValues[2].second = .0;
00156                         vValues[3].second = .0;
00157                         vValues[4].second = .0;
00158                         vValues[5].second = .0;
00159                         
00160                         //get info:
00161                         for(std::vector<EcalPedestal>::const_iterator iItems = vItems.begin(); iItems != vItems.end(); ++iItems){
00162                                 vValues[0].second += iItems->mean(1);
00163                                 vValues[1].second += iItems->rms(1);
00164                                 vValues[2].second += iItems->mean(2);
00165                                 vValues[3].second += iItems->rms(2);
00166                                 vValues[4].second += iItems->mean(3);
00167                                 vValues[5].second += iItems->rms(3);
00168                         }
00169                         return vValues;
00170                 }
00171         };
00172 
00173         template<>
00174         std::string PayLoadInspector<EcalPedestals>::summary() const {
00175                 std::stringstream ss;
00176                 EcalPedestalsHelper helper;
00177                 ss << helper.printBarrelsEndcaps(object().barrelItems(), object().endcapItems());
00178                 return ss.str();
00179         }  // summary
00180 
00181 
00182         // return the real name of the file including extension...
00183         template<>
00184         std::string PayLoadInspector<EcalPedestals>::plot(std::string const & filename,
00185                 std::string const &, 
00186                 std::vector<int> const&, 
00187                 std::vector<float> const& ) const {
00188                         gStyle->SetPalette(1);
00189                         //    TCanvas canvas("CC map","CC map",840,600);
00190                         TCanvas canvas("CC map","CC map",800,1200);
00191 
00192                         float xmi[3] = {0.0 , 0.22, 0.78};
00193                         float xma[3] = {0.22, 0.78, 1.00};
00194                         TPad*** pad = new TPad**[6];
00195                         for (int gId = 0; gId < 6; gId++) {
00196                                 pad[gId] = new TPad*[3];
00197                                 for (int obj = 0; obj < 3; obj++) {
00198                                         float yma = 1.- (0.17 * gId);
00199                                         float ymi = yma - 0.15;
00200                                         pad[gId][obj] = new TPad(Form("p_%i_%i", obj, gId),Form("p_%i_%i", obj, gId),
00201                                                 xmi[obj], ymi, xma[obj], yma);
00202                                         pad[gId][obj]->Draw();
00203                                 }
00204                         }
00205 
00206                         const int kGains       = 3;
00207                         const int gainValues[3] = {12, 6, 1};
00208                         const int kSides       = 2;
00209                         const int kBarlRings   = EBDetId::MAX_IETA;
00210                         const int kBarlWedges  = EBDetId::MAX_IPHI;
00211                         const int kEndcWedgesX = EEDetId::IX_MAX;
00212                         const int kEndcWedgesY = EEDetId::IY_MAX;
00213 
00214                         TH2F** barrel_m = new TH2F*[3];
00215                         TH2F** endc_p_m = new TH2F*[3];
00216                         TH2F** endc_m_m = new TH2F*[3];
00217                         TH2F** barrel_r = new TH2F*[3];
00218                         TH2F** endc_p_r = new TH2F*[3];
00219                         TH2F** endc_m_r = new TH2F*[3];
00220                         for (int gainId = 0; gainId < kGains; gainId++) {
00221                                 barrel_m[gainId] = new TH2F(Form("EBm%i",gainId),Form("mean %i EB",gainValues[gainId]),360,0,360, 170, -85,85);
00222                                 endc_p_m[gainId] = new TH2F(Form("EE+m%i",gainId),Form("mean %i EE+",gainValues[gainId]),100,1,101,100,1,101);
00223                                 endc_m_m[gainId] = new TH2F(Form("EE-m%i",gainId),Form("mean %i EE-",gainValues[gainId]),100,1,101,100,1,101);
00224                                 barrel_r[gainId] = new TH2F(Form("EBr%i",gainId),Form("rms %i EB",gainValues[gainId]),360,0,360, 170, -85,85);
00225                                 endc_p_r[gainId] = new TH2F(Form("EE+r%i",gainId),Form("rms %i EE+",gainValues[gainId]),100,1,101,100,1,101);
00226                                 endc_m_r[gainId] = new TH2F(Form("EE-r%i",gainId),Form("rms %i EE-",gainValues[gainId]),100,1,101,100,1,101);
00227                         }
00228 
00229                         for (int sign=0; sign < kSides; sign++) {
00230                                 int thesign = sign==1 ? 1:-1;
00231 
00232                                 for (int ieta=0; ieta<kBarlRings; ieta++) {
00233                                         for (int iphi=0; iphi<kBarlWedges; iphi++) {
00234                                                 EBDetId id((ieta+1)*thesign, iphi+1);
00235                                                 float y = -1 - ieta;
00236                                                 if(sign == 1) y = ieta;
00237                                                 barrel_m[0]->Fill(iphi, y, object()[id.rawId()].mean_x12);
00238                                                 barrel_r[0]->Fill(iphi, y, object()[id.rawId()].rms_x12);
00239                                                 barrel_m[1]->Fill(iphi, y, object()[id.rawId()].mean_x6);
00240                                                 barrel_r[1]->Fill(iphi, y, object()[id.rawId()].rms_x6);
00241                                                 barrel_m[2]->Fill(iphi, y, object()[id.rawId()].mean_x1);
00242                                                 barrel_r[2]->Fill(iphi, y, object()[id.rawId()].rms_x1);
00243                                         }  // iphi
00244                                 }   // ieta
00245 
00246                                 for (int ix=0; ix<kEndcWedgesX; ix++) {
00247                                         for (int iy=0; iy<kEndcWedgesY; iy++) {
00248                                                 if (! EEDetId::validDetId(ix+1,iy+1,thesign)) continue;
00249                                                 EEDetId id(ix+1,iy+1,thesign);
00250                                                 if (thesign==1) {
00251                                                         endc_p_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x12);
00252                                                         endc_p_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
00253                                                         endc_p_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
00254                                                         endc_p_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
00255                                                         endc_p_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
00256                                                         endc_p_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
00257                                                 }
00258                                                 else{ 
00259                                                         endc_m_m[0]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x12);
00260                                                         endc_m_r[0]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x12);
00261                                                         endc_m_m[1]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x6);
00262                                                         endc_m_r[1]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x6);
00263                                                         endc_m_m[2]->Fill(ix+1,iy+1,object()[id.rawId()].mean_x1);
00264                                                         endc_m_r[2]->Fill(ix+1,iy+1,object()[id.rawId()].rms_x1);
00265                                                 }
00266                                         }  // iy
00267                                 }   // ix
00268                         }    // side
00269 
00270                         //canvas.cd(1);
00271                         float bmin[3] ={0.7, 0.5, 0.4};
00272                         float bmax[3] ={1.7, 1.0, 0.8};
00273                         float emin[3] ={1.5, 0.8, 0.4};
00274                         float emax[3] ={2.5, 1.5, 0.8};
00275                         TLine* l = new TLine(0., 0., 0., 0.);
00276                         l->SetLineWidth(1);
00277                         int ixSectorsEE[202] = {
00278                                 62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 
00279                                 41, 41, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 46, 46, 56, 56, 58, 58, 59, 59, 
00280                                 60, 60, 61, 61, 62, 62,  0,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 
00281                                 81, 76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,
00282                                 9,  6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 
00283                                 26, 36, 36, 41, 41, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 93, 93, 96, 
00284                                 96, 98, 98,101,101,  0, 62, 66, 66, 71, 71, 81, 81, 91, 91, 93,  0, 62, 66, 66, 
00285                                 91, 91, 98,  0, 58, 61, 61, 66, 66, 71, 71, 76, 76, 81, 81,  0, 51, 51,  0, 44, 
00286                                 41, 41, 36, 36, 31, 31, 26, 26, 21, 21,  0, 40, 36, 36, 11, 11,  4,  0, 40, 36, 
00287                                 36, 31, 31, 21, 21, 11, 11,  9,  0, 46, 46, 41, 41, 36, 36,  0, 56, 56, 61, 61, 66, 66};
00288 
00289                                 int iySectorsEE[202] = {
00290                                         51, 56, 56, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 61, 61, 60, 60, 59, 59, 58, 
00291                                         58, 56, 56, 46, 46, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 41, 41, 42, 42, 43, 
00292                                         43, 44, 44, 46, 46, 51,  0, 51, 61, 61, 66, 66, 76, 76, 81, 81, 86, 86, 88, 88, 
00293                                         93, 93, 96, 96, 98, 98,101,101, 98, 98, 96, 96, 93, 93, 88, 88, 86, 86, 81, 81, 
00294                                         76, 76, 66, 66, 61, 61, 41, 41, 36, 36, 26, 26, 21, 21, 16, 16, 14, 14,  9,  9, 
00295                                         6,  6,  4,  4,  1,  1,  4,  4,  6,  6,  9,  9, 14, 14, 16, 16, 21, 21, 26, 26, 
00296                                         36, 36, 41, 41, 51,  0, 46, 46, 41, 41, 36, 36, 31, 31, 26, 26,  0, 51, 51, 56, 
00297                                         56, 61, 61,  0, 61, 61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 62,101,  0, 61, 
00298                                         61, 66, 66, 71, 71, 76, 76, 86, 86, 88,  0, 51, 51, 56, 56, 61, 61,  0, 46, 46, 
00299                                         41, 41, 36, 36, 31, 31, 26, 26,  0, 40, 31, 31, 16, 16,  6,  0, 40, 31, 31, 16, 16,  6};
00300 
00301                                         for (int gId = 0; gId < 3; gId++) {
00302                                                 pad[gId][0]->cd();
00303                                                 endc_m_m[gId]->SetStats(0);
00304                                                 endc_m_m[gId]->SetMaximum(225);
00305                                                 endc_m_m[gId]->SetMinimum(175);
00306                                                 endc_m_m[gId]->Draw("colz");
00307                                                 for ( int i=0; i<201; i=i+1) {
00308                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00309                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00310                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00311                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00312                                                                         l->SetLineWidth(0.2);
00313                                                         }
00314                                                 }
00315                                                 pad[gId + 3][0]->cd();
00316                                                 endc_m_r[gId]->SetStats(0);
00317                                                 endc_m_r[gId]->SetMaximum(emax[gId]);
00318                                                 endc_m_r[gId]->SetMinimum(emin[gId]);
00319                                                 endc_m_r[gId]->Draw("colz");
00320                                                 for ( int i=0; i<201; i=i+1) {
00321                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00322                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00323                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00324                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00325                                                         }
00326                                                 }
00327                                                 //canvas.cd(2);
00328                                                 pad[gId][1]->cd();
00329                                                 barrel_m[gId]->SetStats(0);
00330                                                 barrel_m[gId]->SetMaximum(225);
00331                                                 barrel_m[gId]->SetMinimum(175);
00332                                                 barrel_m[gId]->Draw("colz");
00333                                                 for(int i = 0; i <17; i++) {
00334                                                         Double_t x = 20.+ (i *20);
00335                                                         l = new TLine(x,-85.,x,86.);
00336                                                         l->Draw();
00337                                                 }
00338                                                 l = new TLine(0.,0.,360.,0.);
00339                                                 l->Draw();
00340                                                 pad[gId + 3][1]->cd();
00341                                                 barrel_r[gId]->SetStats(0);
00342                                                 barrel_r[gId]->SetMaximum(bmax[gId]);
00343                                                 barrel_r[gId]->SetMinimum(bmin[gId]);
00344                                                 barrel_r[gId]->Draw("colz");
00345                                                 for(int i = 0; i <17; i++) {
00346                                                         Double_t x = 20.+ (i *20);
00347                                                         l = new TLine(x,-85.,x,86.);
00348                                                         l->Draw();
00349                                                 }
00350                                                 l = new TLine(0.,0.,360.,0.);
00351                                                 l->Draw();
00352                                                 //canvas.cd(3);
00353                                                 pad[gId][2]->cd();
00354                                                 endc_p_m[gId]->SetStats(0);
00355                                                 endc_p_m[gId]->SetMaximum(225);
00356                                                 endc_p_m[gId]->SetMinimum(175);
00357                                                 endc_p_m[gId]->Draw("colz");
00358                                                 for ( int i=0; i<201; i=i+1) {
00359                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00360                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00361                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00362                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00363                                                         }
00364                                                 }
00365                                                 pad[gId + 3][2]->cd();
00366                                                 endc_p_r[gId]->SetStats(0);
00367                                                 endc_p_r[gId]->SetMaximum(emax[gId]);
00368                                                 endc_p_r[gId]->SetMinimum(emin[gId]);
00369                                                 endc_p_r[gId]->Draw("colz");
00370                                                 for ( int i=0; i<201; i=i+1) {
00371                                                         if ( (ixSectorsEE[i]!=0 || iySectorsEE[i]!=0) && 
00372                                                                 (ixSectorsEE[i+1]!=0 || iySectorsEE[i+1]!=0) ) {
00373                                                                         l->DrawLine(ixSectorsEE[i], iySectorsEE[i], 
00374                                                                                 ixSectorsEE[i+1], iySectorsEE[i+1]);
00375                                                         }
00376                                                 }
00377                                         }
00378 
00379                                         canvas.SaveAs(filename.c_str());
00380                                         return filename;
00381         }  // plot
00382 }
00383 
00384 namespace condPython {
00385         template<>
00386         void defineWhat<EcalPedestals>() {
00387                 using namespace boost::python;
00388                 enum_<cond::ecalped::Quantity>("Quantity")
00389                         .value("mean_x12",cond::ecalped::mean_x12)
00390                         .value("mean_x6",  cond::ecalped::mean_x6)
00391                         .value("mean_x3", cond::ecalped::mean_x3)
00392                         ;
00393                 enum_<cond::ecalped::How>("How")
00394                         .value("singleChannel",cond::ecalped::singleChannel)
00395                         .value("bySuperModule",cond::ecalped::bySuperModule) 
00396                         .value("all",cond::ecalped::all)
00397                         ;
00398 
00399                 typedef cond::ExtractWhat<EcalPedestals> What;
00400                 class_<What>("What",init<>())
00401                         .def("set_quantity",&What::set_quantity)
00402                         .def("set_how",&What::set_how)
00403                         .def("set_which",&What::set_which)
00404                         .def("quantity",&What::quantity, return_value_policy<copy_const_reference>())
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 
00412 
00413 PYTHON_WRAPPER(EcalPedestals,EcalPedestals);