Ejemplo n.º 1
0
bool TauAnalysisSelector::process(Long64_t entry) {
  double weight = 1.0;
  if(!fPuWeightName.empty()) {
    weight *= fPuWeight->value();
  }
  fEventCounter.setWeight(weight);

  cAll.increment();

  if(fIsEmbedded) {
    weight *= fGeneratorWeight->value();
    fEventCounter.setWeight(weight);
  }
  cGeneratorWeight.increment();

  /*
  std::cout << "FOO Event " << fEventInfo.event() << ":" << fEventInfo.lumi() << ":" << fEventInfo.run()
            << " electronVeto passed? " << fElectronVetoPassed->value()
            << std::endl;

  if( (fEventInfo.event() == 10069461 && fEventInfo.lumi() == 33572) ||
      (fEventInfo.event() == 10086951 && fEventInfo.lumi() == 33630) ||
      (fEventInfo.event() == 101065527 && fEventInfo.lumi() == 336953) ||
      (fEventInfo.event() == 101450418 && fEventInfo.lumi() == 338236) ||
      (fEventInfo.event() == 101460111 && fEventInfo.lumi() == 338268) ) {
    std::cout << "BAR Event " << fEventInfo.event() << ":" << fEventInfo.lumi() << ":" << fEventInfo.run()
              << " electronVeto passed? " << fElectronVetoPassed->value()
              << " Nelectrons " << fElectrons.size()
              << std::endl;
    for(size_t i=0; i<fElectrons.size(); ++i) {
      ElectronCollection::Electron electron = fElectrons.get(i);
      std::cout << "  Electron " << i
                << " pt " << electron.p4().Pt()
                << " eta " << electron.p4().Eta()
                << " hasGsfTrack " << electron.hasGsfTrack()
                << " hasSuperCluster " << electron.hasSuperCluster()
                << " supercluster eta " << electron.superClusterEta()
                << " passIdVeto " << electron.cutBasedIdVeto()
                << std::endl;
    }
  }

  if(!fElectronVetoPassed->value()) return false;
  cElectronVeto.increment();
  */

  // Tau BR
  if(fIsEmbedded && fEmbeddingNormalizationMode == kIDTriggerEfficiencyTauBR) {
    weight *= (1-0.357); // from my thesis, which took the numbers from PDG
    fEventCounter.setWeight(weight);
  }
  cTauBRWeight.increment();

  // MC status
  std::vector<GenParticleCollection::GenParticle> genTaus;
  GenParticleCollection::GenParticle theGenTau;
  if(isMC()) {
    bool canContinue = true;
    if(fIsEmbedded) {
      canContinue = findGenTaus(fGenTausEmbedded, 2212, &genTaus);
      canContinue = canContinue && genTaus.size() != 0;
    }
    else
      canContinue = findGenTaus(fGenTaus, 6, &genTaus);
    if(!canContinue)
      return false;
  }
  cGenTauFound.increment();
  if(isMC() && fMCTauMultiplicity != kMCTauNone) {
    size_t ntaus = genTaus.size();
    if(fIsEmbedded) {
      std::vector<GenParticleCollection::GenParticle> genTausOrig;
      findGenTaus(fGenTaus, 6, &genTausOrig);
      ntaus = genTausOrig.size();
    }

    if(fIsEmbedded) {
      // For embedded the embedded tau is counted as one
      if(fMCTauMultiplicity == kMCTauZeroTaus) return true;
      if(fMCTauMultiplicity == kMCTauOneTau && ntaus != 0) return true;
      if(fMCTauMultiplicity == kMCTauTwoTaus && ntaus != 1) return true;
      if(fMCTauMultiplicity == kMCTauMoreThanTwoTaus && ntaus < 2) return true;
    }
    else {
      if(fMCTauMultiplicity == kMCTauZeroTaus && ntaus != 0) return true;
      if(fMCTauMultiplicity == kMCTauOneTau && ntaus != 1) return true;
      if(fMCTauMultiplicity == kMCTauTwoTaus && ntaus != 2) return true;
      if(fMCTauMultiplicity == kMCTauMoreThanTwoTaus && ntaus < 3) return true;
    }
   
    if(genTaus.size() > 0)
      theGenTau = genTaus[0];
  }
  cTauMCSelection.increment();

  bool originalMuonIsWMu = false;
  EmbeddingMuonCollection::Muon embeddingMuon;
  if(fIsEmbedded) {
    if(fMuons.size() != 1)
      throw std::runtime_error("Embedding muon collection size is not 1");
    embeddingMuon = fMuons.get(0);
    if(embeddingMuon.p4().Pt() < 41) return true;
    //std::cout << "Muon pt " << muon.p4().Pt() << std::endl;

    originalMuonIsWMu = std::abs(embeddingMuon.pdgId()) == 13 && std::abs(embeddingMuon.motherPdgId()) == 24 && std::abs(embeddingMuon.grandMotherPdgId()) == 6;
    if(!originalMuonIsWMu) return true;
  }

  cOnlyWMu.increment();

  // Per-event correction for W->tau->mu
  if(fIsEmbedded && fEmbeddingWTauMuWeights) {
    embeddingMuon.ensureValidity();
    int bin = fEmbeddingWTauMuWeights->FindBin(embeddingMuon.p4().Pt());
    //std::cout << embeddingMuon.p4().Pt() << " " << bin << std::endl;
    weight *= fEmbeddingWTauMuWeights->GetBinContent(bin);
    fEventCounter.setWeight(weight);
  }
  cWTauMuWeight.increment();

  // Muon trigger efficiency weighting
  if(fIsEmbedded && (fEmbeddingNormalizationMode == kIDTriggerEfficiency || fEmbeddingNormalizationMode == kIDTriggerEfficiencyTauBR)) {
    weight *= 1/embeddingMuon.triggerEfficiency();
    fEventCounter.setWeight(weight);
  }
  cTriggerEffWeight.increment();

  // Muon ID efficiency weighting
  if(fIsEmbedded) {
    weight *= 1/embeddingMuon.idEfficiency();
    fEventCounter.setWeight(weight);
  }
  cIdEffWeight.increment();

  // Weighting by arbitrary histogram, given in constructor argument
  if(fIsEmbedded && fEmbeddingMuonWeights) {
    embeddingMuon.ensureValidity();
    int bin = fEmbeddingMuonWeights->FindFixBin(embeddingMuon.p4().Pt());
    weight *= fEmbeddingMuonWeights->GetBinContent(bin);
    fEventCounter.setWeight(weight);
  }
  cMuonWeight.increment();

  // Jet selection
  /*
  size_t njets = fJets.size();
  size_t nselectedjets = 0;
  for(size_t i=0; i<njets; ++i) {
    JetCollection::Jet jet = fJets.get(i);

    // Clean selected muon from jet collection
    bool matches = false;
    double DR = ROOT::Math::VectorUtil::DeltaR(fMuons.get(0).p4(), jet.p4());
    if(DR < 0.1) {
      matches = true;
    }
    if(matches) continue;

    // Count jets
    ++nselectedjets;
  }
  if(nselectedjets < 3) return true;
  */
  cJetSelection.increment();

  if(fSelectedVertexCount->value() <= 0) return true;
  cPrimaryVertex.increment();

  std::vector<TauCollection::Tau> selectedTaus;
  std::vector<TauCollection::Tau> tmp;

  if(fTaus.size() < 1) return true;
  cAllTauCandidates.increment();

  // Pre pT cut (for some reason in the region pT < 10 there is significantly more normal than embedded)
  for(size_t i=0; i<fTaus.size(); ++i) {
    TauCollection::Tau tau = fTaus.get(i);
    if(tau.p4().Pt() < 10) continue;
    if(!TauID::isolation(tau)) continue;
    //if(isMC() && !fIsEmbedded && tau.genMatchP4().Pt() <= 41) continue; // temporary hack
    selectedTaus.push_back(tau);
    if(!fIsEmbedded) break; // select only the first gen tau
  }
  if(selectedTaus.empty()) return true;
  cPrePtCut.increment();

  // Decay mode finding
  bool atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::decayModeFinding(tau)) continue;

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterDecayModeFindingIsolation.fill(tau.p4(), weight);
    }
    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cDecayModeFinding.increment();
  if(atLeastOneIsolated) {
    if(theGenTau.isValid()) hGenTau_AfterDecayModeFindingIsolation.fill(theGenTau.p4(), weight);
    hVertexCount_AfterDecayModeFindingIsolation->Fill(fVertexCount->value(), weight);
  }

  // Eta cut
  atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::eta(tau)) continue;

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterEtaCutIsolation.fill(tau.p4(), weight);
    }
    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cEtaCut.increment();
  if(atLeastOneIsolated) {
    hGenTau_AfterEtaCutIsolation.fill(theGenTau.p4(), weight);
    hVertexCount_AfterEtaCutIsolation->Fill(fVertexCount->value(), weight);
  }

  // Pt cut
  atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::pt(tau)) continue;

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterPtCutIsolation.fill(tau.p4(), weight);
      hTauLeadingTrackPt_AfterPtCutIsolation->Fill(tau.leadPFChargedHadrCandP4().Pt(), weight);
    }
    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cPtCut.increment();
  if(atLeastOneIsolated) {
    hGenTau_AfterPtCutIsolation.fill(theGenTau.p4(), weight);
    hVertexCount_AfterPtCutIsolation->Fill(fVertexCount->value(), weight);
  }

  // Leading track pt
  atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::leadingChargedHadrCandPt(tau)) continue;
    tmp.push_back(tau);

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterLeadingTrackPtCutIsolation.fill(tau.p4(), weight);
    }
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cLeadingTrackPtCut.increment();
  if(atLeastOneIsolated) {
    hGenTau_AfterLeadingTrackPtCutIsolation.fill(theGenTau.p4(), weight);
  }

  // ECAL fiducial cuts
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::ecalCracks(tau)) continue;
    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cEcalCracks.increment();

  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::ecalGap(tau)) continue;
    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cEcalGap.increment();
  

  // Against electron
  atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::againstElectron(tau)) continue;
    tmp.push_back(tau);

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterAgainstElectronIsolation.fill(tau.p4(), weight);
    }
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cAgainstElectron.increment();
  if(atLeastOneIsolated) {
    hGenTau_AfterAgainstElectronIsolation.fill(theGenTau.p4(), weight);
  }

  // Against muon
  atLeastOneIsolated = false;
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::againstMuon(tau)) continue;
    tmp.push_back(tau);

    if(TauID::isolation(tau)) {
      atLeastOneIsolated = true;
      hTau_AfterAgainstMuonIsolation.fill(tau.p4(), weight);
    }
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cAgainstMuon.increment();
  if(atLeastOneIsolated) {
    hGenTau_AfterAgainstMuonIsolation.fill(theGenTau.p4(), weight);
  }

  // Tau candidate selection finished

  // Isolation
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];

    if(!TauID::isolation(tau)) continue;

    int decayMode = tau.decayMode();
    int fill = -1;
    if     (decayMode <= 2)  fill = decayMode; // 0 = pi+, 1 = pi+pi0, 2 = pi+pi0pi0
    else if(decayMode == 10) fill = 3; // pi+pi-pi+
    else                     fill = 4; // Other

    hTau_AfterIsolation.fill(tau.p4(), weight);
    hTauDecayModeAll_AfterIsolation->Fill(decayMode, weight);
    hTauDecayMode_AfterIsolation->Fill(fill, weight);

    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cIsolation.increment();
  hVertexCount_AfterIsolation->Fill(fVertexCount->value(), weight);
  if(theGenTau.isValid()) hGenTau_AfterIsolation.fill(theGenTau.p4(), weight);

  // One prong
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::oneProng(tau)) continue;
    tmp.push_back(tau);

    hTau_AfterOneProng.fill(tau.p4(), weight);
    hTauP_AfterOneProng->Fill(tau.p4().P(), weight);
    hTauLeadingTrackPt_AfterOneProng->Fill(tau.leadPFChargedHadrCandP4().Pt(), weight);
    hTauLeadingTrackP_AfterOneProng->Fill(tau.leadPFChargedHadrCandP4().P(), weight);
    hTauRtau_AfterOneProng->Fill(tau.rtau(), weight);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cOneProng.increment();
  hGenTau_AfterOneProng.fill(theGenTau.p4(), weight);
  hVertexCount_AfterOneProng->Fill(fVertexCount->value(), weight);

  // Rtau
  for(size_t i=0; i<selectedTaus.size(); ++i) {
    TauCollection::Tau& tau = selectedTaus[i];
    if(!TauID::rtau(tau)) continue;

    hTau_AfterRtau.fill(tau.p4(), weight);

    tmp.push_back(tau);
  }
  selectedTaus.swap(tmp);
  tmp.clear();
  if(selectedTaus.empty()) return true;
  cRtau.increment();
  hVertexCount_AfterRtau->Fill(fVertexCount->value(), weight);
  if(theGenTau.isValid()) hGenTau_AfterRtau.fill(theGenTau.p4(), weight);

  // Tau ID finished

  // Trigger
  /*
  if(fIsEmbedded) {
    if(!fMuTriggerPassed->value()) return true;
  }
  */
  cMuTrigger.increment();


  if(false) {
    std::cout << "Event " << fEventInfo.event() << ":" << fEventInfo.lumi() << ":" << fEventInfo.run() << std::endl;
    for(size_t i=0; i< selectedTaus.size(); ++i) {
      TauCollection::Tau& tau = selectedTaus[i];
      std::cout << "  selected tau pt " << tau.p4().Pt() << " eta " << tau.p4().Eta() << " phi " << tau.p4().Phi() << std::endl;
      if(fIsEmbedded) {
        embeddingMuon.ensureValidity();
        double DR = ROOT::Math::VectorUtil::DeltaR(tau.p4(), embeddingMuon.p4());
        if(DR < 0.5) {
          std::cout << "    matched to embedding muon, DR " << DR << std::endl;
        }
      }
      for(size_t j=0; j<fGenTaus.size(); ++j) {
        GenParticleCollection::GenParticle gen = fGenTaus.get(j);
        double DR = ROOT::Math::VectorUtil::DeltaR(tau.p4(), gen.p4());
        if(DR < 0.5) {
          std::cout << "    matched to generator tau, DR " << DR
                    << " mother " << gen.motherPdgId()
                    << " grandmother " << gen.grandMotherPdgId()
                    << std::endl;
        }
      }
    }
  }

  return true;
}