void superimposeHistos() { TFile* bFile = TFile::Open("Electron_In_Jets_900GeV_bJets.root"); TFile* cFile = TFile::Open("Electron_In_Jets_900GeV_cJets.root"); TFile* udsgFile = TFile::Open("Electron_In_Jets_900GeV_udsgJets.root"); TIter next(bFile->GetListOfKeys()); TFile* newFile = new TFile("testFile.root", "RECREATE"); while(TKey* key = (TKey*)next()) { TH1F* bHist = (TH1F*)bFile->Get(key->GetName()); bHist->SetFillColor(2); TH1F* cHist = (TH1F*)cFile->Get(key->GetName()); cHist->SetFillColor(3); TH1F* udsgHist = (TH1F*)udsgFile->Get(key->GetName()); udsgHist->SetFillColor(4); THStack* stack = new THStack(bHist->GetName(), bHist->GetTitle()); stack->Add(udsgHist, "hist ]["); stack->Add(cHist, "hist ]["); stack->Add(bHist, "hist ]["); TLegend* legend = new TLegend(0.5, 0.68, 0.88, 0.88); legend->AddEntry(bHist, "b-Jets"); legend->AddEntry(cHist, "c-Jets"); legend->AddEntry(udsgHist, "udsg-Jets"); TCanvas* canvas = new TCanvas(bHist->GetName()); stack->Draw(); stack->GetXaxis()->SetTitle(bHist->GetXaxis()->GetTitle()); legend->Draw(); canvas->Write(canvas->GetName()); } newFile->Close(); bFile->Close(); cFile->Close(); udsgFile->Close(); }
// Called just after the main event loop // Can be used to write things out, dump a summary etc // Return non-zero to indicate a problem int IslandAmplitude::AfterLastEntry(TGlobalData* gData,const TSetupData *setup){ // Print extra info if we're debugging this module: if(Debug()){ cout<<"-----IslandAmplitude::AfterLastEntry(): I'm debugging!"<<endl; } double run_norm = fAmpNorm->Integral(0,-1); for(mapSH_t::iterator it = fAmpHist.begin(); it != fAmpHist.end(); ++it) { TH1F* h = it->second; TObject* obj = h->Clone((std::string(h->GetName()) + "_RunNorm").c_str()); TH1* hn = static_cast<TH1*>(obj); hn->SetTitle((std::string(h->GetTitle()) + " (run normalized)").c_str()); hn->Scale(1.0/run_norm); } for(mapSH_t::iterator it = fAmpHistNorm.begin(); it != fAmpHistNorm.end(); ++it) it->second->Scale(1.0/fNProcessed); return 0; }
void plottingmacro_IVF() { double fa = 0.46502; double fb = 0.53498; bool debug_ = true; // std::string path("Nov10thFall11Plots/"); // std::string path("Nov10Fall1160MTopSlimPlots/"); std::string path("Nov10Fall1160MTopIVFPlots_b/"); if(debug_) std::cout << "Init the style form setTDRStyle" << std::endl; setTDRStyle(); gStyle->SetErrorX(0.5); gROOT->ForceStyle(); initOptions(); if(debug_) std::cout << "Init the sample" << std::endl; // std::vector<Sample> s = Nov10thDiJetPtUpdatedSlimHistos(); //std::vector<Sample> s = Nov10Fall1160MTopSlimHistos(); std::vector<Sample> s = Nov10Fall1160MTopIVFHistos(); Sample data(1,"fake data","S1.root",0,true,1000); if(debug_) std::cout << "Init the data sample" << std::endl; for(size_t i=0;i< s.size();i++) if(s[i].data) {data=s[i];break;} if(debug_) std::cout << "Ls data sample" << std::endl; data.file()->ls(); if(debug_) std::cout << "Init the mc sample" << std::endl; for(size_t i=0;i< s.size();i++) s[i].dump(1,fa,fb); std::vector<std::string> names; if(debug_) std::cout << "Get List of Keys" << std::endl; TList * subs = data.file()->GetListOfKeys(); for(size_t i=0;i< subs->GetSize();i++) { TString nn = subs->At(i)->GetName(); if( nn.Contains(TRegexp("Count*")) ) continue; if(debug_) std::cout << "Get List of Keys in subdirs" << std::endl; TList * objs = ((TDirectoryFile *)data.file()->Get(subs->At(i)->GetName()))->GetListOfKeys(); for(size_t j=0;j< objs->GetSize();j++) { if(debug_) std::cout << "Name = " << subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName() << std::endl; names.push_back(subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName()); // std::cout << subs->At(i)->GetName() << "/" << objs->At(j)->GetName() << std::endl; //TODO: select plots via regexp } } if(debug_) std::cout << "Starting plotting" << std::endl; std::string process; for(size_t i = 0 ; i < names.size() ; i++) { std::map<std::string,TH1F *> grouped; TString n=names[i]; // if(!n.Contains(TRegexp("VlightRegionHZee/HiggsPtVlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("VlightRegionHZee/ZPtVlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("VlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZmmSV"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZeeSV"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZcombSV"))) continue; // if(!n.Contains(TRegexp("ZSVPureRegionZcombSV"))) continue; // if(!n.Contains(TRegexp("ZSVTTbarPureRegionZcombSV"))) continue; if(!n.Contains(TRegexp("TTbarRegionZeeSVJets"))) continue; if(n.Contains(TRegexp("RegionHZcomb"))) process = "Z(l^{+}l^{-})H(b#bar{b})"; if(n.Contains(TRegexp("RegionHZmm"))) process = "Z(#mu^{+}#mu^{-})H(b#bar{b})"; if(n.Contains(TRegexp("RegionHZee"))) process = "Z(e^{+}e^{-})H(b#bar{b})"; if(debug_) std::cout << "Creating the Canvas" << std::endl; TCanvas *c = new TCanvas(); c->SetLogy(false); c->SetTitle(names[i].c_str()); if(debug_) std::cout << "Creating histograms" << std::endl; TH1F *hd = ((TH1F*)data.file()->Get(names[i].c_str())); hd->Sumw2(); Options o=options[names[i]]; // hd->Rebin(o.rebin); hd->SetMarkerStyle(20); hd->GetXaxis()->SetLabelOffset(99); hd->SetYTitle(o.yaxis.c_str()); double nbin = hd->GetNbinsX(); double min_bin = hd->GetXaxis()->GetXmin(); double max_bin = hd->GetXaxis()->GetXmax(); TH1F *hmc = new TH1F("hmc","hmc", nbin, min_bin, max_bin); hmc->SetFillColor(kWhite); hmc->Sumw2(); // hmc->Rebin(o.rebin); if(debug_) std::cout << "Creating the THStack and Legend" << std::endl; THStack * sta = new THStack("sta",hd->GetTitle()); TLegend * l = new TLegend(o.legendx1,o.legendy1,o.legendx2,o.legendy2); //0.7,0.1,0.9,0.6); l->SetFillColor(kWhite); l->SetBorderSize(0); l->SetTextFont(62); l->SetTextSize(0.03); if(debug_) std::cout << "Adding data to the legend" << std::endl; l->AddEntry(hd, "Data","P"); if(debug_) std::cout << "Adding MC to the THStack" << std::endl; //with the proper trigger eff // double SF[] = {1.01,1.03,1.00}; // double SF[] = {1.03,1.054,1.032}; double SF[] = {1.0,1.0,1.0}; if(debug_){ for(int i = 0; i< 3; ++i) std::cout << "SF [" << i << "] = " << SF[i] << std::endl; } double mcIntegral=0; for(size_t j=0;j< s.size() ;j++) { if(!s[j].data) { if(debug_) std::cout << "Creating TH1F from file " << s[j].name << std::endl; TH1F * h = ((TH1F*)s[j].file()->Get(names[i].c_str())); h->Sumw2(); if(debug_){ std::cout << "TH1F created from file " << s[j].name << std::endl; std::cout << "Scaling : " << s[j].scale(data.lumi(),fa,fb) << std::endl; std::cout << "Scaling with SF : " << s[j].scale(data.lumi(),fa,fb,SF) << std::endl; std::cout << "Histo integral before scaling = " << h->Integral() << std::endl; } h->Scale(s[j].scale(data.lumi(),fa,fb,SF)); if(debug_){ std::cout << "Histo integral after scaling = " << h->Integral() << std::endl; std::cout << "Managing style... " << std::endl; } h->SetLineWidth(1.); h->SetFillColor(s[j].color); h->SetLineColor(s[j].color); // h->Rebin(options[names[i]].rebin); if(debug_) std::cout << "Cloning and update legend " << std::endl; if(grouped.find(s[j].name) == grouped.end()){ l->AddEntry(h,s[j].name.c_str(),"F"); } std::cout << "Sample : " << s[j].name << " - Integral for plot " << names[i] << " = " << h->Integral(-10000,10000) << std::endl; mcIntegral += h->Integral(); sta->Add(h); hmc->Add(h); //TO FIX grouped map // sovrascrive histo con lo stesso nome tipo VV o ST etc... grouped[s[j].name]=(TH1F *)h->Clone(("_"+names[i]).c_str()); } } if(debug_){ std::cout << "Data total = " << hd->Integral() << std::endl; std::cout << "MC = " << mcIntegral << std::endl; std::cout << "Data/MC = " << hd->Integral()/mcIntegral << std::endl; } TPad * TopPad = new TPad("TopPad","Top Pad",0.,0.3,1.,1. ) ; TPad * BtmPad = new TPad("BtmPad","Bottom Pad",0.,0.,1.,0.313 ) ; TopPad->SetBottomMargin(0.02); BtmPad->SetTopMargin(0.0); BtmPad->SetFillStyle(4000); TopPad->SetFillStyle(4000); BtmPad->SetFillColor(0); BtmPad->SetBottomMargin(0.35); TopPad->Draw() ; BtmPad->Draw() ; std::cout << "hd maximum = " << hd->GetMaximum() << " sta maximum = " << sta->GetMaximum() << std::endl; double maxY; if(hd->GetMaximum() > sta->GetMaximum()) maxY = (hd->GetMaximum())*1.5; else maxY = (sta->GetMaximum())*1.5; TopPad->cd(); hd->Draw("E1X0"); sta->Draw("sameHIST"); hmc->Draw("sameE2"); hmc->SetFillColor(2); hmc->SetMarkerSize(0); hmc->SetFillStyle(3013); hd->Draw("E1X0same"); l->Draw("same"); std::cout << "Set Maximum to = " << maxY << std::endl; hd->GetYaxis()->SetRangeUser(0.,maxY); hd->GetXaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); BtmPad->cd(); std::cout << "Division" << std::endl; TH1D * divisionErrorBand = (TH1D*)(hmc)->Clone("divisionErrorBand"); divisionErrorBand->Sumw2(); divisionErrorBand->Divide(hmc); divisionErrorBand->Draw("E2"); divisionErrorBand->SetMaximum(2.49); divisionErrorBand->SetMinimum(0); divisionErrorBand->SetMarkerStyle(20); divisionErrorBand->SetMarkerSize(0.55); divisionErrorBand->GetXaxis()->SetTitleOffset(1.12); divisionErrorBand->GetXaxis()->SetLabelSize(0.12); divisionErrorBand->GetXaxis()->SetTitleSize(0.5); divisionErrorBand->GetYaxis()->SetTitle("Data/MC"); divisionErrorBand->GetYaxis()->SetLabelSize(0.12); divisionErrorBand->GetYaxis()->SetTitleSize(0.12); divisionErrorBand->GetYaxis()->SetTitleOffset(0.40); divisionErrorBand->GetYaxis()->SetNdivisions(505); //divisionErrorBand->UseCurrentStyle(); divisionErrorBand->SetFillColor(2); divisionErrorBand->SetFillStyle(3001); divisionErrorBand->SetMarkerSize(0.); TH1D * division = (TH1D*)(hd)->Clone("division"); division->Sumw2(); division->Divide(hmc); // division->SetMaximum(2.5); // division->SetMinimum(0); // division->SetMarkerStyle(20); // division->SetMarkerSize(0.55); // division->GetXaxis()->SetLabelSize(0.12); // division->GetXaxis()->SetTitleSize(0.14); // division->GetYaxis()->SetLabelSize(0.10); // division->GetYaxis()->SetTitleSize(0.10); // division->GetYaxis()->SetTitle("Data/MC"); Double_t min = division->GetXaxis()->GetXmin(); Double_t max = division->GetXaxis()->GetXmax(); division->Draw("E1X0same"); TLine *line = new TLine(min, 1.0, max, 1.0); line->SetLineColor(kRed); line->Draw("same"); TLegend * leg3 =new TLegend(0.50,0.86,0.69,0.96); leg3->AddEntry(divisionErrorBand,"MC uncert. (stat.)","f"); leg3->SetFillColor(0); leg3->SetLineColor(0); leg3->SetShadowColor(0); leg3->SetTextFont(62); leg3->SetTextSize(0.06); leg3->Draw(); TPaveText *pave = new TPaveText(0.15,0.85,0.32,0.96,"brNDC"); pave->SetTextAlign(12); pave->SetLineColor(0); pave->SetFillColor(0); pave->SetShadowColor(0); //TText *text = pave->AddText(Form("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f",histDt->Chi2Test(histCopyMC5,"UWCHI2/NDF"),histDt->KolmogorovTest(histCopyMC5))); // stat + sys TText *text = pave->AddText(Form("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f",hd->Chi2Test(hmc,"UWCHI2/NDF"),hd->KolmogorovTest(hmc))); // stat only text->SetTextFont(62); text->SetTextSize(0.08); pave->Draw(); TopPad->cd(); TLatex latex; latex.SetNDC(); latex.SetTextAlign(12); latex.SetTextSize(0.052); latex.DrawLatex(0.17,0.89,"CMS Preliminary"); latex.SetTextSize(0.04); latex.DrawLatex(0.17,0.84,"#sqrt{s} = 7 TeV, L = 4.7 fb^{-1}"); // latex.DrawLatex(0.17,0.79,"Z(e^{+}e^{-})H(b#bar{b})"); latex.DrawLatex(0.17,0.79,process.c_str()); c->Update(); std::string cName= hd->GetName(); cName += "_bare.pdf"; cName = path+cName; c->Print(cName.c_str(),"pdf"); // std::cout << names[i] << " d: " << hd->Integral() << " "; // THStack * sta2 = new THStack("sta2",hd->GetTitle()); // float tot=0; // float toterr2=0; // if(debug_) // std::cout << "Putting the iterator in the for loop" << std::endl; // for(std::map<std::string,TH1F *>::reverse_iterator it=grouped.rbegin(); it!=grouped.rend();++it) // { // if(debug_) // std::cout << "Using the iterator" << std::endl; // std::cout << (*it).first << " " << (*it).second->Integral() << " | " << std::endl ; // if((*it).second->GetEntries() > 0) { // float er=1.*sqrt((*it).second->GetEntries())/(*it).second->GetEntries()*(*it).second->Integral(); // toterr2+=er*er; // } // tot+=(*it).second->Integral(); // sta2->Add(it->second); // } // std::cout << " Tot: " << tot << "+-" << sqrt(toterr2) << " SF: " << hd->Integral()/tot << std::endl; // TCanvas *c2 = new TCanvas(); // c2->SetTitle(names[i].c_str()); // std::cout << "hd maximum = " << hd->GetMaximum() << " sta2 maximum = " << sta2->GetMaximum() << std::endl; // if(hd->GetMaximum() > sta2->GetMaximum()) maxY = hd->GetBinContent(hd->GetMaximumBin()) * 1.5; // else maxY = ( sta2->GetMaximum())*1.5; // // hd->Draw("E1"); // sta2->Draw("PADSHIST"); // // hd->Draw("E1same"); // // l->Draw("same"); // std::cout << "Set Maximum to = " << maxY << std::endl; // hd->GetYaxis()->SetRangeUser(0.,maxY); // hd->GetXaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); // c2->Update(); // std::string c2Name = hd->GetName(); // c2Name = path+c2Name; // c2Name += "_norm.pdf"; // c2->Print(c2Name.c_str(),"pdf"); } }
void changeAxis(bool armed, TString iExprMatch,float iMin, const char* filename, const char* pattern="", unsigned int debug=0) { std::vector<std::string> histnames; histnames.clear(); if( debug>0 ){ std::cout << "file = " << filename << std::endl; std::cout << "old = " << iExprMatch.Data() << std::endl; std::cout << "armed = " << armed << std::endl; } TFile* file = new TFile(filename, "update"); TIter nextDirectory(file->GetListOfKeys()); std::vector<std::string> buffer; TKey* idir; while((idir = (TKey*)nextDirectory())){ buffer.clear(); if( idir->IsFolder() ){ file->cd(); // make sure to start in directory head if( debug>0 ){ std::cout << "Found directory: " << idir->GetName() << std::endl; } if( file->GetDirectory(idir->GetName()) ){ file->cd(idir->GetName()); // change to sub-directory buffer = signalList(idir->GetName(), pattern,iExprMatch.Data(), debug); } // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for directory " << idir->GetName() << std::endl; } } } // pick up files which are not kept in an extra folder file->cd(); buffer.clear(); buffer = signalList("", pattern,iExprMatch.Data(), debug); // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for file head" << std::endl; } for(std::vector<std::string>::const_iterator hist=histnames.begin(); hist!=histnames.end(); ++hist){ file->cd(); TH1F* h = (TH1F*)file->Get(hist->c_str()); std::string histName; if(hist->find("/")!=std::string::npos){ histName = hist->substr(hist->find("/")+1); } else{ histName = *hist; } TH1F* hout = (TH1F*)h->Clone(histName.c_str()); if(debug>1){ std::cout << "...folder : " << hist->substr(0, hist->find("/")).c_str() << std::endl; std::cout << "...histogram : " << hout->GetName () << " / " << hist->c_str() << std::endl; } for(int i0 = 0; i0 < hout->GetNbinsX()+1; i0++) if(hout->GetXaxis()->GetBinCenter(i0) < iMin) hout->SetBinContent(i0,0); if(debug>1){ std::cout << "...new name : " << hout->GetName() << std::endl; std::cout << "...new title : " << hout->GetTitle() << std::endl; } if(armed){ if(hist->find("/")!=std::string::npos){ file->cd(hist->substr(0, hist->find("/")).c_str()); } else{ file->cd(); } std::cout << "writing to file: " << hout->GetName() << " -- " << hist->substr(hist->find("/")+1).c_str() << std::endl; hout->Write(hist->substr(hist->find("/")+1).c_str(), TObject::kOverwrite); } } file->Close(); return; }
void plotTree(TTree *tree_, std::string whichfit, std::string selectString){ // Create a map for plotting the pullsummaries: std::map < const char*, std::pair <double,double> > pullSummaryMap; int nPulls=0; TObjArray *l_branches = tree_->GetListOfBranches(); int nBranches = l_branches->GetEntries(); TCanvas *c = new TCanvas("c","",960,800); std::string treename = tree_->GetName(); c->SaveAs(Form("%s.pdf[",treename.c_str())); for (int iobj=0;iobj<nBranches;iobj++){ TBranch *br =(TBranch*) l_branches->At(iobj); // Draw the normal histogram const char* name = br->GetName(); bool fitPull=false; bool plotLH=false; TGraph *gr=0; double p_mean =0; double p_err =0; int nToysInTree = tree_->GetEntries(); if (doPull && findNuisancePre(name)){ p_mean = bfvals_[name].first; // toy constrainits thrown about best fit to data p_err = prevals_[name].second; // uncertainties taken from card const char* drawInput = Form("(%s-%f)/%f",name,p_mean,p_err); tree_->Draw(Form("%s>>%s",drawInput,name),""); tree_->Draw(Form("%s>>%s_fail",drawInput,name),selectString.c_str(),"same"); fitPull = true; if (doLH) { gr = graphLH(name,p_err,whichfit); plotLH=true; } } else{ tree_->Draw(Form("%s>>%s",name,name),""); tree_->Draw(Form("%s>>%s_fail",name,name),"mu<0","same"); } TH1F* bH = (TH1F*) gROOT->FindObject(Form("%s",name))->Clone(); TH1F* bHf = (TH1F*) gROOT->FindObject(Form("%s_fail",name))->Clone(); bHf->SetLineColor(2); bH->GetXaxis()->SetTitle(bH->GetTitle()); bH->GetYaxis()->SetTitle(Form("no toys (%d total)",nToysInTree)); bH->GetYaxis()->SetTitleOffset(1.32); bH->SetTitle(""); if (fitPull) bH->Fit("gaus"); c->Clear(); TPad pad1("t1","",0.01,0.02,0.59,0.98); TPad pad2("t2","",0.59,0.04,0.98,0.62); TPad pad3("t3","",0.59,0.64,0.98,0.90); pad1.SetNumber(1); pad2.SetNumber(2); pad3.SetNumber(3); pad1.Draw(); pad2.Draw();pad3.Draw(); pad2.SetGrid(true); c->cd(1); bH->Draw(); bHf->Draw("same"); TLatex *titletext = new TLatex();titletext->SetNDC();titletext->SetTextSize(0.04); titletext->DrawLatex(0.1,0.95,name); TLegend *legend = new TLegend(0.6,0.8,0.9,0.89); legend->SetFillColor(0); legend->AddEntry(bH,"All Toys","L"); legend->AddEntry(bHf,selectString.c_str(),"L"); legend->Draw(); if (doPull && plotLH) { c->cd(2); gr->Draw("ALP"); } if (fitPull){ c->cd(3); TLatex *tlatex = new TLatex(); tlatex->SetNDC(); tlatex->SetTextSize(0.12); tlatex->DrawLatex(0.15,0.75,Form("Mean : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParError(1))); tlatex->DrawLatex(0.15,0.60,Form("Sigma : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(2),bH->GetFunction("gaus")->GetParError(2))); tlatex->DrawLatex(0.15,0.35,Form("Pre-fit : %.3f ",prevals_[name].first)); tlatex->DrawLatex(0.15,0.2,Form("Best-fit (B) : %.3f ",p_mean)); tlatex->DrawLatex(0.15,0.05,Form("Best-fit (S+B): %.3f ",bfvals_sb_[name].first)); pullSummaryMap[name]=std::make_pair<double,double>(bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParameter(2)); nPulls++; } c->SaveAs(Form("%s.pdf",treename.c_str())); } if (doPull && nPulls>0){ int nRemainingPulls = nPulls; TCanvas *hc = new TCanvas("hc","",3000,2000); hc->SetGrid(0); std::map < const char*, std::pair <double,double> >::iterator pull_it = pullSummaryMap.begin(); std::map < const char*, std::pair <double,double> >::iterator pull_end = pullSummaryMap.end(); while (nRemainingPulls > 0){ int nThisPulls = min(15,nRemainingPulls); TH1F pullSummaryHist("pullSummary","",nThisPulls,0,nThisPulls); for (int pi=1;pull_it!=pull_end && pi<=nThisPulls ;pull_it++,pi++){ pullSummaryHist.GetXaxis()->SetBinLabel(pi,(*pull_it).first); pullSummaryHist.SetBinContent(pi,((*pull_it).second).first); pullSummaryHist.SetBinError(pi,((*pull_it).second).second); nRemainingPulls--; } pullSummaryHist.SetMarkerStyle(21);pullSummaryHist.SetMarkerSize(1.5);pullSummaryHist.SetMarkerColor(2);pullSummaryHist.SetLabelSize(0.018); pullSummaryHist.GetYaxis()->SetRangeUser(-3,3);pullSummaryHist.GetYaxis()->SetTitle("pull summary");pullSummaryHist.Draw("E1"); hc->SaveAs(Form("%s.pdf",treename.c_str())); } delete hc; } c->SaveAs(Form("%s.pdf]",treename.c_str())); delete c; return; }
void plottingmacro() { setTDRStyle(); gROOT->ForceStyle(); initOptions(); std::vector<Sample> s = samples(); Sample data(1,"fake data","S1.root",0,true,1000); for(size_t i=0;i< s.size();i++) if(s[i].data) {data=s[i];break;} data.file()->ls(); for(size_t i=0;i< s.size();i++) s[i].dump(data.lumi()); std::vector<std::string> names; TList * subs = data.file()->GetListOfKeys(); for(size_t i=0;i< subs->GetSize();i++) { TList * objs = ((TDirectoryFile *) data.file()->Get(subs->At(i)->GetName()))->GetListOfKeys(); for(size_t j=0;j< objs->GetSize();j++) { names.push_back(subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName()); // std::cout << subs->At(i)->GetName() << "/" << objs->At(j)->GetName() << std::endl; //TODO: select plots via regexp } } for(size_t i = 0 ; i < names.size() ; i++) { std::map<std::string,TH1F *> grouped; TString n=names[i]; if(!n.Contains(TRegexp("V.*RegionH.*mu.*HiggsMass"))) continue; TCanvas *c = new TCanvas(); c->SetLogy(true); c->SetTitle(names[i].c_str()); TH1F *hd = ((TH1F*)data.file()->Get(names[i].c_str())); Options o=options[names[i]]; hd->Rebin(o.rebin); hd->SetMarkerStyle(21); hd->Draw("E1"); hd->SetYTitle(o.yaxis.c_str()); THStack * sta = new THStack("sta",hd->GetTitle()); TLegend * l = new TLegend(o.legendx1,o.legendy1,o.legendx2,o.legendy2); //0.7,0.1,0.9,0.6); l->AddEntry(hd, "Data","LP"); for(size_t j=0;j< s.size() ;j++) { if(!s[j].data) { TH1F * h = ((TH1F*)s[j].file()->Get(names[i].c_str())); h->Scale(s[j].scale(data.lumi())); h->SetLineColor(s[j].color); h->SetFillColor(s[j].color); h->Rebin(options[names[i]].rebin); if(grouped.find(s[j].name)==grouped.end()) { grouped[s[j].name]=(TH1F *)h->Clone(("_"+names[i]).c_str()); l->AddEntry(h,s[j].name.c_str(),"F"); } else { grouped[s[j].name]->Add(h); } sta->Add(h); // h->Draw("same"); } } sta->Draw("same"); hd->Draw("E1same"); hd->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); l->Draw(); std::cout << names[i] << " d: " << hd->Integral() << " "; THStack * sta2 = new THStack("sta2",hd->GetTitle()); float tot=0; float toterr2=0; for(std::map<std::string,TH1F *>::iterator it = grouped.begin(); it != grouped.end();it++) { std::cout << it->first << " " << it->second->Integral() << " | " ; if(it->second->GetEntries() > 0) { float er=1.*sqrt(it->second->GetEntries())/it->second->GetEntries()*it->second->Integral(); toterr2+=er*er; } tot+=it->second->Integral(); sta2->Add(it->second); } std::cout << " Tot: " << tot << "+-" << sqrt(toterr2) << " SF: " << hd->Integral()/tot << std::endl; c = new TCanvas(); sta2->Draw(); hd->Draw("E1,same"); sta2->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); hd->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); l->Draw(); } }
void vs_PlotQCDcomp() { Bool_t saveC = false; Bool_t diJets = true; Bool_t isMC = false; TString vsSave; vsSave = "_T07w"; TString jSave; if(diJets) jSave = "2j"; else jSave = "3j"; TString sample; if(diJets) sample = "select_1ph_2jets"; else sample = "select_1ph_3jets"; TString FOtag; if(isMC) FOtag = "_FO_CorrMC"; // else FOtag = "_FO_Corr"; else FOtag = "_FO"; TString sysu = "SYSTUP_"; TString sysd = "SYSTDOWN_"; TString outDir; outDir = "Plots_PhotonSusyAnalysis/QCDweights/"; // string inputFile1 = "/data/user/vsola/CMSSW_Releases/CMSSW_5_3_9/src/Plots_PhotonSusyAnalysis/Merged_QCD_PhotonJet_T07w_PAT/mergedHistos.root"; // string inputFile1 = "/data/user/vsola/CMSSW_Releases/CMSSW_5_3_9/src/Plots_PhotonSusyAnalysis/Merged_Data_V05w_PAT/mergedHistos.root"; string inputFile1 = "/data/user/vsola/CMSSW_Releases/CMSSW_5_3_9/src/Plots_PhotonSusyAnalysis/PhotonHadReReco_22Jan2013_V05_PAT/mergedHistos.root"; setMyTDRStyle(); gROOT->SetStyle("mytdrStyle"); // gStyle->SetHistMinimumZero(); // gStyle->SetPaintTextFormat("4.2f"); gStyle->SetHistLineWidth(2); gStyle->UseCurrentStyle(); // gStyle->SetPadTopMargin(1.0); // gStyle->SetPadLeftMargin(3.2); // gStyle->SetPadRightMargin(4.5); // gStyle->SetPadBottomMargin(3.2); gROOT->ForceStyle(1); static const Int_t nHt = 7; static const Int_t nPt = 13; static const Int_t nMet = 16; static const Int_t nJet = 15; Double_t binPt[nPt+1] = {0.,75.,90.,120.,160.,210.,260.,320.,400.,500.,650.,800.,1000.,1500.}; Double_t binHt[nHt+1] = {0.,400.,450.,550.,700.,900.,1200.,1500.}; Double_t binMet[nMet+1] = {0.,10.,20.,30.,40.,50.,60.,70.,80.,90.,100.,120.,160.,200.,270.,350.,500.}; Double_t binJet[nJet+1] = {0.,1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15}; TLatex *as = new TLatex(); as->SetNDC(true); as->SetTextColor(12); as->SetTextFont(43); as->SetTextSize(19); TLegend *legend = new TLegend(0.60, 0.70, 0.75, 0.85, ""); legend->SetFillColor(10); legend->SetFillStyle(1001); legend->SetTextSize(0.04); legend->SetBorderSize(0); legend->SetShadowColor(0); std::string outLumi = "CMS Work in Progress - QCD MC #sqrt{s} = 8 TeV - #geq 1 #gamma, #geq 2 j"; TFile* f1 = TFile::Open( inputFile1.c_str() ); //photon Pt TCanvas * cPt = new TCanvas("cPt","cPt"); TH1F* hpt = (TH1F*) f1->Get( sample+"/PreselCut_photonPt" ); TH1F* hptFO = (TH1F*) f1->Get( sample+FOtag+"/PreselCut_photonPt" ); TH1F *hptR = (TH1F*) hpt->Rebin(nPt,hpt->GetTitle(),binPt); if (hptR->GetSumw2N() == 0) hptR->Sumw2(); TH1F *hptFOR = (TH1F*) hptFO->Rebin(nPt,hptFO->GetTitle(),binPt); if (hptFOR->GetSumw2N() == 0) hptFOR->Sumw2(); TH1F* hptSU = (TH1F*) f1->Get( sample+FOtag+"/PreselCut_"+sysu+"photonPt" ); TH1F* hptSD = (TH1F*) f1->Get( sample+FOtag+"/PreselCut_"+sysd+"photonPt" ); TH1F *hptSUR = (TH1F*) hptSU->Rebin(nPt,hptSU->GetTitle(),binPt); if (hptSUR->GetSumw2N() == 0) hptSUR->Sumw2(); TH1F *hptSDR = (TH1F*) hptSD->Rebin(nPt,hptSD->GetTitle(),binPt); if (hptSDR->GetSumw2N() == 0) hptSDR->Sumw2(); if ( hptFOR != 0 ) { for (int b = 0; b < hptFOR->GetNbinsX(); b++) { Double_t mainHistoContent = hptFOR->GetBinContent(b); Double_t systUpHistoContent = hptSUR->GetBinContent(b); Double_t systDnHistoContent = hptSDR->GetBinContent(b); Double_t systDiffUp = fabs( (double) systUpHistoContent - mainHistoContent ); Double_t systDiffDn = fabs( (double) mainHistoContent - systDnHistoContent ); // use average error for histogram Double_t systDiff = ( systDiffUp + systDiffDn ) / 2.; Double_t statErr = hptFOR->GetBinError(b); Double_t combError = sqrt( systDiff * systDiff + statErr * statErr ); hptFOR->SetBinError(b, combError); } //for }//if cPt->SetLogy(1); gPad->Update(); cPt->Update(); // hptR->Scale(1,"width"); // hptR->SetMinimum(0.02); // hptR->SetMaximum(1000); hptR->GetXaxis()->SetTitle("1^{st} photon p_{T} [GeV]"); hptR->GetYaxis()->SetTitle("Number of Events / GeV"); if(isMC) hptR->SetMarkerSize(0); if(isMC) hptR->Draw("histE"); else hptR->Draw("E X0"); hptFOR->SetMarkerSize(0); hptFOR->SetLineColor(2); hptFOR->SetFillColor(2); hptFOR->SetFillStyle(3004); hptFOR->Draw("same hist ][ E2"); legend->Clear(); if(isMC) legend->SetHeader("#gamma/QCD (Sim)"); else legend->SetHeader("#gamma/QCD (Data)"); if(isMC) legend->AddEntry(hptR, "#gamma", "l"); else legend->AddEntry(hptR, "#gamma", "p"); legend->AddEntry(hptFOR, "Pred (from #gamma_{jet})", "f"); legend->Draw(); as->DrawLatex(0.17, 0.93, outLumi.c_str() ); cPt->Update(); cPt->SetBottomMargin(0.2 + 0.8 * cPt->GetBottomMargin() - 0.2 * cPt->GetTopMargin()); TPad *ratioPt = new TPad("BottomPad", "", 0, 0, 1, 1); ratioPt->SetTopMargin(0.8 - 0.8 * ratioPt->GetBottomMargin() + 0.2 * ratioPt->GetTopMargin()); ratioPt->SetFillStyle(0); ratioPt->SetFrameFillColor(10); ratioPt->SetFrameBorderMode(0); ratioPt->Draw(); ratioPt->cd(); ratioPt->SetLogy(0); TH1F *hptRat = (TH1F*) divideHistosForRatio(hptR,hptFOR); hptRat->SetMinimum(0.); hptRat->SetMaximum(10.); hptRat->GetXaxis()->SetNdivisions(505); if(isMC) hptRat->GetYaxis()->SetTitle("Sim/Pred"); else hptRat->GetYaxis()->SetTitle("Data/Pred"); hptRat->GetYaxis()->SetTitleSize(0.04); hptRat->GetYaxis()->SetLabelSize(0.03); hptRat->GetYaxis()->SetTitleOffset(1.3); hptRat->GetYaxis()->SetNdivisions(505); hptRat->SetMarkerStyle(20); hptRat->SetMarkerSize(1); hptRat->SetMarkerColor(1); hptRat->SetLineColor(1); hptRat->Draw("E X0"); TH1F *hptFRat = (TH1F*) getSystErrForRatio(hptFOR); hptFRat->SetLineColor(2); hptFRat->SetFillColor(2); hptFRat->SetFillStyle(3004); hptFRat->Draw("same hist ][ E2"); TLine * line = new TLine( hptRat->GetXaxis()->GetXmin(), 1., hptRat->GetXaxis()->GetXmax(), 1. ); line->SetLineColor(1); line->SetLineWidth(0.5); line->SetLineStyle(1); line->Draw("same"); hptR->GetXaxis()->SetLabelSize(0); hptR->GetXaxis()->SetTitle(""); cPt->RedrawAxis(); gPad->Update(); cPt->Update(); return; }
//================================================ void make_histo_pt() { const int nPtBins = nPtBins_pt; const double* ptBinsLow = ptBins_low_pt; const double* ptBinsHigh = ptBins_high_pt; const char** ptName = pt_Name_pt; const int nCentBins = nCentBins_pt; const int* centBinsLow = centBins_low_pt; const int* centBinsHigh = centBins_high_pt; const char** centName = cent_Name_pt; const char *hName[3] = {"hJpsiInfo","hBkgLSPos","hBkgLSNeg"}; THnSparseF *hnInvMass[2][3] = {0x0}; TH1F *hInvMass[2][5][nCentBins][nPtBins][3] = {0x0}; // same event char name[512]; for(int w=0; w<2; w++) // event weights { for(Int_t j=0; j<3; j++) // pair type { if(w==0) sprintf(name,"m%s_%s",hName[j],trigName[kTrigType]); else sprintf(name,"m%sWeight_%s",hName[j],trigName[kTrigType]); hnInvMass[w][j] = (THnSparseF*)f->Get(name); if(!hnInvMass[w][j]) continue; hnInvMass[w][j]->GetAxis(2)->SetRangeUser(pt1_cut+0.01,100); hnInvMass[w][j]->GetAxis(3)->SetRangeUser(pt2_cut+0.01,100); for(Int_t i=0; i<nPtBins; i++) // pt bins { hnInvMass[w][j]->GetAxis(1)->SetRangeUser(ptBinsLow[i]+0.01,ptBinsHigh[i]-0.01); for(int k=0; k<nCentBins; k++) // centrality bins { hnInvMass[w][j]->GetAxis(4)->SetRange(centBinsLow[k],centBinsHigh[k]); for(int t=0; t<gNTrgSetup; t++) // trigger setup { if(t>0) hnInvMass[w][j]->GetAxis(5)->SetRange(t,t); hInvMass[w][t][k][i][j] = (TH1F*)hnInvMass[w][j]->Projection(0); hInvMass[w][t][k][i][j]->SetName(Form("%d_%s_%s_InvMass_jpsi_PtBin%d_CentBin%d_P%d",w,hName[j],trigName[kTrigType],i,k,t)); hInvMass[w][t][k][i][j]->Sumw2(); hnInvMass[w][j]->GetAxis(5)->SetRange(0,-1); } hnInvMass[w][j]->GetAxis(4)->SetRange(0,-1); } hnInvMass[w][j]->GetAxis(1)->SetRange(0,-1); } hnInvMass[w][j]->GetAxis(2)->SetRange(0,-1); hnInvMass[w][j]->GetAxis(3)->SetRange(0,-1); } } for(int w=0; w<2; w++) { for(int t=0; t<gNTrgSetup; t++) { for(int k=0; k<nCentBins; k++) { for(Int_t i=0; i<nPtBins; i++) { if(hInvMass[w][t][k][i][1]) hInvMass[w][t][k][i][1]->Add(hInvMass[w][t][k][i][2]); } } } } // mixed event TFile *fmix = 0; if(year==2014) { char *mixName = Form("%s.Mix.pt%1.1f.pt%1.1f.%sroot",run_type.Data(),pt1_cut,pt2_cut,run_config); fmix = TFile::Open(Form("Output/%s",mixName),"read"); cout << "Mix file: " << fmix->GetName() << endl; TH1F *hMixInvMass[nCentBins][nPtBins][3]; printf("INFO: using Shuai's mixed events\n"); TH3D *hMixMmumuvsPtCen[3]; hMixMmumuvsPtCen[0] = (TH3D*)fmix->Get("hMixULMmumuvsPtCen"); hMixMmumuvsPtCen[1] = (TH3D*)fmix->Get("hMixLPosMmumuvsPtCen"); hMixMmumuvsPtCen[2] = (TH3D*)fmix->Get("hMixLNegMmumuvsPtCen"); for(Int_t j=0; j<3; j++) { for(int i=0; i<nPtBins; i++) { int ybin_min = hMixMmumuvsPtCen[j]->GetYaxis()->FindFixBin(ptBinsLow[i]+1e-4); int ybin_max = hMixMmumuvsPtCen[j]->GetYaxis()->FindFixBin(ptBinsHigh[i]-1e-4); for(int k=0; k<nCentBins; k++) { TH1F *htmp = (TH1F*)hMixMmumuvsPtCen[j]->ProjectionZ(Form("mix_%s_%s_InvMass_jpsi_PtBin%d_CentBin%d_tmp",hName[j],trigName[kTrigType],i,k),centBinsLow[k],centBinsHigh[k],ybin_min,ybin_max); hMixInvMass[k][i][j] = new TH1F(Form("mix_%s_%s_InvMass_jpsi_PtBin%d_CentBin%d",hName[j],trigName[kTrigType],i,k),htmp->GetTitle(),1400,0,14); for(int bin=1; bin<=htmp->GetNbinsX(); bin++) { hMixInvMass[k][i][j]->SetBinContent(bin,htmp->GetBinContent(bin)); hMixInvMass[k][i][j]->SetBinError(bin,htmp->GetBinError(bin)); } } } } for(int k=0; k<nCentBins; k++) { for(Int_t i=0; i<nPtBins; i++) { hMixInvMass[k][i][1]->Add(hMixInvMass[k][i][2]); } } } TString fileName = Form("Rootfiles/%s.Jpsi.pt%1.1f.pt%1.1f.%sroot",run_type.Data(),pt1_cut,pt2_cut,run_config); TFile *fout = NULL; if(nCentBins_pt==5) fout = TFile::Open(fileName,"recreate"); else fout = TFile::Open(fileName,"update"); const char* pair_name[2] = {"UL","LS"}; for(int w=0; w<2; w++) { for(int t=0; t<gNTrgSetup; t++) { for(int k=0; k<nCentBins; k++) { for(Int_t i=0; i<nPtBins; i++) { if(hInvMass[w][t][k][i][0]) { for(int j=0; j<2; j++) { hInvMass[w][t][k][i][j]->SetTitle(""); hInvMass[w][t][k][i][j]->Write(Form("InvMass_%s_pt%s_cent%s%s%s",pair_name[j],ptName[i],centName[k],gWeightName[w],gTrgSetupName[t]),TObject::kOverwrite); } } } } } } if(fmix) { for(int k=0; k<nCentBins; k++) { for(Int_t i=0; i<nPtBins; i++) { for(int j=0; j<2; j++) { hMixInvMass[k][i][j]->SetTitle(""); hMixInvMass[k][i][j]->Write(Form("Mix_InvMass_%s_pt%s_cent%s",pair_name[j],ptName[i],centName[k]),TObject::kOverwrite); } } } } fout->Close(); }
void DrawMLPoutputMovie( TFile* file, const TString& methodType, const TString& methodTitle ) { gROOT->SetBatch( 1 ); // define Canvas layout here! const Int_t width = 600; // size of canvas // this defines how many canvases we need TCanvas* c = 0; Float_t nrms = 4; Float_t xmin = -1.2; Float_t xmax = 1.2; Float_t ymin = 0; Float_t ymax = 0; Float_t maxMult = 6.0; Int_t countCanvas = 0; Bool_t first = kTRUE; TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring"; TDirectory *epochDir = (TDirectory*)file->Get( dirname ); if (!epochDir) { cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl; exit(1); } // now read all evolution histograms TIter keyItTit(epochDir->GetListOfKeys()); TKey *titkeyTit; while ((titkeyTit = (TKey*)keyItTit())) { if (!gROOT->GetClass(titkeyTit->GetClassName())->InheritsFrom("TH1F")) continue; TString name = titkeyTit->GetName(); if (!name.BeginsWith("convergencetest___")) continue; if (!name.Contains("_train_")) continue; // only for training so far if (name.EndsWith( "_B")) continue; // must be signal histogram if (!name.EndsWith( "_S")) { cout << "Big troubles with histogram: " << name << " -> should end with _S" << endl; exit(1); } // create canvas countCanvas++; TString ctitle = Form("TMVA response %s",methodTitle.Data()); c = new TCanvas( Form("canvas%d", countCanvas), ctitle, 0, 0, width, (Int_t)width*0.78 ); TH1F* sig = (TH1F*)titkeyTit->ReadObj(); sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) ); TString dataType = (name.Contains("_train_") ? "(training sample)" : "(test sample)"); // find background TString nbn = sig->GetName(); nbn[nbn.Length()-1] = 'B'; TH1F* bgd = dynamic_cast<TH1F*>(epochDir->Get( nbn )); if (bgd == 0) { cout << "Big troubles with histogram: " << bgd << " -> cannot find!" << endl; exit(1); } cout << "sig = " << sig->GetName() << endl; cout << "bgd = " << bgd->GetName() << endl; // set the histogram style TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd ); // normalise both signal and background TMVAGlob::NormalizeHists( sig, bgd ); // set only first time, then same for all plots if (first) { if (xmin == 0 && xmax == 0) { xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(), bgd->GetMean() - nrms*bgd->GetRMS() ), sig->GetXaxis()->GetXmin() ); xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(), bgd->GetMean() + nrms*bgd->GetRMS() ), sig->GetXaxis()->GetXmax() ); } ymin = 0; ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult; first = kFALSE; } // build a frame Int_t nb = 100; TString hFrameName(TString("frame") + methodTitle); TObject *o = gROOT->FindObject(hFrameName); if(o) delete o; TH2F* frame = new TH2F( hFrameName, sig->GetTitle(), nb, xmin, xmax, nb, ymin, ymax ); frame->GetXaxis()->SetTitle( methodTitle + " response" ); frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx"); TMVAGlob::SetFrameStyle( frame ); // find epoch number (4th token) TObjArray* tokens = name.Tokenize("_"); TString es = ((TObjString*)tokens->At(4))->GetString(); if (!es.IsFloat()) { cout << "Big troubles in epoch parsing: \"" << es << "\" is not float" << endl; exit(1); } Int_t epoch = es.Atoi(); // eventually: draw the frame frame->Draw(); c->GetPad(0)->SetLeftMargin( 0.105 ); frame->GetYaxis()->SetTitleOffset( 1.2 ); // Draw legend TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12, c->GetLeftMargin() + 0.5, 1 - c->GetTopMargin() ); legend->SetFillStyle( 1 ); legend->AddEntry(sig,TString("Signal ") + dataType, "F"); legend->AddEntry(bgd,TString("Background ") + dataType, "F"); legend->SetBorderSize(1); legend->SetMargin( 0.15 ); legend->Draw("same"); TText* t = new TText(); t->SetTextSize( 0.04 ); t->SetTextColor( 1 ); t->SetTextAlign( 31 ); t->DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() + 0.015, Form( "Epoch: %i", epoch) ); // overlay signal and background histograms sig->Draw("samehist"); bgd->Draw("samehist"); // save to file TString dirname = "movieplots"; TString foutname = dirname + "/" + name; foutname.Resize( foutname.Length()-2 ); foutname.ReplaceAll("convergencetest___",""); foutname += ".gif"; cout << "storing file: " << foutname << endl; c->Update(); c->Print(foutname); } }
void plotTreeNorms(TTree *tree_, std::string selectString, bool do7TeV){ // Create a map for plotting the pullsummaries: std::map < const char*, std::pair <double,double> > pullSummaryMap; int nPulls=0; TObjArray *l_branches = tree_->GetListOfBranches(); int nBranches = l_branches->GetEntries(); gStyle->SetPadTopMargin(0.01); TCanvas *c = new TCanvas("c","",960,800); std::string treename = tree_->GetName(); c->SaveAs(Form("%s_normresiduals.pdf[",treename.c_str())); // File to store plots in TFile *fOut = new TFile(Form("%s_normresiduals.root",treename.c_str()),"RECREATE"); TH1F *bHd = new TH1F("bHd","",50,-1.0,1.0); TH1F *bHfd = new TH1F("bHfd","",50,-1.0,1.0); for (int iobj=0;iobj<nBranches;iobj++){ TBranch *br =(TBranch*) l_branches->At(iobj); // Draw the normal histogram const char* name = br->GetName(); // select only the normalizations string namestr(name); if(namestr.find("n_exp")==string::npos) continue; bool fitPull=true; bool fitPullf=true; double p_mean =0; int nToysInTree = tree_->GetEntries(); // Find out if paramter is fitted value or constraint term. bool isFitted = true; p_mean = prenorms_[name].first; // toy initial parameters from the datacards std::cout << "******* "<< name << " *******"<<std::endl; std::cout << p_mean << std::endl; std::cout << "******************************" <<std::endl; TH1F* bH = (TH1F*)bHd->Clone(Form("%s",name)); TH1F* bHf = (TH1F*)bHfd->Clone(Form("%s_fail",name)); const char* drawInput = Form("(%s-%f)/%f",name,p_mean,p_mean); tree_->Draw(Form("%s>>%s",drawInput,name),""); tree_->Draw(Form("%s>>%s_fail",drawInput,name),selectString.c_str(),"same"); fitPull = true; fitPullf = true; bHf->SetLineColor(2); bH->GetXaxis()->SetTitle(bH->GetTitle()); bH->GetYaxis()->SetTitle(Form("no toys (%d total)",nToysInTree)); bH->GetYaxis()->SetTitleOffset(1.05); bH->GetXaxis()->SetTitleOffset(0.9); bH->GetYaxis()->SetTitleSize(0.05); bH->GetXaxis()->SetTitleSize(0.05); bH->GetXaxis()->SetTitle(Form("%s",name)); bH->SetTitle(""); if ( bH->Integral() <=0 ) fitPull = false; if (fitPull) {bH->Fit("gaus"); bH->GetFunction("gaus")->SetLineColor(4);} if ( bHf->Integral() <=0 ) fitPullf = false; if (fitPullf) {bHf->Fit("gaus"); bHf->GetFunction("gaus")->SetLineColor(2);} c->Clear(); TPad pad1("t1","",0.01,0.01,0.66,0.95); TPad pad2("t2","",0.70,0.20,0.98,0.80); pad1.SetNumber(1); pad2.SetNumber(2); if ( isFitted ) {pad2.Draw();} pad1.Draw(); pad1.SetGrid(true); TLatex *titletext = new TLatex();titletext->SetNDC(); c->cd(1); bH->Draw(); bHf->Draw("same"); TLegend *legend = new TLegend(0.6,0.8,0.9,0.89); legend->SetFillColor(0); legend->AddEntry(bH,"All Toys","L"); legend->AddEntry(bHf,selectString.c_str(),"L"); legend->Draw(); if (fitPull){ c->cd(2); double gap; TLatex *tlatex = new TLatex(); tlatex->SetNDC(); if (fitPullf) {tlatex->SetTextSize(0.09); gap=0.12;} else {tlatex->SetTextSize(0.11);gap=0.14;} tlatex->SetTextColor(4); tlatex->DrawLatex(0.11,0.80,Form("Mean : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParError(1))); tlatex->DrawLatex(0.11,0.80-gap,Form("Sigma : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(2),bH->GetFunction("gaus")->GetParError(2))); if (fitPullf){ tlatex->SetTextColor(2); tlatex->DrawLatex(0.11,0.60,Form("Mean : %.3f #pm %.3f",bHf->GetFunction("gaus")->GetParameter(1),bHf->GetFunction("gaus")->GetParError(1))); tlatex->DrawLatex(0.11,0.60-gap,Form("Sigma : %.3f #pm %.3f",bHf->GetFunction("gaus")->GetParameter(2),bHf->GetFunction("gaus")->GetParError(2))); } tlatex->SetTextSize(0.10); tlatex->SetTextColor(1); tlatex->DrawLatex(0.11,0.33,Form("Pre-fit: %.3f",p_mean)); pullSummaryMap[name]=std::make_pair<double,double>(bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParameter(2)); nPulls++; } // double titleSize = isFitted ? 0.1 : 0.028; //titletext->SetTextSize(titleSize);titletext->SetTextAlign(21); titletext->DrawLatex(0.55,0.92,name); c->SaveAs(Form("%s_normresiduals_%s.pdf",treename.c_str(),(do7TeV ? "7TeV": "8TeV"))); c->SaveAs(Form("mlfit/%s_residual_%s_%s.pdf",name,treename.c_str(),(do7TeV ? "7TeV": "8TeV"))); fOut->WriteObject(c,Form("%s_%s",treename.c_str(),name)); } if (nPulls>0){ std::cout << "Generating Pull Summaries" <<std::endl; int nRemainingPulls = nPulls; TCanvas *hc = new TCanvas("hc","",3000,2000); hc->SetGrid(0); std::map < const char*, std::pair <double,double> >::iterator pull_it = pullSummaryMap.begin(); std::map < const char*, std::pair <double,double> >::iterator pull_end = pullSummaryMap.end(); int pullPlots = 1; while (nRemainingPulls > 0){ int nThisPulls = min(maxPullsPerPlot,nRemainingPulls); TH1F pullSummaryHist("pullSummary","",nThisPulls,0,nThisPulls); for (int pi=1;pull_it!=pull_end && pi<=nThisPulls ;pull_it++,pi++){ pullSummaryHist.GetXaxis()->SetBinLabel(pi,(*pull_it).first); pullSummaryHist.SetBinContent(pi,((*pull_it).second).first); pullSummaryHist.SetBinError(pi,((*pull_it).second).second); nRemainingPulls--; } pullSummaryHist.SetMarkerStyle(21);pullSummaryHist.SetMarkerSize(1.5);pullSummaryHist.SetMarkerColor(2);pullSummaryHist.SetLabelSize(pullLabelSize); pullSummaryHist.GetYaxis()->SetRangeUser(-1,1);pullSummaryHist.GetYaxis()->SetTitle("residual summary (relative)");pullSummaryHist.Draw("E1"); hc->SaveAs(Form("%s_normresiduals_%s.pdf",treename.c_str(),(do7TeV ? "7TeV": "8TeV"))); hc->SaveAs(Form("mlfit/residual_summary_%d_%s_%s.pdf",pullPlots,treename.c_str(),(do7TeV ? "7TeV": "8TeV"))); fOut->WriteObject(hc,Form("comb_pulls_%s_%d",treename.c_str(),pullPlots)); // hc->SaveAs(Form("comb_pulls_%s_%d.pdf",treename.c_str(),pullPlots)); pullPlots++; } delete hc; } c->SaveAs(Form("%s_normresiduals_%s.pdf]",treename.c_str(),(do7TeV ? "7TeV": "8TeV"))); fOut->Close(); delete c; return; }
void plotTree(TTree *tree_, std::string whichfit, std::string selectString){ // Create a map for plotting the pullsummaries: std::map < const char*, std::pair <double,double> > pullSummaryMap; int nPulls=0; TObjArray *l_branches = tree_->GetListOfBranches(); int nBranches = l_branches->GetEntries(); gStyle->SetPadTopMargin(0.01); TCanvas *c = new TCanvas("c","",960,800); std::string treename = tree_->GetName(); c->SaveAs(Form("%s.pdf[",treename.c_str())); // File to store plots in TFile *fOut = new TFile(Form("%s.root",treename.c_str()),"RECREATE"); for (int iobj=0;iobj<nBranches;iobj++){ TBranch *br =(TBranch*) l_branches->At(iobj); // Draw the normal histogram const char* name = br->GetName(); // names with - are not allowed string namestr(name); if(namestr.find("-")!=string::npos) { std::cout << "Variable " << name << " contains a bad character: -. Skipping. " << std::endl; continue; } bool fitPull=false; bool fitPullf=false; bool plotLH=false; TGraph *gr=NULL; double p_mean =0; double p_err =0; int nToysInTree = tree_->GetEntries(); // Find out if paramter is fitted value or constraint term bool isFitted = findNuisancePre(name); if (doPull && isFitted){ p_mean = bfvals_[name].first; // toy constrainits thrown about best fit to data if(namestr.find("n_exp")==string::npos) p_err = prevals_[name].second; // uncertainties taken from card std::cout << "******* "<< name << " *******"<<std::endl; std::cout << p_mean << " " << p_err << std::endl; std::cout << "******************************" <<std::endl; const char* drawInput; // if the parameter is a normalization, the error is not available. Do the residual instead of the pull if(namestr.find("n_exp")!=string::npos) drawInput = Form("(%s-%f)/%f",name,p_mean,p_mean); else drawInput = Form("(%s-%f)/%f",name,p_mean,p_err); tree_->Draw(Form("%s>>%s",drawInput,name),""); tree_->Draw(Form("%s>>%s_fail",drawInput,name),selectString.c_str(),"same"); fitPull = true; fitPullf = true; if (doLH) { gr = graphLH(name,p_err,whichfit); if (gr) plotLH=true; } } else{ tree_->Draw(Form("%s>>%s",name,name),""); tree_->Draw(Form("%s>>%s_fail",name,name),selectString.c_str(),"same"); } TH1F* bH = (TH1F*) gROOT->FindObject(Form("%s",name))->Clone(); TH1F* bHf = (TH1F*) gROOT->FindObject(Form("%s_fail",name))->Clone(); bHf->SetLineColor(2); bH->GetXaxis()->SetTitle(bH->GetTitle()); bH->GetYaxis()->SetTitle(Form("no toys (%d total)",nToysInTree)); bH->GetYaxis()->SetTitleOffset(1.05); bH->GetXaxis()->SetTitleOffset(0.9); bH->GetYaxis()->SetTitleSize(0.05); bH->GetXaxis()->SetTitleSize(0.05); if (isFitted) {bH->GetXaxis()->SetTitle(Form("(%s-#theta_{B})/#sigma_{#theta}",name));} else {bH->GetXaxis()->SetTitle(Form("%s",name));} bH->SetTitle(""); if ( bH->Integral() <=0 ) fitPull = false; if (fitPull) {bH->Fit("gaus"); bH->GetFunction("gaus")->SetLineColor(4);} if ( bHf->Integral() <=0 ) fitPullf = false; if (fitPullf) {bHf->Fit("gaus"); bHf->GetFunction("gaus")->SetLineColor(2);} c->Clear(); //TPad pad1("t1","",0.01,0.02,0.59,0.98); // Pad 1 sizes depend on the parameter type ... double pad1_x1,pad1_x2,pad1_y1,pad1_y2; if ( !isFitted ) { pad1_x1 = 0.01; pad1_x2 = 0.98; pad1_y1 = 0.045; pad1_y2 = 0.98; } else { pad1_x1 = 0.01; pad1_x2 = 0.59; pad1_y1 = 0.56; pad1_y2 = 0.98; } TPad pad1("t1","",pad1_x1,pad1_y1,pad1_x2,pad1_y2); TPad pad1a("t1a","",0.01,0.045,0.59,0.522); TPad pad2("t2","",0.59,0.04,0.98,0.62); TPad pad3("t3","",0.55,0.64,0.96,0.95); pad1.SetNumber(1); pad2.SetNumber(2); pad3.SetNumber(3); pad1a.SetNumber(4); if ( isFitted ) {pad1a.Draw();pad2.Draw();pad3.Draw();} pad1.Draw(); pad2.SetGrid(true); TLatex *titletext = new TLatex();titletext->SetNDC(); if ( isFitted ){ c->cd(4); tree_->Draw(Form("%s:%s_In>>%s_%s_2d",name,name,name,tree_->GetName()),""); //TH2D *h2d_corr = (TH2D*)gROOT->FindObject(Form("%s_2d",name)); //h2d_corr->SetMarkerColor(4); //h2d_corr->SetTitle(""); //h2d_corr->GetXaxis()->SetTitle(Form("%s_In",name)); //h2d_corr->GetYaxis()->SetTitle(Form("%s",name)); titletext->SetTextAlign(11); titletext->SetTextSize(0.05); titletext->DrawLatex(0.05,0.02,Form("%s_In",name)); titletext->SetTextAngle(90); titletext->DrawLatex(0.04,0.06,Form("%s",name)); titletext->SetTextAngle(0); } c->cd(1); bH->Draw(); bHf->Draw("same"); TLegend *legend = new TLegend(0.6,0.8,0.9,0.89); legend->SetFillColor(0); legend->AddEntry(bH,"All Toys","L"); legend->AddEntry(bHf,selectString.c_str(),"L"); legend->Draw(); if (doPull && plotLH) { c->cd(2); gr->Draw("ALP"); } if (fitPull){ c->cd(3); double gap; TLatex *tlatex = new TLatex(); tlatex->SetNDC(); if (fitPullf) {tlatex->SetTextSize(0.09); gap=0.12;} else {tlatex->SetTextSize(0.11);gap=0.14;} tlatex->SetTextColor(4); tlatex->DrawLatex(0.11,0.80,Form("Mean : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParError(1))); tlatex->DrawLatex(0.11,0.80-gap,Form("Sigma : %.3f #pm %.3f",bH->GetFunction("gaus")->GetParameter(2),bH->GetFunction("gaus")->GetParError(2))); if (fitPullf){ tlatex->SetTextColor(2); tlatex->DrawLatex(0.11,0.60,Form("Mean : %.3f #pm %.3f",bHf->GetFunction("gaus")->GetParameter(1),bHf->GetFunction("gaus")->GetParError(1))); tlatex->DrawLatex(0.11,0.60-gap,Form("Sigma : %.3f #pm %.3f",bHf->GetFunction("gaus")->GetParameter(2),bHf->GetFunction("gaus")->GetParError(2))); } tlatex->SetTextSize(0.10); tlatex->SetTextColor(1); if(namestr.find("n_exp")!=string::npos) tlatex->DrawLatex(0.11,0.33,Form("Pre-fit: %.3f",prevals_[name].first)); else tlatex->DrawLatex(0.11,0.33,Form("Pre-fit #pm #sigma_{#theta}: %.3f #pm %.3f",prevals_[name].first, p_err)); tlatex->DrawLatex(0.11,0.18,Form("Best-fit (#theta_{B}) : %.3f ",p_mean)); tlatex->DrawLatex(0.11,0.03,Form("Best-fit (#theta_{S+B}): %.3f ",bfvals_sb_[name].first)); pullSummaryMap[name]=std::make_pair<double,double>(bH->GetFunction("gaus")->GetParameter(1),bH->GetFunction("gaus")->GetParameter(2)); nPulls++; } double titleSize = isFitted ? 0.1 : 0.028; titletext->SetTextSize(titleSize);titletext->SetTextAlign(21); titletext->DrawLatex(0.55,0.92,name); c->SaveAs(Form("%s.pdf",treename.c_str())); fOut->WriteObject(c,Form("%s_%s",treename.c_str(),name)); //c->SaveAs(Form("%s_%s.pdf",treename.c_str(),name)); } if (doPull && nPulls>0){ std::cout << "Generating Pull Summaries" <<std::endl; int nRemainingPulls = nPulls; TCanvas *hc = new TCanvas("hc","",3000,2000); hc->SetGrid(0); std::map < const char*, std::pair <double,double> >::iterator pull_it = pullSummaryMap.begin(); std::map < const char*, std::pair <double,double> >::iterator pull_end = pullSummaryMap.end(); int pullPlots = 1; while (nRemainingPulls > 0){ int nThisPulls = min(maxPullsPerPlot,nRemainingPulls); TH1F pullSummaryHist("pullSummary","",nThisPulls,0,nThisPulls); for (int pi=1;pull_it!=pull_end && pi<=nThisPulls ;pull_it++,pi++){ pullSummaryHist.GetXaxis()->SetBinLabel(pi,(*pull_it).first); pullSummaryHist.SetBinContent(pi,((*pull_it).second).first); pullSummaryHist.SetBinError(pi,((*pull_it).second).second); nRemainingPulls--; } pullSummaryHist.SetMarkerStyle(21);pullSummaryHist.SetMarkerSize(1.5);pullSummaryHist.SetMarkerColor(2);pullSummaryHist.SetLabelSize(pullLabelSize); pullSummaryHist.GetYaxis()->SetRangeUser(-3,3);pullSummaryHist.GetYaxis()->SetTitle("pull summary (n#sigma)");pullSummaryHist.Draw("E1"); hc->SaveAs(Form("%s.pdf",treename.c_str())); fOut->WriteObject(hc,Form("comb_pulls_%s_%d",treename.c_str(),pullPlots)); // hc->SaveAs(Form("comb_pulls_%s_%d.pdf",treename.c_str(),pullPlots)); pullPlots++; } delete hc; } c->SaveAs(Form("%s.pdf]",treename.c_str())); fOut->Close(); delete c; return; }
int main(int argc, char* argv[]) { string htitle = "Events rate, cloud "; htitle = htitle+string(argv[1]); string drootpath; if (strcmp(argv[2],"ttreecache")==0) { htitle = htitle + ", TTreeCache enabled, protocol: WebDAV"; drootpath = "../../../Histograms/TDavix/"; } else if (strcmp(argv[2],"xrootd")==0) { htitle = htitle + ", TTreeCache enabled xrootd"; drootpath = "../../../Histograms/xrootd/"; } else { htitle = htitle + ", TTreeCache disabled, protocol: WebDAV "; drootpath = "../../../Histograms/Davix/"; } string cloudpath = argv[1] ; cloudpath = cloudpath + "/"; DIR* dcloudrep = opendir(string2char(drootpath+cloudpath)); struct dirent* dcloudfichier = NULL; string allgrid; allgrid = string(argv[3]); vector<ratioMatrixValue> th2array; while ((dcloudfichier = readdir(dcloudrep))!=0) { if (dcloudfichier->d_name[0]=='.') continue; string dcloudpath = cloudpath + dcloudfichier->d_name + "/"; //DIR* drootfile = opendir(string2char(drootpath+dcloudpath)); //struct dirent* drootfichier = NULL; string dfilepath = drootpath + dcloudpath + allgrid + "/" + dcloudfichier->d_name + ".root"; ratioMatrixValue column; TFile *dfr = TFile::Open(string2char(dfilepath)); if (dfr==NULL) continue; column.analy = dcloudfichier->d_name; for (int i=0;i<dfr->GetListOfKeys()->GetEntries();i++) { string dstrRead = string2char(dfr->GetListOfKeys()->At(i)->GetName()); TH1F* dhist = (TH1F*)dfr->Get(string2char(dstrRead)); if (dhist==NULL) continue; string otherTitle = string(dhist->GetName()) + "No Zero"; TH1F* histSansZeros = new TH1F(string2char(otherTitle),dhist->GetTitle(),3000.,0,3000); for ( int l=2;l<3000;l++) for (int k=0;k<dhist->GetBinContent(l);k++) histSansZeros->Fill(l-1); column.datadisk.push_back(string2char(dstrRead)); column.ratio.push_back(histSansZeros->GetMean()); column.error.push_back(histSansZeros->GetRMSError()); } th2array.push_back(column); } TCanvas * c = new TCanvas("c", "c"); c->SetLeftMargin(0.20); c->SetBottomMargin(0.15); c->SetWindowSize(2100,1500); /*for (unsigned int i=0;i<th2array.size();i++) { cout << th2array[i].analy << "\t"; for (unsigned int j=0;j<th2array[i].datadisk.size();j++) cout << th2array[i].datadisk[j] << "\t" ; cout << endl; }*/ cout << "--------------------------" << endl; th2array = ordenate(th2array,string(argv[1])); cout << "--------------------------" << endl; /*for (unsigned int i=0;i<th2array.size();i++) { cout << th2array[i].analy << "\t"; for (unsigned int j=0;j<th2array[i].datadisk.size();j++) cout << th2array[i].datadisk[j] << "\t" ; cout << endl; }*/ unsigned int datadisk_number=0; unsigned int analy_number=0; for (unsigned int i=0;i<th2array.size();i++) { if (datadisk_number < th2array[i].datadisk.size()) { datadisk_number = th2array[i].datadisk.size(); } if (th2array[i].datadisk.size()!=0) analy_number++; } char *title = string2char(htitle); TH2F *h = new TH2F("eventMatrix",title,analy_number,0.,analy_number,datadisk_number,0.,datadisk_number); for (unsigned int i=0;i<th2array.size();i++) { //cout << "coucou " << th2array[i].analy << " " << th2array[i].datadisk.size() << endl; for (unsigned int j=0;j<th2array[i].datadisk.size();j++) { h->Fill(th2array[i].analy,th2array[i].datadisk[j],th2array[i].ratio[j]); //cout << i << " " << j << " " << th2array[i].error[j] << endl; h->SetCellError(i+1,j+1,th2array[i].error[j]); //cout << i << " " << j << " " << h->GetBinErrorLow(i+1,j+1) << " " << h->GetBinErrorUp(i+1,j+1) << endl; } } const Int_t Number = 5; Double_t Red[Number] = { 1.00, 0.75, 1.00, 0.25, 0.00 }; Double_t Green[Number] = { 0.00, 0.25, 0.50, 0.75, 1.00 }; Double_t Blue[Number] = { 0.00, 0.00, 0.00, 0.00, 0.00 }; Double_t Stops[Number] = { 0.00, 0.34, 0.61, 0.84, 1.00 }; Int_t nb=50; TColor::CreateGradientColorTable(Number,Stops,Red,Green,Blue,nb); h->GetXaxis()->SetTitle("Processing site"); h->GetXaxis()->CenterTitle(); h->GetYaxis()->SetTitle("Data source site"); h->GetYaxis()->CenterTitle(); h->SetLabelSize(0.03,"Y"); h->SetTitleOffset(2.3,"Y"); h->SetLabelSize(0.03,"X"); h->SetTitleOffset(2.0,"X"); h->GetYaxis()->SetTitle("Data source site"); h->Draw("COLZ"); h->Draw("TEXTE SAME"); gStyle->SetOptStat(0); //gStyle->SetTitleY(0.1f); h->SetMaximum(750); h->SetMinimum(0); c->Print(Form("canvas.eps")); return 0; }
//5_3_6: gROOT->ProcessLine(".include /afs/cern.ch/cms/slc5_amd64_gcc462/lcg/roofit/5.32.03-cms9/include/") //7_1_0: gROOT->ProcessLine(".include /afs/cern.ch/cms/slc6_amd64_gcc481/lcg/roofit/5.34.22-cms2/include/") //Usage: .x Total_fit.C+("root://eoscms//eos/cms/store/group/dpg_ecal/alca_ecalcalib/lpernie/ //ALL_Neutrino_Pt2to20_AVE40BX25_FoldEtaRing_eta01/iter_0/epsilonPlots.root", "Comp_2015A", "", true, true, false ) void Total_fit( TString File, TString folder, TString Hname, bool RunOnAll, bool isEB=true, bool Are_pi0_=true ){ //OLD STYLE gROOT->SetStyle("Plain"); gStyle->SetPalette(1); gStyle->SetOptStat(1111111); // Show overflow, underflow + SumOfWeights gStyle->SetOptFit(111110); gStyle->SetOptFile(1); gStyle->SetMarkerStyle(20); gStyle->SetMarkerSize(.3); gStyle->SetMarkerColor(1); TCanvas* myc1 = new TCanvas("myc1"," ",500,500); myc1->SetLeftMargin(0.16); //Files TString Comm = "mkdir -p " + folder; system( Comm.Data() ); cout<<"Opening: "<<File.Data()<<endl; TFile* fin = TFile::Open(File.Data()); //Histos int MaxH = 1; if( RunOnAll ) MaxH = isEB ? 61200 : 14648; cout<<"Running on "<<MaxH<<" events"<<endl; for(int i=0; i<MaxH; i++){ std::stringstream ind; ind << (int) i; TString EBEE = isEB ? "EB" : "EE"; TString BarEnd = isEB ? "Barrel" : "Endcap"; TString isPi0 = Are_pi0_ ? "pi0" : "eta"; TString name = BarEnd + "/epsilon_" + EBEE + "_iR_" + TString(ind.str()); if( ! RunOnAll ) name = Hname; TH1F *h = (TH1F*) fin->Get( name.Data() ); h->SetTitle(""); TString outName = folder + "/" + EBEE + "_iR_" + TString(ind.str()) + "_" + isPi0 + ".png"; TString outName1 = folder + "/" + EBEE + "_iR_" + TString(ind.str()) + "_" + isPi0 +".pdf"; TString outName2 = folder + "/" + EBEE + "_iR_" + TString(ind.str()) + "_" + isPi0 +".C"; TString outName3 = folder + "/" + EBEE + "_iR_" + TString(ind.str()) + "_" + isPi0 +".root"; //Fit Method int ngaus=1; //1: simple Gaussian, 2: two Gaussian float xlo = Are_pi0_? 0.08:0.4, xhi = Are_pi0_? 0.22:0.65; RooRealVar x("x","#gamma#gamma invariant mass",xlo, xhi, "GeV/c^{2}"); RooDataHist dh("dh","#gamma#gamma invariant mass",RooArgList(x),h); RooRealVar mean("mean","#pi^{0} peak position", Are_pi0_? 0.116:0.57, Are_pi0_? 0.105:0.5, Are_pi0_? 0.150:0.62,"GeV/c^{2}"); RooRealVar sigma("sigma","#pi^{0} core #sigma",0.013, 0.005,0.020,"GeV/c^{2}"); if(!isEB) { mean.setRange( Are_pi0_? 0.10:0.45, Are_pi0_? 0.140:0.62); // 0.200 mean.setVal(Are_pi0_? 0.120:0.55); sigma.setRange(0.005, 0.060); } if(isEB){ mean.setRange(Are_pi0_? 0.105:0.47, Are_pi0_? 0.155:0.62); sigma.setRange(0.003, 0.030); } RooRealVar Nsig("Nsig","#pi^{0} yield",1000.,0.,1.e7); Nsig.setVal( h->GetSum()*0.1); RooGaussian gaus("gaus","Core Gaussian",x, mean,sigma); RooRealVar sigmaTail("sigmaTail","#pi^{0} tail #sigma",0.040, 0.020,0.065,"GeV/c^{2}"); RooGaussian gaus2("gaus2","Tail Gaussian",x, mean,sigmaTail); RooRealVar fcore("fcore","f_{core}",0.9,0.,1.); RooAddPdf signal("signal","signal model",RooArgList(gaus,gaus2),fcore); RooRealVar p0("p0","p0", 1000.,-1.e5,1.e5); RooRealVar p1("p1","p1", -3000.,-1.e5,1.e5); RooRealVar p2("p2","p2", 10000.,-1.e5,1.e5); RooRealVar p3("p3","p3", -10000.,-1.e5,1.e5); RooRealVar p4("p4","p4",-4000.,-1.e5,1.e5); RooRealVar p5("p5","p5", 5.,-1.e5,1.e5); RooRealVar p6("p6","p6", 6.,-1.e5,1.e5); RooRealVar cb0("cb0","cb0", 0.2, -1.,1.); RooRealVar cb1("cb1","cb1",-0.1, -1.,1.); RooRealVar cb2("cb2","cb2", 0.1, -1.,1.); RooRealVar cb3("cb3","cb3",-0.1, -0.5,0.5); RooRealVar cb4("cb4","cb4", 0.1, -1.,1.); RooRealVar cb5("cb5","cb5", 0.1, -1.,1.); RooRealVar cb6("cb6","cb6", 0.3, -1.,1.); RooArgList cbpars(cb0,cb1,cb2); if(Are_pi0_) cbpars.add( cb3); // if(isEB){ // cb3.setRange(-1,1.); // cb4.setRange(-0.3,0.3); // cbpars.add( cb4 ); // } RooChebychev bkg("bkg","bkg model", x, cbpars ); RooRealVar Nbkg("Nbkg","background yield",1.e3,0.,1.e8); Nbkg.setVal( h->GetSum()*0.8 ); RooAbsPdf* model=0; RooAddPdf model1("model","sig+bkg",RooArgList(gaus,bkg),RooArgList(Nsig,Nbkg)); RooAddPdf model2("model","sig+bkg",RooArgList(signal,bkg),RooArgList(Nsig,Nbkg)); if(ngaus==1) model = &model1; else if(ngaus==2) model = &model2; RooNLLVar nll("nll","log likelihood var",*model,dh,RooFit::Extended(true));//RooFit::Extended(true) fundamental for right ormalization //RooAbsReal * nll = model->createNLL(dh); Suggested way RooMinuit m(nll); m.setVerbose(kFALSE); m.migrad(); //RooFitResult* res = m.save() ; x.setRange("sobRange",mean.getVal()-2.*sigma.getVal(), mean.getVal()+2.*sigma.getVal()); RooAbsReal* integralSig = gaus.createIntegral(x,NormSet(x),Range("sobRange")); RooAbsReal* integralBkg = bkg.createIntegral(x,NormSet(x),Range("sobRange")); RooChi2Var chi2("chi2","chi2 var",*model,dh, true); //int ndof = h->GetNbinsX() - res->floatParsFinal().getSize(); x.setRange("sobRange",mean.getVal()-2.*sigma.getVal(), mean.getVal()+2.*sigma.getVal()); RooPlot* xframe = x.frame(h->GetNbinsX()); xframe->SetTitle(h->GetTitle()); dh.plotOn(xframe); model->plotOn(xframe,Components(RooArgSet(bkg,gaus)),LineStyle(kDashed), LineColor(kRed)); model->plotOn(xframe); //xframe->GetYaxis()->SetTitle("# Events"); if(Are_pi0_) xframe->GetYaxis()->SetTitleOffset(2.05); else xframe->GetYaxis()->SetTitleOffset(1.3); xframe->Draw(); TLatex lat; char line[300]; lat.SetNDC(); lat.SetTextSize(0.040); lat.SetTextColor(1); sprintf(line,"CMS Preliminary"); lat.DrawLatex(0.2, 0.93, line); float xmin(0.5), yhi(0.80), ypass(0.05); if(!Are_pi0_) xmin=0.30; sprintf(line,"m_{#gamma#gamma} = %.2f #pm %.2f", mean.getVal()*1000., mean.getError()*1000. ); lat.DrawLatex(xmin,yhi, line); sprintf(line,"#sigma = %.2f #pm %.2f", sigma.getVal()*1000., sigma.getError()*1000. ); lat.DrawLatex(xmin,yhi-ypass, line); //sprintf(line,"N = %.0f #pm %.0f", Nsig.getVal(), Nsig.getError() ); //lat.DrawLatex(xmin,yhi-2*ypass, line); //sprintf(line,"Yield: %.0f #pm %.0f", Nsig.getVal(), Nsig.getError() ); //lat.DrawLatex(xmin,yhi, line); //sprintf(line,"m_{#gamma#gamma}: %.2f #pm %.2f", mean.getVal()*1000., mean.getError()*1000. ); //lat.DrawLatex(xmin,yhi-ypass, line); //sprintf(line,"#sigma: %.2f #pm %.2f (%.2f%s)", sigma.getVal()*1000., sigma.getError()*1000., sigma.getVal()*100./mean.getVal(), "%" ); //lat.DrawLatex(xmin,yhi-2.*ypass, line); sprintf(line,"S/B (2#sigma): %.2f", (integralSig->getVal()*Nsig.getVal())/(integralBkg->getVal()*Nbkg.getVal()) ); lat.DrawLatex(xmin,yhi-2.*ypass, line); sprintf(line,"S (2#sigma): %.2f", (integralSig->getVal()*Nsig.getVal()) ); lat.DrawLatex(xmin,yhi-3.*ypass, line); sprintf(line,"B (2#sigma): %.2f", (integralBkg->getVal()*Nbkg.getVal()) ); lat.DrawLatex(xmin,yhi-4.*ypass, line); myc1->SaveAs(outName.Data()); myc1->SaveAs(outName1.Data()); myc1->SaveAs(outName2.Data()); myc1->SaveAs(outName3.Data()); //float binSize = h->GetXaxis()->GetBinWidth(10); //cout<<"BinSize is: "<<binSize<<endl; //cout<<"Histo Entries: "<<h->GetEntries()<<" and integral: "<<h->Integral()<<" and integral width "<<h->Integral("width")<<endl; //cout<<"h->GetSum()*0.1 (0.8 for bkg) "<<h->GetSum()<<" * 0.1 = "<<h->GetSum()*0.1<<endl; //cout<<"integralSig->getVal()*Nsig.getVal(): "<<integralSig->getVal()*Nsig.getVal()<<" : "<<integralSig->getVal()<<" * "<<Nsig.getVal()<<endl; //cout<<"gaus int "<<gaus.getVal()<<endl; ////cout<<"integralBkg->getVal()*Nbkg.getVal(): "<<integralBkg->getVal()*Nbkg.getVal()<<" : "<<integralBkg->getVal()<<" * "<<Nbkg.getVal()<<endl; //RooAbsReal* integralSig2 = gaus.createIntegral(x,NormSet(x)); //RooAbsReal* integralSig3 = gaus.createIntegral(x); //RooAbsReal* integralSig4 = gaus.createIntegral(x,NormSet(x),Range("sobRange")); //cout<<"integralSig "<<integralSig->getVal()<<" integralSig2 "<<integralSig2->getVal()<<" integralSig3 "<<integralSig3->getVal()<<" integralSig4 "<<integralSig4->getVal()<<endl; //cout<<"Se ci credo "<<endl; //cout<<"S: "<<integralSig->getVal()*Nsig.getVal()<<" B: "<<integralBkg->getVal()*Nbkg.getVal()<<" s/B "<<(integralSig->getVal()*Nsig.getVal())/(integralBkg->getVal()*Nbkg.getVal())<<endl; //cout<<"ALL: "<<endl; //cout<<"S: "<<Nsig.getVal()<<" B: "<<Nbkg.getVal()<<" s/B "<<Nsig.getVal()/Nbkg.getVal()<<" tot "<<(Nsig.getVal()+Nbkg.getVal())<<endl; //TF1 *gau = new TF1("gau","[0]*exp(-0.5*((x-[1])/[2])**2)", 0.08, 0.22); //gau->FixParameter(0,integralSig->getVal()*Nsig.getVal()*binSize); //gau->FixParameter(1,mean.getVal()); //gau->FixParameter(2,sigma.getVal()); //gau->SetLineColor(2); //gau->Draw("same"); //cout<<"TF1: "<<gau->Integral(0.08, 0.22)<<endl; } }