-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedSpecialDijets.cpp
More file actions
391 lines (323 loc) · 12.4 KB
/
Copy pathembedSpecialDijets.cpp
File metadata and controls
391 lines (323 loc) · 12.4 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "Pythia8/Pythia.h"
#include "fastjet/ClusterSequence.hh"
#include "fastjet/PseudoJet.hh"
#include "TF1.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TParameter.h"
#include "TRandom3.h"
#include "TTree.h"
#include "TVector3.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace Pythia8;
static double wrapPhi(double phi) {
while (phi <= -M_PI)
phi += 2.0 * M_PI;
while (phi > M_PI)
phi -= 2.0 * M_PI;
return phi;
}
double deltaR(double eta1, double phi1, double eta2, double phi2) {
double deta = eta1 - eta2;
double dphi = wrapPhi(phi1 - phi2);
return std::sqrt(deta * deta + dphi * dphi);
}
// ---------------------------- config ----------------------------
struct Config {
// acceptance (charged final only)
double part_ptMin;
double part_etaMax;
// jet finder
double jet_R;
double jet_ptMin;
double jet_etaMax;
// generation safety cone (keep constituents well inside R to avoid splitting)
double gen_Rmax; // <= ~R/2 recommended
int nEvents;
Config()
: part_ptMin(0.15), part_etaMax(1.0), jet_R(0.4), jet_ptMin(8.0),
jet_etaMax(1.0 - 0.4), gen_Rmax(0.20), nEvents(10000) {}
};
struct JetConstituents {
float z[100];
float jt[100];
float px[100];
float py[100];
float pz[100];
float e[100];
};
// ============================================================================
// MC Dijet Structure (store fastjet objects directly; no MCParticle needed)
// ============================================================================
struct McDijet {
double jet1_pt, jet1_eta, jet1_phi;
int jet1_nPart;
double jet2_pt, jet2_eta, jet2_phi;
int jet2_nPart;
JetConstituents jet1_constituents;
JetConstituents jet2_constituents;
McDijet()
: jet1_pt(0), jet1_eta(0), jet1_phi(0), jet1_nPart(0), jet2_pt(0),
jet2_eta(0), jet2_phi(0), jet2_nPart(0) {}
};
// ============================================================================
// Tracking efficiency
// ============================================================================
bool isAcceptedTrack(double pt, TF1 &eff) {
// if (pt > 30.0)
// return false;
return gRandom->Rndm() < eff.Eval(pt);
}
// ============================================================================
// Background Analysis
// ============================================================================
struct BackgroundResult {
int multA; // multiplicity in transverse region A
int multB; // multiplicity in transverse region B
BackgroundResult() : multA(0), multB(0) {}
};
BackgroundResult
analyzeBackground(const std::vector<fastjet::PseudoJet> &jets,
const std::vector<fastjet::PseudoJet> &particles,
const Config &cfg) {
BackgroundResult result;
if (jets.size() < 2)
return result;
// Define dijet axis as average of two leading jets
fastjet::PseudoJet dijet = jets[0] + jets[1];
double phi_dijet = dijet.phi(); // in [-pi, pi)
double eta_dijet = dijet.eta();
double phiA = wrapPhi(phi_dijet + M_PI / 2);
double phiB = wrapPhi(phi_dijet - M_PI / 2);
for (const auto &p : particles) {
const double pt = p.pt();
if (pt < cfg.part_ptMin)
continue;
const double eta = p.eta();
if (std::abs(eta) > cfg.part_etaMax)
continue;
const double phi = p.phi();
if (deltaR(eta, phi, eta_dijet, phiA) < cfg.jet_R)
result.multA++;
else if (deltaR(eta, phi, eta_dijet, phiB) < cfg.jet_R)
result.multB++;
}
return result;
}
// ---------------------------- main embedding loop
// ----------------------------
int main(int argc, char **argv) {
Config cfg;
std::string outFile = "embeddedSpecialDijets.root";
if (argc > 1)
cfg.nEvents = std::atoi(argv[1]);
if (argc > 2)
outFile = argv[2];
cfg.jet_etaMax = cfg.part_etaMax - cfg.jet_R;
// Tracking efficiency function
TF1 eff("eff", "[0]*(1-exp(-pow(x/[1],[2])))", 0, 30);
eff.SetParameters(0.88, 0.25, 1.2);
// background Pythia
Pythia pythiaBackground;
pythiaBackground.readString("Beams:idA = 2212");
pythiaBackground.readString("Beams:idB = 2212");
pythiaBackground.readString("Beams:eCM = 200.");
pythiaBackground.readString("SoftQCD:nonDiffractive = on");
if (!pythiaBackground.init()) {
std::cerr << "[ERROR] Pythia background init failed\n";
return 1;
}
fastjet::JetDefinition jetDefinition(fastjet::antikt_algorithm, cfg.jet_R);
// Input
TFile *inputFile = TFile::Open("specialDijets.root", "READ");
TTree *inputTree = (TTree *)inputFile->Get("tree");
McDijet mcDijet;
inputTree->SetBranchAddress("jet1_pt", &mcDijet.jet1_pt);
inputTree->SetBranchAddress("jet1_eta", &mcDijet.jet1_eta);
inputTree->SetBranchAddress("jet1_phi", &mcDijet.jet1_phi);
inputTree->SetBranchAddress("jet2_pt", &mcDijet.jet2_pt);
inputTree->SetBranchAddress("jet2_eta", &mcDijet.jet2_eta);
inputTree->SetBranchAddress("jet2_phi", &mcDijet.jet2_phi);
inputTree->SetBranchAddress("jet1_nNch", &mcDijet.jet1_nPart);
inputTree->SetBranchAddress("jet2_nNch", &mcDijet.jet2_nPart);
inputTree->SetBranchAddress("px1_constituents",
&mcDijet.jet1_constituents.px);
inputTree->SetBranchAddress("py1_constituents",
&mcDijet.jet1_constituents.py);
inputTree->SetBranchAddress("pz1_constituents",
&mcDijet.jet1_constituents.pz);
inputTree->SetBranchAddress("e1_constituents", &mcDijet.jet1_constituents.e);
inputTree->SetBranchAddress("jt1_constituents",
&mcDijet.jet1_constituents.jt);
inputTree->SetBranchAddress("z1_constituents", &mcDijet.jet1_constituents.z);
inputTree->SetBranchAddress("px2_constituents",
&mcDijet.jet2_constituents.px);
inputTree->SetBranchAddress("py2_constituents",
&mcDijet.jet2_constituents.py);
inputTree->SetBranchAddress("pz2_constituents",
&mcDijet.jet2_constituents.pz);
inputTree->SetBranchAddress("e2_constituents", &mcDijet.jet2_constituents.e);
inputTree->SetBranchAddress("jt2_constituents",
&mcDijet.jet2_constituents.jt);
inputTree->SetBranchAddress("z2_constituents", &mcDijet.jet2_constituents.z);
// Output
TFile *fout = new TFile(outFile.c_str(), "RECREATE");
TTree *outTree =
new TTree("events", "Reco jets in embedded SoftQCD background");
// Variables requested
int eventID = 0;
double mc_jet1_pt = 0, mc_jet1_eta = -99, mc_jet1_phi = -99;
int mc_jet1_nPart = 0;
double mc_jet2_pt = 0, mc_jet2_eta = -999, mc_jet2_phi = -99;
int mc_jet2_nPart = 0;
int matched1, matched2;
double reco_jet1_pt, reco_jet1_eta, reco_jet1_phi;
int reco_jet1_nPart;
double reco_jet2_pt, reco_jet2_eta, reco_jet2_phi;
int reco_jet2_nPart;
double dR_mc1_reco1, dR_mc2_reco2;
int bkg_multA, bkg_multB;
outTree->Branch("eventID", &eventID);
outTree->Branch("matched1", &matched1);
outTree->Branch("matched2", &matched2);
outTree->Branch("mc_jet1_pt", &mc_jet1_pt);
outTree->Branch("mc_jet1_eta", &mc_jet1_eta);
outTree->Branch("mc_jet1_phi", &mc_jet1_phi);
outTree->Branch("mc_jet1_nPart", &mc_jet1_nPart);
outTree->Branch("reco_jet1_pt", &reco_jet1_pt);
outTree->Branch("reco_jet1_eta", &reco_jet1_eta);
outTree->Branch("reco_jet1_phi", &reco_jet1_phi);
outTree->Branch("reco_jet1_nPart", &reco_jet1_nPart);
outTree->Branch("dR_mc1_reco1", &dR_mc1_reco1);
outTree->Branch("mc_jet2_pt", &mc_jet2_pt);
outTree->Branch("mc_jet2_eta", &mc_jet2_eta);
outTree->Branch("mc_jet2_phi", &mc_jet2_phi);
outTree->Branch("mc_jet2_nPart", &mc_jet2_nPart);
outTree->Branch("reco_jet2_pt", &reco_jet2_pt);
outTree->Branch("reco_jet2_eta", &reco_jet2_eta);
outTree->Branch("reco_jet2_phi", &reco_jet2_phi);
outTree->Branch("reco_jet2_nPart", &reco_jet2_nPart);
outTree->Branch("dR_mc2_reco2", &dR_mc2_reco2);
outTree->Branch("multA", &bkg_multA);
outTree->Branch("multB", &bkg_multB);
int maxEvents = std::min(cfg.nEvents, (int)inputTree->GetEntries());
// Event loop
for (int iev = 0; iev < maxEvents; ++iev) {
eventID = iev;
if (!pythiaBackground.next())
continue;
inputTree->GetEntry(iev);
// Store truth info
mc_jet1_pt = mcDijet.jet1_pt;
mc_jet1_eta = mcDijet.jet1_eta;
mc_jet1_phi = mcDijet.jet1_phi;
mc_jet1_nPart = mcDijet.jet1_nPart;
mc_jet2_pt = mcDijet.jet2_pt;
mc_jet2_eta = mcDijet.jet2_eta;
mc_jet2_phi = mcDijet.jet2_phi;
mc_jet2_nPart = mcDijet.jet2_nPart;
// ------------------ build reco particles by smearing truthParticles
// ------------------
// ------------------ background truth particles ------------------
std::vector<fastjet::PseudoJet> truthParticles;
truthParticles.reserve(4000);
for (int i = 0; i < pythiaBackground.event.size(); ++i) {
const Particle &p = pythiaBackground.event[i];
if (!p.isFinal() || !p.isVisible() || !p.isCharged())
continue;
if (std::fabs(p.eta()) > cfg.part_etaMax)
continue;
if (p.pT() < cfg.part_ptMin)
continue;
fastjet::PseudoJet pj(p.px(), p.py(), p.pz(), p.e());
pj.set_user_index(0); // background
truthParticles.push_back(pj);
}
// -----------------------------------------------------------------------
// Step 3: Embed MC dijet particles into the event and smear whole event
// -----------------------------------------------------------------------
// add jet 1 constituents
for (int i = 0; i < mcDijet.jet1_nPart; ++i) {
fastjet::PseudoJet pj(
mcDijet.jet1_constituents.px[i], mcDijet.jet1_constituents.py[i],
mcDijet.jet1_constituents.pz[i], mcDijet.jet1_constituents.e[i]);
pj.set_user_index(1); // signal
truthParticles.push_back(pj);
}
// add jet 2 constituents
for (int i = 0; i < mcDijet.jet2_nPart; ++i) {
fastjet::PseudoJet pj(
mcDijet.jet2_constituents.px[i], mcDijet.jet2_constituents.py[i],
mcDijet.jet2_constituents.pz[i], mcDijet.jet2_constituents.e[i]);
pj.set_user_index(1); // signal
truthParticles.push_back(pj);
}
// ------------------ recoParticles (smearing hook) ------------------
std::vector<fastjet::PseudoJet> recoParticles;
recoParticles.reserve(truthParticles.size());
for (size_t i = 0; i < truthParticles.size(); ++i) {
fastjet::PseudoJet smeared = truthParticles[i];
recoParticles.push_back(smeared);
}
// -----------------------------------------------------------------------
// Step 4: Reconstruct jets from combined system
// -----------------------------------------------------------------------
fastjet::ClusterSequence cs(recoParticles, jetDefinition);
std::vector<fastjet::PseudoJet> allRecoJets =
fastjet::sorted_by_pt(cs.inclusive_jets(cfg.jet_ptMin));
std::vector<fastjet::PseudoJet> selectedJets;
for (const auto &jet : allRecoJets) {
if (std::abs(jet.eta()) < cfg.jet_etaMax) {
selectedJets.push_back(jet);
}
}
// Reset reco outputs
matched1 = matched2 = 0;
reco_jet1_pt = reco_jet1_eta = reco_jet1_phi = -999;
reco_jet2_pt = reco_jet2_eta = reco_jet2_phi = -999;
reco_jet1_nPart = reco_jet2_nPart = -1;
dR_mc1_reco1 = dR_mc2_reco2 = 1e3;
bkg_multA = bkg_multB = 0;
// Find best match for jet 1
for (const auto &jet : selectedJets) {
double dr = deltaR(mc_jet1_eta, mc_jet1_phi, jet.eta(), jet.phi());
if (dr < dR_mc1_reco1 && dr < cfg.jet_R) {
dR_mc1_reco1 = dr;
matched1 = true;
reco_jet1_pt = jet.pt();
reco_jet1_eta = jet.eta();
reco_jet1_phi = jet.phi();
reco_jet1_nPart = jet.constituents().size();
}
}
// Find best match for jet 2
for (const auto &jet : selectedJets) {
double dr = deltaR(mc_jet2_eta, mc_jet2_phi, jet.eta(), jet.phi());
if (dr < dR_mc2_reco2 && dr < cfg.jet_R) {
dR_mc2_reco2 = dr;
matched2 = true;
reco_jet2_pt = jet.pt();
reco_jet2_eta = jet.eta();
reco_jet2_phi = jet.phi();
reco_jet2_nPart = jet.constituents().size();
}
}
// ------------------ background multiplicities (transverse regions)
// ------------------
BackgroundResult bkg = analyzeBackground(selectedJets, recoParticles, cfg);
bkg_multA = bkg.multA;
bkg_multB = bkg.multB;
outTree->Fill();
}
outTree->Write();
fout->Close();
delete fout;
pythiaBackground.stat();
return 0;
}