BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
tau_mode.c File Reference
#include <stdlib.h>
#include <TSQLServer.h>
#include <TSQLResult.h>
#include <TSQLRow.h>

Go to the source code of this file.

Functions

Double_t BeamEnergyOnline (Int_t)
 
void tau_mode (int flag=0)
 

Function Documentation

◆ BeamEnergyOnline()

Double_t BeamEnergyOnline ( Int_t  runno)

Definition at line 273 of file tau_mode.c.

274{
275 Double_t dbe = 0; // initialization
276
277 // connect to the BESIII database
278 TSQLServer *db = TSQLServer::Connect("mysql://bes3db2.ihep.ac.cn", "guest", "guestpass");
279 // printf("Server info: %s\n", db->ServerInfo());
280
281 //Int_t runno = 33743;
282 //read beam energy from the online database 'run'
283 const char *ins = "select BER_PRB from run.RunParams where run_number = %d";
284 char sql[4096];
285 sprintf(sql, ins, runno);
286 TSQLResult *res = db->Query(sql);
287 if (res->GetRowCount() == 0) { // not found
288 printf("Cannot find the beam energy of run %d from the database.\n", runno);
289 delete res;
290 delete db;
291 return dbe;
292 }
293 TSQLRow *row = res->Next();
294 TString sbe = row->GetField(0); // beam energy of electron
295 dbe = atof(sbe); // convert string to float
296 // printf("beam energy is : %5.3f\n", dbe);
297 delete row;
298 delete res;
299 delete db;
300
301 return dbe;
302}
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)

Referenced by tau_mode().

◆ tau_mode()

void tau_mode ( int  flag = 0)

Definition at line 16 of file tau_mode.c.

17{
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 =event;
24
25 if (flag!=0)
26 {
27 //Define the canvas
28 TCanvas *myCanvas = new TCanvas();
29 myCanvas->Divide(1,1);
30 TPad *c1_1 = new TPad("c1_1", "c1_1",0.01,0.01,0.99,0.99);
31 c1_1->Draw();
32 c1_1->cd();
33 c1_1->Range(0.425458,-114.842,0.674993,802.951);
34 c1_1->SetBorderSize(2);
35 c1_1->SetBottomMargin(0.125129);
36 c1_1->SetFrameFillColor(0);
37 }
38
39 //Initialize the variables and set the branches' address
40 Bool_t topup = false, is_topup = false;
41 Int_t run = 0;
42 Double_t time = 0.0, etsT1 = 0.0, dltphi = 0.0,costht1 = 0.0,costht2 = 0.0,e1 = 0.0,e2 = 0.0,etot = 0.0,phi1 = 0.0,phi2 = 0.0;
43 Int_t nentries = 0;
44
45 data->SetBranchAddress("topup", &topup);
46 data->SetBranchAddress("run",&run);
47 data->SetBranchAddress("time",&time);
48 data->SetBranchAddress("etsT1",&etsT1);
49 data->SetBranchAddress("dltphi",&dltphi);
50 data->SetBranchAddress("costht1",&costht1);
51 data->SetBranchAddress("costht2",&costht2);
52 data->SetBranchAddress("e1",&e1);
53 data->SetBranchAddress("e2",&e2);
54 data->SetBranchAddress("etot",&etot);
55 data->SetBranchAddress("phi1",&phi1);
56 data->SetBranchAddress("phi2",&phi2);
57
58 nentries = (Int_t)data->GetEntries();
59
60 Int_t runno = 0;
61 Double_t timemin = 0.0,timemax = 0.0;
62 Int_t init_time = 1234567890; // corresponding to 2009-2-14 7:31:30
63 Int_t n_first = 0; // the number of the good first entry
64 do {
65 data->GetEntry(n_first);
66 // timemin = time;
67 n_first += 1;
68 } while ( time < init_time && n_first < nentries) ;
69 runno = run;
70 Double_t E_cms = 2*BeamEnergyOnline(run);
71 cout << "E_cms = " << E_cms << endl;
72 is_topup = topup;
73 timemin = time;
74 // get the total time of the run
75 Int_t tot_time = data->GetMaximum("etsT1");
76 Double_t difft;
77 if ( tot_time > 0 )
78 {
79 difft = tot_time;
80 } else { // etsT1 can not be used, use another method
81 Int_t n_last = nentries -1; // last event in the ntuple
82 // Deal with bad events with wrong time value.
83 // Check the contents of the event can judge whether it is a good event
84 do {
85 data->GetEntry(n_last);
86 n_last -= 1;
87 } while ( etot <= 0 && n_last >= n_first );
88 timemax = time;
89 if ( timemin < init_time || timemax < init_time ) { // protection
90 cout << "\n the time of the event is wrong!";
91 cout << "time range : " << timemin << " - " << timemax << endl;
92 return;
93 }
95 }
96
97 Double_t dlt = 0.0,mean = 0.0,width = 0.0;
98 Double_t luminitial = 0.;
99 Double_t tau = 5000.;
100
101 if (difft<=0.||difft>108000) // protection. 30h. The time of a run shold < 10 hours
102 {
103 cout << "\nThe runtime is abnormal!!!" << endl;
104 cout << "It's value is " << difft << endl;
105 cout << "Please check it carefully!" << endl;
106 return;
107 }
108
109 if (difft<300.) { // the run is too short
110 cout << "\nRuntime less than 300 " << endl;
111 cout << "Using default tau value " << tau << endl;
112 }
113 else if (is_topup) { // topup mode
114 cout << "\n topup mode." << endl;
115 luminitial = 2000;
116 tau = 99999999;
117 }
118 else
119 {
120 //Fill the histogram of dltphi distribution
121 TH1F *dltphi1 = new TH1F("dltphi1","dltphi",150,-30,30);
122 for (Int_t i = 0;i<nentries;i++)
123 {
124 data->GetEntry(i);
125 bool cut = (e1/E_cms)>0.417&&(e1/E_cms)<0.491&&(e2/E_cms)>0.417&&(e2/E_cms)<0.491
126 &&fabs(costht1)<0.8&&fabs(costht2)<0.8
127 &&etot<E_cms+0.5;
128 if (cut)
129 {
130 dltphi1->Fill(dltphi);
131 }
132 }
133
134 //Fit the Di-photon peak and get the value of dlt
135 TF1* h1=new TF1("h1","gaus",-6,4);
136 dltphi1->Fit(h1,"R+");
137 dlt = h1->GetParameter(1);
138
139 //Fit the Bhabha peak and get the values of mean and width
140 TF1* h2=new TF1("h2","gaus",4,30);
141 dltphi1->Fit(h2,"R+");
142 mean = h2->GetParameter(1);
143 Double_t sigma = h2->GetParameter(2);
144 width = 3*sigma;
145
146 if (dlt<-6||dlt>4||mean<4||mean>30||(mean-dlt)-0.5*width<0)
147 cout << "\nSomething wrong with cut Bhabha entries. " << endl;
148 else
149 {
150 //Get 11 time nodes
151 Double_t xtime[11],lum[10];
152 for(Int_t i=0;i<11;i++)
153 {
154 xtime[i]=timemin+(difft/10.)*i;
155 }
156
157
158 //Get 10 histograms of dltphi distribution between time nodes
159 TH1D *hdltphi[10];
160 for(Int_t i=0;i<10;i++)
161 {
162 hdltphi[i] = new TH1D("","dltphi distribution",50,-50.,50.);
163 }
164 TH1 *htime = new TH1D("htime","time", 80, -10., 10.);
165
166 for(Int_t i=0;i<10;i++)
167 {
168 for(Int_t j=0;j<nentries;j++)
169 {
170 data->GetEntry(j);
171 bool lumcut = (e1/E_cms)>0.417&&(e2/E_cms)>0.417&&fabs(costht1)<0.8&&
172 fabs(costht2)<0.8&&fabs(dltphi-dlt)>(mean-dlt)-width&&
173 fabs(dltphi-dlt)<(mean-dlt)+width&&etot<E_cms+0.5;
174 if(lumcut&&time>xtime[i]&&time<xtime[i+1])
175 {
176 hdltphi[i]->Fill(dltphi);
177 }
178 }
179 }
180
181 //Get the average luminosity between time nodes
182 Int_t nevt[10];
183 Double_t x[10];
184 for(Int_t i=0;i<10;i++)
185 {
186 nevt[i] = hdltphi[i]->GetEntries();
187 x[i]=xtime[i+1]-xtime[0];
188 lum[i]=nevt[i];
189 }
190
191 //Set draw options and fit the luminosity curve
192 TCanvas *c2 = new TCanvas("c2"," ",700,500);
193 c2->SetFillColor(kWhite);
194 c2->SetGrid();
195
196 Int_t imin;
197 for(Int_t i=0;lum[i]<lum[i+1];i++)
198 {
199 imin = i+1;
200 }
201 Double_t lummax = lum[imin];
202 Double_t xmin = x[imin];
203 Double_t lumsum;
204 for(Int_t i=imin;i<10;i++)
205 {
206 lumsum += lum[i];
207 }
208 Double_t lumsum2 = lumsum*lumsum;
209
210 const Int_t n = 10;
211 TF1 *g1 = new TF1("g1","[0]*exp(-x/[1])",xmin,10000.);
212 g1->SetParameters(lummax,1e+4.);
213 g1->SetLineColor(2);
214 TGraph* gr = new TGraph(n,x,lum);
215 gr->SetLineColor(2);
216 gr->SetLineWidth(2);
217 gr->SetMarkerColor(4);
218 gr->SetMarkerStyle(21);
219 gr->SetTitle("BbLum");
220 gr->GetXaxis()->SetTitle("Time");
221 gr->GetYaxis()->SetTitle("Luminosity");
222 gr->Fit("g1","R");
223 gr->Draw("ap");
224
225 //pick the fit result
226 luminitial = g1->GetParameter(0);
227 tau = g1->GetParameter(1);
228 }
229 }
230
231
232 if (!is_topup && tau>99999.)
233 {
234 cout << "\nThe result of fit is terrible. " << endl << endl;
235 cout << "set the tau value = 99999" << endl;
236 tau = 99999;
237 }
238
239 //Export the fit result
240 std::ofstream m_outputFile;
241 m_outputFile.open("YYYY/m_txt_dir/LumTau_XXXX.txt", ios_base::app);
242 m_outputFile<< runno <<" "<<difft<<" "<<luminitial<<" "<<(luminitial * exp(- 1.0 * difft / (tau)))<<" "<< tau <<endl;
243 m_outputFile.close();
244
245 if (flag!=0)
246 {
247 //Get the value of tau and print it
248 TString result = Form("tau = %6.2f",tau);
249 TText* text01 = new TText(0.7,0.8,result);
250 text01->SetNDC();
251 text01->Draw("same");
252 c2->Update();
253 c2->GetFrame()->SetFillColor(kWhite);
254 c2->GetFrame()->SetBorderSize(1);
255 c2->Modified();
256 c2->Print("lum_0000XXXX.ps");
257 }
258
259 if (flag!=0)
260 {
261 //Print some parameters
262 cout << "E_cms = " << E_cms << endl;
263 cout << "\nruntime = " << difft << endl;
264 cout << "\ndlt = " << dlt << endl;
265 cout << "mean = " << mean << endl;
266 cout << "width = " << width << endl;
267 cout << "\ntau = " << tau << endl << endl;
268 }
269}
TTree * sigma
Double_t timemax
const Int_t n
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 x[10]
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)
Double_t BeamEnergyOnline(Int_t)
Definition: tau_mode.c:273