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
// -*- C++ -*-
//
// Package:    SiPixelMonitorRecHits
// Class:      SiPixelRecHitSource
//
/**\class

 Description: Pixel DQM source for RecHits

 Implementation:
     Originally based on the code for Digis, adapted
        to read RecHits and create relevant histograms
*/
//
// Original Author:  Vincenzo Chiochia
//         Created:
//
//
// Adapted by:  Keith Rose
//  	For use in SiPixelMonitorClient for RecHits
// Updated by: Lukas Wehrli
// for pixel offline DQM

#include "DQM/SiPixelMonitorRecHit/interface/SiPixelRecHitSource.h"
// Framework
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Framework/interface/MakerMacros.h"
// DQM Framework
#include "DQM/SiPixelCommon/interface/SiPixelFolderOrganizer.h"
#include "DQMServices/Core/interface/DQMStore.h"
// Geometry
#include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
// DataFormats
#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
#include "DataFormats/SiPixelDetId/interface/PixelBarrelNameUpgrade.h"
#include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
#include "DataFormats/SiPixelDetId/interface/PixelEndcapNameUpgrade.h"
#include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"

//
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
using namespace edm;

SiPixelRecHitSource::SiPixelRecHitSource(const edm::ParameterSet &iConfig)
    : conf_(iConfig),
      src_(consumes<SiPixelRecHitCollection>(conf_.getParameter<edm::InputTag>("src"))),
      trackerTopoTokenBeginRun_(esConsumes<TrackerTopology, TrackerTopologyRcd, edm::Transition::BeginRun>()),
      trackerGeomTokenBeginRun_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord, edm::Transition::BeginRun>()),
      saveFile(conf_.getUntrackedParameter<bool>("saveFile", false)),
      isPIB(conf_.getUntrackedParameter<bool>("isPIB", false)),
      slowDown(conf_.getUntrackedParameter<bool>("slowDown", false)),
      modOn(conf_.getUntrackedParameter<bool>("modOn", true)),
      twoDimOn(conf_.getUntrackedParameter<bool>("twoDimOn", true)),
      reducedSet(conf_.getUntrackedParameter<bool>("reducedSet", false)),
      ladOn(conf_.getUntrackedParameter<bool>("ladOn", false)),
      layOn(conf_.getUntrackedParameter<bool>("layOn", false)),
      phiOn(conf_.getUntrackedParameter<bool>("phiOn", false)),
      ringOn(conf_.getUntrackedParameter<bool>("ringOn", false)),
      bladeOn(conf_.getUntrackedParameter<bool>("bladeOn", false)),
      diskOn(conf_.getUntrackedParameter<bool>("diskOn", false)),
      isUpgrade(conf_.getUntrackedParameter<bool>("isUpgrade", false)) {
  firstRun = true;
  LogInfo("PixelDQM") << "SiPixelRecHitSource::SiPixelRecHitSource: Got DQM BackEnd interface" << endl;
  topFolderName_ = conf_.getParameter<std::string>("TopFolderName");
}

SiPixelRecHitSource::~SiPixelRecHitSource() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
  LogInfo("PixelDQM") << "SiPixelRecHitSource::~SiPixelRecHitSource: Destructor" << endl;
  std::map<uint32_t, SiPixelRecHitModule *>::iterator struct_iter;
  for (struct_iter = thePixelStructure.begin(); struct_iter != thePixelStructure.end(); struct_iter++) {
    delete struct_iter->second;
    struct_iter->second = nullptr;
  }
}

void SiPixelRecHitSource::dqmBeginRun(const edm::Run &r, const edm::EventSetup &iSetup) {
  LogInfo("PixelDQM") << " SiPixelRecHitSource::beginJob - Initialisation ... " << std::endl;
  LogInfo("PixelDQM") << "Mod/Lad/Lay/Phi " << modOn << "/" << ladOn << "/" << layOn << "/" << phiOn << std::endl;
  LogInfo("PixelDQM") << "Blade/Disk/Ring" << bladeOn << "/" << diskOn << "/" << ringOn << std::endl;
  LogInfo("PixelDQM") << "2DIM IS " << twoDimOn << "\n";

  if (firstRun) {
    eventNo = 0;
    // Build map
    buildStructure(iSetup);
    // Book Monitoring Elements
    firstRun = false;
  }
}

void SiPixelRecHitSource::bookHistograms(DQMStore::IBooker &iBooker, edm::Run const &, const edm::EventSetup &iSetup) {
  bookMEs(iBooker, iSetup);
}

//------------------------------------------------------------------
// Method called for every event
//------------------------------------------------------------------
void SiPixelRecHitSource::analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
  eventNo++;
  // cout << eventNo << endl;
  // get input data
  edm::Handle<SiPixelRecHitCollection> recHitColl;
  iEvent.getByToken(src_, recHitColl);

  std::map<uint32_t, SiPixelRecHitModule *>::iterator struct_iter;
  for (struct_iter = thePixelStructure.begin(); struct_iter != thePixelStructure.end(); struct_iter++) {
    uint32_t TheID = (*struct_iter).first;

    SiPixelRecHitCollection::const_iterator match = recHitColl->find(TheID);

    // if( pixelrechitRangeIteratorBegin == pixelrechitRangeIteratorEnd) {cout
    // << "oops" << endl;}
    float rechit_x = 0;
    float rechit_y = 0;
    int rechit_count = 0;

    if (match != recHitColl->end()) {
      SiPixelRecHitCollection::DetSet pixelrechitRange = *match;
      SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorBegin = pixelrechitRange.begin();
      SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorEnd = pixelrechitRange.end();
      SiPixelRecHitCollection::DetSet::const_iterator pixeliter = pixelrechitRangeIteratorBegin;

      for (; pixeliter != pixelrechitRangeIteratorEnd; pixeliter++) {
        rechit_count++;
        // cout << TheID << endl;
        SiPixelRecHit::ClusterRef const &clust = pixeliter->cluster();
        int sizeX = (*clust).sizeX();
        // cout << sizeX << endl;
        int sizeY = (*clust).sizeY();
        // cout << sizeY << endl;
        LocalPoint lp = pixeliter->localPosition();
        rechit_x = lp.x();
        rechit_y = lp.y();

        LocalError lerr = pixeliter->localPositionError();
        float lerr_x = sqrt(lerr.xx());
        float lerr_y = sqrt(lerr.yy());
        // std::cout << "errors " << lerr_x << " " << lerr_y << std::endl;
        (*struct_iter)
            .second->fill(rechit_x,
                          rechit_y,
                          sizeX,
                          sizeY,
                          lerr_x,
                          lerr_y,
                          modOn,
                          ladOn,
                          layOn,
                          phiOn,
                          bladeOn,
                          diskOn,
                          ringOn,
                          twoDimOn,
                          reducedSet);
      }
    }
    if (rechit_count > 0)
      (*struct_iter).second->nfill(rechit_count, modOn, ladOn, layOn, phiOn, bladeOn, diskOn, ringOn);
  }

  // slow down...
  if (slowDown)
    usleep(10000);
}

//------------------------------------------------------------------
// Build data structure
//------------------------------------------------------------------
void SiPixelRecHitSource::buildStructure(const edm::EventSetup &iSetup) {
  LogInfo("PixelDQM") << " SiPixelRecHitSource::buildStructure";

  edm::ESHandle<TrackerGeometry> pDD = iSetup.getHandle(trackerGeomTokenBeginRun_);

  edm::ESHandle<TrackerTopology> tTopoHandle = iSetup.getHandle(trackerTopoTokenBeginRun_);
  const TrackerTopology *pTT = tTopoHandle.product();

  LogVerbatim("PixelDQM") << " *** Geometry node for TrackerGeom is  " << &(*pDD) << std::endl;
  LogVerbatim("PixelDQM") << " *** I have " << pDD->dets().size() << " detectors" << std::endl;
  LogVerbatim("PixelDQM") << " *** I have " << pDD->detTypes().size() << " types" << std::endl;

  for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++) {
    if (dynamic_cast<PixelGeomDetUnit const *>((*it)) != nullptr) {
      DetId detId = (*it)->geographicalId();

      if ((detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
          (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap))) {
        uint32_t id = detId();

        if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) {
          if (isPIB)
            continue;
          LogDebug("PixelDQM") << " ---> Adding Barrel Module " << detId.rawId() << endl;
          SiPixelRecHitModule *theModule = new SiPixelRecHitModule(id);
          thePixelStructure.insert(pair<uint32_t, SiPixelRecHitModule *>(id, theModule));

        } else if ((detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap))) {
          LogDebug("PixelDQM") << " ---> Adding Endcap Module " << detId.rawId() << endl;

          PixelEndcapName::HalfCylinder side = PixelEndcapName(DetId(id), pTT, isUpgrade).halfCylinder();
          int disk = PixelEndcapName(DetId(id), pTT, isUpgrade).diskName();
          int blade = PixelEndcapName(DetId(id), pTT, isUpgrade).bladeName();
          int panel = PixelEndcapName(DetId(id), pTT, isUpgrade).pannelName();
          int module = PixelEndcapName(DetId(id), pTT, isUpgrade).plaquetteName();

          char sside[80];
          sprintf(sside, "HalfCylinder_%i", side);
          char sdisk[80];
          sprintf(sdisk, "Disk_%i", disk);
          char sblade[80];
          sprintf(sblade, "Blade_%02i", blade);
          char spanel[80];
          sprintf(spanel, "Panel_%i", panel);
          char smodule[80];
          sprintf(smodule, "Module_%i", module);
          std::string side_str = sside;
          std::string disk_str = sdisk;
          bool mask = side_str.find("HalfCylinder_1") != string::npos ||
                      side_str.find("HalfCylinder_2") != string::npos ||
                      side_str.find("HalfCylinder_4") != string::npos || disk_str.find("Disk_2") != string::npos;
          if (isPIB && mask)
            continue;

          SiPixelRecHitModule *theModule = new SiPixelRecHitModule(id);
          thePixelStructure.insert(pair<uint32_t, SiPixelRecHitModule *>(id, theModule));
        }
      }
    }
  }  // FOR_LOOP

  LogInfo("PixelDQM") << " *** Pixel Structure Size " << thePixelStructure.size() << endl;
}
//------------------------------------------------------------------
// Book MEs
//------------------------------------------------------------------
void SiPixelRecHitSource::bookMEs(DQMStore::IBooker &iBooker, const edm::EventSetup &iSetup) {
  std::map<uint32_t, SiPixelRecHitModule *>::iterator struct_iter;

  SiPixelFolderOrganizer theSiPixelFolder(false);

  edm::ESHandle<TrackerTopology> tTopoHandle = iSetup.getHandle(trackerTopoTokenBeginRun_);
  const TrackerTopology *pTT = tTopoHandle.product();

  for (struct_iter = thePixelStructure.begin(); struct_iter != thePixelStructure.end(); struct_iter++) {
    /// Create folder tree and book histograms
    if (modOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 0, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 0, twoDimOn, reducedSet, isUpgrade);
      } else {
        if (!isPIB)
          throw cms::Exception("LogicError") << "[SiPixelDigiSource::bookMEs] Creation of DQM folder failed";
      }
    }
    if (ladOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 1, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 1, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH LADDER-FOLDER\n";
      }
    }
    if (layOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 2, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 2, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH LAYER-FOLDER\n";
      }
    }
    if (phiOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 3, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 3, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH PHI-FOLDER\n";
      }
    }
    if (bladeOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 4, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 4, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH BLADE-FOLDER\n";
      }
    }
    if (diskOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 5, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 5, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH DISK-FOLDER\n";
      }
    }
    if (ringOn) {
      if (theSiPixelFolder.setModuleFolder(iBooker, (*struct_iter).first, 6, isUpgrade)) {
        (*struct_iter).second->book(conf_, iBooker, pTT, 6, twoDimOn, reducedSet, isUpgrade);
      } else {
        LogDebug("PixelDQM") << "PROBLEM WITH RING-FOLDER\n";
      }
    }
  }
}

// define this as a plug-in
DEFINE_FWK_MODULE(SiPixelRecHitSource);