TGraph* smoothData( TGraphErrors* gin ) { TGraph* gout; // Kernel Smoother // create new kernel smoother and smooth data with bandwidth = 2.0 TGraphSmooth *gs = new TGraphSmooth("normal"); // gout = gs->SmoothKern(gin,"normal",20); // DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel"); // // redraw ksmooth with bandwidth = 5.0 // grout = gs->SmoothKern(grin,"normal",5.0); // DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","",""); // // // Lowess Smoother // // create new lowess smoother and smooth data with fraction f = 2/3 // grout = gs->SmoothLowess(grin,"",0.67); // DrawSmooth(3,"Lowess: f = 2/3","",""); // // // redraw lowess with fraction f = 0.2 // grout = gs->SmoothLowess(grin,"",0.2); // DrawSmooth(4,"Lowess: f = 0.2","",""); // // // Super Smoother // // create new super smoother and smooth data with default bass = 0 and span = 0 gout = gs->SmoothSuper(gin,"",0,0); // DrawSmooth(5,"Super Smoother: bass = 0","",""); // // // redraw supsmu with bass = 3 (smoother curve) // grout = gs->SmoothSuper(grin,"",3); // DrawSmooth(6,"Super Smoother: bass = 3","",""); // gout = gin; return gout; }
/// /// Make a plot for the CLs stuff. /// The strategy is to convert the hCLExp and hCLErr histogrms /// into TGraphs and TGraphAsymmErrors /// We can then provide some smoothing options as well /// /// \param s The scanner to plot. /// \param smooth /// void OneMinusClPlot::scan1dCLsPlot(MethodAbsScan *s, bool smooth, bool obsError) { if ( arg->debug ){ cout << "OneMinusClPlot::scan1dCLsPlot() : plotting "; cout << s->getName() << " (" << s->getMethodName() << ")" << endl; } m_mainCanvas->cd(); s->checkCLs(); TH1F *hObs = (TH1F*)s->getHCLsFreq()->Clone(getUniqueRootName()); TH1F *hExp = (TH1F*)s->getHCLsExp()->Clone(getUniqueRootName()); TH1F *hErr1Up = (TH1F*)s->getHCLsErr1Up()->Clone(getUniqueRootName()); TH1F *hErr1Dn = (TH1F*)s->getHCLsErr1Dn()->Clone(getUniqueRootName()); TH1F *hErr2Up = (TH1F*)s->getHCLsErr2Up()->Clone(getUniqueRootName()); TH1F *hErr2Dn = (TH1F*)s->getHCLsErr2Dn()->Clone(getUniqueRootName()); if ( !hObs ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hObs" << endl; if ( !hExp ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hExp" << endl; if ( !hErr1Up ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hErr1Up" << endl; if ( !hErr1Dn ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hErr1Dn" << endl; if ( !hErr2Up ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hErr2Up" << endl; if ( !hErr2Dn ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - can't find histogram hErr2Dn" << endl; // convert obs to graph TGraph *gObs = convertTH1ToTGraph(hObs,obsError); // convert others to raw graphs TGraph *gExpRaw = convertTH1ToTGraph(hExp); TGraph *gErr1UpRaw = convertTH1ToTGraph(hErr1Up); TGraph *gErr1DnRaw = convertTH1ToTGraph(hErr1Dn); TGraph *gErr2UpRaw = convertTH1ToTGraph(hErr2Up); TGraph *gErr2DnRaw = convertTH1ToTGraph(hErr2Dn); // smoothing if needed TGraph *gExp; TGraph *gErr1Up; TGraph *gErr1Dn; TGraph *gErr2Up; TGraph *gErr2Dn; TGraphSmooth *smoother = new TGraphSmooth(); if (smooth) { if ( arg->debug ) cout << "OneMinusClPlot::scan1dCLsPlot() : smoothing graphs" << endl; gExp = (TGraph*)smoother->SmoothSuper( gExpRaw )->Clone("gExp"); gErr1Up = (TGraph*)smoother->SmoothSuper( gErr1UpRaw )->Clone("gErr1Up"); gErr1Dn = (TGraph*)smoother->SmoothSuper( gErr1DnRaw )->Clone("gErr1Dn"); gErr2Up = (TGraph*)smoother->SmoothSuper( gErr2UpRaw )->Clone("gErr2Up"); gErr2Dn = (TGraph*)smoother->SmoothSuper( gErr2DnRaw )->Clone("gErr2Dn"); if ( arg->debug ) cout << "OneMinusClPlot::scan1dCLsPlot() : done smoothing graphs" << endl; } else { gExp = gExpRaw; gErr1Up = gErr1UpRaw; gErr1Dn = gErr1DnRaw; gErr2Up = gErr2UpRaw; gErr2Dn = gErr2DnRaw; } if ( !gObs ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gObs" << endl; if ( !gExp ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gExp" << endl; if ( !gErr1Up ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gErr1Up" << endl; if ( !gErr1Dn ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gErr1Dn" << endl; if ( !gErr2Up ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gErr2Up" << endl; if ( !gErr2Dn ) cout << "OneMinusClPlot::scan1dCLsPlot() : problem - null graph gErr2Dn" << endl; gObs->SetName("gObs"); gExp->SetName("gExp"); gErr1Up->SetName("gErr1Up"); gErr1Dn->SetName("gErr1Dn"); gErr2Up->SetName("gErr2Up"); gErr2Dn->SetName("gErr2Dn"); // now make the graphs for the error bands TGraphAsymmErrors *gErr1 = new TGraphAsymmErrors( gExp->GetN() ); gErr1->SetName("gErr1"); TGraphAsymmErrors *gErr2 = new TGraphAsymmErrors( gExp->GetN() ); gErr2->SetName("gErr2"); double x,y,yerrUp,yerrDn; double xerr = (hExp->GetBinCenter(2)-hExp->GetBinCenter(1))/2.; // protect against smoothing over 1 for (int i=0; i<gExp->GetN(); i++) { gExp->GetPoint(i,x,y); gExp->SetPoint(i,x,TMath::Min(y,1.)); gErr1Up->GetPoint(i,x,y); gErr1Up->SetPoint(i,x,TMath::Min(y,1.)); gErr1Dn->GetPoint(i,x,y); gErr1Dn->SetPoint(i,x,TMath::Min(y,1.)); gErr2Up->GetPoint(i,x,y); gErr2Up->SetPoint(i,x,TMath::Min(y,1.)); gErr2Dn->GetPoint(i,x,y); gErr2Dn->SetPoint(i,x,TMath::Min(y,1.)); } for (int i=0; i<gExp->GetN(); i++) { gExp->GetPoint(i,x,y); gErr1->SetPoint(i,x,y); gErr2->SetPoint(i,x,y); gErr1Up->GetPoint(i,x,yerrUp); gErr1Dn->GetPoint(i,x,yerrDn); gErr1->SetPointError(i, xerr, xerr, y-yerrDn, yerrUp-y ); gErr2Up->GetPoint(i,x,yerrUp); gErr2Dn->GetPoint(i,x,yerrDn); gErr2->SetPointError(i, xerr, xerr, y-yerrDn, yerrUp-y ); } gErr2->SetFillColor( TColor::GetColor("#3182bd") ); gErr2->SetLineColor( TColor::GetColor("#3182bd") ); gErr1->SetFillColor( TColor::GetColor("#9ecae1") ); gErr1->SetLineColor( TColor::GetColor("#9ecae1") ); gExp->SetLineColor(kRed); gExp->SetLineWidth(3); gObs->SetLineColor(kBlack); gObs->SetMarkerColor(kBlack); gObs->SetLineWidth(3); gObs->SetMarkerSize(1); gObs->SetMarkerStyle(20); float min = arg->scanrangeMin == arg->scanrangeMax ? hObs->GetXaxis()->GetXmin() : arg->scanrangeMin; float max = arg->scanrangeMin == arg->scanrangeMax ? hObs->GetXaxis()->GetXmax() : arg->scanrangeMax; TH1F *haxes = new TH1F("haxes"+getUniqueRootName(), "", 100, min, max); haxes->SetStats(0); haxes->GetXaxis()->SetTitle(s->getScanVar1()->GetTitle()); haxes->GetYaxis()->SetTitle("CL_{S}"); haxes->GetXaxis()->SetLabelFont(font); haxes->GetYaxis()->SetLabelFont(font); haxes->GetXaxis()->SetTitleFont(font); haxes->GetYaxis()->SetTitleFont(font); haxes->GetXaxis()->SetTitleOffset(0.9); haxes->GetYaxis()->SetTitleOffset(0.85); haxes->GetXaxis()->SetLabelSize(labelsize); haxes->GetYaxis()->SetLabelSize(labelsize); haxes->GetXaxis()->SetTitleSize(titlesize); haxes->GetYaxis()->SetTitleSize(titlesize); int xndiv = arg->ndiv==-1 ? 407 : abs(arg->ndiv); bool optimizeNdiv = arg->ndiv<0 ? true : false; haxes->GetXaxis()->SetNdivisions(xndiv, optimizeNdiv); haxes->GetYaxis()->SetNdivisions(407, true); haxes->GetYaxis()->SetRangeUser(0.,1.); // Legend: // make the legend short, the text will extend over the boundary, but the symbol will be shorter float legendXmin = 0.68 ; float legendYmin = 0.58 ; float legendXmax = legendXmin + 0.25 ; float legendYmax = legendYmin + 0.22 ; TLegend* leg = new TLegend(legendXmin,legendYmin,legendXmax,legendYmax); leg->SetFillColor(kWhite); leg->SetFillStyle(0); leg->SetLineColor(kWhite); leg->SetBorderSize(0); leg->SetTextFont(font); leg->SetTextSize(legendsize*0.75); if (obsError) leg->AddEntry( gObs, "Observed", "LEP" ); else leg->AddEntry( gObs, "Observed", "LP" ); leg->AddEntry( gExp, "Expected", "L" ); leg->AddEntry( gErr1, "#pm 1#sigma", "F"); leg->AddEntry( gErr2, "#pm 2#sigma", "F"); haxes->Draw("AXIS+"); gErr2->Draw("E3same"); gErr1->Draw("E3same"); gExp->Draw("Lsame"); if (obsError) gObs->Draw("LEPsame"); else gObs->Draw("LPsame"); leg->Draw("same"); drawCLguideLine(0.1); double yGroup = 0.83; if ( arg->plotprelim || arg->plotunoff ) yGroup = 0.8; drawGroup(yGroup); m_mainCanvas->SetTicks(); m_mainCanvas->RedrawAxis(); m_mainCanvas->Update(); m_mainCanvas->Modified(); m_mainCanvas->Show(); savePlot( m_mainCanvas, name+"_cls"+arg->plotext ); m_mainCanvas->SetTicks(false); }
void motorcycle() { // data taken from R library MASS: mcycle.txt TString dir = gROOT->GetTutorialDir(); dir.Append("/graphs/"); dir.ReplaceAll("/./","/"); // read file and add to fit object Double_t *x = new Double_t[133]; Double_t *y = new Double_t[133]; Double_t vX, vY; Int_t vNData = 0; ifstream vInput; vInput.open(Form("%smotorcycle.dat",dir.Data())); while (1) { vInput >> vX >> vY; if (!vInput.good()) break; x[vNData] = vX; y[vNData] = vY; vNData++; }//while vInput.close(); grin = new TGraph(vNData,x,y); // draw graph vC1 = new TCanvas("vC1","Smooth Regression",200,10,900,700); vC1->Divide(2,3); // Kernel Smoother // create new kernel smoother and smooth data with bandwidth = 2.0 TGraphSmooth *gs = new TGraphSmooth("normal"); grout = gs->SmoothKern(grin,"normal",2.0); DrawSmooth(1,"Kernel Smoother: bandwidth = 2.0","times","accel"); // redraw ksmooth with bandwidth = 5.0 grout = gs->SmoothKern(grin,"normal",5.0); DrawSmooth(2,"Kernel Smoother: bandwidth = 5.0","",""); // Lowess Smoother // create new lowess smoother and smooth data with fraction f = 2/3 grout = gs->SmoothLowess(grin,"",0.67); DrawSmooth(3,"Lowess: f = 2/3","",""); // redraw lowess with fraction f = 0.2 grout = gs->SmoothLowess(grin,"",0.2); DrawSmooth(4,"Lowess: f = 0.2","",""); // Super Smoother // create new super smoother and smooth data with default bass = 0 and span = 0 grout = gs->SmoothSuper(grin,"",0,0); DrawSmooth(5,"Super Smoother: bass = 0","",""); // redraw supsmu with bass = 3 (smoother curve) grout = gs->SmoothSuper(grin,"",3); DrawSmooth(6,"Super Smoother: bass = 3","",""); // cleanup delete [] x; delete [] y; delete gs; }