CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/DQM/EcalBarrelMonitorDbModule/src/MonitorElementsDb.cc

Go to the documentation of this file.
00001 // $Id: MonitorElementsDb.cc,v 1.24 2012/02/28 16:38:09 yiiyama Exp $
00002 
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 
00013 #include <iostream>
00014 #include <fstream>
00015 #include <cmath>
00016 
00017 #include "DQMServices/Core/interface/MonitorElement.h"
00018 
00019 #include "DQMServices/Core/interface/DQMStore.h"
00020 
00021 #include "RelationalAccess/ITransaction.h"
00022 #include "RelationalAccess/ISchema.h"
00023 #include "RelationalAccess/IQuery.h"
00024 #include "RelationalAccess/ICursor.h"
00025 
00026 #include "CoralBase/Attribute.h"
00027 #include "CoralBase/AttributeList.h"
00028 
00029 #include "TCanvas.h"
00030 #include "TStyle.h"
00031 
00032 #include "TH1.h"
00033 #include "TH2.h"
00034 #include "TProfile.h"
00035 
00036 #include "DQM/EcalBarrelMonitorDbModule/interface/MonitorElementsDb.h"
00037 
00038 MonitorElementsDb::MonitorElementsDb( const edm::ParameterSet& ps, std::string& xmlFile ){
00039 
00040   xmlFile_ = xmlFile;
00041 
00042   dqmStore_ = edm::Service<DQMStore>().operator->();
00043 
00044   prefixME_ = ps.getUntrackedParameter<std::string>("prefixME", "");
00045 
00046   if ( dqmStore_ ) {
00047 
00048     dqmStore_->setCurrentFolder( prefixME_ );
00049 
00050     parser_ = new MonitorXMLParser( xmlFile_ );
00051     try {
00052       parser_->load();
00053     } catch( const std::runtime_error e ) {
00054       delete parser_;
00055       parser_ = 0;
00056       std::cerr << "Error loading parser: " << e.what() << std::endl;
00057     }
00058 
00059     if(parser_) MEinfo_ = parser_->getDB_ME();
00060 
00061     for( unsigned int i=0; i< MEinfo_.size(); i++ ) {
00062 
00063       MonitorElement* tmp;
00064       tmp = 0;
00065       if( strcmp(MEinfo_[i].type.c_str(), "th1d") == 0 ) {
00066         tmp = dqmStore_->book1D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto );
00067       }
00068       else if( strcmp(MEinfo_[i].type.c_str(), "th2d") == 0 ) {
00069         tmp = dqmStore_->book2D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00070                             MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto );
00071       }
00072       else if( strcmp(MEinfo_[i].type.c_str(), "tprofile") == 0 ) {
00073       tmp = dqmStore_->bookProfile( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00074                                MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto );
00075       }
00076       else if( strcmp(MEinfo_[i].type.c_str(), "tprofile2d") == 0 ) {
00077         tmp = dqmStore_->bookProfile2D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00078                                    MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto,
00079                                    MEinfo_[i].zbins, MEinfo_[i].zfrom, MEinfo_[i].zto );
00080       }
00081 
00082       MEs_.push_back( tmp );
00083     }
00084 
00085   }
00086 
00087   ievt_ = 0;
00088 
00089 }
00090 
00091 MonitorElementsDb::~MonitorElementsDb(){
00092 
00093   delete parser_;
00094 
00095 }
00096 
00097 void MonitorElementsDb::beginJob( void ){
00098 
00099   ievt_ = 0;
00100 
00101 }
00102 
00103 void MonitorElementsDb::endJob( void ){
00104 
00105   std::cout << "MonitorElementsDb: analyzed " << ievt_ << " events" << std::endl;
00106   for( unsigned int i = 0; i<MEs_.size(); i++ ) {
00107     if( MEs_[i] != 0 ) dqmStore_->removeElement( MEs_[i]->getName() );
00108   }
00109 
00110 }
00111 
00112 void MonitorElementsDb::analyze( const edm::Event& e, const edm::EventSetup& c, coral::ISessionProxy* session ){
00113 
00114   ievt_++;
00115 
00116   bool atLeastAQuery;
00117   atLeastAQuery = false;
00118 
00119   std::vector<std::string> vars;
00120 
00121   if ( session )  {
00122 
00123     for( unsigned int i=0; i<MEinfo_.size(); i++ ) {
00124 
00125       // i-th ME...
00126 
00127       if( MEs_[i] != 0 && ( ievt_ % MEinfo_[i].ncycle ) == 0 ) {
00128 
00129         MEs_[i]->Reset();
00130 
00131         vars.clear();
00132 
00133         try {
00134 
00135           atLeastAQuery = true;
00136 
00137           session->transaction().start(true);
00138 
00139           coral::ISchema& schema = session->nominalSchema();
00140 
00141           coral::IQuery* query = schema.newQuery();
00142 
00143           for( unsigned int j=0; j<MEinfo_[i].queries.size(); j++ ) {
00144             if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToTableList") == 0 ) {
00145               query->addToTableList( MEinfo_[i].queries[j].arg );
00146             }
00147             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOutputList") == 0 ) {
00148               query->addToOutputList( MEinfo_[i].queries[j].arg, MEinfo_[i].queries[j].alias );
00149               vars.push_back( MEinfo_[i].queries[j].alias );
00150             }
00151             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "setCondition") == 0 ) {
00152               query->setCondition( MEinfo_[i].queries[j].arg, coral::AttributeList() );
00153             }
00154             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOrderList") == 0 ) {
00155               query->addToOrderList( MEinfo_[i].queries[j].arg );
00156             }
00157           }
00158 
00159           coral::ICursor& cursor = query->execute();
00160 
00161           unsigned int k = 0;
00162 
00163           while ( cursor.next() && k < MEinfo_[i].loop ) {
00164             //while ( cursor.next() ) {
00165 
00166             const coral::AttributeList& row = cursor.currentRow();
00167 
00168             std::vector<float> vvars;
00169             vvars.clear();
00170             for( unsigned int l=0; l<vars.size(); l++ ) {
00171               if( !vars[l].empty() ) {
00172                 vvars.push_back( row[vars[l].c_str()].data<float>() );
00173               }
00174             }
00175             if( vvars.size() == 2 ) {
00176               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << std::endl;
00177               MEs_[i]->Fill( vvars[0], vvars[1] );
00178             }
00179             else if( vvars.size() == 3 ) {
00180               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << " -- " << vvars[2] << std::endl;
00181               MEs_[i]->Fill( vvars[0], vvars[1], vvars[2] );
00182             }
00183             else if( vvars.size() == 4 ) {
00184               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << " -- " << vvars[2] << " -- " << vvars[3] << std::endl;
00185               MEs_[i]->Fill( vvars[0], vvars[1], vvars[2], vvars[3] );
00186             }
00187             else{
00188               std::cerr << "Too many variables to plot..." << std::endl;
00189               exit(1);
00190             }
00191 
00192             k++;
00193 
00194           }
00195 
00196           delete query;
00197 
00198         } catch ( coral::Exception& e ) {
00199           std::cerr << "CORAL Exception : " << e.what() << std::endl;
00200         } catch ( std::exception& e ) {
00201           std::cerr << "Standard C++ exception : " << e.what() << std::endl;
00202         } catch ( ... ) {
00203           std::cerr << "Exception caught (...)" << std::endl;
00204         }
00205 
00206       }
00207 
00208     }
00209 
00210     if( atLeastAQuery ) session->transaction().commit();
00211 
00212   }
00213 
00214 }
00215 
00216 void MonitorElementsDb::htmlOutput(std::string& htmlDir){
00217 
00218   gStyle->SetOptStat(0);
00219   gStyle->SetOptFit();
00220   gStyle->SetPalette(1,0);
00221 
00222   for( unsigned int i=0; i<MEinfo_.size(); i++ ) {
00223 
00224     if( MEs_[i] != 0 && ( ievt_ % MEinfo_[i].ncycle ) == 0 ) {
00225 
00226       TCanvas* c1;
00227       int n = MEinfo_[i].xbins > MEinfo_[i].ybins ? int( round( float( MEinfo_[i].xbins ) / float( MEinfo_[i].ybins ) ) ) :
00228         int( round( float( MEinfo_[i].ybins ) / float( MEinfo_[i].xbins ) ) );
00229       if( MEinfo_[i].xbins > MEinfo_[i].ybins ) {
00230         c1 = new TCanvas( "c1", "dummy", 400*n, 400 );
00231       }
00232       else {
00233         c1 = new TCanvas( "c1", "dummy", 400, 400*n );
00234       }
00235       c1->SetGrid();
00236       c1->cd();
00237 
00238       const double histMax = 1.e15;
00239 
00240       TObject* ob = const_cast<MonitorElement*>(MEs_[i])->getRootObject();
00241       if ( ob ) {
00242         if( dynamic_cast<TH1F*>( ob ) ) {
00243           TH1F* h = dynamic_cast<TH1F*> ( ob );
00244           h->Draw( );
00245         }
00246         else if( dynamic_cast<TH2F*>( ob ) ) {
00247           TH2F* h = dynamic_cast<TH2F*>( ob );
00248           if( h->GetMaximum(histMax) > 1.e4 ) {
00249             gPad->SetLogz(1);
00250           } else {
00251             gPad->SetLogz(0);
00252           }
00253           h->Draw( "colz" );
00254         }
00255         else if( dynamic_cast<TProfile*>( ob ) ) {
00256           TProfile* h = dynamic_cast<TProfile*>( ob );
00257           if( h->GetMaximum(histMax) > 1.e4 ) {
00258             gPad->SetLogz(1);
00259           } else {
00260             gPad->SetLogz(0);
00261           }
00262           h->Draw( "colz" );
00263         }
00264       }
00265 
00266       c1->Update();
00267       std::string name = htmlDir + "/" + MEinfo_[i].title + ".png";
00268       c1->SaveAs( name.c_str() );
00269 
00270       delete c1;
00271 
00272     }
00273   }
00274 }
00275