BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
histgen.cxx
Go to the documentation of this file.
1//
2// This program is to generate histograms for sampling of dE/dx simulation
3// First the optimized binning of beta-gamma of hadron is read from an external file
4// No optimization for muon and electron, just use equi-width for them
5// Then save the dE/dx hit values to tree with equi-width of angle
6// Finally, binning them with equi-frequency
7//
8
9#include "TCanvas.h"
10#include "TFile.h"
11#include "TTree.h"
12#include "TH1F.h"
13#include <sstream>
14#include <iostream>
15#include <string>
16#include <cstring>
17#include <vector>
18#include <algorithm>
19#include <functional>
20#include <cmath>
21using namespace std;
22// binning information for beta-gamma is read from file "binning.h", only modify it if necessary
23#include "binning.h"
24
25const int na = 10; // #angle
26const int nc = 2; // #charge
27
28const double muon_step = (double)(bg_muon_max - bg_muon_min) / (double)bg_muon_bin;
29const double electron_step = (double)(bg_electron_max - bg_electron_min) / (double)bg_electron_bin;
30
31static int histNum = 0;
32
33int find_bgidx(int type, double bgg, double bg_min, double bg_step, int nbin);
34double find_boundary(int type, int idx, double bg_min, double bg_step);
35void t_to_h(TTree *t, TH1F *h);
36void histgen(string, int, TObjArray&, vector<double>&);
37void histps(string);
38void hist_update(); // update histograms to the form suitable for database
39const bool ps_flag(false);
40const bool m_debug(false);
41TFile *f_temp = new TFile("temp.root", "recreate");
42
43int main(){
44 std::cout << "in main()" << std::endl;
45 //call fuction histgen to generate histograms
46 TObjArray histCol;
47 vector<double> bg;
48 // now only three types, 0 for hadron, 3 for muon, 4 for electron
49 std::cout << "before histgen" << std::endl;
50 histgen("pre_data.root", 0, histCol, bg);
51 std::cout << "hadron hists has been generated" << std::endl;
52
53 histgen("pre_data.root", 3, histCol, bg);
54 std::cout << "muon hists has been generated" << std::endl;
55
56 histgen("pre_data.root", 4, histCol, bg);
57 std::cout << "electron hists has been generated" << std::endl;
58
59 //define file and write to it
60 string filename = "DedxSampleHists.root";
61 TFile* f = new TFile(filename.c_str(), "recreate");
62
63 int total = histCol.GetLast() + 1;
64 int bg_num = bg.size();
65 if(m_debug) cout << "total: " << total << " bg num = " << bg_num << endl;
66 double betagamma[3000];
67 for (unsigned int i = 0; i < bg.size(); i++) betagamma[i] = bg[i];
68
69 TTree* t = new TTree("bin", "bin information");
70 t->Branch("totalNum", &total, "total/I");
71 t->Branch("betagammaBounds", &bg_num, "bg_num/I");
72 t->Branch("betagamma", betagamma, "betagamma[bg_num]/D");
73 t->Fill();
74 t->Write();
75
76 std::cout << "begin write hists" << std::endl;
77 for (int i=0; i<total; i++){
78 if(i%100==0) cout << "i: " << i << " histogram is written now" << endl;
79 ((TH1F*)histCol[i])->Write();
80 }
81 std::cout << "write hists finish" << std::endl;
82
83 f->Close();
84 f_temp->Close();
85 if(ps_flag) histps(filename);
87 return 0;
88}
89
90
91// find beta-gamma index by beta-gamma value
92int find_bgidx(int type, double bgg, double bg_min, double bg_step, int nbin){
93 if(type==0){
94 for(int i=0; i<=nbin; i++){
95 if(bgg<vec_bg_hadron[i+1]) return i;
96 }
97 cout << "not found correct bgidx, something is wrong!" << endl;
98 return -1;
99 }
100 else return((int)(bgg-bg_min)/bg_step);
101}
102
103// find beta-gamma boundary by index
104double find_boundary(int type, int idx, double bg_min, double bg_step){
105 if(type==0) return vec_bg_hadron[idx];
106 else return(bg_min + bg_step*idx);
107}
108
109// generate histograms from tree with equi-frequency method
110void t_to_h(TTree *t, TH1F *h){
111 vector<double> vec_dedx(0);
112 double m_dedx;
113 t->SetBranchAddress("dedx_hit", &m_dedx);
114 for(int i=0; i<t->GetEntries(); i++){
115 t->GetEntry(i);
116 vec_dedx.push_back(m_dedx);
117 }
118 sort(vec_dedx.begin(), vec_dedx.end());
119 double points[101];
120 int step = t->GetEntries()/100;
121 int rad = t->GetEntries()%100;
122 int count = 0;
123 int sum = 1;
124 points[0] = vec_dedx.front();
125 if(m_debug) cout << "points[0] " << points[0] << endl;
126 for(unsigned int i=0; i<vec_dedx.size(); i++){
127 count ++;
128 if(count>=step){
129 points[sum] = (vec_dedx[i]+vec_dedx[i+1])/2;
130 sum ++;
131 count = 0;
132 }
133 if(m_debug) cout << "i= " << i << " rad " << rad << " step " << step << " count " << count << " sum " << sum-1 << " point " << points[sum-1] << endl;
134 if(sum==100) break;
135 }
136 points[100] = vec_dedx.back();
137 // if(m_debug) cout << "points[100] " << points[100] << endl;
138 double points_mod[111];
139 for(int j=0; j<111; j++){
140 if(j<11) points_mod[j] = points[1]/11.*j;
141 else points_mod[j] = points[j-10];
142 if(m_debug) cout << "j= " << j << " modified point " << points_mod[j] << endl;
143 }
144 h->SetBins(110, points_mod);
145 t->Project(h->GetName(), "dedx_hit");
146 t->Delete();
147}
148
149void histgen(string filename, int type, TObjArray& hist, vector<double>& bg){
150 float exraw[100];
151 float bgg, dEdx_meas, costheta, charge;
152 int nbin, ndedxhit;
153 double bg_min, bg_max, bg_step;
154
155 TFile* f = new TFile(filename.c_str());
156 TTree* trk = (TTree*)f->Get("n103");
157 trk->SetBranchAddress("dEdx_meas", &dEdx_meas);
158 trk->SetBranchAddress("bg", &bgg);
159 trk->SetBranchAddress("costheta", &costheta);
160 trk->SetBranchAddress("charge", &charge);
161 trk->SetBranchAddress("dEdx_hit", exraw);
162 trk->SetBranchAddress("nhits", &ndedxhit);
163
164 //initialize variables
165 switch (type) {
166 case 0:
167 nbin = bg_hadron_bin;
168 bg_min = bg_hadron_min;
169 bg_max = bg_hadron_max;
170 break;
171 case 3:
172 nbin = bg_muon_bin;
173 bg_min = bg_muon_min;
174 bg_max = bg_muon_max;
175 break;
176 case 4:
177 nbin = bg_electron_bin;
178 bg_min = bg_electron_min;
179 bg_max = bg_electron_max;
180 break;
181 default:
182 std::cout << "Error: no such sample type!" << std::endl;
183 }
184
185 //define trees and histograms
186 f_temp->cd();
187 TH1F** h = new TH1F*[nbin * 2 * na];
188 TTree** t = new TTree*[nbin * 2 * na];
189 stringstream ss, sst;
190 double m_dedx_hit;
191 for (int i = 0; i < nbin * 2 * na; i++){ // nbin is different for different type of particle
192 ss.str(""); sst.str("");
193 ss << "h" << histNum + i; sst << "t" << histNum + i;
194 h[i] = new TH1F(ss.str().c_str(), ss.str().c_str(), 1, 0, 1);
195 f_temp->cd();
196 t[i] = new TTree(sst.str().c_str(), sst.str().c_str());
197 t[i]->Branch("dedx_hit", &m_dedx_hit, "dedx_hit/D");
198 }
199 histNum += nbin*2*na; // the number of total histograms is a sum of No. of hists of each particle type
200
201 //loop every hit of n103
202 int bgidx(-1), angleidx(-1), chargeidx(-1), treeidx(-1);
203
204 for (int i = 0; i < trk->GetEntries(); i++){
205 trk->GetEntry(i);
206 if(bgg<bg_min || bgg>bg_max) continue;
207 // if(m_debug) cout << "i " << i << " dedx " << dEdx_meas << " bgg " << bgg << " costheta " << costheta << " charge " << charge << " nhits " << ndedxhit << endl;
208
209 // betagamma bin index
210 bg_step = (bg_max-bg_min)/nbin;
211 bgidx = find_bgidx(type, bgg, bg_min, bg_step, nbin);
212 if(bgidx<0 || bgidx>=nbin) continue;
213
214 // costheta bin index
215 angleidx = (int)(fabs(costheta) * na / 0.93);
216 if(angleidx<0 || angleidx>=na) continue;
217
218 // charge bin index
219 if (charge == 0) continue;
220 else chargeidx = (charge > 0) ? 0 : 1;
221 // if(m_debug) cout << "bgidx " << bgidx << " angleidx " << angleidx << " chargeidx " << chargeidx <<endl;
222
223 // fill tree
224 treeidx = -1;
225 for (int j = 0; j <ndedxhit; j++) {
226 // for(int idx_charge = chargeidx; idx_charge< chargeidx+2; idx_charge++){
227 treeidx = bgidx*na*2 + angleidx*2 + chargeidx;
228 m_dedx_hit = exraw[j];
229 // if(m_debug) cout << "j " << j << "dedx_hit " << exraw[j] << endl;
230 if(m_dedx_hit>0 && m_dedx_hit<500000) t[treeidx]->Fill(); // a very loose cut on dE/dx value
231 //}
232 }// end of loop in all hits in one track
233 }// end of loop in all tracks in one type of particle
234
235 //push the histograms and betagammas to the vector
236 for (int i = 0; i < nbin * na * nc; i++){
237 if(t[i]->GetEntries()<1000) cout << "i " << i << " no." << t[i]->GetEntries() << endl;
238 t_to_h(t[i], h[i]);
239 hist.Add(h[i]);
240 }
241 for (int i = 0; i < nbin; i++){
242 bg.push_back(find_boundary(type, i, bg_min, bg_step));
243 bg.push_back(find_boundary(type, i+1, bg_min, bg_step));
244 }
245}// end of histgen() function
246
247
248void histps(string name){
249 TFile f(name.c_str());
250 TTree* t = (TTree*)f.Get("bin");
251 Int_t total, bgb;
252 t->SetBranchAddress("totalNum", &total);
253 t->SetBranchAddress("betagammaBounds", &bgb);
254 t->GetEntry(0);
255 TH1F* h;
256 stringstream ss;
257 TCanvas c1("c1", "canvas", 800, 600);
258 c1.Print((name + ".ps[").c_str());
259 for (Int_t i = 0; i < total; i++) {
260 if(i%100==0) cout << "i: " << i << " ps is written now" << endl;
261 ss.str("");
262 ss << "h" << i;
263 h = (TH1F*)f.Get(ss.str().c_str());
264 h->Draw();
265 c1.Print((name + ".ps").c_str());
266 }
267 c1.Print((name + ".ps]").c_str());
268}
269
270
272 char hname[200];
273 TH1F *h1 = new TH1F();
274 TTree *tree = new TTree("TH1F_Col", "TH1F_Col");
275 tree->Branch("TH1F_Col", "TH1F", &h1, 3200, 0);
276 TFile* f1 = new TFile("DedxSampleHists.root", "UPDATE");
277 TTree* bin = (TTree*)f1->Get("bin");
278 Int_t totNum;
279 bin->SetBranchAddress("totalNum", &totNum);
280 bin->GetEntry(0);
281
282 for (Int_t i = 0; i < totNum; i++){
283 if(i >= 10000) sprintf(hname, "h%05d", i);
284 if(i >= 1000) sprintf(hname, "h%04d", i);
285 if(i >= 100) sprintf(hname, "h%03d", i);
286 if(i >= 10) sprintf(hname, "h%02d", i);
287 if(i < 10) sprintf(hname, "h%01d", i);
288 h1 = (TH1F*)f1->Get(hname);
289 tree->Fill();
290 }
291 tree->Write();
292 f1->Close();
293}
294
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
TFile * f1
*******INTEGER m_nBinMax INTEGER m_NdiMax !No of bins in histogram for cell exploration division $ !Last vertex $ !Last active cell $ !Last cell in buffer $ !No of sampling when dividing cell $ !No of function total $ !Flag for random ceel for $ !Flag for type of for WtMax $ !Flag which decides whether vertices are included in the sampling $ entire domain is hyp !Maximum effective eevents per bin
Definition: FoamA.h:85
TTree * t
Definition: binning.cxx:23
#define bg_hadron_max
Definition: binning.h:2
#define bg_muon_bin
Definition: binning.h:7
#define bg_electron_max
Definition: binning.h:10
#define bg_muon_min
Definition: binning.h:5
#define bg_electron_min
Definition: binning.h:9
#define bg_hadron_min
Definition: binning.h:1
#define bg_hadron_bin
Definition: binning.h:3
#define bg_muon_max
Definition: binning.h:6
#define bg_electron_bin
Definition: binning.h:11
double vec_bg_hadron[bg_hadron_bin+1]
Definition: binning.h:15
TFile * f_temp
Definition: histgen.cxx:41
void t_to_h(TTree *t, TH1F *h)
Definition: histgen.cxx:110
const double muon_step
Definition: histgen.cxx:28
double find_boundary(int type, int idx, double bg_min, double bg_step)
Definition: histgen.cxx:104
const double electron_step
Definition: histgen.cxx:29
void histgen(string, int, TObjArray &, vector< double > &)
Definition: histgen.cxx:149
const int nc
Definition: histgen.cxx:26
const int na
Definition: histgen.cxx:25
void histps(string)
Definition: histgen.cxx:248
const bool ps_flag(false)
int find_bgidx(int type, double bgg, double bg_min, double bg_step, int nbin)
Definition: histgen.cxx:92
int main()
Definition: histgen.cxx:43
void hist_update()
Definition: histgen.cxx:271
float costheta
float charge
float dEdx_meas
float bg
const float rad
Definition: vector3.h:134