12 #include "GaudiKernel/PhysicalConstants.h"
13 using namespace Gaudi::Units;
28 return std::make_pair(separator_hack, event_number);
33 class GenEventSorter {
35 typedef std::map<IndexKey, int> PileUpBackgroundMap;
36 GenEventSorter(
const PileUpBackgroundMap& backgroundClassificationMap) : m_backgroundClassificationMap(backgroundClassificationMap) {}
37 bool operator() (
const HepMC::GenEvent *pGenEvent1,
const HepMC::GenEvent *pGenEvent2) {
40 const IndexKey key1(makekey(signal_process_id1, event_number1,separator_hack1));
41 const IndexKey key2(makekey(signal_process_id2, event_number2,separator_hack2));
42 const PileUpBackgroundMap::const_iterator event1=m_backgroundClassificationMap.find(
key1);
43 const PileUpBackgroundMap::const_iterator event2=m_backgroundClassificationMap.find(
key2);
44 if(event1==m_backgroundClassificationMap.end()) {
45 throw std::runtime_error(
"GenEventSorter::operator() : IndexKey 1 not found in backgroundClassificationMap");
47 if(event2==m_backgroundClassificationMap.end()) {
48 throw std::runtime_error(
"GenEventSorter::operator() : IndexKey 2 not found in backgroundClassificationMap");
52 if(event1->second==event2->second) {
53 if (is_separator(signal_process_id1, event_number1)) {
return true;}
54 if (is_separator(signal_process_id2, event_number2)) {
return false;}
56 if (signal_process_id1==signal_process_id2) {
57 return (event_number1<event_number2);
59 return (signal_process_id1<signal_process_id2);
61 return (event1->second<event2->second);
66 const PileUpBackgroundMap& m_backgroundClassificationMap;
71 const std::string&
name,
72 const IInterface *
parent) :
95 return StatusCode::SUCCESS;
107 <<
"prepareEvent: TimedTruthList with key "
110 return StatusCode::RECOVERABLE;
114 return StatusCode::SUCCESS;
121 ATH_MSG_FATAL (
"processAllSubEvents: Could not find PileUpMergeSvc" );
122 return StatusCode::FAILURE;
129 TimedTruthList truthList;
132 <<
"execute: Can not find TimedTruthList with key "
134 return StatusCode::RECOVERABLE;
144 <<
"execute: TimedTruthList with key "
147 return StatusCode::RECOVERABLE;
153 while (timedTruthListIter != endOfTimedTruthList) {
156 if (!
processEvent(pBackgroundMcEvtColl, currentPileUpTimeEventIndex.
time(), currentPileUpTimeEventIndex.
index(),
static_cast<int>(currentPileUpTimeEventIndex.
type())).isSuccess()) {
158 return StatusCode::FAILURE;
160 ++timedTruthListIter;
176 ATH_MSG_FATAL(
"Signal GenEvent is not first in the list! Something has gone wrong during the GenEvent sorting!");
177 return StatusCode::FAILURE;
182 return StatusCode::SUCCESS;
192 while (iEvt != eSubEvents) {
195 bunchXing, iEvt).isSuccess()){
197 return StatusCode::FAILURE;
200 if (!
processEvent(pMEC,iEvt->time(),iEvt->index(),
static_cast<int>(iEvt->type())).isSuccess()) {
201 ATH_MSG_ERROR (
"processBunchXing: Failed to process McEventCollection." );
202 return StatusCode::FAILURE;
206 return StatusCode::SUCCESS;
213 return StatusCode::FAILURE;
230 ATH_MSG_FATAL(
"Signal GenEvent is not first in the list! Something has gone wrong during the GenEvent sorting!");
231 return StatusCode::FAILURE;
236 return StatusCode::SUCCESS;
240 ATH_MSG_INFO (
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
248 ATH_MSG_INFO (
"GenEvent #"<<event_number<<
", signal_process_id="<<
signal_process_id<<
", category="<<
event->second<<
", number of Vertices="<<(*outputEventItr)->vertices_size() );
251 ATH_MSG_INFO (
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
263 m_pOvrlMcEvColl->at(0)->add_attribute(
"BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(0));
264 m_pOvrlMcEvColl->at(0)->add_attribute(
"PileUpType",std::make_shared<HepMC3::IntAttribute>(0));
268 ATH_MSG_DEBUG(
"execute: copied original event McEventCollection" );
276 ATH_MSG_DEBUG (
"Total Size of Output McEventCollection = " << outCollSize );
289 unsigned int currentMcEventCollectionIndex(1);
293 currentMcEventCollectionIndex += 1;
297 ATH_MSG_DEBUG (
"Placing Separator for Type: "<<
type<<
" at Posistion: " << currentMcEventCollectionIndex-1 );
308 const HepMC::GenEvent* currentBackgroundEvent = *(pMcEvtColl->begin());
310 if (currentBackgroundEvent->particles().size()!=1)
return false;
311 if (currentBackgroundEvent->particles().at(0)->pdg_id()==999)
return true;
313 if (currentBackgroundEvent->particles_size()!=1)
return false;
314 if ((*(currentBackgroundEvent->particles_begin()))->pdg_id()==999)
return true;
325 return StatusCode::SUCCESS;
334 return StatusCode::SUCCESS;
337 ATH_MSG_ERROR (
"Failed to process a Truth Filtered GenEvent.");
341 if (
processUnfilteredEvent(pMcEvtColl, currentEventTime, currentBkgEventIndex, pileupType).isSuccess() ) {
343 return StatusCode::SUCCESS;
351 return StatusCode::FAILURE;
357 if (pMcEvtColl->at(0)->heavy_ion())
361 HepMC::GenHeavyIonPtr hinew=std::make_shared<HepMC::GenHeavyIon>(*(pMcEvtColl->at(0)->heavy_ion()));
364 m_pOvrlMcEvColl->at(0)->set_heavy_ion(*(pMcEvtColl->at(0)->heavy_ion()));
367 return StatusCode::SUCCESS;
372 ATH_MSG_VERBOSE (
"processTruthFilteredEvent(): Event Type: " << pileupType );
378 const int bunchCrossingTime=
static_cast<int>(currentEventTime);
379 currentBackgroundEvent.add_attribute(
"BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime));
380 currentBackgroundEvent.add_attribute(
"PileUpType",std::make_shared<HepMC3::IntAttribute>(pileupType));
383 currentBackgroundEvent.set_event_number(currentBkgEventIndex);
385 if ( std::abs(currentEventTime)<51.0 ) {
386 currentGenEventClassification = ( std::abs(currentEventTime)<1.0 ) ?
INTIME :
OUTOFTIME;
389 currentBackgroundEvent.event_number(),
390 0, currentGenEventClassification,
true);
391 return StatusCode::SUCCESS;
395 ATH_MSG_VERBOSE (
"processUnfilteredEvent() Event Type: " << pileupType );
397 const HepMC::GenEvent& currentBackgroundEvent(**(pMcEvtColl->begin()));
406 const int bunchCrossingTime=
static_cast<int>(currentEventTime);
407 evt->add_attribute(
"BunchCrossingTime",std::make_shared<HepMC3::IntAttribute>(bunchCrossingTime));
408 evt->add_attribute(
"PileUpType",std::make_shared<HepMC3::IntAttribute>(pileupType));
410 evt->set_event_number(currentBkgEventIndex);
411 evt->add_vertex(pCopyOfGenVertex);
417 unsigned int nCollisionVerticesFound(0);
420 auto currentVertexIter = currentBackgroundEvent.vertices().begin();
421 auto endOfCurrentListOfVertices = currentBackgroundEvent.vertices().end();
423 if (
HepMC::signal_process_vertex(¤tBackgroundEvent) ) pCopyOfGenVertex =
new HepMC::GenVertex ( *currentBackgroundEvent.signal_process_vertex() );
428 unsigned int nCollisionVerticesFound(0);
431 auto currentVertexIter = currentBackgroundEvent.vertices_begin();
432 auto endOfCurrentListOfVertices = currentBackgroundEvent.vertices_end();
434 for (; currentVertexIter != endOfCurrentListOfVertices; ++currentVertexIter) {
435 const auto& pCurrentVertex=*currentVertexIter;
440 bool isCollisionVertex(
false);
443 if(isCollisionVertex) ++nCollisionVerticesFound;
449 ATH_MSG_VERBOSE(
"Found a particle at location " << std::hex << currentVertexParticle << std::dec <<
" with PDG ID = " << currentVertexParticle->pdg_id() );
451 puType particleClassification(
classifyVertex(currentVertexParticle, pCurrentParticleProductionVertex,currentEventTime));
453 if(isCollisionVertex &&
NOPUTYPE==particleClassification) {
454 particleClassification=
INTIME;
458 if (!pCopyOfVertexForClassification[particleClassification]) {
459 pCopyOfVertexForClassification[particleClassification] =
HepMC::newGenVertexPtr(pCurrentVertex->position());
460 ATH_MSG_VERBOSE(
"Added bkg vertex " << pCopyOfVertexForClassification[particleClassification] <<
" at position " << pCopyOfVertexForClassification[particleClassification] <<
" for pu type = " << particleClassification );
463 pCopyOfVertexForClassification[particleClassification]->add_particle_out(std::make_shared<HepMC3::GenParticle>(currentVertexParticle->data()));
465 pCopyOfVertexForClassification[particleClassification]->add_particle_out(
new HepMC::GenParticle(*currentVertexParticle) );
467 ATH_MSG_VERBOSE(
"Added bkg particle at location " << std::hex << currentVertexParticle << std::dec <<
" with PDG ID = " << currentVertexParticle->pdg_id() );
473 for (
const auto& currentVertexParticle: pCurrentVertex->particles_in()) {
474 pCopyOfVertexForClassification[
INTIME]->add_particle_in (std::make_shared<HepMC3::GenParticle>(currentVertexParticle->data()));
477 HepMC::GenVertex::particles_in_const_iterator currentVertexParticleIter(pCurrentVertex->particles_in_const_begin());
478 const HepMC::GenVertex::particles_in_const_iterator endOfListOfParticlesFromCurrentVertex(pCurrentVertex->particles_in_const_end());
479 for (; currentVertexParticleIter != endOfListOfParticlesFromCurrentVertex; ++currentVertexParticleIter) {
486 if (!pCopyOfVertexForClassification[
type])
continue;
487 int n_particles_out=pCopyOfVertexForClassification[
type]->particles_out_size();
494 return StatusCode::SUCCESS;
500 for (
const auto& pCurrentVertexParticle: pCurrentVertex->particles_in()) {
502 auto currentVertexParticleIter = pCurrentVertex->particles_in_const_begin();
503 auto endOfListOfParticlesFromCurrentVertex = pCurrentVertex->particles_in_const_end();
504 for( ;currentVertexParticleIter != endOfListOfParticlesFromCurrentVertex; ++currentVertexParticleIter ) {
505 auto pCurrentVertexParticle = *currentVertexParticleIter;
507 if (
MC::isBeam(pCurrentVertexParticle))
return true;
517 if ( pCurrentParticleProductionVertex ) {
521 if ( std::abs(pCurrentParticleProductionVertex->position().z()) <
m_zRange ) {
522 const float xi((pCurrentParticleProductionVertex->position()).x());
523 const float yi((pCurrentParticleProductionVertex->position()).y());
525 const double eta(pCurrentVertexParticle->momentum().pseudoRapidity());
528 if ( currentEventIsInTime ) {
530 if ( 0.0==currentEventTime ) {
531 particleClassification=
INTIME;
548 double mass(pCurrentVertexParticle->momentum().m());
550 const double kineticEnergy(pCurrentVertexParticle->momentum().e() -
mass);
552 particleClassification=
CAVERN;
557 return particleClassification;
569 ++currentClassification;
572 if((*outputEventItr)->vertices_empty()) {
575 ATH_MSG_VERBOSE(
"compressOutputMcEventCollection() Removed Empty GenEvent #" << event_number <<
", signal_process_id(" <<
signal_process_id <<
"), category = " << currentClassification);
581 return StatusCode::SUCCESS;
587 ATH_MSG_ERROR(
"updateClassidificationMap: GenEvent #" << event_number <<
", signal_process_id(" <<
signal_process_id <<
"), category = " << classification );
592 ATH_MSG_ERROR(
"updateClassidificationMap: GenEvent #" << event_number <<
", signal_process_id(" <<
signal_process_id <<
"), category = " << classification );
593 ATH_MSG_FATAL (
"Should only ever be one signal event in the background classification map! Bailing out.");
599 key=makekey(separator_hack,event_number);
601 ATH_MSG_VERBOSE(
"updateClassidificationMap: GenEvent #" << event_number <<
", signal_process_id(" <<
signal_process_id <<
"), category = " << classification <<
", key = " <<
key);
604 if(firstUpdateForThisEvent) {
605 ATH_MSG_ERROR(
"updateClassidificationMap: Repeated KEY! "<<
key <<
". Previous category = " <<
event->second );
608 ATH_MSG_DEBUG(
"updateClassidificationMap: Updating category for existing key "<<
key <<
". Previous category = " <<
event->second <<
", new category = " << classification );
611 if(
event->second<=classification)
return;