-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
GenParticles2HepMCConverter.cc
197 lines (163 loc) · 7.59 KB
/
GenParticles2HepMCConverter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Candidate/interface/CandidateFwd.h"
#include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
#include <iostream>
#include <map>
using namespace std;
class GenParticles2HepMCConverter : public edm::stream::EDProducer<>
{
public:
explicit GenParticles2HepMCConverter(const edm::ParameterSet& pset);
~GenParticles2HepMCConverter() override {};
void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
private:
edm::EDGetTokenT<reco::CandidateView> genParticlesToken_;
edm::EDGetTokenT<GenEventInfoProduct> genEventInfoToken_;
edm::ESHandle<ParticleDataTable> pTable_;
std::vector<int> signalParticlePdgIds_;
private:
inline HepMC::FourVector FourVector(const reco::Candidate::Point& point)
{
return HepMC::FourVector(10*point.x(), 10*point.y(), 10*point.z(), 0);
};
inline HepMC::FourVector FourVector(const reco::Candidate::LorentzVector& lvec)
{
// Avoid negative mass, set minimum m^2 = 0
return HepMC::FourVector(lvec.px(), lvec.py(), lvec.pz(), std::hypot(lvec.P(), std::max(0., lvec.mass())));
};
};
GenParticles2HepMCConverter::GenParticles2HepMCConverter(const edm::ParameterSet& pset)
{
genParticlesToken_ = consumes<reco::CandidateView>(pset.getParameter<edm::InputTag>("genParticles"));
genEventInfoToken_ = consumes<GenEventInfoProduct>(pset.getParameter<edm::InputTag>("genEventInfo"));
signalParticlePdgIds_ = pset.getParameter<std::vector<int>>("signalParticlePdgIds");
produces<edm::HepMCProduct>("unsmeared");
}
void GenParticles2HepMCConverter::produce(edm::Event& event, const edm::EventSetup& eventSetup)
{
edm::Handle<reco::CandidateView> genParticlesHandle;
event.getByToken(genParticlesToken_, genParticlesHandle);
edm::Handle<GenEventInfoProduct> genEventInfoHandle;
event.getByToken(genEventInfoToken_, genEventInfoHandle);
eventSetup.getData(pTable_);
HepMC::GenEvent* hepmc_event = new HepMC::GenEvent();
hepmc_event->set_event_number(event.id().event());
hepmc_event->set_signal_process_id(genEventInfoHandle->signalProcessID());
hepmc_event->set_event_scale(genEventInfoHandle->qScale());
hepmc_event->set_alphaQED(genEventInfoHandle->alphaQED());
hepmc_event->set_alphaQCD(genEventInfoHandle->alphaQCD());
hepmc_event->weights() = genEventInfoHandle->weights();
// Set PDF
const gen::PdfInfo* pdf = genEventInfoHandle->pdf();
const int pdf_id1 = pdf->id.first, pdf_id2 = pdf->id.second;
const double pdf_x1 = pdf->x.first, pdf_x2 = pdf->x.second;
const double pdf_scalePDF = pdf->scalePDF;
const double pdf_xPDF1 = pdf->xPDF.first, pdf_xPDF2 = pdf->xPDF.second;
HepMC::PdfInfo hepmc_pdfInfo(pdf_id1, pdf_id2, pdf_x1, pdf_x2, pdf_scalePDF, pdf_xPDF1, pdf_xPDF2);
hepmc_event->set_pdf_info(hepmc_pdfInfo);
// Prepare list of HepMC::GenParticles
std::map<const reco::Candidate*, HepMC::GenParticle*> genCandToHepMCMap;
HepMC::GenParticle* hepmc_parton1 = nullptr, * hepmc_parton2 = nullptr;
std::vector<HepMC::GenParticle*> hepmc_particles;
const reco::Candidate* parton1 = nullptr, * parton2 = nullptr;
for ( unsigned int i=0, n=genParticlesHandle->size(); i<n; ++i )
{
const reco::Candidate* p = &genParticlesHandle->at(i);
HepMC::GenParticle* hepmc_particle = new HepMC::GenParticle(FourVector(p->p4()), p->pdgId(), p->status());
hepmc_particle->suggest_barcode(i+1);
// Assign particle's generated mass from the standard particle data table
double particleMass;
if ( pTable_->particle(p->pdgId()) ) particleMass = pTable_->particle(p->pdgId())->mass();
else particleMass = p->mass();
hepmc_particle->set_generated_mass(particleMass);
hepmc_particles.push_back(hepmc_particle);
genCandToHepMCMap[p] = hepmc_particle;
// Find incident proton pair
if ( p->pdgId() == 2212 and std::abs(p->eta()) > 100 and std::abs(p->pz()) > 1000 ) {
if ( !parton1 and p->pz() > 0 ) {
parton1 = p;
hepmc_parton1 = hepmc_particle;
}
else if ( !parton2 and p->pz() < 0 ) {
parton2 = p;
hepmc_parton2 = hepmc_particle;
}
}
}
// Put incident beam particles : proton -> parton vertex
HepMC::GenVertex* vertex1 = new HepMC::GenVertex(FourVector(parton1->vertex()));
HepMC::GenVertex* vertex2 = new HepMC::GenVertex(FourVector(parton2->vertex()));
hepmc_event->add_vertex(vertex1);
hepmc_event->add_vertex(vertex2);
vertex1->add_particle_in(hepmc_parton1);
vertex2->add_particle_in(hepmc_parton2);
hepmc_event->set_beam_particles(hepmc_parton1, hepmc_parton2);
// Prepare vertex list
typedef std::map<const reco::Candidate*, HepMC::GenVertex*> ParticleToVertexMap;
ParticleToVertexMap particleToVertexMap;
particleToVertexMap[parton1] = vertex1;
particleToVertexMap[parton2] = vertex2;
for ( unsigned int i=0, n=genParticlesHandle->size(); i<n; ++i )
{
const reco::Candidate* p = &genParticlesHandle->at(i);
if ( p == parton1 or p == parton2 ) continue;
// Connect mother-daughters for the other cases
for ( unsigned int j=0, nMothers=p->numberOfMothers(); j<nMothers; ++j )
{
// Mother-daughter hierarchy defines vertex
const reco::Candidate* elder = p->mother(j)->daughter(0);
HepMC::GenVertex* vertex;
if ( particleToVertexMap.find(elder) == particleToVertexMap.end() )
{
vertex = new HepMC::GenVertex(FourVector(elder->vertex()));
hepmc_event->add_vertex(vertex);
particleToVertexMap[elder] = vertex;
}
else
{
vertex = particleToVertexMap[elder];
}
// Vertex is found. Now connect each other
const reco::Candidate* mother = p->mother(j);
vertex->add_particle_in(genCandToHepMCMap[mother]);
vertex->add_particle_out(hepmc_particles[i]);
}
}
// Finalize HepMC event record
bool hasSignalVertex = false;
if ( !signalParticlePdgIds_.empty() ) {
// Loop over all vertices to assign the signal vertex, decaying to a signal particle
for ( auto v = hepmc_event->vertices_begin(); v != hepmc_event->vertices_end(); ++v ) {
for ( auto p = (*v)->particles_begin(HepMC::children);
p != (*v)->particles_end(HepMC::children); ++p ) {
const int pdgId = (*p)->pdg_id();
if ( std::find(signalParticlePdgIds_.begin(), signalParticlePdgIds_.end(), pdgId) != signalParticlePdgIds_.end() ) {
hepmc_event->set_signal_process_vertex(*v);
hasSignalVertex = true;
break;
}
}
if ( hasSignalVertex ) break;
}
}
// Set the default signal vertex if still not set
if ( !hasSignalVertex ) hepmc_event->set_signal_process_vertex(*(vertex1->vertices_begin()));
std::unique_ptr<edm::HepMCProduct> hepmc_product(new edm::HepMCProduct());
hepmc_product->addHepMCData(hepmc_event);
event.put(std::move(hepmc_product), "unsmeared");
}
DEFINE_FWK_MODULE(GenParticles2HepMCConverter);