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  void addTrackster(const int& index,
45  const std::vector<std::pair<edm::Ref<reco::CaloClusterCollection>, std::pair<float, float>>>& lcVec,
46  const std::vector<float>& inputClusterMask,
47  const float& fractionCut_,
48  const float& energy,
49  const int& pdgId,
50  const int& charge,
51  const edm::ProductID& seed,
52  const Trackster::IterationIndex iter,
53  std::vector<float>& output_mask,
54  std::vector<Trackster>& result);
55 
56 private:
58  const bool doNose_ = false;
62 
65 
70  const double fractionCut_;
71 };
73 
75  : detector_(ps.getParameter<std::string>("detector")),
76  doNose_(detector_ == "HFNose"),
77  clusters_token_(consumes(ps.getParameter<edm::InputTag>("layer_clusters"))),
78  clustersTime_token_(consumes(ps.getParameter<edm::InputTag>("time_layerclusters"))),
79  filtered_layerclusters_mask_token_(consumes(ps.getParameter<edm::InputTag>("filtered_mask"))),
80  simclusters_token_(consumes(ps.getParameter<edm::InputTag>("simclusters"))),
81  caloparticles_token_(consumes(ps.getParameter<edm::InputTag>("caloparticles"))),
82  associatorMapSimClusterToReco_token_(
83  consumes(ps.getParameter<edm::InputTag>("layerClusterSimClusterAssociator"))),
84  associatorMapCaloParticleToReco_token_(
85  consumes(ps.getParameter<edm::InputTag>("layerClusterCaloParticleAssociator"))),
86  geom_token_(esConsumes()),
87  fractionCut_(ps.getParameter<double>("fractionCut")) {
88  produces<TracksterCollection>();
89  produces<std::vector<float>>();
90  produces<TracksterCollection>("fromCPs");
91  produces<std::vector<float>>("fromCPs");
92  produces<std::map<uint, std::vector<uint>>>();
93 }
94 
96  // hgcalMultiClusters
98  desc.add<std::string>("detector", "HGCAL");
99  desc.add<edm::InputTag>("layer_clusters", edm::InputTag("hgcalLayerClusters"));
100  desc.add<edm::InputTag>("time_layerclusters", edm::InputTag("hgcalLayerClusters", "timeLayerCluster"));
101  desc.add<edm::InputTag>("filtered_mask", edm::InputTag("filteredLayerClustersSimTracksters", "ticlSimTracksters"));
102  desc.add<edm::InputTag>("simclusters", edm::InputTag("mix", "MergedCaloTruth"));
103  desc.add<edm::InputTag>("caloparticles", edm::InputTag("mix", "MergedCaloTruth"));
104  desc.add<edm::InputTag>("layerClusterSimClusterAssociator",
105  edm::InputTag("layerClusterSimClusterAssociationProducer"));
106  desc.add<edm::InputTag>("layerClusterCaloParticleAssociator",
107  edm::InputTag("layerClusterCaloParticleAssociationProducer"));
108  desc.add<double>("fractionCut", 0.);
109 
110  descriptions.addWithDefaultLabel(desc);
111 }
112 
114  const int& index,
115  const std::vector<std::pair<edm::Ref<reco::CaloClusterCollection>, std::pair<float, float>>>& lcVec,
116  const std::vector<float>& inputClusterMask,
117  const float& fractionCut_,
118  const float& energy,
119  const int& pdgId,
120  const int& charge,
121  const edm::ProductID& seed,
122  const Trackster::IterationIndex iter,
123  std::vector<float>& output_mask,
124  std::vector<Trackster>& result) {
125  if (lcVec.empty())
126  return;
127 
128  Trackster tmpTrackster;
129  tmpTrackster.zeroProbabilities();
130  tmpTrackster.vertices().reserve(lcVec.size());
131  tmpTrackster.vertex_multiplicity().reserve(lcVec.size());
132  for (auto const& [lc, energyScorePair] : lcVec) {
133  if (inputClusterMask[lc.index()] > 0) {
134  double fraction = energyScorePair.first / lc->energy();
135  if (fraction < fractionCut_)
136  continue;
137  tmpTrackster.vertices().push_back(lc.index());
138  output_mask[lc.index()] -= fraction;
139  tmpTrackster.vertex_multiplicity().push_back(1. / fraction);
140  }
141  }
142 
144  tmpTrackster.setRegressedEnergy(energy);
145  tmpTrackster.setIteration(iter);
146  tmpTrackster.setSeed(seed, index);
147  result.emplace_back(tmpTrackster);
148 }
149 
151  auto result = std::make_unique<TracksterCollection>();
152  auto output_mask = std::make_unique<std::vector<float>>();
153  auto result_fromCP = std::make_unique<TracksterCollection>();
154  auto output_mask_fromCP = std::make_unique<std::vector<float>>();
155  auto cpToSc_SimTrackstersMap = std::make_unique<std::map<uint, std::vector<uint>>>();
156  const auto& layerClusters = evt.get(clusters_token_);
157  const auto& layerClustersTimes = evt.get(clustersTime_token_);
158  const auto& inputClusterMask = evt.get(filtered_layerclusters_mask_token_);
159  output_mask->resize(layerClusters.size(), 1.f);
160  output_mask_fromCP->resize(layerClusters.size(), 1.f);
161 
162  const auto& simclusters = evt.get(simclusters_token_);
163  const auto& caloparticles = evt.get(caloparticles_token_);
164 
165  const auto& simClustersToRecoColl = evt.get(associatorMapSimClusterToReco_token_);
166  const auto& caloParticlesToRecoColl = evt.get(associatorMapCaloParticleToReco_token_);
167 
168  const auto& geom = es.getData(geom_token_);
170  const auto num_simclusters = simclusters.size();
171  result->reserve(num_simclusters); // Conservative size, will call shrink_to_fit later
172  const auto num_caloparticles = caloparticles.size();
173  result_fromCP->reserve(num_caloparticles);
174 
175  for (const auto& [key, lcVec] : caloParticlesToRecoColl) {
176  auto const& cp = *(key);
177  auto cpIndex = &cp - &caloparticles[0];
178 
179  auto regr_energy = cp.energy();
180  std::vector<uint> scSimTracksterIdx;
181  scSimTracksterIdx.reserve(cp.simClusters().size());
182 
183  // Create a Trackster from the object entering HGCal
184  if (cp.g4Tracks()[0].crossedBoundary()) {
185  regr_energy = cp.g4Tracks()[0].getMomentumAtBoundary().energy();
186 
187  addTrackster(cpIndex,
188  lcVec,
189  inputClusterMask,
190  fractionCut_,
191  regr_energy,
192  cp.pdgId(),
193  cp.charge(),
194  key.id(),
196  *output_mask,
197  *result);
198  } else {
199  for (const auto& scRef : cp.simClusters()) {
200  const auto& it = simClustersToRecoColl.find(scRef);
201  if (it == simClustersToRecoColl.end())
202  continue;
203  const auto& lcVec = it->val;
204  auto const& sc = *(scRef);
205  auto const scIndex = &sc - &simclusters[0];
206 
207  addTrackster(scIndex,
208  lcVec,
209  inputClusterMask,
210  fractionCut_,
211  sc.g4Tracks()[0].getMomentumAtBoundary().energy(),
212  sc.pdgId(),
213  sc.charge(),
214  scRef.id(),
216  *output_mask,
217  *result);
218 
219  if (result->empty())
220  continue;
221  const auto index = result->size() - 1;
222  if (std::find(scSimTracksterIdx.begin(), scSimTracksterIdx.end(), index) == scSimTracksterIdx.end()) {
223  scSimTracksterIdx.emplace_back(index);
224  }
225  }
226  scSimTracksterIdx.shrink_to_fit();
227  }
228 
229  // Create a Trackster from any CP
230  addTrackster(cpIndex,
231  lcVec,
232  inputClusterMask,
233  fractionCut_,
234  regr_energy,
235  cp.pdgId(),
236  cp.charge(),
237  key.id(),
239  *output_mask_fromCP,
240  *result_fromCP);
241 
242  if (result_fromCP->empty())
243  continue;
244  const auto index = result_fromCP->size() - 1;
245  if (cpToSc_SimTrackstersMap->find(index) == cpToSc_SimTrackstersMap->end()) {
246  (*cpToSc_SimTrackstersMap)[index] = scSimTracksterIdx;
247  }
248  }
249 
252  result->shrink_to_fit();
254  *result_fromCP, layerClusters, layerClustersTimes, rhtools_.getPositionLayer(rhtools_.lastLayerEE(doNose_)).z());
255  result_fromCP->shrink_to_fit();
256 
257  evt.put(std::move(result));
258  evt.put(std::move(output_mask));
259  evt.put(std::move(result_fromCP), "fromCPs");
260  evt.put(std::move(output_mask_fromCP), "fromCPs");
261  evt.put(std::move(cpToSc_SimTrackstersMap));
262 }
void setSeed(edm::ProductID pid, int index)
Definition: Trackster.h:60
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
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void setRegressedEnergy(float value)
Definition: Trackster.h:68
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:347
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)
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)
Trackster::ParticleType tracksterParticleTypeFromPdgId(int pdgId, int charge)
Definition: commons.h:14
void produce(edm::Event &, const edm::EventSetup &) override
double f[11][100]
#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_
void zeroProbabilities()
Definition: Trackster.h:107
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
const edm::EDGetTokenT< hgcal::SimToRecoCollection > associatorMapCaloParticleToReco_token_
SimTrackstersProducer(const edm::ParameterSet &)
std::vector< unsigned int > & vertices()
Definition: Trackster.h:57
const edm::EDGetTokenT< std::vector< SimCluster > > simclusters_token_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
hgcal::RecHitTools rhtools_
std::vector< float > & vertex_multiplicity()
Definition: Trackster.h:58
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
void setIteration(const Trackster::IterationIndex i)
Definition: Trackster.h:56
void setIdProbability(ParticleType type, float value)
Definition: Trackster.h:117
def move(src, dest)
Definition: eostools.py:511
unsigned int lastLayerEE(bool nose=false) const
Definition: RecHitTools.h:75