Skip to content

Commit 5e5fb33

Browse files
author
osahin
committed
minor cout fixes, adding a new output vector
1 parent 2e8d3a3 commit 5e5fb33

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

lib/csvc_interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class csvc_interface:public svm_interface {
3636
constexpr static double kSig = -1.0;
3737
public:
3838
virtual void set_sample (const svm_container&, samp_type);
39-
virtual void obtain_probabilities(double,double,int);
39+
virtual void obtain_probabilities(double,double,int,std::vector<int>*);
4040
csvc_interface (unsigned n_tot, unsigned bkg_tot, unsigned sig_tot):svm_interface("c-svc",n_tot,bkg_tot,sig_tot){
4141
gamma_array_size = 9; /* this number should be an odd number */
4242
gamma.resize(gamma_array_size);
@@ -136,7 +136,7 @@ class csvc_interface:public svm_interface {
136136
double sum_of_value (const svm_problem& tbs, int);//move this to svm_interface
137137
double sum_of_y_index (const svm_problem& tbs, int);//move this to svm_interface
138138
void get_extrema_features (const svm_problem &tbs, std::vector<double> & min, std::vector<double> & max);
139-
void scale (svm_problem& tbs, const std::vector<double> & min, const std::vector<double> & max);
139+
void scale (svm_problem& tbs, const std::vector<double> & min, const std::vector<double> & max, const bool print_out = false);
140140
virtual void set_parameters() {
141141
//setting svm model parameters
142142
parameters_set = true;

lib/svm_hint.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class svm_interface {
8888
virtual void set_parameters ()=0;
8989
virtual void gen_class_index ()=0;
9090
virtual void set_indexes ()=0;
91-
virtual void obtain_probabilities (double,double,int) = 0;
91+
virtual void obtain_probabilities (const double,const double,const int,std::vector<int>*) = 0;
9292
virtual void parameter_scan (double,double) = 0;
9393
virtual void classify (const svm_model *, const svm_problem &, vvector<double> &, vvector<double> &, int) = 0;
9494
virtual double parameter_comparison (const vvvector<double> &,const vvvector<double> &, const vvvector<double> &,const vvvector<double> &, const int, const double, double) = 0;
@@ -128,13 +128,13 @@ class svm_analyze{
128128
};
129129
void set_eval(const svm_container &eval, int eval_bkg){
130130
iEval_bkg = eval_bkg;
131-
given_svm->set_sample(eval,svm_interface::EVALUATE);
131+
given_svm->set_sample(eval, svm_interface::EVALUATE);
132132
};
133133
void set_filename(TString name){filename=name;}
134-
void Scan_parameters(){
134+
void Scan_parameters(std::vector<int> * output = 0){
135135
given_svm->parameter_scan(0.0001, 0.);
136136
std:: cout << given_svm->highest_accur_C << " "<<given_svm->highest_accur_gamma << std::endl;
137-
this->Obtain_probabilities(given_svm->highest_accur_C,given_svm->highest_accur_gamma);
137+
this->Obtain_probabilities(given_svm->highest_accur_C, given_svm->highest_accur_gamma, output);
138138
}
139139
void Do_probability_calc(){
140140
given_svm->do_probability_calc();
@@ -151,18 +151,18 @@ class svm_analyze{
151151
omp_set_num_threads(nthread);
152152
std::cout << " Number of threads in OMP is manually set to " << nthread << std::endl;
153153
}
154-
void Obtain_probabilities(double C, double g) const{
155-
given_svm->obtain_probabilities((double)C,(double)g,iEval_bkg);
154+
void Obtain_probabilities(const double C, const double g, std::vector<int> * output = 0) const{
155+
given_svm->obtain_probabilities((double)C,(double)g,iEval_bkg, output);
156156
TFile *f = new TFile(filename,"RECREATE");
157157
given_svm->disc_S->Write();
158158
given_svm->disc_B->Write();
159159
given_svm->cuteff->Write();
160160
given_svm->roc->Write();
161161
f->Close();
162162
}
163-
void Obtain_probabilities(double C, double g, double high_cut){
163+
void Obtain_probabilities(double C, double g, double high_cut, std::vector<int> * output = 0){
164164
given_svm->highest_accur_cut = high_cut;
165-
Obtain_probabilities((double)C, (double)g);
165+
Obtain_probabilities((double)C, (double)g, output);
166166
}
167167
};
168168
#endif

src/csvc_interface.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#define fEpsilon 1.e-2
1414
using std::cout;
1515
using std::endl;
16-
void csvc_interface::obtain_probabilities(double c_p , double g_p, int eval_bkg){
17-
cout << "Entering the obtain probabilities function... " << endl;
18-
cout << " input variables - C: " << c_p <<
19-
" gamma: " << g_p <<
20-
" highest accuracy cut: " << highest_accur_cut <<
21-
" # of bkg events in eval sample: " << eval_bkg << endl;
16+
void csvc_interface::obtain_probabilities(const double c_p , const double g_p, const int eval_bkg, std::vector<int> * output ){
17+
cout << "\n Classifying the evaluation sample... " << endl;
18+
cout << " input variables - C: " << c_p <<
19+
" gamma: " << g_p <<
20+
" highest significance cut on probability: " << highest_accur_cut <<
21+
" # of bkg events in eval sample: " << eval_bkg << endl;
2222
timer deltat;
2323
disc_S = new TH1D ("svm_disc_signal" , "SVM probability signal" , disc_nbin , 0., 1.);
2424
disc_B = new TH1D ("svm_disc_background" , "SVM probability background" , disc_nbin , 0., 1.);
@@ -34,6 +34,7 @@ void csvc_interface::obtain_probabilities(double c_p , double g_p, int eval_bkg)
3434
disc_B->Sumw2();
3535
/* svm prob container */
3636
double prob[2];
37+
bool pre_doprobabilitycalc = doprobabilitycalc ;
3738
doprobabilitycalc = true;
3839
set_parameters();
3940
set_indexes();
@@ -45,7 +46,7 @@ void csvc_interface::obtain_probabilities(double c_p , double g_p, int eval_bkg)
4546
if(min_features.empty() && max_features.empty()){
4647
get_extrema_features(sample_train, min_features, max_features);
4748
}
48-
scale(sample_train, min_features, max_features);
49+
scale(sample_train, min_features, max_features, true);
4950
scale(sample_eval, min_features, max_features);
5051
// adjust_weights(sample_train, kBkg, (double)nsig_train/(double)nbkg_train);
5152
deltat.start();
@@ -71,10 +72,12 @@ void csvc_interface::obtain_probabilities(double c_p , double g_p, int eval_bkg)
7172
for(int comp = 0; comp < nsamp_eval; comp++){
7273
svm_predict_probability(csvc_svm_model, sample_eval.x[comp], prob);
7374
if(eval_bkg > comp){
75+
if(output) output -> push_back(0);
7476
disc_B -> Fill(prob[1], sample_eval.W[comp]);
7577
disc_B_roc-> Fill(prob[1], sample_eval.W[comp]);
7678
if(prob[1] > highest_accur_cut) {bkg_yield += sample_eval.W[comp];}
7779
} else{
80+
if(output) output -> push_back(1);
7881
disc_S -> Fill(prob[1], sample_eval.W[comp]);
7982
disc_S_roc-> Fill(prob[1], sample_eval.W[comp]);
8083
if(prob[1] > highest_accur_cut) {sig_yield += sample_eval.W[comp];}
@@ -87,13 +90,19 @@ void csvc_interface::obtain_probabilities(double c_p , double g_p, int eval_bkg)
8790
tut->Close();
8891
/* assume 25% uncertainty */
8992
fom FOM (systematical_unc);
90-
FOM.maxSignificance(disc_S, disc_B, true, 0., cuteff);
93+
if(!pre_doprobabilitycalc){
94+
FOM.maxSignificance(disc_S, disc_B, true, 0., cuteff);
95+
} else{
96+
FOM.maxSignificance(disc_S, disc_B, false, 0., cuteff);
97+
}
9198
roc = FOM.ROC(disc_S_roc, disc_B_roc);
9299
FOM.setSignal (sig_yield);
93100
FOM.setBackground(bkg_yield);
94-
cout << " Significance obtained from the given cut: " << FOM.getSignificance(fom::asimov) <<
101+
if(pre_doprobabilitycalc){
102+
cout << " Significance obtained from the given cut: " << FOM.getSignificance(fom::asimov) <<
95103
" signal yield: " << sig_yield <<
96104
" background yield: " << bkg_yield << endl;
105+
}
97106
}
98107
void csvc_interface::set_gamma_array(std::vector<double> & gamma_arr, unsigned iter){
99108
int mid = (gamma_arr.size() - 1)/2;
@@ -220,7 +229,7 @@ double csvc_interface::parameter_comparison(const vvvector<double>& vSig, const
220229
high_accuracy_settings[3] = gamma[highest];
221230
highest_accur_gamma = gamma[highest];
222231
cout <<" For given C value: " << std::setprecision(16) << C <<
223-
" highest accuracy: " << std::setprecision(6) << accuracy->at(highest) <<
232+
" highest significance: " << std::setprecision(6) << accuracy->at(highest) <<
224233
" obtained with gamma: " << std::setprecision(16) << gamma [highest] <<
225234
" on the bin " << std::setprecision(6) << max_cut_bin->at(highest) <<
226235
" in the iteration " << iteration << endl;
@@ -251,20 +260,20 @@ bool csvc_interface::parameter_decision() {
251260
if (highest_accur_C > C_cut && highest_accur_C / above_cut_inc > C_cut){
252261
above_cut_inc -= (above_cut_inc - 1.) * 4. / 5.;
253262
below_cut_inc -= (below_cut_inc - 1.) * 4. / 5.;
254-
cout << " entering precise scan with initial C " << highest_accur_C / (above_cut_inc * above_cut_inc) << " and previous accuracy " << it_max->at(0) << endl;
263+
cout << " entering precise scan with initial C " << highest_accur_C / (above_cut_inc * above_cut_inc) << " and previous signifincance " << it_max->at(0) << endl;
255264
this->parameter_scan(highest_accur_C - 2. * above_cut_inc , it_max->at(0));
256265
} else {
257266
above_cut_inc -= (above_cut_inc - 1.) * 4. / 5.;
258267
below_cut_inc -= (below_cut_inc - 1.) * 4. / 5.;
259-
cout << " entering precise scan with initial C " << highest_accur_C / (below_cut_inc * below_cut_inc) << " and previous accuracy " << it_max->at(0) << endl;
268+
cout << " entering precise scan with initial C " << highest_accur_C / (below_cut_inc * below_cut_inc) << " and previous significance " << it_max->at(0) << endl;
260269
this->parameter_scan(highest_accur_C / (below_cut_inc * below_cut_inc) , it_max->at(0));
261270
}
262271
} else {
263-
cout << " Optimized C " << std::setprecision(16) << std::fixed << highest_accur_C <<
272+
cout << "\n Optimized C " << std::setprecision(16) << std::fixed << highest_accur_C <<
264273
" optimized gamma " << std::setprecision(16) << std::fixed << highest_accur_gamma <<
265274
" optimized discriminator cut " << std::setprecision(4) << std::fixed << highest_accur_cut <<
266-
" with the accuracy of " << std::setprecision(4) << std::fixed << it_max->at(0) <<
267-
" in the test sample " << std::setprecision(12) << endl;
275+
" with the significance of " << std::setprecision(4) << std::fixed << it_max->at(0) <<
276+
" in the test sample \n " << std::setprecision(12) << endl;
268277
clean(para_scan_problem_test);
269278
clean(para_scan_problem_train);
270279
return true;
@@ -349,10 +358,10 @@ void csvc_interface::get_extrema_features (const svm_problem &tbs, std::vector<d
349358
}
350359
}
351360
}
352-
void csvc_interface::scale (svm_problem &tbs, const std::vector<double> & min, const std::vector<double> & max){
353-
cout << " Entering scale function: " << endl;
361+
void csvc_interface::scale (svm_problem &tbs, const std::vector<double> & min, const std::vector<double> & max, const bool print_out){
362+
if(print_out) cout << " Entering scale function: " << endl;
354363
for (unsigned feat = 0; feat < nfeature; feat++) {
355-
cout << " feature: " << feat + 1 << " min: " << min.at(feat) << " max: " << max.at(feat) << endl;
364+
if(print_out) cout << " feature: " << feat + 1 << " min: " << min.at(feat) << " max: " << max.at(feat) << endl;
356365
}
357366
for(unsigned ind = 0; ind < tbs.l; ind++) {
358367
for( unsigned feat = 0; feat < nfeature; feat++) {

test/svm_hint_tutor.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ int main(){
8989
size_t nsamp_tot = svm.svm_cont->size();
9090
size_t nsig_tot = nsamp_tot - nbkg_tot;
9191
size_t neval_tot = svm_eval.svm_cont->size();
92+
std::vector<int> * output = new vector<int>;
9293
std::cout << " Total event number in the svm container: " << svm.svm_cont->size() <<
9394
" total event number in the svm evaluation container: " << svm_eval.svm_cont->size() << std::endl;
9495
svm_interface * csvc = new csvc_interface(nsamp_tot,nbkg_tot,nsig_tot);
@@ -97,18 +98,25 @@ int main(){
9798
stop.set_svm_interface(csvc);
9899
stop.setup_svm(svm);
99100
stop.set_eval(svm_eval,nbkg_eval);
100-
/* timer prob;
101-
stop.Obtain_probabilities(1, 1000.,0.575);// c , gamma
102-
prob.stop("svm training and test takes: ");*/
101+
103102
//Enable probabilistic output
104103
stop.Do_probability_calc();
105104
//set the systematical unc
106105
stop.Set_systematical_unc(0.25);
107106
//set # of threads
108-
stop.Set_omp_threads(8);
107+
stop.Set_omp_threads(12);
108+
109+
/* timer prob;
110+
stop.Obtain_probabilities(1, 1000.,0.575, output);// c , gamma
111+
prob.stop("svm training and test takes: ");*/
109112
timer tmain;
110113
tmain.start();
111-
stop.Scan_parameters();
114+
stop.Scan_parameters(output);
112115
tmain.stop("scanning parameters takes: ");
116+
/*
117+
for(auto it =output->begin(); it!=output->end(); it++){
118+
std::cout << (*it) << " ";
119+
}
120+
*/
113121
return 0;
114122
}

0 commit comments

Comments
 (0)