BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtHis2F.cc
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2//
3// Environment:
4//
5// Module: EvtGen/EvtHis2F.hh
6//
7// Description: Class to handle H2F histogram
8//
9// Modification history:
10//
11// PING RG September 8, 2010 Module created
12//
13//------------------------------------------------------------------------
14
15#include "EvtHis2F.hh"
16#include <fstream>
17#include <iostream>
18#include <stdlib.h>
19#include <string>
20
21using namespace std;
22using std::endl;
23
25
26const char* EvtHis2F::getFile() { return datafile; }
27
28const char* EvtHis2F::getHTitle() { return datatitle; }
29
30void EvtHis2F::setFile( const char* dtfile ) {
31 datafile = dtfile;
32 return;
33}
34
35void EvtHis2F::setHTitle( const char* htitle ) {
36 datatitle = htitle;
37 return;
38}
39
40void EvtHis2F::setHmc( TH2F* H2 ) {
41 HMC = (TH2F*)H2->Clone( "HMC" );
42 return;
43}
44
45void EvtHis2F::setHdt( TH2F* H2 ) {
46 HDATA = (TH2F*)H2->Clone( "HDATA" );
47 return;
48}
49
50TH2F* EvtHis2F::getHmc() { return HMC; }
51
52TH2F* EvtHis2F::getHdt() { return HDATA; }
53
54TH2F* EvtHis2F::getHwt() { return HWT; }
55
56void EvtHis2F::show( TH2F* h2 ) {
57 TAxis* Xaxis = h2->GetXaxis();
58 TAxis* Yaxis = h2->GetYaxis();
59
60 int bx = Xaxis->GetLast();
61 int by = Yaxis->GetLast();
62
63 for ( int i = 1; i < bx + 1; i++ )
64 {
65 for ( int j = 1; j < by + 1; j++ )
66 {
67 int ij = h2->GetBin( i, j );
68 double xij = h2->GetBinContent( ij );
69 std::cout << "i,j,cell,value " << i << " " << j << " " << ij << " " << xij << std::endl;
70 }
71 }
72}
73
74void EvtHis2F::showFrame( TH2F* h2 ) {
75 TAxis* Xaxis = h2->GetXaxis();
76 TAxis* Yaxis = h2->GetYaxis();
77
78 int bx = Xaxis->GetLast();
79 int by = Yaxis->GetLast();
80
81 double x1 = Xaxis->GetBinLowEdge( 1 );
82 double y1 = Yaxis->GetBinLowEdge( 1 );
83 double x2 = Xaxis->GetBinUpEdge( bx );
84 double y2 = Yaxis->GetBinUpEdge( by );
85 std::cout << "N_x, x_low, x_up; N_y, y_low, y_up " << bx << " " << x1 << " " << x2 << " "
86 << by << " " << y1 << " " << y2 << std::endl;
87}
88
90 const char* dtfile = getFile(); // get data root file as input
91 const char* htitle = getHTitle(); // get Dalitz plot title
92
93 TFile dataf( dtfile );
94 HDATA = (TH2F*)dataf.Get( htitle );
95
96 double ndata = HDATA->Integral();
97 std::cout << "Number events in HDATA= " << ndata << std::endl;
98
99 TAxis* Xaxis = HDATA->GetXaxis();
100 TAxis* Yaxis = HDATA->GetYaxis();
101
102 BINSx = Xaxis->GetLast();
103 BINSy = Yaxis->GetLast();
104
105 xlow = Xaxis->GetBinLowEdge( 1 );
106 ylow = Yaxis->GetBinLowEdge( 1 );
107 xup = Xaxis->GetBinUpEdge( BINSx );
108 yup = Yaxis->GetBinUpEdge( BINSy );
109
110 std::cout << "BINSx,xlow,xup,BINSy,ylow,yup: " << BINSx << " ," << xlow << ", " << xup
111 << ", " << BINSy << ", " << ylow << ", " << yup << std::endl;
112 HMC = new TH2F( "myHMC", "", BINSx, xlow, xup, BINSy, ylow, yup );
113 HWT = new TH2F( "myHWT", "", BINSx, xlow, xup, BINSy, ylow, yup );
114 HMC->SetDirectory( 0 );
115 HWT->SetDirectory( 0 );
116 HDATA->SetDirectory( 0 );
117 icount = 0;
118 return;
119}
120
121void EvtHis2F::HFill( double m12s, double m13s ) {
122 HMC->Fill( m12s, m13s, 1.0 );
123 icount++;
124 // if(icount==100000)show(HMC);
125 if ( icount == 100000 ) showFrame( HMC );
126 return;
127}
128
129void EvtHis2F::HReweight() { // reweight Dalitz Plot after get HDATA and HMC
130 std::cout << "Now I reweight the MC to the data Dalitz Plot" << std::endl;
131 double ndata = HDATA->Integral();
132 std::cout << "Number events in Dalitz plot = " << ndata << std::endl;
133 double nmc = HMC->Integral();
134 std::cout << "Number of events in HMC reweight= " << nmc << std::endl;
135 HWT->Divide( HDATA, HMC );
136 ndata = HWT->Integral();
137 std::cout << "Number of events after reweight= " << ndata << std::endl;
138 double norm = nmc / ndata;
139 HWT->Scale( norm );
140 nmc = HMC->Integral();
141 std::cout << "Number of events in HMC after scale= " << nmc << std::endl;
142}
143
144void EvtHis2F::setBins( TH2F* h2 ) {
145 TAxis* Xaxis = h2->GetXaxis();
146 TAxis* Yaxis = h2->GetYaxis();
147 BINSx = Xaxis->GetLast();
148 BINSy = Yaxis->GetLast();
149 xlow = Xaxis->GetBinLowEdge( 1 );
150 ylow = Yaxis->GetBinLowEdge( 1 );
151 xup = Xaxis->GetBinUpEdge( BINSx );
152 yup = Yaxis->GetBinUpEdge( BINSy );
153}
154void EvtHis2F::setZmax( TH2F* hwt ) {
155 double ymax = 0;
156 setBins( hwt );
157
158 for ( int dx = 1; dx <= BINSx; dx++ )
159 {
160 for ( int dy = 1; dy <= BINSy; dy++ )
161 {
162 int nxy = hwt->GetBin( dx, dy );
163 double ncell = hwt->GetBinContent( nxy );
164 if ( ncell <= 0 ) continue;
165 if ( ncell > ymax ) { ymax = ncell; }
166 }
167 }
168 zmax = ymax;
169}
170
172 double ymax = 0;
173 setBins( HWT );
174
175 for ( int dx = 1; dx <= BINSx; dx++ )
176 {
177 for ( int dy = 1; dy <= BINSy; dy++ )
178 {
179 int nxy = HWT->GetBin( dx, dy );
180 double ncell = HWT->GetBinContent( nxy );
181 if ( ncell <= 0 ) continue;
182 if ( ncell > ymax ) { ymax = ncell; }
183 }
184 }
185 zmax = ymax;
186}
187
188double EvtHis2F::getZmax() { return zmax; }
189
190double EvtHis2F::getZvalue( double xmass2, double ymass2 ) {
191 int xbin = HWT->GetXaxis()->FindBin( xmass2 );
192 int ybin = HWT->GetYaxis()->FindBin( ymass2 );
193 int xybin = HWT->GetBin( xbin, ybin );
194 double zvalue = HWT->GetBinContent( xybin );
195 // std::cout<<"xmass, ymass, zvalue "<<xmass2<<" "<<ymass2<<" "<<zvalue<<std::endl;
196 return zvalue;
197}
198
199bool EvtHis2F::AR( double xmass2, double ymass2 ) { // accept: true. reject: false
200 // call this function after initiation and filling MC histogram HMC
201 bool accept;
202 accept = false;
203 if ( xmass2 < xlow || xmass2 > xup || ymass2 < ylow || ymass2 > yup ) { return accept; }
204 double zvalue = getZvalue( xmass2, ymass2 );
205 double ratio = zvalue / zmax;
206 double rndm = EvtRandom::Flat( 0.0, 1.0 );
207 // std::cout<<"zvalue, zmax , rndm "<<zvalue<<" "<<zmax<<" "<<rndm<<std::endl;
208 if ( ratio > rndm ) { accept = true; }
209 else { accept = false; }
210 return accept;
211}
212
213bool EvtHis2F::AR( double xmass2, double ymass2, double zmax,
214 TH2F* h2 ) { // accept: true. reject: false
215 // call this function after initiation and filling MC histogram HMC
216 bool accept;
217 accept = false;
218 setBins( h2 );
219 if ( xmass2 < xlow || xmass2 > xup || ymass2 < ylow || ymass2 > yup ) { return accept; }
220 int xbin = h2->GetXaxis()->FindBin( xmass2 );
221 int ybin = h2->GetYaxis()->FindBin( ymass2 );
222 int xybin = h2->GetBin( xbin, ybin );
223 double zvalue = h2->GetBinContent( xybin );
224
225 double ratio = zvalue / zmax;
226 double rndm = EvtRandom::Flat( 0.0, 1.0 );
227 // std::cout<<"zvalue, zmax , rndm "<<zvalue<<" "<<zmax<<" "<<rndm<<std::endl;
228 if ( ratio > rndm ) { accept = true; }
229 else { accept = false; }
230 return accept;
231}
232
233void EvtHis2F::init( TH2F* hmc, TH2F* hdt, TH2F* hwt ) {
234 setHmc( hmc );
235 setHdt( hdt );
236 setHwt( hwt );
237 HReweight();
238 setZmax();
239}
void show(TH2F *h2)
Definition EvtHis2F.cc:56
const char * getHTitle()
Definition EvtHis2F.cc:28
void HReweight()
Definition EvtHis2F.cc:129
void setBins(TH2F *h2)
Definition EvtHis2F.cc:144
void setHwt(TH2F *H2)
Definition EvtHis2F.hh:50
TH2F * getHmc()
Definition EvtHis2F.cc:50
void setHdt(TH2F *H2)
Definition EvtHis2F.cc:45
void init()
Definition EvtHis2F.cc:89
virtual ~EvtHis2F()
Definition EvtHis2F.cc:24
double getZmax()
Definition EvtHis2F.cc:188
const char * getFile()
Definition EvtHis2F.cc:26
void setHmc(TH2F *H2)
Definition EvtHis2F.cc:40
bool AR(double xmass2, double ymass2)
Definition EvtHis2F.cc:199
TH2F * getHwt()
Definition EvtHis2F.cc:54
void HFill(double xmass2, double ymass2)
Definition EvtHis2F.cc:121
void setHTitle(const char *htitle)
Definition EvtHis2F.cc:35
void setZmax()
Definition EvtHis2F.cc:171
double getZvalue(double m12_square, double m13_square)
Definition EvtHis2F.cc:190
TH2F * getHdt()
Definition EvtHis2F.cc:52
void setFile(const char *dtfile)
Definition EvtHis2F.cc:30
void showFrame(TH2F *h2)
Definition EvtHis2F.cc:74
static double Flat()
Definition EvtRandom.cc:69