CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATMuonSlimmer.cc
Go to the documentation of this file.
1 
11 
15 
22 
23 namespace pat {
24 
26  public:
27  explicit PATMuonSlimmer(const edm::ParameterSet & iConfig);
28  virtual ~PATMuonSlimmer() { }
29 
30  virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
31  virtual void beginLuminosityBlock(const edm::LuminosityBlock&, const edm::EventSetup&) override final;
32 
33  private:
34  const edm::EDGetTokenT<pat::MuonCollection> src_;
35  std::vector<edm::EDGetTokenT<reco::PFCandidateCollection>> pf_;
36  std::vector<edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>>> pf2pc_;
40  std::unique_ptr<pat::ObjectModifier<pat::Muon> > muonModifier_;
41  };
42 
43 } // namespace
44 
46  src_(consumes<pat::MuonCollection>(iConfig.getParameter<edm::InputTag>("src"))),
47  linkToPackedPF_(iConfig.getParameter<bool>("linkToPackedPFCandidates")),
48  saveTeVMuons_(iConfig.getParameter<std::string>("saveTeVMuons")),
49  modifyMuon_(iConfig.getParameter<bool>("modifyMuons"))
50 {
51  if (linkToPackedPF_) {
52  const std::vector<edm::InputTag> & pf = (iConfig.existsAs<std::vector<edm::InputTag>>("pfCandidates") ?
53  iConfig.getParameter<std::vector<edm::InputTag>>("pfCandidates") :
54  std::vector<edm::InputTag>(1,iConfig.getParameter<edm::InputTag>("pfCandidates")));
55  const std::vector<edm::InputTag> & pf2pc = (iConfig.existsAs<std::vector<edm::InputTag>>("packedPFCandidates") ?
56  iConfig.getParameter<std::vector<edm::InputTag>>("packedPFCandidates") :
57  std::vector<edm::InputTag>(1,iConfig.getParameter<edm::InputTag>("packedPFCandidates")));
58  if (pf.size() != pf2pc.size()) throw cms::Exception("Configuration") << "Mismatching pfCandidates and packedPFCandidates\n";
59  for (const edm::InputTag &tag : pf) pf_.push_back(consumes<reco::PFCandidateCollection>(tag));
60  for (const edm::InputTag &tag : pf2pc) pf2pc_.push_back(consumes<edm::Association<pat::PackedCandidateCollection>>(tag));
61  }
62 
64  if( modifyMuon_ ) {
65  const edm::ParameterSet& mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
66  muonModifier_.reset(new pat::ObjectModifier<pat::Muon>(mod_config) );
67  muonModifier_->setConsumes(sumes);
68  } else {
69  muonModifier_.reset(nullptr);
70  }
71  produces<std::vector<pat::Muon> >();
72 }
73 
74 void
76  if( modifyMuon_ ) muonModifier_->setEventContent(iSetup);
77 }
78 
79 void
81  using namespace edm;
82  using namespace std;
83 
85  iEvent.getByToken(src_, src);
86 
87  auto_ptr<vector<pat::Muon> > out(new vector<pat::Muon>());
88  out->reserve(src->size());
89 
90  if( modifyMuon_ ) { muonModifier_->setEvent(iEvent); }
91 
92  std::map<reco::CandidatePtr,pat::PackedCandidateRef> mu2pc;
93  if (linkToPackedPF_) {
96  for (unsigned int ipfh = 0, npfh = pf_.size(); ipfh < npfh; ++ipfh) {
97  iEvent.getByToken(pf_[ipfh], pf);
98  iEvent.getByToken(pf2pc_[ipfh], pf2pc);
99  const auto & pfcoll = (*pf);
100  const auto & pfmap = (*pf2pc);
101  for (unsigned int i = 0, n = pf->size(); i < n; ++i) {
102  const reco::PFCandidate &p = pfcoll[i];
103  if (p.muonRef().isNonnull()) mu2pc[refToPtr(p.muonRef())] = pfmap[reco::PFCandidateRef(pf, i)];
104  }
105  }
106  }
107 
108  for (vector<pat::Muon>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
109  out->push_back(*it);
110  pat::Muon & mu = out->back();
111 
112  if( modifyMuon_ ) { muonModifier_->modify(mu); }
113 
114  if (saveTeVMuons_(mu)){mu.embedPickyMuon(); mu.embedTpfmsMuon(); mu.embedDytMuon();}
115  if (linkToPackedPF_) {
116  mu.refToOrig_ = refToPtr(mu2pc[mu.refToOrig_]);
117  }
118  }
119 
120  iEvent.put(out);
121 }
122 
124 using namespace pat;
const edm::EDGetTokenT< pat::MuonCollection > src_
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
void embedDytMuon()
embed reference to the above dyt Track
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
void embedTpfmsMuon()
embed reference to the above tpfms Track
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
PATMuonSlimmer(const edm::ParameterSet &iConfig)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const bool linkToPackedPF_
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
std::vector< pat::PackedCandidate > PackedCandidateCollection
std::vector< edm::EDGetTokenT< reco::PFCandidateCollection > > pf_
std::unique_ptr< pat::ObjectModifier< pat::Muon > > muonModifier_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
const int mu
Definition: Constants.h:22
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:450
Slimmer of PAT Muons.
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
string const
Definition: compareJSON.py:14
std::vector< Muon > MuonCollection
Definition: Muon.h:33
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
std::vector< edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > > pf2pc_
const StringCutObjectSelector< pat::Muon > saveTeVMuons_
void embedPickyMuon()
embed reference to the above picky Track
Analysis-level muon class.
Definition: Muon.h:49
edm::Ptr< reco::Candidate > refToOrig_
Definition: PATObject.h:421
virtual void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) overridefinal