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
electrons_cff.bool
bool
Definition: electrons_cff.py:393
input
static const std::string input
Definition: EdmProvDump.cc:48
funct::false
false
Definition: Factorize.h:29
edm::Handle::product
T const * product() const
Definition: Handle.h:70
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
MixEvtVtxGenerator
Definition: MixEvtVtxGenerator.cc:33
edm::EDGetTokenT< reco::VertexCollection >
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
MixEvtVtxGenerator::fVertex
HepMC::FourVector * fVertex
Definition: MixEvtVtxGenerator.cc:45
EDProducer.h
MixEvtVtxGenerator::boost_
TMatrixD * boost_
Definition: MixEvtVtxGenerator.cc:46
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::Handle
Definition: AssociativeIterator.h:50
MixEvtVtxGenerator::~MixEvtVtxGenerator
~MixEvtVtxGenerator() override
Definition: MixEvtVtxGenerator.cc:78
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
EDMException.h
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MixCollection.h
MixCollection
Definition: MixCollection.h:11
Service.h
MixEvtVtxGenerator::cfLabel
edm::EDGetTokenT< CrossingFrame< HepMCProduct > > cfLabel
Definition: MixEvtVtxGenerator.cc:51
Provenance.h
MixEvtVtxGenerator::getVertex
virtual HepMC::FourVector * getVertex(edm::Event &)
Definition: MixEvtVtxGenerator.cc:86
MixEvtVtxGenerator::useRecVertex
bool useRecVertex
Definition: MixEvtVtxGenerator.cc:53
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:531
Vertex.h
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
MixEvtVtxGenerator::MixEvtVtxGenerator
MixEvtVtxGenerator(const edm::ParameterSet &)
Definition: MixEvtVtxGenerator.cc:58
ModuleDef.h
MixEvtVtxGenerator::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: MixEvtVtxGenerator.cc:155
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:57
edm::HepMCProduct::GetEvent
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
edm::HepMCProduct::isVtxGenApplied
bool isVtxGenApplied() const
Definition: HepMCProduct.h:36
MixEvtVtxGenerator::vtxOffset
std::vector< double > vtxOffset
Definition: MixEvtVtxGenerator.cc:54
InputTag.h
MixEvtVtxGenerator::hiLabel
edm::EDGetTokenT< reco::VertexCollection > hiLabel
Definition: MixEvtVtxGenerator.cc:49
VertexFwd.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
MixEvtVtxGenerator::signalLabel
edm::EDGetTokenT< HepMCProduct > signalLabel
Definition: MixEvtVtxGenerator.cc:50
MatchRecVtx_cfi.useRecVertex
useRecVertex
Definition: MatchRecVtx_cfi.py:10
HepMC
Definition: GenParticle.h:15
Exception
Definition: hltDiff.cc:246
Exception.h
cms::Exception
Definition: Exception.h:70
edm::HepMCProduct
Definition: HepMCProduct.h:18
ParameterSet.h
HepMCProduct.h
edm::Event
Definition: Event.h:73
GeneratorMix_cff.mix
mix
Definition: GeneratorMix_cff.py:6
edm::InputTag
Definition: InputTag.h:15
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
MixEvtVtxGenerator::getRecVertex
virtual HepMC::FourVector * getRecVertex(edm::Event &)
Definition: MixEvtVtxGenerator.cc:138
MixEvtVtxGenerator::useCF_
bool useCF_
Definition: MixEvtVtxGenerator.cc:55