CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DQM/DTMonitorClient/src/DTOccupancyClusterBuilder.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2010/07/20 02:58:23 $
00006  *  $Revision: 1.4 $
00007  *  \author G. Cerminara - INFN Torino
00008  */
00009 
00010 #include "DTOccupancyClusterBuilder.h"
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 #include "TCanvas.h"
00014 #include "TH2F.h"
00015 
00016 #include <algorithm>
00017 #include <sstream>
00018 #include <iostream>
00019 
00020 using namespace std;
00021 using namespace edm;
00022 
00023 DTOccupancyClusterBuilder::  DTOccupancyClusterBuilder() : maxMean(-1.),
00024                                                            maxRMS(-1.) {
00025 }
00026 
00027 DTOccupancyClusterBuilder::~DTOccupancyClusterBuilder(){}
00028 
00029 
00030 
00031 void DTOccupancyClusterBuilder::addPoint(const DTOccupancyPoint& point) {
00032   // loop over points already stored
00033   for(set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end(); ++pt) {
00034     theDistances[(*pt).distance(point)] = make_pair(*pt, point);
00035   }
00036   //   cout << "[DTOccupancyClusterBuilder] Add point with mean: " << point.mean()
00037   //        << " RMS: " << point.rms() << endl;
00038   thePoints.insert(point);
00039 }
00040 
00041 
00042 void DTOccupancyClusterBuilder::buildClusters() {
00043   //   cout << "[DTOccupancyClusterBuilder] buildClusters" << endl;
00044   while(buildNewCluster()) {
00045     //     cout << "New cluster builded" << endl;
00046     //     cout << "# of remaining points: " << thePoints.size() << endl;
00047     if(thePoints.size() <= 1) break;
00048   }
00049     
00050   // build single point clusters with the remaining points
00051   for(set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end();
00052       ++pt) {
00053     DTOccupancyCluster clusterCandidate(*pt);
00054     theClusters.push_back(clusterCandidate);
00055     // store the range for building the histograms later
00056     if(clusterCandidate.maxMean() > maxMean) maxMean = clusterCandidate.maxMean();
00057     if(clusterCandidate.maxRMS() > maxRMS) maxRMS = clusterCandidate.maxRMS();
00058   }
00059   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00060     << " # of valid clusters: " << theClusters.size() << endl;
00061   sortClusters();
00062   
00063 }
00064 
00065 
00066 void DTOccupancyClusterBuilder::drawClusters(std::string canvasName) {
00067   int nBinsX = 100;
00068   int nBinsY = 100;
00069   int colorMap[12] = {632, 600, 800, 400, 820, 416, 432, 880, 616, 860, 900, 920};
00070 
00071   //   cout << "Draw clusters: " << endl;
00072   //   cout << "    max mean: " << maxMean << " max rms: " << maxRMS << endl;
00073 
00074   TCanvas *canvas = new TCanvas(canvasName.c_str(),canvasName.c_str()); 
00075   canvas->cd();
00076   for(vector<DTOccupancyCluster>::const_iterator cluster = theClusters.begin();
00077       cluster != theClusters.end(); ++cluster) {
00078     stringstream stream;
00079     stream << canvasName << "_" << cluster-theClusters.begin();
00080     string histoName = stream.str();
00081     TH2F *histo = (*cluster).getHisto(histoName, nBinsX, 0, maxMean+3*maxMean/100.,
00082                                       nBinsY, 0, maxRMS+3*maxRMS/100., colorMap[cluster-theClusters.begin()]);
00083     if(cluster == theClusters.begin()) 
00084       histo->Draw("box");
00085     else
00086       histo->Draw("box,same");
00087   }
00088 }
00089 
00090 
00091 std::pair<DTOccupancyPoint, DTOccupancyPoint> DTOccupancyClusterBuilder::getInitialPair() {
00092   return theDistances.begin()->second;
00093 }
00094 
00095 void DTOccupancyClusterBuilder::computePointToPointDistances() {
00096   theDistances.clear();
00097   for(set<DTOccupancyPoint>::const_iterator pt_i = thePoints.begin(); pt_i != thePoints.end();
00098       ++pt_i) { // i loopo
00099     for(set<DTOccupancyPoint>::const_iterator pt_j = thePoints.begin(); pt_j != thePoints.end();
00100         ++pt_j) { // j loop
00101       if(*pt_i != *pt_j) {
00102         theDistances[pt_i->distance(*pt_j)] = make_pair(*pt_i, *pt_j);
00103       }
00104     }
00105   }
00106 }
00107 
00108 
00109 
00110 void DTOccupancyClusterBuilder::computeDistancesToCluster(const DTOccupancyCluster& cluster) {
00111   theDistancesFromTheCluster.clear();
00112   for(set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end(); ++pt) {
00113     theDistancesFromTheCluster[cluster.distance(*pt)] = *pt;
00114   }
00115 }
00116 
00117 
00118 bool DTOccupancyClusterBuilder::buildNewCluster() {
00119   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00120     << "--------- New Cluster Candidate ----------------------" << endl;
00121   pair<DTOccupancyPoint, DTOccupancyPoint> initialPair = getInitialPair();
00122   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00123     << "   Initial Pair: " << endl
00124     << "           point1: mean " << initialPair.first.mean()
00125     << " rms " << initialPair.first.rms() << endl
00126     << "           point2: mean " << initialPair.second.mean()
00127     << " rms " << initialPair.second.rms() << endl;
00128   DTOccupancyCluster clusterCandidate(initialPair.first, initialPair.second);
00129   if(clusterCandidate.isValid()) {
00130     //     cout <<   " cluster candidate is valid" << endl;
00131     // remove already used pair
00132     thePoints.erase(initialPair.first);
00133     thePoints.erase(initialPair.second);
00134     if(thePoints.size() != 0) {
00135       computeDistancesToCluster(clusterCandidate);
00136       while(clusterCandidate.addPoint(theDistancesFromTheCluster.begin()->second)) {
00137         thePoints.erase(theDistancesFromTheCluster.begin()->second);
00138         if(thePoints.size() ==0) break;
00139         computeDistancesToCluster(clusterCandidate);
00140       }
00141     }
00142   } else {
00143     return false;
00144   }
00145   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00146     << "   # of layers: " << clusterCandidate.nPoints()
00147     << " avrg. mean: " << clusterCandidate.averageMean() << " avrg. rms: " << clusterCandidate.averageRMS() << endl;
00148   theClusters.push_back(clusterCandidate);
00149   // store the range for building the histograms later
00150   if(clusterCandidate.maxMean() > maxMean) maxMean = clusterCandidate.maxMean();
00151   if(clusterCandidate.maxRMS() > maxRMS) maxRMS = clusterCandidate.maxRMS();
00152   computePointToPointDistances();
00153   return true;
00154 }
00155   
00156 
00157 
00158 void DTOccupancyClusterBuilder::sortClusters() {
00159   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder") << " sorting" << endl;
00160   sort(theClusters.begin(), theClusters.end(), clusterIsLessThan);
00161   // we save the detid of the clusters which are not the best one
00162   for(vector<DTOccupancyCluster>::const_iterator cluster = ++(theClusters.begin());
00163       cluster != theClusters.end(); ++cluster) { // loop over clusters skipping the first
00164     set<DTLayerId> clusterLayers = (*cluster).getLayerIDs();
00165     LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00166       << "     # layers in the cluster: " << clusterLayers.size() << endl;
00167     theProblematicLayers.insert(clusterLayers.begin(), clusterLayers.end());
00168   }
00169   LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
00170     << " # of problematic layers: " << theProblematicLayers.size() << endl;
00171 }
00172 
00173 
00174 DTOccupancyCluster DTOccupancyClusterBuilder::getBestCluster() const {
00175   return theClusters.front();
00176 }
00177 
00178 bool DTOccupancyClusterBuilder::isProblematic(DTLayerId layerId) const {
00179   if(theProblematicLayers.find(layerId) != theProblematicLayers.end()) {
00180     return true;
00181   }
00182   return false;
00183 }