StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MIPfit_loop.C
1 #include "TH1.h"
2 #include "TF1.h"
3 #include "TROOT.h"
4 #include "TStyle.h"
5 #include "TMath.h"
6 #include "TFile.h"
7 #include "TGraph.h"
8 #include "TCanvas.h"
9 
10 Double_t langaufun(Double_t *x, Double_t *par) {
11 
12  // Numeric constants
13  Double_t invsq2pi = 0.3989422804014; // (2 pi)^(-1/2)
14  Double_t mpshift = -0.22278298; // Landau maximum location
15 
16  // Control constants
17  Double_t np = 100.0; // number of convolution steps
18  Double_t sc = 5.0; // convolution extends to +-sc Gaussian sigmas
19 
20  // Variables
21  Double_t xx;
22  Double_t mpc;
23  Double_t fland;
24  Double_t sum = 0.0;
25  Double_t xlow,xupp;
26  Double_t step;
27  Double_t i;
28 
29  // MP shift correction
30  mpc = par[1] - mpshift * par[0];
31 
32  // Range of convolution integral
33  xlow = x[0] - sc * par[3];
34  xupp = x[0] + sc * par[3];
35 
36  step = (xupp-xlow) / np;
37 
38  // Convolution integral of Landau and Gaussian by sum
39  for(i=1.0; i<=np/2; i++) {
40  xx = xlow + (i-.5) * step;
41  fland = TMath::Landau(xx,mpc,par[0]) / par[0];
42  sum += fland * TMath::Gaus(x[0],xx,par[3]);
43 
44  xx = xupp - (i-.5) * step;
45  fland = TMath::Landau(xx,mpc,par[0]) / par[0];
46  sum += fland * TMath::Gaus(x[0],xx,par[3]);
47  }
48  return ((par[2]*step*sum*invsq2pi/par[3]));
49 }
50 
51 TF1 *langaufit(TH1F *his, Double_t *fitrange, Double_t *startvalues, Double_t *parlimitslo, Double_t *parlimitshi, Double_t *fitparams, Double_t *fiterrors)
52 {
53  // Once again, here are the Landau * Gaussian parameters:
54  // par[0]=Width (scale) parameter of Landau density
55  // par[1]=Most Probable (MP, location) parameter of Landau density
56  // par[2]=Total area (integral -inf to inf, normalization constant)
57  // par[3]=Width (sigma) of convoluted Gaussian function
58  //
59  // Variables for langaufit call:
60  // his histogram to fit
61  // fitrange[2] lo and hi boundaries of fit range
62  // startvalues[4] reasonable start values for the fit
63  // parlimitslo[4] lower parameter limits
64  // parlimitshi[4] upper parameter limits
65  // fitparams[4] returns the final fit parameters
66  // fiterrors[4] returns the final fit errors
67 
68  Int_t i;
69  Char_t FunName[100];
70 
71  sprintf(FunName,"Fitfcn_%s",his->GetName());
72 
73  cout<<" NAME = "<<FunName<<endl;
74  TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName);
75  if (ffitold) delete ffitold;
76 
77  TF1 *ffit = new TF1(FunName,langaufun,fitrange[0],fitrange[1],4);
78  ffit->SetParameters(startvalues);
79  ffit->SetParNames("Width","MP","Area","GSigma");
80 
81  for (i=0; i<4; i++) {
82  ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]);
83  }
84 
85  his->Fit(FunName,"RB0"); // fit within specified range, use ParLimits, do not plot
86 
87  ffit->GetParameters(fitparams); // obtain fit parameters
88  for (i=0; i<4; i++) {
89  fiterrors[i] = ffit->GetParError(i); // obtain fit parameter errors
90  }
91  return (ffit);
92 }
93 
94 //Background fit funciton
95 Double_t background(Double_t *x, Double_t *par){
96  return (par[0]*TMath::Power(x[0],par[1]));
97 }
98 
99 //total fit funct with convolution applied
100 Double_t total_wconvo(Double_t *x, Double_t *par){
101  return (background(x,par) + langaufun(x,&par[2]));
102 }
103 
104 void MIPfit_loop(int rebinFactor= 10, int minTow = 0, int maxTow = 63, int MIPrange=325, TString filename="minbias_checkmaker.root"){
105 
106  TFile *file=new TFile(filename);
107  TString outputFile="mip_fits.pdf";
108  TString openOutput=outputFile+"(";
109  TString closeOutput=outputFile+")";
110 
111  cout<<" YOU'RE READING IN..."<<filename<<endl;
112  cout<<" YOU'RE READING OUT..."<<outputFile<<endl;
113 
114  TH1F *h[64],*h_Clone[64];
115  TH1F *MIPbgsub1[64], *MIPbg[64];
116  TF1 *bg[64], *mip[64], *mipconvo, *mipconvo2;
117  TString title,mip_title,bg_title;
118  char mipbg_t[100],mipbg_n[100],mipbgsub_t[100],mipbgsub_n[100];
119  Int_t xmin,xmax;
120  Int_t fitxmin=60, fitxmax=150;
121  TAxis *y,*x;
122 
123  TH1F *chisq_edge=new TH1F("chiqr2","MIP Chisquare/DOF",100,0,10);
124  TH1F *chisq=new TH1F("chiqr","MIP Chisquare/DOF",100,0,10);
125 
126  float chisq_values[64];
127  float mostprob_values[64];
128  float id_list[64];
129 
130  int towids[64]={7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8,23,22,21,20,19,18,17,16,31,30,29,28,27,26,25,24,39,38,37,36,35,34,33,32,47,46,45,44,43,42,41,40,55,54,53,52,51,50,49,48,63,62,61,60,59,58,57,56};
131 
132  int edgetowids[15]={7,15,23,31,39,47,55,63,62,61,60,59,58,57,56};
133  float edgetowids_length=sizeof(edgetowids)/sizeof(edgetowids[0]);
134 
135  for(int id=minTow; id<maxTow+1; id++){
136 
137  fitxmax=150;
138 
139  //give edge tows a different start fit range
140  for(int i=0; i<edgetowids_length; i++){
141  if(edgetowids[i]==id){
142  fitxmax=700;
143  }
144  }
145 
147  bg_title="background_id";
148  bg_title+=id;
149  bg[id]=new TF1(bg_title, "[0]*x^[1]",fitxmin,fitxmax);
150 
151 
152  //get tower MIP spectrum
153  title="adcsum_det1_id";
154  title+=id;
155  cout<<"*****************************************"<<endl;
156  cout<<"*****************************************"<<endl;
157  cout<<"*********GETTING HISTOGRAM...#"<<id<<endl;
158  h[id]=(TH1F*)file->Get(title);
159  h[id]->Rebin(rebinFactor);
160  x=h[id]->GetXaxis();
161  x->SetTitle("ADC Sum");
162  x->SetRangeUser(20,900);
163 
164  // clone histogram for first iteration of fit
165  h_Clone[id]=(TH1F*)h[id]->Clone();
166 
167  //define histograms for MIP fit and MIP bg sub
168  sprintf(mipbg_t,"mipbackground id%d",id);
169  sprintf(mipbg_n,"mipbackground_id%d",id);
170 
171  int nbinX_hold=h[id]->GetNbinsX();
172  const int nbinX=nbinX_hold;
173  float binwidth=h[id]->GetBinWidth(nbinX);
174  float hist_nbins=nbinX*rebinFactor;
175  float hist_range=binwidth*nbinX;
176 
177  MIPbg[id]= new TH1F(mipbg_n,mipbg_t,hist_nbins,0,hist_range);
178  MIPbg[id]->Rebin(rebinFactor);
179  MIPbg[id]->Sumw2();
180 
181  sprintf(mipbgsub_t,"mipbackground1_sub_id%d",id);
182  sprintf(mipbgsub_n,"mipbackground1_sub_id%d",id);
183  MIPbgsub1[id]= new TH1F(mipbgsub_n,mipbgsub_t,hist_nbins,0,hist_range);
184  MIPbgsub1[id]->Rebin(rebinFactor);
185  MIPbgsub1[id]->Sumw2();
186 
187  //apply background fit to cloned histogram for subtraction
188  h_Clone[id]->Fit(bg[id],"","",fitxmin,fitxmax);
189 
190  //obtain fit params for bg fit, to see if fit failed
191  Double_t bgparam[4];
192  bg[id]->GetParameters(&bgparam[0]);
193  float bgwidth=bgparam[0];
194  float bgmp=bgparam[1];
195  cout<<"BG PARAMS: "<<bgwidth<<" "<<bgmp<<endl;
196 
197  //if bg fit failed, change fit range to find one that works
198  for(int j=0; j<=14;j++){
199  if(bgwidth<100){//if fit failed(small/zero fit params), try new fit max
200 
201  fitxmax=150+50*j;
202  h_Clone[id]->Fit(bg[id],"","",fitxmin,fitxmax);
203 
204  cout<<"TRYING FITMAX= "<<fitxmax<<endl;
205  bg[id]->GetParameters(&bgparam[0]);
206  bgwidth=bgparam[0];
207  }
208  }
209 
210  //fill background histograms
211  for(int bin=4; bin<nbinX; bin++){
212  float XbinCenter=x->GetBinCenter(bin);//get x value of bin center
213  float bg_at_center=bg[id]->Eval(XbinCenter);//evaluate the fit at that xval
214  MIPbg[id]->SetBinContent(bin,bg_at_center);
215  }
216  MIPbgsub1[id]->Add(h[id],MIPbg[id],1,-1);//perform background sub
217 
218  //SET UP CONVOLUTION FIT
220  Double_t fr[2];
221  Double_t sv[4], pllo[4], plhi[4], fp[4], fpe[4];
222 
224  float ncounts[nbinX];
225  for(int bin=4; bin<nbinX; bin++){
226  ncounts[bin]=MIPbgsub1[id]->GetBinContent(bin);//counts in the bin
227  }
228  float mostprob=ncounts[nbinX-1];//initialize mpv to last element in array
229  int mostprob_bin=nbinX-1;
230  float mostprob_x=0;
231  mostprob_x=x->GetBinCenter(mostprob_bin);
232 
233  int flag=0;//stop searching when there are 4 points to the left of peak
234  for(int i=nbinX-1; i>=0; i--){//loop backwards through array
235 
236  if((ncounts[i]>mostprob)&&(flag<4)){ //find local max
237  mostprob=ncounts[i];//most prob value
238  mostprob_bin=i;//bin of most prob value
239  mostprob_x=x->GetBinCenter(mostprob_bin);
240 
241  flag=0;
242  }
243  if(mostprob_x>MIPrange){//MIPrange is anticipated xmax location of MIP
244  flag=0;
245  }
246  else if((ncounts[i]<=mostprob)&&(mostprob_bin!=nbinX-1)){
247  flag++;
248  }
249  }
250  mostprob_x=x->GetBinCenter(mostprob_bin);
251  fr[0]=0.6*mostprob_x; // <-- fit range xmin
252  fr[1]=1.3*mostprob_x; // <-- fit range xmax
253 
254  cout<<"MIP FIT RANGE IS: "<< fr[0] <<" -- "<<fr[1]<<endl;
255 
256  pllo[0]=1; pllo[1]=0.6*mostprob_x; pllo[2]=10.0; pllo[3]=5.0;
257  plhi[0]=10; plhi[1]=1.3*mostprob_x; plhi[2]=8000.0; plhi[3]=100.0;
258  sv[0]=1; sv[1]=mostprob_x; sv[2]=1000.0; sv[3]=20;
259 
260  //fit bg subtracted MIP spectra w convolution
261  mipconvo = langaufit(MIPbgsub1[id],fr,sv,pllo,plhi,fp,fpe);
262  MIPbgsub1[id]->Fit(mipconvo,"R+");
263  MIPbgsub1[id]->GetListOfFunctions()->Add(mipconvo);
264  mipconvo->SetNpx(10000);
265 
266  Double_t param[4];
267  mipconvo->GetParameters(&param[0]);
268  float width=param[0];
269  float mp=param[1];
270  float area=param[2];
271  float gsigma=param[3];
272 
273  Double_t param2[4];
274  mipconvo->GetParameters(&param2[0]);
275  float mpv2=param2[1];
276  mostprob_values[id]=mpv2;
277  id_list[id]=id;
278 
279  //get chi2/dof for each fit
280  float chi=mipconvo->GetChisquare();
281  float DOF=mipconvo->GetNDF();
282  float chiDOF=chi/DOF;
283  chisq_values[id]=chiDOF;
284 
285  chisq->Fill(chiDOF);
286  }
287 
288  //plot MIPs
289  gStyle->SetOptStat(0);
290  TCanvas *c=new TCanvas("c","mip fit",1100,900);
291 
292  for (int id =minTow; id < maxTow+1; id++)
293  {
294  c->cd(towids[id]+1);
295  c->SetGrid(0,0);
296  c->SetLogy();
297 
298  x1=MIPbgsub1[id]->GetXaxis();
299  x1->SetTitle("ADC Sum");
300  x1->SetRangeUser(20,900);
301 
302  MIPbgsub1[id]->Draw();
303  MIPbgsub1[id]->SetLineColor(kBlue);
304 
305  //print MIPs to multi-page pdf
306  if(id==0){
307  c->Print(openOutput, "pdf");
308  }
309  else if(id==63){
310  c->Print(closeOutput,"pdf");
311  }
312  else{
313  c->Print(outputFile, "pdf");
314  }
315 
316  }
317 
318  //plot chisq/dof for each tower
319  TCanvas *d=new TCanvas("d","av chisqr",1100,900);
320  chisq_edge->SetLineColor(kBlue);
321  chisq->Draw();
322  chisq_edge->Draw("same");
323  d->Print("mip_chisqr.pdf");
324 
325 
326  //plot most probable values of MIP for each tower
327  TCanvas *e=new TCanvas("e", "mean values",1100,900);
328  TGraph* mean_vs_id=new TGraph(64,id_list,mostprob_values);
329  mean_vs_id->GetXaxis()->SetTitle("Ecal Tower ID");
330  mean_vs_id->GetYaxis()->SetTitle("Most Prob. Value (ADCSum)");
331  mean_vs_id->SetMarkerStyle(21);
332  mean_vs_id->SetMarkerSize(2);
333  mean_vs_id->SetMarkerColor(2);
334  mean_vs_id->Draw("AP");
335  e->Print("mip_mpvs.pdf");
336 
337 }
338