00001
00002
00003
00004
00005 #include "PhysicsTools/PatAlgos/plugins/PATJetProducer.h"
00006
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009
00010 #include "DataFormats/Common/interface/ValueMap.h"
00011 #include "DataFormats/Common/interface/Association.h"
00012 #include "DataFormats/Candidate/interface/CandAssociation.h"
00013
00014 #include "DataFormats/JetReco/interface/JetTracksAssociation.h"
00015 #include "DataFormats/BTauReco/interface/JetTag.h"
00016 #include "DataFormats/BTauReco/interface/TrackProbabilityTagInfo.h"
00017 #include "DataFormats/BTauReco/interface/TrackIPTagInfo.h"
00018 #include "DataFormats/BTauReco/interface/TrackCountingTagInfo.h"
00019 #include "DataFormats/BTauReco/interface/SecondaryVertexTagInfo.h"
00020 #include "DataFormats/BTauReco/interface/SoftLeptonTagInfo.h"
00021
00022 #include "DataFormats/Candidate/interface/CandMatchMap.h"
00023 #include "SimDataFormats/JetMatching/interface/JetFlavourMatching.h"
00024
00025 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00026 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00027
00028 #include "DataFormats/Math/interface/deltaR.h"
00029
00030 #include "DataFormats/PatCandidates/interface/JetCorrFactors.h"
00031
00032 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00033 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00034
00035 #include "FWCore/Framework/interface/Selector.h"
00036
00037 #include <vector>
00038 #include <memory>
00039 #include <algorithm>
00040
00041
00042 using namespace pat;
00043
00044
00045 PATJetProducer::PATJetProducer(const edm::ParameterSet& iConfig) :
00046 useUserData_(iConfig.exists("userData"))
00047 {
00048
00049 jetsSrc_ = iConfig.getParameter<edm::InputTag>( "jetSource" );
00050 embedCaloTowers_ = iConfig.getParameter<bool>( "embedCaloTowers" );
00051 embedPFCandidates_ = iConfig.getParameter<bool>( "embedPFCandidates" );
00052 getJetMCFlavour_ = iConfig.getParameter<bool>( "getJetMCFlavour" );
00053 jetPartonMapSource_ = iConfig.getParameter<edm::InputTag>( "JetPartonMapSource" );
00054 addGenPartonMatch_ = iConfig.getParameter<bool>( "addGenPartonMatch" );
00055 embedGenPartonMatch_ = iConfig.getParameter<bool>( "embedGenPartonMatch" );
00056 genPartonSrc_ = iConfig.getParameter<edm::InputTag>( "genPartonMatch" );
00057 addGenJetMatch_ = iConfig.getParameter<bool>( "addGenJetMatch" );
00058 embedGenJetMatch_ = iConfig.getParameter<bool>( "embedGenJetMatch" );
00059 genJetSrc_ = iConfig.getParameter<edm::InputTag>( "genJetMatch" );
00060 addPartonJetMatch_ = iConfig.getParameter<bool>( "addPartonJetMatch" );
00061 partonJetSrc_ = iConfig.getParameter<edm::InputTag>( "partonJetSource" );
00062 addJetCorrFactors_ = iConfig.getParameter<bool>( "addJetCorrFactors" );
00063 jetCorrFactorsSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "jetCorrFactorsSource" );
00064 addBTagInfo_ = iConfig.getParameter<bool>( "addBTagInfo" );
00065 addDiscriminators_ = iConfig.getParameter<bool>( "addDiscriminators" );
00066 discriminatorTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "discriminatorSources" );
00067 addTagInfos_ = iConfig.getParameter<bool>( "addTagInfos" );
00068 tagInfoTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "tagInfoSources" );
00069 addAssociatedTracks_ = iConfig.getParameter<bool>( "addAssociatedTracks" );
00070 trackAssociation_ = iConfig.getParameter<edm::InputTag>( "trackAssociationSource" );
00071 addJetCharge_ = iConfig.getParameter<bool>( "addJetCharge" );
00072 jetCharge_ = iConfig.getParameter<edm::InputTag>( "jetChargeSource" );
00073 addJetID_ = iConfig.getParameter<bool>( "addJetID");
00074 jetIDMapLabel_ = iConfig.getParameter<edm::InputTag>( "jetIDMap");
00075
00076 addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00077 if (addEfficiencies_) {
00078 efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00079 }
00080
00081 addResolutions_ = iConfig.getParameter<bool>("addResolutions");
00082 if (addResolutions_) {
00083 resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
00084 }
00085 if (discriminatorTags_.empty()) {
00086 addDiscriminators_ = false;
00087 } else {
00088 for (std::vector<edm::InputTag>::const_iterator it = discriminatorTags_.begin(), ed = discriminatorTags_.end(); it != ed; ++it) {
00089 std::string label = it->label();
00090 std::string::size_type pos = label.find("JetTags");
00091 if ((pos != std::string::npos) && (pos != label.length() - 7)) {
00092 label.erase(pos+7);
00093 }
00094 discriminatorLabels_.push_back(label);
00095 }
00096 }
00097 if (tagInfoTags_.empty()) {
00098 addTagInfos_ = false;
00099 } else {
00100 for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed; ++it) {
00101 std::string label = it->label();
00102 std::string::size_type pos = label.find("TagInfos");
00103 if ((pos != std::string::npos) && (pos != label.length() - 8)) {
00104 label.erase(pos+8);
00105 }
00106 tagInfoLabels_.push_back(label);
00107 }
00108 }
00109 if (!addBTagInfo_) { addDiscriminators_ = false; addTagInfos_ = false; }
00110
00111 if ( useUserData_ ) {
00112 userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"));
00113 }
00114
00115 produces<std::vector<Jet> >();
00116 produces<reco::GenJetCollection> ("genJets");
00117 produces<std::vector<CaloTower> > ("caloTowers");
00118 produces<reco::PFCandidateCollection > ("pfCandidates");
00119 produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
00120 }
00121
00122
00123 PATJetProducer::~PATJetProducer() {
00124
00125 }
00126
00127
00128 void PATJetProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
00129 {
00130
00131 if (iEvent.isRealData()){
00132 getJetMCFlavour_ = false;
00133 addGenPartonMatch_ = false;
00134 addGenJetMatch_ = false;
00135 addPartonJetMatch_ = false;
00136 }
00137
00138
00139 edm::Handle<edm::View<reco::Jet> > jets;
00140 iEvent.getByLabel(jetsSrc_, jets);
00141
00142 if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00143 if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
00144
00145
00146 edm::Handle<reco::JetFlavourMatchingCollection> jetFlavMatch;
00147 if (getJetMCFlavour_) iEvent.getByLabel (jetPartonMapSource_, jetFlavMatch);
00148
00149
00150 edm::Handle<edm::Association<reco::GenParticleCollection> > partonMatch;
00151 if (addGenPartonMatch_) iEvent.getByLabel(genPartonSrc_, partonMatch);
00152
00153 edm::Handle<edm::Association<reco::GenJetCollection> > genJetMatch;
00154 if (addGenJetMatch_) iEvent.getByLabel(genJetSrc_, genJetMatch);
00155
00156
00157
00158
00159
00160
00161
00162 std::vector<edm::ValueMap<JetCorrFactors> > jetCorrs;
00163 if (addJetCorrFactors_) {
00164 for ( size_t i = 0; i < jetCorrFactorsSrc_.size(); ++i ) {
00165 edm::Handle<edm::ValueMap<JetCorrFactors> > jetCorr;
00166 iEvent.getByLabel(jetCorrFactorsSrc_[i], jetCorr);
00167 jetCorrs.push_back( *jetCorr );
00168 }
00169 }
00170
00171
00172 std::vector<edm::Handle<reco::JetFloatAssociation::Container> > jetDiscriminators;
00173 if (addBTagInfo_ && addDiscriminators_) {
00174 jetDiscriminators.resize(discriminatorTags_.size());
00175 for (size_t i = 0; i < discriminatorTags_.size(); ++i) {
00176 iEvent.getByLabel(discriminatorTags_[i], jetDiscriminators[i]);
00177 }
00178 }
00179 std::vector<edm::Handle<edm::View<reco::BaseTagInfo> > > jetTagInfos;
00180 if (addBTagInfo_ && addTagInfos_) {
00181 jetTagInfos.resize(tagInfoTags_.size());
00182 for (size_t i = 0; i < tagInfoTags_.size(); ++i) {
00183 iEvent.getByLabel(tagInfoTags_[i], jetTagInfos[i]);
00184 }
00185 }
00186
00187
00188 edm::Handle<reco::JetTracksAssociation::Container > hTrackAss;
00189 if (addAssociatedTracks_) iEvent.getByLabel(trackAssociation_, hTrackAss);
00190 edm::Handle<reco::JetFloatAssociation::Container > hJetChargeAss;
00191 if (addJetCharge_) iEvent.getByLabel(jetCharge_, hJetChargeAss);
00192
00193
00194 edm::Handle<reco::JetIDValueMap> hJetIDMap;
00195 if ( addJetID_ ) iEvent.getByLabel( jetIDMapLabel_, hJetIDMap );
00196
00197
00198 std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() );
00199
00200 std::auto_ptr<reco::GenJetCollection > genJetsOut ( new reco::GenJetCollection() );
00201 std::auto_ptr<std::vector<CaloTower> > caloTowersOut( new std::vector<CaloTower> () );
00202 std::auto_ptr<reco::PFCandidateCollection > pfCandidatesOut( new reco::PFCandidateCollection() );
00203 std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );
00204
00205
00206 edm::RefProd<reco::GenJetCollection > h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection >( "genJets" );
00207 edm::RefProd<std::vector<CaloTower> > h_caloTowersOut = iEvent.getRefBeforePut<std::vector<CaloTower> > ( "caloTowers" );
00208 edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
00209 edm::RefProd<edm::OwnVector<reco::BaseTagInfo> > h_tagInfosOut = iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo> > ( "tagInfos" );
00210
00211
00212
00213
00214 bool first=true;
00215 for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
00216
00217
00218 unsigned int idx = itJet - jets->begin();
00219 edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
00220 edm::Ptr<reco::Jet> jetPtr = jets->ptrAt(idx);
00221 Jet ajet(jetRef);
00222
00223
00224 if ( (ajet.isCaloJet() || ajet.isJPTJet() ) && embedCaloTowers_) {
00225 const reco::CaloJet *cj = 0;
00226 const reco::JPTJet * jptj = 0;
00227 if ( ajet.isCaloJet()) cj = dynamic_cast<const reco::CaloJet *>(jetRef.get());
00228 else {
00229 jptj = dynamic_cast<const reco::JPTJet *>(jetRef.get() );
00230 cj = dynamic_cast<const reco::CaloJet *>(jptj->getCaloJetRef().get() );
00231 }
00232 pat::CaloTowerFwdPtrCollection itowersRef;
00233 std::vector< CaloTowerPtr > itowers = cj->getCaloConstituents();
00234 for ( std::vector<CaloTowerPtr>::const_iterator towBegin = itowers.begin(),
00235 towEnd = itowers.end(), itow = towBegin;
00236 itow != towEnd; ++itow ) {
00237 caloTowersOut->push_back( **itow );
00238
00239 edm::Ref<std::vector<CaloTower> > caloTowerRef( h_caloTowersOut, caloTowersOut->size() - 1);
00240 edm::Ptr<CaloTower> caloForwardRef ( h_caloTowersOut.id(), caloTowerRef.key(), h_caloTowersOut.productGetter() );
00241
00242 edm::Ptr<CaloTower> caloBackRef ( *itow );
00243
00244 itowersRef.push_back( pat::CaloTowerFwdPtrCollection::value_type ( caloForwardRef, caloBackRef ) );
00245
00246 }
00247 ajet.setCaloTowers( itowersRef );
00248 }
00249
00250
00251 if (ajet.isPFJet() && embedPFCandidates_) {
00252 const reco::PFJet *cj = dynamic_cast<const reco::PFJet *>(jetRef.get());
00253 pat::PFCandidateFwdPtrCollection iparticlesRef;
00254 std::vector< reco::PFCandidatePtr > iparticles = cj->getPFConstituents();
00255 for ( std::vector<reco::PFCandidatePtr>::const_iterator partBegin = iparticles.begin(),
00256 partEnd = iparticles.end(), ipart = partBegin;
00257 ipart != partEnd; ++ipart ) {
00258 pfCandidatesOut->push_back( **ipart );
00259
00260 edm::Ref<reco::PFCandidateCollection> pfCollectionRef( h_pfCandidatesOut, pfCandidatesOut->size() - 1);
00261 edm::Ptr<reco::PFCandidate> pfForwardRef ( h_pfCandidatesOut.id(), pfCollectionRef.key(), h_pfCandidatesOut.productGetter() );
00262
00263 edm::Ptr<reco::PFCandidate> pfBackRef ( *ipart );
00264
00265 iparticlesRef.push_back( pat::PFCandidateFwdPtrCollection::value_type ( pfForwardRef, pfBackRef ) );
00266 }
00267 ajet.setPFCandidates( iparticlesRef );
00268 }
00269
00270 if (addJetCorrFactors_) {
00271
00272 for ( unsigned int i=0; i<jetCorrFactorsSrc_.size(); ++i ) {
00273 const JetCorrFactors& jcf = jetCorrs[i][jetRef];
00274
00275
00276 ajet.addJECFactors(jcf);
00277 }
00278 std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels();
00279 if(std::find(levels.begin(), levels.end(), "L2L3Residual")!=levels.end()){
00280 ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"));
00281 }
00282 else if(std::find(levels.begin(), levels.end(), "L3Absolute")!=levels.end()){
00283 ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"));
00284 }
00285 else{
00286 ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
00287 if(first){
00288 edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the correction applied jetCorrFactors \n"
00289 << "of module " << jetCorrs[0][jetRef].jecSet() << " jets will remain"
00290 << " uncorrected."; first=false;
00291 }
00292 }
00293 }
00294
00295
00296 if (getJetMCFlavour_) {
00297 ajet.setPartonFlavour( (*jetFlavMatch)[edm::RefToBase<reco::Jet>(jetRef)].getFlavour() );
00298 }
00299
00300 if (addGenPartonMatch_) {
00301 reco::GenParticleRef parton = (*partonMatch)[jetRef];
00302 if (parton.isNonnull() && parton.isAvailable()) {
00303 ajet.setGenParton(parton, embedGenPartonMatch_);
00304 }
00305 }
00306
00307 if (addGenJetMatch_) {
00308 reco::GenJetRef genjet = (*genJetMatch)[jetRef];
00309 if (genjet.isNonnull() && genjet.isAvailable()) {
00310 genJetsOut->push_back( *genjet );
00311
00312 edm::Ref<reco::GenJetCollection > genForwardRef ( h_genJetsOut, genJetsOut->size() - 1 );
00313
00314 edm::Ref<reco::GenJetCollection > genBackRef ( genjet );
00315
00316 edm::FwdRef<reco::GenJetCollection > genjetFwdRef ( genForwardRef, genBackRef );
00317 ajet.setGenJetRef(genjetFwdRef );
00318 }
00319 }
00320
00321 if (efficiencyLoader_.enabled()) {
00322 efficiencyLoader_.setEfficiencies( ajet, jetRef );
00323 }
00324
00325
00326 if (resolutionLoader_.enabled()) {
00327 resolutionLoader_.setResolutions(ajet);
00328 }
00329
00330
00331 if (addPartonJetMatch_) {
00332 }
00333
00334
00335 if (addBTagInfo_) {
00336 if (addDiscriminators_) {
00337 for (size_t k=0; k<jetDiscriminators.size(); ++k) {
00338 float value = (*jetDiscriminators[k])[jetRef];
00339 ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
00340 }
00341 }
00342 if (addTagInfos_) {
00343 for (size_t k=0; k<jetTagInfos.size(); ++k) {
00344 const edm::View<reco::BaseTagInfo> & taginfos = *jetTagInfos[k];
00345
00346 edm::Ptr<reco::BaseTagInfo> match;
00347
00348 if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
00349 match = taginfos.ptrAt(idx);
00350 } else {
00351
00352 for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end(); itTI != edTI; ++itTI) {
00353 if (itTI->jet() == jetRef) { match = taginfos.ptrAt( itTI - taginfos.begin() ); break; }
00354 }
00355 }
00356 if (match.isNonnull()) {
00357 tagInfosOut->push_back( match->clone() );
00358
00359 edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr ( h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1 );
00360
00361 edm::Ptr<reco::BaseTagInfo> tagInfoBackPtr ( match );
00362
00363 TagInfoFwdPtrCollection::value_type tagInfoFwdPtr( tagInfoForwardPtr, tagInfoBackPtr ) ;
00364 ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr );
00365 }
00366 }
00367 }
00368 }
00369
00370 if (addAssociatedTracks_) ajet.setAssociatedTracks( (*hTrackAss)[jetRef] );
00371
00372 if (addJetCharge_) ajet.setJetCharge( (*hJetChargeAss)[jetRef] );
00373
00374
00375 if (addJetID_ && ajet.isCaloJet() ) {
00376 reco::JetID jetId = (*hJetIDMap)[ jetRef ];
00377 ajet.setJetID( jetId );
00378 }
00379
00380 else if ( addJetID_ && ajet.isJPTJet() ){
00381 const reco::JPTJet *jptj = dynamic_cast<const reco::JPTJet *>(jetRef.get());
00382 reco::JetID jetId = (*hJetIDMap)[ jptj->getCaloJetRef() ];
00383 ajet.setJetID( jetId );
00384 }
00385 if ( useUserData_ ) {
00386 userDataHelper_.add( ajet, iEvent, iSetup );
00387 }
00388 patJets->push_back(ajet);
00389 }
00390
00391
00392 std::sort(patJets->begin(), patJets->end(), pTComparator_);
00393
00394
00395 iEvent.put(patJets);
00396
00397 iEvent.put( genJetsOut, "genJets" );
00398 iEvent.put( caloTowersOut, "caloTowers" );
00399 iEvent.put( pfCandidatesOut, "pfCandidates" );
00400 iEvent.put( tagInfosOut, "tagInfos" );
00401
00402
00403 }
00404
00405
00406 void PATJetProducer::fillDescriptions(edm::ConfigurationDescriptions & descriptions)
00407 {
00408 edm::ParameterSetDescription iDesc;
00409 iDesc.setComment("PAT jet producer module");
00410
00411
00412 iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");
00413
00414
00415 iDesc.add<bool>("embedCaloTowers", true)->setComment("embed external calo towers");
00416 iDesc.add<bool>("embedPFCandidates", true)->setComment("embed external PFCandidates");
00417
00418
00419 iDesc.add<bool>("addGenPartonMatch", true)->setComment("add MC matching");
00420 iDesc.add<bool>("embedGenPartonMatch", false)->setComment("embed MC matched MC information");
00421 iDesc.add<edm::InputTag>("genPartonMatch", edm::InputTag())->setComment("input with MC match information");
00422
00423 iDesc.add<bool>("addGenJetMatch", true)->setComment("add MC matching");
00424 iDesc.add<bool>("embedGenJetMatch", false)->setComment("embed MC matched MC information");
00425 iDesc.add<edm::InputTag>("genJetMatch", edm::InputTag())->setComment("input with MC match information");
00426
00427 iDesc.add<bool>("addJetCharge", true);
00428 iDesc.add<edm::InputTag>("jetChargeSource", edm::InputTag("patJetCharge"));
00429
00430
00431 iDesc.add<bool>("addJetID", true)->setComment("Add jet ID information");
00432 iDesc.add<edm::InputTag>("jetIDMap", edm::InputTag())->setComment("jet id map");
00433
00434 iDesc.add<bool>("addPartonJetMatch", false);
00435 iDesc.add<edm::InputTag>("partonJetSource", edm::InputTag("NOT IMPLEMENTED"));
00436
00437
00438 iDesc.add<bool>("addAssociatedTracks", true);
00439 iDesc.add<edm::InputTag>("trackAssociationSource", edm::InputTag("ic5JetTracksAssociatorAtVertex"));
00440
00441
00442 iDesc.add<bool>("addTagInfos", true);
00443 std::vector<edm::InputTag> emptyVInputTags;
00444 iDesc.add<std::vector<edm::InputTag> >("tagInfoSources", emptyVInputTags);
00445
00446
00447 iDesc.add<bool>("addJetCorrFactors", true);
00448 iDesc.add<std::vector<edm::InputTag> >("jetCorrFactorsSource", emptyVInputTags);
00449
00450
00451 iDesc.add<bool>("addBTagInfo",true);
00452 iDesc.add<bool>("addDiscriminators", true);
00453 iDesc.add<std::vector<edm::InputTag> >("discriminatorSources", emptyVInputTags);
00454
00455
00456 iDesc.add<bool>("getJetMCFlavour", true);
00457 iDesc.add<edm::InputTag>("JetPartonMapSource", edm::InputTag("jetFlavourAssociation"));
00458
00459 pat::helper::KinResolutionsLoader::fillDescription(iDesc);
00460
00461
00462 edm::ParameterSetDescription efficienciesPSet;
00463 efficienciesPSet.setAllowAnything();
00464 iDesc.add("efficiencies", efficienciesPSet);
00465 iDesc.add<bool>("addEfficiencies", false);
00466
00467
00468 edm::ParameterSetDescription userDataPSet;
00469 PATUserDataHelper<Jet>::fillDescription(userDataPSet);
00470 iDesc.addOptional("userData", userDataPSet);
00471
00472 descriptions.add("PATJetProducer", iDesc);
00473 }
00474
00475 #include "FWCore/Framework/interface/MakerMacros.h"
00476
00477 DEFINE_FWK_MODULE(PATJetProducer);
00478