void myControlPlots(const char *cuttablefilename,
		    const char *qcdcuttablefilename,
		    const char *samplefilename,
		    const plotVar_t plotvars[])
{
  //gROOT->ProcessLine(".L tdrstyle.C");

  TString unwtcutstring, qcdcutstring;

  loadCutString(cuttablefilename, unwtcutstring);
  if (strlen(qcdcuttablefilename))
    loadCutString(qcdcuttablefilename, qcdcutstring);

  //  const char* the_cut = "1";
  //  double BINWIDTH = ((MAXRange-MINRange)/NBINS);

 // Get the input trees:

  vector<Sample *> samples;

  loadSamples(samplefilename,samples);

 // Data

  Sample *sdata = samples[0];
  cout << "ndata =" << sdata->Tree()->GetEntries() <<endl;

  TFile f("plotvar_histo.root", "RECREATE");

  //============================================================
  //  VARIABLE LOOP
  //============================================================

  for (int ivar=0; ; ivar++) {

    plotVar_t pv = plotvars[ivar];
 
    if ( !pv.plotvar.Length() ) break;

    cout << pv.plotvar << "\t"<<pv.MINRange<<"\t" << pv.MAXRange<<"\t" << pv.NBINS<<"\tTHE CUT " << endl;

    if ( sdata->Tree()->Draw(pv.plotvar,"","goff",1) == -1 ) { // check if the variable exists in the tree
      cout << "\t...can't be plotted!" << endl;
      continue;
    }

    TCut the_cut(TString("effwt*puwt*")+unwtcutstring);
    TCut the_cutE(TString("effwt*puwt*puwt*")+unwtcutstring);
    TCut qcd_cut;
    if (qcdcutstring.Length())
      qcd_cut = TCut(TString("effwt*puwt*")+qcdcutstring);

    TCut nullcut("");

    const double BINWIDTH = ((pv.MAXRange-pv.MINRange)/pv.NBINS);

    map<TString, TH1 *> m_histos;
    map<TString, bool> m_stacked;

    double totevents = 0.;
    TH1 * th1qcd = NULL;
    double qcdfrac = 0.;

    //============================================================
    // DRAW THE VARIABLE FOR ALL SAMPLES, CREATE HISTOS
    //============================================================

    for (size_t isamp=0; isamp<samples.size(); isamp++) {
      Sample *s = samples[isamp];

      m_stacked[s->name()] = false;

      TH1 *h;

      if (s->name().EqualTo("data"))            h = s->Draw(pv, the_cut, nullcut); // effwt*puwt==1 for data!
      else if (s->filename().Contains("QCD")) { h = s->Draw(pv, qcd_cut, nullcut);
	th1qcd = h;
	qcdfrac = s->otherscale();
      } else {                                  h = s->Draw(pv, the_cut, the_cutE);
	if (s->stackit()) {
	  totevents += h->Integral();
	}
      }

      map<TString, TH1 *>::iterator mit = m_histos.find(s->name());
      if (mit == m_histos.end()) {
	if (s->stackit()) {
	  h->SetFillColor(s->colorcode());
	  h->SetLineColor(s->colorcode());
	  h->SetLineWidth(0);
	}
	m_histos[s->name()] = h;
      } else {
	mit->second->Add(h);
      }
    }

    //============================================================
    // COUNT EVENTS, RENORM TO DATA, CONSTRUCT THE TSTACK & LEGEND
    //============================================================

    TH1 *th1data = m_histos["data"];

    assert(th1data);

    double ndata = th1data->Integral();

    if (th1qcd) {
      // QCD = qcdfrac * data, QCD + Sum(MC) = data, ergo...
      //th1qcd->Scale(qcdfrac*totevents/((1-qcdfrac)*th1qcd->Integral()));

      th1qcd->Scale(qcdfrac*ndata/th1qcd->Integral()); // matches previous script

      totevents += th1qcd->Integral();
    }

    double renorm = ndata/totevents;

    cout << "den = " << totevents << endl;
    cout << "data = " << ndata  <<endl;
    cout  << "data/den = " << renorm << endl;


    // Setup the stack and total
    THStack* hs = new THStack("hs","MC contribution");
    TH1D *th1tot = new TH1D("th1tot", "th1tot", pv.NBINS, pv.MINRange, pv.MAXRange);

    // Set up the legend

    float  legX0=0.65, legX1=0.99, legY0=0.4, legY1=0.88;
    // float  legX0=0.35, legX1=0.85, legY0=0.4, legY1=0.88;
    // float  legX0=0.18, legX1=0.52, legY0=0.4, legY1=0.88;
    TLegend * Leg = new TLegend( legX0, legY0, legX1, legY1);
    Leg->SetFillColor(0);
    Leg->SetFillStyle(0);
    Leg->SetTextSize(0.04);

    if (TString(cuttablefilename).Contains("Mu"))
      Leg->AddEntry(th1data,  "Muon Data",  "PLE");
    else
      Leg->AddEntry(th1data,  "Electron Data",  "PLE");

    vector<double> binErrSQ(pv.NBINS,0.);

    vector<pair<TString, TH1 *> > v_legentries;

    for (size_t isamp=1; isamp<samples.size(); isamp++) {
      Sample *s = samples[isamp];
      if (m_stacked[s->name()]) continue;

      map<TString, TH1 *>::iterator mit = m_histos.find(s->name());
      TH1 *h = mit->second;
      h->Scale(renorm);

      cout << s->name() << " = " << h->Integral() << endl;

      if(s->stackit()) {
	hs->Add(h);
	th1tot->Add(h);
	m_stacked[s->name()] = true;
	v_legentries.push_back(*mit);
	for (int ibin=1; ibin <= pv.NBINS; ibin++)
	  binErrSQ[ibin-1] += h->GetBinError(ibin)*h->GetBinError(ibin);
      }
    }

    // Reverse the order for the legend
    for (vector<pair<TString, TH1 *> >::reverse_iterator
	   rit = v_legentries.rbegin();
	 rit != v_legentries.rend();
	 rit++)
      Leg->AddEntry(rit->second, rit->first, "F");

    TH1D* th1totClone = ( TH1D*) th1tot->Clone("th1totClone");
    th1totClone->SetMarkerStyle(0);
    th1totClone->SetFillStyle(3003);
    th1totClone->SetFillColor(11);
    th1totClone->SetLineColor(0);

    for(int ibin=1; ibin<=th1totClone->GetNbinsX(); ++ibin) {
      th1totClone->SetBinError(ibin, sqrt(binErrSQ[ibin-1]));
    }

    //============================================================
    // SETUP THE CANVAS
    //============================================================

//    gROOT->ProcessLine(".L tdrstyle.C");
    setTDRStyle();
    tdrStyle->SetErrorX(0.5);
    tdrStyle->SetPadRightMargin(0.05);

    tdrStyle->SetLegendBorderSize(0);

    TCanvas* c1 = new TCanvas(pv.plotvar,pv.plotvar,10,10, 800, 800);
    TPad *d1, *d2;

    c1->Divide(1,2,0,0);
    d1 = (TPad*)c1->GetPad(1);
    d1->SetPad(0.01,0.30,0.95,0.99);
    d2 = (TPad*)c1->GetPad(2);
    d2->SetPad(0.01,0.02,0.95,0.30);

    // Compose the stack

    d1->cd();
    gPad->SetBottomMargin(0.0);
    gPad->SetTopMargin(0.1);
    gPad->SetRightMargin(0.05);
    gPad->SetLeftMargin(0.14);

    Leg->AddEntry(th1tot,  "MC Uncertainty",  "f");

    Leg->SetFillColor(0);

    TH1* th1totempty = new TH1D("th1totempty", "th1totempty", pv.ANBINS, pv.AMINRange, pv.AMAXRange);
    th1data->SetMarkerStyle(20);
    th1data->SetMarkerSize(1.25);
    th1data->SetLineWidth(2);

    th1tot->SetFillStyle(3001);
    th1tot->SetFillColor(1);
    th1tot->SetLineColor(1);
    th1tot->SetMarkerStyle(0);

    char tmpc[100];    sprintf(tmpc,"Events / %.1f GeV",BINWIDTH);
    if (pv.slog==1)    sprintf(tmpc,"Events/ %.1f",BINWIDTH);
    if (pv.slog==2)    sprintf(tmpc,"Events/ %.2f",BINWIDTH);
    if (pv.slog==3)    sprintf(tmpc,"Events/ %.0f GeV",BINWIDTH);
    if (pv.slog==6)    sprintf(tmpc,"Events/ %.1f rad",BINWIDTH);
    th1totempty->SetYTitle(tmpc);
    //  th1totempty->GetYaxis()->SetTitleSize(0.1);
    th1totempty->GetYaxis()->SetTitleOffset(1.2);
    th1totempty->GetYaxis()->SetLabelOffset(0.01);
    //  th1totempty->GetYaxis()->CenterTitle(true);
    th1totempty->GetYaxis()->SetLabelSize(0.04);
    // th1totClone->Draw("e3");   

    th1tot->SetMinimum(0.01);
    int maxbin = th1data->GetMaximumBin();
    float maxval = th1data->GetBinContent(maxbin);
    cout << "maxval " <<maxval <<endl;
//    th1totempty->SetMaximum(2.5*maxval);
    th1totempty->SetMaximum(1.6*maxval);
    th1totempty->SetMinimum(0.01);
    if(pv.slog==1) th1totempty->SetMaximum(1.6*maxval);
    th1data->SetMinimum(0.01);

    // Draw it all

    th1totempty->Draw();
    //th1tot->Draw("e2same");
    th1data->Draw("esame");
    hs->Draw("samehist");

    th1tot->Draw("e2same");

    th1data->Draw("esame");
    cmspre(intLUMIinvpb/1000.0);    
    if (pv.drawleg ==1)  Leg->Draw();  
    // th1data->Draw("Axissame");
    gPad->RedrawAxis();
    d2->cd();

    TH1F    * hhratio    = (TH1F*) th1data->Clone("hhratio")  ;
    hhratio->Sumw2();
    hhratio->SetStats(0);

    gPad->SetLeftMargin(0.14);
    gPad->SetTopMargin(0);
    gPad->SetRightMargin(0.05);
    gPad->SetFrameBorderSize(0);
    gPad->SetBottomMargin(0.3);
    gPad->SetTickx();

    hhratio->SetMarkerSize(1.25);
    //  hhratio->GetYaxis()->SetRangeUser(0.48,1.52);
    hhratio->GetYaxis()->SetRangeUser(0.3,1.7);
    hhratio->GetXaxis()->SetTitle(pv.xlabel);
    hhratio->GetXaxis()->SetTitleOffset(0.9);
    hhratio->GetXaxis()->SetTitleSize(0.15);
    hhratio->GetXaxis()->SetLabelSize(0.15);
    hhratio->GetYaxis()->SetTitleSize(0.1);
    hhratio->GetYaxis()->SetTitleOffset(0.5);
    hhratio->GetYaxis()->CenterTitle(true);
    hhratio->GetYaxis()->SetLabelSize(0.1);
    cout << hhratio->GetNbinsX() << endl;
    cout << th1tot->GetNbinsX() << endl;
    hhratio->Divide(th1tot);
    double binError(0.0), mcbinentry(0.0), mcerror(0.0);
    for(int i=0; i<hhratio->GetNbinsX(); ++i) {
      binError = hhratio->GetBinError(i);
      mcerror = th1tot->GetBinError(i);
      mcbinentry = th1tot->GetBinContent(i);
      if(mcbinentry>0.) mcerror /= mcbinentry;
      else mcerror = 0.0;
      binError = sqrt(binError*binError + mcerror*mcerror);
      hhratio->SetBinError(i, binError);
    }
    TH1D *th1emptyclone = new TH1D("th1emptyclone", "th1emptyclone", pv.ANBINS, pv.AMINRange, pv.AMAXRange);
    th1emptyclone->GetYaxis()->SetRangeUser(0.6,1.3999);
    th1emptyclone->GetXaxis()->SetTitle(pv.xlabel);
    th1emptyclone->GetXaxis()->SetTitleOffset(0.9);
    th1emptyclone->GetXaxis()->SetTitleSize(0.15);
    th1emptyclone->GetXaxis()->SetLabelSize(0.15);
    th1emptyclone->SetYTitle("Ratio Data/MC");
    th1emptyclone->GetYaxis()->SetTitleSize(0.1);
    th1emptyclone->GetXaxis()->SetNdivisions(505);
    th1emptyclone->GetYaxis()->SetNdivisions(505);
    th1emptyclone->GetYaxis()->SetTitleOffset(0.5);
    th1emptyclone->GetYaxis()->CenterTitle(true);
    th1emptyclone->GetYaxis()->SetLabelSize(0.1);
    th1emptyclone->Draw();

    TBox *errbox = new TBox(pv.AMINRange,0.974,pv.AMAXRange,1.026); // lumi systematic uncertainty
    errbox->SetFillColor(kGray);
    errbox->Draw();

#if 0
 	TF1 *f1 = new TF1("f1", "pol1",  pv.AMINRange, pv.AMAXRange);
	//f1->SetParameters(1.0,0.0);
       // f1->SetParameters(1,0.0);
	f1->FixParameter(0,1);
	f1->FixParameter(1,0);
	//cout<<" par1   "f1->GetParameter(0)<<endl;
	//cout<<" par2   "f1->GetParameter(1)<<endl;

        TFitResultPtr r =  hhratio->Fit("f1", "RBS");
	//TFitResultPtr r = hhratio->Fit(myFunc,"S");
       	    r->Print("V");     // print full information of fit including covariance matrix
#endif

    hhratio->Draw("esame");
    TLine *line; line = new TLine(pv.AMINRange,1.0,pv.AMAXRange,1.0);
    line->SetLineStyle(1);
    line->SetLineWidth(1);
    line->SetLineColor(1);
    line->Draw();

    TString outfile = TString("OutDir/")+TString(gSystem->BaseName(cuttablefilename)).ReplaceAll(".txt","")+TString("_")+pv.outfile;

    c1->Print(outfile+".png");
    c1->Print(outfile+".C");
    //gPad->WaitPrimitive();
    c1->Modified();
    c1->Update();
    c1->SaveAs(outfile+".pdf"); 

  } // var loop

  f.Write();

}                                                                // myControlPlots