-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
421 lines (330 loc) · 12.2 KB
/
test.c
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
void randomizer() {
using std::vector;
char fname[ 120 ];
int nfiles = 9;
int mass_energy[] = { 5, 10, 15, 20, 25, 30, 40, 50, 60 };
int energy_count[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned int nEvents[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int max = 7500;
std::cout << "Trying to create output file... ";
TFile *out = new TFile("gun_k0L_allgev_FTFP_BERT_10000evt_ILD_l5_v02steel.root", "RECREATE");
if (!out) {
out->Close();
std::cout << " [FAIL]" << std::endl;
return;
}
std::cout << "[OK]" << std::endl;
TFile* files[ nfiles ];
TTree* trees[ nfiles ];
std::cout << "Trying to open files... " << endl;
for( unsigned int i = 0; i < nfiles; i++ ) {
sprintf( fname, "gun_k0L_%dgev_FTFP_BERT_10000evt_ILD_l5_v02steel.root", mass_energy[ i ] );
files[i] = new TFile( fname, "READ" );
if (!files[i]) { // if error occure then exit
std::cout << fname << " [FAIL]" << std::endl;
return;
}
std::cout << fname << " [OK]" << std::endl;
}
std::cout << "Setup trees... " << endl;
for( unsigned int i = 0; i < nfiles; i++ ) {
sprintf( fname, "gun_k0L_%dgev_FTFP_BERT_10000evt_ILD_l5_v02steel.root", mass_energy[ i ] );
trees[i] = (TTree *)files[i]->Get("tree");
if ( !trees[i] ) {
std::cout << fname << " [FAIL]" << std::endl;
files[i]->Close();
return;
}
std::cout << fname << " [OK]" << std::endl;
}
//unsigned int nEvents = trees[0]->GetEntries();
for( unsigned int i = 0; i < nfiles; i++ ) {
nEvents[i] = trees[i]->GetEntries();
}
float temp_energy[ nfiles ];
float temp_eecal[ nfiles ];
float temp_ehcal[ nfiles ];
float temp_mc_energy[ nfiles ];
float temp_cogx[ nfiles ];
float temp_cogy[ nfiles ];
float temp_cogz[ nfiles ];
float temp_ecogx[ nfiles ];
float temp_ecogy[ nfiles ];
float temp_ecogz[ nfiles ];
float temp_hcogx[ nfiles ];
float temp_hcogy[ nfiles ];
float temp_hcogz[ nfiles ];
float temp_nhits[ nfiles ];
float temp_nhits_ecal[ nfiles ];
float temp_nhits_hcal[ nfiles ];
float temp_time[ nfiles ];
float temp_etime[ nfiles ];
float temp_htime[ nfiles ];
float temp_radius[ nfiles ];
float temp_eradius[ nfiles ];
float temp_hradius[ nfiles ];
float temp_angle[ nfiles ];
float temp_elayer[ nfiles ];
float temp_hlayer[ nfiles ];
float temp_energy_hit[ nfiles ];
float temp_energy_hit_ecal[ nfiles ];
float temp_energy_hit_hcal[ nfiles ];
float temp_muon_energy[ nfiles ];
float temp_muon_energy_hit[ nfiles ];
float temp_muon_nhits[ nfiles ];
float temp_muon_time[ nfiles ];
for( unsigned int i = 0; i < nfiles; i++ ) {
trees[i]->SetBranchAddress("energy", &temp_energy[i]);
trees[i]->SetBranchAddress("eecal", &temp_eecal[i]);
trees[i]->SetBranchAddress("ehcal", &temp_ehcal[i]);
trees[i]->SetBranchAddress("mc_energy", &temp_mc_energy[i]);
trees[i]->SetBranchAddress("cogx", &temp_cogx[i]);
trees[i]->SetBranchAddress("cogy", &temp_cogy[i]);
trees[i]->SetBranchAddress("cogz", &temp_cogz[i]);
trees[i]->SetBranchAddress("ecogx", &temp_ecogx[i]);
trees[i]->SetBranchAddress("ecogy", &temp_ecogy[i]);
trees[i]->SetBranchAddress("ecogz", &temp_ecogz[i]);
trees[i]->SetBranchAddress("hcogx", &temp_hcogx[i]);
trees[i]->SetBranchAddress("hcogy", &temp_hcogy[i]);
trees[i]->SetBranchAddress("hcogz", &temp_hcogz[i]);
trees[i]->SetBranchAddress("nhits", &temp_nhits[i]);
trees[i]->SetBranchAddress("nhits_ecal", &temp_nhits_ecal[i]);
trees[i]->SetBranchAddress("nhits_hcal", &temp_nhits_hcal[i]);
trees[i]->SetBranchAddress("time", &temp_time[i]);
trees[i]->SetBranchAddress("etime", &temp_etime[i]);
trees[i]->SetBranchAddress("htime", &temp_htime[i]);
trees[i]->SetBranchAddress("radius", &temp_radius[i]);
trees[i]->SetBranchAddress("eradius", &temp_eradius[i]);
trees[i]->SetBranchAddress("hradius", &temp_hradius[i]);
trees[i]->SetBranchAddress("angle", &temp_angle[i]);
trees[i]->SetBranchAddress("elayer", &temp_elayer[i]);
trees[i]->SetBranchAddress("hlayer", &temp_hlayer[i]);
trees[i]->SetBranchAddress("energy_hit", &temp_energy_hit[i]);
trees[i]->SetBranchAddress("energy_hit_ecal", &temp_energy_hit_ecal[i]);
trees[i]->SetBranchAddress("energy_hit_hcal", &temp_energy_hit_hcal[i]);
trees[i]->SetBranchAddress("muon_energy", &temp_muon_energy[i]);
trees[i]->SetBranchAddress("muon_energy_hit", &temp_muon_energy_hit[i]);
trees[i]->SetBranchAddress("muon_nhits", &temp_muon_nhits[i]);
trees[i]->SetBranchAddress("muon_time", &temp_muon_time[i]);
}
float energy = 0;
float eecal = 0;
float ehcal = 0;
float mc_energy = 0;
float cogx = 0;
float cogy = 0;
float cogz = 0;
float ecogx = 0;
float ecogy = 0;
float ecogz = 0;
float hcogx = 0;
float hcogy = 0;
float hcogz = 0;
float nhits = 0;
float nhits_ecal = 0;
float nhits_hcal = 0;
float time = 0;
float etime = 0;
float htime = 0;
float radius = 0;
float eradius = 0;
float hradius = 0;
float angle = 0;
float elayer = 0;
float hlayer = 0;
float energy_hit = 0;
float energy_hit_ecal = 0;
float energy_hit_hcal = 0;
float muon_energy = 0;
float muon_energy_hit = 0;
float muon_nhits = 0;
float muon_time = 0;
TTree* tree = new TTree("tree", "tree");
tree->Branch("energy", &energy, "energy");
tree->Branch("eecal", &eecal, "eecal");
tree->Branch("ehcal", &ehcal, "ehcal");
tree->Branch("mc_energy", &mc_energy, "mc_energy");
tree->Branch("cogx", &cogx, "cogx" );
tree->Branch("cogy", &cogy, "cogy" );
tree->Branch("cogz", &cogz, "cogz" );
tree->Branch("ecogx", &ecogx, "ecogx" );
tree->Branch("ecogy", &ecogy, "ecogy" );
tree->Branch("ecogz", &ecogz, "ecogz" );
tree->Branch("hcogx", &hcogx, "hcogx" );
tree->Branch("hcogy", &hcogy, "hcogy" );
tree->Branch("hcogz", &hcogz, "hcogz" );
tree->Branch("nhits", &nhits, "nhits" );
tree->Branch("nhits_ecal", &nhits_ecal, "nhits_ecal" );
tree->Branch("nhits_hcal", &nhits_hcal, "nhits_hcal" );
tree->Branch("time", &time, "time" );
tree->Branch("etime", &etime, "etime" );
tree->Branch("htime", &htime, "htime" );
tree->Branch("radius", &radius, "radius" );
tree->Branch("eradius", &eradius, "eradius" );
tree->Branch("hradius", &hradius, "hradius" );
tree->Branch("angle", &angle, "angle" );
tree->Branch("elayer", &elayer, "elayer" );
tree->Branch("hlayer", &hlayer, "hlayer" );
tree->Branch("energy_hit", &energy_hit, "energy_hit" );
tree->Branch("energy_hit_ecal", &energy_hit_ecal, "energy_hit_ecal" );
tree->Branch("energy_hit_hcal", &energy_hit_hcal, "energy_hit_hcal" );
tree->Branch("muon_energy", &muon_energy, "muon_energy" );
tree->Branch("muon_energy_hit", &muon_energy_hit, "muon_energy_hit" );
tree->Branch("muon_nhits", &muon_nhits, "muon_nhits" );
tree->Branch("muon_time", &muon_time, "muon_time" );
//for ( unsigned int i = 0; i < nEvents; i++ ) {
for ( unsigned int i = 0; i < max; i++ ) {
int per = rand() % nfiles;
energy_count[ per ] = energy_count[ per ] + 1;
trees[ per ]->GetEntry( rand() % nEvents[ per ] );
energy = temp_energy[ per ];
eecal = temp_eecal[ per ];
ehcal = temp_ehcal[ per ];
mc_energy = temp_mc_energy[ per ];
cogx = temp_cogx[ per ];
cogy = temp_cogy[ per ];
cogz = temp_cogz[ per ];
ecogx = temp_ecogx[ per ];
ecogy = temp_ecogy[ per ];
ecogz = temp_ecogz[ per ];
hcogx = temp_hcogx[ per ];
hcogy = temp_hcogy[ per ];
hcogz = temp_hcogz[ per ];
nhits = temp_nhits[ per ];
nhits_ecal = temp_nhits_ecal[ per ];
nhits_hcal = temp_nhits_hcal[ per ];
time = temp_time[ per ];
etime = temp_etime[ per ];
htime = temp_htime[ per ];
radius = temp_radius[ per ];
eradius = temp_eradius[ per ];
hradius = temp_hradius[ per ];
angle = temp_angle[ per ];
elayer = temp_elayer[ per ];
hlayer = temp_hlayer[ per ];
energy_hit = temp_energy_hit[ per ];
energy_hit_ecal = temp_energy_hit_ecal[ per ];
energy_hit_hcal = temp_energy_hit_hcal[ per ];
muon_energy = temp_muon_energy[ per ];
muon_energy_hit = temp_muon_energy_hit[ per ];
muon_nhits = temp_muon_nhits[ per ];
muon_time = temp_muon_time[ per ];
tree->Fill();
}
//for ( unsigned int i = 0; i < nfiles; i++ ) std::cout << "Energy === " << mass_energy[i] << " and energy_count === " << energy_count[i] << std::endl;
out->cd();
tree->Write();
out->Write();
out->Close();
delete out;
for( unsigned int i = 0; i < nfiles; i++ ) files[i]->Close();
//delete tree_r;
//for( unsigned int i = 0; i < nfiles; i++ ) delete trees[i];
std::cout << "That's all... " << endl;
}
/*
void test() {
Char_t fname[ 120 ];
Int_t nfiles = 9;
Int_t mass_energy[] = { 5, 10, 15, 20, 25, 30, 40, 50, 60 };
std::cout << "Trying to open \"ALL\" file... ";
TFile *all = new TFile("gun_k0L_allgev_FTFP_BERT_5000evt_ILD_l5_v02steel.root", "READ");
if (!all) {
all->Close();
std::cout << " [FAIL]" << std::endl;
return;
}
std::cout << "[OK]" << std::endl;
TFile* files[ nfiles ];
TTree* trees[ nfiles ];
std::cout << "Trying to open files... " << endl;
for( unsigned int i = 0; i < nfiles; i++ ) {
sprintf( fname, "gun_k0L_%dgev_FTFP_BERT_5000evt_ILD_l5_v02steel.root", mass_energy[ i ] );
files[i] = new TFile( fname, "READ" );
if (!files[i]) { // if error occure then exit
std::cout << fname << " [FAIL]" << std::endl;
return;
}
std::cout << files[i] << " [OK]" << std::endl;
}
std::cout << "Setup trees... " << endl;
for( unsigned int i = 0; i < nfiles; i++ ) {
sprintf( fname, "gun_k0L_%dgev_FTFP_BERT_5000evt_ILD_l5_v02steel.root", mass_energy[ i ] );
trees[i] = (TTree *)files[i]->Get("tree");
if ( !trees[i] ) {
std::cout << fname << " [FAIL]" << std::endl;
files[i]->Close();
return;
}
std::cout << fname << " [OK]" << std::endl;
}
std::cout << "Setup a tree for \"ALL\"... ";
TTree* all_tree = (TTree *)all->Get("tree");
if ( !all_tree ) {
std::cout << " [FAIL]" << std::endl;
return;
}
std::cout << " [OK] " << std::endl;
unsigned int nEvents = trees[0]->GetEntries();
unsigned int all_nEvents = all_tree->GetEntries();
if ( nEvents == all_nEvents ) cout << "The number of entries is the same" << endl;
if ( nEvents == all_nEvents ) cout << nEvents << " === " << all_nEvents << endl;
Float_t temp_energy[ nfiles ];
Float_t temp_eecal[ nfiles ];
Float_t temp_ehcal[ nfiles ];
Float_t temp_mc_energy[ nfiles ];
for( unsigned int i = 0; i < nfiles; i++ ) {
trees[i]->SetBranchAddress("energy", &temp_energy[i]);
trees[i]->SetBranchAddress("eecal", &temp_eecal[i]);
trees[i]->SetBranchAddress("ehcal", &temp_ehcal[i]);
trees[i]->SetBranchAddress("mc_energy", &temp_mc_energy[i]);
}
Float_t energy = 0;
Float_t eecal = 0;
Float_t ehcal = 0;
Float_t mc_energy = 0;
all_tree->SetBranchAddress("energy", &energy);
all_tree->SetBranchAddress("eecal", &eecal);
all_tree->SetBranchAddress("ehcal", &ehcal);
all_tree->SetBranchAddress("mc_energy", &mc_energy);
// Float_t test_energy = 0;
// Float_t test_eecal = 0;
// Float_t test_ehcal = 0;
// Float_t test_mc_energy = 0;
Int_t index = 0;
Int_t index2 = 0;
Int_t count = 0;
for ( unsigned int i = 0; i < nEvents; i++ ) {
all_tree -> GetEntry( i );
// test_energy = energy;
// test_eecal = eecal;
// test_ehcal = ehcal;
// test_mc_energy = mc_energy;
// test_mc_energy = roundf( mc_energy );
for ( unsigned int j = 0; j < nfiles; j++ ) {
if ( roundf( mc_energy ) == mass_energy[j] ) index = j;
}
for ( unsigned int k = 0; k < nEvents; k++ ) {
trees[ index ] -> GetEntry( k );
if ( energy == temp_energy[k] ) index2 = k;
}
trees[ index ] -> GetEntry( index2 );
if ( energy == temp_energy[ index2 ] ) count++;
if ( eecal == temp_eecal[ index2 ] ) count++;
if ( ehcal == temp_ehcal[ index2 ] ) count++;
if ( mc_energy == temp_mc_energy[ index2 ] ) count++;
if ( count == 4 ) cout << "True!!!" << endl;
else cout << "Not true!!!" << endl;
// test_energy = 0;
// test_eecal = 0;
// test_ehcal = 0;
// test_mc_energy = 0;
count = 0;
}
all->Close();
for( unsigned int i = 0; i < nfiles; i++ ) files[i]->Close();
//delete tree_r;
//for( unsigned int i = 0; i < nfiles; i++ ) delete trees[i];
std::cout << "That's all... " << endl;
}
*/