Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:34

0001 //
0002 // Original Author:  Marco Rovere
0003 //         Created:  Fri, 10 Nov 2017 14:39:18 GMT
0004 //
0005 //
0006 //
0007 // system include files
0008 #include <memory>
0009 #include <iostream>
0010 #include <numeric>
0011 #include <algorithm>
0012 
0013 // user include files
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 
0018 #include "FWCore/Framework/interface/Event.h"
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/Framework/interface/ESHandle.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 
0025 #include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
0026 #include "SimDataFormats/Track/interface/SimTrack.h"
0027 #include "SimDataFormats/Vertex/interface/SimVertex.h"
0028 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0029 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0030 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0031 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
0032 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0033 
0034 #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
0035 #include "Geometry/HGCalGeometry/interface/HGCalGeometry.h"
0036 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0037 #include "SimDataFormats/CaloTest/interface/HGCalTestNumbering.h"
0038 #include "Geometry/CaloTopology/interface/HGCalTopology.h"
0039 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0040 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0041 #include "Geometry/HcalCommonData/interface/HcalHitRelabeller.h"
0042 
0043 //
0044 // class declaration
0045 //
0046 
0047 class CaloParticleDebugger : public edm::one::EDAnalyzer<> {
0048 public:
0049   explicit CaloParticleDebugger(const edm::ParameterSet&);
0050   ~CaloParticleDebugger() override;
0051 
0052   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0053 
0054 private:
0055   void beginJob() override;
0056   void analyze(const edm::Event&, const edm::EventSetup&) override;
0057   void endJob() override;
0058   void fillSimHits(std::map<int, float>&, const edm::Event&, const edm::EventSetup&);
0059   edm::InputTag simTracks_;
0060   edm::InputTag genParticles_;
0061   edm::InputTag genBarcodes_;
0062   edm::InputTag simVertices_;
0063   edm::InputTag trackingParticles_;
0064   edm::InputTag caloParticles_;
0065   edm::InputTag simClusters_;
0066   std::vector<edm::InputTag> collectionTags_;
0067   edm::EDGetTokenT<std::vector<SimTrack>> simTracksToken_;
0068   edm::EDGetTokenT<std::vector<reco::GenParticle>> genParticlesToken_;
0069   edm::EDGetTokenT<std::vector<int>> genBarcodesToken_;
0070   edm::EDGetTokenT<std::vector<SimVertex>> simVerticesToken_;
0071   edm::EDGetTokenT<std::vector<TrackingParticle>> trackingParticlesToken_;
0072   edm::EDGetTokenT<std::vector<CaloParticle>> caloParticlesToken_;
0073   edm::EDGetTokenT<std::vector<SimCluster>> simClustersToken_;
0074   std::vector<edm::EDGetTokenT<std::vector<PCaloHit>>> collectionTagsToken_;
0075   edm::ESGetToken<CaloGeometry, CaloGeometryRecord> geomToken_;
0076   int geometryType_ = 0;
0077   // ----------member data ---------------------------
0078 };
0079 
0080 //
0081 // constants, enums and typedefs
0082 //
0083 
0084 //
0085 // static data member definitions
0086 //
0087 
0088 //
0089 // constructors and destructor
0090 //
0091 CaloParticleDebugger::CaloParticleDebugger(const edm::ParameterSet& iConfig)
0092     : simTracks_(iConfig.getParameter<edm::InputTag>("simTracks")),
0093       genParticles_(iConfig.getParameter<edm::InputTag>("genParticles")),
0094       genBarcodes_(iConfig.getParameter<edm::InputTag>("genBarcodes")),
0095       simVertices_(iConfig.getParameter<edm::InputTag>("simVertices")),
0096       trackingParticles_(iConfig.getParameter<edm::InputTag>("trackingParticles")),
0097       caloParticles_(iConfig.getParameter<edm::InputTag>("caloParticles")),
0098       simClusters_(iConfig.getParameter<edm::InputTag>("simClusters")),
0099       collectionTags_(iConfig.getParameter<std::vector<edm::InputTag>>("collectionTags")) {
0100   edm::ConsumesCollector&& iC = consumesCollector();
0101   simTracksToken_ = iC.consumes<std::vector<SimTrack>>(simTracks_);
0102   genParticlesToken_ = iC.consumes<std::vector<reco::GenParticle>>(genParticles_);
0103   genBarcodesToken_ = iC.consumes<std::vector<int>>(genBarcodes_);
0104   simVerticesToken_ = iC.consumes<std::vector<SimVertex>>(simVertices_);
0105   trackingParticlesToken_ = iC.consumes<std::vector<TrackingParticle>>(trackingParticles_);
0106   caloParticlesToken_ = iC.consumes<std::vector<CaloParticle>>(caloParticles_);
0107   simClustersToken_ = iC.consumes<std::vector<SimCluster>>(simClusters_);
0108   for (auto const& collectionTag : collectionTags_) {
0109     collectionTagsToken_.push_back(iC.consumes<std::vector<PCaloHit>>(collectionTag));
0110   }
0111   geomToken_ = iC.esConsumes<CaloGeometry, CaloGeometryRecord>();
0112 }
0113 
0114 CaloParticleDebugger::~CaloParticleDebugger() {}
0115 
0116 //
0117 // member functions
0118 //
0119 
0120 // ------------ method called for each event  ------------
0121 void CaloParticleDebugger::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0122   using namespace edm;
0123   using std::begin;
0124   using std::end;
0125 
0126   edm::Handle<std::vector<SimTrack>> simTracksH;
0127   edm::Handle<std::vector<reco::GenParticle>> genParticlesH;
0128   edm::Handle<std::vector<int>> genBarcodesH;
0129   edm::Handle<std::vector<SimVertex>> simVerticesH;
0130   edm::Handle<std::vector<TrackingParticle>> trackingParticlesH;
0131   edm::Handle<std::vector<CaloParticle>> caloParticlesH;
0132   edm::Handle<std::vector<SimCluster>> simClustersH;
0133 
0134   iEvent.getByToken(simTracksToken_, simTracksH);
0135   auto const& tracks = *simTracksH.product();
0136 
0137   iEvent.getByToken(genParticlesToken_, genParticlesH);
0138   auto const& genParticles = *genParticlesH.product();
0139 
0140   iEvent.getByToken(genBarcodesToken_, genBarcodesH);
0141   auto const& genBarcodes = *genBarcodesH.product();
0142 
0143   iEvent.getByToken(simVerticesToken_, simVerticesH);
0144   auto const& vertices = *simVerticesH.product();
0145 
0146   iEvent.getByToken(trackingParticlesToken_, trackingParticlesH);
0147   auto const& trackingpart = *trackingParticlesH.product();
0148 
0149   iEvent.getByToken(caloParticlesToken_, caloParticlesH);
0150   auto const& calopart = *caloParticlesH.product();
0151 
0152   iEvent.getByToken(simClustersToken_, simClustersH);
0153   auto const& simclusters = *simClustersH.product();
0154 
0155   // Let's first fill in hits information
0156   std::map<int, float> detIdToTotalSimEnergy;
0157   fillSimHits(detIdToTotalSimEnergy, iEvent, iSetup);
0158 
0159   std::map<int, int> trackid_to_track_index;
0160   LogVerbatim("CaloParticleDebuggerSimTracks") << "\n\n**Printing SimTracks information **";
0161   LogVerbatim("CaloParticleDebuggerSimTracks") << "IDX\tTrackId\tPDGID\tMOMENTUM(x,y,z,E)\tVertexIdx\tGenPartIdx";
0162   for (size_t i = 0; i < tracks.size(); ++i) {
0163     auto const& t = tracks[i];
0164     LogVerbatim("CaloParticleDebuggerSimTracks")
0165         << i << "\t" << t.trackId() << "\t" << t << "  Crossed  Boundary: " << t.crossedBoundary()
0166         << "  Position Boundary: " << t.getPositionAtBoundary() << "  Momentum Boundary: " << t.getMomentumAtBoundary()
0167         << "  Vtx:               " << t.vertIndex() << "  Momemtum Origin:   " << t.momentum();
0168     trackid_to_track_index[t.trackId()] = i;
0169   }
0170 
0171   LogVerbatim("CaloParticleDebuggerGenParticles") << "\n\n**Printing GenParticles information **";
0172   LogVerbatim("CaloParticleDebuggerGenParticles") << "BARCODE\tPDGID\tMOMENTUM(x,y,z)\tVertex(x,y,z)";
0173   for (size_t i = 0; i < genParticles.size(); ++i) {
0174     auto const& gp = genParticles[i];
0175     LogVerbatim("CaloParticleDebuggerGenParticles")
0176         << genBarcodes[i] << "\t" << gp.pdgId() << "\t" << gp.momentum() << "\t" << gp.vertex();
0177   }
0178 
0179   LogVerbatim("CaloParticleDebuggerSimVertices") << "\n\n**Printing SimVertex information **";
0180   LogVerbatim("CaloParticleDebuggerSimVertices") << "IDX\tPOSITION(x,y,z)\tPARENT_INDEX\tVERTEX_ID";
0181   for (size_t i = 0; i < vertices.size(); ++i) {
0182     auto const& v = vertices[i];
0183     LogVerbatim("CaloParticleDebuggerSimVertices") << i << "\t" << v;
0184   }
0185 
0186   LogVerbatim("CaloParticleDebuggerTrackingParticles") << "\n\n**Printing TrackingParticles information **";
0187   for (size_t i = 0; i < trackingpart.size(); ++i) {
0188     auto const& tp = trackingpart[i];
0189     LogVerbatim("CaloParticleDebuggerTrackingParticles") << i << "\t" << tp;
0190   }
0191 
0192   LogVerbatim("CaloParticleDebuggerCaloParticles") << "\n\n**Printing CaloParticles information **";
0193   for (size_t i = 0; i < calopart.size(); ++i) {
0194     auto const& cp = calopart[i];
0195     LogVerbatim("CaloParticleDebuggerCaloParticles") << "\n\n"
0196                                                      << i << "\tType: " << cp.pdgId() << "\tEnergy: " << cp.energy()
0197                                                      << "\tBarcode: " << cp.g4Tracks()[0].genpartIndex()
0198                                                      << " G4_trackID: " << cp.g4Tracks()[0].trackId();  // << cp ;
0199     double total_sim_energy = 0.;
0200     double total_cp_energy = 0.;
0201     LogVerbatim("CaloParticleDebuggerCaloParticles") << "--> Overall simclusters in CP: " << cp.simClusters().size();
0202     // All the next mess just to print the simClusters ordered
0203     auto const& simcs = cp.simClusters();
0204     for (size_t j = 0; j < simcs.size(); ++j) {
0205       LogVerbatim("CaloParticleDebuggerCaloParticles") << *(simcs[j]);
0206     }
0207 
0208     for (auto const& sc : cp.simClusters()) {
0209       for (auto const& cl : sc->hits_and_fractions()) {
0210         total_sim_energy += detIdToTotalSimEnergy[cl.first] * cl.second;
0211         total_cp_energy += cp.energy() * cl.second;
0212       }
0213     }
0214     LogVerbatim("CaloParticleDebuggerCaloParticles")
0215         << "--> Overall SC energy (sum using sim energies): " << total_sim_energy;
0216     LogVerbatim("CaloParticleDebuggerCaloParticles")
0217         << "--> Overall SC energy (sum using CaloP energies): " << total_cp_energy;
0218   }
0219 
0220   LogVerbatim("CaloParticleDebuggerSimClusters") << "\n\n**Printing SimClusters information **";
0221   for (size_t i = 0; i < simclusters.size(); ++i) {
0222     auto const& simcl = simclusters[i];
0223     LogVerbatim("CaloParticleDebuggerSimClusters")
0224         << "\n\n"
0225         << i << "\tType: " << simcl.pdgId() << "\tEnergy: " << simcl.energy() << "\tKey: " << i;  // << simcl ;
0226     auto const& simtrack = simcl.g4Tracks()[0];
0227     LogVerbatim("CaloParticleDebuggerSimClusters") << "  Crossed  Boundary: " << simtrack.crossedBoundary()
0228                                                    << "  Position Boundary: " << simtrack.getPositionAtBoundary()
0229                                                    << "  Momentum Boundary: " << simtrack.getMomentumAtBoundary();
0230     double total_sim_energy = 0.;
0231     LogVerbatim("CaloParticleDebuggerSimClusters") << "--> Overall simclusters's size: " << simcl.numberOfRecHits();
0232     for (auto const& cl : simcl.hits_and_fractions()) {
0233       total_sim_energy += detIdToTotalSimEnergy[cl.first] * cl.second;
0234     }
0235     LogVerbatim("CaloParticleDebuggerSimClusters") << simcl;
0236     LogVerbatim("CaloParticleDebuggerSimClusters")
0237         << "--> Overall SimCluster energy (sum using sim energies): " << total_sim_energy;
0238   }
0239 }
0240 
0241 // ------------ method called once each job just before starting event loop  ------------
0242 void CaloParticleDebugger::beginJob() {}
0243 
0244 // ------------ method called once each job just after ending the event loop  ------------
0245 void CaloParticleDebugger::endJob() {}
0246 
0247 void CaloParticleDebugger::fillSimHits(std::map<int, float>& detIdToTotalSimEnergy,
0248                                        const edm::Event& iEvent,
0249                                        const edm::EventSetup& iSetup) {
0250   // Taken needed quantities from the EventSetup
0251   const auto& geom = iSetup.getHandle(geomToken_);
0252   const HGCalGeometry *eegeom = nullptr, *fhgeom = nullptr, *bhgeomnew = nullptr;
0253   const HcalGeometry* bhgeom = nullptr;
0254   const HGCalDDDConstants* hgddd[3];
0255   const HGCalTopology* hgtopo[3];
0256   const HcalDDDRecConstants* hcddd = nullptr;
0257   eegeom =
0258       static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::HGCalEE, ForwardSubdetector::ForwardEmpty));
0259   //check if it's the new geometry
0260   if (eegeom) {
0261     geometryType_ = 1;
0262     fhgeom = static_cast<const HGCalGeometry*>(
0263         geom->getSubdetectorGeometry(DetId::HGCalHSi, ForwardSubdetector::ForwardEmpty));
0264     bhgeomnew = static_cast<const HGCalGeometry*>(
0265         geom->getSubdetectorGeometry(DetId::HGCalHSc, ForwardSubdetector::ForwardEmpty));
0266   } else {
0267     geometryType_ = 0;
0268     eegeom = static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCEE));
0269     fhgeom = static_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCHEF));
0270     bhgeom = static_cast<const HcalGeometry*>(geom->getSubdetectorGeometry(DetId::Hcal, HcalEndcap));
0271   }
0272   hgtopo[0] = &(eegeom->topology());
0273   hgtopo[1] = &(fhgeom->topology());
0274   if (bhgeomnew)
0275     hgtopo[2] = &(bhgeomnew->topology());
0276   else
0277     hgtopo[2] = nullptr;
0278 
0279   for (unsigned i = 0; i < 3; ++i) {
0280     if (hgtopo[i])
0281       hgddd[i] = &(hgtopo[i]->dddConstants());
0282   }
0283 
0284   if (bhgeom)
0285     hcddd = bhgeom->topology().dddConstants();
0286 
0287   // loop over the collections
0288   int token = 0;
0289   for (auto const& collectionTag : collectionTags_) {
0290     edm::Handle<std::vector<PCaloHit>> hSimHits;
0291     const bool isHcal = (collectionTag.instance().find("HcalHits") != std::string::npos);
0292     iEvent.getByToken(collectionTagsToken_[token++], hSimHits);
0293     for (auto const& simHit : *hSimHits) {
0294       DetId id(0);
0295       const uint32_t simId = simHit.id();
0296       if (geometryType_ == 1) {
0297         //no test numbering in new geometry
0298         id = simId;
0299       } else if (isHcal) {
0300         assert(hcddd);
0301         HcalDetId hid = HcalHitRelabeller::relabel(simId, hcddd);
0302         if (hid.subdet() == HcalEndcap)
0303           id = hid;
0304       } else {
0305         int subdet, layer, cell, sec, subsec, zp;
0306         HGCalTestNumbering::unpackHexagonIndex(simId, subdet, zp, layer, sec, subsec, cell);
0307         const HGCalDDDConstants* ddd = hgddd[subdet - 3];
0308         std::pair<int, int> recoLayerCell = ddd->simToReco(cell, layer, sec, hgtopo[subdet - 3]->detectorType());
0309         cell = recoLayerCell.first;
0310         layer = recoLayerCell.second;
0311         // skip simhits with bad barcodes or non-existant layers
0312         if (layer == -1 || simHit.geantTrackId() == 0)
0313           continue;
0314         id = HGCalDetId((ForwardSubdetector)subdet, zp, layer, subsec, sec, cell);
0315       }
0316 
0317       if (DetId(0) == id)
0318         continue;
0319 
0320       detIdToTotalSimEnergy[id.rawId()] += simHit.energy();
0321     }
0322   }  // end of loop over InputTags
0323 }
0324 
0325 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0326 void CaloParticleDebugger::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0327   edm::ParameterSetDescription desc;
0328   desc.add<edm::InputTag>("simTracks", edm::InputTag("g4SimHits"));
0329   desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
0330   desc.add<edm::InputTag>("genBarcodes", edm::InputTag("genParticles"));
0331   desc.add<edm::InputTag>("simVertices", edm::InputTag("g4SimHits"));
0332   desc.add<edm::InputTag>("trackingParticles", edm::InputTag("mix", "MergedTrackTruth"));
0333   desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0334   desc.add<edm::InputTag>("simClusters", edm::InputTag("mix", "MergedCaloTruth"));
0335   desc.add<std::vector<edm::InputTag>>("collectionTags",
0336                                        {edm::InputTag("g4SimHits", "HGCHitsEE"),
0337                                         edm::InputTag("g4SimHits", "HGCHitsHEfront"),
0338                                         edm::InputTag("g4SimHits", "HcalHits")});
0339   descriptions.add("caloParticleDebugger", desc);
0340 }
0341 
0342 // define this as a plug-in
0343 DEFINE_FWK_MODULE(CaloParticleDebugger);