Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-26 03:56:14

0001 // Author: Felice Pantaleo,Marco Rovere - felice.pantaleo@cern.ch,marco.rovere@cern.ch
0002 // Date: 09/2018
0003 
0004 // user include files
0005 #include <vector>
0006 
0007 #include "FWCore/Framework/interface/ESHandle.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/Frameworkfwd.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/stream/EDProducer.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0015 #include "FWCore/ParameterSet/interface/PluginDescription.h"
0016 
0017 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
0018 #include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
0019 
0020 #include "DataFormats/HGCalReco/interface/Trackster.h"
0021 #include "DataFormats/HGCalReco/interface/TICLLayerTile.h"
0022 #include "DataFormats/HGCalReco/interface/TICLSeedingRegion.h"
0023 
0024 #include "RecoHGCal/TICL/plugins/PatternRecognitionPluginFactory.h"
0025 #include "PatternRecognitionbyCA.h"
0026 #include "PatternRecognitionbyMultiClusters.h"
0027 
0028 #include "PhysicsTools/TensorFlow/interface/TfGraphRecord.h"
0029 #include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
0030 #include "PhysicsTools/TensorFlow/interface/TfGraphDefWrapper.h"
0031 
0032 using namespace ticl;
0033 
0034 class TrackstersProducer : public edm::stream::EDProducer<> {
0035 public:
0036   explicit TrackstersProducer(const edm::ParameterSet&);
0037   ~TrackstersProducer() override {}
0038   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0039 
0040   void produce(edm::Event&, const edm::EventSetup&) override;
0041 
0042   // static methods for handling the global cache
0043   static std::unique_ptr<TrackstersCache> initializeGlobalCache(const edm::ParameterSet&);
0044   static void globalEndJob(TrackstersCache*);
0045 
0046 private:
0047   std::string detector_;
0048   bool doNose_;
0049   const std::string tfDnnLabel_;
0050   const edm::ESGetToken<TfGraphDefWrapper, TfGraphRecord> tfDnnToken_;
0051   const tensorflow::Session* tfSession_;
0052   std::unique_ptr<PatternRecognitionAlgoBaseT<TICLLayerTiles>> myAlgo_;
0053   std::unique_ptr<PatternRecognitionAlgoBaseT<TICLLayerTilesHFNose>> myAlgoHFNose_;
0054 
0055   const edm::EDGetTokenT<std::vector<reco::CaloCluster>> clusters_token_;
0056   const edm::EDGetTokenT<std::vector<float>> filtered_layerclusters_mask_token_;
0057   const edm::EDGetTokenT<std::vector<float>> original_layerclusters_mask_token_;
0058   const edm::EDGetTokenT<edm::ValueMap<std::pair<float, float>>> clustersTime_token_;
0059   edm::EDGetTokenT<TICLLayerTiles> layer_clusters_tiles_token_;
0060   edm::EDGetTokenT<TICLLayerTilesHFNose> layer_clusters_tiles_hfnose_token_;
0061   const edm::EDGetTokenT<std::vector<TICLSeedingRegion>> seeding_regions_token_;
0062   const std::string itername_;
0063   ticl::Trackster::IterationIndex iterIndex_ = ticl::Trackster::IterationIndex(0);
0064 };
0065 DEFINE_FWK_MODULE(TrackstersProducer);
0066 
0067 TrackstersProducer::TrackstersProducer(const edm::ParameterSet& ps)
0068     : detector_(ps.getParameter<std::string>("detector")),
0069       doNose_(detector_ == "HFNose"),
0070       tfDnnLabel_(ps.getParameter<std::string>("tfDnnLabel")),
0071       tfDnnToken_(esConsumes(edm::ESInputTag("", tfDnnLabel_))),
0072       tfSession_(nullptr),
0073       clusters_token_(consumes<std::vector<reco::CaloCluster>>(ps.getParameter<edm::InputTag>("layer_clusters"))),
0074       filtered_layerclusters_mask_token_(consumes<std::vector<float>>(ps.getParameter<edm::InputTag>("filtered_mask"))),
0075       original_layerclusters_mask_token_(consumes<std::vector<float>>(ps.getParameter<edm::InputTag>("original_mask"))),
0076       clustersTime_token_(
0077           consumes<edm::ValueMap<std::pair<float, float>>>(ps.getParameter<edm::InputTag>("time_layerclusters"))),
0078       seeding_regions_token_(
0079           consumes<std::vector<TICLSeedingRegion>>(ps.getParameter<edm::InputTag>("seeding_regions"))),
0080       itername_(ps.getParameter<std::string>("itername")) {
0081   auto plugin = ps.getParameter<std::string>("patternRecognitionBy");
0082   auto pluginPSet = ps.getParameter<edm::ParameterSet>("pluginPatternRecognitionBy" + plugin);
0083   if (doNose_) {
0084     myAlgoHFNose_ = PatternRecognitionHFNoseFactory::get()->create(
0085         ps.getParameter<std::string>("patternRecognitionBy"), pluginPSet, consumesCollector());
0086     layer_clusters_tiles_hfnose_token_ =
0087         consumes<TICLLayerTilesHFNose>(ps.getParameter<edm::InputTag>("layer_clusters_hfnose_tiles"));
0088   } else {
0089     myAlgo_ = PatternRecognitionFactory::get()->create(
0090         ps.getParameter<std::string>("patternRecognitionBy"), pluginPSet, consumesCollector());
0091     layer_clusters_tiles_token_ = consumes<TICLLayerTiles>(ps.getParameter<edm::InputTag>("layer_clusters_tiles"));
0092   }
0093 
0094   if (itername_ == "TrkEM")
0095     iterIndex_ = ticl::Trackster::TRKEM;
0096   else if (itername_ == "EM")
0097     iterIndex_ = ticl::Trackster::EM;
0098   else if (itername_ == "Trk")
0099     iterIndex_ = ticl::Trackster::TRKHAD;
0100   else if (itername_ == "HAD")
0101     iterIndex_ = ticl::Trackster::HAD;
0102   else if (itername_ == "MIP")
0103     iterIndex_ = ticl::Trackster::MIP;
0104 
0105   produces<std::vector<Trackster>>();
0106   produces<std::vector<float>>();  // Mask to be applied at the next iteration
0107 }
0108 
0109 void TrackstersProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0110   // hgcalMultiClusters
0111   edm::ParameterSetDescription desc;
0112   desc.add<std::string>("detector", "HGCAL");
0113   desc.add<edm::InputTag>("layer_clusters", edm::InputTag("hgcalLayerClusters"));
0114   desc.add<edm::InputTag>("filtered_mask", edm::InputTag("filteredLayerClusters", "iterationLabelGoesHere"));
0115   desc.add<edm::InputTag>("original_mask", edm::InputTag("hgcalLayerClusters", "InitialLayerClustersMask"));
0116   desc.add<edm::InputTag>("time_layerclusters", edm::InputTag("hgcalLayerClusters", "timeLayerCluster"));
0117   desc.add<edm::InputTag>("layer_clusters_tiles", edm::InputTag("ticlLayerTileProducer"));
0118   desc.add<edm::InputTag>("layer_clusters_hfnose_tiles", edm::InputTag("ticlLayerTileHFNose"));
0119   desc.add<edm::InputTag>("seeding_regions", edm::InputTag("ticlSeedingRegionProducer"));
0120   desc.add<std::string>("patternRecognitionBy", "CA");
0121   desc.add<std::string>("itername", "unknown");
0122   desc.add<std::string>("tfDnnLabel", "tracksterSelectionTf");
0123 
0124   // CA Plugin
0125   edm::ParameterSetDescription pluginDesc;
0126   pluginDesc.addNode(edm::PluginDescription<PatternRecognitionFactory>("type", "CA", true));
0127   desc.add<edm::ParameterSetDescription>("pluginPatternRecognitionByCA", pluginDesc);
0128   //
0129   // CLUE3D Plugin
0130   edm::ParameterSetDescription pluginDescClue3D;
0131   pluginDescClue3D.addNode(edm::PluginDescription<PatternRecognitionFactory>("type", "CLUE3D", true));
0132   desc.add<edm::ParameterSetDescription>("pluginPatternRecognitionByCLUE3D", pluginDescClue3D);
0133 
0134   // FastJet Plugin
0135   edm::ParameterSetDescription pluginDescFastJet;
0136   pluginDescFastJet.addNode(edm::PluginDescription<PatternRecognitionFactory>("type", "FastJet", true));
0137   desc.add<edm::ParameterSetDescription>("pluginPatternRecognitionByFastJet", pluginDescFastJet);
0138 
0139   descriptions.add("trackstersProducer", desc);
0140 }
0141 
0142 void TrackstersProducer::produce(edm::Event& evt, const edm::EventSetup& es) {
0143   auto result = std::make_unique<std::vector<Trackster>>();
0144   auto output_mask = std::make_unique<std::vector<float>>();
0145 
0146   const std::vector<float>& original_layerclusters_mask = evt.get(original_layerclusters_mask_token_);
0147   const auto& layerClusters = evt.get(clusters_token_);
0148   const auto& inputClusterMask = evt.get(filtered_layerclusters_mask_token_);
0149   const auto& layerClustersTimes = evt.get(clustersTime_token_);
0150   const auto& seeding_regions = evt.get(seeding_regions_token_);
0151 
0152   tfSession_ = es.getData(tfDnnToken_).getSession();
0153 
0154   std::unordered_map<int, std::vector<int>> seedToTrackstersAssociation;
0155   // if it's regional iteration and there are seeding regions
0156   if (!seeding_regions.empty() and seeding_regions[0].index != -1) {
0157     auto numberOfSeedingRegions = seeding_regions.size();
0158     for (unsigned int i = 0; i < numberOfSeedingRegions; ++i) {
0159       seedToTrackstersAssociation.emplace(seeding_regions[i].index, 0);
0160     }
0161   }
0162 
0163   if (doNose_) {
0164     const auto& layer_clusters_hfnose_tiles = evt.get(layer_clusters_tiles_hfnose_token_);
0165     const typename PatternRecognitionAlgoBaseT<TICLLayerTilesHFNose>::Inputs inputHFNose(evt,
0166                                                                                          es,
0167                                                                                          layerClusters,
0168                                                                                          inputClusterMask,
0169                                                                                          layerClustersTimes,
0170                                                                                          layer_clusters_hfnose_tiles,
0171                                                                                          seeding_regions,
0172                                                                                          tfSession_);
0173 
0174     myAlgoHFNose_->makeTracksters(inputHFNose, *result, seedToTrackstersAssociation);
0175 
0176   } else {
0177     const auto& layer_clusters_tiles = evt.get(layer_clusters_tiles_token_);
0178     const typename PatternRecognitionAlgoBaseT<TICLLayerTiles>::Inputs input(
0179         evt, es, layerClusters, inputClusterMask, layerClustersTimes, layer_clusters_tiles, seeding_regions, tfSession_);
0180 
0181     myAlgo_->makeTracksters(input, *result, seedToTrackstersAssociation);
0182   }
0183   // Now update the global mask and put it into the event
0184   output_mask->reserve(original_layerclusters_mask.size());
0185   // Copy over the previous state
0186   std::copy(
0187       std::begin(original_layerclusters_mask), std::end(original_layerclusters_mask), std::back_inserter(*output_mask));
0188 
0189   for (auto& trackster : *result) {
0190     trackster.setIteration(iterIndex_);
0191     // Mask the used elements, accordingly
0192     for (auto const v : trackster.vertices()) {
0193       // TODO(rovere): for the moment we mask the layer cluster completely. In
0194       // the future, properly compute the fraction of usage.
0195       (*output_mask)[v] = 0.;
0196     }
0197   }
0198 
0199   evt.put(std::move(result));
0200   evt.put(std::move(output_mask));
0201 }