void drawPrunedJM_noDiboson(){ gStyle->SetOptStat(0); gStyle->SetOptFit(11111111); TFile *f = TFile::Open("prunedJetMMu_noDiboson.root"); TH1D* h_MCWW = (TH1D*)(f->Get("prunedJetMMu_WW_pythia_filtered")); TH1D* h_MCWZ = (TH1D*)(f->Get("prunedJetMMu_WZ_pythia_filtered")); TH1D* h_MCZZ = (TH1D*)(f->Get("prunedJetMMu_ZZ_pythia_filtered")); TH1D* h_dataMu = (TH1D*)(f->Get("prunedJetMMu_data_DoubleMu")); Double_t lumi_MCWW = 9959752/56.0; Double_t lumi_MCWZ = 9910267/22.4; Double_t lumi_MCZZ = 9769891/7.6; Double_t lumi_dataMu = 19671.225; h_MCWW->Scale(lumi_dataMu/lumi_MCWW); h_MCWZ->Scale(lumi_dataMu/lumi_MCWZ); h_MCZZ->Scale(lumi_dataMu/lumi_MCZZ); TH1D* h_allDibosonMC = new TH1D("h_allDibosonMC", "", 100, 40, 240); h_allDibosonMC->Add(h_MCWW); h_allDibosonMC->Add(h_MCWZ); h_allDibosonMC->Add(h_MCZZ); TH1D* h_dataMu_noDiboson = new TH1D("h_dataMu_noDiboson", "", 100, 40, 240); h_dataMu_noDiboson->SetTitle("Pruned jet mass distribution (data)"); h_dataMu_noDiboson->GetXaxis()->SetTitle("Mass"); h_dataMu_noDiboson->GetYaxis()->SetTitle("Event numbers"); Int_t NBIN = h_allDibosonMC->GetNbinsX(); for(Int_t i = 1; i < NBIN; i++){ Int_t binCenter = h_dataMu_noDiboson->GetBinCenter(i); Int_t binContent = h_dataMu->GetBinContent(i) - h_allDibosonMC->GetBinContent(i); if( binCenter > 110 && binCenter < 140 ) continue; h_dataMu_noDiboson->SetBinContent(i, binContent); } TF1* fitCurve = new TF1("fitCurve", fitFunc, 40, 240, 4); h_dataMu_noDiboson->Fit(fitCurve, "", "", 40, 240); Double_t nBkgSig = fitCurve->Integral(110,140)/h_dataMu->GetBinWidth(1); vector<Double_t> par; vector<Double_t> posPar; vector<Double_t> negPar; for(Int_t i = 0; i < 4; i++){ par.push_back(fitCurve->GetParameters()[i]); posPar.push_back(par[i] + fitCurve->GetParErrors()[i]); negPar.push_back(par[i] - fitCurve->GetParErrors()[i]); } Double_t x; TF1* posFit[4]; posFit[0] = new TF1("posFit1", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",posPar[0],par[1],par[2],par[3]), 40, 240); posFit[1] = new TF1("posFit2", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],posPar[1],par[2],par[3]), 40, 240); posFit[2] = new TF1("posFit3", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],par[1],posPar[2],par[3]), 40, 240); posFit[3] = new TF1("posFit4", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],par[1],par[2],posPar[3]), 40, 240); TF1* negFit[4]; negFit[0] = new TF1("negFit1", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",negPar[0],par[1],par[2],par[3]), 40, 240); negFit[1] = new TF1("negFit2", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],negPar[1],par[2],par[3]), 40, 240); negFit[2] = new TF1("negFit3", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],par[1],negPar[2],par[3]), 40, 240); negFit[3] = new TF1("negFit4", Form("%f*TMath::Exp(%f*x)*0.5*(1+TMath::Erf((x-(%f))/%f))",par[0],par[1],par[2],negPar[3]), 40, 240); TMatrixD posColM(4,1); TMatrixD negColM(4,1); TMatrixD posRowM(1,4); TMatrixD negRowM(1,4); for(Int_t i = 0; i < 4; i++){ posColM(i,0) = fabs(nBkgSig - posFit[i]->Integral(110,140)/h_dataMu->GetBinWidth(1)); negColM(i,0) = fabs(nBkgSig - negFit[i]->Integral(110,140)/h_dataMu->GetBinWidth(1)); posRowM(0,i) = posColM(i,0); negRowM(0,i) = negColM(i,0); } gMinuit->mnmatu(1); TFitResultPtr fitptr = h_dataMu_noDiboson->Fit(fitCurve, "S"); TFitResult fitresult = (*fitptr); TMatrixD corrM = fitresult.GetCorrelationMatrix(); TMatrixD posTemp = posRowM*(corrM*posColM); TMatrixD negTemp = negRowM*(corrM*negColM); Double_t posSystUnc = TMath::Sqrt(posTemp(0,0)); Double_t negSystUnc = TMath::Sqrt(negTemp(0,0)); cout << "\nNumber of background events in signal region: " << nBkgSig << " +" << posSystUnc << " -" << negSystUnc << "\n" << endl; TCanvas* c = new TCanvas("c","",0,0,800,800); c->cd(); h_dataMu_noDiboson->SetFillColor(kCyan); h_dataMu_noDiboson->Draw(); c->Print("dataMu_noDiboson.pdf"); }
Stat_t AnalysisClass::doFit(Int_t RunNumber,Char_t *Variable, Char_t *SubDetName, Char_t *Layer,Char_t *label){ TH1 *htoFit=0; pLanGausS[0]=0; pLanGausS[1]=0; pLanGausS[2]=0; pLanGausS[3]=0; epLanGausS[0]=0; epLanGausS[1]=0; epLanGausS[2]=0; epLanGausS[3]=0; if (debug) cout << d1->GetTitle() << " " << Variable << " " << SubDetName << endl; pPar[0]=0; pPar[1]=0; TIter it(d1->GetListOfKeys()); TObject * o; while ( (o = it())) { TObject * d = d1->Get(o->GetName()); if(d->IsA()->InheritsFrom("TDirectory") && strstr(d->GetName(),SubDetName)){ if (debug) cout << "Found " << SubDetName << endl; TIter it2(((TDirectoryFile * )d)->GetListOfKeys()); TObject *o2; while( ( o2 = it2()) ){ TObject *d2 = ((TDirectoryFile * )d)->Get(o2->GetName()); if(d2->IsA()->InheritsFrom("TDirectory") && strstr(d2->GetName(),Layer) ){ if (debug) cout << "Found Layer" << Layer << endl; TIter it3(((TDirectoryFile * )d2)->GetListOfKeys()); TObject *o3; while( ( o3 = it3()) ){ TObject *d3 = ((TDirectoryFile * )d2)->Get(o3->GetName()); if(strstr(d3->GetName(),Variable) && strstr(d3->GetName(),label)){ htoFit = (TH1*) d3; if (debug) cout << "Found " << Variable << endl; if (htoFit->GetEntries()!=0) { cout<<"Fitting "<< htoFit->GetTitle() <<endl; // Setting fit range and start values Double_t fr[2]; Double_t sv[4], pllo[4], plhi[4]; fr[0]=0.5*htoFit->GetMean(); fr[1]=3.0*htoFit->GetMean(); // (EM) parameters setting good for signal only Int_t imax=htoFit->GetMaximumBin(); Double_t xmax=htoFit->GetBinCenter(imax); Double_t ymax=htoFit->GetBinContent(imax); Int_t i[2]; Int_t iArea[2]; i[0]=htoFit->GetXaxis()->FindBin(fr[0]); i[1]=htoFit->GetXaxis()->FindBin(fr[1]); iArea[0]=htoFit->GetXaxis()->FindBin(fr[0]); iArea[1]=htoFit->GetXaxis()->FindBin(fr[1]); Double_t AreaFWHM=htoFit->Integral(iArea[0],iArea[1],"width"); sv[1]=xmax; sv[2]=htoFit->Integral(i[0],i[1],"width"); sv[3]=AreaFWHM/(4*ymax); sv[0]=sv[3]; plhi[0]=25.0; plhi[1]=200.0; plhi[2]=1000000.0; plhi[3]=50.0; pllo[0]=1.5 ; pllo[1]=10.0 ; pllo[2]=1.0 ; pllo[3]= 1.0; // create different landau+gaussians for different runs Char_t FunName[100]; sprintf(FunName,"FitfcnLG_%s%d",htoFit->GetName(),fRun); TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName); if (ffitold) delete ffitold; langausFit = new TF1(FunName,langaufun,fr[0],fr[1],4); langausFit->SetParameters(sv); langausFit->SetParNames("Width","MP","Area","GSigma"); for (Int_t i=0; i<4; i++) { langausFit->SetParLimits(i,pllo[i],plhi[i]); } htoFit->Fit(langausFit,"R0"); // "R" fit in a range,"0" quiet fit langausFit->SetRange(fr[0],fr[1]); pLanGausS=langausFit->GetParameters(); epLanGausS=langausFit->GetParErrors(); chi2GausS =langausFit->GetChisquare(); // obtain chi^2 nDofGausS = langausFit->GetNDF(); // obtain ndf Double_t sPeak, sFWHM; langaupro(pLanGausS,sPeak,sFWHM); pLanConv[0]=sPeak; pLanConv[1]=sFWHM; cout << "langaupro: max " << sPeak << endl; cout << "langaupro: FWHM " << sFWHM << endl; TCanvas *cAll = new TCanvas("Fit",htoFit->GetTitle(),1); Char_t fitFileName[60]; sprintf(fitFileName,"Fits/Run_%d/%s/Fit_%s.png",RunNumber,SubDetName,htoFit->GetTitle()); htoFit->Draw("pe"); htoFit->SetStats(100); langausFit->Draw("lsame"); gStyle->SetOptFit(1111111); cAll->Print(fitFileName,"png"); } else { pLanGausS[0]=-10; pLanGausS[1]=-10; pLanGausS[2]=-10; pLanGausS[3]=-10; epLanGausS[0]=-10; epLanGausS[1]=-10; epLanGausS[2]=-10; epLanGausS[3]=-10; pLanConv[0]=-10; pLanConv[1]=-10; chi2GausS=-10; nDofGausS=-10; } } } } } } } return htoFit->GetEntries(); }
//Noise section Stat_t AnalysisClass::doNoiseFit(Int_t RunNumber, Char_t *Variable, Char_t *SubDetName, Char_t *Layer, Char_t *label){ TH1 *hNtoFit=0; if (debug) cout << d1->GetTitle() << " " << Variable << " " << SubDetName << endl; pPar[0]=0; pPar[1]=0; TIter it(d1->GetListOfKeys()); TObject * o; while ( (o = it())) { TObject * d = d1->Get(o->GetName()); if(d->IsA()->InheritsFrom("TDirectory") && strstr(d->GetName(),SubDetName)){ if (debug) cout << "Found " << SubDetName << endl; TIter it2(((TDirectoryFile * )d)->GetListOfKeys()); TObject *o2; while( ( o2 = it2()) ){ TObject *d2 = ((TDirectoryFile * )d)->Get(o2->GetName()); if(d2->IsA()->InheritsFrom("TDirectory") && strstr(d2->GetName(),Layer) ){ if (debug) cout << "Found Layer" << Layer << endl; TIter it3(((TDirectoryFile * )d2)->GetListOfKeys()); TObject *o3; while( ( o3 = it3()) ){ TObject *d3 = ((TDirectoryFile * )d2)->Get(o3->GetName()); if(strstr(d3->GetName(),Variable) && strstr(d3->GetName(),label)){ hNtoFit = (TH1*) d3; if (debug) cout << "Found " << Variable << endl; if (hNtoFit->GetEntries()!=0) { cout<<"Fitting "<< hNtoFit->GetTitle() <<endl; // Setting fit range and start values Double_t fr[2]; Double_t sv[3], pllo[3], plhi[3]; fr[0]=hNtoFit->GetMean()-5*hNtoFit->GetRMS(); fr[1]=hNtoFit->GetMean()+5*hNtoFit->GetRMS(); Int_t imax=hNtoFit->GetMaximumBin(); Double_t xmax=hNtoFit->GetBinCenter(imax); Double_t ymax=hNtoFit->GetBinContent(imax); Int_t i[2]; Int_t iArea[2]; i[0]=hNtoFit->GetXaxis()->FindBin(fr[0]); i[1]=hNtoFit->GetXaxis()->FindBin(fr[1]); iArea[0]=hNtoFit->GetXaxis()->FindBin(fr[0]); iArea[1]=hNtoFit->GetXaxis()->FindBin(fr[1]); Double_t AreaFWHM=hNtoFit->Integral(iArea[0],iArea[1],"width"); sv[2]=AreaFWHM/(4*ymax); sv[1]=xmax; sv[0]=hNtoFit->Integral(i[0],i[1],"width"); plhi[0]=1000000.0; plhi[1]=10.0; plhi[2]=10.; pllo[0]=1.5 ; pllo[1]=0.1; pllo[2]=0.3; Char_t FunName[100]; sprintf(FunName,"FitfcnLG_%s%d",hNtoFit->GetName(),fRun); TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName); if (ffitold) delete ffitold; gausFit = new TF1(FunName,Gauss,fr[0],fr[1],3); gausFit->SetParameters(sv); gausFit->SetParNames("Constant","GaussPeak","Sigma"); for (Int_t i=0; i<3; i++) { gausFit->SetParLimits(i,pllo[i],plhi[i]); } hNtoFit->Fit(gausFit,"R0"); gausFit->SetRange(fr[0],fr[1]); pGausS=gausFit->GetParameters(); epGausS=gausFit->GetParErrors(); chi2GausS =langausFit->GetChisquare(); // obtain chi^2 nDofGausS = langausFit->GetNDF();// obtain ndf TCanvas *cAllN = new TCanvas("NoiseFit",hNtoFit->GetTitle(),1); Char_t fitFileName[60]; sprintf(fitFileName,"Fits/Run_%d/%s/Fit_%s.png",RunNumber,SubDetName,hNtoFit->GetTitle()); hNtoFit->Draw("pe"); gStyle->SetOptFit(1111111); gausFit->Draw("lsame"); cAllN->Print(fitFileName,"png"); }else { pGausS[0]=-10; pGausS[1]=-10; pGausS[2]=-10; epGausS[0]=-10; epGausS[1]=-10; epGausS[2]=-10; } } } } } } } return hNtoFit->GetEntries(); }
/** * This function fits TH2F slices with a selected fitType function (gaussian, lorentz, ...). */ TGraphErrors* fit2DProj(TString name, TString path, int minEntries, int rebinX, int rebinY, int fitType, TFile * outputFile, const TString & resonanceType, const double & xDisplace, const TString & append) { //Read the TH2 from file TFile *inputFile = new TFile(path); TH2 * histo = (TH2*) inputFile->Get(name); if( rebinX > 0 ) histo->RebinX(rebinX); if( rebinY > 0 ) histo->RebinY(rebinY); //Declare some variables TH1 * histoY; TString nameY; std::vector<double> Ftop; std::vector<double> Fwidth; std::vector<double> Fmass; std::vector<double> Etop; std::vector<double> Ewidth; std::vector<double> Emass; std::vector<double> Fchi2; std::vector<double> Xcenter; std::vector<double> Ex; TString fileOutName("fitCompare2"+name); fileOutName += append; fileOutName += ".root"; TFile *fileOut=new TFile(fileOutName,"RECREATE"); for (int i=1; i<=(histo->GetXaxis()->GetNbins());i++) { //Project on Y (set name and title) std::stringstream number; number << i; TString numberString(number.str()); nameY = name + "_" + numberString; // std::cout << "nameY " << nameY << std::endl; histoY = histo->ProjectionY(nameY, i, i); double xBin = histo->GetXaxis()->GetBinCenter(i); std::stringstream xBinString; xBinString << xBin; TString title("Projection of x = "); title += xBinString.str(); histoY->SetName(title); if (histoY->GetEntries() > minEntries) { //Make the dirty work! TF1 *fit; std::cout << "fitType = " << fitType << std::endl; if(fitType == 1) fit = gaussianFit(histoY, resonanceType); else if(fitType == 2) fit = lorentzianFit(histoY, resonanceType); else if(fitType == 3) fit = linLorentzianFit(histoY); else { std::cout<<"Wrong fit type: 1=gaussian, 2=lorentzian, 3=lorentzian+linear."<<std::endl; abort(); } double *par = fit->GetParameters(); double *err = fit->GetParErrors(); // Check the histogram alone TCanvas *canvas = new TCanvas(nameY+"alone", nameY+" alone"); histoY->Draw(); canvas->Write(); // Only for check TCanvas *c = new TCanvas(nameY, nameY); histoY->Draw(); fit->Draw("same"); fileOut->cd(); c->Write(); if( par[0] == par[0] ) { //Store the fit results Ftop.push_back(par[0]); Fwidth.push_back(fabs(par[1]));//sometimes the gaussian has negative width (checked with Rene Brun) Fmass.push_back(par[2]); Etop.push_back(err[0]); Ewidth.push_back(err[1]); Emass.push_back(err[2]); Fchi2.push_back(fit->GetChisquare()/fit->GetNDF()); double xx= histo->GetXaxis()->GetBinCenter(i); Xcenter.push_back(xx); double ex = 0; Ex.push_back(ex); } else { // Skip nan std::cout << "Skipping nan" << std::endl; Ftop.push_back(0); Fwidth.push_back(0); Fmass.push_back(0); Etop.push_back(1); Ewidth.push_back(1); Emass.push_back(1); Fchi2.push_back(100000); Xcenter.push_back(0); Ex.push_back(1); } } } fileOut->Close(); //Plots the fit results in TGraphs const int nn= Ftop.size(); double x[nn],ym[nn],e[nn],eym[nn]; double yw[nn],eyw[nn],yc[nn]; // std::cout << "number of bins = " << nn << std::endl; // std::cout << "Values:" << std::endl; for (int j=0;j<nn;j++){ // std::cout << "xCenter["<<j<<"] = " << Xcenter[j] << std::endl; x[j]=Xcenter[j]+xDisplace; // std::cout << "Fmass["<<j<<"] = " << Fmass[j] << std::endl; ym[j]=Fmass[j]; // std::cout << "Emass["<<j<<"] = " << Emass[j] << std::endl; eym[j]=Emass[j]; // std::cout << "Fwidth["<<j<<"] = " << Fwidth[j] << std::endl; yw[j]=Fwidth[j]; // std::cout << "Ewidth["<<j<<"] = " << Ewidth[j] << std::endl; eyw[j]=Ewidth[j]; // std::cout << "Fchi2["<<j<<"] = " << Fchi2[j] << std::endl; yc[j]=Fchi2[j]; e[j]=0; } TGraphErrors *grM = new TGraphErrors(nn,x,ym,e,eym); grM->SetTitle(name+"_M"); grM->SetName(name+"_M"); TGraphErrors *grW = new TGraphErrors(nn,x,yw,e,eyw); grW->SetTitle(name+"_W"); grW->SetName(name+"_W"); TGraphErrors *grC = new TGraphErrors(nn,x,yc,e,e); grC->SetTitle(name+"_chi2"); grC->SetName(name+"_chi2"); grM->SetMarkerColor(4); grM->SetMarkerStyle(20); grW->SetMarkerColor(4); grW->SetMarkerStyle(20); grC->SetMarkerColor(4); grC->SetMarkerStyle(20); //Draw and save the graphs outputFile->cd(); TCanvas * c1 = new TCanvas(name+"_W",name+"_W"); c1->cd(); grW->Draw("AP"); c1->Write(); TCanvas * c2 = new TCanvas(name+"_M",name+"_M"); c2->cd(); grM->Draw("AP"); c2->Write(); TCanvas * c3 = new TCanvas(name+"_C",name+"_C"); c3->cd(); grC->Draw("AP"); c3->Write(); return grM; }