BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
binning.cxx
Go to the documentation of this file.
1//
2// This program is to optimize the binning, a mixture method is used
3// angle: equi-width, dedx: equi-frequency, beta-gamma: maximize information entropy
4// for information entropy method, firstly we should classified data into different groups with respect to beta-gamma
5//
6
7#include "TCanvas.h"
8#include "TFile.h"
9#include "TTree.h"
10#include "TH1F.h"
11#include "TMath.h"
12#include "TObjectTable.h"
13#include <sstream>
14#include <iostream>
15#include <fstream>
16#include <string>
17#include <cstring>
18#include <vector>
19#include <cmath>
20
21using namespace std;
22
23TTree* t = new TTree("t", "tree to save original data");
24double m_left(0), m_right(0);
25int m_bins(0);
26typedef std::vector<double> vdouble;
27bool m_debug(true);
29vdouble v(0); // a temp vector
30void f_opt(double left, double right, int bins); // optimize binning, find beta-gamma points
31double f_cals(double left, double right, double step); // calculate entropy
32void f_gen_hists(){};
33void print_vec(vdouble v);
35const double test_times(2);
36double total_n(0); // total nhits in one region
37TTree *t_save = new TTree("t_save", "tree to save entropy"); // used to save calculated entropy
38double save_left(0), save_right(0), save_pos(0), save_s(0);
39int tot_num_group; // number of groups
40double x_group[2000]; // save the boundary of groups
41double hits_group[2000]; // save hits in a group
42
43int main(int argc, char **argv){
44 // gObjectTable->Print();
45 if(argc<2){ cout << "please append config file, ex. binning.exe config.txt" << endl; return 0;}
46 TFile *f = new TFile("pre_data.root");
47 t = (TTree*)f->Get("n103");
48 ifstream config_file(argv[1]);
49 if(!config_file){
50 cout << "cannot read config file correctly!" << endl;
51 return 1;
52 }
53 TFile *f_group = new TFile("group_points.root");
54 if(!f_group){
55 cout << "cannot read group points file correctly!" << endl;
56 return 1;
57 }
58 TTree *t_group = (TTree*)f_group->Get("t");
59 double m_x_temp(0);
60 t_group->SetBranchAddress("x", &m_x_temp);
61 tot_num_group = t_group->GetEntries();
62 for(int i=0; i<tot_num_group; i++) { t_group->GetEntry(i); x_group[i] = m_x_temp; }
63 // t_group->Delete();
64 f_group->Close();
65 delete f_group;
66 // prepare tree t_save
67 t_save->Branch("save_left", &save_left, "save_left/D");
68 t_save->Branch("save_right", &save_right, "save_right/D");
69 t_save->Branch("save_pos", &save_pos, "save_pos/D");
70 t_save->Branch("save_s", &save_s, "save_s/D");
71 //
72 while(m_bins!=-1){ // -1 is the end signal
73 config_file >> m_left >> m_right >> m_bins >> m_debug;
74 if(m_debug) cout << "config: " << m_left << " " << m_right << " " << m_bins << endl;
76 cout <<"finial results for this time binning" << endl;
79 }
80 t->Delete();
81 t_save->Delete();
82 f->Close();
83 delete f;
84 return 0;
85}
86
87
88void f_opt(double m_left, double m_right, int bins){
89 if(m_debug) cout << "f_opt() is happily running!" << endl;
90 int total(1); // number of bins
91 vec_bg.clear();
92 vec_bg.push_back(m_left);
93 vec_bg.push_back(m_right);
94 TTree *tree = new TTree("tree", "tree to save temp data");
95 tree = t->CopyTree(Form("bg>%f && bg<%f", m_left, m_right));
96 int m_hits;
97 total_n = 0;
98 tree->SetBranchAddress("nhits", &m_hits);
99 int m_num_total(tree->GetEntries());
100 for(int k=0; k<m_num_total; k++){
101 tree->GetEntry(k);
102 total_n += m_hits;
103 }
104 tree->Delete();
105
106 while(total<bins){
107 double temp_s(0), temp_pos(0);
108 int temp_i(-1);
109 for(unsigned int i=0; i<vec_bg.size(); i++){
110 double pos_step((vec_bg[i+1]-vec_bg[i])/test_times);
111 double ori_s(-1);
112 for(double pos=vec_bg[i]+pos_step; pos<vec_bg[i+1]-0.00001; pos+=pos_step){
113 if(ori_s<0) ori_s = f_cals(vec_bg[i], vec_bg[i+1], vec_bg[i+1]);// for the ori one, no mid point
114 v.clear();
115 v = vec_bg;
116 v.insert(v.begin()+i+1, pos);
117 double diff_s = ori_s - f_cals(vec_bg[i], vec_bg[i+1], pos); // information gain
118 if(diff_s > temp_s){
119 temp_s = diff_s;
120 temp_i = i;
121 temp_pos = pos;
122 }
123 if(m_debug){
124 cout << "i:" << i << " pos:" << pos << " diff_s:" << diff_s << " temp_s:" << temp_s << endl;
125 cout << "temp_v: " << endl;
126 print_vec(v);
127 cout << "vec_bg: " << endl;
129 }// end of print results
130 }// end of loop in inert region
131 }// end of loop in whole region
132 vec_bg.insert(vec_bg.begin()+temp_i+1, temp_pos);
133 cout << "============== vec_bg =============" <<endl;
135 cout << endl << endl;
136 total ++;
137 }// end of while()
138}// end of f_opt()
139
140
141void f_group(double left, double right, int &num, int &index){
142 num = 0;
143 index = -1;
144 for(int i=1; i<tot_num_group; i++){
145 if(num==0 && left<x_group[i]) { index = i-1; num = 1; }
146 if(right<=x_group[i]){ num = i - index; break;}
147 }
148}
149
150int f_group(double bg){
151 for(int i=1; i<tot_num_group; i++){
152 if(bg<x_group[i]) return i-1;
153 }
154 return tot_num_group;
155}
156
157double f_cals(double left, double right, double pos){
158 // gObjectTable->Print();
159 if(m_debug) cout << "f_cal() is happyily running!" << endl;
160 for(int i=0; i<t_save->GetEntries(); i++){
161 t_save->GetEntry(i);
162 if(fabs(save_left-left)<0.0001 && fabs(save_right-right)<0.0001 && fabs(save_pos-pos)<0.0001) return save_s;
163 }
164 double left_s(0), right_s(0), total_s(0);
165 double left_ratio(0), right_ratio(0);
166 TFile *file = new TFile("temp.root", "recreate");
167 TTree *tree = t->CopyTree(Form("bg>%f && bg<%f", left, right));
168 if(m_debug){ cout << "after open temp.root" << endl; gDirectory->pwd(); gDirectory->ls(); }
169 double a_step(0.93*2/10);
170 for(int i=0; i<2; i++){// only do test twice
171 if(i==0) {m_left = left; m_right = pos;}
172 else {m_left = pos; m_right = right;}
173 int m_num_group(0);// how many groups will be covered by this bin
174 int m_index_group(-1); // the index of left boundary of the groups
175 f_group(m_left, m_right, m_num_group, m_index_group);
176 if(m_debug) cout << "left " << m_left << " right " << m_right << " num of groups: " << m_num_group << " boundary index: " << m_index_group << endl;
177 for(int j=0; j<10; j++){// index of angle
178 TTree *temp_tree = new TTree("tepm_tree", "temp tree");
179 temp_tree = tree->CopyTree(Form("bg>%f && bg<%f && costheta>%f && costheta<%f", m_left, m_right, -0.93+j*a_step, -0.93+(j+1)*a_step));
180 int nhits(0);
181 Float_t m_bg(0);
182 double tot_nhits(0);
183 temp_tree->SetBranchAddress("nhits", &nhits);
184 temp_tree->SetBranchAddress("bg", &m_bg);
185 for(int m=0; m<tot_num_group; m++) hits_group[m]=0;
186 for(int k=0; k<temp_tree->GetEntries(); k++){// loop in track
187 temp_tree->GetEntry(k);
188 tot_nhits += nhits;
189 hits_group[f_group(m_bg)] += nhits;
190 }
191 if(tot_nhits<2000){ file->Close(); return(100000.); } // if too small hits in a bin, bad (large) entropy
192 else{
193 for(int n=0; n<m_num_group; n++){ // loop in groups
194 if(hits_group[m_index_group+n]==0) continue;
195 // cout << "group_hits " << hits_group[m_index_group+n] << " tot_nhits " << tot_nhits << endl;
196 double p(hits_group[m_index_group+n]/tot_nhits); // the probability to find a hit of group_k in a bin
197 if(p>1) cout << "m_index_group " << m_index_group << " n " << n << " hits_group[] " << hits_group[m_index_group+n] << " tot_nhits " << tot_nhits << endl;
198 if(i==0) left_s += -p*TMath::Log2(p);
199 if(i==1) right_s += -p*TMath::Log2(p);
200 }// end of loop in groups
201 if(i==0) left_ratio += tot_nhits;
202 if(i==1) right_ratio += tot_nhits;
203 }
204 if(m_debug) cout << "m_left:" << m_left << " m_right:" << m_right << " j:" << j << " left_s:" << left_s << " right_s:" << right_s << endl;
205 // temp_tree->Delete();
206 }// end of loop angle
207 if(fabs(right-pos)<0.00001) break;
208 }// end of loop beta-gamma
209 // tree->Delete();
210 file->Close();
211 left_ratio = left_ratio/(left_ratio+right_ratio);
212 right_ratio = 1-left_ratio;
213 total_s = left_ratio*left_s + right_ratio*right_s;
214 if(m_debug) cout << "left_ratio: " << left_ratio << " right_ratio: " << right_ratio << " total_s: " << total_s << endl;
215 // save to the tree t_save
216 save_left = left;
217 save_right = right;
218 save_pos = pos;
219 save_s = total_s;
220 t_save->Fill();
221 return(total_s);
222}
223
225 for(vdouble::iterator i=v.begin(); i<v.end(); i++) cout << *i << " " ;
226 cout << endl;
227}
228
230 cout << "vec[" << m_bins << "] = {" ;
231 for(vdouble::iterator i=v.begin(); i<v.end(); i++) cout << *i << ", " ;
232 cout << "};" << endl;
233}
char * file
Definition: DQA_TO_DB.cxx:15
int bins[20]
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
TTree * t_save
Definition: binning.cxx:37
TTree * t
Definition: binning.cxx:23
void f_group(double left, double right, int &num, int &index)
Definition: binning.cxx:141
vdouble vec_bg(0)
double m_right(0)
int m_bins(0)
std::vector< double > vdouble
Definition: binning.cxx:26
double save_pos(0)
double hits_group[2000]
Definition: binning.cxx:41
int tot_num_group
Definition: binning.cxx:39
void f_opt(double left, double right, int bins)
Definition: binning.cxx:88
double save_right(0)
void print_vec_verso(vdouble v)
Definition: binning.cxx:229
double save_s(0)
void f_gen_hists()
Definition: binning.cxx:32
double save_left(0)
const double test_times(2)
double m_left(0)
double total_n(0)
double x_group[2000]
Definition: binning.cxx:40
double f_cals(double left, double right, double step)
Definition: binning.cxx:157
void print_vec(vdouble v)
Definition: binning.cxx:224
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
int nhits
float bg
int main()
Definition: test_IFile.cxx:11