GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DoFit1.C
Go to the documentation of this file.
1 
7 void DoFit1(Float_t sigma = 1.0, Char_t *logname = NULL)
8 {
9  const Int_t MAXMARKERS = 50 ; TVirtualFitter::Fitter(0,3*MAXMARKERS); //to get a maximum of 150 parameters
10 
11  Int_t i, nbmarkers, nbgauss, ind[MAXMARKERS];
12  Float_t x[MAXMARKERS], y[MAXMARKERS], xgauss[MAXMARKERS], ygauss[MAXMARKERS], xlow, xhigh; Double_t par[3*MAXMARKERS];
13 
14  FILE *flog; if ( TVirtualPad::Pad() == NULL ) return;
15 
16  TH1 *hist;
17  TIter next(gPad->GetListOfPrimitives());
18 
19  hist = NULL; nbmarkers = 0;
20  while ( (obj = next()) ) { // look for an histogram and for markers
21  if ( obj->InheritsFrom("TMarker") ) {
22  x[nbmarkers] = ((TMarker *)obj)->GetX();
23  y[nbmarkers++] = ((TMarker *)obj)->GetY();
24  }
25  if ( obj->InheritsFrom("TH1") && !obj->InheritsFrom("TH2") ) { hist = (TH1 *)obj; }
26  }
27  if ( hist == NULL || nbmarkers < 3 ) {
28  printf("There is no histograms or enough clicks in the current pad to realise a graphical fit \n"); return; }
29 
30  if ( logname ) { // results are written in a file
31  flog = fopen(logname,"a"); if ( flog == NULL ) return;
32  }
33 
34  // first one and last one are for background
35  TMath::Sort(nbmarkers,x,ind,false); xlow = x[ind[0]]; xhigh = x[ind[nbmarkers-1]];
36 
37  // histograms used to fit
38  TH1 *lochist = (TH1 *)hist->Clone("fitter"); lochist->Reset("ICE");
39 
40  // to get the backgound
41  Int_t first_bin, last_bin; Stat_t first_bin_c, last_bin_c, bin_c;
42 
43  first_bin = hist->FindBin( xlow );
44  last_bin = hist->FindBin( xhigh );
45  first_bin_c = hist->GetBinContent(first_bin); last_bin_c = hist->GetBinContent(last_bin);
46 
47  // determine the linear function for the background
48  TF1 *fbg;
49  if ( ( fbg = (TF1 *)gROOT->GetListOfFunctions()->FindObject("Background") ) ) { delete fbg; }
50 
51  fbg = new TF1("Background","pol1",xlow,xhigh);
52  fbg->SetParameters(first_bin_c-xlow*(last_bin_c-first_bin_c)/(xhigh-xlow),(last_bin_c-first_bin_c)/(xhigh-xlow));
53 
54  fbg->SetLineColor(30); fbg->SetLineWidth(3); fbg->SetLineStyle(1); fbg->Draw("same");
55 
56  // fill the histogram that is used to fit
57  for ( i = first_bin; i < last_bin ; i++ ) {
58  bin_c = hist->GetBinContent(i) -
59  ( Float_t(i-first_bin)*(last_bin_c-first_bin_c)/(last_bin-first_bin) + first_bin_c ) ;
60  lochist->SetBinContent(i,bin_c);
61  }
62  // now the function used to fit is created
63  char fname[500]; TString ffit, fdisplay = "pol1(0)";
64 
65  nbgauss = 0;
66  for ( i = 1; i < nbmarkers - 1; i++ ) {
67 
68  if ( y[ind[i]] < fbg->Eval(x[ind[i]]) ) { // background too high cannot add a gaussian at this position
69  printf("background too high at position %.1f \n",x[ind[i]]); continue;
70  }
71  // add a new gaussian and init parameters
72  par[3*nbgauss] = y[ind[i]] - fbg->Eval(x[ind[i]]);
73  par[3*nbgauss+1] = x[ind[i]];
74  par[3*nbgauss+2] = sigma;
75 
76  sprintf(fname,"gaus\(%d\)",nbgauss*3); ffit += "+"; ffit += fname;
77  sprintf(fname,"gaus\(%d\)",2+nbgauss*3); fdisplay += "+"; fdisplay += fname;
78 
79  nbgauss++;
80  }
81 
82  // perform the final fit and display the result
83  TF1 *f; if ( ( f = (TF1 *)gROOT->GetListOfFunctions()->FindObject("ResultFit") ) ) { delete f; }
84 
85  ffit.Remove(0,1);
86  f = new TF1("ResultFit",ffit.Data(),xlow,xhigh);
87 
88  // init parameters and limits
89  for ( i = 0; i < nbgauss; i++ ) {
90  f->SetParameter(i,par[i]);
91  // futur developments in case we would like to add limits
92  // f->SetParLimits(3*i,);
93  // f->SetParLimits(3*i+1,);
94  // f->SetParLimits(3*i+2,0.5*sigma,1.5*sigma);
95  // f->ReleaseParameter(3*i);f->ReleaseParameter(3*i+1);f->ReleaseParameter(3*i+2);
96  }
97  lochist->Fit(f,"NR");
98 
99  Stat_t intensity, intensity_err;
100  printf("\n");
101  printf("******************* DOFIT1 RESULTS ******************* \n");
102  printf(" Chi2 %.2f / %d = %.2f \n",f->GetChisquare(),f->GetNDF(),f->GetChisquare()/f->GetNDF());
103 
104 // printf(" Background %f %f \n",par[0],par[1]);
105 
106  for ( i = 0; i < nbgauss; i++ ) {
107 
108  intensity = f->GetParameter(2+3*i)*f->GetParameter(3*i)*TMath::Sqrt(2*TMath::Pi());
109  intensity_err = intensity * (f->GetParError(1+3*i)/f->GetParameter(1+3*i) + f->GetParError(2+3*i)/f->GetParameter(2+3*i));
110 
111  printf(" Peak %d : Position %.2f +- %.3f, width %.3f +- %.3f, Height %.1f +- %.1f, \t Intensity %.1f +- %.1f\n",
112  i+1,f->GetParameter(1+3*i),f->GetParError(1+3*i),2*f->GetParameter(2+3*i),2*f->GetParError(2+3*i),
113  f->GetParameter(3*i),f->GetParError(3*i),intensity,intensity_err);
114 
115  sprintf(fname,"peak%d",i);
116  TF1 *fpeak;
117  if ( ( fpeak = (TF1 *)gROOT->GetListOfFunctions()->FindObject(fname) ) ) { delete fpeak; }
118 
119  fpeak = new TF1(fname,"gaus",f->GetParameter(1+3*i)-10,f->GetParameter(1+3*i)+10);
120  fpeak->SetLineStyle(3); fpeak->SetLineColor(16);
121 
122  fpeak->SetParameters(f->GetParameter(3*i),f->GetParameter(1+3*i),f->GetParameter(2+3*i)); fpeak->Draw("same");
123 
124  if ( logname ) { // results are kept in a file
125  fprintf(flog,"#");
126  fprintf(flog,"# Position %.2f +- %.3f, width %.3f +- %.3f, Height %.1f +- %.1f, \t Intensity %.1f +- %.1f");
127  fprintf(flog,"#");
128  fprintf(flog,"%.2f\t %.3f\t%.3f\t%.3f\t%.1f\t%.1f\t%.1f\t%.1f\n",
129  f->GetParameter(1+3*i),f->GetParError(1+3*i),2*f->GetParameter(2+3*i),2*f->GetParError(2+3*i),
130  f->GetParameter(3*i),f->GetParError(3*i),intensity,intensity_err);
131  }
132  }
133  printf("****************************************************** \n");
134 
135  // to display the final result
136  par[0] = fbg->GetParameter(0);
137  par[1] = fbg->GetParameter(1); f->GetParameters(&par[2]);
138 
139  if ( ( f = (TF1 *)gROOT->GetListOfFunctions()->FindObject("DisplayFit") ) ) { delete f; }
140 
141  f = new TF1("DisplayFit",fdisplay.Data(),xlow,xhigh); f->SetParameters(par);
142  f->SetLineColor(6); f->SetLineWidth(3);
143 
144  delete lochist; // no longer useful
145 
146  f->Draw("same"); if ( logname ) { fclose(flog); } TVirtualPad::Pad()->Update();
147 }
148 
printf("******************************************************************** \n")
void DoFit1(Float_t sigma=1.0, Char_t *logname=NULL)
Function to fit gaussians.
Definition: DoFit1.C:7