CMS 3D CMS Logo

CaloMuonMerger.cc
Go to the documentation of this file.
1 //
2 //
3 
15 
19 
21 
23 public:
24  explicit CaloMuonMerger(const edm::ParameterSet& iConfig);
25  ~CaloMuonMerger() override {}
26 
27  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
28 
29 private:
39 
43 };
44 
46  : muons_(iConfig.getParameter<edm::InputTag>("muons")),
47  muonsCut_(iConfig.existsAs<std::string>("muonsCut") ? iConfig.getParameter<std::string>("muonsCut") : ""),
48  mergeCaloMuons_(iConfig.existsAs<bool>("mergeCaloMuons") ? iConfig.getParameter<bool>("mergeCaloMuons") : true),
49  caloMuons_(iConfig.getParameter<edm::InputTag>("caloMuons")),
50  caloMuonsCut_(iConfig.existsAs<std::string>("caloMuonsCut") ? iConfig.getParameter<std::string>("caloMuonsCut")
51  : ""),
52  minCaloCompatibility_(mergeCaloMuons_ ? iConfig.getParameter<double>("minCaloCompatibility") : 0),
53  mergeTracks_(iConfig.existsAs<bool>("mergeTracks") ? iConfig.getParameter<bool>("mergeTracks") : false),
54  tracks_(mergeTracks_ ? iConfig.getParameter<edm::InputTag>("tracks") : edm::InputTag()),
55  tracksCut_(iConfig.existsAs<std::string>("tracksCut") ? iConfig.getParameter<std::string>("tracksCut") : "") {
56  muonToken_ = consumes<std::vector<reco::Muon>>(muons_);
57  caloMuonToken_ = consumes<std::vector<reco::CaloMuon>>(caloMuons_);
58  trackToken_ = consumes<std::vector<reco::Track>>(tracks_);
59  produces<std::vector<reco::Muon>>();
60 }
61 
66 
67  iEvent.getByToken(muonToken_, muons);
68  if (mergeCaloMuons_)
69  iEvent.getByToken(caloMuonToken_, caloMuons);
70  if (mergeTracks_)
71  iEvent.getByToken(trackToken_, tracks);
72 
73  auto out = std::make_unique<std::vector<reco::Muon>>();
74  out->reserve(muons->size() + (mergeTracks_ ? tracks->size() : 0));
75 
76  // copy reco::Muons, turning on the CaloCompatibility flag if enabled and possible
77  for (std::vector<reco::Muon>::const_iterator it = muons->begin(), ed = muons->end(); it != ed; ++it) {
78  if (!muonsCut_(*it))
79  continue;
80  out->push_back(*it);
81  reco::Muon& mu = out->back();
82  if (mergeCaloMuons_ && mu.track().isNonnull()) {
83  if (mu.isCaloCompatibilityValid()) {
84  if (mu.caloCompatibility() >= minCaloCompatibility_) {
85  mu.setType(mu.type() | reco::Muon::CaloMuon);
86  }
87  } else
88  throw cms::Exception("Boh") << "Muon with track and no CaloCompatibility; pt = " << mu.pt()
89  << ", eta = " << mu.eta() << ", type = " << mu.type() << "\n";
90  }
91  }
92 
93  if (mergeCaloMuons_) {
94  // copy reco::CaloMuon
95  for (std::vector<reco::CaloMuon>::const_iterator it = caloMuons->begin(), ed = caloMuons->end(); it != ed; ++it) {
96  if (!caloMuonsCut_(*it))
97  continue;
98  // make a reco::Muon
99  reco::TrackRef track = it->track();
100  double energy = sqrt(track->p() * track->p() + 0.011163691);
101  math::XYZTLorentzVector p4(track->px(), track->py(), track->pz(), energy);
102  out->push_back(reco::Muon(track->charge(), p4, track->vertex()));
103  reco::Muon& mu = out->back();
104  // fill info
105  mu.setCalEnergy(it->calEnergy());
106  mu.setCaloCompatibility(it->caloCompatibility());
107  mu.setInnerTrack(track);
108  mu.setType(reco::Muon::CaloMuon);
109  }
110  }
111 
112  // merge reco::Track avoiding duplication of innerTracks
113  if (mergeTracks_) {
114  for (size_t i = 0; i < tracks->size(); i++) {
116  if (!tracksCut_(track))
117  continue;
118  // check if it is a muon or calomuon
119  bool isMuon = false;
120  for (std::vector<reco::Muon>::const_iterator muon = muons->begin(); muon < muons->end(); muon++) {
121  if (muon->innerTrack() == track) {
122  isMuon = true;
123  break;
124  }
125  }
126  if (isMuon)
127  continue;
128  if (mergeCaloMuons_) {
129  bool isCaloMuon = false;
130  for (std::vector<reco::CaloMuon>::const_iterator muon = caloMuons->begin(); muon < caloMuons->end(); muon++) {
131  if (muon->innerTrack() == track) {
132  isCaloMuon = true;
133  break;
134  }
135  }
136  if (isCaloMuon)
137  continue;
138  }
139  // make a reco::Muon
140  double energy = sqrt(track->p() * track->p() + 0.011163691);
141  math::XYZTLorentzVector p4(track->px(), track->py(), track->pz(), energy);
142  out->push_back(reco::Muon(track->charge(), p4, track->vertex()));
143  reco::Muon& mu = out->back();
144  // fill info
145  mu.setInnerTrack(track);
146  }
147  }
148 
149  iEvent.put(std::move(out));
150 }
151 
bool isMuon(const Candidate &part)
Definition: pdgIdUtils.h:9
StringCutObjectSelector< reco::CaloMuon, false > caloMuonsCut_
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:214
double minCaloCompatibility_
edm::EDGetTokenT< std::vector< reco::Track > > trackToken_
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
int iEvent
Definition: GenABIO.cc:224
StringCutObjectSelector< reco::TrackRef, false > tracksCut_
edm::InputTag tracks_
T sqrt(T t)
Definition: SSEVec.h:23
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< std::vector< reco::Muon > > muonToken_
CaloMuonMerger(const edm::ParameterSet &iConfig)
edm::InputTag muons_
~CaloMuonMerger() override
StringCutObjectSelector< reco::Muon, false > muonsCut_
edm::EDGetTokenT< std::vector< reco::CaloMuon > > caloMuonToken_
static const unsigned int CaloMuon
Definition: Muon.h:292
Merges reco::CaloMuons, reco::Muons and optionally reco::Tracks avoiding innerTrack duplications in a...
HLT enums.
edm::InputTag caloMuons_
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override