CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MixEvtVtxGenerator.cc
Go to the documentation of this file.
1 #ifndef HI_MixEvtVtxGenerator_H
2 #define HI_MixEvtVtxGenerator_H
3 /*
4 * $Date: 2013/04/18 21:57:33 $
5 * $Revision: 1.7 $
6 */
9 
17 
21 
24 
25 #include "TMatrixD.h"
26 
27 using namespace edm;
28 using namespace std;
29 
30 
31 namespace HepMC {
32  class FourVector ;
33 }
34 
35 
37 {
38  public:
39 
40  // ctor & dtor
41  explicit MixEvtVtxGenerator( const edm::ParameterSet& );
42  virtual ~MixEvtVtxGenerator();
43 
44  virtual void produce( edm::Event&, const edm::EventSetup& );
45 
46  virtual HepMC::FourVector* getVertex(edm::Event&);
47  virtual HepMC::FourVector* getRecVertex(edm::Event&);
48 
49  protected:
50 
51  HepMC::FourVector* fVertex ;
52  TMatrixD *boost_;
53 
54  private :
55 
59 
61  std::vector<double> vtxOffset;
62  bool useCF_;
63 
64 };
65 
67  : fVertex(0), boost_(0),
68  signalLabel(pset.getParameter<edm::InputTag>("signalLabel")),
69  hiLabel(pset.getParameter<edm::InputTag>("heavyIonLabel")),
70  useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
71 
72 {
73  produces<bool>("matchedVertex");
74  vtxOffset.resize(3);
75  if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
76 
77  if(useRecVertex) useCF_ = 0;
78  else{
79  useCF_ = pset.getUntrackedParameter<bool>("useCF",false);
80  cfLabel = pset.getUntrackedParameter<edm::InputTag>("mixLabel",edm::InputTag("mixGen","generator"));
81  }
82 }
83 
85 {
86  delete fVertex ;
87  if (boost_ != 0 ) delete boost_;
88  // no need since now it's done in HepMCProduct
89  // delete fEvt ;
90 }
91 
92 HepMC::FourVector* MixEvtVtxGenerator::getVertex( Event& evt){
93 
94  HepMC::GenVertex* genvtx = 0;
95  const HepMC::GenEvent* inev = 0;
96 
97  //cout<<" use CF "<<useCF_<<endl;
98 
99  if(useCF_){
101  evt.getByLabel(cfLabel,cf);
103  if(mix.size() < 2){
104  cout<<"Less than 2 sub-events, mixing seems to have failed!"<<endl;
105  }
106  const HepMCProduct& bkg = mix.getObject(1);
107  if(!(bkg.isVtxGenApplied())){
108  cout<<"Input does not have smeared vertex!"<<endl;
109  }else{
110  inev = bkg.GetEvent();
111  }
112  }else{
113  //cout<<" hiLabel "<<hiLabel<<endl;
115  evt.getByLabel(hiLabel,input);
116  inev = input->GetEvent();
117  }
118 
119  genvtx = inev->signal_process_vertex();
120  if(!genvtx){
121  //cout<<"No Signal Process Vertex!"<<endl;
122  HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
123  HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
124  while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
125  //if(!genvtx) cout<<"No Gen Vertex!"<<endl;
126  if(pt == ptend) cout<<"End reached, No Gen Vertex!"<<endl;
127  genvtx = (*pt)->production_vertex();
128  ++pt;
129  }
130  }
131  double aX,aY,aZ,aT;
132 
133  aX = genvtx->position().x();
134  aY = genvtx->position().y();
135  aZ = genvtx->position().z();
136  aT = genvtx->position().t();
137 
138  if(!fVertex){
139  fVertex = new HepMC::FourVector();
140  //cout<<" creating new vertex "<<endl;
141  }
142  //cout<<" setting vertex "<<" aX "<<aX<<" aY "<<aY<<" aZ "<<aZ<<" aT "<<aT<<endl;
143  fVertex->set(aX,aY,aZ,aT);
144 
145 
146  return fVertex;
147 
148 }
149 
150 
151 HepMC::FourVector* MixEvtVtxGenerator::getRecVertex( Event& evt){
152 
154  evt.getByLabel(hiLabel,input);
155 
156  double aX,aY,aZ;
157 
158  aX = input->begin()->position().x() + vtxOffset[0];
159  aY = input->begin()->position().y() + vtxOffset[1];
160  aZ = input->begin()->position().z() + vtxOffset[2];
161 
162  /*
163  std::cout << "reco::Vertex = " << input->begin()->position().x()
164  << ", " << input->begin()->position().y()
165  << ", " << input->begin()->position().z()
166  << std::endl;
167 
168  std::cout << "offset = " << vtxOffset[0]
169  << ", " << vtxOffset[1]
170  << ", " << vtxOffset[2]
171  << std::endl;
172 
173  std::cout << "embedded GEN vertex = " << aX
174  << ", " << aY << ", " << aZ << std::endl;
175 
176  */
177  if(!fVertex) fVertex = new HepMC::FourVector();
178  fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
179 
180  return fVertex;
181 
182 }
183 
185 {
186 
187 
188  Handle<HepMCProduct> HepMCEvt ;
189 
190  evt.getByLabel( signalLabel, HepMCEvt ) ;
191 
192  // generate new vertex & apply the shift
193  //
194 
195  HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
196 
197  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
198  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
199 
200  // OK, create a (pseudo)product and put in into edm::Event
201  //
202  auto_ptr<bool> NewProduct(new bool(true)) ;
203  evt.put( NewProduct ,"matchedVertex") ;
204 
205  return ;
206 
207 }
208 
210 
211 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
virtual HepMC::FourVector * getRecVertex(edm::Event &)
std::vector< double > vtxOffset
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
MixEvtVtxGenerator(const edm::ParameterSet &)
virtual HepMC::FourVector * getVertex(edm::Event &)
edm::InputTag signalLabel
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:35
HepMC::FourVector * fVertex
bool isVtxGenApplied() const
Definition: HepMCProduct.h:37
T const * product() const
Definition: Handle.h:74
virtual void produce(edm::Event &, const edm::EventSetup &)
tuple cout
Definition: gather_cfg.py:121