void shTest(){ Double_t par[] = {0.3164, 3., 1.5}; // guesses Double_t points(24); TF1 * sT = new TF1("sT","0.3164*pow(x,4)*exp(-x*1.5)",0., points); sT->Draw(); Double_t shapeInt = sT->Integral(0., points); Double_t shapeMax = sT->GetMaximum(0.,points); Double_t shapePeak= sT->GetMaximumX(0.,points); Double_t shapeRMS = sqrt(sT->Moment(2,0.,points)-sT->Moment(1,0.,points)*sT->Moment(1,0.,points)); cout<<"SHAPE Peak = "<<shapePeak<<" Max = "<<shapeMax<<" RMS = "<<shapeRMS<<" Integral = "<<shapeInt<<endl; // return par[0]*pow(x1,4.)*(exp(-x1*16./fN)); // fN is set to (float)NSAMPLES }
void GetFWHM(TF1* floo, double& mean, double& fwhm) { TF1* fgt = (TF1*) gROOT->FindObject("fgt"); if (fgt != 0) delete fgt; fgt = new TF1("fgt", gaustail, 0, 6000, 5); for (int i=0;i<5;i++) fgt->SetParameter(i,floo->GetParameter(i)); double mean = fgt->GetMaximumX(); double max = fgt->GetMaximum(); double left = fgt->GetX(max/2.,0,mean); double right = fgt->GetX(max/2.,mean,6000); fwhm = right - left; cout << "Found " << mean << " " << fwhm << endl; }
int AngOptimize() { ifstream infile("angSNR.dat"); ofstream outfile("OptAng.dat",ios::app); istringstream iss; char line[1000]; cout<<"aaaaaaaa"<<endl; const int nene=21; const int nep = 28; double epn[nene][nep]; double sigN[nene][nep]; double bckN[nene][nep]; double snr[nene][nep]; int iene=-1; int iep=-1; double enes[nene]; double opteps[nene]; char title[nene][1000]; TF1 *fit = new TF1("fit","[0]*(x-[2])+[1]/(x-[2])+[3]",178,179.6); fit->SetParameter(0,1.5); fit->SetParameter(1,1); fit->SetParameter(2,180); fit->SetParLimits(2,180,180) fit->SetParameter(3,40); bool start=0; if (!infile.is_open()) return -1; while (!infile.eof()){ //cout<<"b"<<endl; infile.getline(line,1000); if (line[0]!='K') { if (!start) continue; if (line[0]=='#' || line[0]==' ') continue; } else { iep=-1; start=1; iene++; if (iene>=21) break; string tit; iss.clear(); iss.str(line); iss >> tit; std::cout<<tit<<endl; sprintf(title[iene],"%s",tit.c_str()); continue; } iep++; //cout<<line<<endl; iss.clear(); iss.str(line); double ratio,signal,background; iss >> ratio >> signal >> background; // 3 sigma //iss >> signal >> background; // 5 sigma epn[iene][iep] = ratio; sigN[iene][iep] = signal; bckN[iene][iep] = background; if (sigN[iene][iep]==0) snr[iene][iep] = 0; else snr[iene][iep] = sigN[iene][iep]/sqrt(sigN[iene][iep]+bckN[iene][iep]); //cout<< epn[iene][i]<<sigN[iene][i]<<bckN[iene][i]<<endl; } for (iene=0;iene<nene;iene++){ TGraph *graph = new TGraph(nep,epn[iene],snr[iene]); graph->SetTitle(title[iene]); graph->SetMarkerStyle(25); graph->GetYaxis()->SetTitle("S/#sqrt{S+N}"); graph->GetXaxis()->SetTitle("#theta"); TCanvas *ca1 = new TCanvas(title[iene], title[iene]); graph->Draw("AP"); //graph->Fit("pol4","","",178,179.9); ////////TF1 *fpol2 = graph->GetFunction("pol2"); ////////fit->SetParameter(0,fpol2->GetParameter(0)); ////////fit->SetParameter(1,fpol2->GetParameter(1)); ////////fit->SetParameter(2,fpol2->GetParameter(2)); //graph->Fit(fit,"","",178,179.6); graph->Fit(fit,"","",172.0+iene*6.0/21.0,179.9); double optep = fit->GetMaximumX(176,179.6); outfile<<"Optmized theta at "<< title[iene] << " is "<<optep<<std::endl; enes[iene] = atof(&title[iene][3])/10000.; opteps[iene] = optep; char ofname[1000]; sprintf(ofname,"output/Angopt_%s.pdf",title[iene]); ca1->Print(ofname); } TGraph* gep = new TGraph(iene,enes,opteps); gep->SetTitle("Optimized #theta"); gep->SetMarkerStyle(25); gep->GetYaxis()->SetTitle("S/#sqrt{S+N}"); gep->GetXaxis()->SetTitle("#theta"); TCanvas *ca1 = new TCanvas("gep","Optimized #theta"); gep->Draw("AP"); gep->Fit("pol1"); ca1->Print("output/Angopt.pdf"); double c0 = gep->GetFunction("pol1")->GetParameter(0); double c1 = gep->GetFunction("pol1")->GetParameter(1); //double c2 = gep->GetFunction("pol2")->GetParameter(2); outfile << "Theta = "<< c0 << " + " << c1 << "*x + " <<std::endl; std::cout << "Theta = "<< c0 << " + " << c1 << "*x + "<<std::endl; return 0; }
void dumpProfile() { std::string fileName_ = "Profile_SM10.root"; TFile *shapeFile_ = TFile::Open(fileName_.c_str(),"old"); TProfile* PROF_704 = (TProfile*) shapeFile_->Get("SHAPE_XTAL_704"); ofstream out; out.open("dat.txt"); int nBinsHisto_ = 250; std::vector<double> shapeArray(nBinsHisto_,0.0); double max = -999; int imax = 0; for(int ibin=0; ibin < nBinsHisto_; ++ibin) { out << "shapeArray[" << ibin << "] = " << PROF_704->GetBinContent(ibin+1) << " ; \n"; shapeArray[ibin] = PROF_704->GetBinContent(ibin); std::cout << "Original shape, ns = " << ibin << " shape = " << shapeArray[ibin] << std::endl; if ( shapeArray[ibin] > max ) { max = shapeArray[ibin]; imax = ibin; } }//loop out.close(); double xMinHisto_ = -1.; double xMaxHisto_ = 9.; double binw = (xMaxHisto_ - xMinHisto_)/(shapeArray.size()); int nbins = shapeArray.size()/10; float low = xMinHisto_+(double)(imax-nbins/2+0.5)*binw; float up = xMinHisto_+(double)(imax+nbins/2+0.5)*binw; double* x = new double[nbins]; double* y = new double[nbins]; for (int i = 0; i < nbins; i++) { x[i] = xMinHisto_ + (double)(imax - nbins/2 + i + 0.5)*binw; y[i] = shapeArray[imax - nbins/2 + i]; std::cout << " x,y = " << x[i] << " " << y[i] << " " << (double)(imax - nbins/2 + i + 0.5) << std::endl; } TGraph* graph = new TGraph(nbins, x, y); graph->Fit("pol3", "V");//"Q 0"); TF1* fFit = graph->GetFunction("pol3"); double tMax = fFit->GetMaximumX(); std:;cout << "Maxiumum = " << tMax << std::endl; gStyle->SetOptFit(1111); TCanvas *MyC = new TCanvas("MyC","Test canvas",1); MyC->Divide(2,1); MyC->cd(1); PROF_704->Draw(); MyC->cd(2); fFit->Draw(); MyC->SaveAs("PROF_704.jpg"); }
void fitTF1(TCanvas *canvas, TH1F h, double XMIN_, double XMAX_, double dX_, double params[], Color_t LC_=kBlack) { //double& FWHM_, double& x0_, double& x1_, double& x2_, double& y0_, double& y1_, double& INT_, double& YIELD_) { //TCanvas* fitTF1(TH1F h, double HSCALE_, double XMIN_, double XMAX_) { //TF1* fitTF1(TH1F h, double HSCALE_, double XMIN_, double XMAX_) { gROOT->ForceStyle(); RooMsgService::instance().setSilentMode(kTRUE); for(int i=0;i<2;i++) RooMsgService::instance().setStreamStatus(i,kFALSE); //TCanvas *canvas = new TCanvas(TString::Format("canvas_%s",h.GetName()),TString::Format("canvas_%s",h.GetName()),1800,1200); RooDataHist *RooHistFit = 0; RooAddPdf *model = 0; RooWorkspace w = RooWorkspace("w","workspace"); RooRealVar x("mbbReg","mbbReg",XMIN_,XMAX_); RooRealVar kJES("CMS_scale_j","CMS_scale_j",1,0.9,1.1); RooRealVar kJER("CMS_res_j","CMS_res_j",1,0.8,1.2); kJES.setConstant(kTRUE); kJER.setConstant(kTRUE); TString hname = h.GetName(); RooHistFit = new RooDataHist("fit_"+hname,"fit_"+hname,x,&h); RooRealVar YieldVBF = RooRealVar("yield_"+hname,"yield_"+hname,h.Integral()); RooRealVar m("mean_"+hname,"mean_"+hname,125,100,150); RooRealVar s("sigma_"+hname,"sigma_"+hname,12,3,30); RooFormulaVar mShift("mShift_"+hname,"@0*@1",RooArgList(m,kJES)); RooFormulaVar sShift("sShift_"+hname,"@0*@1",RooArgList(m,kJER)); RooRealVar a("alpha_"+hname,"alpha_"+hname,1,-10,10); RooRealVar n("exp_"+hname,"exp_"+hname,1,0,100); RooRealVar b0("b0_"+hname,"b0_"+hname,0.5,0.,1.); RooRealVar b1("b1_"+hname,"b1_"+hname,0.5,0.,1.); RooRealVar b2("b2_"+hname,"b2_"+hname,0.5,0.,1.); RooRealVar b3("b3_"+hname,"b3_"+hname,0.5,0.,1.); RooBernstein bkg("signal_bkg_"+hname,"signal_bkg_"+hname,x,RooArgSet(b0,b1,b2)); RooRealVar fsig("fsig_"+hname,"fsig_"+hname,0.7,0.0,1.0); RooCBShape sig("signal_gauss_"+hname,"signal_gauss_"+hname,x,mShift,sShift,a,n); model = new RooAddPdf("signal_model_"+hname,"signal_model_"+hname,RooArgList(sig,bkg),fsig); //RooFitResult *res = model->fitTo(*RooHistFit,RooFit::Save(),RooFit::SumW2Error(kFALSE),"q"); model->fitTo(*RooHistFit,RooFit::Save(),RooFit::SumW2Error(kFALSE),"q"); //res->Print(); //model->Print(); canvas->cd(); canvas->SetTopMargin(0.1); RooPlot *frame = x.frame(); // no scale RooHistFit->plotOn(frame); model->plotOn(frame,RooFit::LineColor(LC_),RooFit::LineWidth(2));//,RooFit::LineStyle(kDotted)); model->plotOn(frame,RooFit::Components(bkg),RooFit::LineColor(LC_),RooFit::LineWidth(2),RooFit::LineStyle(kDashed)); // with scale // RooHistFit->plotOn(frame,RooFit::Normalization(HSCALE_,RooAbsReal::NumEvent)); // model->plotOn(frame,RooFit::Normalization(HSCALE_,RooAbsReal::NumEvent),RooFit::LineWidth(1)); // model->plotOn(frame,RooFit::Components(bkg),RooFit::LineColor(kBlue),RooFit::LineWidth(1),RooFit::LineStyle(kDashed),RooFit::Normalization(HSCALE_,RooAbsReal::NumEvent)); frame->GetXaxis()->SetLimits(50,200); frame->GetXaxis()->SetNdivisions(505); frame->GetXaxis()->SetTitle("M_{b#bar{b}} (GeV)"); frame->GetYaxis()->SetTitle("Events"); frame->Draw(); h.SetFillColor(kGray); h.Draw("hist,same"); frame->Draw("same"); gPad->RedrawAxis(); TF1 *tmp = model->asTF(x,fsig,x); //tmp->Print(); double y0_ = tmp->GetMaximum(); double x0_ = tmp->GetMaximumX(); double x1_ = tmp->GetX(y0_/2.,XMIN_,x0_); double x2_ = tmp->GetX(y0_/2.,x0_,XMAX_); double FWHM_ = x2_-x1_; double INT_ = tmp->Integral(XMIN_,XMAX_); double YIELD_= YieldVBF.getVal(); double y1_ = dX_*0.5*y0_*(YieldVBF.getVal()/tmp->Integral(XMIN_,XMAX_)); params[0] = x0_; params[1] = x1_; params[2] = x2_; params[3] = y0_; params[4] = y1_; params[5] = FWHM_; params[6] = INT_; params[7] = YIELD_; //cout<<"Int = "<<tmp->Integral(XMIN_,XMAX_)<<", Yield = "<<YieldVBF.getVal()<<", y0 = "<<y0_<<", y1 = "<<y1_ <<", x0 = "<< x0_ << ", x1 = "<<x1_<<", x2 = "<<x2_<<", FWHM = "<<FWHM_<<endl; TLine ln = TLine(x1_,y1_,x2_,y1_); ln.SetLineColor(kMagenta+3); ln.SetLineStyle(7); ln.SetLineWidth(2); ln.Draw(); canvas->Update(); canvas->SaveAs("testC.png"); // tmp->Delete(); // frame->Delete(); // res->Delete(); // TF1 *f1 = model->asTF(x,fsig,x); // return f1; ////tmp->Delete(); ////ln->Delete(); ////model->Delete(); ////RooHistFit->Delete(); ////w->Delete(); ////YieldVBF->Delete(); ////frame->Delete(); ////res->Delete(); //delete tmp; //delete ln; //delete model; //delete RooHistFit; //delete w; //delete YieldVBF; //delete frame; //delete res; // return canvas; }