CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SimpleConvertedPhotonAnalyzer.cc
Go to the documentation of this file.
24 
25 #include "TFile.h"
26 #include "TH1.h"
27 #include "TH2.h"
28 #include "TTree.h"
29 #include "TVector3.h"
30 #include "TProfile.h"
31 
32 #include <iostream>
33 #include <map>
34 #include <vector>
35 
37 public:
38  //
41 
42  void analyze(const edm::Event&, const edm::EventSetup&) override;
43  void beginJob() override;
44  void endJob() override;
45 
46 private:
47  float etaTransformation(float a, float b);
48 
49  //
51 
53  TFile* fOutputFile_;
54 
55  int nEvt_;
56  int nMCPho_;
57  int nMatched_;
58 
63 
66 
67  TH1F* h_ErecoEMC_;
68  TH1F* h_deltaPhi_;
69  TH1F* h_deltaEta_;
70 
72  TH1F* h_MCphoE_;
73  TH1F* h_MCphoPhi_;
74  TH1F* h_MCphoEta_;
75 
77  TH1F* h_MCConvE_;
78  TH1F* h_MCConvPt_;
79  TH1F* h_MCConvEta_;
80 
81  // SC from reco photons
82  TH1F* h_scE_;
83  TH1F* h_scEta_;
84  TH1F* h_scPhi_;
85  //
86  TH1F* h_phoE_;
87  TH1F* h_phoEta_;
88  TH1F* h_phoPhi_;
89  //
90  // All tracks from reco photons
92  //
94 };
95 
98 
99 using namespace std;
100 
102  : fOutputFileName_(pset.getUntrackedParameter<string>("HistOutFile", std::string("TestConversions.root"))),
103  fOutputFile_(nullptr) {
105  convertedPhotonCollection_ = pset.getParameter<std::string>("convertedPhotonCollection");
106  //
107 }
108 
110 
112  nEvt_ = 0;
113  nMCPho_ = 0;
114  nMatched_ = 0;
115 
117 
118  fOutputFile_ = new TFile(fOutputFileName_.c_str(), "RECREATE");
119 
121  h_ErecoEMC_ = new TH1F("deltaE", " delta(reco-mc) energy", 100, 0., 2.);
122  h_deltaPhi_ = new TH1F("deltaPhi", " delta(reco-mc) phi", 100, -0.1, 0.1);
123  h_deltaEta_ = new TH1F("deltaEta", " delta(reco-mc) eta", 100, -0.05, 0.05);
124 
126  h_MCphoE_ = new TH1F("MCphoE", "MC photon energy", 100, 0., 100.);
127  h_MCphoPhi_ = new TH1F("MCphoPhi", "MC photon phi", 40, -3.14, 3.14);
128  h_MCphoEta_ = new TH1F("MCphoEta", "MC photon eta", 40, -3., 3.);
129 
131  h_MCConvE_ = new TH1F("MCConvE", "MC converted photon energy", 100, 0., 100.);
132  h_MCConvPt_ = new TH1F("MCConvPt", "MC converted photon pt", 100, 0., 100.);
133  h_MCConvEta_ = new TH1F("MCConvEta", "MC converted photon eta", 50, 0., 2.5);
134 
136  h_scE_ = new TH1F("scE", "Uncorrected converted photons : SC Energy ", 100, 0., 200.);
137  h_scEta_ = new TH1F("scEta", "Uncorrected converted photons: SC Eta ", 40, -3., 3.);
138  h_scPhi_ = new TH1F("scPhi", "Uncorrected converted photons: SC Phi ", 40, -3.14, 3.14);
139  //
140  h_phoE_ = new TH1F("phoE", "Uncorrected converted photons : Energy ", 100, 0., 200.);
141  h_phoEta_ = new TH1F("phoEta", "Uncorrected converted photons: Eta ", 40, -3., 3.);
142  h_phoPhi_ = new TH1F("phoPhi", "Uncorrected converted photons: Phi ", 40, -3.14, 3.14);
143 
144  // Recontructed tracks from converted photon candidates
145  h2_tk_nHitsVsR_ = new TH2F("tknHitsVsR", "Tracks Hits vs R ", 12, 0., 120., 20, 0.5, 20.5);
146  h2_tk_inPtVsR_ = new TH2F("tkInPtvsR", "Tracks inner Pt vs R ", 12, 0., 120., 100, 0., 100.);
147 
148  return;
149 }
150 
151 float SimpleConvertedPhotonAnalyzer::etaTransformation(float EtaParticle, float Zvertex) {
152  //---Definitions
153  const float PI = 3.1415927;
154  //UNUSED const float TWOPI = 2.0*PI;
155 
156  //---Definitions for ECAL
157  const float R_ECAL = 136.5;
158  const float Z_Endcap = 328.0;
159  const float etaBarrelEndcap = 1.479;
160 
161  //---ETA correction
162 
163  float Theta = 0.0;
164  float ZEcal = R_ECAL * sinh(EtaParticle) + Zvertex;
165 
166  if (ZEcal != 0.0)
167  Theta = atan(R_ECAL / ZEcal);
168  if (Theta < 0.0)
169  Theta = Theta + PI;
170  float ETA = -log(tan(0.5 * Theta));
171 
172  if (fabs(ETA) > etaBarrelEndcap) {
173  float Zend = Z_Endcap;
174  if (EtaParticle < 0.0)
175  Zend = -Zend;
176  float Zlen = Zend - Zvertex;
177  float RR = Zlen / sinh(EtaParticle);
178  Theta = atan(RR / Zend);
179  if (Theta < 0.0)
180  Theta = Theta + PI;
181  ETA = -log(tan(0.5 * Theta));
182  }
183  //---Return the result
184  return ETA;
185  //---end
186 }
187 
189  using namespace edm;
190  const float etaPhiDistance = 0.01;
191  // Fiducial region
192  //UNUSED const float TRK_BARL =0.9;
193  const float BARL = 1.4442; // DAQ TDR p.290
194  const float END_LO = 1.566;
195  const float END_HI = 2.5;
196  // Electron mass
197  //UNUSED const Float_t mElec= 0.000511;
198 
199  nEvt_++;
200  LogInfo("ConvertedPhotonAnalyzer") << "ConvertedPhotonAnalyzer Analyzing event number: " << e.id()
201  << " Global Counter " << nEvt_ << "\n";
202  // LogDebug("ConvertedPhotonAnalyzer") << "ConvertedPhotonAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ <<"\n";
203  std::cout << "ConvertedPhotonAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ << "\n";
204 
206  Handle<reco::ConversionCollection> convertedPhotonHandle;
208  const reco::ConversionCollection phoCollection = *(convertedPhotonHandle.product());
209  std::cout << "ConvertedPhotonAnalyzer Converted photon collection size " << phoCollection.size() << "\n";
210 
212  std::cout << " ConvertedPhotonAnalyzer Looking for MC truth "
213  << "\n";
214 
215  //get simtrack info
216  std::vector<SimTrack> theSimTracks;
217  std::vector<SimVertex> theSimVertices;
218 
221  e.getByLabel("g4SimHits", SimTk);
222  e.getByLabel("g4SimHits", SimVtx);
223 
224  theSimTracks.insert(theSimTracks.end(), SimTk->begin(), SimTk->end());
225  theSimVertices.insert(theSimVertices.end(), SimVtx->begin(), SimVtx->end());
226 
227  std::vector<PhotonMCTruth> mcPhotons = thePhotonMCTruthFinder_->find(theSimTracks, theSimVertices);
228  std::cout << " ConvertedPhotonAnalyzer mcPhotons size " << mcPhotons.size() << std::endl;
229 
230  // Loop over simulated photons
231  //UNUSED int iDet=0;
232  //UNUSED int iRadius=-1;
233  //UNUSED int indPho=0;
234 
235  for (std::vector<PhotonMCTruth>::const_iterator mcPho = mcPhotons.begin(); mcPho != mcPhotons.end(); mcPho++) {
236  float mcPhi = (*mcPho).fourMomentum().phi();
237  float mcEta = (*mcPho).fourMomentum().pseudoRapidity();
238  mcEta = etaTransformation(mcEta, (*mcPho).primaryVertex().z());
239 
240  if (!(fabs(mcEta) <= BARL || (fabs(mcEta) >= END_LO && fabs(mcEta) <= END_HI))) {
241  continue;
242  } // all ecal fiducial region
243 
244  std::cout << " ConvertedPhotonAnalyzer MC Photons before matching " << std::endl;
245  std::cout << " ConvertedPhotonAnalyzer Photons isAconversion " << (*mcPho).isAConversion()
246  << " mcMatchingPhoton energy " << (*mcPho).fourMomentum().e() << " conversion vertex R "
247  << (*mcPho).vertex().perp() << " Z " << (*mcPho).vertex().z() << " x " << (*mcPho).vertex().x() << " y "
248  << (*mcPho).vertex().y() << " z " << (*mcPho).vertex().z() << std::endl;
249  std::cout << " ConvertedPhotonAnalyzer mcEta " << mcEta << " mcPhi " << mcPhi << std::endl;
250 
251  h_MCphoE_->Fill((*mcPho).fourMomentum().e());
252  h_MCphoEta_->Fill((*mcPho).fourMomentum().eta());
253  h_MCphoPhi_->Fill((*mcPho).fourMomentum().phi());
254 
255  // keep only visible conversions
256  if ((*mcPho).isAConversion() == 0)
257  continue;
258 
259  nMCPho_++;
260 
261  h_MCConvEta_->Fill(fabs((*mcPho).fourMomentum().pseudoRapidity()) - 0.001);
262 
263  bool REJECTED;
264 
266  std::cout << " ConvertedPhotonAnalyzer Starting loop over photon candidates "
267  << "\n";
268  for (reco::ConversionCollection::const_iterator iPho = phoCollection.begin(); iPho != phoCollection.end(); iPho++) {
269  REJECTED = false;
270 
271  std::cout << " ConvertedPhotonAnalyzer Reco SC energy " << (*iPho).caloCluster()[0]->energy() << "\n";
272 
273  float phiClu = (*iPho).caloCluster()[0]->phi();
274  float etaClu = (*iPho).caloCluster()[0]->eta();
275  float deltaPhi = phiClu - mcPhi;
276  float deltaEta = etaClu - mcEta;
277 
278  if (deltaPhi > Geom::pi())
279  deltaPhi -= Geom::twoPi();
280  if (deltaPhi < -Geom::pi())
281  deltaPhi += Geom::twoPi();
282  deltaPhi = std::pow(deltaPhi, 2);
283  deltaEta = std::pow(deltaEta, 2);
284  float delta = deltaPhi + deltaEta;
285  if (delta >= etaPhiDistance)
286  REJECTED = true;
287 
288  // if ( ! ( fabs(etaClu) <= BARL || ( fabs(etaClu) >= END_LO && fabs(etaClu) <=END_HI ) ) ) REJECTED=true;
289 
290  if (REJECTED)
291  continue;
292  std::cout << " MATCHED " << std::endl;
293  nMatched_++;
294 
295  std::cout << " ConvertedPhotonAnalyzer Matching candidate " << std::endl;
296 
297  std::cout << " ConvertedPhotonAnalyzer Photons isAconversion " << (*mcPho).isAConversion()
298  << " mcMatchingPhoton energy " << (*mcPho).fourMomentum().e()
299  << " ConvertedPhotonAnalyzer conversion vertex R " << (*mcPho).vertex().perp() << " Z "
300  << (*mcPho).vertex().z() << std::endl;
301 
302  h_ErecoEMC_->Fill((*iPho).caloCluster()[0]->energy() / (*mcPho).fourMomentum().e());
303  h_deltaPhi_->Fill((*iPho).caloCluster()[0]->position().phi() - mcPhi);
304  h_deltaEta_->Fill((*iPho).caloCluster()[0]->position().eta() - mcEta);
305 
306  h_scE_->Fill((*iPho).caloCluster()[0]->energy());
307  h_scEta_->Fill((*iPho).caloCluster()[0]->position().eta());
308  h_scPhi_->Fill((*iPho).caloCluster()[0]->position().phi());
309 
310  for (unsigned int i = 0; i < (*iPho).tracks().size(); i++) {
311  std::cout << " ConvertedPhotonAnalyzer Reco Track charge " << (*iPho).tracks()[i]->charge()
312  << " Num of RecHits " << (*iPho).tracks()[i]->recHitsSize() << " inner momentum "
313  << sqrt((*iPho).tracks()[i]->innerMomentum().Mag2()) << "\n";
314 
315  h2_tk_nHitsVsR_->Fill((*mcPho).vertex().perp(), (*iPho).tracks()[i]->recHitsSize());
316  h2_tk_inPtVsR_->Fill((*mcPho).vertex().perp(), sqrt((*iPho).tracks()[i]->innerMomentum().Mag2()));
317  }
318 
319  }
320 
321  }
322 }
323 
325  fOutputFile_->Write();
326  fOutputFile_->Close();
327 
328  edm::LogInfo("ConvertedPhotonAnalyzer") << "Analyzed " << nEvt_ << "\n";
329  // std::cout << "::endJob Analyzed " << nEvt_ << " events " << " with total " << nPho_ << " Photons " << "\n";
330  std::cout << "ConvertedPhotonAnalyzer::endJob Analyzed " << nEvt_ << " events "
331  << "\n";
332 
333  return;
334 }
static std::vector< std::string > checklist log
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static constexpr float R_ECAL
std::vector< Conversion > ConversionCollection
collectin of Conversion objects
Definition: ConversionFwd.h:9
static const double deltaEta
Definition: CaloConstants.h:8
#define ETA
SimpleConvertedPhotonAnalyzer(const edm::ParameterSet &)
T sqrt(T t)
Definition: SSEVec.h:19
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
static constexpr float etaBarrelEndcap
#define PI
Definition: QcdUeDQM.h:37
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:500
Log< level::Info, false > LogInfo
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double b
Definition: hdecay.h:118
std::vector< PhotonMCTruth > find(const std::vector< SimTrack > &simTracks, const std::vector< SimVertex > &simVertices)
edm::EventID id() const
Definition: EventBase.h:59
double a
Definition: hdecay.h:119
static constexpr float ZEcal
tuple cout
Definition: gather_cfg.py:144
constexpr double pi()
Definition: Pi.h:31
constexpr double twoPi()
Definition: Pi.h:32
void analyze(const edm::Event &, const edm::EventSetup &) override
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
static constexpr float Z_Endcap