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