CMS 3D CMS Logo

SimTrackstersProducer.cc
Go to the documentation of this file.
1 // Author: Felice Pantaleo, Leonardo Cristella - felice.pantaleo@cern.ch, leonardo.cristella@cern.ch
2 // Date: 09/2021
3 
4 // user include files
5 
14 
17 
19 
23 
27 
29 
30 #include "TrackstersPCA.h"
31 #include <vector>
32 #include <map>
33 #include <iterator>
34 #include <algorithm>
35 
36 using namespace ticl;
37 
39 public:
41  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
42 
43  void produce(edm::Event&, const edm::EventSetup&) override;
44 
45 private:
47  const bool doNose_ = false;
51 
54 
59  const double fractionCut_;
60 };
62 
64  : detector_(ps.getParameter<std::string>("detector")),
65  doNose_(detector_ == "HFNose"),
66  clusters_token_(consumes(ps.getParameter<edm::InputTag>("layer_clusters"))),
67  clustersTime_token_(consumes(ps.getParameter<edm::InputTag>("time_layerclusters"))),
68  filtered_layerclusters_mask_token_(consumes(ps.getParameter<edm::InputTag>("filtered_mask"))),
69  simclusters_token_(consumes(ps.getParameter<edm::InputTag>("simclusters"))),
70  caloparticles_token_(consumes(ps.getParameter<edm::InputTag>("caloparticles"))),
71  associatorMapSimClusterToReco_token_(
72  consumes(ps.getParameter<edm::InputTag>("layerClusterSimClusterAssociator"))),
73  associatorMapCaloParticleToReco_token_(
74  consumes(ps.getParameter<edm::InputTag>("layerClusterCaloParticleAssociator"))),
75  geom_token_(esConsumes()),
76  fractionCut_(ps.getParameter<double>("fractionCut")) {
77  produces<TracksterCollection>();
78  produces<std::vector<float>>();
79  produces<TracksterCollection>("fromCPs");
80  produces<std::vector<float>>("fromCPs");
81  produces<std::map<uint, std::vector<uint>>>();
82 }
83 
85  // hgcalMultiClusters
87  desc.add<std::string>("detector", "HGCAL");
88  desc.add<edm::InputTag>("layer_clusters", edm::InputTag("hgcalLayerClusters"));
89  desc.add<edm::InputTag>("time_layerclusters", edm::InputTag("hgcalLayerClusters", "timeLayerCluster"));
90  desc.add<edm::InputTag>("filtered_mask", edm::InputTag("filteredLayerClustersSimTracksters", "ticlSimTracksters"));
91  desc.add<edm::InputTag>("simclusters", edm::InputTag("mix", "MergedCaloTruth"));
92  desc.add<edm::InputTag>("caloparticles", edm::InputTag("mix", "MergedCaloTruth"));
93  desc.add<edm::InputTag>("layerClusterSimClusterAssociator",
94  edm::InputTag("layerClusterSimClusterAssociationProducer"));
95  desc.add<edm::InputTag>("layerClusterCaloParticleAssociator",
96  edm::InputTag("layerClusterCaloParticleAssociationProducer"));
97  desc.add<double>("fractionCut", 0.);
98 
99  descriptions.addWithDefaultLabel(desc);
100 }
101 
103  auto result = std::make_unique<TracksterCollection>();
104  auto output_mask = std::make_unique<std::vector<float>>();
105  auto result_fromCP = std::make_unique<TracksterCollection>();
106  auto output_mask_fromCP = std::make_unique<std::vector<float>>();
107  auto cpToSc_SimTrackstersMap = std::make_unique<std::map<uint, std::vector<uint>>>();
108  const auto& layerClusters = evt.get(clusters_token_);
109  const auto& layerClustersTimes = evt.get(clustersTime_token_);
110  const auto& inputClusterMask = evt.get(filtered_layerclusters_mask_token_);
111  output_mask->resize(layerClusters.size(), 1.f);
112  output_mask_fromCP->resize(layerClusters.size(), 1.f);
113 
114  const auto& simclusters = evt.get(simclusters_token_);
115  const auto& caloparticles = evt.get(caloparticles_token_);
116 
117  const auto& simClustersToRecoColl = evt.get(associatorMapSimClusterToReco_token_);
118  const auto& caloParticlesToRecoColl = evt.get(associatorMapCaloParticleToReco_token_);
119 
120  const auto& geom = es.getData(geom_token_);
122  const auto num_simclusters = simclusters.size();
123  result->reserve(num_simclusters); // Conservative size, will call shrink_to_fit later
124  const auto num_caloparticles = caloparticles.size();
125  result_fromCP->reserve(num_caloparticles);
126 
127  for (const auto& [key, lcVec] : caloParticlesToRecoColl) {
128  auto const& cp = *(key);
129  auto cpIndex = &cp - &caloparticles[0];
130 
131  auto regr_energy = cp.energy();
132  std::vector<uint> scSimTracksterIdx;
133  scSimTracksterIdx.reserve(cp.simClusters().size());
134 
135  // Create a Trackster from the object entering HGCal
136  if (cp.g4Tracks()[0].crossedBoundary()) {
137  regr_energy = cp.g4Tracks()[0].getMomentumAtBoundary().energy();
138 
139  addTrackster(cpIndex,
140  lcVec,
141  inputClusterMask,
142  fractionCut_,
143  regr_energy,
144  cp.pdgId(),
145  cp.charge(),
146  key.id(),
148  *output_mask,
149  *result);
150  } else {
151  for (const auto& scRef : cp.simClusters()) {
152  const auto& it = simClustersToRecoColl.find(scRef);
153  if (it == simClustersToRecoColl.end())
154  continue;
155  const auto& lcVec = it->val;
156  auto const& sc = *(scRef);
157  auto const scIndex = &sc - &simclusters[0];
158 
159  addTrackster(scIndex,
160  lcVec,
161  inputClusterMask,
162  fractionCut_,
163  sc.g4Tracks()[0].getMomentumAtBoundary().energy(),
164  sc.pdgId(),
165  sc.charge(),
166  scRef.id(),
168  *output_mask,
169  *result);
170 
171  if (result->empty())
172  continue;
173  const auto index = result->size() - 1;
174  if (std::find(scSimTracksterIdx.begin(), scSimTracksterIdx.end(), index) == scSimTracksterIdx.end()) {
175  scSimTracksterIdx.emplace_back(index);
176  }
177  }
178  scSimTracksterIdx.shrink_to_fit();
179  }
180 
181  // Create a Trackster from any CP
182  addTrackster(cpIndex,
183  lcVec,
184  inputClusterMask,
185  fractionCut_,
186  regr_energy,
187  cp.pdgId(),
188  cp.charge(),
189  key.id(),
191  *output_mask_fromCP,
192  *result_fromCP);
193 
194  if (result_fromCP->empty())
195  continue;
196  const auto index = result_fromCP->size() - 1;
197  if (cpToSc_SimTrackstersMap->find(index) == cpToSc_SimTrackstersMap->end()) {
198  (*cpToSc_SimTrackstersMap)[index] = scSimTracksterIdx;
199  }
200  }
201 
204  result->shrink_to_fit();
206  *result_fromCP, layerClusters, layerClustersTimes, rhtools_.getPositionLayer(rhtools_.lastLayerEE(doNose_)).z());
207  result_fromCP->shrink_to_fit();
208 
209  evt.put(std::move(result));
210  evt.put(std::move(output_mask));
211  evt.put(std::move(result_fromCP), "fromCPs");
212  evt.put(std::move(output_mask_fromCP), "fromCPs");
213  evt.put(std::move(cpToSc_SimTrackstersMap));
214 }
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
const edm::EDGetTokenT< hgcal::SimToRecoCollectionWithSimClusters > associatorMapSimClusterToReco_token_
const edm::EDGetTokenT< std::vector< float > > filtered_layerclusters_mask_token_
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geom_token_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool energyWeight=true)
static void addTrackster(const int &index, const std::vector< std::pair< edm::Ref< reco::CaloClusterCollection >, std::pair< float, float >>> &lcVec, const std::vector< float > &inputClusterMask, const float &fractionCut_, const float &energy, const int &pdgId, const int &charge, const edm::ProductID &seed, const Trackster::IterationIndex iter, std::vector< float > &output_mask, std::vector< Trackster > &result)
Definition: commons.h:36
void produce(edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::EDGetTokenT< std::vector< CaloParticle > > caloparticles_token_
const edm::EDGetTokenT< std::vector< reco::CaloCluster > > clusters_token_
bool getData(T &iHolder) const
Definition: EventSetup.h:122
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
const edm::EDGetTokenT< hgcal::SimToRecoCollection > associatorMapCaloParticleToReco_token_
SimTrackstersProducer(const edm::ParameterSet &)
const edm::EDGetTokenT< std::vector< SimCluster > > simclusters_token_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
hgcal::RecHitTools rhtools_
GlobalPoint getPositionLayer(int layer, bool nose=false) const
Definition: RecHitTools.cc:141
void setGeometry(CaloGeometry const &)
Definition: RecHitTools.cc:68
HLT enums.
Definition: Common.h:8
def move(src, dest)
Definition: eostools.py:511
unsigned int lastLayerEE(bool nose=false) const
Definition: RecHitTools.h:75