BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
tau_mode.c
Go to the documentation of this file.
1#include <stdlib.h>
2#ifndef __CINT__
3# include <TSQLResult.h>
4# include <TSQLRow.h>
5# include <TSQLServer.h>
6#endif
7
8//
9// get the online beam energy from the database for a specific run
10// within ROOT
11// <jixb@ihep.ac.cn>
12//
13
14const Int_t MaxRunTime = 108000;
15Double_t BeamEnergyOnline( Int_t );
16
17void tau_mode( int flag = 0 ) {
18 // Reset root
19 gROOT->Reset();
20
21 // Get the root file and E_cms
22 TFile* f1 = new TFile( "YYYY/m_root_dir/LumTau_XXXX.root" );
23 TTree* data;
24 f1->GetObject( "event", data );
25
26 if ( flag != 0 )
27 {
28 // Define the canvas
29 TCanvas* myCanvas = new TCanvas();
30 myCanvas->Divide( 1, 1 );
31 TPad* c1_1 = new TPad( "c1_1", "c1_1", 0.01, 0.01, 0.99, 0.99 );
32 c1_1->Draw();
33 c1_1->cd();
34 c1_1->Range( 0.425458, -114.842, 0.674993, 802.951 );
35 c1_1->SetBorderSize( 2 );
36 c1_1->SetBottomMargin( 0.125129 );
37 c1_1->SetFrameFillColor( 0 );
38 }
39
40 // Initialize the variables and set the branches' address
41 Bool_t topup = false, is_topup = false;
42 Int_t run = 0;
43 Double_t time = 0.0, etsT1 = 0.0, dltphi = 0.0, costht1 = 0.0, costht2 = 0.0, e1 = 0.0,
44 e2 = 0.0, etot = 0.0, phi1 = 0.0, phi2 = 0.0;
45 Int_t nentries = 0;
46
47 data->SetBranchAddress( "topup", &topup );
48 data->SetBranchAddress( "run", &run );
49 data->SetBranchAddress( "time", &time );
50 data->SetBranchAddress( "etsT1", &etsT1 );
51 data->SetBranchAddress( "dltphi", &dltphi );
52 data->SetBranchAddress( "costht1", &costht1 );
53 data->SetBranchAddress( "costht2", &costht2 );
54 data->SetBranchAddress( "e1", &e1 );
55 data->SetBranchAddress( "e2", &e2 );
56 data->SetBranchAddress( "etot", &etot );
57 data->SetBranchAddress( "phi1", &phi1 );
58 data->SetBranchAddress( "phi2", &phi2 );
59
60 nentries = (Int_t)data->GetEntries();
61
62 Int_t runno = 0;
63 Double_t timemin = 0.0, timemax = 0.0;
64 Int_t init_time = 1234567890; // corresponding to 2009-2-14 7:31:30
65 Int_t n_first = 0; // the number of the good first entry
66 do {
67 data->GetEntry( n_first );
68 // timemin = time;
69 n_first += 1;
70 } while ( time < init_time && n_first < nentries );
71 runno = run;
72 Double_t E_cms = 2 * BeamEnergyOnline( run );
73 cout << "E_cms = " << E_cms << endl;
74 is_topup = topup;
75 timemin = time;
76 // get the total time of the run
77 // etsT1 has some problem before round 13 data, so do not use it
78 Int_t tot_time = 0;
79 if ( run >= 63075 )
80 tot_time = data->GetMaximum( "etsT1" ); // 63075 is the 1st run of round 13 data
81 Double_t difft;
82 if ( tot_time > 0 && tot_time < MaxRunTime ) { difft = tot_time; }
83 else
84 { // etsT1 can not be used, use another method
85 Int_t n_last = nentries - 1; // last event in the ntuple
86 // Deal with bad events with wrong time value.
87 // Check the contents of the event can judge whether it is a good event
88 do {
89 data->GetEntry( n_last );
90 n_last -= 1;
91 } while ( etot <= 0 && n_last >= n_first );
92 timemax = time;
93 if ( timemin < init_time || timemax < init_time )
94 { // protection
95 cout << "\n the time of the event is wrong!";
96 cout << "time range : " << timemin << " - " << timemax << endl;
97 return;
98 }
100 }
101
102 Double_t dlt = 0.0, mean = 0.0, width = 0.0;
103 Double_t luminitial = 0.;
104 Double_t tau = 5000.;
105
106 if ( difft <= 0. ||
107 difft > MaxRunTime ) // protection. 30h. The time of a run shold < 10 hours
108 {
109 cout << "\nThe runtime is abnormal!!!" << endl;
110 cout << "It's value is " << difft << endl;
111 cout << "Please check it carefully!" << endl;
112 return;
113 }
114
115 // Set draw options and fit the luminosity curve
116 TCanvas* c2 = new TCanvas( "c2", " ", 700, 500 );
117 c2->SetFillColor( kWhite );
118 c2->SetGrid();
119
120 if ( difft < 300. )
121 { // the run is too short
122 cout << "\nRuntime less than 300 " << endl;
123 cout << "Using default tau value " << tau << endl;
124 }
125 else if ( is_topup )
126 { // topup mode
127 cout << "\n topup mode." << endl;
128 luminitial = 2000;
129 tau = 99999999;
130 }
131 else
132 {
133 // Fill the histogram of dltphi distribution
134 TH1F* dltphi1 = new TH1F( "dltphi1", "dltphi", 150, -30, 30 );
135 for ( Int_t i = 0; i < nentries; i++ )
136 {
137 data->GetEntry( i );
138 bool cut = ( e1 / E_cms ) > 0.417 && ( e1 / E_cms ) < 0.55 && ( e2 / E_cms ) > 0.417 &&
139 ( e2 / E_cms ) < 0.55 && fabs( costht1 ) < 0.8 && fabs( costht2 ) < 0.8 &&
140 etot < E_cms + 0.5;
141 if ( cut ) { dltphi1->Fill( dltphi ); }
142 }
143
144 // Fit the Di-photon peak and get the value of dlt
145 TF1* h1 = new TF1( "h1", "gaus", -6, 4 );
146 dltphi1->Fit( h1, "R+" );
147 dlt = h1->GetParameter( 1 );
148
149 // Fit the Bhabha peak and get the values of mean and width
150 TF1* h2 = new TF1( "h2", "gaus", 4, 30 );
151 dltphi1->Fit( h2, "R+" );
152 mean = h2->GetParameter( 1 );
153 Double_t sigma = h2->GetParameter( 2 );
154 width = 3 * sigma;
155
156 const Int_t nbin = 10; // number of bin
157 if ( dlt < -6 || dlt > 4 || mean < 4 || mean > 30 || ( mean - dlt ) - 0.5 * width < 0 )
158 cout << "\nSomething wrong with cut Bhabha entries. " << endl;
159 else
160 {
161 // Get 11 time nodes
162 Double_t xtime[nbin + 1], lum[nbin];
163 for ( Int_t i = 0; i < nbin + 1; i++ ) { xtime[i] = timemin + ( difft / 10. ) * i; }
164
165 // Get 10 histograms of dltphi distribution between time nodes
166 TH1D* hdltphi[nbin];
167 for ( Int_t i = 0; i < nbin; i++ )
168 { hdltphi[i] = new TH1D( "", "dltphi distribution", 50, -50., 50. ); }
169 TH1D* htime = new TH1D( "htime", "time", 80, -10., 10. );
170
171 for ( Int_t i = 0; i < nbin; i++ )
172 {
173 for ( Int_t j = 0; j < nentries; j++ )
174 {
175 data->GetEntry( j );
176 bool lumcut = ( e1 / E_cms ) > 0.417 && ( e2 / E_cms ) > 0.417 &&
177 fabs( costht1 ) < 0.8 && fabs( costht2 ) < 0.8 &&
178 fabs( dltphi - dlt ) > ( mean - dlt ) - width &&
179 fabs( dltphi - dlt ) < ( mean - dlt ) + width && etot < E_cms + 0.5;
180 if ( lumcut && time > xtime[i] && time < xtime[i + 1] )
181 { hdltphi[i]->Fill( dltphi ); }
182 }
183 }
184
185 // Get the average luminosity between time nodes
186 Int_t nevt[nbin];
187 Double_t x[nbin];
188 for ( Int_t i = 0; i < nbin; i++ )
189 {
190 nevt[i] = hdltphi[i]->GetEntries();
191 x[i] = xtime[i + 1] - xtime[0];
192 lum[i] = nevt[i];
193 }
194
195 Int_t imin = 0;
196 for ( Int_t i = 0; i < nbin - 1 && lum[i] < lum[i + 1]; i++ ) { imin = i + 1; }
197 Double_t lummax = lum[imin];
198 Double_t xmin = x[imin];
199 Double_t lumsum;
200 for ( Int_t i = imin; i < nbin; i++ ) { lumsum += lum[i]; }
201 Double_t lumsum2 = lumsum * lumsum;
202
203 TF1* g1 = new TF1( "g1", "[0]*exp(-x/[1])", xmin, 10000. );
204 g1->SetParameters( lummax, 1.e+4 );
205 g1->SetLineColor( 2 );
206 TGraph* gr = new TGraph( nbin, x, lum );
207 gr->SetLineColor( 2 );
208 gr->SetLineWidth( 2 );
209 gr->SetMarkerColor( 4 );
210 gr->SetMarkerStyle( 21 );
211 gr->SetTitle( "BbLum" );
212 gr->GetXaxis()->SetTitle( "Time" );
213 gr->GetYaxis()->SetTitle( "Luminosity" );
214 gr->Fit( "g1", "R" );
215 gr->Draw( "ap" );
216
217 // pick the fit result
218 luminitial = g1->GetParameter( 0 );
219 tau = g1->GetParameter( 1 );
220 }
221 }
222
223 if ( !is_topup && tau > 99999. )
224 {
225 cout << "\nThe result of fit is terrible. " << endl << endl;
226 cout << "set the tau value = 99999" << endl;
227 tau = 99999;
228 }
229
230 // Export the fit result
231 std::ofstream m_outputFile;
232 m_outputFile.open( "YYYY/m_txt_dir/LumTau_XXXX.txt", ios_base::app );
233 m_outputFile << runno << " " << difft << " " << luminitial
234 << " " << ( luminitial * exp( -1.0 * difft / ( tau ) ) )
235 << " " << tau << endl;
236 m_outputFile.close();
237
238 if ( flag != 0 )
239 {
240 // Get the value of tau and print it
241 TString result = Form( "tau = %6.2f", tau );
242 TText* text01 = new TText( 0.7, 0.8, result );
243 text01->SetNDC();
244 text01->Draw( "same" );
245 c2->Update();
246 c2->GetFrame()->SetFillColor( kWhite );
247 c2->GetFrame()->SetBorderSize( 1 );
248 c2->Modified();
249 c2->Print( "lum_0000XXXX.ps" );
250 }
251
252 if ( flag != 0 )
253 {
254 // Print some parameters
255 cout << "E_cms = " << E_cms << endl;
256 cout << "\nruntime = " << difft << endl;
257 cout << "\ndlt = " << dlt << endl;
258 cout << "mean = " << mean << endl;
259 cout << "width = " << width << endl;
260 cout << "\ntau = " << tau << endl << endl;
261 }
262}
263
264Double_t BeamEnergyOnline( Int_t runno ) {
265 Double_t dbe = 0; // initialization
266
267 // connect to the BESIII database
268 TSQLServer* db = TSQLServer::Connect( "mysql://bes3db2.ihep.ac.cn", "guest", "guestpass" );
269 // printf("Server info: %s\n", db->ServerInfo());
270
271 // Int_t runno = 33743;
272 // read beam energy from the online database 'run'
273 const char* ins = "select BER_PRB from run.RunParams where run_number = %d";
274 char sql[4096];
275 sprintf( sql, ins, runno );
276 TSQLResult* res = db->Query( sql );
277 if ( res->GetRowCount() == 0 )
278 { // not found
279 printf( "Cannot find the beam energy of run %d from the database.\n", runno );
280 delete res;
281 delete db;
282 return dbe;
283 }
284 TSQLRow* row = res->Next();
285 TString sbe = row->GetField( 0 ); // beam energy of electron
286 dbe = atof( sbe ); // convert string to float
287 // printf("beam energy is : %5.3f\n", dbe);
288 delete row;
289 delete res;
290 delete db;
291
292 return dbe;
293}
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)
Double_t timemax
std::ofstream m_outputFile
Double_t phi2
TTree * data
Double_t lum[10]
TFile * f1
Double_t costht2
Double_t etot
TH1D * hdltphi[10]
TF1 * g1
Double_t dltphi
Double_t phi1
Double_t timemin
TH1 * htime
Double_t costht1
Int_t nevt[10]
TGraph * gr
Double_t time
Double_t e1
Double_t xtime[11]
Int_t nentries
Double_t difft
Double_t e2
EvtComplex exp(const EvtComplex &c)
*********Class see also m_nmax DOUBLE PRECISION m_MasPhot DOUBLE PRECISION m_phsu DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_r2 DOUBLE PRECISION m_WtMass INTEGER m_nmax INTEGER m_Nevgen INTEGER m_IsFSR INTEGER m_MarTot *COMMON c_KarFin $ !Output file $ !Event serial number $ !alpha QED at Thomson limit $ !minimum energy at CMS for remooval $ !infrared cut
Definition KarFin.h:27
Double_t BeamEnergyOnline(Int_t)
Definition tau_mode.c:264
const Int_t MaxRunTime
Definition tau_mode.c:14
void tau_mode(int flag=0)
Definition tau_mode.c:17