CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HGCalTriggerNtupleGen.cc
Go to the documentation of this file.
5 
8 
13 
17 
18 // NOTE: most of this code is borrowed by https://github.com/CMS-HGCAL/reco-ntuples
19 // kudos goes to the original authors. Ideally the 2 repos should be merged since they share part of the use case
20 #include <memory>
21 
22 namespace HGCal_helpers {
23 
24  class Coordinates {
25  public:
26  Coordinates() : x(0), y(0), z(0), eta(0), phi(0) {}
27  float x, y, z, eta, phi;
29  };
30 
32  public:
34  ROOT::Math::SMatrixIdentity id;
36  const float uncert = 0.001;
37  C *= uncert;
39  }
40  void setPropagationTargetZ(const float &z);
41 
42  bool propagate(const double px,
43  const double py,
44  const double pz,
45  const double x,
46  const double y,
47  const double z,
48  const float charge,
49  Coordinates &coords) const;
50 
51  bool propagate(const math::XYZTLorentzVectorD &momentum,
53  const float charge,
54  Coordinates &coords) const;
55 
56  private:
58  const RKPropagatorInS &RKProp() const { return prod_.propagator; }
63  float absz_target_;
64  };
65 
70  }
71  bool SimpleTrackPropagator::propagate(const double px,
72  const double py,
73  const double pz,
74  const double x,
75  const double y,
76  const double z,
77  const float charge,
78  Coordinates &output) const {
79  output = Coordinates();
80 
82  GlobalPoint startingPosition(x, y, z);
83  GlobalVector startingMomentum(px, py, pz);
85  TSOS startingStateP(
86  GlobalTrajectoryParameters(startingPosition, startingMomentum, charge, field_), err_, *startingPlane);
87 
88  TSOS trackStateP;
89  if (pz > 0) {
90  trackStateP = RKProp().propagate(startingStateP, *targetPlaneForward_);
91  } else {
92  trackStateP = RKProp().propagate(startingStateP, *targetPlaneBackward_);
93  }
94  if (trackStateP.isValid()) {
95  output.x = trackStateP.globalPosition().x();
96  output.y = trackStateP.globalPosition().y();
97  output.z = trackStateP.globalPosition().z();
98  output.phi = trackStateP.globalPosition().phi();
99  output.eta = trackStateP.globalPosition().eta();
100  return true;
101  }
102  return false;
103  }
104 
107  const float charge,
108  Coordinates &output) const {
109  return propagate(
110  momentum.px(), momentum.py(), momentum.pz(), position.x(), position.y(), position.z(), charge, output);
111  }
112 
113 } // namespace HGCal_helpers
114 
116 public:
118 
119  void initialize(TTree &, const edm::ParameterSet &, edm::ConsumesCollector &&) final;
120  void fill(const edm::Event &, const HGCalTriggerNtupleEventSetup &) final;
121 
123 
124 private:
125  void clear() final;
126 
129 
130  int gen_n_;
133 
134  float vtx_x_;
135  float vtx_y_;
136  float vtx_z_;
137 
139  // GenParticles
140  //
141  std::vector<float> genpart_eta_;
142  std::vector<float> genpart_phi_;
143  std::vector<float> genpart_pt_;
144  std::vector<float> genpart_energy_;
145  std::vector<float> genpart_dvx_;
146  std::vector<float> genpart_dvy_;
147  std::vector<float> genpart_dvz_;
148  std::vector<float> genpart_ovx_;
149  std::vector<float> genpart_ovy_;
150  std::vector<float> genpart_ovz_;
151  std::vector<float> genpart_exx_;
152  std::vector<float> genpart_exy_;
154  std::vector<float> genpart_exphi_;
155  std::vector<float> genpart_exeta_;
156  std::vector<float> genpart_fbrem_;
157  std::vector<int> genpart_pid_;
158  std::vector<int> genpart_gen_;
161  std::vector<std::vector<float>> genpart_posx_;
162  std::vector<std::vector<float>> genpart_posy_;
163  std::vector<std::vector<float>> genpart_posz_;
164 
166  // reco::GenParticles
167  //
168  std::vector<float> gen_eta_;
169  std::vector<float> gen_phi_;
170  std::vector<float> gen_pt_;
171  std::vector<float> gen_energy_;
172  std::vector<int> gen_charge_;
173  std::vector<int> gen_pdgid_;
174  std::vector<int> gen_status_;
175  std::vector<std::vector<int>> gen_daughters_;
176 
177  // -------convenient tool to deal with simulated tracks
179 
180  // and also the magnetic field
182 
184 
188 };
189 
191 
192 HGCalTriggerNtupleGen::HGCalTriggerNtupleGen(const edm::ParameterSet &conf) : HGCalTriggerNtupleBase(conf) {
193  accessEventSetup_ = false;
194 }
195 
197  edm::ParameterSet particleFilter_(conf.getParameter<edm::ParameterSet>("particleFilter"));
198  mySimEvent_ = std::make_unique<FSimEvent>(particleFilter_);
199 
200  gen_token_ = collector.consumes<reco::GenParticleCollection>(conf.getParameter<edm::InputTag>("GenParticles"));
201  gen_PU_token_ = collector.consumes<std::vector<PileupSummaryInfo>>(conf.getParameter<edm::InputTag>("GenPU"));
202  tree.Branch("gen_n", &gen_n_, "gen_n/I");
203  tree.Branch("gen_PUNumInt", &gen_PUNumInt_, "gen_PUNumInt/I");
204  tree.Branch("gen_TrueNumInt", &gen_TrueNumInt_, "gen_TrueNumInt/F");
205 
206  hepmcev_token_ = collector.consumes<edm::HepMCProduct>(conf.getParameter<edm::InputTag>("MCEvent"));
207 
208  simTracks_token_ = collector.consumes<std::vector<SimTrack>>(conf.getParameter<edm::InputTag>("SimTracks"));
209  simVertices_token_ = collector.consumes<std::vector<SimVertex>>(conf.getParameter<edm::InputTag>("SimVertices"));
210 
211  tree.Branch("vtx_x", &vtx_x_);
212  tree.Branch("vtx_y", &vtx_y_);
213  tree.Branch("vtx_z", &vtx_z_);
214 
215  tree.Branch("gen_eta", &gen_eta_);
216  tree.Branch("gen_phi", &gen_phi_);
217  tree.Branch("gen_pt", &gen_pt_);
218  tree.Branch("gen_energy", &gen_energy_);
219  tree.Branch("gen_charge", &gen_charge_);
220  tree.Branch("gen_pdgid", &gen_pdgid_);
221  tree.Branch("gen_status", &gen_status_);
222  tree.Branch("gen_daughters", &gen_daughters_);
223 
224  tree.Branch("genpart_eta", &genpart_eta_);
225  tree.Branch("genpart_phi", &genpart_phi_);
226  tree.Branch("genpart_pt", &genpart_pt_);
227  tree.Branch("genpart_energy", &genpart_energy_);
228  tree.Branch("genpart_dvx", &genpart_dvx_);
229  tree.Branch("genpart_dvy", &genpart_dvy_);
230  tree.Branch("genpart_dvz", &genpart_dvz_);
231  tree.Branch("genpart_ovx", &genpart_ovx_);
232  tree.Branch("genpart_ovy", &genpart_ovy_);
233  tree.Branch("genpart_ovz", &genpart_ovz_);
234  tree.Branch("genpart_mother", &genpart_mother_);
235  tree.Branch("genpart_exphi", &genpart_exphi_);
236  tree.Branch("genpart_exeta", &genpart_exeta_);
237  tree.Branch("genpart_exx", &genpart_exx_);
238  tree.Branch("genpart_exy", &genpart_exy_);
239  tree.Branch("genpart_fbrem", &genpart_fbrem_);
240  tree.Branch("genpart_pid", &genpart_pid_);
241  tree.Branch("genpart_gen", &genpart_gen_);
242  tree.Branch("genpart_reachedEE", &genpart_reachedEE_);
243  tree.Branch("genpart_fromBeamPipe", &genpart_fromBeamPipe_);
244  tree.Branch("genpart_posx", &genpart_posx_);
245  tree.Branch("genpart_posy", &genpart_posy_);
246  tree.Branch("genpart_posz", &genpart_posz_);
247 }
248 
250  clear();
251 
253  iEvent.getByToken(gen_PU_token_, PupInfo_h);
254  const std::vector<PileupSummaryInfo> &PupInfo = *PupInfo_h;
255 
256  mySimEvent_->initializePdt(&(*es.pdt));
257  aField_ = &(*es.magfield);
259 
260  // This balck magic is needed to use the mySimEvent_
262  edm::Handle<std::vector<SimTrack>> simTracksHandle;
263  edm::Handle<std::vector<SimVertex>> simVerticesHandle;
264 
265  iEvent.getByToken(hepmcev_token_, hevH);
266  iEvent.getByToken(simTracks_token_, simTracksHandle);
267  iEvent.getByToken(simVertices_token_, simVerticesHandle);
268  mySimEvent_->fill(*simTracksHandle, *simVerticesHandle);
269 
270  HepMC::GenVertex *primaryVertex = *(hevH)->GetEvent()->vertices_begin();
271  const float mm2cm = 0.1;
272  vtx_x_ = primaryVertex->position().x() * mm2cm; // to put in official units
273  vtx_y_ = primaryVertex->position().y() * mm2cm;
274  vtx_z_ = primaryVertex->position().z() * mm2cm;
275 
277  toHGCalPropagator.setPropagationTargetZ(triggerTools_.getLayerZ(1));
278  std::vector<FSimTrack *> allselectedgentracks;
279  const float eeInnerRadius = 25.;
280  const float eeOuterRadius = 160.;
281  unsigned int npart = mySimEvent_->nTracks();
282  for (unsigned int i = 0; i < npart; ++i) {
283  std::vector<float> xp, yp, zp;
284  FSimTrack &myTrack(mySimEvent_->track(i));
285  math::XYZTLorentzVectorD vtx(0, 0, 0, 0);
286 
287  int reachedEE = ReachHGCal::notReach; // compute the extrapolations for the particles reaching EE
288  // and for the gen particles
289  double fbrem = -1;
290 
291  if (std::abs(myTrack.vertex().position().z()) >= triggerTools_.getLayerZ(1))
292  continue;
293 
294  const unsigned nlayers = triggerTools_.lastLayerBH();
295  if (myTrack.noEndVertex()) // || myTrack.genpartIndex()>=0)
296  {
297  HGCal_helpers::Coordinates propcoords;
298  bool reachesHGCal =
299  toHGCalPropagator.propagate(myTrack.momentum(), myTrack.vertex().position(), myTrack.charge(), propcoords);
300  vtx = propcoords.toVector();
301 
302  if (reachesHGCal && vtx.Rho() < eeOuterRadius && vtx.Rho() > eeInnerRadius) {
303  reachedEE = ReachHGCal::onEESurface;
304  double dpt = 0;
305 
306  for (int i = 0; i < myTrack.nDaughters(); ++i)
307  dpt += myTrack.daughter(i).momentum().pt();
308  if (abs(myTrack.type()) == 11)
309  fbrem = dpt / myTrack.momentum().pt();
310  } else if (reachesHGCal && vtx.Rho() > eeOuterRadius)
311  reachedEE = ReachHGCal::outsideEESurface;
312 
313  HGCal_helpers::SimpleTrackPropagator indiv_particleProp(aField_);
314  for (unsigned il = 1; il <= nlayers; ++il) {
315  const float charge = myTrack.charge();
316  indiv_particleProp.setPropagationTargetZ(triggerTools_.getLayerZ(il));
317  HGCal_helpers::Coordinates propCoords;
318  indiv_particleProp.propagate(myTrack.momentum(), myTrack.vertex().position(), charge, propCoords);
319 
320  xp.push_back(propCoords.x);
321  yp.push_back(propCoords.y);
322  zp.push_back(propCoords.z);
323  }
324  } else {
325  vtx = myTrack.endVertex().position();
326  }
327  auto orig_vtx = myTrack.vertex().position();
328 
329  allselectedgentracks.push_back(&mySimEvent_->track(i));
330  // fill branches
331  genpart_eta_.push_back(myTrack.momentum().eta());
332  genpart_phi_.push_back(myTrack.momentum().phi());
333  genpart_pt_.push_back(myTrack.momentum().pt());
334  genpart_energy_.push_back(myTrack.momentum().energy());
335  genpart_dvx_.push_back(vtx.x());
336  genpart_dvy_.push_back(vtx.y());
337  genpart_dvz_.push_back(vtx.z());
338 
339  genpart_ovx_.push_back(orig_vtx.x());
340  genpart_ovy_.push_back(orig_vtx.y());
341  genpart_ovz_.push_back(orig_vtx.z());
342 
343  HGCal_helpers::Coordinates hitsHGCal;
344  toHGCalPropagator.propagate(myTrack.momentum(), orig_vtx, myTrack.charge(), hitsHGCal);
345 
346  genpart_exphi_.push_back(hitsHGCal.phi);
347  genpart_exeta_.push_back(hitsHGCal.eta);
348  genpart_exx_.push_back(hitsHGCal.x);
349  genpart_exy_.push_back(hitsHGCal.y);
350 
351  genpart_fbrem_.push_back(fbrem);
352  genpart_pid_.push_back(myTrack.type());
353  genpart_gen_.push_back(myTrack.genpartIndex());
354  genpart_reachedEE_.push_back(reachedEE);
355  genpart_fromBeamPipe_.push_back(true);
356 
357  genpart_posx_.push_back(xp);
358  genpart_posy_.push_back(yp);
359  genpart_posz_.push_back(zp);
360  }
361 
362  edm::Handle<std::vector<reco::GenParticle>> genParticlesHandle;
363  iEvent.getByToken(gen_token_, genParticlesHandle);
364  gen_n_ = genParticlesHandle->size();
365 
366  for (const auto &particle : *genParticlesHandle) {
367  gen_eta_.push_back(particle.eta());
368  gen_phi_.push_back(particle.phi());
369  gen_pt_.push_back(particle.pt());
370  gen_energy_.push_back(particle.energy());
371  gen_charge_.push_back(particle.charge());
372  gen_pdgid_.push_back(particle.pdgId());
373  gen_status_.push_back(particle.status());
374  std::vector<int> daughters(particle.daughterRefVector().size(), 0);
375  for (unsigned j = 0; j < particle.daughterRefVector().size(); ++j) {
376  daughters[j] = static_cast<int>(particle.daughterRefVector().at(j).key());
377  }
378  gen_daughters_.push_back(daughters);
379  }
380 
381  // associate gen particles to mothers
382  genpart_mother_.resize(genpart_posz_.size(), -1);
383  for (size_t i = 0; i < allselectedgentracks.size(); i++) {
384  const auto tracki = allselectedgentracks.at(i);
385 
386  for (size_t j = i + 1; j < allselectedgentracks.size(); j++) {
387  const auto trackj = allselectedgentracks.at(j);
388 
389  if (!tracki->noMother()) {
390  if (&tracki->mother() == trackj)
391  genpart_mother_.at(i) = j;
392  }
393  if (!trackj->noMother()) {
394  if (&trackj->mother() == tracki)
395  genpart_mother_.at(j) = i;
396  }
397  }
398  }
399 
400  for (const auto &PVI : PupInfo) {
401  if (PVI.getBunchCrossing() == 0) {
402  gen_PUNumInt_ = PVI.getPU_NumInteractions();
403  gen_TrueNumInt_ = PVI.getTrueNumInteractions();
404  }
405  }
406 }
407 
409  gen_n_ = 0;
410  gen_PUNumInt_ = 0;
411  gen_TrueNumInt_ = 0.;
412 
413  vtx_x_ = 0;
414  vtx_y_ = 0;
415  vtx_z_ = 0;
416 
417  //
418  genpart_eta_.clear();
419  genpart_phi_.clear();
420  genpart_pt_.clear();
421  genpart_energy_.clear();
422  genpart_dvx_.clear();
423  genpart_dvy_.clear();
424  genpart_dvz_.clear();
425  genpart_ovx_.clear();
426  genpart_ovy_.clear();
427  genpart_ovz_.clear();
428  genpart_exx_.clear();
429  genpart_exy_.clear();
430  genpart_mother_.clear();
431  genpart_exphi_.clear();
432  genpart_exeta_.clear();
433  genpart_fbrem_.clear();
434  genpart_pid_.clear();
435  genpart_gen_.clear();
436  genpart_reachedEE_.clear();
437  genpart_fromBeamPipe_.clear();
438  genpart_posx_.clear();
439  genpart_posy_.clear();
440  genpart_posz_.clear();
441 
443  // reco::GenParticles
444  //
445  gen_eta_.clear();
446  gen_phi_.clear();
447  gen_pt_.clear();
448  gen_energy_.clear();
449  gen_charge_.clear();
450  gen_pdgid_.clear();
451  gen_status_.clear();
452  gen_daughters_.clear();
453 }
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
const MagneticField * aField_
std::vector< int > genpart_mother_
std::vector< std::vector< int > > gen_daughters_
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
uint16_t *__restrict__ id
edm::ESHandle< MagneticField > magfield
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T y() const
Definition: PV3DBase.h:60
std::vector< std::vector< float > > genpart_posy_
GlobalPoint globalPosition() const
double npart
Definition: HydjetWrapper.h:46
std::vector< float > genpart_phi_
float getLayerZ(const unsigned &layerWithOffset) const
edm::ESHandle< HGCalTriggerGeometryBase > geometry
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
void initialize(TTree &, const edm::ParameterSet &, edm::ConsumesCollector &&) final
void setGeometry(const HGCalTriggerGeometryBase *const)
std::vector< float > gen_energy_
std::vector< float > genpart_exy_
std::unique_ptr< FSimEvent > mySimEvent_
edm::EDGetToken simVertices_token_
std::vector< float > gen_eta_
std::vector< std::vector< float > > genpart_posz_
int iEvent
Definition: GenABIO.cc:224
std::vector< int > gen_status_
std::vector< float > genpart_dvy_
std::vector< int > genpart_reachedEE_
std::vector< int > gen_pdgid_
std::vector< float > genpart_exx_
std::vector< float > gen_phi_
static PlanePointer build(Args &&...args)
Definition: Plane.h:33
edm::ESHandle< HepPDT::ParticleDataTable > pdt
T z() const
Definition: PV3DBase.h:61
std::vector< int > genpart_pid_
std::vector< int > gen_charge_
std::vector< bool > genpart_fromBeamPipe_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< float > genpart_fbrem_
std::vector< float > genpart_eta_
std::vector< float > genpart_pt_
std::vector< float > genpart_ovz_
const RKPropagatorInS & RKProp() const
unsigned lastLayerBH() const
HGCalTriggerTools triggerTools_
TrajectoryStateOnSurface TSOS
Definition: TestHits.cc:19
std::vector< std::vector< float > > genpart_posx_
std::vector< float > genpart_ovy_
T const * product() const
Definition: ESHandle.h:86
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< float > gen_pt_
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
std::vector< float > genpart_exphi_
bool propagate(const double px, const double py, const double pz, const double x, const double y, const double z, const float charge, Coordinates &coords) const
T eta() const
Definition: PV3DBase.h:73
std::vector< float > genpart_energy_
math::XYZTLorentzVectorD toVector()
static int position[264][3]
Definition: ReadPGInfo.cc:289
std::vector< float > genpart_ovx_
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< float > genpart_exeta_
void fill(const edm::Event &, const HGCalTriggerNtupleEventSetup &) final
HGCalTriggerNtupleGen(const edm::ParameterSet &)
std::vector< float > genpart_dvz_
std::vector< int > genpart_gen_
T x() const
Definition: PV3DBase.h:59
std::vector< float > genpart_dvx_