Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-26 17:54:27

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     LogDebug("SimTauProducer").format(" TO BE SAVED {} ", resonance_idx);
0073     auto const& gen_particle_barcode = gen_particle_barcodes[gen_particle_key];
0074     auto const& found_in_caloparticles = std::find_if(caloPartVec.begin(), caloPartVec.end(), [&](const auto& p) {
0075       return p.g4Tracks()[0].genpartIndex() == gen_particle_barcode;
0076     });
0077     if (found_in_caloparticles != caloPartVec.end()) {
0078       auto calo_particle_idx = (found_in_caloparticles - caloPartVec.begin());
0079       t.calo_particle_leaves.push_back(CaloParticleRef(calo_particle_h, calo_particle_idx));
0080       t.leaves.push_back(
0081           {gen_particle.pdgId(), resonance_idx, (int)t.calo_particle_leaves.size() - 1, gen_particle_key});
0082       LogDebug("SimTauProducer").format(" CP {} {}", calo_particle_idx, caloPartVec[calo_particle_idx].pdgId());
0083     } else {
0084       t.leaves.push_back({gen_particle.pdgId(), resonance_idx, -1, gen_particle_key});
0085     }
0086     return;
0087   } else if (generation != 0) {
0088     t.resonances.push_back({gen_particle.pdgId(), resonance_idx});
0089     resonance_idx = t.resonances.size() - 1;
0090     LogDebug("SimTauProducer").format(" RESONANCE/INTERMEDIATE {} ", resonance_idx);
0091   }
0092 
0093   ++generation;
0094   for (auto daughter = daughters.begin(); daughter != daughters.end(); ++daughter) {
0095     int gen_particle_key = (*daughter).key();
0096     LogDebug("SimTauProducer").format(" gen {} {} {} ", generation, gen_particle_key, (*daughter)->pdgId());
0097     buildSimTau(t, generation, resonance_idx, *(*daughter), gen_particle_key, calo_particle_h, gen_particle_barcodes);
0098   }
0099 }
0100 
0101 void SimTauProducer::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const& iSetup) const {
0102   using namespace edm;
0103 
0104   auto caloParticles_h = iEvent.getHandle(caloParticles_token_);
0105   const auto& genParticles = iEvent.get(genParticles_token_);
0106   const auto& genBarcodes = iEvent.get(genBarcodes_token_);
0107 
0108   auto tauDecayVec = std::make_unique<std::vector<SimTauCPLink>>();
0109   for (auto const& g : genParticles) {
0110     auto const& flags = g.statusFlags();
0111     if (std::abs(g.pdgId()) == 15 and flags.isPrompt() and flags.isDecayedLeptonHadron()) {
0112       SimTauCPLink t;
0113       buildSimTau(t, 0, -1, g, -1, caloParticles_h, genBarcodes);
0114       t.decayMode = t.buildDecayModes();
0115 #ifdef EDM_ML_DEBUG
0116       t.dumpFullDecay();
0117       t.dump();
0118 #endif
0119       (*tauDecayVec).push_back(t);
0120     }
0121   }
0122   iEvent.put(std::move(tauDecayVec));
0123 }
0124 
0125 void SimTauProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0126   edm::ParameterSetDescription desc;
0127   desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0128   desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
0129   desc.add<edm::InputTag>("genBarcodes", edm::InputTag("genParticles"));
0130   descriptions.add("SimTauProducer", desc);
0131 }
0132 
0133 DEFINE_FWK_MODULE(SimTauProducer);