FairRoot/PandaRoot
convolutionAnalysis.C
Go to the documentation of this file.
1 #include "TF1.h"
2 #include "TH1D.h"
3 #include "TMath.h"
4 #include "TRandom3.h"
5 #include "TROOT.h"
6 #include "TGraphErrors.h"
7 #include "TFile.h"
8 #include <iostream>
9 
12 
13 TRandom3 *myran;
14 
15 TF1 *func=NULL;
16 TF1 *fResponse=NULL;
17 TF1 *fSignal=NULL;
18 TF1 *fBW=NULL;
19 
21 {
22  Double_t f1=TMath::Exp((-(par[0]-x[0])*(par[0]-x[0]))/(2.*par[1]*par[1]));
23  Double_t f2=(x[0]-par[2])*(x[0]-par[2])+par[3]*par[3]/4.;
24  return f1/f2;
25 }
26 
27 /*
28  par[0]=scale
29  par[1]=background strength
30  par[2]=signal strength
31  par[3]=beam resolution
32  par[4]=mass of resonance
33  par[5]=width of resonance
34  */
35 
37 {
38  if (!func) func=new TF1("func",nastyFunction,xmin,xmax,4);
39  func->SetNpx(1000);
40  Double_t xx =x[0];
41  func->SetParameter(0,xx);
42  func->SetParameter(1,par[3]);
43  func->SetParameter(2,par[4]);
44  func->SetParameter(3,par[5]);
45  Double_t f1 = func->Integral(xx-10*par[3],xx+10*par[3]);
46  Double_t f2 = par[1]+f1*par[2]*par[5]*par[5]/(4.*TMath::Sqrt(2*TMath::Pi())*par[3]);
47  return par[0]*f2;
48 }
49 
50 
52 {
53  if (!func) func=new TF1("func",nastyFunction,xmin,xmax,4);
54  func->SetNpx(1000);
55  Double_t xx =x[0];
56  func->SetParameter(0,xx);
57  func->SetParameter(1,par[3]);
58  func->SetParameter(2,par[4]);
59  func->SetParameter(3,par[5]);
60  Double_t f1 = func->Integral(xx-10*par[3],xx+10*par[3]);
61  Double_t f2 = f1*par[2]*par[5]*par[5]/(4.*TMath::Sqrt(2*TMath::Pi())*par[3]);
62  return par[0]*f2;
63 }
64 
66 {
67  Double_t f1=par[0]*par[2]*par[2];
68  Double_t f2=(x[0]-par[1])*(x[0]-par[1])+par[2]*par[2]/4.;
69  return f1/f2;
70 }
71 
72 void setShape(Double_t stat,Double_t peaktobackground, Double_t beamresolution,Double_t resonancewidth, Double_t resonancemass)
73 {
74  if (NULL==fResponse)
75  {
76  fResponse=new TF1("fResponse",pandaResponse,xmin,xmax,6);
77  fResponse->SetNpx(1000);
78  }
79 
80  fResponse->SetParameter(0,stat);
81  fResponse->SetParameter(1,1./peaktobackground);
82  fResponse->FixParameter(2,1);
83  fResponse->FixParameter(3,beamresolution*1e-6);
84  fResponse->SetParameter(4,resonancemass);
85  fResponse->SetParameter(5,resonancewidth*1e-6);
86 
87  return;
88 }
89 
90 void getCounts(Int_t n,Double_t *masses, Double_t *counts)
91 {
92  for (Int_t i=0; i<n; i++)
93  {
94  counts[i]=myran->Poisson(fResponse->Eval(masses[i]));
95  }
96  return;
97 }
98 
99 void convolutionAnalysis(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)
100 {
101  Double_t mass[2],width[2],chisqr;
102  Double_t xbins[15]={3.505, 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, 3.545};
103  Double_t masses[14], counts[14];
104 
105  myran=new TRandom3(seed);
106 
107  xmin=xbins[0];xmax=xbins[14];
108 
109  for (Int_t i=0; i<15; i++)
110  {
111  masses[i]=(xbins[i]+xbins[i+1])/2.;
112  }
113 
114  char command[128];
115  sprintf(command,"output_stat=%i_p2b=%i_pres=%i_width=%i.root",(int) stat, (int) peaktobackground, (int) beamresolution, (int) resonancewidth);
116  TFile *f=new TFile(command,"RECREATE");
117 
118  TH1D *hisData=new TH1D("hisdata","",14,xbins);
119  TH1D *his=new TH1D("his","",20,resonancewidth-10*beamresolution,resonancewidth+10*beamresolution);
120  TH1D *his2=new TH1D("his2","",20,-resonancewidth*2e-7+(xmin+xmax)/2.,resonancewidth*2e-7+(xmin+xmax)/2.);
121  TH1D *hisxsq=new TH1D("hisxsq","",30,0,5);
122 
123  Double_t sumemass=0;
124  Double_t sumewidth=0;
125  Int_t count=0;
126 
127  TGraphErrors *grData=new TGraphErrors(14);
128 
129  for (Int_t i=0; i<nroftests; i++)
130  {
131  setShape(stat,peaktobackground,beamresolution,resonancewidth,resonancemass);
132  getCounts(14,masses,counts);
133  for (Int_t j=0; j<14; j++)
134  {
135  hisData->SetBinContent(j+1,counts[j]);
136  grData->SetPointError(j,0,TMath::Sqrt(counts[j]));
137  grData->SetPoint(j,masses[j],counts[j]);
138  }
139 
140  cout << i << " of the " << nroftests << endl;
141  Int_t retval = hisData->Fit("fResponse",option);
142  if (0==retval)
143  {
144  count++;
145  mass[0]=fResponse->GetParameter(4);
146  mass[1]=fResponse->GetParError(4);
147  width[0]=fabs(1e6*fResponse->GetParameter(5));
148  width[1]=fabs(1e6*fResponse->GetParError(5));
149  chisqr=fResponse->GetChisquare()/fResponse->GetNDF();
150 
151  cout << "Chisq = " << chisqr << endl;
152  cout << "Mass = " << (Double_t) mass[0] << " +- " << (Double_t) mass[1] << " GeV" << endl;
153  cout << "Width = " << (Double_t) width[0] << " +- " << (Double_t) width[1] << " keV" << endl;
154  his->Fill(width[0]);
155  his2->Fill(mass[0]);
156  hisxsq->Fill(chisqr);
157  sumemass+=mass[1];
158  sumewidth+=width[1];
159 
160  if (NULL==fSignal)
161  {
162  fSignal=new TF1("fSignal",signalResponse,xmin,xmax,6);
163  fSignal->SetNpx(1000);
164  fSignal->SetLineColor(kRed);
165  }
166 
167  for (Int_t i=0; i<6; i++)
168  {
169  fSignal->SetParameter(i,fResponse->GetParameter(i));
170  }
171 
172  if (NULL==fBW)
173  {
174  fBW=new TF1("fBW",breitWigner,xmin,xmax,3);
175  fBW->SetNpx(1000);
176  fBW->SetLineColor(kBlue);
177  }
178 
179  fBW->SetParameter(0,fResponse->GetParameter(0));
180  fBW->SetParameter(1,fResponse->GetParameter(4));
181  fBW->SetParameter(2,fResponse->GetParameter(5));
182 
183  }
184  cout << "Mass error " << 1e6*sumemass/((Double_t) count) << endl;
185  cout << "Width error " << sumewidth/((Double_t) count) << endl;
186  }
187 
188  TH1D *dum=new TH1D("dum","",100,xmin,xmax);
189  dum->SetMinimum(0);
190  dum->SetMaximum(1.2*fResponse->Eval(resonancemass));
191 
192  Double_t val=1.1*fResponse->Eval(resonancemass)/fBW->Eval(resonancemass);
193  fBW->SetParameter(0,val*fBW->GetParameter(0));
194 
195  dum->Write();
196  fResponse->Write();
197  fBW->Write();
198  grData->Write();
199  hisData->Write();
200  his->Write();
201  his2->Write();
202  hisxsq->Write();
203 
204  f->Close();
205 }
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]
void convolutionAnalysis(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)
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