CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CaloRecHitMixer.h
Go to the documentation of this file.
1 
21 
24 
25 #include <string>
26 #include <iostream>
27 #include <map>
28 
29 template <typename T>
31 {
32  uint32_t rawDetId_;
33 
34  double energy1_;
35  bool isRecHit1_;
36  const T* recHit1_;
37 
38  double energy2_;
39  bool isRecHit2_;
40  const T* recHit2_;
41 
42  double energySum_;
44 };
45 
46 template <typename T>
48 {
49  public:
50  explicit CaloRecHitMixer(const edm::ParameterSet&);
52 
53  private:
54  virtual void produce(edm::Event&, const edm::EventSetup&);
55 
57 
59  {
67  };
68  std::vector<todoListEntryType> todoList_;
69 
71 
72  typedef std::map<uint32_t, CaloRecHitMixer_mixedRecHitInfoType<T> > detIdToMixedRecHitInfoMap;
74 
75  void updateRecHitInfos(const RecHitCollection&, int);
77 
80  enum { kAbsolute };
82 
83  typedef std::map<uint32_t, float> detIdToFloatMap;
84 
86 };
87 
88 template <typename T>
90  : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
91  srcEnergyDepositMapMuPlus_(cfg.getParameter<edm::InputTag>("srcEnergyDepositMapMuPlus")),
92  srcEnergyDepositMapMuMinus_(cfg.getParameter<edm::InputTag>("srcEnergyDepositMapMuMinus"))
93 {
94  edm::VParameterSet todoList = cfg.getParameter<edm::VParameterSet>("todo");
95  if ( todoList.size() == 0 ) {
96  throw cms::Exception("Configuration")
97  << "Empty to-do list !!\n";
98  }
99 
100  std::string typeEnergyDepositMap_string = cfg.getParameter<std::string>("typeEnergyDepositMap");
101  if ( typeEnergyDepositMap_string == "absolute" ) typeEnergyDepositMap_ = kAbsolute;
102  else throw cms::Exception("Configuration")
103  << "Invalid Configuration parameter 'typeEnergyDepositMap' = " << typeEnergyDepositMap_string << " !!\n";
104 
105  for ( edm::VParameterSet::const_iterator todoItem = todoList.begin();
106  todoItem != todoList.end(); ++todoItem ) {
107  todoListEntryType todoListEntry;
108  todoListEntry.srcRecHitCollection1_ = todoItem->getParameter<edm::InputTag>("collection1");
109  todoListEntry.killNegEnergyBeforeMixing1_ = todoItem->getParameter<bool>("killNegEnergyBeforeMixing1");
110  todoListEntry.srcRecHitCollection2_ = todoItem->getParameter<edm::InputTag>("collection2");
111  todoListEntry.killNegEnergyBeforeMixing2_ = todoItem->getParameter<bool>("killNegEnergyBeforeMixing2");
112  std::string muonEnSutractionMode_string = todoItem->getParameter<std::string>("muonEnSutractionMode");
113  if ( muonEnSutractionMode_string == "subtractFromCollection1BeforeMixing" ) todoListEntry.muonEnSutractionMode_ = todoListEntryType::kSubtractFromCollection1BeforeMixing;
114  else if ( muonEnSutractionMode_string == "subtractFromCollection2BeforeMixing" ) todoListEntry.muonEnSutractionMode_ = todoListEntryType::kSubtractFromCollection2BeforeMixing;
115  else if ( muonEnSutractionMode_string == "subtractAfterMixing" ) todoListEntry.muonEnSutractionMode_ = todoListEntryType::kSubtractAfterMixing;
116  else throw cms::Exception("Configuration")
117  << "Invalid Configuration parameter 'muonEnSutractionMode' = " << muonEnSutractionMode_string << " !!\n";
118  todoListEntry.killNegEnergyAfterMixing_ = todoItem->getParameter<bool>("killNegEnergyAfterMixing");
119 
120  std::string instanceLabel1 = todoListEntry.srcRecHitCollection1_.instance();
121  std::string instanceLabel2 = todoListEntry.srcRecHitCollection2_.instance();
122  if ( instanceLabel1 != instanceLabel2 ) {
123  throw cms::Exception("Configuration")
124  << "Mismatch in Instance labels for collection 1 = " << instanceLabel1 << " and 2 = " << instanceLabel2 << " !!\n";
125  }
126 
127  todoList_.push_back(todoListEntry);
128 
129  produces<RecHitCollection>(instanceLabel1);
130  std::string instanceLabel_removedEnergyMuMinus = "removedEnergyMuMinus";
131  if ( instanceLabel1 != "" ) instanceLabel_removedEnergyMuMinus.append("#").append(instanceLabel1);
132  produces<double>(instanceLabel_removedEnergyMuMinus.data());
133  std::string instanceLabel_removedEnergyMuPlus = "removedEnergyMuPlus";
134  if ( instanceLabel1 != "" ) instanceLabel_removedEnergyMuPlus.append("#").append(instanceLabel1);
135  produces<double>(instanceLabel_removedEnergyMuPlus.data());
136  }
137 
138  verbosity_ = ( cfg.exists("verbosity") ) ?
139  cfg.getParameter<int>("verbosity") : 0;
140 }
141 
142 template <typename T>
144 {
145 // nothing to be done yet...
146 }
147 
148 namespace
149 {
150  double getCorrection(uint32_t rawDetId, const std::map<uint32_t, float>& energyDepositMap)
151  {
152  double correction = 0.;
153  std::map<uint32_t, float>::const_iterator energyDepositEntry = energyDepositMap.find(rawDetId);
154  if ( energyDepositEntry != energyDepositMap.end() ) {
155  correction = energyDepositEntry->second;
156  }
157  return correction;
158  }
159 }
160 
161 template <typename T>
163 {
164  if ( verbosity_ ) std::cout << "<CaloRecHitMixer::produce (" << moduleLabel_ << ")>:" << std::endl;
165 
166  edm::Handle<detIdToFloatMap> energyDepositMapMuPlus;
167  evt.getByLabel(srcEnergyDepositMapMuPlus_, energyDepositMapMuPlus);
168  edm::Handle<detIdToFloatMap> energyDepositMapMuMinus;
169  evt.getByLabel(srcEnergyDepositMapMuMinus_, energyDepositMapMuMinus);
170 
171  for ( typename std::vector<todoListEntryType>::const_iterator todoItem = todoList_.begin();
172  todoItem != todoList_.end(); ++todoItem ) {
173  edm::Handle<RecHitCollection> recHitCollection1;
174  evt.getByLabel(todoItem->srcRecHitCollection1_, recHitCollection1);
175  edm::Handle<RecHitCollection> recHitCollection2;
176  evt.getByLabel(todoItem->srcRecHitCollection2_, recHitCollection2);
177  if ( verbosity_ ) {
178  std::cout << "recHitCollection(input1 = " << todoItem->srcRecHitCollection1_.label() << ":" << todoItem->srcRecHitCollection1_.instance() << ":" << todoItem->srcRecHitCollection1_.process() << "):"
179  << " #entries = " << recHitCollection1->size() << std::endl;
180  std::cout << "recHitCollection(input2 = " << todoItem->srcRecHitCollection2_.label() << ":" << todoItem->srcRecHitCollection2_.instance() << ":" << todoItem->srcRecHitCollection2_.process() << "):"
181  << " #entries = " << recHitCollection2->size() << std::endl;
182  }
183 
184  mixedRecHitInfos_.clear();
185  updateRecHitInfos(*recHitCollection1, 0);
186  updateRecHitInfos(*recHitCollection2, 1);
187 
188  std::auto_ptr<RecHitCollection> recHitCollection_output(new RecHitCollection());
189  std::auto_ptr<double> removedEnergyMuPlus(new double(0.));
190  std::auto_ptr<double> removedEnergyMuMinus(new double(0.));
191 
192  double muPlusEnergySum = 0.;
193  double muMinusEnergySum = 0.;
194 
195  for ( typename detIdToMixedRecHitInfoMap::iterator mixedRecHitInfo = mixedRecHitInfos_.begin();
196  mixedRecHitInfo != mixedRecHitInfos_.end(); ++mixedRecHitInfo ) {
197  uint32_t rawDetId = mixedRecHitInfo->second.rawDetId_;
198 
199  double muPlusEnergyDeposit = getCorrection(rawDetId, *energyDepositMapMuPlus);
200  double muMinusEnergyDeposit = getCorrection(rawDetId, *energyDepositMapMuMinus);
201  double muonEnergyDeposit = muPlusEnergyDeposit + muMinusEnergyDeposit;
202 
203  muPlusEnergySum += muPlusEnergyDeposit;
204  muMinusEnergySum += muMinusEnergyDeposit;
205 
206  if ( muonEnergyDeposit > 0. ) {
207  if ( verbosity_ ) std::cout << "removing muon energy: detId = " << rawDetId << ", subtracted = " << muonEnergyDeposit << std::endl;
208  if ( mixedRecHitInfo->second.isRecHit1_ && todoItem->muonEnSutractionMode_ == todoListEntryType::kSubtractFromCollection1BeforeMixing ) {
209  if ( todoItem->killNegEnergyBeforeMixing1_ && mixedRecHitInfo->second.energy1_ < muonEnergyDeposit ) {
210  if ( verbosity_ ) std::cout << "--> killing recHit1: detId = " << rawDetId << ", energy = " << (mixedRecHitInfo->second.energy1_ - muonEnergyDeposit) << std::endl;
211  (*removedEnergyMuPlus) += ((mixedRecHitInfo->second.energy1_/muonEnergyDeposit)*muPlusEnergyDeposit);
212  (*removedEnergyMuMinus) += ((mixedRecHitInfo->second.energy1_/muonEnergyDeposit)*muMinusEnergyDeposit);
213  mixedRecHitInfo->second.energy1_ = 0.;
214  mixedRecHitInfo->second.isRecHit1_ = false;
215  } else {
216  (*removedEnergyMuPlus) += muPlusEnergyDeposit;
217  (*removedEnergyMuMinus) += muMinusEnergyDeposit;
218  mixedRecHitInfo->second.energy1_ -= muonEnergyDeposit;
219  }
220  }
221  if ( mixedRecHitInfo->second.isRecHit2_ && todoItem->muonEnSutractionMode_ == todoListEntryType::kSubtractFromCollection2BeforeMixing ) {
222  if ( todoItem->killNegEnergyBeforeMixing2_ && mixedRecHitInfo->second.energy2_ < muonEnergyDeposit ) {
223  if ( verbosity_ ) std::cout << "--> killing recHit2: detId = " << rawDetId << ", energy = " << (mixedRecHitInfo->second.energy2_ - muonEnergyDeposit) << std::endl;
224  (*removedEnergyMuPlus) += ((mixedRecHitInfo->second.energy2_/muonEnergyDeposit)*muPlusEnergyDeposit);
225  (*removedEnergyMuMinus) += ((mixedRecHitInfo->second.energy2_/muonEnergyDeposit)*muMinusEnergyDeposit);
226  mixedRecHitInfo->second.energy2_ = 0.;
227  mixedRecHitInfo->second.isRecHit2_ = false;
228  } else {
229  (*removedEnergyMuPlus) += muPlusEnergyDeposit;
230  (*removedEnergyMuMinus) += muMinusEnergyDeposit;
231  mixedRecHitInfo->second.energy2_ -= muonEnergyDeposit;
232  }
233  }
234  }
235 
236  mixedRecHitInfo->second.energySum_ = 0.;
237  mixedRecHitInfo->second.isRecHitSum_ = false;
238  if ( mixedRecHitInfo->second.isRecHit1_ ) {
239  mixedRecHitInfo->second.energySum_ += mixedRecHitInfo->second.energy1_;
240  mixedRecHitInfo->second.isRecHitSum_ = true;
241  }
242  if ( mixedRecHitInfo->second.isRecHit2_ ) {
243  mixedRecHitInfo->second.energySum_ += mixedRecHitInfo->second.energy2_;
244  mixedRecHitInfo->second.isRecHitSum_ = true;
245  }
246  if ( muonEnergyDeposit > 0. ) {
247  if ( mixedRecHitInfo->second.isRecHitSum_ && todoItem->muonEnSutractionMode_ == todoListEntryType::kSubtractAfterMixing ) {
248  if ( todoItem->killNegEnergyAfterMixing_ && mixedRecHitInfo->second.energySum_ < muonEnergyDeposit ) {
249  if ( verbosity_ ) std::cout << "--> killing recHitSum: detId = " << rawDetId << ", energy = " << (mixedRecHitInfo->second.energySum_ - muonEnergyDeposit) << std::endl;
250  (*removedEnergyMuPlus) += ((mixedRecHitInfo->second.energySum_/muonEnergyDeposit)*muPlusEnergyDeposit);
251  (*removedEnergyMuMinus) += ((mixedRecHitInfo->second.energySum_/muonEnergyDeposit)*muMinusEnergyDeposit);
252  mixedRecHitInfo->second.energySum_ = 0.;
253  mixedRecHitInfo->second.isRecHitSum_ = false;
254  } else {
255  (*removedEnergyMuPlus) += muPlusEnergyDeposit;
256  (*removedEnergyMuMinus) += muMinusEnergyDeposit;
257  mixedRecHitInfo->second.energySum_ -= muonEnergyDeposit;
258  }
259  }
260  }
261 
262  if ( mixedRecHitInfo->second.isRecHitSum_ ) {
263  if ( verbosity_ ) {
264  if ( muonEnergyDeposit > 1.e-3 ) std::cout << "--> adding recHitSum (cleaned): detId = " << rawDetId << ", energy = " << mixedRecHitInfo->second.energySum_ << std::endl;
265  else std::cout << "--> adding recHitSum (uncleaned): detId = " << rawDetId << ", energy = " << mixedRecHitInfo->second.energySum_ << std::endl;
266  }
267  recHitCollection_output->push_back(buildRecHit(mixedRecHitInfo->second));
268  }
269  }
270 
271  if ( verbosity_ ) {
272  std::cout << " mu+: sum(EnergyDeposits) = " << muPlusEnergySum << " (removed = " << (*removedEnergyMuPlus) << ")" << std::endl;
273  std::cout << " mu-: sum(EnergyDeposits) = " << muMinusEnergySum << " (removed = " << (*removedEnergyMuMinus) << ")" << std::endl;
274  std::cout << "recHitCollection(output = " << moduleLabel_ << ":" << todoItem->srcRecHitCollection1_.instance() << "): #entries = " << recHitCollection_output->size() << std::endl;
275  }
276 
277  std::string instanceLabel = todoItem->srcRecHitCollection1_.instance();
278  evt.put(recHitCollection_output, instanceLabel);
279  std::string instanceLabel_removedEnergyMuMinus = "removedEnergyMuMinus";
280  if ( instanceLabel != "" ) instanceLabel_removedEnergyMuMinus.append("#").append(instanceLabel);
281  evt.put(removedEnergyMuMinus, instanceLabel_removedEnergyMuMinus.data());
282  std::string instanceLabel_removedEnergyMuPlus = "removedEnergyMuPlus";
283  if ( instanceLabel != "" ) instanceLabel_removedEnergyMuPlus.append("#").append(instanceLabel);
284  evt.put(removedEnergyMuPlus, instanceLabel_removedEnergyMuPlus.data());
285  }
286 }
287 
288 template <typename T>
290 {
291  for ( typename RecHitCollection::const_iterator recHit = recHitCollection.begin();
292  recHit != recHitCollection.end(); ++recHit ) {
293  uint32_t rawDetId = recHit->detid().rawId();
294 
295  bool isNewRecHit = (mixedRecHitInfos_.find(rawDetId) == mixedRecHitInfos_.end());
296  if ( isNewRecHit ) {
298  mixedRecHitInfo.rawDetId_ = rawDetId;
299  mixedRecHitInfo.energy1_ = 0.;
300  mixedRecHitInfo.isRecHit1_ = false;
301  mixedRecHitInfo.recHit1_ = 0;
302  mixedRecHitInfo.energy2_ = 0.;
303  mixedRecHitInfo.isRecHit2_ = false;
304  mixedRecHitInfo.recHit2_ = 0;
305  mixedRecHitInfo.energySum_ = 0.;
306  mixedRecHitInfo.isRecHitSum_ = false;
307  mixedRecHitInfos_.insert(std::pair<uint32_t, CaloRecHitMixer_mixedRecHitInfoType<T> >(rawDetId, mixedRecHitInfo));
308  }
309 
310  typename detIdToMixedRecHitInfoMap::iterator mixedRecHitInfo = mixedRecHitInfos_.find(rawDetId);
311  assert(mixedRecHitInfo != mixedRecHitInfos_.end());
312 
313  if ( verbosity_ ) {
314  if ( isNewRecHit ) std::cout << "creating new recHit: detId = " << rawDetId << ", energy = " << recHit->energy() << std::endl;
315  else std::cout << "merging recHits: detId = " << rawDetId << ", total energy = " << (mixedRecHitInfo->second.energy1_ + mixedRecHitInfo->second.energy2_)
316  << " (added = " << recHit->energy() << ")" << std::endl;
317  }
318 
319  if ( idx == 0 ) {
320  mixedRecHitInfo->second.energy1_ = recHit->energy();
321  mixedRecHitInfo->second.isRecHit1_ = true;
322  mixedRecHitInfo->second.recHit1_ = &(*recHit);
323  } else if ( idx == 1 ) {
324  mixedRecHitInfo->second.energy2_ = recHit->energy();
325  mixedRecHitInfo->second.isRecHit2_ = true;
326  mixedRecHitInfo->second.recHit2_ = &(*recHit);
327  } else assert(0);
328  }
329 }
T getParameter(std::string const &) const
T buildRecHit(const CaloRecHitMixer_mixedRecHitInfoType< T > &)
tuple cfg
Definition: looper.py:237
CaloRecHitMixer(const edm::ParameterSet &)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
detIdToMixedRecHitInfoMap mixedRecHitInfos_
std::vector< T >::const_iterator const_iterator
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::InputTag srcEnergyDepositMapMuPlus_
std::map< uint32_t, float > detIdToFloatMap
edm::InputTag srcEnergyDepositMapMuMinus_
virtual void produce(edm::Event &, const edm::EventSetup &)
void updateRecHitInfos(const RecHitCollection &, int)
std::vector< todoListEntryType > todoList_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
edm::SortedCollection< T > RecHitCollection
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
const_iterator end() const
std::string moduleLabel_
std::map< uint32_t, CaloRecHitMixer_mixedRecHitInfoType< T > > detIdToMixedRecHitInfoMap
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
tuple cout
Definition: gather_cfg.py:121
moduleLabel_(iConfig.getParameter< string >("@module_label"))
long double T
std::string const & instance() const
Definition: InputTag.h:43
const_iterator begin() const