481 std::vector<TCanvas*> rooplots;
484 std::cerr <<
"ERROR::NULL workspace. Will return empty vector!" <<
std::endl;
493 RooSimultaneous* pdf = (RooSimultaneous*)
work->pdf(
"simPdf");
495 std::cerr <<
"ERROR::No pdf found in workspace. Will return empty vector!" <<
std::endl;
496 std::cerr <<
"ERROR::No pdf found in workspace. Will return empty vector!" <<
std::endl;
497 std::cerr <<
"ERROR::No pdf found in workspace. Will return empty vector!" <<
std::endl;
505 RooCategory* categories =
work->cat(
"channelCat");
508 data->table(*((RooAbsCategory*)categories))->Print(
"v");
510 std::vector<TString> categoriesName;
511 TIterator* iter = categories->typeIterator();
513 while( (catType = (RooCatType*) iter->Next())) {
514 TString catname = catType->GetName();
515 categoriesName.push_back(catname);
518 for(UInt_t i = 0; i < categoriesName.size(); i++){
519 TString catname = categoriesName[i];
521 if(categories->setLabel(catname)){
522 std::cout <<
"WARNING::Category " << catname.Data() <<
" is not a member of channelCat. Will skip." <<
std::endl;
526 RooAbsPdf* subpdf = (RooAbsPdf*)pdf->getPdf(catname.Data());
528 std::cout <<
"WARNING::Can't find sub-pdf for region " << catname.Data() <<
". Will skip." <<
std::endl;
532 TString subdataset_str = Form(
"channelCat==channelCat::%s",catname.Data());
533 RooAbsData* subdataset = (RooAbsData*)
data->reduce(subdataset_str.Data());
535 std::cout <<
"WARNING::Can't find sub-dataset for region " << catname.Data() <<
". Will skip." <<
std::endl;
539 RooRealVar*
var =(RooRealVar*) ((RooArgSet*) subpdf->getObservables(*subdataset))->find(Form(
"obs_x_%s", catname.Data()));
542 TString canName = Form(
"canvas_%s_%s_%s", catname.Data(),
name.Data(), plottodraw.Data());
543 TCanvas*
c =
new TCanvas(canName, canName);
546 TLegend* legend =
new TLegend(0.10,0.70,0.90,0.90,
"");
547 legend->SetFillStyle(0);
548 legend->SetFillColor(0);
549 legend->SetBorderSize(0);
550 legend->SetTextSize(0.036);
552 legend->SetNColumns(3);
554 TLegendEntry*
entry = legend->AddEntry(
"",
"Data",
"pl") ;
555 entry->SetMarkerColor(kBlack);
556 entry->SetMarkerStyle(20);
557 entry=legend->AddEntry(
"",
"MC Total",
"lf") ;
558 entry->SetLineColor(kBlack);
559 entry->SetFillColor(kBlue);
560 entry->SetFillStyle(3444);
562 RooPlot* frame = var->frame();
563 frame->SetName(Form(
"frame_%s_%s_%s", catname.Data(),
name.Data(), plottodraw.Data()));
566 if(
error.Contains(
"SumW2") ||
error.Contains(
"sumw2") ||
error.Contains(
"sumW2"))
567 subdataset->plotOn(frame, RooFit::DataError(RooAbsData::SumW2), RooFit::MarkerColor(1), RooFit::LineColor(1));
569 subdataset->plotOn(frame, RooFit::DataError(RooAbsData::Poisson), RooFit::MarkerColor(1), RooFit::LineColor(1));
571 TString RRSumPdfName = Form(
"%s_model",catname.Data());
572 RooRealSumPdf* RRSumPdf = (RooRealSumPdf*) subpdf->getComponents()->
find(RRSumPdfName);
574 RooArgList RRSumComponentsList = RRSumPdf->funcList();
576 TString binWidthName = Form(
"binWidth_obs_x_%s_0",catname.Data());
577 RooRealVar* regionBinWidth = ((RooRealVar*) RRSumPdf->getVariables()->find(Form(
"binWidth_obs_x_%s_0",catname.Data()))) ;
578 if(regionBinWidth == NULL)
579 std::cout <<
"WARNING::bindWidth variable not found for region(" << catname <<
"). PLOTTING COMPONENTS WILL BE WRONG!" <<
std::endl;
582 double normCount = subpdf->expectedEvents(*var);
584 std::cout <<
"INFO::MC events after fit = " << normCount <<
" for " << subpdf->GetName() <<
std::endl;
586 RooLinkedListIter iter = RRSumComponentsList.iterator() ;
587 RooProduct* component;
589 std::vector<TString> compNameVec;
590 std::vector <double> compFracVec;
591 std::vector<TString> compStackNameVec;
592 std::vector <double> compStackFracVec;
594 compStackNameVec.clear();
596 compStackFracVec.clear();
598 while( (component = (RooProduct*) iter.Next()) ){
599 TString componentName = component->GetName();
600 TString stackComponentName = componentName;
602 if(!compStackNameVec.empty())
603 stackComponentName = Form(
"%s,%s",compStackNameVec.back().Data() ,componentName.Data());
604 compNameVec.push_back(componentName);
605 compStackNameVec.push_back(stackComponentName);
607 RooAbsReal* i_RRSumPdf = ((RooAbsPdf*)
work->pdf(RRSumPdfName))->createIntegral(RooArgSet(*var));
608 Double_t Int_RRSumPdf = i_RRSumPdf->getVal();
609 RooAbsReal* i_component = ((RooProduct*)
work->obj(componentName))->createIntegral(RooArgSet(*var));
610 Double_t Int_component = i_component->getVal();
612 Double_t componentFrac = 0.;
613 if(Int_RRSumPdf != 0.)
614 componentFrac = Int_component * regionBinWidth->getVal() / Int_RRSumPdf;
616 double stackComponentFrac = componentFrac;
617 if(!compStackFracVec.empty())
618 stackComponentFrac = compStackFracVec.back() + componentFrac;
620 compFracVec.push_back(componentFrac);
621 compStackFracVec.push_back(stackComponentFrac);
625 Int_t sigcolor[13] = {2,3,4,5,6,7,8,9,kMagenta, 1, kGreen+2, kTeal, kOrange+10};
626 for(
int i = (compFracVec.size()-1); i > -1; i--){
627 Int_t compPlotColor = i;
628 if(compNameVec[i].Contains(
"ChannelABS_CEX") || compNameVec[i].Contains(
"ChannelCEX_ABS")){
631 else if(compNameVec[i].Contains(
"RecoBin")){
635 compPlotColor = sigcolor[
counter];
638 std::cout <<
"INFO::Drawing " << compNameVec[i] <<
" " << counter <<
" " << compPlotColor <<
std::endl;
640 subpdf->plotOn(frame,RooFit::Components(compStackNameVec[i].
Data()),RooFit::FillColor(compPlotColor),RooFit::FillStyle(3001),RooFit::DrawOption(
"F"),RooFit::Normalization(compStackFracVec[i]*normCount,RooAbsReal::NumEvent),RooFit::Precision(1
e-5));
644 subpdf->plotOn(frame, RooFit::Normalization(1,RooAbsReal::RelativeExpected), RooFit::Precision(1
e-5), RooFit::VisualizeError(*
result), RooFit::FillColor(kBlue), RooFit::FillStyle(3444));
647 subpdf->plotOn(frame, RooFit::Normalization(1,RooAbsReal::RelativeExpected), RooFit::Precision(1
e-5), RooFit::LineColor(1));
648 if(
error.Contains(
"SumW2") ||
error.Contains(
"sumw2") ||
error.Contains(
"sumW2"))
649 subdataset->plotOn(frame, RooFit::DataError(RooAbsData::SumW2), RooFit::MarkerColor(1), RooFit::LineColor(1));
651 subdataset->plotOn(frame, RooFit::DataError(RooAbsData::Poisson), RooFit::MarkerColor(1), RooFit::LineColor(1));
657 bool found =
false;
bool found2 =
false;
659 for(
unsigned int i = 0; i < compFracVec.size(); i++){
660 std::cout <<
"NAME: " << compNameVec[i] <<
std::endl;
661 Int_t compPlotColor = i;
662 if(compNameVec[i].Contains(
"ChannelABS_CEX") && !
found){
664 entry=legend->AddEntry(
"",
"CEX Bkg",
"f");
665 entry->SetLineColor(46);
666 entry->SetFillColor(compPlotColor);
667 entry->SetFillStyle(3001);
671 else if(compNameVec[i].Contains(
"ChannelCEX_ABS") && !found){
673 entry=legend->AddEntry(
"",
"ABS Bkg",
"f");
674 entry->SetLineColor(46);
675 entry->SetFillColor(compPlotColor);
676 entry->SetFillStyle(3001);
680 else if(compNameVec[i].Contains(
"RecoBin") && !found2){
682 entry=legend->AddEntry(
"",
"Signal",
"f");
683 entry->SetLineColor(40);
684 entry->SetFillColor(compPlotColor);
685 entry->SetFillStyle(3001);
691 compPlotColor = sigcolor[
counter];
694 if(counter2 < (
int)binnames.size()){
695 TString legName = binnames[counter2];
696 if (compNameVec[i].Contains(
"Incident"))
697 legName = incidentBinNames[counter2];
698 if (compNameVec[i].Contains(
"Sideband"))
699 legName = sidebandBinNames[counter2];
701 entry=legend->AddEntry(
"",legName.Data(),
"f");
702 entry->SetLineColor(compPlotColor);
703 entry->SetFillColor(compPlotColor);
704 entry->SetFillStyle(3001);
711 float bottomMarginP1=0.015;
712 TPad *pad1 =
new TPad(Form(
"%s_pad1",canName.Data()),Form(
"%s_pad1",canName.Data()),0.,yMinP1,.99,1);
713 pad1->SetBottomMargin(bottomMarginP1);
714 pad1->SetFillColor(kWhite);
717 TPad *pad2 =
new TPad(Form(
"%s_pad2",canName.Data()),Form(
"%s_pad2",canName.Data()),0.,0.01,.99,0.295);
718 pad2->SetTopMargin(0.021);
719 pad2->SetBottomMargin(0.3);
720 pad2->SetFillColor(kWhite);
724 frame->GetXaxis()->SetLabelSize(0.);
727 frame->SetTitle(measurement.Data());
729 frame->GetXaxis()->SetTitle(
"Reco Bin");
730 frame->GetYaxis()->SetTitle(
"Events");
735 legend->AddEntry((TObject*)0x0, chi2_text.c_str(),
"");
740 RooPlot* frame_dummy = var->frame();
742 subdataset->plotOn(frame_dummy,RooFit::Cut(subdataset_str),RooFit::DataError(RooAbsData::Poisson));
745 const char* curvename = 0;
746 RooHist* nominal_hist = (RooHist*) frame_dummy->findObject(curvename, RooHist::Class());
748 std::cerr <<
"ERROR::Drawing the data/MC histogram. Can't find nominal histogram." <<
std::endl;
753 subpdf->plotOn(frame_dummy,RooFit::Normalization(1,RooAbsReal::RelativeExpected),RooFit::Precision(1
e-5));
756 RooCurve* nominal_curve = (RooCurve*) frame_dummy->findObject(curvename, RooCurve::Class());
758 std::cerr <<
"ERROR::Drawing the data/MC histogram. Can't find nominal curve." <<
std::endl;
764 RooHist* hratio = NULL;
765 RooCurve* ratio_curve =
new RooCurve;
767 if(plottodraw ==
"pull"){
768 hratio = (RooHist*) frame_dummy->pullHist();
769 hratio->SetTitle(
"Pull Distribution");
771 else if(plottodraw ==
"residuals"){
772 hratio = (RooHist*) frame_dummy->residHist();
773 hratio->SetTitle(
"Residual Distribution");
775 else if(plottodraw ==
"ratio"){
777 subpdf->plotOn(frame_dummy, RooFit::Normalization(1,RooAbsReal::RelativeExpected), RooFit::Precision(1
e-5), RooFit::VisualizeError(*
result, 1), RooFit::FillColor(kBlue-5), RooFit::FillStyle(3004));
780 RooCurve* error_curve = (RooCurve*)frame_dummy->findObject(curvename, RooCurve::Class());
782 std::cerr <<
"ERROR::Drawing the data/MC histogram. Can't find error curve." <<
std::endl;
786 ratio_curve->SetName(Form(
"%s_ratiocurve",nominal_curve->GetName()));
787 ratio_curve->SetLineColor(kBlue-5);
788 ratio_curve->SetFillColor(kBlue-5);
789 ratio_curve->SetFillStyle(3004);
790 ratio_curve->SetLineWidth(1);
794 bool bottomCurve =
false;
795 for(Int_t i=1; i < error_curve->GetN()-1; i++){
798 error_curve->GetPoint(i,x,y) ;
800 if( i >= (nominal_curve->GetN()-1) ) bottomCurve =
true;
805 if( i == (nominal_curve->GetN() - 1) || i == nominal_curve->GetN() ){
806 ratio_curve->addPoint(x, 0.);
811 nominal_curve->GetPoint(j,xNom,yNom);
816 nominal_curve->GetPoint(j,xNom,yNom);
819 if(fabs(yNom) > 0.00001){
820 ratio_curve->addPoint(x, (y / yNom));
823 ratio_curve->addPoint(x, 0.);
828 hratio =
new RooHist(nominal_hist->getNominalBinWidth());
831 Double_t xstart, xstop,
y;
832 nominal_curve->GetPoint(2,xstart,y);
833 nominal_curve->GetPoint(nominal_curve->GetN()-1,xstop,
y);
835 for(Int_t i=0; i < nominal_hist->GetN(); i++){
837 nominal_hist->GetPoint(i,x,ypoint);
839 if(x < xstart || x > xstop)
continue;
840 if(fabs(ypoint) < 0.000001 )
continue;
843 Double_t yerrorl = nominal_hist->GetErrorYlow(i);
844 Double_t yerrorh = nominal_hist->GetErrorYhigh(i);
857 yy = ypoint / nominal_curve->interpolate(x);
858 yerrorl /= nominal_curve->interpolate(x);
859 yerrorh /= nominal_curve->interpolate(x);
861 hratio->addBinWithError(x,yy,yerrorl,yerrorh);
866 std::cerr <<
"ERROR::Drawing data and MC histogram failed. RooHist is not found." <<
std::endl;
870 hratio->SetMarkerColor(kRed);
871 hratio->SetLineColor(kRed);
874 RooPlot* frame2 = var->frame();
875 if(plottodraw ==
"ratio" &&
result) frame2->addPlotable(ratio_curve,
"F");
876 frame2->addPlotable(hratio,
"P");
878 if (catname.Contains(
"Sideband")) {
880 frame2->GetXaxis()->SetTitle(
"Generic Label");
881 for(
size_t i = 1; i < sidebandBins.size(); i++){
882 TString ibinstr = Form(
"%.1f-%.1f",sidebandBins[i-1],sidebandBins[i]);
883 frame2->GetXaxis()->SetBinLabel(i, ibinstr.Data());
887 if (doNegativeReco) {
888 frame2->GetXaxis()->SetBinLabel(1,
"< 0.");
889 for(
size_t i = 1; i < recobins.size(); i++){
890 TString ibinstr = Form(
"%.1f-%.1f",recobins[i-1],recobins[i]);
891 frame2->GetXaxis()->SetBinLabel(i+1, ibinstr.Data());
895 for(
size_t i = 1; i < recobins.size(); i++){
896 TString ibinstr = Form(
"%.1f-%.1f",recobins[i-1],recobins[i]);
897 frame2->GetXaxis()->SetBinLabel(i, ibinstr.Data());
900 frame2->GetXaxis()->SetTitle(
"E_{reco} [MeV]");
904 int firstbin = frame_dummy->GetXaxis()->GetFirst();
905 int lastbin = frame_dummy->GetXaxis()->GetLast();
906 double xmax = frame_dummy->GetXaxis()->GetBinUpEdge(lastbin);
907 double xmin = frame_dummy->GetXaxis()->GetBinLowEdge(firstbin);
909 if(plottodraw==
"pull"){
910 TLine* lp1 =
new TLine(xmin,1.,xmax,1.);
911 TLine* lp2 =
new TLine(xmin,2.,xmax,2.);
912 TLine* lp3 =
new TLine(xmin,3.,xmax,3.);
913 TLine* lp4 =
new TLine(xmin,4.,xmax,4.);
915 TLine* lp6 =
new TLine(xmin,-1.,xmax,-1.);
916 TLine* lp7 =
new TLine(xmin,-2.,xmax,-2.);
917 TLine* lp8 =
new TLine(xmin,-3.,xmax,-3.);
918 TLine* lp9 =
new TLine(xmin,-4.,xmax,-4.);
920 TLine* lp10 =
new TLine(xmin,0,xmax,0);
922 lp1->SetLineStyle(3);
923 lp2->SetLineStyle(3);
924 lp3->SetLineStyle(3);
925 lp4->SetLineStyle(3);
926 lp6->SetLineStyle(3);
927 lp7->SetLineStyle(3);
928 lp8->SetLineStyle(3);
929 lp9->SetLineStyle(3);
930 lp10->SetLineStyle(3);
932 frame2->addObject(lp1);
933 frame2->addObject(lp2);
934 frame2->addObject(lp3);
935 frame2->addObject(lp4);
936 frame2->addObject(lp6);
937 frame2->addObject(lp7);
938 frame2->addObject(lp8);
939 frame2->addObject(lp9);
940 frame2->addObject(lp10);
942 frame2->SetMinimum(-4.0);
943 frame2->SetMaximum(4.0);
945 frame2->GetYaxis()->SetTitle(
"Pull");
947 else if(plottodraw ==
"residuals"){
948 TLine*
l =
new TLine(xmin,0.,xmax,0.);
951 frame2->addObject(l);
952 frame2->GetYaxis()->SetTitle(
"Residual");
954 else if(plottodraw ==
"ratio"){
955 TLine* lp1 =
new TLine(xmin,1.,xmax,1.);
956 TLine* lp2 =
new TLine(xmin,0.5,xmax,0.5);
957 TLine* lp3 =
new TLine(xmin,1.5,xmax,1.5);
958 TLine* lp4 =
new TLine(xmin,2.,xmax,2.);
959 TLine* lp5 =
new TLine(xmin,2.5,xmax,2.5);
960 lp1->SetLineWidth(1);
961 lp1->SetLineStyle(3);
962 lp2->SetLineStyle(3);
963 lp3->SetLineStyle(3);
964 lp4->SetLineStyle(3);
965 lp5->SetLineStyle(3);
966 frame2->addObject(lp1);
967 frame2->addObject(lp2);
968 frame2->addObject(lp3);
969 frame2->addObject(lp4);
970 frame2->addObject(lp5);
972 frame2->SetMinimum(.0);
973 frame2->SetMaximum(3.0);
975 frame2->GetYaxis()->SetTitle(
"Data / MC");
978 frame2->GetYaxis()->SetLabelSize(0.10);
979 frame2->GetYaxis()->SetNdivisions(504);
980 frame2->GetXaxis()->SetLabelSize(0.10);
981 frame2->GetYaxis()->SetTitleSize(0.10);
982 frame2->GetXaxis()->SetTitleSize(0.10);
983 frame2->GetYaxis()->SetTitleOffset(0.35);
984 frame2->GetXaxis()->SetTitleOffset(1.);
985 frame2->GetYaxis()->SetLabelOffset(0.01);
986 frame2->GetXaxis()->SetLabelOffset(0.03);
987 frame2->GetXaxis()->SetTickLength(0.06);
989 frame2->SetTitle(
"");
990 frame2->GetYaxis()->CenterTitle();
993 rooplots.push_back(c);
const std::string instance
int find(const type *d) const
void RemoveEmptyBins(RooPlot *frame)
double GetDataMCChi2(RooWorkspace *work, TString channelname, RooAbsData *data=NULL)
std::string to_string(ModuleType const mt)
QTextStream & endl(QTextStream &s)