StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FitPtMass.C
1 // This is a simple macro to test the fitting function and result interactively. The input data is a non-tree root file from pi0 tree. The default name here is 'allruns.hist.root', but users may change it by your preference.
2 // This macro will basically generate a fitted plot with multiple parts: raw histogram, overall fit, background fit, pi0 after background subtraction and residual, etc.
3 // Author: Weihong He
4 
5 const int mx=1;
6 const int mt=1;
7 TFile *fd[mx];
8 TCanvas *c1=0;
9 
10 FitPtMass() {
11 
12  float ptmin=7.0,ptmax=8.0;
13  float fit_x1=0.07,fit_x2=0.4;
14  const int nend=int(fit_x1/0.01);
15  const int nstart=int(fit_x2/0.01);
16  cout<<"nend="<<nend<<" nstart="<<nstart<<endl;
17  int minbin,maxbin;
18  TH1F* h1= new TH1F("hMassAny","diphoton invariant mass",120,0.,1.2 );
19  TH1F* h2= new TH1F("hMassAny","diphoton invariant mass",120,0.,1.2 );
20  TH1F* hResidual= new TH1F("hMassAny","diphoton invariant mass",120,0.,1.2 );
21  FILE* fout=fopen("mass.txt","w");assert(fout);
22  fprintf(fout,"Bin# Entry\n");
23  char *PlotName[mx]={"hPTAny"};
24  char *fName[mt]={"allruns.hist"};
25  TString inPath="";
26  c1=new TCanvas("tmp","tmp",700,600);
27  gStyle->SetPalette(1,0);
28  int i;
29  for(i=0;i<mt;i++) {
30  TString hFile=inPath+fName[i];
31  hFile+=".root";
32  fd0=new TFile(hFile); assert(fd0->IsOpen());
33  fd[i]=fd0;
34  }
35  c1->Divide(1,1);
36  c1->cd(1);
37  int i;
38  for(i=0;i<mx;i++)
39  {
40 
41  //gStyle->SetPalette(1,0);
42  // .... inp eve
43  TString hname=PlotName[i];
44  printf("=%s=\n",hname.Data());
45  h0=(TH2F*)fd[0]->Get(hname); assert(h0);
46  minbin=h0->GetYaxis()->FindBin(ptmin);
47  maxbin=h0->GetYaxis()->FindBin(ptmax)-1;
48  h1->Add(h0->ProjectionX("htemp",minbin,maxbin,""));
49  int ent=h1->Integral();
50  cout<<"entry="<<ent<<endl;
51  h1->SetEntries(ent);
52  int ccount=0;
53  float sum=0;
54  float tpt=ptmin;
55  for(int i=minbin;i<=maxbin;i++){
56  ccount+=h0->ProjectionX("",i,i,"")->Integral();
57  sum+=(h0->ProjectionX("",i,i,"")->Integral())*tpt;
58  tpt+=(ptmax-ptmin)/(maxbin+1-minbin);
59  }
60  float ptmean=sum/ccount;
61  cout<<"raw="<<int(h1->Integral(11,19))<<" ptmean="<<ptmean<<endl;
62  h1->Draw();
63  h1->SetMinimum(-ent/100);
64  TF1* f1=new TF1("ffit","exp([0]+[1]*x)+[2]*exp(-0.5*((x-[3])/([4]*(1.0+6.11*(x-[3]))))**2)",fit_x1,fit_x2);
65  //TF1* f1=new TF1("ffit","exp([0]+[1]*x+[2]/x)+[3]*exp(-0.5*((x-[4])/([5]*(1.0+6.11*(x-[4]))))**2)",fit_x1,fit_x2);
66  f1->SetParameters(9.0,-8.0,10000,0.135,0.02);
67  //f1->FixParameter(1,-2.854);
68  //f1->FixParameter(2,0.002);
69  //f1->FixParameter(4,0.1366);
70  //f1->FixParameter(5,0.02676);
71  h1->Fit(f1,"R+","",fit_x1,fit_x2);
72  int nx=h1->GetNbinsX();
73 
74  float sumx=0,sum2;
75  for(int ix=1;ix<=nx;ix++)
76  {
77  fprintf(fout,"%d %d\n",ix,h1->GetBinContent(ix));
78  sumx+=h1->GetBinContent(ix);
79  }
80 
81  //cout<<"sumx="<<sumx<<endl;
82  //TF1* f2=new TF1("fit2","expo(0)",0.,1.2);
83  TF1* f2=new TF1("fit2","exp([0]+[1]*x+[2]/x)",0.,1.2);
84  //TF1* f2=new TF1("fit2","exp(5.7-6.4*x)",0.,1.2);
85  h2->Add(h1);
86  hResidual->Add(h1);
87  hResidual->Add(f1,-1);
88  for (int ix= 1; ix <= nend; ix++) {
89  hResidual->SetBinContent(ix, 0);
90  }
91  for (int ix= nstart; ix <= nx; ix++) {
92  hResidual->SetBinContent(ix, 0);
93  }
94  hResidual->SetLineColor(11);
95  hResidual->Draw("same");
96  f2->SetParameters(f1->GetParameter(0),f1->GetParameter(1));
97  h2->Add(f2,-1);
98  for (int ix= 1; ix <= 8; ix++) {
99  h2->SetBinContent(ix, 0);
100  }
101  //for(int ix=9;ix<=19;ix++){
102  //sum2+=h2->GetBinContent(ix);
103  //}
104  h2->SetLineColor(kBlue);
105  TF1* f3=new TF1("f3","exp([0]+[1]*x+[2]/x)",fit_x1,fit_x2);
106  f3->SetParameters(f1->GetParameter(0),f1->GetParameter(1));
107  cout<<"p0="<<f1->GetParameter(0)<<endl;
108  //h2->Fit("gaus","","",0.42,0.66);
109  int Realpi=h2->Integral(11,19);
110  int backpi=h1->Integral(11,19)-h2->Integral(11,19);
111  int totpi=h1->Integral(11,19);
112  cout<<"total pi="<<totpi<<" Real Pi0 integral="<<Realpi<<" backpi="<<backpi<<endl;
113  h2->Draw("same");
114  f3->SetLineColor(6);
115  f3->Draw("same");
116  //h2->Draw("error");
117  l1=new TLine(0.135,0.,0.135,7000);
118  l1->SetLineColor(kRed);
119  l1->Draw();
120  l2=new TLine(0.18,0.,0.18,7000);
121  l2->SetLineColor(kGreen);
122  l2->Draw();
123  l3=new TLine(0.1,0.,0.1,7000);
124  l3->SetLineColor(kGreen);
125  l3->Draw();
126  //h2->SetStats(kFALSE);
127  c1->Print("hMassAny.gif");
128 
129  }
130  return;
131 
132 }
133