CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"))),
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(),
147  *output_mask,
148  *result);
149  } else {
150  for (const auto& scRef : cp.simClusters()) {
151  const auto& it = simClustersToRecoColl.find(scRef);
152  if (it == simClustersToRecoColl.end())
153  continue;
154  const auto& lcVec = it->val;
155  auto const& sc = *(scRef);
156  auto const scIndex = &sc - &simclusters[0];
157 
158  addTrackster(scIndex,
159  lcVec,
160  inputClusterMask,
161  fractionCut_,
162  sc.g4Tracks()[0].getMomentumAtBoundary().energy(),
163  sc.pdgId(),
164  sc.charge(),
165  scRef.id(),
166  *output_mask,
167  *result);
168 
169  if (result->empty())
170  continue;
171  const auto index = result->size() - 1;
172  if (std::find(scSimTracksterIdx.begin(), scSimTracksterIdx.end(), index) == scSimTracksterIdx.end()) {
173  scSimTracksterIdx.emplace_back(index);
174  }
175  }
176  scSimTracksterIdx.shrink_to_fit();
177  }
178 
179  // Create a Trackster from any CP
180  addTrackster(cpIndex,
181  lcVec,
182  inputClusterMask,
183  fractionCut_,
184  regr_energy,
185  cp.pdgId(),
186  cp.charge(),
187  key.id(),
188  *output_mask_fromCP,
189  *result_fromCP);
190 
191  if (result_fromCP->empty())
192  continue;
193  const auto index = result_fromCP->size() - 1;
194  if (cpToSc_SimTrackstersMap->find(index) == cpToSc_SimTrackstersMap->end()) {
195  (*cpToSc_SimTrackstersMap)[index] = scSimTracksterIdx;
196  }
197  }
198 
200  *result, layerClusters, layerClustersTimes, rhtools_.getPositionLayer(rhtools_.lastLayerEE(doNose_)).z());
201  result->shrink_to_fit();
203  *result_fromCP, layerClusters, layerClustersTimes, rhtools_.getPositionLayer(rhtools_.lastLayerEE(doNose_)).z());
204  result_fromCP->shrink_to_fit();
205 
206  evt.put(std::move(result));
207  evt.put(std::move(output_mask));
208  evt.put(std::move(result_fromCP), "fromCPs");
209  evt.put(std::move(output_mask_fromCP), "fromCPs");
210  evt.put(std::move(cpToSc_SimTrackstersMap));
211 }
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
tuple result
Definition: mps_fire.py:311
bool getData(T &iHolder) const
Definition: EventSetup.h:128
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool energyWeight=true)
void produce(edm::Event &, const edm::EventSetup &) override
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, std::vector< float > &output_mask, std::vector< Trackster > &result)
Definition: commons.h:36
def move
Definition: eostools.py:511
tuple key
prepare the HTCondor submission files and eventually submit them
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
const edm::EDGetTokenT< std::vector< CaloParticle > > caloparticles_token_
const edm::EDGetTokenT< std::vector< reco::CaloCluster > > clusters_token_
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
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
unsigned int lastLayerEE(bool nose=false) const
Definition: RecHitTools.h:75
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283