示例#1
0
文件: RA4abcd.C 项目: wa01/usercode
//
// scan over parameter space
//
void RA4Mult (const RA4WorkingPoint& muChannel,
	      const RA4WorkingPoint& eleChannel,
	      StatMethod method) {

  //
  // Prepare workspace
  //   no syst. parameters: efficiency / sig.cont. / kappa
  //
  bool noEffSyst(false);
  bool noSContSyst(false);
  bool noKappaSyst(false);
  RA4WorkSpace ra4WSpace("wspace",noEffSyst,noSContSyst,noKappaSyst);


  TFile* fYield[2];
  TFile* fKFactor[2];
  //
  // Muon channel
  //
  unsigned int nf(0);
  RA4WorkSpace::ChannelType channelTypes[2];
  const RA4WorkingPoint* workingPoints[2];
  addChannel(muChannel,RA4WorkSpace::MuChannel,ra4WSpace,fYield,fKFactor,
	     nf,channelTypes,workingPoints);
  addChannel(eleChannel,RA4WorkSpace::EleChannel,ra4WSpace,fYield,fKFactor,
	     nf,channelTypes,workingPoints);
  if ( nf==0 ) {
    std::cout << "No input file" << std::endl;
    return;
  }
  //
  // finish definition of model
  //
  ra4WSpace.finalize();
  RooWorkspace* wspace = ra4WSpace.workspace();
//   wspace->Print("v");
//   RooArgSet allVars = wspace->allVars();
//   // allVars.printLatex(std::cout,1);
//   TIterator* it = allVars.createIterator();
//   RooRealVar* var;
//   while ( var=(RooRealVar*)it->Next() ) {
//     var->Print("v");
//     var->printValue(std::cout);
//   }
  //
  // preparation of histograms with yields and k-factors
  //
  const char* cRegion = { "ABCD" };
  TH2* hYields[4][2];
  TH2* hYields05[4][2];
  TH2* hYields20[4][2];
  TH2* hYEntries[4][2];
  TH2* hYESmooth[4][2];
  for ( unsigned int j=0; j<nf; ++j ) {
    for ( unsigned int i=0; i<4; ++i ) {
      hYields[i][j] = 0;
      hYields05[i][j] = 0;
      hYields20[i][j] = 0;
      hYEntries[i][j] = 0;
      hYESmooth[i][j] = 0;
    }
  }
  TH2* hKF05[2];
  TH2* hKF10[2];
  TH2* hKF20[2];
  for ( unsigned int j=0; j<nf; ++j ) {
    hKF05[j] = 0;
    hKF10[j] = 0;
    hKF20[j] = 0;
  }
  //
  // Retrieval of histograms with k-factors
  //
  for ( unsigned int j=0; j<nf; ++j ) {
    hKF05[j] = (TH2*)fKFactor[j]->Get("hKF05D");
    hKF10[j] = (TH2*)fKFactor[j]->Get("hKF10D");
    hKF20[j] = (TH2*)fKFactor[j]->Get("hKF20D");
    if ( hKF05[j]==0 || hKF10==0 || hKF20==0 ) {
      std::cout << "Missing histogram for kfactor for channel " << j << std::endl;
      return;
    }
  }
  //
  // Retrieval of histograms with yields
  //
  std::string hName;
  for ( unsigned int j=0; j<nf; ++j ) {
    for ( unsigned int i=0; i<4; ++i ) {
      hName = "Events";
      hName += cRegion[i];

      hYields[i][j] = (TH2*)fYield[j]->Get(hName.c_str())->Clone();
      hYields05[i][j] = (TH2*)fYield[j]->Get(hName.c_str())->Clone();
      hYields20[i][j] = (TH2*)fYield[j]->Get(hName.c_str())->Clone();
      if ( hYields[i][j]==0 ) {
	std::cout << "Missing histogram for region " << cRegion[i] << std::endl;
	return;
      }
      hYields[i][j]->Multiply(hYields[i][j],hKF10[j]);
      hYields05[i][j]->Multiply(hYields05[i][j],hKF05[j]);
      hYields20[i][j]->Multiply(hYields20[i][j],hKF20[j]);

      hName = "Entries";
      hName += cRegion[i];
      hYEntries[i][j] = (TH2*)fYield[j]->Get(hName.c_str());
      if ( hYEntries[i][j]==0 ) {
	std::cout << "Missing histogram for region " << cRegion[i] << std::endl;
	return;
      }
      hName = "SmoothEntries";
      hName += cRegion[i];
      hYESmooth[i][j] = (TH2*)fYield[j]->Get(hName.c_str());
      if ( hYESmooth[i][j]==0 ) {
	std::cout << "Missing histogram for region " << cRegion[i] << std::endl;
	return;
      }
      // convert to efficiency (assume 10000 MC events/bin)
      hYEntries[i][j]->Scale(1/10000.);
      hYESmooth[i][j]->Scale(1/10000.);
      // convert yield to cross section
      hYields[i][j]->Divide(hYields[i][j],hYEntries[i][j]);
      hYields05[i][j]->Divide(hYields05[i][j],hYEntries[i][j]);
      hYields20[i][j]->Divide(hYields20[i][j],hYEntries[i][j]);
    }
  }
  //
  // histograms with exclusion and limits
  //
  gROOT->cd();
  TH2* hExclusion = (TH2*)hYields[0][0]->Clone("Exclusion");
  hExclusion->Reset();
  hExclusion->SetTitle("Exclusion");
  TH2* hLowerLimit = (TH2*)hYields[0][0]->Clone("LowerLimit");
  hLowerLimit->Reset();
  hLowerLimit->SetTitle("LowerLimit");
  TH2* hUpperLimit = (TH2*)hYields[0][0]->Clone("UpperLimit");
  hUpperLimit->Reset();
  hUpperLimit->SetTitle("UpperLimit");

  double yields[4][2];
  double yields05[4][2];
  double yields20[4][2];
  double entries[4][2];

//   double bkgs[4][2];

//   double kappa = (bkgs[0]*bkgs[3])/(bkgs[1]*bkgs[2]);
//   double sigma_kappa_base = 0.10;
//   double delta_kappa_abs = kappa - 1.;
//   double sigma_kappa = sqrt(sigma_kappa_base*sigma_kappa_base+delta_kappa_abs*delta_kappa_abs);
//   sigma_kappa = sqrt(0.129*0.129+0.1*0.1);

#ifndef DEBUG
  int nbx = hYields[0][0]->GetNbinsX();
  int nby = hYields[0][0]->GetNbinsY();
  for ( int ix=1; ix<=nbx; ++ix ) {
    for ( int iy=1; iy<=nby; ++iy ) {
#else
   { 
     int ix=40;
     {
       int iy=11;
#endif

      bool process(false);
      for ( unsigned int j=0; j<nf; ++j ) {
	ra4WSpace.setBackground(channelTypes[j],
				workingPoints[j]->bkg_[0],workingPoints[j]->bkg_[1],
				workingPoints[j]->bkg_[2],workingPoints[j]->bkg_[3]);
	ra4WSpace.setObserved(channelTypes[j],
			      workingPoints[j]->obs_[0],workingPoints[j]->obs_[1],
			      workingPoints[j]->obs_[2],workingPoints[j]->obs_[3]);
	for ( unsigned int i=0; i<4; ++i ) {
	  yields[i][j] = hYields[i][j]->GetBinContent(ix,iy);
	  yields05[i][j] = hYields05[i][j]->GetBinContent(ix,iy);
	  yields20[i][j] = hYields20[i][j]->GetBinContent(ix,iy);
	  entries[i][j] = hYESmooth[i][j]->GetBinContent(ix,iy);
	}
	if ( yields[3][j]>0.01 && yields[3][j]<10000 && entries[3][j]>0.0001 )  process = true;
	ra4WSpace.setSignal(channelTypes[j],
			    yields[0][j],yields[1][j],
			    yields[2][j],yields[3][j],
			    entries[0][j],entries[1][j],
			    entries[2][j],entries[3][j]);
#ifdef DEBUG
	std::cout << "yields for channel " << j << " =";
	for ( unsigned int i=0; i<4; ++i )
	  std::cout << " " << yields[i][j];
	std::cout << endl;
	std::cout << "effs for channel " << j << " =";
	for ( unsigned int i=0; i<4; ++i )
	  std::cout << " " << entries[i][j];
	std::cout << endl;
	std::cout << "backgrounds for channel " << j << " =";
	for ( unsigned int i=0; i<4; ++i )
	  std::cout << " " << workingPoints[j]->bkg_[i];
	std::cout << endl;
#endif
      }

      MyLimit limit(true,0.,999999999.);
      double sumD(0.);
      for ( unsigned int j=0; j<nf; ++j ) {
	sumD += (yields[3][j]*entries[3][j]);
      }
      if ( !process || sumD<0.01 ) {
	hExclusion->SetBinContent(ix,iy,limit.isInInterval);
	hLowerLimit->SetBinContent(ix,iy,limit.lowerLimit);
	hUpperLimit->SetBinContent(ix,iy,limit.upperLimit);
#ifndef DEBUG
	continue;
#endif
      }

      double sigK(0.);
      for ( unsigned int j=0; j<nf; ++j ) {
 	if ( workingPoints[j]->sigKappa_>sigK )
	  sigK = workingPoints[j]->sigKappa_;
// 	  sigK += workingPoints[j]->sigKappa_;
      }
//       sigK /= nf;
      double sigEffBase(0.15);
      double sigEffLept(0.05);
      double sigEffNLO(0.);
      for ( unsigned int j=0; j<nf; ++j ) {
	double sige = max(fabs(yields05[3][j]-yields[3][j]),
			  fabs(yields20[3][j]-yields[3][j]));
	sige /= yields[3][j];
	if ( sige>sigEffNLO )  sigEffNLO = sige;
      }
      double sigEff = sqrt(sigEffBase*sigEffBase+sigEffLept*sigEffLept+sigEffNLO*sigEffNLO);
      std::cout << "Systematics are " << sigK << " " << sigEff << std::endl;
      sigEff = 0.20;
      if ( !noKappaSyst ) 
	wspace->var("sigmaKappa")->setVal(sigK);
      if ( !noSContSyst )
	wspace->var("sigmaScont")->setVal(sigEff);
      if ( !noEffSyst )
	wspace->var("sigmaEff")->setVal(sigEff);
      
//       wspace->var("sigmaKappa")->setVal(sqrt(0.129*0.129+0.1*0.1)*0.967);
      // for the time being: work with yields
//       if ( muChannel.valid_ ) {
// 	wspace->var("effM")->setVal(1.);
//  	wspace->var("sadM")->setVal(0.);
//  	wspace->var("sbdM")->setVal(0.);
//  	wspace->var("scdM")->setVal(0.);
//       }
//       if ( eleChannel.valid_ ) {
// 	wspace->var("effE")->setVal(1.);
//  	wspace->var("sadE")->setVal(0.);
//  	wspace->var("sbdE")->setVal(0.);
//  	wspace->var("scdE")->setVal(0.);
//       }
#ifdef DEBUG
      wspace->Print("v");
      RooArgSet allVars = wspace->allVars();
      // allVars.printLatex(std::cout,1);
      TIterator* it = allVars.createIterator();
      RooRealVar* var;
      while ( var=(RooRealVar*)it->Next() ) {
	var->Print("v");
	var->printValue(std::cout);
	std::cout << std::endl;
      }
#endif
      std::cout << "Checked ( " << hExclusion->GetXaxis()->GetBinCenter(ix) << " , "
		<< hExclusion->GetYaxis()->GetBinCenter(iy) << " ) with signal " 
		<< yields[3][nf-1] << std::endl;
	

      RooDataSet data("data","data",*wspace->set("obs"));
      data.add(*wspace->set("obs"));
      data.Print("v");
      
      limit = computeLimit(wspace,&data,method);
      std::cout << "  Limit [ " << limit.lowerLimit << " , "
		<< limit.upperLimit << " ] ; isIn = " << limit.isInInterval << std::endl;
      

      double excl = limit.isInInterval;
      if ( limit.upperLimit<limit.lowerLimit )  excl = -1;
      hExclusion->SetBinContent(ix,iy,excl);
      hLowerLimit->SetBinContent(ix,iy,limit.lowerLimit);
      hUpperLimit->SetBinContent(ix,iy,limit.upperLimit);
//       return;
      
    }
  }
  
  TFile* out = new TFile("RA4abcd.root","RECREATE");
  hExclusion->SetDirectory(out);
  hExclusion->SetMinimum(); hExclusion->SetMaximum();
  hExclusion->SetContour(1); hExclusion->SetContourLevel(0,0.5);
  hLowerLimit->SetDirectory(out);
  hLowerLimit->SetMinimum(); hLowerLimit->SetMaximum();
  hUpperLimit->SetDirectory(out);
  hUpperLimit->SetMinimum(); hUpperLimit->SetMaximum();
  for ( unsigned int j=0; j<nf; ++j ) {
    hYields[3][j]->SetDirectory(out);
    hYields[3][j]->SetMinimum(); hYields[3][j]->SetMaximum();
  }
  out->Write();
  delete out;
}