CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DQM/SiStripMonitorClient/src/SiStripSummaryCreator.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripMonitorClient/interface/SiStripSummaryCreator.h"
00002 #include "DQM/SiStripMonitorClient/interface/SiStripConfigParser.h"
00003 #include "DQMServices/Core/interface/DQMStore.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "FWCore/ParameterSet/interface/FileInPath.h"
00006 
00007 #include <iostream>
00008 //
00009 // -- Constructor
00010 // 
00011 SiStripSummaryCreator::SiStripSummaryCreator() {
00012   edm::LogInfo("SiStripSummaryCreator") << 
00013     " Creating SiStripSummaryCreator " << "\n" ;
00014   summaryMEMap.clear();
00015   summaryFrequency_ = -1;
00016 }
00017 //
00018 // --  Destructor
00019 // 
00020 SiStripSummaryCreator::~SiStripSummaryCreator() {
00021   edm::LogInfo("SiStripSummaryCreator") << 
00022     " Deleting SiStripSummaryCreator " << "\n" ;
00023   summaryMEMap.clear();
00024 }
00025 //
00026 // -- Read Configuration
00027 //
00028 bool SiStripSummaryCreator::readConfiguration(std::string& file_path) {
00029     summaryMEMap.clear();
00030   SiStripConfigParser config_parser;
00031   config_parser.getDocument(edm::FileInPath(file_path).fullPath());
00032   if (!config_parser.getFrequencyForSummary(summaryFrequency_)){
00033     std::cout << "SiStripSummaryCreator::readConfiguration: Failed to read Summary configuration parameters!! ";
00034     summaryFrequency_ = -1;
00035     return false;
00036   }  
00037   if (!config_parser.getMENamesForSummary(summaryMEMap)) {
00038     std::cout << "SiStripSummaryCreator::readConfiguration: Failed to read Summary configuration parameters!! ";
00039     return false;
00040   }
00041   return true;
00042 }
00043 //
00044 // -- Set Summary ME names
00045 //
00046 void SiStripSummaryCreator::setSummaryMENames(std::map<std::string, std::string>& me_names) {
00047 
00048   summaryMEMap.clear();
00049   for (std::map<std::string,std::string>::const_iterator isum = me_names.begin();
00050        isum != me_names.end(); isum++) {    
00051     summaryMEMap.insert(std::pair<std::string,std::string>(isum->first, isum->second));
00052   }
00053 }
00054 //
00055 // -- Browse through the Folder Structure
00056 //
00057 void SiStripSummaryCreator::createSummary(DQMStore* dqm_store) {
00058   if (summaryMEMap.size() == 0) return;
00059   std::string currDir = dqm_store->pwd();
00060   std::vector<std::string> subdirs = dqm_store->getSubdirs();
00061   int nmod = 0;
00062   for (std::vector<std::string>::const_iterator it = subdirs.begin();
00063        it != subdirs.end(); it++) {
00064     if ( (*it).find("module_") == std::string::npos) continue;
00065     nmod++;       
00066   }  
00067   if (nmod > 0) {
00068     fillSummaryHistos(dqm_store);
00069   } else {  
00070     for (std::vector<std::string>::const_iterator it = subdirs.begin();
00071        it != subdirs.end(); it++) {
00072       dqm_store->cd(*it);
00073       createSummary(dqm_store);
00074       dqm_store->goUp();
00075     }
00076     fillGrandSummaryHistos(dqm_store);
00077   }
00078 }
00079 //
00080 // -- Create and Fill Summary Histograms at the lowest level of the structure
00081 //
00082 void SiStripSummaryCreator::fillSummaryHistos(DQMStore* dqm_store) {
00083   std::string currDir = dqm_store->pwd();
00084   std::map<std::string, MonitorElement*> MEMap;
00085   std::vector<std::string> subdirs = dqm_store->getSubdirs();
00086   if (subdirs.size() ==0) return;
00087   
00088 
00089   for (std::map<std::string,std::string>::const_iterator isum = summaryMEMap.begin();
00090        isum != summaryMEMap.end(); isum++) {    
00091     std::string name = isum->first;
00092     int iBinStep = 0;
00093     int ndet = 0;
00094     std::string htype = isum->second;
00095     for (std::vector<std::string>::const_iterator it = subdirs.begin();
00096          it != subdirs.end(); it++) {
00097       if ( (*it).find("module_") == std::string::npos) continue;
00098       dqm_store->cd(*it);
00099       ndet++;
00100       std::vector<MonitorElement*> contents = dqm_store->getContents(dqm_store->pwd());
00101       dqm_store->goUp();
00102       for (std::vector<MonitorElement *>::const_iterator im = contents.begin();
00103                 im != contents.end(); im++) {
00104         MonitorElement * me_i = (*im);
00105         if (!me_i) continue;
00106         std::string name_i = me_i->getName();
00107         if (name_i.find(name) == std::string::npos) continue;
00108         std::map<std::string, MonitorElement*>::iterator iPos = MEMap.find(name); 
00109         MonitorElement* me;
00110         // Get the Summary ME
00111         if (iPos == MEMap.end()){
00112             me = getSummaryME(dqm_store, name, htype);
00113             MEMap.insert(std::pair<std::string, MonitorElement*>(name, me));
00114         } else  me =  iPos->second;
00115         // Fill it now
00116         fillHistos(ndet, iBinStep, htype, me_i, me); 
00117         iBinStep += me_i->getNbinsX();
00118         break;
00119       }
00120     }
00121   }
00122 }
00123 //
00124 //  -- Fill Summary Histogram at higher level
00125 //
00126 void SiStripSummaryCreator::fillGrandSummaryHistos(DQMStore* dqm_store) {
00127   std::map<std::string, MonitorElement*> MEMap;
00128   std::string currDir = dqm_store->pwd();
00129   std::string dir_name =  currDir.substr(currDir.find_last_of("/")+1);
00130   if ((dir_name.find("SiStrip") == 0) ||
00131       (dir_name.find("Collector") == 0) ||
00132       (dir_name.find("MechanicalView") == 0) ||
00133       (dir_name.find("FU") == 0) ) return;
00134   std::vector<std::string> subdirs = dqm_store->getSubdirs();
00135   if (subdirs.size() == 0) return;;
00136   for (std::map<std::string,std::string>::const_iterator isum = summaryMEMap.begin();
00137        isum != summaryMEMap.end(); isum++) {
00138     std::string name, summary_name;
00139     name = isum->first;
00140     if (isum->second == "sum" || isum->second == "sum")    
00141       summary_name = "Summary_" + isum->first;
00142     else 
00143       summary_name = "Summary_Mean" + isum->first;
00144     std::string htype = isum->second;
00145     int ibinStep =0;
00146     for (std::vector<std::string>::const_iterator it = subdirs.begin();
00147          it != subdirs.end(); it++) {
00148       dqm_store->cd(*it);
00149       std::vector<MonitorElement*> contents = dqm_store->getContents(dqm_store->pwd());
00150       dqm_store->goUp();
00151       for (std::vector<MonitorElement *>::const_iterator im = contents.begin();
00152                 im != contents.end(); im++) {
00153         MonitorElement * me_i = (*im);
00154         if (!me_i) continue;
00155         std::string name_i = me_i->getName();
00156         if (name_i.find((summary_name)) != std::string::npos) {
00157           
00158           std::map<std::string, MonitorElement*>::iterator iPos = MEMap.find(name); 
00159           MonitorElement* me; 
00160           if (iPos == MEMap.end()) {
00161             if (htype == "sum" || htype == "Sum") {
00162               me = getSummaryME(dqm_store, name, htype);
00163             } else {
00164               me = getSummaryME(dqm_store, name, "bin-by-bin");              
00165             }
00166             MEMap.insert(std::pair<std::string, MonitorElement*>(name, me));
00167           } else  me =  iPos->second;
00168           if (htype == "sum" || htype == "Sum") {
00169             fillHistos(0, ibinStep, htype, me_i, me);
00170           } else {
00171             fillHistos(0, ibinStep,"bin-by-bin", me_i, me);
00172           }
00173           ibinStep += me_i->getNbinsX();
00174           break;
00175         }
00176       }
00177     }
00178   }
00179 }
00180 //
00181 // -- Get Summary ME
00182 //
00183 MonitorElement* SiStripSummaryCreator::getSummaryME(DQMStore* dqm_store, 
00184                          std::string& name, std::string htype) {
00185   MonitorElement* me = 0;
00186   std::string currDir = dqm_store->pwd();
00187   std::string sum_name, tag_name;
00188  
00189   std::string dname = currDir.substr(currDir.find_last_of("/")+1);
00190   if (dname.find("_") != std::string::npos) dname.insert(dname.find("_"),"_");
00191   if (htype == "sum" && htype == "Sum") {
00192     sum_name = "Summary" + name + "__" + dname;
00193     tag_name = "Summary" + name;
00194   } else {
00195     sum_name = "Summary_Mean" + name + "__" + dname;
00196     tag_name = "Summary_Mean" + name;
00197   }
00198   // If already booked
00199   std::vector<MonitorElement*> contents = dqm_store->getContents(currDir);
00200   for (std::vector<MonitorElement *>::const_iterator im = contents.begin();
00201                 im != contents.end(); im++) {
00202     MonitorElement * me = (*im);
00203     if (!me)  continue;
00204     std::string me_name = me->getName();
00205     if (me_name.find(sum_name) == 0) {
00206       if (me->kind() == MonitorElement::DQM_KIND_TH1F ||     
00207           me->kind() == MonitorElement::DQM_KIND_TH2F ||
00208           me->kind() == MonitorElement::DQM_KIND_TPROFILE) {
00209         TH1* hist1 = me->getTH1();
00210         if (hist1) {
00211           hist1->Reset();
00212           return me;
00213         }
00214       }
00215     }
00216   }
00217   std::map<int, std::string> tags;
00218   if (!me) {
00219     int nBins = 0;
00220     std::vector<std::string> subdirs = dqm_store->getSubdirs();
00221     // set # of bins of the histogram
00222     if (htype == "mean" || htype == "Mean" ) {
00223        nBins = subdirs.size();
00224        me = dqm_store->book1D(sum_name,sum_name,nBins,0.5,nBins+0.5);
00225        int ibin = 0;
00226        for (std::vector<std::string>::const_iterator it = subdirs.begin();
00227           it != subdirs.end(); it++) {
00228          std::string subdir_name = (*it).substr((*it).find_last_of("/")+1);
00229          ibin++;
00230          tags.insert(std::pair<int,std::string>(ibin, (subdir_name)));        
00231        }
00232     } else if (htype == "bin-by-bin" || htype == "Bin-by-Bin") {
00233       for (std::vector<std::string>::const_iterator it = subdirs.begin();
00234            it != subdirs.end(); it++) {
00235         dqm_store->cd(*it);
00236         std::string subdir_name = (*it).substr((*it).find_last_of("/")+1);
00237         std::vector<MonitorElement*> s_contents = dqm_store->getContents(dqm_store->pwd());
00238         for (std::vector<MonitorElement *>::const_iterator iv = s_contents.begin();
00239                 iv != s_contents.end(); iv++) {
00240           MonitorElement* s_me = (*iv);
00241           if (!s_me) continue;
00242           std::string s_me_name = s_me->getName();
00243           if (s_me_name.find(name) == 0 || s_me_name.find(tag_name) == 0) {
00244             int ibin = s_me->getNbinsX();
00245             nBins += ibin;
00246             tags.insert(std::pair<int,std::string>(nBins-ibin/2, (subdir_name)));        
00247             break;
00248           }
00249         }
00250         dqm_store->goUp();
00251       }
00252       me = dqm_store->book1D(sum_name,sum_name,nBins,0.5,nBins+0.5);
00253     } else if (htype == "sum" || htype == "Sum") {
00254       for (std::vector<std::string>::const_iterator it = subdirs.begin();
00255            it != subdirs.end(); it++) {
00256         dqm_store->cd(*it);
00257         std::vector<MonitorElement*> s_contents = dqm_store->getContents(dqm_store->pwd());
00258         dqm_store->goUp();        
00259         for (std::vector<MonitorElement *>::const_iterator iv = s_contents.begin();
00260                 iv != s_contents.end(); iv++) {
00261           MonitorElement* s_me = (*iv);
00262           if (!s_me) continue;
00263           std::string s_me_name = s_me->getName();
00264           if (s_me_name.find(name) == std::string::npos) continue;
00265           if (s_me->kind() == MonitorElement::DQM_KIND_TH1F) {
00266             TH1F* hist1 = s_me->getTH1F();
00267             if (hist1) {
00268               nBins = s_me->getNbinsX();
00269               me = dqm_store->book1D(sum_name,sum_name,nBins,
00270                  hist1->GetXaxis()->GetXmin(),hist1->GetXaxis()->GetXmax());
00271               break;
00272             }
00273           }
00274         }
00275       }
00276     }
00277   }
00278   // Set the axis title 
00279   if (me && me->kind() == MonitorElement::DQM_KIND_TH1F 
00280       && (htype != "sum" || htype != "Sum")) {
00281     TH1F* hist = me->getTH1F();
00282     if (hist) {
00283       if (name.find("NoisyStrips") != std::string::npos) hist->GetYaxis()->SetTitle("Noisy Strips (%)");
00284       else hist->GetYaxis()->SetTitle(name.c_str());
00285     }
00286     for (std::map<int,std::string>::const_iterator ic = tags.begin();
00287          ic != tags.end(); ic++) {
00288       hist->GetXaxis()->SetBinLabel(ic->first, (ic->second).c_str());
00289     }
00290     hist->LabelsOption("uv");
00291   }
00292   return me;
00293 }
00294 //
00295 //
00296 //
00297 void SiStripSummaryCreator::fillHistos(int ival, int istep, std::string htype, 
00298                        MonitorElement* me_src, MonitorElement* me) {
00299   
00300   if (me->getTH1()) {
00301     //    TProfile* prof = 0;
00302     TH1F* hist1 = 0;
00303     TH2F* hist2 = 0;
00304     if (me->kind() == MonitorElement::DQM_KIND_TH1F)    hist1 = me->getTH1F();
00305     if (me->kind() == MonitorElement::DQM_KIND_TH2F)    hist2 = me->getTH2F();
00306     //    if (me->kind() == MonitorElement::DQM_KIND_TPROFILE) prof = me->getTProfile();
00307     
00308     int nbins = me_src->getNbinsX();
00309     std::string name = me_src->getName();
00310     if (htype == "mean" || htype == "Mean" ) {
00311       if (hist2 &&  name.find("NoisyStrips") != std::string::npos) {
00312         float bad = 0.0;
00313         float entries = me_src->getEntries();
00314         if (entries > 0.0) {
00315           float binEntry = entries/nbins;
00316           for (int k=1; k<nbins+1; k++) {
00317             float noisy = me_src->getBinContent(k,3)+me_src->getBinContent(k,5);
00318             float dead = me_src->getBinContent(k,2)+me_src->getBinContent(k,4);
00319 //          float good = me_src->getBinContent(k,1);
00320             if (noisy >= binEntry*0.5 || dead >= binEntry*0.5) bad++;
00321           }
00322           bad = bad*100.0/nbins;    
00323           me->Fill(ival, bad);
00324         }
00325       } else me->Fill(ival, me_src->getMean());
00326     } else if (htype == "bin-by-bin" || htype == "Bin-by-Bin") {
00327       for (int k=1; k<nbins+1; k++) {
00328         me->setBinContent(istep+k,me_src->getBinContent(k));
00329       }
00330     } else if (htype == "sum" || htype == "Sum") {  
00331       if ( hist1) {
00332         for (int k=1; k<nbins+1; k++) {
00333           float val = me_src->getBinContent(k) + me->getBinContent(k) ;
00334           me->setBinContent(k,val);
00335         }
00336       }        
00337     }
00338   }
00339 }