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 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/*
 *  See header file for a description of this class.
 *
 *  \author D. Fasanella - INFN Bologna
 *
 *  threadsafe version (//-) oct/nov 2014 - WATWanAbdullah -ncpp-um-my
 *
 */

// This class header
#include "DQM/DTMonitorClient/src/DTTriggerLutTest.h"

// Framework headers
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DQMServices/Core/interface/DQMStore.h"
// Geometry
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "Geometry/DTGeometry/interface/DTGeometry.h"

// Root
#include "TF1.h"
//#include "TSpectrum.h"

//C++ headers
#include <iostream>
#include <sstream>

using namespace edm;
using namespace std;

DTTriggerLutTest::DTTriggerLutTest(const edm::ParameterSet& ps) {
  setConfig(ps, "DTTriggerLut");
  baseFolderTM = "DT/03-LocalTrigger-TM/";
  thresholdWarnPhi = ps.getUntrackedParameter<double>("thresholdWarnPhi");
  thresholdErrPhi = ps.getUntrackedParameter<double>("thresholdErrPhi");
  thresholdWarnPhiB = ps.getUntrackedParameter<double>("thresholdWarnPhiB");
  thresholdErrPhiB = ps.getUntrackedParameter<double>("thresholdErrPhiB");
  validRange = ps.getUntrackedParameter<double>("validRange");
  detailedAnalysis = ps.getUntrackedParameter<bool>("detailedAnalysis");

  bookingdone = false;
}

DTTriggerLutTest::~DTTriggerLutTest() {}

void DTTriggerLutTest::Bookings(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
  bookingdone = true;

  vector<string>::const_iterator iTr = trigSources.begin();
  vector<string>::const_iterator trEnd = trigSources.end();
  vector<string>::const_iterator iHw = hwSources.begin();
  vector<string>::const_iterator hwEnd = hwSources.end();

  //Booking
  if (parameters.getUntrackedParameter<bool>("staticBooking")) {
    for (; iTr != trEnd; ++iTr) {
      trigSource = (*iTr);
      for (; iHw != hwEnd; ++iHw) {
        hwSource = (*iHw);
        // Loop over the TriggerUnits
        for (int wh = -2; wh <= 2; ++wh) {
          if (detailedAnalysis) {
            bookWheelHistos(ibooker, wh, "PhiResidualPercentage");
            bookWheelHistos(ibooker, wh, "PhibResidualPercentage");
          }

          bookWheelHistos(ibooker, wh, "PhiLutSummary", "Summaries");
          bookWheelHistos(ibooker, wh, "PhibLutSummary", "Summaries");

          if (detailedAnalysis) {
            bookWheelHistos(ibooker, wh, "PhiResidualMean");
            bookWheelHistos(ibooker, wh, "PhiResidualRMS");
            bookWheelHistos(ibooker, wh, "PhibResidualMean");
            bookWheelHistos(ibooker, wh, "PhibResidualRMS");
            bookWheelHistos(ibooker, wh, "CorrelationFactorPhi");
            bookWheelHistos(ibooker, wh, "CorrelationFactorPhib");
            bookWheelHistos(ibooker, wh, "DoublePeakFlagPhib");
          }
        }

        bookCmsHistos(ibooker, "TrigLutSummary", "", true);
        bookCmsHistos(ibooker, "PhiLutSummary");
        bookCmsHistos(ibooker, "PhibLutSummary");
        if (detailedAnalysis) {
          bookCmsHistos1d(ibooker, "PhiPercentageSummary");
          bookCmsHistos1d(ibooker, "PhibPercentageSummary");
        }
      }
    }
  }
}

void DTTriggerLutTest::beginRun(const edm::Run& r, const edm::EventSetup& c) { DTLocalTriggerBaseTest::beginRun(r, c); }

void DTTriggerLutTest::runClientDiagnostic(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
  if (!bookingdone)
    Bookings(ibooker, igetter);

  // Reset lut percentage 1D summaries
  if (detailedAnalysis) {
    cmsME.find(fullName("PhiPercentageSummary"))->second->Reset();
    cmsME.find(fullName("PhibPercentageSummary"))->second->Reset();
  }

  // Loop over Trig & Hw sources
  for (vector<string>::const_iterator iTr = trigSources.begin(); iTr != trigSources.end(); ++iTr) {
    trigSource = (*iTr);
    for (vector<string>::const_iterator iHw = hwSources.begin(); iHw != hwSources.end(); ++iHw) {
      hwSource = (*iHw);
      vector<const DTChamber*>::const_iterator chIt = muonGeom->chambers().begin();
      vector<const DTChamber*>::const_iterator chEnd = muonGeom->chambers().end();
      for (; chIt != chEnd; ++chIt) {
        DTChamberId chId((*chIt)->id());
        int wh = chId.wheel();
        int sect = chId.sector();
        int stat = chId.station();

        std::map<std::string, MonitorElement*>& innerME = whME[wh];

        // Make Phi Residual Summary
        TH1F* PhiResidual = getHisto<TH1F>(igetter.get(getMEName("PhiResidual", "Segment", chId)));
        int phiSummary = 1;
        if (PhiResidual && PhiResidual->GetEntries() > 10) {
          if (innerME.find(fullName("PhiResidualPercentage")) == innerME.end()) {
            bookWheelHistos(ibooker, wh, "PhiResidualPercentage");
          }

          float rangeBin = validRange / (PhiResidual->GetBinWidth(1));
          float center = (PhiResidual->GetNbinsX()) / 2.;
          float perc =
              (PhiResidual->Integral(floor(center - rangeBin), ceil(center + rangeBin))) / (PhiResidual->Integral());
          fillWhPlot(innerME.find(fullName("PhiResidualPercentage"))->second, sect, stat, perc, false);
          phiSummary = performLutTest(perc, thresholdWarnPhi, thresholdErrPhi);
          if (detailedAnalysis)
            cmsME.find(fullName("PhiPercentageSummary"))->second->Fill(perc);
        }

        fillWhPlot(innerME.find(fullName("PhiLutSummary"))->second, sect, stat, phiSummary);

        if (detailedAnalysis) {
          if ((phiSummary == 0) || (phiSummary == 3)) {  //Information on the Peak

            if (innerME.find(fullName("PhiResidualMean")) == innerME.end()) {
              bookWheelHistos(ibooker, wh, "PhiResidualMean");
              bookWheelHistos(ibooker, wh, "PhiResidualRMS");
            }

            float center = (PhiResidual->GetNbinsX()) / 2.;
            float rangeBin = validRange / (PhiResidual->GetBinWidth(1));
            PhiResidual->GetXaxis()->SetRange(floor(center - rangeBin), ceil(center + rangeBin));
            float max = PhiResidual->GetMaximumBin();
            float maxBin = PhiResidual->GetXaxis()->FindBin(max);
            float nBinMax = 0.5 / (PhiResidual->GetBinWidth(1));
            PhiResidual->GetXaxis()->SetRange(floor(maxBin - nBinMax), ceil(maxBin + nBinMax));
            float Mean = PhiResidual->GetMean();
            float rms = PhiResidual->GetRMS();

            fillWhPlot(innerME.find(fullName("PhiResidualMean"))->second, sect, stat, Mean);
            fillWhPlot(innerME.find(fullName("PhiResidualRMS"))->second, sect, stat, rms);
          }

          TH2F* TrackPhitkvsPhitrig = getHisto<TH2F>(igetter.get(getMEName("PhitkvsPhitrig", "Segment", chId)));

          if (TrackPhitkvsPhitrig && TrackPhitkvsPhitrig->GetEntries() > 100) {
            float corr = TrackPhitkvsPhitrig->GetCorrelationFactor();
            if (innerME.find(fullName("CorrelationFactorPhi")) == innerME.end()) {
              bookWheelHistos(ibooker, wh, "CorrelationFactorPhi");
            }
            fillWhPlot(innerME.find(fullName("CorrelationFactorPhi"))->second, sect, stat, corr, false);
          }
        }

        // Make Phib Residual Summary
        TH1F* PhibResidual = getHisto<TH1F>(igetter.get(getMEName("PhibResidual", "Segment", chId)));
        int phibSummary = stat == 3 ? -1 : 1;  // station 3 has no meaningful MB3 phi bending information

        if (stat != 3 && PhibResidual &&
            PhibResidual->GetEntries() > 10) {  // station 3 has no meaningful MB3 phi bending information

          if (innerME.find(fullName("PhibResidualPercentage")) == innerME.end()) {
            bookWheelHistos(ibooker, wh, "PhibResidualPercentage");
          }

          float rangeBin = validRange / (PhibResidual->GetBinWidth(1));
          float center = (PhibResidual->GetNbinsX()) / 2.;
          float perc =
              (PhibResidual->Integral(floor(center - rangeBin), ceil(center + rangeBin))) / (PhibResidual->Integral());

          fillWhPlot(innerME.find(fullName("PhibResidualPercentage"))->second, sect, stat, perc, false);
          phibSummary = performLutTest(perc, thresholdWarnPhiB, thresholdErrPhiB);
          if (detailedAnalysis)
            cmsME.find(fullName("PhibPercentageSummary"))->second->Fill(perc);
        }

        fillWhPlot(innerME.find(fullName("PhibLutSummary"))->second, sect, stat, phibSummary);

        if (detailedAnalysis) {
          if ((phibSummary == 0) || (phibSummary == 3)) {
            if (innerME.find(fullName("PhibResidualMean")) == innerME.end()) {
              bookWheelHistos(ibooker, wh, "PhibResidualMean");
              bookWheelHistos(ibooker, wh, "PhibResidualRMS");
            }

            float center = (PhibResidual->GetNbinsX()) / 2.;
            float rangeBin = validRange / (PhibResidual->GetBinWidth(1));
            PhibResidual->GetXaxis()->SetRange(floor(center - rangeBin), ceil(center + rangeBin));
            float max = PhibResidual->GetMaximumBin();
            float maxBin = PhibResidual->GetXaxis()->FindBin(max);
            float nBinMax = 0.5 / (PhibResidual->GetBinWidth(1));
            PhibResidual->GetXaxis()->SetRange(floor(maxBin - nBinMax), ceil(maxBin + nBinMax));
            float Mean = PhibResidual->GetMean();
            float rms = PhibResidual->GetRMS();

            fillWhPlot(innerME.find(fullName("PhibResidualMean"))->second, sect, stat, Mean);
            fillWhPlot(innerME.find(fullName("PhibResidualRMS"))->second, sect, stat, rms);
          }

          TH2F* TrackPhibtkvsPhibtrig = getHisto<TH2F>(igetter.get(getMEName("PhibtkvsPhibtrig", "Segment", chId)));
          if (TrackPhibtkvsPhibtrig && TrackPhibtkvsPhibtrig->GetEntries() > 100) {
            float corr = TrackPhibtkvsPhibtrig->GetCorrelationFactor();
            if (innerME.find(fullName("CorrelationFactorPhib")) == innerME.end()) {
              bookWheelHistos(ibooker, wh, "CorrelationFactorPhib");
            }

            fillWhPlot(innerME.find(fullName("CorrelationFactorPhib"))->second, sect, stat, corr, false);
          }
        }
      }
    }
  }

  // Barrel Summary Plots
  for (vector<string>::const_iterator iTr = trigSources.begin(); iTr != trigSources.end(); ++iTr) {
    trigSource = (*iTr);
    for (vector<string>::const_iterator iHw = hwSources.begin(); iHw != hwSources.end(); ++iHw) {
      hwSource = (*iHw);
      for (int wh = -2; wh <= 2; ++wh) {
        std::map<std::string, MonitorElement*>* innerME = &(whME[wh]);

        TH2F* phiWhSummary = getHisto<TH2F>(innerME->find(fullName("PhiLutSummary"))->second);
        TH2F* phibWhSummary = getHisto<TH2F>(innerME->find(fullName("PhibLutSummary"))->second);

        for (int sect = 1; sect <= 12; ++sect) {
          int phiSectorTotal = 0;  // CB dai 1 occhio a questo
          int phibSectorTotal = 0;
          int nullphi = 0;
          int nullphib = 0;
          int phiStatus = 5;
          int phibStatus = 5;
          int glbStatus = 0;

          for (int stat = 1; stat <= 4; ++stat) {
            if (phiWhSummary->GetBinContent(sect, stat) == 2) {
              phiSectorTotal += 1;
              glbStatus += 1;
            }
            if (phiWhSummary->GetBinContent(sect, stat) == 1)
              nullphi += 1;
            if (phibWhSummary->GetBinContent(sect, stat) == 2) {
              phibSectorTotal += 1;
              glbStatus += 1;
            }
            if (phibWhSummary->GetBinContent(sect, stat) == 1)
              nullphib += 1;
          }
          if (nullphi != 4)
            phiStatus = phiSectorTotal;
          else
            phiStatus = 5;
          if (nullphib != 3)
            phibStatus = phibSectorTotal;
          else
            phibStatus = 5;

          cmsME.find("TrigLutSummary")->second->setBinContent(sect, wh + wheelArrayShift, glbStatus);
          cmsME.find(fullName("PhiLutSummary"))->second->setBinContent(sect, wh + wheelArrayShift, phiStatus);
          cmsME.find(fullName("PhibLutSummary"))->second->setBinContent(sect, wh + wheelArrayShift, phibStatus);
        }
      }
    }
  }
}

int DTTriggerLutTest::performLutTest(double perc, double thresholdWarn, double thresholdErr) {
  bool isInWarn = perc < thresholdWarn;
  bool isInErr = perc < thresholdErr;

  int result = isInErr ? 2 : isInWarn ? 3 : 0;

  return result;
}

void DTTriggerLutTest::bookCmsHistos1d(DQMStore::IBooker& ibooker, string hTag, string folder) {
  string basedir = topFolder();
  if (!folder.empty()) {
    basedir += folder + "/";
  }
  ibooker.setCurrentFolder(basedir);

  string hName = fullName(hTag);
  LogTrace(category()) << "[" << testName << "Test]: booking " << basedir << hName;

  MonitorElement* me = ibooker.book1D(hName.c_str(), hName.c_str(), 101, -0.005, 1.005);
  me->setAxisTitle("Percentage", 1);
  cmsME[hName] = me;
}

void DTTriggerLutTest::fillWhPlot(MonitorElement* plot, int sect, int stat, float value, bool lessIsBest) {
  if (sect > 12) {
    int scsect = sect == 13 ? 4 : 10;
    if ((value > plot->getBinContent(scsect, stat)) == lessIsBest) {
      plot->setBinContent(scsect, stat, value);
    }
  } else {
    plot->setBinContent(sect, stat, value);
  }

  return;
}