Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:21

0001 //Framework Headers
0002 #include "FWCore/Framework/interface/ConsumesCollector.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Utilities/interface/ESInputTag.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006 
0007 #include "FastSimulation/SimplifiedGeometryPropagator/interface/SimplifiedGeometryFactory.h"
0008 #include "FastSimulation/SimplifiedGeometryPropagator/interface/ForwardSimplifiedGeometry.h"
0009 #include "FastSimulation/SimplifiedGeometryPropagator/interface/BarrelSimplifiedGeometry.h"
0010 
0011 #include "MagneticField/UniformEngine/interface/UniformMagneticField.h"
0012 #include "FastSimulation/SimplifiedGeometryPropagator/interface/Geometry.h"
0013 
0014 #include <iostream>
0015 #include <map>
0016 #include <memory>
0017 
0018 using namespace fastsim;
0019 
0020 Geometry::~Geometry() { ; }
0021 
0022 Geometry::Geometry(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0023     : cacheIdentifierTrackerRecoGeometry_(0),
0024       cacheIdentifierIdealMagneticField_(0),
0025       geometricSearchTracker_(nullptr),
0026       magneticField_(nullptr),
0027       useFixedMagneticFieldZ_(cfg.exists("magneticFieldZ")),
0028       fixedMagneticFieldZ_(cfg.getUntrackedParameter<double>("magneticFieldZ", 0.)),
0029       useTrackerRecoGeometryRecord_(cfg.getUntrackedParameter<bool>("useTrackerRecoGeometryRecord", true)),
0030       trackerAlignmentLabel_(cfg.getUntrackedParameter<std::string>("trackerAlignmentLabel", "")),
0031       barrelLayerCfg_(cfg.getParameter<std::vector<edm::ParameterSet>>("BarrelLayers")),
0032       forwardLayerCfg_(cfg.getParameter<std::vector<edm::ParameterSet>>("EndcapLayers")),
0033       maxRadius_(cfg.getUntrackedParameter<double>("maxRadius", 500.)),
0034       maxZ_(cfg.getUntrackedParameter<double>("maxZ", 1200.)),
0035       barrelBoundary_(cfg.exists("trackerBarrelBoundary"))  // Hack to interface "old" calo to "new" tracking
0036       ,
0037       forwardBoundary_(cfg.exists("trackerForwardBoundary"))  // Hack to interface "old" calo to "new" tracking
0038       ,
0039       trackerBarrelBoundaryCfg_(barrelBoundary_
0040                                     ? cfg.getParameter<edm::ParameterSet>("trackerBarrelBoundary")
0041                                     : edm::ParameterSet())  // Hack to interface "old" calo to "new" tracking
0042       ,
0043       trackerForwardBoundaryCfg_(forwardBoundary_
0044                                      ? cfg.getParameter<edm::ParameterSet>("trackerForwardBoundary")
0045                                      : edm::ParameterSet())  // Hack to interface "old" calo to "new" tracking
0046 {
0047   if (useTrackerRecoGeometryRecord_) {
0048     geometricSearchTrackerESToken_ = iC.esConsumes(edm::ESInputTag("", trackerAlignmentLabel_));
0049   }
0050   if (!useFixedMagneticFieldZ_) {
0051     magneticFieldESToken_ = iC.esConsumes();
0052   }
0053 }
0054 
0055 void Geometry::update(const edm::EventSetup& iSetup,
0056                       const std::map<std::string, fastsim::InteractionModel*>& interactionModelMap) {
0057   if (iSetup.get<TrackerRecoGeometryRecord>().cacheIdentifier() == cacheIdentifierTrackerRecoGeometry_ &&
0058       iSetup.get<IdealMagneticFieldRecord>().cacheIdentifier() == cacheIdentifierIdealMagneticField_) {
0059     return;
0060   }
0061 
0062   //----------------
0063   // find tracker reconstruction geometry
0064   //----------------
0065   if (iSetup.get<TrackerRecoGeometryRecord>().cacheIdentifier() != cacheIdentifierTrackerRecoGeometry_) {
0066     if (useTrackerRecoGeometryRecord_) {
0067       geometricSearchTracker_ = &iSetup.getData(geometricSearchTrackerESToken_);
0068     }
0069   }
0070 
0071   //----------------
0072   // update magnetic field
0073   //----------------
0074   if (iSetup.get<IdealMagneticFieldRecord>().cacheIdentifier() != cacheIdentifierIdealMagneticField_) {
0075     if (useFixedMagneticFieldZ_)  // use constant magnetic field
0076     {
0077       ownedMagneticField_ = std::make_unique<UniformMagneticField>(fixedMagneticFieldZ_);
0078       magneticField_ = ownedMagneticField_.get();
0079     } else  // get magnetic field from EventSetup
0080     {
0081       magneticField_ = &iSetup.getData(magneticFieldESToken_);
0082     }
0083   }
0084 
0085   //---------------
0086   // layer factory
0087   //---------------
0088   SimplifiedGeometryFactory simplifiedGeometryFactory(
0089       geometricSearchTracker_, *magneticField_, interactionModelMap, maxRadius_, maxZ_);
0090 
0091   //---------------
0092   // update barrel layers
0093   //---------------
0094   barrelLayers_.clear();
0095   for (const edm::ParameterSet& layerCfg : barrelLayerCfg_) {
0096     barrelLayers_.push_back(simplifiedGeometryFactory.createBarrelSimplifiedGeometry(layerCfg));
0097   }
0098 
0099   // Hack to interface "old" calo to "new" tracking
0100   if (barrelBoundary_) {
0101     barrelLayers_.push_back(simplifiedGeometryFactory.createBarrelSimplifiedGeometry(trackerBarrelBoundaryCfg_));
0102     barrelLayers_.back()->setCaloType(SimplifiedGeometry::TRACKERBOUNDARY);
0103   }
0104 
0105   for (unsigned index = 0; index < barrelLayers_.size(); index++) {
0106     // set index
0107     barrelLayers_[index]->setIndex(index);
0108     // check order
0109     if (index > 0) {
0110       if (barrelLayers_[index]->getRadius() <= barrelLayers_[index - 1]->getRadius()) {
0111         throw cms::Exception("fastsim::Geometry")
0112             << "barrel layers must be ordered according to increading radius"
0113             << "\nbarrel layer " << index << " has radius smaller than or equal to radius of barrel layer " << index - 1
0114             << " (" << barrelLayers_[index]->getRadius() << "/" << barrelLayers_[index - 1]->getRadius() << ")";
0115       }
0116     }
0117   }
0118 
0119   //--------------
0120   // update forward layers
0121   //--------------
0122   forwardLayers_.clear();
0123   for (const edm::ParameterSet& layerCfg : forwardLayerCfg_) {
0124     forwardLayers_.push_back(simplifiedGeometryFactory.createForwardSimplifiedGeometry(
0125         fastsim::SimplifiedGeometryFactory::POSFWD, layerCfg));
0126     forwardLayers_.insert(forwardLayers_.begin(),
0127                           simplifiedGeometryFactory.createForwardSimplifiedGeometry(
0128                               fastsim::SimplifiedGeometryFactory::NEGFWD, layerCfg));
0129   }
0130 
0131   // Hack to interface "old" calo to "new" tracking
0132   if (forwardBoundary_) {
0133     forwardLayers_.push_back(simplifiedGeometryFactory.createForwardSimplifiedGeometry(
0134         fastsim::SimplifiedGeometryFactory::POSFWD, trackerForwardBoundaryCfg_));
0135     forwardLayers_.back()->setCaloType(SimplifiedGeometry::TRACKERBOUNDARY);
0136     forwardLayers_.insert(forwardLayers_.begin(),
0137                           simplifiedGeometryFactory.createForwardSimplifiedGeometry(
0138                               fastsim::SimplifiedGeometryFactory::NEGFWD, trackerForwardBoundaryCfg_));
0139     forwardLayers_.front()->setCaloType(SimplifiedGeometry::TRACKERBOUNDARY);
0140   }
0141 
0142   for (unsigned index = 0; index < forwardLayers_.size(); index++) {
0143     // set index
0144     forwardLayers_[index]->setIndex(index);
0145     // check order
0146     if (index > 0) {
0147       if (forwardLayers_[index]->getZ() <= forwardLayers_[index - 1]->getZ()) {
0148         throw cms::Exception("fastsim::Geometry")
0149             << "forward layers must be ordered according to increasing z"
0150             << "forward layer " << index << " has z smaller than or equal to z of forward layer " << index - 1;
0151       }
0152     }
0153   }
0154 }
0155 
0156 double fastsim::Geometry::getMagneticFieldZ(const math::XYZTLorentzVector& position) const {
0157   return magneticField_->inTesla(GlobalPoint(position.X(), position.Y(), position.Z())).z();
0158 }
0159 
0160 std::ostream& fastsim::operator<<(std::ostream& os, const fastsim::Geometry& geometry) {
0161   os << "-----------"
0162      << "\n# fastsim::Geometry"
0163      << "\n## BarrelLayers:";
0164   for (const auto& layer : geometry.barrelLayers_) {
0165     os << "\n   " << *layer << layer->getInteractionModels().size() << " interaction models";
0166   }
0167   os << "\n## ForwardLayers:";
0168   for (const auto& layer : geometry.forwardLayers_) {
0169     os << "\n   " << *layer << layer->getInteractionModels().size() << " interaction models";
0170   }
0171   os << "\n-----------";
0172   return os;
0173 }