CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:33:35 2009 for CMSSW by  doxygen 1.5.4