Bool_t BranchBase::EventCuts() {

    int event = 0;

    // cleaning
    if(isData){
    	if(  
    
		isGoodEvent(RunNumber,lbn,bcid) ==1 && 
		
		//Good vertex
		(vx_n > 1)  &&
		(vx_nTracks->at(0) > 2) && 

		//mbts timing
		//    if ( (mbtime_timeA == 75) || (mbtime_timeC == 75)) continue;
		//    if ( (mbtime_timeA ==-75) || (mbtime_timeC ==-75)) continue;
		//    if ( (mbtime_timeA == 0 ) || (mbtime_timeC == 0 ))  continue;

		((fabs(mbtime_timeA-mbtime_timeC) < 3)) 
		
     		) event = 1;    
     }

     else if(isMC){
	    	if(  
    
		//Good vertex
		(vx_n > 1)  &&
		(vx_nTracks->at(0) > 2)  

     		) event = 1;    
 
     }

     else{ 
     	std::cout << "Dataset must be flagged as MC or DATA. Abort " << std::endl;
	exit(0);
     }

    Fcal_Et=(cccEt_Et_Eh_FCal->at(0)+cccEt_Et_Eh_FCal->at(1)+cccEt_Et_Eh_FCal->at(2))/1000000.0;  //Fcal energy in TeV

    hasHiPtMuon=0;

    //additional missPt and phi with 
    // global vx parameters but w/o trk cuts
    for(int ivx = 0; ivx<1; ivx++){
        pTmissID_nocuts.SetPxPyPzE(vx_px->at(ivx),vx_py->at(ivx),vx_pz->at(ivx),vx_E->at(ivx));
        nu_pt_nocuts = pTmissID_nocuts.Pt();
        nu_phi_nocuts = pTmissID_nocuts.Phi();
    } //PV

    return event;

}
ScopedXmlPullParser::~ScopedXmlPullParser() {
    while (isGoodEvent(next()));
}
Пример #3
0
void Ntp1Analyzer_QG::Loop()
{


   DEBUG_VERBOSE_ = false;

   if (fChain == 0) return;


   Long64_t nentries;

   if( DEBUG_ ) nentries = 100000;
   else nentries = fChain->GetEntries();


   Long64_t nbytes = 0, nb = 0;

   TRandom3 rand;

   for (Long64_t jentry=0; jentry<nentries;jentry++) {
     Long64_t ientry = LoadTree(jentry);
     if (ientry < 0) break;
     nb = fChain->GetEntry(jentry);   nbytes += nb;
     // if (Cut(ientry) < 0) continue;

if( DEBUG_VERBOSE_ ) std::cout << "entry n." << jentry << std::endl;

     if( (jentry%100000) == 0 ) std::cout << "Event #" << jentry  << " of " << nentries << std::endl;


   //HLT_Mu11_ = this->PassedHLT("HLT_Mu11");
   //HLT_Ele17_SW_EleId_L1R_ = this->PassedHLT("HLT_Ele17_SW_EleId_L1R");
   //HLT_DoubleMu3_ = this->PassedHLT("HLT_DoubleMu3");

     run_ = runNumber;
     LS_ = lumiBlock;
     event_ = eventNumber;
     eventWeight_ = -1.; //default
     nvertex_ = nPV;
     rhoPF_ = rhoFastjet;

     if( !isGoodEvent(jentry) ) continue; //this takes care also of integrated luminosity and trigger

     if( nPV==0 ) continue;
     bool goodVertex = (ndofPV[0] >= 4.0 && sqrt(PVxPV[0]*PVxPV[0]+PVyPV[0]*PVyPV[0]) < 2. && fabs(PVzPV[0]) < 24. );
     if( !goodVertex ) continue;
  
     for( unsigned iBX=0; iBX<nBX; ++iBX ) {
       if( bxPU[iBX]==0 ) nPU_ = nPU[iBX]; 
     }


     ptHat_ = (isMC_) ? genPtHat : ptHat_;

     if( isMC_ ) 
       if( (ptHat_ > ptHatMax_) || (ptHat_ < ptHatMin_) ) continue;


     bool noLeptons = false;
     TLorentzVector lept1MC, lept2MC;
     int zIndexqq=-1;
     int zIndexll=-1;



     // -----------------------------
     //      FROM NOW ON RECO
     // -----------------------------




     // ------------------
     // MUONS
     // ------------------

     std::vector<TLorentzVector> muons;
     int chargeFirstMuon;

     for( unsigned int iMuon=0; iMuon<nMuon && (muons.size()<2); ++iMuon ) {

       TLorentzVector thisMuon( pxMuon[iMuon], pyMuon[iMuon], pzMuon[iMuon], energyMuon[iMuon] );

       // --------------
       // kinematics:
       // --------------
       if( thisMuon.Pt() < 20. ) continue;


       // --------------
       // ID:
       // --------------
       if( !( (muonIdMuon[iMuon]>>8)&1 ) ) continue; //GlobalMuonPromptTight
       if( !( (muonIdMuon[iMuon]>>11)&1 ) ) continue; //AllTrackerMuons
       if( pixelHitsTrack[trackIndexMuon[iMuon]]==0 ) continue;


       // to compute dxy, look for primary vertex:
       int hardestPV = -1;
       float sumPtMax = 0.0;
       for(int v=0; v<nPV; v++) {
         if(SumPtPV[v] > sumPtMax) {
           sumPtMax = SumPtPV[v];
           hardestPV = v;
         }
       }  
   
       float dxy;
       if( hardestPV==-1 ) {
         dxy = 0.;
       } else {
         dxy = fabs(trackDxyPV(PVxPV[hardestPV], PVyPV[hardestPV], PVzPV[hardestPV],
                              trackVxTrack[trackIndexMuon[iMuon]], trackVyTrack[trackIndexMuon[iMuon]], trackVzTrack[trackIndexMuon[iMuon]],
                              pxTrack[trackIndexMuon[iMuon]], pyTrack[trackIndexMuon[iMuon]], pzTrack[trackIndexMuon[iMuon]]));
       }

       if( dxy > 0.02 ) continue;


       float dz = fabs(trackVzTrack[trackIndexMuon[iMuon]]-PVzPV[hardestPV]);
       if(dz > 1.0) continue;



       // --------------
       // isolation:
       // --------------
       // (this is sum pt tracks)
       //if( sumPt03Muon[iMuon] >= 3. ) continue;
       // combined isolation < 15%:
       if( (sumPt03Muon[iMuon] + emEt03Muon[iMuon] + hadEt03Muon[iMuon]) >= 0.15*thisMuon.Pt() ) continue;



       // for now simple selection, will have to optimize this (T&P?)
       if( muons.size()==0 ) {
         muons.push_back( thisMuon );
         chargeFirstMuon = chargeMuon[iMuon];
       } else {
         if( chargeMuon[iMuon]==chargeFirstMuon ) continue;
         if( fabs(muons[0].Eta())>2.1 && fabs(thisMuon.Eta())>2.1 ) continue;
         muons.push_back(thisMuon);
       }

     } //for muons



     // ------------------
     // ELECTRONS
     // ------------------

     std::vector<TLorentzVector> electrons;
     int chargeFirstEle = 0;
     bool firstPassedVBTF80 = false;

     for( unsigned int iEle=0; (iEle<nEle) && (electrons.size()<2); ++iEle ) {

       TLorentzVector thisEle( pxEle[iEle], pyEle[iEle], pzEle[iEle], energyEle[iEle] );

       // --------------
       // kinematics:
       // --------------
       if( thisEle.Pt() < 20. ) continue;
       if( (fabs(thisEle.Eta()) > 2.5) || ( fabs(thisEle.Eta())>1.4442 && fabs(thisEle.Eta())<1.566) ) continue;


       Float_t dr03TkSumPt_thresh95;
       Float_t dr03EcalRecHitSumEt_thresh95;
       Float_t dr03HcalTowerSumEt_thresh95;
       Float_t combinedIsoRel_thresh95;
       Float_t sigmaIetaIeta_thresh95;
       Float_t deltaPhiAtVtx_thresh95;
       Float_t deltaEtaAtVtx_thresh95;
       Float_t hOverE_thresh95;

       Float_t dr03TkSumPt_thresh80;
       Float_t dr03EcalRecHitSumEt_thresh80;
       Float_t dr03HcalTowerSumEt_thresh80;
       Float_t combinedIsoRel_thresh80;
       Float_t sigmaIetaIeta_thresh80;
       Float_t deltaPhiAtVtx_thresh80;
       Float_t deltaEtaAtVtx_thresh80;
       Float_t hOverE_thresh80;

       if( fabs(thisEle.Eta())<1.4442 ) {
         dr03TkSumPt_thresh95 = 0.15;
         dr03EcalRecHitSumEt_thresh95 = 2.;
         dr03HcalTowerSumEt_thresh95 = 0.12;
         combinedIsoRel_thresh95 = 0.15;

         dr03TkSumPt_thresh80 = 0.09;
         dr03EcalRecHitSumEt_thresh80 = 0.07;
         dr03HcalTowerSumEt_thresh80 = 0.10;
         combinedIsoRel_thresh80 = 0.07;

         sigmaIetaIeta_thresh95 = 0.01;
         deltaPhiAtVtx_thresh95 = 0.8;
         deltaEtaAtVtx_thresh95 = 0.007;
         hOverE_thresh95 = 0.15;

         sigmaIetaIeta_thresh80 = 0.01;
         deltaPhiAtVtx_thresh80 = 0.06;
         deltaEtaAtVtx_thresh80 = 0.004;
         hOverE_thresh80 = 0.04;
       } else {
         dr03TkSumPt_thresh95 = 0.08;
         dr03EcalRecHitSumEt_thresh95 = 0.06;
         dr03HcalTowerSumEt_thresh95 = 0.05;
         combinedIsoRel_thresh95 = 0.1;

         dr03TkSumPt_thresh80 = 0.04;
         dr03EcalRecHitSumEt_thresh80 = 0.05;
         dr03HcalTowerSumEt_thresh80 = 0.025;
         combinedIsoRel_thresh80 = 0.06;

         sigmaIetaIeta_thresh80 = 0.03;
         deltaPhiAtVtx_thresh80 = 0.7;
         deltaEtaAtVtx_thresh80 = 0.007;
         hOverE_thresh80 = 0.025;

         sigmaIetaIeta_thresh95 = 0.03;
         deltaPhiAtVtx_thresh95 = 0.7;
         deltaEtaAtVtx_thresh95 = 0.01; 
         hOverE_thresh95 = 0.07;
       }


       // --------------
       // isolation:
       // --------------
     //// no relative iso, using combined
     //if( dr03TkSumPtEle[iEle]/thisEle.Pt() > dr03TkSumPt_thresh ) continue;
     //if( dr03EcalRecHitSumEtEle[iEle]/thisEle.Pt() > dr03EcalRecHitSumEt_thresh ) continue;
     //if( dr03HcalTowerSumEtEle[iEle]/thisEle.Pt() > dr03HcalTowerSumEt_thresh ) continue;

       Float_t combinedIsoRel;
       if( fabs(thisEle.Eta())<1.4442 )
         combinedIsoRel = ( dr03TkSumPtEle[iEle] + TMath::Max(0., dr03EcalRecHitSumEtEle[iEle] - 1.) + dr03HcalTowerSumEtEle[iEle] ) / thisEle.Pt();
       else
         combinedIsoRel = ( dr03TkSumPtEle[iEle] + dr03EcalRecHitSumEtEle[iEle] + dr03HcalTowerSumEtEle[iEle] ) / thisEle.Pt();

       bool iso_VBTF95 = (combinedIsoRel < combinedIsoRel_thresh95);
       bool iso_VBTF80 = (combinedIsoRel < combinedIsoRel_thresh80);

       
       // --------------
       // electron ID:
       // --------------
       bool eleID_VBTF95 = (covIEtaIEtaSC[iEle] < sigmaIetaIeta_thresh95) &&
                           (fabs(deltaPhiAtVtxEle[iEle]) < deltaPhiAtVtx_thresh95) &&
                           (fabs(deltaEtaAtVtxEle[iEle]) < deltaEtaAtVtx_thresh95) &&
                           (hOverEEle[iEle] < hOverE_thresh95);

       bool eleID_VBTF80 = (covIEtaIEtaSC[iEle] < sigmaIetaIeta_thresh80) &&
                           (fabs(deltaPhiAtVtxEle[iEle]) < deltaPhiAtVtx_thresh80) &&
                           (fabs(deltaEtaAtVtxEle[iEle]) < deltaEtaAtVtx_thresh80) &&
                           (hOverEEle[iEle] < hOverE_thresh80);

       bool passed_VBTF95 = (iso_VBTF95 && eleID_VBTF95);
       bool passed_VBTF80 = (iso_VBTF80 && eleID_VBTF80);


       if( !passed_VBTF95 ) continue;

       // check that not matched to muon (clean electrons faked by muon MIP):
       bool matchedtomuon=false;
       for( std::vector<TLorentzVector>::iterator iMu=muons.begin(); iMu!=muons.end(); ++iMu )
         if( iMu->DeltaR(thisEle)<0.1 ) matchedtomuon=true;

       if( matchedtomuon ) continue;


       // for now simple selection, will have to optimize this (T&P?)
       // one electron required to pass VBTF80, the other VBTF95
       if( electrons.size()==0 ) {
         electrons.push_back( thisEle );
         chargeFirstEle = chargeEle[iEle];
         if( passed_VBTF80 ) firstPassedVBTF80 = true;
       } else if( chargeEle[iEle] != chargeFirstEle && ( firstPassedVBTF80||passed_VBTF80 ) ) {
         electrons.push_back( thisEle );
       }


     } //for electrons


     if( requireLeptons_ )
       if( electrons.size() < 2 && muons.size() < 2 ) continue;


     std::vector< TLorentzVector > leptons;

     if( electrons.size() == 2 && muons.size() == 2 ) { //veto H->ZZ->4l

       continue;

     } else if( electrons.size() == 2 ) {

       leptType_ = 1;

       if( electrons[0].Pt() > electrons[1].Pt() ) {

         leptons.push_back( electrons[0] );
         leptons.push_back( electrons[1] );

       } else {

         leptons.push_back( electrons[1] );
         leptons.push_back( electrons[0] );

       }

     } else if( muons.size() == 2 ) {

       leptType_ = 0;

       if( muons[0].Pt() > muons[1].Pt() ) {

         leptons.push_back( muons[0] );
         leptons.push_back( muons[1] );

       } else {

         leptons.push_back( muons[1] );
         leptons.push_back( muons[0] );

       }

     } else {

     //std::cout << "There must be an error this is not possible." << std::endl;
     //exit(9101);

     }

     
     eLept1_ = (leptons.size()>0) ? leptons[0].Energy() : 0.;
     ptLept1_ = (leptons.size()>0) ? leptons[0].Pt() : 0.;
     etaLept1_ = (leptons.size()>0) ? leptons[0].Eta() : 0.;
     phiLept1_ = (leptons.size()>0) ? leptons[0].Phi() : 0.;
     
     eLept2_ = (leptons.size()>1) ? leptons[1].Energy() : 0.;
     ptLept2_ = (leptons.size()>1) ? leptons[1].Pt() : 0.;
     etaLept2_ = (leptons.size()>1) ? leptons[1].Eta() : 0.;
     phiLept2_ = (leptons.size()>1) ? leptons[1].Phi() : 0.;


     // --------------------
     // match leptons to MC:
     // --------------------
     int correctIdMc = (leptType_==0 ) ? 13 : 11;

     for( unsigned iLept=0; iLept<leptons.size(); ++iLept ) {

       float deltaRmin = 100.;
       TLorentzVector matchedLeptonMC;

       for( unsigned iMc=0; iMc<nMc; ++iMc ) {

         if( statusMc[iMc]==1 && fabs(idMc[iMc])==correctIdMc && idMc[mothMc[mothMc[iMc]]]==23 ) {

           TLorentzVector* thisParticle = new TLorentzVector();
           thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );
           float thisDeltaR = leptons[iLept].DeltaR( *thisParticle );
           if( thisDeltaR < deltaRmin ) {
             deltaRmin = thisDeltaR;
             matchedLeptonMC = *thisParticle;
           }

           delete thisParticle;
           thisParticle = 0;

         } //if correct id mc

       } // for i mc


     } //for i leptons



     // ------------------
     // JETS
     // ------------------

     float jetPt_thresh = 20.;

     // first save leading jets in event:
     std::vector<AnalysisJet> leadJets;
     std::vector<int> leadJetsIndex; //index in the event collection (needed afterwards for PFCandidates)


     if( chargedHadronSubtraction_ ) {

       for( unsigned int iJet=0; iJet<nAK5PFNoPUJet; ++iJet ) {

         AnalysisJet thisJet( pxAK5PFNoPUJet[iJet], pyAK5PFNoPUJet[iJet], pzAK5PFNoPUJet[iJet], energyAK5PFNoPUJet[iJet] );

         // save at least 3 lead jets (if event has them) and all jets with pt>thresh:
         if( leadJets.size()>=3 && thisJet.Pt()<jetPt_thresh ) break;

         // far away from leptons:
         if( leptons.size()>0 )
           if( thisJet.DeltaR( leptons[0] ) <= 0.5 ) continue;
         if( leptons.size()>1 )
           if( thisJet.DeltaR( leptons[1] ) <= 0.5 ) continue;

         thisJet.nCharged = chargedHadronMultiplicityAK5PFNoPUJet[iJet] +
                            electronMultiplicityAK5PFNoPUJet[iJet] + 
                            muonMultiplicityAK5PFNoPUJet[iJet];
         thisJet.nNeutral = neutralHadronMultiplicityAK5PFNoPUJet[iJet] +
                            photonMultiplicityAK5PFNoPUJet[iJet] + 
                            HFHadronMultiplicityAK5PFNoPUJet[iJet] +
                            HFEMMultiplicityAK5PFNoPUJet[iJet];
         thisJet.ptD = ptDAK5PFNoPUJet[iJet];
         thisJet.rmsCand = rmsCandAK5PFNoPUJet[iJet];

         thisJet.axis1 = axis1AK5PFNoPUJet[iJet];
         thisJet.axis2 = axis2AK5PFNoPUJet[iJet];
         thisJet.pull = pullAK5PFNoPUJet[iJet];
         thisJet.tana = tanaAK5PFNoPUJet[iJet];

         thisJet.ptD_QC = ptD_QCAK5PFNoPUJet[iJet];
         thisJet.rmsCand_QC = rmsCand_QCAK5PFNoPUJet[iJet];
         thisJet.axis1_QC = axis1_QCAK5PFNoPUJet[iJet];
         thisJet.axis2_QC = axis2_QCAK5PFNoPUJet[iJet];
         thisJet.pull_QC = pull_QCAK5PFNoPUJet[iJet];
         thisJet.tana_QC = tana_QCAK5PFNoPUJet[iJet];

         thisJet.nChg_ptCut = nChg_ptCutAK5PFNoPUJet[iJet];
         thisJet.nChg_QC = nChg_QCAK5PFNoPUJet[iJet];
         thisJet.nChg_ptCut_QC = nChg_ptCut_QCAK5PFNoPUJet[iJet];
         thisJet.nNeutral_ptCut = nNeutral_ptCutAK5PFNoPUJet[iJet];

         thisJet.Rchg = RchgAK5PFNoPUJet[iJet];
         thisJet.Rneutral = RneutralAK5PFNoPUJet[iJet];
         thisJet.R = RAK5PFNoPUJet[iJet];
         thisJet.Rchg_QC = Rchg_QCAK5PFNoPUJet[iJet];

         thisJet.pTMax = pTMaxAK5PFNoPUJet[iJet];
         thisJet.pTMaxChg = pTMaxChgAK5PFNoPUJet[iJet];
         thisJet.pTMaxNeutral = pTMaxNeutralAK5PFNoPUJet[iJet];
         thisJet.pTMaxChg_QC = pTMaxChg_QCAK5PFNoPUJet[iJet];

         thisJet.betastar = betastarAK5PFNoPUJet[iJet];


         leadJets.push_back(thisJet);
         leadJetsIndex.push_back(iJet);


       } //for jets

     } else { // 'normal' PFJets:

       for( unsigned int iJet=0; iJet<nAK5PFPUcorrJet; ++iJet ) {

         AnalysisJet thisJet( pxAK5PFPUcorrJet[iJet], pyAK5PFPUcorrJet[iJet], pzAK5PFPUcorrJet[iJet], energyAK5PFPUcorrJet[iJet] );

         // save at least 3 lead jets (if event has them) and all jets with pt>thresh:
         if( leadJets.size()>=3 && thisJet.Pt()<jetPt_thresh ) break;

         // far away from leptons:
         if( leptons.size()>0 )
           if( thisJet.DeltaR( leptons[0] ) <= 0.5 ) continue;
         if( leptons.size()>1 )
           if( thisJet.DeltaR( leptons[1] ) <= 0.5 ) continue;

         thisJet.nCharged = chargedHadronMultiplicityAK5PFPUcorrJet[iJet] +
                            electronMultiplicityAK5PFPUcorrJet[iJet] + 
                            muonMultiplicityAK5PFPUcorrJet[iJet];
         thisJet.nNeutral = neutralHadronMultiplicityAK5PFPUcorrJet[iJet] +
                            photonMultiplicityAK5PFPUcorrJet[iJet] + 
                            HFHadronMultiplicityAK5PFPUcorrJet[iJet] +
                            HFEMMultiplicityAK5PFPUcorrJet[iJet];
         thisJet.ptD = ptDAK5PFPUcorrJet[iJet];
         thisJet.rmsCand = rmsCandAK5PFPUcorrJet[iJet];

         thisJet.axis1 = axis1AK5PFPUcorrJet[iJet];
         thisJet.axis2 = axis2AK5PFPUcorrJet[iJet];
         thisJet.pull = pullAK5PFPUcorrJet[iJet];
         thisJet.tana = tanaAK5PFPUcorrJet[iJet];

         thisJet.ptD_QC = ptD_QCAK5PFPUcorrJet[iJet];
         thisJet.rmsCand_QC = rmsCand_QCAK5PFPUcorrJet[iJet];
         thisJet.axis1_QC = axis1_QCAK5PFPUcorrJet[iJet];
         thisJet.axis2_QC = axis2_QCAK5PFPUcorrJet[iJet];
         thisJet.pull_QC = pull_QCAK5PFPUcorrJet[iJet];
         thisJet.tana_QC = tana_QCAK5PFPUcorrJet[iJet];

         thisJet.nChg_ptCut = nChg_ptCutAK5PFPUcorrJet[iJet];
         thisJet.nChg_QC = nChg_QCAK5PFPUcorrJet[iJet];
         thisJet.nChg_ptCut_QC = nChg_ptCut_QCAK5PFPUcorrJet[iJet];
         thisJet.nNeutral_ptCut = nNeutral_ptCutAK5PFPUcorrJet[iJet];

         thisJet.Rchg = RchgAK5PFPUcorrJet[iJet];
         thisJet.Rneutral = RneutralAK5PFPUcorrJet[iJet];
         thisJet.R = RAK5PFPUcorrJet[iJet];
         thisJet.Rchg_QC = Rchg_QCAK5PFPUcorrJet[iJet];

         thisJet.pTMax = pTMaxAK5PFPUcorrJet[iJet];
         thisJet.pTMaxChg = pTMaxChgAK5PFPUcorrJet[iJet];
         thisJet.pTMaxNeutral = pTMaxNeutralAK5PFPUcorrJet[iJet];
         thisJet.pTMaxChg_QC = pTMaxChg_QCAK5PFPUcorrJet[iJet];

         thisJet.betastar = betastarAK5PFPUcorrJet[iJet];


         leadJets.push_back(thisJet);
         leadJetsIndex.push_back(iJet);

       } //for jets

     } //if/else CHS


     nJet_ = 0;
     nPart_ = 0;


     for( unsigned iJet=0; iJet<leadJets.size(); ++iJet ) {
   
       AnalysisJet thisJet = leadJets[iJet];

       // --------------
       // kinematics:
       // --------------
       if( thisJet.Pt() < jetPt_thresh ) continue;
       //if( fabs(thisJet.Eta()) > 2.4 ) continue;


       if( nJet_ < 20 ) {

         eJet_[nJet_] = leadJets[nJet_].Energy();
         ptJet_[nJet_] = leadJets[nJet_].Pt();
         etaJet_[nJet_] = leadJets[nJet_].Eta();
         phiJet_[nJet_] = leadJets[nJet_].Phi();
         nCharged_[nJet_] = leadJets[nJet_].nCharged;
         nNeutral_[nJet_] = leadJets[nJet_].nNeutral;
         ptD_[nJet_] = leadJets[nJet_].ptD;
         rmsCand_[nJet_] = leadJets[nJet_].rmsCand;

         axis1_[nJet_] = thisJet.axis1;
         axis2_[nJet_] = thisJet.axis2;
         pull_[nJet_] = thisJet.pull;
         tana_[nJet_] = thisJet.tana;

         ptD_QC_[nJet_] = thisJet.ptD_QC;
         rmsCand_QC_[nJet_] = thisJet.rmsCand_QC;
         axis1_QC_[nJet_] = thisJet.axis1_QC;
         axis2_QC_[nJet_] = thisJet.axis2_QC;
         pull_QC_[nJet_] = thisJet.pull_QC;
         tana_QC_[nJet_] = thisJet.tana_QC;

         nChg_ptCut_[nJet_] = thisJet.nChg_ptCut;
         nChg_QC_[nJet_] = thisJet.nChg_QC;
         nChg_ptCut_QC_[nJet_] = thisJet.nChg_ptCut_QC;
         nNeutral_ptCut_[nJet_] = thisJet.nNeutral_ptCut;

         Rchg_[nJet_] = thisJet.Rchg;
         Rneutral_[nJet_] = thisJet.Rneutral;
         R_[nJet_] = thisJet.R;
         Rchg_QC_[nJet_] = thisJet.Rchg_QC;

         pTMax_[nJet_] = thisJet.pTMax;
         pTMaxChg_[nJet_] = thisJet.pTMaxChg;
         pTMaxNeutral_[nJet_] = thisJet.pTMaxNeutral;
         pTMaxChg_QC_[nJet_] = thisJet.pTMaxChg_QC;

         betastar_[nJet_] = thisJet.betastar;

         // match to gen jet:
         float deltaR_genJet_best = 999.;
         TLorentzVector foundGenJet;
         for( unsigned iGenJet=0; iGenJet<nAK5GenJet; ++iGenJet ) {

           TLorentzVector* thisGenJet = new TLorentzVector( pxAK5GenJet[iGenJet], pyAK5GenJet[iGenJet], pzAK5GenJet[iGenJet], energyAK5GenJet[iGenJet] );
           
           if( thisGenJet->Pt()<3. ) continue;
           float deltaR = thisGenJet->DeltaR(leadJets[nJet_]);

           if( deltaR<deltaR_genJet_best ) {
             deltaR_genJet_best = deltaR;
             foundGenJet = *thisGenJet;
           }
           
         } //for genjets

         if( deltaR_genJet_best<999. ) {

           eJetGen_[nJet_]   = foundGenJet.Energy();
           ptJetGen_[nJet_]  = foundGenJet.Pt();
           etaJetGen_[nJet_] = foundGenJet.Eta();
           phiJetGen_[nJet_] = foundGenJet.Phi();

         } else {

           eJetGen_[nJet_]   = 0.;
           ptJetGen_[nJet_]  = 0.;
           etaJetGen_[nJet_] = 0.;
           phiJetGen_[nJet_] = 0.;

         } 

         nJet_++;
          
       } //if less than 20

            
     } //for i



     if( isMC_ ) {

       // store event partons in tree:
       for( unsigned iMc=0; iMc<nMc; ++iMc ) {

         if( statusMc[iMc]==3 && (fabs(idMc[iMc])<=6 || idMc[iMc]==21) ) {

           TLorentzVector* thisParticle = new TLorentzVector();
           thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );

           if( nPart_<20 ) {

             ptPart_[nPart_] = thisParticle->Pt();
             etaPart_[nPart_] = thisParticle->Eta();
             phiPart_[nPart_] = thisParticle->Phi();
             ePart_[nPart_] = thisParticle->Energy();
             pdgIdPart_[nPart_] = idMc[iMc];

             nPart_++;

           } else {
      
             std::cout << "Found more than 20 partons, skipping." << std::endl;

           }

           delete thisParticle;
           thisParticle = 0;

         } //if correct id mc

       } // for i mc

     } // if is mc


     reducedTree_->Fill(); 


   } //for entries

} //loop
Пример #4
0
void Ntp1Analyzer_HWWlvjj::Loop(){

   DEBUG_VERBOSE_ = false;

   if (fChain == 0) return;

   Long64_t nentries;

   if( DEBUG_ ) nentries = 100000;
   else nentries = fChain->GetEntries();

   Long64_t nbytes = 0, nb = 0;

   TRandom3 rand;
   float Cont_inclusive=0., Cont_PV=0., Cont_MU=0., Cont_ELE=0., Cont_VetoMU=0., Cont_VetoELE=0., Cont_JetsELE=0., Cont_JetsMU=0.;
   Long64_t Jentry;

requiredTriggerElectron.push_back("1-164237:HLT_Ele27_CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT_v");
requiredTriggerElectron.push_back("165085-166967:HLT_Ele32_CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT_v");
requiredTriggerElectron.push_back("166968-999999:HLT_Ele52_CaloIdVT_TrkIdT_v");
requiredTriggerMuon.push_back("1-163261:HLT_Mu15_v");
requiredTriggerMuon.push_back("163262-164237:HLT_Mu24_v");
requiredTriggerMuon.push_back("165085-166967:HLT_Mu30_v");
requiredTriggerMuon.push_back("163262-166967:HLT_IsoMu17_v");
requiredTriggerMuon.push_back("167039-999999:HLT_IsoMu20_eta2p1_v");
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
     Long64_t ientry = LoadTree(jentry);
     if (ientry < 0) break;
     nb = fChain->GetEntry(jentry);   nbytes += nb;
     // if (Cut(ientry) < 0) continue;

     Jentry=jentry;

     if( DEBUG_VERBOSE_ ) std::cout << "entry n." << jentry << std::endl;
     

     if( (jentry%100000) == 0 ) std::cout << "Event #" << jentry  << " of " << nentries << std::endl;
  
   //HLT_Mu11_ = this->PassedHLT("HLT_Mu11");
   //HLT_Ele17_SW_EleId_L1R_ = this->PassedHLT("HLT_Ele17_SW_EleId_L1R");
   //HLT_DoubleMu3_ = this->PassedHLT("HLT_DoubleMu3");

     run_ = runNumber;
     LS_ = lumiBlock;
     event_ = eventNumber;
     eventWeight_ = -1.; //default

     Cont_inclusive++;//Other;

     if( !isGoodEvent( jentry ) ) continue; //this takes care also of integrated luminosity and trigger

       if( nPV==0 ) continue;
     bool goodVertex = (ndofPV[0] >= 4.0 && sqrt(PVxPV[0]*PVxPV[0]+PVyPV[0]*PVyPV[0]) < 2. && fabs(PVzPV[0]) < 24. );
     if( !goodVertex ) continue;
     nvertex_ = nPV;
     rhoPF_ = rhoFastjet;

     Cont_PV++; //Other

     //trigger:
     // not yet

     if( !isMC_ ){
     reloadTriggerMask(runNumber);
     bool passedElectronTrigger=true, passedMuonTrigger=true;
     std::string SingEle("SingleElectron_6july");//WW
     std::string SingMuo("SingleMu_6july");
     int elect=dataset_.compare(SingEle), muo=dataset_.compare(SingMuo);
     if( elect==0 ){ passedElectronTrigger = hasPassedHLT(0); }
     if( muo==0 ){ passedMuonTrigger = hasPassedHLT(1); }
     if( elect!=0 && muo!=0  ){ std::cout<<"Trigger doesn't works; dataset wrong"<<std::endl; }
     if( !passedElectronTrigger && elect==0 ){ continue;}
     if( !passedMuonTrigger && muo==0 ){ continue;}
     }
         ptHat_ = (isMC_) ? genPtHat : ptHat_;

     //if( isMC_ ) 
     //  if( (ptHat_ > ptHatMax_) || (ptHat_ < ptHatMin_) ) continue;

     bool noLeptons = false;
     TLorentzVector lept1MC, lept2MC;
     int wIndexqq=-1;
     int wIndexll=-1;
   
     if( isMC_ ) {
       
       // first look for W->qq'
       std::vector<TLorentzVector> quarksMC;

       for( unsigned iMc=0; iMc<nMc && quarksMC.size()<2; ++iMc ) {

         // quarks have status 3
         if( statusMc[iMc] != 3 ) continue;

         TLorentzVector* thisParticle = new TLorentzVector();
         thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );

         if( fabs(idMc[iMc])<7 && fabs(idMc[mothMc[iMc]])==24 ) {
           wIndexqq = mothMc[iMc];
           quarksMC.push_back( *thisParticle );
         }
       }

       // (checked that always 2 quarks are found)
       if( quarksMC.size()==2 && wIndexqq!=-1 ) {
	   eQuark1_=quarksMC[0].E();
	   ptQuark1_=quarksMC[0].Pt();
	   etaQuark1_=quarksMC[0].Eta();
	   phiQuark1_=quarksMC[0].Phi();
	   eQuark2_=quarksMC[1].E();
	   ptQuark2_=quarksMC[1].Pt();
	   etaQuark2_=quarksMC[1].Eta();
	   phiQuark2_=quarksMC[1].Phi();
 
         TLorentzVector WqqMC;
         WqqMC.SetPtEtaPhiE( pMc[wIndexqq]*sin(thetaMc[wIndexqq]), etaMc[wIndexqq], phiMc[wIndexqq], energyMc[wIndexqq] );
         ptWqqMC_  = WqqMC.Pt();
         eWqqMC_   = WqqMC.Energy();
         etaWqqMC_ = WqqMC.Eta();
         phiWqqMC_ = WqqMC.Phi();
	 //float deltaRqq = quarksMC[0].DeltaR(quarksMC[1]);
	 // h1_deltaRqq->Fill(deltaRqq);
       }
       
       // now look for W->lv

       std::vector<TLorentzVector> electronMC;
       std::vector<TLorentzVector> muonMC;
       std::vector<TLorentzVector> neutrinoMC;

       for( unsigned iMc=0; iMc<nMc; ++iMc ) {
	 
	 if( statusMc[iMc] != 3 ) continue;
	 
         TLorentzVector* thisParticle = new TLorentzVector();
         thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );

           if( fabs(idMc[iMc])==11 && fabs(idMc[mothMc[iMc]])==24 ) {electronMC.push_back( *thisParticle ); wIndexll = mothMc[iMc]; }
	   if( fabs(idMc[iMc])==13 && fabs(idMc[mothMc[iMc]])==24 ) {muonMC.push_back( *thisParticle ); wIndexll = mothMc[iMc]; }
	   if( (fabs(idMc[iMc])==12 || fabs(idMc[iMc])==14 ) && fabs(idMc[mothMc[iMc]])==24 ) {
	     neutrinoMC.push_back( *thisParticle );  wIndexll = mothMc[iMc];
	       }//for comparison with fit
 
         delete thisParticle;
         thisParticle = 0;
       }
       
       if( electronMC.size()==1 && neutrinoMC.size()==1 ) {
	 lept1MC = electronMC[0];
	 lept2MC = neutrinoMC[0];
	
       	 if( (fabs(lept1MC.Eta()) < 2.5) && ( fabs(lept1MC.Eta())<1.4442 || fabs(lept1MC.Eta())>1.566) ){
       	 h1_nEvents_vs_ptEle->Fill( lept1MC.Pt() );}
       	 h1_nEvents_vs_ptEle->Fill( lept2MC.Pt() );
       	 }
       else if( muonMC.size()==1 && neutrinoMC.size()==1 ) {
	 lept1MC = muonMC[0];
	 lept2MC = neutrinoMC[0];

	 if( fabs(lept1MC.Eta()) < 2.4 ){ 
         h1_nEvents_vs_ptMuon->Fill( lept1MC.Pt() );} h1_nEvents_vs_ptMuon->Fill( lept2MC.Pt() );
       }
       else {
	 //taus
	 noLeptons=true;
       }

       if( !noLeptons ) {
       eLeptMC_=lept1MC.E();
       ptLeptMC_=lept1MC.Pt();
       etaLeptMC_=lept1MC.Eta();
       phiLeptMC_=lept1MC.Phi();       
       eNeuMC_=lept2MC.E();
       ptNeuMC_=lept2MC.Pt();
       etaNeuMC_=lept2MC.Eta();
       phiNeuMC_=lept2MC.Phi();
       }
       //if is a tau or a not recognized e/mu default values are setted
       if( noLeptons ) {
       eLeptMC_=1.;
       ptLeptMC_=1.;
       etaLeptMC_=10.;
       phiLeptMC_=1.;       
       eNeuMC_=1.;
       ptNeuMC_=1.;
       etaNeuMC_=10.;
       phiNeuMC_=1.;    
       }

       if( !noLeptons ) {
       TLorentzVector WllMC;
       WllMC.SetPtEtaPhiE( pMc[wIndexll]*sin(thetaMc[wIndexll]), etaMc[wIndexll], phiMc[wIndexll], energyMc[wIndexll] );
       
       ptWllMC_  = WllMC.Pt();
       eWllMC_   = WllMC.Energy();
       etaWllMC_ = WllMC.Eta();
       phiWllMC_ = WllMC.Phi();
       }
       
       // now look for the higgs:
       if( wIndexll!=-1 && wIndexqq!=-1 ) {
	 
         int higgsIndex = mothMc[wIndexll];
         if( idMc[higgsIndex] == 25 ) {
           TLorentzVector HiggsMC;
           HiggsMC.SetPtEtaPhiE( pMc[higgsIndex]*sin(thetaMc[higgsIndex]), etaMc[higgsIndex], phiMc[higgsIndex], energyMc[higgsIndex] );

           eHiggsMC_   = HiggsMC.Energy(); 
           ptHiggsMC_  = HiggsMC.Pt(); 
           etaHiggsMC_ = HiggsMC.Eta(); 
           phiHiggsMC_ = HiggsMC.Phi(); 
         } // if higgs

       } //if found two W's
     
     } //if isMC
    
     // -----------------------------
     //      FROM NOW ON RECO
     // -----------------------------

     energyPFMet_ = energyPFMet[0];
     phiPFMet_ = phiPFMet[0];
     pxPFMet_ = pxPFMet[0];
     pyPFMet_ = pyPFMet[0];     
     
     // -----------------------------
     //###      uncorrEnergyJet (to use in Neutrino's fit if you don't have SumEt)
     // -----------------------------
     
       for( int i=0;i<nAK5Jet;i++ ){
       uncorrEnergyAK5Jet_ += uncorrEnergyAK5Jet[i];
       }

     // -----------------------------
     //###      SumEt
     // -----------------------------
       
     SumEt_ = sumEtPFMet[0]; 
 
     // ------------------
     // MUON
     // ------------------

     std::vector<AnalysisMuon> muon;
     bool looseMuon=false;

     for( unsigned int iMuon=0; iMuon<nMuon; ++iMuon ) {

         AnalysisMuon thisMuon( pxMuon[iMuon], pyMuon[iMuon], pzMuon[iMuon], energyMuon[iMuon] );

       // --------------
       // kinematics:
       // --------------

       if( thisMuon.Pt() < 10. ) continue;
       if( fabs(thisMuon.Eta()) > 2.4 ) continue; //2.4 OtherMine

	 thisMuon.isGlobalMuonPromptTight = (muonIdMuon[iMuon]>>8)&1;
	 thisMuon.isAllTrackerMuon = (muonIdMuon[iMuon]>>11)&1;

       //     // --------------
       //     // ID:
       //     // --------------
       //     if( !( (muonIdMuon[iMuon]>>8)&1 ) ) continue; //GlobalMuonPromptTight
       //     if( !( (muonIdMuon[iMuon]>>11)&1 ) ) continue; //AllTrackerMuon
       //if( numberOfValidPixelBarrelHitsTrack[trackIndexMuon[iMuon]]==0 && numberOfValidPixelEndcapHitsTrack[trackIndexMuon[iMuon]]==0 ) continue;      
         
	thisMuon.pixelHits = numberOfValidPixelBarrelHitsTrack[trackIndexMuon[iMuon]]+numberOfValidPixelEndcapHitsTrack[trackIndexMuon[iMuon]];
        thisMuon.trackerHits = trackValidHitsTrack[trackIndexMuon[iMuon]];
        thisMuon.nMatchedStations = numberOfMatchesMuon[iMuon]; // This branch not exists yet in WW500

       // to compute dxy, look for leading primary vertex:
       int hardestPV = -1;
       float sumPtMax = 0.0;
       for(int v=0; v<nPV; v++) {
         if(SumPtPV[v] > sumPtMax) {
           sumPtMax = SumPtPV[v];
           hardestPV = v;
         }
       }  
      
       float dxy;
       if( hardestPV==-1 ) {
         dxy = 0.;
       } else {
         dxy = fabs(this->trackDxyPV(PVxPV[hardestPV], PVyPV[hardestPV], PVzPV[hardestPV],
                              trackVxTrack[trackIndexMuon[iMuon]], trackVyTrack[trackIndexMuon[iMuon]], trackVzTrack[trackIndexMuon[iMuon]],
				     pxTrack[trackIndexMuon[iMuon]], pyTrack[trackIndexMuon[iMuon]], pzTrack[trackIndexMuon[iMuon]]));
       }
 
       float dz = fabs(trackVzTrack[trackIndexMuon[iMuon]]-PVzPV[hardestPV]);
      
       thisMuon.dxy = dxy;
       thisMuon.dz = dz;
       
       thisMuon.sumPt03 = sumPt03Muon[iMuon];
       thisMuon.emEt03  = emEt03Muon[iMuon];
       thisMuon.hadEt03 = hadEt03Muon[iMuon];

       if( !thisMuon.passedVBTF() ) continue;
     
       // --------------
       // isolation:
       // --------------
       // (this is sum pt tracks)
       //if( sumPt03Muon[iMuon] >= 3. ) continue;
       // combined isolation < 15%:
       //if( (sumPt03Muon[iMuon] + emEt03Muon[iMuon] + hadEt03Muon[iMuon]) >= 0.15*thisMuon.Pt() ) continue;

       // looking at loose muon
       looseMuon=true;
       if( thisMuon.Pt()<20. ) continue;

       // for now simple selection, will have to optimize this (T&P?)
       chargeLept_=chargeMuon[iMuon];
     muon.push_back( thisMuon );
       looseMuon=false;

   } //for muon

     // ------------------
     // ELECTRON
     // ------------------

     std::vector<AnalysisElectron> electron;    
     bool looseEle = false; //Other
     
     for( unsigned int iEle=0; iEle<nEle ; ++iEle ) {

       AnalysisElectron thisEle( pxEle[iEle], pyEle[iEle], pzEle[iEle], energyEle[iEle] );

       // --------------
       // kinematics:
       // --------------
       if( thisEle.Pt() < 15. ) continue;
       if( (fabs(thisEle.Eta()) > 2.5) || ( fabs(thisEle.Eta())>1.4442 && fabs(thisEle.Eta())<1.566) ) continue;

       // isolation
       thisEle.dr03TkSumPt = dr03TkSumPtEle[iEle];
       thisEle.dr03EcalRecHitSumEt = dr03EcalRecHitSumEtEle[iEle];
       thisEle.dr03HcalTowerSumEt = dr03HcalTowerSumEtEle[iEle];

       // electron ID
       thisEle.sigmaIetaIeta = (superClusterIndexEle[iEle]>=0) ? covIEtaIEtaSC[superClusterIndexEle[iEle]] : covIEtaIEtaSC[PFsuperClusterIndexEle[iEle]];
       thisEle.deltaPhiAtVtx = deltaPhiAtVtxEle[iEle];
       thisEle.deltaEtaAtVtx = deltaEtaAtVtxEle[iEle];
       thisEle.hOverE = hOverEEle[iEle];

       // conversion rejection
       thisEle.expInnerLayersGsfTrack = expInnerLayersGsfTrack[gsfTrackIndexEle[iEle]];
       thisEle.convDist = convDistEle[iEle];
       thisEle.convDcot = convDcotEle[iEle];

       bool passed_VBTF95 = thisEle.passedVBTF95();
       bool passed_VBTF80 = thisEle.passedTrigger80();//thisEle.passedVBTF80();

       if( !passed_VBTF80 && passed_VBTF95 ) looseEle=true; //Other
       if( !passed_VBTF80 ) continue; 
       if( thisEle.Pt() < 20. ) continue; // if is not a loose ele I require a higher Pt     

       // check that not matched to muon (clean electron faked by muon MIP):
       bool matchedtomuon=false;
       for( std::vector<AnalysisMuon>::iterator iMu=muon.begin(); iMu!=muon.end(); ++iMu )
         if( iMu->DeltaR(thisEle)<0.1 ) matchedtomuon=true;

       if( matchedtomuon ) continue;

       // for now simple selection, will have to optimize this (T&P?)
       // one electron required to pass VBTF80, the other VBTF95
       chargeLept_=chargeEle[iEle];
       electron.push_back( thisEle );
     } //for electron 
                                         //  FILL LEPTONS
// Fill Efficiency for Others

bool findJetELE=false;
bool findJetMU=false;

if( electron.size() >= 1 ) { Cont_ELE++;
  if( electron.size() == 1 && muon.size()==0 && !looseEle && !looseMuon ) { Cont_VetoELE++; findJetELE=true; }
  }

if( muon.size() >= 1 ) { Cont_MU++;
  if( muon.size() == 1 && electron.size()==0 && !looseEle && !looseMuon ) { Cont_VetoMU++; findJetMU=true; }
  }
// Fill lepton
    if( electron.size() > 1 || muon.size() > 1 ) continue;

     if( electron.size() < 1 &&  muon.size() < 1 ) continue;
     
     //   // clean electron faked by muon MIP in ECAL
     //   for( std::vector<TLorentzVector>::iterator iEle=electron.begin(); iEle!=electron.end(); ++iEle ) {
     //     for( std::vector<TLorentzVector>::iterator iMu=muon.begin(); iMu!=muon.end(); ++iMu ) {
     //       if( iMu->DeltaR(*iEle)<0.1 ) {
     //         std::cout << "lll" << std::endl;
     //         electron.erase(iEle);
     //       }
     //     } //for ele
     //   } //for mu
     
     //   if( electron.size() < 2 && muon.size() < 1 ) continue;
    
     std::vector< AnalysisLepton > leptons;

     if( electron.size() == 1 && muon.size() == 1 ) continue;
     if( electron.size() == 1 && (looseEle || looseMuon) ) continue;
     if( muon.size() == 1 && (looseEle || looseMuon) ) continue;

     if( electron.size() == 1 && (!looseEle || !looseMuon) ) { //Other 
       leptType_ = 1;
       leptons.push_back( electron[0] );

     } else if( muon.size() == 1 && (!looseEle || !looseMuon) ) {
       leptType_ = 0;
       leptons.push_back( muon[0] );

     } else {
       std::cout << "There must be an error this is not possible." << std::endl;
       exit(9101);
     }

     eLept_ = leptons[0].Energy();
     ptLept_ = leptons[0].Pt();
     etaLept_ = leptons[0].Eta();
     phiLept_ = leptons[0].Phi(); //chargeLept_ filled before
    
     // --------------------
     // match leptons to MC:
     // --------------------
     int correctIdMc = (leptType_==0 ) ? 13 : 11;

	float deltaRmin = 100.;
	TLorentzVector matchedLeptonMC;
	for( unsigned iMc=0; iMc<nMc; ++iMc ){
	   if( statusMc[iMc]==1 && fabs(idMc[iMc])==correctIdMc && idMc[mothMc[mothMc[iMc]]]==24 ) {
	     
	     TLorentzVector* thisParticle = new TLorentzVector();
	     thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );
	     float thisDeltaR = leptons[0].DeltaR( *thisParticle );
	     if( thisDeltaR < deltaRmin ) {
	       deltaRmin = thisDeltaR;
	       matchedLeptonMC = *thisParticle;
	     }
	     
	     delete thisParticle;
	     thisParticle = 0; 
	   } //if correct id mc
	   
	  } // for i mc

	  if( !noLeptons ) {
	    if( leptType_==0 ) {
	      h1_deltaRmatching_muons->Fill( deltaRmin );
	      if( deltaRmin<0.1 ) {
		h1_passed_vs_ptMuon->Fill( matchedLeptonMC.Pt() );
	      }
	    } else if( leptType_==1 ) { 
	      h1_deltaRmatching_electrons->Fill( deltaRmin );
	      if( deltaRmin<0.1 ) {
		h1_passed_vs_ptEle->Fill( matchedLeptonMC.Pt() );
	      }
	    }  //if lept type
	  } //if yes leptons
   
      // ------------------
      // JETS
      // ------------------
	     
	float jetPt_thresh = 20.;
	bool matched=false;      
	
	// first save leading jets in event:
	std::vector<AnalysisJet> leadJets;
	std::vector<int> leadJetsIndex; //index in the event collection (needed afterwards for PFCandidates)
	int nJets30=0;
	
	//QGLikelihoodCalculator qglc;
     for( unsigned int iJet=0; iJet<nAK5PFPUcorrJet; ++iJet ) {
       
       AnalysisJet thisJet( pxAK5PFPUcorrJet[iJet], pyAK5PFPUcorrJet[iJet], pzAK5PFPUcorrJet[iJet], energyAK5PFPUcorrJet[iJet] );
       
       thisJet.eChargedHadrons = chargedHadronEnergyAK5PFPUcorrJet[iJet];
       thisJet.ePhotons        = photonEnergyAK5PFPUcorrJet[iJet];
       thisJet.eNeutralEm      = neutralEmEnergyAK5PFPUcorrJet[iJet];
       thisJet.eNeutralHadrons = neutralHadronEnergyAK5PFPUcorrJet[iJet];
       thisJet.eElectrons      = electronEnergyAK5PFPUcorrJet[iJet];
       thisJet.eMuons          = muonEnergyAK5PFPUcorrJet[iJet];

       thisJet.nChargedHadrons = chargedHadronMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.nPhotons        = photonMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.nNeutralHadrons = neutralHadronMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.nElectrons      = electronMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.nMuons          = muonMultiplicityAK5PFPUcorrJet[iJet];

       thisJet.nCharged = chargedHadronMultiplicityAK5PFPUcorrJet[iJet]+electronMultiplicityAK5PFPUcorrJet[iJet]+muonMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.nNeutral = neutralHadronMultiplicityAK5PFPUcorrJet[iJet]+photonMultiplicityAK5PFPUcorrJet[iJet];
       thisJet.rmsCand =  rmsCandAK5PFPUcorrJet[iJet];
       thisJet.ptD =  ptDAK5PFPUcorrJet[iJet];

       thisJet.trackCountingHighEffBJetTag = trackCountingHighEffBJetTagsAK5PFPUcorrJet[iJet];

       if( thisJet.Pt()>jetPt_thresh ) nJets30++;

       // save at least 3 lead jets (if event has them) and all jets with pt>thresh:
       if( leadJets.size()>=3 && thisJet.Pt()<jetPt_thresh ) break;

       // far away from leptons:
      if( leptType_==0){ if(thisJet.DeltaR( leptons[0] ) < 0.5 ) continue;}    
      if( leptType_==1){ if(thisJet.DeltaR( leptons[0] ) < 0.5 ) continue;} 

       // jet ID:
       int multiplicity = thisJet.nCharged +  thisJet.nNeutral + HFEMMultiplicityAK5PFPUcorrJet[iJet] + HFHadronMultiplicityAK5PFPUcorrJet[iJet];
       if( multiplicity < 2 ) continue;
       if( fabs(thisJet.Eta())<2.4 && thisJet.nChargedHadrons == 0 ) continue;
       if( thisJet.eNeutralHadrons >= 0.99*thisJet.Energy() ) continue;
       if( thisJet.ePhotons >= 0.99*thisJet.Energy() ) continue;
       // match to genjet:

       float bestDeltaR=999.;
       TLorentzVector matchedGenJet;
       bool matched=false;
       for( unsigned iGenJet=0; iGenJet<nAK5GenJet; ++iGenJet ) {
         TLorentzVector thisGenJet(pxAK5GenJet[iGenJet], pyAK5GenJet[iGenJet], pzAK5GenJet[iGenJet], energyAK5GenJet[iGenJet]);
         if( thisGenJet.DeltaR(thisJet) < bestDeltaR ) {
           bestDeltaR=thisGenJet.DeltaR(thisJet);
           matchedGenJet=thisGenJet; matched==true;
         }
       }
       if(matched){
       thisJet.ptGen  = (isMC_) ? matchedGenJet.Pt() : 0.;
       thisJet.etaGen = (isMC_) ? matchedGenJet.Eta() : 20.;
       thisJet.phiGen = (isMC_) ? matchedGenJet.Phi() : 0.;
       thisJet.eGen   = (isMC_) ? matchedGenJet.Energy() : 0.;}
       else{
       thisJet.ptGen  = 0.;
       thisJet.etaGen = 20.;
       thisJet.phiGen = 0.;
       thisJet.eGen   = 0.;}
       // match to parton:
       float bestDeltaR_part=999.;
       TLorentzVector matchedPart;
       int pdgIdPart=0;
       for( unsigned iPart=0; iPart<nMc; ++iPart ) {
         if( statusMc[iPart]!=3 ) continue; //partons
         if( idMc[iPart]!=21 && abs(idMc[iPart])>6 ) continue; //quarks or gluons
         TLorentzVector thisPart;
         thisPart.SetPtEtaPhiE(pMc[iPart]*sin(thetaMc[iPart]), etaMc[iPart], phiMc[iPart], energyMc[iPart]);
         if( thisPart.DeltaR(thisJet) < bestDeltaR_part ) {
           bestDeltaR_part=thisPart.DeltaR(thisJet);
           matchedPart=thisPart;
           pdgIdPart=idMc[iPart];
         }
       }

       leadJets.push_back(thisJet);
       leadJetsIndex.push_back(iJet); 
     }//iJet

     h1_nJets30->Fill(nJets30);

     if( leadJets.size()<2 ) continue;
     if( leadJets[1].Pt()<jetPt_thresh ) continue; //at least 2 jets over thresh
   
     // now look for best invariant mass jet pair 
     float Wmass = 80.399;
     float bestMass = 0.;
     int best_i=-1;
     int best_j=-1;
     int best_i_eventIndex=-1;
     int best_j_eventIndex=-1;

     nPairs_ = 0;
     nPart_ = 0;
    
     for( unsigned iJet=0; iJet<leadJets.size(); ++iJet ) {
   
       AnalysisJet thisJet = leadJets[iJet];

       // --------------
       // kinematics:
       // --------------
       if( thisJet.Pt() < jetPt_thresh ) continue;
       if( fabs(thisJet.Eta()) > 2.4 ) continue;

       for( unsigned int jJet=iJet+1; jJet<leadJets.size(); ++jJet ) {

         AnalysisJet otherJet = leadJets[jJet];

         // --------------
         // kinematics:
         // --------------
         if( otherJet.Pt() < jetPt_thresh ) continue;
         if( fabs(otherJet.Eta()) > 2.4 ) continue;

         if( nPairs_>=50 ) {
        
           std::cout << "MORE than 50 jet pairs found. SKIPPING!!" << std::endl;

         } else {

           eJet1_[nPairs_] = leadJets[iJet].Energy();
           ptJet1_[nPairs_] = leadJets[iJet].Pt();
           etaJet1_[nPairs_] = leadJets[iJet].Eta();
           phiJet1_[nPairs_] = leadJets[iJet].Phi();
           eChargedHadronsJet1_[nPairs_] = leadJets[iJet].eChargedHadrons;
           ePhotonsJet1_[nPairs_]        = leadJets[iJet].ePhotons;
           eNeutralEmJet1_[nPairs_]      = leadJets[iJet].eNeutralEm;
           eNeutralHadronsJet1_[nPairs_] = leadJets[iJet].eNeutralHadrons;
           eElectronsJet1_[nPairs_]      = leadJets[iJet].eElectrons;
           eMuonsJet1_[nPairs_]          = leadJets[iJet].eMuons;
           nChargedHadronsJet1_[nPairs_] = leadJets[iJet].nChargedHadrons;
           nPhotonsJet1_[nPairs_]        = leadJets[iJet].nPhotons;
           nNeutralHadronsJet1_[nPairs_] = leadJets[iJet].nNeutralHadrons;
           nElectronsJet1_[nPairs_]      = leadJets[iJet].nElectrons;
           nMuonsJet1_[nPairs_]          = leadJets[iJet].nMuons;

           trackCountingHighEffBJetTagJet1_[nPairs_] = leadJets[iJet].trackCountingHighEffBJetTag;
           trackCountingHighEffBJetTagJet2_[nPairs_] = leadJets[jJet].trackCountingHighEffBJetTag;

           ptDJet1_[nPairs_] = leadJets[iJet].ptD;
           rmsCandJet1_[nPairs_] = leadJets[iJet].rmsCand;
           nChargedJet1_[nPairs_] = leadJets[iJet].nCharged;
           nNeutralJet1_[nPairs_] = leadJets[iJet].nNeutral;
           QGlikelihoodJet1_[nPairs_] = leadJets[iJet].QGlikelihood;

           eJet1Gen_[nPairs_] = leadJets[iJet].eGen;
           ptJet1Gen_[nPairs_] = leadJets[iJet].ptGen;
           etaJet1Gen_[nPairs_] = leadJets[iJet].etaGen;
           phiJet1Gen_[nPairs_] = leadJets[iJet].phiGen;
            
           eJet2_[nPairs_] = leadJets[jJet].Energy();
           ptJet2_[nPairs_] = leadJets[jJet].Pt();
           etaJet2_[nPairs_] = leadJets[jJet].Eta();
           phiJet2_[nPairs_] = leadJets[jJet].Phi();
           eChargedHadronsJet2_[nPairs_] = leadJets[jJet].eChargedHadrons;
           ePhotonsJet2_[nPairs_]        = leadJets[jJet].ePhotons;
           eNeutralEmJet2_[nPairs_]      = leadJets[jJet].eNeutralEm;
           eNeutralHadronsJet2_[nPairs_] = leadJets[jJet].eNeutralHadrons;
           eElectronsJet2_[nPairs_]      = leadJets[jJet].eElectrons;
           eMuonsJet2_[nPairs_]          = leadJets[jJet].eMuons;
           nChargedHadronsJet2_[nPairs_] = leadJets[jJet].nChargedHadrons;
           nPhotonsJet2_[nPairs_]        = leadJets[jJet].nPhotons;
           nNeutralHadronsJet2_[nPairs_] = leadJets[jJet].nNeutralHadrons;
           nElectronsJet2_[nPairs_]      = leadJets[jJet].nElectrons;
           nMuonsJet2_[nPairs_]          = leadJets[jJet].nMuons;

           ptDJet2_[nPairs_] = leadJets[jJet].ptD;
           rmsCandJet2_[nPairs_] = leadJets[jJet].rmsCand;
           nChargedJet2_[nPairs_] = leadJets[jJet].nCharged;
           nNeutralJet2_[nPairs_] = leadJets[jJet].nNeutral;
           QGlikelihoodJet2_[nPairs_] = leadJets[jJet].QGlikelihood;

           eJet2Gen_[nPairs_] = leadJets[jJet].eGen;
           ptJet2Gen_[nPairs_] = leadJets[jJet].ptGen;
           etaJet2Gen_[nPairs_] = leadJets[jJet].etaGen;
           phiJet2Gen_[nPairs_] = leadJets[jJet].phiGen;
            

           nPairs_++;
          
         }
	
       } //for j
     } //for i
   if( findJetELE && nPairs_>=1 ) {Cont_JetsELE++;} //Other 
   if( findJetMU && nPairs_>=1 ) {Cont_JetsMU++;} //Other 

 
     if( isMC_ ) {

       // store event partons in tree:
       for( unsigned iMc=0; iMc<nMc; ++iMc ) {

         //if( statusMc[iMc]==3 && (fabs(idMc[iMc])<=6 || idMc[iMc]==21) ) {
         if( statusMc[iMc]==3 && pMc[iMc]*sin(thetaMc[iMc])>0.1 ) {

           TLorentzVector* thisParticle = new TLorentzVector();
           thisParticle->SetPtEtaPhiE( pMc[iMc]*sin(thetaMc[iMc]), etaMc[iMc], phiMc[iMc], energyMc[iMc] );

           if( nPart_<20 ) {

             ptPart_[nPart_] = thisParticle->Pt();
             etaPart_[nPart_] = thisParticle->Eta();
             phiPart_[nPart_] = thisParticle->Phi();
             ePart_[nPart_] = thisParticle->Energy();
             pdgIdPart_[nPart_] = idMc[iMc];
             motherPart_[nPart_] = idMc[mothMc[iMc]];

             nPart_++;

           } else {
      
             std::cout << "Found more than 20 partons, skipping." << std::endl;

           }

           delete thisParticle;
           thisParticle = 0;

         } //if correct id mc

       } // for i mc

     } // if is mc

     reducedTree_->Fill(); 

   } //for entries
                       // Other
float proportion = 2061760. /*109989.*/ /Cont_inclusive;
	if( Jentry == (nentries-1) ){
//  std::cout << std::fixed << std::setprecision(6) << "Inclusive: " << Cont_inclusive*proportion << " PV: " << Cont_PV*proportion << " MU: " << Cont_MU*proportion << " ELE: " << Cont_ELE*proportion << " VetoMU: " << Cont_VetoMU*proportion 
  //<< " VetoELE: " << Cont_VetoELE*proportion << " JetsELE: " << Cont_JetsELE*proportion << " JetsMU: " << Cont_JetsMU*proportion << std::endl;

h1_Cont_inclusive->SetBinContent(1,Cont_inclusive*proportion);
h1_Cont_PV->SetBinContent(1,Cont_PV*proportion);
h1_Cont_TightMu->SetBinContent(1,Cont_MU*proportion);
h1_Cont_TightEle->SetBinContent(1,Cont_ELE*proportion);
h1_Cont_VetoMU->SetBinContent(1,Cont_VetoMU*proportion);
h1_Cont_VetoELE->SetBinContent(1,Cont_VetoELE*proportion);
h1_Cont_JetsELE->SetBinContent(1,Cont_JetsELE*proportion);
h1_Cont_JetsMU->SetBinContent(1,Cont_JetsMU*proportion);
}

} //loop