ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yangyong/Pi0Calibration/Common/fitpeak.cc
Revision: 1.1
Committed: Thu Jul 26 08:48:21 2012 UTC (12 years, 9 months ago) by yangyong
Content type: text/plain
Branch: MAIN
CVS Tags: V00-00-01g, V00-00-01f, V00-00-01e, V00-00-01d, V00-00-01c, V00-00-01b, V00-00-01a, pi0calibrator_v01a, HEAD
Log Message:
first commit

File Contents

# User Rev Content
1 yangyong 1.1 void pi0_mfitpeak(TH1F *mh1, Int_t etapi0flag, float xmin, float xmax, int npol, double res[], string dirName,string histName)
2    
3     {
4    
5     gROOT->Reset();
6     // gStyle->SetOptFit();
7     // gStyle->SetOptFit(0);
8     // gStyle->SetOptStat(0);
9     // gStyle->SetOptTitle(0);
10     Bool_t NOTE=1;
11     if(NOTE) gStyle->SetCanvasBorderMode(0);
12    
13     gStyle->SetPadTopMargin(0.08);
14     gStyle->SetPadBottomMargin(0.12);
15     gStyle->SetPadLeftMargin(0.15);
16     gStyle->SetPadRightMargin(0.08);
17    
18     mh1->GetXaxis()->SetRangeUser(xmin,xmax);
19    
20    
21    
22     // cout<<FileName<<" "<<HistName<<endl;
23    
24     // TFile f(FileName);
25     // TH1F *mh1 = (TH1F*) f.Get(HistName);
26     mh1->SetMarkerStyle(20);
27     mh1->SetMarkerSize(1.);
28     mh1->SetStats(0); // 1/0 to set the stat box
29    
30     mh1->GetXaxis()->SetTitle("Invariant Mass of Photon Pairs [GeV]");
31    
32    
33     float binwidth = mh1->GetBinWidth(1);
34    
35     char *ytitle = new char[100];
36    
37     sprintf(ytitle,"Photon Pairs / %4.3f GeV",binwidth);
38    
39    
40     mh1->GetYaxis()->SetTitle(ytitle);
41    
42    
43     mh1->GetXaxis()->SetTitleSize(0.055);
44     mh1->GetYaxis()->SetTitleSize(0.055);
45     mh1->GetXaxis()->SetLabelSize(0.045);
46     mh1->GetYaxis()->SetLabelSize(0.045);
47     mh1->GetXaxis()->SetTitleOffset(0.90);
48     mh1->GetXaxis()->CenterTitle();
49     mh1->GetYaxis()->SetTitleOffset(1.32);
50    
51    
52     // First work with the histogram and find the peak and fit ranges
53     TAxis *xaxis = mh1->GetXaxis();
54     Float_t binsiz= xaxis->GetBinCenter(3) - xaxis->GetBinCenter(2);
55     Int_t nbins = xaxis->GetNbins();
56     Float_t nevtperbin0[10000];
57     //Float_t errorbin0[10000];
58     Float_t nevttot;
59     Float_t maxbin=0; Int_t nmaxbin=0, nminbord=0, nmaxbord=nbins;
60    
61     double maxbin_val = 0;
62    
63     for (Int_t nn=1; nn <= nbins; nn++)
64     {
65     nevtperbin0[nn] = mh1->GetBinContent(nn);
66     if(nevtperbin0[nn] > maxbin) {
67     maxbin_val = mh1->GetBinCenter(nn);
68     maxbin=nevtperbin0[nn]; nmaxbin=nn; }
69     //errorbin0[nn] = mh1->GetBinError(nn);
70     nevttot+=nevtperbin0[nn];
71     if(nevtperbin0[nn] > 0 && nminbord == 0) nminbord=nn;
72     if(nevtperbin0[nn] == 0 && (nn > nminbord +10) && nmaxbord==0 && nminbord > 0) nmaxbord=nn;
73     }
74     //cout<<"Minbordl "<<nminbord<<" with events: "<<nevtperbin0[nminbord]<<endl;
75     //cout<<"Maxbordl "<<nmaxbord<<" with events: "<<nevtperbin0[nmaxbord]<<endl;
76     nminbord+=0;
77     nmaxbord-=0;
78     // Int_t nmin0=nminbord;
79     while(nevtperbin0[nminbord] < nevtperbin0[nmaxbin]*0.025) nminbord++;
80     while(nevtperbin0[nmaxbord] < nevtperbin0[nmaxbin]*0.025) nmaxbord--;
81     // the above was just to get the info and low/high bins
82    
83     // Set the fit range ! This is for total fit !
84     Float_t fitl=xmin;
85     float fith=xmax;
86     // Float_t fitl=0.07, fith=0.2;// this works better for pileup
87     // Float_t fitl=0.08, fith=0.18;// this works even better for pileup
88     // if(etapi0flag == 1)
89     // {
90     // fitl=0.35; fith=0.75;
91     //}
92    
93    
94     // if(fitl < xaxis->GetBinCenter(nmin0)) fitl = xaxis->GetBinCenter(nmin0);
95     //if(fith > xaxis->GetBinCenter(nmaxbord)) fith = xaxis->GetBinCenter(nmaxbord);
96    
97    
98    
99    
100     //cout<<" fit range "<<fitl<<" -- "<<fith<<endl;
101     //cout <<"Bin size "<<binsiz<<endl;
102     ///cout<<"Total events "<<nevttot<<endl;
103     ///cout<<"MaxBin "<<nmaxbin<<" with events: "<<nevtperbin0[nmaxbin]<<endl;
104     //cout<<"Minbord "<<nminbord<<" with events: "<<nevtperbin0[nminbord]<<endl;
105     //cout<<"Maxbord "<<nmaxbord<<" with events: "<<nevtperbin0[nmaxbord]<<endl;
106     //mh1->DrawCopy("sep");
107     if(etapi0flag ==0){
108     if( maxbin_val < 0.09 || maxbin_val >0.18){
109     maxbin_val = 0.13;
110     }
111     }else if(etapi0flag == 1){
112     if( maxbin_val < 0.45 || maxbin_val >0.65){
113     maxbin_val = 0.54;
114     }
115     }
116    
117    
118    
119    
120     Double_t lowgauss= maxbin_val-4.*0.010;
121     Double_t highgauss= maxbin_val+4.*0.010;
122     if(etapi0flag == 1)
123     {
124     lowgauss=maxbin_val-5.*0.025;
125     highgauss=maxbin_val+5.*0.025;
126     }
127    
128    
129     Int_t nlowgauss=Int_t((lowgauss-xaxis->GetBinCenter(1))/Float_t(binsiz)+0.5);
130     Int_t nhighgauss=Int_t((highgauss-xaxis->GetBinCenter(1))/Float_t(binsiz)+0.5);
131     //cout <<xaxis->GetBinCenter(nlowgauss)<<" "<<xaxis->GetBinCenter(nhighgauss)<<endl;
132     // now make the "background" histogram and fit it with p4
133     Float_t lowvalgauss=nevtperbin0[nlowgauss];
134     Float_t increm=(nevtperbin0[nhighgauss]-nevtperbin0[nlowgauss])/Float_t(nhighgauss-nlowgauss);
135     TH1F *hbkg = (TH1F*)mh1->Clone();
136     // hbkg->SetName("bkg_clone");
137     for (Int_t nn=nlowgauss; nn<=nhighgauss; nn++)
138     {
139     hbkg->SetBinContent(nn,Float_t(lowvalgauss+(nn-nlowgauss)*increm));
140     hbkg->SetBinError(nn,sqrt(lowvalgauss+(nn-nlowgauss)*increm));
141     }
142     //hbkg->DrawCopy("samesep");
143     // break;
144     // Now define the "gaussian" histogram
145     TH1F *hgauss = (TH1F*)mh1->Clone();
146     hgauss->SetName("gauss_clone");
147     hgauss->Sumw2();
148     hgauss->Add(mh1,hbkg,1,-1); // if errors are independent Add needs to be used !
149     for (Int_t nn=1; nn <= nbins; nn++)
150     {
151     if(hgauss->GetBinContent(nn) < 0.) hgauss->SetBinContent(nn,0.001*nevtperbin0[nmaxbin]);
152     hgauss->SetBinError(nn,sqrt(hgauss->GetBinContent(nn)));
153     }
154    
155     // Declare function with wich to fit
156     TF1 *g1 = new TF1("g1","gaus",lowgauss,highgauss);
157    
158     hgauss->Fit(g1,"R0q");
159    
160     //hgauss->DrawCopy("sep");
161     //g1->Draw("same");
162    
163    
164     char *polff = new char[20];
165    
166     sprintf(polff,"pol%d",npol);
167    
168     TF1 *p4bkg;
169     if(etapi0flag != 1)
170     p4bkg = new TF1("pm2",polff, xaxis->GetBinCenter(nminbord),xaxis->GetBinCenter(nmaxbord));
171     else
172     p4bkg = new TF1("pm2",polff, 0.35,0.75);
173    
174    
175    
176     hbkg->Fit(p4bkg,"R0q");
177     //hbkg->DrawCopy("sep");
178    
179     //p4bkg->Draw("same");
180    
181    
182     Double_t par[20],parf[20],errparf[20];
183     g1->GetParameters(&par[0]);
184     p4bkg->GetParameters(&par[3]);
185    
186     char *totff = new char[20];
187    
188     sprintf(totff,"gaus(0)+pol%d(3)",npol);
189    
190    
191     TF1 *total = new TF1("total",totff,fitl,fith);
192     TF1 *p4bkgfin = new TF1("pm2",polff,fitl,fith);
193    
194    
195     if(etapi0flag==0){
196    
197     double allintegral = mh1->Integral(int(0.08/binsiz),int(0.18/binsiz));
198     total->SetParLimits(0,1,allintegral);
199    
200     total->SetParLimits(1,0.08,0.18);
201     total->SetParLimits(2,0.135*0.03,0.135*0.3);
202    
203     if( par[0] >= allintegral) par[0] = allintegral-1;
204     if( par[1] <0.08 || par[1] >0.18) par[1] = 0.135;
205     if( par[2] < 0.135*0.03 || par[2] > 0.135*0.3) par[2] = 0.01;
206    
207    
208     }else{
209    
210    
211     double allintegral = mh1->Integral(int(0.4/binsiz),int(0.65/binsiz));
212     total->SetParLimits(0,1,allintegral);
213     total->SetParLimits(1,0.4,0.65);
214     total->SetParLimits(2,0.55*0.02,0.55*0.3);
215    
216     if( par[0] >= allintegral) par[0] = allintegral-1;
217     if( par[1] <0.4 || par[1] >0.65) par[1] = 0.55;
218     if( par[2] < 0.55*0.02 || par[2] > 0.55*0.3) par[2] = 0.55*0.04;
219    
220    
221     //total->SetParLimits(1,0.4,0.6);
222     // total->SetParLimits(2,0.55*0.02,0.55*0.2);
223     // total->SetParLimits(2,0.50*0.09,0.50*0.11);
224    
225    
226     }
227    
228    
229     // total->FixParameter(1,1.21340e-01);
230     // total->FixParameter(2,2.69780e-02);
231    
232    
233     total->SetParameters(par);
234    
235    
236    
237     mh1->Fit(total,"R0q");
238    
239    
240    
241    
242     //cout<<" yield.. "<< total->GetParameter(0) <<"+/- " << total->GetParError(0)<<endl;
243    
244    
245     total->GetParameters(parf);
246    
247    
248     for( Int_t nn=0; nn < 3+npol+1; nn++) errparf[nn]=total->GetParError(nn);
249     g1->SetParameters(&parf[0]);
250     p4bkgfin->SetParameters(&parf[3]);
251    
252     Float_t int_min=parf[1]-2.*parf[2];
253     Float_t int_max=parf[1]+2.*parf[2];
254     Float_t sig_peak=g1->Integral(int_min,int_max)/binsiz;
255     Float_t bkgd_peak=p4bkgfin->Integral(int_min,int_max)/binsiz;
256     Float_t SB=sig_peak/bkgd_peak; Float_t SBerr=SB*(sqrt(1./sig_peak+1./bkgd_peak));
257    
258     int_min=parf[1]-5.*parf[2];
259     int_max=parf[1]+5.*parf[2];
260     float S_all = g1->Integral(int_min,int_max)/binsiz;
261     float Serr_all = errparf[0]/ parf[0] * S_all;
262    
263     res[0] = parf[1];
264     res[1] = errparf[1];
265    
266     res[2] = S_all;
267     res[3] = Serr_all;
268    
269     res[4] = parf[2];
270     res[5] = errparf[2];
271    
272     res[6] = SB;
273     res[7] = SBerr;
274    
275    
276    
277     if( dirName != ""){
278    
279     TCanvas *c2 = new TCanvas("c2", "",448,184,625,583);
280    
281     total->SetLineWidth(3);
282    
283     total->SetLineColor(kBlue);
284    
285     p4bkgfin->SetLineWidth(3);
286    
287     p4bkgfin->SetLineColor(kRed);
288     p4bkgfin->SetLineStyle(2);
289    
290     mh1->DrawCopy("sep");
291    
292     // total->SetRange(0.07,0.185);
293     // p4bkgfin->SetRange(0.07,0.185);
294     total->Draw("same");
295     p4bkgfin->Draw("same");
296    
297     TLatex l;
298     l.SetTextSize(0.05);
299     l.SetTextColor(1);
300     l.SetNDC();
301    
302     float sigma = parf[2]/ parf[1]*100;
303     float sigmaerr = errparf[2]/ parf[1]*100;
304     char *sigma_name = new char[50];
305     sprintf(sigma_name,"#sigma = %3.2f #pm %3.2f %% ",sigma,sigmaerr);
306    
307     l.DrawLatex(0.43,0.67,sigma_name);
308    
309     // sprintf(sigma_name,"S/B = %3.2f #pm %3.2f ",SB,SBerr);
310     //
311     sprintf(sigma_name,"N_{sig} = %d #pm %d ",int(S_all),int(Serr_all));
312     //
313    
314     l.DrawLatex(0.43,0.54,sigma_name);
315    
316     sprintf(sigma_name,"M = %3.2f #pm %3.2f MeV",parf[1]*1000.,errparf[1]*1000);
317     l.DrawLatex(0.42,0.73,sigma_name);
318    
319     sprintf(sigma_name,"S/B_{#pm2#sigma} = %3.2f #pm %3.2f ",SB,SBerr);
320     l.DrawLatex(0.383,0.47,sigma_name);
321    
322     //l.DrawLatex(0.169,470.,"d)");
323    
324    
325     c2->Modified();
326    
327     c2->Update();
328     char *filename = new char[1000];
329     sprintf(filename,"%s/%s.gif",dirName.c_str(),histName.c_str());
330     c2->SaveAs(filename);
331    
332     cout<<"res[0]: "<< res[0] <<" "<< res[2] <<" "<< res[6] <<" "<<res[1] <<endl;
333    
334     }
335    
336    
337     p4bkg->Delete();
338     g1->Delete();
339     hbkg->Delete();
340     hgauss->Delete();
341    
342     total->Delete();
343     p4bkgfin->Delete();
344    
345    
346     }