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
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/TrackerGeometryBuilder/interface/PlaneBuilderForGluedDet.h"
#include "Geometry/CommonDetUnit/interface/GeomDet.h"
#include "Geometry/CommonDetUnit/interface/GluedGeomDet.h"
#include "Geometry/CommonDetUnit/interface/StackGeomDet.h"
#include "Geometry/CommonDetUnit/interface/PixelGeomDetType.h"
#include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetType.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
#include "Geometry/TrackerGeometryBuilder/interface/PixelTopologyBuilder.h"
#include "Geometry/TrackerGeometryBuilder/interface/PixelPhase2TopologyBuilder.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripTopologyBuilder.h"
#include "CondFormats/GeometryObjects/interface/PTrackerParameters.h"
#include "CondFormats/GeometryObjects/interface/PTrackerAdditionalParametersPerDet.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
#include "DataFormats/GeometrySurface/interface/MediumProperties.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"

#include <cfloat>
#include <cassert>
using std::string;
using std::vector;

namespace {
  void verifyDUinTG(TrackerGeometry const& tg) {
    int off = 0;
    int end = 0;
    for (int i = 1; i != 7; i++) {
      auto det = GeomDetEnumerators::tkDetEnum[i];
      off = tg.offsetDU(det);
      end = tg.endsetDU(det);
      assert(end >= off);  // allow empty subdetectors. Needed for upgrade
      for (int j = off; j != end; ++j) {
        assert(tg.detUnits()[j]->geographicalId().subdetId() == i);
        assert(GeomDetEnumerators::subDetGeom[tg.detUnits()[j]->subDetector()] == det);
        assert(tg.detUnits()[j]->index() == j);
      }
    }
  }
}  // namespace

TrackerGeometry* TrackerGeomBuilderFromGeometricDet::build(const GeometricDet* gd,
                                                           const PTrackerAdditionalParametersPerDet* ptitpx,
                                                           const PTrackerParameters& ptp,
                                                           const TrackerTopology* tTopo) {
  if (ptp.vpars.size() != 6) {
    throw cms::Exception("TrackerGeomBuilderFromGeometricDet")
        << "Tracker parameters block from XMLs called vPars is expected to have 6 entries, but has " << ptp.vpars.size()
        << " entrie(s).";
  }

  // phase-0, phase-1; BIG_PIX_PER_ROC from trackerParameters.xml
  // phase-2: BIG_PIX_PER_ROC from pixelStructureTopology.xml (redefined later in buildPixel() )
  const int BIG_PIX_PER_ROC_X = ptp.vpars[2];
  const int BIG_PIX_PER_ROC_Y = ptp.vpars[3];

  thePixelDetTypeMap.clear();
  theStripDetTypeMap.clear();

  TrackerGeometry* tracker = new TrackerGeometry(gd);
  std::vector<const GeometricDet*> comp;
  gd->deepComponents(comp);

  if (tTopo)
    theTopo = tTopo;

  //define a vector which associate to the detid subdetector index -1 (from 0 to 5) the GeometridDet enumerator to be able to know which type of subdetector it is

  std::vector<GeometricDet::GDEnumType> gdsubdetmap(
      6, GeometricDet::unknown);  // hardcoded "6" should not be a surprise...
  GeometricDet::ConstGeometricDetContainer subdetgd = gd->components();

  LogDebug("SubDetectorGeometricDetType") << "GeometriDet enumerator values of the subdetectors";
  for (unsigned int i = 0; i < subdetgd.size(); ++i) {
    assert(subdetgd[i]->geographicalId().subdetId() > 0 && subdetgd[i]->geographicalId().subdetId() < 7);
    gdsubdetmap[subdetgd[i]->geographicalId().subdetId() - 1] = subdetgd[i]->type();
    LogTrace("SubDetectorGeometricDetType")
        << "subdet " << i << " type " << subdetgd[i]->type() << " detid " << subdetgd[i]->geographicalId().rawId()
        << " subdetid " << subdetgd[i]->geographicalId().subdetId();
  }

  std::vector<const GeometricDet*> dets[6];
  std::vector<const GeometricDet*>& pixB = dets[0];
  pixB.reserve(comp.size());
  std::vector<const GeometricDet*>& pixF = dets[1];
  pixF.reserve(comp.size());
  std::vector<const GeometricDet*>& tib = dets[2];
  tib.reserve(comp.size());
  std::vector<const GeometricDet*>& tid = dets[3];
  tid.reserve(comp.size());
  std::vector<const GeometricDet*>& tob = dets[4];
  tob.reserve(comp.size());
  std::vector<const GeometricDet*>& tec = dets[5];
  tec.reserve(comp.size());

  for (auto& i : comp)
    dets[i->geographicalId().subdetId() - 1].emplace_back(i);

  //loop on all the six elements of dets and firstly check if they are from pixel-like detector and call buildPixel, then loop again and check if they are strip and call buildSilicon. "unknown" can be filled either way but the vector of GeometricDet must be empty !!
  // this order is VERY IMPORTANT!!!!! For the moment I (AndreaV) understand that some pieces of code rely on pixel-like being before strip-like

  // now building the Pixel-like subdetectors
  for (unsigned int i = 0; i < 6; ++i) {
    if (gdsubdetmap[i] == GeometricDet::PixelBarrel)
      buildPixel(
          dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::PixelBarrel, BIG_PIX_PER_ROC_X, BIG_PIX_PER_ROC_Y);
    if (gdsubdetmap[i] == GeometricDet::PixelPhase1Barrel)
      buildPixel(
          dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P1PXB, BIG_PIX_PER_ROC_X, BIG_PIX_PER_ROC_Y);
    if (gdsubdetmap[i] == GeometricDet::PixelEndCap)
      buildPixel(
          dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::PixelEndcap, BIG_PIX_PER_ROC_X, BIG_PIX_PER_ROC_Y);
    if (gdsubdetmap[i] == GeometricDet::PixelPhase1EndCap)
      buildPixel(
          dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P1PXEC, BIG_PIX_PER_ROC_X, BIG_PIX_PER_ROC_Y);
    // Phase2 case
    if (gdsubdetmap[i] == GeometricDet::PixelPhase2Barrel)
      buildPixelPhase2(dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P2PXB);
    if (gdsubdetmap[i] == GeometricDet::PixelPhase2EndCap)
      buildPixelPhase2(dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P2PXEC);
    if (gdsubdetmap[i] == GeometricDet::OTPhase2Barrel)
      buildPixelPhase2(dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P2OTB);
    if (gdsubdetmap[i] == GeometricDet::OTPhase2EndCap)
      buildPixelPhase2(dets[i], ptitpx, tracker, GeomDetEnumerators::SubDetector::P2OTEC);
  }
  //now building Strips
  for (unsigned int i = 0; i < 6; ++i) {
    if (gdsubdetmap[i] == GeometricDet::TIB)
      buildSilicon(dets[i], tracker, GeomDetEnumerators::SubDetector::TIB, "barrel");
    if (gdsubdetmap[i] == GeometricDet::TID)
      buildSilicon(dets[i], tracker, GeomDetEnumerators::SubDetector::TID, "endcap");
    if (gdsubdetmap[i] == GeometricDet::TOB)
      buildSilicon(dets[i], tracker, GeomDetEnumerators::SubDetector::TOB, "barrel");
    if (gdsubdetmap[i] == GeometricDet::TEC)
      buildSilicon(dets[i], tracker, GeomDetEnumerators::SubDetector::TEC, "endcap");
  }
  // and finally the "empty" subdetectors (maybe it is not needed)
  for (unsigned int i = 0; i < 6; ++i) {
    if (gdsubdetmap[i] == GeometricDet::unknown) {
      if (!dets[i].empty())
        throw cms::Exception("NotEmptyUnknownSubDet")
            << "Subdetector " << i + 1 << " is unknown but it is not empty: " << dets[i].size();
      buildSilicon(
          dets[i], tracker, GeomDetEnumerators::tkDetEnum[i + 1], "barrel");  // "barrel" is used but it is irrelevant
    }
  }
  buildGeomDet(tracker);  //"GeomDet"

  verifyDUinTG(*tracker);

  return tracker;
}

void TrackerGeomBuilderFromGeometricDet::buildPixel(std::vector<const GeometricDet*> const& gdv,
                                                    const PTrackerAdditionalParametersPerDet* const& ptitp,
                                                    TrackerGeometry* tracker,
                                                    GeomDetType::SubDetector det,
                                                    int BIG_PIX_PER_ROC_X,  // in x direction, rows.
                                                    int BIG_PIX_PER_ROC_Y)  // in y direction, cols.
{
  LogDebug("BuildingGeomDetUnits") << " Pixel type. Size of vector: " << gdv.size()
                                   << " GeomDetType subdetector: " << det
                                   << " logical subdetector: " << GeomDetEnumerators::subDetGeom[det]
                                   << " big pix per ROC x: " << BIG_PIX_PER_ROC_X << " y: " << BIG_PIX_PER_ROC_Y;

  tracker->setOffsetDU(GeomDetEnumerators::subDetGeom[det]);

  for (auto const& i : gdv) {
    std::string const& detName = i->name();
    if (thePixelDetTypeMap.find(detName) == thePixelDetTypeMap.end()) {
      std::unique_ptr<const Bounds> bounds(i->bounds());
      PixelTopology* t = PixelTopologyBuilder().build(bounds.get(),
                                                      (int)i->pixROCRows(),
                                                      (int)i->pixROCCols(),
                                                      BIG_PIX_PER_ROC_X,
                                                      BIG_PIX_PER_ROC_Y,
                                                      (int)i->pixROCx(),
                                                      (int)i->pixROCy());

      thePixelDetTypeMap[detName] = new PixelGeomDetType(t, detName, det);
      tracker->addType(thePixelDetTypeMap[detName]);
    }

    PlaneBuilderFromGeometricDet::ResultType plane = buildPlaneWithMaterial(i);
    GeomDetUnit* temp = new PixelGeomDetUnit(&(*plane), thePixelDetTypeMap[detName], i->geographicalId());

    tracker->addDetUnit(temp);
    tracker->addDetUnitId(i->geographicalId());
  }
  tracker->setEndsetDU(GeomDetEnumerators::subDetGeom[det]);
}

void TrackerGeomBuilderFromGeometricDet::buildPixelPhase2(std::vector<const GeometricDet*> const& gdv,
                                                          const PTrackerAdditionalParametersPerDet* const& ptitp,
                                                          TrackerGeometry* tracker,
                                                          GeomDetType::SubDetector det) {
  LogDebug("BuildingGeomDetUnits") << " Phase2 Pixel type. Size of vector: " << gdv.size()
                                   << " GeomDetType subdetector: " << det
                                   << " logical subdetector: " << GeomDetEnumerators::subDetGeom[det];

  tracker->setOffsetDU(GeomDetEnumerators::subDetGeom[det]);

  for (auto const& i : gdv) {
    std::string const& detName = i->name();
    if (thePixelDetTypeMap.find(detName) == thePixelDetTypeMap.end()) {
      std::unique_ptr<const Bounds> bounds(i->bounds());
      int BIG_PIX_PER_ROC_X = i->bigPixelsx();       // in x direction, rows
      int BIG_PIX_PER_ROC_Y = i->bigPixelsy();       // in y direction, cols
      float BIG_PIX_PITCH_X = i->bigPixelsPitchx();  // in x direction, rows
      float BIG_PIX_PITCH_Y = i->bigPixelsPitchy();  // in y direction, cols
      PixelTopology* t = PixelPhase2TopologyBuilder().build(bounds.get(),
                                                            (int)i->pixROCRows(),
                                                            (int)i->pixROCCols(),
                                                            BIG_PIX_PER_ROC_X,
                                                            BIG_PIX_PER_ROC_Y,
                                                            BIG_PIX_PITCH_X,
                                                            BIG_PIX_PITCH_Y,
                                                            (int)i->pixROCx(),
                                                            (int)i->pixROCy());

      thePixelDetTypeMap[detName] = new PixelGeomDetType(t, detName, det);
      tracker->addType(thePixelDetTypeMap[detName]);
    }

    PlaneBuilderFromGeometricDet::ResultType plane = buildPlaneWithMaterial(i);
    GeomDetUnit* temp = new PixelGeomDetUnit(&(*plane), thePixelDetTypeMap[detName], i->geographicalId());

    tracker->addDetUnit(temp);
    tracker->addDetUnitId(i->geographicalId());
  }
  tracker->setEndsetDU(GeomDetEnumerators::subDetGeom[det]);
}

void TrackerGeomBuilderFromGeometricDet::buildSilicon(std::vector<const GeometricDet*> const& gdv,
                                                      TrackerGeometry* tracker,
                                                      GeomDetType::SubDetector det,
                                                      const std::string& part) {
  LogDebug("BuildingGeomDetUnits") << " Strip type. Size of vector: " << gdv.size()
                                   << " GeomDetType subdetector: " << det
                                   << " logical subdetector: " << GeomDetEnumerators::subDetGeom[det] << " part "
                                   << part;

  tracker->setOffsetDU(GeomDetEnumerators::subDetGeom[det]);

  for (auto const& i : gdv) {
    std::string const& detName = i->name();
    if (theStripDetTypeMap.find(detName) == theStripDetTypeMap.end()) {
      std::unique_ptr<const Bounds> bounds(i->bounds());
      StripTopology* t = StripTopologyBuilder().build(bounds.get(), i->siliconAPVNum(), part);
      theStripDetTypeMap[detName] = new StripGeomDetType(t, detName, det, i->stereo());
      tracker->addType(theStripDetTypeMap[detName]);
    }

    double scale = (theTopo->partnerDetId(i->geographicalId())) ? 0.5 : 1.0;

    PlaneBuilderFromGeometricDet::ResultType plane = buildPlaneWithMaterial(i, scale);
    GeomDetUnit* temp = new StripGeomDetUnit(&(*plane), theStripDetTypeMap[detName], i->geographicalId());

    tracker->addDetUnit(temp);
    tracker->addDetUnitId(i->geographicalId());
  }
  tracker->setEndsetDU(GeomDetEnumerators::subDetGeom[det]);
}

void TrackerGeomBuilderFromGeometricDet::buildGeomDet(TrackerGeometry* tracker) {
  PlaneBuilderForGluedDet gluedplaneBuilder;
  auto const& gdu = tracker->detUnits();
  auto const& gduId = tracker->detUnitIds();

  for (u_int32_t i = 0; i < gdu.size(); i++) {
    tracker->addDet(gdu[i]);
    tracker->addDetId(gduId[i]);
    string gduTypeName = gdu[i]->type().name();

    //this step is time consuming >> TO FIX with a MAP?
    if ((gduTypeName.find("Ster") != std::string::npos || gduTypeName.find("Lower") != std::string::npos ||
         gduTypeName.find("One") != std::string::npos) &&
        (theTopo->glued(gduId[i]) != 0 || theTopo->stack(gduId[i]) != 0 || theTopo->doubleSensor(gduId[i]))) {
      int partner_pos = -1;
      for (u_int32_t jj = 0; jj < gduId.size(); jj++) {
        if (theTopo->partnerDetId(gduId[i]) == gduId[jj]) {
          partner_pos = jj;
          break;
        }
      }
      if (partner_pos == -1) {
        throw cms::Exception("Configuration") << "Module Type is Stereo or Lower but no partner detector found \n"
                                              << "There is a problem on Tracker geometry configuration\n";
      }

      const GeomDetUnit* dus = gdu[i];
      const GeomDetUnit* dum = gdu[partner_pos];
      std::vector<const GeomDetUnit*> composed(2);
      composed[0] = dum;
      composed[1] = dus;
      DetId composedDetId;
      if (gduTypeName.find("Ster") != std::string::npos) {
        PlaneBuilderForGluedDet::ResultType plane = gluedplaneBuilder.plane(composed);
        composedDetId = theTopo->glued(gduId[i]);
        GluedGeomDet* gluedDet = new GluedGeomDet(&(*plane), dum, dus, composedDetId);
        tracker->addDet((GeomDet*)gluedDet);
        tracker->addDetId(composedDetId);

      } else if (gduTypeName.find("Lower") != std::string::npos) {
        //The plane is *not* built in the middle, but on the Lower surface
        Plane* plane = new Plane(dus->surface());
        composedDetId = theTopo->stack(gduId[i]);
        StackGeomDet* stackDet = new StackGeomDet(&(*plane), dus, dum, composedDetId);
        tracker->addDet((GeomDet*)stackDet);
        tracker->addDetId(composedDetId);
      }
    }
  }
}

PlaneBuilderFromGeometricDet::ResultType TrackerGeomBuilderFromGeometricDet::buildPlaneWithMaterial(
    const GeometricDet* gd, double scale) const {
  PlaneBuilderFromGeometricDet planeBuilder;
  PlaneBuilderFromGeometricDet::ResultType plane = planeBuilder.plane(gd);
  //
  // set medium properties (if defined)
  //
  plane->setMediumProperties(MediumProperties(gd->radLength() * scale, gd->xi() * scale));

  return plane;
}