CMS 3D CMS Logo

CTPPSBeamSmearingValidator.cc
Go to the documentation of this file.
1 /****************************************************************************
2  * Authors:
3  * Jan Kašpar
4  ****************************************************************************/
5 
8 
11 
13 
14 #include "TFile.h"
15 #include "TH1D.h"
16 
17 #include <map>
18 
19 //----------------------------------------------------------------------------------------------------
20 
22 public:
24 
26 
27 private:
28  void analyze(const edm::Event &, const edm::EventSetup &) override;
29 
30  void endJob() override;
31 
34 
36 
37  std::unique_ptr<TH1D> h_de_vtx_x_, h_de_vtx_y_, h_de_vtx_z_, h_de_vtx_t_;
38 
39  struct SectorPlots {
40  std::unique_ptr<TH1D> h_de_th_x, h_de_th_y, h_de_p;
41 
43  : h_de_th_x(new TH1D("", ";#Delta#theta_{x} (rad)", 100, 0., 0.)),
44  h_de_th_y(new TH1D("", ";#Delta#theta_{y} (rad)", 100, 0., 0.)),
45  h_de_p(new TH1D("", ";#Deltap (GeV)", 100, 0., 0.)) {}
46 
47  void write() const {
48  h_de_th_x->Write("h_de_th_x");
49  h_de_th_y->Write("h_de_th_y");
50  h_de_p->Write("h_de_p");
51  }
52  };
53 
54  std::map<unsigned int, SectorPlots> sectorPlots_;
55 };
56 
57 //----------------------------------------------------------------------------------------------------
58 
59 using namespace std;
60 using namespace edm;
61 using namespace HepMC;
62 
63 //----------------------------------------------------------------------------------------------------
64 
66  : tokenBeforeSmearing_(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("tagBeforeSmearing"))),
67  tokenAfterSmearing_(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("tagAfterSmearing"))),
68  outputFile_(iConfig.getParameter<string>("outputFile")),
69  h_de_vtx_x_(new TH1D("h_de_vtx_x", ";#Delta vtx_{x} (mm)", 100, 0., 0.)),
70  h_de_vtx_y_(new TH1D("h_de_vtx_y", ";#Delta vtx_{y} (mm)", 100, 0., 0.)),
71  h_de_vtx_z_(new TH1D("h_de_vtx_z", ";#Delta vtx_{z} (mm)", 100, 0., 0.)),
72  h_de_vtx_t_(new TH1D("h_de_vtx_t", ";#Delta vtx_{t} (mm)", 100, 0., 0.)) {}
73 
74 //----------------------------------------------------------------------------------------------------
75 
77  // get input
78  edm::Handle<edm::HepMCProduct> hBeforeSmearing;
79  iEvent.getByToken(tokenBeforeSmearing_, hBeforeSmearing);
80  HepMC::GenEvent *orig = (HepMC::GenEvent *)hBeforeSmearing->GetEvent();
81 
82  edm::Handle<edm::HepMCProduct> hAfterSmearing;
83  iEvent.getByToken(tokenAfterSmearing_, hAfterSmearing);
84  HepMC::GenEvent *smear = (HepMC::GenEvent *)hAfterSmearing->GetEvent();
85 
86  // vertices
87  GenEvent::vertex_const_iterator vold, vnew;
88  for (vold = orig->vertices_begin(), vnew = smear->vertices_begin();
89  vold != orig->vertices_end() && vnew != smear->vertices_end();
90  ++vold, ++vnew) {
91  const FourVector &vo = (*vold)->position();
92  const FourVector &vn = (*vnew)->position();
93 
94  // HepMC gives vertex in mm
95  h_de_vtx_x_->Fill(vn.x() - vo.x());
96  h_de_vtx_y_->Fill(vn.y() - vo.y());
97  h_de_vtx_z_->Fill(vn.z() - vo.z());
98  h_de_vtx_t_->Fill(vn.t() - vo.t());
99  }
100 
101  // particles
102  GenEvent::particle_const_iterator pold, pnew;
103  for (pold = orig->particles_begin(), pnew = smear->particles_begin();
104  pold != orig->particles_end() && pnew != smear->particles_end();
105  ++pold, ++pnew) {
106  FourVector o = (*pold)->momentum(), n = (*pnew)->momentum();
107 
108  // determine direction region
109  signed int idx = -1;
110  const double thetaLim = 0.01; // rad
111  double th = o.theta();
112 
113  if (th < thetaLim)
114  idx = 0;
115  if (th > (M_PI - thetaLim))
116  idx = 1;
117 
118  if (idx < 0)
119  continue;
120 
121  /*
122  cout << "particle\n\told: [" << o.x() << ", " << o.y() << ", " << o.z() << ", " << o.t()
123  << "]\n\tnew: [" << n.x() << ", " << n.y() << ", " << n.z() << ", " << n.t()
124  << "]\n\tregion: " << idx << endl;
125  */
126 
127  // fill histograms
128  auto &sp = sectorPlots_[idx];
129 
130  double othx = o.x() / o.rho(), othy = o.y() / o.rho();
131  double nthx = n.x() / n.rho(), nthy = n.y() / n.rho();
132 
133  sp.h_de_p->Fill(n.rho() - o.rho());
134 
135  sp.h_de_th_x->Fill(nthx - othx);
136  sp.h_de_th_y->Fill(nthy - othy);
137  }
138 }
139 
140 //----------------------------------------------------------------------------------------------------
141 
143  auto f_out = std::make_unique<TFile>(outputFile_.c_str(), "recreate");
144 
145  h_de_vtx_x_->Write();
146  h_de_vtx_y_->Write();
147  h_de_vtx_z_->Write();
148  h_de_vtx_t_->Write();
149 
150  gDirectory = f_out->mkdir("sector 45");
151  sectorPlots_[0].write();
152 
153  gDirectory = f_out->mkdir("sector 56");
154  sectorPlots_[1].write();
155 }
156 
157 //----------------------------------------------------------------------------------------------------
158 
void analyze(const edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< edm::HepMCProduct > tokenAfterSmearing_
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
#define M_PI
HLT enums.
edm::EDGetTokenT< edm::HepMCProduct > tokenBeforeSmearing_
std::map< unsigned int, SectorPlots > sectorPlots_
CTPPSBeamSmearingValidator(const edm::ParameterSet &)