CMS 3D CMS Logo

PFCandidateMuonUntagger.cc
Go to the documentation of this file.
1 // - a PF candidate collection (which uses the old muons)
3 // - a map from the old muons to the new muons (in which some muons have been un-tagged and so are no longer PF muons)
4 // format: edm::Association<std::vector<reco::Muon>>
5 // Produce:
6 // - a new PFCandidate collection using the new muons, and in which the muons that have been un-tagged are removed
7 // - a second PFCandidate collection with just those discarded muons
8 // - a ValueMap<reco::PFCandidateRef> that maps the old to the new, and vice-versa
9 
13 
15 
19 
22 #include <iostream>
23 
25  public:
27  ~PFCandidateMuonUntagger() override {};
28 
29  void produce(edm::StreamID iID, edm::Event&, const edm::EventSetup&) const override;
30 
31  private:
34 
35  template<typename H1>
36  void writeValueMap(edm::Event &out, const H1 &from, const std::vector<int> values, const std::string &name) {
37  typedef edm::ValueMap<int> IntMap;
38  std::unique_ptr<IntMap> intmap(new IntMap());
39  typename IntMap::Filler filler(*intmap);
40  filler.insert(from, values.begin(), values.end());
41  filler.fill();
42  out.put(std::move(intmap), name);
43  }
44 
45 };
46 
48  pfcandidates_(consumes<std::vector<reco::PFCandidate>>(iConfig.getParameter<edm::InputTag>("pfcandidates"))),
49  oldToNewMuons_(consumes<edm::Association<std::vector<reco::Muon>>>(iConfig.getParameter<edm::InputTag>("oldToNewMuons")))
50 {
51  produces<std::vector<reco::PFCandidate>>();
52  produces<std::vector<reco::PFCandidate>>("discarded");
53  produces<edm::ValueMap<reco::PFCandidateRef>>();
54 }
55 
57 {
59  iEvent.getByToken(oldToNewMuons_, oldToNewMuons);
60 
62  iEvent.getByToken(pfcandidates_, pfcandidates);
63 
64  int n = pfcandidates->size();
65  std::unique_ptr<std::vector<reco::PFCandidate>> copy(new std::vector<reco::PFCandidate>());
66  std::unique_ptr<std::vector<reco::PFCandidate>> discarded(new std::vector<reco::PFCandidate>());
67  copy->reserve(n);
68  std::vector<int> oldToNew(n), newToOld, badToOld;
69  newToOld.reserve(n);
70 
71  int i = -1;
72  for (const reco::PFCandidate &pf : *pfcandidates) {
73  ++i;
74  if (pf.muonRef().isNonnull()) {
75  reco::MuonRef newRef = (*oldToNewMuons)[pf.muonRef()];
76  if (abs(pf.pdgId()) == 13 && !newRef->isPFMuon()) { // was untagging
77  discarded->push_back(pf);
78  oldToNew[i] = (-discarded->size());
79  badToOld.push_back(i);
80  discarded->back().setMuonRef(newRef);
81  } else {
82  copy->push_back(pf);
83  oldToNew[i] = (copy->size());
84  newToOld.push_back(i);
85  copy->back().setMuonRef(newRef);
86  }
87  } else {
88  copy->push_back(pf);
89  oldToNew[i] = (copy->size());
90  newToOld.push_back(i);
91  }
92  }
93 
94  // Now we put things in the event
96  edm::OrphanHandle<std::vector<reco::PFCandidate>> badpf = iEvent.put(std::move(discarded), "discarded");
97 
98  std::unique_ptr<edm::ValueMap<reco::PFCandidateRef>> pf2pf(new edm::ValueMap<reco::PFCandidateRef>());
100  std::vector<reco::PFCandidateRef> refs; refs.reserve(n);
101  // old to new
102  for (i = 0; i < n; ++i) {
103  if (oldToNew[i] > 0) {
104  refs.push_back(reco::PFCandidateRef(newpf, oldToNew[i]-1));
105  } else {
106  refs.push_back(reco::PFCandidateRef(badpf,-oldToNew[i]-1));
107  }
108  }
109  filler.insert(pfcandidates, refs.begin(), refs.end());
110  // new good to old
111  refs.clear();
112  for (int i : newToOld) {
113  refs.push_back(reco::PFCandidateRef(pfcandidates,i));
114  }
115  filler.insert(newpf, refs.begin(), refs.end());
116  // new bad to old
117  refs.clear();
118  for (int i : badToOld) {
119  refs.push_back(reco::PFCandidateRef(pfcandidates,i));
120  }
121  filler.insert(badpf, refs.begin(), refs.end());
122  // done
123  filler.fill();
124  iEvent.put(std::move(pf2pf));
125 }
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
def copy(args, dbName)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
edm::EDGetTokenT< edm::Association< std::vector< reco::Muon > > > oldToNewMuons_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
PFCandidateMuonUntagger(const edm::ParameterSet &)
Definition: Muon.py:1
void writeValueMap(edm::Event &out, const H1 &from, const std::vector< int > values, const std::string &name)
def oldToNew(schema)
Definition: lumidbDDL.py:402
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void produce(edm::StreamID iID, edm::Event &, const edm::EventSetup &) const override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
def newToOld(schema)
Definition: lumidbDDL.py:416
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< std::vector< reco::PFCandidate > > pfcandidates_