CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/DQM/EcalBarrelMonitorDbModule/src/MonitorElementsDb.cc

Go to the documentation of this file.
00001 // $Id: MonitorElementsDb.cc,v 1.22 2010/08/08 08:46:03 dellaric 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       std::cerr << "Error loading parser: " << e.what() << std::endl;
00055     }
00056 
00057     MEinfo_ = parser_->getDB_ME();
00058 
00059     for( unsigned int i=0; i< MEinfo_.size(); i++ ) {
00060 
00061       MonitorElement* tmp;
00062       tmp = 0;
00063       if( strcmp(MEinfo_[i].type.c_str(), "th1d") == 0 ) {
00064         tmp = dqmStore_->book1D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto );
00065       }
00066       else if( strcmp(MEinfo_[i].type.c_str(), "th2d") == 0 ) {
00067         tmp = dqmStore_->book2D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00068                             MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto );
00069       }
00070       else if( strcmp(MEinfo_[i].type.c_str(), "tprofile") == 0 ) {
00071       tmp = dqmStore_->bookProfile( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00072                                MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto );
00073       }
00074       else if( strcmp(MEinfo_[i].type.c_str(), "tprofile2d") == 0 ) {
00075         tmp = dqmStore_->bookProfile2D( MEinfo_[i].title, MEinfo_[i].title, MEinfo_[i].xbins, MEinfo_[i].xfrom, MEinfo_[i].xto,
00076                                    MEinfo_[i].ybins, MEinfo_[i].yfrom, MEinfo_[i].yto,
00077                                    MEinfo_[i].zbins, MEinfo_[i].zfrom, MEinfo_[i].zto );
00078       }
00079 
00080       MEs_.push_back( tmp );
00081     }
00082 
00083   }
00084 
00085 }
00086 
00087 MonitorElementsDb::~MonitorElementsDb(){
00088 
00089 }
00090 
00091 void MonitorElementsDb::beginJob( void ){
00092 
00093   ievt_ = 0;
00094 
00095 }
00096 
00097 void MonitorElementsDb::endJob( void ){
00098 
00099   std::cout << "MonitorElementsDb: analyzed " << ievt_ << " events" << std::endl;
00100   for( unsigned int i = 0; i<MEs_.size(); i++ ) {
00101     if( MEs_[i] != 0 ) dqmStore_->removeElement( MEs_[i]->getName() );
00102   }
00103 
00104 }
00105 
00106 void MonitorElementsDb::analyze( const edm::Event& e, const edm::EventSetup& c, coral::ISessionProxy* session ){
00107 
00108   ievt_++;
00109 
00110   bool atLeastAQuery;
00111   atLeastAQuery = false;
00112 
00113   std::vector<std::string> vars;
00114 
00115   if ( session )  {
00116 
00117     for( unsigned int i=0; i<MEinfo_.size(); i++ ) {
00118 
00119       // i-th ME...
00120 
00121       if( MEs_[i] != 0 && ( ievt_ % MEinfo_[i].ncycle ) == 0 ) {
00122 
00123         MEs_[i]->Reset();
00124 
00125         vars.clear();
00126 
00127         try {
00128 
00129           atLeastAQuery = true;
00130 
00131           session->transaction().start(true);
00132 
00133           coral::ISchema& schema = session->nominalSchema();
00134 
00135           coral::IQuery* query = schema.newQuery();
00136 
00137           for( unsigned int j=0; j<MEinfo_[i].queries.size(); j++ ) {
00138             if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToTableList") == 0 ) {
00139               query->addToTableList( MEinfo_[i].queries[j].arg );
00140             }
00141             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOutputList") == 0 ) {
00142               query->addToOutputList( MEinfo_[i].queries[j].arg, MEinfo_[i].queries[j].alias );
00143               vars.push_back( MEinfo_[i].queries[j].alias );
00144             }
00145             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "setCondition") == 0 ) {
00146               query->setCondition( MEinfo_[i].queries[j].arg, coral::AttributeList() );
00147             }
00148             else if( strcmp(MEinfo_[i].queries[j].query.c_str(), "addToOrderList") == 0 ) {
00149               query->addToOrderList( MEinfo_[i].queries[j].arg );
00150             }
00151           }
00152 
00153           coral::ICursor& cursor = query->execute();
00154 
00155           unsigned int k = 0;
00156 
00157           while ( cursor.next() && k < MEinfo_[i].loop ) {
00158             //while ( cursor.next() ) {
00159 
00160             const coral::AttributeList& row = cursor.currentRow();
00161 
00162             std::vector<float> vvars;
00163             vvars.clear();
00164             for( unsigned int l=0; l<vars.size(); l++ ) {
00165               if( !vars[l].empty() ) {
00166                 vvars.push_back( row[vars[l].c_str()].data<float>() );
00167               }
00168             }
00169             if( vvars.size() == 2 ) {
00170               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << std::endl;
00171               MEs_[i]->Fill( vvars[0], vvars[1] );
00172             }
00173             else if( vvars.size() == 3 ) {
00174               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << " -- " << vvars[2] << std::endl;
00175               MEs_[i]->Fill( vvars[0], vvars[1], vvars[2] );
00176             }
00177             else if( vvars.size() == 4 ) {
00178               //std::cout << k << " -- " << vvars[0] << " -- " << vvars[1] << " -- " << vvars[2] << " -- " << vvars[3] << std::endl;
00179               MEs_[i]->Fill( vvars[0], vvars[1], vvars[2], vvars[3] );
00180             }
00181             else{
00182               std::cerr << "Too many variables to plot..." << std::endl;
00183               exit(1);
00184             }
00185 
00186             k++;
00187 
00188           }
00189 
00190           delete query;
00191 
00192         } catch ( coral::Exception& e ) {
00193           std::cerr << "CORAL Exception : " << e.what() << std::endl;
00194         } catch ( std::exception& e ) {
00195           std::cerr << "Standard C++ exception : " << e.what() << std::endl;
00196         } catch ( ... ) {
00197           std::cerr << "Exception caught (...)" << std::endl;
00198         }
00199 
00200       }
00201 
00202     }
00203 
00204     if( atLeastAQuery ) session->transaction().commit();
00205 
00206   }
00207 
00208 }
00209 
00210 void MonitorElementsDb::htmlOutput(std::string& htmlDir){
00211 
00212   gStyle->SetOptStat(0);
00213   gStyle->SetOptFit();
00214   gStyle->SetPalette(1,0);
00215 
00216   for( unsigned int i=0; i<MEinfo_.size(); i++ ) {
00217 
00218     if( MEs_[i] != 0 && ( ievt_ % MEinfo_[i].ncycle ) == 0 ) {
00219 
00220       TCanvas* c1;
00221       int n = MEinfo_[i].xbins > MEinfo_[i].ybins ? int( round( float( MEinfo_[i].xbins ) / float( MEinfo_[i].ybins ) ) ) :
00222         int( round( float( MEinfo_[i].ybins ) / float( MEinfo_[i].xbins ) ) );
00223       if( MEinfo_[i].xbins > MEinfo_[i].ybins ) {
00224         c1 = new TCanvas( "c1", "dummy", 400*n, 400 );
00225       }
00226       else {
00227         c1 = new TCanvas( "c1", "dummy", 400, 400*n );
00228       }
00229       c1->SetGrid();
00230       c1->cd();
00231 
00232       const double histMax = 1.e15;
00233 
00234       TObject* ob = const_cast<MonitorElement*>(MEs_[i])->getRootObject();
00235       if ( ob ) {
00236         if( dynamic_cast<TH1F*>( ob ) ) {
00237           TH1F* h = dynamic_cast<TH1F*> ( ob );
00238           h->Draw( );
00239         }
00240         else if( dynamic_cast<TH2F*>( ob ) ) {
00241           TH2F* h = dynamic_cast<TH2F*>( ob );
00242           if( h->GetMaximum(histMax) > 1.e4 ) {
00243             gPad->SetLogz(1);
00244           } else {
00245             gPad->SetLogz(0);
00246           }
00247           h->Draw( "colz" );
00248         }
00249         else if( dynamic_cast<TProfile*>( ob ) ) {
00250           TProfile* h = dynamic_cast<TProfile*>( ob );
00251           if( h->GetMaximum(histMax) > 1.e4 ) {
00252             gPad->SetLogz(1);
00253           } else {
00254             gPad->SetLogz(0);
00255           }
00256           h->Draw( "colz" );
00257         }
00258       }
00259 
00260       c1->Update();
00261       std::string name = htmlDir + "/" + MEinfo_[i].title + ".png";
00262       c1->SaveAs( name.c_str() );
00263 
00264       delete c1;
00265 
00266     }
00267   }
00268 }
00269