Skip to content

Commit

Permalink
added SimTauAnalyzer for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
agrubercms committed Nov 20, 2023
1 parent 5ea668c commit 80a542c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 177 deletions.
189 changes: 18 additions & 171 deletions SimGeneral/Debugging/plugins/SimTauAnalyzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,207 +12,54 @@
// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/ConsumesCollector.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/CaloParticleFwd.h"
#include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
#include "SimDataFormats/CaloAnalysis/interface/SimTauDecayCaloParticle.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"

#define DEBUG 1

struct TauDecay {
struct DecayNav {
int pdgId;
int resonance_idx;
int calo_particle_idx;
int first() const { return pdgId; }
int second() const { return resonance_idx; }
int third() const { return calo_particle_idx; }
};

std::vector<std::pair<int, int>> resonances;
std::vector<DecayNav> leaves;
CaloParticleRefVector calo_particle_leaves;

void dump(void) {
for (auto const & l : leaves) {
std::cout << "L " << l.first() << " " << l.second() << " CP " << l.third() << std::endl;
}
for (auto const & r : resonances) {
std::cout << "R " << r.first << " " << r.second << std::endl;
}
}

void dumpDecay(const std::pair<int, int> & 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.first(), leaf.second()});
}
}
};

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 &, int, edm::Handle<std::vector<CaloParticle>>, const std::vector<int> &);
// ----------member data ---------------------------
const edm::EDGetTokenT<std::vector<CaloParticle>> caloParticle_token_;
const edm::EDGetTokenT<std::vector<SimCluster>> simClusters_token_;
const edm::EDGetTokenT<std::vector<reco::GenParticle>> genParticles_token_;
const edm::EDGetTokenT<std::vector<int>> genBarcodes_token_;

const edm::EDGetTokenT<std::vector<SimTauDecayCaloParticle>> simTau_token_;
};

SimTauAnalyzer::SimTauAnalyzer(const edm::ParameterSet& iConfig)
: caloParticle_token_(consumes<std::vector<CaloParticle>>(iConfig.getParameter<edm::InputTag>("CaloParticle"))),
simClusters_token_(consumes<std::vector<SimCluster>>(iConfig.getParameter<edm::InputTag>("SimClusters"))),
genParticles_token_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("GenParticles"))),
genBarcodes_token_(consumes<std::vector<int>>(iConfig.getParameter<edm::InputTag>("genParticles"))) {}


void SimTauAnalyzer::buildSimTau(TauDecay &t,
uint8_t generation,
int resonance_idx,
const reco::GenParticle & gen_particle,
int gen_particle_key,
edm::Handle<std::vector<CaloParticle>> calo_particle_h,
const std::vector<int> & gen_particle_barcodes) {

const auto& caloPartVec = *calo_particle_h;
auto &daughters = gen_particle.daughterRefVector();
bool is_leaf = (daughters.size() == 0);
if (is_leaf) {
if (DEBUG)
std::cout << " TO BE SAVED " << resonance_idx << " ";
auto const & gen_particle_barcode = gen_particle_barcodes[gen_particle_key];
auto const & found_in_caloparticles = std::find_if(
caloPartVec.begin(),
caloPartVec.end(),
[&](const auto &p) {
return p.g4Tracks()[0].genpartIndex() == gen_particle_barcode;}
);
if (found_in_caloparticles != caloPartVec.end()) {
auto calo_particle_idx = (found_in_caloparticles - caloPartVec.begin());
t.calo_particle_leaves.push_back(CaloParticleRef(calo_particle_h, calo_particle_idx));
t.leaves.push_back({gen_particle.pdgId(), resonance_idx, (int)t.calo_particle_leaves.size()-1});
if (DEBUG)
std::cout << " CP " << calo_particle_idx << " " << caloPartVec[calo_particle_idx];
} else {
t.leaves.push_back({gen_particle.pdgId(), resonance_idx,-1});
}
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();
int gen_particle_key = (*daughter).key();
if (DEBUG) {
std::cout << separator << " gen " << (int)generation << " " << gen_particle_key << " " << (*daughter)->pdgId() << " ";
for (unsigned int bit = 0; bit <= reco::GenStatusFlags::kIsLastCopyBeforeFSR; ++bit) {
std::cout << daughter_flags.flags_[bit] << " ";
}
}
buildSimTau(t, generation, resonance_idx, *(*daughter), gen_particle_key, calo_particle_h, gen_particle_barcodes);
if (DEBUG)
std::cout << std::endl;
}
}
: simTau_token_(consumes<std::vector<SimTauDecayCaloParticle>>(iConfig.getParameter<edm::InputTag>("simTau"))) {}

// ------------ method called for each event ------------
void SimTauAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
using namespace edm;

Handle<std::vector<CaloParticle>> CaloParticle_h;
iEvent.getByToken(caloParticle_token_, CaloParticle_h);

edm::Handle<std::vector<reco::GenParticle>> gen_particles_h;
iEvent.getByToken(genParticles_token_, gen_particles_h);

Handle<std::vector<int>> gen_barcodes_h;
iEvent.getByToken(genBarcodes_token_, gen_barcodes_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, -1, CaloParticle_h, genBarcodes);
t.dumpFullDecay();
t.dump();
}
}
}

// ------------ method called once each job just before starting event loop ------------
void SimTauAnalyzer::beginJob() {
// please remove this method if not needed
}
edm::Handle<std::vector<SimTauDecayCaloParticle>> simTau_h;
iEvent.getByToken(simTau_token_, simTau_h);

// ------------ method called once each job just after ending the event loop ------------
void SimTauAnalyzer::endJob() {
// please remove this method if not needed
}
const auto& simTaus = *simTau_h;

// ------------ 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);
for (auto const & simTau : simTaus) {
simTau.dumpFullDecay();
simTau.dump();
int counter =0;
for (auto const & CP: simTau.calo_particle_leaves) {
std::cout << "Matched CP #" << counter << " pdgId: " << CP->pdgId() << std::endl;
++counter;
}
}
}

//define this as a plug-in
//DEFINE_FWK_MODULE(SimTauAnalyzer);
DEFINE_FWK_MODULE(SimTauAnalyzer);
34 changes: 34 additions & 0 deletions SimGeneral/Debugging/test/simTauAnalyzer_cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import FWCore.ParameterSet.Config as cms
import sys

process = cms.Process("SimTauAnalyzer")

process.load("FWCore.MessageService.MessageLogger_cfi")

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1)
)
process.source = cms.Source("PoolSource",
fileNames = cms.untracked.vstring(
#'file:test_input.root'
#'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24127.0_QQToHToTauTau_14TeV+2026D96/step2.root'
#'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24034.0_TTbar_14TeV+2026D96/step3.root'
#'file:/data/rovere/HGCAL/SimTau/CMSSW_13_3_0_pre3/24036.0_ZTT_14TeV+2026D96/step2.root',
'file:SimTauProducer_test.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('test.root')
)

process.SimTauAnalyzer = cms.EDAnalyzer('SimTauAnalyzer',
CaloParticle = cms.InputTag('mix', 'MergedCaloTruth'),
SimClusters = cms.InputTag('mix', 'MergedCaloTruth'),
GenParticles = cms.InputTag('genParticles'),
genParticles = cms.InputTag('genParticles'),
simTau = cms.InputTag('SimTauProducer')
)

process.p = cms.Path(process.SimTauAnalyzer)
12 changes: 6 additions & 6 deletions SimGeneral/Debugging/test/simTauDecayDump_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
process.source = cms.Source("PoolSource",
fileNames = cms.untracked.vstring(
#'file:test_input.root'
'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24127.0_QQToHToTauTau_14TeV+2026D96/step3.root'
#'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24034.0_TTbar_14TeV+2026D96/step3.root'
#'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24127.0_QQToHToTauTau_14TeV+2026D96/step2.root'
'file:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24034.0_TTbar_14TeV+2026D96/step2.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:/data/agruber/patatrack/CMSSW_13_3_0_pre3/src/24036.0_ZTT_14TeV+2026D96/step2.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'
)
Expand All @@ -27,13 +27,13 @@
CaloParticle = cms.InputTag('mix', 'MergedCaloTruth'),
SimClusters = cms.InputTag('mix', 'MergedCaloTruth'),
GenParticles = cms.InputTag('genParticles'),
genParticles = cms.InputTag('genParticles')
genParticles = cms.InputTag('genParticles'),
simTau = cms.InputTag('SimTauProducer')
)

process.out = cms.OutputModule("PoolOutputModule",
fileName = cms.untracked.string('file:SimTauProducer_test.root')
,outputCommands = cms.untracked.vstring('drop *',
'keep *SimTau*_*_*_*'
,outputCommands = cms.untracked.vstring('keep *'
)
)

Expand Down

0 comments on commit 80a542c

Please sign in to comment.