CMS 3D CMS Logo

MuonDetCleaner.cc
Go to the documentation of this file.
2 
6 
11 
17 
18 template <typename T1, typename T2>
20  : mu_input_(consumes<edm::View<pat::Muon>>(iConfig.getParameter<edm::InputTag>("MuonCollection"))),
21  m_dtDigisToken(consumes<DTDigiCollection>(iConfig.getParameter<edm::InputTag>("dtDigiCollectionLabel"))),
22  m_cscDigisToken(consumes<CSCStripDigiCollection>(iConfig.getParameter<edm::InputTag>("cscDigiCollectionLabel"))),
23  m_dtGeometryToken(esConsumes<edm::Transition::BeginRun>()),
24  m_cscGeometryToken(esConsumes<edm::Transition::BeginRun>()),
25  propagatorToken_(esConsumes(edm::ESInputTag("", "SteppingHelixPropagatorAny"))),
26  m_digiMaxDistanceX(iConfig.getParameter<double>("digiMaxDistanceX")) {
27  std::vector<edm::InputTag> inCollections = iConfig.getParameter<std::vector<edm::InputTag>>("oldCollection");
28  for (const auto &inCollection : inCollections) {
29  inputs_[inCollection.instance()] = consumes<RecHitCollection>(inCollection);
30  produces<RecHitCollection>(inCollection.instance());
31  }
32  edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
33  edm::ConsumesCollector iC = consumesCollector();
35 }
36 
37 template <typename T1, typename T2>
39  // nothing to be done yet...
40 }
41 
42 template <typename T1, typename T2>
43 void MuonDetCleaner<T1, T2>::beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) {
44  // get the geometries from the event setup everytime the run (of the event which is processed right now to the previous event) changes
45  m_dtGeometry = iSetup.getHandle(m_dtGeometryToken);
46  m_cscGeometry = iSetup.getHandle(m_cscGeometryToken);
47 }
48 
49 template <typename T1, typename T2>
51  std::map<T1, std::vector<T2>> recHits_output; // This data format is easyer to handle
52  std::vector<uint32_t> vetoHits;
53 
54  // First fill the veto RecHits colletion with the Hits from the input muons
56  iEvent.getByToken(mu_input_, muonHandle);
57  edm::View<pat::Muon> muons = *muonHandle;
58  for (edm::View<pat::Muon>::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
59  const reco::Track *track = nullptr;
60  if (iMuon->isGlobalMuon())
61  track = iMuon->outerTrack().get();
62  else if (iMuon->isStandAloneMuon())
63  track = iMuon->outerTrack().get();
64  else if (iMuon->isRPCMuon())
65  track = iMuon->innerTrack().get(); // To add, try to access the rpc track
66  else if (iMuon->isTrackerMuon())
67  track = iMuon->innerTrack().get();
68  else {
69  edm::LogError("TauEmbedding") << "The imput muon: " << (*iMuon)
70  << " must be either global or does or be tracker muon";
71  assert(0);
72  }
73 
74  for (trackingRecHit_iterator hitIt = track->recHitsBegin(); hitIt != track->recHitsEnd(); ++hitIt) {
75  const TrackingRecHit &murechit = **hitIt; // Base class for all rechits
76  if (!(murechit).isValid())
77  continue;
78  if (!checkrecHit(murechit))
79  continue; // Check if the hit belongs to a specifc detector section
80  fillVetoHits(murechit, &vetoHits); // Go back to the very basic rechits
81  }
82 
83  sort(vetoHits.begin(), vetoHits.end());
84  vetoHits.erase(unique(vetoHits.begin(), vetoHits.end()), vetoHits.end());
85  iEvent.getByToken(m_dtDigisToken, m_dtDigis);
86  iEvent.getByToken(m_cscDigisToken, m_cscDigis);
88  trackAssociator_.setPropagator(&iSetup.getData(propagatorToken_));
90  trackAssociator_.associate(iEvent, iSetup, *track, parameters_, TrackDetectorAssociator::Any);
91 
92  // inspired from Muon Identification algorithm: https://github.com/cms-sw/cmssw/blob/3b943c0dbbdf4494cd66064a5a147301f38af295/RecoMuon/MuonIdentification/plugins/MuonIdProducer.cc#L911
93  // and the MuonShowerDigiFiller: https://github.com/cms-sw/cmssw/blob/29909891e150c9781f4ade2a6f7b5beb0bd67a6e/RecoMuon/MuonIdentification/interface/MuonShowerDigiFiller.h & https://github.com/cms-sw/cmssw/blob/29909891e150c9781f4ade2a6f7b5beb0bd67a6e/RecoMuon/MuonIdentification/src/MuonShowerDigiFiller.cc
94  for (const auto &chamber : info.chambers) {
95  if (chamber.id.subdetId() == MuonSubdetId::RPC)
96  continue;
97 
98  reco::MuonChamberMatch matchedChamber;
99 
100  const auto &lErr = chamber.tState.localError();
101  const auto &lPos = chamber.tState.localPosition();
102  const auto &lDir = chamber.tState.localDirection();
103  const auto &localError = lErr.positionError();
104 
105  matchedChamber.x = lPos.x();
106  matchedChamber.y = lPos.y();
107  matchedChamber.xErr = sqrt(localError.xx());
108  matchedChamber.yErr = sqrt(localError.yy());
109  matchedChamber.dXdZ = lDir.z() != 0 ? lDir.x() / lDir.z() : 9999;
110  matchedChamber.dYdZ = lDir.z() != 0 ? lDir.y() / lDir.z() : 9999;
111 
112  // DANGEROUS - compiler cannot guaranty parameters ordering
113  AlgebraicSymMatrix55 trajectoryCovMatrix = lErr.matrix();
114  matchedChamber.dXdZErr = trajectoryCovMatrix(1, 1) > 0 ? sqrt(trajectoryCovMatrix(1, 1)) : 0;
115  matchedChamber.dYdZErr = trajectoryCovMatrix(2, 2) > 0 ? sqrt(trajectoryCovMatrix(2, 2)) : 0;
116 
117  matchedChamber.edgeX = chamber.localDistanceX;
118  matchedChamber.edgeY = chamber.localDistanceY;
119 
120  matchedChamber.id = chamber.id;
121 
122  // DT chamber
123  if (matchedChamber.detector() == MuonSubdetId::DT) {
124  double xTrack = matchedChamber.x;
125 
126  for (int sl = 1; sl <= DTChamberId::maxSuperLayerId; sl += 2) {
127  for (int layer = 1; layer <= DTChamberId::maxLayerId; ++layer) {
128  const DTLayerId layerId(DTChamberId(matchedChamber.id.rawId()), sl, layer);
129  auto range = m_dtDigis->get(layerId);
130 
131  for (auto digiIt = range.first; digiIt != range.second; ++digiIt) {
132  const auto topo = m_dtGeometry->layer(layerId)->specificTopology();
133  double xWire = topo.wirePosition((*digiIt).wire());
134  double dX = std::abs(xWire - xTrack);
135 
136  if (dX < m_digiMaxDistanceX) {
137  vetoHits.push_back(matchedChamber.id.rawId());
138  }
139  }
140  }
141  }
142  }
143 
144  else if (matchedChamber.detector() == MuonSubdetId::CSC) {
145  double xTrack = matchedChamber.x;
146  double yTrack = matchedChamber.y;
147 
148  for (int iLayer = 1; iLayer <= CSCDetId::maxLayerId(); ++iLayer) {
149  const CSCDetId chId(matchedChamber.id.rawId());
150  const CSCDetId layerId(chId.endcap(), chId.station(), chId.ring(), chId.chamber(), iLayer);
151  auto range = m_cscDigis->get(layerId);
152 
153  for (auto digiIt = range.first; digiIt != range.second; ++digiIt) {
154  std::vector<int> adcVals = digiIt->getADCCounts();
155  bool hasFired = false;
156  float pedestal = 0.5 * (float)(adcVals[0] + adcVals[1]);
157  float threshold = 13.3;
158  float diff = 0.;
159  for (const auto &adcVal : adcVals) {
160  diff = (float)adcVal - pedestal;
161  if (diff > threshold) {
162  hasFired = true;
163  break;
164  }
165  }
166 
167  if (!hasFired)
168  continue;
169 
170  const CSCLayerGeometry *layerGeom = m_cscGeometry->layer(layerId)->geometry();
171  Float_t xStrip = layerGeom->xOfStrip(digiIt->getStrip(), yTrack);
172  float dX = std::abs(xStrip - xTrack);
173 
174  if (dX < m_digiMaxDistanceX) {
175  vetoHits.push_back(matchedChamber.id.rawId());
176  }
177  }
178  }
179  }
180  }
181 
182  // End improved cleaning in the muon chambers
183  }
184 
185  sort(vetoHits.begin(), vetoHits.end());
186  vetoHits.erase(unique(vetoHits.begin(), vetoHits.end()), vetoHits.end());
187 
188  // Now this can also handle different instance
189  for (auto input_ : inputs_) {
190  // Second read in the RecHit Colltection which is to be replaced, without the vetoRecHits
191  typedef edm::Handle<RecHitCollection> RecHitCollectionHandle;
192  RecHitCollectionHandle RecHitinput;
193  iEvent.getByToken(input_.second, RecHitinput);
194  for (typename RecHitCollection::const_iterator recHit = RecHitinput->begin(); recHit != RecHitinput->end();
195  ++recHit) { // loop over the basic rec hit collection (DT CSC or RPC)
196  if (find(vetoHits.begin(), vetoHits.end(), getRawDetId(*recHit)) != vetoHits.end())
197  continue; // If the hit is not in the
198  T1 detId(getRawDetId(*recHit));
199  recHits_output[detId].push_back(*recHit);
200  }
201 
202  // Last step savet the output in the CMSSW Data Format
203  std::unique_ptr<RecHitCollection> output(new RecHitCollection());
204  for (typename std::map<T1, std::vector<T2>>::const_iterator recHit = recHits_output.begin();
205  recHit != recHits_output.end();
206  ++recHit) {
207  output->put(recHit->first, recHit->second.begin(), recHit->second.end());
208  }
209  output->post_insert();
210  iEvent.put(std::move(output), input_.first);
211  }
212 }
213 
214 template <typename T1, typename T2>
215 void MuonDetCleaner<T1, T2>::fillVetoHits(const TrackingRecHit &rh, std::vector<uint32_t> *HitsList) {
216  std::vector<const TrackingRecHit *> rh_components = rh.recHits();
217  if (rh_components.empty()) {
218  HitsList->push_back(rh.rawId());
219  } else {
220  for (std::vector<const TrackingRecHit *>::const_iterator rh_component = rh_components.begin();
221  rh_component != rh_components.end();
222  ++rh_component) {
223  fillVetoHits(**rh_component, HitsList);
224  }
225  }
226 }
227 
228 //-------------------------------------------------------------------------------
229 // define 'getRawDetId' functions used for different types of recHits
230 //-------------------------------------------------------------------------------
231 
232 template <typename T1, typename T2>
234  assert(0); // CV: make sure general function never gets called;
235  // always use template specializations
236 }
237 
238 template <>
240  return recHit.cscDetId().rawId();
241 }
242 
243 template <>
245  return recHit.geographicalId().rawId();
246 }
247 
248 template <>
250  return recHit.rpcId().rawId();
251 }
252 
253 //-------------------------------------------------------------------------------
254 // find out what the kind of RecHit used by imput muons rechit
255 //-------------------------------------------------------------------------------
256 
257 template <typename T1, typename T2>
259  edm::LogError("TauEmbedding") << "!!!! Please add the checkrecHit for the individual class templates " assert(0);
260 }
261 
262 template <>
264  const std::type_info &hit_type = typeid(recHit);
265  if (hit_type == typeid(CSCSegment)) {
266  return true;
267  } // This should be the default one (which are included in the global (outer) muon track)
268  else if (hit_type == typeid(CSCRecHit2D)) {
269  return true;
270  }
271  return false;
272 }
273 
274 template <>
276  return recHit.cscDetId().rawId();
277 }
278 
279 template <>
281  return recHit.geographicalId().rawId();
282 }
283 
284 template <>
286  const std::type_info &hit_type = typeid(recHit);
287  if (hit_type == typeid(DTRecSegment4D)) {
288  return true;
289  } // This should be the default one (which are included in the global (outer) muon track)
290  else if (hit_type == typeid(DTRecHit1D)) {
291  return true;
292  } else if (hit_type == typeid(DTSLRecCluster)) {
293  return true;
294  } else if (hit_type == typeid(DTSLRecSegment2D)) {
295  return true;
296  }
297  return false;
298 }
299 
300 template <>
302  const std::type_info &hit_type = typeid(recHit);
303  if (hit_type == typeid(RPCRecHit)) {
304  return true;
305  } // This should be the default one (which are included in the global (outer) muon track)
306  return false;
307 }
308 
309 template <>
311  const std::type_info &hit_type = typeid(recHit);
312  if (hit_type == typeid(CSCSegment)) {
313  return true;
314  } // This should be the default one (which are included in the global (outer) muon track)
315  return false;
316 }
317 
318 template <>
320  const std::type_info &hit_type = typeid(recHit);
321  if (hit_type == typeid(DTRecSegment4D)) {
322  return true;
323  } // This should be the default one (which are included in the global (outer) muon track)
324  else if (hit_type == typeid(DTRecHit1D)) {
325  return true;
326  } else if (hit_type == typeid(DTSLRecCluster)) {
327  return true;
328  } else if (hit_type == typeid(DTSLRecSegment2D)) {
329  return true;
330  }
331  return false;
332 }
333 
MuonDetCleaner< CSCDetId, CSCSegment > CSCSegmentColCleaner
MuonDetCleaner< DTChamberId, DTRecSegment4D > DTRecSegment4DColCleaner
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
static const TGPicture * info(bool iBackgroundIsBlack)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
uint32_t getRawDetId(const T2 &)
void loadParameters(const edm::ParameterSet &, edm::ConsumesCollector &)
MuonDetCleaner< CSCDetId, CSCRecHit2D > CSCRecHitColCleaner
bool checkrecHit(const TrackingRecHit &)
~MuonDetCleaner() override
id_type rawId() const
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:214
assert(be >=bs)
Definition: HeavyIon.h:7
void beginRun(const edm::Run &, const edm::EventSetup &) override
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
int iEvent
Definition: GenABIO.cc:224
TrackAssociatorParameters parameters_
Definition: Muon.py:1
static const int maxLayerId
highest layer id
Definition: DTChamberId.h:73
T sqrt(T t)
Definition: SSEVec.h:23
def unique(seq, keepstr=True)
Definition: tier0.py:24
virtual std::vector< const TrackingRecHit * > recHits() const =0
Access to component RecHits (if any)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Transition
Definition: Transition.h:12
void produce(edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static const int maxSuperLayerId
highest superlayer id
Definition: DTChamberId.h:69
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
float dX(const MatchPair &match)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
static constexpr int RPC
Definition: MuonSubdetId.h:13
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
MuonDetCleaner(const edm::ParameterSet &)
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:88
MuonDetCleaner< RPCDetId, RPCRecHit > RPCRecHitColCleaner
void fillVetoHits(const TrackingRecHit &, std::vector< uint32_t > *)
MuonDetCleaner< DTLayerId, DTRecHit1DPair > DTRecHitColCleaner
Definition: output.py:1
float xOfStrip(int strip, float y=0.) const
static constexpr int DT
Definition: MuonSubdetId.h:11
static constexpr int CSC
Definition: MuonSubdetId.h:12
static int maxLayerId()
Definition: CSCDetId.h:243
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
std::map< std::string, edm::EDGetTokenT< RecHitCollection > > inputs_