void grabDataSubtractedHistograms(int nJet, int massRange) {
  TFile f("Histograms_data_and_template.root", "update");


  // Figure out the plot directory name first
  TString dir2jet = "./plots_10172011_2jetsample";
  TString dir3jet = "./plots_10172011_3jetsample";
  TString dirName = "";
  if(nJet==2) dirName = dir2jet;
  if(nJet==3) dirName = dir3jet;

  TString massStr = "";
  if(nJet==2 && massRange==1) massStr = "150-230";
  else if(nJet==2 && massRange==2) massStr = "200-400";
  else if(nJet==2 && massRange==3) massStr = "360-500";
  else if(nJet==2 && massRange==4) massStr = "450-800";
  else if(nJet==3 && massRange==1) massStr = "150-230";
  else if(nJet==3 && massRange==2) massStr = "200-400";
  else if(nJet==3 && massRange==3) massStr = "360-800";

  TString ConnectorStr = "";
  if( !(massStr=="") ) ConnectorStr = "-";
  TString fitFileName = TString("mLnuJJ-") + massStr + ConnectorStr + TString("combined-fit");


  TFile* fitFile = new TFile( dirName + TString("/") + fitFileName+".root", "read");
  TCanvas* fitCan = (TCanvas*) fitFile->Get( fitFileName );
  RooHist* data = (RooHist*) fitCan->FindObject( "h_data" );
  RooCurve* fit = (RooCurve*) fitCan->FindObject( "h_total" );
  RooCurve* fit_wjj = (RooCurve*) fitCan->FindObject( "h_Wjets" );
  RooCurve* fit_diboson = (RooCurve*) fitCan->FindObject( "h_diboson" );
  RooCurve* fit_Top = (RooCurve*) fitCan->FindObject( "h_Top" );
  RooCurve* fit_QCD = (RooCurve*) fitCan->FindObject( "h_QCD" );
  RooCurve* fit_Zjets = (RooCurve*) fitCan->FindObject( "h_Zjets" );


  TFile* systFileUp = new TFile( dirName + TString("SystUp/") + fitFileName+".root", "read");
  TCanvas* systFileUpCan = (TCanvas*) systFileUp->Get( fitFileName );
  RooCurve* fit_wjj_systUp = (RooCurve*) systFileUpCan->FindObject( "h_Wjets" );
  RooCurve* fit_systUp = (RooCurve*) systFileUpCan->FindObject( "h_total" );

  TFile* systFileDown = new TFile( dirName + TString("SystDown/") + fitFileName+".root", "read");
  TCanvas* systFileDownCan = (TCanvas*) systFileDown->Get( fitFileName );
  RooCurve* fit_wjj_systDown = (RooCurve*) systFileDownCan->FindObject( "h_Wjets" );
  RooCurve* fit_systDown = (RooCurve*) systFileDownCan->FindObject("h_total");


  ///// Now save everything ///////////////
  TString outPrefix = TString("2jet_MassRange_") + massStr + TString("_");
  if(nJet==3) outPrefix = TString("3jet_MassRange_") + massStr + TString("_");


  data->SetName( outPrefix+"hist_data" );
  fit->SetName( outPrefix+"curve_fitTotal" );
  fit_wjj->SetName( outPrefix+"curve_WJets" );
  fit_diboson->SetName( outPrefix+"curve_diboson" );
  fit_Top->SetName(  outPrefix+"curve_Top" );
  fit_QCD->SetName(  outPrefix+"curve_QCD" );
  fit_Zjets->SetName(  outPrefix+"curve_Zjets" );
  fit_wjj_systUp->SetName( outPrefix+"curve_WJets_SystUp" );
  fit_wjj_systDown->SetName( outPrefix+"curve_WJets_SystDown" );
  fit_systUp->SetName( outPrefix+"curve_fitTotal_SystUp" );
  fit_systDown->SetName( outPrefix+"curve_fitTotal_SystDown" );

  
  f.cd();
  data->Write();
  fit->Write();
  fit_wjj->Write();
  fit_diboson->Write();
  fit_Top->Write();
  fit_QCD->Write();
  fit_Zjets->Write();
  fit_wjj_systUp->Write();
  fit_wjj_systDown->Write();
  fit_systUp->Write();
  fit_systDown->Write();

  f.Close();
  delete fitFile;
  delete systFileUp;
  delete systFileDown;
}
void grabDataSubtractedHistograms() {
  TFile f("Histograms_Mjj_data_and_template.root", "update");


  TFile* fitFile = new TFile( plots_dir + "/mJJ-combined-fit.root", "read");
  TCanvas* fitCan = (TCanvas*) fitFile->Get( "mJJ-combined-fit" );
  RooHist* data = (RooHist*) fitCan->FindObject( "h_data" );
  RooCurve* fit_total = (RooCurve*) fitCan->FindObject( "h_total" );
  RooCurve* fit_diboson = (RooCurve*) fitCan->FindObject( "h_diboson" );
  RooCurve* fit_Wjets = (RooCurve*) fitCan->FindObject( "h_Wjets" );

  RooCurve* fit_Top = (RooCurve*) fitCan->FindObject( "h_Top" );
  RooCurve* fit_QCD = (RooCurve*) fitCan->FindObject( "h_QCD" );
  RooCurve* fit_Zjets = (RooCurve*) fitCan->FindObject( "h_Zjets" );
//   RooCurve* fit_Ztautau = (RooCurve*) fitCan->FindObject( "h_Ztautau" );

  TFile* subtrFile = new TFile( plots_dir + "/mJJ-combined-fit-subtracted.root", "read");
  TCanvas* subtrCan = (TCanvas*) subtrFile->Get( "mJJ-combined-fit-subtracted" );
  RooHist* subtrHist = (RooHist*) subtrCan->FindObject( "resid_h_data_h_Background" );
  RooCurve* Diboson = (RooCurve*) subtrCan->FindObject( "h_diboson" );



//   TFile* fitFile1 = new TFile( plots_dir + "/mJJ-mu-fit.root", "read");
//   TCanvas* fitCan1 = (TCanvas*) fitFile1->Get( "mJJ-mu-fit" );
//   RooHist* data1 = (RooHist*) fitCan1->FindObject( "h_data" );
//   RooCurve* fit1 = (RooCurve*) fitCan1->FindObject( "h_total" );

//   TFile* subtrFile1 = new TFile( plots_dir + "/mJJ-mu-fit-subtracted.root", "read");
//   TCanvas* subtrCan1 = (TCanvas*) subtrFile1->Get( "mJJ-mu-fit-subtracted" );
//   RooHist* subtrHist1 = (RooHist*) subtrCan1->FindObject( "resid_h_data_h_Background" );
//   RooCurve* Diboson1 = (RooCurve*) subtrCan1->FindObject( "h_diboson" );



//   TFile* fitFile2 = new TFile( plots_dir + "/mJJ-ele-fit.root", "read");
//   TCanvas* fitCan2 = (TCanvas*) fitFile2->Get( "mJJ-ele-fit" );
//   RooHist* data2 = (RooHist*) fitCan2->FindObject( "h_data" );
//   RooCurve* fit2 = (RooCurve*) fitCan2->FindObject( "h_total" );

//   TFile* subtrFile2 = new TFile( plots_dir + "/mJJ-ele-fit-subtracted.root", "read");
//   TCanvas* subtrCan2 = (TCanvas*) subtrFile2->Get( "mJJ-ele-fit-subtracted" );
//   RooHist* subtrHist2 = (RooHist*) subtrCan2->FindObject( "resid_h_data_h_Background" );
//   RooCurve* Diboson2 = (RooCurve*) subtrCan2->FindObject( "h_diboson" );


  // --------- Now save everything in the output file -------
 f.cd();
 data->Write("hist_data");
 fit_total->Write("fit_total");
 fit_diboson->Write("fit_diboson");
 fit_Wjets->Write("fit_Wjets");
 fit_Top->Write("fit_Top");
 fit_QCD->Write("fit_QCD");
 fit_Zjets->Write("fit_Zjets");
//  fit_Ztautau->Write("fit_Ztautau");

 subtrHist->Write("hist_data_subtracted");
 Diboson->Write("curve_diboson");

//  data1->Write("hist_data_muon");
//  fit1->Write("curve_fit_muon");
//  subtrHist1->Write("hist_data_subtracted_muon");
//  Diboson1->Write("curve_diboson_muon");

//  data2->Write("hist_data_electron");
//  fit2->Write("curve_fit_electron");
//  subtrHist2->Write("hist_data_subtracted_electron");
//  Diboson2->Write("curve_diboson_electron");

 f.Close();
}