CMS 3D CMS Logo

PFMuonUntagger.cc
Go to the documentation of this file.
1 // Produce:
3 // a Muon collection in which those muons are no longer PF
4 // a Muon collection with the original copy of only the muons that were changed
5 // a ValueMap<int> keyed to the new collection, with the old PF id
6 // Association<MuonCollection> that maps old to new and vice-versa,
7 // and from bad to new and vice-versa
8 
12 
14 
16 
19 #include <iostream>
20 
22  public:
24  ~PFMuonUntagger() override {};
25 
26  void produce(edm::StreamID iID, edm::Event&, const edm::EventSetup&) const override;
27 
28  private:
30  std::vector<edm::EDGetTokenT<reco::CandidateView>> badmuons_;
31 
32  template<typename H1, typename H2>
33  void writeAssociation(edm::Event &out, const H1 &from, const H2 &to, const std::vector<int> indices, const std::string &name) const {
35  std::unique_ptr<AssoMap> assomap(new AssoMap(to));
36  typename AssoMap::Filler filler(*assomap);
37  filler.insert(from, indices.begin(), indices.end());
38  filler.fill();
39  out.put(std::move(assomap), name);
40  }
41 
42  template<typename H1>
43  void writeValueMap(edm::Event &out, const H1 &from, const std::vector<int> values, const std::string &name) const {
44  typedef edm::ValueMap<int> IntMap;
45  std::unique_ptr<IntMap> intmap(new IntMap());
46  typename IntMap::Filler filler(*intmap);
47  filler.insert(from, values.begin(), values.end());
48  filler.fill();
49  out.put(std::move(intmap), name);
50  }
51 
52 };
53 
55  muons_(consumes<std::vector<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons")))
56 {
57  for (const auto & src : iConfig.getParameter<std::vector<edm::InputTag>>("badmuons")) {
58  badmuons_.push_back(consumes<reco::CandidateView>(src));
59  }
60 
61  produces<std::vector<reco::Muon>>();
62  produces<edm::ValueMap<int>>("oldPF");
63  produces<edm::Association<std::vector<reco::Muon>>>("newToOld");
64  produces<edm::Association<std::vector<reco::Muon>>>("oldToNew");
65  produces<std::vector<reco::Muon>>("bad");
66  produces<edm::Association<std::vector<reco::Muon>>>("badToNew");
67  produces<edm::Association<std::vector<reco::Muon>>>("newToBad");
68 }
69 
71 {
73  iEvent.getByToken(muons_, muons);
74 
75  unsigned int n = muons->size();
76  std::unique_ptr<std::vector<reco::Muon>> copy(new std::vector<reco::Muon>(*muons));
77  std::vector<int> oldPF(n);
78  std::vector<int> dummyIndices(n);
79  for (unsigned int i = 0; i < n; ++i) {
80  oldPF[i] = (*copy)[i].isPFMuon();
81  dummyIndices[i] = i;
82  }
83 
85  for (const auto & tag : badmuons_) {
86  iEvent.getByToken(tag, badmuons);
87  for (unsigned int j = 0, nj = badmuons->size(); j < nj; ++j) {
88  reco::CandidatePtr p = badmuons->ptrAt(j);
89  if (p.isNonnull() && p.id() == muons.id()) {
90  reco::Muon &mu = (*copy)[p.key()];
91  mu.setType(mu.type() & ~reco::Muon::PFMuon);
92  }
93  }
94  }
95 
96  std::unique_ptr<std::vector<reco::Muon>> bad(new std::vector<reco::Muon>());
97  std::vector<int> good2bad(n,-1), bad2good;
98  for (unsigned int i = 0; i < n; ++i) {
99  const reco::Muon &mu = (*copy)[i];
100  if (oldPF[i] != mu.isPFMuon()) {
101  bad->push_back((*muons)[i]);
102  bad2good.push_back(i);
103  good2bad[i] = (bad->size()-1);
104  }
105  }
106 
107  // Now we put things in the event
109  edm::OrphanHandle<std::vector<reco::Muon>> badmu = iEvent.put(std::move(bad), "bad");
110 
111  // Now we create the associations
112  writeAssociation(iEvent, newmu, muons, dummyIndices, "newToOld");
113  writeAssociation(iEvent, muons, newmu, dummyIndices, "oldToNew");
114  writeAssociation(iEvent, newmu, badmu, good2bad, "newToBad");
115  writeAssociation(iEvent, badmu, newmu, bad2good, "badToNew");
116 
117  // Now we create the valuemap
118  writeValueMap(iEvent, newmu, oldPF, "oldPF");
119 }
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
~PFMuonUntagger() override
void produce(edm::StreamID iID, edm::Event &, const edm::EventSetup &) const override
void setType(unsigned int type)
Definition: Muon.h:285
edm::EDGetTokenT< std::vector< reco::Muon > > muons_
def copy(args, dbName)
key_type key() const
Definition: Ptr.h:185
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Ptr< value_type > ptrAt(size_type i) const
void writeAssociation(edm::Event &out, const H1 &from, const H2 &to, const std::vector< int > indices, const std::string &name) const
size_type size() const
void writeValueMap(edm::Event &out, const H1 &from, const std::vector< int > values, const std::string &name) const
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: Muon.py:1
const int mu
Definition: Constants.h:22
std::vector< edm::EDGetTokenT< reco::CandidateView > > badmuons_
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:168
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:180
PFMuonUntagger(const edm::ParameterSet &)
fixed size matrix
HLT enums.
unsigned int type() const
Definition: Muon.h:286
bool isPFMuon() const
Definition: Muon.h:295
Take a Muon collection and one or more lists of bad muons to un-PF-tag.
def move(src, dest)
Definition: eostools.py:511