17 #include <TProcessID.h>
36 #include <HepMC3/ReaderAsciiHepMC2.h>
37 #include <HepMC3/ReaderAscii.h>
38 #include "HepMC3/GenVertex.h"
42 #include <HepMC3/Version.h>
43 #if HEPMC3_VERSION_CODE < 3002004
46 #include <HepMC3/ReaderFactory.h>
50 #include <TParticlePDG.h>
51 #include <TLorentzVector.h>
52 #include <TDatabasePDG.h>
53 #include <TObjArray.h>
54 #include <TObjString.h>
69 struct TProcessIdObjectCount {
72 count = TProcessID::GetObjectCount();
79 TProcessID::SetObjectCount(
count);
101 HepMC3::GenEvent evt(HepMC3::Units::GEV,HepMC3::Units::MM);
102 adapter->read_event(evt);
103 if ( adapter->failed() )
return false;
121 std::map < HepMC3::GenParticlePtr, int > hepmcp_index;
122 hepmcp_index.clear();
126 auto beams = evt.beams();
132 assert ( beams.size() == 2 );
133 auto hadron = beams.at(0);
134 auto lepton = beams.at(1);
139 if ( hadron->pid()==22 || lepton->pid()==22 ){
140 if ( evt.particles().size() > 2 ){
141 cout <<
"Warning: Treating the event as beam gas but found "
142 << evt.particles().size() <<
" particles" << endl;
145 if ( lepton->pid()==22 ) {
151 auto beam_e_mom = lepton->momentum() + photon->momentum();
156 auto beam_e = std::make_shared<HepMC3::GenParticle>( beam_e_mom, 11, 21 );
157 auto v = lepton->production_vertex();
158 v->add_particle_in (beam_e);
161 auto pz_p = -10.0 *beam_e_mom.z();
162 auto e_p = sqrt ( pow(pz_p,2) + pow(0.938,2) );
163 HepMC3::FourVector beam_p_mom ( 0,0,pz_p, e_p);
164 auto beam_p = std::make_shared<HepMC3::GenParticle>( beam_p_mom, 2212, 21 );
179 auto pdgl = TDatabasePDG::Instance()->GetParticle( lepton->pid() );
180 auto pdgh = TDatabasePDG::Instance()->GetParticle( hadron->pid() );
183 bool b0islepton = (string(pdgl->ParticleClass()) ==
"Lepton");
184 bool b1islepton = (string(pdgh->ParticleClass()) ==
"Lepton");
187 if ( b0islepton == b1islepton ){
208 HepMC3::GenParticlePtr scatteredlepton=0;
209 HepMC3::GenParticlePtr photon=0;
212 if ( lepton->children().size() >2 || lepton->children().size()==0 ){
213 cerr <<
"electron has " << lepton->children().size() <<
" daughters." << endl;
214 throw std::runtime_error (
"Wrong number of lepton daughters; should be 1 (lepton) or 2 (lepton+boson).");
218 if ( lepton->children().size() == 2 ){
219 scatteredlepton = lepton->children().at(0);
220 photon = lepton->children().at(1);
221 if ( scatteredlepton->pid() != 22 && photon->pid() != 22 ){
222 cerr <<
"lepton child 1 pid = " << scatteredlepton->pid() << endl;
223 cerr <<
"lepton child 2 pid = " << photon->pid() << endl;
224 throw std::runtime_error (
"Found two beam lepton daughters, none or both of them a photon.");
226 if ( photon->pid() != 22 ){
232 if ( lepton->children().size() == 1 ){
233 scatteredlepton = lepton->children().at(0);
234 auto pdgs = TDatabasePDG::Instance()->GetParticle( scatteredlepton->pid() );
235 if ( !TString(pdgs->ParticleClass()).Contains(
"Lepton") ){
236 throw std::runtime_error (
"Found one beam lepton daughter, and it is not a lepton.");
247 photon=std::make_shared<HepMC3::GenParticle>();
249 photon->set_pid( 0 );
250 photon->set_status( 0 );
252 scatteredlepton->production_vertex()->add_particle_out(photon);
264 bool foundbranch=
true;
265 while ( scatteredlepton->children().size() > 0 ){
267 throw std::runtime_error (
"none of this lepton's children is a lepton.");
270 for (
auto&
c : scatteredlepton->children() ){
271 auto pdgl = TDatabasePDG::Instance()->GetParticle(
c->pid() );
272 if (
string(pdgl->ParticleClass()) ==
"Lepton" ){
298 auto finished = FinishEvent();
299 return (finished==0);
301 catch(std::exception& error) {
302 std::cerr <<
"Exception building particle: " << error.what() << std::endl;
312 if (!AddParticle()) {
313 mEvent.reset(
nullptr);
316 return mEvent.release();
320 void HandleHepmcParticle(
const HepMC3::GenParticlePtr&
p, std::map < HepMC3::GenParticlePtr, int >& hepmcp_index,
int& particleindex, std::unique_ptr<erhic::EventHepMC>& mEvent ){
322 auto it = hepmcp_index.find(p);
323 if (
it != hepmcp_index.end() )
return;
327 auto v = p->production_vertex();
329 if (
v ) vertex = TVector3(
v->position().x(),
v->position().y(),
v->position().z());
334 TLorentzVector lovec(p->momentum().x(),p->momentum().y(),p->momentum().z(),p->momentum().e());
340 particle.
SetId( p->pid() );
344 particle.
SetIndex ( particleindex );
347 hepmcp_index[
p] = particleindex;
356 void HandleAllVertices( HepMC3::GenEvent& evt, std::map < HepMC3::GenParticlePtr, int >& hepmcp_index,
int& particleindex, std::unique_ptr<erhic::EventHepMC>& mEvent ){
360 for (
auto&
v : evt.vertices() ){
361 for (
auto&
p :
v->particles_out() ) {
372 for (
auto&
v : evt.vertices() ){
373 for (
auto&
p :
v->particles_out() ) {
375 int treeindex = hepmcp_index[
p]-1;
376 assert ( treeindex >=0 );
377 auto track = mEvent->
GetTrack(treeindex);
380 vector<int> allparents;
381 for (
auto& parent :
p->parents() ) {
382 allparents.push_back( hepmcp_index[parent] );
387 if ( allparents.size() == 1){
388 track->SetParentIndex( allparents.at(0) );
390 if ( allparents.size() >= 2){
392 track->SetParentIndex( *min_element(allparents.begin(), allparents.end() ) );
393 track->SetParentIndex1( *max_element(allparents.begin(), allparents.end() ) );
397 vector<int> allchildren;
398 for (
auto& child :
p->children() ) {
399 allchildren.push_back( hepmcp_index[child] );
401 if ( allchildren.size() == 1){
402 track->SetChild1Index( allchildren.at(0) );
404 if ( allchildren.size() >= 2){
406 track->SetChild1Index( *min_element(allchildren.begin(), allchildren.end() ) );
407 track->SetChildNIndex( *max_element(allchildren.begin(), allchildren.end() ) );
415 void UpdateRuninfo( std::vector<VirtualEventFactory::NamedObjects>& mObjectsToWriteAtTheEnd,
416 const HepMC3::GenEvent& evt ){
420 TString xsecerr =
"";
421 if (
auto xsecinfo=evt.cross_section() ){
423 xsec += xsecinfo->xsec();
424 xsecerr += xsecinfo->xsec_err();
435 if ( mObjectsToWriteAtTheEnd.size() == 0 ){
436 Xsec =
new TObjString;
437 Xsecerr =
new TObjString;
445 Xsec = (TObjString*) mObjectsToWriteAtTheEnd.at(0).second;
446 Xsec->String() = xsec;
447 Xsecerr = (TObjString*) mObjectsToWriteAtTheEnd.at(1).second;
448 Xsecerr->String() = xsecerr;