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