Пример #1
0
char* initialize ( char* cmdline )
{
	// Get comnmand line
	std::string str = cmdline;
	std::vector<std::string>	args;
	while ( str.length() > 0) {
		args.push_back ( strSplit ( str, " " ) );		
	}
	fileName = "";
	for (int n=0; n < args.size(); n++ ) {
		if ( args[n].compare ( "-f" ) == 0 ) {
			startFrame = strToI ( args[n+1] );
		}
		if ( args[n].compare ( "-d" ) == 0 ) {		// max_draw
			maxDraw = strToI ( args[n+1] );
		}
		if ( args[n].find_first_of ( "." ) != std::string::npos ) {
			fileName = args[n];
		}
	}
	if ( fileName.length()== 0 || args.size()==0 ) {
		app_printf ( "USAGE:  state_view [-f #] [-d #] filename.raw\n\n" );
		app_printf ( "  -f #   Start at frame \n" );
		app_printf ( "  -d #   Maximum number of draw calls to read \n" );
		_getch();
		exit(-1);
	}

	for (int n=0; n < 10; n++ ) bShow[n] = true;

	// Initialize camera
	cam.setPos ( 0, 0, 1 );
	cam.setToPos ( -window_width*3/8, -window_height*3/8, 1 );
	cam.updateMatricies ();

	// Initialize bin and call names
	setup_bins ();

	// Load trace file	
	char fname[256];
	strcpy ( fname, fileName.c_str() );
	load_trace_raw ( fname );	

	// required init functions
	init2D ( "arial_12" );		// specify font file (.bin/tga)
	setText ( 1.0, -0.5 );		// scale by 0.5, kerning adjust -0.5 pixels
	setview2D ( window_width, window_height );

	// draw visualization layer
	drawMatrix ();
	
	PERF_INIT ( false );						// Enable CPU perf (do not require nv-perfmarker DLL)
	PERF_SET ( false, 2, false, "" );		// Perf CPU?, CPU level, NV Perfmarkers, Log filename

	return "NVIDIA State Viewer, by Rama Hoetzlein";	// Return Window title
}
   void make_hadtau_input_files1( const char* input_root_file  = "non-qcd-inputs-topup2/ARElog60_12.9ifb_HadTauEstimation_data_formatted_New.root",
                                  const char* output_text_file = "outputfiles/combine-input-hadtau.txt",
                                  const char* nbsum_text_file  = "outputfiles/nbsum-input-hadtau.txt"
                               ) {

      setup_bins();

      gDirectory -> Delete( "h*" ) ;

      TFile* tf_input = new TFile( input_root_file, "read" ) ;
      if ( tf_input == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }
      if ( !(tf_input -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }

      printf("\n") ;
      tf_input -> ls() ;
      printf("\n") ;

      FILE* ofp_combine ;
      if ( (ofp_combine = fopen( output_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine output file: %s\n\n", output_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum ;
      if ( (ofp_nbsum = fopen( nbsum_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum output file: %s\n\n", nbsum_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum_stat_syst ;
      TString systfile_nbsum( nbsum_text_file ) ;
      systfile_nbsum.ReplaceAll("input","stat-syst") ;
      if ( (ofp_nbsum_stat_syst = fopen( systfile_nbsum.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum stat-syst output file: %s\n\n", systfile_nbsum.Data() ) ;
         return ;
      }

      FILE* ofp_combine_stat_syst ;
      TString systfile_combine( output_text_file ) ;
      systfile_combine.ReplaceAll("input","stat-syst") ;
      if ( (ofp_combine_stat_syst = fopen( systfile_combine.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine stat-syst output file: %s\n\n", systfile_combine.Data() ) ;
         return ;
      }


      ///////TH1F* h_ldp = (TH1F*) tf -> Get( "QCDBin_LowDphi_nominal_fullstatuncertainty" ) ;
      TH1* h_pred_lowdphi = get_hist( tf_input, "QCDBin_LowDphi_nominal_fullstatuncertainty" ) ;

      /////////TH1F* h_hdp = (TH1F*) tf -> Get( "QCDBin_HiDphi_nominal_fullstatuncertainty" ) ;
      TH1* h_pred_highdphi = get_hist( tf_input, "QCDBin_HiDphi_nominal_fullstatuncertainty" ) ;





      TH1* h_systerr_lowdphi[100] ;
      TH1* h_systerr_highdphi[100] ;
      char systerr_name[100][100] ;
      int n_systerr(0) ;

      {
         int si(0) ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_closureUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_closureUncertainty" ) ;
         sprintf( systerr_name[si], "Closure" ) ;
         si++ ;

     //----------

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_BMistagUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_BMistagUp" ) ;
         sprintf( systerr_name[si], "Btag" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuRecoSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuRecoSysUp" ) ;
         sprintf( systerr_name[si], "MuRecoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuIsoSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuIsoSysUp" ) ;
         sprintf( systerr_name[si], "MuIsoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuRecoIsoUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuRecoIsoUp" ) ;
         sprintf( systerr_name[si], "MuRecoIso" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_JECSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_JECSysUp" ) ;
         sprintf( systerr_name[si], "JEC" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MTSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MTSysUp" ) ;
         sprintf( systerr_name[si], "MT" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MtEffStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MtEffStat" ) ;
         sprintf( systerr_name[si], "MtEffStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_IsoTrkVetoEffUncertaintySys" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_IsoTrkVetoEffUncertaintySys" ) ;
         sprintf( systerr_name[si], "IsoTrkVetoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccStat" ) ;
         sprintf( systerr_name[si], "AccStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccSysPDFUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccSysPDFUp" ) ;
         sprintf( systerr_name[si], "AccSysPDF" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccSysScaleUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccSysScaleUp" ) ;
         sprintf( systerr_name[si], "AccSysScale" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuFromTauStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuFromTauStat" ) ;
         sprintf( systerr_name[si], "MuFromTauStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_DileptonUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_DileptonUncertainty" ) ;
         sprintf( systerr_name[si], "Dilepton" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_TrigEffUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_TrigEffUncertainty" ) ;
         sprintf( systerr_name[si], "TrigEff" ) ;
         si++ ;

         n_systerr = si ;
      }




      int bi_hist(0) ;
      int bi_control(0) ;
      int bi_search(0) ;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               if ( bin_edges_nj[bi_nj-1] < 2.5 ) continue;// because lost lepton input file lacks NJets = 2 bin
               bi_hist ++ ;

               double ldp_val = h_pred_lowdphi -> GetBinContent( bi_hist ) ;
               double ldp_hist_err = h_pred_lowdphi -> GetBinError( bi_hist ) ;
               double ldp_err = 1. ;
               if ( ldp_val > 0 ) ldp_err = ldp_hist_err ;

               double hdp_val = h_pred_highdphi -> GetBinContent( bi_hist ) ;
               double hdp_hist_err = h_pred_highdphi -> GetBinError( bi_hist ) ;
               double hdp_err = 1. ;
               if ( hdp_val > 0 ) hdp_err = hdp_hist_err ;


               double total_syst2_lowdphi_events(0.) ;
               double total_syst2_highdphi_events(0.) ;
               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi = fabs( h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  double syst_highdphi = fabs( h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst2_lowdphi_events  += pow( syst_lowdphi  * ldp_val, 2. ) ;
                     total_syst2_highdphi_events += pow( syst_highdphi * hdp_val, 2. ) ;
                  }
               } // si

               double ldp_syst = sqrt( total_syst2_lowdphi_events ) ;
               double hdp_syst = sqrt( total_syst2_highdphi_events ) ;

               TString hist_bin_label( h_pred_lowdphi -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               int bi_ht = 0, bi_mht = 0;

               if ( bi_htmht == 1 ) { bi_ht = 1; bi_mht = 1; }
               if ( bi_htmht == 2 ) { bi_ht = 2; bi_mht = 1; }
               if ( bi_htmht == 3 ) { bi_ht = 3; bi_mht = 1; }

               if ( bi_htmht == 4 ) { bi_ht = 1; bi_mht = 2; }
               if ( bi_htmht == 5 ) { bi_ht = 2; bi_mht = 2; }
               if ( bi_htmht == 6 ) { bi_ht = 3; bi_mht = 2; }

               if ( bi_htmht == 7 ) { bi_ht = 1; bi_mht = 3; }
               if ( bi_htmht == 8 ) { bi_ht = 2; bi_mht = 3; }
               if ( bi_htmht == 9 ) { bi_ht = 3; bi_mht = 3; }

               if ( bi_htmht ==10 ) { bi_ht = 2; bi_mht = 4; }
               if ( bi_htmht ==11 ) { bi_ht = 3; bi_mht = 4; }

               if ( bi_htmht ==12 ) { bi_ht = 2; bi_mht = 5; }
               if ( bi_htmht ==13 ) { bi_ht = 3; bi_mht = 5; }

               if ( bi_mht == 1 ) {
                  bi_control ++ ;
               } else {
                  bi_search ++ ;
               }

               char mhtchar[10] ;
               if ( bi_mht == 1 ) {
                  sprintf( mhtchar, "C" ) ;
               } else {
                  sprintf( mhtchar, "%d", bi_mht-1 ) ;
               }

               char label[1000] ;
               sprintf( label, " %3d  %s %3d  Nj%d-Nb%d-MHT%s-HT%d",
                   bi_hist, (bi_mht==1)?"C":"S", (bi_mht==1)?bi_control:bi_search,
                   bi_nj, bi_nb-1, mhtchar, bi_ht ) ;

               //// printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;

               double ldp_stat_over_sqrtn(0.8), ldp_syst_over_n(0.15) ;
               if ( ldp_val > 0 ) {
                  ldp_stat_over_sqrtn = ldp_err / sqrt( ldp_val ) ;
                  ldp_syst_over_n = ldp_syst / ldp_val ;
                  if ( ldp_stat_over_sqrtn > 1.5 ) ldp_stat_over_sqrtn = 1.0 ;
               }
               double hdp_stat_over_sqrtn(0.8), hdp_syst_over_n(0.15) ;
               if ( hdp_val > 0 ) {
                  hdp_stat_over_sqrtn = hdp_err / sqrt( hdp_val ) ;
                  hdp_syst_over_n = hdp_syst / hdp_val ;
                  if ( hdp_stat_over_sqrtn > 1.5 ) hdp_stat_over_sqrtn = 1.0 ;
               }

               printf(               "%s      %8.1f +/- %5.1f +/- %5.1f          %8.1f +/- %5.1f +/- %5.1f\n",
                   label,
                   ldp_val, ldp_err, ldp_syst,
                   hdp_val, hdp_err, hdp_syst ) ;

               fprintf( ofp_combine, "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, ldp_err, ldp_syst,   hdp_val, hdp_err, hdp_syst ) ;

               if ( bi_htmht > 3 ) {
                  char this_label[100] ;
                  sprintf( this_label, "%3d S-Nj%d-Nb%d-MHT%d-HT%d (S%d)", bi_search, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht-3 ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               } else {
                  char this_label[100] ;
                  sprintf( this_label, "%3d C-Nj%d-Nb%d-MHT%d-HT%d (C%d)", bi_control, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               }

            } // bi_htmht
         } // bi_nb
      } // bi_nj

      fclose( ofp_combine ) ;
      printf("\n\n Wrote %s\n\n", output_text_file ) ;

     //-----

      for ( int bi_ht=1; bi_ht<=nBinsHT; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

            if ( bin_edges_nj[bi_nj-1] < 2.5 ) continue; // because znuznu input file lacks NJets = 2 bin

            float ldp_nbsum_val(0.) ;
            float hdp_nbsum_val(0.) ;

            float ldp_nbsum_stat_err2(0.) ;
            float hdp_nbsum_stat_err2(0.) ;

            double total_syst_lowdphi_events[100] ;
            double total_syst_highdphi_events[100] ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_lowdphi_events[si]  = 0 ;
               total_syst_highdphi_events[si] = 0 ;
            }

            for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {

               bi_hist = (bi_nj-1)*(nb_nb)*(nb_htmht) + (bi_nb-1)*(nb_htmht) + bi_ht ;

               double ldp_val = h_pred_lowdphi -> GetBinContent( bi_hist ) ;
               double ldp_hist_err = h_pred_lowdphi -> GetBinError( bi_hist ) ;
               double ldp_err = 1. ;
               if ( ldp_val > 0 ) ldp_err = ldp_hist_err ;

               double hdp_val = h_pred_highdphi -> GetBinContent( bi_hist ) ;
               double hdp_hist_err = h_pred_highdphi -> GetBinError( bi_hist ) ;
               double hdp_err = 1. ;
               if ( hdp_val > 0 ) hdp_err = hdp_hist_err ;


               //////////////double ldp_syst = 0.10 * ldp_val ; //*** flat 10% syst for now.
               //////////////double hdp_syst = 0.10 * hdp_val ; //*** flat 10% syst for now.

               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi = fabs( h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  double syst_highdphi = fabs( h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst_lowdphi_events[si]  += syst_lowdphi  * ldp_val ;
                     total_syst_highdphi_events[si] += syst_highdphi * hdp_val ;
                  }
               } // si

               ldp_nbsum_val += ldp_val ;
               hdp_nbsum_val += hdp_val ;

               ldp_nbsum_stat_err2 += pow( ldp_err, 2. ) ;
               hdp_nbsum_stat_err2 += pow( hdp_err, 2. ) ;

               TString hist_bin_label( h_pred_lowdphi -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               char label[1000] ;
               sprintf( label, " %3d  Nj%d-Nb%d-MHTC-HT%d", bi_hist, bi_nj, bi_nb-1, bi_ht ) ;

            } // bi_nb

            ///printf("    Systematics:\n") ;
            double total_syst_err2_lowdphi(0.) ;
            double total_syst_err2_highdphi(0.) ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_err2_lowdphi += pow( total_syst_lowdphi_events[si], 2.)   ;
               total_syst_err2_highdphi += pow( total_syst_highdphi_events[si], 2.)  ;
               ///printf("  %2d : %25s :   %7.1f  %7.1f\n", si, systerr_name[si], total_syst_lowdphi_events[si], total_syst_highdphi_events[si] ) ;
            }
            double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;
            double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;


            float ldp_nbsum_stat_err = sqrt( ldp_nbsum_stat_err2 ) ;
            float hdp_nbsum_stat_err = sqrt( hdp_nbsum_stat_err2 ) ;

            double ldp_syst_over_n(0.) ;
            double ldp_stat_over_sqrtn(0.) ;
            if ( ldp_nbsum_val > 0 ) ldp_syst_over_n = total_syst_err_lowdphi / ldp_nbsum_val  ;
            if ( ldp_nbsum_val > 0 ) ldp_stat_over_sqrtn = ldp_nbsum_stat_err / sqrt(ldp_nbsum_val)  ;

            double hdp_syst_over_n(0.) ;
            double hdp_stat_over_sqrtn(0.) ;
            if ( hdp_nbsum_val > 0 ) hdp_syst_over_n = total_syst_err_highdphi / hdp_nbsum_val  ;
            if ( hdp_nbsum_val > 0 ) hdp_stat_over_sqrtn = hdp_nbsum_stat_err / sqrt(hdp_nbsum_val)  ;

            printf(          "   Nj%d-HT%d      %8.1f +/- %5.1f +/- %5.1f  (%6.3f)        %8.1f +/- %5.1f +/- %5.1f   (%6.3f)\n",
                bi_nj, bi_ht,
                ldp_nbsum_val, ldp_nbsum_stat_err, total_syst_err_lowdphi,    ldp_syst_over_n,
                hdp_nbsum_val, hdp_nbsum_stat_err, total_syst_err_highdphi,   hdp_syst_over_n ) ;

            fprintf( ofp_nbsum, "   Nj%d-HT%d   %8.1f +/- %5.1f +/- %5.1f    %8.1f +/- %5.1f +/- %5.1f\n", bi_nj, bi_ht,
                  ldp_nbsum_val, ldp_nbsum_stat_err, total_syst_err_lowdphi,
                  hdp_nbsum_val, hdp_nbsum_stat_err, total_syst_err_highdphi   ) ;
            fprintf( ofp_nbsum_stat_syst, "   Nj%d-HT%d    %6.3f  %6.3f       %6.3f  %6.3f\n", bi_nj, bi_ht, 
                  ldp_stat_over_sqrtn, ldp_syst_over_n,    hdp_stat_over_sqrtn, hdp_syst_over_n ) ;

         } // bi_nj
      } // bi_ht

      fclose( ofp_nbsum ) ;
      fclose( ofp_nbsum_stat_syst ) ;
      printf("\n\n Wrote %s\n\n", nbsum_text_file ) ;
      printf("\n\n Wrote %s\n\n", systfile_nbsum.Data() ) ;
      printf("\n\n Wrote %s\n\n", systfile_combine.Data() ) ;



   } // make_hadtau_input_files1
Пример #3
0
   void draw_qcd_ratio_v3( const char* infile = "outputfiles/hists-v2d-qcd.root", const char* outputdir = "outputfiles/" ) {
     
      setup_bins();
      TLine* line0 = new TLine() ;
      line0 -> SetLineColor(4) ;
      TString tstring ;
      TLine* rline = new TLine() ;
      rline -> SetLineColor(2) ;

      double ratio_max = 1.0 ;

      char command[10000] ;
      sprintf( command, "mkdir -p %s", outputdir ) ;
      gSystem -> Exec( command ) ;


      gStyle -> SetOptStat(0) ;

      gDirectory -> Delete( "h*" ) ;

      loadHist( infile ) ;

      char hname[1000] ;
      char label[100] ;

      TH1F* h_ldp = get_hist( "h_ldp" ) ;
      TH1F* h_hdp = get_hist( "h_hdp" ) ;
      TH1F* h_max_ldp_weight = get_hist( "h_max_ldp_weight" ) ;

      TH1F* h_ratio = new TH1F( "h_ratio", "QCD H/L ratio", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;
      TH1F* h_max_ldp_weight_search_bins = new TH1F( "h_max_ldp_weight_search_bins", "max LDP weight", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;
      TH1F* h_ldp_search_bins = new TH1F( "h_ldp_search_bins", "LDP counts, search bins", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;
      TH1F* h_hdp_search_bins = new TH1F( "h_hdp_search_bins", "HDP counts, search bins", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;

      TH1F * h_ratio_nb[10], * h_ratio_nj[10];

      for ( int nb_count = 0; nb_count < nb_nb; nb_count++)
      {

      TString nb_str; nb_str.Form("%d",nb_count);
      h_ratio_nb[nb_count] = new TH1F( "h_ratio_nb"+nb_str, "QCD H/L ratio, Nb"+nb_str, no_bin_bjet[nb_count], 0.5, no_bin_bjet[nb_count]+0.5 ) ;

      }

      for ( int nj_count = 1; nj_count <= nb_nj; nj_count++)
      {

      TString nj_str; nj_str.Form("%d",nj_count);
      h_ratio_nj[nj_count] = new TH1F( "h_ratio_nj"+nj_str, "QCD H/L ratio, Nj"+nj_str, no_bin_njet[nj_count-1], 0.5, no_bin_njet[nj_count-1]+0.5 ) ;

      }

      int bi_hist(0) ;
      int bi_search_hist(0) ;
      int bi_nb_hist[10] = {};
      int bi_nj_hist[10] = {};
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               bi_hist++ ;  // this line should be reviewd when updating the code that produces h_ldp and h_hdp

               if ( is_this_bin_excluded(bi_nj-1, bi_nb-1, bi_htmht-1) ) continue;
               sprintf( label, "%s", h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;
               //printf( " %3d : Nj%d Nb%d HTMHT%d : %30s\n", bi_hist, bi_nj, bi_nb-1, bi_htmht, label ) ;
               if ( bi_htmht > 3 ) {
                  bi_search_hist ++ ;
                  double ldp_val = h_ldp -> GetBinContent( bi_hist ) ;
                  double ldp_err = h_ldp -> GetBinError( bi_hist ) ;
                  double hdp_val = h_hdp -> GetBinContent( bi_hist ) ;
                  double hdp_err = h_hdp -> GetBinError( bi_hist ) ;
                  double ratio_val(0.) ;
                  double ratio_err(0.) ;
                  if ( ldp_val > 0. && hdp_val > 0. ) {
                     ratio_val = hdp_val / ldp_val ;
                     ratio_err = ratio_val * sqrt( pow( ldp_err/ldp_val, 2. ) + pow( hdp_err/hdp_val, 2. ) ) ;
                  }
                  h_ratio -> SetBinContent( bi_search_hist, ratio_val ) ;
                  h_ratio -> SetBinError( bi_search_hist, ratio_err ) ;
                  h_ratio -> GetXaxis() -> SetBinLabel( bi_search_hist, label ) ;
                  h_max_ldp_weight_search_bins -> SetBinContent( bi_search_hist, h_max_ldp_weight->GetBinContent( bi_hist ) ) ;
                  h_max_ldp_weight_search_bins -> GetXaxis() -> SetBinLabel( bi_search_hist, label ) ;
                  h_ldp_search_bins -> SetBinContent( bi_search_hist, ldp_val ) ;
                  h_ldp_search_bins -> SetBinError( bi_search_hist, ldp_err ) ;
                  h_ldp_search_bins -> GetXaxis() -> SetBinLabel( bi_search_hist, label ) ;
                  h_hdp_search_bins -> SetBinContent( bi_search_hist, hdp_val ) ;
                  h_hdp_search_bins -> SetBinError( bi_search_hist, hdp_err ) ;
                  h_hdp_search_bins -> GetXaxis() -> SetBinLabel( bi_search_hist, label ) ;
                  printf( "  search %3d : %30s : R= %6.4f +/- %6.4f\n", bi_search_hist, label, ratio_val, ratio_err ) ;
                  TH1F* hp_nb(0x0) ;
                  hp_nb = h_ratio_nb[bi_nb-1] ;
                  bi_nb_hist[bi_nb-1]++;
                  hp_nb -> SetBinContent( bi_nb_hist[bi_nb-1], ratio_val ) ;
                  hp_nb -> SetBinError( bi_nb_hist[bi_nb-1], ratio_err ) ;
                  hp_nb -> GetXaxis() -> SetBinLabel( bi_nb_hist[bi_nb-1], label ) ;
                  TH1F* hp_nj(0x0) ;
                  hp_nj = h_ratio_nj[bi_nj] ;
                  bi_nj_hist[bi_nj-1]++;
                  hp_nj -> SetBinContent( bi_nj_hist[bi_nj-1], ratio_val ) ;
                  hp_nj -> SetBinError  ( bi_nj_hist[bi_nj-1], ratio_err ) ;
                  hp_nj -> GetXaxis() -> SetBinLabel( bi_nj_hist[bi_nj-1], label ) ;
               } // not control bin.
            } // bi_htmht
         } // bi_nb
      } // bi_nj



      h_ratio -> GetXaxis() -> LabelsOption( "v" ) ;
      h_ratio -> SetMarkerStyle(20) ;

      h_max_ldp_weight_search_bins -> GetXaxis() -> LabelsOption( "v" ) ;
      h_ldp_search_bins -> GetXaxis() -> LabelsOption( "v" ) ;
      h_hdp_search_bins -> GetXaxis() -> LabelsOption( "v" ) ;


      for ( int nb_count = 0; nb_count < nb_nb; nb_count++)
      {
         h_ratio_nb[nb_count] -> GetXaxis() -> LabelsOption( "v" ) ;
         h_ratio_nb[nb_count] -> SetMarkerStyle(20) ;
      }

      for ( int nj_count = 1; nj_count <= nb_nj; nj_count++)
      {
         h_ratio_nj[nj_count] -> GetXaxis() -> LabelsOption( "v" ) ;
         h_ratio_nj[nj_count] -> SetMarkerStyle(20) ;
      }
      h_ratio -> Draw() ;
      gPad -> SetGridy(1) ;

      saveHist("outputfiles/qcdmc-ratio-v3.root","h*") ;

   } // draw_qcd_ratio_v3
   void make_hadtau_lh_input_files1( const char* input_root_file  = "../non-qcd-inputs-fall16c/ARElog98_36.35ifb_HadTauEstimation_data_formatted_V11.root",
                                  const char* output_text_file = "outputfiles/combine-input-hadtau.txt",
                                  const char* nbsum_ldp_text_file  = "outputfiles/nbsum-ldp-input-hadtau.txt",
                                  const char* nbsum_hdp_text_file  = "outputfiles/nbsum-hdp-input-hadtau.txt"
                               ) {

      setup_bins();

      gSystem -> Exec( "mkdir -p outputfiles" ) ;

      gDirectory -> Delete( "h*" ) ;

      TFile* tf_input = new TFile( input_root_file, "read" ) ;
      if ( tf_input == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }
      if ( !(tf_input -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }

      printf("\n") ;
      tf_input -> ls() ;
      printf("\n") ;

      FILE* ofp_combine ;
      if ( (ofp_combine = fopen( output_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine output file: %s\n\n", output_text_file ) ;
         return ;
      }

      FILE* ofp_ldp_nbsum ;
      if ( (ofp_ldp_nbsum = fopen( nbsum_ldp_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening LDP nbsum output file: %s\n\n", nbsum_ldp_text_file ) ;
         return ;
      }

      FILE* ofp_hdp_nbsum ;
      if ( (ofp_hdp_nbsum = fopen( nbsum_hdp_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening HDP nbsum output file: %s\n\n", nbsum_hdp_text_file ) ;
         return ;
      }



      TH1* h_pred_lowdphi = get_hist( tf_input, "QCDBin_LowDphi_nominal" ) ;

      TH1* h_pred_highdphi = get_hist( tf_input, "QCDBin_HiDphi_nominal" ) ;




      TH1* h_systerr_lowdphi[100] ;
      TH1* h_systerr_highdphi[100] ;
      char systerr_name[100][100] ;
      int n_systerr(0) ;

      {
         int si(0) ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_closureUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_closureUncertainty" ) ;
         sprintf( systerr_name[si], "Closure" ) ;
         si++ ;

     //----------

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_BMistagUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_BMistagUp" ) ;
         sprintf( systerr_name[si], "Btag" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuRecoSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuRecoSysUp" ) ;
         sprintf( systerr_name[si], "MuRecoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuIsoSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuIsoSysUp" ) ;
         sprintf( systerr_name[si], "MuIsoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuRecoIsoUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuRecoIsoUp" ) ;
         sprintf( systerr_name[si], "MuRecoIso" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_JECSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_JECSysUp" ) ;
         sprintf( systerr_name[si], "JEC" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MTSysUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MTSysUp" ) ;
         sprintf( systerr_name[si], "MT" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MtEffStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MtEffStat" ) ;
         sprintf( systerr_name[si], "MtEffStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_IsoTrkVetoEffUncertaintySys" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_IsoTrkVetoEffUncertaintySys" ) ;
         sprintf( systerr_name[si], "IsoTrkVetoSys" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccStat" ) ;
         sprintf( systerr_name[si], "AccStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccSysPDFUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccSysPDFUp" ) ;
         sprintf( systerr_name[si], "AccSysPDF" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_AccSysScaleUp" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_AccSysScaleUp" ) ;
         sprintf( systerr_name[si], "AccSysScale" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_MuFromTauStat" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_MuFromTauStat" ) ;
         sprintf( systerr_name[si], "MuFromTauStat" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_DileptonUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_DileptonUncertainty" ) ;
         sprintf( systerr_name[si], "Dilepton" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_input, "QCDBin_LowDphi_TrigEffUncertainty" ) ;
         h_systerr_highdphi[si] = get_hist( tf_input, "QCDBin_HiDphi_TrigEffUncertainty" ) ;
         sprintf( systerr_name[si], "TrigEff" ) ;
         si++ ;

         n_systerr = si ;
      }




      int bi_hist(0) ;
      int bi_control(0) ;
      int bi_search(0) ;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               int bi_hist = hadtau_global_bin_with_mhtc( bi_nj, bi_nb, bi_htmht ) ;
               if ( bi_hist > 0 ) {
                  int hbl_nj, hbl_nb, hbl_mht, hbl_htmht ;
                  sscanf( h_pred_lowdphi->GetXaxis()->GetBinLabel( bi_hist ), "NJets%d_BTags%d_MHT%d_HT%d", &hbl_nj, &hbl_nb, &hbl_mht, &hbl_htmht ) ;
                  if ( bi_nj != (hbl_nj+1) ) { printf("\n\n*** Inconsistent Njets.  %d != %d\n", bi_nj, hbl_nj+1) ; gSystem -> Exit(-1) ; }
                  if ( bi_nb != (hbl_nb+1) ) { printf("\n\n*** Inconsistent Nb. %d != %d\n", bi_nb, hbl_nb+1 ) ; gSystem -> Exit(-1) ; }
               }

               double ldp_val(0.) ;
               double ldp_hist_err(0.) ;
               if ( bi_hist > 0 ) {
                  ldp_val = h_pred_lowdphi -> GetBinContent( bi_hist ) ;
                  ldp_hist_err = h_pred_lowdphi -> GetBinError( bi_hist ) ;
               }
               double ldp_err = 1. ;
               if ( ldp_val > 0 ) ldp_err = ldp_hist_err ;

               double hdp_val(0.) ;
               double hdp_hist_err(0.) ;
               if ( bi_hist > 0 ) {
                  hdp_val = h_pred_highdphi -> GetBinContent( bi_hist ) ;
                  hdp_hist_err = h_pred_highdphi -> GetBinError( bi_hist ) ;
               }
               double hdp_err = 1. ;
               if ( hdp_val > 0 ) hdp_err = hdp_hist_err ;


               double total_syst2_lowdphi_events(0.) ;
               double total_syst2_highdphi_events(0.) ;
               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi(0.) ;
                  double syst_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     syst_lowdphi = fabs( h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                     syst_highdphi = fabs( h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  }
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst2_lowdphi_events  += pow( syst_lowdphi  * ldp_val, 2. ) ;
                     total_syst2_highdphi_events += pow( syst_highdphi * hdp_val, 2. ) ;
                  }
               } // si

               double ldp_syst = sqrt( total_syst2_lowdphi_events ) ;
               double hdp_syst = sqrt( total_syst2_highdphi_events ) ;

               TString hist_bin_label ;
               if ( bi_hist > 0 ) { hist_bin_label = h_pred_lowdphi -> GetXaxis() -> GetBinLabel( bi_hist ) ; }

               int bi_ht = 0, bi_mht = 0;

               if ( bi_htmht == 1 ) { bi_ht = 1; bi_mht = 1; }
               if ( bi_htmht == 2 ) { bi_ht = 2; bi_mht = 1; }
               if ( bi_htmht == 3 ) { bi_ht = 3; bi_mht = 1; }

               if ( bi_htmht == 4 ) { bi_ht = 1; bi_mht = 2; }
               if ( bi_htmht == 5 ) { bi_ht = 2; bi_mht = 2; }
               if ( bi_htmht == 6 ) { bi_ht = 3; bi_mht = 2; }

               if ( bi_htmht == 7 ) { bi_ht = 1; bi_mht = 3; }
               if ( bi_htmht == 8 ) { bi_ht = 2; bi_mht = 3; }
               if ( bi_htmht == 9 ) { bi_ht = 3; bi_mht = 3; }

               if ( bi_htmht ==10 ) { bi_ht = 2; bi_mht = 4; }
               if ( bi_htmht ==11 ) { bi_ht = 3; bi_mht = 4; }

               if ( bi_htmht ==12 ) { bi_ht = 2; bi_mht = 5; }
               if ( bi_htmht ==13 ) { bi_ht = 3; bi_mht = 5; }

               if ( bi_mht == 1 ) {
                  bi_control ++ ;
               } else {
                  bi_search ++ ;
               }

               char mhtchar[10] ;
               if ( bi_mht == 1 ) {
                  sprintf( mhtchar, "C" ) ;
               } else {
                  sprintf( mhtchar, "%d", bi_mht-1 ) ;
               }

               char label[1000] ;
               sprintf( label, " %3d  %s %3d  Nj%d-Nb%d-MHT%s-HT%d",
                   bi_hist, (bi_mht==1)?"C":"S", (bi_mht==1)?bi_control:bi_search,
                   bi_nj, bi_nb-1, mhtchar, bi_ht ) ;

               //// printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;
               if ( TMath::IsNaN( ldp_val ) ) ldp_val = 0. ;
               if ( TMath::IsNaN( ldp_err ) ) ldp_err = 0. ;
               if ( TMath::IsNaN( ldp_syst ) ) ldp_syst = 0. ;
               if ( TMath::IsNaN( hdp_val ) ) hdp_val = 0. ;
               if ( TMath::IsNaN( hdp_err ) ) hdp_err = 0. ;
               if ( TMath::IsNaN( hdp_syst ) ) hdp_syst = 0. ;


               printf(               "%s      %8.1f +/- %5.1f +/- %5.1f          %8.1f +/- %5.1f +/- %5.1f\n",
                   label,
                   ldp_val, ldp_err, ldp_syst,
                   hdp_val, hdp_err, hdp_syst ) ;

               fprintf( ofp_combine, "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, ldp_err, ldp_syst,   hdp_val, hdp_err, hdp_syst ) ;


            } // bi_htmht
         } // bi_nb
      } // bi_nj

      fclose( ofp_combine ) ;
      printf("\n\n Wrote %s\n\n", output_text_file ) ;










     //-----

      fprintf( ofp_ldp_nbsum, "  hadtau ldp nsyst=%d\n", n_systerr ) ;
      fprintf( ofp_hdp_nbsum, "  hadtau hdp nsyst=%d\n", n_systerr ) ;

      fprintf( ofp_ldp_nbsum, "  bin label    value   stat  " ) ;
      fprintf( ofp_hdp_nbsum, "  bin label    value   stat  " ) ;
      fprintf( ofp_ldp_nbsum, " %15s ", "total-syst" ) ;
      fprintf( ofp_hdp_nbsum, " %15s ", "total-syst" ) ;
      for ( int si=0; si<n_systerr; si++ ) {
         fprintf( ofp_ldp_nbsum, " %15s ", systerr_name[si] ) ;
         fprintf( ofp_hdp_nbsum, " %15s ", systerr_name[si] ) ;
      } // si
      fprintf( ofp_ldp_nbsum, "\n") ;
      fprintf( ofp_hdp_nbsum, "\n") ;

      for ( int bi_ht=1; bi_ht<=nBinsHT; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

            if ( bi_ht==1 && bi_nj>3 ) continue ;

            float ldp_nbsum_val(0.) ;
            float hdp_nbsum_val(0.) ;

            float ldp_nbsum_stat_err2(0.) ;
            float hdp_nbsum_stat_err2(0.) ;

            double total_syst_lowdphi_events[100] ;
            double total_syst_highdphi_events[100] ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_lowdphi_events[si]  = 0 ;
               total_syst_highdphi_events[si] = 0 ;
            }

            for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {

               bi_hist = hadtau_global_bin_with_mhtc( bi_nj, bi_nb, bi_ht ) ;

               if ( bi_hist > 0 ) {
                  int hbl_nj, hbl_nb, hbl_mht, hbl_htmht ;
                  sscanf( h_pred_lowdphi->GetXaxis()->GetBinLabel( bi_hist ), "NJets%d_BTags%d_MHT%d_HT%d", &hbl_nj, &hbl_nb, &hbl_mht, &hbl_htmht ) ;
                  if ( bi_nj != (hbl_nj+1) ) { printf("\n\n*** Inconsistent Njets.  %d != %d\n", bi_nj, hbl_nj+1) ; gSystem -> Exit(-1) ; }
                  if ( bi_nb != (hbl_nb+1) ) { printf("\n\n*** Inconsistent Nb. %d != %d\n", bi_nb, hbl_nb+1 ) ; gSystem -> Exit(-1) ; }
               }

               double ldp_val(0.) ;
               double ldp_hist_err(0.) ;
               if ( bi_hist > 0 ) {
                  ldp_val = h_pred_lowdphi -> GetBinContent( bi_hist ) ;
                  ldp_hist_err = h_pred_lowdphi -> GetBinError( bi_hist ) ;
               }
               if ( TMath::IsNaN( ldp_val ) ) ldp_val = 0. ;
               if ( TMath::IsNaN( ldp_hist_err ) ) ldp_hist_err = 0. ;
               double ldp_err = 1. ;
               if ( ldp_val > 0 ) ldp_err = ldp_hist_err ;

               double hdp_val(0.) ;
               double hdp_hist_err (0.) ;
               if ( bi_hist > 0 ) {
                  hdp_val = h_pred_highdphi -> GetBinContent( bi_hist ) ;
                  hdp_hist_err = h_pred_highdphi -> GetBinError( bi_hist ) ;
               }
               if ( TMath::IsNaN( hdp_val ) ) hdp_val = 0. ;
               if ( TMath::IsNaN( hdp_hist_err ) ) hdp_hist_err = 0. ;
               double hdp_err = 1. ;
               if ( hdp_val > 0 ) hdp_err = hdp_hist_err ;


               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi(0.) ;
                  double syst_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     syst_lowdphi = fabs( h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                     syst_highdphi = fabs( h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ) ;
                  }
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst_lowdphi_events[si]  += syst_lowdphi  * ldp_val ;
                     total_syst_highdphi_events[si] += syst_highdphi * hdp_val ;
                  }
               } // si

               ldp_nbsum_val += ldp_val ;
               hdp_nbsum_val += hdp_val ;

               ldp_nbsum_stat_err2 += pow( ldp_err, 2. ) ;
               hdp_nbsum_stat_err2 += pow( hdp_err, 2. ) ;

               TString hist_bin_label ;
               if ( bi_hist > 0 ) { hist_bin_label = h_pred_lowdphi -> GetXaxis() -> GetBinLabel( bi_hist ) ; }

               char label[1000] ;
               sprintf( label, " %3d  Nj%d-Nb%d-MHTC-HT%d", bi_hist, bi_nj, bi_nb-1, bi_ht ) ;

            } // bi_nb

            float ldp_nbsum_stat_err = sqrt( ldp_nbsum_stat_err2 ) ;
            float hdp_nbsum_stat_err = sqrt( hdp_nbsum_stat_err2 ) ;

            fprintf( ofp_ldp_nbsum, "   Nj%d-HT%d   %8.1f   %5.1f ", bi_nj, bi_ht, ldp_nbsum_val, ldp_nbsum_stat_err ) ;
            fprintf( ofp_hdp_nbsum, "   Nj%d-HT%d   %8.1f   %5.1f ", bi_nj, bi_ht, hdp_nbsum_val, hdp_nbsum_stat_err ) ;

            double total_syst_err2_lowdphi(0.) ;
            double total_syst_err2_highdphi(0.) ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_err2_lowdphi += pow( total_syst_lowdphi_events[si], 2.)   ;
               total_syst_err2_highdphi += pow( total_syst_highdphi_events[si], 2.)  ;
            }
            double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;
            double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;


            double rel_ldp_total_err(0.) ;
            if ( ldp_nbsum_val > 0 ) rel_ldp_total_err = total_syst_err_lowdphi / ldp_nbsum_val ;
            double rel_hdp_total_err(0.) ;
            if ( ldp_nbsum_val > 0 ) rel_hdp_total_err = total_syst_err_highdphi / hdp_nbsum_val ;
            fprintf( ofp_ldp_nbsum, "  %5.1f (%7.4f)", total_syst_err_lowdphi, rel_ldp_total_err ) ;
            fprintf( ofp_hdp_nbsum, "  %5.1f (%7.4f)", total_syst_err_highdphi, rel_hdp_total_err ) ;

            for ( int si=0; si<n_systerr; si++ ) {
               double rel_ldp_err(0.) ;
               if ( ldp_nbsum_val > 0 ) rel_ldp_err = total_syst_lowdphi_events[si] / ldp_nbsum_val ;
               double rel_hdp_err(0.) ;
               if ( ldp_nbsum_val > 0 ) rel_hdp_err = total_syst_highdphi_events[si] / hdp_nbsum_val ;
               fprintf( ofp_ldp_nbsum, "  %5.1f (%7.4f)", total_syst_lowdphi_events[si], rel_ldp_err ) ;
               fprintf( ofp_hdp_nbsum, "  %5.1f (%7.4f)", total_syst_highdphi_events[si], rel_hdp_err ) ;
            }

            fprintf( ofp_ldp_nbsum, "\n") ;
            fprintf( ofp_hdp_nbsum, "\n") ;



         } // bi_nj
      } // bi_ht

      fclose( ofp_ldp_nbsum ) ;
      printf("\n\n Wrote %s\n\n", nbsum_ldp_text_file ) ;
      fclose( ofp_hdp_nbsum ) ;
      printf("\n\n Wrote %s\n\n", nbsum_hdp_text_file ) ;



   } // make_hadtau_lh_input_files1
Пример #5
0
   void gen_modelfit_input1(
         const char* data_file    = "outputfiles/nbsum-input-data.txt",
         const char* lostlep_file = "outputfiles/nbsum-input-lostlep.txt",
         const char* hadtau_file  = "outputfiles/nbsum-input-hadtau.txt",
         const char* znunu_file   = "outputfiles/nbsum-input-znunu.txt",
         const char* output_hist_file = "outputfiles/modelfit-input-data.root"
                         ) {


      gDirectory -> Delete( "h*" ) ;

      setup_bins() ;
      ifstream ifs_data ;
      ifs_data.open( data_file ) ;
      if ( !ifs_data.good() ) { printf("\n\n *** Problem opening data file: %s\n\n", data_file ) ; return ; }

      ifstream ifs_lostlep ;
      ifs_lostlep.open( lostlep_file ) ;
      if ( !ifs_lostlep.good() ) { printf("\n\n *** Problem opening lostlep file: %s\n\n", lostlep_file ) ; return ; }

      ifstream ifs_hadtau ;
      ifs_hadtau.open( hadtau_file ) ;
      if ( !ifs_hadtau.good() ) { printf("\n\n *** Problem opening hadtau file: %s\n\n", hadtau_file ) ; return ; }

      ifstream ifs_znunu ;
      ifs_znunu.open( znunu_file ) ;
      if ( !ifs_znunu.good() ) { printf("\n\n *** Problem opening znunu file: %s\n\n", znunu_file ) ; return ; }

      int    nobs_ldp[10][10] ;
      double nonqcd_val_ldp[10][10] ;
      double nonqcd_err_ldp[10][10] ;

      int    nobs_hdp[10][10] ;
      double nonqcd_val_hdp[10][10] ;
      double nonqcd_err_hdp[10][10] ;

      TH1F* h_ratio = new TH1F( "h_ratio", "H/L ratio", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;

      TH1F* h_ldp_lostlep = new TH1F( "h_ldp_lostlep", "ldp, lostlep", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_ldp_hadtau = new TH1F( "h_ldp_hadtau", "ldp, hadtau", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_ldp_znunu = new TH1F( "h_ldp_znunu", "ldp, znunu", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_ldp_data = new TH1F( "h_ldp_data", "ldp, data", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;


      TH1F* h_hdp_lostlep = new TH1F( "h_hdp_lostlep", "hdp, lostlep", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_hdp_hadtau = new TH1F( "h_hdp_hadtau", "hdp, hadtau", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_hdp_znunu = new TH1F( "h_hdp_znunu", "hdp, znunu", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;
      TH1F* h_hdp_data = new TH1F( "h_hdp_data", "hdp, data", nBinsHT * nb_nj, 0.5, nBinsHT * nb_nj + 0.5 ) ;


      int bi_hist(0) ;

      for ( int bi_ht=1; bi_ht<=nBinsHT; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

               bi_hist ++ ;

               TString line ;
               int global_bi, region_bi ;
               char region_tag[5] ;
               char label[100] ;
               int r_nobs_ldp, r_nobs_hdp ;
               float nbg_ldp_val, nbg_ldp_stat, nbg_ldp_syst ;
               float nbg_hdp_val, nbg_hdp_stat, nbg_hdp_syst ;


              //-------
               line.ReadLine( ifs_data ) ;
               sscanf( line.Data(), "%s  %d  %d", label, &r_nobs_ldp, &r_nobs_hdp ) ;
               printf( "  Data    :   %s  Nldp = %5d ,                        Nhdp = %5d\n",
                    label, r_nobs_ldp, r_nobs_hdp ) ;

               nobs_ldp[bi_ht][bi_nj] = r_nobs_ldp ;
               nobs_hdp[bi_ht][bi_nj] = r_nobs_hdp ;

               h_ldp_data -> SetBinContent( bi_hist, r_nobs_ldp ) ;
               h_ldp_data -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

               h_hdp_data -> SetBinContent( bi_hist, r_nobs_hdp ) ;
               h_hdp_data -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;


               double total_bg_ldp_val = 0. ;
               double total_bg_ldp_err2 = 0. ;

               double total_bg_hdp_val = 0. ;
               double total_bg_hdp_err2 = 0. ;

              //-------
               line.ReadLine( ifs_lostlep ) ;
               sscanf( line.Data(), "%s  %f +/- %f +/- %f    %f +/- %f +/- %f",
                    label,  &nbg_ldp_val, &nbg_ldp_stat, &nbg_ldp_syst,  &nbg_hdp_val, &nbg_hdp_stat, &nbg_hdp_syst ) ;
               printf( "  Lostlep :   %s  Nldp = %7.1f +/- %5.1f +/- %5.1f ,  Nhdp = %7.1f +/- %5.1f +/- %5.1f\n",
                    label, nbg_ldp_val, nbg_ldp_stat, nbg_ldp_syst,  nbg_hdp_val, nbg_hdp_stat, nbg_hdp_syst ) ;

               total_bg_ldp_val += nbg_ldp_val ;
               total_bg_hdp_val += nbg_hdp_val ;
               total_bg_ldp_err2 += pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ;
               total_bg_hdp_err2 += pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ;

               h_ldp_lostlep -> SetBinContent( bi_hist, nbg_ldp_val ) ;
               h_ldp_lostlep -> SetBinError( bi_hist, sqrt( pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ) ) ;
               h_ldp_lostlep -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

               h_hdp_lostlep -> SetBinContent( bi_hist, nbg_hdp_val ) ;
               h_hdp_lostlep -> SetBinError( bi_hist, sqrt( pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ) ) ;
               h_hdp_lostlep -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

              //-------
               line.ReadLine( ifs_hadtau ) ;
               sscanf( line.Data(), "%s  %f +/- %f +/- %f    %f +/- %f +/- %f",
                    label,  &nbg_ldp_val, &nbg_ldp_stat, &nbg_ldp_syst,  &nbg_hdp_val, &nbg_hdp_stat, &nbg_hdp_syst ) ;
               printf( "  Hadtau  :   %s  Nldp = %7.1f +/- %5.1f +/- %5.1f ,  Nhdp = %7.1f +/- %5.1f +/- %5.1f\n",
                    label, nbg_ldp_val, nbg_ldp_stat, nbg_ldp_syst,  nbg_hdp_val, nbg_hdp_stat, nbg_hdp_syst ) ;

               total_bg_ldp_val += nbg_ldp_val ;
               total_bg_hdp_val += nbg_hdp_val ;
               total_bg_ldp_err2 += pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ;
               total_bg_hdp_err2 += pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ;

               h_ldp_hadtau -> SetBinContent( bi_hist, nbg_ldp_val ) ;
               h_ldp_hadtau -> SetBinError( bi_hist, sqrt( pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ) ) ;
               h_ldp_hadtau -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

               h_hdp_hadtau -> SetBinContent( bi_hist, nbg_hdp_val ) ;
               h_hdp_hadtau -> SetBinError( bi_hist, sqrt( pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ) ) ;
               h_hdp_hadtau -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

              //-------
               line.ReadLine( ifs_znunu ) ;
               sscanf( line.Data(), "%s  %f +/- %f +/- %f    %f +/- %f +/- %f",
                    label,  &nbg_ldp_val, &nbg_ldp_stat, &nbg_ldp_syst,  &nbg_hdp_val, &nbg_hdp_stat, &nbg_hdp_syst ) ;
               printf( "  Znunu   :   %s  Nldp = %7.1f +/- %5.1f +/- %5.1f ,  Nhdp = %7.1f +/- %5.1f +/- %5.1f\n",
                    label, nbg_ldp_val, nbg_ldp_stat, nbg_ldp_syst,  nbg_hdp_val, nbg_hdp_stat, nbg_hdp_syst ) ;

               total_bg_ldp_val += nbg_ldp_val ;
               total_bg_hdp_val += nbg_hdp_val ;
               total_bg_ldp_err2 += pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ;
               total_bg_hdp_err2 += pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ;

               h_ldp_znunu -> SetBinContent( bi_hist, nbg_ldp_val ) ;
               h_ldp_znunu -> SetBinError( bi_hist, sqrt( pow( nbg_ldp_stat, 2. ) + pow( nbg_ldp_syst, 2. ) ) ) ;
               h_ldp_znunu -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;

               h_hdp_znunu -> SetBinContent( bi_hist, nbg_hdp_val ) ;
               h_hdp_znunu -> SetBinError( bi_hist, sqrt( pow( nbg_hdp_stat, 2. ) + pow( nbg_hdp_syst, 2. ) ) ) ;
               h_hdp_znunu -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;


               double total_bg_ldp_err = sqrt( total_bg_ldp_err2 ) ;
               double total_bg_hdp_err = sqrt( total_bg_hdp_err2 ) ;

               printf( " total BG :   %s  Nldp = %7.1f +/- %5.1f           ,  Nhdp = %7.1f +/- %5.1f          \n",
                   label, total_bg_ldp_val, total_bg_ldp_err,   total_bg_hdp_val, total_bg_hdp_err ) ;

               double qcd_ldp_val = r_nobs_ldp - total_bg_ldp_val ;
               double qcd_ldp_err = sqrt( r_nobs_ldp + total_bg_ldp_err*total_bg_ldp_err ) ;

               double qcd_hdp_val = r_nobs_hdp - total_bg_hdp_val ;
               double qcd_hdp_err = sqrt( r_nobs_hdp + total_bg_hdp_err*total_bg_hdp_err ) ;
               double qcd_hdp_rel_err(0.) ;
               if ( qcd_hdp_val > 0 ) qcd_hdp_rel_err = qcd_hdp_err / qcd_hdp_val ;

               double ratio_val(0.) ;
               double ratio_err(0.) ;
               double ratio_rel_err(0.) ;
               if ( qcd_ldp_val != 0 ) {
                  ratio_val = qcd_hdp_val / qcd_ldp_val ;
                  if ( qcd_hdp_val != 0 ) {
                     ratio_err = fabs(ratio_val) * sqrt( pow( qcd_ldp_err/qcd_ldp_val, 2.) + pow( qcd_hdp_err/qcd_hdp_val, 2. ) ) ;
                     ratio_rel_err = ratio_err / ratio_val ;
                  }
               }

               printf( " QCD      :   %s  Nldp = %7.1f +/- %5.1f           ,  Nhdp = %7.1f +/- %5.1f  (%6.3f)      R(H/L) = %6.3f +/- %6.3f  (%6.3f)\n",
                   label, qcd_ldp_val, qcd_ldp_err,   qcd_hdp_val, qcd_hdp_err, qcd_hdp_rel_err,    ratio_val, ratio_err, ratio_rel_err ) ;

               printf("\n") ;

               if ( !(bi_ht==1 && bi_nj>nb_nj-2) ) {  // skip top two njets bins for lowest HT
                  h_ratio -> SetBinContent( bi_hist, ratio_val ) ;
                  h_ratio -> SetBinError( bi_hist, ratio_err ) ;
               } else {
                  h_ratio -> SetBinContent( bi_hist, -9 ) ;
                  h_ratio -> SetBinError( bi_hist, 0. ) ;
               }
               h_ratio -> GetXaxis() -> SetBinLabel( bi_hist, label ) ;


         } // bi_nj
      } // bi_ht

      TH1F* h_ldp_allnonqcd = (TH1F*) h_ldp_lostlep -> Clone( "h_ldp_allnonqcd" ) ;
      h_ldp_allnonqcd -> Add( h_ldp_hadtau ) ;
      h_ldp_allnonqcd -> Add( h_ldp_znunu ) ;

      TH1F* h_hdp_allnonqcd = (TH1F*) h_hdp_lostlep -> Clone( "h_hdp_allnonqcd" ) ;
      h_hdp_allnonqcd -> Add( h_hdp_hadtau ) ;
      h_hdp_allnonqcd -> Add( h_hdp_znunu ) ;



      char fname[1000] ;

      h_ldp_lostlep -> GetXaxis() -> LabelsOption( "v" ) ;
      h_ldp_hadtau -> GetXaxis() -> LabelsOption( "v" ) ;
      h_ldp_znunu -> GetXaxis() -> LabelsOption( "v" ) ;
      h_ldp_data -> GetXaxis() -> LabelsOption( "v" ) ;

      h_hdp_lostlep -> GetXaxis() -> LabelsOption( "v" ) ;
      h_hdp_hadtau -> GetXaxis() -> LabelsOption( "v" ) ;
      h_hdp_znunu -> GetXaxis() -> LabelsOption( "v" ) ;
      h_hdp_data -> GetXaxis() -> LabelsOption( "v" ) ;

      h_ldp_lostlep -> SetFillColor( kBlue-10 ) ;
      h_ldp_hadtau  -> SetFillColor( kCyan-10 ) ;
      h_ldp_znunu   -> SetFillColor( kGreen-7 ) ;
      h_ldp_data -> SetLineWidth(3) ;

      h_hdp_lostlep -> SetFillColor( kBlue-10 ) ;
      h_hdp_hadtau  -> SetFillColor( kCyan-10 ) ;
      h_hdp_znunu   -> SetFillColor( kGreen-7 ) ;
      h_hdp_data -> SetLineWidth(3) ;

      TLegend* legend = new TLegend( 0.80, 0.80, 0.95, 0.95 ) ;
      legend -> AddEntry( h_ldp_znunu, "znunu" ) ;
      legend -> AddEntry( h_ldp_hadtau, "hadtau" ) ;
      legend -> AddEntry( h_ldp_lostlep, "lostlep" ) ;
      legend -> AddEntry( h_ldp_data, "data" ) ;


      h_ratio -> GetXaxis() -> LabelsOption( "v" ) ;

      gStyle -> SetPadBottomMargin(0.20) ;
      gStyle -> SetOptStat(0) ;




      saveHist( output_hist_file, "h*", false ) ;

     //-------

      TCanvas* can_ratio = new TCanvas( "can_ratio", "Ratio", 900, 800 ) ;

      h_ratio -> SetMarkerStyle(20) ;
      h_ratio -> SetMinimum(-0.1) ;
      h_ratio -> Draw() ;
      gPad -> SetGridy(1) ;


     //-------

      TCanvas* can_ldp = new TCanvas( "can_ldp", "LDP", 900, 800 ) ;

      THStack* hstack_ldp = new THStack( "hstack_ldp", "hstack_ldp" ) ;
      hstack_ldp -> Add( h_ldp_lostlep ) ;
      hstack_ldp -> Add( h_ldp_hadtau ) ;
      hstack_ldp -> Add( h_ldp_znunu ) ;

      h_ldp_data -> Draw() ;
      hstack_ldp -> Draw( "hist same" ) ;
      hstack_ldp -> Draw( "same" ) ;
      h_ldp_data -> Draw( "axis same" ) ;
      h_ldp_data -> Draw( "axig same" ) ;

      legend -> Draw() ;

      gPad -> SetGridy(1) ;

      can_ldp -> Update() ; can_ldp -> Draw() ;
      can_ldp -> SaveAs( "outputfiles/modelfit-input-ldp-liny-full.pdf" ) ;

      h_ldp_data -> SetMaximum( 1.1 * (h_ldp_data -> GetBinContent(2)) ) ;
      can_ldp -> Update() ; can_ldp -> Draw() ;
      can_ldp -> SaveAs( "outputfiles/modelfit-input-ldp-liny-zoom1.pdf" ) ;

      h_ldp_data -> SetMaximum( 1.1 * (h_ldp_data -> GetBinContent(8)) ) ;
      can_ldp -> Update() ; can_ldp -> Draw() ;
      can_ldp -> SaveAs( "outputfiles/modelfit-input-ldp-liny-zoom2.pdf" ) ;

      h_ldp_data -> SetMaximum( 1.1 * (h_ldp_data -> GetBinContent(13)) ) ;
      can_ldp -> Update() ; can_ldp -> Draw() ;
      can_ldp -> SaveAs( "outputfiles/modelfit-input-ldp-liny-zoom3.pdf" ) ;

      h_ldp_data -> SetMaximum( 3 * (h_ldp_data -> GetBinContent(8)) ) ;
      h_ldp_data -> SetMinimum(0.5) ;
      gPad -> SetLogy(1) ;
      can_ldp -> Update() ; can_ldp -> Draw() ;
      can_ldp -> SaveAs( "outputfiles/modelfit-input-ldp-logy-full.pdf" ) ;



     //-------

      TCanvas* can_hdp = new TCanvas( "can_hdp", "hdp", 900, 800 ) ;

      THStack* hstack_hdp = new THStack( "hstack_hdp", "hstack_hdp" ) ;
      hstack_hdp -> Add( h_hdp_lostlep ) ;
      hstack_hdp -> Add( h_hdp_hadtau ) ;
      hstack_hdp -> Add( h_hdp_znunu ) ;

      h_hdp_data -> Draw() ;
      hstack_hdp -> Draw( "hist same" ) ;
      hstack_hdp -> Draw( "same" ) ;
      h_hdp_data -> Draw( "axis same" ) ;
      h_hdp_data -> Draw( "axig same" ) ;

      legend -> Draw() ;

      gPad -> SetGridy(1) ;

      can_hdp -> Update() ; can_hdp -> Draw() ;
      can_hdp -> SaveAs( "outputfiles/modelfit-input-hdp-liny-full.pdf" ) ;

      h_hdp_data -> SetMaximum( 1.1 * (h_hdp_data -> GetBinContent(7)) ) ;
      can_hdp -> Update() ; can_hdp -> Draw() ;
      can_hdp -> SaveAs( "outputfiles/modelfit-input-hdp-liny-zoom1.pdf" ) ;

      h_hdp_data -> SetMaximum( 1.1 * (h_hdp_data -> GetBinContent(12)) ) ;
      can_hdp -> Update() ; can_hdp -> Draw() ;
      can_hdp -> SaveAs( "outputfiles/modelfit-input-hdp-liny-zoom2.pdf" ) ;

      h_hdp_data -> SetMaximum( 3 * (h_hdp_data -> GetBinContent(2)) ) ;
      h_hdp_data -> SetMinimum(0.5) ;
      gPad -> SetLogy(1) ;
      can_hdp -> Update() ; can_hdp -> Draw() ;
      can_hdp -> SaveAs( "outputfiles/modelfit-input-hdp-logy-full.pdf" ) ;





  //  TFile rf( output_hist_file, "recreate" ) ;
  //  h_ratio -> Write() ;
  //  rf.Close() ;
  //  printf("\n\n Saved ratio histogram in %s\n\n", output_hist_file ) ;




   } // gen_combine_input
   void make_lostlep_input_files2(
                                 const char* ldp_input_root_file = "non-qcd-inputs-fall16c/LLPrediction_QCDLDP_notCombined.root",
                                 const char* hdp_input_root_file = "non-qcd-inputs-fall16c/LLPrediction_QCDHDP_notCombined.root",
                                 const char* output_text_file = "outputfiles/combine-input-lostlep.txt",
                                 const char* nbsum_text_file = "outputfiles/nbsum-input-lostlep.txt"
                               ) {

      setup_bins();

      //bool verb(false) ;
      bool verb(true) ;

      gDirectory -> Delete( "h*" ) ;

      TFile* tf_ldp = new TFile( ldp_input_root_file, "read" ) ;
      if ( tf_ldp == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", ldp_input_root_file ) ; return ; }
      if ( !(tf_ldp -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", ldp_input_root_file ) ; return ; }

      TFile* tf_hdp = new TFile( hdp_input_root_file, "read" ) ;
      if ( tf_hdp == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", hdp_input_root_file ) ; return ; }
      if ( !(tf_hdp -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", hdp_input_root_file ) ; return ; }

      tf_ldp -> cd( "Prediction_data" ) ;
      tf_hdp -> cd( "Prediction_data" ) ;

      printf("\n") ;
      tf_ldp -> ls() ;
      printf("\n") ;

      FILE* ofp_combine ;
      if ( (ofp_combine = fopen( output_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine output file: %s\n\n", output_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum ;
      if ( (ofp_nbsum = fopen( nbsum_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum output file: %s\n\n", nbsum_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum_stat_syst ;
      TString systfile_nbsum( nbsum_text_file ) ;
      systfile_nbsum.ReplaceAll("input","stat-syst") ;
      if ( (ofp_nbsum_stat_syst = fopen( systfile_nbsum.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum stat-syst output file: %s\n\n", systfile_nbsum.Data() ) ;
         return ;
      }

      FILE* ofp_combine_stat_syst ;
      TString systfile_combine( output_text_file ) ;
      systfile_combine.ReplaceAll("input","stat-syst") ;
      if ( (ofp_combine_stat_syst = fopen( systfile_combine.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine stat-syst output file: %s\n\n", systfile_combine.Data() ) ;
         return ;
      }

      TH1F* h_ldp = (TH1F*) tf_ldp -> Get( "Prediction_data/totalPred_LL" ) ;
      if ( h_ldp == 0x0 ) { printf("\n\n *** Missing totalPred_LL for LDP\n\n") ; return ; }

      TH1F* h_hdp = (TH1F*) tf_hdp -> Get( "Prediction_data/totalPred_LL" ) ;
      if ( h_hdp == 0x0 ) { printf("\n\n *** Missing totalPred_LL for HDP\n\n") ; return ; }

      TH1* h_systerr_lowdphi[100] ;
      TH1* h_systerr_highdphi[100] ;
      char systerr_name[100][100] ;
      int n_systerr(0) ;

      {
         int si(0) ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredIsoTrackSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredIsoTrackSysUp_LL" ) ;
         sprintf( systerr_name[si], "IsoTrk" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMTWSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMTWSysUp_LL" ) ;
         sprintf( systerr_name[si], "MTW" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredPuritySysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredPuritySysUp_LL" ) ;
         sprintf( systerr_name[si], "Purity" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredSingleLepPuritySysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredSingleLepPuritySysUp_LL" ) ;
         sprintf( systerr_name[si], "SingleLepPurity" ) ;
         si++ ;

         ///////////h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredDiLepFoundSysUp_LL" ) ;
         ///////////h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredDiLepFoundSysUp_LL" ) ;
         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredDiLepSRSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredDiLepSRSysUp_LL" ) ;
         sprintf( systerr_name[si], "DiLepFound" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuIsoSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuIsoSysUp_LL" ) ;
         sprintf( systerr_name[si], "MuIso" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuRecoSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuRecoSysUp_LL" ) ;
         sprintf( systerr_name[si], "MuReco" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuAccSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuAccSysUp_LL" ) ;
         sprintf( systerr_name[si], "MuAcc" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuAccQsquareSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuAccQsquareSysUp_LL" ) ;
         sprintf( systerr_name[si], "MuAccQ2" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecIsoSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecIsoSysUp_LL" ) ;
         sprintf( systerr_name[si], "ElecIso" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecRecoSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecRecoSysUp_LL" ) ;
         sprintf( systerr_name[si], "ElecReco" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecAccSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecAccSysUp_LL" ) ;
         sprintf( systerr_name[si], "ElecAcc" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecAccQsquareSysUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecAccQsquareSysUp_LL" ) ;
         sprintf( systerr_name[si], "ElecAccQ2" ) ;
         si++ ;

         h_systerr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredNonClosureUp_LL" ) ;
         h_systerr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredNonClosureUp_LL" ) ;
         sprintf( systerr_name[si], "NonClosure" ) ;
         si++ ;


         n_systerr = si ;
      }




      TH1* h_staterr_lowdphi[100] ;
      TH1* h_staterr_highdphi[100] ;
      char staterr_name[100][100] ;
      int n_staterr(0) ;

      {
         int si(0) ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredIsoTrackStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredIsoTrackStatUp_LL" ) ;
         sprintf( staterr_name[si], "IsoTrk" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMTWStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMTWStatUp_LL" ) ;
         sprintf( staterr_name[si], "MTW" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredPurityStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredPurityStatUp_LL" ) ;
         sprintf( staterr_name[si], "Purity" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredSingleLepPurityStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredSingleLepPurityStatUp_LL" ) ;
         sprintf( staterr_name[si], "SingleLepPurity" ) ;
         si++ ;

         //////////////////////h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredDiLepFoundStatUp_LL" ) ;
         //////////////////////h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredDiLepFoundStatUp_LL" ) ;
         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredDiLepSRStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredDiLepSRStatUp_LL" ) ;
         sprintf( staterr_name[si], "DiLepFound" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuIsoStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuIsoStatUp_LL" ) ;
         sprintf( staterr_name[si], "MuIso" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuRecoStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuRecoStatUp_LL" ) ;
         sprintf( staterr_name[si], "MuReco" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredMuAccStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredMuAccStatUp_LL" ) ;
         sprintf( staterr_name[si], "MuAcc" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecIsoStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecIsoStatUp_LL" ) ;
         sprintf( staterr_name[si], "ElecIso" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecRecoStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecRecoStatUp_LL" ) ;
         sprintf( staterr_name[si], "ElecReco" ) ;
         si++ ;

         h_staterr_lowdphi[si]  = get_hist( tf_ldp, "Prediction_data/totalPredElecAccStatUp_LL" ) ;
         h_staterr_highdphi[si]  = get_hist( tf_hdp, "Prediction_data/totalPredElecAccStatUp_LL" ) ;
         sprintf( staterr_name[si], "ElecAcc" ) ;
         si++ ;

         n_staterr = si ;
      }










      int bi_hist(0) ;
      int bi_control(0) ;
      int bi_search(0) ;
      int count_all(0) ;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               count_all++ ;

               int bi_hist = global_bin_with_mhtc( bi_nj, bi_nb, bi_htmht ) ;

               double ldp_val(0.) ;
               double ldp_hist_err(0.) ;

               double hdp_val(0.) ;
               double hdp_hist_err(0.) ;

               if ( bi_hist > 0 ) {
                  ldp_val = h_ldp -> GetBinContent( bi_hist ) ;
                  ldp_hist_err = h_ldp -> GetBinError( bi_hist ) ;

                  hdp_val = h_hdp -> GetBinContent( bi_hist ) ;
                  hdp_hist_err = h_hdp -> GetBinError( bi_hist ) ;
               }



               double total_syst_err2_lowdphi(0.) ;
               double total_syst_err2_highdphi(0.) ;
               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi(0.) ;
                  double syst_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                     syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  }
                  double syst_lowdphi_events(0.) ;
                  double syst_highdphi_events(0.) ;
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     syst_lowdphi_events  = syst_lowdphi  * ldp_val ;
                     syst_highdphi_events = syst_highdphi * hdp_val ;
                  }
                  total_syst_err2_lowdphi  += pow( syst_lowdphi_events , 2. ) ;
                  total_syst_err2_highdphi += pow( syst_highdphi_events, 2. ) ;
               } // si

               double total_stat_err2_lowdphi(0.) ;
               double total_stat_err2_highdphi(0.) ;
               for ( int si=0; si<n_staterr; si++ ) {
                  double stat_lowdphi(0.) ;
                  double stat_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     stat_lowdphi = h_staterr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                     stat_highdphi = h_staterr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  }
                  double stat_lowdphi_events(0.) ;
                  double stat_highdphi_events(0.) ;
                  if ( stat_lowdphi > 0 && stat_highdphi > 0 ) {
                     stat_lowdphi_events  = stat_lowdphi  * ldp_val ;
                     stat_highdphi_events = stat_highdphi * hdp_val ;
                  }
                  total_stat_err2_lowdphi  += pow( stat_lowdphi_events , 2. ) ;
                  total_stat_err2_highdphi += pow( stat_highdphi_events, 2. ) ;
                  printf(" LDP stat err : %12s %6.3f  %8.1f\n", staterr_name[si], stat_lowdphi, stat_lowdphi_events ) ;
               } // si

               printf(" LDP total stat err :  %8.1f   %8.1f\n", sqrt(total_stat_err2_lowdphi), ldp_hist_err  ) ;
               ///////////////double combined_stat_err_lowdphi = sqrt( pow( total_stat_err2_lowdphi, 2. ) + pow( ldp_hist_err, 2. ) ) ; // BUG
               double combined_stat_err_lowdphi = sqrt( total_stat_err2_lowdphi + pow( ldp_hist_err, 2. ) ) ;
               double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;

               ///////////double combined_stat_err_highdphi = sqrt( pow( total_stat_err2_highdphi, 2. ) + pow( hdp_hist_err, 2. ) ) ; // BUG
               double combined_stat_err_highdphi = sqrt( total_stat_err2_highdphi + pow( hdp_hist_err, 2. ) ) ;
               double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;





               TString hist_bin_label( h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               int bi_ht = 0, bi_mht = 0;

               if ( bi_htmht == 1 ) { bi_ht = 1; bi_mht = 1; }
               if ( bi_htmht == 2 ) { bi_ht = 2; bi_mht = 1; }
               if ( bi_htmht == 3 ) { bi_ht = 3; bi_mht = 1; }

               if ( bi_htmht == 4 ) { bi_ht = 1; bi_mht = 2; }
               if ( bi_htmht == 5 ) { bi_ht = 2; bi_mht = 2; }
               if ( bi_htmht == 6 ) { bi_ht = 3; bi_mht = 2; }

               if ( bi_htmht == 7 ) { bi_ht = 1; bi_mht = 3; }
               if ( bi_htmht == 8 ) { bi_ht = 2; bi_mht = 3; }
               if ( bi_htmht == 9 ) { bi_ht = 3; bi_mht = 3; }

               if ( bi_htmht ==10 ) { bi_ht = 2; bi_mht = 4; }
               if ( bi_htmht ==11 ) { bi_ht = 3; bi_mht = 4; }

               if ( bi_htmht ==12 ) { bi_ht = 2; bi_mht = 5; }
               if ( bi_htmht ==13 ) { bi_ht = 3; bi_mht = 5; }

               if ( bi_mht == 1 ) {
                  bi_control ++ ;
               } else {
                  bi_search ++ ;
               }

               char mhtchar[10] ;
               if ( bi_mht == 1 ) {
                  sprintf( mhtchar, "C" ) ;
               } else {
                  sprintf( mhtchar, "%d", bi_mht-1 ) ;
               }

               char label[1000] ;
               sprintf( label, " %3d  %s %3d  Nj%d-Nb%d-MHT%s-HT%d",
                   count_all, (bi_mht==1)?"C":"S", (bi_mht==1)?bi_control:bi_search,
                   bi_nj, bi_nb-1, mhtchar, bi_ht ) ;

               //// printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;

               double ldp_stat_over_sqrtn(0.8), ldp_syst_over_n(0.15) ;
               if ( ldp_val > 0 ) {
                  ldp_stat_over_sqrtn = combined_stat_err_lowdphi / sqrt( ldp_val ) ;
                  ldp_syst_over_n = total_syst_err_lowdphi / ldp_val ;
               }
               double hdp_stat_over_sqrtn(0.8), hdp_syst_over_n(0.15) ;
               if ( hdp_val > 0 ) {
                  hdp_stat_over_sqrtn = combined_stat_err_highdphi / sqrt( hdp_val ) ;
                  hdp_syst_over_n = total_syst_err_highdphi / hdp_val ;
               }

               printf(               "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, combined_stat_err_lowdphi, total_syst_err_lowdphi,   hdp_val, combined_stat_err_highdphi, total_syst_err_highdphi ) ;

               fprintf( ofp_combine, "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, combined_stat_err_lowdphi, total_syst_err_lowdphi,   hdp_val, combined_stat_err_highdphi, total_syst_err_highdphi ) ;

               if ( bi_htmht > 3 ) {
                  char this_label[100] ;
                  sprintf( this_label, "%3d S-Nj%d-Nb%d-MHT%d-HT%d (S%d)", bi_search, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht-3 ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               } else {
                  char this_label[100] ;
                  sprintf( this_label, "%3d C-Nj%d-Nb%d-MHT%d-HT%d (C%d)", bi_control, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               }

            } // bi_htmht
         } // bi_nb
      } // bi_nj

      fclose( ofp_combine ) ;
      printf("\n\n Wrote %s\n\n", output_text_file ) ;

     //---------

      for ( int bi_ht=1; bi_ht<=nBinsHT; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

            float nbsum_lowdphi_val(0.) ;
            float nbsum_lowdphi_err2(0.) ;

            float nbsum_highdphi_val(0.) ;
            float nbsum_highdphi_err2(0.) ;

            double total_syst_lowdphi_events[100] ;
            double total_syst_highdphi_events[100] ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_lowdphi_events[si]  = 0 ;
               total_syst_highdphi_events[si] = 0 ;
            }
            double total_stat2_lowdphi_events[100] ;
            double total_stat2_highdphi_events[100] ;
            for ( int si=0; si<n_staterr; si++ ) {
               total_stat2_lowdphi_events[si]  = 0 ;
               total_stat2_highdphi_events[si] = 0 ;
            }

            for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {

               int bi_hist = global_bin_with_mhtc( bi_nj, bi_nb, bi_ht ) ;

               double lowdphi_val(0.) ;
               double lowdphi_err(0.) ;
               if ( bi_hist > 0 ) {
                  lowdphi_val = h_ldp -> GetBinContent( bi_hist ) ;
                  lowdphi_err = h_ldp -> GetBinError( bi_hist ) ;
               }

               nbsum_lowdphi_val += lowdphi_val ;
               nbsum_lowdphi_err2 += pow( lowdphi_err, 2. ) ;

               double highdphi_val = h_hdp -> GetBinContent( bi_hist ) ;
               double highdphi_err = h_hdp -> GetBinError( bi_hist ) ;

               nbsum_highdphi_val += highdphi_val ;
               nbsum_highdphi_err2 += pow( highdphi_err, 2. ) ;

               for ( int si=0; si<n_systerr; si++ ) {
                  double syst_lowdphi(0.) ;
                  double syst_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                     syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  }
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst_lowdphi_events[si]  += syst_lowdphi  * lowdphi_val ;
                     total_syst_highdphi_events[si] += syst_highdphi * highdphi_val ;
                  }
               } // si

               for ( int si=0; si<n_staterr; si++ ) {
                  double stat_lowdphi(0.) ;
                  double stat_highdphi(0.) ;
                  if ( bi_hist > 0 ) {
                     stat_lowdphi = h_staterr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                     stat_highdphi = h_staterr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  }
                  if ( stat_lowdphi > 0 && stat_highdphi > 0 ) {
                     total_stat2_lowdphi_events[si]  += pow( stat_lowdphi  * lowdphi_val, 2 ) ;
                     total_stat2_highdphi_events[si] += pow( stat_highdphi * highdphi_val, 2 ) ;
                  }
               } // si
            } // bi_nb

            double total_syst_err2_lowdphi(0.) ;
            double total_syst_err2_highdphi(0.) ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_err2_lowdphi += pow( total_syst_lowdphi_events[si], 2.)   ;
               total_syst_err2_highdphi += pow( total_syst_highdphi_events[si], 2.)  ;
               if (verb) printf(" syst %2d : %25s :   %7.1f  %7.1f\n", si, systerr_name[si], total_syst_lowdphi_events[si], total_syst_highdphi_events[si] ) ;
            }
            double total_stat_err2_lowdphi(0.) ;
            double total_stat_err2_highdphi(0.) ;
            for ( int si=0; si<n_staterr; si++ ) {
               total_stat_err2_lowdphi +=  total_stat2_lowdphi_events[si]  ;
               total_stat_err2_highdphi +=  total_stat2_highdphi_events[si]  ;
               if (verb) printf(" stat %2d : %25s :   %7.1f  %7.1f\n", si, staterr_name[si], sqrt(total_stat2_lowdphi_events[si]), sqrt(total_stat2_highdphi_events[si]) ) ;
            }
            double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;
            double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;
            double total_stat_err_lowdphi = sqrt( nbsum_lowdphi_err2 + total_stat_err2_lowdphi ) ;
            double total_stat_err_highdphi = sqrt( nbsum_highdphi_err2 + total_stat_err2_highdphi ) ;

            double ldp_syst_over_n(0.5) ;
            double ldp_stat_over_sqrtn(0.5) ;
            if ( nbsum_lowdphi_val > 0 ) {
               ldp_syst_over_n =  total_syst_err_lowdphi / nbsum_lowdphi_val  ;
               ldp_stat_over_sqrtn = total_stat_err_lowdphi / sqrt(nbsum_lowdphi_val) ;
            }
            double hdp_syst_over_n(0.5) ;
            double hdp_stat_over_sqrtn(0.5) ;
            if ( nbsum_highdphi_val > 0 ) {
               hdp_syst_over_n =  total_syst_err_highdphi / nbsum_highdphi_val  ;
               hdp_stat_over_sqrtn = total_stat_err_highdphi / sqrt(nbsum_highdphi_val) ;
            }

            printf( "   Nj%d-HT%d   %8.1f +/- %5.1f +/- %5.1f (%6.3f)     %8.1f +/- %5.1f +/- %5.1f (%6.3f)\n", bi_nj, bi_ht,
                  nbsum_lowdphi_val, total_stat_err_lowdphi, total_syst_err_lowdphi,  ldp_syst_over_n,
                  nbsum_highdphi_val, total_stat_err_highdphi, total_syst_err_highdphi,  hdp_syst_over_n ) ;
            fprintf( ofp_nbsum, "   Nj%d-HT%d   %8.1f +/- %5.1f +/- %5.1f    %8.1f +/- %5.1f +/- %5.1f\n", bi_nj, bi_ht,
                  nbsum_lowdphi_val, total_stat_err_lowdphi, total_syst_err_lowdphi,
                  nbsum_highdphi_val, total_stat_err_highdphi, total_syst_err_highdphi ) ;
            fprintf( ofp_nbsum_stat_syst, "   Nj%d-HT%d    %6.3f  %6.3f       %6.3f  %6.3f\n", bi_nj, bi_ht, 
                ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;

         } // bi_nj
      } // bi_ht

      fclose( ofp_nbsum ) ;
      fclose( ofp_nbsum_stat_syst ) ;
      fclose( ofp_combine_stat_syst ) ;
      printf("\n\n Wrote %s\n\n", nbsum_text_file ) ;
      printf("\n\n Wrote %s\n\n", systfile_nbsum.Data() ) ;
      printf("\n\n Wrote %s\n\n", systfile_combine.Data() ) ;



   } // make_lostlep_input_files2
   void make_signalmc_input_files1( const char* input_root_file = "outputfiles/hists-v2d-T1bbbbH.root",
                                    const char* output_text_file = "outputfiles/combine-input-T1bbbbH.txt",
                                    const char* nbsum_text_file = "outputfiles/nbsum-input-T1bbbbH.txt"
                               ) {

      setup_bins();
      gDirectory -> Delete( "h*" ) ;

      TFile* tf = new TFile( input_root_file, "read" ) ;
      if ( tf == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }
      if ( !(tf -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", input_root_file ) ; return ; }

      printf("\n") ;
      tf -> ls() ;
      printf("\n") ;

      FILE* ofp_combine ;
      if ( (ofp_combine = fopen( output_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine output file: %s\n\n", output_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum ;
      if ( (ofp_nbsum = fopen( nbsum_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum output file: %s\n\n", nbsum_text_file ) ;
         return ;
      }

      TH1F* h_ldp = (TH1F*) tf -> Get( "h_ldp" ) ;
      if ( h_ldp == 0x0 ) { printf("\n\n *** Missing h_ldp\n\n") ; return ; }

      TH1F* h_hdp = (TH1F*) tf -> Get( "h_hdp" ) ;
      if ( h_hdp == 0x0 ) { printf("\n\n *** Missing h_hdp\n\n") ; return ; }

      int bi_hist(0) ;


      for ( int bi_ht=1; bi_ht<=nb_ht[1]; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

            double ldp_nbsum_val(0.) ;
            double ldp_nbsum_err2(0.) ;

            double hdp_nbsum_val(0.) ;
            double hdp_nbsum_err2(0.) ;

            for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {

               bi_hist = (bi_nj-1)*(nb_nb)*(nb_htmht) + (bi_nb-1)*(nb_htmht) + bi_ht ;

               double ldp_val = h_ldp -> GetBinContent( bi_hist ) ;

               double hdp_val = h_hdp -> GetBinContent( bi_hist ) ;

               ldp_nbsum_val += ldp_val ;
               hdp_nbsum_val += hdp_val ;

               TString hist_bin_label( h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               char label[1000] ;
               sprintf( label, " %3d  Nj%d-Nb%d-MHTC-HT%d", bi_hist, bi_nj, bi_nb-1, bi_ht ) ;

       ////    printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;

            } // bi_nb

            printf( "   Nj%d-HT%d   %8.1f      %8.1f\n", 
                bi_nj, bi_ht, ldp_nbsum_val, hdp_nbsum_val ) ;

            fprintf( ofp_nbsum, "   Nj%d-HT%d   %8.1f      %8.1f\n", 
                bi_nj, bi_ht, ldp_nbsum_val, hdp_nbsum_val ) ;


         } // bi_nj
      } // bi_ht


      fclose( ofp_nbsum ) ;
      printf("\n\n Wrote %s\n\n", nbsum_text_file ) ;





     //----------
      bi_hist = 0 ;
      int bi_control(0) ;
      int bi_search(0) ;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               bi_hist ++ ;

               double ldp_val = h_ldp -> GetBinContent( bi_hist ) ;

               double hdp_val = h_hdp -> GetBinContent( bi_hist ) ;


               TString hist_bin_label( h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               int bi_ht, bi_mht ;

               if ( bi_htmht == 1 ) { bi_ht = 1; bi_mht = 1; }
               if ( bi_htmht == 2 ) { bi_ht = 2; bi_mht = 1; }
               if ( bi_htmht == 3 ) { bi_ht = 3; bi_mht = 1; }

               if ( bi_htmht == 4 ) { bi_ht = 1; bi_mht = 2; }
               if ( bi_htmht == 5 ) { bi_ht = 2; bi_mht = 2; }
               if ( bi_htmht == 6 ) { bi_ht = 3; bi_mht = 2; }

               if ( bi_htmht == 7 ) { bi_ht = 1; bi_mht = 3; }
               if ( bi_htmht == 8 ) { bi_ht = 2; bi_mht = 3; }
               if ( bi_htmht == 9 ) { bi_ht = 3; bi_mht = 3; }

               if ( bi_htmht ==10 ) { bi_ht = 2; bi_mht = 4; }
               if ( bi_htmht ==11 ) { bi_ht = 3; bi_mht = 4; }

               if ( bi_htmht ==12 ) { bi_ht = 2; bi_mht = 5; }
               if ( bi_htmht ==13 ) { bi_ht = 3; bi_mht = 5; }

               if ( bi_mht == 1 ) {
                  bi_control ++ ;
               } else {
                  bi_search ++ ;
               }

               char mhtchar[10] ;
               if ( bi_mht == 1 ) {
                  sprintf( mhtchar, "C" ) ;
               } else {
                  sprintf( mhtchar, "%d", bi_mht-1 ) ;
               }

               char label[1000] ;
               sprintf( label, " %3d  %s %3d  Nj%d-Nb%d-MHT%s-HT%d",
                   bi_hist, (bi_mht==1)?"C":"S", (bi_mht==1)?bi_control:bi_search,
                   bi_nj, bi_nb-1, mhtchar, bi_ht ) ;

               //// printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;


               printf(               "%s      %8.1f          %8.1f\n", label,    ldp_val,    hdp_val ) ;

               fprintf( ofp_combine, "%s      %8.1f          %8.1f\n", label,    ldp_val,    hdp_val ) ;


            } // bi_htmht
         } // bi_nb
      } // bi_nj

      fclose( ofp_combine ) ;
      printf("\n\n Wrote %s\n\n", output_text_file ) ;

     //---------


   } // make_signalmc_input_files1
   void make_znunu_input_files1( const char* ldp_input_root_file = "non-qcd-inputs-topup2/ZinvHistos_ldp.root",
                                 const char* hdp_input_root_file = "non-qcd-inputs-topup2/ZinvHistos_hdp.root",
                                 const char* output_text_file = "outputfiles/combine-input-znunu.txt",
                                 const char* nbsum_text_file  = "outputfiles/nbsum-input-znunu.txt"
                               ) {
      setup_bins();


      gDirectory -> Delete( "h*" ) ;

      TFile* tf_ldp = new TFile( ldp_input_root_file, "read" ) ;
      if ( tf_ldp == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", ldp_input_root_file ) ; return ; }
      if ( !(tf_ldp -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", ldp_input_root_file ) ; return ; }

      TFile* tf_hdp = new TFile( hdp_input_root_file, "read" ) ;
      if ( tf_hdp == 0x0 ) { printf("\n\n *** Bad input file: %s\n\n", hdp_input_root_file ) ; return ; }
      if ( !(tf_hdp -> IsOpen() ) ) { printf("\n\n *** Bad input file: %s\n\n", hdp_input_root_file ) ; return ; }

      printf("\n") ;
      tf_ldp -> ls() ;
      printf("\n") ;

      FILE* ofp_combine ;
      if ( (ofp_combine = fopen( output_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine output file: %s\n\n", output_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum ;
      if ( (ofp_nbsum = fopen( nbsum_text_file, "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum output file: %s\n\n", nbsum_text_file ) ;
         return ;
      }

      FILE* ofp_nbsum_stat_syst ;
      TString systfile_nbsum( nbsum_text_file ) ;
      systfile_nbsum.ReplaceAll("input","stat-syst") ;
      if ( (ofp_nbsum_stat_syst = fopen( systfile_nbsum.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening nbsum stat-syst output file: %s\n\n", systfile_nbsum.Data() ) ;
         return ;
      }

      FILE* ofp_combine_stat_syst ;
      TString systfile_combine( output_text_file ) ;
      systfile_combine.ReplaceAll("input","stat-syst") ;
      if ( (ofp_combine_stat_syst = fopen( systfile_combine.Data(), "w" ))==NULL ) {
         printf( "\n\n *** Problem opening combine stat-syst output file: %s\n\n", systfile_combine.Data() ) ;
         return ;
      }


      TH1F* h_ldp_nonzero = (TH1F*) tf_ldp -> Get( "ZinvBGpred" ) ;
      if ( h_ldp_nonzero == 0x0 ) { printf("\n\n *** Missing ZinvBGpred\n\n") ; return ; }

      TH1F* h_hdp_nonzero = (TH1F*) tf_hdp -> Get( "ZinvBGpred" ) ;
      if ( h_hdp_nonzero == 0x0 ) { printf("\n\n *** Missing ZinvBGpred\n\n") ; return ; }



      TH1F* h_ldp_zero = (TH1F*) tf_ldp -> Get( "ZinvBG0EVpred" ) ;
      if ( h_ldp_zero == 0x0 ) { printf("\n\n *** Missing ZinvBG0EVpred\n\n") ; return ; }

      TH1F* h_hdp_zero = (TH1F*) tf_hdp -> Get( "ZinvBG0EVpred" ) ;
      if ( h_hdp_zero == 0x0 ) { printf("\n\n *** Missing ZinvBG0EVpred\n\n") ; return ; }

      TH1F* h_ldp = (TH1F*) h_ldp_nonzero -> Clone( "h_ldp" ) ;
      h_ldp -> Add( h_ldp_zero ) ;

      TH1F* h_hdp = (TH1F*) h_hdp_nonzero -> Clone( "h_hdp" ) ;
      h_hdp -> Add( h_hdp_zero ) ;

      TH1* h_systerr_lowdphi[100] ;
      TH1* h_systerr_highdphi[100] ;
      char systerr_name[100][100] ;
      int n_systerr(0) ;

      {
         int si(0) ;

         TH1* h_syst_ldp_nonzero ;
         TH1* h_syst_ldp_zero ;
         TH1* h_syst_ldp ;

         TH1* h_syst_hdp_nonzero ;
         TH1* h_syst_hdp_zero ;
         TH1* h_syst_hdp ;

        //--- Note: These are in events.

         h_syst_ldp_nonzero = get_hist( tf_ldp, "ZinvBGsysUp" ) ;
         h_syst_ldp_zero = get_hist( tf_ldp, "ZinvBG0EVsysUp" ) ;
         h_syst_ldp = (TH1*) h_syst_ldp_nonzero -> Clone( "ZinvBGsysUp_combined" ) ;
         h_syst_ldp -> Add( h_syst_ldp_zero ) ;

         h_systerr_lowdphi[si] = h_syst_ldp ;

         h_syst_hdp_nonzero = get_hist( tf_hdp, "ZinvBGsysUp" ) ;
         h_syst_hdp_zero = get_hist( tf_hdp, "ZinvBG0EVsysUp" ) ;
         h_syst_hdp = (TH1*) h_syst_hdp_nonzero -> Clone( "ZinvBGsysUp_combined" ) ;
         h_syst_hdp -> Add( h_syst_hdp_zero ) ;

         h_systerr_highdphi[si] = h_syst_hdp ;


         sprintf( systerr_name[si], "Total" ) ;
         si++ ;

         n_systerr = si ;
      }







      int bi_hist(0) ;
      int bi_control(0) ;
      int bi_search(0) ;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=1; bi_htmht<=nb_htmht; bi_htmht++ ) {

               if ( bin_edges_nj[bi_nj-1] < 2.5 ) continue; // because znuznu input file lasks NJets = 2 bin
                  bi_hist ++ ;

               double ldp_val = h_ldp -> GetBinContent( bi_hist ) ;
               double ldp_hist_err = h_ldp -> GetBinError( bi_hist ) ;

               double hdp_val = h_hdp -> GetBinContent( bi_hist ) ;
               double hdp_hist_err = h_hdp -> GetBinError( bi_hist ) ;

               double total_syst_lowdphi_events[100] ;
               double total_syst_highdphi_events[100] ;
               for ( int si=0; si<n_systerr; si++ ) {
                  total_syst_lowdphi_events[si]  = 0 ;
                  total_syst_highdphi_events[si] = 0 ;
               }
               for ( int si=0; si<n_systerr; si++ ) {
                  ////////////////double syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  ////////////////double syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  double syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) ;
                  double syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) ;
                  ///////printf(" DEBUG1 syst hist content:  ldp %9.3f  hdp %9.3f\n", 
                      ///////h_systerr_lowdphi[si] -> GetBinContent( bi_hist ),
                      ///////h_systerr_highdphi[si] -> GetBinContent( bi_hist ) ) ;
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst_lowdphi_events[si]  += syst_lowdphi  ;
                     total_syst_highdphi_events[si] += syst_highdphi  ;
                  }
               } // si
               double total_syst_err2_lowdphi(0.) ;
               double total_syst_err2_highdphi(0.) ;
               for ( int si=0; si<n_systerr; si++ ) {
                  total_syst_err2_lowdphi += pow( total_syst_lowdphi_events[si], 2.)   ;
                  total_syst_err2_highdphi += pow( total_syst_highdphi_events[si], 2.)  ;
                  //////printf("  %2d : %25s :   %7.1f  %7.1f\n", si, systerr_name[si], total_syst_lowdphi_events[si], total_syst_highdphi_events[si] ) ;
               }
               double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;
               double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;
               //////////printf("  DEBUG2 syst ldp %9.3f   syst hdp %9.3f\n", total_syst_err_lowdphi, total_syst_err_highdphi ) ;

               TString hist_bin_label( h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               int bi_ht = 0, bi_mht = 0;

               if ( bi_htmht == 1 ) { bi_ht = 1; bi_mht = 1; }
               if ( bi_htmht == 2 ) { bi_ht = 2; bi_mht = 1; }
               if ( bi_htmht == 3 ) { bi_ht = 3; bi_mht = 1; }

               if ( bi_htmht == 4 ) { bi_ht = 1; bi_mht = 2; }
               if ( bi_htmht == 5 ) { bi_ht = 2; bi_mht = 2; }
               if ( bi_htmht == 6 ) { bi_ht = 3; bi_mht = 2; }

               if ( bi_htmht == 7 ) { bi_ht = 1; bi_mht = 3; }
               if ( bi_htmht == 8 ) { bi_ht = 2; bi_mht = 3; }
               if ( bi_htmht == 9 ) { bi_ht = 3; bi_mht = 3; }

               /////////////////if ( bi_htmht ==10 ) { bi_ht = 1; bi_mht = 4; }
               /////////////////if ( bi_htmht ==11 ) { bi_ht = 2; bi_mht = 4; }

               /////////////////if ( bi_htmht ==12 ) { bi_ht = 1; bi_mht = 5; }
               /////////////////if ( bi_htmht ==13 ) { bi_ht = 2; bi_mht = 5; }

               if ( bi_htmht ==10 ) { bi_ht = 2; bi_mht = 4; }
               if ( bi_htmht ==11 ) { bi_ht = 3; bi_mht = 4; }

               if ( bi_htmht ==12 ) { bi_ht = 2; bi_mht = 5; }
               if ( bi_htmht ==13 ) { bi_ht = 3; bi_mht = 5; }

               if ( bi_mht == 1 ) {
                  bi_control ++ ;
               } else {
                  bi_search ++ ;
               }

               char mhtchar[10] ;
               if ( bi_mht == 1 ) {
                  sprintf( mhtchar, "C" ) ;
               } else {
                  sprintf( mhtchar, "%d", bi_mht-1 ) ;
               }

               char label[1000] ;
               sprintf( label, " %3d  %s %3d  Nj%d-Nb%d-MHT%s-HT%d",
                   bi_hist, (bi_mht==1)?"C":"S", (bi_mht==1)?bi_control:bi_search,
                   bi_nj, bi_nb-1, mhtchar, bi_ht ) ;

               //// printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;

               double ldp_stat_over_sqrtn(0.8), ldp_syst_over_n(0.20) ;
               if ( ldp_val > 0 ) {
                  ldp_stat_over_sqrtn = ldp_hist_err / sqrt( ldp_val ) ;
                  if ( total_syst_err_lowdphi > 0 ) ldp_syst_over_n = total_syst_err_lowdphi / ldp_val ;
               }
               double hdp_stat_over_sqrtn(0.8), hdp_syst_over_n(0.20) ;
               if ( hdp_val > 0 ) {
                  hdp_stat_over_sqrtn = hdp_hist_err / sqrt( hdp_val ) ;
                  if ( total_syst_err_highdphi ) hdp_syst_over_n = total_syst_err_highdphi / hdp_val ;
               }

               printf(               "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, ldp_hist_err, total_syst_err_lowdphi,   hdp_val, hdp_hist_err, total_syst_err_highdphi ) ;

               fprintf( ofp_combine, "%s      %8.1f +/- %5.1f +/- %5.1f         %8.1f +/- %5.1f +/- %5.1f\n",
                   label,    ldp_val, ldp_hist_err, total_syst_err_lowdphi,   hdp_val, hdp_hist_err, total_syst_err_highdphi ) ;

               if ( bi_htmht > 3 ) {
                  char this_label[100] ;
                  sprintf( this_label, "%3d S-Nj%d-Nb%d-MHT%d-HT%d (S%d)", bi_search, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht-3 ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               } else {
                  char this_label[100] ;
                  sprintf( this_label, "%3d C-Nj%d-Nb%d-MHT%d-HT%d (C%d)", bi_control, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht ) ;
                  fprintf( ofp_combine_stat_syst, "   %-30s   %6.3f  %6.3f       %6.3f  %6.3f\n",
                      this_label,
                      ldp_stat_over_sqrtn, ldp_syst_over_n,   hdp_stat_over_sqrtn, hdp_syst_over_n ) ;
               }


            } // bi_htmht
         } // bi_nb
      } // bi_nj

      fclose( ofp_combine ) ;
      printf("\n\n Wrote %s\n\n", output_text_file ) ;

     //-----

      for ( int bi_ht=1; bi_ht<=nBinsHT; bi_ht++ ) {
         for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {

            if ( bin_edges_nj[bi_nj-1] < 2.5 ) continue; // because znuznu input file lasks NJets = 2 bin

            float nbsum_lowdphi_val(0.) ;
            float nbsum_lowdphi_err2(0.) ;

            float nbsum_highdphi_val(0.) ;
            float nbsum_highdphi_err2(0.) ;

            double total_syst_lowdphi_events[100] ;
            double total_syst_highdphi_events[100] ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_lowdphi_events[si]  = 0 ;
               total_syst_highdphi_events[si] = 0 ;
            }

            for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {

               bi_hist = (bi_nj-1)*(nb_nb)*(nb_htmht) + (bi_nb-1)*(nb_htmht) + bi_ht ;

               double ldp_val = h_ldp -> GetBinContent( bi_hist ) ;
               double ldp_hist_err = h_ldp -> GetBinError( bi_hist ) ;

               double hdp_val = h_hdp -> GetBinContent( bi_hist ) ;
               double hdp_hist_err = h_hdp -> GetBinError( bi_hist ) ;


               nbsum_lowdphi_val += ldp_val ;
               nbsum_highdphi_val += hdp_val ;

               nbsum_lowdphi_err2 += pow( ldp_hist_err, 2. ) ;
               nbsum_highdphi_err2 += pow( hdp_hist_err, 2. ) ;

               for ( int si=0; si<n_systerr; si++ ) {
                  ////////double syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  ////////double syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) - 1. ;
                  double syst_lowdphi = h_systerr_lowdphi[si] -> GetBinContent( bi_hist )  ;
                  double syst_highdphi = h_systerr_highdphi[si] -> GetBinContent( bi_hist ) ;
                  if ( syst_lowdphi > 0 && syst_highdphi > 0 ) {
                     total_syst_lowdphi_events[si]  += syst_lowdphi  ;
                     total_syst_highdphi_events[si] += syst_highdphi  ;
                  }
               } // si


               TString hist_bin_label( h_ldp -> GetXaxis() -> GetBinLabel( bi_hist ) ) ;

               char label[1000] ;
               sprintf( label, " %3d  Nj%d-Nb%d-MHTC-HT%d", bi_hist, bi_nj, bi_nb-1, bi_ht ) ;

       ////    printf("  label : %s   ,  hist label %s\n", label, hist_bin_label.Data() ) ;


            } // bi_nb

            double total_syst_err2_lowdphi(0.) ;
            double total_syst_err2_highdphi(0.) ;
            for ( int si=0; si<n_systerr; si++ ) {
               total_syst_err2_lowdphi += pow( total_syst_lowdphi_events[si], 2.)   ;
               total_syst_err2_highdphi += pow( total_syst_highdphi_events[si], 2.)  ;
               //printf("  %2d : %25s :   %7.1f  %7.1f\n", si, systerr_name[si], total_syst_lowdphi_events[si], total_syst_highdphi_events[si] ) ;
            }
            double total_syst_err_lowdphi = sqrt( total_syst_err2_lowdphi ) ;
            double total_syst_err_highdphi = sqrt( total_syst_err2_highdphi ) ;

            double ldp_syst_over_n(0.2) ;
            double ldp_stat_over_sqrtn(0.8) ;
            if ( nbsum_lowdphi_val > 0 ) {
               ldp_syst_over_n =  total_syst_err_lowdphi / nbsum_lowdphi_val  ;
               ldp_stat_over_sqrtn = 1. ;
            }
            double hdp_syst_over_n(0.2) ;
            double hdp_stat_over_sqrtn(0.8) ;
            if ( nbsum_highdphi_val > 0 ) {
               hdp_syst_over_n =  total_syst_err_highdphi / nbsum_highdphi_val  ;
               hdp_stat_over_sqrtn = 1. ;
            }
            printf( "   Nj%d-HT%d   %8.1f +/- %5.1f +/- %5.1f  (%6.3f)      %8.1f +/- %5.1f +/- %5.1f  (%6.3f)\n", bi_nj, bi_ht,
                  nbsum_lowdphi_val, sqrt(nbsum_lowdphi_err2), total_syst_err_lowdphi,  ldp_syst_over_n,
                  nbsum_highdphi_val, sqrt(nbsum_highdphi_err2), total_syst_err_highdphi,  hdp_syst_over_n ) ;
            fprintf( ofp_nbsum, "   Nj%d-HT%d   %8.1f +/- %5.1f +/- %5.1f    %8.1f +/- %5.1f +/- %5.1f\n", bi_nj, bi_ht,
                  nbsum_lowdphi_val, sqrt(nbsum_lowdphi_err2), total_syst_err_lowdphi,
                  nbsum_highdphi_val, sqrt(nbsum_highdphi_err2), total_syst_err_highdphi ) ;
            fprintf( ofp_nbsum_stat_syst, "   Nj%d-HT%d    %6.3f  %6.3f      %6.3f  %6.3f\n", bi_nj, bi_ht,
                   ldp_stat_over_sqrtn, ldp_syst_over_n,      hdp_stat_over_sqrtn, hdp_syst_over_n ) ;

         } // bi_nj
      } // bi_ht

      fclose( ofp_nbsum ) ;
      fclose( ofp_nbsum_stat_syst ) ;
      printf("\n\n Wrote %s\n\n", nbsum_text_file ) ;
      printf("\n\n Wrote %s\n\n", systfile_nbsum.Data() ) ;
      printf("\n\n Wrote %s\n\n", systfile_combine.Data() ) ;



   } // make_znunu_input_files1
   void create_model_ratio_hist1( const char* model_pars_file = "outputfiles/model-pars-qcdmc3.txt",
                                  const char* qcd_ratio_file = "outputfiles/qcdmc-ratio-v3.root" ) {
      setup_bins(); 
      gDirectory -> Delete( "h*" ) ;

      loadHist( qcd_ratio_file, "qcdmc" ) ;

      read_pars( model_pars_file ) ;

      TH1F* h_ratio_all = new TH1F( "h_ratio_all", "QCD model H/L ratio", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;

      TH1F* h_max_ldp_weight_search_bins = get_hist( "h_max_ldp_weight_search_bins_qcdmc" ) ;
      TH1F* h_ldp_search_bins = get_hist( "h_ldp_search_bins_qcdmc" ) ;
      TH1F* h_hdp_search_bins = get_hist( "h_hdp_search_bins_qcdmc" ) ;
      TH1F* h_ratio_qcdmc = get_hist( "h_ratio_qcdmc" ) ;

      int bi_hist_with_exclusion(0) ;

      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=4; bi_htmht<=nb_htmht; bi_htmht++ ) {
               
               if ( is_this_bin_excluded(bi_nj-1, bi_nb-1, bi_htmht-1) ) continue;

	       bi_hist_with_exclusion++;  // these few lines should be changed when we update the code that produces qcdmc-ratio-v3.root

               int bi_ht, bi_mht ;
               htmht_bin_to_ht_and_mht_bins( bi_htmht, bi_ht, bi_mht ) ;

               char label[100] ;
               sprintf( label, " %3d Nj%d-Nb%d-MHT%d-HT%d (%d)", bi_hist_with_exclusion, bi_nj, bi_nb-1, bi_mht-1, bi_ht, bi_htmht-3 ) ;

               double model_ratio_val = 0;
               double model_ratio_err = 0;

                  model_ratio_val = par_val_ht[bi_ht] * par_val_njet[bi_nj] * par_val_ht_mht[bi_ht][bi_mht] * par_val_nb[bi_nb] ;
                  model_ratio_err = model_ratio_val * sqrt(
                         pow( par_err_ht_fit[bi_ht]/par_val_ht[bi_ht], 2. )
                      +  pow( par_err_ht_syst[bi_ht]/par_val_ht[bi_ht], 2. )
                      +  pow( par_err_njet_fit[bi_nj]/par_val_njet[bi_nj], 2. )
                      +  pow( par_err_njet_syst[bi_nj]/par_val_njet[bi_nj], 2. )
                      +  pow( par_err_ht_mht[bi_ht][bi_mht]/par_val_ht_mht[bi_ht][bi_mht], 2. )
                      +  pow( par_err_nb[bi_nb]/par_val_nb[bi_nb], 2. )
                    ) ;
                  printf("  %s : Nj %6.4f Nb %6.4f MHT %6.4f HT %6.4f  model ratio = %6.4f +/- %6.4f\n", label,
                    par_val_njet[bi_nj], par_val_nb[bi_nb], par_val_ht_mht[bi_ht][bi_mht], par_val_ht[bi_ht], model_ratio_val, model_ratio_err  ) ;

               h_ratio_all -> GetXaxis() -> SetBinLabel( bi_hist_with_exclusion, label ) ;

               h_ratio_all -> SetBinContent( bi_hist_with_exclusion, model_ratio_val ) ;
               h_ratio_all -> SetBinError( bi_hist_with_exclusion, model_ratio_err ) ;

            } // bi_htmht
         } // bi_nb
      } // bi_nj

      gStyle -> SetOptStat(0) ;
      gStyle -> SetPadBottomMargin(0.30) ;

      h_ratio_all -> SetMarkerStyle( 22 ) ;
      h_ratio_all -> SetMarkerColor( 2 ) ;

      h_ratio_all -> GetXaxis() -> LabelsOption("v") ;
      h_ratio_all -> Draw() ;
      gPad -> SetGridy(1) ;


     //---------------

      TH1F* h_ratio_qcdmc_minus_model = new TH1F( "h_ratio_qcdmc_minus_model", "QCD H/L ratio difference (QCD MC - model)", nb_global_after_exclusion, 0.5, nb_global_after_exclusion + 0.5 ) ;

      printf("\n\n") ;
      bi_hist_with_exclusion = 0;
      for ( int bi_nj=1; bi_nj<=nb_nj; bi_nj++ ) {
         for ( int bi_nb=1; bi_nb<=nb_nb; bi_nb++ ) {
            for ( int bi_htmht=4; bi_htmht<=nb_htmht; bi_htmht++ ) {
               if ( is_this_bin_excluded(bi_nj-1, bi_nb-1, bi_htmht-1) ) continue;
               bi_hist_with_exclusion++; // these few lines should be changed when we update the code that produces qcdmc-ratio-v3.root

	       float model_val = h_ratio_all -> GetBinContent( bi_hist_with_exclusion ) ;
               float qcdmc_val = h_ratio_qcdmc -> GetBinContent( bi_hist_with_exclusion ) ;
               float ldp_val = h_ldp_search_bins -> GetBinContent( bi_hist_with_exclusion ) ;
               float hdp_val = h_hdp_search_bins -> GetBinContent( bi_hist_with_exclusion ) ;
               float max_ldp_weight = h_max_ldp_weight_search_bins -> GetBinContent( bi_hist_with_exclusion ) ;
               char label[100] ;
               sprintf( label, "%s", h_ratio_all -> GetXaxis() -> GetBinLabel( bi_hist_with_exclusion ) ) ;
               float diff_val(0.) ;
               float diff_err(0.) ;
               printf(" debug1 : model bin label = %s , qcdmc bin label = %s\n", h_ratio_all -> GetXaxis() -> GetBinLabel( bi_hist_with_exclusion ), h_ratio_qcdmc -> GetXaxis() -> GetBinLabel( bi_hist_with_exclusion ) ) ;
               if ( hdp_val > 0 ) {
                  diff_val = qcdmc_val - model_val ;
	std::cout << qcdmc_val << " " << model_val << " " << diff_val << std::endl;
                  diff_err = diff_val ;
                  printf("  %40s : LDP %7.1f  HDP %7.1f   max LDP weight %5.3f, diff err = %5.3f\n", label, ldp_val, hdp_val, max_ldp_weight, diff_err ) ;
               } else {
                  diff_val = 0. ;
                  if ( ldp_val > 0 ) {
                     diff_err = max_ldp_weight / ldp_val ;
                     printf("  %40s : LDP %7.1f  HDP %7.1f   max LDP weight %5.3f,  zero HDP H/L err = %5.3f\n", label, ldp_val, hdp_val, max_ldp_weight, diff_err ) ;
                  } else {
                     //diff_err = 0.5 ;
                     //diff_err = 0.2;
                     diff_err = 0.0;
                     printf("  %40s : LDP %7.1f  HDP %7.1f   max LDP weight %5.3f,  *** both zero\n", label, ldp_val, hdp_val, max_ldp_weight ) ;
                  }
               }
               h_ratio_qcdmc_minus_model -> SetBinContent( bi_hist_with_exclusion, diff_val ) ;
               h_ratio_qcdmc_minus_model -> SetBinError( bi_hist_with_exclusion, diff_err ) ;
               h_ratio_qcdmc_minus_model -> GetXaxis() -> SetBinLabel( bi_hist_with_exclusion, label ) ;
            } // bi_htmht
	 }//bi_nb
      }//bi_nj

      printf("\n\n") ;

      h_ratio_qcdmc_minus_model -> GetXaxis() -> LabelsOption( "v" ) ;


      saveHist("outputfiles/model-ratio-hist1.root", "h*" ) ;

   } // create_model_ratio_hist1