Ejemplo n.º 1
0
void getLimitCL95(std::string oDir, 
		  double ilum = 195600., 
		  double slum = 558.) {
  //
  // compute limit in a counting experiment
  //

  // read backgrounds etc from target dir
  std::cout << "Backgrounds" << std::endl;
  double bgZjets(0.), err_bgZjets(0.);
  double bgWjets(0.), err_bgWjets(0.);
  double bgQCD(0.), err_bgQCD(0.);

  ifstream file;
  double n, err_n;
  std::string name;

  file.open( (oDir+std::string("/zjets.txt")).c_str() );
  while (file >> name >> n >> err_n) {
    if (name == "Signal") {
      bgZjets = n;
      err_bgZjets = err_n;
    }
  }
  file.close();

  std::cout << "Z+jets  : " << bgZjets << " +/- " << err_bgZjets << std::endl;

  file.open( (oDir+std::string("/wjets.txt")).c_str() );
  while (file >> name >> n >> err_n) {
    if (name == "Signal") {
      bgWjets = n;
      err_bgWjets = err_n;
    }
  }
  file.close();

  std::cout << "W+jets  : " << bgWjets << " +/- " << err_bgWjets << std::endl;

  file.open( (oDir+std::string("/qcd.txt")).c_str() );
  while (file >> name >> n >> err_n) {
    if (name == "Signal") {
      bgQCD = n;
      err_bgQCD = err_n;
    }
  }
  file.close();

  std::cout << "QCD     : " << bgQCD << " +/- " << err_bgQCD << std::endl;

  // total BG
  double bgTot     = bgZjets + bgWjets + bgQCD;
  double err_bgTot = sqrt( pow(err_bgZjets, 2) + pow(err_bgWjets, 2) + pow(err_bgQCD, 2) );

  std::cout << "Total   : " << bgTot << " +/- " << err_bgTot << std::endl;
  std::cout << std::endl;

  // observed
  std::cout << "Observed" << std::endl;
  int nObs = int(bgTot);

//   file.open((oDir+std::string("/obs.txt")).c_str());
//   while (file >> name >> n) {
//     if (name == "Signal") {
//       nObs = n;
//     }
//   }
//   file.close();

  std::cout << "Obs     : " << nObs << std::endl;
  std::cout << std::endl;

  // read signal efficiencies from file
  std::cout << "Signal efficiency" << std::endl;
  std::vector<double> mH, xsH, effSignal, err_effSignal;

  file.open( (oDir+std::string("/signal.txt")).c_str() );
  std::string line;
  getline(file, line);

  int i=0;
  double m, eff, err, a,b,c,d,x;
  while (file >> name >> m >> eff >> err >> a >> b >> c >> d >> x) {
    mH.push_back( m );
    xsH.push_back( x );
    effSignal.push_back( eff );
    err_effSignal.push_back( err );
    std::cout << "Signal " << m << " xs=" << x << " : " << eff << " +/- " << err << std::endl;
  }
  file.close();

  std::cout << std::endl;

  std::cout << "Going to compute limit for mH=" << mH[0] << ".  VBF x-section=" << xsH[0] << std::endl;
  std::cout << std::endl;
  

  // optional: set some parameters
  SetParameter("Optimize", false);
  SetParameter("CorrelatedLumiSyst", false);
  SetParameter("MakePlot", true);
  SetParameter("GaussianStatistics", false);

  SetParameter("NClsSteps", 10);
  SetParameter("NToys", 1000);
  SetParameter("CalculatorType", 0); // 0 for frequentist
  SetParameter("TestStatType", 3); // LHC-style 1-sided profile likelihood
  SetParameter("Verbosity", 3);
  SetParameter("RandomSeed", 0);

  SetParameter("ConfidenceLevel", 0.95);
  SetParameter("NToysRatio", 2.0);

  
  // will work with any method, example shown for Bayesian
  LimitResult expected = GetExpectedLimit(ilum, slum,
					  effSignal[0], err_effSignal[0],
					  bgTot, err_bgTot,
					  100,
					  "bayesian");
    
  std::cout << " expected limit (median) " << expected.GetExpectedLimit() << std::endl;
  std::cout << " expected limit (-1 sig) " << expected.GetOneSigmaLowRange() << std::endl;
  std::cout << " expected limit (+1 sig) " << expected.GetOneSigmaHighRange() << std::endl;
  std::cout << " expected limit (-2 sig) " << expected.GetTwoSigmaLowRange() << std::endl;
  std::cout << " expected limit (+2 sig) " << expected.GetTwoSigmaHighRange() << std::endl;

  // write summary file
  ofstream oFile;
  oFile.open( (oDir+std::string("/RooStatsCL95.txt")).c_str() );

  oFile << "Lumi=" << ilum << " +/- " << slum << std::endl;
  oFile << std::endl;
  oFile << "Backgrounds" << std::endl;
  oFile << "Z+jets       : " << bgZjets << " +/- " << err_bgZjets << std::endl;
  oFile << "W+jets       : " << bgWjets << " +/- " << err_bgWjets << std::endl;
  oFile << "QCD          : " << bgQCD   << " +/- " << err_bgQCD << std::endl;
  oFile << "Total        : " << bgTot << " +/- " << err_bgTot << std::endl;
  oFile << std::endl;
  oFile << "Observed     : " << nObs << std::endl;
  oFile << std::endl;
  oFile << "Signal point" << std::endl;
  oFile << "mH=" << mH[0] << ".  VBF x-section=" << xsH[0] << std::endl;
  oFile << "eff=" << effSignal[0] << " +/- " << err_effSignal[0] << std::endl;
  oFile << "yield (100% inv BF)=" << int(xsH[0]*ilum*effSignal[0]) << std::endl;
  oFile << std::endl;
  oFile << "Results" << std::endl;
  oFile << "  expected limit (median) " << expected.GetExpectedLimit() << std::endl;
  oFile << "  expected limit (-1 sig) " << expected.GetOneSigmaLowRange() << std::endl;
  oFile << "  expected limit (+1 sig) " << expected.GetOneSigmaHighRange() << std::endl;
  oFile << "  expected limit (-2 sig) " << expected.GetTwoSigmaLowRange() << std::endl;
  oFile << "  expected limit (+2 sig) " << expected.GetTwoSigmaHighRange() << std::endl;
  oFile << std::endl;

  oFile.close();

  return;
}
Ejemplo n.º 2
0
void
CalcLimit(bool useCLs=true, string inName="nEvents.txt", string outName="nLimit.txt"){
  gErrorIgnoreLevel = kWarning;
  gSystem->SetIncludePath( "-I$ROOFITSYS/include" );
  gSystem->Load("libRooFit");
//  gSystem->SetIncludePath( "-I/afs/hep.wisc.edu/cern/.root/root_v5.30.00.Linux-slc5_amd64-gcc4.3/include/RooStats" );
  gROOT->ProcessLine(".L ../../../StatisticalTools/RooStatsRoutines/root/roostats_cl95.C+");
  
  string outfile(outName.c_str());
  ofstream out(outfile.c_str());
  if(!out) { 
    cout << "Cannot open file " << outfile << endl; 
    abort();
  } 
  
  out  << setiosflags(ios::fixed) << setprecision(4) << setiosflags(ios::left);
    
  out<<"SignalCode/F:"
     <<"Mass/F:"
     <<"Lumi/F:"
     <<"sLumi/F:"
     <<"Eff/F:"
     <<"sEff/F:"
     <<"DataEvts/F:"
     <<"BkgEvts/F:"
     <<"sBkgEvts/F:"
     <<"ObsLimit/F:"
     <<"ExpLimit/F:"
     <<"ExpLimitP1/F:"
     <<"ExpLimitM1/F:"
     <<"ExpLimitP2/F:"
     <<"ExpLimitM2/F"
     <<endl;

  TTree* tree = new TTree("tree", "Number of Events");
  tree->ReadFile(inName.c_str());
  tree->Draw("SignalCode:Mass:Lumi:DataEvts:BkgEvts:sBkgEvts:Eff:sEff", 
             "", "para goff");
  float n = tree->GetSelectedRows(); 
  for(int isample=0; isample<n; ++isample){
    const Double_t SignalCode = tree->GetVal(0)[isample];
    const Double_t  mass = tree->GetVal(1)[isample];
    const Double_t  lumi = tree->GetVal(2)[isample];
    const Double_t  DataEvts = tree->GetVal(3)[isample];
    const Double_t    BkgEvts = tree->GetVal(4)[isample];
    const Double_t   sBkgEvts = tree->GetVal(5)[isample];
    const Double_t       Eff = tree->GetVal(6)[isample];
    const Double_t      sEff = tree->GetVal(7)[isample];
    
    cout<<"Calculating limit for mass: "<<mass<<" and lumi: "<<lumi<<endl;
        
    float sLumi = sLumiFrac*lumi;
    
    Double_t obs_limit, exp_limit;
    Double_t exp_up, exp_down, exp_2up, exp_2down;
    
    if(useCLs){////CLs Limits
      //Does not work for bayesian, only works with cls    
      LimitResult limit = roostats_limit(lumi, sLumi, Eff, sEff, BkgEvts, sBkgEvts, DataEvts, false, 0, "cls", "", 12345);
      cout<<"\nCompleted Limit Calc\n";
      obs_limit = limit.GetObservedLimit();
      exp_limit = limit.GetExpectedLimit();
      exp_up    = limit.GetOneSigmaHighRange();
      exp_down  = limit.GetOneSigmaLowRange();
      exp_2up   = limit.GetTwoSigmaHighRange();
      exp_2down = limit.GetTwoSigmaLowRange();        
    }else{
      ////Bayesian Limits
      LimitResult limit  = roostats_clm (lumi, sLumi, Eff, sEff, BkgEvts, sBkgEvts);
      //obs_limit = limit.GetObservedLimit();
      obs_limit = roostats_cl95(lumi, sLumi, Eff, sEff, BkgEvts, sBkgEvts, DataEvts, false, 0, "bayesian", "");
      
      exp_limit = limit.GetExpectedLimit();
      exp_up    = limit.GetOneSigmaHighRange();
      exp_down  = limit.GetOneSigmaLowRange();
      exp_2up   = limit.GetTwoSigmaHighRange();
      exp_2down = limit.GetTwoSigmaLowRange();        
    }
  
    out<<setprecision(1)
       <<SignalCode<<"\t"
       <<setprecision(0)
       <<mass<<"\t"
       <<lumi<<"\t"
       <<sLumi<<"\t"
       <<setprecision(4)
       <<Eff<<"\t"
       <<sEff<<"\t"
       <<setprecision(0)
       <<DataEvts<<"\t"
       <<setprecision(4)
       <<BkgEvts<<"\t"
       <<sBkgEvts<<"\t";
//    out<<Value(obs_limit,-4)<<"\t"
//       <<Value(exp_limit,-4)<<"\t"
//       <<Value(exp_up,-4)<<"\t"
//       <<Value(exp_down,-4)<<"\t"
//       <<Value(exp_2up,-4)<<"\t"
//       <<Value(exp_2down,-4)
//       <<endl;
    out<<setprecision(8)
       <<obs_limit<<"\t"
       <<exp_limit<<"\t"
       <<exp_up<<"\t"
       <<exp_down<<"\t"
       <<exp_2up<<"\t"
       <<exp_2down
       <<endl;
  }
  
  out.close(); 
  cout<<"Done\n";
  return;
}