CMS 3D CMS Logo

PATTauProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATTauProducer.cc,v 1.18.2.2 2009/04/15 16:47:59 vadler Exp $
00003 //
00004 
00005 #include "PhysicsTools/PatAlgos/plugins/PATTauProducer.h"
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009 #include "DataFormats/Common/interface/View.h"
00010 #include "DataFormats/Common/interface/Association.h"
00011 #include "DataFormats/Common/interface/Ref.h"
00012 
00013 #include "DataFormats/TauReco/interface/PFTau.h"
00014 #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
00015 #include "DataFormats/TauReco/interface/PFTauDecayMode.h"
00016 #include "DataFormats/TauReco/interface/PFTauDecayModeAssociation.h"
00017 #include "DataFormats/TauReco/interface/CaloTau.h"
00018 #include "DataFormats/TauReco/interface/CaloTauDiscriminator.h"
00019 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00020 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00021 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00022 
00023 #include <vector>
00024 #include <memory>
00025 
00026 using namespace pat;
00027 
00028 PATTauProducer::PATTauProducer(const edm::ParameterSet & iConfig):
00029   isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
00030   useUserData_(iConfig.exists("userData"))
00031 {
00032   // initialize the configurables
00033   tauSrc_               = iConfig.getParameter<edm::InputTag>( "tauSource" );
00034 
00035   embedIsolationTracks_ = iConfig.getParameter<bool>         ( "embedIsolationTracks" );
00036   embedLeadTrack_       = iConfig.getParameter<bool>         ( "embedLeadTrack" );
00037   embedSignalTracks_    = iConfig.getParameter<bool>         ( "embedSignalTracks" );
00038 
00039   addGenMatch_    = iConfig.getParameter<bool>         ( "addGenMatch" );
00040 
00041   if (addGenMatch_) {
00042       embedGenMatch_ = iConfig.getParameter<bool>         ( "embedGenMatch" );
00043       if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00044           genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00045       } else {
00046           genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00047       }
00048   }
00049 
00050   addGenJetMatch_    = iConfig.getParameter<bool>         ( "addGenJetMatch" );
00051   if(addGenJetMatch_) {
00052     embedGenJetMatch_  = iConfig.getParameter<bool>         ( "embedGenJetMatch" );
00053     genJetMatchSrc_    = iConfig.getParameter<edm::InputTag>( "genJetMatch" );
00054   }
00055 
00056   addTrigMatch_   = iConfig.getParameter<bool>               ( "addTrigMatch" );
00057   trigMatchSrc_   = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
00058   addResolutions_ = iConfig.getParameter<bool>         ( "addResolutions" );
00059 
00060   // tau ID configurables
00061   addTauID_       = iConfig.getParameter<bool>         ( "addTauID" );
00062   if ( addTauID_ ) {
00063     // it might be a single tau ID
00064     if (iConfig.existsAs<edm::InputTag>("tauIDSource")) {
00065       tauIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("tauIDSource")));
00066     }
00067     // or there might be many of them
00068     if (iConfig.existsAs<edm::ParameterSet>("tauIDSources")) {
00069       // please don't configure me twice
00070       if (!tauIDSrcs_.empty()) throw cms::Exception("Configuration") << 
00071         "PATTauProducer: you can't specify both 'tauIDSource' and 'tauIDSources'\n";
00072       // read the different tau ID names
00073       edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("tauIDSources");
00074       std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
00075       for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
00076         tauIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
00077       }
00078     }
00079     // but in any case at least once
00080     if (tauIDSrcs_.empty()) throw cms::Exception("Configuration") <<
00081       "PATTauProducer: id addTauID is true, you must specify either:\n" <<
00082       "\tInputTag tauIDSource = <someTag>\n" << "or\n" <<
00083       "\tPSet tauIDSources = { \n" <<
00084       "\t\tInputTag <someName> = <someTag>   // as many as you want \n " <<
00085       "\t}\n";
00086   }
00087   
00088   // tau decay mode configurables
00089   addDecayMode_ = iConfig.getParameter<bool>         ( "addDecayMode" );
00090   if ( addDecayMode_ ) {
00091     decayModeSrc_ = iConfig.getParameter<edm::InputTag>( "decayModeSrc" );
00092   }
00093 
00094   // IsoDeposit configurables
00095   if (iConfig.exists("isoDeposits")) {
00096     edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00097     if ( depconf.exists("tracker")         ) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
00098     if ( depconf.exists("ecal")            ) isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
00099     if ( depconf.exists("hcal")            ) isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
00100     if ( depconf.exists("pfAllParticles")  ) isoDepositLabels_.push_back(std::make_pair(ParticleIso, depconf.getParameter<edm::InputTag>("pfAllParticles")));
00101     if ( depconf.exists("pfChargedHadron") ) isoDepositLabels_.push_back(std::make_pair(ChargedParticleIso, depconf.getParameter<edm::InputTag>("pfChargedHadron")));
00102     if ( depconf.exists("pfNeutralHadron") ) isoDepositLabels_.push_back(std::make_pair(NeutralParticleIso,depconf.getParameter<edm::InputTag>("pfNeutralHadron")));
00103     if ( depconf.exists("pfGamma")         ) isoDepositLabels_.push_back(std::make_pair(GammaParticleIso, depconf.getParameter<edm::InputTag>("pfGamma")));
00104     
00105     if ( depconf.exists("user") ) {
00106       std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00107       std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00108       int key = UserBaseIso;
00109       for ( ; it != ed; ++it, ++key) {
00110         isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00111       }
00112     }
00113   }
00114 
00115   // Efficiency configurables
00116   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00117   if (addEfficiencies_) {
00118      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00119   }
00120 
00121   // Check to see if the user wants to add user data
00122   if ( useUserData_ ) {
00123     userDataHelper_ = PATUserDataHelper<Tau>(iConfig.getParameter<edm::ParameterSet>("userData"));
00124   }
00125 
00126   // produces vector of taus
00127   produces<std::vector<Tau> >();
00128 }
00129 
00130 PATTauProducer::~PATTauProducer() 
00131 {
00132 }
00133 
00134 void PATTauProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) 
00135 { 
00136   // Get the collection of taus from the event
00137   edm::Handle<edm::View<reco::BaseTau> > anyTaus;
00138   try {
00139     iEvent.getByLabel(tauSrc_, anyTaus);
00140   } catch (const edm::Exception &e) {
00141     edm::LogWarning("DataSource") << "WARNING! No Tau collection found. This missing input will not block the job. Instead, an empty tau collection is being be produced.";
00142     std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>()); 
00143     iEvent.put(patTaus);
00144     return;
00145   }
00146 
00147   if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00148 
00149   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00150    
00151   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00152   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00153     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00154   }
00155 
00156   // prepare the MC matching
00157   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
00158   if (addGenMatch_) {
00159     for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00160       iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00161     }
00162   }
00163 
00164   edm::Handle<edm::Association<reco::GenJetCollection> > genJetMatch;  
00165   if (addGenJetMatch_) iEvent.getByLabel(genJetMatchSrc_, genJetMatch); 
00166 
00167   std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>()); 
00168 
00169   for (size_t idx = 0, ntaus = anyTaus->size(); idx < ntaus; ++idx) {
00170     edm::RefToBase<reco::BaseTau> tausRef = anyTaus->refAt(idx);
00171     edm::Ptr<reco::BaseTau> tausPtr = anyTaus->ptrAt(idx);
00172     
00173     Tau aTau(tausRef);
00174     if (embedLeadTrack_)       aTau.embedLeadTrack();
00175     if (embedSignalTracks_)    aTau.embedSignalTracks();
00176     if (embedIsolationTracks_) aTau.embedIsolationTracks();
00177 
00178     // store the match to the generated final state muons
00179     if (addGenMatch_) {
00180       for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00181           reco::GenParticleRef genTau = (*genMatches[i])[tausRef];
00182           aTau.addGenParticleRef(genTau);
00183       }
00184       if (embedGenMatch_) aTau.embedGenParticle();
00185     }
00186     
00187     // store the match to the visible part of the generated tau
00188     if (addGenJetMatch_) {
00189       reco::GenJetRef genJetTau = (*genJetMatch)[tausRef];
00190       if (genJetTau.isNonnull() && genJetTau.isAvailable() ) {
00191         aTau.setGenJet( genJetTau );
00192       } // leave empty if no match found
00193     }
00194         
00195     // matches to trigger primitives
00196     if ( addTrigMatch_ ) {
00197       for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
00198         edm::Handle<edm::Association<TriggerPrimitiveCollection> > trigMatch;
00199         iEvent.getByLabel(trigMatchSrc_[i], trigMatch);
00200         TriggerPrimitiveRef trigPrim = (*trigMatch)[tausRef];
00201         if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
00202           aTau.addTriggerMatch(*trigPrim);
00203         }
00204       }
00205     }
00206 
00207     // prepare ID extraction 
00208     if ( addTauID_ ) {
00209       std::vector<pat::Tau::IdPair> ids(tauIDSrcs_.size());
00210       for ( size_t i = 0; i < tauIDSrcs_.size(); ++i ) {
00211         edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
00212         iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
00213 
00214         edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
00215         iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
00216         
00217         if ( typeid(*tausRef) == typeid(reco::PFTau) ) {
00218           //std::cout << "filling PFTauDiscriminator '" << tauIDSrcs_[i].first << "' into pat::Tau object..." << std::endl;
00219           edm::Handle<reco::PFTauCollection> pfTauCollection; 
00220           iEvent.getByLabel(tauSrc_, pfTauCollection);
00221 
00222           edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
00223           iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
00224 
00225           ids[i].first = tauIDSrcs_[i].first;
00226           ids[i].second = getTauIdDiscriminator(pfTauCollection, idx, pfTauIdDiscr);
00227         } else if ( typeid(*tausRef) == typeid(reco::CaloTau) ) {
00228           //std::cout << "filling CaloTauDiscriminator '" << tauIDSrcs_[i].first << "' into pat::Tau object..." << std::endl;
00229           edm::Handle<reco::CaloTauCollection> caloTauCollection; 
00230           iEvent.getByLabel(tauSrc_, caloTauCollection);
00231 
00232           edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
00233           iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
00234 
00235           ids[i].first = tauIDSrcs_[i].first;
00236           ids[i].second = getTauIdDiscriminator(caloTauCollection, idx, caloTauIdDiscr);
00237         } else {
00238           throw cms::Exception("Type Mismatch") <<
00239             "PATTauProducer: unsupported datatype '" << typeid(*tausRef).name() << "' for tauSource\n";
00240         }
00241       }
00242 
00243       aTau.setTauIDs(ids);
00244     }
00245 
00246     // extraction of reconstructed tau decay mode 
00247     if ( addDecayMode_ ) {
00248       edm::Handle<reco::PFTauDecayModeAssociation> pfDecayModeAssoc;
00249       iEvent.getByLabel(decayModeSrc_, pfDecayModeAssoc);
00250       edm::Handle<reco::PFTauCollection> pfTaus;
00251       iEvent.getByLabel(tauSrc_, pfTaus);
00252       reco::PFTauRef pfTauRef(pfTaus, idx);
00253       // need PFTauRef (edm::RefToBase<reco::BaseTau> does not suffice) 
00254       // for PFTauDecayMode look-up
00255       //const reco::PFTauDecayMode& pfDecayMode = (*pfDecayModeAssoc)[tausRef];
00256       const reco::PFTauDecayMode& pfDecayMode = (*pfDecayModeAssoc)[pfTauRef];
00257       aTau.setDecayMode(pfDecayMode.getDecayMode());
00258     }
00259 
00260     // Isolation
00261     if (isolator_.enabled()) {
00262       isolator_.fill(*anyTaus, idx, isolatorTmpStorage_);
00263       typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00264       // better to loop backwards, so the vector is resized less times
00265       for ( IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), 
00266               ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00267         aTau.setIsolation(it->first, it->second);
00268       }
00269     }
00270     
00271     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00272       aTau.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[tausRef]);
00273     }
00274 
00275     if (efficiencyLoader_.enabled()) {
00276       efficiencyLoader_.setEfficiencies( aTau, tausRef );
00277     }
00278 
00279     if ( useUserData_ ) {
00280       userDataHelper_.add( aTau, iEvent, iSetup );
00281     }
00282 
00283     patTaus->push_back(aTau);
00284   }
00285 
00286   // sort taus in pT
00287   std::sort(patTaus->begin(), patTaus->end(), pTTauComparator_);
00288 
00289   // put genEvt object in Event
00290   iEvent.put(patTaus);
00291 
00292   // clean up
00293   if (isolator_.enabled()) isolator_.endEvent();
00294 }
00295 
00296 template <typename TauCollectionType, typename TauDiscrType>
00297 float PATTauProducer::getTauIdDiscriminator(const edm::Handle<TauCollectionType>& tauCollection, size_t tauIdx, const edm::Handle<TauDiscrType>& tauIdDiscr)
00298 {
00299   edm::Ref<TauCollectionType> tauRef(tauCollection, tauIdx);
00300   return (*tauIdDiscr)[tauRef];
00301 }     
00302 
00303 #include "FWCore/Framework/interface/MakerMacros.h"
00304 
00305 DEFINE_FWK_MODULE(PATTauProducer);
00306 
00307 

Generated on Tue Jun 9 17:41:44 2009 for CMSSW by  doxygen 1.5.4