Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-22 06:27:47

0001 // -*- C++ -*-
0002 //
0003 //
0004 // Authors:  Marco Rovere, Andreas Gruber
0005 //         Created:  Mon, 16 Oct 2023 14:24:35 GMT
0006 //
0007 //
0008 
0009 // system include files
0010 #include <memory>
0011 
0012 // user include files
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/global/EDProducer.h"
0015 
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/Utilities/interface/StreamID.h"
0021 #include "FWCore/Utilities/interface/InputTag.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 
0024 #include "DataFormats/HGCalReco/interface/TICLCandidate.h"
0025 #include "SimDataFormats/CaloAnalysis/interface/CaloParticleFwd.h"
0026 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0027 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0028 #include "SimDataFormats/CaloAnalysis/interface/SimTauCPLink.h"
0029 
0030 class SimTauProducer : public edm::global::EDProducer<> {
0031 public:
0032   explicit SimTauProducer(const edm::ParameterSet&);
0033   ~SimTauProducer() override = default;
0034 
0035   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0036 
0037 private:
0038   void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0039 
0040   void buildSimTau(SimTauCPLink&,
0041                    uint8_t,
0042                    int,
0043                    const reco::GenParticle&,
0044                    int,
0045                    edm::Handle<std::vector<CaloParticle>>,
0046                    const std::vector<int>&) const;
0047   // ----------member data ---------------------------
0048   const edm::EDGetTokenT<std::vector<CaloParticle>> caloParticles_token_;
0049   const edm::EDGetTokenT<std::vector<reco::GenParticle>> genParticles_token_;
0050   const edm::EDGetTokenT<std::vector<int>> genBarcodes_token_;
0051 };
0052 
0053 SimTauProducer::SimTauProducer(const edm::ParameterSet& iConfig)
0054     : caloParticles_token_(consumes<std::vector<CaloParticle>>(iConfig.getParameter<edm::InputTag>("caloParticles"))),
0055       genParticles_token_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("genParticles"))),
0056       genBarcodes_token_(consumes<std::vector<int>>(iConfig.getParameter<edm::InputTag>("genBarcodes"))) {
0057   produces<std::vector<SimTauCPLink>>();
0058 }
0059 
0060 void SimTauProducer::buildSimTau(SimTauCPLink& t,
0061                                  uint8_t generation,
0062                                  int resonance_idx,
0063                                  const reco::GenParticle& gen_particle,
0064                                  int gen_particle_key,
0065                                  edm::Handle<std::vector<CaloParticle>> calo_particle_h,
0066                                  const std::vector<int>& gen_particle_barcodes) const {
0067   const auto& caloPartVec = *calo_particle_h;
0068   auto& daughters = gen_particle.daughterRefVector();
0069   bool is_leaf = (daughters.empty());
0070 
0071   if (is_leaf) {
0072     auto const& gen_particle_barcode = gen_particle_barcodes[gen_particle_key];
0073     LogDebug("SimTauProducer")
0074         .format(" TO BE SAVED {}, key {}, barcode {}", resonance_idx, gen_particle_key, gen_particle_barcode);
0075     auto const& found_in_caloparticles = std::find_if(caloPartVec.begin(), caloPartVec.end(), [&](const auto& p) {
0076       return p.g4Tracks()[0].genpartIndex() == gen_particle_barcode;
0077     });
0078     if (found_in_caloparticles != caloPartVec.end()) {
0079       auto calo_particle_idx = (found_in_caloparticles - caloPartVec.begin());
0080       t.calo_particle_leaves.push_back(CaloParticleRef(calo_particle_h, calo_particle_idx));
0081       t.leaves.push_back(
0082           {gen_particle.pdgId(), resonance_idx, (int)t.calo_particle_leaves.size() - 1, gen_particle_key});
0083       LogDebug("SimTauProducer").format(" CP {} {}", calo_particle_idx, caloPartVec[calo_particle_idx].pdgId());
0084     } else {
0085       t.leaves.push_back({gen_particle.pdgId(), resonance_idx, -1, gen_particle_key});
0086     }
0087     return;
0088   } else if (generation != 0) {
0089     t.resonances.push_back({gen_particle.pdgId(), resonance_idx});
0090     resonance_idx = t.resonances.size() - 1;
0091     LogDebug("SimTauProducer").format(" RESONANCE/INTERMEDIATE {}", resonance_idx);
0092   }
0093 
0094   ++generation;
0095   for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) {
0096     int gen_particle_key = (*daughter).key();
0097     LogDebug("SimTauProducer").format(" gen {} {} {}", generation, gen_particle_key, (*daughter)->pdgId());
0098     buildSimTau(t, generation, resonance_idx, *(*daughter), gen_particle_key, calo_particle_h, gen_particle_barcodes);
0099   }
0100 }
0101 
0102 void SimTauProducer::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const& iSetup) const {
0103   using namespace edm;
0104 
0105   auto caloParticles_h = iEvent.getHandle(caloParticles_token_);
0106   const auto& genParticles = iEvent.get(genParticles_token_);
0107   const auto& genBarcodes = iEvent.get(genBarcodes_token_);
0108 
0109   auto tauDecayVec = std::make_unique<std::vector<SimTauCPLink>>();
0110   for (auto const& g : genParticles) {
0111     auto const& flags = g.statusFlags();
0112     if (std::abs(g.pdgId()) == 15 and flags.isPrompt() and flags.isDecayedLeptonHadron()) {
0113       SimTauCPLink t;
0114       buildSimTau(t, 0, -1, g, -1, caloParticles_h, genBarcodes);
0115       t.decayMode = t.buildDecayModes();
0116 #ifdef EDM_ML_DEBUG
0117       t.dumpFullDecay();
0118       t.dump();
0119 #endif
0120       (*tauDecayVec).push_back(t);
0121     }
0122   }
0123   iEvent.put(std::move(tauDecayVec));
0124 }
0125 
0126 void SimTauProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0127   edm::ParameterSetDescription desc;
0128   desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0129   desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
0130   desc.add<edm::InputTag>("genBarcodes", edm::InputTag("genParticles"));
0131   descriptions.add("SimTauProducer", desc);
0132 }
0133 
0134 DEFINE_FWK_MODULE(SimTauProducer);