Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   const edm::ESGetToken<SiPixelTemplateDBObject, SiPixelTemplateDBObjectESProducerRcd> siPixelTemplateDBObjectESToken_;
0052   const edm::ESGetToken<std::vector<SiPixelTemplateStore>, SiPixelTemplateDBObjectESProducerRcd>
0053       siPixelTemplateStoreESToken_;
0054   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> trackerGeometryESToken_;
0055   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyESToken_;
0056 
0057 public:
0058   TrackingRecHitProducer(const edm::ParameterSet& config);
0059 
0060   void beginRun(edm::Run const&, const edm::EventSetup& eventSetup) override;
0061 
0062   void beginStream(edm::StreamID id) override;
0063 
0064   void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
0065 
0066   void endStream() override;
0067 
0068   ~TrackingRecHitProducer() override;
0069 };
0070 
0071 TrackingRecHitProducer::TrackingRecHitProducer(const edm::ParameterSet& config)
0072     : siPixelTemplateDBObjectESToken_(esConsumes<edm::Transition::BeginRun>()),
0073       siPixelTemplateStoreESToken_(esConsumes<edm::Transition::BeginRun>()),
0074       trackerGeometryESToken_(esConsumes()),
0075       trackerTopologyESToken_(esConsumes()) {
0076   edm::ConsumesCollector consumeCollector = consumesCollector();
0077   const std::vector<edm::ParameterSet>& pluginConfigs = config.getParameter<std::vector<edm::ParameterSet>>("plugins");
0078 
0079   for (unsigned int iplugin = 0; iplugin < pluginConfigs.size(); ++iplugin) {
0080     const edm::ParameterSet& pluginConfig = pluginConfigs[iplugin];
0081     const std::string pluginType = pluginConfig.getParameter<std::string>("type");
0082     const std::string pluginName = pluginConfig.getParameter<std::string>("name");
0083 
0084     std::unique_ptr<TrackingRecHitAlgorithm> recHitAlgorithm{
0085         TrackingRecHitAlgorithmFactory::get()->tryToCreate(pluginType, pluginName, pluginConfig, consumeCollector)};
0086     if (recHitAlgorithm) {
0087       edm::LogInfo("TrackingRecHitProducer: ")
0088           << "adding plugin type '" << pluginType << "' as '" << pluginName << "'" << std::endl;
0089       _recHitAlgorithms.push_back(std::move(recHitAlgorithm));
0090     } else {
0091       throw cms::Exception("TrackingRecHitAlgorithm plugin not found: ")
0092           << "plugin type = " << pluginType << "\nconfiguration=\n"
0093           << pluginConfig.dump();
0094     }
0095   }
0096 
0097   edm::InputTag simHitTag = config.getParameter<edm::InputTag>("simHits");
0098   _simHitToken = consumes<std::vector<PSimHit>>(simHitTag);
0099 
0100   produces<FastTrackerRecHitCollection>();
0101   produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
0102 }
0103 
0104 TrackingRecHitProducer::~TrackingRecHitProducer() {}
0105 
0106 void TrackingRecHitProducer::beginStream(edm::StreamID id) {
0107   for (auto& algo : _recHitAlgorithms) {
0108     algo->beginStream(id);
0109   }
0110 }
0111 
0112 void TrackingRecHitProducer::beginRun(edm::Run const& run, const edm::EventSetup& eventSetup) {
0113   //--- Since all pixel algorithms (of which there could be several) use the same
0114   //    templateStore, filled out from the same DB Object, we need to it centrally
0115   //    (namely here), and then distribute it to the algorithms.  Note that only
0116   //    the pixel algorithms implement beginRun(), for the strip tracker this defaults
0117   //    to a no-op.
0118 
0119   const SiPixelTemplateDBObject& pixelTemplateDBObject = eventSetup.getData(siPixelTemplateDBObjectESToken_);
0120   const auto& pixelTemplateStore = eventSetup.getData(siPixelTemplateStoreESToken_);
0121 
0122   for (auto& algo : _recHitAlgorithms) {
0123     algo->beginRun(run, eventSetup, &pixelTemplateDBObject, pixelTemplateStore);
0124   }
0125 }
0126 
0127 void TrackingRecHitProducer::setupDetIdPipes(const edm::EventSetup& eventSetup) {
0128   auto const& trackerGeomRec = eventSetup.get<TrackerDigiGeometryRecord>();
0129   auto const& trackerTopoRec = eventSetup.get<TrackerTopologyRcd>();
0130   if (trackerGeomRec.cacheIdentifier() != _trackerGeometryCacheID or
0131       trackerTopoRec.cacheIdentifier() != _trackerTopologyCacheID) {
0132     _trackerGeometryCacheID = trackerGeomRec.cacheIdentifier();
0133     _trackerTopologyCacheID = trackerTopoRec.cacheIdentifier();
0134 
0135     const TrackerGeometry& trackerGeometry = trackerGeomRec.get(trackerGeometryESToken_);
0136     const TrackerTopology& trackerTopology = trackerTopoRec.get(trackerTopologyESToken_);
0137 
0138     _detIdPipes.clear();
0139 
0140     //build pipes for all detIds
0141     const std::vector<DetId>& detIds = trackerGeometry.detIds();
0142     std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(), 0);
0143 
0144     for (const DetId& detId : detIds) {
0145       TrackerDetIdSelector selector(detId, trackerTopology);
0146 
0147       TrackingRecHitPipe pipe;
0148       for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo) {
0149         auto& algo = _recHitAlgorithms[ialgo];
0150         if (selector.passSelection(algo->getSelectionString())) {
0151           numberOfDetIdsPerAlgorithm[ialgo] += 1;
0152           pipe.addAlgorithm(algo.get());
0153         }
0154       }
0155       if (pipe.size() == 0) {
0156         throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! (" +
0157                              trackerTopology.print(detId) + ")");
0158       }
0159       _detIdPipes[detId.rawId()] = pipe;
0160     }
0161   }
0162 }
0163 
0164 void TrackingRecHitProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup) {
0165   //resetup pipes if new iov
0166   setupDetIdPipes(eventSetup);
0167   //begin event
0168   for (auto& algo : _recHitAlgorithms) {
0169     algo->beginEvent(event, eventSetup);
0170   }
0171 
0172   //build DetId -> PSimHit map
0173   edm::Handle<std::vector<PSimHit>> simHits;
0174   event.getByToken(_simHitToken, simHits);
0175 
0176   auto output_recHits = std::make_unique<FastTrackerRecHitCollection>();
0177   output_recHits->reserve(simHits->size());
0178 
0179   edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd =
0180       event.getRefBeforePut<FastTrackerRecHitCollection>();
0181   auto output_recHitRefs = std::make_unique<FastTrackerRecHitRefCollection>(simHits->size(), FastTrackerRecHitRef());
0182 
0183   std::map<unsigned int, std::vector<std::pair<unsigned int, const PSimHit*>>> simHitsIdPairPerDetId;
0184   for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit) {
0185     const PSimHit* simHit = &(*simHits)[ihit];
0186     simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit, simHit));
0187   }
0188 
0189   for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end();
0190        ++simHitsIdPairIt) {
0191     const DetId& detId = simHitsIdPairIt->first;
0192     std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
0193     if (pipeIt != _detIdPipes.cend()) {
0194       auto& simHitIdPairList = simHitsIdPairIt->second;
0195       const TrackingRecHitPipe& pipe = pipeIt->second;
0196 
0197       TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId, simHitIdPairList);
0198 
0199       product = pipe.produce(product);
0200 
0201       const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList =
0202           product->getRecHitToSimHitIdPairs();
0203 
0204       for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit) {
0205         output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
0206         const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList =
0207             recHitToSimHitIdPairsList[irecHit].second;
0208         double energyLoss_tot = 0;  //
0209         for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit) {
0210           unsigned int simHitId = simHitIdPairList[isimHit].first;
0211           const PSimHit* simHit = simHitIdPairList[isimHit].second;
0212           energyLoss_tot +=
0213               simHit
0214                   ->energyLoss();  //energy loss of sim hit in GeV std::cout << "energyLoss_tot" << energyLoss_tot << std::endl;
0215           if (not(*output_recHitRefs)[simHitId].isNull()) {
0216             throw cms::Exception("FastSimulation/TrackingRecHitProducer",
0217                                  "A PSimHit cannot lead to multiple FastTrackerRecHits");
0218           }
0219           (*output_recHitRefs)[simHitId] = FastTrackerRecHitRef(output_recHits_refProd, output_recHits->size() - 1);
0220         }
0221         static_cast<FastSingleTrackerRecHit&>(output_recHits->back()).setEnergyLoss(energyLoss_tot);
0222       }
0223     } else {
0224       //there should be at least an empty pipe for each DetId
0225       throw cms::Exception(
0226           "FastSimulation/TrackingRecHitProducer",
0227           "A PSimHit carries a DetId which does not belong to the TrackerGeometry: " + std::to_string(detId));
0228     }
0229   }
0230 
0231   //end event
0232   for (auto& algo : _recHitAlgorithms) {
0233     algo->endEvent(event, eventSetup);
0234   }
0235 
0236   // note from lukas:
0237   // all rechits need a unique id numbers
0238   for (unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex) {
0239     ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
0240   }
0241 
0242   event.put(std::move(output_recHits));
0243   event.put(std::move(output_recHitRefs), "simHit2RecHitMap");
0244 }
0245 
0246 void TrackingRecHitProducer::endStream() {
0247   for (auto& algo : _recHitAlgorithms) {
0248     algo->endStream();
0249   }
0250 }
0251 
0252 DEFINE_FWK_MODULE(TrackingRecHitProducer);