22 #include "GaudiKernel/ThreadLocalContext.h"
23 static const std::set<std::string> attributes_to_ignore {
25 "flows",
"flow1",
"flow2",
"flow3",
30 "filterWeight",
"filterHT",
"filterMET",
31 "event_scale",
"alphaQCD",
"alphaQED",
"random_states",
"weights",
32 "GenCrossSection",
"GenPdfInfo",
"GenHeavyIon"};
40 m_isPileup(false),m_hepMCWeightSvc(
"HepMCWeightSvc",
"McEventCollectionCnv_p7")
45 m_isPileup(false),m_hepMCWeightSvc(
"HepMCWeightSvc",
"McEventCollectionCnv_p7")
70 const EventContext& ctx = Gaudi::Hive::currentContext();
72 msg <<
MSG::DEBUG <<
"Loading McEventCollection from persistent state..."
83 datapools.
part.prepareToAdd(nParts);
84 const unsigned int nEvts = persObj->
m_genEvents.size();
85 datapools.
evt.prepareToAdd(nEvts);
88 for ( std::vector<GenEvent_p7>::const_iterator
94 HepMC::GenEvent * genEvt(
nullptr);
96 genEvt =
new HepMC::GenEvent();
101 genEvt->add_attribute (
"barcodes", std::make_shared<HepMC::GenEventBarcodes>());
108 genEvt->add_attribute(
"signal_process_id", std::make_shared<HepMC3::IntAttribute>(persEvt.
m_signalProcessId));
110 genEvt->add_attribute(
"mpi", std::make_shared<HepMC3::IntAttribute>(persEvt.
m_mpi));
111 genEvt->add_attribute(
"event_scale", std::make_shared<HepMC3::DoubleAttribute>(persEvt.
m_eventScale));
112 genEvt->add_attribute(
"alphaQCD", std::make_shared<HepMC3::DoubleAttribute>(persEvt.
m_alphaQCD));
113 genEvt->add_attribute(
"alphaQED", std::make_shared<HepMC3::DoubleAttribute>(persEvt.
m_alphaQED));
114 genEvt->add_attribute(
"filterWeight", std::make_shared<HepMC3::DoubleAttribute>(persEvt.
m_filterWeight));
115 genEvt->add_attribute(
"filterHT", std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_filterHT));
116 genEvt->add_attribute(
"filterMET", std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_filterMET));
118 genEvt->add_attribute(
"random_states", std::make_shared<HepMC3::VectorLongIntAttribute>(persEvt.
m_randomStates));
120 genEvt->set_units(
static_cast<HepMC3::Units::MomentumUnit
>(persEvt.
m_momentumUnit),
121 static_cast<HepMC3::Units::LengthUnit
>(persEvt.
m_lengthUnit));
124 if(!genEvt->run_info()) {
125 HepMC3::GenRunInfoData ri_read;
132 auto ri = std::make_shared<HepMC3::GenRunInfo>();
133 ri->read_data(ri_read);
134 genEvt->set_run_info(std::move(ri));
139 auto cs = std::make_shared<HepMC3::GenCrossSection>();
141 if(
static_cast<bool>(xsection[0]) )
142 cs->set_cross_section(xsection[2],xsection[1]);
144 cs->set_cross_section(-1.0, -1.0);
145 genEvt->set_cross_section(std::move(cs));
150 auto hi = std::make_shared<HepMC3::GenHeavyIon>();
151 const std::vector<float>& hIon = persEvt.
m_heavyIon;
154 static_cast<int>(hIon[12]),
155 static_cast<int>(hIon[11]),
156 static_cast<int>(hIon[10]),
157 static_cast<int>(hIon[9]),
158 static_cast<int>(hIon[8]),
159 static_cast<int>(hIon[7]),
160 static_cast<int>(hIon[6]),
161 static_cast<int>(hIon[5]),
162 static_cast<int>(hIon[4]),
167 genEvt->set_heavy_ion(std::move(hi));
177 pi->set(
static_cast<int>(
pdf[8]),
178 static_cast<int>(
pdf[7]),
184 static_cast<int>(
pdf[6]),
185 static_cast<int>(
pdf[5]));
186 genEvt->set_pdf_info(std::move(
pi));
196 std::map<int, HepMC::GenVertexPtr> brc_to_vertex;
200 for (
unsigned int iVtx = persEvt.
m_verticesBegin; iVtx != endVtx; ++iVtx ) {
202 brc_to_vertex[persObj->
m_genVertices[iVtx].m_barcode] = std::move(vtx);
207 if ( sigProcVtx != 0 && brc_to_vertex.count(sigProcVtx) ) {
212 for (
auto &
p : partToEndVtx) {
213 if ( brc_to_vertex.count(
p.second) ) {
214 auto decayVtx = brc_to_vertex[
p.second];
215 decayVtx->add_particle_in(
p.first );
217 msg << MSG::ERROR <<
"GenParticle points to null end vertex !!" <<
endmsg;
223 if ( beamPart1 != 0 && beamPart2 != 0 ) {
231 genEvt->m_mpi = persEvt.
m_mpi;
235 genEvt->m_signal_process_vertex = 0;
236 genEvt->m_beam_particle_1 = 0;
237 genEvt->m_beam_particle_2 = 0;
240 genEvt->m_vertex_barcodes.clear();
241 genEvt->m_particle_barcodes.clear();
242 genEvt->m_momentum_unit =
static_cast<HepMC::Units::MomentumUnit
>(persEvt.
m_momentumUnit);
243 genEvt->m_position_unit =
static_cast<HepMC::Units::LengthUnit
>(persEvt.
m_lengthUnit);
249 if( genEvt->m_cross_section )
250 delete genEvt->m_cross_section;
251 genEvt->m_cross_section = 0;
254 genEvt->m_cross_section =
new HepMC::GenCrossSection();
256 if(
static_cast<bool>(xsection[0]) )
257 genEvt->m_cross_section->set_cross_section(xsection[2],xsection[1]);
261 if(genEvt->m_heavy_ion )
262 delete genEvt->m_heavy_ion;
263 genEvt->m_heavy_ion = 0;
265 const std::vector<float>& hIon = persEvt.
m_heavyIon;
266 genEvt->m_heavy_ion =
new HepMC::HeavyIon
268 static_cast<int>(hIon[12]),
269 static_cast<int>(hIon[11]),
270 static_cast<int>(hIon[10]),
271 static_cast<int>(hIon[9]),
272 static_cast<int>(hIon[8]),
273 static_cast<int>(hIon[7]),
274 static_cast<int>(hIon[6]),
275 static_cast<int>(hIon[5]),
276 static_cast<int>(hIon[4]),
286 if(genEvt->m_pdf_info)
287 delete genEvt->m_pdf_info;
288 genEvt->m_pdf_info = 0;
291 genEvt->m_pdf_info =
new HepMC::PdfInfo
293 static_cast<int>(
pdf[8]),
294 static_cast<int>(
pdf[7]),
300 static_cast<int>(
pdf[6]),
301 static_cast<int>(
pdf[5])
316 for (
unsigned int iVtx= persEvt.
m_verticesBegin; iVtx != endVtx; ++iVtx ) {
325 if ( sigProcVtx != 0 ) {
326 genEvt->set_signal_process_vertex( genEvt->barcode_to_vertex( sigProcVtx ) );
331 p = partToEndVtx.begin(),
332 endItr = partToEndVtx.end();
337 decayVtx->add_particle_in(
p->first );
340 <<
"GenParticle points to null end vertex !!"
348 if ( beamPart1 != 0 && beamPart2 !=0 ) {
349 genEvt->set_beam_particles(genEvt->barcode_to_particle(beamPart1),
350 genEvt->barcode_to_particle(beamPart2));
358 msg <<
MSG::DEBUG <<
"Loaded McEventCollection from persistent state [OK]"
366 const EventContext& ctx = Gaudi::Hive::currentContext();
368 msg <<
MSG::DEBUG <<
"Creating persistent state of McEventCollection..."
372 const std::pair<unsigned int,unsigned int>
stats = nbrParticlesAndVertices( transObj );
382 const HepMC::GenEvent* genEvt = *itr;
384 auto ri = genEvt->run_info();
386 HepMC3::GenRunInfoData ri_data;
388 ri->write_data(ri_data);
389 if (!ri_data.weight_names.empty()) {
390 m_hepMCWeightSvc->setWeightNames( names_to_name_index_map(ri_data.weight_names), ctx ).ignore();
398 auto A_mpi=genEvt->attribute<HepMC3::IntAttribute>(
"mpi");
399 auto A_signal_process_id=genEvt->attribute<HepMC3::IntAttribute>(
"signal_process_id");
400 auto A_event_scale=genEvt->attribute<HepMC3::DoubleAttribute>(
"event_scale");
401 auto A_alphaQCD=genEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQCD");
402 auto A_alphaQED=genEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQED");
403 auto A_filterWeight=genEvt->attribute<HepMC3::DoubleAttribute>(
"filterWeight");
404 auto A_filterHT=genEvt->attribute<HepMC3::DoubleAttribute>(
"filterHT");
405 auto A_filterMET=genEvt->attribute<HepMC3::DoubleAttribute>(
"filterMET");
410 auto event_spv = genEvt->attribute<HepMC3::IntAttribute>(
"signal_process_vertex");
416 auto A_random_states=genEvt->attribute<HepMC3::VectorLongIntAttribute>(
"random_states");
417 auto beams=genEvt->beams();
419 emplace_back(A_signal_process_id?(A_signal_process_id->value()):-1,
420 genEvt->event_number(),
421 A_mpi?(A_mpi->value()):-1,
422 A_event_scale?(A_event_scale->value()):0.0,
423 A_alphaQCD?(A_alphaQCD->value()):0.0,
424 A_alphaQED?(A_alphaQED->value()):0.0,
425 A_filterWeight?(A_filterWeight->value()):1.0,
426 A_filterHT?(A_filterHT->value()):-13.,
427 A_filterMET?(A_filterMET->value()):-13.0,
432 A_random_states?(A_random_states->value()):std::vector<long>(),
433 std::vector<double>(),
434 std::vector<float>(),
435 std::vector<double>(),
436 genEvt->momentum_unit(),
437 genEvt->length_unit(),
439 nPersVtx + genEvt->vertices().size(),
441 nPersParts + genEvt->particles().size() );
444 std::map< std::string, std::map<int, std::shared_ptr<HepMC3::Attribute> > > e_atts = genEvt->attributes();
448 for (
auto& attmap: e_atts) {
449 if (attributes_to_ignore.count(attmap.first))
continue;
450 if (attmap.first ==
"ShadowParticle")
continue;
451 if (attmap.first ==
"ShadowParticleId")
continue;
452 for (
auto& att: attmap.second) {
456 att.second->to_string(
st);
479 if (genEvt->cross_section()) {
480 auto cs=genEvt->cross_section();
500 if (genEvt->heavy_ion()) {
501 auto hi=genEvt->heavy_ion();
503 std::vector<float>& heavyIon = persEvt.
m_heavyIon;
505 heavyIon[12] =
static_cast<float>(hi->Ncoll_hard);
506 heavyIon[11] =
static_cast<float>(hi->Npart_proj);
507 heavyIon[10] =
static_cast<float>(hi->Npart_targ);
508 heavyIon[9] =
static_cast<float>(hi->Ncoll);
509 heavyIon[8] =
static_cast<float>(hi->spectator_neutrons);
510 heavyIon[7] =
static_cast<float>(hi->spectator_protons);
511 heavyIon[6] =
static_cast<float>(hi->N_Nwounded_collisions);
512 heavyIon[5] =
static_cast<float>(hi->Nwounded_N_collisions);
513 heavyIon[4] =
static_cast<float>(hi->Nwounded_Nwounded_collisions);
514 heavyIon[3] = hi->impact_parameter;
515 heavyIon[2] = hi->event_plane_angle;
516 heavyIon[1] = hi->eccentricity;
517 heavyIon[0] = hi->sigma_inel_NN;
521 if (genEvt->pdf_info()) {
522 auto pi=genEvt->pdf_info();
524 std::vector<double>& pdfinfo = persEvt.
m_pdfinfo;
526 pdfinfo[8] =
static_cast<double>(
pi->parton_id[0]);
527 pdfinfo[7] =
static_cast<double>(
pi->parton_id[1]);
528 pdfinfo[6] =
static_cast<double>(
pi->pdf_id[0]);
529 pdfinfo[5] =
static_cast<double>(
pi->pdf_id[1]);
530 pdfinfo[4] =
pi->x[0];
531 pdfinfo[3] =
pi->x[1];
532 pdfinfo[2] =
pi->scale;
533 pdfinfo[1] =
pi->xf[0];
534 pdfinfo[0] =
pi->xf[1];
538 for (
const auto&
v: genEvt->vertices()) {
542 const int signalProcessVtx = genEvt->m_signal_process_vertex
543 ? genEvt->m_signal_process_vertex->barcode()
545 const int beamParticle1Barcode = genEvt->m_beam_particle_1
546 ? genEvt->m_beam_particle_1->barcode()
548 const int beamParticle2Barcode = genEvt->m_beam_particle_2
549 ? genEvt->m_beam_particle_2->barcode()
553 m_hepMCWeightSvc->setWeightNames( genEvt->m_weights.m_names, ctx ).ignore();
557 push_back(
GenEvent_p7( genEvt->m_signal_process_id,
558 genEvt->m_event_number,
560 genEvt->m_event_scale,
565 beamParticle1Barcode,
566 beamParticle2Barcode,
567 genEvt->m_weights.m_weights,
568 genEvt->m_random_states,
569 std::vector<double>(),
570 std::vector<float>(),
571 std::vector<double>(),
572 genEvt->m_momentum_unit,
573 genEvt->m_position_unit,
575 nPersVtx + genEvt->vertices_size(),
577 nPersParts + genEvt->particles_size() ) );
579 if (genEvt->m_cross_section) {
583 crossSection[2] = genEvt->m_cross_section->m_cross_section;
584 crossSection[1] = genEvt->m_cross_section->m_cross_section_error;
585 crossSection[0] =
static_cast<double>(genEvt->m_cross_section->m_is_set);
589 if (genEvt->m_heavy_ion) {
591 std::vector<float>& heavyIon = persEvt.
m_heavyIon;
593 heavyIon[12] =
static_cast<float>(genEvt->m_heavy_ion->m_Ncoll_hard);
594 heavyIon[11] =
static_cast<float>(genEvt->m_heavy_ion->m_Npart_proj);
595 heavyIon[10] =
static_cast<float>(genEvt->m_heavy_ion->m_Npart_targ);
596 heavyIon[9] =
static_cast<float>(genEvt->m_heavy_ion->m_Ncoll);
597 heavyIon[8] =
static_cast<float>(genEvt->m_heavy_ion->m_spectator_neutrons);
598 heavyIon[7] =
static_cast<float>(genEvt->m_heavy_ion->m_spectator_protons);
599 heavyIon[6] =
static_cast<float>(genEvt->m_heavy_ion->m_N_Nwounded_collisions);
600 heavyIon[5] =
static_cast<float>(genEvt->m_heavy_ion->m_Nwounded_N_collisions);
601 heavyIon[4] =
static_cast<float>(genEvt->m_heavy_ion->m_Nwounded_Nwounded_collisions);
602 heavyIon[3] = genEvt->m_heavy_ion->m_impact_parameter;
603 heavyIon[2] = genEvt->m_heavy_ion->m_event_plane_angle;
604 heavyIon[1] = genEvt->m_heavy_ion->m_eccentricity;
605 heavyIon[0] = genEvt->m_heavy_ion->m_sigma_inel_NN;
609 if (genEvt->m_pdf_info) {
611 std::vector<double>& pdfinfo = persEvt.
m_pdfinfo;
613 pdfinfo[8] =
static_cast<double>(genEvt->m_pdf_info->m_id1);
614 pdfinfo[7] =
static_cast<double>(genEvt->m_pdf_info->m_id2);
615 pdfinfo[6] =
static_cast<double>(genEvt->m_pdf_info->m_pdf_id1);
616 pdfinfo[5] =
static_cast<double>(genEvt->m_pdf_info->m_pdf_id2);
617 pdfinfo[4] = genEvt->m_pdf_info->m_x1;
618 pdfinfo[3] = genEvt->m_pdf_info->m_x2;
619 pdfinfo[2] = genEvt->m_pdf_info->m_scalePDF;
620 pdfinfo[1] = genEvt->m_pdf_info->m_pdf1;
621 pdfinfo[0] = genEvt->m_pdf_info->m_pdf2;
625 const HepMC::GenEvent::vertex_const_iterator endVtx=genEvt->vertices_end();
626 for ( HepMC::GenEvent::vertex_const_iterator
i = genEvt->vertices_begin();
654 vtx->set_position(HepMC::FourVector( persVtx.
m_x , persVtx.
m_y , persVtx.
m_z ,persVtx.
m_t ));
656 vtx->set_status(persVtx.
m_id);
659 vtx->add_attribute(
"weights",std::make_shared<HepMC3::VectorDoubleAttribute>(
weights));
663 for (
unsigned int i = 0;
i != nPartsIn; ++
i ) {
669 for (
unsigned int i = 0;
i != nPartsOut; ++
i ) {
673 vtx->m_position.setX( persVtx.
m_x );
674 vtx->m_position.setY( persVtx.
m_y );
675 vtx->m_position.setZ( persVtx.
m_z );
676 vtx->m_position.setT( persVtx.
m_t );
677 vtx->m_particles_in.clear();
678 vtx->m_particles_out.clear();
679 vtx->m_id = persVtx.
m_id;
680 vtx->m_weights.m_weights.reserve( persVtx.
m_weights.size() );
681 vtx->m_weights.m_weights.assign ( persVtx.
m_weights.begin(),
688 for (
unsigned int i = 0;
i != nPartsIn; ++
i ) {
696 for (
unsigned int i = 0;
i != nPartsOut; ++
i ) {
720 p->add_attribute(
"phi",std::make_shared<HepMC3::DoubleAttribute>(persPart.
m_phiPolarization));
721 p->add_attribute(
"theta",std::make_shared<HepMC3::DoubleAttribute>(persPart.
m_thetaPolarization));
731 double temp_e = std::sqrt( (
long double)(persPart.
m_px)*persPart.
m_px +
732 (
long double)(persPart.
m_py)*persPart.
m_py +
733 (
long double)(persPart.
m_pz)*persPart.
m_pz +
734 (
long double)(persPart.
m_m) *persPart.
m_m );
735 p->set_momentum( HepMC::FourVector(persPart.
m_px,persPart.
m_py,persPart.
m_pz,temp_e));
737 const int signM2 = ( persPart.
m_m >= 0. ? 1 : -1 );
738 const double persPart_ene =
739 std::sqrt( std::abs((
long double)(persPart.
m_px)*persPart.
m_px +
740 (
long double)(persPart.
m_py)*persPart.
m_py +
741 (
long double)(persPart.
m_pz)*persPart.
m_pz +
742 signM2* (
long double)(persPart.
m_m)* persPart.
m_m));
743 const int signEne = ( persPart.
m_recoMethod == 1 ? 1 : -1 );
744 p->set_momentum(HepMC::FourVector( persPart.
m_px,
747 signEne * persPart_ene ));
751 std::vector<int> flows;
752 const unsigned int nFlow = persPart.
m_flow.size();
753 for (
unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
754 flows.push_back(persPart.
m_flow[iFlow].second );
757 p->add_attribute(
"flows", std::make_shared<HepMC3::VectorIntAttribute>(flows));
763 p->m_production_vertex = 0;
775 p->m_momentum.setPx( persPart.
m_px);
776 p->m_momentum.setPy( persPart.
m_py);
777 p->m_momentum.setPz( persPart.
m_pz);
778 double temp_e = std::sqrt( (
long double)(persPart.
m_px)*persPart.
m_px +
779 (
long double)(persPart.
m_py)*persPart.
m_py +
780 (
long double)(persPart.
m_pz)*persPart.
m_pz +
781 (
long double)(persPart.
m_m) *persPart.
m_m );
782 p->m_momentum.setE( temp_e);
784 const int signM2 = ( persPart.
m_m >= 0. ? 1 : -1 );
785 const double persPart_ene =
786 std::sqrt( std::abs((
long double)(persPart.
m_px)*persPart.
m_px +
787 (
long double)(persPart.
m_py)*persPart.
m_py +
788 (
long double)(persPart.
m_pz)*persPart.
m_pz +
789 signM2* (
long double)(persPart.
m_m)* persPart.
m_m));
790 const int signEne = ( persPart.
m_recoMethod == 1 ? 1 : -1 );
791 p->m_momentum.set( persPart.
m_px,
794 signEne * persPart_ene );
798 const unsigned int nFlow = persPart.
m_flow.size();
800 for (
unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
801 p->m_flow.set_icode( persPart.
m_flow[iFlow].first,
802 persPart.
m_flow[iFlow].second );
817 const HepMC::FourVector& position = vtx->position();
818 auto A_weights=vtx->attribute<HepMC3::VectorDoubleAttribute>(
"weights");
819 auto A_barcode=vtx->attribute<HepMC3::IntAttribute>(
"barcode");
822 auto weights_d = A_weights->value();
823 for (
auto&
w: weights_d)
weights.push_back(
w);
832 A_barcode?(A_barcode->value()):vtx->id() );
837 for (
const auto&
p: vtx->particles_in()) {
838 if ( !
p->production_vertex() ||
p->production_vertex()->id() == 0 ) {
844 for (
const auto&
p: vtx->particles_out()) {
853 const HepMC::FourVector& position = vtx.m_position;
860 vtx.m_weights.m_weights.begin(),
861 vtx.m_weights.m_weights.end(),
866 const std::vector<HepMC::GenParticlePtr>::const_iterator endInVtx = vtx.m_particles_in.end();
868 for ( std::vector<HepMC::GenParticlePtr>::const_iterator
p = vtx.m_particles_in.begin();
871 if ( 0 == (*p)->production_vertex() ) {
876 const std::vector<HepMC::GenParticlePtr>::const_iterator endOutVtx = vtx.m_particles_out.end();
878 for ( std::vector<HepMC::GenParticlePtr>::const_iterator
p = vtx.m_particles_out.begin();
892 const HepMC::FourVector
mom =
p->momentum();
893 const double ene =
mom.e();
894 const double m2 =
mom.m2();
897 const bool useP2M2 = !(
m2 > 0) &&
899 !(std::abs(
m2) < 2.0*DBL_EPSILON*ene*ene);
900 auto A_flows=
p->attribute<HepMC3::VectorIntAttribute>(
"flows");
901 auto A_phi=
p->attribute<HepMC3::DoubleAttribute>(
"phi");
902 auto A_theta=
p->attribute<HepMC3::DoubleAttribute>(
"theta");
904 const short recoMethod = ( !useP2M2 ? 0: ( ene >= 0.? 1: 2 ) );
906 emplace_back(
mom.px(),
912 A_flows?(A_flows->value().size()):0,
913 A_theta?(A_theta->value()):0.0,
914 A_phi?(A_phi->value()):0.0,
921 std::vector< std::pair<int,int> > flow_hepmc2;
922 if(A_flows) flow_hepmc2=vector_to_vector_int_int(A_flows->value());
923 persEvt.
m_genParticles.back().m_flow.assign( flow_hepmc2.begin(),flow_hepmc2.end() );
934 const HepMC::FourVector&
mom =
p.m_momentum;
935 const double ene =
mom.e();
936 const double m2 =
mom.m2();
939 const bool useP2M2 = !(
m2 > 0) &&
941 !(std::abs(
m2) < 2.0*DBL_EPSILON*ene*ene);
943 const short recoMethod = ( !useP2M2
957 p.m_polarization.theta(),
958 p.m_polarization.phi(),
959 p.m_production_vertex
960 ?
p.m_production_vertex->barcode()
963 ?
p.m_end_vertex->barcode()