Пример #1
0
int main(int argc, char **argv)
{
	bool printeff = true;
	string fc = "none";
	
	gROOT->ProcessLine(".x lhcbStyle.C");

	if(argc > 1)
	{
		for(int a = 1; a < argc; a++)
		{
			string arg = argv[a];
			string str = arg.substr(2,arg.length()-2);

			if(arg.find("-E")!=string::npos) fc = str;
			if(arg=="-peff") printeff = true;
		}
	}
	
	int nexp = 100;
	int nbins = 6;
	double q2min[] = {8.,15.,11.0,15,16,18};
	double q2max[] = {11.,20.,12.5,16,18,20};

	TString datafilename = "/afs/cern.ch/work/p/pluca/weighted/Lmumu/candLb.root";
	TreeReader * data = new TreeReader("candLb2Lmumu");
	data->AddFile(datafilename);
	TreeReader * datajpsi = new TreeReader("candLb2JpsiL");
	datajpsi->AddFile(datafilename);

	TFile * histFile = new TFile("Afb_bkgSys.root","recreate");

	string options = "-quiet-noPlot-lin-stdAxis-XM(#Lambda#mu#mu) (MeV/c^{2})-noCost-noParams";
	Analysis::SetPrintLevel("s");

	RooRealVar * cosThetaL = new RooRealVar("cosThetaL","cosThetaL",0.,-1.,1.);
	RooRealVar * cosThetaB = new RooRealVar("cosThetaB","cosThetaB",0.,-1.,1.);
	RooRealVar * MM = new RooRealVar("Lb_MassConsLambda","Lb_MassConsLambda",5621.,5400.,6000.);
	MM->setRange("Signal",5600,5640);
	RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR);

	//TGraphAsymmErrors * fL_vs_q2 = new TGraphAsymmErrors();
	//TCanvas * ceff = new TCanvas();

	RooCategory * samples = new RooCategory("samples","samples");
	samples->defineType("DD");
	samples->defineType("LL");

	RooRealVar * afb = new RooRealVar("afb","afb",0.,-0.75,0.75);
	RooRealVar * fL = new RooRealVar("fL","fL",0.6,0.,1.);
	TString afbLpdf = "((3./8.)*(1.-fL)*(1 + TMath::Power(cosThetaL,2)) + afb*cosThetaL + (3./4.)*fL*(1 - TMath::Power(cosThetaL,2)))";
	RooRealVar * afbB = new RooRealVar("afbB","afbB",0.,-0.5,0.5);
	TString afbBpdf = "(1 + 2*afbB*cosThetaB)";
	RooAbsPdf * teoPdf = new RooGenericPdf("teoPdf",afbLpdf,RooArgSet(*cosThetaL,*afb,*fL));
	RooAbsPdf * teoPdfB = new RooGenericPdf("teoPdfB",afbBpdf,RooArgSet(*cosThetaB,*afbB));

	TreeReader * mydata = datajpsi;
	Str2VarMap jpsiParsLL = getJpsiPars("LL", CutsDef::LLcut, histFile);
	Str2VarMap jpsiParsDD = getJpsiPars("DD", CutsDef::DDcut, histFile);

	vector<TH1 *> fLsysh, afbsysh, afbBsysh, fLsysh_frac, afbsysh_frac, afbBsysh_frac;

	for(int i = 0; i < nbins; i++)
	{
		TString q2name = ((TString)Form("q2_%4.2f_%4.2f",q2min[i],q2max[i])).ReplaceAll(".","");
		if(i>0) { mydata = data; MM->setRange(5400,6000); }
		else { q2name = "jpsi"; MM->setRange(5500,5850); }
		TString curq2cut = Form("TMath::Power(J_psi_1S_MM/1000,2) >= %e && TMath::Power(J_psi_1S_MM/1000,2) < %e",q2min[i],q2max[i]);	
		
		cout << "------------------- q2 bin: " << q2min[i] << " - " << q2max[i] << " -----------------------" << endl;

		/**               GET AND FIT EFFICIENCIES                  **/

		RooAbsPdf * effDDpdf = NULL, * effLLpdf = NULL, * effLLBpdf = NULL, * effDDBpdf = NULL;	
		getEfficiencies(q2min[i],q2max[i],&effLLpdf,&effDDpdf,&effLLBpdf,&effDDBpdf,printeff);
		cout << "Efficiencies extracted" << endl;
		histFile->cd();


		/**                    FIT AFB                  **/


		afb->setVal(0);
		afbB->setVal(-0.37);
		fL->setVal(0.6);

		RooAbsPdf * corrPdfLL = new RooProdPdf("sigPdfLL"+q2name,"corrPdfLL",*teoPdf,*effLLpdf);
		RooAbsPdf * corrPdfDD = new RooProdPdf("sigPdfDD"+q2name,"corrPdfDD",*teoPdf,*effDDpdf);
		RooAbsPdf * corrPdfLLB = new RooProdPdf("sigPdfLLB"+q2name,"corrPdfLLB",*teoPdfB,*effLLBpdf);
		RooAbsPdf * corrPdfDDB = new RooProdPdf("sigPdfDDB"+q2name,"corrPdfDDB",*teoPdfB,*effDDBpdf);

		TCut baseCut = "";
		TCut cutLL = CutsDef::LLcut + (TCut)curq2cut + baseCut;
		TCut cutDD = CutsDef::DDcut + (TCut)curq2cut + baseCut;

		histFile->cd();
		double fracDDv[2], fracLLv[2];
		double nsigDD, nsigLL;
		RooDataSet * dataLL = getDataAndFrac("LL",q2name,mydata,cutLL,MM,&fracLLv[0],jpsiParsLL,&nsigLL);
		RooDataSet * dataDD = getDataAndFrac("DD",q2name,mydata,cutDD,MM,&fracDDv[0],jpsiParsDD,&nsigDD);
		double nevts = nsigDD+nsigLL;

		cout << fixed << setprecision(3) << fracDDv[0] << "   " << fracDDv[1] << endl;
		RooRealVar * fracLL = new RooRealVar("fracLL","fracLL",fracLLv[0]);
		RooRealVar * fracDD = new RooRealVar("fracDD","fracDD",fracDDv[0]);

		RooAbsPdf * bkgLL = NULL, * bkgLLB = NULL, * bkgDD = NULL, * bkgDDB = NULL;
		buildBkgPdfs(q2min[i],q2max[i],"LL",CutsDef::LLcut,&bkgLL,&bkgLLB);
		buildBkgPdfs(q2min[i],q2max[i],"DD",CutsDef::DDcut,&bkgDD,&bkgDDB);
	
		cout << "Backgrounds extracted" << endl;

		RooAbsPdf * modelLL = new RooAddPdf("modelLL","modelLL",RooArgSet(*corrPdfLL,*bkgLL),*fracLL);
		RooAbsPdf * modelDD = new RooAddPdf("modelDD","modelDD",RooArgSet(*corrPdfDD,*bkgDD),*fracDD);
		RooAbsPdf * modelLLB = new RooAddPdf("modelLLB","modelLLB",RooArgSet(*corrPdfLLB,*bkgLLB),*fracLL);
		RooAbsPdf * modelDDB = new RooAddPdf("modelDDB","modelDDB",RooArgSet(*corrPdfDDB,*bkgDDB),*fracDD);

		// CREATE COMBINED DATASET
		RooDataSet * combData = new RooDataSet(Form("combData_%i",i),"combined data",RooArgSet(*MM,*cosThetaL,*cosThetaB),Index(*samples),Import("DD",*dataDD),Import("LL",*dataLL));

		Str2VarMap params;
		params["fL"] = fL;
		params["afb"] = afb;	
		Str2VarMap paramsB;
		paramsB["afbB"] = afbB;

		// FIT COS LEPTON
		RooSimultaneous * combModel = new RooSimultaneous(Form("combModel_%i",i),"",*samples);
		combModel->addPdf(*modelLL,"LL");
		combModel->addPdf(*modelDD,"DD");

		RooFitResult * res = safeFit(combModel,combData,params,&isInAllowedArea);	
	
		// FIT COS HADRON
		RooSimultaneous * combModelB = new RooSimultaneous(Form("combModelB_%i",i),"",*samples);
		combModelB->addPdf(*modelLLB,"LL");
		combModelB->addPdf(*modelDDB,"DD");

		RooFitResult * resB = safeFit(combModelB,combData,paramsB,&isInAllowedAreaB);

		cout << endl << fixed << setprecision(6) << "AfbB = " << afbB->getVal() << " +/- " << afbB->getError() << endl;
		cout << "Afb = " << afb->getVal() << " +/- " << afb->getError() << endl;
		cout << "fL = " << fL->getVal() << " +/- " << fL->getError() << endl;
		cout << endl;
		cout << "lepton:  " << res->edm() << "   "  << res->covQual() << endl;
		cout << "baryon:  " << resB->edm() << "   "  << resB->covQual() << endl;
		cout << endl;

		TH1F * fLsys = new TH1F(Form("fLsys_%i",i),"fLsys",40,-1,1);
		TH1F * afbsys = new TH1F(Form("afbsys_%i",i),"afbsys",40,-1,1);
		TH1F * afbBsys = new TH1F(Form("afbBsys_%i",i),"afbBsys",40,-1,1);
		TH1F * fLsys_frac = new TH1F(Form("fLsys_frac%i",i),"fLsys",40,-1,1);
		TH1F * afbsys_frac = new TH1F(Form("afbsys_frac%i",i),"afbsys",40,-1,1);
		TH1F * afbBsys_frac = new TH1F(Form("afbBsys_frac%i",i),"afbBsys",40,-1,1);


		RooAbsPdf * mybkgDD_2 = NULL, * mybkgDDB_2 = NULL;
		buildBkgPdfs(q2min[i],q2max[i],"DD",CutsDef::DDcut,&mybkgDD_2,&mybkgDDB_2,"RooKeyPdf");

		//cout << nevts << endl;
		//TRandom3 r(0);

		for(int e = 0; e < nexp; e++)
		{
			histFile->cd();
			RooAbsPdf * toypdf = (RooAbsPdf *)modelDD->Clone();
			Analysis * toy = new Analysis("toy",cosThetaL,modelDD,nevts);
			RooAbsPdf * toypdfB = (RooAbsPdf *)modelDDB->Clone();
			Analysis * toyB = new Analysis("toyB",cosThetaB,modelDDB,nevts);
			
			afb->setVal(0);
			afbB->setVal(-0.37);
			fL->setVal(0.6);

			safeFit(toypdf,toy->GetDataSet("-recalc"),params,&isInAllowedArea);
			safeFit(toypdfB,toyB->GetDataSet("-recalc"),paramsB,&isInAllowedAreaB);
			double def_afb = afb->getVal();
			double def_fL = fL->getVal();
			double def_afbB = afbB->getVal();

			afb->setVal(0);
			afbB->setVal(-0.37);
			fL->setVal(0.6);

			RooAbsPdf * modelDD_2 = new RooAddPdf("modelDD_2","modelDD",RooArgSet(*corrPdfDD,*mybkgDD_2),*fracDD);
			RooAbsPdf * modelDDB_2 = new RooAddPdf("modelDDB_2","modelDDB",RooArgSet(*corrPdfDDB,*mybkgDDB_2),*fracDD);
			safeFit(modelDD_2,toy->GetDataSet("-recalc"),params,&isInAllowedArea);
			safeFit(modelDDB_2,toyB->GetDataSet("-recalc"),paramsB,&isInAllowedAreaB);
			double oth_afb = afb->getVal();
			double oth_fL = fL->getVal();
			double oth_afbB = afbB->getVal();

			fLsys->Fill(oth_fL-def_fL);
			afbsys->Fill(oth_afb-def_afb);
			afbBsys->Fill(oth_afbB-def_afbB);
			

			afb->setVal(0.);
			afbB->setVal(-0.37);
			fL->setVal(0.6);

			//double rdm_frac = r.Gaus(fracDDv[0],fracDDv[1]);
			double rdm_frac = fracDDv[0] + fracDDv[1];
			RooRealVar * fracDD_2 = new RooRealVar("fracDD_2","fracDD_2",rdm_frac);	
			RooAbsPdf * modelDD_3 = new RooAddPdf("modelDD_3","modelDD",RooArgSet(*corrPdfDD,*bkgDD),*fracDD_2);
			RooAbsPdf * modelDDB_3 = new RooAddPdf("modelDDB_3","modelDDB",RooArgSet(*corrPdfDDB,*bkgDDB),*fracDD_2);
			safeFit(modelDD_3,toy->GetDataSet("-recalc"),params,&isInAllowedArea);
			safeFit(modelDDB_3,toyB->GetDataSet("-recalc"),paramsB,&isInAllowedAreaB);

			double frc_afb = afb->getVal();
			double frc_fL = fL->getVal();
			double frc_afbB = afbB->getVal();

			fLsys_frac->Fill(frc_fL-def_fL);
			afbsys_frac->Fill(frc_afb-def_afb);
			afbBsys_frac->Fill(frc_afbB-def_afbB);
			
		}

		afbsysh.push_back(afbsys);
		afbBsysh.push_back(afbBsys);
		fLsysh.push_back(fLsys);
		afbsysh_frac.push_back(afbsys_frac);
		afbBsysh_frac.push_back(afbBsys_frac);
		fLsysh_frac.push_back(fLsys_frac);

	}

	
	for(int q = 0; q < nbins; q++)
	{
		cout << fixed << setprecision(2) << "-------- Bin " << q2min[q] << "-" << q2max[q] << endl;
		cout << fixed << setprecision(5) << "fL sys = " << fLsysh[q]->GetMean() << " +/- " << fLsysh[q]->GetMeanError() << endl;
		cout << "Afb sys = " << afbsysh[q]->GetMean() << " +/- " << afbsysh[q]->GetMeanError() << endl;
		cout << "AfbB sys = " << afbBsysh[q]->GetMean() << " +/- " << afbBsysh[q]->GetMeanError() << endl;
	}

	cout << "#################################################################" << endl;
	for(int q = 0; q < nbins; q++)
	{
		cout << fixed << setprecision(2) << "-------- Bin " << q2min[q] << "-" << q2max[q] << endl;
		cout << fixed << setprecision(5) << "fL sys = " << fLsysh_frac[q]->GetMean() << " +/- " << fLsysh_frac[q]->GetMeanError() << endl;
		cout << "Afb sys = " << afbsysh_frac[q]->GetMean() << " +/- " << afbsysh_frac[q]->GetMeanError() << endl;
		cout << "AfbB sys = " << afbBsysh_frac[q]->GetMean() << " +/- " << afbBsysh_frac[q]->GetMeanError() << endl;
	}

	cout << "#################################################################" << endl;
	for(int q = 0; q < nbins; q++)
	{
		cout << fixed << setprecision(2) << "-------- Bin " << q2min[q] << "-" << q2max[q] << endl;
		cout << fixed << setprecision(5) << "fL sys = " << TMath::Sqrt(TMath::Power(fLsysh_frac[q]->GetMean(),2) + TMath::Power(fLsysh[q]->GetMean(),2) )  << endl;
		cout << "Afb sys = " << TMath::Sqrt(TMath::Power(afbsysh_frac[q]->GetMean(),2) + TMath::Power(afbsysh[q]->GetMean(),2) ) << endl;
		cout << "AfbB sys = " << TMath::Sqrt(TMath::Power(afbBsysh_frac[q]->GetMean(),2) + TMath::Power(afbBsysh[q]->GetMean(),2) ) << endl;
	}

}
Пример #2
0
int main(int argc, char** argv)
{
  RooMsgService::instance().deleteStream(0);
  RooMsgService::instance().deleteStream(1);
  
  
  //Check if all nedeed arguments to parse are there
  if(argc != 2)
  {
    std::cerr << ">>>>> drawWorkspace::usage: " << argv[0] << " configFileName" << std::endl ;
    return 1;
  }
  
  
  // Parse the config file
  parseConfigFile (argv[1]);
  
  
  //[Input]
  std::string inputDir       = gConfigParser -> readStringOption("Input::inputDir");
  std::string analysisMethod = gConfigParser -> readStringOption("Input::analysisMethod");
  std::string fitMethod      = gConfigParser -> readStringOption("Input::fitMethod");
  
  //[Output]
  std::string outputDir = gConfigParser -> readStringOption("Output::outputDir");
  
  //[Options]
  float mass = gConfigParser -> readIntOption("Options::mH");
    
  float xWidth = gConfigParser -> readFloatOption("Options::xWidth");
  char xWidthChar[50];
  sprintf(xWidthChar,"%d",int(xWidth));
  
  int step = gConfigParser -> readIntOption("Options::step");
  char stepChar[50];
  sprintf(stepChar,"%d",step);
  
  std::string additionalCuts = gConfigParser -> readStringOption("Options::additionalCuts");
  
  std::string flavour = gConfigParser -> readStringOption("Options::flavour");
  
  float sigStrength = gConfigParser -> readFloatOption("Options::sigStrength");
  
  int nToys = gConfigParser -> readIntOption("Options::nToys");
  
  
  if( additionalCuts == "none" )
  {
    inputDir += "/combine_signal/binWidth" + std::string(xWidthChar) + "/step" + std::string(stepChar) + "/";
  }
  else
  {
    inputDir += "/coumbine_signal/binWidth" + std::string(xWidthChar) + "/step" + std::string(stepChar) + "_" + additionalCuts + "/";
  }
  
  // define infile
  std::stringstream inFileName;
  if( analysisMethod != "sidebands" )
    inFileName << inputDir << "/shapes_" << analysisMethod << "_" << fitMethod << "_" << mass << "_" << flavour << ".root";
  else
    inFileName << inputDir << "/shapes_" << analysisMethod << "_" << mass << "_" << flavour << ".root";
  
  std::stringstream outFileName;
  if( analysisMethod != "sidebands" )
    outFileName << outputDir << "/drawWorkspace_" << analysisMethod << "_" << fitMethod << "_" << mass << "_" << flavour << ".root";
  else
    outFileName << outputDir << "/drawWorkspace_" << analysisMethod << "_" << mass << "_" << flavour << ".root";
  
  
  
  
  
  
  //------------------------------------
  // open the file and get the workspace
  
  std::cout << ">>> drawWorkspace::open file " << inFileName.str() << std::endl;
  TFile* inFile  = new TFile((inFileName.str()).c_str(), "READ");
  TFile* outFile = new TFile((outFileName.str()).c_str(),"RECREATE");
  
  inFile -> cd();
  RooWorkspace* workspace = (RooWorkspace*)( inFile->Get("workspace") );
  workspace -> Print();
  
  
  
  //-------------------
  // get the x variable
  
  RooRealVar* x         = (RooRealVar*)( workspace->var("x"));
  RooRealVar* rooXMin   = (RooRealVar*)( workspace->var("rooXMin"));
  RooRealVar* rooXMax   = (RooRealVar*)( workspace->var("rooXMax"));
  RooRealVar* rooXWidth = (RooRealVar*)( workspace->var("rooXWidth"));
 
  x -> setMin(rooXMin->getVal());
  x -> setMax(rooXMax->getVal());
  x -> setBins(int((rooXMax->getVal()-rooXMin->getVal())/rooXWidth->getVal()));
  x -> setRange("signal",GetLepNuWMMIN(mass),GetLepNuWMMAX(mass));
  
  
  
  //-------------------------
  // get the number of events
  
  RooRealVar* rooN_data_obs = (RooRealVar*)( workspace->var("rooN_data_obs") );
  RooRealVar* rooN_ggH      = (RooRealVar*)( workspace->var("rooN_ggH") );
  RooRealVar* rooN_qqH      = (RooRealVar*)( workspace->var("rooN_qqH") );
  
  double n_data_obs = rooN_data_obs -> getVal();
  double n_ggH = sigStrength * (rooN_ggH -> getVal());
  double n_qqH = sigStrength * (rooN_qqH -> getVal());
  double n_H = n_ggH + n_qqH;
  
  
  
  //------------
  // get the pdf
  RooDataHist* data_obs = (RooDataHist*)( workspace->data("data_obs") );
  RooAbsPdf* ggH = (RooAbsPdf*)( workspace->pdf("ggH") );
  RooAbsPdf* qqH = (RooAbsPdf*)( workspace->pdf("qqH") );
  RooGenericPdf* bkg = (RooGenericPdf*)( workspace->pdf("bkg") );
  
  
  
  //-------------------
  // get the parameters  
  
  int nPars = 0;
  if( fitMethod == "exponential" )                 nPars = 1;
  if( fitMethod == "attenuatedExponential" )       nPars = 3;
  if( fitMethod == "doubleExponential" )           nPars = 3;
  if( fitMethod == "attenuatedDoubleExponential" ) nPars = 5;
  
  float* initPars = new float[nPars];
  std::string* initParNames = new std::string[nPars];
  
  if( fitMethod == "exponential")
  {
    RooRealVar* roo_L1 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L1").c_str()) );
    initPars[0] = roo_L1 -> getVal();
    initParNames[0] == "L1";
  }
  
  if( fitMethod == "attenuatedExponential")
  {
    RooRealVar* roo_mu = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_mu").c_str()) );
    initPars[0] = roo_mu -> getVal();
    initParNames[0] = "mu";
    
    RooRealVar* roo_kT = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_kT").c_str()) );
    initPars[1] = roo_kT -> getVal();
    initParNames[1] = "kT";
    
    RooRealVar* roo_L1 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L1").c_str()) );
    initPars[2] = roo_L1 -> getVal();
    initParNames[2] = "L1";
  }
  
  if( fitMethod == "doubleExponential")
  {
    RooRealVar* roo_N = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_N").c_str()) );
    initPars[0] = roo_N -> getVal();
    initParNames[0] = "N";
    
    RooRealVar* roo_L1 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L1").c_str()) );
    initPars[1] = roo_L1 -> getVal();
    initParNames[1] = "L1";
    
    RooRealVar* roo_L2 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L2").c_str()) );
    initPars[2] = roo_L2 -> getVal();
    initParNames[2] = "L2";
  }
  
  if( fitMethod == "attenuatedDoubleExponential")
  {
    RooRealVar* roo_mu = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_mu").c_str()) );
    initPars[0] = roo_mu -> getVal();
    initParNames[0] = "mu";
    
    RooRealVar* roo_kT = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_kT").c_str()) );
    initPars[1] = roo_kT -> getVal();
    initParNames[1] = "kT";
    
    RooRealVar* roo_N = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_N").c_str()) );
    initPars[2] = roo_N -> getVal();
    initParNames[2] = "N";
    
    RooRealVar* roo_L1 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L1").c_str()) );
    initPars[3] = roo_L1 -> getVal();
    initParNames[3] = "L1";
    
    RooRealVar* roo_L2 = (RooRealVar*)( workspace->var(("CMS_HWWlvjj_"+flavour+"_L2").c_str()) );
    initPars[4] = roo_L2 -> getVal();
    initParNames[4] = "L2";
  }
  
  
  
  
  
  
  std::cout << "\n\n\n***********************************************************" << std::endl;
  std::cout << "*** VARIABLES ***" << std::endl;
  std::cout << "***********************************************************" << std::endl;
  
  std::cout << "x: "      << x->getVal() << std::endl;
  std::cout << "xMin: "   << rooXMin->getVal() << std::endl;
  std::cout << "xMax: "   << rooXMax->getVal() << std::endl;
  std::cout << "xWidth: " << rooXWidth->getVal() << std::endl;
  
  std::cout << "n_data_obs: " << n_data_obs << std::endl;
  std::cout << "n_ggH: "      << n_ggH      << std::endl;
  std::cout << "n_qqH: "      << n_qqH      << std::endl;
  
  
  
  std::cout << "\n\n\n***********************************************************" << std::endl;
  std::cout << "*** PARAMETERS ***" << std::endl;
  std::cout << "***********************************************************" << std::endl;
  
  for(int parIt = 0; parIt < nPars; ++parIt)
  {
    std::cout << initParNames[parIt] << ": " << initPars[parIt] << std::endl;
  }
  
  
  
  
  
  
  std::cout << "\n\n\n***********************************************************" << std::endl;
  std::cout << "*** PRINT HISTOGRAMS ***" << std::endl;
  std::cout << "***********************************************************" << std::endl;
  
  outFile -> cd();
  
  TCanvas* c_data_obs = new TCanvas("c_data_obs","c_data_obs");  
  RooPlot* plot_data_obs = x->frame();
  data_obs -> plotOn(plot_data_obs);
  plot_data_obs -> Draw();
  c_data_obs -> Write();
  delete plot_data_obs;
  delete c_data_obs;
  
  
  TCanvas* c_H = new TCanvas("c_H","c_H");
  RooPlot* plot_H = x->frame();
  ggH -> plotOn(plot_H,LineColor(kRed));
  qqH -> plotOn(plot_H,LineColor(kBlue));
  plot_H -> Draw();
  c_H -> Write();
  delete plot_H;
  delete c_H;
  

  TCanvas* c_bkg = new TCanvas("c_bkg","c_bkg");
  RooPlot* plot_bkg = x->frame();
  bkg -> plotOn(plot_bkg,LineColor(kRed));
  plot_bkg -> Draw();
  c_bkg -> Write();
  delete plot_bkg;
  delete c_bkg;
  
  
  
  
  
  
  std::cout << "\n\n\n***********************************************************" << std::endl;
  std::cout << "*** FIT B ***" << std::endl;
  std::cout << "***********************************************************" << std::endl;
  
  inFile -> cd();
  
  RooRealVar* B = new RooRealVar("B","",n_data_obs,0.,2.*n_data_obs);
  RooAddPdf* rooTotPdf_B = new RooAddPdf("rooTotPdf_B","",RooArgList(*bkg),RooArgList(*B));
  rooTotPdf_B -> fitTo(*data_obs,Extended(kTRUE),Save(),PrintLevel(-1));
  
  std::cout  << ">>> B: " << B -> getVal() << std::endl;
  
  
  outFile -> cd();
  
  TCanvas* c_fit_B = new TCanvas("c_fit_B","c_fit_B");
  RooPlot* plot_fit_B = x->frame();
  data_obs -> plotOn(plot_fit_B);
  rooTotPdf_B -> plotOn(plot_fit_B);
  plot_fit_B -> Draw();
  c_fit_B -> Write();
  delete plot_fit_B;
  delete c_fit_B;
  
  
  
  
  
  
  std::cout << "\n\n\n***********************************************************" << std::endl;
  std::cout << "*** TOY EXPERIMENTS ***" << std::endl;
  std::cout << "***********************************************************" << std::endl;
  
  inFile -> cd();
  
  TH1F* h_diffB_parentB_fitB   = new TH1F("h_diffB_parentB_fitB",  "",400,-1.,1.);
  TH1F* h_diffB_parentBS_fitB  = new TH1F("h_diffB_parentBS_fitB", "",400,-1.,1.);
  TH1F* h_diffB_parentBS_fitBS = new TH1F("h_diffB_parentBS_fitBS","",400,-1.,1.);
  TH1F* h_diffS_parentBS_fitBS = new TH1F("h_diffS_parentBS_fitBS","",400,-1.,1.);
  
  
  
  //--------------------------------------1.
  // define background parent distribution
  
  RooRealVar** pars = new RooRealVar*[nPars];
  RooGenericPdf* rooParentPdf_B;
  
  if( fitMethod == "exponential" )
  {
    pars[0] = new RooRealVar("parent_L1","",initPars[0],initPars[0],initPars[0]);
    rooParentPdf_B = new RooGenericPdf("rooParentPdf_B","","exp(-1*@1*@0)",RooArgSet(*x,*pars[0]));
  }
  
  if( fitMethod == "attenuatedExponential" )
  {
    pars[0] = new RooRealVar("parent_mu","",initPars[0],initPars[0],initPars[0]);
    pars[1] = new RooRealVar("parent_kT","",initPars[1],initPars[1],initPars[1]);
    pars[2] = new RooRealVar("parent_L1","",initPars[2],initPars[2],initPars[2]);
    rooParentPdf_B = new RooGenericPdf("rooParentPdf_B","","1./(exp(-1.*(@0-@1)/@2)+1.) * exp(-1*@3*@0)",RooArgSet(*x,*pars[0],*pars[1],*pars[2]));
  }
  
  if( fitMethod == "doubleExponential" )
  {
    pars[0] = new RooRealVar("parent_N","", initPars[0],initPars[0],initPars[0]);
    pars[1] = new RooRealVar("parent_L1","",initPars[1],initPars[1],initPars[1]);
    pars[2] = new RooRealVar("parent_L2","",initPars[2],initPars[2],initPars[2]);
    rooParentPdf_B = new RooGenericPdf("rooParentPdf_B","","(exp(-1*@2*@0) + @1*exp(-1*@3*@0))",RooArgSet(*x,*pars[0],*pars[1],*pars[2]));
  }
  
  if( fitMethod == "attenuatedDoubleExponential" )
  {
    pars[0] = new RooRealVar("parent_mu","",initPars[0],initPars[0],initPars[0]);
    pars[1] = new RooRealVar("parent_kT","",initPars[1],initPars[1],initPars[1]);
    pars[2] = new RooRealVar("parent_N","", initPars[2],initPars[2],initPars[2]);
    pars[3] = new RooRealVar("parent_L1","",initPars[3],initPars[3],initPars[3]);
    pars[4] = new RooRealVar("parent_L2","",initPars[4],initPars[4],initPars[4]);
    rooParentPdf_B = new RooGenericPdf("bkg","","1./(exp(-1.*(@0-@1)/@2)+1.) * (exp(-1*@4*@0) + @3*exp(-1*@5*@0))",RooArgSet(*x,*pars[0],*pars[1],*pars[2],*pars[3],*pars[4]));
  }
  
  RooAbsReal* integral_parent_B = rooParentPdf_B -> createIntegral(*x,NormSet(*x),Range("signal"));
  double n_parent_B = integral_parent_B->getVal() * int(n_data_obs);
  std::cout << ">>> n_parent_B: " << n_parent_B << std::endl; 
  
  
  
  //----------------------------------
  // define signal parent distribution
  
  RooGenericPdf* rooParentPdf_ggS = (RooGenericPdf*)( ggH->Clone("rooParentPdf_ggS") );
  RooGenericPdf* rooParentPdf_qqS = (RooGenericPdf*)( qqH->Clone("rooParentPdf_qqS") );
  
  RooAddPdf* rooParentPdf_S = new RooAddPdf("rooParentPdf_S","",RooArgList(*rooParentPdf_ggS,*rooParentPdf_qqS),RooArgList(*rooN_ggH,*rooN_qqH));
  
  RooAbsReal* integral_parent_S = rooParentPdf_S -> createIntegral(*x,NormSet(*x),Range("signal"));
  double n_parent_S = integral_parent_S->getVal() * (n_H);
  std::cout << ">>> n_parent_S: " << n_parent_S << std::endl; 
  
  
  
  //------------
  // create toys
  
  for(int toyIt = 0; toyIt < nToys; ++toyIt)
  {
    if(toyIt%100 == 0) std::cout << ">>> generating toy " << toyIt << " / " << nToys << "\r" << std::flush;
    
    RooDataSet* ds_B_toy = rooParentPdf_B -> generate(*x,int(n_data_obs));
    RooDataHist* dh_B_toy = ds_B_toy -> binnedClone();
    
    RooDataSet* ds_BS_toy = rooParentPdf_S -> generate(*x,int(n_H));
    ds_BS_toy -> append(*ds_B_toy);
    RooDataHist* dh_BS_toy = ds_BS_toy -> binnedClone();
    
    
    
    // generate B - fit B
    RooRealVar* B_toy = new RooRealVar("B_toy","",n_data_obs,0.,2.*n_data_obs);
    
    RooRealVar** pars_toy = new RooRealVar*[nPars];
    RooGenericPdf* bkg_toy;
    InitializeBkgPdf(x,&bkg_toy,pars_toy,initPars,fitMethod,nPars);
    
    RooAddPdf* rooTotPdf_B_toy = new RooAddPdf("rooTotPdf_B_toy","",RooArgList(*bkg_toy),RooArgList(*B_toy));
    rooTotPdf_B_toy -> fitTo(*dh_B_toy,Extended(kTRUE),Save(),PrintLevel(-10));
    
    RooAbsReal* integral_B_toy = rooTotPdf_B_toy -> createIntegral(*x,NormSet(*x),Range("signal"));
    double n_B_toy = integral_B_toy->getVal() * B_toy->getVal();
    
    h_diffB_parentB_fitB -> Fill(n_B_toy/n_parent_B - 1.);
    
    
    
    // generate BS - fit B
    RooRealVar* B2_toy = new RooRealVar("B2_toy","",n_data_obs,0.,2.*n_data_obs);
    
    RooRealVar** pars2_toy = new RooRealVar*[nPars];
    RooGenericPdf* bkg2_toy;
    InitializeBkgPdf(x,&bkg2_toy,pars2_toy,initPars,fitMethod,nPars);
    
    RooAddPdf* rooTotPdf_B2_toy = new RooAddPdf("rooTotPdf_B2_toy","",RooArgList(*bkg2_toy),RooArgList(*B2_toy));
    rooTotPdf_B2_toy -> fitTo(*dh_BS_toy,Extended(kTRUE),Save(),PrintLevel(-10));
    
    RooAbsReal* integral_B2_toy = rooTotPdf_B2_toy -> createIntegral(*x,NormSet(*x),Range("signal"));
    double n_B2_toy = integral_B2_toy->getVal() * B2_toy->getVal();
    
    h_diffB_parentBS_fitB -> Fill(n_B2_toy/n_parent_B - 1.);
    
    
    
    // generate BS - fit BS
    RooRealVar* B3_toy = new RooRealVar("B3_toy","",n_data_obs,0.,2.*n_data_obs);
    RooRealVar* S3_toy = new RooRealVar("S3_toy","",n_H,0.,2.*n_H);
    
    RooRealVar** pars3_toy = new RooRealVar*[nPars];
    RooGenericPdf* bkg3_toy;
    InitializeBkgPdf(x,&bkg3_toy,pars3_toy,initPars,fitMethod,nPars);
    
    RooGenericPdf* sig3_toy = (RooGenericPdf*)( rooParentPdf_S -> Clone("sig3_toy") );
    
    RooAddPdf* rooTotPdf_BS3_toy = new RooAddPdf("rooTotPdf_BS3_toy","",RooArgList(*bkg3_toy,*sig3_toy),RooArgList(*B3_toy,*S3_toy));
    rooTotPdf_BS3_toy -> fitTo(*dh_BS_toy,Extended(kTRUE),Save(),PrintLevel(-10));
    
    RooAbsReal* integral_B3_toy = bkg3_toy -> createIntegral(*x,NormSet(*x),Range("signal"));
    RooAbsReal* integral_S3_toy = sig3_toy -> createIntegral(*x,NormSet(*x),Range("signal"));
    double n_B3_toy = integral_B3_toy->getVal() * B3_toy->getVal();
    double n_S3_toy = integral_S3_toy->getVal() * S3_toy->getVal();
    
    h_diffB_parentBS_fitBS -> Fill(n_B3_toy/n_parent_B - 1.);
    h_diffS_parentBS_fitBS -> Fill(n_S3_toy/n_parent_S - 1.);
    
    
    
    if(toyIt < 10)
    {
      outFile -> cd();
      
      char dirName[50];
      sprintf(dirName,"toy%d",toyIt);
      
      outFile -> mkdir(dirName);
      outFile -> cd(dirName);
      
      char canvasName[50];
      
      sprintf(canvasName,"parentB_fitB_%d",toyIt);
      TCanvas* c_parentB_fitB_toy = new TCanvas(canvasName);
      RooPlot* plot_parentB_fitB_toy = x->frame();
      dh_B_toy -> plotOn(plot_parentB_fitB_toy);
      rooTotPdf_B_toy -> plotOn(plot_parentB_fitB_toy);
      plot_parentB_fitB_toy -> Draw();
      c_parentB_fitB_toy -> Write();
      delete plot_parentB_fitB_toy;
      delete c_parentB_fitB_toy;
      
      sprintf(canvasName,"parentBS_fitB_%d",toyIt);
      TCanvas* c_parentBS_fitB_toy = new TCanvas(canvasName);
      RooPlot* plot_parentBS_fitB_toy = x->frame();
      dh_BS_toy -> plotOn(plot_parentBS_fitB_toy);
      rooTotPdf_B2_toy -> plotOn(plot_parentBS_fitB_toy);
      plot_parentBS_fitB_toy -> Draw();
      c_parentBS_fitB_toy -> Write();
      delete plot_parentBS_fitB_toy;
      delete c_parentBS_fitB_toy;
      
      sprintf(canvasName,"parentBS_fitBS_%d",toyIt);
      TCanvas* c_parentBS_fitBS_toy = new TCanvas(canvasName);
      RooPlot* plot_parentBS_fitBS_toy = x->frame();
      dh_BS_toy -> plotOn(plot_parentBS_fitBS_toy);
      rooTotPdf_BS3_toy -> plotOn(plot_parentBS_fitBS_toy);
      plot_parentBS_fitBS_toy -> Draw();
      c_parentBS_fitBS_toy -> Write();
      delete plot_parentBS_fitBS_toy;
      delete c_parentBS_fitBS_toy;
    }
    
    
    
    delete integral_B_toy;
    delete rooTotPdf_B_toy;
    delete bkg_toy;
    for(int parIt = 0; parIt < nPars; ++parIt)
      delete pars_toy[parIt];
    delete B_toy;
    
    delete integral_B2_toy;
    delete rooTotPdf_B2_toy;
    delete bkg2_toy;
    for(int parIt = 0; parIt < nPars; ++parIt)
      delete pars2_toy[parIt];
    delete B2_toy;
    
    delete integral_B3_toy;
    delete rooTotPdf_BS3_toy;
    delete bkg3_toy;
    for(int parIt = 0; parIt < nPars; ++parIt)
      delete pars3_toy[parIt];
    delete B3_toy;
    
    delete integral_S3_toy;
    delete sig3_toy;
    delete S3_toy;
    
    delete dh_B_toy;
    delete ds_B_toy;
    delete dh_BS_toy;
    delete ds_BS_toy;
  }
  
  
  
  
  outFile -> cd();
  
  h_diffB_parentB_fitB -> Write();
  h_diffB_parentBS_fitB -> Write();
  h_diffB_parentBS_fitBS -> Write();
  h_diffS_parentBS_fitBS -> Write();
  
  outFile -> Close();
  
  
    
  return 0;
}