CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQMServices/Examples/src/DQMClientExample.cc

Go to the documentation of this file.
00001 /*
00002  * \file DQMClientExample.cc
00003  * \author M. Zanetti - CERN
00004  *
00005  * Last Update:
00006  * $Date: 2009/12/14 22:22:23 $
00007  * $Revision: 1.16 $
00008  * $Author: wmtan $
00009  *
00010  */
00011 
00012 
00013 /*  Description: Simple example showing how to access the histograms 
00014  *  already defined and filled by the DQM producer(source) 
00015  *  and how to access the quality test results.
00016  */
00017 
00018 /* Jan 17, 2009: the code has been modified significantly
00019  * to steer the client operations  
00020  * Author: D.Volyanskyy
00021  */
00022 
00023 
00024 #include "DQMServices/Examples/interface/DQMClientExample.h"
00025 #include "FWCore/ServiceRegistry/interface/Service.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 
00029 #include <TF1.h>
00030 #include <stdio.h>
00031 #include <sstream>
00032 #include <math.h>
00033 
00034 using namespace edm;
00035 using namespace std;
00036 
00037 //==================================================================//
00038 //================= Constructor and Destructor =====================//
00039 //==================================================================//
00040 DQMClientExample::DQMClientExample(const edm::ParameterSet& ps)
00041 {
00042   parameters_=ps;
00043   initialize();
00044 }
00045 
00046 DQMClientExample::~DQMClientExample(){
00047 }
00048 
00049 //==================================================================//
00050 //======================= Initialise ===============================//
00051 //==================================================================//
00052 void DQMClientExample::initialize(){ 
00053 
00055   counterEvt_=0;   counterLS_  = 0; 
00056   counterClientOperation = 0;
00057   
00059   dbe_ = Service<DQMStore>().operator->();
00060   
00062   monitorName_ = parameters_.getUntrackedParameter<string>("monitorName","YourSubsystemName");
00063   cout << "DQMClientExample: Monitor name = " << monitorName_ << endl;
00064   if (monitorName_ != "" ) monitorName_ = monitorName_+"/" ;
00065 
00067   prescaleLS_  = parameters_.getUntrackedParameter<int>("prescaleLS",  -1);
00068   cout << "DQMClientExample: DQM lumi section prescale = " << prescaleLS_ << " lumi section(s)"<< endl;
00069   prescaleEvt_ = parameters_.getUntrackedParameter<int>("prescaleEvt", -1);
00070   cout << "DQMClientExample: DQM event prescale = " << prescaleEvt_ << " events(s)"<< endl;
00071 
00072   //-- QTestName that is going to bu run on clienHisto, as defined in XML file
00073   QTestName_ = parameters_.getUntrackedParameter<string>("QTestName","exampleQTest");  
00074   cout << "DQMClientExample: QTest name to be ran on clientHisto = " << QTestName_ << endl;
00075   //-- define where to run the Client
00076   clientOnEachEvent  = parameters_.getUntrackedParameter<bool>("clientOnEachEvent",false);
00077   if(clientOnEachEvent) cout << "DQMClientExample: run Client on each event" << endl;
00078   clientOnEndLumi  = parameters_.getUntrackedParameter<bool>("clientOnEndLumi",true);
00079   if(clientOnEndLumi) cout << "DQMClientExample: run Client at the end of each lumi section" << endl;
00080   clientOnEndRun   = parameters_.getUntrackedParameter<bool>("clientOnEndRun",false);
00081   if(clientOnEndRun) cout << "DQMClientExample: run Client at the end of each run" << endl;
00082   clientOnEndJob   = parameters_.getUntrackedParameter<bool>("clientOnEndJob",false);
00083   if(clientOnEndJob) cout << "DQMClientExample: run Client at the end of the job" << endl;
00084 
00085 }
00086 //==================================================================//
00087 //========================= beginJob ===============================//
00088 //==================================================================//
00089 void DQMClientExample::beginJob(){
00090 
00092   dbe_ = Service<DQMStore>().operator->();
00093 
00095   dbe_->setCurrentFolder(monitorName_+"DQMclient");
00096   clientHisto = dbe_->book1D("clientHisto", "Guassian fit results.", 2, 0, 1);
00097 }
00098 
00099 //==================================================================//
00100 //========================= beginRun ===============================//
00101 //==================================================================//
00102 void DQMClientExample::beginRun(const Run& r, const EventSetup& context) {
00103 }
00104 
00105 //==================================================================//
00106 //==================== beginLuminosityBlock ========================//
00107 //==================================================================//
00108 void DQMClientExample::beginLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
00109    // optionally reset histograms here
00110    // clientHisto->Reset();
00111 }
00112 
00113 //==================================================================//
00114 //==================== analyse (takes each event) ==================//
00115 //==================================================================//
00116 void DQMClientExample::analyze(const Event& e, const EventSetup& context){
00117   counterEvt_++;
00118   if(clientOnEachEvent){
00119    if (prescaleEvt_>0 && counterEvt_ % prescaleEvt_ == 0) performClient();
00120   }
00121 }
00122 
00123 //==================================================================//
00124 //========================= endLuminosityBlock =====================//
00125 //==================================================================//
00126 void DQMClientExample::endLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
00127  counterLS_ ++;
00128   if(!clientOnEachEvent && clientOnEndLumi){
00129   if( prescaleLS_ > 0  && counterLS_ % prescaleLS_ == 0) performClient();
00130  } 
00131 }
00132 //==================================================================//
00133 //============================= endRun =============================//
00134 //==================================================================//
00135 void DQMClientExample::endRun(const Run& r, const EventSetup& context){
00136   if(clientOnEndRun) performClient();
00137 }
00138 
00139 //==================================================================//
00140 //============================= endJob =============================//
00141 //==================================================================//
00142 void DQMClientExample::endJob(){
00143    std::cout << "DQMSourceClient::endJob()" << std::endl;
00144    if(clientOnEndJob) performClient();
00145 }
00146 
00147 
00148 //==================================================================//
00149 //======================= performClient ============================//
00150 //==================================================================//
00151 void DQMClientExample::performClient(){
00152 
00153    std::cout << "***** run  Client operations as defined in: *****" << std::endl;
00154    std::cout << "***** DQMClientExample::performClient    ********" << std::endl;
00155 
00156   //----------------------------------------------------------------------------------------------
00157   // example how to retrieve a ME created by the DQM producer (in Examples/src/DQMSourceExample.cc) 
00158   //---------------------------------------------------------------------------------------------
00159    float mean =0;  float rms = 0;
00160   
00162   string histoName = monitorName_ + "DQMsource/C1/histo2";
00163   MonitorElement * meHisto = dbe_->get(histoName);
00165   if (meHisto) { 
00167     if (TH1F *rootHisto = meHisto->getTH1F()) {
00168       if(counterClientOperation<1)
00169       std::cout<<"=== DQMClientExample::performClient => the histo is found: entries="<< rootHisto->GetEntries() << "\n" 
00170               <<" mean=" << rootHisto->GetMean() << " rms=" << rootHisto->GetRMS() << std::endl;
00171 
00173       TF1 *f1 = new TF1("f1","gaus",1,3);
00174       rootHisto->Fit("f1");
00175       mean = f1->GetParameter(1);
00176       rms = f1->GetParameter(2);
00177     }
00178   clientHisto->setBinContent(1,mean);
00179   clientHisto->setBinContent(2,rms);
00180   }
00181 
00182   else { //when it is not found !
00183   clientHisto->setBinContent(1,-1);
00184   clientHisto->setBinContent(2,-1);
00185  if(counterClientOperation<1)  edm::LogError ("DQMClientExample") <<"--- The following ME :"<< histoName <<" cannot be found\n" 
00186                                <<"--- on the DQM source side !\n";
00187   }
00188  
00189   //----------------------------------------------------------------------------------------------
00190   // example  how to access the Quality test result for clientHisto
00191   //---------------------------------------------------------------------------------------------
00192 
00193  // qtest to be ran on clientHisto is defined in Examples/test/QualityTests.xml
00194   const QReport * theQReport = clientHisto->getQReport(QTestName_);
00195   if(theQReport) {
00196     if(counterClientOperation<1)
00197     edm::LogWarning ("DQMClientExample") <<"*** Summary of Quality Test for clientHisto: \n"
00198                                        //<<"---  value  ="<< theQReport->getQTresult()<<"\n"
00199                                       <<"--- status ="<< theQReport->getStatus() << "\n"
00200                                       <<"--- message ="<< theQReport->getMessage() << "\n";
00201 
00202    vector<dqm::me_util::Channel> badChannels = theQReport->getBadChannels();
00203     for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00204          channel != badChannels.end(); channel++) {
00205         if(counterClientOperation<1) 
00206         edm::LogError ("DQMClientExample") <<" Bad channels: "<<(*channel).getBin()<<" "<<(*channel).getContents();
00207     }
00208   } 
00209   else { 
00210   if(counterClientOperation<1) edm::LogError ("DQMClientExample") <<"--- No QReport is found for clientHisto!\n"
00211   << "--- Please check your XML and config files -> QTestName ( " << QTestName_ << " )must be the same!\n";
00212   }
00213 
00214  counterClientOperation++;
00215 }
00216