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 ); }
/// /// Build both the covariance and the correlation matrix /// (members covMatrix and corMatrix) /// from the stat and syst correlation matrics and the /// respective errors. /// void PDF_Abs::buildCov() { // add diagonals, symmetrize buildCorMatrix(corStatMatrix); buildCorMatrix(corSystMatrix); // make total cov matrix TMatrixDSym *covStat = buildCovMatrix(corStatMatrix, StatErr); TMatrixDSym *covSyst = buildCovMatrix(corSystMatrix, SystErr); covMatrix = *covStat + *covSyst; // check if total cov matrix is invertible if ( covMatrix.Determinant()==0 ) { cout << "PDF_Abs::buildCov() : ERROR : Total covariance matrix is not invertable (det(COV)=0)." << endl; cout << "PDF_Abs::buildCov() : ERROR : Check inputs! Ordering correct? Nobs correct?" << endl; cout << "PDF_Abs::buildCov() : PDF: " << name << endl; cout << "PDF_Abs::buildCov() : stat cov: " << endl; covStat->Print("v"); cout << "PDF_Abs::buildCov() : syst cov: " << endl; covSyst->Print("v"); cout << "PDF_Abs::buildCov() : full cov: " << endl; covMatrix.Print("v"); //exit(1); throw TString("need help"); } // make total cor matrix for ( int i=0; i<covMatrix.GetNcols(); i++ ) for ( int j=0; j<covMatrix.GetNcols(); j++ ) { corMatrix[i][j] = covMatrix[i][j]/sqrt(covMatrix[i][i])/sqrt(covMatrix[j][j]); } // check if total cor matrix is positive definite if ( ! isPosDef(&corMatrix) ) { cout << "PDF_Abs::buildCov() : ERROR : Total correlation matrix is not positive definite." << endl; cout << "PDF_Abs::buildCov() : ERROR : Check inputs! Ordering correct?" << endl; cout << "PDF_Abs::buildCov() : Sometimes this happens when for very large correlations" << endl; cout << "PDF_Abs::buildCov() : the given precision is not enough (e.g. rho=0.98 rather than 0.978)." << endl; cout << "PDF_Abs::buildCov() : PDF: " << name << endl; cout << "PDF_Abs::buildCov() : stat cor: " << endl; corStatMatrix.Print("v"); cout << "PDF_Abs::buildCov() : syst cor: " << endl; corSystMatrix.Print("v"); //exit(1); throw TString("need help"); } delete covStat; delete covSyst; // this is needed for the pull computation and the PDF_Abs::print() function: storeErrorsInObs(); }
//_________________________________________________ void TestJeffreysGaussSigma(){ // this one is VERY sensitive // if the Gaussian is narrow ~ range(x)/nbins(x) then the peak isn't resolved // and you get really bizzare shapes // if the Gaussian is too wide range(x) ~ sigma then PDF gets renormalized // and the PDF falls off too fast at high sigma RooWorkspace w("w"); w.factory("Gaussian::g(x[0,-20,20],mu[0,-5,5],sigma[1,1,5])"); w.factory("n[100,.1,2000]"); w.factory("ExtendPdf::p(g,n)"); // w.var("sigma")->setConstant(); w.var("mu")->setConstant(); w.var("n")->setConstant(); w.var("x")->setBins(301); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; // w.defineSet("poi","mu,sigma"); //w.defineSet("poi","mu,sigma,n"); w.defineSet("poi","sigma"); w.defineSet("obs","x"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",3); const RooArgSet* temp = w.set("poi"); pi.getParameters(*temp)->Print(); // return; // return; RooGenericPdf* test = new RooGenericPdf("test","test","sqrt(2.)/sigma",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("sigma")->frame(); pi.plotOn(plot); test->plotOn(plot,LineColor(kRed),LineStyle(kDotted)); plot->Draw(); }
/// /// Build a full correlation matrix by /// filling diagonal elements with unity /// and symmetrizing. /// void Utils::buildCorMatrix(TMatrixDSym &cor) { // fill diagonals for ( int i=0; i<cor.GetNcols(); i++ ) { cor[i][i] = 1.; } // symmetrize for ( int i=0; i<cor.GetNcols(); i++ ) for ( int j=0; j<cor.GetNcols(); j++ ) { if ( cor[i][j]!=0.0 && cor[j][i]==0.0 ) cor[j][i] = cor[i][j]; if ( cor[i][j]==0.0 && cor[j][i]!=0.0 ) cor[i][j] = cor[j][i]; } }
void LatexMaker::writeCorrMatrix( ofstream& file, TMatrixDSym mat, RooArgList *observables, vector<TString> labels ) { file << "\\begin{tabular}{ l |"; for ( int i=0; i < mat.GetNcols(); i++) file << "c"; file << "}" << endl; file << "\\hline" << endl; file << "\\hline" << endl; file << Form("%-15s",""); for ( int i=0; i < mat.GetNcols(); i++) { TString title = observables->at(i)->GetTitle(); title.ReplaceAll("#","\\"); if ( i < labels.size() ) title = labels[i]; file << Form(" & %s",title.Data()); } file << "\\\\" << endl; file << "\\hline" << endl; for ( int i=0; i < mat.GetNrows(); i++) { TString title = observables->at(i)->GetTitle(); title.ReplaceAll("#","\\"); if ( i < labels.size() ) title = labels[i]; file << Form("%-15s",title.Data()); for (int j=0; j < mat.GetNcols(); j++) { if ( mat[i][j] < 0 ) { file << Form(" & %4.2f",mat[i][j]); } else if ( TMath::Abs(mat[i][j]-1) < 1.e-6 ) { file << " & 1"; } else if ( mat[i][j] > 0 ) { file << Form(" & %4.2f",mat[i][j]); } else { file << " & 0"; } } file << " \\\\" << endl; } file << "\\hline" << endl; file << "\\hline" << endl; file << "\\end{tabular}" << endl; }
void JeffreysPriorDemo(){ RooWorkspace w("w"); w.factory("Uniform::u(x[0,1])"); w.factory("mu[100,1,200]"); w.factory("ExtendPdf::p(u,mu)"); // w.factory("Poisson::pois(n[0,inf],mu)"); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); // RooDataHist* asimov2 = w.pdf("pois")->generateBinned(*w.var("n"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; w.defineSet("poi","mu"); w.defineSet("obs","x"); // w.defineSet("obs2","n"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; // pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",10); // JeffreysPrior pi2("jeffreys2","jeffreys",*w.pdf("pois"),*w.set("poi"),*w.set("obs2")); // return; RooGenericPdf* test = new RooGenericPdf("test","test","1./sqrt(mu)",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("mu")->frame(); // pi.plotOn(plot, Normalization(1,RooAbsReal::Raw),Precision(.1)); pi.plotOn(plot); // pi2.plotOn(plot,LineColor(kGreen),LineStyle(kDotted)); test->plotOn(plot,LineColor(kRed)); plot->Draw(); }
//_________________________________________________ void TestJeffreysGaussMean(){ RooWorkspace w("w"); w.factory("Gaussian::g(x[0,-20,20],mu[0,-5,5],sigma[1,0,10])"); w.factory("n[10,.1,200]"); w.factory("ExtendPdf::p(g,n)"); w.var("sigma")->setConstant(); w.var("n")->setConstant(); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; // w.defineSet("poi","mu,sigma"); w.defineSet("poi","mu"); w.defineSet("obs","x"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; // pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",3); const RooArgSet* temp = w.set("poi"); pi.getParameters(*temp)->Print(); // return; RooGenericPdf* test = new RooGenericPdf("test","test","1",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("mu")->frame(); pi.plotOn(plot); test->plotOn(plot,LineColor(kRed),LineStyle(kDotted)); plot->Draw(); }
double operator() (double *x, double *p) { // 4 parameters int dim = X.GetNrows(); int k = 0; for (int i = 0; i<dim; ++i) { X[i] = x[i] - p[k]; k++; } for (int i = 0; i<dim; ++i) { CovMat(i,i) = p[k]*p[k]; k++; } for (int i = 0; i<dim; ++i) { for (int j = i+1; j<dim; ++j) { // p now are the correlations N(N-1)/2 CovMat(i,j) = p[k]*sqrt(CovMat(i,i)*CovMat(j,j)); CovMat(j,i) = CovMat(i,j); k++; } } if (debug) { X.Print(); CovMat.Print(); } double det = CovMat.Determinant(); if (det <= 0) { Fatal("GausND","Determinant is <= 0 det = %f",det); CovMat.Print(); return 0; } double norm = std::pow( 2. * TMath::Pi(), dim/2) * sqrt(det); // compute the gaussians CovMat.Invert(); double fval = std::exp( - 0.5 * CovMat.Similarity(X) )/ norm; if (debug) { std::cout << "det " << det << std::endl; std::cout << "norm " << norm << std::endl; std::cout << "fval " << fval << std::endl; } return fval; }
/// /// Return a submatrix of a given input matrix, defined by the rows /// and columns provided. /// /// \param source - the input matrix /// \param target - the output matrix /// \param indices - vector of the row/column indices that should make up the submatrix /// void PDF_Abs::getSubMatrix(TMatrixDSym& target, TMatrixDSym& source, vector<int>& indices) { if ( indices.size()==0 ){ cout << "PDF_Abs::getSubMatrix() : vector 'indices' can't be empty" << endl; exit(1); } if ( target.GetNcols() != indices.size() ){ cout << "PDF_Abs::getSubMatrix() : 'target' matrix doesn't have size of 'indices' vector" << endl; exit(1); } for ( int i=0; i<indices.size(); i++ ){ // check requested index if ( indices[i]<0 || indices[i]>=source.GetNcols() ){ cout << "PDF_Abs::getSubMatrix() : ERROR : requested index for submatrix is out of range of parent matrix" << endl; exit(1); } // copy over row and column for ( int j=0; j<indices.size(); j++ ){ target[i][j] = source[indices[i]][indices[j]]; target[j][i] = source[indices[j]][indices[i]]; } } }
void MultiGaus(const TVectorD& parMeans, const TMatrixDSym& covMatrix, TVectorD& genPars) { TRandom3 rnd(0); int nPars = parMeans.GetNrows(); if(nPars <= 0) { Error("MultiGaus", "Must have >0 pars"); return; } if(covMatrix.GetNrows() != nPars) { Error("MultiGaus", "parMeans.GetNrows() != covMatrix.GetNrows()"); return; } // Check that covMatrix is symmetric for(int iRow = 0; iRow < nPars; iRow++) { for(int iCol = iRow; iCol < nPars; iCol++) { if(covMatrix(iRow, iCol) != covMatrix(iCol, iRow)) { Error("MultiGaus", "malformed cov matrix at row %d, col %d", iRow, iCol); return; } } } genPars.ResizeTo(nPars); TMatrixDSymEigen eigenvariances(covMatrix); TMatrixD V = eigenvariances.GetEigenVectors(); TVectorD rotParMeans = V * parMeans; for(int iPar = 0; iPar < nPars; iPar++) { double variance = eigenvariances.GetEigenValues()[iPar]; // check for positive-definiteness of covMatrix if(variance < 0) { Error("MultiGaus", "Got a negative eigenvariance (%f) on iPar = %d", variance, iPar); } genPars[iPar] = rnd.Gaus(rotParMeans[iPar], sqrt(variance)); } V.Invert(); genPars = V * genPars; }
void KinZfitter::SetFitOutput(KinZfitter::FitInput &input, KinZfitter::FitOutput &output, double &l1, double &l2, double &lph1, double &lph2, vector<double> &pTerrsREFIT_lep, vector<double> &pTerrsREFIT_gamma, TMatrixDSym &covMatrixZ) { l1 = output.pT1_lep/input.pTRECO1_lep; l2 = output.pT2_lep/input.pTRECO2_lep; pTerrsREFIT_lep.push_back(output.pTErr1_lep); pTerrsREFIT_lep.push_back(output.pTErr2_lep); if (debug_) { cout << "lep1 pt before: " << input.pTRECO1_lep << ", lep1 pt after: " << output.pT1_lep << endl; cout << "lep2 pt before: " << input.pTRECO2_lep << ", lep2 pt after: " << output.pT2_lep << endl; } if (input.nFsr >= 1) { // lph1 = output.pT1_gamma/input.pTRECO1_gamma; // pTerrsREFIT_gamma.push_back(output.pTErr1_gamma); lph1 = 1; pTerrsREFIT_gamma.push_back(input.pTErr1_gamma); } if (input.nFsr == 2) { // lph2 = output.pT2_gamma/input.pTRECO2_gamma; // pTerrsREFIT_gamma.push_back(output.pTErr2_gamma); lph2 = 1; pTerrsREFIT_gamma.push_back(input.pTErr2_gamma); } int size = output.covMatrixZ.GetNcols(); covMatrixZ.ResizeTo(size,size); covMatrixZ = output.covMatrixZ; }
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; }
/// /// Set an external systematic correlation matrix. /// After modifying, call buildCov() and buildPdf(); /// void PDF_Abs::setSystCorrelation(TMatrixDSym &corSystMatrix) { assert(corSystMatrix.GetNcols()==nObs); this->corSystMatrix = corSystMatrix; corSource = corSource + " (syst. cor. set manually)"; }
//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; }
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"); }