00001
00002
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/CaloTau.h"
00016 #include "DataFormats/TauReco/interface/CaloTauDiscriminator.h"
00017 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00018 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00019 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00020
00021 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00023
00024 #include <vector>
00025 #include <memory>
00026
00027 using namespace pat;
00028
00029 PATTauProducer::PATTauProducer(const edm::ParameterSet & iConfig):
00030 isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), false) ,
00031 useUserData_(iConfig.exists("userData"))
00032 {
00033
00034 tauSrc_ = iConfig.getParameter<edm::InputTag>( "tauSource" );
00035
00036 embedIsolationTracks_ = iConfig.getParameter<bool> ( "embedIsolationTracks" );
00037 embedLeadTrack_ = iConfig.getParameter<bool> ( "embedLeadTrack" );
00038 embedSignalTracks_ = iConfig.getParameter<bool> ( "embedSignalTracks" );
00039 embedLeadPFCand_ = iConfig.getParameter<bool>( "embedLeadPFCand" );
00040 embedLeadPFChargedHadrCand_ = iConfig.getParameter<bool>( "embedLeadPFChargedHadrCand" );
00041 embedLeadPFNeutralCand_ = iConfig.getParameter<bool>( "embedLeadPFNeutralCand" );
00042 embedSignalPFCands_ = iConfig.getParameter<bool>( "embedSignalPFCands" );
00043 embedSignalPFChargedHadrCands_ = iConfig.getParameter<bool>( "embedSignalPFChargedHadrCands" );
00044 embedSignalPFNeutralHadrCands_ = iConfig.getParameter<bool>( "embedSignalPFNeutralHadrCands" );
00045 embedSignalPFGammaCands_ = iConfig.getParameter<bool>( "embedSignalPFGammaCands" );
00046 embedIsolationPFCands_ = iConfig.getParameter<bool>( "embedIsolationPFCands" );
00047 embedIsolationPFChargedHadrCands_ = iConfig.getParameter<bool>( "embedIsolationPFChargedHadrCands" );
00048 embedIsolationPFNeutralHadrCands_ = iConfig.getParameter<bool>( "embedIsolationPFNeutralHadrCands" );
00049 embedIsolationPFGammaCands_ = iConfig.getParameter<bool>( "embedIsolationPFGammaCands" );
00050
00051 addGenMatch_ = iConfig.getParameter<bool> ( "addGenMatch" );
00052
00053 if (addGenMatch_) {
00054 embedGenMatch_ = iConfig.getParameter<bool> ( "embedGenMatch" );
00055 if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00056 genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00057 } else {
00058 genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00059 }
00060 }
00061
00062 addGenJetMatch_ = iConfig.getParameter<bool> ( "addGenJetMatch" );
00063 if(addGenJetMatch_) {
00064 embedGenJetMatch_ = iConfig.getParameter<bool> ( "embedGenJetMatch" );
00065 genJetMatchSrc_ = iConfig.getParameter<edm::InputTag>( "genJetMatch" );
00066 }
00067
00068
00069
00070 addTauID_ = iConfig.getParameter<bool> ( "addTauID" );
00071 if ( addTauID_ ) {
00072
00073 if (iConfig.existsAs<edm::InputTag>("tauIDSource")) {
00074 tauIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("tauIDSource")));
00075 }
00076
00077 if (iConfig.existsAs<edm::ParameterSet>("tauIDSources")) {
00078
00079 if (!tauIDSrcs_.empty()) throw cms::Exception("Configuration") <<
00080 "PATTauProducer: you can't specify both 'tauIDSource' and 'tauIDSources'\n";
00081
00082 edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("tauIDSources");
00083 std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
00084 for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
00085 tauIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
00086 }
00087 }
00088
00089 if (tauIDSrcs_.empty()) throw cms::Exception("Configuration") <<
00090 "PATTauProducer: id addTauID is true, you must specify either:\n" <<
00091 "\tInputTag tauIDSource = <someTag>\n" << "or\n" <<
00092 "\tPSet tauIDSources = { \n" <<
00093 "\t\tInputTag <someName> = <someTag> // as many as you want \n " <<
00094 "\t}\n";
00095 }
00096
00097
00098 if (iConfig.exists("isoDeposits")) {
00099 edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00100 if ( depconf.exists("tracker") ) isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
00101 if ( depconf.exists("ecal") ) isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
00102 if ( depconf.exists("hcal") ) isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
00103 if ( depconf.exists("pfAllParticles") ) isoDepositLabels_.push_back(std::make_pair(pat::PfAllParticleIso, depconf.getParameter<edm::InputTag>("pfAllParticles")));
00104 if ( depconf.exists("pfChargedHadron") ) isoDepositLabels_.push_back(std::make_pair(pat::PfChargedHadronIso, depconf.getParameter<edm::InputTag>("pfChargedHadron")));
00105 if ( depconf.exists("pfNeutralHadron") ) isoDepositLabels_.push_back(std::make_pair(pat::PfNeutralHadronIso,depconf.getParameter<edm::InputTag>("pfNeutralHadron")));
00106 if ( depconf.exists("pfGamma") ) isoDepositLabels_.push_back(std::make_pair(pat::PfGammaIso, depconf.getParameter<edm::InputTag>("pfGamma")));
00107
00108 if ( depconf.exists("user") ) {
00109 std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00110 std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00111 int key = UserBaseIso;
00112 for ( ; it != ed; ++it, ++key) {
00113 isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00114 }
00115 }
00116 }
00117
00118
00119 addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00120 if (addEfficiencies_) {
00121 efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00122 }
00123
00124
00125 addResolutions_ = iConfig.getParameter<bool>("addResolutions");
00126 if (addResolutions_) {
00127 resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
00128 }
00129
00130
00131 if ( useUserData_ ) {
00132 userDataHelper_ = PATUserDataHelper<Tau>(iConfig.getParameter<edm::ParameterSet>("userData"));
00133 }
00134
00135
00136 produces<std::vector<Tau> >();
00137 }
00138
00139 PATTauProducer::~PATTauProducer()
00140 {
00141 }
00142
00143 void PATTauProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
00144 {
00145
00146 edm::Handle<edm::View<reco::BaseTau> > anyTaus;
00147 try {
00148 iEvent.getByLabel(tauSrc_, anyTaus);
00149 } catch (const edm::Exception &e) {
00150 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.";
00151 std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>());
00152 iEvent.put(patTaus);
00153 return;
00154 }
00155
00156 if (iEvent.isRealData()){
00157 addGenMatch_ = false;
00158 embedGenMatch_ = false;
00159 addGenJetMatch_ = false;
00160 }
00161
00162 if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00163
00164 if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00165 if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
00166
00167 std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00168 for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00169 iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00170 }
00171
00172
00173 std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
00174 if (addGenMatch_) {
00175 for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00176 iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00177 }
00178 }
00179
00180 edm::Handle<edm::Association<reco::GenJetCollection> > genJetMatch;
00181 if (addGenJetMatch_) iEvent.getByLabel(genJetMatchSrc_, genJetMatch);
00182
00183 std::auto_ptr<std::vector<Tau> > patTaus(new std::vector<Tau>());
00184
00185 for (size_t idx = 0, ntaus = anyTaus->size(); idx < ntaus; ++idx) {
00186 edm::RefToBase<reco::BaseTau> tausRef = anyTaus->refAt(idx);
00187 edm::Ptr<reco::BaseTau> tausPtr = anyTaus->ptrAt(idx);
00188
00189 Tau aTau(tausRef);
00190 if (embedLeadTrack_) aTau.embedLeadTrack();
00191 if (embedSignalTracks_) aTau.embedSignalTracks();
00192 if (embedIsolationTracks_) aTau.embedIsolationTracks();
00193 if (embedLeadPFCand_) {
00194 if (aTau.isPFTau() )
00195 aTau.embedLeadPFCand();
00196 else
00197 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00198 }
00199 if (embedLeadPFChargedHadrCand_) {
00200 if (aTau.isPFTau() )
00201 aTau.embedLeadPFChargedHadrCand();
00202 else
00203 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00204 }
00205 if (embedLeadPFNeutralCand_) {
00206 if (aTau.isPFTau() )
00207 aTau.embedLeadPFNeutralCand();
00208 else
00209 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00210 }
00211 if (embedSignalPFCands_) {
00212 if (aTau.isPFTau() )
00213 aTau.embedSignalPFCands();
00214 else
00215 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00216 }
00217 if (embedSignalPFChargedHadrCands_) {
00218 if (aTau.isPFTau() )
00219 aTau.embedSignalPFChargedHadrCands();
00220 else
00221 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00222 }
00223 if (embedSignalPFNeutralHadrCands_) {
00224 if (aTau.isPFTau() )
00225 aTau.embedSignalPFNeutralHadrCands();
00226 else
00227 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00228 }
00229 if (embedSignalPFGammaCands_) {
00230 if (aTau.isPFTau() )
00231 aTau.embedSignalPFGammaCands();
00232 else
00233 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00234 }
00235 if (embedIsolationPFCands_) {
00236 if (aTau.isPFTau() )
00237 aTau.embedIsolationPFCands();
00238 else
00239 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00240 }
00241 if (embedIsolationPFChargedHadrCands_) {
00242 if (aTau.isPFTau() )
00243 aTau.embedIsolationPFChargedHadrCands();
00244 else
00245 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00246 }
00247 if (embedIsolationPFNeutralHadrCands_) {
00248 if (aTau.isPFTau() )
00249 aTau.embedIsolationPFNeutralHadrCands();
00250 else
00251 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00252 }
00253 if (embedIsolationPFGammaCands_) {
00254 if (aTau.isPFTau() )
00255 aTau.embedIsolationPFGammaCands();
00256 else
00257 edm::LogWarning("Type Error") << "Embedding a PFTau-specific information into a pat::Tau which wasn't made from a reco::PFTau is impossible.\n";
00258 }
00259
00260
00261 if (addGenMatch_) {
00262 for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00263 reco::GenParticleRef genTau = (*genMatches[i])[tausRef];
00264 aTau.addGenParticleRef(genTau);
00265 }
00266 if (embedGenMatch_) aTau.embedGenParticle();
00267 }
00268
00269
00270 if (addGenJetMatch_) {
00271 reco::GenJetRef genJetTau = (*genJetMatch)[tausRef];
00272 if (genJetTau.isNonnull() && genJetTau.isAvailable() ) {
00273 aTau.setGenJet( genJetTau );
00274 }
00275 }
00276
00277
00278 if ( addTauID_ ) {
00279 std::vector<pat::Tau::IdPair> ids(tauIDSrcs_.size());
00280 for ( size_t i = 0; i < tauIDSrcs_.size(); ++i ) {
00281 edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
00282 iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
00283
00284 edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
00285 iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
00286
00287 if ( typeid(*tausRef) == typeid(reco::PFTau) ) {
00288
00289 edm::Handle<reco::PFTauCollection> pfTauCollection;
00290 iEvent.getByLabel(tauSrc_, pfTauCollection);
00291
00292 edm::Handle<reco::PFTauDiscriminator> pfTauIdDiscr;
00293 iEvent.getByLabel(tauIDSrcs_[i].second, pfTauIdDiscr);
00294
00295 ids[i].first = tauIDSrcs_[i].first;
00296 ids[i].second = getTauIdDiscriminator(pfTauCollection, idx, pfTauIdDiscr);
00297 } else if ( typeid(*tausRef) == typeid(reco::CaloTau) ) {
00298
00299 edm::Handle<reco::CaloTauCollection> caloTauCollection;
00300 iEvent.getByLabel(tauSrc_, caloTauCollection);
00301
00302 edm::Handle<reco::CaloTauDiscriminator> caloTauIdDiscr;
00303 iEvent.getByLabel(tauIDSrcs_[i].second, caloTauIdDiscr);
00304
00305 ids[i].first = tauIDSrcs_[i].first;
00306 ids[i].second = getTauIdDiscriminator(caloTauCollection, idx, caloTauIdDiscr);
00307 } else {
00308 throw cms::Exception("Type Mismatch") <<
00309 "PATTauProducer: unsupported datatype '" << typeid(*tausRef).name() << "' for tauSource\n";
00310 }
00311 }
00312
00313 aTau.setTauIDs(ids);
00314 }
00315
00316
00317
00318 if ( aTau.isPFTau() ) {
00319 edm::Handle<reco::PFTauCollection> pfTaus;
00320 iEvent.getByLabel(tauSrc_, pfTaus);
00321 reco::PFTauRef pfTauRef(pfTaus, idx);
00322
00323 aTau.setDecayMode(pfTauRef->decayMode());
00324 }
00325
00326
00327 if (isolator_.enabled()) {
00328 isolator_.fill(*anyTaus, idx, isolatorTmpStorage_);
00329 typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00330
00331 for ( IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(),
00332 ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00333 aTau.setIsolation(it->first, it->second);
00334 }
00335 }
00336
00337 for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00338 aTau.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[tausRef]);
00339 }
00340
00341 if (efficiencyLoader_.enabled()) {
00342 efficiencyLoader_.setEfficiencies( aTau, tausRef );
00343 }
00344
00345 if (resolutionLoader_.enabled()) {
00346 resolutionLoader_.setResolutions(aTau);
00347 }
00348
00349 if ( useUserData_ ) {
00350 userDataHelper_.add( aTau, iEvent, iSetup );
00351 }
00352
00353 patTaus->push_back(aTau);
00354 }
00355
00356
00357 std::sort(patTaus->begin(), patTaus->end(), pTTauComparator_);
00358
00359
00360 iEvent.put(patTaus);
00361
00362
00363 if (isolator_.enabled()) isolator_.endEvent();
00364 }
00365
00366 template <typename TauCollectionType, typename TauDiscrType>
00367 float PATTauProducer::getTauIdDiscriminator(const edm::Handle<TauCollectionType>& tauCollection, size_t tauIdx, const edm::Handle<TauDiscrType>& tauIdDiscr)
00368 {
00369 edm::Ref<TauCollectionType> tauRef(tauCollection, tauIdx);
00370 return (*tauIdDiscr)[tauRef];
00371 }
00372
00373
00374 void PATTauProducer::fillDescriptions(edm::ConfigurationDescriptions & descriptions)
00375 {
00376 edm::ParameterSetDescription iDesc;
00377 iDesc.setComment("PAT tau producer module");
00378
00379
00380 iDesc.add<edm::InputTag>("tauSource", edm::InputTag())->setComment("input collection");
00381
00382
00383 iDesc.add<bool>("embedIsolationTracks", false)->setComment("embed external isolation tracks");
00384 iDesc.add<bool>("embedLeadTrack", false)->setComment("embed external leading track");
00385 iDesc.add<bool>("embedLeadTracks", false)->setComment("embed external signal tracks");
00386
00387
00388 iDesc.add<bool>("addGenMatch", true)->setComment("add MC matching");
00389 iDesc.add<bool>("embedGenMatch", false)->setComment("embed MC matched MC information");
00390 std::vector<edm::InputTag> emptySourceVector;
00391 iDesc.addNode( edm::ParameterDescription<edm::InputTag>("genParticleMatch", edm::InputTag(), true) xor
00392 edm::ParameterDescription<std::vector<edm::InputTag> >("genParticleMatch", emptySourceVector, true)
00393 )->setComment("input with MC match information");
00394
00395
00396 iDesc.add<bool>("addGenJetMatch", true)->setComment("add MC jet matching");
00397 iDesc.add<bool>("embedGenJetMatch", false)->setComment("embed MC jet matched jet information");
00398 iDesc.add<edm::InputTag>("genJetMatch", edm::InputTag("tauGenJetMatch"));
00399
00400
00401 pat::helper::KinResolutionsLoader::fillDescription(iDesc);
00402
00403
00404 iDesc.add<bool>("addTauID", true)->setComment("add tau ID variables");
00405 edm::ParameterSetDescription tauIDSourcesPSet;
00406 tauIDSourcesPSet.setAllowAnything();
00407 iDesc.addNode( edm::ParameterDescription<edm::InputTag>("tauIDSource", edm::InputTag(), true) xor
00408 edm::ParameterDescription<edm::ParameterSetDescription>("tauIDSources", tauIDSourcesPSet, true)
00409 )->setComment("input with electron ID variables");
00410
00411
00412 edm::ParameterSetDescription isoDepositsPSet;
00413 isoDepositsPSet.addOptional<edm::InputTag>("tracker");
00414 isoDepositsPSet.addOptional<edm::InputTag>("ecal");
00415 isoDepositsPSet.addOptional<edm::InputTag>("hcal");
00416 isoDepositsPSet.addOptional<edm::InputTag>("pfAllParticles");
00417 isoDepositsPSet.addOptional<edm::InputTag>("pfChargedHadron");
00418 isoDepositsPSet.addOptional<edm::InputTag>("pfNeutralHadron");
00419 isoDepositsPSet.addOptional<edm::InputTag>("pfGamma");
00420 isoDepositsPSet.addOptional<std::vector<edm::InputTag> >("user");
00421 iDesc.addOptional("isoDeposits", isoDepositsPSet);
00422
00423
00424 edm::ParameterSetDescription efficienciesPSet;
00425 efficienciesPSet.setAllowAnything();
00426 iDesc.add("efficiencies", efficienciesPSet);
00427 iDesc.add<bool>("addEfficiencies", false);
00428
00429
00430 edm::ParameterSetDescription userDataPSet;
00431 PATUserDataHelper<Tau>::fillDescription(userDataPSet);
00432 iDesc.addOptional("userData", userDataPSet);
00433
00434 edm::ParameterSetDescription isolationPSet;
00435 isolationPSet.setAllowAnything();
00436 iDesc.add("userIsolation", isolationPSet);
00437
00438 }
00439
00440 #include "FWCore/Framework/interface/MakerMacros.h"
00441
00442 DEFINE_FWK_MODULE(PATTauProducer);
00443
00444