Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

/*
 *  See header file for a description of this class.
 *
 *  \author G. Cerminara - INFN Torino
 */

#include "DTOccupancyClusterBuilder.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "TCanvas.h"
#include "TH2F.h"

#include <algorithm>
#include <sstream>
#include <iostream>

using namespace std;
using namespace edm;

DTOccupancyClusterBuilder::DTOccupancyClusterBuilder() : maxMean(-1.), maxRMS(-1.) {}

DTOccupancyClusterBuilder::~DTOccupancyClusterBuilder() {}

void DTOccupancyClusterBuilder::addPoint(const DTOccupancyPoint& point) {
  // loop over points already stored
  for (set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end(); ++pt) {
    theDistances[(*pt).distance(point)] = make_pair(*pt, point);
  }
  thePoints.insert(point);
}

void DTOccupancyClusterBuilder::buildClusters() {
  while (buildNewCluster()) {
    if (thePoints.size() <= 1)
      break;
  }

  // build single point clusters with the remaining points
  for (set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end(); ++pt) {
    DTOccupancyCluster clusterCandidate(*pt);
    theClusters.push_back(clusterCandidate);
    // store the range for building the histograms later
    if (clusterCandidate.maxMean() > maxMean)
      maxMean = clusterCandidate.maxMean();
    if (clusterCandidate.maxRMS() > maxRMS)
      maxRMS = clusterCandidate.maxRMS();
  }
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
      << " # of valid clusters: " << theClusters.size() << endl;
  sortClusters();
}

void DTOccupancyClusterBuilder::drawClusters(std::string canvasName) {
  int nBinsX = 100;
  int nBinsY = 100;
  int colorMap[12] = {632, 600, 800, 400, 820, 416, 432, 880, 616, 860, 900, 920};

  TCanvas* canvas = new TCanvas(canvasName.c_str(), canvasName.c_str());
  canvas->cd();
  for (vector<DTOccupancyCluster>::const_iterator cluster = theClusters.begin(); cluster != theClusters.end();
       ++cluster) {
    stringstream stream;
    stream << canvasName << "_" << cluster - theClusters.begin();
    string histoName = stream.str();
    TH2F* histo = (*cluster).getHisto(histoName,
                                      nBinsX,
                                      0,
                                      maxMean + 3 * maxMean / 100.,
                                      nBinsY,
                                      0,
                                      maxRMS + 3 * maxRMS / 100.,
                                      colorMap[cluster - theClusters.begin()]);
    if (cluster == theClusters.begin())
      histo->Draw("box");
    else
      histo->Draw("box,same");
  }
}

std::pair<DTOccupancyPoint, DTOccupancyPoint> DTOccupancyClusterBuilder::getInitialPair() {
  return theDistances.begin()->second;
}

void DTOccupancyClusterBuilder::computePointToPointDistances() {
  theDistances.clear();
  for (set<DTOccupancyPoint>::const_iterator pt_i = thePoints.begin(); pt_i != thePoints.end(); ++pt_i) {    // i loopo
    for (set<DTOccupancyPoint>::const_iterator pt_j = thePoints.begin(); pt_j != thePoints.end(); ++pt_j) {  // j loop
      if (*pt_i != *pt_j) {
        theDistances[pt_i->distance(*pt_j)] = make_pair(*pt_i, *pt_j);
      }
    }
  }
}

void DTOccupancyClusterBuilder::computeDistancesToCluster(const DTOccupancyCluster& cluster) {
  theDistancesFromTheCluster.clear();
  for (set<DTOccupancyPoint>::const_iterator pt = thePoints.begin(); pt != thePoints.end(); ++pt) {
    theDistancesFromTheCluster[cluster.distance(*pt)] = *pt;
  }
}

bool DTOccupancyClusterBuilder::buildNewCluster() {
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
      << "--------- New Cluster Candidate ----------------------" << endl;
  pair<DTOccupancyPoint, DTOccupancyPoint> initialPair = getInitialPair();
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
      << "   Initial Pair: " << endl
      << "           point1: mean " << initialPair.first.mean() << " rms " << initialPair.first.rms() << endl
      << "           point2: mean " << initialPair.second.mean() << " rms " << initialPair.second.rms() << endl;
  DTOccupancyCluster clusterCandidate(initialPair.first, initialPair.second);
  if (clusterCandidate.isValid()) {
    // remove already used pair
    thePoints.erase(initialPair.first);
    thePoints.erase(initialPair.second);
    if (!thePoints.empty()) {
      computeDistancesToCluster(clusterCandidate);
      while (clusterCandidate.addPoint(theDistancesFromTheCluster.begin()->second)) {
        thePoints.erase(theDistancesFromTheCluster.begin()->second);
        if (thePoints.empty())
          break;
        computeDistancesToCluster(clusterCandidate);
      }
    }
  } else {
    return false;
  }
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
      << "   # of layers: " << clusterCandidate.nPoints() << " avrg. mean: " << clusterCandidate.averageMean()
      << " avrg. rms: " << clusterCandidate.averageRMS() << endl;
  theClusters.push_back(clusterCandidate);
  // store the range for building the histograms later
  if (clusterCandidate.maxMean() > maxMean)
    maxMean = clusterCandidate.maxMean();
  if (clusterCandidate.maxRMS() > maxRMS)
    maxRMS = clusterCandidate.maxRMS();
  computePointToPointDistances();
  return true;
}

void DTOccupancyClusterBuilder::sortClusters() {
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder") << " sorting" << endl;
  sort(theClusters.begin(), theClusters.end(), clusterIsLessThan);
  // we save the detid of the clusters which are not the best one
  for (vector<DTOccupancyCluster>::const_iterator cluster = ++(theClusters.begin()); cluster != theClusters.end();
       ++cluster) {  // loop over clusters skipping the first
    set<DTLayerId> clusterLayers = (*cluster).getLayerIDs();
    LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
        << "     # layers in the cluster: " << clusterLayers.size() << endl;
    theProblematicLayers.insert(clusterLayers.begin(), clusterLayers.end());
  }
  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest|DTOccupancyClusterBuilder")
      << " # of problematic layers: " << theProblematicLayers.size() << endl;
}

DTOccupancyCluster DTOccupancyClusterBuilder::getBestCluster() const { return theClusters.front(); }

bool DTOccupancyClusterBuilder::isProblematic(DTLayerId layerId) const {
  if (theProblematicLayers.find(layerId) != theProblematicLayers.end()) {
    return true;
  }
  return false;
}