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
#include "CommonTools/TriggerUtils/interface/GenericTriggerEventFlag.h"
#include "DQM/SiPixelCommon/interface/SiPixelFolderOrganizer.h"
#include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
#include "DQM/SiStripCommon/interface/SiStripHistoId.h"
#include "DQM/TrackerMonitorTrack/interface/MonitorTrackResiduals.h"
#include "DQMServices/Core/interface/DQMStore.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/Transition.h"

template <TrackerType pixel_or_strip>
MonitorTrackResidualsBase<pixel_or_strip>::MonitorTrackResidualsBase(const edm::ParameterSet &iConfig)
    : conf_(iConfig),
      tkDetMapToken_{esConsumes<TkDetMap, TrackerTopologyRcd, edm::Transition::BeginRun>()},
      trackerTopologyRunToken_{esConsumes<TrackerTopology, TrackerTopologyRcd, edm::Transition::BeginRun>()},
      trackerGeometryToken_{esConsumes<TrackerGeometry, TrackerDigiGeometryRecord, edm::Transition::BeginRun>()},
      trackerTopologyEventToken_{esConsumes<TrackerTopology, TrackerTopologyRcd>()},
      m_cacheID_(0),
      genTriggerEventFlag_(new GenericTriggerEventFlag(
          iConfig.getParameter<edm::ParameterSet>("genericTriggerEventPSet"), consumesCollector(), *this)),
      avalidator_(iConfig, consumesCollector()) {
  applyVertexCut_ = conf_.getUntrackedParameter<bool>("VertexCut", true);
  ModOn = conf_.getParameter<bool>("Mod_On");
  offlinePrimaryVerticesToken_ = consumes<reco::VertexCollection>(std::string("offlinePrimaryVertices"));
}

template <TrackerType pixel_or_strip>
MonitorTrackResidualsBase<pixel_or_strip>::~MonitorTrackResidualsBase() {
  if (genTriggerEventFlag_)
    delete genTriggerEventFlag_;
}

template <TrackerType pixel_or_strip>
void MonitorTrackResidualsBase<pixel_or_strip>::bookHistograms(DQMStore::IBooker &ibooker,
                                                               const edm::Run &run,
                                                               const edm::EventSetup &iSetup) {
  unsigned long long cacheID = iSetup.get<TrackerDigiGeometryRecord>().cacheIdentifier();
  if (m_cacheID_ != cacheID) {
    m_cacheID_ = cacheID;
    this->createMEs(ibooker, iSetup);
  }
  std::string topFolderName_ = "SiStrip";
  SiStripFolderOrganizer folder_organizer;
  folder_organizer.setSiStripFolderName(topFolderName_);
  const TkDetMap *tkDetMap = &iSetup.getData(tkDetMapToken_);
  tkhisto_ResidualsMean =
      std::make_unique<TkHistoMap>(tkDetMap, ibooker, topFolderName_, "TkHMap_ResidualsMean", 0.0, true);
}

template <TrackerType pixel_or_strip>
void MonitorTrackResidualsBase<pixel_or_strip>::dqmBeginRun(edm::Run const &run, edm::EventSetup const &iSetup) {
  // Initialize the GenericTriggerEventFlag
  if (genTriggerEventFlag_->on())
    genTriggerEventFlag_->initRun(run, iSetup);
}

template <TrackerType pixel_or_strip>
std::pair<std::string, int32_t> MonitorTrackResidualsBase<pixel_or_strip>::findSubdetAndLayer(
    uint32_t ModuleID, const TrackerTopology *tTopo) {
  std::string subdet = "";
  int32_t layer = 0;
  auto id = DetId(ModuleID);
  switch (id.subdetId()) {
    // Pixel Barrel, Endcap
    case 1:
      subdet = "BPIX";
      layer = tTopo->pxbLayer(id);
      break;
    case 2:
      subdet = "FPIX";
      layer = tTopo->pxfDisk(id) * (tTopo->pxfSide(ModuleID) == 1 ? -1 : +1);
      break;
    // Strip TIB, TID, TOB, TEC
    case 3:
      subdet = "TIB";
      layer = tTopo->tibLayer(id);
      break;
    case 4:
      subdet = "TID";
      layer = tTopo->tidWheel(id) * (tTopo->tidSide(ModuleID) == 1 ? -1 : +1);
      break;
    case 5:
      subdet = "TOB";
      layer = tTopo->tobLayer(id);
      break;
    case 6:
      subdet = "TEC";
      layer = tTopo->tecWheel(id) * (tTopo->tecSide(ModuleID) == 1 ? -1 : +1);
      break;
    default:
      // TODO: Fail loudly.
      subdet = "UNKNOWN";
      layer = 0;
  }
  return std::make_pair(subdet, layer);
}

template <TrackerType pixel_or_strip>
void MonitorTrackResidualsBase<pixel_or_strip>::createMEs(DQMStore::IBooker &ibooker, const edm::EventSetup &iSetup) {
  // Retrieve tracker topology and geometry
  const TrackerTopology *const tTopo = &iSetup.getData(trackerTopologyRunToken_);
  const TrackerGeometry *TG = &iSetup.getData(trackerGeometryToken_);

  Parameters = conf_.getParameter<edm::ParameterSet>("TH1ResModules");
  int32_t i_residuals_Nbins = Parameters.getParameter<int32_t>("Nbinx");
  double d_residual_xmin = Parameters.getParameter<double>("xmin");
  double d_residual_xmax = Parameters.getParameter<double>("xmax");
  Parameters = conf_.getParameter<edm::ParameterSet>("TH1NormResModules");
  int32_t i_normres_Nbins = Parameters.getParameter<int32_t>("Nbinx");
  double d_normres_xmin = Parameters.getParameter<double>("xmin");
  double d_normres_xmax = Parameters.getParameter<double>("xmax");

  // use SistripHistoId for producing histogram id (and title)
  SiStripHistoId hidmanager;

  SiStripFolderOrganizer strip_organizer;
  auto pixel_organizer = SiPixelFolderOrganizer(false);

  // Collect list of modules from Tracker Geometry
  // book histo per each detector module
  auto ids = TG->detIds();  // or detUnitIds?
  for (DetId id : ids) {
    auto ModuleID = id.rawId();
    auto isPixel = id.subdetId() == 1 || id.subdetId() == 2;
    if (isPixel != (pixel_or_strip == TRACKERTYPE_PIXEL))
      continue;

    // Book module histogramms?
    if (ModOn) {
      switch (id.subdetId()) {
        case 1:
          pixel_organizer.setModuleFolder(ibooker, ModuleID, 0);
          break;
        case 2:
          pixel_organizer.setModuleFolder(ibooker, ModuleID, 0);
          break;
        default:
          strip_organizer.setDetectorFolder(ModuleID, tTopo);
      }
      {
        // this sounds strip specific but also works for pixel
        std::string hid = hidmanager.createHistoId("HitResidualsX", "det", ModuleID);
        std::string normhid = hidmanager.createHistoId("NormalizedHitResidualsX", "det", ModuleID);
        auto &histos = m_ModuleResiduals[std::make_pair("", ModuleID)];
        histos.x.base = ibooker.book1D(hid, hid, i_residuals_Nbins, d_residual_xmin, d_residual_xmax);
        histos.x.base->setAxisTitle("(x_{pred} - x_{rec})' [cm]");
        histos.x.normed = ibooker.book1D(normhid, normhid, i_normres_Nbins, d_normres_xmin, d_normres_xmax);
        histos.x.normed->setAxisTitle("(x_{pred} - x_{rec})'/#sigma");
      }
      {
        std::string hid = hidmanager.createHistoId("HitResidualsY", "det", ModuleID);
        std::string normhid = hidmanager.createHistoId("NormalizedHitResidualsY", "det", ModuleID);
        auto &histos = m_ModuleResiduals[std::make_pair("", ModuleID)];
        histos.y.base = ibooker.book1D(hid, hid, i_residuals_Nbins, d_residual_xmin, d_residual_xmax);
        histos.y.base->setAxisTitle("(y_{pred} - y_{rec})' [cm]");
        histos.y.normed = ibooker.book1D(normhid, normhid, i_normres_Nbins, d_normres_xmin, d_normres_xmax);
        histos.y.normed->setAxisTitle("(y_{pred} - y_{rec})'/#sigma");
      }
    }

    auto subdetandlayer = findSubdetAndLayer(ModuleID, tTopo);
    if (m_SubdetLayerResiduals.find(subdetandlayer) == m_SubdetLayerResiduals.end()) {
      // add new histograms
      auto &histos = m_SubdetLayerResiduals[subdetandlayer];
      switch (id.subdetId()) {
        // Pixel Barrel, Endcap
        // We can't use the folder organizer here (SiPixelActionExecutor.cc#1638
        // does the same)
        case 1:
          ibooker.setCurrentFolder("Pixel/Barrel");
          break;
        case 2:
          ibooker.setCurrentFolder("Pixel/Endcap");
          break;
        // All strip
        default:
          strip_organizer.setLayerFolder(ModuleID, tTopo, subdetandlayer.second);
      }

      auto isBarrel = subdetandlayer.first.find("B") != std::string::npos;

      auto xy = std::vector<std::pair<HistoPair &, const char *>>{std::make_pair(std::ref(histos.x), "X"),
                                                                  std::make_pair(std::ref(histos.y), "Y")};
      for (auto &histopair : xy) {
        // book histogramms on layer level, check for barrel/pixel only for
        // correct labeling

        // Skip the Y plots for strips.
        if (!isPixel && histopair.second[0] == 'Y')
          continue;

        std::string histoname = isPixel ? (  // Pixel name
                                              Form("HitResiduals%s_%s%d",
                                                   histopair.second,
                                                   isBarrel ? "L" : (subdetandlayer.second > 0 ? "Dp" : "Dm"),
                                                   std::abs(subdetandlayer.second)))
                                        : (Form("HitResiduals_%s__%s__%d",  // Strip TODO: We use a
                                                                            // legacy name.
                                                subdetandlayer.first.c_str(),
                                                isBarrel ? "Layer" : "wheel",
                                                std::abs(subdetandlayer.second)));

        std::string histotitle = Form("HitResiduals %s on %s%s full %s %d",
                                      histopair.second,
                                      subdetandlayer.first.c_str(),
                                      isBarrel ? "" : (subdetandlayer.second > 0 ? "+" : "-"),
                                      isBarrel ? "Layer" : (isPixel ? "Disk" : "Wheel"),
                                      std::abs(subdetandlayer.second));

        std::string normhistoname = Form("Normalized%s", histoname.c_str());
        std::string normhistotitle = Form("Normalized%s", histotitle.c_str());

        // std::cout << "##### Booking: " << ibooker.pwd() << " title " <<
        // histoname << std::endl;

        histopair.first.base =
            ibooker.book1D(histoname.c_str(), histotitle.c_str(), i_residuals_Nbins, d_residual_xmin, d_residual_xmax);
        histopair.first.base->setAxisTitle("(x_{pred} - x_{rec})' [cm]");

        histopair.first.normed = ibooker.book1D(
            normhistoname.c_str(), normhistotitle.c_str(), i_normres_Nbins, d_normres_xmin, d_normres_xmax);
        histopair.first.normed->setAxisTitle("(x_{pred} - x_{rec})'/#sigma");
      }
    }
  }  // end loop over activeDets
}

template <TrackerType pixel_or_strip>
void MonitorTrackResidualsBase<pixel_or_strip>::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
  auto vtracks = std::vector<TrackerValidationVariables::AVTrackStruct>();
  // Filter out events if Trigger Filtering is requested
  if (genTriggerEventFlag_->on() && !genTriggerEventFlag_->accept(iEvent, iSetup))
    return;

  edm::Handle<reco::VertexCollection> vertices;
  if (applyVertexCut_) {
    iEvent.getByToken(offlinePrimaryVerticesToken_, vertices);
    if (!vertices.isValid() || vertices->empty())
      return;
  }

  // Retrieve tracker topology from geometry
  const TrackerTopology *const tTopo = &iSetup.getData(trackerTopologyEventToken_);

  avalidator_.fillTrackQuantities(
      iEvent,
      iSetup,
      // tell the validator to only look at good tracks
      [&](const reco::Track &track) -> bool {
        return (!applyVertexCut_ ||
                (track.pt() > 0.75 && abs(track.dxy(vertices->at(0).position())) < 5 * track.dxyError()));
      },
      vtracks);

  for (auto &track : vtracks) {
    for (auto &it : track.hits) {
      uint RawId = it.rawDetId;

      auto id = DetId(RawId);
      auto isPixel = id.subdetId() == 1 || id.subdetId() == 2;
      if (isPixel != (pixel_or_strip == TRACKERTYPE_PIXEL))
        continue;

      if (ModOn) {
        auto &mod_histos = m_ModuleResiduals[std::make_pair("", RawId)];
        mod_histos.x.base->Fill(it.resXprime);
        mod_histos.x.normed->Fill(it.resXprime / it.resXprimeErr);
        mod_histos.y.base->Fill(it.resYprime);
        mod_histos.y.normed->Fill(it.resYprime / it.resYprimeErr);
      }

      auto subdetandlayer = findSubdetAndLayer(RawId, tTopo);
      auto histos = m_SubdetLayerResiduals[subdetandlayer];
      // fill if its error is not zero
      if (it.resXprimeErr != 0 && histos.x.base) {
        histos.x.base->Fill(it.resXprime);
        histos.x.normed->Fill(it.resXprime / it.resXprimeErr);
        if (!isPixel)
          tkhisto_ResidualsMean->fill(RawId, it.resXprime);
      }
      if (it.resYprimeErr != 0 && histos.y.base) {
        histos.y.base->Fill(it.resYprime);
        histos.y.normed->Fill(it.resYprime / it.resYprimeErr);
      }
    }
  }
}

DEFINE_FWK_MODULE(MonitorTrackResiduals);
DEFINE_FWK_MODULE(SiPixelMonitorTrackResiduals);