CMS 3D CMS Logo

CTPPSProtonReconstructionSimulationValidator.cc
Go to the documentation of this file.
1 /****************************************************************************
2  * Authors:
3  * Jan Kašpar
4  ****************************************************************************/
5 
12 
14 
17 
19 
23 
24 #include "TFile.h"
25 #include "TH1D.h"
26 #include "TH2D.h"
27 #include "TProfile.h"
28 #include "TGraphErrors.h"
29 
30 #include "CLHEP/Units/GlobalPhysicalConstants.h"
31 
32 #include <map>
33 #include <string>
34 
35 //----------------------------------------------------------------------------------------------------
36 
38 public:
40 
41 private:
42  void analyze(const edm::Event &, const edm::EventSetup &) override;
43  void endJob() override;
44 
45  void fillPlots(unsigned int meth_idx,
46  unsigned int idx,
47  const reco::ForwardProton &rec_pr,
48  const HepMC::FourVector &vtx,
49  const HepMC::FourVector &mom,
50  const LHCInfo &lhcInfo);
51 
54 
57 
59 
61 
62  struct PlotGroup {
63  std::unique_ptr<TH1D> h_de_xi;
64  std::unique_ptr<TProfile> p_de_xi_vs_xi_simu;
65  std::unique_ptr<TH2D> h_xi_reco_vs_xi_simu;
66 
67  std::unique_ptr<TH1D> h_de_th_x;
68  std::unique_ptr<TProfile> p_de_th_x_vs_xi_simu;
69 
70  std::unique_ptr<TH1D> h_de_th_y;
71  std::unique_ptr<TProfile> p_de_th_y_vs_xi_simu;
72 
73  std::unique_ptr<TH1D> h_de_vtx_y;
74  std::unique_ptr<TProfile> p_de_vtx_y_vs_xi_simu;
75 
76  std::unique_ptr<TH1D> h_de_t;
77  std::unique_ptr<TProfile> p_de_t_vs_xi_simu, p_de_t_vs_t_simu;
78  std::unique_ptr<TH2D> h2_de_t_vs_t_simu;
79 
81  : h_de_xi(new TH1D("", ";#xi_{reco} - #xi_{simu}", 100, 0., 0.)),
82  p_de_xi_vs_xi_simu(new TProfile("", ";#xi_{simu};#xi_{reco} - #xi_{simu}", 50, 0., 0.25)),
83  h_xi_reco_vs_xi_simu(new TH2D("", ";#xi_{simu};#xi_{reco}", 100, 0., 0.30, 100, 0., 0.30)),
84 
85  h_de_th_x(new TH1D("", ";#theta_{x,reco} - #theta_{x,simu}", 100, 0., 0.)),
86  p_de_th_x_vs_xi_simu(new TProfile("", ";#xi_{simu};#theta_{x,reco} - #theta_{x,simu}", 50, 0., 0.25)),
87 
88  h_de_th_y(new TH1D("", ";#theta_{y,reco} - #theta_{y,simu}", 100, 0., 0.)),
89  p_de_th_y_vs_xi_simu(new TProfile("", ";#xi_{simu};#theta_{y,reco} - #theta_{y,simu}", 50, 0., 0.25)),
90 
91  h_de_vtx_y(new TH1D("", ";vtx_{y,reco} - vtx_{y,simu} (mm)", 100, 0., 0.)),
92  p_de_vtx_y_vs_xi_simu(new TProfile("", ";#xi_{simu};vtx_{y,reco} - vtx_{y,simu} (mm)", 50, 0., 0.25)),
93 
94  h_de_t(new TH1D("", ";t_{reco} - t_{simu}", 100, -1., +1.)),
95  p_de_t_vs_xi_simu(new TProfile("", ";xi_{simu};t_{reco} - t_{simu}", 50, 0., 0.25)),
96  p_de_t_vs_t_simu(new TProfile("", ";t_{simu};t_{reco} - t_{simu}", 20, 0., 5.)),
97  h2_de_t_vs_t_simu(new TH2D("", ";t_{simu};t_{reco} - t_{simu}", 150, 0., 5., 300, -2., +2.)) {}
98 
99  static TGraphErrors profileToRMSGraph(TProfile *p, const char *name = "") {
100  TGraphErrors gr_err;
101  gr_err.SetName(name);
102 
103  for (int bi = 1; bi <= p->GetNbinsX(); ++bi) {
104  double c = p->GetBinCenter(bi);
105  double w = p->GetBinWidth(bi);
106 
107  double N = p->GetBinEntries(bi);
108  double Sy = p->GetBinContent(bi) * N;
109  double Syy = p->GetSumw2()->At(bi);
110 
111  double si_sq = Syy / N - Sy * Sy / N / N;
112  double si = (si_sq >= 0.) ? sqrt(si_sq) : 0.;
113  double si_unc_sq = si_sq / 2. / N; // Gaussian approximation
114  double si_unc = (si_unc_sq >= 0.) ? sqrt(si_unc_sq) : 0.;
115 
116  int idx = gr_err.GetN();
117  gr_err.SetPoint(idx, c, si);
118  gr_err.SetPointError(idx, w / 2., si_unc);
119  }
120 
121  return gr_err;
122  }
123 
124  void write() const {
125  h_xi_reco_vs_xi_simu->Write("h_xi_reco_vs_xi_simu");
126  h_de_xi->Write("h_de_xi");
127  p_de_xi_vs_xi_simu->Write("p_de_xi_vs_xi_simu");
128  profileToRMSGraph(p_de_xi_vs_xi_simu.get(), "g_rms_de_xi_vs_xi_simu").Write();
129 
130  h_de_th_x->Write("h_de_th_x");
131  p_de_th_x_vs_xi_simu->Write("p_de_th_x_vs_xi_simu");
132  profileToRMSGraph(p_de_th_x_vs_xi_simu.get(), "g_rms_de_th_x_vs_xi_simu").Write();
133 
134  h_de_th_y->Write("h_de_th_y");
135  p_de_th_y_vs_xi_simu->Write("p_de_th_y_vs_xi_simu");
136  profileToRMSGraph(p_de_th_y_vs_xi_simu.get(), "g_rms_de_th_y_vs_xi_simu").Write();
137 
138  h_de_vtx_y->Write("h_de_vtx_y");
139  p_de_vtx_y_vs_xi_simu->Write("p_de_vtx_y_vs_xi_simu");
140  profileToRMSGraph(p_de_vtx_y_vs_xi_simu.get(), "g_rms_de_vtx_y_vs_xi_simu").Write();
141 
142  h_de_t->Write("h_de_t");
143  p_de_t_vs_xi_simu->Write("p_de_t_vs_xi_simu");
144  profileToRMSGraph(p_de_t_vs_xi_simu.get(), "g_rms_de_t_vs_xi_simu").Write();
145  p_de_t_vs_t_simu->Write("p_de_t_vs_t_simu");
146  profileToRMSGraph(p_de_t_vs_t_simu.get(), "g_rms_de_t_vs_t_simu").Write();
147  h2_de_t_vs_t_simu->Write("h2_de_t_vs_t_simu");
148  }
149  };
150 
151  std::map<unsigned int, std::map<unsigned int, PlotGroup>> plots_;
152 
154  std::unique_ptr<TH2D> h2_t_sh_vs_vtx_t, h2_t_dh_vs_vtx_z;
155  std::unique_ptr<TH1D> h_t_sh_minus_vtx_t, h_t_dh_minus_vtx_z;
156 
158  : h2_t_sh_vs_vtx_t(new TH2D("", ";vtx_t (mm);(t_56 + t_45)/2 (mm)", 100, -250., +250., 100, -250., +250.)),
159  h2_t_dh_vs_vtx_z(new TH2D("", ";vtx_z (mm);(t_56 - t_45)/2 (mm)", 100, -250., +250., 100, -250., +250.)),
160  h_t_sh_minus_vtx_t(new TH1D("", ";(t_56 + t_45)/2 - vtx_t (mm)", 100, -100., +100.)),
161  h_t_dh_minus_vtx_z(new TH1D("", ";(t_56 - t_45)/2 - vtx_z (mm)", 100, -100., +100.)) {}
162 
163  void fill(double time_45, double time_56, double vtx_z, double vtx_t) {
164  const double t_sum_half = (time_56 + time_45) / 2. * CLHEP::c_light;
165  const double t_dif_half = (time_56 - time_45) / 2. * CLHEP::c_light;
166 
167  h2_t_sh_vs_vtx_t->Fill(vtx_t, t_sum_half);
168  h_t_sh_minus_vtx_t->Fill(t_sum_half - vtx_t);
169 
170  h2_t_dh_vs_vtx_z->Fill(vtx_z, t_dif_half);
171  h_t_dh_minus_vtx_z->Fill(t_dif_half - vtx_z);
172  }
173 
174  void write() const {
175  h2_t_sh_vs_vtx_t->Write("h2_t_sh_vs_vtx_t");
176  h_t_sh_minus_vtx_t->Write("h_t_sh_minus_vtx_t");
177 
178  h2_t_dh_vs_vtx_z->Write("h2_t_dh_vs_vtx_z");
179  h_t_dh_minus_vtx_z->Write("h_t_dh_minus_vtx_z");
180  }
181  };
182 
184 };
185 
186 //----------------------------------------------------------------------------------------------------
187 
188 using namespace std;
189 using namespace edm;
190 using namespace HepMC;
191 
192 //----------------------------------------------------------------------------------------------------
193 
195  const edm::ParameterSet &iConfig)
196  : tokenHepMCBeforeSmearing_(
197  consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("tagHepMCBeforeSmearing"))),
198  tokenHepMCAfterSmearing_(
199  consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("tagHepMCAfterSmearing"))),
200  tokenRecoProtonsSingleRP_(
201  consumes<reco::ForwardProtonCollection>(iConfig.getParameter<InputTag>("tagRecoProtonsSingleRP"))),
202  tokenRecoProtonsMultiRP_(
203  consumes<reco::ForwardProtonCollection>(iConfig.getParameter<InputTag>("tagRecoProtonsMultiRP"))),
204  lhcInfoESToken_(esConsumes(ESInputTag("", iConfig.getParameter<std::string>("lhcInfoLabel")))),
205  outputFile_(iConfig.getParameter<string>("outputFile")) {}
206 
207 //----------------------------------------------------------------------------------------------------
208 
210  // get conditions
211  const auto &lhcInfo = iSetup.getData(lhcInfoESToken_);
212 
213  // get input
214  edm::Handle<edm::HepMCProduct> hHepMCBeforeSmearing;
215  iEvent.getByToken(tokenHepMCBeforeSmearing_, hHepMCBeforeSmearing);
216  HepMC::GenEvent *hepMCEventBeforeSmearing = (HepMC::GenEvent *)hHepMCBeforeSmearing->GetEvent();
217 
218  edm::Handle<edm::HepMCProduct> hHepMCAfterSmearing;
219  iEvent.getByToken(tokenHepMCAfterSmearing_, hHepMCAfterSmearing);
220  HepMC::GenEvent *hepMCEventAfterSmearing = (HepMC::GenEvent *)hHepMCAfterSmearing->GetEvent();
221 
222  Handle<reco::ForwardProtonCollection> hRecoProtonsSingleRP;
223  iEvent.getByToken(tokenRecoProtonsSingleRP_, hRecoProtonsSingleRP);
224 
225  Handle<reco::ForwardProtonCollection> hRecoProtonsMultiRP;
226  iEvent.getByToken(tokenRecoProtonsMultiRP_, hRecoProtonsMultiRP);
227 
228  // extract vertex position
229  bool vertex_set = false;
230  FourVector vtx;
231  for (auto it = hepMCEventAfterSmearing->vertices_begin(); it != hepMCEventAfterSmearing->vertices_end(); ++it) {
232  if (vertex_set) {
233  LogError("CTPPSProtonReconstructionSimulationValidator") << "Multiple vertices found.";
234  return;
235  }
236 
237  vertex_set = true;
238  vtx = (*it)->position();
239  }
240 
241  // extract forward protons
242  bool proton_45_set = false;
243  bool proton_56_set = false;
244  FourVector mom_45, mom_56;
245 
246  for (auto it = hepMCEventBeforeSmearing->particles_begin(); it != hepMCEventBeforeSmearing->particles_end(); ++it) {
247  const auto &part = *it;
248 
249  // accept only stable non-beam protons
250  if (part->pdg_id() != 2212)
251  continue;
252 
253  if (part->status() != 1)
254  continue;
255 
256  if (part->is_beam())
257  continue;
258 
259  const auto &mom = part->momentum();
260 
261  if (mom.e() < 4500.)
262  continue;
263 
264  if (mom.z() > 0) {
265  // 45
266  if (proton_45_set) {
267  LogError("CTPPSProtonReconstructionSimulationValidator") << "Found multiple protons in sector 45.";
268  return;
269  }
270 
271  proton_45_set = true;
272  mom_45 = mom;
273  } else {
274  // 56
275  if (proton_56_set) {
276  LogError("CTPPSProtonReconstructionSimulationValidator") << "Found multiple protons in sector 56.";
277  return;
278  }
279 
280  proton_56_set = true;
281  mom_56 = mom;
282  }
283  }
284 
285  // single-arm comparison
286  for (const auto &handle : {hRecoProtonsSingleRP, hRecoProtonsMultiRP}) {
287  for (const auto &rec_pr : *handle) {
288  if (!rec_pr.validFit())
289  continue;
290 
291  unsigned int idx = 12345;
292 
293  bool mom_set = false;
294  FourVector mom;
295 
296  if (rec_pr.lhcSector() == reco::ForwardProton::LHCSector::sector45) {
297  idx = 0;
298  mom_set = proton_45_set;
299  mom = mom_45;
300  }
301  if (rec_pr.lhcSector() == reco::ForwardProton::LHCSector::sector56) {
302  idx = 1;
303  mom_set = proton_56_set;
304  mom = mom_56;
305  }
306 
307  if (!mom_set)
308  continue;
309 
310  unsigned int meth_idx = 1234;
311 
313  meth_idx = 0;
314 
315  CTPPSDetId rpId((*rec_pr.contributingLocalTracks().begin())->rpId());
316  idx = 100 * rpId.arm() + 10 * rpId.station() + rpId.rp();
317  }
318 
320  meth_idx = 1;
321 
322  fillPlots(meth_idx, idx, rec_pr, vtx, mom, lhcInfo);
323  }
324  }
325 
326  // double-arm comparison
327  bool time_set_45 = false, time_set_56 = false;
328  double time_45 = 0., time_56 = 0.;
329  for (const auto &rec_pr : *hRecoProtonsMultiRP) {
330  if (!rec_pr.validFit())
331  continue;
332 
333  // skip protons with no timing information
334  if (rec_pr.timeError() < 1E-3)
335  continue;
336 
337  if (rec_pr.lhcSector() == reco::ForwardProton::LHCSector::sector45) {
338  time_set_45 = true;
339  time_45 = rec_pr.time();
340  }
341  if (rec_pr.lhcSector() == reco::ForwardProton::LHCSector::sector56) {
342  time_set_56 = true;
343  time_56 = rec_pr.time();
344  }
345  }
346 
347  if (time_set_45 && time_set_56)
348  double_arm_plots_.fill(time_45, time_56, vtx.z(), vtx.t());
349 }
350 
351 //----------------------------------------------------------------------------------------------------
352 
354  unsigned int idx,
355  const reco::ForwardProton &rec_pr,
356  const HepMC::FourVector &vtx,
357  const HepMC::FourVector &mom,
358  const LHCInfo &lhcInfo) {
359  const double p_nom = lhcInfo.energy();
360  const double xi_simu = (p_nom - mom.rho()) / p_nom;
361  const double th_x_simu = mom.x() / mom.rho();
362  const double th_y_simu = mom.y() / mom.rho();
363  const double vtx_y_simu = vtx.y();
364  const double th_simu = sqrt(th_x_simu * th_x_simu + th_y_simu * th_y_simu);
365  const double t_simu = -reco::ForwardProton::calculateT(p_nom, mom.rho(), th_simu);
366 
367  const double xi_reco = rec_pr.xi();
368  const double th_x_reco = rec_pr.thetaX();
369  const double th_y_reco = rec_pr.thetaY();
370  const double vtx_y_reco = rec_pr.vertex().y() * 10.; // conversion: cm --> mm
371  const double t_reco = -rec_pr.t();
372 
373  auto &plt = plots_[meth_idx][idx];
374 
375  plt.h_xi_reco_vs_xi_simu->Fill(xi_simu, xi_reco);
376  plt.h_de_xi->Fill(xi_reco - xi_simu);
377  plt.p_de_xi_vs_xi_simu->Fill(xi_simu, xi_reco - xi_simu);
378 
379  plt.h_de_th_x->Fill(th_x_reco - th_x_simu);
380  plt.p_de_th_x_vs_xi_simu->Fill(xi_simu, th_x_reco - th_x_simu);
381 
382  plt.h_de_th_y->Fill(th_y_reco - th_y_simu);
383  plt.p_de_th_y_vs_xi_simu->Fill(xi_simu, th_y_reco - th_y_simu);
384 
385  plt.h_de_vtx_y->Fill(vtx_y_reco - vtx_y_simu);
386  plt.p_de_vtx_y_vs_xi_simu->Fill(xi_simu, vtx_y_reco - vtx_y_simu);
387 
388  plt.h_de_t->Fill(t_reco - t_simu);
389  plt.p_de_t_vs_xi_simu->Fill(xi_simu, t_reco - t_simu);
390  plt.p_de_t_vs_t_simu->Fill(t_simu, t_reco - t_simu);
391  plt.h2_de_t_vs_t_simu->Fill(t_simu, t_reco - t_simu);
392 }
393 
394 //----------------------------------------------------------------------------------------------------
395 
397  auto f_out = std::make_unique<TFile>(outputFile_.c_str(), "recreate");
398 
399  for (const auto &mit : plots_) {
400  const char *method = (mit.first == 0) ? "single rp" : "multi rp";
401  TDirectory *d_method = f_out->mkdir(method);
402 
403  for (const auto &eit : mit.second) {
404  gDirectory = d_method->mkdir(Form("%i", eit.first));
405  eit.second.write();
406  }
407  }
408 
409  gDirectory = f_out->mkdir("double arm");
411 }
412 
413 //----------------------------------------------------------------------------------------------------
414 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
static float calculateT(double beam_mom, double proton_mom, double theta)
compute the squared four-momentum transfer from incident and scattered momenta, and angular informati...
T w() const
float thetaY() const
horizontal scattering angle, in rad
Definition: ForwardProton.h:83
float const energy() const
Definition: LHCInfo.cc:190
edm::EDGetTokenT< reco::ForwardProtonCollection > tokenRecoProtonsSingleRP_
float thetaX() const
vertical scattering angle, in rad
Definition: ForwardProton.h:81
Log< level::Error, false > LogError
int iEvent
Definition: GenABIO.cc:224
T sqrt(T t)
Definition: SSEVec.h:19
edm::EDGetTokenT< reco::ForwardProtonCollection > tokenRecoProtonsMultiRP_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
float xi() const
longitudinal fractional momentum loss
Definition: ForwardProton.h:79
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
#define N
Definition: blowfish.cc:9
static TGraphErrors profileToRMSGraph(TProfile *p, const char *name="")
part
Definition: HCALResponse.h:20
const Point & vertex() const
fitted vertex position
Definition: ForwardProton.h:51
void fill(double time_45, double time_56, double vtx_z, double vtx_t)
void analyze(const edm::Event &, const edm::EventSetup &) override
Base class for CTPPS detector IDs.
Definition: CTPPSDetId.h:32
fixed size matrix
HLT enums.
ap_int< 5 > vtx_t
Definition: datatypes.h:29
std::vector< ForwardProton > ForwardProtonCollection
Collection of ForwardProton objects.
float t() const
four-momentum transfer squared, in GeV^2
void fillPlots(unsigned int meth_idx, unsigned int idx, const reco::ForwardProton &rec_pr, const HepMC::FourVector &vtx, const HepMC::FourVector &mom, const LHCInfo &lhcInfo)
std::map< unsigned int, std::map< unsigned int, PlotGroup > > plots_