CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EnergyScaleAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EnergyScaleAnalyzer
4 // Class: EnergyScaleAnalyzer
5 //
13 // Original Author: Keti Kaadze
14 // Created: Thu Jun 21 08:59:42 CDT 2007
15 //
16 
17 //#include "RecoEcal/EnergyScaleAnalyzer/interface/EnergyScaleAnalyzer.h"
20 
21 //Framework
32 
33 //Geometry
40 
41 #include "TFile.h"
42 //Reconstruction classes
51 
55 
63 
64 #include <iostream>
65 #include <fstream>
66 #include <iomanip>
67 #include <ios>
68 #include <map>
69 #include "TString.h"
71 
72 //========================================================================
74 //========================================================================
75 {
76 
77  hepMCLabel_ = ps.getParameter<std::string>("hepMCLabel");
78 
79  outputFile_ = ps.getParameter<std::string>("outputFile");
80  rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE"); // open output file to store histograms
81 
82  evtN = 0;
83 }
84 
85 
86 //========================================================================
88 //========================================================================
89 {
90  delete rootFile_;
91 }
92 
93 //========================================================================
94 void
96 //========================================================================
97 
98  mytree_ = new TTree("energyScale","");
99  TString treeVariables = "mc_npar/I:parID:mc_sep/F:mc_e:mc_et:mc_phi:mc_eta:mc_theta:"; // MC information
100  treeVariables += "em_dR/F:"; // MC <-> EM matching information
101  treeVariables += "em_isInCrack/I:em_scType:em_e/F:em_et:em_phi:em_eta:em_theta:em_nCell/I:em_nBC:"; // EM SC info
102  treeVariables += "em_pet/F:em_pe:em_peta:em_ptheta:" ; // EM SC physics (eta corrected information)
103 
104  treeVariables += "emCorr_e/F:emCorr_et:emCorr_eta:emCorr_phi:emCorr_theta:";// CMSSW standard corrections
105  treeVariables += "emCorr_pet/F:emCorr_peta:emCorr_ptheta:" ;// CMSSW standard physics
106 
107  treeVariables += "em_pw/F:em_ew:em_br" ; // EM widths pw -- phiWidth, ew -- etaWidth, ratios of pw/ew
108 
109  mytree_->Branch("energyScale",&(tree_.mc_npar),treeVariables);
110 
111 }
112 
113 //========================================================================
114 void
116  using namespace edm; // needed for all fwk related classes
117  using namespace std;
118 
119  // std::cout << "Proceccing event # " << ++evtN << std::endl;
120 
121  //Get containers for MC truth, SC etc. ===================================================
122  // =======================================================================================
123  // =======================================================================================
124  Handle<HepMCProduct> hepMC;
125  evt.getByLabel( hepMCLabel_, hepMC ) ;
126 
127  const HepMC::GenEvent* genEvent = hepMC->GetEvent();
128  if ( !(hepMC.isValid())) {
129  LogInfo("EnergyScaleAnalyzer") << "Could not get MC Product!";
130  return;
131  }
132 
133 
134  //=======================For Vertex correction
135  std::vector< Handle< HepMCProduct > > evtHandles ;
136  evt.getManyByType( evtHandles ) ;
137 
138  for ( unsigned int i=0; i<evtHandles.size(); ++i) {
139  if ( evtHandles[i].isValid() ) {
140  const HepMC::GenEvent* evt = evtHandles[i]->GetEvent() ;
141 
142  // take only 1st vertex for now - it's been tested only of PGuns...
143  //
144  HepMC::GenEvent::vertex_const_iterator vtx = evt->vertices_begin() ;
145  if ( evtHandles[i].provenance()->moduleLabel() == hepMCLabel_ ) {
146  //Corrdinates of Vertex w.r.o. the point (0,0,0)
147  xVtx_ = 0.1*(*vtx)->position().x();
148  yVtx_ = 0.1*(*vtx)->position().y();
149  zVtx_ = 0.1*(*vtx)->position().z();
150  }
151  }
152  }
153  //==============================================================================
154  //Get handle to SC collections
155 
157  try {
158  evt.getByLabel("hybridSuperClusters","",hybridSuperClusters);
159  }catch (cms::Exception& ex) {
160  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer hybridSuperClusters.";
161  }
162 
164  try {
165  evt.getByLabel("dynamicHybridSuperClusters","",dynamicHybridSuperClusters);
166  }catch (cms::Exception& ex) {
167  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer dynamicHybridSuperClusters.";
168  }
169 
170  Handle<reco::SuperClusterCollection> fixedMatrixSuperClustersWithPS;
171  try {
172  evt.getByLabel("fixedMatrixSuperClustersWithPreshower","",fixedMatrixSuperClustersWithPS);
173  }catch (cms::Exception& ex) {
174  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer fixedMatrixSuperClustersWithPreshower.";
175  }
176 
177  //Corrected collections
178  Handle<reco::SuperClusterCollection> correctedHybridSC;
179  try {
180  evt.getByLabel("correctedHybridSuperClusters","",correctedHybridSC);
181  }catch (cms::Exception& ex) {
182  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer correctedHybridSuperClusters.";
183  }
184 
185  Handle<reco::SuperClusterCollection> correctedDynamicHybridSC;
186  try{
187  evt.getByLabel("correctedDynamicHybridSuperClusters","",correctedDynamicHybridSC);
188  }catch (cms::Exception& ex) {
189  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer correctedDynamicHybridSuperClusters.";
190  }
191 
192  Handle<reco::SuperClusterCollection> correctedFixedMatrixSCWithPS;
193  try {
194  evt.getByLabel("correctedFixedMatrixSuperClustersWithPreshower","",correctedFixedMatrixSCWithPS);
195  }catch (cms::Exception& ex ) {
196  edm::LogError("EnergyScaleAnalyzer") << "Can't get collection with producer correctedFixedMatrixSuperClustersWithPreshower.";
197  }
198 
200  const reco::SuperClusterCollection* hSC = hybridSuperClusters.product();
201  const reco::SuperClusterCollection* fmSC = fixedMatrixSuperClustersWithPS.product();
202  const reco::SuperClusterCollection* chSC = correctedHybridSC.product();
203  const reco::SuperClusterCollection* cdSC = correctedDynamicHybridSC.product();
204  const reco::SuperClusterCollection* cfmSC = correctedFixedMatrixSCWithPS.product();
205 
206  // ----------------------- Print outs for debugging
207  /*
208  std::cout << "MC truth" << std::endl;
209  int counterI = 0;
210  for( HepMC::GenEvent::particle_const_iterator p = genEvent->particles_begin();
211  p != genEvent->particles_end(); ++p ) {
212  if ( fabs((*p)->momentum().eta()) < 1.5 ) {
213  std::cout << ++counterI << " " << (*p)->momentum().e() << " "
214  << (*p)->momentum().phi() << " " << (*p)->momentum().eta() << std::endl;
215  }
216  }
217 
218  std::cout << "Standard clusters" << std::endl;
219  counterI = 0;
220  for(reco::SuperClusterCollection::const_iterator em = hSC->begin();
221  em != hSC->end(); ++em )
222  std::cout << ++counterI << " " << em->energy() << " " << em->position().phi() << " " << em->position().eta() << std::endl;
223 
224  std::cout << "Dynamic clusters" << std::endl;
225  counterI = 0;
226  for(reco::SuperClusterCollection::const_iterator em = dSC->begin();
227  em != dSC->end(); ++em )
228  std::cout << ++counterI << " " << em->energy() << " " << em->position().phi() << " " << em->position().eta() << std::endl;
229 
230  std::cout << "FixedMatrix clusters with PS" << std::endl;
231  counterI = 0;
232  for(reco::SuperClusterCollection::const_iterator em = fmSC->begin();
233  em != fmSC->end(); ++em )
234  std::cout << ++counterI << " " << em->energy() << " " << em->position().phi() << " " << em->position().eta() << std::endl;
235  */
236  // -----------------------------
237  //=====================================================================
238  // All containers are loaded, perform the analysis
239  //====================================================================
240 
241  // --------------------------- Store MC particles
242  HepMC::GenEvent::particle_const_iterator p = genEvent->particles_begin();
243 
244  // Search for MC electrons or photons that satisfy the criteria
245  float min_eT = 5;
246  float max_eta = 2.5;
247 
248  std::vector<HepMC::GenParticle* > mcParticles;
249  //int counter = 0;
250  for ( HepMC::GenEvent::particle_const_iterator p = genEvent->particles_begin();
251  p != genEvent->particles_end();
252  ++p ) {
253  //LogInfo("EnergyScaleAnalyzer") << "Particle " << ++counter
254  //<< " PDG ID = " << (*p)->pdg_id() << " pT = " << (*p)->momentum().perp();
255  // require photon or electron
256  if ( (*p)->pdg_id() != 22 && abs((*p)->pdg_id()) != 11 ) continue;
257 
258  // require selection criteria
259  bool satisfySelectionCriteria =
260  (*p)->momentum().perp() > min_eT &&
261  fabs((*p)->momentum().eta()) < max_eta;
262 
263  if ( ! satisfySelectionCriteria ) continue;
264 
265  // EM MC particle is found, save it in the vector
266  mcParticles.push_back(*p);
267  }
268  // separation in dR between 2 first MC particles
269  // should not be used for MC samples with > 2 em objects generated!
270  if ( mcParticles.size() == 2 ) {
271  HepMC::GenParticle* mc1 = mcParticles[0];
272  HepMC::GenParticle* mc2 = mcParticles[1];
273  tree_.mc_sep = kinem::delta_R(mc1->momentum().eta(), mc1->momentum().phi(),
274  mc2->momentum().eta(), mc2->momentum().phi());
275  } else
276  tree_.mc_sep = -100;
277 
278 
279  // now loop over MC particles, find the match with SC and do everything we need
280  // then save info in the tree for every MC particle
281  for(std::vector<HepMC::GenParticle* >::const_iterator p = mcParticles.begin();
282  p != mcParticles.end(); ++p) {
283  HepMC::GenParticle* mc = *p;
284 
285  // Fill MC information
286  tree_.mc_npar = mcParticles.size();
287  tree_.parID = mc->pdg_id();
288  tree_.mc_e = mc->momentum().e();
289  tree_.mc_et = mc->momentum().e()*sin(mc->momentum().theta());
290  tree_.mc_phi = mc->momentum().phi();
291  tree_.mc_eta = mc->momentum().eta();
292  tree_.mc_theta = mc->momentum().theta();
293 
294 
295  //Call function to fill tree
296  // scType coprreponds:
297  // HybridSuperCluster -- 1
298  // DynamicHybridSuperCluster -- 2
299  // FixedMatrixSuperClustersWithPreshower -- 3
300 
301  fillTree( hSC, chSC, mc, tree_, xVtx_, yVtx_, zVtx_, 1);
302  // std::cout << " TYPE " << 1 << " : " << tree_.em_e << " : " << tree_.em_phi << " : " << tree_.em_eta << std::endl;
303 
304  fillTree( dSC, cdSC, mc, tree_, xVtx_, yVtx_, zVtx_, 2);
305  // std::cout << " TYPE " << 2 << " : " << tree_.em_e << " : " << tree_.em_phi << " : " << tree_.em_eta << std::endl;
306 
307  fillTree( fmSC, cfmSC, mc, tree_, xVtx_, yVtx_, zVtx_, 3);
308  // std::cout << " TYPE " << 3 << " : " << tree_.em_e << " : " << tree_.em_phi << " : " << tree_.em_eta << std::endl;
309 
310  // mytree_->Fill();
311  } // loop over particles
312 }
313 
314 
316  HepMC::GenParticle* mc, tree_structure_& tree_, float xV, float yV, float zV, int scType) {
317 
318  // ----------------------------- SuperClusters before energy correction
319  reco::SuperClusterCollection::const_iterator em = scColl->end();
320  float energyMax = -100.0; // dummy energy of the matched SC
321  for(reco::SuperClusterCollection::const_iterator aClus = scColl->begin();
322  aClus != scColl->end(); ++aClus) {
323  // check the matching
324  float dR = kinem::delta_R(mc ->momentum().eta(), mc ->momentum().phi(),
325  aClus->position().eta(), aClus->position().phi());
326  if (dR < 0.4) { // a rather loose matching cut
327  float energy = aClus->energy();
328  if ( energy < energyMax ) continue;
329  energyMax = energy;
330  em = aClus;
331  }
332  }
333 
334  if (em == scColl->end() ) {
335  // std::cout << "No matching SC with type " << scType << " was found for MC particle! " << std::endl;
336  // std::cout << "Going to next type of SC. " << std::endl;
337  return;
338  }
339  // ------------
340 
341  tree_.em_scType = scType;
342 
343  tree_.em_isInCrack = 0;
344  double emAbsEta = fabs(em->position().eta());
345  // copied from RecoEgama/EgammaElectronAlgos/src/EgammaElectronClassification.cc
346  if ( emAbsEta < 0.018 ||
347  (emAbsEta > 0.423 && emAbsEta < 0.461) ||
348  (emAbsEta > 0.770 && emAbsEta < 0.806) ||
349  (emAbsEta > 1.127 && emAbsEta < 1.163) ||
350  (emAbsEta > 1.460 && emAbsEta < 1.558) )
351  tree_.em_isInCrack = 1;
352 
353  tree_.em_dR = kinem::delta_R(mc->momentum().eta(), mc->momentum().phi(),
354  em->position().eta(), em->position().phi());
355  tree_.em_e = em->energy();
356  tree_.em_et = em->energy() * sin(em->position().theta());
357  tree_.em_phi = em->position().phi();
358  tree_.em_eta = em->position().eta();
359  tree_.em_theta = em->position().theta();
360  tree_.em_nCell = em->size();
361  tree_.em_nBC = em->clustersSize();
362 
363  //Get physics e, et etc:
364  //Coordinates of EM object with respect of the point (0,0,0)
365  xClust_zero_ = em->position().x();
366  yClust_zero_ = em->position().y();
367  zClust_zero_ = em->position().z();
368  //Coordinates of EM object w.r.o. the Vertex position
369  xClust_vtx_ = xClust_zero_ - xV;
370  yClust_vtx_ = yClust_zero_ - yV;
371  zClust_vtx_ = zClust_zero_ - zV;
372 
373  energyMax_ = em->energy();
374  thetaMax_ = em->position().theta();
375  etaMax_ = em->position().eta();
376  phiMax_ = em->position().phi();
378  if ( phiMax_ < 0) phiMax_ += 2*3.14159;
379 
381  thetaMaxVtx_ = acos(zClust_vtx_/rClust_vtx_);
382  etaMaxVtx_ = - log(tan(thetaMaxVtx_/2));
385  if ( phiMaxVtx_ < 0) phiMaxVtx_ += 2*3.14159;
386  //=============================
387  //parametres of EM object after vertex correction
388  tree_.em_pet = eTMaxVtx_;
389  tree_.em_pe = tree_.em_pet/sin(thetaMaxVtx_);
390  tree_.em_peta = etaMaxVtx_;
391  tree_.em_ptheta = thetaMaxVtx_;
392 
393 
394  //------------------------------- Get SC after energy correction
395  em = corrSCColl->end();
396  energyMax = -100.0; // dummy energy of the matched SC
397  for(reco::SuperClusterCollection::const_iterator aClus = corrSCColl->begin();
398  aClus != corrSCColl->end(); ++aClus) {
399  // check the matching
400  float dR = kinem::delta_R(mc ->momentum().eta(), mc ->momentum().phi(),
401  aClus->position().eta(), aClus->position().phi());
402  if (dR < 0.4) {
403  float energy = aClus->energy();
404  if ( energy < energyMax ) continue;
405  energyMax = energy;
406  em = aClus;
407  }
408  }
409 
410  if (em == corrSCColl->end() ) {
411  // std::cout << "No matching corrected SC with type " << scType << " was found for MC particle! " << std::endl;
412  // std::cout << "Going to next type of SC. " << std::endl;
413  return;
414  }
415  // ------------
416 
418  tree_.emCorr_e = em->energy();
419  tree_.emCorr_et = em->energy() * sin(em->position().theta());
420  tree_.emCorr_phi = em->position().phi();
421  tree_.emCorr_eta = em->position().eta();
422  tree_.emCorr_theta = em->position().theta();
423 
424  // =========== Eta and Theta wrt Vertex does not change after energy corrections are applied
425  // =========== So, no need to calculate them again
426 
427  tree_.emCorr_peta = tree_.em_peta;
428  tree_.emCorr_ptheta = tree_.em_ptheta;
429  tree_.emCorr_pet = tree_.emCorr_e/cosh(tree_.emCorr_peta);
430 
431  tree_.em_pw = em->phiWidth();
432  tree_.em_ew = em->etaWidth();
433  tree_.em_br = tree_.em_pw/tree_.em_ew;
434 
435  mytree_->Fill();
436 
437 }
438 
439 //==========================================================================
440 void
442  //========================================================================
443  //Fill ROOT tree
444  rootFile_->Write();
445 }
446 
void getManyByType(std::vector< Handle< PROD > > &results) const
Definition: Event.h:424
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
virtual void analyze(const edm::Event &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T eta() const
void fillTree(const reco::SuperClusterCollection *scColl, const reco::SuperClusterCollection *corrSCColl, HepMC::GenParticle *mc, tree_structure_ &tree_, float xV, float yV, float zV, int scType)
std::vector< SuperCluster > SuperClusterCollection
collection of SuperCluser objectr
T sqrt(T t)
Definition: SSEVec.h:48
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
tuple genEvent
Definition: MCTruth.py:33
double delta_R(double eta1, double phi1, double eta2, double phi2)
Definition: AnglesUtil.h:116
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
EnergyScaleAnalyzer(const edm::ParameterSet &)
Definition: DDAxes.h:10