void writeDataBackgroundHistosForModel(const std::map<TString,TGraph*>& m_bkgds,
				       const std::vector<TH1D *>& vchans,
				       TFile  *allHistFile)
{
  for (std::map<TString,TGraph*>::const_iterator it = m_bkgds.begin();
       it != m_bkgds.end();
       it++) {
    const TString& name = it->first;

    // determine binning from the signal histogram for this channel
    // - (sigh) have to find it first...
    //
    TString channame = name.Copy().Remove(0,strlen("Bckgrndtot_"));
    TH1D *sigh=(TH1D*)0;
    for (int ichan=0; ichan<NUMCHAN; ichan++) {
      sigh = vchans.at(ichan);
      if (strstr(sigh->GetName(),channame.Data()))
	break;
    }
    assert (sigh);


    // for variable binning - all histos must have the same binning per channel
    TAxis *xax = sigh->GetXaxis();

    TVectorD xbins   = TVectorD(sigh->GetNbinsX(),xax->GetXbins()->GetArray());

    int lobin = xax->FindFixBin(sumwinmin);
    int hibin = xax->FindFixBin(sumwinmax)-1;
    int nbins = hibin-lobin+1;

    TVectorD xwindow = xbins.GetSub(lobin-1,hibin);
    
    printf("Booking TH1D(%s,%s,%d,xwindowarray)\n",
	   name.Data(),name.Data(),nbins);
    TH1D *h = new TH1D(name.Data(),name.Data(),nbins,xwindow.GetMatrixArray());

    for (int ibin=1; ibin <= nbins; ibin++)
      h->SetBinContent(ibin,
		       it->second->Eval(h->GetBinCenter(ibin))
		       * h->GetBinWidth(ibin)
		       );

    allHistFile->WriteTObject(h);
  }

}                                            //  writeDataBackgroundHistosForModel
void writeSignalHistosForModel(std::vector<TH1D *>& vsd,
			       const TString& sigmodel,
			       TFile *allHistFile)
{
  for (int ichan=0; ichan<NUMCHAN; ichan++) {
    TH1D * sdh = vsd[ichan];

    // Find limit window from gaussian fit to signal peak.
    //
    double wid  = sdh->GetRMS();
    double mean = sdh->GetMean();

    //TCanvas *c1 = new TCanvas(s,s,300,300);
    TFitResultPtr r = sdh->Fit("gaus","QNS","",mean-2.5*wid,mean+2.5*wid);

    cout<<" mean= "<<mean<<", RMS= "<<wid<<", Fit sigma= "<<r->Parameter(2)<<endl;
    //cout<<r->Parameter(0)<<" "<<r->Parameter(1)<<" "<<r->Parameter(2)<<endl;

    TAxis *xax = sdh->GetXaxis();

#if 0
    int lobin = xax->FindFixBin(r->Parameter(1)-2*r->Parameter(2));
    int hibin = xax->FindFixBin(r->Parameter(1)+2*r->Parameter(2));
    sd.sumwinmin = xax->GetBinLowEdge(lobin);
    sd.sumwinmax = xax->GetBinUpEdge(hibin);
#elif 0
    int lobin = xax->FindFixBin(140);   sd.sumwinmin=140; // 1 bin  left,
    int hibin = xax->FindFixBin(170)-1; sd.sumwinmax=170; // 2 bins right
#else
    int lobin = xax->FindFixBin(sumwinmin);
    int hibin = xax->FindFixBin(sumwinmax)-1;
#endif
    int nbins = hibin-lobin+1;

    // for variable binning - all histos must have the same binning per channel
    TVectorD xbins   = TVectorD(sdh->GetNbinsX(),sdh->GetXaxis()->GetXbins()->GetArray());
    TVectorD xwindow = xbins.GetSub(lobin-1,hibin);

    xax->SetRange(lobin,hibin);

    // Copy contents to window-restricted signal histogram
    // and write to output file.
    //
    TString name = Form("Signal%s_%s",sigmodel.Data(),channames[ichan]);
    printf("Booking TH1D(%s,%s,%d,xwindowarray)\n",
	   name.Data(),sdh->GetTitle(),nbins);
    TH1D *signm = new TH1D(name.Data(),
			   sdh->GetTitle(),
			   nbins,
			   xwindow.GetMatrixArray());

    // make copies of the histograms that are restricted to the
    // bin range lobin-hibin
    //
    for (int ibin=lobin; ibin<=hibin; ibin++) 
      signm->SetBinContent((ibin-lobin+1),
			   sdh->GetBinContent(ibin)
			   *sdh->GetBinWidth(ibin)
			   );

    if (!sigmodel.CompareTo("wh"))
      signm->Scale(whsigscaleto);
    else
      signm->Scale(sigscaleto);

    allHistFile->WriteTObject(signm);

  } // channel loop
}                                                     // writeSignalHistosForModel