void vertexAssociationFit(TFile* input, TFile* output) { // style gStyle->SetOptStat(0); gStyle->SetOptFit(111); gStyle->SetOptTitle(1); // margins gStyle->SetStatFormat("6.4g"); gStyle->SetFitFormat("5.2g"); gStyle->SetPadTopMargin(0.07); gStyle->SetPadBottomMargin(0.12); gStyle->SetPadLeftMargin(0.1); gStyle->SetPadRightMargin(0.02); // prepare the output // * canvas to put the fit per bin TCanvas* canvas_0 = new TCanvas("FitPerBin","Fit for each vertex bin",1200,900); canvas_0->Divide(4,4); // * canvas to put the plots per bin TCanvas* canvas_1 = new TCanvas("EffPerBin","Efficiency vs purity for each vertex bin",1200,900); canvas_1->Divide(4,4); // * canvas with other plots: slope vs nvertices, efficiency and purity for fixed cut, etc. TCanvas* canvas_2 = new TCanvas("jvAssociation","Efficiency vs purity summary plots",1000,600); canvas_2->Divide(4,2); // summary graphs TGraphErrors* slopeGraph = new TGraphErrors(nbins); slopeGraph->SetNameTitle("slopeGraph","expo slope in each n_PU bin"); TGraphErrors* constGraph = new TGraphErrors(nbins); constGraph->SetNameTitle("constGraph","expo normalization in each n_PU bin"); TGraphErrors* cuteffGraph = new TGraphErrors(nbins); cuteffGraph->SetNameTitle("cuteffGraph","Efficiency in each n_PU bin"); TGraphErrors* cutpurGraph = new TGraphErrors(nbins); cutpurGraph->SetNameTitle("cutpurGraph","Purity in each n_PU bin"); TGraphErrors* effcutGraph = new TGraphErrors(nbins); effcutGraph->SetNameTitle("effcutGraph","Cut for fixed efficiency in each n_PU bin"); TGraphErrors* effpurGraph = new TGraphErrors(nbins); effpurGraph->SetNameTitle("effpurGraph","Purity for fixed efficiency in each n_PU bin"); TGraphErrors* purcutGraph = new TGraphErrors(nbins); purcutGraph->SetNameTitle("purcutGraph","Cut for fixed purity in each n_PU bin"); TGraphErrors* pureffGraph = new TGraphErrors(nbins); pureffGraph->SetNameTitle("pureffGraph","Efficiency for fixed purity in each n_PU bin"); // loop over the directories double slope = 0.; double slopeError = -999.; double constnorm = 0.; double constError = -999.; TGraphErrors* result = NULL; TFitResultPtr fitRes(-1); double x,y, xe,ye; unsigned int index; TString histonames[nbins+1]; TString labelnames[nbins+1]; // get the histogram TH1* evts_per_PUbin = (TH1*)input->Get("finaldistros_ssvhpt/GoodJet/GoodJet_nvertices"); double sumTot(0), effTot(0); TLatex *latexLabel = new TLatex(); latexLabel->SetTextSize(0.05); latexLabel->SetTextFont(42); latexLabel->SetLineWidth(2); latexLabel->SetNDC(); TLatex *latexLabel2 = new TLatex(); latexLabel2->SetTextSize(0.09); latexLabel2->SetTextFont(42); latexLabel2->SetLineWidth(2); latexLabel2->SetNDC(); for(unsigned int i=0; i<=nbins; ++i) { // get the histogram labelnames[i]=Form("n_PU = %d bin",i); std::cout<<"labelnames["<<i<<"]"<<labelnames[i]<<std::endl; histonames[i]=Form("finaldistros_ssvhpt/GoodJet/GoodJet_jetvtx_ratio2b_%d_PUvtx",i); std::cout<<"seizing: histonames["<<i<<"]:"<<histonames[i]<<std::endl; TH1* fraction = (TH1*)input->Get(histonames[i]); //fraction->Rebin(2); // do it canvas_0->cd(i+1); fraction->SetTitle(""); MakeNiceHistoStyle(fraction); if(fraction) vertexAssociationAnalysis(fraction,i,slope,slopeError,constnorm,constError,result,fitRes); latexLabel2->DrawLatex(0.15, 0.85,labelnames[i]); latexLabel2->DrawLatex(0.15, 0.75,"CMS #sqrt{s}= 7 TeV"); gPad->Draw(); gPad->SetLogy(); MakeNiceStatBox(fraction); output->cd(); fraction->Write(); // plot canvas_1->cd(i+1); result->SetTitle(""); MakeNiceGraphStyle(result); result->Draw("alP"); latexLabel->DrawLatex(0.05, 0.95,labelnames[i]); result->GetXaxis()->SetTitle("Purity"); result->GetYaxis()->SetTitle("Efficiency"); result->GetXaxis()->SetRangeUser(0.8,1.2); result->GetYaxis()->SetRangeUser(0.85,1.0); output->cd(); result->Write(); // extract other quantities slopeGraph->SetPoint(i,i,slope); slopeGraph->SetPointError(i,0,slopeError); slopeGraph->GetXaxis()->SetTitle("n_{PU} vertices"); constGraph->SetPoint(i,i,constnorm); constGraph->SetPointError(i,0,constError); constGraph->GetXaxis()->SetTitle("n_{PU} vertices"); index = unsigned(((defaultCut-cutmin)/(cutmax-cutmin)*nSteps)+0.5); findWorkingPoint(result,index,x,y,xe,ye,1); // to compute the weighted efficiency int ivtx_bin=evts_per_PUbin->FindBin(i+1); sumTot+=evts_per_PUbin->GetBinContent(ivtx_bin); effTot+=y*evts_per_PUbin->GetBinContent(ivtx_bin); cuteffGraph->SetPoint(i,i,y); cuteffGraph->SetPointError(i,0,ye); cuteffGraph->GetXaxis()->SetTitle("n_{PU} vertices"); cutpurGraph->SetPoint(i,i,x); cutpurGraph->SetPointError(i,0,xe); cutpurGraph->GetXaxis()->SetTitle("n_{PU} vertices"); x = defaultPur; findWorkingPoint(result,index,x,y,xe,ye,2); purcutGraph->SetPoint(i,i,cutmin+(cutmax-cutmin)/nSteps*index); purcutGraph->GetXaxis()->SetTitle("n_{PU} vertices"); pureffGraph->SetPoint(i,i,y); pureffGraph->SetPointError(i,0,ye); pureffGraph->GetXaxis()->SetTitle("n_{PU} vertices"); y = defaultEff; findWorkingPoint(result,index,x,y,xe,ye,3); effcutGraph->SetPoint(i,i,cutmin+(cutmax-cutmin)/nSteps*index); effcutGraph->GetXaxis()->SetTitle("n_{PU} vertices"); effpurGraph->SetPoint(i,i,x); effpurGraph->SetPointError(i,0,xe); effpurGraph->GetXaxis()->SetTitle("n_{PU} vertices"); std::cout<<"end of other quantities: bin["<<i<<"]"<<std::endl; } std::cout << "Weighted Efficiency" << effTot/sumTot << std::endl; // plot summary graphs TF1* f=NULL; canvas_2->cd(1); MakeNiceGraphStyle(slopeGraph); slopeGraph->Draw("alP"); slopeGraph->Fit("pol1"); f = slopeGraph->GetFunction("pol1"); if(f->GetParError(1)>fabs(f->GetParameter(1))) slopeGraph->Fit("pol0"); canvas_2->cd(2); MakeNiceGraphStyle(constGraph); constGraph->Draw("alP"); constGraph->Fit("pol2"); f = constGraph->GetFunction("pol2"); canvas_2->cd(3); MakeNiceGraphStyle(cuteffGraph); cuteffGraph->Draw("alP"); cuteffGraph->Fit("pol1"); f = cuteffGraph->GetFunction("pol1"); if(f->GetParError(1)>fabs(f->GetParameter(1))) cuteffGraph->Fit("pol0"); latexLabel2->DrawLatex(0.15, 0.15,Form("Jet Fraction cut: %.2f",defaultCut)); canvas_2->cd(4); MakeNiceGraphStyle(cutpurGraph); cutpurGraph->Draw("alP"); cutpurGraph->Fit("pol1"); f = cutpurGraph->GetFunction("pol1"); if(f->GetParError(1)>fabs(f->GetParameter(1))) cutpurGraph->Fit("pol0"); latexLabel2->DrawLatex(0.15, 0.15,Form("Jet Fraction cut: %.2f",defaultCut)); canvas_2->cd(5); MakeNiceGraphStyle(effcutGraph); effcutGraph->Draw("alP"); effcutGraph->Fit("expo"); latexLabel2->DrawLatex(0.15, 0.15,Form("Fixed efficiency: %.2f",defaultEff)); canvas_2->cd(6); MakeNiceGraphStyle(effpurGraph); effpurGraph->Draw("alP"); effpurGraph->Fit("pol1"); f = effpurGraph->GetFunction("pol1"); if(f->GetParError(1)>fabs(f->GetParameter(1))) effpurGraph->Fit("pol0"); latexLabel2->DrawLatex(0.15, 0.15,Form("Fixed efficiency: %.2f",defaultEff)); canvas_2->cd(7); gPad->SetLeftMargin(0.12); MakeNiceGraphStyle(purcutGraph); purcutGraph->Draw("alP"); purcutGraph->Fit("expo"); latexLabel2->DrawLatex(0.15, 0.15,Form("Fixed purity: %.3f",defaultPur)); canvas_2->cd(8); MakeNiceGraphStyle(pureffGraph); pureffGraph->Draw("alP"); pureffGraph->Fit("pol1"); f = pureffGraph->GetFunction("pol1"); if(f->GetParError(1)>fabs(f->GetParameter(1))) pureffGraph->Fit("pol0"); latexLabel2->DrawLatex(0.15, 0.15,Form("Fixed purity: %.3f",defaultPur)); // save output->cd(); canvas_0->Write(); canvas_1->Write(); canvas_2->Write(); canvas_0->SaveAs((TString)(canvas_0->GetName())+".png"); canvas_1->SaveAs((TString)(canvas_1->GetName())+".png"); canvas_2->SaveAs((TString)(canvas_2->GetName())+".png"); }
void DrawCosmicResult(){ TFile* tf = new TFile("Cosmic3Out.root"); TTree* trin = (TTree*)tf->Get("trOut"); const int nCSI = 2716; Int_t RunNumber; Int_t EventNumber; Double_t ScintiSignal = 0; Double_t ScintiHHTime = -500.; Double_t ScintiTime =-500.; Int_t nCSIDigi = 0; Double_t CSIDigiE[nCSI];//nCSIDigi Double_t CSIDigiTime[nCSI];//nCSIDigi Double_t CSIDigiHHTime[nCSI];//nCSIDigi Int_t CSIDigiID[nCSI];//nCSIDigi Double_t CSIDigiSignal[nCSI];//nCSIDigi Double_t FitP0[2]; Double_t FitP1[2]; Double_t FitChisq[2]; Double_t CSIDigiDeltaT0[nCSI];//nCSIDigi Double_t CSIDigiDeltaT1[nCSI];//nCSIDigi Int_t CosmicTrigUp; Int_t CosmicTrigDn; Double_t Roh; Double_t Theta; trin->SetBranchAddress( "RunNumber" , &RunNumber ); trin->SetBranchAddress( "EventNumber" , &EventNumber ); trin->SetBranchAddress( "ScintiSignal" , &ScintiSignal ); trin->SetBranchAddress( "ScintiHHTimne" , &ScintiHHTime ); trin->SetBranchAddress( "ScintiTime" , &ScintiTime ); trin->SetBranchAddress( "nCSIDigi" , &nCSIDigi ); trin->SetBranchAddress( "CSIDigiE" , CSIDigiE ); trin->SetBranchAddress( "CSIDigiTime" , CSIDigiTime ); trin->SetBranchAddress( "CSIDigiHHTime" , CSIDigiHHTime ); trin->SetBranchAddress( "CSIDigiID" , CSIDigiID ); trin->SetBranchAddress( "CSIDigiSignal" , CSIDigiSignal ); trin->SetBranchAddress( "CSIDigiDeltaT0" , CSIDigiDeltaT0 ); trin->SetBranchAddress( "CSIDigiDeltaT1" , CSIDigiDeltaT1 ); trin->SetBranchAddress( "FitP0" , FitP0 ); trin->SetBranchAddress( "FitP1" , FitP1 ); trin->SetBranchAddress( "FitChisq" , FitChisq ); trin->SetBranchAddress( "CosmicTrigUp" , &CosmicTrigUp ); trin->SetBranchAddress( "CosmicTrigDn" , &CosmicTrigDn ); trin->SetBranchAddress( "Roh" , &Roh ); trin->SetBranchAddress( "Theta" , &Theta ); TFile* tfout = new TFile("CosmicOut_hist3.root", "recreate"); TH2D* hisDeltaChannel = new TH2D("hisDeltaChannel","hisDeltaChannel",2716,0,2716,100,-10,10); TH1D* hisDelta[2716]; TGraphErrors* grDelta = new TGraphErrors(); TGraphErrors* grRES = new TGraphErrors(); TCanvas *can = new TCanvas("can","",800,800); for( int i = 0; i< 2716; i++){ hisDelta[i] = new TH1D(Form("hisDelta%d",i ),Form("hisDelta%d",i),100,-10,10); } for( int ievent = 0; ievent < trin->GetEntries(); ievent++){ trin->GetEntry(ievent); for( int idigi = 0; idigi < nCSIDigi ; idigi++){ hisDelta[ CSIDigiID[ idigi ] ]->Fill( CSIDigiDeltaT1[ idigi ] ); hisDeltaChannel->Fill( CSIDigiID[ idigi ] , CSIDigiDeltaT1[ idigi ] ); //std::cout << CSIDigiID[ idigi ] << std::endl; } } for( int i = 0; i< 2716; i++){ //std::cout << hisDelta[i]->GetEntries() << std::endl; if( hisDelta[i]->GetEntries() > 10){ int rst = hisDelta[i]->Fit("gaus","Q","",hisDelta[i]->GetBinCenter( hisDelta[i]->GetMaximumBin() ) - 3, hisDelta[i]->GetBinCenter( hisDelta[i]->GetMaximumBin() )+3); TF1* func = NULL; func = hisDelta[i]->GetFunction("gaus"); if( func != NULL ){ grDelta->SetPoint( grDelta->GetN(), i, func->GetParameter(1)); grDelta->SetPointError( grDelta->GetN()-1, 0, func->GetParError(2)); grRES->SetPoint( grRES->GetN() , i , func->GetParameter(2)); } } /* hisDelta[i]->Draw(); can->Modified(); can->Update(); getchar(); */ hisDelta[ i ] ->Write(); } std::ofstream ofs("TimeResolutionCosmic3.dat"); int ID[2716]; double Delta[2716]; double Resolution[2716]; for( int i = 0; i< 2716; i++){ Resolution[i] = 0xFFFF; Delta[i] = 0xFFFF; } for( int i = 0; i< grRES->GetN(); i++){ Delta[(int)(grDelta->GetX()[i])] = grDelta->GetY()[i]; Resolution[(int)(grRES->GetX()[i])] = grRES->GetY()[i] ; } for( int i = 0; i< 2716; i++){ ofs << i << "\t" << Delta[i] << "\t" << Resolution[i] << "\n"; } grDelta->SetNameTitle("grDelta","grDelta"); grRES->SetNameTitle("grRES","grRES"); grDelta->Write(); grRES->Write(); hisDeltaChannel->Write(); tfout->Close(); ofs.close(); }