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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#include "GeneratorInterface/GenFilters/plugins/MCFilterZboostHelper.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/global/EDFilter.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include <vector>
#include <iostream>
#include <unordered_map>
#include <tuple>
class MCMultiParticleMassFilter : public edm::global::EDFilter<> {
public:
explicit MCMultiParticleMassFilter(const edm::ParameterSet&);
~MCMultiParticleMassFilter() override;
private:
bool filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const override;
bool recurseLoop(std::vector<HepMC::GenParticle*>& particlesThatPassCuts, std::vector<int> indices, int depth) const;
/* Member data */
const edm::EDGetTokenT<edm::HepMCProduct> token_;
const std::vector<int> particleID;
std::vector<double> ptMin;
std::vector<double> etaMax;
std::vector<int> status;
//Maps each particle ID provided to its required pt, max eta, and status
std::unordered_map<int, std::tuple<double, double, int>> cutPerParticle;
const double minTotalMass;
const double maxTotalMass;
double minTotalMassSq;
double maxTotalMassSq;
int nParticles;
int particleSumTo;
int particleProdTo;
};
using namespace edm;
using namespace std;
MCMultiParticleMassFilter::MCMultiParticleMassFilter(const edm::ParameterSet& iConfig)
: token_(consumes<edm::HepMCProduct>(
iConfig.getUntrackedParameter<edm::InputTag>("src", edm::InputTag("generator", "unsmeared")))),
particleID(iConfig.getParameter<std::vector<int>>("ParticleID")),
ptMin(iConfig.getParameter<std::vector<double>>("PtMin")),
etaMax(iConfig.getParameter<std::vector<double>>("EtaMax")),
status(iConfig.getParameter<std::vector<int>>("Status")),
minTotalMass(iConfig.getParameter<double>("minTotalMass")),
maxTotalMass(iConfig.getParameter<double>("maxTotalMass")) {
nParticles = particleID.size();
minTotalMassSq = minTotalMass * minTotalMass;
maxTotalMassSq = maxTotalMass * maxTotalMass;
//These two values dictate what particles it accepts as combinations
particleSumTo = 0;
particleProdTo = 1;
for (const int i : particleID) {
particleSumTo += i;
particleProdTo *= i;
}
// if any of the vectors are of size 1, take that to mean it is a new default
double defaultPtMin = 1.8;
if ((int)ptMin.size() == 1) {
defaultPtMin = ptMin[0];
}
if ((int)ptMin.size() < nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given pT value size"
"< size of the number of particle IDs."
" Filling remaining values with "
<< defaultPtMin << endl;
while ((int)ptMin.size() < nParticles) {
ptMin.push_back(defaultPtMin);
}
} else if ((int)ptMin.size() > nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given pT value size"
"> size of the number of particle IDs."
" Ignoring extraneous values "
<< endl;
ptMin.resize(nParticles);
}
double defaultEtaMax = 2.7;
if ((int)etaMax.size() == 1) {
defaultEtaMax = etaMax[0];
}
if ((int)etaMax.size() < nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given eta value size"
"< size of the number of particle IDs."
" Filling remaining values with "
<< defaultEtaMax << endl;
while ((int)etaMax.size() < nParticles) {
etaMax.push_back(defaultEtaMax);
}
} else if ((int)etaMax.size() > nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given eta value size"
"> size of the number of particle IDs."
" Ignoring extraneous values "
<< endl;
etaMax.resize(nParticles);
}
int defaultStatus = 1;
if ((int)status.size() == 1) {
defaultStatus = status[0];
}
if ((int)status.size() < nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given status value size"
"< size of the number of particle IDs."
" Filling remaining values with "
<< defaultStatus << endl;
while ((int)status.size() < nParticles) {
status.push_back(defaultStatus);
}
} else if ((int)status.size() > nParticles) {
edm::LogWarning("MCMultiParticleMassFilter") << "Warning: Given status value size"
"> size of the number of particle IDs."
" Ignoring extraneous values "
<< endl;
status.resize(nParticles);
}
for (int i = 0; i < nParticles; i++) {
std::tuple<double, double, int> cutForParticle = std::make_tuple(ptMin[i], etaMax[i], status[i]);
cutPerParticle[particleID[i]] = cutForParticle;
} //assign the set of cuts you decided upon matched to each ID value in order
}
MCMultiParticleMassFilter::~MCMultiParticleMassFilter() {}
bool MCMultiParticleMassFilter::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const {
edm::Handle<edm::HepMCProduct> evt;
iEvent.getByToken(token_, evt);
const HepMC::GenEvent* myGenEvent = evt->GetEvent();
std::vector<HepMC::GenParticle*> particlesThatPassCuts = std::vector<HepMC::GenParticle*>();
for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
++p) {
for (const int i : particleID) {
if (i == (*p)->pdg_id()) {
//if the particle ID is one of the ones you specified, check for cuts per ID
const auto cuts = cutPerParticle.at(i);
if (((*p)->status() == get<2>(cuts)) && ((*p)->momentum().perp() >= get<0>(cuts)) &&
(std::fabs((*p)->momentum().eta()) <= get<1>(cuts))) {
particlesThatPassCuts.push_back(*p);
break;
}
}
}
}
int nIterables = particlesThatPassCuts.size();
if (nIterables < nParticles) {
return false;
} else {
int i = 0;
//only iterate while there are enough particles that pass cuts
while ((nIterables - i) >= nParticles) {
vector<int> indices;
//start recursing from index 0, 1, 2, ...
indices.push_back(i);
bool success = recurseLoop(particlesThatPassCuts, indices, 1);
if (success) {
return true;
}
i++;
}
}
return false;
}
bool MCMultiParticleMassFilter::recurseLoop(std::vector<HepMC::GenParticle*>& particlesThatPassCuts,
std::vector<int> indices,
int depth) const {
int lastIndex = indices.back();
int nIterables = (int)(particlesThatPassCuts.size());
if (lastIndex >= nIterables) {
return false;
} else if (depth == nParticles) {
//you have the right number of particles!
int tempSum = 0;
int tempProd = 1;
double px, py, pz, e;
px = py = pz = e = 0;
for (const int i : indices) {
int id = particlesThatPassCuts[i]->pdg_id();
tempSum += id;
tempProd *= id;
HepMC::FourVector tempVec = particlesThatPassCuts[i]->momentum();
px += tempVec.px();
py += tempVec.py();
pz += tempVec.pz();
e += tempVec.e();
}
if ((tempSum != particleSumTo) || (tempProd != particleProdTo)) {
return false; //check if the ids are what you want
}
double invMassSq = e * e - px * px - py * py - pz * pz;
//taking the root is computationally expensive, so use the squared term
if ((invMassSq >= minTotalMassSq) && (invMassSq <= maxTotalMassSq)) {
return true;
}
return false;
} else {
vector<bool> recursionResult;
//propagate recursion across all combinations of remaining indices
for (int i = 1; i < nIterables - lastIndex; i++) {
vector<int> newIndices = indices;
newIndices.push_back(lastIndex + i);
//always up the depth by 1 to make sure there is no infinite recursion
recursionResult.push_back(recurseLoop(particlesThatPassCuts, newIndices, depth + 1));
}
//search the results to look for one successful combination
for (bool r : recursionResult) {
if (r) {
return true;
}
}
return false;
}
}
DEFINE_FWK_MODULE(MCMultiParticleMassFilter);
|