CMS 3D CMS Logo

MixEvtVtxGenerator.cc
Go to the documentation of this file.
1 #ifndef HI_MixEvtVtxGenerator_H
2 #define HI_MixEvtVtxGenerator_H
3 /*
4 */
7 
15 
19 
22 
23 #include "TMatrixD.h"
24 #include <iostream>
25 
26 using namespace edm;
27 using namespace std;
28 
29 namespace HepMC {
30  class FourVector;
31 }
32 
34 public:
35  // ctor & dtor
36  explicit MixEvtVtxGenerator(const edm::ParameterSet&);
37  ~MixEvtVtxGenerator() override;
38 
39  void produce(edm::Event&, const edm::EventSetup&) override;
40 
41  virtual HepMC::FourVector* getVertex(edm::Event&);
42  virtual HepMC::FourVector* getRecVertex(edm::Event&);
43 
44 protected:
45  HepMC::FourVector* fVertex;
46  TMatrixD* boost_;
47 
48 private:
52 
54  std::vector<double> vtxOffset;
55  bool useCF_;
56 };
57 
59  : fVertex(nullptr),
60  boost_(nullptr),
61  useRecVertex(pset.exists("useRecVertex") ? pset.getParameter<bool>("useRecVertex") : false)
62 
63 {
64  produces<edm::HepMCProduct>();
65  vtxOffset.resize(3);
66  if (pset.exists("vtxOffset"))
67  vtxOffset = pset.getParameter<std::vector<double> >("vtxOffset");
68 
69  if (useRecVertex)
70  useCF_ = false;
71  else {
72  useCF_ = pset.getUntrackedParameter<bool>("useCF", false);
73  cfLabel = consumes<CrossingFrame<HepMCProduct> >(pset.getParameter<edm::InputTag>("mixLabel"));
74  }
75  signalLabel = consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("signalLabel"));
76 }
77 
79  delete fVertex;
80  if (boost_ != nullptr)
81  delete boost_;
82  // no need since now it's done in HepMCProduct
83  // delete fEvt ;
84 }
85 
86 HepMC::FourVector* MixEvtVtxGenerator::getVertex(Event& evt) {
87  HepMC::GenVertex* genvtx = nullptr;
88  const HepMC::GenEvent* inev = nullptr;
89 
90  if (useCF_) {
92  evt.getByToken(cfLabel, cf);
94  if (mix.size() < 2) {
95  throw cms::Exception("MatchVtx") << "Mixing has " << mix.size() << " sub-events, should have been at least 2"
96  << endl;
97  }
98  const HepMCProduct& bkg = mix.getObject(1);
99  if (!(bkg.isVtxGenApplied())) {
100  throw cms::Exception("MatchVtx") << "Input background does not have smeared vertex!" << endl;
101  } else {
102  inev = bkg.GetEvent();
103  }
104  } else {
107  inev = input->GetEvent();
108  }
109 
110  genvtx = inev->signal_process_vertex();
111  if (!genvtx) {
112  HepMC::GenEvent::particle_const_iterator pt = inev->particles_begin();
113  HepMC::GenEvent::particle_const_iterator ptend = inev->particles_end();
114  while (!genvtx || (genvtx->particles_in_size() == 1 && pt != ptend)) {
115  if (pt == ptend)
116  cout << "End reached, No Gen Vertex!" << endl;
117  genvtx = (*pt)->production_vertex();
118  ++pt;
119  }
120  }
121  double aX, aY, aZ, aT;
122 
123  aX = genvtx->position().x();
124  aY = genvtx->position().y();
125  aZ = genvtx->position().z();
126  aT = genvtx->position().t();
127 
128  if (!fVertex) {
129  fVertex = new HepMC::FourVector();
130  }
131  LogInfo("MatchVtx") << " setting vertex "
132  << " aX " << aX << " aY " << aY << " aZ " << aZ << " aT " << aT << endl;
133  fVertex->set(aX, aY, aZ, aT);
134 
135  return fVertex;
136 }
137 
138 HepMC::FourVector* MixEvtVtxGenerator::getRecVertex(Event& evt) {
140  evt.getByToken(hiLabel, input);
141 
142  double aX, aY, aZ;
143 
144  aX = input->begin()->position().x() + vtxOffset[0];
145  aY = input->begin()->position().y() + vtxOffset[1];
146  aZ = input->begin()->position().z() + vtxOffset[2];
147 
148  if (!fVertex)
149  fVertex = new HepMC::FourVector();
150  fVertex->set(10.0 * aX, 10.0 * aY, 10.0 * aZ, 0.0); // HepMC positions in mm (RECO in cm)
151 
152  return fVertex;
153 }
154 
156  Handle<HepMCProduct> HepUnsmearedMCEvt;
157 
158  evt.getByToken(signalLabel, HepUnsmearedMCEvt);
159 
160  // generate new vertex & apply the shift
161  //
162  if (HepUnsmearedMCEvt->isVtxGenApplied())
163  throw cms::Exception("MatchVtx")
164  << "Signal HepMCProduct is not compatible for embedding - it's vertex is already smeared." << std::endl;
165  // Copy the HepMC::GenEvent
166  HepMC::GenEvent* genevt = new HepMC::GenEvent(*HepUnsmearedMCEvt->GetEvent());
167  std::unique_ptr<edm::HepMCProduct> HepMCEvt(new edm::HepMCProduct(genevt));
168  // generate new vertex & apply the shift
169  //
170  HepMCEvt->applyVtxGen(useRecVertex ? getRecVertex(evt) : getVertex(evt));
171 
172  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
173  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
174 
175  evt.put(std::move(HepMCEvt));
176 
177  return;
178 }
179 
181 
182 #endif
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
virtual HepMC::FourVector * getRecVertex(edm::Event &)
bool isVtxGenApplied() const
Definition: HepMCProduct.h:39
std::vector< double > vtxOffset
T const * product() const
Definition: Handle.h:70
MixEvtVtxGenerator(const edm::ParameterSet &)
void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:526
virtual HepMC::FourVector * getVertex(edm::Event &)
static std::string const input
Definition: EdmProvDump.cc:50
edm::EDGetTokenT< CrossingFrame< HepMCProduct > > cfLabel
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
Log< level::Info, false > LogInfo
edm::EDGetTokenT< reco::VertexCollection > hiLabel
edm::EDGetTokenT< HepMCProduct > signalLabel
HepMC::FourVector * fVertex
HLT enums.
~MixEvtVtxGenerator() override
def move(src, dest)
Definition: eostools.py:511