Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-14 02:33:51

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007 #include "FWCore/Framework/interface/Run.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/Utilities/interface/EDGetToken.h"
0010 
0011 #include "FastSimulation/TrackingRecHitProducer/interface/TrackingRecHitAlgorithm.h"
0012 #include "FastSimulation/TrackingRecHitProducer/interface/TrackingRecHitAlgorithmFactory.h"
0013 #include "FastSimulation/TrackingRecHitProducer/interface/TrackingRecHitPipe.h"
0014 #include "FastSimulation/TrackingRecHitProducer/interface/PixelTemplateSmearerBase.h"
0015 
0016 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0017 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0018 
0019 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0020 
0021 #include "DataFormats/Common/interface/DetSetNew.h"
0022 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0023 
0024 #include "DataFormats/DetId/interface/DetId.h"
0025 #include "DataFormats/Common/interface/RefVector.h"
0026 
0027 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0028 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0029 // Pixel-related stuff:
0030 #include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.h"
0031 #include "CondFormats/SiPixelTransient/interface/SiPixelTemplate.h"
0032 #include "CalibTracker/Records/interface/SiPixelTemplateDBObjectESProducerRcd.h"
0033 
0034 #include "FastSimulation/TrackingRecHitProducer/interface/TrackerDetIdSelector.h"
0035 
0036 #include "DataFormats/TrackerRecHit2D/interface/FastSingleTrackerRecHit.h"
0037 #include "DataFormats/TrackerRecHit2D/interface/FastTrackerRecHitCollection.h"
0038 
0039 #include <map>
0040 #include <memory>
0041 #include <vector>
0042 
0043 class TrackingRecHitProducer : public edm::stream::EDProducer<> {
0044 private:
0045   edm::EDGetTokenT<std::vector<PSimHit>> _simHitToken;
0046   std::vector<std::unique_ptr<TrackingRecHitAlgorithm>> _recHitAlgorithms;
0047   unsigned long long _trackerGeometryCacheID = 0;
0048   unsigned long long _trackerTopologyCacheID = 0;
0049   std::map<unsigned int, TrackingRecHitPipe> _detIdPipes;
0050   void setupDetIdPipes(const edm::EventSetup& eventSetup);
0051   std::vector<SiPixelTemplateStore> _pixelTempStore;  // pixel template storage
0052   const edm::ESGetToken<SiPixelTemplateDBObject, SiPixelTemplateDBObjectESProducerRcd> siPixelTemplateDBObjectESToken_;
0053   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> trackerGeometryESToken_;
0054   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyESToken_;
0055 
0056 public:
0057   TrackingRecHitProducer(const edm::ParameterSet& config);
0058 
0059   void beginRun(edm::Run const&, const edm::EventSetup& eventSetup) override;
0060 
0061   void beginStream(edm::StreamID id) override;
0062 
0063   void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
0064 
0065   void endStream() override;
0066 
0067   ~TrackingRecHitProducer() override;
0068 };
0069 
0070 TrackingRecHitProducer::TrackingRecHitProducer(const edm::ParameterSet& config)
0071     : siPixelTemplateDBObjectESToken_(esConsumes<edm::Transition::BeginRun>()),
0072       trackerGeometryESToken_(esConsumes()),
0073       trackerTopologyESToken_(esConsumes()) {
0074   edm::ConsumesCollector consumeCollector = consumesCollector();
0075   const std::vector<edm::ParameterSet>& pluginConfigs = config.getParameter<std::vector<edm::ParameterSet>>("plugins");
0076 
0077   for (unsigned int iplugin = 0; iplugin < pluginConfigs.size(); ++iplugin) {
0078     const edm::ParameterSet& pluginConfig = pluginConfigs[iplugin];
0079     const std::string pluginType = pluginConfig.getParameter<std::string>("type");
0080     const std::string pluginName = pluginConfig.getParameter<std::string>("name");
0081 
0082     std::unique_ptr<TrackingRecHitAlgorithm> recHitAlgorithm{
0083         TrackingRecHitAlgorithmFactory::get()->tryToCreate(pluginType, pluginName, pluginConfig, consumeCollector)};
0084     if (recHitAlgorithm) {
0085       edm::LogInfo("TrackingRecHitProducer: ")
0086           << "adding plugin type '" << pluginType << "' as '" << pluginName << "'" << std::endl;
0087       _recHitAlgorithms.push_back(std::move(recHitAlgorithm));
0088     } else {
0089       throw cms::Exception("TrackingRecHitAlgorithm plugin not found: ")
0090           << "plugin type = " << pluginType << "\nconfiguration=\n"
0091           << pluginConfig.dump();
0092     }
0093   }
0094 
0095   edm::InputTag simHitTag = config.getParameter<edm::InputTag>("simHits");
0096   _simHitToken = consumes<std::vector<PSimHit>>(simHitTag);
0097 
0098   produces<FastTrackerRecHitCollection>();
0099   produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
0100 }
0101 
0102 TrackingRecHitProducer::~TrackingRecHitProducer() {}
0103 
0104 void TrackingRecHitProducer::beginStream(edm::StreamID id) {
0105   for (auto& algo : _recHitAlgorithms) {
0106     algo->beginStream(id);
0107   }
0108 }
0109 
0110 void TrackingRecHitProducer::beginRun(edm::Run const& run, const edm::EventSetup& eventSetup) {
0111   //--- Since all pixel algorithms (of which there could be several) use the same
0112   //    templateStore, filled out from the same DB Object, we need to it centrally
0113   //    (namely here), and then distribute it to the algorithms.  Note that only
0114   //    the pixel algorithms implement beginRun(), for the strip tracker this defaults
0115   //    to a no-op.
0116 
0117   const SiPixelTemplateDBObject& pixelTemplateDBObject = eventSetup.getData(siPixelTemplateDBObjectESToken_);
0118 
0119   //--- Now that we have the DB object, load the correct templates from the DB.
0120   //    (They are needed for data and full sim MC, so in a production FastSim
0121   //    run, everything should already be in the DB.)
0122   if (!SiPixelTemplate::pushfile(pixelTemplateDBObject, _pixelTempStore)) {
0123     throw cms::Exception("TrackingRecHitProducer:")
0124         << "SiPixel Templates not loaded correctly from the DB object!" << std::endl;
0125   }
0126 
0127   for (auto& algo : _recHitAlgorithms) {
0128     algo->beginRun(run, eventSetup, &pixelTemplateDBObject, _pixelTempStore);
0129   }
0130 }
0131 
0132 void TrackingRecHitProducer::setupDetIdPipes(const edm::EventSetup& eventSetup) {
0133   auto const& trackerGeomRec = eventSetup.get<TrackerDigiGeometryRecord>();
0134   auto const& trackerTopoRec = eventSetup.get<TrackerTopologyRcd>();
0135   if (trackerGeomRec.cacheIdentifier() != _trackerGeometryCacheID or
0136       trackerTopoRec.cacheIdentifier() != _trackerTopologyCacheID) {
0137     _trackerGeometryCacheID = trackerGeomRec.cacheIdentifier();
0138     _trackerTopologyCacheID = trackerTopoRec.cacheIdentifier();
0139 
0140     const TrackerGeometry& trackerGeometry = trackerGeomRec.get(trackerGeometryESToken_);
0141     const TrackerTopology& trackerTopology = trackerTopoRec.get(trackerTopologyESToken_);
0142 
0143     _detIdPipes.clear();
0144 
0145     //build pipes for all detIds
0146     const std::vector<DetId>& detIds = trackerGeometry.detIds();
0147     std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(), 0);
0148 
0149     for (const DetId& detId : detIds) {
0150       TrackerDetIdSelector selector(detId, trackerTopology);
0151 
0152       TrackingRecHitPipe pipe;
0153       for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo) {
0154         auto& algo = _recHitAlgorithms[ialgo];
0155         if (selector.passSelection(algo->getSelectionString())) {
0156           numberOfDetIdsPerAlgorithm[ialgo] += 1;
0157           pipe.addAlgorithm(algo.get());
0158         }
0159       }
0160       if (pipe.size() == 0) {
0161         throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! (" +
0162                              trackerTopology.print(detId) + ")");
0163       }
0164       _detIdPipes[detId.rawId()] = pipe;
0165     }
0166   }
0167 }
0168 
0169 void TrackingRecHitProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup) {
0170   //resetup pipes if new iov
0171   setupDetIdPipes(eventSetup);
0172   //begin event
0173   for (auto& algo : _recHitAlgorithms) {
0174     algo->beginEvent(event, eventSetup);
0175   }
0176 
0177   //build DetId -> PSimHit map
0178   edm::Handle<std::vector<PSimHit>> simHits;
0179   event.getByToken(_simHitToken, simHits);
0180 
0181   auto output_recHits = std::make_unique<FastTrackerRecHitCollection>();
0182   output_recHits->reserve(simHits->size());
0183 
0184   edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd =
0185       event.getRefBeforePut<FastTrackerRecHitCollection>();
0186   auto output_recHitRefs = std::make_unique<FastTrackerRecHitRefCollection>(simHits->size(), FastTrackerRecHitRef());
0187 
0188   std::map<unsigned int, std::vector<std::pair<unsigned int, const PSimHit*>>> simHitsIdPairPerDetId;
0189   for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit) {
0190     const PSimHit* simHit = &(*simHits)[ihit];
0191     simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit, simHit));
0192   }
0193 
0194   for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end();
0195        ++simHitsIdPairIt) {
0196     const DetId& detId = simHitsIdPairIt->first;
0197     std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
0198     if (pipeIt != _detIdPipes.cend()) {
0199       auto& simHitIdPairList = simHitsIdPairIt->second;
0200       const TrackingRecHitPipe& pipe = pipeIt->second;
0201 
0202       TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId, simHitIdPairList);
0203 
0204       product = pipe.produce(product);
0205 
0206       const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList =
0207           product->getRecHitToSimHitIdPairs();
0208 
0209       for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit) {
0210         output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
0211         const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList =
0212             recHitToSimHitIdPairsList[irecHit].second;
0213         double energyLoss_tot = 0;  //
0214         for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit) {
0215           unsigned int simHitId = simHitIdPairList[isimHit].first;
0216           const PSimHit* simHit = simHitIdPairList[isimHit].second;
0217           energyLoss_tot +=
0218               simHit
0219                   ->energyLoss();  //energy loss of sim hit in GeV std::cout << "energyLoss_tot" << energyLoss_tot << std::endl;
0220           if (not(*output_recHitRefs)[simHitId].isNull()) {
0221             throw cms::Exception("FastSimulation/TrackingRecHitProducer",
0222                                  "A PSimHit cannot lead to multiple FastTrackerRecHits");
0223           }
0224           (*output_recHitRefs)[simHitId] = FastTrackerRecHitRef(output_recHits_refProd, output_recHits->size() - 1);
0225         }
0226         static_cast<FastSingleTrackerRecHit&>(output_recHits->back()).setEnergyLoss(energyLoss_tot);
0227       }
0228     } else {
0229       //there should be at least an empty pipe for each DetId
0230       throw cms::Exception(
0231           "FastSimulation/TrackingRecHitProducer",
0232           "A PSimHit carries a DetId which does not belong to the TrackerGeometry: " + std::to_string(detId));
0233     }
0234   }
0235 
0236   //end event
0237   for (auto& algo : _recHitAlgorithms) {
0238     algo->endEvent(event, eventSetup);
0239   }
0240 
0241   // note from lukas:
0242   // all rechits need a unique id numbers
0243   for (unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex) {
0244     ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
0245   }
0246 
0247   event.put(std::move(output_recHits));
0248   event.put(std::move(output_recHitRefs), "simHit2RecHitMap");
0249 }
0250 
0251 void TrackingRecHitProducer::endStream() {
0252   for (auto& algo : _recHitAlgorithms) {
0253     algo->endStream();
0254   }
0255 }
0256 
0257 DEFINE_FWK_MODULE(TrackingRecHitProducer);