CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/DQMServices/Diagnostic/src/DQMHistoryServiceBase.cc

Go to the documentation of this file.
00001 #include "DQMServices/Diagnostic/interface/DQMHistoryServiceBase.h"
00002 #include "DQMServices/Core/interface/MonitorElement.h"
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 #include "DQMServices/Diagnostic/interface/HDQMfitUtilities.h"
00005 #include <string>
00006 #include <sstream>
00007 #include <cctype>
00008 #include <time.h>
00009 #include <boost/cstdint.hpp>
00010 #include <boost/regex.hpp>
00011 
00012 DQMHistoryServiceBase::DQMHistoryServiceBase(const edm::ParameterSet& iConfig,const edm::ActivityRegistry& aReg):
00013  iConfig_(iConfig),
00014  fSep("@")
00015 {
00016   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::DQMHistoryServiceBase]";
00017 }
00018 
00019 
00020 DQMHistoryServiceBase::~DQMHistoryServiceBase() { 
00021   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::~DQMHistoryServiceBase]";
00022 }
00023 
00024 void DQMHistoryServiceBase::initialize(){
00025   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::initialize]";
00026   fitME = new HDQMfitUtilities();
00027 }
00028 
00029 void DQMHistoryServiceBase::setSeparator (std::string const& in) {
00030   fSep = in;
00031   return;
00032 }
00033 
00034 bool DQMHistoryServiceBase::checkForCompatibility(std::string ss){
00035   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::checkForCompatibility]";
00036   if(ss=="")
00037     return true;
00038 
00039   uint32_t previousRun=atoi(ss.substr(ss.find("Run ")+4).c_str());
00040   
00041   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::checkForCompatibility] extracted string " << previousRun ;
00042   return previousRun<getRunNumber();
00043 }
00044 
00045 void DQMHistoryServiceBase::createSummary(){
00046     
00047   //LOOP OVER THE LIST OF SUMMARY OBJECTS TO INSERT IN DB
00048 
00049   obj_=new HDQMSummary();
00050 
00051   obj_->setRunNr(getRunNumber());
00052 
00053   // DISCOVER SET OF HISTOGRAMS & QUANTITIES TO BE UPLOADED
00054   std::vector<std::string> userDBContent;
00055   typedef std::vector<edm::ParameterSet> VParameters;
00056   VParameters histoList = iConfig_.getParameter<VParameters>("histoList");
00057   VParameters::iterator ithistoList = histoList.begin();
00058   VParameters::iterator ithistoListEnd = histoList.end();
00059   
00060   for(; ithistoList != ithistoListEnd; ++ithistoList ) {    
00061     std::string keyName = ithistoList->getUntrackedParameter<std::string>("keyName");
00062     std::vector<std::string> Quantities = ithistoList->getUntrackedParameter<std::vector<std::string> >("quantitiesToExtract"); 
00063     for (size_t i=0;i<Quantities.size();++i){
00064       
00065       if  ( Quantities[i] == "landau" )
00066         setDBLabelsForLandau(keyName, userDBContent);
00067       else if  ( Quantities[i] == "gauss" )
00068         setDBLabelsForGauss(keyName, userDBContent);
00069       else if  ( Quantities[i] == "stat" )
00070         setDBLabelsForStat(keyName, userDBContent);
00071       else 
00072         setDBLabelsForUser(keyName, userDBContent, Quantities[i]);
00073     }
00074   }
00075   obj_->setUserDBContent(userDBContent);
00076   
00077   std::stringstream ss;
00078   ss << "[DQMHistoryServiceBase::scanTreeAndFillSummary] QUANTITIES TO BE INSERTED IN DB :" << std::endl;  
00079   std::vector<std::string> userDBContentA = obj_->getUserDBContent();
00080   for (size_t i=0;i<userDBContentA.size();++i){
00081     ss << userDBContentA[i]<< std::endl;
00082   }
00083   edm::LogInfo("HDQMSummary") << ss.str();
00084 
00085   // OPEN DQM FILE
00086   openRequestedFile();
00087   const std::vector<MonitorElement*>& MEs = dqmStore_->getAllContents(iConfig_.getUntrackedParameter<std::string>("ME_DIR","DQMData"));
00088 
00089   // FILL SUMMARY
00090   edm::LogInfo("HDQMSummary") << "\nSTARTING TO FILL OBJECT " << std::endl;
00091   ithistoList = histoList.begin();
00092   for(; ithistoList != ithistoListEnd; ++ithistoList ) {
00093     std::string keyName = ithistoList->getUntrackedParameter<std::string>("keyName");
00094     std::vector<std::string> Quantities = ithistoList->getUntrackedParameter<std::vector<std::string> >("quantitiesToExtract"); 
00095     scanTreeAndFillSummary(MEs, obj_, keyName, Quantities);
00096   }
00097 }
00098 
00099 void DQMHistoryServiceBase::openRequestedFile() { 
00100 
00101   dqmStore_ = edm::Service<DQMStore>().operator->(); 
00102 
00103   if( iConfig_.getParameter<bool>("accessDQMFile") ){
00104     
00105     std::string fileName = iConfig_.getUntrackedParameter<std::string>("FILE_NAME","");
00106     
00107     edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::openRequestedFile] Accessing root File" << fileName;
00108 
00109     dqmStore_->open(fileName, false); 
00110   } else {
00111     edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::openRequestedFile] Accessing dqmStore stream in Online Operation";
00112   }
00113 }
00114 
00115 
00116 void DQMHistoryServiceBase::scanTreeAndFillSummary(const std::vector<MonitorElement*>& MEs,HDQMSummary* summary,std::string& keyName, std::vector<std::string>& Quantities){
00117   //
00118   // -- Scan full root file and fill module numbers and histograms
00119   //
00120   //-----------------------------------------------------------------------------------------------
00121 
00122   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::scanTreeAndFillSummary] keyName " << keyName;
00123 
00124   std::vector<MonitorElement*>::const_iterator iterMes = MEs.begin(); 
00125   std::vector<MonitorElement*>::const_iterator iterMesEnd = MEs.end(); 
00126   std::stringstream ss;
00127 
00128   // Use boost regex for more flexibility
00129   boost::regex re;
00130   try {
00131     re.assign(keyName);
00132   }
00133   catch( boost::regex_error& e ) {
00134     std::cout << "Error: " << keyName << " is not a valid regular expression: \""
00135               << e.what() << "\"" << std::endl;
00136     std::cout << "Skip search for matches" << std::endl;
00137     return;
00138   }
00139   for (; iterMes!=iterMesEnd; ++iterMes){
00140     // Name including path
00141     std::string me_name;
00142     if( iConfig_.getUntrackedParameter<bool>("useFullPath", false) ) {
00143       me_name = (*iterMes)->getFullname();
00144     }
00145     else {
00146       me_name = (*iterMes)->getName();
00147       // If the line does not start with a "^" add it
00148       if( me_name.find("^") != 0 ) {
00149         me_name = "^" + me_name;
00150       }
00151     }
00152     // regex_search has grep-like behaviour
00153     if( boost::regex_search(me_name, re) ) {
00154 
00155       HDQMSummary::InputVector values;
00156       std::vector<std::string> userDBContent;
00157 
00158       ss << "\nFound compatible ME " << me_name << " for key " << keyName << std::endl;
00159 
00160       for(size_t i=0;i<Quantities.size();++i) {
00161 
00162         if(Quantities[i]  == "landau"){
00163           setDBLabelsForLandau(keyName, userDBContent);
00164           setDBValuesForLandau(iterMes,values);
00165         }
00166         else if(Quantities[i]  == "gauss"){
00167           setDBLabelsForGauss(keyName, userDBContent);
00168           setDBValuesForGauss(iterMes,values);
00169         }
00170         else if(Quantities[i]  == "stat"){
00171           setDBLabelsForStat(keyName, userDBContent);
00172           setDBValuesForStat(iterMes,values);
00173         }
00174         else{
00175           setDBLabelsForUser(keyName, userDBContent,Quantities[i]);
00176           setDBValuesForUser(iterMes,values,Quantities[i]);
00177         }
00178       }
00179       uint32_t detid=returnDetComponent(*iterMes);
00180 
00181       ss << "detid " << detid << " \n";
00182       for(size_t i=0;i<values.size();++i)
00183         ss << "Quantity " << userDBContent[i] << " value " << values[i] << std::endl;
00184       
00185       summary->put(detid,values,userDBContent);
00186     }
00187   }
00188   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::scanTreeAndFillSummary] " << ss.str();
00189 }   
00190  
00191 
00192 bool DQMHistoryServiceBase::setDBLabelsForLandau(std::string& keyName, std::vector<std::string>& userDBContent){ 
00193   userDBContent.push_back(keyName+fSep+std::string("landauPeak"));
00194   userDBContent.push_back(keyName+fSep+std::string("landauPeakErr"));
00195   userDBContent.push_back(keyName+fSep+std::string("landauSFWHM"));
00196   userDBContent.push_back(keyName+fSep+std::string("landauChi2NDF"));
00197   return true;
00198 }
00199 
00200 bool DQMHistoryServiceBase::setDBLabelsForGauss(std::string& keyName, std::vector<std::string>& userDBContent){ 
00201   userDBContent.push_back(keyName+fSep+std::string("gaussMean"));
00202   userDBContent.push_back(keyName+fSep+std::string("gaussSigma"));
00203   userDBContent.push_back(keyName+fSep+std::string("gaussChi2NDF"));
00204   return true;
00205 }       
00206 bool DQMHistoryServiceBase::setDBLabelsForStat(std::string& keyName, std::vector<std::string>& userDBContent){ 
00207   userDBContent.push_back(keyName+fSep+std::string("entries"));
00208   userDBContent.push_back(keyName+fSep+std::string("mean"));
00209   userDBContent.push_back(keyName+fSep+std::string("rms"));
00210   return true;
00211 }
00212 
00213 bool DQMHistoryServiceBase::setDBValuesForLandau(std::vector<MonitorElement*>::const_iterator iterMes, HDQMSummary::InputVector& values){
00214   fitME->doLanGaussFit(*iterMes);
00215   values.push_back( fitME->getLanGaussPar("mpv")    ); 
00216   values.push_back( fitME->getLanGaussParErr("mpv") ); 
00217   values.push_back( fitME->getLanGaussConv("fwhm")  );
00218   if (fitME->getFitnDof()!=0 ) values.push_back( fitME->getFitChi()/fitME->getFitnDof() );
00219   else                         values.push_back(-99.);
00220   return true;
00221 }
00222 
00223 bool DQMHistoryServiceBase::setDBValuesForGauss(std::vector<MonitorElement*>::const_iterator iterMes, HDQMSummary::InputVector& values){  
00224   fitME->doGaussFit(*iterMes);
00225   values.push_back( fitME->getGaussPar("mean")  );
00226   values.push_back( fitME->getGaussPar("sigma") );
00227   if (fitME->getFitnDof()!=0 ) values.push_back( fitME->getFitChi()/fitME->getFitnDof() );
00228   else                         values.push_back(-99.);
00229   return true;
00230 }
00231 
00232 bool DQMHistoryServiceBase::setDBValuesForStat(std::vector<MonitorElement*>::const_iterator iterMes, HDQMSummary::InputVector& values){  
00233   values.push_back( (*iterMes)->getEntries());
00234   values.push_back( (*iterMes)->getMean());
00235   values.push_back( (*iterMes)->getRMS());
00236   return true;
00237 }
00238 
00239 uint32_t DQMHistoryServiceBase::getRunNumber() const {
00240   edm::LogInfo("DQMHistoryServiceBase") <<  "[DQMHistoryServiceBase::getRunNumber] " << iConfig_.getParameter<uint32_t>("RunNb");
00241   return iConfig_.getParameter<uint32_t>("RunNb");
00242 }
00243