-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_fit_chi_ratio.c
266 lines (202 loc) · 8.44 KB
/
run_fit_chi_ratio.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
void fit_data( const int fenergy = 5, const int nBins = 1000 ) {
char fname[ 200 ];
char ftempname[ 200 ];
char fname2[ 200 ];
char fname3[ 200 ];
// const float cut = sqrtf( float( fenergy ) );
// cout << "Cut === " << cut << endl;
for ( int i = 0; i < 200; i++ ) fname[ i ] = 0;
for ( int i = 0; i < 200; i++ ) ftempname[ i ] = 0;
for ( int i = 0; i < 200; i++ ) fname2[ i ] = 0;
for ( int i = 0; i < 200; i++ ) fname3[ i ] = 0;
sprintf( ftempname, "gun_k0L_%dgev_FTFP_BERT_5000evt_ILD_l5_v02steel", fenergy );
sprintf( fname, "%s.root", ftempname );
TH1F *hist;
TH1F *hist_mean;
TH1F *hist_sigma;
TH1F *hist_chi_full;
TH1F *hist_chi;
TH1F *hist_meanerror;
TH1F *hist_sigmaerror;
TCanvas *c1 = new TCanvas("c1", "c1", 1);
gStyle->SetOptStat(0);
const char *treeName = "tree";
const float binLo = 0.0;
const float binHi = fenergy + 30.0;
const int nBins_fit = 10;
const float binHi_fit = ( binHi - fenergy ) / 2.0;
const float binHi_fit_error = binHi_fit - 8.0;
// const float cut = 2.0; //0.6 * sqrt( energy );
//
// Open data file
//
std::cout << "Trying to open data file... ";
TFile *file = new TFile(fname, "READ");
if (!file) { // if error occure then exit
std::cout << "[FAIL]" << std::endl;
return;
}
std::cout << "[OK]" << std::endl;
sprintf( fname3, "%d_out_data.txt", fenergy );
//
// Open file for output
//
FILE *out = fopen(fname3, "a+");
printf("File open... ");
if (!out) {
printf("[FAIL]\n");
return;
} else printf("[OK]\n");
//
// Setup a TTree
//
std::cout << "Setup a tree... ";
TTree *tree = (TTree *)file->Get(treeName);
if (!tree) {
std::cout << "[FAIL]" << std::endl;
file->Close();
return;
}
std::cout << "[OK]" << std::endl;
unsigned int nEvents = tree->GetEntries();
//
// Setup a branch
//
Float_t energy = 0;
tree->SetBranchAddress("energy", &energy);
//
// Create a histogram and random generator
//
hist = new TH1F("hist", "hist", nBins, binLo, binHi);
TAxis *xaxis = hist->GetXaxis();
hist_mean = new TH1F("hist_mean", "hist_mean", nBins_fit, binLo, binHi);
hist_sigma = new TH1F("hist_sigma", "hist_sigma", nBins_fit, binLo, binHi_fit);
hist_chi_full = new TH1F("hist_chi_full", "hist_chi_full", nBins_fit, binLo, binHi_fit);
hist_chi = new TH1F("hist_chi", "hist_chi", nBins_fit, binLo, binHi_fit);
hist_meanerror = new TH1F("hist_meanerror", "hist_meanerror", nBins_fit, binLo, binHi_fit_error);
hist_sigmaerror = new TH1F("hist_sigmaerror", "hist_sigmaerror", nBins_fit, binLo, binHi_fit_error);
for ( int i = 0; i <nEvents; i++ ) {
tree -> GetEntry(i);
if ( energy > 0 ) hist -> Fill( energy );
}
cout << "Underflow hist === " << hist->GetBinContent( 0 ) << endl;
cout << "Overflow hist === " << hist->GetBinContent( nBins + 1 ) << endl;
float mean = hist -> GetMean();
float meanerror = hist -> GetMeanError();
float rms = hist -> GetRMS();
float rmserror = hist -> GetRMSError();
cout << " 0:"
<< " Mean: " << mean
<< " Mean error: " << meanerror
<< " RMS: " << rms
<< " RMS error: " << rmserror
<< endl;
float step = 0.1;
float mass_chi_full[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_chi_full[ i ] = 0;
float mass_chi[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_chi[ i ] = 0;
float mass_mean[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_mean[ i ] = 0;
float mass_sigma[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_sigma[ i ] = 0;
float mass_meanerror[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_meanerror[ i ] = 0;
float mass_sigmaerror[ 20 ];
for ( int i = 0; i < 20; i++ ) mass_sigmaerror[ i ] = 0;
int i_const = 10;
int integral = 0;
float fraction = 0.0;
for ( int i = 10; i <= 25; i++ ) {
cout << "i === " << i << endl;
float range = i * step;
TF1 *fitFunc = new TF1("fitFunc", "gaus", mean - range * rms, mean + range * rms);
hist -> Fit( fitFunc, "R" );
float fitmean = fitFunc -> GetParameter(1);
float fitsigma = fitFunc -> GetParameter(2);
float fitchi = fitFunc -> GetChisquare() / fitFunc -> GetNDF();
float fitmeanerror = fitFunc -> GetParError(1);
float fitsigmaerror = fitFunc -> GetParError(2);
integral = hist->Integral( xaxis->FindBin( mean - range * rms ), xaxis->FindBin( mean + range * rms ) );
cout << "Integral === " << integral << endl;
// cout << "Integral Error === " << hist->IntegralError( xaxis->FindBin( mean - range * rms ), xaxis->FindBin( mean + range * rms ) ) << endl;
cout << "chi === " << fitchi << endl;
fraction = integral / hist->GetEntries();
fprintf( out, "%d %d %0.1f %E %d %f\n", fenergy, int( float( i ) - 10.0 ), ( float( i ) / 10.0 ), fitchi, integral, fraction );
hist_chi_full -> Fill( fitchi );
mass_chi_full[ i - i_const ] = fitchi;
if ( fitchi < 1.5 ) {
hist_mean -> Fill( fitmean );
mass_mean[ i - i_const ] = fitmean;
hist_sigma -> Fill( fitsigma );
mass_sigma[ i - i_const ] = fitsigma;
hist_chi -> Fill( fitchi );
mass_chi[ i - i_const ] = fitchi;
hist_meanerror -> Fill( fitmeanerror );
mass_meanerror[ i - i_const ] = fitmeanerror;
hist_sigmaerror -> Fill( fitsigmaerror );
mass_sigmaerror[ i - i_const ] = fitsigmaerror;
}
cout << " 1:"
<< " Mean: " << fitmean
<< " Mean error: " << fitmeanerror
<< " Sigma: " << fitsigma
<< " Sigma error: " << fitsigmaerror
<< " Chi2/ndf: " << fitchi
<< endl;
// sprintf( fname2, "%d_%s.jpg", i, ftempname );
// c1->SaveAs( fname2 );
// for ( int j = 0; j < 200; j++ ) fname2[ j ] = 0;
delete fitFunc;
}
cout << "Underflow hist_mean === " << hist_mean->GetBinContent( 0 ) << endl;
cout << "Overflow hist_mean === " << hist_mean->GetBinContent( nBins_fit + 1 ) << endl;
cout << "Underflow hist_sigma === " << hist_sigma->GetBinContent( 0 ) << endl;
cout << "Overflow hist_sigma === " << hist_sigma->GetBinContent( nBins_fit + 1 ) << endl;
cout << "Underflow hist_chi === " << hist_chi->GetBinContent( 0 ) << endl;
cout << "Overflow hist_chi === " << hist_chi->GetBinContent( nBins_fit + 1 ) << endl;
cout << "Underflow hist_chi_full === " << hist_chi_full->GetBinContent( 0 ) << endl;
cout << "Overflow hist_chi_full === " << hist_chi_full->GetBinContent( nBins_fit + 1 ) << endl;
cout << "Underflow hist_meanerror === " << hist_meanerror->GetBinContent( 0 ) << endl;
cout << "Overflow hist_meanerror === " << hist_meanerror->GetBinContent( nBins_fit + 1 ) << endl;
cout << "Underflow hist_sigmaerror === " << hist_sigmaerror->GetBinContent( 0 ) << endl;
cout << "Overflow hist_sigmaerror === " << hist_sigmaerror->GetBinContent( nBins_fit + 1 ) << endl;
float min_chi = 2.5;
float min = 0.0;
int i_min = 0;
for ( int i = 0; i < 20; i++ ) {
if ( mass_chi[ i ] > 0.0 ) {
if ( mass_chi[ i ] < min_chi ) {
min_chi = mass_chi[ i ];
i_min = i;
}
}
}
// float mean_chi = hist_chi -> GetMean();
// float rms_chi = hist_chi -> GetRMS();
float mean_chi = mass_chi[ i_min ];
cout << " Chi:"
<< " Mean_chi: " << mean_chi
// << " RMS_chi: " << rms_chi
<< endl;
// fprintf( out, "%d %E %E %E %E %E %E %E %E %E %E %E %E %E %E\n", fenergy, mean, meanerror, rms, rmserror, mean_mean, rms_mean, mean_meanerror, rms_meanerror, mean_sigma, rms_sigma, mean_sigmaerror, rms_sigmaerror, mean_chi, rms_chi );
// fprintf( out, "%d %E %E %E %E %E %E %E %E %E %E %E\n", fenergy, mean, meanerror, rms, rmserror, mean_mean, rms_mean, mean_meanerror, mean_sigma, rms_sigma, mean_sigmaerror, mean_chi );
// fprintf( out, "%d %E %E %E %E %E %E %E %E %E\n", fenergy, mean, meanerror, rms, rmserror, mean_mean, mean_meanerror, mean_sigma, mean_sigmaerror, mean_chi );
delete hist;
fclose( out );
file -> Close();
for ( int i = 0; i < 20; i++ ) printf( "%E\n", mass_chi[ i ] );
printf("-------------------\n");
printf( "%E\n", mass_chi[ i_min ] );
printf( "%E\n", min_chi );
printf("-------------------\n");
for ( int i = 0; i < 20; i++ ) printf( "%E\n", mass_chi_full[ i ] );
}
void run_fit_chi_ratio( const int bins = 1000 ) {
// int mass[] = { 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90 };
int mass[] = { 5, 10, 15, 20, 25, 30, 40, 50, 60 };
int dim = 9; // 16
for (int i = 0; i < dim; i++ ) {
fit_data( mass[ i ], bins );
}
}