CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonDetRecHitMixer.h
Go to the documentation of this file.
1 
19 
22 
24 
25 #include <string>
26 #include <vector>
27 #include <map>
28 
29 template <typename T1, typename T2>
31 {
32  public:
33  explicit MuonDetRecHitMixer(const edm::ParameterSet&);
35 
36  private:
37  virtual void produce(edm::Event&, const edm::EventSetup&);
38 
40 
41  typedef std::map<uint32_t, int> detIdToIntMap;
42 
43  void addRecHits(std::map<T1, std::vector<T2> >&, const RecHitCollection&, bool, const detIdToIntMap&, const detIdToIntMap&, int&);
44 
45  void printHitMapRH(const edm::EventSetup&, const RecHitCollection&);
46 
47  uint32_t getRawDetId(const T2&);
48 
50 
52  {
57  };
58  std::vector<TodoListEntry> todoList_;
59 
62 
64 };
65 
66 template <typename T1, typename T2>
68  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
69  srcHitMapMuPlus_(cfg.getParameter<edm::InputTag>("srcHitMapMuPlus")),
70  srcHitMapMuMinus_(cfg.getParameter<edm::InputTag>("srcHitMapMuMinus"))
71 {
72  edm::VParameterSet cfgTodoList = cfg.getParameter<edm::VParameterSet>("todo");
73  for ( edm::VParameterSet::const_iterator cfgTodoItem = cfgTodoList.begin();
74  cfgTodoItem != cfgTodoList.end(); ++cfgTodoItem ) {
75  TodoListEntry todoItem;
76  todoItem.srcCollection1_ = cfgTodoItem->getParameter<edm::InputTag>("collection1");
77  todoItem.cleanCollection1_ = cfgTodoItem->getParameter<bool>("cleanCollection1");
78  todoItem.srcCollection2_ = cfgTodoItem->getParameter<edm::InputTag>("collection2");
79  todoItem.cleanCollection2_ = cfgTodoItem->getParameter<bool>("cleanCollection2");
80 
81  std::string instanceLabel1 = todoItem.srcCollection1_.instance();
82  std::string instanceLabel2 = todoItem.srcCollection2_.instance();
83  if ( instanceLabel1 != instanceLabel2 ) {
84  throw cms::Exception("Configuration")
85  << "Mismatch in Instance labels for collection 1 = " << instanceLabel1 << " and 2 = " << instanceLabel2 << " !!\n";
86  }
87 
88  if ( instanceLabel1 == "" ) produces<RecHitCollection>();
89  else produces<RecHitCollection>(instanceLabel1);
90 
91  todoList_.push_back(todoItem);
92  }
93 
94  if ( todoList_.size() == 0 ) {
95  throw cms::Exception("Configuration")
96  << "Empty to-do list !!\n";
97  }
98 
99  verbosity_ = ( cfg.exists("verbosity") ) ?
100  cfg.getParameter<int>("verbosity") : 0;
101 }
102 
103 template <typename T1, typename T2>
105 {
106 // nothing to be done yet...
107 }
108 
109 template <typename T1, typename T2>
111 {
112  edm::Handle<detIdToIntMap> hitMapMuPlus;
113  evt.getByLabel(srcHitMapMuPlus_, hitMapMuPlus);
114  edm::Handle<detIdToIntMap> hitMapMuMinus;
115  evt.getByLabel(srcHitMapMuMinus_, hitMapMuMinus);
116 
117  for ( typename std::vector<TodoListEntry>::const_iterator todoItem = todoList_.begin();
118  todoItem != todoList_.end(); ++todoItem ) {
119 
120  typedef edm::Handle<RecHitCollection> RecHitCollectionHandle;
121  RecHitCollectionHandle recHitCollection1;
122  evt.getByLabel(todoItem->srcCollection1_, recHitCollection1);
123  RecHitCollectionHandle recHitCollection2;
124  evt.getByLabel(todoItem->srcCollection2_, recHitCollection2);
125 
126  std::map<T1, std::vector<T2> > recHits_output;
127 
128  int numHits_cleaned = 0;
129  addRecHits(recHits_output, *recHitCollection1, todoItem->cleanCollection1_, *hitMapMuPlus, *hitMapMuMinus, numHits_cleaned);
130  addRecHits(recHits_output, *recHitCollection2, todoItem->cleanCollection2_, *hitMapMuPlus, *hitMapMuMinus, numHits_cleaned);
131 
132  std::auto_ptr<RecHitCollection> recHitCollection_output(new RecHitCollection());
133  for ( typename std::map<T1, std::vector<T2> >::const_iterator recHit = recHits_output.begin();
134  recHit != recHits_output.end(); ++recHit ) {
135  recHitCollection_output->put(recHit->first, recHit->second.begin(), recHit->second.end());
136  }
137  std::string instanceLabel = todoItem->srcCollection1_.instance();
138  if ( verbosity_ ) {
139  std::cout << "<MuonDetRecHitMixer::produce>:" << std::endl;
140  std::cout << " #Hits(input1 = " << todoItem->srcCollection1_.label() << ":" << todoItem->srcCollection1_.instance() << ") = " << recHitCollection1->size() << std::endl;
141  if ( verbosity_ >= 2 ) printHitMapRH(es, *recHitCollection1);
142  std::cout << " #Hits(input2 = " << todoItem->srcCollection2_.label() << ":" << todoItem->srcCollection2_.instance() << ") = " << recHitCollection2->size() << std::endl;
143  if ( verbosity_ >= 2 ) printHitMapRH(es, *recHitCollection2);
144  std::cout << " #Hits(output = " << moduleLabel_ << ":" << instanceLabel << ") = " << recHitCollection_output->size() << std::endl;
145  if ( verbosity_ >= 2 ) printHitMapRH(es, *recHitCollection_output);
146  std::cout << " #Hits = " << numHits_cleaned << " removed during cleaning." << std::endl;
147  }
148  evt.put(recHitCollection_output, instanceLabel);
149  }
150 }
151 
152 namespace
153 {
154  bool matchHitMapRH(uint32_t rawDetId, const std::map<uint32_t, int>& hitMap)
155  {
156  for ( std::map<uint32_t, int>::const_iterator rh = hitMap.begin();
157  rh != hitMap.end(); ++rh ) {
158  if ( matchMuonDetId(rawDetId, rh->first) && rh->second > 0 ) return true;
159  }
160  return false;
161  }
162 }
163 
164 template <typename T1, typename T2>
165 void MuonDetRecHitMixer<T1,T2>::addRecHits(std::map<T1, std::vector<T2> >& recHits_output, const RecHitCollection& recHitCollection_input,
166  bool applyCleaning, const detIdToIntMap& hitMapMuPlus, const detIdToIntMap& hitMapMuMinus, int& numHits_cleaned)
167 {
168  for ( typename RecHitCollection::const_iterator recHit = recHitCollection_input.begin();
169  recHit != recHitCollection_input.end(); ++recHit ) {
170  uint32_t rawDetId = getRawDetId(*recHit);
171 
172  bool isToBeCleaned = false;
173  if ( applyCleaning ) {
174  isToBeCleaned |= matchHitMapRH(rawDetId, hitMapMuPlus);
175  isToBeCleaned |= matchHitMapRH(rawDetId, hitMapMuMinus);
176  }
177 
178  if ( !isToBeCleaned ) {
179  T1 detId(rawDetId);
180  recHits_output[detId].push_back(*recHit);
181  } else {
182  ++numHits_cleaned;
183  }
184  }
185 }
186 
187 template <typename T1, typename T2>
189 {
190  std::cout << "detIds:";
191  for ( typename RecHitCollection::const_iterator rh = recHits.begin();
192  rh != recHits.end(); ++rh ) {
193  printMuonDetId(es, getRawDetId(*rh));
194  }
195 }
std::vector< TodoListEntry > todoList_
std::map< uint32_t, int > detIdToIntMap
T getParameter(std::string const &) const
edm::RangeMap< T1, edm::OwnVector< T2 > > RecHitCollection
bool matchMuonDetId(uint32_t, uint32_t)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
C::const_iterator begin() const
first collection iterator
Definition: RangeMap.h:132
bool exists(std::string const &parameterName) const
checks if a parameter exists
uint32_t getRawDetId(const T2 &)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
edm::InputTag srcHitMapMuPlus_
virtual void produce(edm::Event &, const edm::EventSetup &)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
edm::InputTag srcHitMapMuMinus_
MuonDetRecHitMixer(const edm::ParameterSet &)
void printHitMapRH(const edm::EventSetup &, const RecHitCollection &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
void printMuonDetId(const edm::EventSetup &, uint32_t)
tuple cout
Definition: gather_cfg.py:121
C::const_iterator end() const
last collection iterator
Definition: RangeMap.h:134
void addRecHits(std::map< T1, std::vector< T2 > > &, const RecHitCollection &, bool, const detIdToIntMap &, const detIdToIntMap &, int &)
moduleLabel_(iConfig.getParameter< string >("@module_label"))
std::string const & instance() const
Definition: InputTag.h:43