CMS 3D CMS Logo

MCElectronAnalyzer.cc
Go to the documentation of this file.
18 
19 #include "TFile.h"
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TTree.h"
23 #include "TVector3.h"
24 #include "TProfile.h"
25 
26 #include <iostream>
27 #include <map>
28 #include <vector>
29 
31 public:
32  //
33  explicit MCElectronAnalyzer(const edm::ParameterSet&);
34  ~MCElectronAnalyzer() override;
35 
36  void analyze(const edm::Event&, const edm::EventSetup&) override;
37  void beginJob() override;
38  void endJob() override;
39 
40 private:
41  float etaTransformation(float a, float b);
42  float phiNormalization(float& a);
43 
44  //
46 
48 
50  TFile* fOutputFile_;
51 
52  int nEvt_;
53  int nMatched_;
54 
56  double mcPhi_;
57  double mcEta_;
58 
63 
64  TH1F* h_MCEleE_;
65  TH1F* h_MCEleEta_;
66  TH1F* h_MCElePhi_;
67  TH1F* h_BremFrac_;
69 
70  TProfile* p_BremVsR_;
71  TProfile* p_BremVsEta_;
72 };
73 
76 
77 using namespace std;
78 
80  : fOutputFileName_(pset.getUntrackedParameter<string>("HistOutFile", std::string("TestConversions.root"))),
81  fOutputFile_(nullptr) {}
82 
84 
86  nEvt_ = 0;
87 
89 
90  fOutputFile_ = new TFile(fOutputFileName_.c_str(), "RECREATE");
91 
93  h_MCEleE_ = new TH1F("MCEleE", "MC ele energy", 100, 0., 200.);
94  h_MCElePhi_ = new TH1F("MCElePhi", "MC ele phi", 40, -3.14, 3.14);
95  h_MCEleEta_ = new TH1F("MCEleEta", "MC ele eta", 40, -3., 3.);
96  h_BremFrac_ = new TH1F("bremFrac", "brem frac ", 100, 0., 1.);
97  h_BremEnergy_ = new TH1F("BremE", "Brem energy", 100, 0., 200.);
98 
99  p_BremVsR_ = new TProfile("BremVsR", " Mean Brem energy vs R ", 48, 0., 120.);
100  p_BremVsEta_ = new TProfile("BremVsEta", " Mean Brem energy vs Eta ", 50, -2.5, 2.5);
101 
102  return;
103 }
104 
105 float MCElectronAnalyzer::etaTransformation(float EtaParticle, float Zvertex) {
106  //---Definitions
107  const float PI = 3.1415927;
108  //const float TWOPI = 2.0*PI;
109 
110  //---Definitions for ECAL
111  const float R_ECAL = 136.5;
112  const float Z_Endcap = 328.0;
113  const float etaBarrelEndcap = 1.479;
114 
115  //---ETA correction
116 
117  float Theta = 0.0;
118  float ZEcal = R_ECAL * sinh(EtaParticle) + Zvertex;
119 
120  if (ZEcal != 0.0)
121  Theta = atan(R_ECAL / ZEcal);
122  if (Theta < 0.0)
123  Theta = Theta + PI;
124  float ETA = -log(tan(0.5 * Theta));
125 
126  if (std::abs(ETA) > etaBarrelEndcap) {
127  float Zend = Z_Endcap;
128  if (EtaParticle < 0.0)
129  Zend = -Zend;
130  float Zlen = Zend - Zvertex;
131  float RR = Zlen / sinh(EtaParticle);
132  Theta = atan(RR / Zend);
133  if (Theta < 0.0)
134  Theta = Theta + PI;
135  ETA = -log(tan(0.5 * Theta));
136  }
137  //---Return the result
138  return ETA;
139  //---end
140 }
141 
143  //---Definitions
144  const float PI = 3.1415927;
145  const float TWOPI = 2.0 * PI;
146 
147  if (phi > PI) {
148  phi = phi - TWOPI;
149  }
150  if (phi < -PI) {
151  phi = phi + TWOPI;
152  }
153 
154  // cout << " Float_t PHInormalization out " << PHI << endl;
155  return phi;
156 }
157 
159  using namespace edm;
160  //const float etaPhiDistance=0.01;
161  // Fiducial region
162  //const float TRK_BARL =0.9;
163  //const float BARL = 1.4442; // DAQ TDR p.290
164  //const float END_LO = 1.566;
165  //const float END_HI = 2.5;
166  // Electron mass
167  //const Float_t mElec= 0.000511;
168 
169  nEvt_++;
170  LogInfo("MCElectronAnalyzer") << "MCElectronAnalyzer Analyzing event number: " << e.id() << " Global Counter "
171  << nEvt_ << "\n";
172  // LogDebug("MCElectronAnalyzer") << "MCElectronAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ <<"\n";
173  std::cout << "MCElectronAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ << "\n";
174 
176  std::cout << " MCElectronAnalyzer Looking for MC truth "
177  << "\n";
178 
179  //get simtrack info
180  std::vector<SimTrack> theSimTracks;
181  std::vector<SimVertex> theSimVertices;
182 
185  e.getByLabel("g4SimHits", SimTk);
186  e.getByLabel("g4SimHits", SimVtx);
187 
188  theSimTracks.insert(theSimTracks.end(), SimTk->begin(), SimTk->end());
189  theSimVertices.insert(theSimVertices.end(), SimVtx->begin(), SimVtx->end());
190  std::cout << " MCElectronAnalyzer This Event has " << theSimTracks.size() << " sim tracks " << std::endl;
191  std::cout << " MCElectronAnalyzer This Event has " << theSimVertices.size() << " sim vertices " << std::endl;
192  if (theSimTracks.empty())
193  std::cout << " Event number " << e.id() << " has NO sim tracks " << std::endl;
194 
195  std::vector<ElectronMCTruth> MCElectronctrons = theElectronMCTruthFinder_->find(theSimTracks, theSimVertices);
196  std::cout << " MCElectronAnalyzer MCElectronctrons size " << MCElectronctrons.size() << std::endl;
197 
198  for (std::vector<ElectronMCTruth>::const_iterator iEl = MCElectronctrons.begin(); iEl != MCElectronctrons.end();
199  ++iEl) {
200  h_MCEleE_->Fill((*iEl).fourMomentum().e());
201  h_MCEleEta_->Fill((*iEl).fourMomentum().pseudoRapidity());
202  h_MCElePhi_->Fill((*iEl).fourMomentum().phi());
203 
204  float totBrem = 0;
205  unsigned int iBrem;
206  for (iBrem = 0; iBrem < (*iEl).bremVertices().size(); ++iBrem) {
207  float rBrem = (*iEl).bremVertices()[iBrem].perp();
208  float etaBrem = (*iEl).bremVertices()[iBrem].eta();
209  if (rBrem < 120) {
210  totBrem += (*iEl).bremMomentum()[iBrem].e();
211  p_BremVsR_->Fill(rBrem, (*iEl).bremMomentum()[iBrem].e());
212  p_BremVsEta_->Fill(etaBrem, (*iEl).bremMomentum()[iBrem].e());
213  }
214  }
215 
216  h_BremFrac_->Fill(totBrem / (*iEl).fourMomentum().e());
217  h_BremEnergy_->Fill(totBrem);
218  }
219 }
220 
222  fOutputFile_->Write();
223  fOutputFile_->Close();
224 
225  edm::LogInfo("MCElectronAnalyzer") << "Analyzed " << nEvt_ << "\n";
226  std::cout << "MCElectronAnalyzer::endJob Analyzed " << nEvt_ << " events "
227  << "\n";
228 
229  return;
230 }
double mcPhi_
global variable for the MC photon
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MCElectronAnalyzer(const edm::ParameterSet &)
ElectronMCTruthFinder * theElectronMCTruthFinder_
static constexpr float R_ECAL
const TrackerGeometry * trackerGeom
#define ETA
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define TWOPI
static constexpr float etaBarrelEndcap
float phiNormalization(float &a)
#define PI
Definition: QcdUeDQM.h:37
Log< level::Info, false > LogInfo
float etaTransformation(float a, float b)
void endJob() override
double b
Definition: hdecay.h:118
HLT enums.
double a
Definition: hdecay.h:119
std::vector< ElectronMCTruth > find(const std::vector< SimTrack > &simTracks, const std::vector< SimVertex > &simVertices)
static constexpr float ZEcal
void analyze(const edm::Event &, const edm::EventSetup &) override
~MCElectronAnalyzer() override
std::string fOutputFileName_
void beginJob() override
static constexpr float Z_Endcap