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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/*
 *  See header file for a description of this class.
 *
 *  \author G. Mila - INFN Torino
 */

#include "DTChamberEfficiencyTask.h"

//Framework
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ServiceRegistry/interface/Service.h"

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

#include <iterator>
#include <iostream>
#include <cmath>
using namespace edm;
using namespace std;

DTChamberEfficiencyTask::DTChamberEfficiencyTask(const ParameterSet& pset)
    : muonGeomToken_(esConsumes<edm::Transition::BeginRun>()) {
  debug = pset.getUntrackedParameter<bool>("debug", false);

  edm::LogVerbatim("DTDQM|DTMonitorModule|DTChamberEfficiencyTask") << "[DTChamberEfficiencyTask] Constructor called!";

  parameters = pset;

  // the name of the 4D rec hits collection
  recHits4DToken_ =
      consumes<DTRecSegment4DCollection>(edm::InputTag(parameters.getUntrackedParameter<string>("recHits4DLabel")));

  // parameters to use for the segment quality check
  theMinHitsSegment = static_cast<unsigned int>(parameters.getParameter<int>("minHitsSegment"));
  theMinChi2NormSegment = parameters.getParameter<double>("minChi2NormSegment");
  // parameter to use for the exstrapolated segment check
  theMinCloseDist = parameters.getParameter<double>("minCloseDist");

  // the running modality
  onlineMonitor = parameters.getUntrackedParameter<bool>("onlineMonitor");

  // the analysis mode
  detailedAnalysis = parameters.getUntrackedParameter<bool>("detailedAnalysis");
}

DTChamberEfficiencyTask::~DTChamberEfficiencyTask() {
  edm::LogVerbatim("DTDQM|DTMonitorModule|DTChamberEfficiencyTask") << "[DTChamberEfficiencyTask] Destructor called!";
}

void DTChamberEfficiencyTask::beginLuminosityBlock(LuminosityBlock const& lumiSeg, EventSetup const& context) {
  edm::LogVerbatim("DTDQM|DTMonitorModule|DTChamberEfficiencyTask")
      << "[DTChamberEfficiencyTask]: Begin of LS transition";

  if (lumiSeg.id().luminosityBlock() % parameters.getUntrackedParameter<int>("ResetCycle", 3) == 0 && onlineMonitor) {
    for (map<DTChamberId, vector<MonitorElement*> >::const_iterator histo = histosPerCh.begin();
         histo != histosPerCh.end();
         histo++) {
      int size = (*histo).second.size();
      for (int i = 0; i < size; i++) {
        (*histo).second[i]->Reset();
      }
    }
  }
}

void DTChamberEfficiencyTask::dqmBeginRun(const edm::Run& run, const edm::EventSetup& setup) {
  // Get the DT Geometry
  dtGeom = &setup.getData(muonGeomToken_);
}

void DTChamberEfficiencyTask::bookHistograms(DQMStore::IBooker& ibooker,
                                             edm::Run const& iRun,
                                             edm::EventSetup const& context) {
  ibooker.setCurrentFolder("DT/DTChamberEfficiencyTask");

  // Loop over all the chambers
  vector<const DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
  vector<const DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
  for (; ch_it != ch_end; ++ch_it) {
    // histo booking
    bookHistos(ibooker, (*ch_it)->id());
  }
}

// Book a set of histograms for a given Layer
void DTChamberEfficiencyTask::bookHistos(DQMStore::IBooker& ibooker, DTChamberId chId) {
  edm::LogVerbatim("DTDQM|DTMonitorModule|DTChamberEfficiencyTask") << "   Booking histos for CH : " << chId;

  // Compose the chamber name
  stringstream wheel;
  wheel << chId.wheel();
  stringstream station;
  station << chId.station();
  stringstream sector;
  sector << chId.sector();

  string HistoName = "_W" + wheel.str() + "_St" + station.str() + "_Sec" + sector.str();

  ibooker.setCurrentFolder("DT/01-DTChamberEfficiency/Task/Wheel" + wheel.str() + "/Sector" + sector.str() +
                           "/Station" + station.str());

  // Create the monitor elements
  vector<MonitorElement*> histos;

  //efficiency selection cuts
  // a- number of segments of the top chamber > 0 && number of segments of the bottom chamber > 0
  // b- number of segments of the middle chamber > 0
  // c- check of the top and bottom segment quality
  // d- check if interpolation falls inside the middle chamber
  // e- check of the middle segment quality
  // f- check if the distance between the reconstructed and the exstrapolated segments is ok

  // histo for efficiency with cuts a-/c-/d-
  histos.push_back(ibooker.book2D(
      "hEffGoodSegVsPosDen" + HistoName, "Eff vs local position (good) ", 25, -250., 250., 25, -250., 250.));
  // histo for efficiency with cuts a-/b-/c-/d-/e-/f-
  histos.push_back(ibooker.book2D("hEffGoodCloseSegVsPosNum" + HistoName,
                                  "Eff vs local position (good and close segs) ",
                                  25,
                                  -250.,
                                  250.,
                                  25,
                                  -250.,
                                  250.));
  if (detailedAnalysis) {
    histos.push_back(
        ibooker.book1D("hDistSegFromExtrap" + HistoName, "Distance segments from extrap position ", 200, 0., 200.));
    // histo for efficiency from segment counting
    histos.push_back(ibooker.book1D("hNaiveEffSeg" + HistoName, "Naive eff ", 10, 0., 10.));
    // histo for efficiency with cuts a-/c-
    histos.push_back(ibooker.book2D(
        "hEffSegVsPosDen" + HistoName, "Eff vs local position (all) ", 25, -250., 250., 25, -250., 250.));
    // histo for efficiency with cuts a-/b-/c-/d-
    histos.push_back(
        ibooker.book2D("hEffSegVsPosNum" + HistoName, "Eff vs local position ", 25, -250., 250., 25, -250., 250.));
    // histo for efficiency with cuts a-/b-/c-/d-/e-
    histos.push_back(ibooker.book2D(
        "hEffGoodSegVsPosNum" + HistoName, "Eff vs local position (good segs) ", 25, -250., 250., 25, -250., 250.));
  }
  histosPerCh[chId] = histos;
}

void DTChamberEfficiencyTask::analyze(const edm::Event& event, const edm::EventSetup& setup) {
  edm::LogVerbatim("DTDQM|DTMonitorModule|DTChamberEfficiencyTask")
      << "[DTChamberEfficiencyTask] Analyze #Run: " << event.id().run() << " #Event: " << event.id().event();

  // Get the 4D rechit collection from the event
  event.getByToken(recHits4DToken_, segs);

  int bottom = 0, top = 0;

  // Loop over all the chambers
  vector<const DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
  vector<const DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
  for (; ch_it != ch_end; ++ch_it) {
    DTChamberId ch = (*ch_it)->id();
    int wheel = ch.wheel();
    int sector = ch.sector();
    int station = ch.station();

    DTChamberId MidId(wheel, station, sector);

    // get efficiency for MB1 using MB2 and MB3
    if (station == 1) {
      bottom = 2;
      top = 3;
    }

    // get efficiency for MB2 using MB1 and MB3
    if (station == 2) {
      bottom = 1;
      top = 3;
    }

    // get efficiency for MB2 using MB2 and MB4
    if (station == 3) {
      bottom = 2;
      top = 4;
    }

    // get efficiency for MB4 using MB2 and MB3
    if (station == 4) {
      bottom = 2;
      top = 3;
    }

    // Select events with (good) segments in Bot and Top
    DTChamberId BotId(wheel, bottom, sector);
    DTChamberId TopId(wheel, top, sector);

    // Get segments in the bottom chambers (if any)
    DTRecSegment4DCollection::range segsBot = segs->get(BotId);
    int nSegsBot = segsBot.second - segsBot.first;
    // check if any segments is there
    if (nSegsBot == 0)
      continue;

    vector<MonitorElement*> histos = histosPerCh[MidId];

    // Get segments in the top chambers (if any)
    DTRecSegment4DCollection::range segsTop = segs->get(TopId);
    int nSegsTop = segsTop.second - segsTop.first;

    // Select one segment for the bottom chamber
    const DTRecSegment4D& bestBotSeg = getBestSegment(segsBot);

    // Select one segment for the top chamber
    DTRecSegment4D* pBestTopSeg = nullptr;
    if (nSegsTop > 0)
      pBestTopSeg = const_cast<DTRecSegment4D*>(&getBestSegment(segsTop));
    //if top chamber is MB4 sector 10, consider also sector 14
    if (TopId.station() == 4 && TopId.sector() == 10) {
      DTChamberId TopId14(wheel, top, 14);
      DTRecSegment4DCollection::range segsTop14 = segs->get(TopId14);
      int nSegsTop14 = segsTop14.second - segsTop14.first;
      if (nSegsTop14) {
        DTRecSegment4D* pBestTopSeg14 = const_cast<DTRecSegment4D*>(&getBestSegment(segsTop14));
        // get best between sector 10 and 14
        pBestTopSeg = const_cast<DTRecSegment4D*>(getBestSegment(pBestTopSeg, pBestTopSeg14));
      }
    }
    if (!pBestTopSeg)
      continue;
    const DTRecSegment4D& bestTopSeg = *pBestTopSeg;

    DTRecSegment4DCollection::range segsMid = segs->get(MidId);
    int nSegsMid = segsMid.second - segsMid.first;

    if (detailedAnalysis) {
      // very trivial efficiency, just count segments
      histos[3]->Fill(0);
      if (nSegsMid > 0)
        histos[3]->Fill(1);
    }

    // get position at Mid by interpolating the position (not direction) of best
    // segment in Bot and Top to Mid surface
    LocalPoint posAtMid = interpolate(bestBotSeg, bestTopSeg, MidId);

    // is best segment good enough?
    if (isGoodSegment(bestBotSeg) && isGoodSegment(bestTopSeg)) {
      if (detailedAnalysis)
        histos[4]->Fill(posAtMid.x(), posAtMid.y());
      //check if interpolation fall inside middle chamber
      if ((dtGeom->chamber(MidId))->surface().bounds().inside(posAtMid)) {
        histos[0]->Fill(posAtMid.x(), posAtMid.y());
        if (nSegsMid > 0) {
          if (detailedAnalysis) {
            histos[3]->Fill(2);
            histos[5]->Fill(posAtMid.x(), posAtMid.y());
          }

          const DTRecSegment4D& bestMidSeg = getBestSegment(segsMid);
          // check if middle segments is good enough
          if (isGoodSegment(bestMidSeg)) {
            if (detailedAnalysis)
              histos[6]->Fill(posAtMid.x(), posAtMid.y());
            LocalPoint midSegPos = bestMidSeg.localPosition();

            // check if middle segments is also close enough
            double dist;
            if (bestMidSeg.hasPhi()) {
              if (bestTopSeg.hasZed() && bestBotSeg.hasZed() && bestMidSeg.hasZed()) {
                dist = (midSegPos - posAtMid).mag();
              } else {
                dist = fabs((midSegPos - posAtMid).x());
              }
            } else {
              dist = fabs((midSegPos - posAtMid).y());
            }
            if (dist < theMinCloseDist) {
              histos[1]->Fill(posAtMid.x(), posAtMid.y());
            }
            if (detailedAnalysis)
              histos[2]->Fill(dist);
          }
        }
      }
    }
  }  // loop over stations
}

// requirements : max number of hits and min chi2
const DTRecSegment4D& DTChamberEfficiencyTask::getBestSegment(const DTRecSegment4DCollection::range& segs) const {
  DTRecSegment4DCollection::const_iterator bestIter;
  unsigned int nHitBest = 0;
  double chi2Best = 99999.;
  for (DTRecSegment4DCollection::const_iterator seg = segs.first; seg != segs.second; ++seg) {
    unsigned int nHits = ((*seg).hasPhi() ? (*seg).phiSegment()->recHits().size() : 0);
    nHits += ((*seg).hasZed() ? (*seg).zSegment()->recHits().size() : 0);

    if (nHits == nHitBest) {
      if ((*seg).chi2() / (*seg).degreesOfFreedom() < chi2Best) {
        chi2Best = (*seg).chi2() / (*seg).degreesOfFreedom();
        bestIter = seg;
      }
    } else if (nHits > nHitBest) {
      nHitBest = nHits;
      bestIter = seg;
    }
  }
  return *bestIter;
}

const DTRecSegment4D* DTChamberEfficiencyTask::getBestSegment(const DTRecSegment4D* s1,
                                                              const DTRecSegment4D* s2) const {
  if (!s1)
    return s2;
  if (!s2)
    return s1;
  unsigned int nHits1 = (s1->hasPhi() ? s1->phiSegment()->recHits().size() : 0);
  nHits1 += (s1->hasZed() ? s1->zSegment()->recHits().size() : 0);

  unsigned int nHits2 = (s2->hasPhi() ? s2->phiSegment()->recHits().size() : 0);
  nHits2 += (s2->hasZed() ? s2->zSegment()->recHits().size() : 0);

  if (nHits1 == nHits2) {
    if (s1->chi2() / s1->degreesOfFreedom() < s2->chi2() / s2->degreesOfFreedom())
      return s1;
    else
      return s2;
  } else if (nHits1 > nHits2)
    return s1;
  return s2;
}

LocalPoint DTChamberEfficiencyTask::interpolate(const DTRecSegment4D& seg1,
                                                const DTRecSegment4D& seg3,
                                                const DTChamberId& id2) const {
  // Get GlobalPoition of Seg in MB1
  GlobalPoint gpos1 = (dtGeom->chamber(seg1.chamberId()))->toGlobal(seg1.localPosition());

  // Get GlobalPoition of Seg in MB3
  GlobalPoint gpos3 = (dtGeom->chamber(seg3.chamberId()))->toGlobal(seg3.localPosition());

  // interpolate
  // get all in MB2 frame
  LocalPoint pos1 = (dtGeom->chamber(id2))->toLocal(gpos1);
  LocalPoint pos3 = (dtGeom->chamber(id2))->toLocal(gpos3);

  // case 1: 1 and 3 has both projection. No problem

  // case 2: one projection is missing for one of the segments. Keep the other's segment position
  if (!seg1.hasZed())
    pos1 = LocalPoint(pos1.x(), pos3.y(), pos1.z());
  if (!seg3.hasZed())
    pos3 = LocalPoint(pos3.x(), pos1.y(), pos3.z());

  if (!seg1.hasPhi())
    pos1 = LocalPoint(pos3.x(), pos1.y(), pos1.z());
  if (!seg3.hasPhi())
    pos3 = LocalPoint(pos1.x(), pos3.y(), pos3.z());

  // direction
  LocalVector dir = (pos3 - pos1).unit();  // z points inward!
  LocalPoint pos2 = pos1 + dir * pos1.z() / (-dir.z());

  return pos2;
}

bool DTChamberEfficiencyTask::isGoodSegment(const DTRecSegment4D& seg) const {
  if (seg.chamberId().station() != 4 && !seg.hasZed())
    return false;
  unsigned int nHits = (seg.hasPhi() ? seg.phiSegment()->recHits().size() : 0);
  nHits += (seg.hasZed() ? seg.zSegment()->recHits().size() : 0);
  return (nHits >= theMinHitsSegment && seg.chi2() / seg.degreesOfFreedom() < theMinChi2NormSegment);
}

// Local Variables:
// show-trailing-whitespace: t
// truncate-lines: t
// End: