Пример #1
0
bool Analysis::DphiCuts(){
	bool IsCutPass = false; 
	if (branchJet && branchMissingET){
	  MissingET *met = (MissingET*) branchMissingET->At(0);	
	  int nJets = branchJet->GetEntriesFast();	
	  for (int i=0; i < nJets; i++){
	    Jet *jet = (Jet*) branchJet->At(i);	
		if (jet->PT > 200.){
			if (jet->P4().DeltaR(met->P4()) > 1.) 
				IsCutPass=true; 
			else {IsCutPass=false; break;}
		}
	  }
	}
	IsEventPass = IsEventPass && IsCutPass;
	return IsCutPass;
}
Пример #2
0
void DiHiggstollbb::DiHiggstollbbrun()
{

    //loop over events 1. find h->b bbar; 2 find two bjets after cuts; 3 fill Tree
// TClonesArray *branchParticle = treeReader->UseBranch("Particle");
    //TClonesArray *branchElectron = treeReader->UseBranch("Electron");
    ///TClonesArray *branchPhoton = treeReader->UseBranch("Photon");
    TClonesArray *branchMuon = treeReader->UseBranch("Muon");
    //TClonesArray *branchMuonBeforeIso = treeReader->UseBranch("MuonBeforeIso");

    //TClonesArray *branchTrack = treeReader->UseBranch("Track");
//  TClonesArray *branchTower = treeReader->UseBranch("Tower");

    //TClonesArray *branchEFlowTrack = treeReader->UseBranch("EFlowTrack");
// TClonesArray *branchEFlowPhoton = treeReader->UseBranch("EFlowPhoton");
// TClonesArray *branchEFlowNeutralHadron = treeReader->UseBranch("EFlowNeutralHadron");
    TClonesArray *branchJet = treeReader->UseBranch("Jet");
    //TClonesArray *branchGenJet = treeReader->UseBranch("GenJet");

    TClonesArray *branchMissingET = treeReader->UseBranch("MissingET");
    //TClonesArray *branchGenMissingET = treeReader->UseBranch("GenMissingET");

    long allEntries = treeReader->GetEntries();

    cout << "** Chain contains " << allEntries << " events" << endl;

    long entry;
    Jet *jet, *b1jet, *b2jet;
    std::vector<Jet*> allbjets;
    std::vector<Jet> testvec;
    std::vector<Muon*> allMuon1;
    std::vector<Muon*> allMuon2;
    b1jet=0;
    b2jet=0;
    MissingET *Met;
    Met=0;
    //Electron *electron, *electron1, *electron2;
    //electron1 =0; electron2 = 0;
    //Photon *photon;
    Muon *muon;// *muon1, *muon2;// *muon1_beforeIso, *muon2_beforeIso;
    //muon1 =0; muon2=0; //muon1_beforeIso =0; muon2_beforeIso =0;
    //Track *track;
    //Tower *tower;

    TLorentzVector momentum;
    TLorentzVector totjets_lorentz = TLorentzVector();
    //incase compilation error

    int i =0;
    // Loop over all events
    //TFile *MMCfile = new TFile("testMMC.root", "recreate");
    if (nEvents < 0 || nEvents>allEntries) nEvents = allEntries;
    for(entry = 0; entry < nEvents; ++entry)
        //for(entry = 0; entry < 1000; ++entry)
        //for(entry = 0; entry < 10000; ++entry)
    {
        //gen
        initBranches();
        allbjets.clear();

        // Load selected branches with data from specified event
        bool readsuccess = treeReader->ReadEntry(entry);
        if (not readsuccess) {
            cout <<"can not read Entry through treeReader" << endl;
            exit(0);
        }
        cout <<" event id " << entry << endl;

        //loop all reco jets
        for (i =0;  i<  branchJet->GetEntries(); i++)
        {
            jet = (Jet*) branchJet->At(i);
            //Btag does not work here?????????
            //cout <<"Jet  eta "<< jet->Eta  <<" Pt "<< jet->PT <<" btag "<< jet->BTag << std::endl;
            if (jet->PT < jetsPt_ || abs(jet->Eta)> jetsEta_) continue;
            totjets_lorentz +=jet->P4();
            if ( not(jet->BTag) || jet->PT < bjetsPt_ || abs(jet->Eta)> bjetsEta_) continue;
            insertInJetVector(allbjets, jet);
            //allbjets.push_back(jet);

        }
        numbjets = allbjets.size();
        //  for (vector<Jet *>::iterator it = allbjets.begin(); it != allbjets.end(); it++){
//		printSortableObject<Jet>(*it);
//	}
        cout <<" size of bjet vector  " << allbjets.size() << endl;
        unsigned int b1at = 0;
        unsigned int b2at = 1;
        if (allbjets.size() < 2) {
            hasb1jet = false;
            hasb2jet = false;
        }
        else if (allbjets.size() == 2) {
            hasb1jet = true;
            hasb2jet = true;
            b1jet = allbjets.at(0);
            b2jet = allbjets.at(1);
        }
        else if (allbjets.size() > 2) {
            cout <<" event has more than two bjets passing acceptance cut " << endl;
            //find two bjets that have invariant mass close to 125
            float diff_higgsmass = 125.0;
            for (unsigned k =0; k<allbjets.size()-1; k++)
                for (unsigned int j =i+1; j<allbjets.size(); j++) {
                    TLorentzVector bjets_p4 = allbjets[k]->P4()+allbjets[j]->P4();
                    if (fabs(bjets_p4.M()-125)>diff_higgsmass) continue;
                    b1at = k;
                    b2at = j;
                    diff_higgsmass = fabs(bjets_p4.M()-125);
                }
            hasb1jet = true;
            hasb2jet = true;
        }

        // b1jet should be different from b2jet
        if (hasb1jet && hasb2jet) {
            b1jet = allbjets[b1at];
            b2jet = allbjets[b2at];
            b1jet_p4 = b1jet->P4();
            b1jet_px = b1jet_p4.Px();
            b1jet_py = b1jet_p4.Py();
            b1jet_pz= b1jet_p4.Pz();
            b1jet_energy = b1jet_p4.Energy();
            b1jet_eta = b1jet_p4.Eta();
            b1jet_phi = b1jet_p4.Phi();
            b1jet_pt = b1jet_p4.Pt();
            b2jet_p4 = b2jet->P4();
            b2jet_px = b2jet_p4.Px();
            b2jet_py = b2jet_p4.Py();
            b2jet_pz= b2jet_p4.Pz();
            b2jet_energy = b2jet_p4.Energy();
            b2jet_eta = b2jet_p4.Eta();
            b2jet_phi = b2jet_p4.Phi();
            b2jet_pt = b2jet_p4.Pt();
        }
        // TLorentzVector htobb_jets = b1jet->P4()+b2jet->P4();
        //cout <<" htobb_jet " << htobb_jets.M(); htobb_jets.Print();

        // Analyse missing ET, generator level

        //apply muon cuts on muons
        // Analyse missing ET, reco level
        if(branchMissingET->GetEntriesFast() > 0)
        {
            Met = (MissingET*) branchMissingET->At(0);
            met = Met->MET;
            met_phi = Met->Phi;
            met_px = Met->P4().Px();
            met_py = Met->P4().Py();
            Met_p4 = TLorentzVector();
            Met_p4.SetXYZT(met_px, met_py, 0, met);
            if (met > metPt_) hasMET = true;
        }

        //if (hasMET) cout <<" has MET " << met << endl;
        //apply muon cuts on muons
        //Loop over all Muon(before Isolation) in event
        //how to check whether deltaR matching and matching genparticle has same performance??

        // Loop over all Muon in event, reco muon
        for(i = 0; i < branchMuon->GetEntriesFast(); ++i)
        {
            muon = (Muon*) branchMuon->At(i);
//      printGenParticle(particle);
            //check charge and deltaR, genmu1: charge<0, genmu2: charge>0
            if (muon->Charge<0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_)
                //insertInVector<Muon>(allMuon1, muon);
                allMuon1.push_back(muon);
            else if(muon->Charge>0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_)
                //insertInVector<Muon>(allMuon2, muon);
                allMuon2.push_back(muon);
            //cout <<" muon eta " << muon->Eta << " phi " << muon->Phi << " Pt "<< muon->PT << endl;
        }

        numlepton1 = allMuon1.size();
        numlepton2 = allMuon1.size();
        if (allMuon1.size()>0 and allMuon2.size()>0) {
            lepton1_p4 = (allMuon1.at(0))->P4();
            haslepton1 =true;
            lepton2_p4 = (allMuon2.at(0))->P4();
            haslepton2 =true;
        }
        // Loop over all electrons in event
        // do similar thing for electron when electrons are also taken as final state
        /*
        for(i = 0; i < branchElectron->GetEntriesFast(); ++i)
        {
          electron = (Electron*) branchElectron->At(i);
          //particle = (GenParticle*) electron->Particle.GetObject();
          if (electron->Charge<0 and abs(electron->Eta)<electronsEta_ and electron->PT >= electronPt1_) {
        if ((not haselectron1) or (haselectron1 and electron->PT>electron1->PT))	electron1 = electron;
        haselectron1 = true;
        numlepton1++;
          }
          else if(electron->Charge>0 and abs(electron->Eta)<electronsEta_ and electron->PT >= electronPt1_) {
        if ((not haselectron2) or (haselectron2 and electron->PT>electron2->PT))	electron2 = electron;
            haselectron2 = true;
        numlepton2++;
          }
          //cout <<" electron "; printSortableObject<Electron>(electron);
        }

        if (hasmuon1){
        lepton1_p4 = muon1->P4();
        haslepton1 = true;
        }
        else if(haselectron1){
        lepton1_p4 = electron1->P4();
        haslepton1 = true;
        }
        if (haslepton2){
        lepton2_p4 = muon2->P4();
        haslepton2 = true;
        }
        else if (haselectron2){
        lepton2_p4 = electron2->P4();
        haslepton2 = true;
        }*/
        if (haslepton1) {
            lepton1_px = lepton1_p4.Px();
            lepton1_py = lepton1_p4.Py();
            lepton1_pz = lepton1_p4.Pz();
            lepton1_energy = lepton1_p4.E();
            lepton1_eta = lepton1_p4.Eta();
            lepton1_phi = lepton1_p4.Phi();
            lepton1_pt = lepton1_p4.Pt();
        }
        if (haslepton2) {
            lepton2_px = lepton2_p4.Px();
            lepton2_py = lepton2_p4.Py();
            lepton2_pz = lepton2_p4.Pz();
            lepton2_energy = lepton2_p4.E();
            lepton2_eta = lepton2_p4.Eta();
            lepton2_phi = lepton2_p4.Phi();
            lepton2_pt = lepton2_p4.Pt();
        }

        if (hasb1jet and hasb2jet and haslepton1 and haslepton2) {
            dR_b1l1 = b1jet_p4.DeltaR(lepton1_p4);
            dR_b1l2 = b1jet_p4.DeltaR(lepton2_p4);
            dR_b2l1 = b2jet_p4.DeltaR(lepton1_p4);
            dR_b2l2 = b2jet_p4.DeltaR(lepton2_p4);
            dR_b1b2 = b1jet_p4.DeltaR(b2jet_p4);
            dR_l1l2 = lepton1_p4.DeltaR(lepton2_p4);
            TLorentzVector ll_p4 = lepton1_p4+lepton2_p4;
            TLorentzVector bjets_p4 = b1jet_p4+b2jet_p4;
            dphi_llbb = TVector2::Phi_mpi_pi(ll_p4.Phi()-bjets_p4.Phi());
            mass_l1l2 = ll_p4.M();
            mass_b1b2 = bjets_p4.M();
            if (dR_b1l1>jetleptonDeltaR_ and dR_b1l2>jetleptonDeltaR_ and dR_b2l1>jetleptonDeltaR_ and dR_b2l2>jetleptonDeltaR_) hasdRljet =true;

        }

        // Loop over all tracks in event
        /*
        for(i = 0; i < branchTrack->GetEntriesFast(); ++i)
        {
          track = (Track*) branchTrack->At(i);
          particle = (GenParticle*) track->Particle.GetObject();
          cout <<" Track "; printGenParticle(particle);

        }*/
        // calculate the DeltaR between bjet and lepton
        if (runMMC_ and hasdRljet and hasMET) {
            //TLorentzVector bjets_lorentz=genb1jet->P4()+genb2jet->P4();
            //cout <<" m_{bjets} " << bjets_lorentz.M(); bjets_lorentz.Print();
            cout <<" start to run MMC for this event " << endl;
            TLorentzVector bjet_pt1_lorentz, bjet_pt2_lorentz, bgenp_pt1_lorentz, bgenp_pt2_lorentz;
            if (b1jet_p4.Pt()>b2jet_p4.Pt()) {
                bjet_pt1_lorentz = b1jet_p4;
                bjet_pt2_lorentz = b2jet_p4;
            } else {
                bjet_pt1_lorentz = b2jet_p4;
                bjet_pt2_lorentz = b1jet_p4;
            }
            TLorentzVector null_lorentz = TLorentzVector();
            bool simulation_ = false;
            bool weightfromonshellnupt_func = false;
            bool weightfromonshellnupt_hist = true;
            bool weightfromonoffshellWmass_hist=true;
            int iterations = 1000000;
            std::string RefPDFfile("MMCRefPDF.ROOT");
            bool useMET = true;
            int bjetrescaleAlgo = 2;//0.no correction; 1. simpel rescale, 2.elaborate rescale, -1.ideal case
            int onshellMarker_ =0;
            // rescale bjets in MMC?????
            //MMC *thismmc = new MMC();
            thismmc = new MMC(&lepton1_p4, &lepton2_p4, &bjet_pt1_lorentz, &bjet_pt2_lorentz, &totjets_lorentz, &Met_p4,
                              &null_lorentz, &null_lorentz, &null_lorentz, &null_lorentz, &null_lorentz,
                              onshellMarker_, simulation_, entry, weightfromonshellnupt_func, weightfromonshellnupt_hist, weightfromonoffshellWmass_hist,
                              iterations, RefPDFfile, useMET, bjetrescaleAlgo);

            if (thismmc->runMMC()) {
                //MMCtree =  (thismmc->getMMCTree())->CloneTree();
                //std::cout <<" MMCtree entries " << MMCtree->GetEntries() << std::endl;
                TH1F* MMC_h2mass =(TH1F*)(thismmc->getMMCh2()).Clone("MMC_h2mass");
                TH1F* MMC_h2mass_weight1 =(TH1F*)(thismmc->getMMCh2weight1()).Clone("MMC_h2massweight1");
                TH1F* MMC_h2mass_weight4 =(TH1F*)(thismmc->getMMCh2weight4()).Clone("MMC_h2massweight4");
                std::cout <<" Mass_h2mass in Analyzer " << std::endl;
                MMC_h2mass_prob = (MMC_h2mass->GetXaxis())->GetBinCenter(MMC_h2mass->GetMaximumBin());
                MMC_h2massweight1_prob = (MMC_h2mass_weight1->GetXaxis())->GetBinCenter(MMC_h2mass_weight1->GetMaximumBin());
                MMC_h2massweight4_prob = (MMC_h2mass_weight4->GetXaxis())->GetBinCenter(MMC_h2mass_weight4->GetMaximumBin());
                MMC_h2mass_RMS = MMC_h2mass->GetRMS();
                MMC_h2mass_Entries = MMC_h2mass->GetEntries();
                MMC_h2mass_Mean = MMC_h2mass->GetMean();
                int nbin=(MMC_h2mass->GetXaxis())->GetNbins();
                MMC_h2mass_overflow = MMC_h2mass->GetBinContent(nbin+1);
                MMC_h2mass_underflow = MMC_h2mass->GetBinContent(-1);
                std::cout <<" most prob " << MMC_h2mass_prob <<" RMS "<< MMC_h2mass_RMS << " entries " << MMC_h2mass_Entries
                          << " most prob weight1 "<< MMC_h2massweight1_prob <<" weight4 "<< MMC_h2massweight4_prob <<std::endl;
            }
            delete thismmc;
        }
//fill branches
        if (hasb1jet or hasb2jet or haslepton1 or haslepton2) evtree->Fill();
    }

}
Пример #3
0
void DiHiggstollbb::DiHiggstollbbrun()
{

	//loop over events 1. find h->b bbar; 2 find two bjets after cuts; 3 fill Tree
 // TClonesArray *branchParticle = treeReader->UseBranch("Particle");
  TClonesArray *branchElectron = treeReader->UseBranch("Electron");
  TClonesArray *branchPhoton = treeReader->UseBranch("Photon");
  TClonesArray *branchMuon = treeReader->UseBranch("Muon");
  //TClonesArray *branchMuonBeforeIso = treeReader->UseBranch("MuonBeforeIso");

  //TClonesArray *branchTrack = treeReader->UseBranch("Track");
//  TClonesArray *branchTower = treeReader->UseBranch("Tower");

  //TClonesArray *branchEFlowTrack = treeReader->UseBranch("EFlowTrack");
 // TClonesArray *branchEFlowPhoton = treeReader->UseBranch("EFlowPhoton");
 // TClonesArray *branchEFlowNeutralHadron = treeReader->UseBranch("EFlowNeutralHadron");
  TClonesArray *branchJet = treeReader->UseBranch("Jet");
  //TClonesArray *branchGenJet = treeReader->UseBranch("GenJet");

  TClonesArray *branchMissingET = treeReader->UseBranch("MissingET");
  //TClonesArray *branchGenMissingET = treeReader->UseBranch("GenMissingET");

  long allEntries = treeReader->GetEntries();

  cout << "** Chain contains " << allEntries << " events" << endl;

  long entry;
  Jet *jet, *b1jet, *b2jet;
  b1jet=0; b2jet=0;
  MissingET *Met;
  Met=0;
  //GenParticle *particle, *genh2, *genhiggs1, *genhiggs2, *genhtobb, *genb1, *genb2;
  //genhtobb =0; genb1= 0; genb2=0;
  //GenParticle *genhtoWW, *genW1, *genW2, *genmu1, *genmu2, *gennu1, *gennu2;
  //genmu1=0; genmu2=0; gennu1=0; gennu2=0;
  Electron *electron;
  Photon *photon;
  Muon *muon, *muon1, *muon2;// *muon1_beforeIso, *muon2_beforeIso;
  muon1 =0; muon2=0; //muon1_beforeIso =0; muon2_beforeIso =0;
  //Track *track;
  //Tower *tower;
  
  TLorentzVector momentum;
  TLorentzVector Muon1_p4, Muon2_p4, b1jet_p4, b2jet_p4;
  TLorentzVector totjets_lorentz = TLorentzVector();
  //incase compilation error

  int i =0;
  // Loop over all events
  //TFile *MMCfile = new TFile("testMMC.root", "recreate"); 
  if (nEvents < 0) nEvents = allEntries;
  for(entry = 0; entry < nEvents; ++entry)
  //for(entry = 0; entry < 1000; ++entry)
  //for(entry = 0; entry < 10000; ++entry)
  {
    //gen
    initBranches();

    // Load selected branches with data from specified event
    bool readsuccess = treeReader->ReadEntry(entry);
    if (not readsuccess) {
	cout <<"can not read Entry through treeReader" << endl;
	exit(0);
	}
    cout <<" event id " << entry << endl;
    /*for (i =0; i < branchParticle->GetEntries(); ++i){
	genP = (GenParticle*) branchParticle->At(i);
        //defualt M1 M2 D1 D2 is -1;
        cout << " genP Id " << genP->PID <<" Pt " << genP->PT << " M1 "<< genP->M1<<" M2 "<< genP->M2 << " D1 "<< genP->D1 <<" D2 "<<genP->D2<< endl;
        if ( genP->M1 >= 0 && genP->M1 <branchParticle->GetEntries()){
        	GenParticle *M1P= (GenParticle*) branchParticle->At(genP->M1);
        	cout <<" M1 Id " << M1P->PID <<" Pt " << M1P->PT << std::endl;
	}
        if ( genP->D1 >= 0 && genP->D1 <branchParticle->GetEntries()){
        	GenParticle *D1P= (GenParticle*) branchParticle->At(genP->D1);
        	cout <<" D1 Id " << D1P->PID <<" Pt " << D1P->PT << std::endl;
         }
     }
     genh2 =  (GenParticle*) branchParticle->At(0); //printGenParticle(genP);
     //printGenParticle(genh2); 
     h2tohh_mass = genh2->Mass;
     genhiggs1 =  (GenParticle*) branchParticle->At(genh2->D1); 
     genhiggs2 =  (GenParticle*) branchParticle->At(genh2->D2); 
     //printGenParticle(genhiggs2);
     while ((genhiggs1->D1>0 && ((GenParticle*)branchParticle->At(genhiggs1->D1))->PID == genhiggs1->PID) 
	    || (genhiggs1->D2>0 && ((GenParticle*)branchParticle->At(genhiggs1->D2))->PID == genhiggs1->PID)){
	if (genhiggs1->D1>0 && ((GenParticle*)branchParticle->At(genhiggs1->D1))->PID == genhiggs1->PID) 
		genhiggs1 = (GenParticle*)branchParticle->At(genhiggs1->D1);
        else   genhiggs1 = (GenParticle*)branchParticle->At(genhiggs1->D2);
     }
     while ((genhiggs2->D1>0 && ((GenParticle*)branchParticle->At(genhiggs2->D1))->PID == genhiggs2->PID) 
	    || (genhiggs2->D2>0 && ((GenParticle*)branchParticle->At(genhiggs2->D2))->PID == genhiggs2->PID)){
	if (genhiggs2->D1>0 && ((GenParticle*)branchParticle->At(genhiggs2->D1))->PID == genhiggs2->PID) 
		genhiggs2 = (GenParticle*)branchParticle->At(genhiggs2->D1);
        else   genhiggs2 = (GenParticle*)branchParticle->At(genhiggs2->D2);
     }
    if (abs(((GenParticle*)branchParticle->At(genhiggs1->D1))->PID) ==5 ) {
		//printGenParticle(genhiggs1);
		//printGenParticle((GenParticle*)branchParticle->At(genhiggs1->D1));
		//printGenParticle((GenParticle*)branchParticle->At(genhiggs1->D2));
		htobb = true;
		genhtobb = genhiggs1;
    }
    else if (abs(((GenParticle*)branchParticle->At(genhiggs2->D1))->PID) ==5 ) {
		//printGenParticle(genhiggs2);
		//printGenParticle((GenParticle*)branchParticle->At(genhiggs2->D1));
		//printGenParticle((GenParticle*)branchParticle->At(genhiggs2->D2));
		htobb = true;
		genhtobb = genhiggs2;
     }
  
    if (abs(((GenParticle*)branchParticle->At(genhiggs2->D1))->PID) == 24){
		htoWW = true;
		genhtoWW = genhiggs2;
    }else if (abs(((GenParticle*)branchParticle->At(genhiggs1->D1))->PID) == 24){
		htoWW = true;
		genhtoWW = genhiggs1;
     }
    
    if (htobb){
		if (((GenParticle*)branchParticle->At(genhtobb->D1))->PID == 5){
 			genb1 = (GenParticle*)branchParticle->At(genhtobb->D1);	
 			genb2 = (GenParticle*)branchParticle->At(genhtobb->D2);	
		} 
  		else {
 			genb1 = (GenParticle*)branchParticle->At(genhtobb->D2);	
 			genb2 = (GenParticle*)branchParticle->At(genhtobb->D1);	
		}
    //move on to "final state" bquarks
        //printGenParticle(genhtobb);
	getFinalState(genb1, branchParticle);
	getFinalState(genb2, branchParticle);
           b1_px = genb1->Px;
	   b1_py = genb1->Py;
	   b1_pz = genb1->Pz;
	   b1_eta = genb1->Eta;
	   b1_phi = genb1->Phi;
	   b1_pt = genb1->PT;
	   b1_energy = genb1->E;
	   b2_px = genb2->Px;
	   b2_py = genb2->Py;
	   b2_pz = genb2->Pz;
	   b2_eta = genb2->Eta;
	   b2_phi = genb2->Phi;
	   b2_pt = genb2->PT;
	   b2_energy = genb2->E;
           TLorentzVector bbbar_lorentz = genb1->P4()+genb2->P4();
           //cout << " bbbar_lorentz Mass "<< bbbar_lorentz.M(); bbbar_lorentz.Print();
           htobb_px = genhtobb->Px; htobb_py= genhtobb->Py; htobb_pz = genhtobb->Pz; htobb_energy = genhtobb->E;
	   htobb_mass = genhtobb->Mass;
    }

    if (htoWW){
		if (((GenParticle*)branchParticle->At(genhtoWW->D1))->PID == -24){//W-
 			genW1 = (GenParticle*)branchParticle->At(genhtoWW->D1);	//to muon(13)
 			genW2 = (GenParticle*)branchParticle->At(genhtoWW->D2);	
		} 
  		else {
 			genW1 = (GenParticle*)branchParticle->At(genhtoWW->D2);	
 			genW2 = (GenParticle*)branchParticle->At(genhtoWW->D1);	
		}
	
    	getFinalState(genW1, branchParticle);	
	//cout <<" htoWW genW1 "; printGenParticle(genW1);
    	getFinalState(genW2, branchParticle);	
	//cout <<" htoWW genW2 "; printGenParticle(genW2);

       if (genW1->D1>0 && ((GenParticle*)branchParticle->At(genW1->D1))->PID == 13){
	 	genmu1 = (GenParticle*)branchParticle->At(genW1->D1);
		gennu1 = (GenParticle*)branchParticle->At(genW1->D2);
		Wtomu1nu1 = true;
       }else if (genW1->D2 >0 && ((GenParticle*)branchParticle->At(genW1->D2))->PID == 13){
	 	genmu1 = (GenParticle*)branchParticle->At(genW1->D2);
		gennu1 = (GenParticle*)branchParticle->At(genW1->D1);
		Wtomu1nu1 = true;
	}
       if (genW2->D1>0 && ((GenParticle*)branchParticle->At(genW2->D1))->PID == -13){
	 	genmu2 = (GenParticle*)branchParticle->At(genW2->D1);
		gennu2 = (GenParticle*)branchParticle->At(genW2->D2);
		Wtomu2nu2 = true;
	} else if (genW2->D1>0 && ((GenParticle*)branchParticle->At(genW2->D2))->PID == -13){
	 	genmu2 = (GenParticle*)branchParticle->At(genW2->D2);
		gennu2 = (GenParticle*)branchParticle->At(genW2->D1);
		Wtomu2nu2 = true;
	}
    }
   	
    if (Wtomu1nu1){
    	getFinalState(genmu1, branchParticle);	
    	getFinalState(gennu1, branchParticle);	
	mu1_px = genmu1->Px; mu1_py = genmu1->Py; mu1_pz = genmu1->Pz; mu1_energy = genmu1->E;
	mu1_eta = genmu1->Eta; mu1_phi = genmu1->Phi; mu1_pt = genmu1->PT;
	nu1_px = gennu1->Px; nu1_py = gennu1->Py; nu1_pz = gennu1->Pz; nu1_energy = gennu1->E;
	nu1_eta = gennu1->Eta; nu1_phi = gennu1->Phi; nu1_pt = gennu1->PT;
   	//cout << "mu1 from W "; printGenParticle(genmu1);
    }
    if (Wtomu2nu2){
    	getFinalState(genmu2, branchParticle);	
    	getFinalState(gennu2, branchParticle);	
	mu2_px = genmu2->Px; mu2_py = genmu2->Py; mu2_pz = genmu2->Pz; mu2_energy = genmu2->E;
	mu2_eta = genmu2->Eta; mu2_phi = genmu2->Phi; mu2_pt = genmu2->PT;
	nu2_px = gennu2->Px; nu2_py = gennu2->Py; nu2_pz = gennu2->Pz; nu2_energy = gennu2->E;
	nu2_eta = gennu2->Eta; nu2_phi = gennu2->Phi; nu2_pt = gennu2->PT;
        //cout << "mu2 from W "; printGenParticle(genmu2);
    }
 
   //loop all Gen jets 
    for (i =0;  i<  branchGenJet->GetEntries(); i++)
    {
      genjet = (Jet*) branchGenJet->At(i);
      if (genjet->PT < bjetsPt_ || abs(genjet->Eta)> bjetsEta_) continue;
      TLorentzVector genjet_p4 = genjet->P4();
      if (htobb && genjet_p4.DeltaR(genb1->P4()) < dR_genb1jet && genjet_p4.DeltaR(genb1->P4())<jetsDeltaR_) {
	dR_genb1jet = genjet_p4.DeltaR(genb1->P4());
        genb1jet_px = genjet_p4.Px(); genb1jet_py = genjet_p4.Py(); genb1jet_pz=genjet_p4.Pz(); genb1jet_energy = genjet_p4.Energy();
        genb1jet_eta = genjet_p4.Eta(); genb1jet_phi = genjet_p4.Phi();
	genb1jet_pt = genjet_p4.Pt();
	hasgenb1jet = true;
	genb1jet = genjet;
	//cout <<"genb1jet pt "<< genb1jet_pt << endl;
        
       }
      if (htobb && genjet_p4.DeltaR(genb2->P4()) < dR_genb2jet && genjet_p4.DeltaR(genb2->P4()) < jetsDeltaR_){
        dR_genb2jet = genjet_p4.DeltaR(genb2->P4());
        genb2jet_px = genjet_p4.Px(); genb2jet_py = genjet_p4.Py(); genb2jet_pz=genjet_p4.Pz(); genb2jet_energy = genjet_p4.Energy();
        genb2jet_eta = genjet_p4.Eta(); genb2jet_phi = genjet_p4.Phi();
	genb2jet_pt = genjet_p4.Pt();
	hasgenb2jet = true;
	genb2jet = genjet;
	//cout <<"genb2jet pt "<< genb2jet_pt << endl;
       }
    }*/

   //loop all reco jets 
    for (i =0;  i<  branchJet->GetEntries(); i++)
    {
      jet = (Jet*) branchJet->At(i);
      if (jet->PT < jetsPt_ || abs(jet->Eta)> jetsEta_) continue;
      totjets_lorentz +=jet->P4();
      if (jet->PT < bjetsPt_ || abs(jet->Eta)> bjetsEta_) continue;
      TLorentzVector jet_p4 = jet->P4();
      //how to whether it a bjet, b1jet has larger PT
      if (jet->BTag and !hasb1jet and !hasb2jet) {
      	b1jet = jet;
	b1jet_p4 = jet_p4;
        b1jet_px = jet_p4.Px(); b1jet_py = jet_p4.Py(); b1jet_pz=jet_p4.Pz(); b1jet_energy = jet_p4.Energy();
	b1jet_eta = jet_p4.Eta(); b1jet_phi = jet_p4.Phi();
	b1jet_pt = jet_p4.Pt();
	//cout <<"b1jet pt "<< b1jet_pt << endl;
	hasb1jet = true;
        
       }
      else if (jet->BTag and hasb1jet and ((!hasb2jet and jet->PT<b1jet->PT) || (hasb2jet and jet->PT>b2jet->PT))){
	b2jet = jet;
	b2jet_p4 = jet_p4;
        b2jet_px = jet_p4.Px(); b2jet_py = jet_p4.Py(); b2jet_pz=jet_p4.Pz(); b2jet_energy = jet_p4.Energy();
	b2jet_eta = jet_p4.Eta(); b2jet_phi = jet_p4.Phi();
	b2jet_pt = jet_p4.Pt();
	hasb2jet = true;
	//cout <<"b2jet pt "<< b2jet_pt << endl;
       }
      else if (jet->BTag and hasb1jet and jet->PT>b1jet->PT){
	b2jet = b1jet;
	b2jet_p4 = b1jet->P4();
        b2jet_px = b2jet_p4.Px(); b2jet_py = b2jet_p4.Py(); b2jet_pz=b2jet_p4.Pz(); b2jet_energy = b2jet_p4.Energy();
	b2jet_eta = b2jet_p4.Eta(); b2jet_phi = b2jet_p4.Phi();
	b2jet_pt = b2jet_p4.Pt();

      	b1jet = jet;
	b1jet_p4 = jet_p4;
        b1jet_px = jet_p4.Px(); b1jet_py = jet_p4.Py(); b1jet_pz=jet_p4.Pz(); b1jet_energy = jet_p4.Energy();
	b1jet_eta = jet_p4.Eta(); b1jet_phi = jet_p4.Phi();
	b1jet_pt = jet_p4.Pt();
	}
       
    }
      // b1jet should be different from b2jet
    if (hasb1jet && hasb2jet && b1jet == b2jet){
	hasb1jet = false;
	hasb2jet = false;
     }
    else if (hasb1jet && hasb2jet){
     cout <<" b1jet Pt " << b1jet->PT  <<"  b2jet Pt " << b2jet->PT << endl; 
    // TLorentzVector htobb_jets = b1jet->P4()+b2jet->P4();
     //cout <<" htobb_jet " << htobb_jets.M(); htobb_jets.Print();
    }
    
    // Analyse missing ET, generator level
    /*
    if(branchGenMissingET->GetEntriesFast() > 0)
    {
      genMet = (MissingET*) branchGenMissingET->At(0);
      genmet = genMet->MET;
      genmet_phi = genMet->Phi;
      genmet_px = genMet->P4().Px();
      genmet_py = genMet->P4().Py();
      if (genmet > metPt_) hasGenMET = true;
      //cout <<" has genMET " << genmet << endl;
    }*/

    //apply muon cuts on muons 
    // Analyse missing ET, reco level
    if(branchMissingET->GetEntriesFast() > 0)
    {
      Met = (MissingET*) branchMissingET->At(0);
      met = Met->MET;
      met_phi = Met->Phi;
      met_px = Met->P4().Px();
      met_py = Met->P4().Py();
      if (met > metPt_) hasMET = true;
    }

    if (hasMET) cout <<" has MET " << met << endl;
    //apply muon cuts on muons 
    //Loop over all Muon(before Isolation) in event
    //how to check whether deltaR matching and matching genparticle has same performance??
    /*
    for(i = 0; i < branchMuonBeforeIso->GetEntriesFast(); ++i)
    {
      muon = (Muon*) branchMuonBeforeIso->At(i);
      TLorentzVector muon_p4 = muon->P4(); 
      //
      //particle = (GenParticle*) muon->Particle.GetObject();
//      printGenParticle(particle);
//      how to choose muon if more than one muon could pass acceptance cuts
      if (muon->Charge<0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_) {
	muon1_beforeIso = muon;
	hasMuon1_beforeIso = true;
      }
      else if(muon->Charge>0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_) {
	muon2_beforeIso = muon;
        hasMuon2_beforeIso = true;
      }
      if (hasMuon1_beforeIso) std::cout <<" has reco Muon1 before Iso  " << std::endl;
      if (hasMuon2_beforeIso) std::cout <<" has reco Muon2 before Iso  " << std::endl;
    }
    if (hasMuon1_beforeIso){
	Muon1_beforeIso_px = muon1_beforeIso->P4().Px(); Muon1_beforeIso_py = muon1_beforeIso->P4().Py(); Muon1_beforeIso_pz = muon1_beforeIso->P4().Pz(); Muon1_beforeIso_energy = muon1_beforeIso->P4().E();
	Muon1_beforeIso_eta = muon1_beforeIso->P4().Eta(); Muon1_beforeIso_phi = muon1_beforeIso->P4().Phi();
	Muon1_pt = muon1_beforeIso->PT;
	}
    if (hasMuon2_beforeIso){
	Muon2_beforeIso_px = muon2_beforeIso->P4().Px(); Muon2_beforeIso_py = muon2_beforeIso->P4().Py(); Muon2_beforeIso_pz = muon2_beforeIso->P4().Pz(); Muon2_beforeIso_energy = muon2_beforeIso->P4().E();
	Muon2_beforeIso_eta = muon2_beforeIso->P4().Eta(); Muon2_beforeIso_phi = muon2_beforeIso->P4().Phi();
	Muon2_pt = muon2_beforeIso->PT;
	}
      */
    // Loop over all Muon in event, reco muon
    for(i = 0; i < branchMuon->GetEntriesFast(); ++i)
    {
      muon = (Muon*) branchMuon->At(i);
      TLorentzVector muon_p4 = muon->P4(); 
//      printGenParticle(particle);
      //if (hasMuon1_beforeIso and  muon->Eta == muon1_beforeIso->Eta and muon->Phi == muon1_beforeIso->Phi) Muon1_beforeIso_passIso = true;
      //if (hasMuon2_beforeIso and  muon->Eta == muon2_beforeIso->Eta and muon->Phi == muon2_beforeIso->Phi) Muon2_beforeIso_passIso = true;
      //check charge and deltaR, genmu1: charge<0, genmu2: charge>0
      if (muon->Charge<0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_) {
	muon1 = muon;
	hasMuon1 = true;
      }
      else if(muon->Charge>0 and abs(muon->Eta)<muonsEta_ and muon->PT >= muonPt1_) {
	muon2 = muon;
        hasMuon2 = true;
      }
      if (hasMuon1) std::cout <<" has reco Muon1 " << std::endl;
      if (hasMuon2) std::cout <<" has reco Muon2 " << std::endl;
      //cout <<" muon eta " << muon->Eta << " phi " << muon->Phi << " Pt "<< muon->PT << endl; 
    }

    if (hasMuon1){
	Muon1_p4 = muon1->P4();
	Muon1_px = muon1->P4().Px(); Muon1_py = muon1->P4().Py(); Muon1_pz = muon1->P4().Pz(); Muon1_energy = muon1->P4().E();
	Muon1_eta = muon1->P4().Eta(); Muon1_phi = muon1->P4().Phi(); Muon1_pt = muon1->PT;
	}
    if (hasMuon2){
	Muon2_p4 = muon2->P4();
	Muon2_px = muon2->P4().Px(); Muon2_py = muon2->P4().Py(); Muon2_pz = muon2->P4().Pz(); Muon2_energy = muon2->P4().E();
	Muon2_eta = muon2->P4().Eta(); Muon2_phi = muon2->P4().Phi(); Muon2_pt = muon2->PT;
	}
   
    if (hasMuon1 && hasMuon2){
    if (((muon1->PT > muonPt1_ && muon2->PT > muonPt2_) || (muon1->PT > muonPt2_ && muon2->PT > muonPt1_)) 
	&& fabs(muon1->Eta)<muonsEta_ && fabs(muon2->Eta)< muonsEta_) hastwomuons =true;
    }
    // Loop over all electrons in event
    // do similar thing for electron when electrons are also taken as final state
    for(i = 0; i < branchElectron->GetEntriesFast(); ++i)
    {
      electron = (Electron*) branchElectron->At(i);
      //particle = (GenParticle*) electron->Particle.GetObject();
      cout <<" electron "; printSortableObject<Electron>(electron);
    }

    // Loop over all photons in event
    for(i = 0; i < branchPhoton->GetEntriesFast(); ++i)
    {
      photon = (Photon*) branchPhoton->At(i);

      // skip photons with references to multiple particles
      if(photon->Particles.GetEntriesFast() != 1) continue;

      //particle = (GenParticle*) photon->Particles.At(0);
      cout <<" photon "; printSortableObject<Photon>(photon);
    }

   if (hasb1jet and hasb2jet and hasMuon1 and hasMuon2){
	dR_b1l1 = b1jet_p4.DeltaR(Muon1_p4);
	dR_b1l2 = b1jet_p4.DeltaR(Muon2_p4);
	dR_b2l1 = b2jet_p4.DeltaR(Muon1_p4);
	dR_b2l2 = b2jet_p4.DeltaR(Muon2_p4);
	dR_b1b2 = b1jet_p4.DeltaR(b2jet_p4);
	dR_l1l2 = Muon1_p4.DeltaR(Muon2_p4);
	TLorentzVector ll_p4 = Muon1_p4+Muon2_p4;
	TLorentzVector bjets_p4 = b1jet_p4+b2jet_p4;
	mass_l1l2 = ll_p4.M();
        mass_b1b2 = bjets_p4.M();
        if (dR_b1l1>jetleptonDeltaR_ and dR_b1l2>jetleptonDeltaR_ and dR_b2l1>jetleptonDeltaR_ and dR_b2l2>jetleptonDeltaR_) hasdRljet =true;
       
    }
     
    // Loop over all tracks in event
    /*
    for(i = 0; i < branchTrack->GetEntriesFast(); ++i)
    {
      track = (Track*) branchTrack->At(i);
      particle = (GenParticle*) track->Particle.GetObject();
      cout <<" Track "; printGenParticle(particle);

    }*/
   // calculate the DeltaR between bjet and lepton 

    if (runMMC_ and hasdRljet){
	 TLorentzVector bjets_lorentz=b1jet->P4()+b2jet->P4();
          
         cout <<" m_{bjets} " << bjets_lorentz.M(); bjets_lorentz.Print();
	 TLorentzVector met_lorentz = Met->P4();
	 bool weightfromonshellnupt_func = false;
         bool weightfromonshellnupt_hist = true;
	 bool weightfromonoffshellWmass_hist=true; 
	 int iterations = 100000;
	 std::string RefPDFfile("MMCRefPDF.ROOT");
	 bool useMET = true;
         bool onshellMarker_ = -1;
	 // rescale bjets in MMC?????
        //MMC *thismmc = new MMC();
        TLorentzVector null_lorentz = TLorentzVector(); 
         MMC *thismmc = new MMC(Muon1_p4, Muon2_p4, bjets_lorentz, totjets_lorentz, 
          met_lorentz, null_lorentz, null_lorentz, null_lorentz,
	   null_lorentz, onshellMarker_,// only for simulation 
          false, entry, weightfromonshellnupt_func, weightfromonshellnupt_hist, weightfromonoffshellWmass_hist,
          iterations, RefPDFfile, useMET);
	  thismmc->runMMC();
	  TTree *mmctree = (thismmc->getMMCTree())->CloneTree();
          std::cout <<"MMCTree entries " << (thismmc->getMMCTree())->GetEntries() << std::endl;
          std::cout <<"testtree entries " << mmctree->GetEntries()<<" title "<< mmctree->GetTitle() << std::endl;
	  //esttree->SetDirectory((TDirectory*)MMCfile);
	  //MMCfile->WriteObject(mmctree,mmctree->GetTitle());
	delete thismmc;
	}
//fill branches
    if (hasb1jet or hasb2jet or hasMuon1 or hasMuon2) evtree->Fill();
    }
 
}