Example #1
0
void nZHplots(string chan, string btag, string rootfilename, string textfilename){

  readHist data1(Form("output_%s_%sbtag/Single%s-Run2015D-v1_mZHLimit.root",              chan.data(),btag.data(),(chan=="ele") ? "Electron" : "Muon"));
  readHist data2(Form("output_%s_%sbtag/Single%s-Run2015D-v4_mZHLimit.root",              chan.data(),btag.data(),(chan=="ele") ? "Electron" : "Muon"));
  readHist dy100(Form("output_%s_%sbtag/DYJetsToLL_M-50_HT-100to200_13TeV_mZHLimit.root", chan.data(),btag.data()));
  readHist dy200(Form("output_%s_%sbtag/DYJetsToLL_M-50_HT-200to400_13TeV_mZHLimit.root", chan.data(),btag.data()));
  readHist dy400(Form("output_%s_%sbtag/DYJetsToLL_M-50_HT-400to600_13TeV_mZHLimit.root", chan.data(),btag.data()));
  readHist dy600(Form("output_%s_%sbtag/DYJetsToLL_M-50_HT-600toInf_13TeV_mZHLimit.root", chan.data(),btag.data()));
  readHist tt   (Form("output_%s_%sbtag/TT_TuneCUETP8M1_13TeV_mZHLimit.root",             chan.data(),btag.data()));
  readHist ww   (Form("output_%s_%sbtag/WW_TuneCUETP8M1_13TeV_mZHLimit.root",             chan.data(),btag.data()));
  readHist wz   (Form("output_%s_%sbtag/WZ_TuneCUETP8M1_13TeV_mZHLimit.root",             chan.data(),btag.data()));
  readHist zz   (Form("output_%s_%sbtag/ZZ_TuneCUETP8M1_13TeV_mZHLimit.root",             chan.data(),btag.data()));
  readHist zh   (Form("output_%s_%sbtag/ZH_HToBB_ZToLL_M125_13TeV_mZHLimit.root",         chan.data(),btag.data()));
  readHist m800 (Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-800_13TeV_mZHLimit.root",   chan.data(),btag.data()));
  readHist m1000(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-1000_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m1200(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-1200_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m1400(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-1400_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m1600(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-1600_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m1800(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-1800_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m2000(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-2000_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m2500(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-2500_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m3000(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-3000_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m3500(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-3500_13TeV_mZHLimit.root",  chan.data(),btag.data()));
  readHist m4000(Form("output_%s_%sbtag/ZprimeToZhToZlephbb_M-4000_13TeV_mZHLimit.root",  chan.data(),btag.data()));

  string histName = "mZH";

  TH1D* h_Data   = (TH1D*)(data1.getHist(histName.data()))->Clone("h_Data");
  TH1D* h_SubDom = (TH1D*)(tt.getHist(histName.data()))->Clone("h_SubDom");

  h_Data->Reset();
  h_SubDom->Reset();

  h_Data->Add(data1.getHist(histName.data()));
  h_Data->Add(data2.getHist(histName.data()));
    
  h_SubDom->Add(tt.getHist(histName.data()));
  h_SubDom->Add(ww.getHist(histName.data()));
  h_SubDom->Add(wz.getHist(histName.data()));
  h_SubDom->Add(zz.getHist(histName.data()));
  h_SubDom->Add(zh.getHist(histName.data()));

  TH1D* h_M800  = (TH1D*)(m800 .getHist(histName.data()));
  TH1D* h_M1000 = (TH1D*)(m1000.getHist(histName.data()));
  TH1D* h_M1200 = (TH1D*)(m1200.getHist(histName.data()));
  TH1D* h_M1400 = (TH1D*)(m1400.getHist(histName.data()));
  TH1D* h_M1600 = (TH1D*)(m1600.getHist(histName.data()));
  TH1D* h_M1800 = (TH1D*)(m1800.getHist(histName.data()));
  TH1D* h_M2000 = (TH1D*)(m2000.getHist(histName.data()));
  TH1D* h_M2500 = (TH1D*)(m2500.getHist(histName.data()));
  TH1D* h_M3000 = (TH1D*)(m3000.getHist(histName.data()));
  TH1D* h_M3500 = (TH1D*)(m3500.getHist(histName.data()));
  TH1D* h_M4000 = (TH1D*)(m4000.getHist(histName.data()));
        
  // dominant background is comming from alpha method

  TFile* f_Dom = TFile::Open(Form("systUncOnShapes/background_FitDev_root/background_FitDev_%s_cat%s.root",chan.data(),btag.data()));
  TH1D*  h_Dom = (TH1D*)(f_Dom->Get("background_FitDev"));

  TFile* outFile = new TFile(rootfilename.data(), "recreate");

  h_Data  ->Write("data_obs");		 
  h_Dom   ->Write("ZJETS");
  h_SubDom->Write("SUBDOM");
  h_M800  ->Write("SIGM800");
  h_M1000 ->Write("SIGM1000");
  h_M1200 ->Write("SIGM1200");
  h_M1400 ->Write("SIGM1400");
  h_M1600 ->Write("SIGM1600");
  h_M1800 ->Write("SIGM1800");
  h_M2000 ->Write("SIGM2000");
  h_M2500 ->Write("SIGM2500");
  h_M3000 ->Write("SIGM3000");
  h_M3500 ->Write("SIGM3500");
  h_M4000 ->Write("SIGM4000");

  outFile->Write();
  
  fstream ftext;
  ftext.open(textfilename.data(), ios::out);

  ftext << "DATA\t"   << h_Data  ->Integral() << "\n"; 
  ftext << "ZJETS\t"  << h_Dom   ->Integral() << "\n";
  ftext << "SUBDOM\t" << h_SubDom->Integral() << "\n";
  ftext << "M800\t"   << h_M800  ->Integral() << "\n";
  ftext << "M1000\t"  << h_M1000 ->Integral() << "\n";
  ftext << "M1200\t"  << h_M1200 ->Integral() << "\n";
  ftext << "M1400\t"  << h_M1400 ->Integral() << "\n";
  ftext << "M1600\t"  << h_M1600 ->Integral() << "\n";
  ftext << "M1800\t"  << h_M1800 ->Integral() << "\n";
  ftext << "M2000\t"  << h_M2000 ->Integral() << "\n";
  ftext << "M2500\t"  << h_M2500 ->Integral() << "\n";
  ftext << "M3000\t"  << h_M3000 ->Integral() << "\n";
  ftext << "M3500\t"  << h_M3500 ->Integral() << "\n";
  ftext << "M4000\t"  << h_M4000 ->Integral() << "\n";
  
  ftext.close();

}
void CLogitVGPiecewiseBoundLikelihood::precompute()
{
	//This function is based on the Matlab code
	//function [f, gm, gv] = Ellp(m, v, bound, ind), to compute common variables later
	//used in get_variational_expection and get_variational_first_derivative

	const Map<VectorXd> eigen_c(m_bound.get_column_vector(0), m_bound.num_rows);
	const Map<VectorXd> eigen_b(m_bound.get_column_vector(1), m_bound.num_rows);
	const Map<VectorXd> eigen_a(m_bound.get_column_vector(2), m_bound.num_rows);
	const Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
	const Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);

	Map<VectorXd> eigen_l(m_bound.get_column_vector(3), m_bound.num_rows);
	Map<VectorXd> eigen_h(m_bound.get_column_vector(4), m_bound.num_rows);

	index_t num_rows = m_bound.num_rows;
	index_t num_cols = m_mu.vlen;

	m_pl = SGMatrix<float64_t>(num_rows,num_cols);
	m_ph = SGMatrix<float64_t>(num_rows,num_cols);
	m_cdf_diff = SGMatrix<float64_t>(num_rows,num_cols);
	m_l2_plus_s2 = SGMatrix<float64_t>(num_rows,num_cols);
	m_h2_plus_s2 = SGMatrix<float64_t>(num_rows,num_cols);
	m_weighted_pdf_diff = SGMatrix<float64_t>(num_rows,num_cols);

	Map<MatrixXd> eigen_pl(m_pl.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_ph(m_ph.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_cdf_diff(m_cdf_diff.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_l2_plus_s2(m_l2_plus_s2.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_h2_plus_s2(m_h2_plus_s2.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_weighted_pdf_diff(m_weighted_pdf_diff.matrix, num_rows, num_cols);

	SGMatrix<float64_t> zl(num_rows, num_cols);
	Map<MatrixXd> eigen_zl(zl.matrix, num_rows, num_cols);
	SGMatrix<float64_t> zh(num_rows, num_cols);
	Map<MatrixXd> eigen_zh(zh.matrix, num_rows, num_cols);

	//bsxfun(@minus,l,m)
	eigen_zl = ((-eigen_mu).replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array()).matrix();
	//bsxfun(@minus,h,m)
	eigen_zh = ((-eigen_mu).replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array()).matrix();

	VectorXd eigen_s_inv = eigen_s2.array().sqrt().inverse().matrix();

	//zl = bsxfun(@times, bsxfun(@minus,l,m), 1./sqrt(v))
	eigen_zl = (eigen_zl.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
	//zh = bsxfun(@times, bsxfun(@minus,h,m), 1./sqrt(v))
	eigen_zh = (eigen_zh.array().rowwise()*eigen_s_inv.array().transpose()).matrix();

	//Usually we use pdf in log-domain and the log_sum_exp trick
	//to avoid numerical underflow in particular for IID samples
	for (index_t r = 0; r < zl.num_rows; r++)
	{
		for (index_t c = 0; c < zl.num_cols; c++)
		{
			if (CMath::abs(zl(r, c)) == CMath::INFTY)
				m_pl(r, c) = 0;
			else
				m_pl(r, c) = CMath::exp(CGaussianDistribution::univariate_log_pdf(zl(r, c)));

			if (CMath::abs(zh(r, c)) == CMath::INFTY)
				m_ph(r, c) = 0;
			else
				m_ph(r, c) = CMath::exp(CGaussianDistribution::univariate_log_pdf(zh(r, c)));
		}
	}

	//pl = bsxfun(@times, normpdf(zl), 1./sqrt(v));
	eigen_pl = (eigen_pl.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
	//ph = bsxfun(@times, normpdf(zh), 1./sqrt(v));
	eigen_ph = (eigen_ph.array().rowwise()*eigen_s_inv.array().transpose()).matrix();

	SGMatrix<float64_t> & cl = zl;
	SGMatrix<float64_t> & ch = zh;

	for (index_t r = 0; r < zl.num_rows; r++)
	{
		for (index_t c = 0; c < zl.num_cols; c++)
		{
			//cl = 0.5*erf(zl/sqrt(2)); %normal cdf -const
			cl(r, c) = CStatistics::normal_cdf(zl(r, c)) - 0.5;
			//ch = 0.5*erf(zl/sqrt(2)); %normal cdf -const
			ch(r, c) = CStatistics::normal_cdf(zh(r, c)) - 0.5;
		}
	}

	Map<MatrixXd> eigen_cl(cl.matrix, num_rows, num_cols);
	Map<MatrixXd> eigen_ch(ch.matrix, num_rows, num_cols);

	eigen_cdf_diff = eigen_ch - eigen_cl;

	float64_t l_bak = eigen_l(0);
	eigen_l(0) = 0;

	float64_t h_bak = eigen_h(eigen_h.size()-1);
	eigen_h(eigen_h.size()-1) = 0;

	//bsxfun(@plus, l.^2, v)
	eigen_l2_plus_s2 = (eigen_s2.replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array().pow(2)).matrix();
	//bsxfun(@plus, h.^2, v)
	eigen_h2_plus_s2 = (eigen_s2.replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array().pow(2)).matrix();
	//pl.*l - ph.*h
	eigen_weighted_pdf_diff = (eigen_pl.array().colwise() * eigen_l.array() - eigen_ph.array().colwise() * eigen_h.array()).matrix();

	eigen_l(0) = l_bak;
	eigen_h(eigen_h.size()-1) = h_bak;
}
/**
 * Test various generic API methods of PluralRules for API coverage.
 */
void PluralRulesTest::testAPI(/*char *par*/)
{
    UnicodeString pluralTestData[PLURAL_TEST_NUM] = {
            UNICODE_STRING_SIMPLE("a: n is 1"),
            UNICODE_STRING_SIMPLE("a: n mod 10 is 2"),
            UNICODE_STRING_SIMPLE("a: n is not 1"),
            UNICODE_STRING_SIMPLE("a: n mod 3 is not 1"),
            UNICODE_STRING_SIMPLE("a: n in 2..5"),
            UNICODE_STRING_SIMPLE("a: n within 2..5"),
            UNICODE_STRING_SIMPLE("a: n not in 2..5"),
            UNICODE_STRING_SIMPLE("a: n not within 2..5"),
            UNICODE_STRING_SIMPLE("a: n mod 10 in 2..5"),
            UNICODE_STRING_SIMPLE("a: n mod 10 within 2..5"),
            UNICODE_STRING_SIMPLE("a: n mod 10 is 2 and n is not 12"),
            UNICODE_STRING_SIMPLE("a: n mod 10 in 2..3 or n mod 10 is 5"),
            UNICODE_STRING_SIMPLE("a: n mod 10 within 2..3 or n mod 10 is 5"),
            UNICODE_STRING_SIMPLE("a: n is 1 or n is 4 or n is 23"),
            UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n in 1..11"),
            UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n within 1..11"),
            UNICODE_STRING_SIMPLE("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6"),
            "",
    };
    static const int32_t pluralTestResult[PLURAL_TEST_NUM][30] = {
        {1, 0},
        {2,12,22, 0},
        {0,2,3,4,5,0},
        {0,2,3,5,6,8,9,0},
        {2,3,4,5,0},
        {2,3,4,5,0},
        {0,1,6,7,8, 0},
        {0,1,6,7,8, 0},
        {2,3,4,5,12,13,14,15,22,23,24,25,0},
        {2,3,4,5,12,13,14,15,22,23,24,25,0},
        {2,22,32,42,0},
        {2,3,5,12,13,15,22,23,25,0},
        {2,3,5,12,13,15,22,23,25,0},
        {1,4,23,0},
        {1,5,7,9,11,0},
        {1,5,7,9,11,0},
        {1,3,5,7,9,11,13,15,16,0},
    };
    UErrorCode status = U_ZERO_ERROR;

    // ======= Test constructors
    logln("Testing PluralRules constructors");


    logln("\n start default locale test case ..\n");

    PluralRules defRule(status);
    LocalPointer<PluralRules> test(new PluralRules(status));
    LocalPointer<PluralRules> newEnPlural(test->forLocale(Locale::getEnglish(), status));
    if(U_FAILURE(status)) {
        dataerrln("ERROR: Could not create PluralRules (default) - exitting");
        return;
    }

    // ======= Test clone, assignment operator && == operator.
    LocalPointer<PluralRules> dupRule(defRule.clone());
    if (dupRule==NULL) {
        errln("ERROR: clone plural rules test failed!");
        return;
    } else {
        if ( *dupRule != defRule ) {
            errln("ERROR:  clone plural rules test failed!");
        }
    }
    *dupRule = *newEnPlural;
    if (dupRule!=NULL) {
        if ( *dupRule != *newEnPlural ) {
            errln("ERROR:  clone plural rules test failed!");
        }
    }

    // ======= Test empty plural rules
    logln("Testing Simple PluralRules");

    LocalPointer<PluralRules> empRule(test->createRules(UNICODE_STRING_SIMPLE("a:n"), status));
    UnicodeString key;
    for (int32_t i=0; i<10; ++i) {
        key = empRule->select(i);
        if ( key.charAt(0)!= 0x61 ) { // 'a'
            errln("ERROR:  empty plural rules test failed! - exitting");
        }
    }

    // ======= Test simple plural rules
    logln("Testing Simple PluralRules");

    char result[100];
    int32_t max;

    for (int32_t i=0; i<PLURAL_TEST_NUM-1; ++i) {
       LocalPointer<PluralRules> newRules(test->createRules(pluralTestData[i], status));
       setupResult(pluralTestResult[i], result, &max);
       if ( !checkEqual(*newRules, result, max) ) {
            errln("ERROR:  simple plural rules failed! - exitting");
            return;
        }
    }

    // ======= Test complex plural rules
    logln("Testing Complex PluralRules");
    // TODO: the complex test data is hard coded. It's better to implement
    // a parser to parse the test data.
    UnicodeString complexRule = UNICODE_STRING_SIMPLE("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1");
    UnicodeString complexRule2 = UNICODE_STRING_SIMPLE("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1");
    char cRuleResult[] =
    {
       0x6F, // 'o'
       0x63, // 'c'
       0x61, // 'a'
       0x61, // 'a'
       0x61, // 'a'
       0x61, // 'a'
       0x62, // 'b'
       0x62, // 'b'
       0x62, // 'b'
       0x63, // 'c'
       0x6F, // 'o'
       0x63  // 'c'
    };
    LocalPointer<PluralRules> newRules(test->createRules(complexRule, status));
    if ( !checkEqual(*newRules, cRuleResult, 12) ) {
         errln("ERROR:  complex plural rules failed! - exitting");
         return;
    }
    newRules.adoptInstead(test->createRules(complexRule2, status));
    if ( !checkEqual(*newRules, cRuleResult, 12) ) {
         errln("ERROR:  complex plural rules failed! - exitting");
         return;
    }

    // ======= Test decimal fractions plural rules
    UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;");
    UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a");
    status = U_ZERO_ERROR;
    newRules.adoptInstead(test->createRules(decimalRule, status));
    if (U_FAILURE(status)) {
        dataerrln("ERROR: Could not create PluralRules for testing fractions - exitting");
        return;
    }
    double fData[] =     {-101, -100, -1,     -0.0,  0,     0.1,  1,     1.999,  2.0,   100,   100.001 };
    UBool isKeywordA[] = {TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE,   FALSE, FALSE, TRUE };
    for (int32_t i=0; i<LENGTHOF(fData); i++) {
        if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) {
             errln("File %s, Line %d, ERROR: plural rules for decimal fractions test failed!\n"
                   "  number = %g, expected %s", __FILE__, __LINE__, fData[i], isKeywordA[i]?"TRUE":"FALSE");
        }
    }

    // ======= Test Equality
    logln("Testing Equality of PluralRules");

    if ( !testEquality(*test) ) {
         errln("ERROR:  complex plural rules failed! - exitting");
         return;
     }


    // ======= Test getStaticClassID()
    logln("Testing getStaticClassID()");

    if(test->getDynamicClassID() != PluralRules::getStaticClassID()) {
        errln("ERROR: getDynamicClassID() didn't return the expected value");
    }
    // ====== Test fallback to parent locale
    LocalPointer<PluralRules> en_UK(test->forLocale(Locale::getUK(), status));
    LocalPointer<PluralRules> en(test->forLocale(Locale::getEnglish(), status));
    if (en_UK.isValid() && en.isValid()) {
        if ( *en_UK != *en ) {
            errln("ERROR:  test locale fallback failed!");
        }
    }

    LocalPointer<PluralRules> zh_Hant(test->forLocale(Locale::getTaiwan(), status));
    LocalPointer<PluralRules> zh(test->forLocale(Locale::getChinese(), status));
    if (zh_Hant.isValid() && zh.isValid()) {
        if ( *zh_Hant != *zh ) {
            errln("ERROR:  test locale fallback failed!");
        }
    }
}