CMS 3D CMS Logo

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