Пример #1
0
	double FitConfidence::choleskyUncertainty( double xx, TFitResultPtr fitResult, TF1 * f, int nSamples ){
		int nP = f->GetNpar();

		TMatrixDSym cov = fitResult->GetCovarianceMatrix();
		double *covArray = new double[ nP * nP ]; // number of parameters x number of parameters
		covArray = cov.GetMatrixArray();

		return choleskyUncertainty( xx, covArray, f, nSamples );
	}
Пример #2
0
//Make pull plots from the output of a toy study
void ResultFormatter::WriteFlatNtuple( const string FileName, const FitResultVector* ToyResult, const vector<string> inputXML, const vector<string> runtimeArgs, const string XMLForProjections, const string XMLForToys )
{
	TFile * rootFile = TFile::Open( FileName.c_str(), "RECREATE" );
	rootFile->SetCompressionLevel( 9 );

	//cout << "Storing Fit Result Vector" << endl;

	//	Important!
	//	The output from this is typically run through the RapidPlot file
	//	For sake of backwards compatibility the RapidPlot tool makes use of the ability to look for the 'first' TTree in a ROOT file for some of it's internal logic
	//	KEEP THIS TREE AS THE FIRST TREE CREATED AND WRITTEN TO THE FILE TO BE ABLE TO KEEP USING THIS TOOL!!!
	TTree* outputTree = new TTree( "RapidFitResult", "RapidFitResult" );

	ResultParameterSet* resultSet = ToyResult->GetFitResult( 0 )->GetResultParameterSet();

	vector<string> allNames = resultSet->GetAllNames();

	for( unsigned int param_i=0; param_i< allNames.size(); ++param_i )
	{
		string thisParamName( allNames[param_i] );

		vector<double> ParameterValues, ParameterErrors, ParameterOriginalValues;
		vector<double> ParameterPulls, ParameterStepSizes;
		vector<double> ParameterErrorsHigh, ParameterErrorsLow;
		vector<double> ParameterMinimums, ParameterMaximums;
		vector<int> ParameterScanStatus, ParameterFixedStatus;
		for( unsigned int resultNum=0; resultNum< (unsigned)ToyResult->NumberResults(); ++resultNum )
		{
			ResultParameter* thisParam = ToyResult->GetFitResult( (unsigned)resultNum )->GetResultParameterSet()->GetResultParameter( thisParamName );

			ParameterValues.push_back( thisParam->GetValue() );
			ParameterErrors.push_back( thisParam->GetError() );
			ParameterPulls.push_back( thisParam->GetPull() );
			ParameterMinimums.push_back( thisParam->GetMinimum() );
			ParameterMaximums.push_back( thisParam->GetMaximum() );
			ParameterOriginalValues.push_back( thisParam->GetOriginalValue() );

			if( thisParam->GetType() == "Fixed" )
			{
				ParameterFixedStatus.push_back( 1 );
			}
			else
			{
				ParameterFixedStatus.push_back( 0 );
			}

			//if( thisParam->GetScanStatus() ) cout << "Scanned: " << string(thisParamName) << endl;

			if( thisParam->GetScanStatus() )
			{
				ParameterScanStatus.push_back( 1 );
			}
			else
			{
				ParameterScanStatus.push_back( 0 );
			}

			if( thisParam->GetAssym() )
			{
				ParameterErrorsHigh.push_back( thisParam->GetErrHi() );
				ParameterErrorsLow.push_back( thisParam->GetErrLow() );
			} else {
				ParameterErrorsHigh.push_back( 0. );
				ParameterErrorsLow.push_back( 0. );
			}
			ParameterStepSizes.push_back( thisParam->GetStepSize() );
		}
		string BranchName=allNames[param_i];
		ResultFormatter::AddBranch( outputTree, BranchName+"_value", ParameterValues );
		bool fixed_param = ToyResult->GetFitResult(0)->GetResultParameterSet()->GetResultParameter(thisParamName)->GetType() == "Fixed";
		bool scanned_param = ToyResult->GetFitResult(0)->GetResultParameterSet()->GetResultParameter(thisParamName)->GetScanStatus();
		if( (!fixed_param) || (scanned_param) )
		{
			ResultFormatter::AddBranch( outputTree, BranchName+"_error", ParameterErrors );
			ResultFormatter::AddBranch( outputTree, BranchName+"_gen", ParameterOriginalValues );
			ResultFormatter::AddBranch( outputTree, BranchName+"_pull", ParameterPulls );
			ResultFormatter::AddBranch( outputTree, BranchName+"_max", ParameterMaximums );
			ResultFormatter::AddBranch( outputTree, BranchName+"_min", ParameterMinimums );
			ResultFormatter::AddBranch( outputTree, BranchName+"_step", ParameterStepSizes );
			ResultFormatter::AddBranch( outputTree, BranchName+"_errHi", ParameterErrorsHigh );
			ResultFormatter::AddBranch( outputTree, BranchName+"_errLo", ParameterErrorsLow );
		}
		ResultFormatter::AddBranch( outputTree, BranchName+"_scan", ParameterScanStatus );
		ResultFormatter::AddBranch( outputTree, BranchName+"_fix", ParameterFixedStatus );
	}

	//cout << "Stored Parameters" << endl;

	ResultFormatter::AddBranch( outputTree, "Fit_RealTime", ToyResult->GetAllRealTimes() );
	ResultFormatter::AddBranch( outputTree, "Fit_CPUTime", ToyResult->GetAllCPUTimes() );
	ResultFormatter::AddBranch( outputTree, "Fit_GLTime", ToyResult->GetAllGLTimes() );

	vector<int> fitStatus;
	vector<double> NLL_Values, RealTimes, CPUTimes;
	for( unsigned int i=0; i< (unsigned) ToyResult->NumberResults(); ++i )
	{
		fitStatus.push_back( ToyResult->GetFitResult( (int)i )->GetFitStatus() );
		NLL_Values.push_back(  ToyResult->GetFitResult( (int)i )->GetMinimumValue() );
	}

	ResultFormatter::AddBranch( outputTree, "Fit_Status", fitStatus );
	ResultFormatter::AddBranch( outputTree, "NLL", NLL_Values );

	outputTree->Write("",TObject::kOverwrite);

	//ResultFormatter::AddBranch( outputTree, "Fit_RealTime", RealTimes );
	//ResultFormatter::AddBranch( outputTree, "Fit_CPUTime", CPUTimes );

	//	Ntuples are 'stupid' objects in ROOT and the basic one can only handle float type objects
	/*TNtuple * parameterNTuple;
	  parameterNTuple = new TNtuple("RapidFitResult", "RapidFitResult", ToyResult->GetFlatResultHeader());
	  Float_t * resultArr;
	  for( int resultIndex = 0; resultIndex < ToyResult->NumberResults(); ++resultIndex )
	  {
	  vector<double> result = ToyResult->GetFlatResult(resultIndex);
	  resultArr = new Float_t [result.size()];
	  copy( result.begin(), result.end(), resultArr);
	  parameterNTuple->Fill( resultArr );
	  delete [] resultArr;
	  }*/

	//cout << "Storing Correlation Matrix" << endl;

	if( ToyResult->GetFitResult(0)->GetResultParameterSet() != NULL )
	{
		if( !ToyResult->GetFitResult(0)->GetResultParameterSet()->GetAllFloatNames().empty() )
		{
			vector<double> MatrixElements; vector<string> MatrixNames;
			TTree * tree = new TTree("corr_matrix", "Elements from Correlation Matricies");
			tree->Branch("MartrixElements", "std::vector<double>", &MatrixElements );
			tree->Branch("MartrixNames", "std::vector<string>", &MatrixNames );
			for( int resultIndex = 0; resultIndex < ToyResult->NumberResults(); ++resultIndex )
			{
				TMatrixDSym* thisMatrix = ToyResult->GetFitResult(resultIndex)->GetCovarianceMatrix()->thisMatrix;
				if( thisMatrix == NULL ) continue;
				MatrixNames = ToyResult->GetFitResult(resultIndex)->GetCovarianceMatrix()->theseParameters;
				if( MatrixNames.empty() ) continue;
				double* MatrixArray = thisMatrix->GetMatrixArray();
				if( MatrixArray == NULL ) continue;
				MatrixElements.clear();
				for( unsigned int i=0; i< (unsigned) thisMatrix->GetNoElements(); ++i )
				{
					MatrixElements.push_back( MatrixArray[i] );
				}
				if( thisMatrix->GetNoElements() > 0 ) tree->Fill();
			}
			tree->Write("",TObject::kOverwrite);
		}
	}

	//cout << "Saving XML and runtime" << endl;

	if( !inputXML.empty() )
	{
		TTree* XMLTree = new TTree( "FittingXML", "FittingXML" );

		vector<string> thisXML = inputXML;

		XMLTree->Branch( "FittingXML", "std::vector<string>", &thisXML );

		XMLTree->Fill();

		XMLTree->Write("",TObject::kOverwrite);
	}
	if( !runtimeArgs.empty() )
	{
		TTree* RuntimeTree = new TTree( "RuntimeArgs", "RuntimeArgs" );

		vector<string> thisRuntime = runtimeArgs;

		RuntimeTree->Branch( "RuntimeArgs", "std::vector<string>", &thisRuntime );

		RuntimeTree->Fill();

		RuntimeTree->Write("",TObject::kOverwrite);
	}

	if( !XMLForProjections.empty() )
	{
		TTree* ProjectionXML = new TTree( "XMLForProjections", "XMLForProjections" );

		string thisXML = XMLForProjections;

		ProjectionXML->Branch( "ProjectionXML", "std::string", &thisXML );

		ProjectionXML->Fill();

		ProjectionXML->Write("",TObject::kOverwrite);
	}

        if( !XMLForToys.empty() )
        {
                TTree* ToyXML = new TTree( "XMLForToys", "XMLForToys" );

                string thisXML = XMLForToys;

                ToyXML->Branch( "ToyXML", "std::string", &thisXML );

                ToyXML->Fill();

                ToyXML->Write("",TObject::kOverwrite);
        }

	rootFile->Write("",TObject::kOverwrite);
	rootFile->Close();

	//	THIS SHOULD BE SAFE... BUT THIS IS ROOT so 'of course' it isn't...
	//delete parameterNTuple;
	//delete rootFile;
}
Пример #3
0
	TGraphErrors *FitConfidence::choleskyBands( TFitResultPtr fitResult, TF1 * f, int nSamples, int nPoints, Reporter* rp,  double x1, double x2 ){

		int nP = f->GetNpar();
		// INFO( FitConfidence::classname(), "Num Params : " << nP  );
		TMatrixDSym cov = fitResult->GetCovarianceMatrix();
		double *covArray = new double[ nP * nP ]; // number of parameters x number of parameters
		covArray = cov.GetMatrixArray();

		// for ( int i = 0; i < 9; i++ ){
		// 	INFO( FitConfidence::classname(), "[" << i << "] = " << covArray[ i ] );
		// }

		double *fCov = new double[ nP * nP ];
		fCov = cov.GetMatrixArray();
		double *fCovSqrt = new double[ nP * nP ];
		calcCholesky( nP, fCov, fCovSqrt );

		// for ( int i = 0; i < 9; i++ ){
		// 	INFO( FitConfidence::classname(), "[" << i << "] = " << fCovSqrt[ i ] );
		// }

		

		// calculate instead
		if ( -1.0 == x1  && -1.0 == x2 )
			f->GetRange( x1, x2 );

		double step = ( x2 - x1 ) / (double) nPoints;

		double x[ nPoints + 1 ];
		double y[ nPoints + 1 ];
		// double yup[ nPoints + 1 ];
		// double ydown[ nPoints + 1 ];
		double yerr[ nPoints + 1 ];
		vector<double> samples;

		int i = 0;
		for (double xx = x1; xx < x2; xx+= step) {
			x[i] = xx;
			TH1D *hDistributionAtX = new TH1D("hDistributionAtX","",200,f->Eval(x[i]) - .2,f->Eval(x[i]) + .2);
			for (int n = 0; n < nSamples; n++ ) {
				double val = randomSqrtCov(x[i],f,nP,fCovSqrt);
				hDistributionAtX->Fill( val );
				samples.push_back( val );
			}
			//hDistributionAtX->DrawCopy();

			y[i] 		= f->Eval(x[i]);
			yerr[i] 	= hDistributionAtX->GetRMS();
			
			// cross check - should always give the same result
			// but very innefficient
			//yerr[i] = choleskyUncertainty( xx, fitResult, f, nSamples );

			// rp.savePage();
			hDistributionAtX->Delete();
			i++;
		} 

		TGraphErrors * g = new TGraphErrors( i - 1, x, y, 0, yerr );

		return g;
	}
Пример #4
0
void fitToys2(){

  int count=0,countNotFail=0;

  gStyle->SetOptStat(0);

  TCanvas *c1 = new TCanvas("c1","c1",800,600);c1->cd();
  //TCanvas *c2 = new TCanvas("c2","c2",800,600);c2->cd();
  //c1->cd();

  TF1* fit = new TF1("fit","[0]*pow(x,[1])",103,163);
  fit->SetParameters(1.28542e+08,-3.90422);
  fit->SetParError(0,1.27534e+09);
  fit->SetParError(1,2.06166);

  TH1F* h_counts = new TH1F("h_counts","",2,0,2);
  TH1F* h_HiggsWindowFit = new TH1F("h_HiggsWindowFit","# events in Higgs window from fit",80,0,40);
  TH1F* h_pull = new TH1F("h_pull","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_pull_fail = new TH1F("h_pull_fail","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_pull_lowErr  = new TH1F("h_pull_lowErr","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_pull_highErr = new TH1F("h_pull_highErr","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_diff = new TH1F("h_diff","(fit - generated)",160,-40,40);
  TH1F* h_pullU = new TH1F("h_pullU","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_diffU = new TH1F("h_diffU","(fit - generated)",160,-40,40);
  TH1F* h_pullL = new TH1F("h_pullL","(fit - generated)/(fit error)",160,-8,8);
  TH1F* h_diffL = new TH1F("h_diffL","(fit - generated)",160,-40,40);
  h_pullL->SetLineColor(kRed);h_pullL->SetMarkerColor(kRed);
  h_diffL->SetLineColor(kRed);h_diffL->SetMarkerColor(kRed);
  h_pullU->SetLineColor(kBlue);h_pullU->SetMarkerColor(kBlue);
  h_diffU->SetLineColor(kBlue);h_diffU->SetMarkerColor(kBlue);
  h_pull_fail->SetFillColor(kRed);
  //  TRandom3 rr;

  for(int j=0;j<10;j++){
 
    TH1F* h = new TH1F("h","",60,103,163);
    for(int i=0;i<5200;i++){
      //for(int i=0;i<rr.Poisson(52);i++){
      h->Fill(fit->GetRandom(103,163));
    }
    
    //h->Draw("PE");
    
    reject=true;
    TF1* fitCurve = new TF1("fitCurve",fpow,103,163,2);
    
    Double_t avg_l = h->Integral(h->FindBin(103),h->FindBin(118))/float(118-103),avg_u = h->Integral(h->FindBin(133),h->FindBin(163))/float(163-133),avgX_l=(118-103)/2.,avgX_u=(163-133)/2.;
    cout<<avg_l<<"  "<<avg_u<<"  "<<avgX_l<<"  "<<avgX_u<<endl;
    Double_t param1= (log(avg_l) - log(avg_u))/(log(avgX_l) - log(avgX_u));
    Double_t param0= /*7e14;*/avg_l/pow(avgX_l, param1);
    cout<<"param0: "<<param0<<"  param1: "<<param1<<endl;
    fitCurve->SetParameter(0,param0);
    fitCurve->SetParameter(1,param1);
    int status = h->Fit(fitCurve,"L","",103,163);
    //Then to get the result
    TFitResultPtr fitResult = h->Fit(fitCurve,"SLLMEV0","",103,163);
    TMatrixDSym cov = fitResult->GetCovarianceMatrix();
    fitResult->Print("V");
    h->GetXaxis()->SetRangeUser(100,164.9);
    reject=false;
    float YieldBinWidth=h->GetBinWidth(1);
    Double_t PowYieldSig = h->Integral(h->FindBin(120),h->FindBin(131-.1))/YieldBinWidth;
    Double_t PowYieldSigFit = fitCurve->Integral(120,131)/YieldBinWidth;
    Double_t PowYieldSigFitErr = fitCurve->IntegralError(120,131,fitResult->GetParams(),cov.GetMatrixArray() )/YieldBinWidth; 
    Double_t PowSBloYield = h->Integral(h->FindBin(103),h->FindBin(118-.1))/YieldBinWidth;
    Double_t PowSBloYieldFit = fitCurve->Integral(103,118)/YieldBinWidth;
    Double_t PowSBloYieldFitErr = fitCurve->IntegralError(103,118,fitResult->GetParams(),cov.GetMatrixArray() )/YieldBinWidth; 
    Double_t PowSBhiYield = h->Integral(h->FindBin(133),h->FindBin(163-.1))/YieldBinWidth;
    Double_t PowSBhiYieldFit = fitCurve->Integral(133,163)/YieldBinWidth;
    Double_t PowSBhiYieldFitErr = fitCurve->IntegralError(133,163,fitResult->GetParams(),cov.GetMatrixArray() )/YieldBinWidth; 
    
    cout<<"gMinuit->fStatus : "<< gMinuit->fStatus <<"  gMinuit->fCstatu : "<< gMinuit->fCstatu<<endl;

    cout<<" low sideband yield from histo: "<<PowSBloYield<<"  and from fit: "<<PowSBloYieldFit<<" +- "<<PowSBloYieldFitErr<<endl; 
    cout<<" higgs window yield from histo: "<<PowYieldSig <<"  and from fit: "<<PowYieldSigFit<<" +- "<<PowYieldSigFitErr<<endl; 
    cout<<" high sideband yield from histo: "<<PowSBhiYield<<"  and from fit: "<<PowSBhiYieldFit<<" +- "<<PowSBhiYieldFitErr<<endl;
    reject=true;
    
    TString str = (TString)gMinuit->fCstatu;
    cout<<"str: "<<str;
    h_counts->Fill(0);
    if(str.Contains("FAILURE")){
      double pull_fail = (PowYieldSigFit-9.11)/PowYieldSigFitErr;
      h_pull_fail->Fill(pull_fail);
      //  continue;
    }
    h_counts->Fill(1);
    double pull = (PowYieldSigFit-9.11)/PowYieldSigFitErr;
    double pull_lowErr = (PowYieldSigFit-(9.11-1.6))/PowYieldSigFitErr;
    double pull_highErr = (PowYieldSigFit-(9.11+1.6))/PowYieldSigFitErr;
    double diff = (PowYieldSigFit-9.11);
    double pullL = (PowSBloYieldFit-PowSBloYield)/PowSBloYieldFitErr;
    double diffL = (PowSBloYieldFit-PowSBloYield);
    double pullU = (PowSBhiYieldFit-PowSBhiYield)/PowSBhiYieldFitErr;
    double diffU = (PowSBhiYieldFit-PowSBhiYield);
    h_pull->Fill(pull);
    h_pull_lowErr->Fill(pull_lowErr);
    h_pull_highErr->Fill(pull_highErr);
    h_diff->Fill(diff);
    h_pullU->Fill(pullU);
    h_diffU->Fill(diffU);
    h_pullL->Fill(pullL);
    h_diffL->Fill(diffL);
    h_HiggsWindowFit->Fill(PowYieldSigFit);
  }
  /*
  TFile fout("InvarMassFitToys.root","RECREATE");
  fout.cd();
  h_diff->Write();
  h_diffL->Write();
  h_diffU->Write();
  h_pull->Write();
  h_pull_lowErr->Write();
  h_pull_highErr->Write();
  h_pullL->Write();
  h_pullU->Write();
  h_HiggsWindowFit->Write();
  h_counts->Write();
  fout.Close();
  */
  //h->Draw();
  h_diffU->Draw();h_diff->Draw("SAMES");h_diffL->Draw("SAMES");
  TLegend *legd = new TLegend(.6,.55,.8,.85,"","brNDC");
  legd->SetFillStyle(0);legd->SetBorderSize(0);
  legd->AddEntry(h_diff,"Higgs window","l");
  legd->AddEntry(h_diffL,"lower sideband","l");
  legd->AddEntry(h_diffU,"upper sideband","l");
  legd->Draw();
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__diff.png");
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__diff.pdf");


  //c2->cd();
  h_pullU->Draw();h_pull->Draw("SAMES");h_pullL->Draw("SAMES");
  legd->Draw();
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__pull.png");
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__pull.pdf");


  h_diff->Fit("gaus");h_diff->SetTitle("Higgs window (fit - generated) with Gaussian fit");
  h_diff->Draw();
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__diff_gausFit.png");
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__diff_gausFit.pdf");

  h_pull->Fit("gaus");h_pull->SetTitle("Higgs window pull with Gaussian fit");//h_pull_fail->Fit("gaus");
  h_pull->Draw();//h_pull_fail->Draw("SAMES");
  c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__pull_gausFit_withFail.png");
  c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__pull_gausFit_withFail.pdf");

  h_HiggsWindowFit->Fit("gaus");
  h_HiggsWindowFit->Draw();
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__HiggsWindow_gausFit.png");
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__HiggsWindow_gausFit.pdf");
  
  h_counts->Draw();
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__counts.png");
  //c1->Print("Plots/Higgs/Exclusive_WeDataInvMassFit_toys__counts.pdf");

}