コード例 #1
0
ファイル: fitWm.C プロジェクト: ksung25/UserCode
void fitWm(const TString  outputDir,   // output directory
           const Double_t lumi,        // integrated luminosity (/fb)
	   const Double_t nsigma=0     // vary MET corrections by n-sigmas (nsigma=0 means nominal correction)
) {
  gBenchmark->Start("fitWm");

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //==============================================================================================================   
  
  // MET histogram binning and range
  const Int_t    NBINS   = 50;
  const Double_t METMAX  = 100;
  
  const Double_t PT_CUT  = 25;
  const Double_t ETA_CUT = 2.1;

  // file format for output plots
  const TString format("png"); 

    
  // recoil correction
  RecoilCorrector recoilCorr("../Recoil/ZmmData/fits.root");//, (!) uncomment to perform corrections to recoil from W-MC/Z-MC
                             //"../Recoil/WmpMC/fits.root",
			     //"../Recoil/WmmMC/fits.root",
			     //"../Recoil/ZmmMC/fits.root");
   
  // NNLO boson pT k-factors
  TFile nnloCorrFile("/data/blue/ksung/EWKAna/8TeV/Utils/Ratio.root");
  TH1D *hNNLOCorr = (TH1D*)nnloCorrFile.Get("RpT_B");
  
  //
  // input ntuple file names
  //
  enum { eData, eWmunu, eEWK, eAntiData, eAntiWmunu, eAntiEWK };  // data type enum
  vector<TString> fnamev;
  vector<Int_t>   typev;
  
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/Wmunu/ntuples/data_select.root"); typev.push_back(eData);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/Wmunu/ntuples/wm_select.root");   typev.push_back(eWmunu);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/Wmunu/ntuples/ewk_select.root");  typev.push_back(eEWK);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/Wmunu/ntuples/top_select.root");  typev.push_back(eEWK);
  
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/AntiWmunu/ntuples/data_select.root"); typev.push_back(eAntiData);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/AntiWmunu/ntuples/wm_select.root");   typev.push_back(eAntiWmunu);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/AntiWmunu/ntuples/ewk_select.root");  typev.push_back(eAntiEWK);
  fnamev.push_back("/data/blue/ksung/EWKAna/8TeV/Selection/AntiWmunu/ntuples/top_select.root");  typev.push_back(eAntiEWK);


  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code 
  //==============================================================================================================  
  
  // Create output directory
  gSystem->mkdir(outputDir,kTRUE);
  CPlot::sOutDir = outputDir;  
  
  //
  // Declare MET histograms
  //
  TH1D *hDataMet   = new TH1D("hDataMet","",  NBINS,0,METMAX); hDataMet->Sumw2();
  TH1D *hDataMetm  = new TH1D("hDataMetm","", NBINS,0,METMAX); hDataMetm->Sumw2();  
  TH1D *hDataMetp  = new TH1D("hDataMetp","", NBINS,0,METMAX); hDataMetp->Sumw2();
  TH1D *hWmunuMet  = new TH1D("hWmunuMet","", NBINS,0,METMAX); hWmunuMet->Sumw2();
  TH1D *hWmunuMetp = new TH1D("hWmunuMetp","",NBINS,0,METMAX); hWmunuMetp->Sumw2();
  TH1D *hWmunuMetm = new TH1D("hWmunuMetm","",NBINS,0,METMAX); hWmunuMetm->Sumw2();
  TH1D *hEWKMet    = new TH1D("hEWKMet", "",  NBINS,0,METMAX); hEWKMet->Sumw2();
  TH1D *hEWKMetp   = new TH1D("hEWKMetp", "", NBINS,0,METMAX); hEWKMetp->Sumw2();
  TH1D *hEWKMetm   = new TH1D("hEWKMetm", "", NBINS,0,METMAX); hEWKMetm->Sumw2();

  TH1D *hAntiDataMet   = new TH1D("hAntiDataMet","",  NBINS,0,METMAX); hAntiDataMet->Sumw2();
  TH1D *hAntiDataMetm  = new TH1D("hAntiDataMetm","", NBINS,0,METMAX); hAntiDataMetm->Sumw2();  
  TH1D *hAntiDataMetp  = new TH1D("hAntiDataMetp","", NBINS,0,METMAX); hAntiDataMetp->Sumw2();
  TH1D *hAntiWmunuMet  = new TH1D("hAntiWmunuMet","", NBINS,0,METMAX); hAntiWmunuMet->Sumw2();
  TH1D *hAntiWmunuMetp = new TH1D("hAntiWmunuMetp","",NBINS,0,METMAX); hAntiWmunuMetp->Sumw2();
  TH1D *hAntiWmunuMetm = new TH1D("hAntiWmunuMetm","",NBINS,0,METMAX); hAntiWmunuMetm->Sumw2();
  TH1D *hAntiEWKMet    = new TH1D("hAntiEWKMet", "",  NBINS,0,METMAX); hAntiEWKMet->Sumw2();
  TH1D *hAntiEWKMetp   = new TH1D("hAntiEWKMetp", "", NBINS,0,METMAX); hAntiEWKMetp->Sumw2();
  TH1D *hAntiEWKMetm   = new TH1D("hAntiEWKMetm", "", NBINS,0,METMAX); hAntiEWKMetm->Sumw2();

  //
  // Declare variables to read in ntuple
  //
  UInt_t  runNum, lumiSec, evtNum;
  UInt_t  npv, npu;
  Float_t genVPt, genVPhi;
  Float_t scale1fb;
  Float_t met, metPhi, sumEt, mt, u1, u2;
  Int_t   q;
  LorentzVector *lep=0;
  Float_t pfChIso, pfGamIso, pfNeuIso;
    
  TFile *infile=0;
  TTree *intree=0;

  //
  // Loop over files
  //
  for(UInt_t ifile=0; ifile<fnamev.size(); ifile++) {
    
    // Read input file and get the TTrees
    cout << "Processing " << fnamev[ifile] << "..." << endl;
    infile = new TFile(fnamev[ifile]);	  assert(infile);
    intree = (TTree*)infile->Get("Events"); assert(intree);

    intree->SetBranchAddress("runNum",   &runNum);    // event run number
    intree->SetBranchAddress("lumiSec",  &lumiSec);   // event lumi section
    intree->SetBranchAddress("evtNum",   &evtNum);    // event number
    intree->SetBranchAddress("npv",      &npv);       // number of primary vertices
    intree->SetBranchAddress("npu",      &npu);       // number of in-time PU events (MC)
    intree->SetBranchAddress("genVPt",   &genVPt);    // GEN W boson pT (signal MC)
    intree->SetBranchAddress("genVPhi",  &genVPhi);   // GEN W boson phi (signal MC)   
    intree->SetBranchAddress("scale1fb", &scale1fb);  // event weight per 1/fb (MC)
    intree->SetBranchAddress("met",      &met);       // MET
    intree->SetBranchAddress("metPhi",   &metPhi);    // phi(MET)
    intree->SetBranchAddress("sumEt",    &sumEt);     // Sum ET
    intree->SetBranchAddress("mt",       &mt);        // transverse mass
    intree->SetBranchAddress("u1",       &u1);        // parallel component of recoil
    intree->SetBranchAddress("u2",       &u2);        // perpendicular component of recoil
    intree->SetBranchAddress("q",        &q);	      // lepton charge
    intree->SetBranchAddress("lep",      &lep);       // lepton 4-vector
    intree->SetBranchAddress("pfChIso",  &pfChIso);
    intree->SetBranchAddress("pfGamIso", &pfGamIso);
    intree->SetBranchAddress("pfNeuIso", &pfNeuIso);
  
    //
    // loop over events
    //
    for(UInt_t ientry=0; ientry<intree->GetEntries(); ientry++) {
      intree->GetEntry(ientry);
      
      if(lep->Pt()        < PT_CUT)  continue;	
      if(fabs(lep->Eta()) > ETA_CUT) continue;
      
      if( (typev[ifile]==eAntiData || typev[ifile]==eAntiWmunu || typev[ifile]==eAntiEWK) &&
          (pfChIso+pfGamIso+pfNeuIso)>0.5*(lep->Pt()) ) 
	  continue;
      
      if(typev[ifile]==eData) {
        hDataMet->Fill(met);
	if(q>0) { hDataMetp->Fill(met); } 
	else    { hDataMetm->Fill(met); }
      
      } else if(typev[ifile]==eAntiData) {
        hAntiDataMet->Fill(met);
	if(q>0) { hAntiDataMetp->Fill(met); } 
	else    { hAntiDataMetm->Fill(met); }      
      
      } else {
        Double_t weight = 1;
        weight *= scale1fb*lumi;
	
	if(typev[ifile]==eWmunu) {
          Double_t corrMet=met, corrMetPhi=metPhi;
        
	  // apply recoil corrections to W MC
	  Double_t lepPt = lep->Pt();
	  //Double_t lepPt = gRandom->Gaus(lep->Pt(),0.5);  // (!) uncomment to apply scale/res corrections to MC
	  recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lep->Phi(),nsigma,q);
	
          Double_t nnlocorr=1;
          for(Int_t ibin=1; ibin<=hNNLOCorr->GetNbinsX(); ibin++) {
            if(genVPt >= hNNLOCorr->GetBinLowEdge(ibin) &&
               genVPt < (hNNLOCorr->GetBinLowEdge(ibin)+hNNLOCorr->GetBinWidth(ibin)))
              nnlocorr = hNNLOCorr->GetBinContent(ibin);
          }
	  //weight *= nnlocorr;  // (!) uncomment to apply NNLO corrections
	  
          hWmunuMet->Fill(corrMet,weight);
	  if(q>0) { hWmunuMetp->Fill(corrMet,weight); } 
	  else    { hWmunuMetm->Fill(corrMet,weight); }
        }
	if(typev[ifile]==eAntiWmunu) {
          Double_t corrMet=met, corrMetPhi=metPhi;
        
	  // apply recoil corrections to W MC
	  Double_t lepPt = lep->Pt();//gRandom->Gaus(lep->Pt(),0.5);
	  //Double_t lepPt = gRandom->Gaus(lep->Pt(),0.5);  // (!) uncomment to apply scale/res corrections to MC
	  recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lep->Phi(),nsigma,q);
          
	  Double_t nnlocorr=1;
          for(Int_t ibin=1; ibin<=hNNLOCorr->GetNbinsX(); ibin++) {
            if(genVPt >= hNNLOCorr->GetBinLowEdge(ibin) &&
               genVPt < (hNNLOCorr->GetBinLowEdge(ibin)+hNNLOCorr->GetBinWidth(ibin)))
              nnlocorr = hNNLOCorr->GetBinContent(ibin);
          }
	  //weight *= nnlocorr;  // (!) uncomment to apply NNLO corrections
          
	  hAntiWmunuMet->Fill(corrMet,weight);
	  if(q>0) { hAntiWmunuMetp->Fill(corrMet,weight); } 
	  else    { hAntiWmunuMetm->Fill(corrMet,weight); }
        }
        if(typev[ifile]==eEWK) {
          hEWKMet->Fill(met,weight);
	  if(q>0) { hEWKMetp->Fill(met,weight); }
	  else    { hEWKMetm->Fill(met,weight); }
        }
        if(typev[ifile]==eAntiEWK) {
          hAntiEWKMet->Fill(met,weight);
	  if(q>0) { hAntiEWKMetp->Fill(met,weight); }
	  else    { hAntiEWKMetm->Fill(met,weight); }
        }
      }
    }
  }  
  delete infile;
  infile=0, intree=0;   
  
  //
  // Declare fit parameters for signal and background yields
  // Note: W signal and EWK+top PDFs are constrained to the ratio described in MC
  //
  RooRealVar nSig("nSig","nSig",0.7*(hDataMet->Integral()),0,hDataMet->Integral());
  RooRealVar nQCD("nQCD","nQCD",0.3*(hDataMet->Integral()),0,hDataMet->Integral());
  RooRealVar cewk("cewk","cewk",0.1,0,5) ;
  cewk.setVal(hEWKMet->Integral()/hWmunuMet->Integral());
  cewk.setConstant(kTRUE);
  RooFormulaVar nEWK("nEWK","nEWK","cewk*nSig",RooArgList(nSig,cewk));
  RooRealVar nAntiSig("nAntiSig","nAntiSig",0.05*(hAntiDataMet->Integral()),0,hAntiDataMet->Integral());
  RooRealVar nAntiQCD("nAntiQCD","nAntiQCD",0.9*(hDataMet->Integral()),0,hDataMet->Integral());
  RooRealVar dewk("dewk","dewk",0.1,0,5) ;
  dewk.setVal(hAntiEWKMet->Integral()/hAntiWmunuMet->Integral());
  dewk.setConstant(kTRUE);
  RooFormulaVar nAntiEWK("nAntiEWK","nAntiEWK","dewk*nAntiSig",RooArgList(nAntiSig,dewk));
  
  RooRealVar nSigp("nSigp","nSigp",0.7*(hDataMetp->Integral()),0,hDataMetp->Integral());
  RooRealVar nQCDp("nQCDp","nQCDp",0.3*(hDataMetp->Integral()),0,hDataMetp->Integral());
  RooRealVar cewkp("cewkp","cewkp",0.1,0,5) ;
  cewkp.setVal(hEWKMetp->Integral()/hWmunuMetp->Integral());
  cewkp.setConstant(kTRUE);
  RooFormulaVar nEWKp("nEWKp","nEWKp","cewkp*nSigp",RooArgList(nSigp,cewkp));
  RooRealVar nAntiSigp("nAntiSigp","nAntiSigp",0.05*(hAntiDataMetp->Integral()),0,hAntiDataMetp->Integral());
  RooRealVar nAntiQCDp("nAntiQCDp","nAntiQCDp",0.9*(hAntiDataMetp->Integral()),0,hAntiDataMetp->Integral());
  RooRealVar dewkp("dewkp","dewkp",0.1,0,5) ;
  dewkp.setVal(hAntiEWKMetp->Integral()/hAntiWmunuMetp->Integral());
  dewkp.setConstant(kTRUE);
  RooFormulaVar nAntiEWKp("nAntiEWKp","nAntiEWKp","dewkp*nAntiSigp",RooArgList(nAntiSigp,dewkp));
  
  RooRealVar nSigm("nSigm","nSigm",0.7*(hDataMetm->Integral()),0,hDataMetm->Integral());
  RooRealVar nQCDm("nQCDm","nQCDm",0.3*(hDataMetm->Integral()),0,hDataMetm->Integral());
  RooRealVar cewkm("cewkm","cewkm",0.1,0,5) ;
  cewkm.setVal(hEWKMetm->Integral()/hWmunuMetm->Integral());
  cewkm.setConstant(kTRUE);
  RooFormulaVar nEWKm("nEWKm","nEWKm","cewkm*nSigm",RooArgList(nSigm,cewkm));  
  RooRealVar nAntiSigm("nAntiSigm","nAntiSigm",0.05*(hAntiDataMetm->Integral()),0,hAntiDataMetm->Integral());
  RooRealVar nAntiQCDm("nAntiQCDm","nAntiQCDm",0.9*(hAntiDataMetm->Integral()),0,hAntiDataMetm->Integral());
  RooRealVar dewkm("dewkm","dewkm",0.1,0,5) ;
  dewkm.setVal(hAntiEWKMetm->Integral()/hAntiWmunuMetm->Integral());
  dewkm.setConstant(kTRUE);
  RooFormulaVar nAntiEWKm("nAntiEWKm","nAntiEWKm","dewkm*nAntiSigm",RooArgList(nAntiSigm,dewkm));

  //
  // Construct PDFs for fitting
  //
  RooRealVar pfmet("pfmet","pfmet",0,METMAX);
  pfmet.setBins(NBINS);
   
  // Signal PDFs
  RooDataHist wmunuMet ("wmunuMET", "wmunuMET", RooArgSet(pfmet),hWmunuMet);  RooHistPdf pdfWm ("wm", "wm", pfmet,wmunuMet, 1);
  RooDataHist wmunuMetp("wmunuMETp","wmunuMETp",RooArgSet(pfmet),hWmunuMetp); RooHistPdf pdfWmp("wmp","wmp",pfmet,wmunuMetp,1);
  RooDataHist wmunuMetm("wmunuMETm","wmunuMETm",RooArgSet(pfmet),hWmunuMetm); RooHistPdf pdfWmm("wmm","wmm",pfmet,wmunuMetm,1); 
  
  // EWK+top PDFs
  RooDataHist ewkMet ("ewkMET", "ewkMET", RooArgSet(pfmet),hEWKMet);  RooHistPdf pdfEWK ("ewk", "ewk", pfmet,ewkMet, 1);
  RooDataHist ewkMetp("ewkMETp","ewkMETp",RooArgSet(pfmet),hEWKMetp); RooHistPdf pdfEWKp("ewkp","ewkp",pfmet,ewkMetp,1); 
  RooDataHist ewkMetm("ewkMETm","ewkMETm",RooArgSet(pfmet),hEWKMetm); RooHistPdf pdfEWKm("ewkm","ewkm",pfmet,ewkMetm,1); 
  
  // QCD Pdfs
  CPepeModel1 qcd("qcd",pfmet);
  CPepeModel1 qcdp("qcdp",pfmet);
  CPepeModel1 qcdm("qcdm",pfmet);
  
  // Signal + Background PDFs
  RooAddPdf pdfMet ("pdfMet", "pdfMet", RooArgList(pdfWm,pdfEWK,*(qcd.model)),   RooArgList(nSig,nEWK,nQCD));  
  RooAddPdf pdfMetp("pdfMetp","pdfMetp",RooArgList(pdfWmp,pdfEWKp,*(qcdp.model)),RooArgList(nSigp,nEWKp,nQCDp));
  RooAddPdf pdfMetm("pdfMetm","pdfMetm",RooArgList(pdfWmm,pdfEWKm,*(qcdm.model)),RooArgList(nSigm,nEWKm,nQCDm));
    
  
  // Anti-Signal PDFs
  RooDataHist awmunuMet ("awmunuMET", "awmunuMET", RooArgSet(pfmet),hAntiWmunuMet);  RooHistPdf apdfWm ("awm", "awm", pfmet,awmunuMet, 1);
  RooDataHist awmunuMetp("awmunuMETp","awmunuMETp",RooArgSet(pfmet),hAntiWmunuMetp); RooHistPdf apdfWmp("awmp","awmp",pfmet,awmunuMetp,1);
  RooDataHist awmunuMetm("awmunuMETm","awmunuMETm",RooArgSet(pfmet),hAntiWmunuMetm); RooHistPdf apdfWmm("awmm","awmm",pfmet,awmunuMetm,1); 
  
  // Anti-EWK+top PDFs
  RooDataHist aewkMet ("aewkMET", "aewkMET", RooArgSet(pfmet),hAntiEWKMet);  RooHistPdf apdfEWK ("aewk", "aewk", pfmet,aewkMet, 1);
  RooDataHist aewkMetp("aewkMETp","aewkMETp",RooArgSet(pfmet),hAntiEWKMetp); RooHistPdf apdfEWKp("aewkp","aewkp",pfmet,aewkMetp,1); 
  RooDataHist aewkMetm("aewkMETm","aewkMETm",RooArgSet(pfmet),hAntiEWKMetm); RooHistPdf apdfEWKm("aewkm","aewkm",pfmet,aewkMetm,1); 
  
  // Anti-QCD Pdfs
  CPepeModel1 aqcd("aqcd",pfmet,qcd.a1);
  CPepeModel1 aqcdp("aqcdp",pfmet,qcdp.a1);
  CPepeModel1 aqcdm("aqcdm",pfmet,qcdm.a1);
  
  // Anti-selection PDFs
  RooAddPdf apdfMet ("apdfMet", "apdfMet", RooArgList(apdfWm,apdfEWK,*(aqcd.model)),   RooArgList(nAntiSig,nAntiEWK,nAntiQCD));  
  RooAddPdf apdfMetp("apdfMetp","apdfMetp",RooArgList(apdfWmp,apdfEWKp,*(aqcdp.model)),RooArgList(nAntiSigp,nAntiEWKp,nAntiQCDp));
  RooAddPdf apdfMetm("apdfMetm","apdfMetm",RooArgList(apdfWmm,apdfEWKm,*(aqcdm.model)),RooArgList(nAntiSigm,nAntiEWKm,nAntiQCDm));
  
  // PDF for simultaneous fit
  RooCategory rooCat("rooCat","rooCat");
  rooCat.defineType("Select");
  rooCat.defineType("Anti");
  
  RooSimultaneous pdfTotal("pdfTotal","pdfTotal",rooCat);
  pdfTotal.addPdf(pdfMet, "Select");
  pdfTotal.addPdf(apdfMet,"Anti");
  
  RooSimultaneous pdfTotalp("pdfTotalp","pdfTotalp",rooCat);
  pdfTotalp.addPdf(pdfMetp, "Select");
  pdfTotalp.addPdf(apdfMetp,"Anti");
  
  RooSimultaneous pdfTotalm("pdfTotalm","pdfTotalm",rooCat);
  pdfTotalm.addPdf(pdfMetm, "Select");
  pdfTotalm.addPdf(apdfMetm,"Anti");
  
  //
  // Perform fits
  //

  RooDataHist dataMet("dataMet", "dataMet", RooArgSet(pfmet), hDataMet);
  RooDataHist antiMet("antiMet", "antiMet", RooArgSet(pfmet), hAntiDataMet);
  RooDataHist dataTotal("dataTotal","dataTotal", RooArgList(pfmet), Index(rooCat),
                        Import("Select", dataMet),
                        Import("Anti",   antiMet));
  RooFitResult *fitRes = pdfTotal.fitTo(dataTotal,Extended(),Minos(kTRUE),Save(kTRUE));
  
  RooDataHist dataMetp("dataMetp", "dataMetp", RooArgSet(pfmet), hDataMetp);
  RooDataHist antiMetp("antiMetp", "antiMetp", RooArgSet(pfmet), hAntiDataMetp);
  RooDataHist dataTotalp("dataTotalp","dataTotalp", RooArgList(pfmet), Index(rooCat),
                         Import("Select", dataMetp),
                         Import("Anti",   antiMetp));
  RooFitResult *fitResp = pdfTotalp.fitTo(dataTotalp,Extended(),Minos(kTRUE),Save(kTRUE));
  
  RooDataHist dataMetm("dataMetm", "dataMetm", RooArgSet(pfmet), hDataMetm);
  RooDataHist antiMetm("antiMetm", "antiMetm", RooArgSet(pfmet), hAntiDataMetm);
  RooDataHist dataTotalm("dataTotalm","dataTotalm", RooArgList(pfmet), Index(rooCat),
                         Import("Select", dataMetm),
                         Import("Anti",   antiMetm));
  RooFitResult *fitResm = pdfTotalm.fitTo(dataTotalm,Extended(),Minos(kTRUE),Save(kTRUE));
    
  //
  // Use histogram version of fitted PDFs to make ratio plots
  // (Will also use PDF histograms later for Chi^2 and KS tests)
  //
  TH1D *hPdfMet = (TH1D*)(pdfMet.createHistogram("hPdfMet", pfmet));
  hPdfMet->Scale((nSig.getVal()+nEWK.getVal()+nQCD.getVal())/hPdfMet->Integral());
  TH1D *hMetDiff = makeDiffHist(hDataMet,hPdfMet,"hMetDiff");
  hMetDiff->SetMarkerStyle(kFullCircle);
  hMetDiff->SetMarkerSize(0.9);
   
  TH1D *hPdfMetp = (TH1D*)(pdfMetp.createHistogram("hPdfMetp", pfmet));
  hPdfMetp->Scale((nSigp.getVal()+nEWKp.getVal()+nQCDp.getVal())/hPdfMetp->Integral());
  TH1D *hMetpDiff = makeDiffHist(hDataMetp,hPdfMetp,"hMetpDiff");
  hMetpDiff->SetMarkerStyle(kFullCircle);
  hMetpDiff->SetMarkerSize(0.9);
    
  TH1D *hPdfMetm = (TH1D*)(pdfMetm.createHistogram("hPdfMetm", pfmet));
  hPdfMetm->Scale((nSigm.getVal()+nEWKm.getVal()+nQCDm.getVal())/hPdfMetm->Integral());
  TH1D *hMetmDiff = makeDiffHist(hDataMetm,hPdfMetm,"hMetmDiff");
  hMetmDiff->SetMarkerStyle(kFullCircle); 
  hMetmDiff->SetMarkerSize(0.9);
   
  TH1D *hPdfAntiMet = (TH1D*)(apdfMet.createHistogram("hPdfAntiMet", pfmet));
  hPdfAntiMet->Scale((nAntiSig.getVal()+nAntiEWK.getVal()+nAntiQCD.getVal())/hPdfAntiMet->Integral());
  TH1D *hAntiMetDiff = makeDiffHist(hAntiDataMet,hPdfAntiMet,"hAntiMetDiff");
  hAntiMetDiff->SetMarkerStyle(kFullCircle);
  hAntiMetDiff->SetMarkerSize(0.9);
   
  TH1D *hPdfAntiMetp = (TH1D*)(apdfMetp.createHistogram("hPdfAntiMetp", pfmet));
  hPdfAntiMetp->Scale((nAntiSigp.getVal()+nAntiEWKp.getVal()+nAntiQCDp.getVal())/hPdfAntiMetp->Integral());
  TH1D *hAntiMetpDiff = makeDiffHist(hAntiDataMetp,hPdfAntiMetp,"hAntiMetpDiff");
  hAntiMetpDiff->SetMarkerStyle(kFullCircle);
  hAntiMetpDiff->SetMarkerSize(0.9);
    
  TH1D *hPdfAntiMetm = (TH1D*)(apdfMetm.createHistogram("hPdfAntiMetm", pfmet));
  hPdfAntiMetm->Scale((nAntiSigm.getVal()+nAntiEWKm.getVal()+nAntiQCDm.getVal())/hPdfAntiMetm->Integral());
  TH1D *hAntiMetmDiff = makeDiffHist(hAntiDataMetm,hPdfAntiMetm,"hAntiMetmDiff");
  hAntiMetmDiff->SetMarkerStyle(kFullCircle); 
  hAntiMetmDiff->SetMarkerSize(0.9);
   
  
  //--------------------------------------------------------------------------------------------------------------
  // Make plots 
  //==============================================================================================================  
  
  TCanvas *c = MakeCanvas("c","c",800,800);
  c->Divide(1,2,0,0);
  c->cd(1)->SetPad(0,0.3,1.0,1.0);
  c->cd(1)->SetTopMargin(0.1);
  c->cd(1)->SetBottomMargin(0.01);
  c->cd(1)->SetLeftMargin(0.15);  
  c->cd(1)->SetRightMargin(0.07);  
  c->cd(1)->SetTickx(1);
  c->cd(1)->SetTicky(1);  
  c->cd(2)->SetPad(0,0,1.0,0.3);
  c->cd(2)->SetTopMargin(0.05);
  c->cd(2)->SetBottomMargin(0.45);
  c->cd(2)->SetLeftMargin(0.15);
  c->cd(2)->SetRightMargin(0.07);
  c->cd(2)->SetTickx(1);
  c->cd(2)->SetTicky(1);
  gStyle->SetTitleOffset(1.100,"Y");
  TGaxis::SetMaxDigits(3);
  
  char ylabel[100];  // string buffer for y-axis label
  
  // label for lumi
  char lumitext[100];
  if(lumi<0.1) sprintf(lumitext,"%.1f pb^{-1}  at  #sqrt{s} = 8 TeV",lumi*1000.);
  else         sprintf(lumitext,"%.2f fb^{-1}  at  #sqrt{s} = 8 TeV",lumi);
  
  // plot colors
  Int_t linecolorW   = kOrange-3;
  Int_t fillcolorW   = kOrange-2;
  Int_t linecolorEWK = kOrange+10;
  Int_t fillcolorEWK = kOrange+7;
  Int_t linecolorQCD = kViolet+2;
  Int_t fillcolorQCD = kViolet-5;
  Int_t ratioColor   = kGray+2;
  
  //
  // Dummy histograms for TLegend
  // (I can't figure out how to properly pass RooFit objects...)
  //
  TH1D *hDummyData = new TH1D("hDummyData","",0,0,10);
  hDummyData->SetMarkerStyle(kFullCircle);
  hDummyData->SetMarkerSize(0.9);
  
  TH1D *hDummyW = new TH1D("hDummyW","",0,0,10);
  hDummyW->SetLineColor(linecolorW);
  hDummyW->SetFillColor(fillcolorW);
  hDummyW->SetFillStyle(1001);
  
  TH1D *hDummyEWK = new TH1D("hDummyEWK","",0,0,10);
  hDummyEWK->SetLineColor(linecolorEWK);
  hDummyEWK->SetFillColor(fillcolorEWK);
  hDummyEWK->SetFillStyle(1001);
  
  TH1D *hDummyQCD = new TH1D("hDummyQCD","",0,0,10);
  hDummyQCD->SetLineColor(linecolorQCD);
  hDummyQCD->SetFillColor(fillcolorQCD);
  hDummyQCD->SetFillStyle(1001);
   
  //
  // W MET plot
  //
  RooPlot *wmframe = pfmet.frame(Bins(NBINS)); 
  wmframe->GetYaxis()->SetNdivisions(505);
  dataMet.plotOn(wmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  pdfMet.plotOn(wmframe,FillColor(fillcolorW),DrawOption("F"));
  pdfMet.plotOn(wmframe,LineColor(linecolorW));
  pdfMet.plotOn(wmframe,Components(RooArgSet(pdfEWK,*(qcd.model))),FillColor(fillcolorEWK),DrawOption("F"));
  pdfMet.plotOn(wmframe,Components(RooArgSet(pdfEWK,*(qcd.model))),LineColor(linecolorEWK));
  pdfMet.plotOn(wmframe,Components(RooArgSet(*(qcd.model))),FillColor(fillcolorQCD),DrawOption("F"));
  pdfMet.plotOn(wmframe,Components(RooArgSet(*(qcd.model))),LineColor(linecolorQCD));
  pdfMet.plotOn(wmframe,Components(RooArgSet(pdfWm)),LineColor(linecolorW),LineStyle(2));
  dataMet.plotOn(wmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));  
  
  sprintf(ylabel,"Events / %.1f GeV",hDataMet->GetBinWidth(1));
  CPlot plotMet("fitmet",wmframe,"","",ylabel);
  plotMet.SetLegend(0.68,0.57,0.93,0.77);
  plotMet.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotMet.GetLegend()->AddEntry(hDummyW,"W#rightarrow#mu#nu","F");
  plotMet.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotMet.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotMet.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotMet.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
  plotMet.SetYRange(0.1,1.1*(hDataMet->GetMaximum()));
  plotMet.Draw(c,kFALSE,format,1);

  CPlot plotMetDiff("fitmet","","#slash{E}_{T} [GeV]","#chi");
  plotMetDiff.AddHist1D(hMetDiff,"EX0",ratioColor);
  plotMetDiff.SetYRange(-8,8);
  plotMetDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotMetDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotMetDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotMetDiff.Draw(c,kTRUE,format,2);
  
  plotMet.SetName("fitmetlog");
  plotMet.SetLogy();
  plotMet.SetYRange(1e-3*(hDataMet->GetMaximum()),10*(hDataMet->GetMaximum()));
  plotMet.Draw(c,kTRUE,format,1);
    
  RooPlot *awmframe = pfmet.frame(Bins(NBINS));    
  antiMet.plotOn(awmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  apdfMet.plotOn(awmframe,FillColor(fillcolorW),DrawOption("F"));
  apdfMet.plotOn(awmframe,LineColor(linecolorW));
  apdfMet.plotOn(awmframe,Components(RooArgSet(apdfEWK,*(aqcd.model))),FillColor(fillcolorEWK),DrawOption("F"));
  apdfMet.plotOn(awmframe,Components(RooArgSet(apdfEWK,*(aqcd.model))),LineColor(linecolorEWK));
  apdfMet.plotOn(awmframe,Components(RooArgSet(*(aqcd.model))),FillColor(fillcolorQCD),DrawOption("F"));
  apdfMet.plotOn(awmframe,Components(RooArgSet(*(aqcd.model))),LineColor(linecolorQCD));
  apdfMet.plotOn(awmframe,Components(RooArgSet(apdfWm)),LineColor(linecolorW),LineStyle(2));
  antiMet.plotOn(awmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));  
  
  sprintf(ylabel,"Events / %.1f GeV",hAntiDataMet->GetBinWidth(1));
  CPlot plotAntiMet("fitantimet",awmframe,"","",ylabel);
  plotAntiMet.SetLegend(0.68,0.57,0.93,0.77);
  plotAntiMet.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotAntiMet.GetLegend()->AddEntry(hDummyW,"W#rightarrow#mu#nu","F");
  plotAntiMet.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotAntiMet.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotAntiMet.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotAntiMet.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
  plotAntiMet.SetYRange(0.1,1.1*(hAntiDataMet->GetMaximum())); 
  plotAntiMet.Draw(c,kFALSE,format,1);

  CPlot plotAntiMetDiff("fitantimet","","#slash{E}_{T} [GeV]","#chi");
  plotAntiMetDiff.AddHist1D(hMetDiff,"EX0",ratioColor);
  plotAntiMetDiff.SetYRange(-8,8);
  plotAntiMetDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotAntiMetDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotAntiMetDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotAntiMetDiff.Draw(c,kTRUE,format,2);
  
  plotAntiMet.SetName("fitantimetlog");
  plotAntiMet.SetLogy();
  plotAntiMet.SetYRange(1e-3*(hAntiDataMet->GetMaximum()),10*(hAntiDataMet->GetMaximum()));
  plotAntiMet.Draw(c,kTRUE,format,1);
    
  //
  // W+ MET plot
  //
  RooPlot *wmpframe = pfmet.frame(Bins(NBINS));
  wmpframe->GetYaxis()->SetNdivisions(505);
  dataMetp.plotOn(wmpframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  pdfMetp.plotOn(wmpframe,FillColor(fillcolorW),DrawOption("F"));
  pdfMetp.plotOn(wmpframe,LineColor(linecolorW));
  pdfMetp.plotOn(wmpframe,Components(RooArgSet(pdfEWKp,*(qcdp.model))),FillColor(fillcolorEWK),DrawOption("F"));
  pdfMetp.plotOn(wmpframe,Components(RooArgSet(pdfEWKp,*(qcdp.model))),LineColor(linecolorEWK));
  pdfMetp.plotOn(wmpframe,Components(RooArgSet(*(qcdp.model))),FillColor(fillcolorQCD),DrawOption("F"));
  pdfMetp.plotOn(wmpframe,Components(RooArgSet(*(qcdp.model))),LineColor(linecolorQCD));
  pdfMetp.plotOn(wmpframe,Components(RooArgSet(pdfWmp)),LineColor(linecolorW),LineStyle(2));
  dataMetp.plotOn(wmpframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));  
  
  sprintf(ylabel,"Events / %.1f GeV",hDataMetp->GetBinWidth(1));
  CPlot plotMetp("fitmetp",wmpframe,"","",ylabel);
  plotMetp.SetLegend(0.68,0.57,0.93,0.77);
  plotMetp.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotMetp.GetLegend()->AddEntry(hDummyW,"W^{+}#rightarrow#mu^{+}#nu","F");
  plotMetp.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotMetp.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotMetp.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotMetp.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
//  plotMetp.SetYRange(0.1,1.1*(hDataMetp->GetMaximum()));
plotMetp.SetYRange(0.1,4100);
  plotMetp.Draw(c,kFALSE,format,1);

  CPlot plotMetpDiff("fitmetp","","#slash{E}_{T} [GeV]","#chi");
  plotMetpDiff.AddHist1D(hMetpDiff,"EX0",ratioColor);
  plotMetpDiff.SetYRange(-8,8);
  plotMetpDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotMetpDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotMetpDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotMetpDiff.Draw(c,kTRUE,format,2);
  
  plotMetp.SetName("fitmetplog");
  plotMetp.SetLogy();
  plotMetp.SetYRange(1e-3*(hDataMetp->GetMaximum()),10*(hDataMetp->GetMaximum()));
  plotMetp.Draw(c,kTRUE,format,1);

  RooPlot *awmpframe = pfmet.frame(Bins(NBINS));    
  antiMetp.plotOn(awmpframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  apdfMetp.plotOn(awmpframe,FillColor(fillcolorW),DrawOption("F"));
  apdfMetp.plotOn(awmpframe,LineColor(linecolorW));
  apdfMetp.plotOn(awmpframe,Components(RooArgSet(apdfEWKp,*(aqcdp.model))),FillColor(fillcolorEWK),DrawOption("F"));
  apdfMetp.plotOn(awmpframe,Components(RooArgSet(apdfEWKp,*(aqcdp.model))),LineColor(linecolorEWK));
  apdfMetp.plotOn(awmpframe,Components(RooArgSet(*(aqcdp.model))),FillColor(fillcolorQCD),DrawOption("F"));
  apdfMetp.plotOn(awmpframe,Components(RooArgSet(*(aqcdp.model))),LineColor(linecolorQCD));
  apdfMetp.plotOn(awmpframe,Components(RooArgSet(apdfWmp)),LineColor(linecolorW),LineStyle(2));
  antiMetp.plotOn(awmpframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));  
  
  sprintf(ylabel,"Events / %.1f GeV",hAntiDataMetp->GetBinWidth(1));
  CPlot plotAntiMetp("fitantimetp",awmpframe,"","",ylabel);
  plotAntiMetp.SetLegend(0.68,0.57,0.93,0.77);
  plotAntiMetp.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotAntiMetp.GetLegend()->AddEntry(hDummyW,"W^{+}#rightarrow#mu^{+}#nu","F");
  plotAntiMetp.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotAntiMetp.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotAntiMetp.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotAntiMetp.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
//  plotAntiMetp.SetYRange(0.1,1.1*(hAntiDataMetp->GetMaximum()));
plotAntiMetp.SetYRange(0.1,1500);
  plotAntiMetp.Draw(c,kFALSE,format,1);

  CPlot plotAntiMetpDiff("fitantimetp","","#slash{E}_{T} [GeV]","#chi");
  plotAntiMetpDiff.AddHist1D(hAntiMetpDiff,"EX0",ratioColor);
  plotAntiMetpDiff.SetYRange(-8,8);
  plotAntiMetpDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotAntiMetpDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotAntiMetpDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotAntiMetpDiff.Draw(c,kTRUE,format,2);
  
  plotAntiMetp.SetName("fitantimetplog");
  plotAntiMetp.SetLogy();
  plotAntiMetp.SetYRange(1e-3*(hAntiDataMetp->GetMaximum()),10*(hAntiDataMetp->GetMaximum()));
  plotAntiMetp.Draw(c,kTRUE,format,1);
  
  //
  // W- MET plot
  //
  RooPlot *wmmframe = pfmet.frame(Bins(NBINS)); 
  wmmframe->GetYaxis()->SetNdivisions(505);
  dataMetm.plotOn(wmmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  pdfMetm.plotOn(wmmframe,FillColor(fillcolorW),DrawOption("F"));
  pdfMetm.plotOn(wmmframe,LineColor(linecolorW));
  pdfMetm.plotOn(wmmframe,Components(RooArgSet(pdfEWKm,*(qcdm.model))),FillColor(fillcolorEWK),DrawOption("F"));
  pdfMetm.plotOn(wmmframe,Components(RooArgSet(pdfEWKm,*(qcdm.model))),LineColor(linecolorEWK));
  pdfMetm.plotOn(wmmframe,Components(RooArgSet(*(qcdm.model))),FillColor(fillcolorQCD),DrawOption("F"));
  pdfMetm.plotOn(wmmframe,Components(RooArgSet(*(qcdm.model))),LineColor(linecolorQCD));
  pdfMetm.plotOn(wmmframe,Components(RooArgSet(pdfWmm)),LineColor(linecolorW),LineStyle(2));
  dataMetm.plotOn(wmmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  
  sprintf(ylabel,"Events / %.1f GeV",hDataMetm->GetBinWidth(1));
  CPlot plotMetm("fitmetm",wmmframe,"","",ylabel);
  plotMetm.SetLegend(0.68,0.57,0.93,0.77);
  plotMetm.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotMetm.GetLegend()->AddEntry(hDummyW,"W^{-}#rightarrow#mu^{-}#bar{#nu}","F");
  plotMetm.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotMetm.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotMetm.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotMetm.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
//  plotMetm.SetYRange(0.1,1.1*(hDataMetm->GetMaximum()));
plotMetm.SetYRange(0.1,4100);
  plotMetm.Draw(c,kFALSE,format,1);

  CPlot plotMetmDiff("fitmetm","","#slash{E}_{T} [GeV]","#chi");
  plotMetmDiff.AddHist1D(hMetmDiff,"EX0",ratioColor);
  plotMetmDiff.SetYRange(-8,8);
  plotMetmDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotMetmDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotMetmDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotMetmDiff.Draw(c,kTRUE,format,2);
  
  plotMetm.SetName("fitmetmlog");
  plotMetm.SetLogy();
  plotMetm.SetYRange(1e-3*(hDataMetm->GetMaximum()),10*(hDataMetm->GetMaximum()));
  plotMetm.Draw(c,kTRUE,format,1);

  RooPlot *awmmframe = pfmet.frame(Bins(NBINS)); 
  antiMetm.plotOn(awmmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  apdfMetm.plotOn(awmmframe,FillColor(fillcolorW),DrawOption("F"));
  apdfMetm.plotOn(awmmframe,LineColor(linecolorW));
  apdfMetm.plotOn(awmmframe,Components(RooArgSet(apdfEWKm,*(aqcdm.model))),FillColor(fillcolorEWK),DrawOption("F"));
  apdfMetm.plotOn(awmmframe,Components(RooArgSet(apdfEWKm,*(aqcdm.model))),LineColor(linecolorEWK));
  apdfMetm.plotOn(awmmframe,Components(RooArgSet(*(aqcdm.model))),FillColor(fillcolorQCD),DrawOption("F"));
  apdfMetm.plotOn(awmmframe,Components(RooArgSet(*(aqcdm.model))),LineColor(linecolorQCD));
  apdfMetm.plotOn(awmmframe,Components(RooArgSet(apdfWmm)),LineColor(linecolorW),LineStyle(2));
  antiMetm.plotOn(awmmframe,MarkerStyle(kFullCircle),MarkerSize(0.9),DrawOption("ZP"));
  
  sprintf(ylabel,"Events / %.1f GeV",hDataMetm->GetBinWidth(1));
  CPlot plotAntiMetm("fitantimetm",awmmframe,"","",ylabel);
  plotAntiMetm.SetLegend(0.68,0.57,0.93,0.77);
  plotAntiMetm.GetLegend()->AddEntry(hDummyData,"data","PL");
  plotAntiMetm.GetLegend()->AddEntry(hDummyW,"W^{-}#rightarrow#mu^{-}#bar{#nu}","F");
  plotAntiMetm.GetLegend()->AddEntry(hDummyEWK,"EWK+t#bar{t}","F");
  plotAntiMetm.GetLegend()->AddEntry(hDummyQCD,"QCD","F");
  plotAntiMetm.AddTextBox(lumitext,0.55,0.80,0.90,0.86,0);
  plotAntiMetm.AddTextBox("CMS Preliminary",0.63,0.92,0.95,0.99,0);
//  plotAntiMetm.SetYRange(0.1,1.1*(hAntiDataMetm->GetMaximum()));
plotAntiMetm.SetYRange(0.1,1500);
  plotAntiMetm.Draw(c,kFALSE,format,1);

  CPlot plotAntiMetmDiff("fitantimetm","","#slash{E}_{T} [GeV]","#chi");
  plotAntiMetmDiff.AddHist1D(hAntiMetmDiff,"EX0",ratioColor);
  plotAntiMetmDiff.SetYRange(-8,8);
  plotAntiMetmDiff.AddLine(0, 0,METMAX, 0,kBlack,1);
  plotAntiMetmDiff.AddLine(0, 5,METMAX, 5,kBlack,3);
  plotAntiMetmDiff.AddLine(0,-5,METMAX,-5,kBlack,3);
  plotAntiMetmDiff.Draw(c,kTRUE,format,2);
  
  plotAntiMetm.SetName("fitantimetmlog");
  plotAntiMetm.SetLogy();
  plotAntiMetm.SetYRange(1e-3*(hAntiDataMetm->GetMaximum()),10*(hAntiDataMetm->GetMaximum()));
  plotAntiMetm.Draw(c,kTRUE,format,1);

    
  //--------------------------------------------------------------------------------------------------------------
  // Output
  //==============================================================================================================
   
  cout << "*" << endl;
  cout << "* SUMMARY" << endl;
  cout << "*--------------------------------------------------" << endl;  
  
  //
  // Write fit results
  //
  ofstream txtfile;
  char txtfname[100];    
  
  ios_base::fmtflags flags;
  
  Double_t chi2prob, chi2ndf;
  Double_t ksprob, ksprobpe;
  
  chi2prob = hDataMet->Chi2Test(hPdfMet,"PUW");
  chi2ndf  = hDataMet->Chi2Test(hPdfMet,"CHI2/NDFUW");
  ksprob   = hDataMet->KolmogorovTest(hPdfMet);
  ksprobpe = hDataMet->KolmogorovTest(hPdfMet,"DX");
  sprintf(txtfname,"%s/fitresWm.txt",CPlot::sOutDir.Data());
  txtfile.open(txtfname);
  assert(txtfile.is_open());
  
  flags = txtfile.flags();
  txtfile << setprecision(10);
  txtfile << " *** Yields *** " << endl;
  txtfile << "Selected: " << hDataMet->Integral() << endl;
  txtfile << "  Signal: " << nSig.getVal() << " +/- " << nSig.getPropagatedError(*fitRes) << endl;
  txtfile << "     QCD: " << nQCD.getVal() << " +/- " << nQCD.getPropagatedError(*fitRes) << endl;
  txtfile << "   Other: " << nEWK.getVal() << " +/- " << nEWK.getPropagatedError(*fitRes) << endl;
  txtfile << endl;
  txtfile.flags(flags);
  
  fitRes->printStream(txtfile,RooPrintable::kValue,RooPrintable::kVerbose);
  txtfile << endl;
  printCorrelations(txtfile, fitRes);
  txtfile << endl;
  printChi2AndKSResults(txtfile, chi2prob, chi2ndf, ksprob, ksprobpe);
  txtfile.close();
  
  chi2prob = hDataMetp->Chi2Test(hPdfMetp,"PUW");
  chi2ndf  = hDataMetp->Chi2Test(hPdfMetp,"CHI2/NDFUW");
  ksprob   = hDataMetp->KolmogorovTest(hPdfMetp);
  ksprobpe = hDataMetp->KolmogorovTest(hPdfMetp,"DX");  
  sprintf(txtfname,"%s/fitresWmp.txt",CPlot::sOutDir.Data());
  txtfile.open(txtfname);
  assert(txtfile.is_open());
  
  flags = txtfile.flags();
  txtfile << setprecision(10);
  txtfile << " *** Yields *** " << endl;
  txtfile << "Selected: " << hDataMetp->Integral() << endl;
  txtfile << "  Signal: " << nSigp.getVal() << " +/- " << nSigp.getPropagatedError(*fitResp) << endl;
  txtfile << "     QCD: " << nQCDp.getVal() << " +/- " << nQCDp.getPropagatedError(*fitResp) << endl;
  txtfile << "   Other: " << nEWKp.getVal() << " +/- " << nEWKp.getPropagatedError(*fitResp) << endl;
  txtfile << endl; 
  txtfile.flags(flags);
  
  fitResp->printStream(txtfile,RooPrintable::kValue,RooPrintable::kVerbose);
  txtfile << endl;
  printCorrelations(txtfile, fitResp);
  txtfile << endl;
  printChi2AndKSResults(txtfile, chi2prob, chi2ndf, ksprob, ksprobpe);
  txtfile.close();

  chi2prob = hDataMetm->Chi2Test(hPdfMetm,"PUW");
  chi2ndf  = hDataMetm->Chi2Test(hPdfMetm,"CHI2/NDFUW");
  ksprob   = hDataMetm->KolmogorovTest(hPdfMetm);
  ksprobpe = hDataMetm->KolmogorovTest(hPdfMetm,"DX");  
  sprintf(txtfname,"%s/fitresWmm.txt",CPlot::sOutDir.Data());
  txtfile.open(txtfname);
  assert(txtfile.is_open());
  
  flags = txtfile.flags();
  txtfile << setprecision(10);
  txtfile << " *** Yields *** " << endl;
  txtfile << "Selected: " << hDataMetm->Integral() << endl;
  txtfile << "  Signal: " << nSigm.getVal() << " +/- " << nSigm.getPropagatedError(*fitResm) << endl;
  txtfile << "     QCD: " << nQCDm.getVal() << " +/- " << nQCDm.getPropagatedError(*fitResm) << endl;
  txtfile << "   Other: " << nEWKm.getVal() << " +/- " << nEWKm.getPropagatedError(*fitResm) << endl;
  txtfile << endl;
  txtfile.flags(flags);
  
  fitResm->printStream(txtfile,RooPrintable::kValue,RooPrintable::kVerbose);
  txtfile << endl;
  printCorrelations(txtfile, fitResm);
  txtfile << endl;
  printChi2AndKSResults(txtfile, chi2prob, chi2ndf, ksprob, ksprobpe);
  txtfile.close();

  makeHTML(outputDir);
  
  cout << endl;
  cout << "  <> Output saved in " << outputDir << "/" << endl;    
  cout << endl;     
  
  gBenchmark->Show("fitWm");
}
コード例 #2
0
int scaleSmearTemplateFit_Ele(TString RDFile, TString MCFile, char BaseName[30])
{
  cout<<"Processing "<<BaseName<<endl;
  gStyle->SetPalette(1);
  //Output file
  TString ResultDir = "MCRDfitEle_Plot";
  ofstream Fout;
  TString FoutName=ResultDir+"/"+BaseName+"_SummaryFinal.txt";
  Fout.open(FoutName);
  //Variables
  char histName[50];
  //Data and histograms
  TFile *f_RD = new TFile(RDFile);
  TFile *f_MC = new TFile(MCFile);
  TH1D *h1_ZmassDaughEtaRD[ScElCombiBins];
  TH1D *h1_ZmassDaughEtaMC[ScElCombiBins];

  //Plots
  RooPlot *zmassFrameRD;
  RooPlot *zmassFrameMC;
  CPlot *plotFitRD;
  CPlot *plotFitMC;
  RooFitResult* fitResTotalMC;
  RooFitResult* fitResTotalRD;
  //RooFitResult* fitResRD;
  RooFitResult* fitResMCbw[ScElCombiBins];
  RooFitResult* fitResMC[ScElCombiBins];
  RooFitResult* fitResRD[ScElCombiBins];

  TCanvas *myCan = MakeCanvas("myCan","myCan",800,600);
  //Fitting stuff
  RooRealVar *scaleMC[ScaleBins];
  RooRealVar *scaleRD[ScaleBins];
  RooRealVar *smearRD[ScaleBins];
  RooRealVar *smearMC[ScaleBins];


  RooRealVar *nSRD[ScElCombiBins];
  RooRealVar *nBRD[ScElCombiBins];
  RooRealVar *CBalpha[ScElCombiBins];
  RooRealVar *CBn[ScElCombiBins];

  RooFormulaVar *scaleScaleMC[ScElCombiBins];
  RooFormulaVar *scaleScaleRD[ScElCombiBins];
  RooFormulaVar *smearSmearMC[ScElCombiBins];
  RooFormulaVar *smearSmearRD[ScElCombiBins];

  RooRealVar zMass("zMass","zMass",60,120);
  RooCategory rooCat("rooCat","rooCat");
  RooDataHist *ZmassRD[ScElCombiBins];

// RooDataHist *ZmassMC;
 RooDataHist *ZmassMC[ScElCombiBins];
//  CBreitWigner *BW[ScElCombiBins];
  CBreitWignerConvCrystalBall *BWCB[ScElCombiBins];
  CBreitWignerConvCrystalBallScale *BWCBsCale[ScElCombiBins];
  CBreitWignerMC  *BWMC[ScElCombiBins];
  CBreitWignerRD  *BWRD[ScElCombiBins];

  RooGaussian *gaus[ScElCombiBins];
  RooCBShape *cbMC[ScElCombiBins];
  RooCBShape *cbRD[ScElCombiBins];

  CErfExpo *ZbgRD[ScElCombiBins];

  CExponential *ZbgExpRD[ScElCombiBins];

  RooAbsPdf *pdfRDsig[ScElCombiBins];
  RooAbsPdf *pdfMCsig[ScElCombiBins];
  //RooAddPdf *pdfRDsig[ScElCombiBins];
  RooAbsPdf *histPdf[ScElCombiBins];
  RooAddPdf *pdfRD[ScElCombiBins];


  //Initialization
  for(int i(0);i<ScaleBins;i++)
  {
    sprintf(histName,"scaleMC_%d",i);
    scaleMC[i] = new RooRealVar(histName, histName,1.01,0.001,3);
    sprintf(histName,"scaleRD_%d",i);
    scaleRD[i] = new RooRealVar(histName, histName,1.01,0.001,3);
    sprintf(histName,"smearMC_%d",i);
    smearMC[i] = new RooRealVar(histName,histName,0.5,0.01,3);
    sprintf(histName,"smearRD_%d",i);
    smearRD[i] = new RooRealVar(histName,histName,0.5,0.01,3);
  }

  for(int i(0);i<ScElCombiBins;i++)
  {
    sprintf(histName,"nBRD_%d",i);
    nBRD[i] = new RooRealVar(histName, histName,0.1,0.01,1);
    sprintf(histName,"nSRD_%d",i);
    nSRD[i]= new RooRealVar(histName,histName,1,0,2);
    sprintf(histName,"CBalpha_%d",i); 
    CBalpha[i]= new RooRealVar(histName,histName,5,0,20);
    sprintf(histName,"CBn_%d",i);     
    CBn[i]     = new RooRealVar(histName,histName,1,0,10);

    sprintf(histName,"cat_%d",i);
    rooCat.defineType(histName);
  }

  RooSimultaneous pdfTotalMC("pdfTotalMC","pdfTotalMC",rooCat);
  map<string,TH1*>hmapMC;
  RooSimultaneous pdfTotalRD("pdfTotalRD","pdfTotalRD",rooCat);
  map<string,TH1*>hmapRD;

 
  //*
  ///scaleScale
  sprintf(histName,"scaleScaleMC_0");
  scaleScaleMC[0] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[0],*scaleMC[0]));
  sprintf(histName,"scaleScaleMC_1");
  scaleScaleMC[1] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[0],*scaleMC[1]));
  sprintf(histName,"scaleScaleMC_2");
  scaleScaleMC[2] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[0],*scaleMC[2]));
  sprintf(histName,"scaleScaleMC_3");
  scaleScaleMC[3] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[0],*scaleMC[3]));
  sprintf(histName,"scaleScaleMC_4");
  scaleScaleMC[4] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[0],*scaleMC[4]));
  sprintf(histName,"scaleScaleMC_5");
  scaleScaleMC[5] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[0],*scaleMC[5]));
  sprintf(histName,"scaleScaleMC_6");
  scaleScaleMC[6] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[1],*scaleMC[1]));

  sprintf(histName,"scaleScaleMC_7");
  scaleScaleMC[7] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[1],*scaleMC[2]));
  sprintf(histName,"scaleScaleMC_8");
  scaleScaleMC[8] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[1],*scaleMC[3]));
  sprintf(histName,"scaleScaleMC_9");
  scaleScaleMC[9] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[1],*scaleMC[4]));
  sprintf(histName,"scaleScaleMC_10");
  scaleScaleMC[10] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[1],*scaleMC[5]));
  sprintf(histName,"scaleScaleMC_11");
  scaleScaleMC[11] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[2],*scaleMC[2]));

  sprintf(histName,"scaleScaleMC_12");
  scaleScaleMC[12] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[2],*scaleMC[3]));
  sprintf(histName,"scaleScaleMC_13");
  scaleScaleMC[13] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[2],*scaleMC[4]));
  sprintf(histName,"scaleScaleMC_14");
  scaleScaleMC[14] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[2],*scaleMC[5]));
  sprintf(histName,"scaleScaleMC_15");
  scaleScaleMC[15] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[3],*scaleMC[3]));

  sprintf(histName,"scaleScaleMC_16");
  scaleScaleMC[16] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[3],*scaleMC[4]));
  sprintf(histName,"scaleScaleMC_17");
  scaleScaleMC[17] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[3],*scaleMC[5]));
  sprintf(histName,"scaleScaleMC_18");
  scaleScaleMC[18] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[4],*scaleMC[4]));

  sprintf(histName,"scaleScaleMC_19");
  scaleScaleMC[19] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleMC[4],*scaleMC[5]));
  sprintf(histName,"scaleScaleMC_20");
  scaleScaleMC[20] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleMC[5],*scaleMC[5]));
  //*/


  //*
  ///scaleScale
  sprintf(histName,"scaleScaleRD_0");
  scaleScaleRD[0] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[0],*scaleRD[0]));
  sprintf(histName,"scaleScaleRD_1");
  scaleScaleRD[1] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[0],*scaleRD[1]));
  sprintf(histName,"scaleScaleRD_2");
  scaleScaleRD[2] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[0],*scaleRD[2]));
  sprintf(histName,"scaleScaleRD_3");
  scaleScaleRD[3] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[0],*scaleRD[3]));
  sprintf(histName,"scaleScaleRD_4");
  scaleScaleRD[4] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[0],*scaleRD[4]));
  sprintf(histName,"scaleScaleRD_5");
  scaleScaleRD[5] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[0],*scaleRD[5]));
  sprintf(histName,"scaleScaleRD_6");
  scaleScaleRD[6] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[1],*scaleRD[1]));

  sprintf(histName,"scaleScaleRD_7");
  scaleScaleRD[7] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[1],*scaleRD[2]));
  sprintf(histName,"scaleScaleRD_8");
  scaleScaleRD[8] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[1],*scaleRD[3]));
  sprintf(histName,"scaleScaleRD_9");
  scaleScaleRD[9] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[1],*scaleRD[4]));
  sprintf(histName,"scaleScaleRD_10");
  scaleScaleRD[10] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[1],*scaleRD[5]));
  sprintf(histName,"scaleScaleRD_11");
  scaleScaleRD[11] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[2],*scaleRD[2]));

  sprintf(histName,"scaleScaleRD_12");
  scaleScaleRD[12] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[2],*scaleRD[3]));
  sprintf(histName,"scaleScaleRD_13");
  scaleScaleRD[13] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[2],*scaleRD[4]));
  sprintf(histName,"scaleScaleRD_14");
  scaleScaleRD[14] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[2],*scaleRD[5]));
  sprintf(histName,"scaleScaleRD_15");
  scaleScaleRD[15] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[3],*scaleRD[3]));

  sprintf(histName,"scaleScaleRD_16");
  scaleScaleRD[16] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[3],*scaleRD[4]));
  sprintf(histName,"scaleScaleRD_17");
  scaleScaleRD[17] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[3],*scaleRD[5]));
  sprintf(histName,"scaleScaleRD_18");
  scaleScaleRD[18] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[4],*scaleRD[4]));

  sprintf(histName,"scaleScaleRD_19");
  scaleScaleRD[19] = new RooFormulaVar(histName,"1/sqrt(@0*@1)",RooArgSet(*scaleRD[4],*scaleRD[5]));
  sprintf(histName,"scaleScaleRD_20");
  scaleScaleRD[20] = new RooFormulaVar(histName,"1/sqrt(@0*@0)",RooArgSet(*scaleRD[5],*scaleRD[5]));
//*/


  //*
  ///smearSmear
  sprintf(histName,"smearSmearMC_0");
  smearSmearMC[0] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[0],*smearMC[0]));
  sprintf(histName,"smearSmearMC_1");
  smearSmearMC[1] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[0],*smearMC[1]));
  sprintf(histName,"smearSmearMC_2");
  smearSmearMC[2] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[0],*smearMC[2]));
  sprintf(histName,"smearSmearMC_3");
  smearSmearMC[3] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[0],*smearMC[3]));
  sprintf(histName,"smearSmearMC_4");
  smearSmearMC[4] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[0],*smearMC[4]));
  sprintf(histName,"smearSmearMC_5");
  smearSmearMC[5] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[0],*smearMC[5]));
  sprintf(histName,"smearSmearMC_6");
  smearSmearMC[6] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[1],*smearMC[1]));

  sprintf(histName,"smearSmearMC_7");
  smearSmearMC[7] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[1],*smearMC[2]));
  sprintf(histName,"smearSmearMC_8");
  smearSmearMC[8] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[1],*smearMC[3]));
  sprintf(histName,"smearSmearMC_9");
  smearSmearMC[9] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[1],*smearMC[4]));
  sprintf(histName,"smearSmearMC_10");
  smearSmearMC[10] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[1],*smearMC[5]));
  sprintf(histName,"smearSmearMC_11");
  smearSmearMC[11] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[2],*smearMC[2]));

  sprintf(histName,"smearSmearMC_12");
  smearSmearMC[12] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[2],*smearMC[3]));
  sprintf(histName,"smearSmearMC_13");
  smearSmearMC[13] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[2],*smearMC[4]));
  sprintf(histName,"smearSmearMC_14");
  smearSmearMC[14] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[2],*smearMC[5]));
  sprintf(histName,"smearSmearMC_15");
  smearSmearMC[15] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[3],*smearMC[3]));

  sprintf(histName,"smearSmearMC_16");
  smearSmearMC[16] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[3],*smearMC[4]));
  sprintf(histName,"smearSmearMC_17");
  smearSmearMC[17] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[3],*smearMC[5]));
  sprintf(histName,"smearSmearMC_18");
  smearSmearMC[18] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[4],*smearMC[4]));

  sprintf(histName,"smearSmearMC_19");
  smearSmearMC[19] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearMC[4],*smearMC[5]));
  sprintf(histName,"smearSmearMC_20");
  smearSmearMC[20] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearMC[5],*smearMC[5]));
//*/  


  //*
  ///smearSmear
  sprintf(histName,"smearSmearRD_0");
  smearSmearRD[0] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[0],*smearRD[0]));
  sprintf(histName,"smearSmearRD_1");
  smearSmearRD[1] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[0],*smearRD[1]));
  sprintf(histName,"smearSmearRD_2");
  smearSmearRD[2] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[0],*smearRD[2]));
  sprintf(histName,"smearSmearRD_3");
  smearSmearRD[3] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[0],*smearRD[3]));
  sprintf(histName,"smearSmearRD_4");
  smearSmearRD[4] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[0],*smearRD[4]));
  sprintf(histName,"smearSmearRD_5");
  smearSmearRD[5] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[0],*smearRD[5]));
  sprintf(histName,"smearSmearRD_6");
  smearSmearRD[6] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[1],*smearRD[1]));

  sprintf(histName,"smearSmearRD_7");
  smearSmearRD[7] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[1],*smearRD[2]));
  sprintf(histName,"smearSmearRD_8");
  smearSmearRD[8] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[1],*smearRD[3]));
  sprintf(histName,"smearSmearRD_9");
  smearSmearRD[9] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[1],*smearRD[4]));
  sprintf(histName,"smearSmearRD_10");
  smearSmearRD[10] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[1],*smearRD[5]));
  sprintf(histName,"smearSmearRD_11");
  smearSmearRD[11] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[2],*smearRD[2]));

  sprintf(histName,"smearSmearRD_12");
  smearSmearRD[12] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[2],*smearRD[3]));
  sprintf(histName,"smearSmearRD_13");
  smearSmearRD[13] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[2],*smearRD[4]));
  sprintf(histName,"smearSmearRD_14");
  smearSmearRD[14] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[2],*smearRD[5]));
  sprintf(histName,"smearSmearRD_15");
  smearSmearRD[15] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[3],*smearRD[3]));

  sprintf(histName,"smearSmearRD_16");
  smearSmearRD[16] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[3],*smearRD[4]));
  sprintf(histName,"smearSmearRD_17");
  smearSmearRD[17] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[3],*smearRD[5]));
  sprintf(histName,"smearSmearRD_18");
  smearSmearRD[18] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[4],*smearRD[4]));

  sprintf(histName,"smearSmearRD_19");
  smearSmearRD[19] = new RooFormulaVar(histName,"sqrt(@0*@0+@1*@1)",RooArgSet(*smearRD[4],*smearRD[5]));
  sprintf(histName,"smearSmearRD_20");
  smearSmearRD[20] = new RooFormulaVar(histName,"sqrt(@0*@0+@0*@0)",RooArgSet(*smearRD[5],*smearRD[5]));
//*/

  RooRealVar gaussMean("gaussMean","gaussMean",0);
  gaussMean.setConstant(kTRUE);
  RooRealVar CBMean("CBMean","CBMean",0);
  CBMean.setConstant(kTRUE);
  for(int i(0);i<ScElCombiBins;i++)
  {
    //Getting histogram from RD & MC
    sprintf(histName,"h1_ZmassDaughEtaEle_%d",i);
    h1_ZmassDaughEtaMC[i] = (TH1D*)f_MC->Get(histName)->Clone(histName);
    h1_ZmassDaughEtaRD[i] = (TH1D*)f_RD->Get(histName)->Clone(histName);

    sprintf(histName,"ZmassRD_%d",i);
    ZmassRD[i] =
     new RooDataHist(histName,histName,RooArgSet(zMass),h1_ZmassDaughEtaRD[i]);

    sprintf(histName,"cat_%d",i);
    hmapMC[histName] = h1_ZmassDaughEtaMC[i];
    sprintf(histName,"cat_%d",i);
    hmapRD[histName] = h1_ZmassDaughEtaRD[i];

    // Making template from MC
    sprintf(histName,"ZmassMC_%d",i);
    ZmassMC[i] = new RooDataHist(histName,histName,RooArgSet(zMass),h1_ZmassDaughEtaMC[i]);
    
    sprintf(histName,"cbMC_%d",i);
    cbMC[i] = new RooCBShape(histName,histName,zMass,CBMean,*smearSmearMC[i],*CBalpha[i],*CBn[i]);
    sprintf(histName,"BWMC_%d",i);
    BWMC[i] = new CBreitWignerMC(histName,zMass, scaleScaleMC[i]);

    sprintf(histName,"pdfMCsig_%d",i);
    pdfMCsig[i] = new RooFFTConvPdf(histName,histName,zMass,*(BWMC[i]->model),*cbMC[i]);


    //Making RD pdf
//    sprintf(histName,"gaus_%d",i);
//    gaus[i] = new RooGaussian(histName,histName,zMass,gaussMean,*smearSmearRD[i]);
   
//    sprintf(histName,"BWCBsCale_%d",i);
//    BWCBsCale[i] = new CBreitWignerConvCrystalBallScale
 //     (histName,zMass, scaleScaleRD[i],
 //     BWCB[i]->mean->getVal(),BWCB[i]->sigma->getVal(),BWCB[i]->alpha->getVal(),BWCB[i]->n->getVal());

//*

    sprintf(histName,"cbRD_%d",i);
    cbRD[i] = new RooCBShape(histName,histName,zMass,CBMean,*smearSmearRD[i],*CBalpha[i],*CBn[i]);

    sprintf(histName,"BWRD_%d",i);
    BWRD[i] = new CBreitWignerRD(histName,zMass, scaleScaleRD[i]);
  sprintf(histName,"pdfRDsig_%d",i);
  //  pdfRDsig[i] = new RooFFTConvPdf(histName,histName,zMass,*(BWCBsCale[i]->model),*gaus[i]);
  pdfRDsig[i] = new RooFFTConvPdf(histName,histName,zMass,*(BWRD[i]->model),*cbRD[i]);
    

    
    sprintf(histName,"cat_%d",i);
    pdfTotalMC.addPdf(*pdfMCsig[i],histName);
 
    sprintf(histName,"cat_%d",i);
    pdfTotalRD.addPdf(*pdfRDsig[i],histName);
  
}
 RooDataHist MCTotal("MCTotal","MCTotal",zMass,rooCat,hmapMC);
 fitResTotalMC = pdfTotalMC.fitTo(MCTotal,Minos(kTRUE),Save(kTRUE),SumW2Error(kTRUE));

 RooDataHist dataTotal("dataTotal","dataTotal",zMass,rooCat,hmapRD);
 fitResTotalRD = pdfTotalRD.fitTo(dataTotal,Minos(kTRUE),Save(kTRUE),SumW2Error(kTRUE));

 Fout<<"bin range \t scale \t smear "<<endl;

  for(int i(0);i<ScaleBins;i++)
  {
//    cout<<" "<<i<<"\t"<<scaleMC[i]->getVal()<<"+"<<scaleMC[i]->getError()<<"\t"<<smearMC[i]->getVal()<<"+"<<smearMC[i]->getError()<<endl;
//    Fout<<" "<<i<<"\t"<<scaleMC[i]->getVal()<<"+"<<scaleMC[i]->getError()<<"\t"<<smearMC[i]->getVal()<<"+"<<smearMC[i]->getError()<<endl;
  
//    cout<<" "<<i<<"\t"<<scaleRD[i]->getVal()<<"+"<<scaleRD[i]->getError()<<"\t"<<smearRD[i]->getVal()<<"+"<<smearRD[i]->getError()<<endl;
//    Fout<<" "<<i<<"\t"<<scaleRD[i]->getVal()<<"+"<<scaleRD[i]->getError()<<"\t"<<smearRD[i]->getVal()<<"+"<<smearRD[i]->getError()<<endl;
 



cout<<" "<<i<<"\t"<<scaleRD[i]->getVal()/scaleMC[i]->getVal()<<"+-"<<(scaleRD[i]->getVal()/scaleMC[i]->getVal())*sqrt((scaleRD[i]->getError()*scaleRD[i]->getError())/scaleRD[i]->getVal()/scaleRD[i]->getVal()+(scaleMC[i]->getError()*scaleMC[i]->getError())/scaleMC[i]->getVal()/scaleMC[i]->getVal())<<"\t"<<sqrt(smearRD[i]->getVal()*smearRD[i]->getVal()- smearMC[i]->getVal()*smearMC[i]->getVal())<<"+"<<sqrt(smearRD[i]->getError()*smearRD[i]->getError()+ smearMC[i]->getError()*smearMC[i]->getError())<<endl;

  
Fout<<" "<<i<<"\t"<<scaleRD[i]->getVal()/scaleMC[i]->getVal()<<"+-"<<(scaleRD[i]->getVal()/scaleMC[i]->getVal())*sqrt((scaleRD[i]->getError()*scaleRD[i]->getError())/scaleRD[i]->getVal()/scaleRD[i]->getVal()+(scaleMC[i]->getError()*scaleMC[i]->getError())/scaleMC[i]->getVal()/scaleMC[i]->getVal())<<"\t"<<sqrt(smearRD[i]->getVal()*smearRD[i]->getVal()- smearMC[i]->getVal()*smearMC[i]->getVal())  <<"+"<<sqrt(smearRD[i]->getError()*smearRD[i]->getError()+ smearMC[i]->getError()*smearMC[i]->getError())<<endl;

  }
//*
  for(int i(0);i<ScElCombiBins;i++)
  {
    //*
    zmassFrameMC = zMass.frame();
     ZmassMC[i]->plotOn(zmassFrameMC,DrawOption("p"));
     pdfMCsig[i]->plotOn(zmassFrameMC,DrawOption("l"));
     sprintf(histName,"ZmassMC_%s_%d",BaseName,i);

     plotFitMC = new CPlot(histName,zmassFrameMC,"","Di-Lepton M","");
     plotFitMC->setOutDir(ResultDir);
     plotFitMC->Draw(myCan,kTRUE,"png");
//*/
     zmassFrameRD = zMass.frame();
     ZmassRD[i]->plotOn(zmassFrameRD,DrawOption("p"));
     pdfRDsig[i]->plotOn(zmassFrameRD,DrawOption("l"));
     sprintf(histName,"ZmassRD_%s_%d",BaseName,i);

     plotFitRD = new CPlot(histName,zmassFrameRD,"","Di-Lepton M","");
     plotFitRD->setOutDir(ResultDir);
     plotFitRD->Draw(myCan,kTRUE,"png");
  }
//*/  
  Fout.close();
  return 0;
}