CMS 3D CMS Logo

CaloParticleDebugger.cc
Go to the documentation of this file.
1 //
2 // Original Author: Marco Rovere
3 // Created: Fri, 10 Nov 2017 14:39:18 GMT
4 //
5 //
6 //
7 // system include files
8 #include <memory>
9 #include <iostream>
10 #include <numeric>
11 #include <algorithm>
12 
13 // user include files
17 
22 
24 
33 
42 
43 //
44 // class declaration
45 //
46 
48 public:
49  explicit CaloParticleDebugger(const edm::ParameterSet&);
50  ~CaloParticleDebugger() override;
51 
52  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
53 
54 private:
55  void beginJob() override;
56  void analyze(const edm::Event&, const edm::EventSetup&) override;
57  void endJob() override;
58  void fillSimHits(std::map<int, float>&, const edm::Event&, const edm::EventSetup&);
66  std::vector<edm::InputTag> collectionTags_;
74  std::vector<edm::EDGetTokenT<std::vector<PCaloHit>>> collectionTagsToken_;
76  int geometryType_ = 0;
77  // ----------member data ---------------------------
78 };
79 
80 //
81 // constants, enums and typedefs
82 //
83 
84 //
85 // static data member definitions
86 //
87 
88 //
89 // constructors and destructor
90 //
92  : simTracks_(iConfig.getParameter<edm::InputTag>("simTracks")),
93  genParticles_(iConfig.getParameter<edm::InputTag>("genParticles")),
94  genBarcodes_(iConfig.getParameter<edm::InputTag>("genBarcodes")),
95  simVertices_(iConfig.getParameter<edm::InputTag>("simVertices")),
96  trackingParticles_(iConfig.getParameter<edm::InputTag>("trackingParticles")),
97  caloParticles_(iConfig.getParameter<edm::InputTag>("caloParticles")),
98  simClusters_(iConfig.getParameter<edm::InputTag>("simClusters")),
99  collectionTags_(iConfig.getParameter<std::vector<edm::InputTag>>("collectionTags")) {
101  simTracksToken_ = iC.consumes<std::vector<SimTrack>>(simTracks_);
102  genParticlesToken_ = iC.consumes<std::vector<reco::GenParticle>>(genParticles_);
103  genBarcodesToken_ = iC.consumes<std::vector<int>>(genBarcodes_);
104  simVerticesToken_ = iC.consumes<std::vector<SimVertex>>(simVertices_);
105  trackingParticlesToken_ = iC.consumes<std::vector<TrackingParticle>>(trackingParticles_);
106  caloParticlesToken_ = iC.consumes<std::vector<CaloParticle>>(caloParticles_);
107  simClustersToken_ = iC.consumes<std::vector<SimCluster>>(simClusters_);
108  for (auto const& collectionTag : collectionTags_) {
109  collectionTagsToken_.push_back(iC.consumes<std::vector<PCaloHit>>(collectionTag));
110  }
112 }
113 
115 
116 //
117 // member functions
118 //
119 
120 // ------------ method called for each event ------------
122  using namespace edm;
123  using std::begin;
124  using std::end;
125 
128  edm::Handle<std::vector<int>> genBarcodesH;
130  edm::Handle<std::vector<TrackingParticle>> trackingParticlesH;
133 
134  iEvent.getByToken(simTracksToken_, simTracksH);
135  auto const& tracks = *simTracksH.product();
136 
138  auto const& genParticles = *genParticlesH.product();
139 
140  iEvent.getByToken(genBarcodesToken_, genBarcodesH);
141  auto const& genBarcodes = *genBarcodesH.product();
142 
143  iEvent.getByToken(simVerticesToken_, simVerticesH);
144  auto const& vertices = *simVerticesH.product();
145 
146  iEvent.getByToken(trackingParticlesToken_, trackingParticlesH);
147  auto const& trackingpart = *trackingParticlesH.product();
148 
149  iEvent.getByToken(caloParticlesToken_, caloParticlesH);
150  auto const& calopart = *caloParticlesH.product();
151 
152  iEvent.getByToken(simClustersToken_, simClustersH);
153  auto const& simclusters = *simClustersH.product();
154 
155  // Let's first fill in hits information
156  std::map<int, float> detIdToTotalSimEnergy;
157  fillSimHits(detIdToTotalSimEnergy, iEvent, iSetup);
158 
159  std::map<int, int> trackid_to_track_index;
160  LogVerbatim("CaloParticleDebuggerSimTracks") << "\n\n**Printing SimTracks information **";
161  LogVerbatim("CaloParticleDebuggerSimTracks") << "IDX\tTrackId\tPDGID\tMOMENTUM(x,y,z,E)\tVertexIdx\tGenPartIdx";
162  for (size_t i = 0; i < tracks.size(); ++i) {
163  auto const& t = tracks[i];
164  LogVerbatim("CaloParticleDebuggerSimTracks")
165  << i << "\t" << t.trackId() << "\t" << t << " Crossed Boundary: " << t.crossedBoundary()
166  << " Position Boundary: " << t.getPositionAtBoundary() << " Momentum Boundary: " << t.getMomentumAtBoundary()
167  << " Vtx: " << t.vertIndex() << " Momemtum Origin: " << t.momentum();
168  trackid_to_track_index[t.trackId()] = i;
169  }
170 
171  LogVerbatim("CaloParticleDebuggerGenParticles") << "\n\n**Printing GenParticles information **";
172  LogVerbatim("CaloParticleDebuggerGenParticles") << "BARCODE\tPDGID\tMOMENTUM(x,y,z)\tVertex(x,y,z)";
173  for (size_t i = 0; i < genParticles.size(); ++i) {
174  auto const& gp = genParticles[i];
175  LogVerbatim("CaloParticleDebuggerGenParticles")
176  << genBarcodes[i] << "\t" << gp.pdgId() << "\t" << gp.momentum() << "\t" << gp.vertex();
177  }
178 
179  LogVerbatim("CaloParticleDebuggerSimVertices") << "\n\n**Printing SimVertex information **";
180  LogVerbatim("CaloParticleDebuggerSimVertices") << "IDX\tPOSITION(x,y,z)\tPARENT_INDEX\tVERTEX_ID";
181  for (size_t i = 0; i < vertices.size(); ++i) {
182  auto const& v = vertices[i];
183  LogVerbatim("CaloParticleDebuggerSimVertices") << i << "\t" << v;
184  }
185 
186  LogVerbatim("CaloParticleDebuggerTrackingParticles") << "\n\n**Printing TrackingParticles information **";
187  for (size_t i = 0; i < trackingpart.size(); ++i) {
188  auto const& tp = trackingpart[i];
189  LogVerbatim("CaloParticleDebuggerTrackingParticles") << i << "\t" << tp;
190  }
191 
192  LogVerbatim("CaloParticleDebuggerCaloParticles") << "\n\n**Printing CaloParticles information **";
193  for (size_t i = 0; i < calopart.size(); ++i) {
194  auto const& cp = calopart[i];
195  LogVerbatim("CaloParticleDebuggerCaloParticles") << "\n\n"
196  << i << "\tType: " << cp.pdgId() << "\tEnergy: " << cp.energy()
197  << "\tBarcode: " << cp.g4Tracks()[0].genpartIndex()
198  << " G4_trackID: " << cp.g4Tracks()[0].trackId(); // << cp ;
199  double total_sim_energy = 0.;
200  double total_cp_energy = 0.;
201  LogVerbatim("CaloParticleDebuggerCaloParticles") << "--> Overall simclusters in CP: " << cp.simClusters().size();
202  // All the next mess just to print the simClusters ordered
203  auto const& simcs = cp.simClusters();
204  for (size_t j = 0; j < simcs.size(); ++j) {
205  LogVerbatim("CaloParticleDebuggerCaloParticles") << *(simcs[j]);
206  }
207 
208  for (auto const& sc : cp.simClusters()) {
209  for (auto const& cl : sc->hits_and_fractions()) {
210  total_sim_energy += detIdToTotalSimEnergy[cl.first] * cl.second;
211  total_cp_energy += cp.energy() * cl.second;
212  }
213  }
214  LogVerbatim("CaloParticleDebuggerCaloParticles")
215  << "--> Overall SC energy (sum using sim energies): " << total_sim_energy;
216  LogVerbatim("CaloParticleDebuggerCaloParticles")
217  << "--> Overall SC energy (sum using CaloP energies): " << total_cp_energy;
218  }
219 
220  LogVerbatim("CaloParticleDebuggerSimClusters") << "\n\n**Printing SimClusters information **";
221  for (size_t i = 0; i < simclusters.size(); ++i) {
222  auto const& simcl = simclusters[i];
223  LogVerbatim("CaloParticleDebuggerSimClusters")
224  << "\n\n"
225  << i << "\tType: " << simcl.pdgId() << "\tEnergy: " << simcl.energy() << "\tKey: " << i; // << simcl ;
226  auto const& simtrack = simcl.g4Tracks()[0];
227  LogVerbatim("CaloParticleDebuggerSimClusters") << " Crossed Boundary: " << simtrack.crossedBoundary()
228  << " Position Boundary: " << simtrack.getPositionAtBoundary()
229  << " Momentum Boundary: " << simtrack.getMomentumAtBoundary();
230  double total_sim_energy = 0.;
231  LogVerbatim("CaloParticleDebuggerSimClusters") << "--> Overall simclusters's size: " << simcl.numberOfRecHits();
232  for (auto const& cl : simcl.hits_and_fractions()) {
233  total_sim_energy += detIdToTotalSimEnergy[cl.first] * cl.second;
234  }
235  LogVerbatim("CaloParticleDebuggerSimClusters") << simcl;
236  LogVerbatim("CaloParticleDebuggerSimClusters")
237  << "--> Overall SimCluster energy (sum using sim energies): " << total_sim_energy;
238  }
239 }
240 
241 // ------------ method called once each job just before starting event loop ------------
243 
244 // ------------ method called once each job just after ending the event loop ------------
246 
247 void CaloParticleDebugger::fillSimHits(std::map<int, float>& detIdToTotalSimEnergy,
248  const edm::Event& iEvent,
249  const edm::EventSetup& iSetup) {
250  // Taken needed quantities from the EventSetup
251  const auto& geom = iSetup.getHandle(geomToken_);
252  const HGCalGeometry *eegeom = nullptr, *fhgeom = nullptr, *bhgeomnew = nullptr;
253  const HcalGeometry* bhgeom = nullptr;
254  const HGCalDDDConstants* hgddd[3];
255  const HGCalTopology* hgtopo[3];
256  const HcalDDDRecConstants* hcddd = nullptr;
257  eegeom =
258  static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::HGCalEE, ForwardSubdetector::ForwardEmpty));
259  //check if it's the new geometry
260  if (eegeom) {
261  geometryType_ = 1;
262  fhgeom = static_cast<const HGCalGeometry*>(
263  geom->getSubdetectorGeometry(DetId::HGCalHSi, ForwardSubdetector::ForwardEmpty));
264  bhgeomnew = static_cast<const HGCalGeometry*>(
265  geom->getSubdetectorGeometry(DetId::HGCalHSc, ForwardSubdetector::ForwardEmpty));
266  } else {
267  geometryType_ = 0;
268  eegeom = static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCEE));
269  fhgeom = static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCHEF));
270  bhgeom = static_cast<const HcalGeometry*>(geom->getSubdetectorGeometry(DetId::Hcal, HcalEndcap));
271  }
272  hgtopo[0] = &(eegeom->topology());
273  hgtopo[1] = &(fhgeom->topology());
274  if (bhgeomnew)
275  hgtopo[2] = &(bhgeomnew->topology());
276  else
277  hgtopo[2] = nullptr;
278 
279  for (unsigned i = 0; i < 3; ++i) {
280  if (hgtopo[i])
281  hgddd[i] = &(hgtopo[i]->dddConstants());
282  }
283 
284  if (bhgeom)
285  hcddd = bhgeom->topology().dddConstants();
286 
287  // loop over the collections
288  int token = 0;
289  for (auto const& collectionTag : collectionTags_) {
291  const bool isHcal = (collectionTag.instance().find("HcalHits") != std::string::npos);
292  iEvent.getByToken(collectionTagsToken_[token++], hSimHits);
293  for (auto const& simHit : *hSimHits) {
294  DetId id(0);
295  const uint32_t simId = simHit.id();
296  if (geometryType_ == 1) {
297  //no test numbering in new geometry
298  id = simId;
299  } else if (isHcal) {
300  assert(hcddd);
301  HcalDetId hid = HcalHitRelabeller::relabel(simId, hcddd);
302  if (hid.subdet() == HcalEndcap)
303  id = hid;
304  } else {
305  int subdet, layer, cell, sec, subsec, zp;
306  HGCalTestNumbering::unpackHexagonIndex(simId, subdet, zp, layer, sec, subsec, cell);
307  const HGCalDDDConstants* ddd = hgddd[subdet - 3];
308  std::pair<int, int> recoLayerCell = ddd->simToReco(cell, layer, sec, hgtopo[subdet - 3]->detectorType());
309  cell = recoLayerCell.first;
310  layer = recoLayerCell.second;
311  // skip simhits with bad barcodes or non-existant layers
312  if (layer == -1 || simHit.geantTrackId() == 0)
313  continue;
314  id = HGCalDetId((ForwardSubdetector)subdet, zp, layer, subsec, sec, cell);
315  }
316 
317  if (DetId(0) == id)
318  continue;
319 
320  detIdToTotalSimEnergy[id.rawId()] += simHit.energy();
321  }
322  } // end of loop over InputTags
323 }
324 
325 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
328  desc.add<edm::InputTag>("simTracks", edm::InputTag("g4SimHits"));
329  desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
330  desc.add<edm::InputTag>("genBarcodes", edm::InputTag("genParticles"));
331  desc.add<edm::InputTag>("simVertices", edm::InputTag("g4SimHits"));
332  desc.add<edm::InputTag>("trackingParticles", edm::InputTag("mix", "MergedTrackTruth"));
333  desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
334  desc.add<edm::InputTag>("simClusters", edm::InputTag("mix", "MergedCaloTruth"));
335  desc.add<std::vector<edm::InputTag>>("collectionTags",
336  {edm::InputTag("g4SimHits", "HGCHitsEE"),
337  edm::InputTag("g4SimHits", "HGCHitsHEfront"),
338  edm::InputTag("g4SimHits", "HcalHits")});
339  descriptions.add("caloParticleDebugger", desc);
340 }
341 
342 // define this as a plug-in
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Log< level::Info, true > LogVerbatim
edm::EDGetTokenT< std::vector< TrackingParticle > > trackingParticlesToken_
T const * product() const
Definition: Handle.h:70
ForwardSubdetector
assert(be >=bs)
edm::EDGetTokenT< std::vector< int > > genBarcodesToken_
edm::EDGetTokenT< std::vector< SimCluster > > simClustersToken_
CaloParticleDebugger(const edm::ParameterSet &)
constexpr HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:138
int iEvent
Definition: GenABIO.cc:224
std::vector< edm::EDGetTokenT< std::vector< PCaloHit > > > collectionTagsToken_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< std::vector< SimTrack > > simTracksToken_
DetId relabel(const uint32_t testId) const
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
edm::EDGetTokenT< std::vector< CaloParticle > > caloParticlesToken_
Definition: DetId.h:17
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void fillSimHits(std::map< int, float > &, const edm::Event &, const edm::EventSetup &)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< std::vector< reco::GenParticle > > genParticlesToken_
HLT enums.
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
void analyze(const edm::Event &, const edm::EventSetup &) override
edm::InputTag trackingParticles_
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geomToken_
const HGCalDDDConstants & dddConstants() const
Definition: HGCalTopology.h:98
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
std::vector< edm::InputTag > collectionTags_
const HcalTopology & topology() const
Definition: HcalGeometry.h:111
edm::EDGetTokenT< std::vector< SimVertex > > simVerticesToken_