diff --git a/SimGeneral/Debugging/plugins/SimTauAnalyzer.cc b/SimGeneral/Debugging/plugins/SimTauAnalyzer.cc new file mode 100644 index 0000000000000..9c2b2b950fe28 --- /dev/null +++ b/SimGeneral/Debugging/plugins/SimTauAnalyzer.cc @@ -0,0 +1,293 @@ +// -*- C++ -*- +// +// +// Original Author: Andreas Gruber +// Created: Mon, 16 Oct 2023 14:24:35 GMT +// +// + +// system include files +#include + +// user include files +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/one/EDAnalyzer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/InputTag.h" +#include "DataFormats/TrackReco/interface/Track.h" +#include "DataFormats/TrackReco/interface/TrackFwd.h" + +#include "DataFormats/HGCalReco/interface/TICLCandidate.h" +#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h" +#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h" + +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "CommonTools/UtilAlgos/interface/TFileService.h" + +#define DEBUG 0 + +struct TauDecay { + std::vector> resonances; + std::vector> leaves; + + void dump(void) { + for (auto const & l : leaves) { + std::cout << "L " << l.first << " " << l.second << std::endl; + } + for (auto const & r : resonances) { + std::cout << "R " << r.first << " " << r.second << std::endl; + } + } + + void dumpDecay(const std::pair & entry) { + if (entry.second == -1) { // No intermediate mother. + std::cout << entry.first << " " << entry.second << std::endl;; + } else { + std::cout << entry.first << " " << entry.second << " coming from: "; + auto const &mother = resonances[entry.second]; + dumpDecay(mother); + } + } + + void dumpFullDecay(void) { + for (auto const & leaf : leaves) { + dumpDecay(leaf); + } + } +}; + +class SimTauAnalyzer : public edm::one::EDAnalyzer<> { +public: + + explicit SimTauAnalyzer(const edm::ParameterSet&); + ~SimTauAnalyzer() = default; + + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + +private: + void beginJob() override; + void analyze(const edm::Event&, const edm::EventSetup&) override; + void endJob() override; + + void buildSimTau(TauDecay &, uint8_t, int, const reco::GenParticle &, const std::vector &); + // ----------member data --------------------------- + const edm::EDGetTokenT> caloParticle_token_; + const edm::EDGetTokenT> simClusters_token_; + const edm::EDGetTokenT> genParticles_token_; + const edm::EDGetTokenT> genBarcodes_token_; + //edm::Ref matchedCPs; +}; + +// +// static data member definitions +// + +// +// constructors and destructor +// +SimTauAnalyzer::SimTauAnalyzer(const edm::ParameterSet& iConfig) + : caloParticle_token_(consumes>(iConfig.getParameter("CaloParticle"))), + simClusters_token_(consumes>(iConfig.getParameter("SimClusters"))), + genParticles_token_(consumes(iConfig.getParameter("GenParticles"))), + genBarcodes_token_(consumes>(iConfig.getParameter("genParticles"))) {} + + +void SimTauAnalyzer::buildSimTau(TauDecay &t, + uint8_t generation, + int resonance_idx, + const reco::GenParticle & gen_particle, // the decaying tau, + const std::vector &CaloPartVec) { + + auto &daughters = gen_particle.daughterRefVector(); + bool is_leaf = (daughters.size() == 0); + if (is_leaf) { + if (DEBUG) + std::cout << " TO BE SAVED " << resonance_idx << " "; + t.leaves.push_back({gen_particle.pdgId(), resonance_idx}); + return; + } else if (generation !=0) { + t.resonances.push_back({gen_particle.pdgId(), resonance_idx}); + resonance_idx = t.resonances.size() - 1; + if (DEBUG) + std::cout << " RESONANCE/ITERMEDIATE " << resonance_idx << " "; + } + if (DEBUG) + std::cout << std::endl; + + ++generation; + std::string separator(""); + for (uint8_t c = 0; c < generation; ++c) { + separator += " "; + } + for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) { + auto const & daughter_flags = (*daughter)->statusFlags(); + if (DEBUG) { + std::cout << separator << " gen " << (int)generation << " " << (*daughter)->pdgId() << " "; + for (unsigned int bit = 0; bit <= reco::GenStatusFlags::kIsLastCopyBeforeFSR; ++bit) { + std::cout << daughter_flags.flags_[bit] << " "; + } + } + buildSimTau(t, generation, resonance_idx, *(*daughter), CaloPartVec); + if (DEBUG) + std::cout << std::endl; + } +} + +/*edm::Ref SimTauAnalyzer::generatorRef_(const SimTrack &simtrk, const GlobalContext &g) const { + assert(simtrk.genpartIndex() != -1); + // Note that simtrk.genpartIndex() is the barcode, not the index within GenParticleCollection, so I have to search the particle + std::vector::const_iterator it; + if (gg.barcodesAreSorted) { + it = std::lower_bound(g.genBarcodes->begin(), g.genBarcodes->end(), simtrk.genpartIndex()); + } else { + it = std::find(g.genBarcodes->begin(), g.genBarcodes->end(), simtrk.genpartIndex()); + } + // Check that I found something + // I need to check '*it == simtrk.genpartIndex()' because lower_bound just finds the right spot for an item in a sorted list, not the item + if ((it != g.genBarcodes->end()) && (*it == simtrk.genpartIndex())) { + return reco::GenParticleRef(g.gens, it - g.genBarcodes->begin()); + } else { + return reco::GenParticleRef(); + } +} + +int SimTauAnalyzer::findGenPartIdx (const reco::GenParticle &c) { + +}*/ +// +// member functions +// + +// ------------ method called for each event ------------ +void SimTauAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) { + using namespace edm; + + Handle> CaloParticle_h; + iEvent.getByToken(caloParticle_token_, CaloParticle_h); + // Handle> SimClusters_h; + // iEvent.getByToken(simClusters_token_, SimClusters_h); + edm::Handle> gen_particles_h; + iEvent.getByToken(genParticles_token_, gen_particles_h); + // Handle> gen_barcodes_h; + // iEvent.getByToken(genBarcodes_token_, gen_barcodes_h); + + const auto& caloParticle = *CaloParticle_h; + // const auto& SimClusters = *SimClusters_h; + const auto& genParticles = *gen_particles_h; + // const auto& genBarcodes = *gen_barcodes_h; + + int counter=0; + for (auto const & g : genParticles) { + auto const & flags = g.statusFlags(); + if (std::abs(g.pdgId()) == 15 and flags.isPrompt() and flags.isDecayedLeptonHadron()) { + if (DEBUG) { + std::cout << iEvent.eventAuxiliary().event() << " " << counter++ << " " + << " " << g.pdgId() << " "; + for (unsigned int bit = 0; bit <= reco::GenStatusFlags::kIsLastCopyBeforeFSR; ++bit) { + std::cout << flags.flags_[bit] << " "; + } + std::cout << std::endl; + } + TauDecay t; + buildSimTau(t, 0, -1, g, caloParticle); + t.dumpFullDecay(); + // if (std::abs(g.pdgId()) == 15) { + // auto &daughters = g.daughterRefVector(); + // for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) { + // auto const & daughter_flags = (*daughter)->statusFlags(); + // std::cout << "\t\t" << (*daughter)->pdgId() << " "; + // for (unsigned int bit = 0; bit <= reco::GenStatusFlags::kIsLastCopyBeforeFSR; ++bit) { + // std::cout << daughter_flags.flags_[bit] << " "; + // } + // std::cout << std::endl; + // } + // } + } + } +} +// /*for (const auto& genBarcode: genBarcodes) { +// std::cout << "GenParticle Barcode: " << genBarcode << std::endl; +// +// auto &daughters = genParticle.daughterRefVector(); +// for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) { +// daughter.genParticleId(); +// +// }*/ +// int generation=0; +// for (const auto& gen_particle: gen_particles) { +// if (abs(gen_particle.pdgId()) == 15) { +// +// // if 15 is in the list of dauthers, it means that the original +// // Tau went through some brem or similar process. We ignore all the +// // intermediate products (dauthers_ and just keep on following the tau) +// auto const &daughters = gen_particles.daughterRefVector(); +// auto intermediate_tau = std::find(daughters.begin(), daughters.end(), [](const auto &d) {return d.pdgId()==15); +// if (intermediate_tau != daughters.end()) { +// for (const auto& gen_particle: genParticles) { +// if (abs(gen_particle.pdgId()) == 15) { +// recursion_n=0; +// std::cout << "Found Tau #" << tau_n << "!" << std::endl; +// buildSimTau(generation, gen_particle, CaloParticle); +// tau_n++; +// } +// } +// +// /* for (const auto& CP: CaloParticle) { +// std::cout << "SimTrack size: " << CP.g4Tracks().size() << std::endl; +// std::cout << "SimTrack GenPartIndex: " << CP.g4Tracks()[0].genpartIndex() << std::endl; +// assert(CP.g4Tracks()[0].genpartIndex() != -1); +// // Note that simtrk.genpartIndex() is the barcode, not the index within GenParticleCollection, so I have to search the particle +// std::vector::const_iterator it; +// it = std::find(genBarcodes.begin(), genBarcodes.end(), CP.g4Tracks()[0].genpartIndex()); +// if ((it != genBarcodes.end())) { +// auto gen_particle = reco::GenParticleRef(gen_particles_h, it - genBarcodes.begin()); +// auto genPartReturn = *gen_particle; +// std::cout << genPartReturn.pdgId() << std::endl; +// +// } else { +// std::cout << "no genParticle found" << std::endl; +// } +// } +// } +// +// +// int simTrackGenIndex = CP.g4Tracks()[0].genpartIndex(); +// std::cout << "simTrack Gen Index: " << simTrackGenIndex << std::endl; +// std::cout << "CP pdgId: " << CP.pdgId() << std::endl; +// if (simTrackGenIndex < static_cast(genParticles.size())) { +// findGenTau(genParticles[simTrackGenIndex]); +// if (CP.g4Tracks()[0].genpartIndex() == 15) {*/ +// +//} + +// ------------ method called once each job just before starting event loop ------------ +void SimTauAnalyzer::beginJob() { + // please remove this method if not needed +} + +// ------------ method called once each job just after ending the event loop ------------ +void SimTauAnalyzer::endJob() { + // please remove this method if not needed +} + +// ------------ method fills 'descriptions' with the allowed parameters for the module ------------ +void SimTauAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + //The following says we do not know what parameters are allowed so do no validation + // Please change this to state exactly what you do use, even if it is no parameters + edm::ParameterSetDescription desc; + desc.setUnknown(); + descriptions.addDefault(desc); + + //Specify that only 'tracks' is allowed + //To use, remove the default given above and uncomment below + //ParameterSetDescription desc; + //desc.addUntracked("tracks","ctfWithMaterialTracks"); + //descriptions.addWithDefaultLabel(desc); +} + +//define this as a plug-in +DEFINE_FWK_MODULE(SimTauAnalyzer); diff --git a/SimGeneral/Debugging/plugins/patatrackAnalyzer.cc b/SimGeneral/Debugging/plugins/patatrackAnalyzer.cc deleted file mode 100644 index 760ad9c9ae3b8..0000000000000 --- a/SimGeneral/Debugging/plugins/patatrackAnalyzer.cc +++ /dev/null @@ -1,255 +0,0 @@ -// -*- C++ -*- -// -// Package: patatrackAnalyzer/patatrackAnalyzer -// Class: patatrackAnalyzer -// -/**\class patatrackAnalyzer patatrackAnalyzer.cc patatrackAnalyzer/patatrackAnalyzer/plugins/patatrackAnalyzer.cc - - Description: [one line class summary] - - Implementation: - [Notes on implementation] -*/ -// -// Original Author: Andreas Gruber -// Created: Mon, 16 Oct 2023 14:24:35 GMT -// -// - -// system include files -#include - -// user include files -#include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/one/EDAnalyzer.h" - -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/MakerMacros.h" - -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/Utilities/interface/InputTag.h" -#include "DataFormats/TrackReco/interface/Track.h" -#include "DataFormats/TrackReco/interface/TrackFwd.h" - -#include "DataFormats/HGCalReco/interface/TICLCandidate.h" -#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h" -#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h" - -#include "FWCore/ServiceRegistry/interface/Service.h" -#include "CommonTools/UtilAlgos/interface/TFileService.h" - -#include "genTau.h" - - -// -// class declaration -// - -// If the analyzer does not use TFileService, please remove -// the template argument to the base class so the class inherits -// from edm::one::EDAnalyzer<> -// This will improve performance in multithreaded jobs. - -using reco::TrackCollection; - -class patatrackAnalyzer : public edm::one::EDAnalyzer { -public: - explicit patatrackAnalyzer(const edm::ParameterSet&); - ~patatrackAnalyzer() override; - - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - -private: - void beginJob() override; - void analyze(const edm::Event&, const edm::EventSetup&) override; - void endJob() override; - - void buildSimTau (int, const reco::GenParticle &, const std::vector &); - const edm::EDGetTokenT> CaloParticle_token; - const edm::EDGetTokenT> SimClusters_token; - const edm::EDGetTokenT> genParticles_; - const edm::EDGetTokenT> genBarcodesToken_; - - int recursion_n; - int daughter_n; - edm::Ref matchedCPs; - // ----------member data --------------------------- -#ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE - edm::ESGetToken setupToken_; -#endif -}; - -// -// constants, enums and typedefs -// - -// -// static data member definitions -// - -// -// constructors and destructor -// -patatrackAnalyzer::patatrackAnalyzer(const edm::ParameterSet& iConfig) - : CaloParticle_token(consumes>(iConfig.getParameter("CaloParticle"))), - SimClusters_token(consumes>(iConfig.getParameter("SimClusters"))), - genParticles_(consumes(iConfig.getParameter("GenParticles"))), - genBarcodesToken_(mayConsume>(iConfig.getParameter("genParticles"))) - { - - edm::Service fs; - -#ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE - setupDataToken_ = esConsumes(); -#endif - //now do what ever initialization is needed -} - -patatrackAnalyzer::~patatrackAnalyzer() { - // do anything here that needs to be done at desctruction time - // (e.g. close files, deallocate resources etc.) - // - // please remove this method altogether if it would be left empty -} - -void patatrackAnalyzer::buildSimTau (int n, const reco::GenParticle &genPart, const std::vector &CaloPartVec) { - auto &daughters = genPart.daughterRefVector(); - //if(daughters.empty()){ - n++; - //} - - for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) { - if (abs((*daughter)->pdgId()) == 15) { - break; - } - std::cout << "recursion #: " << n << std::endl; - std::cout << (*daughter)->pdgId() << std::endl; - //recursion_n++; - buildSimTau(n, *(*daughter), CaloPartVec); - //std::cout << "Printing daughters of" << ge - } -} - -/*edm::Ref patatrackAnalyzer::generatorRef_(const SimTrack &simtrk, const GlobalContext &g) const { - assert(simtrk.genpartIndex() != -1); - // Note that simtrk.genpartIndex() is the barcode, not the index within GenParticleCollection, so I have to search the particle - std::vector::const_iterator it; - if (gg.barcodesAreSorted) { - it = std::lower_bound(g.genBarcodes->begin(), g.genBarcodes->end(), simtrk.genpartIndex()); - } else { - it = std::find(g.genBarcodes->begin(), g.genBarcodes->end(), simtrk.genpartIndex()); - } - // Check that I found something - // I need to check '*it == simtrk.genpartIndex()' because lower_bound just finds the right spot for an item in a sorted list, not the item - if ((it != g.genBarcodes->end()) && (*it == simtrk.genpartIndex())) { - return reco::GenParticleRef(g.gens, it - g.genBarcodes->begin()); - } else { - return reco::GenParticleRef(); - } -} - -int patatrackAnalyzer::findGenPartIdx (const reco::GenParticle &c) { - -}*/ -// -// member functions -// - -// ------------ method called for each event ------------ -void patatrackAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) { - using namespace edm; - edm::Handle> CaloParticle_h; - iEvent.getByToken(CaloParticle_token, CaloParticle_h); - const auto& CaloParticle = *CaloParticle_h; - edm::Handle> SimClusters_h; - iEvent.getByToken(SimClusters_token, SimClusters_h); - const auto& SimClusters = *SimClusters_h; - edm::Handle> gen_particles_h; - iEvent.getByToken(genParticles_, gen_particles_h); - const auto& genParticles = *gen_particles_h; - edm::Handle> gen_barcodes_h; - iEvent.getByToken(genBarcodesToken_, gen_barcodes_h); - const auto& genBarcodes = *gen_barcodes_h; - - genTau test; - std::cout << test.getSize() << std::endl; - - /*for (const auto& genBarcode: genBarcodes) { - std::cout << "GenParticle Barcode: " << genBarcode << std::endl; - - auto &daughters = genParticle.daughterRefVector(); - for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) { - daughter.genParticleId(); - - }*/ - int tau_n=0; - for (const auto& GP: genParticles) { - if (abs(GP.pdgId()) == 15) { - recursion_n=0; - std::cout << "Found Tau #" << tau_n << "!" << std::endl; - buildSimTau(tau_n, GP, CaloParticle); - tau_n++; - } - } - - /* for (const auto& CP: CaloParticle) { - std::cout << "SimTrack size: " << CP.g4Tracks().size() << std::endl; - std::cout << "SimTrack GenPartIndex: " << CP.g4Tracks()[0].genpartIndex() << std::endl; - assert(CP.g4Tracks()[0].genpartIndex() != -1); - // Note that simtrk.genpartIndex() is the barcode, not the index within GenParticleCollection, so I have to search the particle - std::vector::const_iterator it; - it = std::find(genBarcodes.begin(), genBarcodes.end(), CP.g4Tracks()[0].genpartIndex()); - if ((it != genBarcodes.end())) { - auto GP = reco::GenParticleRef(gen_particles_h, it - genBarcodes.begin()); - auto genPartReturn = *GP; - std::cout << genPartReturn.pdgId() << std::endl; - - } else { - std::cout << "no genParticle found" << std::endl; - } - } - } - - - int simTrackGenIndex = CP.g4Tracks()[0].genpartIndex(); - std::cout << "simTrack Gen Index: " << simTrackGenIndex << std::endl; - std::cout << "CP pdgId: " << CP.pdgId() << std::endl; - if (simTrackGenIndex < static_cast(genParticles.size())) { - findGenTau(genParticles[simTrackGenIndex]); - if (CP.g4Tracks()[0].genpartIndex() == 15) {*/ - -#ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE - // if the SetupData is always needed - auto setup = iSetup.getData(setupToken_); - // if need the ESHandle to check if the SetupData was there or not - auto pSetup = iSetup.getHandle(setupToken_); -#endif -} - -// ------------ method called once each job just before starting event loop ------------ -void patatrackAnalyzer::beginJob() { - // please remove this method if not needed -} - -// ------------ method called once each job just after ending the event loop ------------ -void patatrackAnalyzer::endJob() { - // please remove this method if not needed -} - -// ------------ method fills 'descriptions' with the allowed parameters for the module ------------ -void patatrackAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - //The following says we do not know what parameters are allowed so do no validation - // Please change this to state exactly what you do use, even if it is no parameters - edm::ParameterSetDescription desc; - desc.setUnknown(); - descriptions.addDefault(desc); - - //Specify that only 'tracks' is allowed - //To use, remove the default given above and uncomment below - //ParameterSetDescription desc; - //desc.addUntracked("tracks","ctfWithMaterialTracks"); - //descriptions.addWithDefaultLabel(desc); -} - -//define this as a plug-in -DEFINE_FWK_MODULE(patatrackAnalyzer); diff --git a/SimGeneral/Debugging/test/ConfFile_cfg.py b/SimGeneral/Debugging/test/simTauDecayDump_cfg.py similarity index 65% rename from SimGeneral/Debugging/test/ConfFile_cfg.py rename to SimGeneral/Debugging/test/simTauDecayDump_cfg.py index 23bb5be67cbf8..e79a69d2a1d44 100644 --- a/SimGeneral/Debugging/test/ConfFile_cfg.py +++ b/SimGeneral/Debugging/test/simTauDecayDump_cfg.py @@ -1,29 +1,26 @@ import FWCore.ParameterSet.Config as cms import sys -process = cms.Process("patatrackAnalyzer") +process = cms.Process("SimTauAnalyzer") process.load("FWCore.MessageService.MessageLogger_cfi") -process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(1) +process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) ) process.source = cms.Source("PoolSource", fileNames = cms.untracked.vstring( - 'file:/data/agruber/patatrack/CMSSW_13_2_0_pre3/src/24036.0_ZTT_14TeV+2026D96/step3.root' + 'file:/data/rovere/HGCAL/SimTau/CMSSW_13_3_0_pre3/24036.0_ZTT_14TeV+2026D96/step2.root', +# 'file:/data/agruber/patatrack/CMSSW_13_2_0_pre3/src/24036.0_ZTT_14TeV+2026D96/step3.root' #'file:/eos/user/a/agruber/tauRecoTICL/patatrack14/CMSSW_13_2_0_pre3/src/24088.0_SinglePiPt25Eta1p7_2p7+2026D96/step3.root' #'file:/eos/user/a/agruber/samples/TICLDumperTest/new/zpos/n10000/Eta_29/singletau_flatEGun_hgcalCenter/step3/step3clue3D_singletau_e100GeV_eta29_zpos_events10000_nopu.root' - ) + ) ) -process.TFileService = cms.Service("TFileService", - fileName = cms.string('patatrack_testing.root') - ) - -process.patatrackAnalyzer = cms.EDAnalyzer('patatrackAnalyzer', +process.SimTauAnalyzer = cms.EDAnalyzer('SimTauAnalyzer', CaloParticle = cms.InputTag('mix', 'MergedCaloTruth'), SimClusters = cms.InputTag('mix', 'MergedCaloTruth'), GenParticles = cms.InputTag('genParticles'), genParticles = cms.InputTag('genParticles') ) -process.p = cms.Path(process.patatrackAnalyzer) +process.p = cms.Path(process.SimTauAnalyzer)