CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CaloTowersMerger.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CaloTowersMerger
4 // Class: CaloTowersMerger
5 //
13 //
14 // Original Author: Jean-Roch Vlimant,40 3-A28,+41227671209,
15 // Created: Thu Nov 4 16:36:30 CET 2010
16 //
17 //
18 
19 // Anton Anastassov (Northwestern):
20 // Add code for actual tower merging, define two distinct inputs of
21 // "regular" and "extra" towers
22 // This functionality is subject to some restrictions
23 // (see notes below)
24 
25 
26 
27 // system include files
28 #include <memory>
29 
30 // user include files
33 
36 
38 
41 
42 //
43 // class declaration
44 //
45 
47  public:
48  explicit CaloTowersMerger(const edm::ParameterSet&);
50 
51  CaloTower mergedTower(const CaloTower& t1, const CaloTower& t2);
52 
53  private:
54  virtual void beginJob() override ;
55  virtual void produce(edm::Event&, const edm::EventSetup&) override;
56  virtual void endJob() override ;
57 
58  // ----------member data ---------------------------
59 
63 };
64 
65 //
66 // constants, enums and typedefs
67 //
68 
69 
70 //
71 // static data member definitions
72 //
73 
74 //
75 // constructors and destructor
76 //
78 {
79  regularTowerTag=iConfig.getParameter<edm::InputTag>("regularTowerTag");
80  extraTowerTag=iConfig.getParameter<edm::InputTag>("extraTowerTag");
81 
82  // register for data access
83  tok_reg_ = consumes<CaloTowerCollection>(regularTowerTag);
84  tok_ext_ = consumes<CaloTowerCollection>(extraTowerTag);
85 
86  //register your products
87  produces<CaloTowerCollection>();
88 }
89 
90 
92 {
93 
94  // do anything here that needs to be done at desctruction time
95  // (e.g. close files, deallocate resources etc.)
96 
97 }
98 
99 
100 //
101 // member functions
102 //
103 
104 // ------------ method called to produce the data ------------
105 void
107 {
108  edm::Handle<CaloTowerCollection> regTower,extraTower;
109 
110  iEvent.getByToken(tok_reg_,regTower);
111  iEvent.getByToken(tok_ext_,extraTower);
112 
113  std::auto_ptr<CaloTowerCollection> output;
114 
115  if (!regTower.isValid() && !extraTower.isValid()){
116  edm::LogError("CaloTowersMerger")<<"both input tag:"<<regularTowerTag<<" and "<<extraTowerTag<<" are invalid. empty merged collection";
117  output.reset(new CaloTowerCollection());
118  iEvent.put(output);
119  return;
120  }else if (!regTower.isValid() || !extraTower.isValid()){
121  if (!regTower.isValid() && extraTower.isValid())
122  regTower=extraTower;
123  output.reset(new CaloTowerCollection(*regTower));
124  iEvent.put(output);
125  return;
126  }
127  else{
128  //both valid input collections: merging
129  output.reset(new CaloTowerCollection());
130  output->reserve(regTower->size()+extraTower->size());
131 
132  CaloTowerCollection::const_iterator rt_begin = regTower->begin();
133  CaloTowerCollection::const_iterator rt_end = regTower->end();
134  CaloTowerCollection::const_iterator rt_it = rt_begin;
135 
136  //vector of overlapping towers
137  std::vector<CaloTowerCollection::const_iterator> overlappingTowers;
138  overlappingTowers.reserve(extraTower->size());
139 
140  for (;rt_it!=rt_end;++rt_it){
141  CaloTowerCollection::const_iterator et_it = extraTower->find(rt_it->id());
142  if (et_it != extraTower->end()){
143  //need to merge the components
144  //FIXME
145 
147  //one needs to merge t1 and t2 into mergedTower
148  //end FIXME
149  CaloTower mt = mergedTower(*rt_it, *et_it);
150 
151  output->push_back(mt);
152  overlappingTowers.push_back(et_it);
153 
154  }else{
155  //just copy the regular tower over
156  output->push_back(*rt_it);
157  }
158  }
159  CaloTowerCollection::const_iterator et_begin = extraTower->begin();
160  CaloTowerCollection::const_iterator et_end = extraTower->end();
162  for (;et_it!=et_end;++et_it){
163  if (std::find(overlappingTowers.begin(),overlappingTowers.end(),et_it)==overlappingTowers.end())
164  //non overlapping tower
165  //copy the extra tower over
166  output->push_back(*et_it);
167  }
168  iEvent.put(output);
169  }
170 
171 }
172 
173 // ------------ method called once each job just before starting event loop ------------
174 void
176 {
177 }
178 
179 // ------------ method called once each job just after ending the event loop ------------
180 void
182 }
183 
184 
185 
186 
187 
188 // Make a new tower by merging two towers containing exclusive hits
189 // This cannot be done fully consistently and independent of the
190 // creation mechnaism...
191 // This functionlaity it to be used only for testing the effects
192 // of rejected bad hits.
193 
195 
196  double newOuterE = 0;
197 
198  // HO energies are always saved (even if useHO=false)
199  // make sure there is no double counting
200  // crude test if one has HO energy and the other not (possibly due to bad hit cleanup)
201  // However: for |iEta|>15 E_outer has different meening:
202  // it holds either the energy in the outer depths in HCAL, etc
203 
204  if (rt.ietaAbs()<16 && (fabs(rt.outerEnergy()-et.outerEnergy())<0.00001 ) ) {
205  // there is no differnce in the store HO energies
206  newOuterE = rt.outerEnergy();
207  }
208  else {
209  newOuterE = rt.outerEnergy()+et.outerEnergy();
210  }
211 
212 
213  bool rt_hasEcalConstit = false;
214  bool et_hasEcalConstit = false;
215 
216  bool rt_hasHcalConstit = false;
217  bool et_hasHcalConstit = false;
218 
219 
220  // check if there are HCAL/ECAL constituents in the towers
221 
222  std::vector<DetId>::const_iterator rc_begin=rt.constituents().begin();
223  std::vector<DetId>::const_iterator rc_end=rt.constituents().end();
224  std::vector<DetId>::const_iterator rc_it;
225 
226 
227  for (rc_it=rc_begin; rc_it!=rc_end; ++rc_it) {
228  if (rc_it->det()==DetId::Hcal) rt_hasHcalConstit=true;
229  break;
230  }
231  for (rc_it=rc_begin; rc_it!=rc_end; ++rc_it) {
232  if (rc_it->det()==DetId::Ecal) rt_hasEcalConstit=true;
233  break;
234  }
235 
236  std::vector<DetId>::const_iterator ec_begin=et.constituents().begin();
237  std::vector<DetId>::const_iterator ec_end=et.constituents().end();
238  std::vector<DetId>::const_iterator ec_it;
239 
240  for (ec_it=ec_begin; ec_it!=ec_end; ++ec_it) {
241  if (ec_it->det()==DetId::Hcal) et_hasHcalConstit=true;
242  break;
243  }
244  for (ec_it=ec_begin; ec_it!=ec_end; ++ec_it) {
245  if (ec_it->det()==DetId::Ecal) et_hasEcalConstit=true;
246  break;
247  }
248 
249 
250  std::vector<DetId> combinedConstituents = rt.constituents();
251  for (ec_it=ec_begin; ec_it!=ec_end; ++ec_it) {
252  // if properly resconstructed, the only possible overlap should be for HO hits that
253  // are always listed as constituents if above thereshold (old JetMET request)
254  if (std::find(combinedConstituents.begin(),combinedConstituents.end(), *ec_it)==combinedConstituents.end())
255  combinedConstituents.push_back(*ec_it);
256  }
257 
258 
259 
260  GlobalPoint newEmPosition(0.0, 0.0, 0.0);
261 
262  // The following assumes the current default
263  // momentum reconstruction method (1) and
264  // accepted rechits with some threshod >0
265 
266 
267  if (rt_hasEcalConstit && et_hasEcalConstit) {
268 
269  if (rt.emEnergy()>0 && et.emEnergy()>0) {
270  double sumEmE = rt.emEnergy()+ et.emEnergy();
271 
272  double x = rt.emEnergy()*rt.emPosition().x() + et.emEnergy()*et.emPosition().x();
273  double y = rt.emEnergy()*rt.emPosition().y() + et.emEnergy()*et.emPosition().y();
274  double z = rt.emEnergy()*rt.emPosition().z() + et.emEnergy()*et.emPosition().z();
275 
276  GlobalPoint weightedEmdPosition(x/sumEmE,y/sumEmE,z/sumEmE);
277  newEmPosition = weightedEmdPosition;
278  }
279 
280 
281  }
282  else if (rt_hasEcalConstit && !et_hasEcalConstit) {
283  newEmPosition = rt.emPosition();
284  }
285  else if (!rt_hasEcalConstit && et_hasEcalConstit) {
286  newEmPosition = et.emPosition();
287  }
288 
289 
290  GlobalPoint newHadPosition(0.0, 0.0, 0.0);
291  // had positions are the same if there is at least one constituent
292  if (rt_hasHcalConstit) {
293  newHadPosition = rt.hadPosition();
294  }
295  else if (et_hasHcalConstit) {
296  newHadPosition = et.hadPosition();
297  }
298 
299 
300  // MAke the new tower and set all values
301 
302  CaloTower mergedTower(rt.id(), rt.emEnergy()+et.emEnergy(), rt.hadEnergy()+et.hadEnergy(), newOuterE, -1, -1,
303  rt.p4()+et.p4(), newEmPosition, newHadPosition);
304 
305  mergedTower.addConstituents(combinedConstituents);
306 
307  (rt.hottestCellE() > et.hottestCellE())?
310 
311  unsigned int numBadHcalChan = rt.numBadHcalCells() - et.numProblematicHcalCells() - rt.numRecoveredHcalCells();
312  unsigned int numBadEcalChan = rt.numBadEcalCells() - et.numProblematicEcalCells() - rt.numRecoveredEcalCells();
313 
314  unsigned int numProbHcalChan = rt.numProblematicHcalCells() + et.numProblematicHcalCells();
315  unsigned int numProbEcalChan = rt.numProblematicEcalCells() + et.numProblematicEcalCells();
316 
317  unsigned int numRecHcalChan = rt.numRecoveredHcalCells() + et.numRecoveredHcalCells();
318  unsigned int numRecEcalChan = rt.numRecoveredEcalCells() + et.numRecoveredEcalCells();
319 
320  mergedTower.setCaloTowerStatus(numBadHcalChan, numBadEcalChan,
321  numRecHcalChan, numRecEcalChan,
322  numProbHcalChan, numProbEcalChan);
323 
324  // use timing from the good tower only (for now, in default reco we use information from good hits only)
325  // time is saved as integer but returned as float in (ns)
326  mergedTower.setEcalTime( int(rt.ecalTime()*100.0 + 0.5) );
327  mergedTower.setHcalTime( int(rt.hcalTime()*100.0 + 0.5) );
328 
329 
330  return mergedTower;
331 
332 }
333 
334 
335 
336 //define this as a plug-in
edm::EDGetTokenT< CaloTowerCollection > tok_reg_
unsigned int numRecoveredEcalCells() const
Definition: CaloTower.h:181
T getParameter(std::string const &) const
CaloTower mergedTower(const CaloTower &t1, const CaloTower &t2)
math::PtEtaPhiMLorentzVector p4(double vtxZ) const
Definition: CaloTower.cc:129
CaloTowersMerger(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
virtual void endJob() override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setCaloTowerStatus(unsigned int numBadHcalChan, unsigned int numBadEcalChan, unsigned int numRecHcalChan, unsigned int numRecEcalChan, unsigned int numProbHcalChan, unsigned int numProbEcalChan)
Definition: CaloTower.cc:199
float ecalTime() const
Definition: CaloTower.h:165
edm::EDGetTokenT< CaloTowerCollection > tok_ext_
std::vector< CaloTower >::const_iterator const_iterator
T y() const
Definition: PV3DBase.h:63
edm::InputTag extraTowerTag
unsigned int numRecoveredHcalCells() const
Definition: CaloTower.h:185
unsigned int numBadHcalCells() const
Definition: CaloTower.h:184
double hottestCellE() const
Definition: CaloTower.h:132
virtual void beginJob() override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
const GlobalPoint & emPosition() const
Definition: CaloTower.h:144
float float float z
void setHottestCellE(double e)
Definition: CaloTower.h:81
unsigned int numBadEcalCells() const
Definition: CaloTower.h:180
void addConstituents(const std::vector< DetId > &ids)
Definition: CaloTower.cc:177
int iEvent
Definition: GenABIO.cc:230
double emEnergy() const
Definition: CaloTower.h:94
const GlobalPoint & hadPosition() const
Definition: CaloTower.h:145
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
T z() const
Definition: PV3DBase.h:64
const std::vector< DetId > & constituents() const
Definition: CaloTower.h:88
bool isValid() const
Definition: HandleBase.h:75
double hadEnergy() const
Definition: CaloTower.h:95
virtual void produce(edm::Event &, const edm::EventSetup &) override
CaloTowerDetId id() const
Definition: CaloTower.h:87
unsigned int numProblematicEcalCells() const
Definition: CaloTower.h:182
edm::InputTag regularTowerTag
float hcalTime() const
Definition: CaloTower.h:166
unsigned int numProblematicHcalCells() const
Definition: CaloTower.h:186
int ietaAbs() const
Definition: CaloTower.h:170
void setEcalTime(int t)
Definition: CaloTower.h:68
edm::SortedCollection< CaloTower > CaloTowerCollection
Definition: CaloTowerFwd.h:15
Definition: DDAxes.h:10
void setHcalTime(int t)
Definition: CaloTower.h:69
T x() const
Definition: PV3DBase.h:62
double outerEnergy() const
Definition: CaloTower.h:96