-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess_CreateBootstrapSample.cxx
More file actions
426 lines (377 loc) · 20.8 KB
/
Process_CreateBootstrapSample.cxx
File metadata and controls
426 lines (377 loc) · 20.8 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* @Author: Zhiyong Lu (zhiyong.lu@cern.ch)
* @Date: 2025-05-18 17:23:23
* @Last Modified by: Zhiyong Lu
* @Last Modified time: 2025-10-24 09:57:59
*/
//put in the first lines to ignore the warning message
#pragma GCC diagnostic ignored "-Winconsistent-missing-override"
#pragma GCC diagnostic ignored "-Wwritable-strings"
#include "TFile.h"
#include "TList.h"
#include "TH2D.h"
#include "TRandom3.h"
#include <iostream>
#include <string>
#include <vector>
#include "./include/BasicForDihadron.h"
struct InputUnit {
std::string fileNameSuffix;
Int_t corrType;
Bool_t isNch;
Bool_t isPtdiff;
Int_t minRange;
Int_t maxRange;
InputUnit(std::string _fileNameSuffix, Int_t _corrType, Bool_t _isNch, Bool_t _isPtdiff, Int_t _minRange, Int_t _maxRange) :
fileNameSuffix(_fileNameSuffix), corrType(_corrType), isNch(_isNch), isPtdiff(_isPtdiff), minRange(_minRange), maxRange(_maxRange) {}
};
void CreateBootstrapSample(std::string fileNameSuffix, Int_t corrType, Bool_t isNch, Int_t minRange, Int_t maxRange);
void CreateBootstrapSample_PtDiff(std::string fileNameSuffix, Int_t corrType, Bool_t isNch, Int_t minRange, Int_t maxRange, Double_t pTMin, Double_t pTMax);
void Process_CreateBootstrapSample() {
std::vector<InputUnit> inputList;
// Ne-Ne
// ft0a-ft0c
// inputList.push_back(InputUnit("LHC25af_pass2_594019", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_594019", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_623297", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623297", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// tpc-ft0a/c
// inputList.push_back(InputUnit("LHC25af_pass2_622739", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_622739", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_622739", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_622739", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// O-O
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// PtDiff processing for 3times2PC
// inputList.push_back(InputUnit("LHC25af_pass2_623297", kFT0AFT0C, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623297", kFT0AFT0C, kCent, kPtDiffOn, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0A, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0A, kCent, kPtDiffOn, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0C, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_623540", kTPCFT0C, kCent, kPtDiffOn, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kFT0AFT0C, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kFT0AFT0C, kCent, kPtDiffOn, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0A, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0A, kCent, kPtDiffOn, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0C, kCent, kPtDiffOn, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_623296", kTPCFT0C, kCent, kPtDiffOn, 80, 100));
// Nch-based bootstrap (signal: Nch 10-50, template: Nch 0-10)
// Replace DATASET_ID with the actual run number once the dataset is available.
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kFT0AFT0C, kNch, kPtDiffOff, 10, 50));
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kFT0AFT0C, kNch, kPtDiffOff, 0, 10)); // template
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0A, kNch, kPtDiffOff, 10, 50));
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0A, kNch, kPtDiffOff, 0, 10)); // template
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0C, kNch, kPtDiffOff, 10, 50));
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0C, kNch, kPtDiffOff, 0, 10)); // template
// PtDiff
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0A, kNch, kPtDiffOn, 10, 50));
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0A, kNch, kPtDiffOn, 0, 10)); // template
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0C, kNch, kPtDiffOn, 10, 50));
inputList.push_back(InputUnit("LHC25ae_pass2_634100", kTPCFT0C, kNch, kPtDiffOn, 0, 10)); // template
//innerring
// inputList.push_back(InputUnit("LHC25af_pass2_594018", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_594018", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
//outterring
// inputList.push_back(InputUnit("LHC25af_pass2_594020", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_594020", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// tpc-ft0c
// inputList.push_back(InputUnit("LHC25af_pass2_586466", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_586466", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_565246", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_571364", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//old
// inputList.push_back(InputUnit("LHC25af_pass2_565246", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_565246", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_565246", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_565246", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//new
// inputList.push_back(InputUnit("LHC25af_pass2_591069", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591069", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_591069", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591069", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//innerring
// inputList.push_back(InputUnit("LHC25af_pass2_591387", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591387", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_591387", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591387", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//outterring
// inputList.push_back(InputUnit("LHC25af_pass2_591389", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591389", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_591389", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_591389", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// MC
// inputList.push_back(InputUnit("LHC25af_pass2_586594", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_586594", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_586594", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_586594", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_588055", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_588055", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25af_pass2_588055", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_588055", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// // tpc-tpc
// inputList.push_back(InputUnit("LHC25af_pass2_554626", kTPCTPC, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25af_pass2_554627", kTPCTPC, kCent, kPtDiffOff, 80, 100));
// // inputList.push_back(InputUnit("LHC25af_pass2_554626", kTPCTPC, kCent, kPtDiffOff, 0, 20));
// // inputList.push_back(InputUnit("LHC25af_pass2_554627", kTPCTPC, kCent, kPtDiffOff, 80, 100));
// O-O
// // ft0a-ft0c
// inputList.push_back(InputUnit("LHC25ae_pass2_561907", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_561907", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_561907", kFT0AFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_561907", kFT0AFT0C, kCent, kPtDiffOff, 80, 100));
// // tpc-ft0c
// inputList.push_back(InputUnit("LHC25ae_pass2_587867", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_575598", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//outterring
// inputList.push_back(InputUnit("LHC25ae_pass2_583828", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_583826", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
//innerring
// inputList.push_back(InputUnit("LHC25ae_pass2_583824", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_584612", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_573730", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_575598", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_572180", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_571294", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// // tpc-ft0a
// inputList.push_back(InputUnit("LHC25ae_pass2_587867", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_575598", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// //outterring
// inputList.push_back(InputUnit("LHC25ae_pass2_583828", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_583826", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
//innerring
// inputList.push_back(InputUnit("LHC25ae_pass2_583824", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_584612", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
//MC
// inputList.push_back(InputUnit("LHC25ae_pass2_586778", kTPCFT0C, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_586778", kTPCFT0C, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_586778", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_586778", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_573730", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_575598", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
// inputList.push_back(InputUnit("LHC25ae_pass2_572180", kTPCFT0A, kCent, kPtDiffOff, 0, 20));
// inputList.push_back(InputUnit("LHC25ae_pass2_571294", kTPCFT0A, kCent, kPtDiffOff, 80, 100));
for (auto input : inputList) {
if (input.isPtdiff) {
std::cout << "Processing Bootstrap Sample pT diff: " << input.fileNameSuffix << std::endl;
for (int iPt = 0; iPt < pTBins.size() - 1; iPt++) {
double pTMin = pTBins[iPt];
double pTMax = pTBins[iPt + 1];
CreateBootstrapSample_PtDiff(input.fileNameSuffix, input.corrType, input.isNch, input.minRange, input.maxRange, pTMin, pTMax);
}
} else {
std::cout << "Processing Bootstrap Sample: " << input.fileNameSuffix << std::endl;
CreateBootstrapSample(input.fileNameSuffix, input.corrType, input.isNch, input.minRange, input.maxRange);
}
}
}
void CreateBootstrapSample(std::string fileNameSuffix, Int_t corrType, Bool_t isNch, Int_t minRange, Int_t maxRange) {
std::string splitName = "Mult";
if (!isNch) splitName = "Cent";
// 打开输入文件
TFile* file = TFile::Open(
Form("./ProcessOutput/Mixed_%s_%s_%i_%i_%s.root",
fileNameSuffix.c_str(), splitName.c_str(), minRange, maxRange, DihadronCorrTypeName[corrType].c_str()),
"READ"
);
if (!file || file->IsZombie()) {
std::cerr << "Error opening input file!" << std::endl;
return;
}
// 读取所有样本直方图
std::vector<TH1D*> hists;
for (Int_t sample = 0; sample < maxSample; ++sample) {
TH1D* h = dynamic_cast<TH1D*>(
file->Get(Form("hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, sample))
);
if (!h) {
std::cerr << "Error loading histogram for sample " << sample << std::endl;
continue;
}
hists.push_back(h);
}
if (hists.empty()) {
std::cerr << "No histograms loaded!" << std::endl;
file->Close();
return;
}
// 创建输出文件
TFile* outFile = TFile::Open(
Form("./ProcessOutput/BootstrapSample_%s_%s_%i_%i_%s.root",
fileNameSuffix.c_str(), splitName.c_str(), minRange, maxRange, DihadronCorrTypeName[corrType].c_str()),
"RECREATE"
);
if (!outFile || outFile->IsZombie()) {
std::cerr << "Error creating output file!" << std::endl;
file->Close();
return;
}
TH1D* hAll = dynamic_cast<TH1D*>(file->Get(Form("hPhiSameOverMixed_%d_%d", minRange, maxRange)));
if (!hAll) {
std::cerr << "Error loading all-sample histogram!" << std::endl;
return;
}
hAll->SetName(Form("bsSample_hPhiSameOverMixed_%d_%d", minRange, maxRange));
hAll->SetTitle(Form("bsSample_hPhiSameOverMixed_%d_%d", minRange, maxRange));
hAll->GetXaxis()->SetTitle("#Delta#varphi");
hAll->Write();
// 初始化随机数生成器
TRandom3 randGen;
// 生成 maxSample^2 个bootstrap样本
for (Int_t bs = 0; bs < maxSample * maxSample; ++bs) {
// 随机选择样本索引(允许重复)
std::vector<Int_t> selectedIndices;
selectedIndices.clear();
for (Int_t i = 0; i < maxSample; ++i) {
selectedIndices.push_back(randGen.Integer(maxSample));
}
// 合并选中的直方图
TH1D* hmerge = nullptr;
Double_t totalWeight = 0.0;
for (Int_t idx : selectedIndices) {
if (idx < 0 || idx >= hists.size()) {
std::cerr << "Invalid sample index: " << idx << std::endl;
continue;
}
TH1D* current = hists[idx];
Double_t weight = current->GetEntries();
if (!hmerge) { // 第一次初始化合并直方图
hmerge = dynamic_cast<TH1D*>(current->Clone(
Form("bsSample_hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, bs)
));
hmerge->SetTitle(Form("bsSample_hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, bs));
// hmerge->Scale(weight);
// std::cout << "bs: " << bs << " idx: " << idx << " weight: " << weight << std::endl;
totalWeight = weight;
} else { // 后续累加
// current->Scale(weight);
// hmerge->Add(current, weight);
hmerge->Add(current);
totalWeight += weight;
}
}
// 归一化并保存
if (hmerge && totalWeight > 0) {
// hmerge->Scale(1.0 / totalWeight);
hmerge->Scale(1.0 / selectedIndices.size());
// Printf("size: %d", selectedIndices.size());
// hmerge->SetDirectory(outFile);
hmerge->Write();
// delete hmerge; // 释放内存
}
}
// 清理资源
outFile->Close();
file->Close();
delete outFile;
delete file;
}
void CreateBootstrapSample_PtDiff(std::string fileNameSuffix, Int_t corrType, Bool_t isNch, Int_t minRange, Int_t maxRange, Double_t pTMin, Double_t pTMax) {
std::string splitName = "Mult";
if (!isNch) splitName = "Cent";
// 打开输入文件
TFile* file = TFile::Open(
Form("./ProcessOutput/PtDiff/Mixed_%s_%s_%i_%i_Pt_%0.1f_%0.1f_%s.root",
fileNameSuffix.c_str(), splitName.c_str(), minRange, maxRange, pTMin, pTMax, DihadronCorrTypeName[corrType].c_str()),
"READ"
);
if (!file || file->IsZombie()) {
std::cerr << "Error opening input file!" << std::endl;
return;
}
// 读取所有样本直方图
std::vector<TH1D*> hists;
for (Int_t sample = 0; sample < maxSample; ++sample) {
TH1D* h = dynamic_cast<TH1D*>(
file->Get(Form("hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, sample))
);
if (!h) {
std::cerr << "Error loading histogram for sample " << sample << std::endl;
continue;
}
hists.push_back(h);
}
if (hists.empty()) {
std::cerr << "No histograms loaded!" << std::endl;
file->Close();
return;
}
// 创建输出文件
TFile* outFile = TFile::Open(
Form("./ProcessOutput/PtDiff/BootstrapSample_%s_%s_%i_%i_Pt_%0.1f_%0.1f_%s.root",
fileNameSuffix.c_str(), splitName.c_str(), minRange, maxRange, pTMin, pTMax, DihadronCorrTypeName[corrType].c_str()),
"RECREATE"
);
if (!outFile || outFile->IsZombie()) {
std::cerr << "Error creating output file!" << std::endl;
file->Close();
return;
}
TH1D* hAll = dynamic_cast<TH1D*>(file->Get(Form("hPhiSameOverMixed_%d_%d", minRange, maxRange)));
if (!hAll) {
std::cerr << "Error loading all-sample histogram!" << std::endl;
return;
}
hAll->SetName(Form("bsSample_hPhiSameOverMixed_%d_%d", minRange, maxRange));
hAll->SetTitle(Form("bsSample_hPhiSameOverMixed_%d_%d", minRange, maxRange));
hAll->GetXaxis()->SetTitle("#Delta#varphi");
hAll->Write();
// 初始化随机数生成器
TRandom3 randGen;
// 生成 maxSample^2 个bootstrap样本
for (Int_t bs = 0; bs < maxSample * maxSample; ++bs) {
// 随机选择样本索引(允许重复)
std::vector<Int_t> selectedIndices;
selectedIndices.clear();
for (Int_t i = 0; i < maxSample; ++i) {
selectedIndices.push_back(randGen.Integer(maxSample));
}
// 合并选中的直方图
TH1D* hmerge = nullptr;
Double_t totalWeight = 0.0;
for (Int_t idx : selectedIndices) {
if (idx < 0 || idx >= hists.size()) {
std::cerr << "Invalid sample index: " << idx << std::endl;
continue;
}
TH1D* current = hists[idx];
Double_t weight = current->GetEntries();
if (!hmerge) { // 第一次初始化合并直方图
hmerge = dynamic_cast<TH1D*>(current->Clone(
Form("bsSample_hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, bs)
));
hmerge->SetTitle(Form("bsSample_hPhiSameOverMixed_%d_%d_%d", minRange, maxRange, bs));
// hmerge->Scale(weight);
// std::cout << "bs: " << bs << " idx: " << idx << " weight: " << weight << std::endl;
totalWeight = weight;
} else { // 后续累加
// current->Scale(weight);
// hmerge->Add(current, weight);
hmerge->Add(current);
totalWeight += weight;
}
}
// 归一化并保存
if (hmerge && totalWeight > 0) {
// hmerge->Scale(1.0 / totalWeight);
hmerge->Scale(1.0 / selectedIndices.size());
// Printf("size: %d", selectedIndices.size());
// hmerge->SetDirectory(outFile);
hmerge->Write();
delete hmerge; // 释放内存
}
}
// 清理资源
outFile->Close();
file->Close();
delete outFile;
delete file;
}