CMS 3D CMS Logo

MuonDetCleaner.h
Go to the documentation of this file.
1 
15 #ifndef TauAnalysis_MCEmbeddingTools_MuonDetCleaner_H
16 #define TauAnalysis_MCEmbeddingTools_MuonDetCleaner_H
17 
23 
26 
29 
30 #include <string>
31 #include <vector>
32 #include <map>
33 
34 template <typename T1, typename T2>
36 public:
37  explicit MuonDetCleaner(const edm::ParameterSet&);
38  ~MuonDetCleaner() override;
39 
40 private:
41  void produce(edm::Event&, const edm::EventSetup&) override;
42 
44  void fillVetoHits(const TrackingRecHit&, std::vector<uint32_t>*);
45 
46  uint32_t getRawDetId(const T2&);
47  bool checkrecHit(const TrackingRecHit&);
48 
50 
51  std::map<std::string, edm::EDGetTokenT<RecHitCollection> > inputs_;
52 };
53 
54 template <typename T1, typename T2>
56  : mu_input_(consumes<edm::View<pat::Muon> >(iConfig.getParameter<edm::InputTag>("MuonCollection"))) {
57  std::vector<edm::InputTag> inCollections = iConfig.getParameter<std::vector<edm::InputTag> >("oldCollection");
58  for (const auto& inCollection : inCollections) {
59  inputs_[inCollection.instance()] = consumes<RecHitCollection>(inCollection);
60  produces<RecHitCollection>(inCollection.instance());
61  }
62 }
63 
64 template <typename T1, typename T2>
66  // nothing to be done yet...
67 }
68 
69 template <typename T1, typename T2>
71  std::map<T1, std::vector<T2> > recHits_output; // This data format is easyer to handle
72  std::vector<uint32_t> vetoHits;
73 
74  // First fill the veto RecHits colletion with the Hits from the input muons
76  iEvent.getByToken(mu_input_, muonHandle);
77  edm::View<pat::Muon> muons = *muonHandle;
78  for (edm::View<pat::Muon>::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
79  const reco::Track* track = nullptr;
80  if (iMuon->isGlobalMuon())
81  track = iMuon->outerTrack().get();
82  else if (iMuon->isStandAloneMuon())
83  track = iMuon->outerTrack().get();
84  else if (iMuon->isRPCMuon())
85  track = iMuon->innerTrack().get(); // To add, try to access the rpc track
86  else if (iMuon->isTrackerMuon())
87  track = iMuon->innerTrack().get();
88  else {
89  edm::LogError("TauEmbedding") << "The imput muon: " << (*iMuon)
90  << " must be either global or does or be tracker muon";
91  assert(0);
92  }
93 
94  for (trackingRecHit_iterator hitIt = track->recHitsBegin(); hitIt != track->recHitsEnd(); ++hitIt) {
95  const TrackingRecHit& murechit = **hitIt; // Base class for all rechits
96  if (!(murechit).isValid())
97  continue;
98  if (!checkrecHit(murechit))
99  continue; // Check if the hit belongs to a specifc detector section
100  fillVetoHits(murechit, &vetoHits); // Go back to the very basic rechits
101  }
102  }
103 
104  // Now this can also handle different instance
105  for (auto input_ : inputs_) {
106  // Second read in the RecHit Colltection which is to be replaced, without the vetoRecHits
107  typedef edm::Handle<RecHitCollection> RecHitCollectionHandle;
108  RecHitCollectionHandle RecHitinput;
109  iEvent.getByToken(input_.second, RecHitinput);
110  for (typename RecHitCollection::const_iterator recHit = RecHitinput->begin(); recHit != RecHitinput->end();
111  ++recHit) { // loop over the basic rec hit collection (DT CSC or RPC)
112  //if (find(vetoHits.begin(),vetoHits.end(),getRawDetId(*recHit)) == vetoHits.end()) continue; // For the invertec selcetion
113  if (find(vetoHits.begin(), vetoHits.end(), getRawDetId(*recHit)) != vetoHits.end())
114  continue; // If the hit is not in the
115  T1 detId(getRawDetId(*recHit));
116  recHits_output[detId].push_back(*recHit);
117  }
118 
119  // Last step savet the output in the CMSSW Data Format
120  std::unique_ptr<RecHitCollection> output(new RecHitCollection());
121  for (typename std::map<T1, std::vector<T2> >::const_iterator recHit = recHits_output.begin();
122  recHit != recHits_output.end();
123  ++recHit) {
124  output->put(recHit->first, recHit->second.begin(), recHit->second.end());
125  }
126  output->post_insert();
127  iEvent.put(std::move(output), input_.first);
128  }
129 }
130 
131 template <typename T1, typename T2>
132 void MuonDetCleaner<T1, T2>::fillVetoHits(const TrackingRecHit& rh, std::vector<uint32_t>* HitsList) {
133  std::vector<const TrackingRecHit*> rh_components = rh.recHits();
134  if (rh_components.empty()) {
135  HitsList->push_back(rh.rawId());
136  } else {
137  for (std::vector<const TrackingRecHit*>::const_iterator rh_component = rh_components.begin();
138  rh_component != rh_components.end();
139  ++rh_component) {
140  fillVetoHits(**rh_component, HitsList);
141  }
142  }
143 }
144 
145 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
uint32_t getRawDetId(const T2 &)
bool checkrecHit(const TrackingRecHit &)
edm::RangeMap< T1, edm::OwnVector< T2 > > RecHitCollection
~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
assert(be >=bs)
Definition: HeavyIon.h:7
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
int iEvent
Definition: GenABIO.cc:224
Definition: Muon.py:1
virtual std::vector< const TrackingRecHit * > recHits() const =0
Access to component RecHits (if any)
void produce(edm::Event &, const edm::EventSetup &) override
const edm::EDGetTokenT< edm::View< pat::Muon > > mu_input_
MuonDetCleaner(const edm::ParameterSet &)
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
void fillVetoHits(const TrackingRecHit &, std::vector< uint32_t > *)
Definition: output.py:1
def move(src, dest)
Definition: eostools.py:511
std::map< std::string, edm::EDGetTokenT< RecHitCollection > > inputs_