5 #include <TDirectoryFile.h>
6 #include <TClonesArray.h>
10 #include <AliHFMassFitter.h>
15 //root[0] .x fitD0InvMass.C+
18 //input: nsigma is the number of sigma in which significance, signal and background are calculated
20 //fit of histograms in the directory PWG3_D2H_D0InvMass of AnalysisResults.root . Must specify the list name where the histo are stored. Produce a root file with the fitted histos and a text file with signal, background and significance
22 void fitD0(Int_t rebin=0,TString listname="coutputmassD0mycuts",Int_t nsigma=3, TString pathin="./",TString pathout="./",Int_t btype=2){
24 TString file="AnalysisResults.root"; //"D0InvMass.root";
27 cout<<"Opening "<<file<<endl;
28 TFile *fin=new TFile(file.Data());
30 cout<<"File "<<file.Data()<<" not found"<<endl;
34 gStyle->SetOptFit(0111);
35 gStyle->SetOptStat("nemrou");
38 TString dirname="PWG3_D2H_D0InvMass";
39 TDirectoryFile *dir=(TDirectoryFile*)fin->GetDirectory(dirname);
41 TList *lista = (TList*) dir->Get(listname.Data());
43 cout<<listname<<" doesn't exist"<<endl;
47 TString fileout="table.dat";
48 fileout.Prepend(pathout);
49 ofstream out(fileout.Data());
51 AliHFMassFitter *fitter=new AliHFMassFitter();
53 cout<<"mean = "<<fitter->GetMean()<<"\tsigma= "<<fitter->GetSigma()<<endl;
56 out<<"Bakground type = "<<btype<<";\trebin = "<<rebin<<endl;
58 for(Int_t ipt=0;ipt<npt;ipt++){ //npt
60 out<<"************************************************************\n";
61 out<<"* ptbin * entriesMass * entriesSgn * entriesBkg * entriesRfl *\n";
64 TString nameall="histMass_";
66 TH1F *hMass=(TH1F*)lista->FindObject(nameall);
68 cout<<nameall<<" not found"<<endl;
72 out<<"* "<< ipt+1 <<" * "<<hMass->GetEntries()<<" * ";
74 TString namesgn="histSgn_";
76 TH1F *hSgn=(TH1F*)lista->FindObject(namesgn);
78 cout<<namesgn<<" not found"<<endl;
82 out<<hSgn->GetEntries()<<" * ";
84 TString namebkg="histBkg_";
86 TH1F *hBkg=(TH1F*)lista->FindObject(namebkg);
88 cout<<namebkg<<" not found"<<endl;
92 out<<hBkg->GetEntries()<<" * ";
93 //REFLECTED SIGNAL from MC
94 TString namerfl="histRfl_";
96 TH1F *hRfl=(TH1F*)lista->FindObject(namerfl);
98 cout<<namerfl<<" not found"<<endl;
102 out<<hRfl->GetEntries()<<" *\n";
103 out<<"*************************************************\n";
111 Double_t sgn,errsgn,errsgn2=0,bkg,errbkg,errbkg2=0,sgnf,errsgnf,sgnfMC;
112 Double_t meanfrfit,sigmafrfit, meanMC=hSgn->GetMean(),sigmaMC=hSgn->GetRMS();
114 nbin=hMass->GetNbinsX();
115 min=hMass->GetBinLowEdge(1);
116 max=min+nbin*hMass->GetBinWidth(nbin);
120 TString namentu="ntupt3bin";
121 if(init) fitter->InitNtuParam((char*)namentu.Data());
123 fitter->SetHisto(hMass);
124 fitter->SetRangeFit(min, max);
125 //fitter->SetRangeFit(1.83,1.89);
126 fitter->SetType(btype,0);//(b,s)
127 if(rebin!=0) fitter->RebinMass(rebin);
128 Bool_t fitOK=fitter->MassFitter(kFALSE); //kFALSE = do not draw
130 out<<"Fit return kFALSE, skip "<<hMass->GetName()<<endl;
131 fitter->Reset(); //delete histogram set
134 width=fitter->GetHistoClone()->GetBinWidth(3);
135 cout<<"\nChi^2 = "<<fitter->GetChiSquare()<<"\t Reduced Chi^2 = "<<fitter->GetReducedChiSquare()<<endl;
136 meanfrfit=fitter->GetMean();
137 sigmafrfit=fitter->GetSigma();
138 out<<"mean = "<<meanfrfit<<" (MC "<<meanMC<<")"<<"\tsigma= "<<sigmafrfit<<" (MC "<<sigmaMC<<")"<<endl;
139 //cout<<"old nbin = "<<hMass->GetNbinsX()<<"\tnew nbin = "<<fitter->GetHistoClone()->GetNbinsX()<<endl;
140 if(meanfrfit<0 || sigmafrfit<0 || meanfrfit<min || meanfrfit>max) {
141 cout<<"Fit failed, check"<<endl;
142 out<<hMass->GetName();
143 out<<": \nSgn not available"<<endl;
144 out<<"Bkg not available"<<endl;
145 out<<"Sgnf not available"<<endl;
146 out<<"*************************************************\n";
149 //cout<<"Writing..."<<endl;
150 fitter->WriteHisto(pathout);
151 hMass= fitter->GetHistoClone();
152 //TH1F *hc=fitter->GetHistoClone();
153 //TF1 *fbtest=hc->GetFunction("funcbkgRecalc"); //new version of ALiHFMassFitter
155 fitter->FillNtuParam();
157 Double_t limsx,limdx;
158 limsx=meanfrfit-nsigma*sigmafrfit;
159 limdx=meanfrfit+nsigma*sigmafrfit;
160 // limsx=meanMC-nsigma*sigmaMC;
161 //limdx=meanMC+nsigma*sigmaMC;
164 //determine limit of nsigma in bins
166 binsx=hMass->FindBin(limsx);
167 if (limsx > hMass->GetBinCenter(binsx)) binsx++;
168 bindx=hMass->FindBin(limdx);
169 if (limdx < hMass->GetBinCenter(bindx)) bindx--;
173 sxr=hMass->GetBinLowEdge(binsx);
174 dxr=hMass->GetBinLowEdge(bindx+1);
176 fitter->Signal(sxr,dxr,sgn,errsgn);
177 fitter->Background(sxr,dxr,bkg,errbkg);
178 fitter->Significance(sxr,dxr,sgnf,errsgnf);
181 Float_t inttot,intsgn,intsgnerr;
200 TF1 *fmass=hMass->GetFunction("funcmass");
203 inttot=fmass->GetParameter(0);
204 intsgn=fmass->GetParameter(np);
205 intsgnerr=fmass->GetParError(np);
206 //cout<<"i = "<<i<<"inttot = "<<inttot<<"\tintsgn = "<<intsgn<<"\tintsgnErr = "<<intsgnerr<<"\twidth = "<<width<<endl;
207 Double_t errbkg2rel=errbkg/bkg*TMath::Sqrt((inttot-intsgn)/width/bkg);//intsgnerr/(inttot-intsgn)*TMath::Sqrt((inttot-intsgn)/width/bkg);
208 Double_t errsgn2rel=errsgn/sgn*TMath::Sqrt(intsgn/width/sgn);
209 errbkg2=errbkg2rel*bkg;
210 errsgn2=errsgn2rel*sgn;
214 cout<<"bin sx = "<<binsx<<"\t xsx = "<<sxr<<endl;
215 cout<<"bin dx = "<<bindx<<"\t xdx = "<<dxr<<endl;
217 out<<hMass->GetName();
218 Double_t sgnMC,bkgMC,rflMC;
219 sgnMC=hSgn->Integral(binsx,bindx);
220 bkgMC=hBkg->Integral(binsx,bindx);
221 rflMC=hRfl->Integral(binsx,bindx);
222 sgnfMC=sgnMC/TMath::Sqrt(sgnMC+bkgMC+rflMC);
223 out<<": \nSgn "<<sgn<<"+/-"<<errsgn<<" ("<<errsgn2<<")\tCompare with: "<<sgnMC<<endl;
224 out<<"Bkg "<<bkg<<"+/-"<<errbkg<<" ("<<errbkg2<<")\tCompare with: "<<bkgMC<<" + "<<rflMC<<" = "<<bkgMC+rflMC<<endl;
225 out<<"Sgnf "<<sgnf<<"+/-"<<errsgnf<<"\tCompare with: "<<sgnfMC<<endl;
226 if (sgn != 0 && sgnfMC != 0) out<<"sigmaS/S = "<<errsgn/sgn<<"\tCompare with 1/signif: "<<1./sgnfMC<<endl;
228 out<<"sigmaS/S = "<<errsgn<<"/"<<sgn<<"\tCompare with 1/signif: 1./"<<sgnfMC<<endl;
230 out<<"nsigma considered for comparison = \ndx: "<<(dxr-meanfrfit)/sigmafrfit<<"\nsx: "<<(meanfrfit-sxr)/sigmafrfit<<endl;
231 out<<"Mean = "<<meanfrfit<<"\tSigma = "<<sigmafrfit<<endl;
232 out<<"*************************************************\n";
236 sgn=0; bkg=0; sgnf=0;
237 errsgn=0; errbkg=0; errsgnf=0;
238 if (ipt == npt-1) fitter->WriteNtuple(pathout);
241 fitter->Reset(); //delete histogram set