CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/SiPixelHistoricInfoClient/src/SiPixelHistoricInfoEDAClient.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <math.h>
00003 #include <iostream>
00004 #include <string>
00005 #include <stdio.h>
00006 #include <sys/time.h>
00007 
00008 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00009 #include "DQM/SiPixelHistoricInfoClient/interface/SiPixelHistoricInfoEDAClient.h"
00010 
00011 
00012 using namespace std;
00013 using namespace edm;
00014 // using namespace cgicc;
00015 // using namespace xcept;
00016 
00017 
00018 SiPixelHistoricInfoEDAClient::SiPixelHistoricInfoEDAClient(const ParameterSet& parameterSet) { 
00019   dbe_ = Service<DQMStore>().operator->(); 
00020   
00021   parameterSet_ = parameterSet;  
00022   
00023   printDebug_ = parameterSet_.getUntrackedParameter<bool>("printDebug", false); 
00024   writeHisto_ = parameterSet_.getUntrackedParameter<bool>("writeHisto", false); 
00025   
00026   outputDir_ = parameterSet_.getUntrackedParameter<string>("outputDir", ".");
00027 }
00028 
00029 
00030 SiPixelHistoricInfoEDAClient::~SiPixelHistoricInfoEDAClient() {}
00031 
00032 
00033 void SiPixelHistoricInfoEDAClient::beginJob() {}
00034 
00035 
00036 void SiPixelHistoricInfoEDAClient::beginRun(const Run& run, const EventSetup& eventSetup) {
00037   performanceSummary = new SiPixelPerformanceSummary(); 
00038   performanceSummary->setRunNumber(run.run());
00039 
00040   nEventsInRun = 0; 
00041   firstEventInRun = true; 
00042 }
00043 
00044 
00045 void SiPixelHistoricInfoEDAClient::beginLuminosityBlock(const edm::LuminosityBlock& luminosityBlock, 
00046                                                         const edm::EventSetup& eventSetup) {
00047   performanceSummary->setLuminosityBlock(luminosityBlock.luminosityBlock());
00048 }
00049 
00050 
00051 void SiPixelHistoricInfoEDAClient::analyze(const Event& event, const EventSetup& eventSetup) {
00052   nEventsInRun++; 
00053   if (firstEventInRun) { 
00054     firstEventInRun = false; 
00055 
00056     performanceSummary->setTimeStamp(event.time().value()); 
00057   }
00058 }
00059 
00060 
00061 void SiPixelHistoricInfoEDAClient::endLuminosityBlock(const edm::LuminosityBlock& luminosityBlock, 
00062                                                       const edm::EventSetup& eventSetup) {}
00063 
00064 
00065 void SiPixelHistoricInfoEDAClient::endRun(const Run& run, const EventSetup& eventSetup) {
00066   performanceSummary->setNumberOfEvents(nEventsInRun);
00067 
00068   retrieveMEs();
00069   fillPerformanceSummary();
00070   writeDB(); 
00071   
00072   if (writeHisto_) {
00073     ostringstream endRunOutputFile; 
00074     endRunOutputFile << outputDir_ << "/SiPixelHistoricInfoEDAClient_" << run.run() <<".root"; 
00075     dbe_->save(endRunOutputFile.str()); 
00076   }
00077 }
00078 
00079 
00080 void SiPixelHistoricInfoEDAClient::endJob() {
00081   if (writeHisto_) {
00082     ostringstream endJobOutputFile; 
00083     endJobOutputFile << outputDir_ << "/SiPixelHistoricInfoEDAClient_endJob.root"; 
00084     dbe_->save(endJobOutputFile.str());
00085   }
00086 }
00087 
00088 
00089 void SiPixelHistoricInfoEDAClient::retrieveMEs() {
00090   mapOfdetIDtoMEs.clear(); 
00091   
00092   vector<string> listOfMEswithFullPath;
00093   dbe_->getContents(listOfMEswithFullPath);
00094     
00095   for (vector<string>::const_iterator iMEstr = listOfMEswithFullPath.begin(); 
00096        iMEstr!=listOfMEswithFullPath.end(); iMEstr++) {
00097     if (printDebug_) cout << iMEstr->data() << endl; 
00098       
00099     size_t pathLength = iMEstr->find(":",0);     
00100     string thePath = iMEstr->substr(0, pathLength); 
00101     string allHists = iMEstr->substr(pathLength+1); 
00102         
00103     if (thePath.find("Pixel",0)!=string::npos) { 
00104       if (thePath.find("Track",0)!=string::npos) { // for Pixel/Tracks, Pixel/Clusters/On,OffTrack
00105                                         uint32_t newMEdetID = 77; 
00106         if (thePath.find("On", 0)!=string::npos) newMEdetID = 78; 
00107         if (thePath.find("Off",0)!=string::npos) newMEdetID = 79; 
00108         
00109         size_t histnameLength;
00110         do {
00111           histnameLength = allHists.find(",",0);
00112           string theHist;
00113           if (histnameLength!=string::npos) {
00114             theHist = allHists.substr(0, histnameLength);
00115             allHists.erase(0, histnameLength+1);
00116           }
00117           else theHist = allHists; 
00118 
00119           string fullPathHist = thePath + "/" + theHist; 
00120           MonitorElement* newME = dbe_->get(fullPathHist); 
00121           if (newME) {
00122             if (mapOfdetIDtoMEs.find(newMEdetID)==mapOfdetIDtoMEs.end()) {
00123               vector<MonitorElement*> newMEvector;
00124               newMEvector.push_back(newME);
00125               mapOfdetIDtoMEs.insert(make_pair(newMEdetID, newMEvector));
00126             }
00127             else (mapOfdetIDtoMEs.find(newMEdetID)->second).push_back(newME);
00128           } 
00129         } 
00130         while (histnameLength!=string::npos); 
00131       }
00132       else {
00133         size_t histnameLength; 
00134         do {
00135           histnameLength = allHists.find(",",0);
00136           string theHist;
00137           if (histnameLength!=string::npos) {
00138             theHist = allHists.substr(0, histnameLength);
00139             allHists.erase(0, histnameLength+1);
00140           }
00141           else theHist = allHists; 
00142 
00143           string fullPathHist = thePath + "/" + theHist;
00144           MonitorElement* newME = dbe_->get(fullPathHist);
00145           if (newME) {
00146             uint32_t newMEdetID = histogramManager.getRawId(newME->getName());
00147 
00148             if (mapOfdetIDtoMEs.find(newMEdetID)==mapOfdetIDtoMEs.end()) {
00149               vector<MonitorElement*> newMEvector;
00150               newMEvector.push_back(newME);
00151               mapOfdetIDtoMEs.insert(make_pair(newMEdetID, newMEvector));
00152             }
00153             else ((mapOfdetIDtoMEs.find(newMEdetID))->second).push_back(newME);
00154           }
00155         } 
00156         while (histnameLength!=string::npos); 
00157       }
00158     } 
00159   } 
00160 }
00161 
00162 
00163 void SiPixelHistoricInfoEDAClient::fillPerformanceSummary() const {
00164   for (map< uint32_t, vector<MonitorElement*> >::const_iterator iMEvec=mapOfdetIDtoMEs.begin(); 
00165        iMEvec!=mapOfdetIDtoMEs.end(); iMEvec++) {
00166     uint32_t theMEdetID = iMEvec->first;
00167     vector<MonitorElement*> theMEvector = iMEvec->second;
00168     
00169     if (printDebug_) { 
00170       cout << theMEdetID << ":"; for (vector<MonitorElement*>::const_iterator iME = theMEvector.begin(); 
00171                                       iME!=theMEvector.end(); iME++) cout << (*iME)->getName() << ","; cout << endl; 
00172     }     
00173     for (vector<MonitorElement*>::const_iterator iME = theMEvector.begin(); iME!=theMEvector.end(); iME++) {
00174       string theMEname = (*iME)->getName();
00175 
00176       
00177       if (theMEdetID<50) {
00178         // from SiPixelMonitorRawData
00179         if (theMEname.find("errorType_siPixelDigis")!=string::npos) { 
00180           for (int v=25; v<40; v++) {
00181             int b = (*iME)->getTH1()->GetXaxis()->FindBin(v); 
00182             performanceSummary->setRawDataErrorType(theMEdetID, v-25, (*iME)->getBinContent(b)); 
00183           }
00184         } 
00185       } 
00186       // from SiPixelMonitorTrack
00187       else if (theMEdetID==77) {
00188         if (theMEname.find("ntracks")!=string::npos && theMEname.find("InPixVol")==string::npos) { 
00189           float trkMean=0.0, trkRMS=0.0; 
00190           if ((*iME)->getBinContent(1)>0.0) {
00191             trkMean = float((*iME)->getBinContent(3))/(*iME)->getBinContent(1); // Barrel regionID: 80
00192             trkRMS = sqrt(trkMean*(trkMean+1.0)/(*iME)->getBinContent(1)); 
00193             performanceSummary->setFractionOfTracks(80, trkMean, trkRMS); 
00194 
00195             trkMean = float((*iME)->getBinContent(4))/(*iME)->getBinContent(1); // Endcap regionID: 81
00196             trkRMS = sqrt(trkMean*(trkMean+1.0)/(*iME)->getBinContent(1)); 
00197             performanceSummary->setFractionOfTracks(81, trkMean, trkRMS); 
00198           }
00199         }
00200       } 
00201       else if (theMEdetID==78) { // OnTrack
00202         if (theMEname.find("nclusters_siPixelClusters")!=string::npos) { 
00203           performanceSummary->setNumberOfOnTrackClusters(80, (*iME)->getBinContent(2)); 
00204           performanceSummary->setNumberOfOnTrackClusters(81, (*iME)->getBinContent(3)); 
00205         }
00206         if (theMEname.find("charge_siPixelClusters_Barrel")!=string::npos) {
00207           performanceSummary->setClusterChargeOnTrack(80, (*iME)->getMean(), (*iME)->getRMS()); 
00208         }
00209         if (theMEname.find("charge_siPixelClusters_Endcap")!=string::npos) {
00210           performanceSummary->setClusterChargeOnTrack(81, (*iME)->getMean(), (*iME)->getRMS()); 
00211         }
00212         if (theMEname.find("size_siPixelClusters_Barrel")!=string::npos) {
00213           performanceSummary->setClusterSizeOnTrack(80, (*iME)->getMean(), (*iME)->getRMS()); 
00214         }
00215         if (theMEname.find("size_siPixelClusters_Endcap")!=string::npos) {
00216           performanceSummary->setClusterSizeOnTrack(81, (*iME)->getMean(), (*iME)->getRMS()); 
00217         }
00218       }
00219       else if (theMEdetID==79) { // OffTrack
00220         if (theMEname.find("nclusters_siPixelClusters")!=string::npos) { 
00221           performanceSummary->setNumberOfOffTrackClusters(80, (*iME)->getBinContent(2)); 
00222           performanceSummary->setNumberOfOffTrackClusters(81, (*iME)->getBinContent(3)); 
00223         }
00224         if (theMEname.find("charge_siPixelClusters_Barrel")!=string::npos) {
00225           performanceSummary->setClusterChargeOffTrack(80, (*iME)->getMean(), (*iME)->getRMS()); 
00226         }
00227         if (theMEname.find("charge_siPixelClusters_Endcap")!=string::npos) {
00228           performanceSummary->setClusterChargeOffTrack(81, (*iME)->getMean(), (*iME)->getRMS()); 
00229         }
00230         if (theMEname.find("size_siPixelClusters_Barrel")!=string::npos) {
00231           performanceSummary->setClusterSizeOffTrack(80, (*iME)->getMean(), (*iME)->getRMS()); 
00232         }
00233         if (theMEname.find("size_siPixelClusters_Endcap")!=string::npos) {
00234           performanceSummary->setClusterSizeOffTrack(81, (*iME)->getMean(), (*iME)->getRMS()); 
00235         }
00236       }
00237       else {
00238         // from SiPixelMonitorDigi 
00239         if (theMEname.find("ndigis_siPixelDigis")!=string::npos) { 
00240           performanceSummary->setNumberOfDigis(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00241         } 
00242         if (theMEname.find("adc_siPixelDigis")!=string::npos) { 
00243           performanceSummary->setADC(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00244         } 
00245         // from SiPixelMonitorCluster
00246         if (theMEname.find("nclusters_siPixelClusters")!=string::npos) {
00247           performanceSummary->setNumberOfClusters(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00248         }
00249         if (theMEname.find("charge_siPixelClusters")!=string::npos) {
00250           performanceSummary->setClusterCharge(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00251         } 
00252         if (theMEname.find("size_siPixelClusters")!=string::npos) {
00253           performanceSummary->setClusterSize(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00254         } 
00255         if (theMEname.find("sizeX_siPixelClusters")!=string::npos) {
00256           performanceSummary->setClusterSizeX(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00257         }
00258         if (theMEname.find("sizeY_siPixelClusters")!=string::npos) {
00259           performanceSummary->setClusterSizeY(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00260         }
00261         // from SiPixelMonitorRecHit
00262         if (theMEname.find("nRecHits_siPixelRecHits")!=string::npos) {
00263           performanceSummary->setNumberOfRecHits(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00264         }
00265         // from SiPixelMonitorTrack
00266         if (theMEname.find("residualX_siPixelTrackResiduals")!=string::npos) {
00267           performanceSummary->setResidualX(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00268         }
00269         if (theMEname.find("residualY_siPixelTrackResiduals")!=string::npos) {
00270           performanceSummary->setResidualY(theMEdetID, (*iME)->getMean(), (*iME)->getRMS(), (*iME)->getEntries()==0.0);
00271         } 
00272         // temporary solutions
00273         if (theMEname.find("hitmap_siPixelClusters")!=std::string::npos || 
00274             theMEname.find("hitmap_siPixelDigis")!=std::string::npos) { // if digi map exists, use it; else use cluster map
00275           int nNoisyCells=0, nEmptyCells=0;                             //                     not use xypos_siPixelRecHits
00276           for (int xBin=0; xBin<(*iME)->getNbinsX(); xBin++) {          // 4-1 pixel per bin
00277             for (int yBin=0; yBin<(*iME)->getNbinsY(); yBin++) { 
00278               if ((*iME)->getBinContent(xBin+1, yBin+1)>0.01*(*iME)->getEntries()) nNoisyCells++; 
00279               if ((*iME)->getBinContent(xBin+1, yBin+1)==.0 && (*iME)->getBinError(xBin+1, yBin+1)==0.0) nEmptyCells++; 
00280             } 
00281           } 
00282           performanceSummary->setNumberOfNoisCells(theMEdetID, float(nNoisyCells)); 
00283           performanceSummary->setNumberOfDeadCells(theMEdetID, float(nEmptyCells)); 
00284         } 
00285         // performanceSummary->setNumberOfPixelHitsInTrackFit(theMEdetId, float(nPixelHits)); 
00286       }
00287     }
00288   }
00289 }
00290 
00291 
00292 void SiPixelHistoricInfoEDAClient::writeDB() const {  
00293   if (printDebug_) performanceSummary->printAll(); 
00294   else performanceSummary->print(); 
00295   cout << "SiPixelHistoricInfoEDAClient::writeDB()" << endl; 
00296 
00297   Service<cond::service::PoolDBOutputService> mydbservice; 
00298   if (mydbservice.isAvailable()) {
00299     if (mydbservice->isNewTagRequest("SiPixelPerformanceSummaryRcd")) {
00300       mydbservice->createNewIOV<SiPixelPerformanceSummary>(performanceSummary, 
00301                                                            mydbservice->beginOfTime(), 
00302                                                            mydbservice->endOfTime(), 
00303                                                           "SiPixelPerformanceSummaryRcd"); 
00304     } 
00305     else {
00306       mydbservice->appendSinceTime<SiPixelPerformanceSummary>(performanceSummary, 
00307                                                               mydbservice->currentTime(), 
00308                                                              "SiPixelPerformanceSummaryRcd"); 
00309     }
00310   }
00311   else LogError("writeDB") << "service unavailable" << endl; 
00312 }