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
/*
 * \file DTLocalLutTriggerTask.cc
 *
 * \author D. Fasanella - INFN Bologna
 *
 */

#include "DQM/DTMonitorModule/src/DTLocalTriggerLutTask.h"

// Framework
#include "FWCore/Framework/interface/EventSetup.h"

// DT trigger
#include "DQM/DTMonitorModule/interface/DTTrigGeomUtils.h"

// Geometry
#include "DataFormats/GeometryVector/interface/Pi.h"
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/DTGeometry/interface/DTLayer.h"
#include "Geometry/DTGeometry/interface/DTTopology.h"

//Root
#include "TH1.h"
#include "TAxis.h"

#include <sstream>
#include <iostream>
#include <cmath>

using namespace edm;
using namespace std;

DTLocalTriggerLutTask::DTLocalTriggerLutTask(const edm::ParameterSet& ps)
    : muonGeomToken_(
          esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", ps.getUntrackedParameter<string>("geomLabel")))),
      trigGeomUtils(nullptr) {
  LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerLutTask") << "[DTLocalTriggerLutTask]: Constructor" << endl;

  tm_TokenIn_ = consumes<L1MuDTChambPhContainer>(ps.getUntrackedParameter<InputTag>("inputTagTMin"));
  tm_TokenOut_ = consumes<L1MuDTChambPhContainer>(ps.getUntrackedParameter<InputTag>("inputTagTMout"));
  seg_Token_ = consumes<DTRecSegment4DCollection>(ps.getUntrackedParameter<InputTag>("inputTagSEG"));

  overUnderIn = ps.getUntrackedParameter<bool>("rebinOutFlowsInGraph");
  detailedAnalysis = ps.getUntrackedParameter<bool>("detailedAnalysis");

  if (detailedAnalysis) {
    nPhiBins = 401;
    rangePhi = 10.025;
    nPhibBins = 401;
    rangePhiB = 10.025;
  } else {
    nPhiBins = 51;
    rangePhi = 5.1;
    nPhibBins = 51;
    rangePhiB = 10.2;
  }

  baseFolder = "DT/03-LocalTrigger-TM/";
  parameters = ps;

  nEvents = 0;
  nLumis = 0;
}

DTLocalTriggerLutTask::~DTLocalTriggerLutTask() {
  LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerLutTask")
      << "[DTLocalTriggerLutTask]: analyzed " << nEvents << " events" << endl;
  if (trigGeomUtils) {
    delete trigGeomUtils;
  }
}

void DTLocalTriggerLutTask::dqmBeginRun(const edm::Run& run, const edm::EventSetup& context) {
  // Get the geometry
  muonGeom = &context.getData(muonGeomToken_);
  trigGeomUtils = new DTTrigGeomUtils(muonGeom);
}

void DTLocalTriggerLutTask::bookHistos(DQMStore::IBooker& ibooker, DTChamberId chId) {
  stringstream wheel;
  wheel << chId.wheel();
  stringstream sector;
  sector << chId.sector();
  stringstream station;
  station << chId.station();

  ibooker.setCurrentFolder(topFolder() + "Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str() +
                           "/Segment");

  string chTag = "_W" + wheel.str() + "_Sec" + sector.str() + "_St" + station.str();
  std::map<std::string, MonitorElement*>& chambMap = chHistos[chId.rawId()];

  string hName = "TM_PhiResidualIn";

  chambMap[hName] = ibooker.book1D(hName + chTag,
                                   "Trigger local position In - Segment local position (correlated triggers)",
                                   nPhiBins,
                                   -rangePhi,
                                   rangePhi);

  hName = "TM_PhiResidualOut";

  chambMap[hName] = ibooker.book1D(hName + chTag,
                                   "Trigger local position Out - Segment local position (correlated triggers)",
                                   nPhiBins,
                                   -rangePhi,
                                   rangePhi);

  hName = "TM_PhibResidualIn";

  chambMap[hName] = ibooker.book1D(hName + chTag,
                                   "Trigger local direction In - Segment local direction (correlated triggers)",
                                   nPhibBins,
                                   -rangePhiB,
                                   rangePhiB);

  hName = "TM_PhibResidualOut";

  chambMap[hName] = ibooker.book1D(hName + chTag,
                                   "Trigger local direction Out - Segment local direction (correlated triggers)",
                                   nPhibBins,
                                   -rangePhiB,
                                   rangePhiB);

  if (detailedAnalysis) {
    hName = "TM_PhitkvsPhitrig";

    chambMap[hName] =
        ibooker.book2D(hName + chTag, "Local position: segment vs trigger", 100, -500., 500., 100, -500., 500.);
    hName = "TM_PhibtkvsPhibtrig";

    chambMap[hName] =
        ibooker.book2D(hName + chTag, "Local direction : segment vs trigger", 200, -40., 40., 200, -40., 40.);
    hName = "TM_PhibResidualvsTkPos";

    chambMap[hName] =
        ibooker.book2D(hName + chTag, "Local direction residual vs Segment Position", 100, -500., 500., 200, -10., 10.);
    hName = "TM_PhiResidualvsTkPos";

    chambMap[hName] =
        ibooker.book2D(hName + chTag, "Local Position residual vs Segment Position", 100, -500., 500., 200, -10., 10.);
  }
}

void DTLocalTriggerLutTask::bookHistograms(DQMStore::IBooker& ibooker,
                                           edm::Run const& run,
                                           edm::EventSetup const& context) {
  LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerLutTask") << "[DTLocalTriggerLutTask]: bookHistograms" << endl;

  std::vector<const DTChamber*>::const_iterator chambIt = muonGeom->chambers().begin();
  std::vector<const DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();

  for (; chambIt != chambEnd; ++chambIt)
    bookHistos(ibooker, (*chambIt)->id());
}

void DTLocalTriggerLutTask::beginLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& context) {
  nLumis++;
  LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerLutTask") << "[DTLocalTriggerLutTask]: Begin of LS transition" << endl;

  if (nLumis % parameters.getUntrackedParameter<int>("ResetCycle") == 0) {
    LogTrace("DTDQM|DTMonitorModule|DTLocalTriggerLutTask") << "[DTLocalTriggerLutTask]: Cleaning histos" << endl;
    map<uint32_t, map<string, MonitorElement*> >::const_iterator chambIt = chHistos.begin();
    map<uint32_t, map<string, MonitorElement*> >::const_iterator chambEnd = chHistos.end();

    for (; chambIt != chambEnd; ++chambIt) {
      map<string, MonitorElement*>::const_iterator histoIt = chambIt->second.begin();
      map<string, MonitorElement*>::const_iterator histoEnd = chambIt->second.end();
      for (; histoIt != histoEnd; ++histoIt) {
        histoIt->second->Reset();
      }
    }
  }
}

void DTLocalTriggerLutTask::analyze(const edm::Event& e, const edm::EventSetup& c) {
  nEvents++;

  edm::Handle<L1MuDTChambPhContainer> trigHandleIn;
  e.getByToken(tm_TokenIn_, trigHandleIn);
  edm::Handle<L1MuDTChambPhContainer> trigHandleOut;
  e.getByToken(tm_TokenOut_, trigHandleOut);

  vector<L1MuDTChambPhDigi> const* trigsIn = trigHandleIn->getContainer();
  searchTMBestIn(trigsIn);
  vector<L1MuDTChambPhDigi> const* trigsOut = trigHandleOut->getContainer();
  searchTMBestOut(trigsOut);

  Handle<DTRecSegment4DCollection> segments4D;
  e.getByToken(seg_Token_, segments4D);
  DTRecSegment4DCollection::id_iterator chamberId;

  // Preliminary loop finds best 4D Segment and high quality ones
  vector<const DTRecSegment4D*> best4DSegments;

  for (chamberId = segments4D->id_begin(); chamberId != segments4D->id_end(); ++chamberId) {
    DTRecSegment4DCollection::range rangeInCh = segments4D->get(*chamberId);
    DTRecSegment4DCollection::const_iterator trackIt = rangeInCh.first;
    DTRecSegment4DCollection::const_iterator trackEnd = rangeInCh.second;

    const DTRecSegment4D* tmpBest = nullptr;
    int tmpdof = 0;
    int dof = 0;

    for (; trackIt != trackEnd; ++trackIt) {
      if (trackIt->hasPhi()) {
        dof = trackIt->phiSegment()->degreesOfFreedom();
        if (dof > tmpdof) {
          tmpBest = &(*trackIt);
          tmpdof = dof;
        }
      }
    }

    if (tmpBest)
      best4DSegments.push_back(tmpBest);
  }

  vector<const DTRecSegment4D*>::const_iterator bestTrackIt = best4DSegments.begin();
  vector<const DTRecSegment4D*>::const_iterator bestTrackEnd = best4DSegments.end();

  for (; bestTrackIt != bestTrackEnd; ++bestTrackIt) {
    if ((*bestTrackIt)->hasPhi()) {
      DTChamberId chId = (*bestTrackIt)->chamberId();
      int nHitsPhi = (*bestTrackIt)->phiSegment()->degreesOfFreedom() + 2;

      int wheel = chId.wheel();
      int station = chId.station();
      int scsector = 0;
      float trackPosPhi, trackPosEta, trackDirPhi, trackDirEta;
      trigGeomUtils->computeSCCoordinates((*bestTrackIt), scsector, trackPosPhi, trackDirPhi, trackPosEta, trackDirEta);

      map<string, MonitorElement*>& chMap = chHistos[chId.rawId()];
      // In part
      if (trigQualBestIn[wheel + 3][station][scsector] > 3 &&  // residuals only for correlate triggers
          trigQualBestIn[wheel + 3][station][scsector] < 7 && nHitsPhi >= 7) {
        float trigPos = trigGeomUtils->trigPos(trigBestIn[wheel + 3][station][scsector]);
        float trigDir = trigGeomUtils->trigDir(trigBestIn[wheel + 3][station][scsector]);
        trigGeomUtils->trigToSeg(station, trigPos, trackDirPhi);

        double deltaPos = trigPos - trackPosPhi;
        deltaPos = overUnderIn ? max(min(deltaPos, rangePhi - 0.01), -rangePhi + 0.01) : deltaPos;
        double deltaDir = trigDir - trackDirPhi;
        deltaDir = overUnderIn ? max(min(deltaDir, rangePhiB - 0.01), -rangePhiB + 0.01) : deltaDir;
        chMap.find("TM_PhiResidualIn")->second->Fill(deltaPos);
        chMap.find("TM_PhibResidualIn")->second->Fill(deltaDir);

        if (detailedAnalysis) {
          chMap.find("TM_PhitkvsPhitrig")->second->Fill(trigPos, trackPosPhi);
          chMap.find("TM_PhibtkvsPhibtrig")->second->Fill(trigDir, trackDirPhi);
          chMap.find("TM_PhibResidualvsTkPos")->second->Fill(trackPosPhi, trigDir - trackDirPhi);
          chMap.find("TM_PhiResidualvsTkPos")->second->Fill(trackPosPhi, trigPos - trackPosPhi);
        }
      }

      // Out part
      if (trigQualBestOut[wheel + 3][station][scsector] > 3 &&  // residuals only for correlate triggers
          trigQualBestOut[wheel + 3][station][scsector] < 7 && nHitsPhi >= 7) {
        float trigPos = trigGeomUtils->trigPos(trigBestOut[wheel + 3][station][scsector]);
        float trigDir = trigGeomUtils->trigDir(trigBestOut[wheel + 3][station][scsector]);
        trigGeomUtils->trigToSeg(station, trigPos, trackDirPhi);

        double deltaPos = trigPos - trackPosPhi;
        deltaPos = overUnderIn ? max(min(deltaPos, rangePhi - 0.01), -rangePhi + 0.01) : deltaPos;
        double deltaDir = trigDir - trackDirPhi;
        deltaDir = overUnderIn ? max(min(deltaDir, rangePhiB - 0.01), -rangePhiB + 0.01) : deltaDir;
        chMap.find("TM_PhiResidualOut")->second->Fill(deltaPos);
        chMap.find("TM_PhibResidualOut")->second->Fill(deltaDir);
      }
    }
  }
}

void DTLocalTriggerLutTask::searchTMBestIn(std::vector<L1MuDTChambPhDigi> const* trigs) {
  string histoType;
  string histoTag;

  // define best quality trigger segment
  // start from 1 and zero is kept empty
  for (int st = 0; st <= 4; ++st)
    for (int wh = 0; wh <= 5; ++wh)
      for (int sec = 0; sec <= 12; ++sec)
        trigQualBestIn[wh][st][sec] = -1;

  vector<L1MuDTChambPhDigi>::const_iterator trigIt = trigs->begin();
  vector<L1MuDTChambPhDigi>::const_iterator trigEnd = trigs->end();
  for (; trigIt != trigEnd; ++trigIt) {
    int wh = trigIt->whNum();
    int sec = trigIt->scNum() + 1;  // DTTF -> DT sector range transform
    int st = trigIt->stNum();
    int qual = trigIt->code();

    if (qual > trigQualBestIn[wh + wheelArrayShift][st][sec] && qual < 7) {
      trigQualBestIn[wh + wheelArrayShift][st][sec] = qual;
      trigBestIn[wh + wheelArrayShift][st][sec] = &(*trigIt);
    }
  }
}

void DTLocalTriggerLutTask::searchTMBestOut(std::vector<L1MuDTChambPhDigi> const* trigs) {
  string histoType;
  string histoTag;

  // define best quality trigger segment
  //   // start from 1 and zero is kept empty
  for (int st = 0; st <= 4; ++st)
    for (int wh = 0; wh <= 5; ++wh)
      for (int sec = 0; sec <= 12; ++sec)
        trigQualBestOut[wh][st][sec] = -1;

  vector<L1MuDTChambPhDigi>::const_iterator trigIt = trigs->begin();
  vector<L1MuDTChambPhDigi>::const_iterator trigEnd = trigs->end();
  for (; trigIt != trigEnd; ++trigIt) {
    int wh = trigIt->whNum();
    int sec = trigIt->scNum() + 1;  // DTTF -> DT sector range transform
    int st = trigIt->stNum();
    int qual = trigIt->code();

    if (qual > trigQualBestOut[wh + wheelArrayShift][st][sec] && qual < 7) {
      trigQualBestOut[wh + wheelArrayShift][st][sec] = qual;
      trigBestOut[wh + wheelArrayShift][st][sec] = &(*trigIt);
    }
  }
}

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