TrackerTreeGenerator

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
// -*- C++ -*-
//
// Package:    TrackerTreeGenerator
// Class:      TrackerTreeGenerator
//
/**\class TrackerTreeGenerator TrackerTreeGenerator.cc Alignment/TrackerAlignment/plugins/TrackerTreeGenerator.cc
 Description: <one line class summary>
 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Johannes Hauk
//         Created:  Fri Jan 16 14:09:52 CET 2009
//         Modified by: Gregor Mittag (DESY)
//
//
//

// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/Service.h"

#include "CondFormats/GeometryObjects/interface/PTrackerParameters.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "CommonTools/Utils/interface/TFileDirectory.h"

#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
#include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
#include "DataFormats/GeometryVector/interface/LocalPoint.h"
#include "DataFormats/GeometryVector/interface/GlobalPoint.h"
#include "DataFormats/GeometrySurface/interface/Surface.h"
#include "DataFormats/Math/interface/deltaPhi.h"

#include "Geometry/Records/interface/IdealGeometryRecord.h"
#include "Geometry/Records/interface/TrackerTopologyRcd.h"
#include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
#include "Geometry/Records/interface/PTrackerParametersRcd.h"
#include "Geometry/Records/interface/PTrackerAdditionalParametersPerDetRcd.h"
#include "Geometry/CommonDetUnit/interface/TrackingGeometry.h"
#include "Geometry/CommonDetUnit/interface/GeomDet.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
#include "Geometry/CommonTopologies/interface/StripTopology.h"

#include "Alignment/TrackerAlignment/interface/TrackerTreeVariables.h"
#include "Alignment/TrackerAlignment/interface/AlignableTracker.h"

#include "TTree.h"
//
// class decleration
//

class TrackerTreeGenerator : public edm::one::EDAnalyzer<edm::one::SharedResources> {
public:
  explicit TrackerTreeGenerator(const edm::ParameterSet&);
  ~TrackerTreeGenerator() override = default;

private:
  void beginJob() override;
  void analyze(const edm::Event&, const edm::EventSetup&) override;
  void endJob() override;

  // ----------member data ---------------------------
  const edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
  const edm::ESGetToken<PTrackerParameters, PTrackerParametersRcd> ptpToken_;
  const edm::ESGetToken<PTrackerAdditionalParametersPerDet, PTrackerAdditionalParametersPerDetRcd> ptitpToken_;
  const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;

  const bool createEntryForDoubleSidedModule_;
  std::vector<TrackerTreeVariables> vTkTreeVar_;
  edm::ParameterSet config_;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
TrackerTreeGenerator::TrackerTreeGenerator(const edm::ParameterSet& config)
    : geomDetToken_(esConsumes()),
      ptpToken_(esConsumes()),
      ptitpToken_(esConsumes()),
      topoToken_(esConsumes()),
      createEntryForDoubleSidedModule_(config.getParameter<bool>("createEntryForDoubleSidedModule")),
      config_(config) {
  usesResource(TFileService::kSharedResource);
}

//
// member functions
//

// ------------ method called to for each event  ------------
void TrackerTreeGenerator::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
  // now try to take directly the ideal geometry independent of used geometry in Global Tag
  const GeometricDet* geometricDet = &iSetup.getData(geomDetToken_);
  const PTrackerParameters& ptp = iSetup.getData(ptpToken_);
  const PTrackerAdditionalParametersPerDet* ptitp = &iSetup.getData(ptitpToken_);
  const TrackerTopology* tTopo = &iSetup.getData(topoToken_);

  TrackerGeomBuilderFromGeometricDet trackerBuilder;
  const TrackerGeometry* tkGeom = trackerBuilder.build(geometricDet, ptitp, ptp, tTopo);
  AlignableTracker alignableTracker{tkGeom, tTopo};
  const auto& ns = alignableTracker.trackerNameSpace();

  edm::LogInfo("TrackerTreeGenerator") << "@SUB=TrackerTreeGenerator::analyze"
                                       << "There are " << tkGeom->detIds().size() << " dets and "
                                       << tkGeom->detUnitIds().size() << " detUnits in the Geometry Record";

  if (createEntryForDoubleSidedModule_) {
    edm::LogInfo("TrackerTreeGenerator") << "@SUB=TrackerTreeGenerator::analyze"
                                         << "Create entry for each module AND one entry for virtual "
                                         << "double-sided module in addition";
  } else {
    edm::LogInfo("TrackerTreeGenerator") << "@SUB=TrackerTreeGenerator::analyze"
                                         << "Create one entry for each physical module, do NOT create additional "
                                         << "entry for virtual double-sided module";
  }

  for (const auto& detId : tkGeom->detIds()) {
    const GeomDet& geomDet = *tkGeom->idToDet(detId);
    const Surface& surface = geomDet.surface();

    TrackerTreeVariables tkTreeVar;
    const auto rawId = detId.rawId();
    tkTreeVar.rawId = rawId;
    tkTreeVar.subdetId = detId.subdetId();

    switch (tkTreeVar.subdetId) {
      case PixelSubdetector::PixelBarrel:
        tkTreeVar.layer = tTopo->pxbLayer(detId);
        tkTreeVar.half = ns.tpb().halfBarrelNumber(rawId);
        tkTreeVar.rod = tTopo->pxbLadder(detId);  // ... so, ladder is not per halfBarrel-Layer, but per barrel-layer!
        tkTreeVar.module = tTopo->pxbModule(detId);
        break;
      case PixelSubdetector::PixelEndcap:
        tkTreeVar.layer = tTopo->pxfDisk(detId);
        tkTreeVar.side = tTopo->pxfSide(detId);
        tkTreeVar.half = ns.tpe().halfCylinderNumber(rawId);
        tkTreeVar.blade = tTopo->pxfBlade(detId);
        tkTreeVar.panel = tTopo->pxfPanel(detId);
        tkTreeVar.module = tTopo->pxfModule(detId);
        break;
      case StripSubdetector::TIB:
        tkTreeVar.layer = tTopo->tibLayer(detId);
        tkTreeVar.side = tTopo->tibStringInfo(detId)[0];
        tkTreeVar.half = ns.tib().halfShellNumber(rawId);
        tkTreeVar.rod = tTopo->tibStringInfo(detId)[2];
        tkTreeVar.outerInner = tTopo->tibStringInfo(detId)[1];
        tkTreeVar.module = tTopo->tibModule(detId);
        tkTreeVar.isDoubleSide = tTopo->tibIsDoubleSide(detId);
        tkTreeVar.isRPhi = tTopo->tibIsRPhi(detId);
        tkTreeVar.isStereo = tTopo->tibIsStereo(detId);
        break;
      case StripSubdetector::TID:
        tkTreeVar.layer = tTopo->tidWheel(detId);
        tkTreeVar.side = tTopo->tidSide(detId);
        tkTreeVar.ring = tTopo->tidRing(detId);
        tkTreeVar.outerInner = tTopo->tidModuleInfo(detId)[0];
        tkTreeVar.module = tTopo->tidModuleInfo(detId)[1];
        tkTreeVar.isDoubleSide = tTopo->tidIsDoubleSide(detId);
        tkTreeVar.isRPhi = tTopo->tidIsRPhi(detId);
        tkTreeVar.isStereo = tTopo->tidIsStereo(detId);
        break;
      case StripSubdetector::TOB:
        tkTreeVar.layer = tTopo->tobLayer(detId);
        tkTreeVar.side = tTopo->tobRodInfo(detId)[0];
        tkTreeVar.rod = tTopo->tobRodInfo(detId)[1];
        tkTreeVar.module = tTopo->tobModule(detId);
        tkTreeVar.isDoubleSide = tTopo->tobIsDoubleSide(detId);
        tkTreeVar.isRPhi = tTopo->tobIsRPhi(detId);
        tkTreeVar.isStereo = tTopo->tobIsStereo(detId);
        break;
      case StripSubdetector::TEC:
        tkTreeVar.layer = tTopo->tecWheel(detId);
        tkTreeVar.side = tTopo->tecSide(detId);
        tkTreeVar.ring = tTopo->tecRing(detId);
        tkTreeVar.petal = tTopo->tecPetalInfo(detId)[1];
        tkTreeVar.outerInner = tTopo->tecPetalInfo(detId)[0];
        tkTreeVar.module = tTopo->tecModule(detId);
        tkTreeVar.isDoubleSide = tTopo->tecIsDoubleSide(detId);
        tkTreeVar.isRPhi = tTopo->tecIsRPhi(detId);
        tkTreeVar.isStereo = tTopo->tecIsStereo(detId);
        break;
    }

    LocalPoint lPModule(0., 0., 0.), lUDirection(1., 0., 0.), lVDirection(0., 1., 0.), lWDirection(0., 0., 1.);
    GlobalPoint gPModule = surface.toGlobal(lPModule), gUDirection = surface.toGlobal(lUDirection),
                gVDirection = surface.toGlobal(lVDirection), gWDirection = surface.toGlobal(lWDirection);
    double dR(999.), dPhi(999.), dZ(999.);
    switch (tkTreeVar.subdetId) {
      case PixelSubdetector::PixelBarrel:
      case StripSubdetector::TIB:
      case StripSubdetector::TOB:
        dR = gWDirection.perp() - gPModule.perp();
        dPhi = deltaPhi(gUDirection.barePhi(), gPModule.barePhi());
        dZ = gVDirection.z() - gPModule.z();
        tkTreeVar.uDirection = dPhi > 0. ? 1 : -1;
        tkTreeVar.vDirection = dZ > 0. ? 1 : -1;
        tkTreeVar.wDirection = dR > 0. ? 1 : -1;
        break;
      case PixelSubdetector::PixelEndcap:
        dR = gUDirection.perp() - gPModule.perp();
        dPhi = deltaPhi(gVDirection.barePhi(), gPModule.barePhi());
        dZ = gWDirection.z() - gPModule.z();
        tkTreeVar.uDirection = dR > 0. ? 1 : -1;
        tkTreeVar.vDirection = dPhi > 0. ? 1 : -1;
        tkTreeVar.wDirection = dZ > 0. ? 1 : -1;
        break;
      case StripSubdetector::TID:
      case StripSubdetector::TEC:
        dR = gVDirection.perp() - gPModule.perp();
        dPhi = deltaPhi(gUDirection.barePhi(), gPModule.barePhi());
        dZ = gWDirection.z() - gPModule.z();
        tkTreeVar.uDirection = dPhi > 0. ? 1 : -1;
        tkTreeVar.vDirection = dR > 0. ? 1 : -1;
        tkTreeVar.wDirection = dZ > 0. ? 1 : -1;
        break;
    }
    tkTreeVar.posR = gPModule.perp();
    tkTreeVar.posPhi = gPModule.barePhi();  // = gPModule.barePhi().degrees();
    tkTreeVar.posEta = gPModule.eta();
    tkTreeVar.posX = gPModule.x();
    tkTreeVar.posY = gPModule.y();
    tkTreeVar.posZ = gPModule.z();

    if (auto stripGeomDetUnit = dynamic_cast<const StripGeomDetUnit*>(&geomDet)) {  //is it a single physical module?
      switch (tkTreeVar.subdetId) {
        case StripSubdetector::TIB:
        case StripSubdetector::TOB:
        case StripSubdetector::TID:
        case StripSubdetector::TEC:
          auto& topol = dynamic_cast<const StripTopology&>(stripGeomDetUnit->specificTopology());
          tkTreeVar.nStrips = topol.nstrips();
          break;
      }
    }

    if (!createEntryForDoubleSidedModule_) {
      // do so only for individual modules and not also one entry for the combined doubleSided Module
      if (tkTreeVar.isDoubleSide)
        continue;
    }
    vTkTreeVar_.push_back(tkTreeVar);
  }
}

// ------------ method called once each job just before starting event loop  ------------
void TrackerTreeGenerator::beginJob() {}

// ------------ method called once each job just after ending the event loop  ------------
void TrackerTreeGenerator::endJob() {
  UInt_t rawId(999), subdetId(999), layer(999), side(999), half(999), rod(999), ring(999), petal(999), blade(999),
      panel(999), outerInner(999), module(999), nStrips(999);
  Bool_t isDoubleSide(false), isRPhi(false), isStereo(false);
  Int_t uDirection(999), vDirection(999), wDirection(999);
  Float_t posR(999.F), posPhi(999.F), posEta(999.F), posX(999.F), posY(999.F), posZ(999.F);

  edm::Service<TFileService> fileService;
  TFileDirectory treeDir = fileService->mkdir("TrackerTree");
  auto trackerTree{treeDir.make<TTree>("TrackerTree", "IDs of all modules (ideal geometry)")};
  trackerTree->Branch("RawId", &rawId, "RawId/i");
  trackerTree->Branch("SubdetId", &subdetId, "SubdetId/i");
  trackerTree->Branch("Layer", &layer, "Layer/i");                 // Barrel: Layer, Forward: Disk
  trackerTree->Branch("Side", &side, "Side/i");                    // Rod/Ring in +z or -z
  trackerTree->Branch("Half", &half, "Half/i");                    // PXB: HalfBarrel, PXF: HalfCylinder, TIB: HalfShell
  trackerTree->Branch("Rod", &rod, "Rod/i");                       // Barrel (Ladder or String or Rod)
  trackerTree->Branch("Ring", &ring, "Ring/i");                    // Forward
  trackerTree->Branch("Petal", &petal, "Petal/i");                 // TEC
  trackerTree->Branch("Blade", &blade, "Blade/i");                 // PXF
  trackerTree->Branch("Panel", &panel, "Panel/i");                 // PXF
  trackerTree->Branch("OuterInner", &outerInner, "OuterInner/i");  // front/back String,Ring,Petal
  trackerTree->Branch("Module", &module, "Module/i");              // Module ID
  trackerTree->Branch("NStrips", &nStrips, "NStrips/i");
  trackerTree->Branch("IsDoubleSide", &isDoubleSide, "IsDoubleSide/O");
  trackerTree->Branch("IsRPhi", &isRPhi, "IsRPhi/O");
  trackerTree->Branch("IsStereo", &isStereo, "IsStereo/O");
  trackerTree->Branch("UDirection", &uDirection, "UDirection/I");
  trackerTree->Branch("VDirection", &vDirection, "VDirection/I");
  trackerTree->Branch("WDirection", &wDirection, "WDirection/I");
  trackerTree->Branch("PosR", &posR, "PosR/F");
  trackerTree->Branch("PosPhi", &posPhi, "PosPhi/F");
  trackerTree->Branch("PosEta", &posEta, "PosEta/F");
  trackerTree->Branch("PosX", &posX, "PosX/F");
  trackerTree->Branch("PosY", &posY, "PosY/F");
  trackerTree->Branch("PosZ", &posZ, "PosZ/F");

  for (const auto& iTree : vTkTreeVar_) {
    rawId = iTree.rawId;
    subdetId = iTree.subdetId;
    layer = iTree.layer;
    side = iTree.side;
    half = iTree.half;
    rod = iTree.rod;
    ring = iTree.ring;
    petal = iTree.petal;
    blade = iTree.blade;
    panel = iTree.panel;
    outerInner = iTree.outerInner;
    module = iTree.module;
    nStrips = iTree.nStrips;
    isDoubleSide = iTree.isDoubleSide;
    isRPhi = iTree.isRPhi;
    isStereo = iTree.isStereo;
    uDirection = iTree.uDirection;
    vDirection = iTree.vDirection;
    wDirection = iTree.wDirection;
    posR = iTree.posR;
    posPhi = iTree.posPhi;
    posEta = iTree.posEta;
    posX = iTree.posX;
    posY = iTree.posY;
    posZ = iTree.posZ;

    trackerTree->Fill();
  }
  edm::LogInfo("TrackerTreeGenerator") << "@SUB=TrackerTreeGenerator::endJob"
                                       << "TrackerTree contains " << vTkTreeVar_.size() << " entries overall";
}

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