void ExamineMidasFile(const char *filename) {

    std::ifstream in(filename, std::ifstream::in | std::ifstream::binary);
    if(!in.is_open()) {
        printf("unable to open file %s\n",filename);
        return;
    }
    in.seekg(0, std::ifstream::end);
    long long filesize = in.tellg();
    in.close();

    TMidasFile  mfile;
    TMidasEvent mevent;
    std::map<int,int> type_counter;

    mfile.Open(filename);

    long long starttime    = 0;
    long long stoptime     = 0;
    int currenteventnumber = 0;
    long long bytesread = 0;

    bool loop = true;

    TStopwatch sw;
    sw.Start();
    while(loop) {
        bytesread += mfile.Read(&mevent);
        switch(mevent.GetEventId()) {
        case 0x8000: //run start
            printf( DGREEN );
            mevent.Print();
            printf( RESET_COLOR );
            starttime = mevent.GetTimeStamp();
            break;
        case 0x8001: //run stop
            printf( "                                                                             \r");
            printf( DRED );
            mevent.Print();
            printf( RESET_COLOR );
            stoptime = mevent.GetTimeStamp();
            loop = false;
            break;
        default:
            type_counter[mevent.GetEventId()]++;
            break;
        }
        if((++currenteventnumber%15000)== 0) {
            printf( " Looping over event %i have looped %.2fMB/%.2f MB => %.1f MB/s              "  "\r",
                    currenteventnumber,(bytesread/1000000.0),(filesize/1000000.0),(bytesread/1000000.0)/sw.RealTime());
            sw.Continue();
        }
    }
    printf( " Looping over event %i have looped %.2fMB/%.2f MB => %.1f MB/s              "  "\r",
            currenteventnumber,(bytesread/1000000.0),(filesize/1000000.0),(bytesread/1000000.0)/sw.RealTime());
    printf("\n\n");
    printf("EventTypes Seen: \n");
    for(std::map<int,int>::iterator it=type_counter.begin(); it!=type_counter.end(); it++) {
        printf("\tEventId[%i]  =  %i\n",it->first,it->second);
    }
    printf("\n");
    printf("Run lenght =  %i  seconds\n",stoptime-starttime);
    printf("\n");
    return;
}
void fill_hists_loop_v2c::Loop( bool verb, int nloop )
{

   bool islostlep(false) ;
   bool ishadtau(false) ;
   bool isqcd(false) ;
   TString ts_samplename( samplename ) ;

   if ( ts_samplename.Contains("lostlep") ) {
      printf("\n\n This is a lost lepton run through.\n\n") ;
      islostlep = true ;
   }
   if ( ts_samplename.Contains("hadtau") ) {
      printf("\n\n This is a had tau run through.\n\n") ;
      ishadtau = true ;
   }
   if ( ts_samplename.Contains("qcd") ) {
      printf("\n\n This is a qcd run through.\n\n") ;
      isqcd = true ;
   }

   gDirectory -> Delete( "h*" ) ;

   if ( isqcd ) loadHist( "outputfiles/data-turnon.root" ) ;

   TH1F* h_turnon[5] ;
   if ( isqcd ) {
      h_turnon[1] = (TH1F*) gDirectory -> FindObject( "h_eff_nj1" ) ;
      h_turnon[2] = (TH1F*) gDirectory -> FindObject( "h_eff_nj2" ) ;
      h_turnon[3] = (TH1F*) gDirectory -> FindObject( "h_eff_nj3" ) ;
      h_turnon[4] = (TH1F*) gDirectory -> FindObject( "h_eff_nj4" ) ;
      if ( h_turnon[1] == 0x0 ) { printf("\n\n *** missing nj1 turnon.\n\n") ; return ; }
      if ( h_turnon[2] == 0x0 ) { printf("\n\n *** missing nj2 turnon.\n\n") ; return ; }
      if ( h_turnon[3] == 0x0 ) { printf("\n\n *** missing nj3 turnon.\n\n") ; return ; }
      if ( h_turnon[4] == 0x0 ) { printf("\n\n *** missing nj4 turnon.\n\n") ; return ; }
   }

   setup_bins() ;

   if (fChain == 0) return;


   TH1F* h_hdp = new TH1F( "h_hdp", "HDP events", nb_global, 0.5, nb_global + 0.5 ) ; h_hdp -> Sumw2() ;
   set_bin_labels_div_by_nb( h_hdp ) ;
   TH1F* h_nbsum_hdp = new TH1F( "h_nbsum_hdp", "HDP events", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nbsum_hdp -> Sumw2() ;
   set_bin_labels( h_nbsum_hdp ) ;
   TH1F* h_nb0_hdp = new TH1F( "h_nb0_hdp", "HDP events, Nb=0", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb0_hdp -> Sumw2() ;
   set_bin_labels( h_nb0_hdp ) ;
   TH1F* h_nb1_hdp = new TH1F( "h_nb1_hdp", "HDP events, Nb=1", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb1_hdp -> Sumw2() ;
   set_bin_labels( h_nb1_hdp ) ;
   TH1F* h_nb2_hdp = new TH1F( "h_nb2_hdp", "HDP events, Nb=2", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb2_hdp -> Sumw2() ;
   set_bin_labels( h_nb2_hdp ) ;
   TH1F* h_nb3_hdp = new TH1F( "h_nb3_hdp", "HDP events, Nb=3", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb3_hdp -> Sumw2() ;
   set_bin_labels( h_nb3_hdp ) ;
   TH1F* h_mhtc_hdp = new TH1F( "h_mhtc_hdp", "HDP events, MHTC", nb_nj*nb_nb*nb_ht[1], 0.5, nb_nj*nb_nb*nb_ht[1]+0.5 ) ; h_mhtc_hdp -> Sumw2() ;
   set_bin_labels_mhtc_plot( h_mhtc_hdp ) ;
   TH1F* h_mhtc_nbsum_hdp = new TH1F( "h_mhtc_nbsum_hdp", "HDP events, MHTC, Nbsum", nb_nj*nb_ht[1], 0.5, nb_nj*nb_ht[1]+0.5 ) ; h_mhtc_nbsum_hdp -> Sumw2() ;
   set_bin_labels_mhtc_nbsum_plot( h_mhtc_nbsum_hdp ) ;

   TH1F* h_ldp = new TH1F( "h_ldp", "ldp events", nb_global, 0.5, nb_global + 0.5 ) ; h_ldp -> Sumw2() ;
   set_bin_labels_div_by_nb( h_ldp ) ;
   TH1F* h_nbsum_ldp = new TH1F( "h_nbsum_ldp", "LDP events", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nbsum_ldp -> Sumw2() ;
   set_bin_labels( h_nbsum_ldp ) ;
   TH1F* h_nb0_ldp = new TH1F( "h_nb0_ldp", "ldp events, Nb=0", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb0_ldp -> Sumw2() ;
   set_bin_labels( h_nb0_ldp ) ;
   TH1F* h_nb1_ldp = new TH1F( "h_nb1_ldp", "ldp events, Nb=1", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb1_ldp -> Sumw2() ;
   set_bin_labels( h_nb1_ldp ) ;
   TH1F* h_nb2_ldp = new TH1F( "h_nb2_ldp", "ldp events, Nb=2", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb2_ldp -> Sumw2() ;
   set_bin_labels( h_nb2_ldp ) ;
   TH1F* h_nb3_ldp = new TH1F( "h_nb3_ldp", "ldp events, Nb=3", nb_global/nb_nb, 0.5, nb_global/nb_nb + 0.5 ) ; h_nb3_ldp -> Sumw2() ;
   set_bin_labels( h_nb3_ldp ) ;
   TH1F* h_mhtc_ldp = new TH1F( "h_mhtc_ldp", "ldp events, MHTC", nb_nj*nb_nb*nb_ht[1], 0.5, nb_nj*nb_nb*nb_ht[1]+0.5 ) ; h_mhtc_ldp -> Sumw2() ;
   set_bin_labels_mhtc_plot( h_mhtc_ldp ) ;
   TH1F* h_mhtc_nbsum_ldp = new TH1F( "h_mhtc_nbsum_ldp", "ldp events, MHTC, Nbsum", nb_nj*nb_ht[1], 0.5, nb_nj*nb_ht[1]+0.5 ) ; h_mhtc_nbsum_ldp -> Sumw2() ;
   set_bin_labels_mhtc_nbsum_plot( h_mhtc_nbsum_ldp ) ;

   TH1F* h_jet_eta_badmu = new TH1F( "h_jet_eta_badmu", "Jet eta, bad muon", 55, -5.5, 5.5 ) ;

   TH1F* h_mht_all = new TH1F( "h_mht_all", "MHT, all events", 100, 0., 1000. ) ; h_mht_all -> Sumw2() ;
   TH1F* h_mht_badmu = new TH1F( "h_mht_badmu", "MHT, badmu", 100, 0., 1000. ) ; h_mht_badmu -> Sumw2() ;
   TH1F* h_mht_met100_calomet80_rejected = new TH1F( "h_mht_met100_calomet80_rejected", "MHT, MET<100 or CaloMET<80", 100, 0., 1000. ) ; h_mht_met100_calomet80_rejected -> Sumw2() ;
   TH1F* h_mht_filters = new TH1F( "h_mht_filters", "MHT, all rejected", 100, 0., 1000. ) ; h_mht_filters -> Sumw2() ;
   TH1F* h_mht_allrejected = new TH1F( "h_mht_allrejected", "MHT, all rejected", 100, 0., 1000. ) ; h_mht_allrejected -> Sumw2() ;

   TH1F* h_met_over_calomet_all = new TH1F( "h_met_over_calomet_all", "MET/CaloMET, all events", 100., 0., 10. ) ; h_met_over_calomet_all -> Sumw2() ;
   TH1F* h_met_over_calomet_badmu = new TH1F( "h_met_over_calomet_badmu", "MET/CaloMET, badmu", 100., 0., 10. ) ; h_met_over_calomet_badmu -> Sumw2() ;
   TH1F* h_met_over_calomet_met100_calomet80_rejected = new TH1F( "h_met_over_calomet_met100_calomet80_rejected", "MET/CaloMET, MET<100 or CaloMET<80", 100., 0., 10. ) ; h_met_over_calomet_met100_calomet80_rejected -> Sumw2() ;
   TH1F* h_met_over_calomet_filters = new TH1F( "h_met_over_calomet_filters", "MET/CaloMET, all rejected", 100., 0., 10. ) ; h_met_over_calomet_filters -> Sumw2() ;
   TH1F* h_met_over_calomet_allrejected = new TH1F( "h_met_over_calomet_allrejected", "MET/CaloMET, all rejected", 100., 0., 10. ) ; h_met_over_calomet_allrejected -> Sumw2() ;

   TH1F* h_mdp_all = new TH1F( "h_mdp_all", "Min Delta phi, 4 leading jets", 64, 0., 3.2 ) ;

   TH1F* h_mht = new TH1F( "h_mht", "MHT", 80, 150., 350. ) ;
   TH2F* h_mht_vs_met = new TH2F( "h_mht_vs_met", "MHT vs MET", 100, 0., 400., 100, 0., 400. ) ;
   TH2F* h_mht_vs_calomet = new TH2F( "h_mht_vs_calomet", "MHT vs Calo MET", 100, 0., 400., 100, 0., 400. ) ;
   TH2F* h_met_vs_calomet = new TH2F( "h_met_vs_calomet", "MET vs Calo MET", 100, 0., 400., 100, 0., 400. ) ;
   TH1F* h_mht_met_gt_mht_minus_100 = new TH1F( "h_mht_met_gt_mht_minus_100", "MHT, MET>(MHT-100)", 80, 150., 350. ) ;
   TH1F* h_mht_met_gt_mht_minus_100_calomet80 = new TH1F( "h_mht_met_gt_mht_minus_100_calomet80", "MHT, MET>(MHT-100), CaloMET>80", 80, 150., 350. ) ;
   TH1F* h_mht_calomet80_pfmet100 = new TH1F( "h_mht_calomet80_pfmet100", "MHT, CaloMET>80, pfmet>100", 80, 150., 350. ) ;

   TH1F* h_calomet = new TH1F( "h_calomet", "Calomet", 100, 0., 400. ) ;
   TH1F* h_met = new TH1F( "h_met", "MET", 100, 0., 400. ) ;
   TH1F* h_calomet_met_gt100 = new TH1F( "h_calomet_met_gt100", "Calomet, MET>100", 100, 0., 400. ) ;
   TH1F* h_met_calomet_gt80 = new TH1F( "h_met_calomet_gt80", "MET, Calomet>80", 100, 0., 400. ) ;

   TH1F* h_ht_after_cleaning = new TH1F( "h_ht_after_cleaning", "HT, after all QCD junk cleaning", 50, 0., 5000. ) ;

   TH1F* h_ldp_weight[209] ;
   for ( int sbi=1; sbi<=208; sbi++ ) {
      char hname[100] ;
      sprintf( hname, "h_ldp_weight_bin%03d", sbi ) ;
      h_ldp_weight[sbi] = new TH1F( hname, hname, 100, 0., 2. ) ;
   }



   Long64_t nentries = fChain->GetEntries();

   printf("\n\n") ;
   printf("  Looping over sample: %s\n", samplename ) ;
   printf("  Number of entries: %lld\n\n", nentries ) ;


   Long64_t loopmax = nentries ;
   if ( nloop > 0 ) loopmax = nloop ;

   Long64_t nbytes = 0, nb = 0;

   TStopwatch sw ;
   sw.Start() ;
   int time(0) ;
   float projected_remaining(999999.) ;

   for (Long64_t jentry=0; jentry<loopmax;jentry++) {

      Long64_t ievt = jentry ;

      if ( ievt%1000 == 0 ) { // timer printing stuff
         int thistime = sw.RealTime() ;
         sw.Continue() ;
         if ( thistime < 2 ) {
            printf("   %10llu out of %10llu  (%6.1f%%) \r", ievt, nentries, 100.*ievt/(1.*nentries) ) ;
         } else {
            if ( thistime > time ) projected_remaining = (1.*thistime)/(1.*ievt)*(nentries-ievt) ;
            if ( projected_remaining < 100 ) {
               printf("   %10llu out of %10llu  (%6.1f%%)    seconds remaining %4.0f                       \r", ievt, nentries, 100.*ievt/(1.*nentries), projected_remaining ) ;
            } else if ( projected_remaining < 3600 ) {
               printf("   %10llu out of %10llu  (%6.1f%%)    time remaining     %2d:%02d   \r", ievt, nentries, 100.*ievt/(1.*nentries),
                    TMath::Nint(projected_remaining)/60, TMath::Nint(projected_remaining)%60 ) ;
            } else {
               printf("   %10llu out of %10llu  (%6.1f%%)    time remaining  %2d:%02d:%02d   \r", ievt, nentries, 100.*ievt/(1.*nentries),
                    TMath::Nint(projected_remaining)/3600, (TMath::Nint(projected_remaining)%3600)/60, TMath::Nint(projected_remaining)%60 ) ;
            }
         }
         fflush(stdout) ;
         time = thistime ;
      } // timer printing stuff


      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;

      set_bi() ;


      double hw = Weight * lumi_ ;

      if ( islostlep && hasHadTau ) continue ;
      if ( ishadtau && !hasHadTau ) continue ;

     //-- take out the trash
      bool badMuonEvent(false) ;
      bool isjunk(false) ;
      double badmu_jet_eta(-9.) ;
      for ( unsigned long ji=0; ji<Jets->size(); ji++ ) {
         if ( Jets->at(ji).Pt() < 200 ) continue ;
         if ( Jets_muonEnergyFraction->at(ji) < 0.5 ) continue ;
         double dPhi = Jets->at(ji).Phi() - METPhi ;
         if ( dPhi >  3.1415926 ) dPhi = dPhi - 2*3.14159 ;
         if ( dPhi < -3.1415926 ) dPhi = dPhi + 2*3.14159 ;
         if ( fabs( dPhi ) > 3.1415926 - 0.40 ) { 
            badMuonEvent = true ;
            badmu_jet_eta = Jets->at(ji).Eta() ;
            break ;
         }
      } // ji
      if ( badMuonEvent ) { h_jet_eta_badmu -> Fill( badmu_jet_eta, hw ) ; }
      if ( badMuonEvent ) {
       //printf("\n\n *** Bad Muon event: %lld\n", jentry ) ;
       //printf("    MET = %7.1f  ,  MHT = %7.1f ,  CaloMET = %7.1f ,   METPhi = %6.3f,   MHTPhi = %6.3f\n", MET, MHT, CaloMET, METPhi, MHTPhi ) ;
       //for ( unsigned long ji=0; ji<Jets->size(); ji++ ) {
       //   if ( Jets->at(ji).Pt() < 30 ) break ;
       //   printf(" jet %2lu :  Pt = %7.1f,  phi = %6.3f,  eta = %6.3f,  Muon fr = %5.3f\n",
       //        ji, Jets->at(ji).Pt(), Jets->at(ji).Phi(), Jets->at(ji).Eta(), Jets_muonEnergyFraction->at(ji) ) ;
       //   fflush(stdout) ;
       //} // ji
      } // badMuonEvent?



      h_mht_all -> Fill( MHT, hw ) ;
      if ( CaloMET > 0 ) h_met_over_calomet_all -> Fill( MET/CaloMET, hw ) ;

      if ( CaloMET <= 0 )  isjunk = true ;
      if ( eeBadScFilter < 1 )  isjunk = true ;
      if ( CSCTightHaloFilter < 1 )  isjunk = true ;  // branch types are different in 80X samples
      if ( HBHEIsoNoiseFilter < 1 )  isjunk = true ;  // branch types are different in 80X samples
      if ( HBHENoiseFilter < 1 )  isjunk = true ;  // branch types are different in 80X samples

      if ( isjunk ) {
         h_mht_filters -> Fill( MHT, hw ) ;
         if ( CaloMET > 0 ) h_met_over_calomet_filters -> Fill( MET/CaloMET, hw ) ;
      }

      if ( MET<100 || CaloMET<80 ) {
         h_mht_met100_calomet80_rejected -> Fill( MHT, hw ) ;
         if ( CaloMET > 0 ) {
            h_met_over_calomet_met100_calomet80_rejected -> Fill( MET/CaloMET, hw ) ;
         }
      }

      if ( badMuonEvent ) {
         h_mht_badmu -> Fill( MHT, hw ) ;
         if ( CaloMET > 0 ) h_met_over_calomet_badmu -> Fill( MET/CaloMET, hw ) ;
      }


      if ( badMuonEvent ) isjunk = true ;
      if ( MET/CaloMET > 5 )  isjunk = true ;

      if ( isjunk ) {
         h_mht_allrejected -> Fill( MHT, hw ) ;
         if ( CaloMET > 0 ) h_met_over_calomet_allrejected -> Fill( MET/CaloMET, hw ) ;
      }


      if ( isjunk ) continue ;

    //-- new baseline
      if ( NJets < 3 ) continue ;
      if ( HT < 300 ) continue ;
      ////////if ( bi_mht <= 0 ) continue ;



     //--- if this is QCD, correct for the trigger turnon
      if ( isqcd ) {
         int turnon_bin(0) ;
         float trig_eff(1.) ;
         if ( bi_nj>=1 && bi_nj<=4 ) {
            if ( MHT < (h_turnon[bi_nj] -> GetXaxis() -> GetXmax()) ) {
               turnon_bin = h_turnon[bi_nj] -> GetXaxis() -> FindBin( MHT ) ;
            } else {
               turnon_bin = h_turnon[bi_nj] -> GetNbinsX() ;
            }
         }
         trig_eff = h_turnon[bi_nj] -> GetBinContent( turnon_bin ) ;
         hw = hw * trig_eff ;
      }




      h_mht -> Fill( MHT, hw ) ;
      h_mht_vs_met -> Fill( MET, MHT, hw ) ;
      h_mht_vs_calomet -> Fill( CaloMET, MHT, hw ) ;
      h_met_vs_calomet -> Fill( CaloMET, MET, hw ) ;
      if ( MET>(MHT-100) ) h_mht_met_gt_mht_minus_100 -> Fill( MHT, hw ) ;
      if ( MET>(MHT-100) && CaloMET>80 ) h_mht_met_gt_mht_minus_100_calomet80 -> Fill( MHT, hw ) ;
      if ( CaloMET>80 && MET>100 ) h_mht_calomet80_pfmet100 -> Fill( MHT, hw ) ;
      h_met -> Fill( MET, hw ) ;
      h_calomet -> Fill( CaloMET, hw ) ;
      if ( CaloMET>80 ) h_met_calomet_gt80 -> Fill( MET, hw ) ;
      if ( MET>100 ) h_calomet_met_gt100 -> Fill( CaloMET, hw ) ;

     //******** adding these new cleanup cuts
      if ( MET<100 ) continue ;
      if ( CaloMET<80 ) continue ;
     //********


      h_ht_after_cleaning -> Fill( HT, hw ) ;


      ////////********//////////if ( MHT < 250 ) continue ;

      bool in_ldp(true) ;
      if ( DeltaPhi1 > 0.5 && DeltaPhi2 > 0.5 && DeltaPhi3 > 0.3 && DeltaPhi4 > 0.3 ) in_ldp = false ;
      char tag_ldp[10] ;
      if ( in_ldp ) { sprintf( tag_ldp, "LDP" ) ; } else { sprintf( tag_ldp, "HDP" ) ; }

      double minDeltaPhi = 99. ;
      if ( DeltaPhi1 < minDeltaPhi ) minDeltaPhi = DeltaPhi1 ;
      if ( DeltaPhi2 < minDeltaPhi ) minDeltaPhi = DeltaPhi2 ;
      if ( DeltaPhi3 < minDeltaPhi ) minDeltaPhi = DeltaPhi3 ;
      if ( DeltaPhi4 < minDeltaPhi ) minDeltaPhi = DeltaPhi4 ;

      h_mdp_all -> Fill( minDeltaPhi, hw ) ;

      int bi_mhtc_plot = (bi_nj-1)*nb_nb*(nb_ht[1]) + (bi_nb-1)*(nb_ht[1]) + bi_ht ;
      int bi_mhtc_nbsum_plot = (bi_nj-1)*(nb_ht[1]) + bi_ht ;

      if ( !in_ldp ) {
         h_hdp -> Fill( bi_global, hw ) ;
         h_nbsum_hdp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 0 ) h_nb0_hdp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 1 ) h_nb1_hdp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 2 ) h_nb2_hdp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags >= 3 ) h_nb3_hdp -> Fill( bi_nbsum_global, hw ) ;
         if ( bi_mht == 1 ) h_mhtc_hdp -> Fill( bi_mhtc_plot , hw ) ;
         if ( bi_mht == 1 ) h_mhtc_nbsum_hdp -> Fill( bi_mhtc_nbsum_plot , hw ) ;
      } else {
         h_ldp -> Fill( bi_global, hw ) ;
         if ( bi_global > 0 && bi_global < 208 ) h_ldp_weight[bi_global] -> Fill( hw ) ;
         h_nbsum_ldp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 0 ) h_nb0_ldp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 1 ) h_nb1_ldp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags == 2 ) h_nb2_ldp -> Fill( bi_nbsum_global, hw ) ;
         if ( BTags >= 3 ) h_nb3_ldp -> Fill( bi_nbsum_global, hw ) ;
         if ( bi_mht == 1 ) h_mhtc_ldp -> Fill( bi_mhtc_plot , hw ) ;
         if ( bi_mht == 1 ) h_mhtc_nbsum_ldp -> Fill( bi_mhtc_nbsum_plot , hw ) ;
      }


      if ( verb ) {
         printf("\n\n ===== RunNum %6d, LumiBlockNum %5d, EvtNum %9llu,  Weight %g\n", RunNum, LumiBlockNum, EvtNum, Weight ) ;
         printf("  Number of bins:\n") ;
         printf("     QCD:     %d HT,  %d MHT,  %2d HTMHT,   %d Njet,  %d Nb,   %3d total\n", nb_ht[1], nb_mht, nb_htmht, nb_nj, nb_nb, nb_global ) ;
         printf("  Essential event vars:\n" ) ;
         printf("    NJets = %2d,  bin %d\n", NJets, bi_nj ) ;
         printf("    Nb    = %2d,  bin %d\n", BTags, bi_nb ) ;
         printf("    MHT   = %7.1f,  bin %2d\n", MHT, bi_mht ) ;
         printf("    HT    = %7.1f,  bin %2d\n", HT, bi_ht ) ;
         printf("      htmht bin %2d\n", bi_htmht ) ;
         printf("      global bin %3d\n", bi_global ) ;
         printf("    Dphi1 = %6.3f,  Dphi1 = %6.3f, Dphi1 = %6.3f, Dphi1 = %6.3f,  %s\n", DeltaPhi1, DeltaPhi2, DeltaPhi3, DeltaPhi4, tag_ldp ) ;
      } // verb?


   } // jentry


   TH1F* h_max_ldp_weight = new TH1F( "h_max_ldp_weight", "Max ldp weight", 208, 0.5, 208.5 ) ;

   for ( int sbi=1; sbi<=208; sbi++ ) {
      float max_weight(0.) ;
      for ( int hbi=1; hbi<=h_ldp_weight[sbi]->GetNbinsX(); hbi++ ) {
         float c = h_ldp_weight[sbi] -> GetBinContent( hbi ) ;
         if ( c > 0 ) {
            max_weight = h_ldp_weight[sbi] -> GetXaxis() -> GetBinCenter( hbi ) ;
         }
      } // hbi
      h_max_ldp_weight -> SetBinContent( sbi, max_weight ) ;
      h_max_ldp_weight -> GetXaxis() -> SetBinLabel( sbi, h_ldp -> GetXaxis() -> GetBinLabel( sbi ) ) ;
   } // sbi

   h_max_ldp_weight -> SetFillColor(11) ;
   h_max_ldp_weight -> GetXaxis() -> LabelsOption( "v" ) ;



   printf("\n\n\n Done.\n\n\n") ;

   char histfile[10000] ;
   if ( strlen( samplename ) > 0 ) {
      sprintf( histfile, "outputfiles/hists-v2c-%s.root", samplename ) ;
   } else {
      sprintf( histfile, "outputfiles/hists-v2c.root" ) ;
   }
   saveHist( histfile, "h*" ) ;

} // Loop
Beispiel #3
0
void trcls(const char *fname, const char *oname, const char *tkdbc)
{
  AMSChain ch;
  TString sfn = fname;
  if (!sfn.Contains(".root")) sfn += "*.root";
  if (ch.Add(sfn) <= 0) return;

  Int_t ntr  = ch.GetNtrees();
  Int_t nent = ch.GetEntries();
  if (ntr <= 0 || nent <= 0) return;

  cout << "Ntr,Nent= " << ntr << " " << nent << endl;

  for (Int_t i = 0; i < ntr; i++)
    cout << ch.GetListOfFiles()->At(i)->GetTitle() << endl;

  Int_t   idata[13];
  Float_t fdata[28];

  TFile of(oname, "recreate");
  TTree *tree = new TTree("tree", "trcls");
  tree->Branch("idata", idata, "run/I:event/I:ient/I:time/I:tkml[9]/I");
  tree->Branch("fdata", fdata, "engc/F:enge/F:rgt/F:chrg/F:"
	                       "p0x/F:p0y/F:dzx/F:dzy/F:csqx/F:csqy/F:"
	                       "xcog[9]/F:ycog[9]/F");

  if (tkdbc && tkdbc[0] && tkdbc[0] != '0') {
    ch.GetEvent(0);
    TkDBc::Head->init(3, tkdbc);
  }
  TrExtAlignDB::OverLoadFlag = 0;

  Int_t malg = 2; // kAlcaraz | kMultScat
  Int_t pat0 = 3; // Inner only

  Int_t nevt = 0;
  Int_t nrsl = 0;
  Int_t npsl = 0;
  Int_t nfil = 0;
  Int_t intv = 10000;

  signal(SIGTERM, handler);
  signal(SIGINT,  handler);

  TStopwatch timer;
  timer.Start();

  for (Int_t ient = 0; ient < nent && !SigTERM; ient++) {
    AMSEventR *evt = ch.GetEvent(ient);
    nevt++;

    if (nevt%intv == 0 || nevt == nent) {
      Double_t tm = timer.RealTime();
      timer.Continue();
      cout << Form("%6d %6d %6d %7d (%5.1f%%) %4.0f sec (%4.1f kHz)",
		   nrsl, npsl, nfil, nevt,
		   100.*nevt/nent, tm, nevt/tm*1e-3)
	   << endl;
    }
    if (evt->nTrRecHit() >= 1600) continue;
    if (evt->nTrTrack () !=    1) continue;

    TrTrackR *trk = evt->pTrTrack(0);
    if (!trk) continue;

    Double_t chgp = TrCharge::GetQ(trk, 1);
    Double_t chgn = TrCharge::GetQ(trk, 0);
    if (chgp <= 0 || chgn <= 0) continue;

    Int_t mfp = trk->iTrTrackPar(malg, pat0, 0);
    if (mfp < 0) continue;

    Double_t rgtp = trk->GetRigidity(mfp);
    if (TMath::Abs(rgtp) < 5) continue;


    //////////////////// Recalc and refit ////////////////////
    trk->RecalcHitCoordinates();

    Int_t mf0 = trk->iTrTrackPar(malg, pat0, 2);
    if (mf0 < 0) continue;

    Double_t rgt0 = trk->GetRigidity(mf0);
    if (TMath::Abs(rgt0) < 10) continue;
    nrsl++;


    //////////////////// Pre-selection ////////////////////
    Bool_t psel = kTRUE;
    Int_t  span = (TrTrackSelection::GetSpanFlags(trk) & 0xff);
    if (!(span & TrTrackSelection::kMaxInt) ||
	!(span & TrTrackSelection::kAllPlane)) psel = kFALSE;

    if   (!(span & TrTrackSelection::kHalfL1N)) {
      if (!(span & TrTrackSelection::kHalfL9)) psel = kFALSE;

      AMSPoint pnt = trk->InterpolateLayerJ(9);
      if (TMath::Abs(pnt.x()) > 33) psel = kFALSE;
    }
    if (psel) npsl++;

    TrTrackR::AdvancedFitBits = 0x0f;

    TrRecon trec;
    Int_t nadd = trec.MergeExtHits(trk, mf0);
    if (!psel && nadd <= 0) continue;

    //////////////////// Remerge ////////////////////
    if (nadd > 0) {
      psel = kTRUE;
      span = (TrTrackSelection::GetSpanFlags(trk) & 0xff);
      if (!(span & TrTrackSelection::kMaxInt) ||
	  !(span & TrTrackSelection::kAllPlane)) psel = kFALSE;

      if   (!(span & TrTrackSelection::kHalfL1N)) {
	if (!(span & TrTrackSelection::kHalfL9)) psel = kFALSE;

	AMSPoint pnt = trk->InterpolateLayerJ(9);
	if (TMath::Abs(pnt.x()) > 33) psel = kFALSE;
      }
    }
    if (!psel) continue;

    idata[0] = evt->Run();
    idata[1] = evt->Event();
    idata[2] = ient;
    idata[3] = evt->fHeader.Time[0];

    EcalShowerR *ecal = evt->pEcalShower(0);
    fdata[0] = (ecal) ? ecal->EnergyC : 0;
    fdata[1] = (ecal) ? ecal->EnergyE : 0;
    fdata[2] = rgt0;
    fdata[3] = (chgp+chgn)/2;
    fdata[4] = trk->GetP0x(mf0);
    fdata[5] = trk->GetP0y(mf0);
    fdata[6] = trk->GetThetaXZ(mf0);
    fdata[7] = trk->GetThetaYZ(mf0);
    fdata[8] = trk->GetNormChisqX(mf0);
    fdata[9] = trk->GetNormChisqY(mf0);

    if (evt->nMCEventg() > 0) {
      MCEventgR *mcg = evt->pMCEventg(0);
      if (mcg) fdata[0] = mcg->Momentum;
    }

    Int_t   *tkml = &idata[ 4];
    Float_t *xcog = &fdata[10];
    Float_t *ycog = &fdata[19];
    for (Int_t i = 0; i < 9; i++) { tkml[i] = 0; xcog[i] = ycog[i] = 0; }

    for (Int_t i = 0; i < trk->GetNhits(); i++) {
      TrRecHitR *hit = trk->GetHit(i);
      if (!hit) continue;

      Int_t layer = hit->GetLayer();
      //if (layer != 8 && layer != 9) continue;

      Int_t il   = layer-1;//layer-8;
      Int_t tkid = hit->GetTkId();
      Int_t imlt = hit->GetResolvedMultiplicity();

      TrClusterR *clx = hit->GetXCluster();
      TrClusterR *cly = hit->GetYCluster();
      tkml[il] = TMath::Sign(imlt*1000+TMath::Abs(tkid), tkid);
      xcog[il] = (!clx) ? -(hit->GetDummyX()+640)
	       : clx->GetCofG()+clx->GetSeedAddress();
      ycog[il] = cly->GetCofG()+cly->GetSeedAddress();
    }

    tree->Fill();
    nfil++;
  }

  of.cd();
  tree->Write();
}
Beispiel #4
0
//int ScanTree ( TTree* tree) {
int ScanTree ( TTree* tree, char *fileName) {

  // This reads in the tree.  As you might imagine.
  Init(tree);
  TFile *histFile = new TFile(fileName,"RECREATE");
  TDirectory *histDir = histFile->mkdir("eff_hist");
  TStopwatch timer;
  timer.Start();

  // Declare the usual constants
  double pi = 3.141592653;
  double ptCut = 5.0;
  vector<int> *l1UsedBySim = new vector<int>;
  vector<int> *l2UsedBySim = new vector<int>;
  vector<int> *l3UsedBySim = new vector<int>;

  histDir->cd();
  
  //  int nPtBins = 19;
  //  Double_t pt_Edges[20] = {0, 5, 7, 9, 11, 13, 15, 18, 21, 24, 27, 30, 34, 38, 42, 50, 60, 70, 80, 100};

  //  int nPtBins = 8;
  //  Double_t pt_Edges[9] = {0, 1, 2, 3, 4, 6, 8, 10, 20};

  int nPtBins = 11;
  Double_t pt_Edges[12] = {0, 3, 5, 7, 9, 11, 13, 15, 18, 21, 24, 30};

  TH1F *l3OverXEtaEffNum = new TH1F("l3OverXEtaEffNum","l3 associated sim #eta",24,-2.4,2.4);
  TH1F *l3OverXPtEffNum = new TH1F("l3OverXPtEffNum","l3 associated sim pt",nPtBins,pt_Edges);
  TH2F *l3OverXPtEtaEffNum = new TH2F("l3OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l2OverXEtaEffNum = new TH1F("l2OverXEtaEffNum","l2 associated sim #eta",24,-2.4,2.4);
  TH1F *l2OverXPtEffNum = new TH1F("l2OverXPtEffNum","l2 associated sim pt",nPtBins,pt_Edges);
  TH2F *l2OverXPtEtaEffNum = new TH2F("l2OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l1OverXEtaEffNum = new TH1F("l1OverXEtaEffNum","l1 associated sim #eta",24,-2.4,2.4);
  TH1F *l1OverXPtEffNum = new TH1F("l1OverXPtEffNum","l1 associated sim pt",nPtBins,pt_Edges);
  TH2F *l1OverXPtEtaEffNum = new TH2F("l1OverXPtEtaEffNum","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *lXOverSimEtaEffDenom = new TH1F("lXOverSimEtaEffDenom","sim #eta",24,-2.4,2.4);
  TH1F *lXOverSimPtEffDenom = new TH1F("lXOverSimPtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverSimPtEtaEffDenom = new TH2F("lXOverSimPtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);
  TH1F *lXOverL1EtaEffDenom = new TH1F("lXOverL1EtaEffDenom","l1 associated sim #eta",24,-2.4,2.4);
  TH1F *lXOverL1PtEffDenom = new TH1F("lXOverL1PtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverL1PtEtaEffDenom = new TH2F("lXOverL1PtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);
  TH1F *lXOverL2EtaEffDenom = new TH1F("lXOverL2EtaEffDenom","l2 associated sim #eta",24,-2.4,2.4);
  TH1F *lXOverL2PtEffDenom = new TH1F("lXOverL2PtEffDenom","lX associated sim pt",nPtBins,pt_Edges);
  TH2F *lXOverL2PtEtaEffDenom = new TH2F("lXOverL2PtEtaEffDenom","blah",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverSimEtaEff = new TH1F("l3OverSimEtaEff","l3/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l3OverSimPtEff = new TH1F("l3OverSimPtEff","l3/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverSimPtEtaEff = new TH2F("l3OverSimPtEtaEff","l3/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverL1EtaEff = new TH1F("l3OverL1EtaEff","l3/l1 eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l3OverL1PtEff = new TH1F("l3OverL1PtEff","l3/l1 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverL1PtEtaEff = new TH2F("l3OverL1PtEtaEff","l3/l1 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l3OverL2EtaEff = new TH1F("l3OverL2EtaEff","l3/l2 eff vs. assoc. sim #eta",24,-2.4,2.4);  
  TH1F *l3OverL2PtEff = new TH1F("l3OverL2PtEff","l3/l2 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l3OverL2PtEtaEff = new TH2F("l3OverL2PtEtaEff","l3/l2 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);
  
  TH1F *l2OverSimEtaEff = new TH1F("l2OverSimEtaEff","l2/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l2OverSimPtEff = new TH1F("l2OverSimPtEff","l2/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l2OverSimPtEtaEff = new TH2F("l2OverSimPtEtaEff","l2/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);

  TH1F *l2OverL1EtaEff = new TH1F("l2OverL1EtaEff","l2/l1 eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l2OverL1PtEff = new TH1F("l2OverL1PtEff","l2/l1 eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l2OverL1PtEtaEff = new TH2F("l2OverL1PtEtaEff","l2/l1 eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);
 
  TH1F *l1OverSimEtaEff = new TH1F("l1OverSimEtaEff","l1/sim eff vs. assoc. sim #eta",24,-2.4,2.4);
  TH1F *l1OverSimPtEff = new TH1F("l1OverSimPtEff","l1/sim eff vs. assoc. sim pt",nPtBins,pt_Edges);
  TH2F *l1OverSimPtEtaEff = new TH2F("l1OverSimPtEtaEff","l1/sim eff map: p_{T} vs #eta",nPtBins,pt_Edges,24,-2.4,2.4);


  int nEntries = tree->GetEntries();
    
  //Event Loop
  for( int iEntry = 0; iEntry < nEntries; iEntry++) {
    if (iEntry%1000 == 0) cout << "Event " << iEntry << " time " << timer.RealTime() << " cpu " << timer.CpuTime() << endl;
    timer.Continue();
    tree->GetEntry(iEntry);
    
    double max_simPt = -999;

    try {
      //      cout << "trying passL1" << endl;
      bool passL1 = false;
      for (int iL1 = 0; iL1 < nL1; iL1++) {
	if ((*l1Pt).at(iL1) + 0.01 > 7 && (*l1Quality).at(iL1) >= 4) {
	  passL1 = true;
	}
      }

      //      cout << "trying passL2" << endl;
      bool passL2 = false;
      for (int iL2 = 0; iL2 < nL2; iL2++) {
	if (passL1) {
	  if ((*l2Pt).at(iL2) > 7 && (*l2Eta).at(iL2) > -2.5 && (*l2Eta).at(iL2) < 2.5) {
	    passL2 = true;
	  }
	}
      }
      bool passL3 = false;
      for (int iL3 = 0; iL3 < nL3; iL3++) {
	if (passL2) {
	  if ((*l3Pt).at(iL3) > 9 && (*l3Eta).at(iL3) > -2.5 && (*l3Eta).at(iL3) < 2.5) {
	    passL3 = true;
	  }
	}
      }

      for (int i = 0; i < nSimMuon; i++) {
	if ((*simMuonPt).at(i) > ptCut) {
	
	  lXOverSimEtaEffDenom->Fill((*simMuonEta).at(i));
	  lXOverSimPtEffDenom->Fill((*simMuonPt).at(i));
	  lXOverSimPtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	  //	cout << "trying to associate sim to L1 by DR" << endl;
	  int l1Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l1Eta,l1Phi,1.0,l1UsedBySim);
	  int l2Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l2Eta,l2Phi,0.2,l2UsedBySim);
	  int l3Index = STR_Association_Index((*simMuonEta).at(i),(*simMuonPhi).at(i),l3Eta,l3Phi,0.1,l3UsedBySim);
	  if (l1Index >= 0) {
	    l1OverXEtaEffNum->Fill((*simMuonEta).at(i));
	    l1OverXPtEffNum->Fill((*simMuonPt).at(i));
	    l1OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));
	    
	    lXOverL1EtaEffDenom->Fill((*simMuonEta).at(i));
	    lXOverL1PtEffDenom->Fill((*simMuonPt).at(i));
	    lXOverL1PtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	    if (l2Index >= 0) {
	      l2OverXEtaEffNum->Fill((*simMuonEta).at(i));
	      l2OverXPtEffNum->Fill((*simMuonPt).at(i));
	      l2OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));	      

	      lXOverL2EtaEffDenom->Fill((*simMuonEta).at(i));
	      lXOverL2PtEffDenom->Fill((*simMuonPt).at(i));
	      lXOverL2PtEtaEffDenom->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));

	      if (l3Index >= 0) {
		l3OverXEtaEffNum->Fill((*simMuonEta).at(i));
		l3OverXPtEffNum->Fill((*simMuonPt).at(i));
		l3OverXPtEtaEffNum->Fill((*simMuonPt).at(i),(*simMuonEta).at(i));	      
	      } // l3 associated
	    } // l2 associated
	  } // l1 associated
	} // cut on sim pT
      } // loop over sim
      (*l1UsedBySim).clear();
      (*l2UsedBySim).clear();
      (*l3UsedBySim).clear();
    } // try statement
    catch (...) {
      cout << "F**k it.  Pressing on" << endl;
    }
  } // event loop
  
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverSimEtaEffDenom,l3OverSimEtaEff);
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverL1EtaEffDenom,l3OverL1EtaEff);
  divide_histos_and_errors(l3OverXEtaEffNum,lXOverL2EtaEffDenom,l3OverL2EtaEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverSimPtEffDenom,l3OverSimPtEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverL1PtEffDenom,l3OverL1PtEff);
  divide_histos_and_errors(l3OverXPtEffNum,lXOverL2PtEffDenom,l3OverL2PtEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l3OverSimPtEtaEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverL1PtEtaEffDenom,l3OverL1PtEtaEff);
  divide_TH2_histos_and_errors(l3OverXPtEtaEffNum,lXOverL2PtEtaEffDenom,l3OverL2PtEtaEff);

  divide_histos_and_errors(l2OverXEtaEffNum,lXOverSimEtaEffDenom,l2OverSimEtaEff);
  divide_histos_and_errors(l2OverXEtaEffNum,lXOverL1EtaEffDenom,l2OverL1EtaEff);
  divide_histos_and_errors(l2OverXPtEffNum,lXOverSimPtEffDenom,l2OverSimPtEff);
  divide_histos_and_errors(l2OverXPtEffNum,lXOverL1PtEffDenom,l2OverL1PtEff);  
  divide_TH2_histos_and_errors(l2OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l2OverSimPtEtaEff);
  divide_TH2_histos_and_errors(l2OverXPtEtaEffNum,lXOverL1PtEtaEffDenom,l2OverL1PtEtaEff);

  divide_histos_and_errors(l1OverXEtaEffNum,lXOverSimEtaEffDenom,l1OverSimEtaEff);
  divide_histos_and_errors(l1OverXPtEffNum,lXOverSimPtEffDenom,l1OverSimPtEff);
  divide_TH2_histos_and_errors(l1OverXPtEtaEffNum,lXOverSimPtEtaEffDenom,l1OverSimPtEtaEff);
 
  histDir->Write("",TObject::kOverwrite);
  
  return 0;
}