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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*
 *  See header file for a description of this class.
 *
 *  \author G. Mila - INFN Torino
 *
 *  threadsafe version (//-) oct/nov 2014 - WATWanAbdullah ncpp-um-my
 *
 */

#include "DQM/DTMonitorClient/src/DTSegmentAnalysisTest.h"

// Framework
#include "FWCore/Framework/interface/Event.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

// Geometry
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/DTGeometry/interface/DTLayer.h"
#include "Geometry/DTGeometry/interface/DTTopology.h"

#include "DQMServices/Core/interface/DQMStore.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <cmath>

using namespace edm;
using namespace std;

DTSegmentAnalysisTest::DTSegmentAnalysisTest(const ParameterSet& ps)
    : muonGeomToken_(esConsumes<edm::Transition::BeginRun>()) {
  LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest") << "[DTSegmentAnalysisTest]: Constructor";
  parameters = ps;

  // get the cfi parameters
  detailedAnalysis = parameters.getUntrackedParameter<bool>("detailedAnalysis", false);
  runOnline = parameters.getUntrackedParameter<bool>("runOnline", true);
  // top folder for the histograms in DQMStore
  topHistoFolder = ps.getUntrackedParameter<string>("topHistoFolder", "DT/02-Segments");
  nMinEvts = ps.getUntrackedParameter<int>("nEventsCert", 5000);
  maxPhiHit = ps.getUntrackedParameter<int>("maxPhiHit", 7);
  maxPhiZHit = ps.getUntrackedParameter<int>("maxPhiZHit", 11);

  nLSs = 0;

  bookingdone = false;
}

DTSegmentAnalysisTest::~DTSegmentAnalysisTest() {
  LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest") << "DTSegmentAnalysisTest: analyzed " << nLSs << " LS";
}

void DTSegmentAnalysisTest::beginRun(const Run& run, const EventSetup& context) {
  LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest") << "[DTSegmentAnalysisTest]: BeginRun";
  muonGeom = &context.getData(muonGeomToken_);
}

void DTSegmentAnalysisTest::dqmBeginLuminosityBlock(LuminosityBlock const& lumiSeg, EventSetup const&) {
  nLSs++;
  LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
      << "DTSegmentAnalysisTest: analyzing LS" << lumiSeg.id().luminosityBlock() << " of " << nLSs << endl;
}

void DTSegmentAnalysisTest::dqmEndLuminosityBlock(DQMStore::IBooker& ibooker,
                                                  DQMStore::IGetter& igetter,
                                                  edm::LuminosityBlock const& lumiSeg,
                                                  edm::EventSetup const& context) {
  // book the histos

  if (!bookingdone)
    bookHistos(ibooker);
  bookingdone = true;

  // counts number of lumiSegs
  nLumiSegs = lumiSeg.id().luminosityBlock();

  if (runOnline) {
    LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
        << "[DTSegmentAnalysisTest]: End of LS " << nLumiSegs
        << ". Client called in online mode , perform DQM client operation";

    performClientDiagnostic(igetter);
  }
}

void DTSegmentAnalysisTest::dqmEndJob(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
  if (!runOnline) {
    LogTrace("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
        << "[DTSegmentAnalysisTest]: endJob. Client called in offline mode , perform DQM client operation";

    performClientDiagnostic(igetter);
  }
}

void DTSegmentAnalysisTest::performClientDiagnostic(DQMStore::IGetter& igetter) {
  summaryHistos[3]->Reset();
  summaryHistos[4]->Reset();
  vector<const DTChamber*>::const_iterator ch_it = muonGeom->chambers().begin();
  vector<const DTChamber*>::const_iterator ch_end = muonGeom->chambers().end();

  for (; ch_it != ch_end; ++ch_it) {
    DTChamberId chID = (*ch_it)->id();

    MonitorElement* hNHits = igetter.get(getMEName(chID, "h4DSegmNHits"));
    MonitorElement* hSegmOcc = igetter.get(getMEName(chID, "numberOfSegments"));

    if (hNHits && hSegmOcc) {
      TH1F* hNHits_root = hNHits->getTH1F();
      TH2F* hSegmOcc_root = hSegmOcc->getTH2F();
      TH2F* summary_histo_root = summaryHistos[3]->getTH2F();

      int sector = chID.sector();
      if (sector == 13)
        sector = 4;
      if (sector == 14)
        sector = 10;

      if ((chID.station() != 4 && hNHits_root->GetMaximumBin() < maxPhiZHit) ||
          (chID.station() == 4 && hNHits_root->GetMaximumBin() < maxPhiHit)) {
        summaryHistos[chID.wheel()]->setBinContent(sector, chID.station(), 1);
        if (summary_histo_root->GetBinContent(sector, chID.wheel() + 3) < 1)
          summaryHistos[3]->setBinContent(sector, chID.wheel() + 3, 1);
      } else
        summaryHistos[chID.wheel()]->setBinContent(sector, chID.station(), 0);

      if (detailedAnalysis) {
        if (chID.station() != 4)
          segmRecHitHistos[make_pair(chID.wheel(), chID.sector())]->Fill(chID.station(),
                                                                         abs(12 - hNHits_root->GetMaximumBin()));
        else
          segmRecHitHistos[make_pair(chID.wheel(), chID.sector())]->Fill(chID.station(),
                                                                         abs(8 - hNHits_root->GetMaximumBin()));
      }

      TH2F* summary2_histo_root = summaryHistos[3]->getTH2F();
      float weight = 0.001;
      if (hSegmOcc_root->GetBinContent(sector, chID.station()) == 0) {
        summaryHistos[chID.wheel()]->setBinContent(sector, chID.station(), 2);
        if (summary2_histo_root->GetBinContent(sector, chID.wheel() + 3) < 2)
          summaryHistos[3]->setBinContent(sector, chID.wheel() + 3, 2);
      } else {
        // Fill the percentage of segment occupancy
        weight = 1. / 4.;
        if ((sector == 4 || sector == 10) && chID.station() == 4)
          weight = 1. / 8.;
      }
      summaryHistos[4]->Fill(sector, chID.wheel(), weight);
    } else {
      LogVerbatim("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
          << "[DTSegmentAnalysisTest]: histos not found!!";  // FIXME
    }

    if (detailedAnalysis) {  // switch on detailed analysis

      //test on chi2 segment quality

      MonitorElement* chi2_histo = igetter.get(getMEName(chID, "h4DChi2"));
      if (chi2_histo) {
        TH1F* chi2_histo_root = chi2_histo->getTH1F();
        double threshold = parameters.getUntrackedParameter<double>("chi2Threshold", 5);
        double maximum = chi2_histo_root->GetXaxis()->GetXmax();
        double minimum = chi2_histo_root->GetXaxis()->GetXmin();
        int nbins = chi2_histo_root->GetXaxis()->GetNbins();
        int thresholdBin = int(threshold / ((maximum - minimum) / nbins));

        double badSegments = 0;
        for (int bin = thresholdBin; bin <= nbins; bin++) {
          badSegments += chi2_histo_root->GetBinContent(bin);
        }

        if (chi2_histo_root->GetEntries() != 0) {
          double badSegmentsPercentual = badSegments / double(chi2_histo_root->GetEntries());
          chi2Histos[make_pair(chID.wheel(), chID.sector())]->Fill(chID.station(), badSegmentsPercentual);
        }
      } else {
        LogVerbatim("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
            << "[DTSegmentAnalysisTest]: Histo: " << getMEName(chID, "h4DChi2") << " not found!" << endl;
      }
    }  // end of switch for detailed analysis

  }  //loop over all the chambers

  string nEvtsName = "DT/EventInfo/Counters/nProcessedEventsSegment";

  MonitorElement* meProcEvts = igetter.get(nEvtsName);

  if (meProcEvts) {
    int nProcEvts = meProcEvts->getFloatValue();
    summaryHistos[4]->setEntries(nProcEvts < nMinEvts ? 10. : nProcEvts);
  } else {
    summaryHistos[4]->setEntries(nMinEvts + 1);
    LogVerbatim("DTDQM|DTMonitorClient|DTOccupancyTest")
        << "[DTOccupancyTest] ME: " << nEvtsName << " not found!" << endl;
  }

  if (detailedAnalysis) {
    string chi2CriterionName = parameters.getUntrackedParameter<string>("chi2TestName", "chi2InRange");
    for (map<pair<int, int>, MonitorElement*>::const_iterator histo = chi2Histos.begin(); histo != chi2Histos.end();
         histo++) {
      const QReport* theChi2QReport = (*histo).second->getQReport(chi2CriterionName);
      if (theChi2QReport) {
        vector<dqm::me_util::Channel> badChannels = theChi2QReport->getBadChannels();
        for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); channel != badChannels.end();
             channel++) {
          LogError("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
              << "Wheel: " << (*histo).first.first << " Sector: " << (*histo).first.second
              << " Bad stations: " << (*channel).getBin() << "  Contents : " << (*channel).getContents();
        }
      }
    }

    string segmRecHitCriterionName =
        parameters.getUntrackedParameter<string>("segmRecHitTestName", "segmRecHitInRange");
    for (map<pair<int, int>, MonitorElement*>::const_iterator histo = segmRecHitHistos.begin();
         histo != segmRecHitHistos.end();
         histo++) {
      const QReport* theSegmRecHitQReport = (*histo).second->getQReport(segmRecHitCriterionName);
      if (theSegmRecHitQReport) {
        vector<dqm::me_util::Channel> badChannels = theSegmRecHitQReport->getBadChannels();
        for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); channel != badChannels.end();
             channel++) {
          LogError("DTDQM|DTMonitorClient|DTSegmentAnalysisTest")
              << "Wheel: " << (*histo).first.first << " Sector: " << (*histo).first.second
              << " Bad stations on recHit number: " << (*channel).getBin()
              << "  Contents : " << (*channel).getContents();
        }
      }
    }

  }  // end of detailedAnalysis
}

string DTSegmentAnalysisTest::getMEName(const DTChamberId& chID, string histoTag) {
  stringstream wheel;
  wheel << chID.wheel();
  stringstream station;
  station << chID.station();
  stringstream sector;
  sector << chID.sector();

  string folderName =
      topHistoFolder + "/Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str() + "/";

  string histoname = folderName + histoTag + "_W" + wheel.str() + "_St" + station.str() + "_Sec" + sector.str();

  if (histoTag == "numberOfSegments")
    histoname = topHistoFolder + "/Wheel" + wheel.str() + "/" + histoTag + +"_W" + wheel.str();

  return histoname;
}

void DTSegmentAnalysisTest::bookHistos(DQMStore::IBooker& ibooker) {
  for (int wh = -2; wh <= 2; wh++) {
    stringstream wheel;
    wheel << wh;
    string histoName = "segmentSummary_W" + wheel.str();

    ibooker.setCurrentFolder(topHistoFolder);

    summaryHistos[wh] = ibooker.book2D(histoName.c_str(), histoName.c_str(), 12, 1, 13, 4, 1, 5);
    summaryHistos[wh]->setAxisTitle("Sector", 1);
    summaryHistos[wh]->setBinLabel(1, "MB1", 2);
    summaryHistos[wh]->setBinLabel(2, "MB2", 2);
    summaryHistos[wh]->setBinLabel(3, "MB3", 2);
    summaryHistos[wh]->setBinLabel(4, "MB4", 2);

    if (detailedAnalysis) {
      for (int sect = 1; sect <= 14; sect++) {
        stringstream sector;
        sector << sect;
        string chi2HistoName = "chi2BadSegmPercentual_W" + wheel.str() + "_Sec" + sector.str();
        ibooker.setCurrentFolder(topHistoFolder + "/Wheel" + wheel.str() + "/Tests");
        chi2Histos[make_pair(wh, sect)] = ibooker.book1D(chi2HistoName.c_str(), chi2HistoName.c_str(), 4, 1, 5);
        chi2Histos[make_pair(wh, sect)]->setBinLabel(1, "MB1");
        chi2Histos[make_pair(wh, sect)]->setBinLabel(2, "MB2");
        chi2Histos[make_pair(wh, sect)]->setBinLabel(3, "MB3");
        chi2Histos[make_pair(wh, sect)]->setBinLabel(4, "MB4");

        string segmHistoName = "residualsOnSegmRecHitNumber_W" + wheel.str() + "_Sec" + sector.str();
        segmRecHitHistos[make_pair(wh, sect)] = ibooker.book1D(segmHistoName.c_str(), segmHistoName.c_str(), 4, 1, 5);
        segmRecHitHistos[make_pair(wh, sect)]->setBinLabel(1, "MB1");
        segmRecHitHistos[make_pair(wh, sect)]->setBinLabel(2, "MB2");
        segmRecHitHistos[make_pair(wh, sect)]->setBinLabel(3, "MB3");
        segmRecHitHistos[make_pair(wh, sect)]->setBinLabel(4, "MB4");
      }
    }
  }

  string histoName = "segmentSummary";

  ibooker.setCurrentFolder(topHistoFolder);

  summaryHistos[3] = ibooker.book2D(histoName.c_str(), histoName.c_str(), 12, 1, 13, 5, -2, 3);
  summaryHistos[3]->setAxisTitle("Sector", 1);
  summaryHistos[3]->setAxisTitle("Wheel", 2);

  summaryHistos[4] = ibooker.book2D("SegmentGlbSummary", histoName.c_str(), 12, 1, 13, 5, -2, 3);
  summaryHistos[4]->setAxisTitle("Sector", 1);
  summaryHistos[4]->setAxisTitle("Wheel", 2);
}