//GetBEfficiency input histograms for pt binned only (1d), pt and eta binned (2d), pt binned in eta slices (1d)
void GetBEfficiency_InputHistograms(){
	gROOT->ProcessLine(".x SetStyle_PRD.C");

	gROOT->SetStyle("Plain");
	gStyle->SetOptStat(0);

	bool runQCD   = false;//set false if want to save some time //useful if look at high MT2
	bool onlyQCD  = false;//set false if want to save some time //useful if look at high MT2
	bool calcsusy = true;//at the moment has no genflavour // skip it
	bool runData  = false;//set false if want to save some time //at the moment not used, only for xchecks

	bool fHT      = true;//to change  htskim
	bool fMET     = false; //to change metskim
	bool fChange  = true; //use this only if you want to change the skims by hand

	//not implemented here, yet
	bool ttbarxsecup   = false;
	bool ttbarxsecdown = false;
	bool wjetsxsecup   = false;
	bool wjetsxsecdown = false;

//	TString samples         = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_Stop_53X_0bMET30_corr.dat";//Dileptons
//	TString samples         = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_type1MET_CHS_53X_MET.dat";//MET
//	TString samples         = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_type1MET_CHS_53X_HT.dat";//HT
//	TString samples         = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_type1MET_CHS_53X_MET_newTTbar.dat";//MET
	TString samples         = "/dataLOCAL/paktinat/Projects/MT2/MT2_V02-03-02/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samplesMine.dat";//HT
//	TString samples         = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_type1MET_CHS_53X_METorHT.dat";//HT
	TString outputdir 	= "/dataLOCAL/paktinat/Projects/MT2/MT2_V02-03-02/MT2Analysis/Code/Efficiencies/InputHistos/";//if not empty end with slash
//	TString outputdir 	= "/shome/haweber/MT2Analysis_8TeV/Code/Efficiencies/test/InputHistos/";//if not empty end with slash
	Util::MakeOutputDir(outputdir);
//	TString outputname 	= "BEfficiencies_histos_DileptonicStop_CSVM.root";
//	TString outputname 	= "BEfficiencies_histos_ge2jets_0l_CSVM_HT750orMET200_MinDPhiPt40_noQCD.root";
//	TString outputname 	= "BEfficiencies_histos_ge2jets_0l_CSVM_HT750orMET200_MinDPhi4_noQCD.root";
//	TString outputname 	= "BEfficiencies_histos_ge2jets_CSVM_HT750orMET200_MinDPhiPt40_noQCD.root";
//	TString outputname 	= "BEfficiencies_histos_ge2jets_CSVM_HT750orMET200_MinDPhi4.root";
//	TString outputname 	= "BEfficiencies_histos_ge2jets_CSVM_HT450andMET200_MinDPhi4_newTTbar.root";
	TString outputname 	= "BEfficiencies_histos_ge2jets_CSVM_HT750andMET30_MinDPhi4_newTTbar_noQCD.root";

//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_0l_CSVM_allMT2_MinDPhi40.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge3jets_ge1l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_ge6jets_ge1l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_HT_eq2jets_ge1l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_0l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_ge1l_CSVM_allMT2.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_0l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_ge1l_CSVM_MT2lt125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge3jets_ge1l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_ge6jets_ge1l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_0l_CSVM_MT2ge125.root";
//	TString outputname 	= "BEfficiencies_histos_MET_eq2jets_ge1l_CSVM_MT2ge125.root";

	int btaggername = 4; //0: TCHE, 1: TCHP, 2: SSVHE, 3: SSVHP, 4: CSV, 5: JP
	//CSV: L: 0.244, M: 0.679, T: 0.898
	//JP:  L: 0.275, M: 0.545, T: 0.790
	//TCHPT: 3.41, SSVHEM: 1.74, SSVHPT: 2.00
	double bdiscr_value = 0.679;
	int WP = 1;// WP: 0: loose, 1: medium, 2: tight
	double bpt = 20.;
	double beta = 2.4;
	const int Nptbins   = 17; double ptbins[Nptbins+1]     = {20, 30, 40, 50, 60, 70, 80, 100, 120, 160, 210, 260, 320, 400, 500, 600, 800, 1000};
//	const int Nptbins   = 16; double ptbins[Nptbins+1]     = {20, 30, 40, 50, 60, 70, 80, 100, 120, 160, 210, 260, 320, 400, 500, 670, 750};
	const int NetabinsL =  4; double etabinsL[NetabinsL+1] = {0.0, 0.5, 1.0, 1.5, 2.4};
	const int NetabinsM =  3; double etabinsM[NetabinsM+1] = {0.0, 0.8, 1.6, 2.4};
	const int NetabinsT =  1; double etabinsT[NetabinsT+1] = {0.0, 2.4};

	map<string, TH2D*> histos2d;
	map<string, TH1D*> histospt;

	const int sampletypesize = 10;
	string sample_type[sampletypesize] = {"QCD", "WJets", "ZJets", "TTbar", "SingleTop", "Other", "mc", "mcNOqcd", "susy", "data"};//same as type in samples.dat

	const int prefixsize = 10;
	string prefix[10] = {"B", "C", "L", "NonB", "All", "BTaggedB", "BTaggedC", "BTaggedL", "BTaggedNonB", "BTaggedAll"};


	for(int is = 0; is<sampletypesize; ++is){
	   for(int js = 0; js<10; ++js){
		string hs = string("_") + sample_type[is];
		string mapname;
		mapname = prefix[js] + "Jet" + hs;
		if(WP==0 && histos2d.count(mapname) == 0 ) histos2d[mapname] = new TH2D((mapname+string("_2d")).c_str(), "eff", Nptbins, ptbins, NetabinsL, etabinsL);
		if(WP==1 && histos2d.count(mapname) == 0 ) histos2d[mapname] = new TH2D((mapname+string("_2d")).c_str(), "eff", Nptbins, ptbins, NetabinsM, etabinsM);
		if(WP==2 && histos2d.count(mapname) == 0 ) histos2d[mapname] = new TH2D((mapname+string("_2d")).c_str(), "eff", Nptbins, ptbins, NetabinsT, etabinsT);
		if(         histospt.count(mapname) == 0 ) histospt[mapname] = new TH1D( mapname.c_str(),                "eff", Nptbins, ptbins);
	   }
	}
	
	for(map<string,TH2D*>::iterator h=histos2d.begin(); h!=histos2d.end();++h){
		//cout << "Load " << h->first << endl;
		h->second->Sumw2();}
	for(map<string,TH1D*>::iterator h=histospt.begin(); h!=histospt.end();++h){
		//cout << "Load " << h->first << endl;
		h->second->Sumw2();}

	cout << "Histograms loaded" << endl;
	
	std::ostringstream cutStream;
	cutStream       << " " 	  
     << "(misc.ProcessID!=6||(misc.Event!=1689009&&misc.Event!=2275452&&misc.Event!=1946785&&misc.Event!=1936763&&misc.Event!=1890738&&misc.Event!=1757319))" << "&&" //summer QCD 53X
     << "(misc.ProcessID!=10||(Susy.MassChi==350&&Susy.MassLSP==50))" << "&&" // T2bb M_stop = 350, M_lsp = 50
 //    << "(misc.ProcessID!=10||(Susy.MassChi==600&&Susy.MassLSP==50))" << "&&" // T2bb M_stop = 600, M_lsp = 50

//	  << "misc.MT2 >=0"                                      << "&&" 
//	  << "misc.MT2 < 125"                                    << "&&" 
//	  << "misc.MT2 >=125"                                    << "&&" 
	  << "misc.MET>=30"                                      << "&&"
	  << "misc.HT>=450"                                      << "&&"
	  << "misc.Jet0Pass ==1"                                 << "&&"
	  << "misc.Jet1Pass ==1"                                 << "&&"
//	  << "misc.SecondJPt  >100"                              << "&&"
	  << "misc.PassJetID ==1"                                << "&&"
	  << "misc.Vectorsumpt < 70"                             << "&&"
//	  << "(NJetsIDLoose40 +NEles +NMuons)>=3"                << "&&"
	  << "misc.MinMetJetDPhi4>0.3"                           << "&&"
//	  << "misc.MinMetJetDPhiPt40>0.3"                        << "&&"
//        << "NBJetsCSVM     >=1"                                << "&&"
//        JetSkim
	  << "NJetsIDLoose40 >=2"                                << "&&"
//	  << "NJetsIDLoose40 >=3"                                << "&&"
//	  << "NJetsIDLoose40 >=6"                                << "&&"
//	  << "NJetsIDLoose40 ==2"                                << "&&"
//        HTskim
//	  << "misc.HT >=750"                                     << "&&" 
//        METskim
//	  << "misc.MET >=200"                                    << "&&"
//	  LeptonSkim
//	  << "(NEles+NMuons) ==0"                                << "&&"
//	  << "(NEles+NMuons) >=1"                                << "&&"
// Stop Skim -------------------------
//	  << "NJetsIDLoose40 >=2"                                << "&&"
//        << "(NEles+NMuons) >=2"                                << "&&"
//        << "NBJetsCSVM     >=1"                                << "&&"
//	  << "NJetsIDLoose40 >=3"                                << "&&" 
// Noise -- first 6 are official filters
	  << "( misc.ProcessID==10 || misc.HBHENoiseFlag == 0)"  << "&&"
	  << "misc.CSCTightHaloIDFlag == 0"                      << "&&"
	  << "(misc.ProcessID==10||misc.hcalLaserEventFlag==0)"  << "&&"
//	  << "(misc.isFastSim || misc.HBHENoiseFlag == 0)"       << "&&"
//	  << "(misc.isFastSim || misc.CSCTightHaloIDFlag == 0)"  << "&&"
//	  << "(misc.isFastSim || misc.hcalLaserEventFlag == 0)"  << "&&"
	  << "misc.trackingFailureFlag == 0"                     << "&&"
	  << "misc.eeBadScFlag == 0"                             << "&&"
	  << "misc.EcalDeadCellTriggerPrimitiveFlag == 0";

	TString cuts = cutStream.str().c_str();
/*
	std::ostringstream triggerStreamEE;
	triggerStreamEE << "( "
	  << "(trigger.HLT_DiElectrons==1)" << " )";
	TString triggerEE = triggerStreamEE.str().c_str();
	std::ostringstream triggerStreamEMu;
	triggerStreamEMu << "( "
	  << "(trigger.HLT_EMu==1)" << " )";
	TString triggerEMu = triggerStreamEMu.str().c_str();
	std::ostringstream triggerStreamMuMu;
	triggerStreamMuMu << "( "
	  << "(trigger.HLT_DiMuons==1)" << " )";
	TString triggerMuMu = triggerStreamMuMu.str().c_str();
*//*
	std::ostringstream triggerStream;
	triggerStream << "( "
			<< "trigger.HLT_PFHT650_v5 == 1 || trigger.HLT_PFHT650_v6 == 1 || trigger.HLT_PFHT650_v7 == 1 || "
			<< "trigger.HLT_PFHT650_v8 == 1 || trigger.HLT_PFHT650_v9 == 1 || "
			<< "trigger.HLT_PFNoPUHT650_v1 == 1 || trigger.HLT_PFNoPUHT650_v3 == 1)";
	
	TString trigger = triggerStream.str().c_str();
*/
	std::ostringstream triggerStream;
	triggerStream << "( "
			<< "trigger.HLT_PFMET150_v2 == 1 || trigger.HLT_PFMET150_v3 == 1 || trigger.HLT_PFMET150_v4 == 1 || "
			<< "trigger.HLT_PFMET150_v5 == 1 || trigger.HLT_PFMET150_v6 == 1 || trigger.HLT_PFMET150_v7 == 1 )";
	TString trigger = triggerStream.str().c_str();
  

	load(samples.Data());

	for(size_t i = 0; i < fSamples.size(); ++i){

   	    if(runData==false && fSamples[i].type=="data") continue;
	    if(calcsusy==false && fSamples[i].type=="susy") continue;
	    if(runQCD==false   && fSamples[i].sname=="QCD") continue;
	    if(onlyQCD==true   && fSamples[i].sname!="QCD" && runQCD) continue;

	    string sampletype = (string)fSamples[i].type;
	    bool METskim = false;
	    bool HTskim  = false;
	    TString path = fSamples[i].file->GetName();
	    if(path.Contains("METskim")) METskim = true;
	    if(path.Contains("HTskim"))  HTskim  = true;
	    if(fChange){
		if(fHT)  { HTskim = true;  METskim = false; }
		if(fMET) { HTskim = false; METskim = true;  }
	    	if(!(fMET || fHT)) { HTskim = false; METskim = false; }
	    }
//		cout << true << " " << path << " " << METskim << " " << HTskim << endl;
	    if(!(METskim || HTskim)) continue;
	    if(sampletype==(string)"mc"){
		if(fSamples[i].sname=="QCD") sampletype = (string)fSamples[i].sname;
		else if(fSamples[i].sname=="Wtolnu") sampletype = (string)"WJets";
		else if(fSamples[i].sname=="DY")     sampletype = (string)"ZJets";
		else if(fSamples[i].name=="TTbar")   sampletype = (string)"TTbar";
		else if(fSamples[i].name=="TTbar_Had")   sampletype = (string)"TTbar";
		else if(fSamples[i].name=="TTbar_SemiLep")   sampletype = (string)"TTbar";
		else if(fSamples[i].name=="TTbar_FullyLep")   sampletype = (string)"TTbar";
		else if(fSamples[i].sname=="TTbarV") sampletype = (string)"TTbar";
		else if(fSamples[i].sname=="Top")    sampletype = (string)"SingleTop";//no ttbar
		//else if(fSamples[i].sname=="VV" || fSamples[i].sname=="VVV") sampletype = (string)"VV/VVV";
		else sampletype = (string)"Other";
	    }
	//    if(sampletype==(string)"susy") sampletype=(string)"Stop";

	    Double_t sample_weight = fSamples[i].xsection * fSamples[i].kfact * fSamples[i].lumi / (fSamples[i].nevents);
	    if(fVerbose>2) cout << "B-Efficiency: looping over " << fSamples[i].name << endl;
	    if(fVerbose>2) cout << "              sample has weight " << sample_weight << " and " << fSamples[i].tree->GetEntries() << " entries" << endl; 
	    if(fVerbose>2 && fSamples[i].tree->GetEntries()==0) cout << "skip sample, has no entries" << endl;
            if(fSamples[i].tree->GetEntries()==0) continue;
	    MT2tree* fMT2tree = new MT2tree();
	    fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
	    Long64_t nentries =  fSamples[i].tree->GetEntries();
	    Long64_t nbytes = 0, nb = 0;
	    int nev =0;

	    TString myCuts = cuts;
	    if(HTskim)  myCuts = myCuts + " && misc.HT>750";
	    if(METskim) myCuts = myCuts + " && misc.HT<=750 && misc.MET>200";//METskim excluding HTskim

	/*
	    if( fSamples[i].type=="data" && fSamples[i].sname=="EE-Data") { myCuts += " && " + triggerEE; }//cuts to be aplied only on data
	    else if( fSamples[i].type=="data" && fSamples[i].sname=="EMu-Data") { myCuts += " && " + triggerEMu; }//cuts to be aplied only on data
	    else if( fSamples[i].type=="data" && fSamples[i].sname=="MuMu-Data") { myCuts += " && " + triggerMuMu; } //cuts to be aplied only on data
	    else if(fSamples[i].type=="data") {cout << "data not usuable" << " type " << fSamples[i].type << " Sname " << fSamples[i].sname << endl; continue; }//not 
	*/
	    if( fSamples[i].type=="data") { myCuts += " && " + trigger; }

   	    cout << "Cuts for Flow: " << myCuts << endl;
   	    fSamples[i].tree->Draw(">>selList", myCuts);

	    TEventList *myEvtList = (TEventList*)gDirectory->Get("selList");

	    fSamples[i].tree->SetEventList(myEvtList);

	    int counter=0;
	    cout << "Filtering done, size=" <<myEvtList->GetN()  << endl;
	    if(myEvtList->GetSize()==0) continue;
        while(myEvtList->GetEntry(counter++) !=-1){	
      		int jentry = myEvtList->GetEntry(counter-1);
            
            nb =  fSamples[i].tree->GetEntry(jentry);   nbytes += nb;
            fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
            
            if ( fVerbose>2 && counter % 50000 == 0 )  cout << "+++ Proccessing event " << counter << endl;


	    //if((fSamples[i].sname!="VVV"&&fSamples[i].sname!="TTbarV" && fSamples[i].type!="susy"&&sampletype!="Stop")  && fMT2tree->misc.HBHENoiseFlag!=0     ) continue;//test
	    //if((fSamples[i].sname!="VVV"&&fSamples[i].sname!="TTbarV" && fSamples[i].type!="susy"&&sampletype!="Stop")  && fMT2tree->misc.hcalLaserEventFlag!=0) continue;//test
            Double_t weight = sample_weight;
            if (!fMT2tree->misc.isData ) weight = weight * fMT2tree->pileUp.Weight; // pile-up reweighting for MC 
 /*           Bool_t recoedee   = false;// exact 2 ele, 0 muo
            Bool_t recoedemu  = false;// exact 1 ele, 1 muo
            Bool_t recoedmumu = false;// exact 0 ele, 2 muo
		//change this - might have some inefficiencies - three leptons with one not passing IDMedium==1
		//20-20 selection --> third lepton is 10 GeV 
            if(fMT2tree->NEles>=2 &&fMT2tree->ele[0].lv.Pt()>20&&fMT2tree->ele[1].lv.Pt()>20&&fMT2tree->ele[2].lv.Pt()<20&&(fMT2tree->NMuons==0||fMT2tree->muo[0].lv.Pt()<10) && fMT2tree->ele[0].IDMedium==1 && fMT2tree->ele[1].IDMedium==1) recoedee   = true;
            if(fMT2tree->NMuons>=2&&fMT2tree->muo[0].lv.Pt()>20&&fMT2tree->muo[1].lv.Pt()>20&&fMT2tree->muo[2].lv.Pt()<10&&(fMT2tree->NEles ==0||fMT2tree->ele[0].lv.Pt()<10)) recoedmumu = true;
            if(fMT2tree->NMuons>=1&&fMT2tree->muo[0].lv.Pt()>20&&fMT2tree->muo[1].lv.Pt()<10&&fMT2tree->NEles>=1 &&fMT2tree->ele[0].lv.Pt()>20&&fMT2tree->ele[1].lv.Pt()<10 && fMT2tree->ele[0].IDMedium==1) recoedemu  = true;//XXX change emu to 20/20 selection, left ee,mumu at 20/10 for now
	    //id cuts?

            Bool_t recoedosee   = false;// opposite sign
            Bool_t recoedosemu  = false;// opposite sign
            Bool_t recoedosmumu = false;// opposite sign
	    if(recoedee   && (fMT2tree->ele[0].Charge)*(fMT2tree->ele[1].Charge)==(-1)) recoedosee   = true;
	    if(recoedemu  && (fMT2tree->ele[0].Charge)*(fMT2tree->muo[0].Charge)==(-1)) recoedosemu  = true;
	    if(recoedmumu && (fMT2tree->muo[0].Charge)*(fMT2tree->muo[1].Charge)==(-1)) recoedosmumu = true;
            Bool_t recoedeenZ   = false;// off Z
            Bool_t recoedemunZ  = false;// off Z -> is not requirement, so this variable should be useless
            Bool_t recoedmumunZ = false;// off Z
	    if(recoedee   && ((fMT2tree->ele[0].lv + fMT2tree->ele[1].lv).M()<81 || (fMT2tree->ele[0].lv + fMT2tree->ele[1].lv).M()>101) ) recoedeenZ   = true;
	    if(recoedemu  && ((fMT2tree->ele[0].lv + fMT2tree->muo[0].lv).M()<81 || (fMT2tree->ele[0].lv + fMT2tree->muo[0].lv).M()>101) ) recoedemunZ  = true;
	    if(recoedmumu && ((fMT2tree->muo[0].lv + fMT2tree->muo[1].lv).M()<81 || (fMT2tree->muo[0].lv + fMT2tree->muo[1].lv).M()>101) ) recoedmumunZ = true;

	    if(!(recoedee)   && !(recoedemu)   && !(recoedmumu)  ) continue;//require dilepton
	    if(!(recoedosee) && !(recoedosemu) && !(recoedosmumu) ) continue;//require os-dilepton, maybe comment this for background est.

	    if(recoedee   && !(fMT2tree->misc.isData) ) weight = 0.960 * weight * 0.953366;//from SSb Florida
	    if(recoedemu  && !(fMT2tree->misc.isData) ) weight = 0.934 * weight;//from SSb Florida
	    if(recoedmumu && !(fMT2tree->misc.isData) ) weight = 0.875 * weight * 1.031213;//from SSb Florida
*/
	   	if(wjetsxsecup  ==true && fSamples[i].sname=="Wtolnu")	weight = weight*0.7;
	   	if(wjetsxsecdown==true && fSamples[i].sname=="Wtolnu")	weight = weight*1.3;
	   	if(ttbarxsecup  ==true && fSamples[i].name== "TTbar" )	weight = weight*0.85;
	   	if(ttbarxsecdown==true && fSamples[i].name== "TTbar" )	weight = weight*1.15;

		for(int k = 0; k<fMT2tree->NJets; ++k){
			if(fMT2tree->jet[k].isPFIDLoose==false) continue;
			if(fMT2tree->jet[k].Flavour<=-7777 && fMT2tree->misc.isData==false) continue;//MC sample veto if no btagging information
			if(abs(fMT2tree->jet[k].Flavour>100)) cout << "jet " << k << "has flavour " << fMT2tree->jet[k].Flavour << endl;
			float btagdiscr=-1;
			if(btaggername==0)      btagdiscr = fMT2tree->jet[k].bTagProbTCHE;
			else if(btaggername==1) btagdiscr = fMT2tree->jet[k].bTagProbTCHP;
			else if(btaggername==2) btagdiscr = fMT2tree->jet[k].bTagProbSSVHE;
			else if(btaggername==3) btagdiscr = fMT2tree->jet[k].bTagProbSSVHP;
			else if(btaggername==4) btagdiscr = fMT2tree->jet[k].bTagProbCSV;
			else if(btaggername==5) btagdiscr = fMT2tree->jet[k].bTagProbJProb;
			else                    btagdiscr = fMT2tree->jet[k].bTagProbCSV;
			float jetpt = fMT2tree->jet[k].lv.Pt();
			float jeteta= fabs(fMT2tree->jet[k].lv.Eta() );
			//float jetMT2= fMT2tree->misc.MT2;
			int jetabsflavour = abs(fMT2tree->jet[k].Flavour);
			if(jetpt<bpt)   continue;
			if(jeteta>beta) continue;
			histospt[string("AllJet_") + sampletype]->Fill(jetpt, weight );
			histos2d[string("AllJet_") + sampletype]->Fill(jetpt, jeteta, weight );
			if(jetabsflavour==5){
				histospt[string("BJet_") + sampletype]->Fill(jetpt, weight );
				histos2d[string("BJet_") + sampletype]->Fill(jetpt, jeteta, weight );
			}
			else if(jetabsflavour==4){
				histospt[string("CJet_") + sampletype]->Fill(jetpt, weight );
				histos2d[string("CJet_") + sampletype]->Fill(jetpt, jeteta, weight );
			}
			else{
				histospt[string("LJet_") + sampletype]->Fill(jetpt, weight );
				histos2d[string("LJet_") + sampletype]->Fill(jetpt, jeteta, weight );
			}
			if(btagdiscr>=bdiscr_value){
				histospt[string("BTaggedAllJet_") + sampletype]->Fill(jetpt, weight );
				histos2d[string("BTaggedAllJet_") + sampletype]->Fill(jetpt, jeteta, weight );
				if(jetabsflavour==5){
					histospt[string("BTaggedBJet_") + sampletype]->Fill(jetpt, weight );
					histos2d[string("BTaggedBJet_") + sampletype]->Fill(jetpt, jeteta, weight );
				}
				else if(jetabsflavour==4){
					histospt[string("BTaggedCJet_") + sampletype]->Fill(jetpt, weight );
					histos2d[string("BTaggedCJet_") + sampletype]->Fill(jetpt, jeteta, weight );
				}
				else{
					histospt[string("BTaggedLJet_") + sampletype]->Fill(jetpt, weight );
					histos2d[string("BTaggedLJet_") + sampletype]->Fill(jetpt, jeteta, weight );
				}
			}
		}//for(int k = 0; k<fMT2tree->NJets; ++k)
	   }
	   delete fMT2tree;
	}//for(size_t i = 0; i < fSamples.size(); ++i)

	cout << "add overflow bins" << endl;
	for(map<string,TH2D*>::iterator h=histos2d.begin(); h!=histos2d.end();++h){
		//there should not be any underflow, just look at overflow
		int nbinsx = h->second->GetNbinsX();
		int nbinsy = h->second->GetNbinsY();
		for(int ix = 1; ix<= nbinsx;++ix){
			h->second->SetBinContent(ix, nbinsy, 
						h->second->GetBinContent(ix,nbinsy) +
						h->second->GetBinContent(ix, nbinsy+1));
			h->second->SetBinError(ix, nbinsy, 
						sqrt(h->second->GetBinError(ix,nbinsy)*h->second->GetBinError(ix,nbinsy) +
						h->second->GetBinError(ix, nbinsy+1)*h->second->GetBinError(ix, nbinsy+1)) );
		}
		for(int iy = 1; iy<= nbinsy;++iy){
			h->second->SetBinContent(nbinsx, iy, 
						h->second->GetBinContent(nbinsx,iy) +
						h->second->GetBinContent(nbinsx+1, iy));
			h->second->SetBinError(nbinsx, iy, 
						sqrt(h->second->GetBinError(nbinsx,iy)*h->second->GetBinError(nbinsx,iy) +
						h->second->GetBinError(nbinsx+1, iy)*h->second->GetBinError(nbinsx+1, iy)) );
		}
		h->second->SetBinContent(nbinsx, nbinsy, 
						h->second->GetBinContent(nbinsx,nbinsy) +
						h->second->GetBinContent(nbinsx+1, nbinsy+1));
		h->second->SetBinError(nbinsx, nbinsy, 
					sqrt(h->second->GetBinError(nbinsx,nbinsy)*h->second->GetBinError(nbinsx,nbinsy) +
					h->second->GetBinError(nbinsx+1, nbinsy+1)*h->second->GetBinError(nbinsx+1, nbinsy+1)) );
	}
	for(map<string,TH1D*>::iterator h=histospt.begin(); h!=histospt.end();++h){
		//there should not be any underflow, just look at overflow
		int nbinsx = h->second->GetNbinsX();
		h->second->SetBinContent(nbinsx, h->second->GetBinContent(nbinsx) + h->second->GetBinContent(nbinsx+1));
		h->second->SetBinError(  nbinsx, h->second->GetBinError(nbinsx  )*h->second->GetBinError(nbinsx  ) +
                                                 h->second->GetBinError(nbinsx+1)*h->second->GetBinError(nbinsx+1) );
	}


	cout << "merge mc histograms into one" << endl;
	//fill mc histogram from the samples histograms
	for(int ists = 0; ists<sampletypesize; ++ists){
		if((sample_type[ists])==("data")) continue;
		if((sample_type[ists])==("susy")) continue;
		if((sample_type[ists])==("mc") ) continue;//have all mc subsamples only
		string helptype = string("_") + sample_type[ists];
		string helpmc = "_mc";
		string helpmcnoqcd = "_mcNOqcd";
		histospt[string("BJet")   + helpmc]->Add(histospt[string("BJet")   + helptype], 1.);
		histospt[string("CJet")   + helpmc]->Add(histospt[string("CJet")   + helptype], 1.);
		histospt[string("LJet")   + helpmc]->Add(histospt[string("LJet")   + helptype], 1.);
		histospt[string("AllJet") + helpmc]->Add(histospt[string("AllJet") + helptype], 1.);
		histos2d[string("BJet")   + helpmc]->Add(histos2d[string("BJet")   + helptype], 1.);
		histos2d[string("CJet")   + helpmc]->Add(histos2d[string("CJet")   + helptype], 1.);
		histos2d[string("LJet")   + helpmc]->Add(histos2d[string("LJet")   + helptype], 1.);
		histos2d[string("AllJet") + helpmc]->Add(histos2d[string("AllJet") + helptype], 1.);
		histospt[string("BTaggedBJet")   + helpmc]->Add(histospt[string("BTaggedBJet")   + helptype], 1.);
		histospt[string("BTaggedCJet")   + helpmc]->Add(histospt[string("BTaggedCJet")   + helptype], 1.);
		histospt[string("BTaggedLJet")   + helpmc]->Add(histospt[string("BTaggedLJet")   + helptype], 1.);
		histospt[string("BTaggedAllJet") + helpmc]->Add(histospt[string("BTaggedAllJet") + helptype], 1.);
		histos2d[string("BTaggedBJet")   + helpmc]->Add(histos2d[string("BTaggedBJet")   + helptype], 1.);
		histos2d[string("BTaggedCJet")   + helpmc]->Add(histos2d[string("BTaggedCJet")   + helptype], 1.);
		histos2d[string("BTaggedLJet")   + helpmc]->Add(histos2d[string("BTaggedLJet")   + helptype], 1.);
		histos2d[string("BTaggedAllJet") + helpmc]->Add(histos2d[string("BTaggedAllJet") + helptype], 1.);
		if(sample_type[ists]==string("QCD")) continue;
		histospt[string("BJet")   + helpmcnoqcd]->Add(histospt[string("BJet")   + helptype], 1.);
		histospt[string("CJet")   + helpmcnoqcd]->Add(histospt[string("CJet")   + helptype], 1.);
		histospt[string("LJet")   + helpmcnoqcd]->Add(histospt[string("LJet")   + helptype], 1.);
		histospt[string("AllJet") + helpmcnoqcd]->Add(histospt[string("AllJet") + helptype], 1.);
		histos2d[string("BJet")   + helpmcnoqcd]->Add(histos2d[string("BJet")   + helptype], 1.);
		histos2d[string("CJet")   + helpmcnoqcd]->Add(histos2d[string("CJet")   + helptype], 1.);
		histos2d[string("LJet")   + helpmcnoqcd]->Add(histos2d[string("LJet")   + helptype], 1.);
		histos2d[string("AllJet") + helpmcnoqcd]->Add(histos2d[string("AllJet") + helptype], 1.);
		histospt[string("BTaggedBJet")   + helpmcnoqcd]->Add(histospt[string("BTaggedBJet")   + helptype], 1.);
		histospt[string("BTaggedCJet")   + helpmcnoqcd]->Add(histospt[string("BTaggedCJet")   + helptype], 1.);
		histospt[string("BTaggedLJet")   + helpmcnoqcd]->Add(histospt[string("BTaggedLJet")   + helptype], 1.);
		histospt[string("BTaggedAllJet") + helpmcnoqcd]->Add(histospt[string("BTaggedAllJet") + helptype], 1.);
		histos2d[string("BTaggedBJet")   + helpmcnoqcd]->Add(histos2d[string("BTaggedBJet")   + helptype], 1.);
		histos2d[string("BTaggedCJet")   + helpmcnoqcd]->Add(histos2d[string("BTaggedCJet")   + helptype], 1.);
		histos2d[string("BTaggedLJet")   + helpmcnoqcd]->Add(histos2d[string("BTaggedLJet")   + helptype], 1.);
		histos2d[string("BTaggedAllJet") + helpmcnoqcd]->Add(histos2d[string("BTaggedAllJet") + helptype], 1.);

	}
	cout << "merge C and L histograms to NonB" << endl;
	for(int ists = 0; ists<sampletypesize; ++ists){
		string helptype = string("_") + sample_type[ists];
		histospt[string("NonBJet")   + helptype]->Add(histospt[string("CJet")   + helptype], 1.);
		histospt[string("NonBJet")   + helptype]->Add(histospt[string("LJet")   + helptype], 1.);
		histos2d[string("NonBJet")   + helptype]->Add(histos2d[string("CJet")   + helptype], 1.);
		histos2d[string("NonBJet")   + helptype]->Add(histos2d[string("LJet")   + helptype], 1.);
		histospt[string("BTaggedNonBJet")   + helptype]->Add(histospt[string("BTaggedCJet")   + helptype], 1.);
		histospt[string("BTaggedNonBJet")   + helptype]->Add(histospt[string("BTaggedLJet")   + helptype], 1.);
		histos2d[string("BTaggedNonBJet")   + helptype]->Add(histos2d[string("BTaggedCJet")   + helptype], 1.);
		histos2d[string("BTaggedNonBJet")   + helptype]->Add(histos2d[string("BTaggedLJet")   + helptype], 1.);
	}
	cout << "save histogram" << endl;
	TFile *fsavefile = new TFile(outputdir+outputname,"RECREATE");
	fsavefile->cd();
	for(map<string,TH2D*>::iterator h=histos2d.begin(); h!=histos2d.end();++h){
		h->second->Write();}
	for(map<string,TH1D*>::iterator h=histospt.begin(); h!=histospt.end();++h){
		h->second->Write();}
	fsavefile->Close();

	cout << "Saved histograms in " << outputdir << outputname << endl;

}//void GetBEfficiency_InputHistograms()
//this file stores the data events in our signal regions in several histograms 
//that are used by MT2Results_PlotsAndTables.C
//can also choose to store files in control region or MC events
//additionally it creates a log file with run:lumisection:eventnumber for all those events.
void MakeDataFile(){

	fLogStream = new std::ostringstream();

	bool poisson = false;//set poissonian uncertainties, works from next root version on

	//decide on filename histograms are stored, also define the samples.dat
  	gROOT->ProcessLine(".x SetStyle_PRD.C");
	TString fOutDir                    = "../Results/Filtered/";
	TString outputname = "NewDataNumbers.root";
	if(ourfilter    ) outputname = "NewDataNumbersOurFilter.root";
	if(calometfilter) outputname = "NewDataNumbersOurFilterCaloMETfilter.root";
	if(tobtecfilter ) outputname = "NewDataNumbersOurFilterCaloMETfilterTOBTECFilter.root";
	if(poisson) outputname = "NewDataNumbersPoisson.root";
	TString samples = "samples/samples_HTandMET_filter.dat";

	//definition of the histograms
	map<string, TH1D*>    histos;
	for(int i2 = 0; i2<signalregionsize; ++i2){
	for(int i3 = 0; i3<HTbinsize;        ++i3){
		int NMT2bins;
		if(i3==0){
			if(signal_region[i2]=="2j0b")    NMT2bins = gNMT2bins_2j0b_lHT;
			if(signal_region[i2]=="2j1to2b") NMT2bins = gNMT2bins_2j1b_lHT;
			if(signal_region[i2]=="3to5j0b") NMT2bins = gNMT2bins_3j0b_lHT;
			if(signal_region[i2]=="3to5j1b") NMT2bins = gNMT2bins_3j1b_lHT;
			if(signal_region[i2]=="3to5j2b") NMT2bins = gNMT2bins_3j2b_lHT;
			if(signal_region[i2]=="6j0b")    NMT2bins = gNMT2bins_6j0b_lHT;
			if(signal_region[i2]=="6j1b")    NMT2bins = gNMT2bins_6j1b_lHT;
			if(signal_region[i2]=="6j2b")    NMT2bins = gNMT2bins_6j2b_lHT;
			if(signal_region[i2]=="3b")      NMT2bins = gNMT2bins_3b_lHT;
		} if(i3==1){
			if(signal_region[i2]=="2j0b")    NMT2bins = gNMT2bins_2j0b_mHT;
			if(signal_region[i2]=="2j1to2b") NMT2bins = gNMT2bins_2j1b_mHT;
			if(signal_region[i2]=="3to5j0b") NMT2bins = gNMT2bins_3j0b_mHT;
			if(signal_region[i2]=="3to5j1b") NMT2bins = gNMT2bins_3j1b_mHT;
			if(signal_region[i2]=="3to5j2b") NMT2bins = gNMT2bins_3j2b_mHT;
			if(signal_region[i2]=="6j0b")    NMT2bins = gNMT2bins_6j0b_mHT;
			if(signal_region[i2]=="6j1b")    NMT2bins = gNMT2bins_6j1b_mHT;
			if(signal_region[i2]=="6j2b")    NMT2bins = gNMT2bins_6j2b_mHT;
			if(signal_region[i2]=="3b")      NMT2bins = gNMT2bins_3b_mHT;
		} if(i3==2){
			if(signal_region[i2]=="2j0b")    NMT2bins = gNMT2bins_2j0b_hHT;
			if(signal_region[i2]=="2j1to2b") NMT2bins = gNMT2bins_2j1b_hHT;
			if(signal_region[i2]=="3to5j0b") NMT2bins = gNMT2bins_3j0b_hHT;
			if(signal_region[i2]=="3to5j1b") NMT2bins = gNMT2bins_3j1b_hHT;
			if(signal_region[i2]=="3to5j2b") NMT2bins = gNMT2bins_3j2b_hHT;
			if(signal_region[i2]=="6j0b")    NMT2bins = gNMT2bins_6j0b_hHT;
			if(signal_region[i2]=="6j1b")    NMT2bins = gNMT2bins_6j1b_hHT;
			if(signal_region[i2]=="6j2b")    NMT2bins = gNMT2bins_6j2b_hHT;
			if(signal_region[i2]=="3b")      NMT2bins = gNMT2bins_3b_hHT;
		}
  		double MT2bins[NMT2bins+1];
		if(i3==0){
			if(signal_region[i2]=="2j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j0b_lHT[i0]; }
			if(signal_region[i2]=="2j1to2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j1b_lHT[i0]; }
			if(signal_region[i2]=="3to5j0b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j0b_lHT[i0]; }
			if(signal_region[i2]=="3to5j1b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j1b_lHT[i0]; }
			if(signal_region[i2]=="3to5j2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j2b_lHT[i0]; }
			if(signal_region[i2]=="6j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j0b_lHT[i0]; }
			if(signal_region[i2]=="6j1b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j1b_lHT[i0]; }
			if(signal_region[i2]=="6j2b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j2b_lHT[i0]; }
			if(signal_region[i2]=="3b")      { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3b_lHT[i0];   }
		} if(i3==1){
			if(signal_region[i2]=="2j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j0b_mHT[i0];  }
			if(signal_region[i2]=="2j1to2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j1b_mHT[i0];  }
			if(signal_region[i2]=="3to5j0b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j0b_mHT[i0];  }
			if(signal_region[i2]=="3to5j1b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j1b_mHT[i0];  }
			if(signal_region[i2]=="3to5j2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j2b_mHT[i0];  }
			if(signal_region[i2]=="6j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j0b_mHT[i0];  }
			if(signal_region[i2]=="6j1b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j1b_mHT[i0];  }
			if(signal_region[i2]=="6j2b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j2b_mHT[i0];  }
			if(signal_region[i2]=="3b")      { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3b_mHT[i0];    }
		} if(i3==2){
			if(signal_region[i2]=="2j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j0b_hHT[i0];  }
			if(signal_region[i2]=="2j1to2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_2j1b_hHT[i0];  }
			if(signal_region[i2]=="3to5j0b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j0b_hHT[i0];  }
			if(signal_region[i2]=="3to5j1b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j1b_hHT[i0];  }
			if(signal_region[i2]=="3to5j2b") { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3j2b_hHT[i0];  }
			if(signal_region[i2]=="6j0b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j0b_hHT[i0];  }
			if(signal_region[i2]=="6j1b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j1b_hHT[i0];  }
			if(signal_region[i2]=="6j2b")    { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_6j2b_hHT[i0];  }
			if(signal_region[i2]=="3b")      { for(int i0 = 0; i0<=NMT2bins; ++i0) MT2bins[i0] = gMT2bins_3b_hHT[i0];    }
		}

		string hs = string("_") + HT_bin[i3] + string("_") + signal_region[i2];
		string mapname = "MT2" + hs;
		if(histos.count(mapname) == 0 ) histos[mapname] = new TH1D(mapname.c_str(), "", NMT2bins, MT2bins);
	//	if(poisson) histos[mapname]->SetBinErrorOption((TH1::EBinErrorOpt)1);//store already here the poissonian errors, works from next root version on
	}}
	for(map<string,TH1D*>::iterator h=histos.begin(); h!=histos.end();++h){
		h->second->Sumw2();}

	//signal selection cuts
	std::ostringstream  fCutStreamSignal;
	fCutStreamSignal << " " 
	  << "misc.MT2>=100"                                               << "&&"
	  << "misc.MET>=30"                                                << "&&"
	  << "misc.HT >=450"                                               << "&&"
	  << "NEles==0"                                                    << "&&"
	  << "NMuons==0"                                                   << "&&"
	  << "NTausIDLoose3Hits==0"                                        << "&&"
	  << "misc.Jet0Pass ==1"                                           << "&&"
	  << "misc.Jet1Pass ==1"                                           << "&&"
	  << "misc.PassJet40ID ==1"                                        << "&&"
	  << "NJetsIDLoose40 >=2"                                          << "&&"
	  << "misc.MinMetJetDPhi4Pt40 >0.3"                                << "&&"
	  << "misc.Vectorsumpt < 70"                                       << "&&"
	  // Noise
	  << "(misc.HBHENoiseFlag == 0 || misc.ProcessID==10)"             << "&&" // for rare SM samples
	  << "misc.CSCTightHaloIDFlag == 0"                                << "&&"
	  << "misc.trackingFailureFlag==0"                                 << "&&"
	  << "misc.eeBadScFlag==0"                                         << "&&"
	  << "misc.EcalDeadCellTriggerPrimitiveFlag==0"                    << "&&"
	  << "misc.TrackingManyStripClusFlag==0"                           << "&&"
	  << "misc.TrackingTooManyStripClusFlag==0"                        << "&&"
	  << "misc.TrackingLogErrorTooManyClustersFlag==0"                 << "&&"
          << "misc.CrazyHCAL==0";
	fCutStreamSignal << "&&((misc.MET>=200&&misc.MT2>=200&&misc.HT<750)||(misc.HT>750))";
	fCutStreamSignal << "&&NJetsIDLoose40>=2";
	fCutStreamSignal << "&&misc.MET/misc.CaloMETRaw<=2.";
	TString cuts = fCutStreamSignal.str().c_str();

  std::ostringstream triggerStream;
  triggerStream << "( ( ( "
		<< "trigger.HLT_PFHT650_v5 == 1 || trigger.HLT_PFHT650_v6 == 1 || trigger.HLT_PFHT650_v7 == 1 || trigger.HLT_PFHT650_v8 == 1 || trigger.HLT_PFHT650_v9 == 1 || "
		<< "trigger.HLT_PFNoPUHT650_v1 == 1 || trigger.HLT_PFNoPUHT650_v3 == 1 || trigger.HLT_PFNoPUHT650_v4 == 1) ||";

  triggerStream << "( ( "
		<< "trigger.HLT_PFMET150_v2 == 1 || trigger.HLT_PFMET150_v3 == 1 || trigger.HLT_PFMET150_v4 == 1 || "
  		<< "trigger.HLT_PFMET150_v5 == 1 || trigger.HLT_PFMET150_v6 == 1 || trigger.HLT_PFMET150_v7 == 1 )"
		<< "||("
		<< "trigger.HLT_PFHT350_PFMET100_v3==1 || trigger.HLT_PFHT350_PFMET100_v4==1 || trigger.HLT_PFHT350_PFMET100_v5==1 || "
		<< "trigger.HLT_PFHT350_PFMET100_v6==1 || trigger.HLT_PFHT350_PFMET100_v7==1 || trigger.HLT_PFNoPUHT350_PFMET100_v1==1 || "
		<< "trigger.HLT_PFNoPUHT350_PFMET100_v3==1 || trigger.HLT_PFNoPUHT350_PFMET100_v4==1 ) ) )&&TOBTECTagger<=8&&ExtraBeamHaloFilter==0)";

	TString trigger = triggerStream.str().c_str();
	load(samples.Data());

	int counterAll = 0;

  	for(size_t i = 0; i < fSamples.size(); ++i){

	    string sampletype = (string)fSamples[i].type;
	    if(sampletype!="data") continue;//run only over data - extendable to MC 

	    Double_t sample_weight = fSamples[i].xsection * fSamples[i].kfact * fSamples[i].lumi / (fSamples[i].nevents*fSamples[i].PU_avg_weight);
	    if(fVerbose>2) cout << "ZnunuNumbers: looping over " << fSamples[i].name << " added in " << sampletype << endl;
	    if(fVerbose>2) cout << "              sample has weight " << sample_weight << " and " << fSamples[i].tree->GetEntries() << " entries" << endl; 
	    if(fVerbose>2 && fSamples[i].tree->GetEntries()==0) cout << "skip sample, has no entries" << endl;
            if(fSamples[i].tree->GetEntries()==0) continue;

	    MT2tree* fMT2tree = new MT2tree();
	    fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
	    Long64_t nentries =  fSamples[i].tree->GetEntries();
	    Long64_t nbytes = 0, nb = 0;
	    int nev =0;
        
	    TString myCuts = cuts;// + "&&" + basecuts;
        
	    if( fSamples[i].type=="data") myCuts += " && " + trigger; //cuts to be aplied only on data
        
   	    cout << "Cuts for Flow: " << myCuts << endl;
   	    fSamples[i].tree->Draw(">>selList", myCuts);
        
	    TEventList *myEvtList = (TEventList*)gDirectory->Get("selList");
	    fSamples[i].tree->SetEventList(myEvtList);
	    int counter=0;
	    cout << "Filtering done, size=" <<myEvtList->GetN()  << endl;
	    if(myEvtList->GetSize()==0) continue;
	*fLogStream << "Sample " << fSamples[i].name << endl;
	//run over all selected events
        while(myEvtList->GetEntry(counter++) !=-1){	
		int jentry = myEvtList->GetEntry(counter-1);
            
		nb =  fSamples[i].tree->GetEntry(jentry);   nbytes += nb;
		fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
		
		if ( fVerbose>2 && counter % 5000 == 0  )  cout << "+++ Proccessing event " << counter << endl;

		string sHT;//HT region
		if(fMT2tree->misc.HT<450.)      sHT = "_HTge0";
		else if(fMT2tree->misc.HT<750.) sHT = "_lowHT";
		else if(fMT2tree->misc.HT<1200.)sHT = "_mediumHT";
		else                            sHT = "_highHT";

		string ssignal;//topological region
		if(fMT2tree->NJetsIDLoose40 == 2 &&                                  fMT2tree->NBJets40CSVM == 0) ssignal = "_2j0b";
		if(fMT2tree->NJetsIDLoose40 == 2 &&                                  fMT2tree->NBJets40CSVM >= 1) ssignal = "_2j1to2b";
		if(fMT2tree->NJetsIDLoose40 >= 3 && fMT2tree->NJetsIDLoose40 <= 5 && fMT2tree->NBJets40CSVM == 0) ssignal = "_3to5j0b";
		if(fMT2tree->NJetsIDLoose40 >= 3 && fMT2tree->NJetsIDLoose40 <= 5 && fMT2tree->NBJets40CSVM == 1) ssignal = "_3to5j1b";
		if(fMT2tree->NJetsIDLoose40 >= 3 && fMT2tree->NJetsIDLoose40 <= 5 && fMT2tree->NBJets40CSVM == 2) ssignal = "_3to5j2b";
		if(                                                                  fMT2tree->NBJets40CSVM >= 3) ssignal = "_3b";
		if(fMT2tree->NJetsIDLoose40 >= 6 &&                                  fMT2tree->NBJets40CSVM == 0) ssignal = "_6j0b";
		if(fMT2tree->NJetsIDLoose40 >= 6 &&                                  fMT2tree->NBJets40CSVM == 1) ssignal = "_6j1b";
		if(fMT2tree->NJetsIDLoose40 >= 6 &&                                  fMT2tree->NBJets40CSVM == 2) ssignal = "_6j2b";

		string hh = sHT + ssignal;
		histos[(string)"MT2"        + hh]->Fill(fMT2tree->misc.MT2);//fill histogram
		if(fMT2tree->misc.MT2>histos[(string)"MT2"        + hh]->GetBinLowEdge(1)){
			if(sHT=="_lowHT" && (fMT2tree->trigger.HLT_PFHT350_PFMET100_v3==1||fMT2tree->trigger.HLT_PFHT350_PFMET100_v4==1||fMT2tree->trigger.HLT_PFHT350_PFMET100_v5==1||fMT2tree->trigger.HLT_PFHT350_PFMET100_v6==1||fMT2tree->trigger.HLT_PFHT350_PFMET100_v7==1 || fMT2tree->trigger.HLT_PFNoPUHT350_PFMET100_v1==1 || fMT2tree->trigger.HLT_PFNoPUHT350_PFMET100_v3==1 || fMT2tree->trigger.HLT_PFNoPUHT350_PFMET100_v4==1 ) ) *fLogStream << "HTMHT ";
			else if(sHT=="_lowHT") *fLogStream << "MET   ";
			else *fLogStream << "JetHT ";
			*fLogStream << int(fMT2tree->misc.Run) << ":" << int(fMT2tree->misc.LumiSection) << ":" << int(fMT2tree->misc.Event) << endl;//file log file (including trigger stream
			++counterAll;
		}
	}//while
	delete fMT2tree;
	delete fSamples[i].tree;

	}//for samples

	cout << "add overflow to last bin" << endl;
	for(map<string,TH1D*>::iterator h=histos.begin(); h!=histos.end();++h){
		h->second->SetBinContent(h->second->GetNbinsX(),
					 h->second->GetBinContent(h->second->GetNbinsX()  )+ 
					 h->second->GetBinContent(h->second->GetNbinsX()+1)  );
		h->second->SetBinError(  h->second->GetNbinsX(),
					 sqrt(h->second->GetBinError(h->second->GetNbinsX()  )*
					      h->second->GetBinError(h->second->GetNbinsX()  )+
					      h->second->GetBinError(h->second->GetNbinsX()+1)*
					      h->second->GetBinError(h->second->GetNbinsX()+1)  ));
	}

	cout << "Saving." << endl;
    	TFile *fsavefile = new TFile(fOutDir + outputname,"RECREATE");
	fsavefile->cd();
	for(map<string,TH1D*>::iterator h=histos.begin(); h!=histos.end();++h){
		h->second->Write();
	}
	fsavefile->Close();
	cout << "Saved histograms in " << fOutDir << outputname << endl;

	TString logname ="eventlistMT2inclusive.log"; 
	cout << counterAll << " events in " << logname.Data() << endl;
	ofstream f_log (logname.Data(), ios::trunc);
	f_log << fLogStream->str();
}
//this code create the so-called 'PrintOuts' as done with the MT2tree::PrintOut() function
void PrintOutEvents(){

	bool savetofile = true;//save the printout to the file PrintOut.log, default = true
	bool onlyData = true;  //run only over data, default = true

//	TString  samples = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_MET_filter.dat";//only dummy
//	TString  samples = "/shome/haweber/MT2Analysis_8TeV/Code/MT2AnalysisCode/RootMacros/samples/samples_HT_filter.dat";//only dummy
	TString samples = "samples/samples_HTandMET_filter.dat";

	//event selection for which you want to have the printout - complicated stuff can also be done later
	std::ostringstream cutStream;
	std::ostringstream cutStreamBase;
	cutStream << " " 
	//	<< "NBJets40CSVM >= 3"		<< "&&"
		<< "NJetsIDLoose40 >= 2"		<< "&&"
		<< "NEles+NMuons+NTausIDLoose3Hits  ==0"<< "&&"
	//	<< "misc.MT2>=250"                      << "&&"
		<< "misc.Jet0Pass ==1"                      << "&&"
		<< "misc.Jet1Pass ==1"                      << "&&"
		<< "misc.Vectorsumpt < 70";
		cutStream  << "&& misc.MinMetJetDPhi4Pt40 >0.3";
//	cutStream << "&&misc.HT<750&&misc.HT>=450&&misc.MET>200";
//	cutStream << "&&misc.HT>=750&&misc.MET>30";
	cutStream << "&&((misc.HT<750&&misc.HT>=450&&misc.MET>200)||(misc.HT>=750&&misc.MET>30))";

	//Higgs
//	cutStream <<"&&NJetsIDLoose40 >= 4"<<"&&"
  //  		  <<"NBJetsCSVM>=2"<<"&&"
	//	  <<"GetSelBBMinv()>250";
	
	cutStreamBase << " " 
      << "misc.PassJet40ID ==1"                      << "&&"
      << "(misc.HBHENoiseFlag == 0 || misc.ProcessID==10)"  << "&&" // for rare SM samples
      << "misc.CSCTightHaloIDFlag == 0"             << "&&"
      << "misc.trackingFailureFlag==0"              << "&&"
      << "misc.eeBadScFlag==0"                      << "&&"
      << "misc.EcalDeadCellTriggerPrimitiveFlag==0" << "&&"
      << "misc.TrackingManyStripClusFlag==0"             << "&&"
      << "misc.TrackingTooManyStripClusFlag==0"          << "&&"
      << "misc.TrackingLogErrorTooManyClustersFlag==0"   << "&&"
      << "misc.CrazyHCAL==0";
	cutStreamBase << "&&misc.MET/misc.CaloMETRaw<=2.";
	cutStreamBase << "&&TOBTECTagger<=8&&ExtraBeamHaloFilter==0";// ONLY ON DATA
	TString cuts = cutStream.str().c_str();
	TString basecuts = cutStreamBase.str().c_str();

	load(samples.Data());

	//if have extremely many events can only select a random subset of those
	TRandom3 *random = new TRandom3(0);//somehow truly random with this seed
	vector<int> randvec; randvec.clear();
	for(int i =0; i<40;++i)
		randvec.push_back(random->Integer(7750));
	delete random;
	sort(randvec.begin(), randvec.end());
	//for(int i=0; i<randvec.size();++i) cout << "randvec["<<i<<"]="<<randvec[i] << endl;
	int counter1 = 0; int counter2 = 0;

	//don't run events twice - this vector makes that sure (in case event is in both MET and HT trigger stream)
	   vector<pair<int,pair<int,int> > > rls; rls.clear();


   	for(size_t i = 0; i < fSamples.size(); ++i){
        
   	    if(onlyData && fSamples[i].type!="data") continue;

	    MT2tree* fMT2tree = new MT2tree();
	    fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
	    Long64_t nentries =  fSamples[i].tree->GetEntries();
	    Long64_t nbytes = 0, nb = 0;
	    int nev =0;
        
	    TString myCuts = cuts + "&&" + basecuts;
        
	    //if( fSamples[i].type=="data") myCuts += " && " + trigger; //cuts to be aplied only on data
        
	    cout << "sample " << fSamples[i].name << endl;
   	    cout << "Cuts for Flow: " << myCuts << endl;
   	    fSamples[i].tree->Draw(">>selList", myCuts);
        
	    TEventList *myEvtList = (TEventList*)gDirectory->Get("selList");
	    fSamples[i].tree->SetEventList(myEvtList);
	    int counter=0;
	    cout << "Filtering done, size=" <<myEvtList->GetN()  << endl;
	    if(myEvtList->GetSize()==0) continue;
		//run over selected events
        while(myEvtList->GetEntry(counter++) !=-1){	
		int jentry = myEvtList->GetEntry(counter-1);
            
		nb =  fSamples[i].tree->GetEntry(jentry);   nbytes += nb;
		fSamples[i].tree->SetBranchAddress("MT2tree", &fMT2tree);
		//implementation of running over random subset
//		if(counter1==randvec[counter2]){
//			++counter2;
//			fMT2tree->PrintOut(savetofile);
//		}

		pair <int,int> dummy1(fMT2tree->misc.LumiSection,fMT2tree->misc.Event);
		pair <int,pair<int,int> > dummy(fMT2tree->misc.Run, dummy1);
		bool newevt = true;
		for(int nn = 0; nn<rls.size(); ++nn){
			if(rls[nn].first==dummy.first && rls[nn].second.first==dummy.second.first && rls[nn].second.second==dummy.second.second) {newevt = false; break;}
		}
		if(newevt){
			rls.push_back(dummy);
			fMT2tree->PrintOut(savetofile);//safe the printout
			++counter1;
		}

	}
	delete fMT2tree;
	delete fSamples[i].tree;
	}
	cout << "Printed out " << counter1 << " events." << endl;
}