FairRoot/PandaRoot
fittest.C
Go to the documentation of this file.
1 #include "TF1.h"
2 #include "TH1D.h"
3 #include "TH2D.h"
4 #include "TMath.h"
5 #include "TRandom3.h"
6 #include "TROOT.h"
7 #include "TGraphErrors.h"
8 #include "TFile.h"
9 #include <iostream>
10 
13 
14 TRandom3 *myran;
15 
16 TF1 *func=NULL;
17 TF1 *fResponse=NULL;
18 TF1 *fSignal=NULL;
19 TF1 *fBW=NULL;
20 
22 {
23  Double_t f1=TMath::Exp((-(par[0]-x[0])*(par[0]-x[0]))/(2.*par[1]*par[1]));
24  Double_t f2=(x[0]-par[2])*(x[0]-par[2])+par[3]*par[3]/4.;
25  return f1/f2;
26 }
27 
28 /*
29  par[0]=scale
30  par[1]=background strength
31  par[2]=signal strength
32  par[3]=beam resolution
33  par[4]=mass of resonance
34  par[5]=width of resonance
35  */
36 
38 {
39  if (!func) func=new TF1("func",nastyFunction,xmin,xmax,4);
40  func->SetNpx(1000);
41  Double_t xx =x[0];
42  func->SetParameter(0,xx);
43  func->SetParameter(1,par[3]);
44  func->SetParameter(2,par[4]);
45  func->SetParameter(3,par[5]);
46  Double_t f1 = func->Integral(xx-10*par[3],xx+10*par[3]);
47  Double_t f2 = par[1]+f1*par[2]*par[5]*par[5]/(4.*TMath::Sqrt(2*TMath::Pi())*par[3]);
48  return par[0]*f2;
49 }
50 
51 
53 {
54  if (!func) func=new TF1("func",nastyFunction,xmin,xmax,4);
55  func->SetNpx(1000);
56  Double_t xx =x[0];
57  func->SetParameter(0,xx);
58  func->SetParameter(1,par[3]);
59  func->SetParameter(2,par[4]);
60  func->SetParameter(3,par[5]);
61  Double_t f1 = func->Integral(xx-10*par[3],xx+10*par[3]);
62  Double_t f2 = f1*par[2]*par[5]*par[5]/(4.*TMath::Sqrt(2*TMath::Pi())*par[3]);
63  return par[0]*f2;
64 }
65 
67 {
68  Double_t f1=par[0]*par[2]*par[2];
69  Double_t f2=(x[0]-par[1])*(x[0]-par[1])+par[2]*par[2]/4.;
70  return f1/f2;
71 }
72 
73 void setShape(Double_t stat,Double_t peaktobackground, Double_t beamresolution,Double_t resonancewidth, Double_t resonancemass)
74 {
75  if (NULL==fResponse)
76  {
77  fResponse=new TF1("fResponse",pandaResponse,xmin,xmax,6);
78  fResponse->SetNpx(1000);
79  }
80 
81  fResponse->SetParameter(0,stat);
82  fResponse->SetParameter(1,1./peaktobackground);
83  fResponse->FixParameter(2,1);
84  fResponse->FixParameter(3,beamresolution*1e-6);
85  fResponse->SetParameter(4,resonancemass);
86  fResponse->SetParameter(5,resonancewidth*1e-6);
87 
88  return;
89 }
90 
91 void getCounts(Int_t n,Double_t *masses, Double_t *counts)
92 {
93  for (Int_t i=0; i<n; i++)
94  {
95  counts[i]=myran->Poisson(fResponse->Eval(masses[i]));
96  }
97  return;
98 }
99 
100 void fittest(Int_t nroftests=1, Char_t option[]="LQN", Double_t stat=200,Double_t peaktobackground=8, Double_t beamresolution=50.,Double_t resonancewidth=500., Double_t resonancemass=3.526, Int_t seed=0, Bool_t silent=kFALSE)
101 {
102  Double_t mass[2],width[2],chisqr;
103  Double_t xbins[13]={3.523, 3.525, 3.5252, 3.5254, 3.5256, 3.5258, 3.5260, 3.5262, 3.5264, 3.5266, 3.5268, 3.527, 3.529};
104  Double_t masses[12], counts[12];
105 
106  myran=new TRandom3(seed);
107 
108  xmin=xbins[0];xmax=xbins[12];
109 
110  for (Int_t i=0; i<13; i++)
111  {
112  masses[i]=(xbins[i]+xbins[i+1])/2.;
113  }
114 
115  char command[128];
116  sprintf(command,"output_stat=%i_p2b=%i_pres=%i_width=%i.root",(int) stat, (int) peaktobackground, (int) beamresolution, (int) resonancewidth);
117  TFile *f=new TFile(command,"RECREATE");
118 
119  TH1D *hisData=new TH1D("hisData","",12,xbins);
120  TH1D *his=new TH1D("hisWidth","",100,resonancewidth-10*beamresolution,resonancewidth+10*beamresolution);
121  TH1D *his2=new TH1D("hisMass","",100,-resonancewidth*2e-7+(xmin+xmax)/2.,resonancewidth*2e-7+(xmin+xmax)/2.);
122 
123  TH1D *his3=new TH1D("hiseMass","",100,0,beamresolution);
124  TH1D *his4=new TH1D("hiseWidth","",100,0,resonancewidth);
125 
126  TH2D *hisMassvsError=new TH2D("hisMassvsError","",100,-resonancewidth*2e-7+(xmin+xmax)/2.,resonancewidth*2e-7+(xmin+xmax)/2.,100,0,beamresolution);
127  TH2D *hisWidthvsError=new TH2D("hisWidthvsError","",100,resonancewidth-10*beamresolution,resonancewidth+10*beamresolution,100,0,resonancewidth);
128 
129  TH1D *hisxsq=new TH1D("hisxsq","",100,0,5);
130 
131  Double_t sumemass=0;
132  Double_t sumewidth=0;
133  Int_t count=0;
134 
135  TGraphErrors *grData=new TGraphErrors(12);
136 
137  for (Int_t i=0; i<nroftests; i++)
138  {
139  setShape(stat,peaktobackground,beamresolution,resonancewidth,resonancemass);
140  getCounts(12,masses,counts);
141  for (Int_t j=0; j<12; j++)
142  {
143  hisData->SetBinContent(j+1,counts[j]);
144  grData->SetPointError(j,0,TMath::Sqrt(counts[j]));
145  grData->SetPoint(j,masses[j],counts[j]);
146  }
147 
148  if (!silent) cout << i << " of the " << nroftests << endl;
149  Int_t retval = hisData->Fit("fResponse",option);
150  if (0==retval)
151  {
152  count++;
153  mass[0]=fResponse->GetParameter(4);
154  mass[1]=fResponse->GetParError(4);
155  width[0]=fabs(1e6*fResponse->GetParameter(5));
156  width[1]=fabs(1e6*fResponse->GetParError(5));
157  chisqr=fResponse->GetChisquare()/fResponse->GetNDF();
158 
159  if (!silent)
160  {
161  cout << "Chisq = " << chisqr << endl;
162  cout << "Mass = " << (Double_t) mass[0] << " +- " << (Double_t) mass[1] << " GeV" << endl;
163  cout << "Width = " << (Double_t) width[0] << " +- " << (Double_t) width[1] << " keV" << endl;
164  }
165  his->Fill(width[0]);
166  his2->Fill(mass[0]);
167  hisxsq->Fill(chisqr);
168 
169  his3->Fill(mass[1]*1e6);
170  his4->Fill(width[1]);
171 
172  hisMassvsError->Fill(mass[0],mass[1]*1e6);
173  hisWidthvsError->Fill(width[0],width[1]);
174 
175  sumemass+=mass[1];
176  sumewidth+=width[1];
177 
178  if (NULL==fSignal)
179  {
180  fSignal=new TF1("fSignal",signalResponse,xmin,xmax,6);
181  fSignal->SetNpx(1000);
182  fSignal->SetLineColor(kRed);
183  }
184 
185  for (Int_t i=0; i<6; i++)
186  {
187  fSignal->SetParameter(i,fResponse->GetParameter(i));
188  }
189 
190  if (NULL==fBW)
191  {
192  fBW=new TF1("fBW",breitWigner,xmin,xmax,3);
193  fBW->SetNpx(1000);
194  fBW->SetLineColor(kBlue);
195  }
196 
197  fBW->SetParameter(0,fResponse->GetParameter(0));
198  fBW->SetParameter(1,fResponse->GetParameter(4));
199  fBW->SetParameter(2,fResponse->GetParameter(5));
200 
201  }
202  if (!silent)
203  {
204  cout << "Mass error " << 1e6*sumemass/((Double_t) count) << endl;
205  cout << "Width error " << sumewidth/((Double_t) count) << endl;
206  }
207  }
208 
209  TH1D *dum=new TH1D("dum","",100,xmin,xmax);
210  dum->SetMinimum(0);
211  dum->SetMaximum(1.2*fResponse->Eval(resonancemass));
212 
213  Double_t val=1.1*fResponse->Eval(resonancemass)/fBW->Eval(resonancemass);
214  fBW->SetParameter(0,val*fBW->GetParameter(0));
215 
216  dum->Write();
217  fResponse->Write();
218  fBW->Write();
219  grData->Write();
220  hisData->Write();
221  his->Write();
222  his2->Write();
223  his3->Write();
224  his4->Write();
225  hisxsq->Write();
226  hisMassvsError->Write();
227  hisWidthvsError->Write();
228 
229  f->Close();
230 }
Int_t i
Definition: run_full.C:25
TF1 * f1
Definition: reco_analys2.C:50
TF1 * func
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
Double_t xmax
int n
void setShape(Double_t stat, Double_t peaktobackground, Double_t beamresolution, Double_t resonancewidth, Double_t resonancemass)
Double_t par[3]
Double_t signalResponse(Double_t *x, Double_t *par)
TF1 * fResponse
Double_t pandaResponse(Double_t *x, Double_t *par)
Double_t nastyFunction(Double_t *x, Double_t *par)
Double_t
TF1 * fBW
unsigned int seed
Double_t masses[5]
Definition: dedx_bands.C:112
TFile * f
Definition: bump_analys.C:12
TF1 * fSignal
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
TRandom3 * myran
Double_t x
TFile * f2
Double_t breitWigner(Double_t *x, Double_t *par)
int count
Double_t xmin
void getCounts(Int_t n, Double_t *masses, Double_t *counts)
Double_t Pi
void fittest(Int_t nroftests=1, Char_t option[]="LQN", Double_t stat=200, Double_t peaktobackground=8, Double_t beamresolution=50., Double_t resonancewidth=500., Double_t resonancemass=3.526, Int_t seed=0, Bool_t silent=kFALSE)
Definition: fittest.C:100